1#ifndef FLASH2M
2/*
3 * udhcpc scripts
4 *
5 * Copyright 2004, Broadcom Corporation
6 * All Rights Reserved.
7 *
8 * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
9 * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
10 * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
11 * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
12 *
13 * $Id: udhcpc_ex.c,v 1.1.1.1 2008/10/15 03:28:48 james26_jang Exp $
14 */
15
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <sys/socket.h>
20#include <linux/if.h>
21#include <linux/route.h>
22#include <netinet/in.h>
23#include <arpa/inet.h>
24#include <errno.h>
25#include <time.h>
26#include <unistd.h>
27
28#include <bcmnvram.h>
29#include <shutils.h>
30#include <rc.h>
31
32static char udhcpstate[8];
33
34static int
35expires(char *lan_ifname, unsigned int in)
36{
37	time_t now;
38	FILE *fp;
39	char tmp[100];
40
41	time(&now);
42	snprintf(tmp, sizeof(tmp), "/tmp/udhcpc%d.expires", 0);
43	if (!(fp = fopen(tmp, "w"))) {
44		perror(tmp);
45		return errno;
46	}
47	fprintf(fp, "%d", (unsigned int) now + in);
48	fclose(fp);
49	return 0;
50}
51
52/*
53 * deconfig: This argument is used when udhcpc starts, and when a
54 * leases is lost. The script should put the interface in an up, but
55 * deconfigured state.
56*/
57static int
58deconfig(void)
59{
60	char *lan_ifname = safe_getenv("interface");
61
62	ifconfig(lan_ifname, IFUP,
63		 nvram_safe_get("lan_ipaddr"),
64		 nvram_safe_get("lan_netmask"));
65
66	expires(lan_ifname, 0);
67
68	lan_down_ex(lan_ifname);
69
70	logmessage("dhcp client", "%s: lease is lost", udhcpstate);
71	//wanmessage("lost IP from server");
72
73	dprintf("done\n");
74	return 0;
75}
76
77/*
78 * bound: This argument is used when udhcpc moves from an unbound, to
79 * a bound state. All of the paramaters are set in enviromental
80 * variables, The script should configure the interface, and set any
81 * other relavent parameters (default gateway, dns server, etc).
82*/
83static int
84bound(void)
85{
86	char *lan_ifname = safe_getenv("interface");
87	char *value, *value1;
88	char tmp[100], prefix[] = "lanXXXXXXXXXX_";
89	struct in_addr fixed_addr;
90
91	snprintf(prefix, sizeof(prefix), "lan_");
92	if ((value = getenv("ip")))
93	{
94		inet_aton(value, &fixed_addr);
95		value = inet_ntoa(fixed_addr);
96		nvram_set(strcat_r(prefix, "ipaddr_t", tmp), value);
97	}
98	if ((value = getenv("subnet")))
99		nvram_set(strcat_r(prefix, "netmask_t", tmp), value);
100	if ((value = getenv("router")))
101		nvram_set(strcat_r(prefix, "gateway_t", tmp), value);
102	if ((value = getenv("dns")))
103		nvram_set(strcat_r(prefix, "dns_t", tmp), value);
104	if ((value = getenv("wins")))
105		nvram_set(strcat_r(prefix, "wins_t", tmp), value);
106//	if ((value = getenv("hostname")))
107//		sethostname(value, strlen(value) + 1);
108	if ((value = getenv("domain")))
109		nvram_set(strcat_r(prefix, "domain_t", tmp), value);
110	if ((value = getenv("lease"))) {
111		nvram_set(strcat_r(prefix, "lease_t", tmp), value);
112		expires(lan_ifname, atoi(value));
113	}
114
115	ifconfig(lan_ifname, IFUP,
116		 nvram_safe_get(strcat_r(prefix, "ipaddr_t", tmp)),
117		 nvram_safe_get(strcat_r(prefix, "netmask_t", tmp)));
118
119	lan_up_ex(lan_ifname);
120
121	logmessage("dhcp client", "%s IP : %s from %s",
122		udhcpstate,
123		nvram_safe_get(strcat_r(prefix, "ipaddr_t", tmp)),
124		nvram_safe_get(strcat_r(prefix, "gateway_t", tmp)));
125
126	//wanmessage("");
127
128// 2009.09 James. {
129	char *wsc_argv[] = {"/bin/wsccmd", NULL};
130	pid_t pid;
131
132	nvram_set("wan_ready", "1");
133
134	if(!strcmp(nvram_safe_get("wan_route_x"), "IP_Bridged")
135			&& !strcmp(nvram_safe_get("system_ready"), "1")
136			&& nvram_invmatch("wl_auth_mode", "shared")
137			&& nvram_invmatch("wl_auth_mode", "wpa")
138			&& nvram_invmatch("wl_auth_mode", "wpa2")
139			&& nvram_invmatch("wl_auth_mode", "radius")
140			){
141csprintf("+++ Restart WSC from udhcpc. +++\n");
142		eval("killall", "wsccmd");
143		eval("killall", "nas");
144		//sleep(1);
145		eval("ifconfig", "br0", "down");
146		//sleep(1);
147		eval("ifconfig", "br0", "up");
148		//sleep(1);
149		_eval(wsc_argv, NULL, 0, &pid);
150
151		// add default route, because br0 had reset.
152		char gateway[16];
153		memset(gateway, 0, 16);
154		strcpy(gateway, nvram_safe_get("lan_gateway_t"));
155csprintf("+++ udhcpc_ex: Default to %s in br0. +++\n", gateway);
156		route_add("br0", 0, "0.0.0.0", gateway, "0.0.0.0");
157	}
158// 2009.09 James. }
159	dprintf("done\n");
160	return 0;
161}
162
163/*
164 * renew: This argument is used when a DHCP lease is renewed. All of
165 * the paramaters are set in enviromental variables. This argument is
166 * used when the interface is already configured, so the IP address,
167 * will not change, however, the other DHCP paramaters, such as the
168 * default gateway, subnet mask, and dns server may change.
169 */
170static int
171renew(void)
172{
173	bound();
174
175	dprintf("done\n");
176	return 0;
177}
178
179int
180udhcpc_ex_main(int argc, char **argv)
181{
182	if (argv[1]) strcpy(udhcpstate, argv[1]);
183
184	if (!argv[1])
185		return EINVAL;
186	else if (strstr(argv[1], "deconfig"))
187		return deconfig();
188	else if (strstr(argv[1], "bound"))
189		return bound();
190	else if (strstr(argv[1], "renew"))
191		return renew();
192	else if (strstr(argv[1], "leasefail"))
193		return 0;
194	else return deconfig();
195}
196#endif
197