1/*
2 * Network services
3 *
4 * Copyright 2004, Broadcom Corporation
5 * All Rights Reserved.
6 *
7 * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
8 * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
9 * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
10 * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
11 *
12 * $Id: network.c,v 1.7 2009/03/11 14:29:09 james26_jang Exp $
13 */
14
15#include <stdio.h>
16#include <stdlib.h>
17#include <errno.h>
18#include <syslog.h>
19#include <ctype.h>
20#include <string.h>
21#include <unistd.h>
22#include <sys/stat.h>
23#include <sys/ioctl.h>
24#include <sys/types.h>
25#include <sys/socket.h>
26#include <net/if.h>
27#include <netinet/in.h>
28#include <arpa/inet.h>
29#include <net/if_arp.h>
30#include <signal.h>
31typedef u_int64_t u64;
32typedef u_int32_t u32;
33typedef u_int16_t u16;
34typedef u_int8_t u8;
35#include <linux/sockios.h>
36#include <linux/ethtool.h>
37#include <bcmnvram.h>
38#include <netconf.h>
39#include <shutils.h>
40#include <wlutils.h>
41#include <nvparse.h>
42#include <rc.h>
43#include <bcmutils.h>
44#include <etioctl.h>
45#include <bcmparams.h>
46
47extern int qos_get_wan_rate(void);
48void lan_up(char *);
49
50static int
51add_routes(char *prefix, char *var, char *ifname)
52{
53	char word[80], *next;
54	char *ipaddr, *netmask, *gateway, *metric;
55	char tmp[100];
56
57	foreach(word, nvram_safe_get(strcat_r(prefix, var, tmp)), next) {
58
59		netmask = word;
60		ipaddr = strsep(&netmask, ":");
61		if (!ipaddr || !netmask)
62			continue;
63		gateway = netmask;
64		netmask = strsep(&gateway, ":");
65		if (!netmask || !gateway)
66			continue;
67		metric = gateway;
68		gateway = strsep(&metric, ":");
69		if (!gateway || !metric)
70			continue;
71		if (inet_addr_(gateway) == INADDR_ANY)
72	                gateway = nvram_safe_get("wanx_gateway");
73
74
75		dprintf("\n\n\nadd %s %d %s %s %s\n\n\n", ifname, atoi(metric), ipaddr, gateway, netmask);
76
77		route_add(ifname, atoi(metric) + 1, ipaddr, gateway, netmask);
78	}
79
80	return 0;
81}
82
83void add_wanx_routes(char *prefix, char *ifname, int metric)
84{
85        char *routes, *msroutes, *tmp;
86        char buf[30];
87
88        char ipaddr[] = "255.255.255.255";
89        char gateway[] = "255.255.255.255";
90        char netmask[] = "255.255.255.255";
91
92        if (!nvram_match("dr_enable_x", "1"))
93                return;
94
95        /* routes */
96        routes = strdup(nvram_safe_get(strcat_r(prefix, "routes", buf)));
97        for (tmp = routes; tmp && *tmp; )
98        {
99                char *ipaddr = strsep(&tmp, " ");
100                char *gateway = strsep(&tmp, " ");
101                if (gateway) {
102                        route_add(ifname, metric + 1, ipaddr, gateway, netmask);
103                }
104        }
105        free(routes);
106
107        /* ms routes */
108        for (msroutes = nvram_get(strcat_r(prefix, "msroutes", buf)); msroutes && isdigit(*msroutes); )
109        {
110                /* read net length */
111                int bit, bits = strtol(msroutes, &msroutes, 10);
112                struct in_addr ip, gw, mask;
113
114                if (bits < 1 || bits > 32 || *msroutes != ' ')
115                        break;
116
117                mask.s_addr = htonl(0xffffffff << (32 - bits));
118
119                /* read network address */
120                for (ip.s_addr = 0, bit = 24; bit > (24 - bits); bit -= 8)
121                {
122                        if (*msroutes++ != ' ' || !isdigit(*msroutes))
123                                goto bad_data;
124
125                        ip.s_addr |= htonl(strtol(msroutes, &msroutes, 10) << bit);
126                }
127
128                /* read gateway */
129                for (gw.s_addr = 0, bit = 24; bit >= 0 && *msroutes; bit -= 8)
130                {
131                        if (*msroutes++ != ' ' || !isdigit(*msroutes))
132                                goto bad_data;
133
134                        gw.s_addr |= htonl(strtol(msroutes, &msroutes, 10) << bit);
135                }
136
137                /* clear bits per RFC */
138                ip.s_addr &= mask.s_addr;
139
140                strcpy(ipaddr, inet_ntoa(ip));
141                strcpy(gateway, inet_ntoa(gw));
142                strcpy(netmask, inet_ntoa(mask));
143
144                route_add(ifname, metric + 1, ipaddr, gateway, netmask);
145
146                if (*msroutes == ' ')
147                        msroutes++;
148        }
149bad_data:
150        return;
151}
152
153static int
154del_routes(char *prefix, char *var, char *ifname)
155{
156	char word[80], *next;
157	char *ipaddr, *netmask, *gateway, *metric;
158	char tmp[100];
159
160	foreach(word, nvram_safe_get(strcat_r(prefix, var, tmp)), next) {
161		dprintf("add %s\n", word);
162
163		netmask = word;
164		ipaddr = strsep(&netmask, ":");
165		if (!ipaddr || !netmask)
166			continue;
167		gateway = netmask;
168		netmask = strsep(&gateway, ":");
169		if (!netmask || !gateway)
170			continue;
171		metric = gateway;
172		gateway = strsep(&metric, ":");
173		if (!gateway || !metric)
174			continue;
175
176                if (inet_addr_(gateway) == INADDR_ANY)
177                        gateway = nvram_safe_get("wanx_gateway");
178
179		dprintf("add %s\n", ifname);
180
181		route_del(ifname, atoi(metric) + 1, ipaddr, gateway, netmask);
182	}
183
184	return 0;
185}
186
187static int
188add_lan_routes(char *lan_ifname)
189{
190	return add_routes("lan_", "route", lan_ifname);
191}
192
193static int
194del_lan_routes(char *lan_ifname)
195{
196	return del_routes("lan_", "route", lan_ifname);
197}
198
199static void
200start_igmpproxy(char *wan_ifname)
201{
202	static char *igmpproxy_conf = "/tmp/igmpproxy.conf";
203	struct stat     st_buf;
204	FILE            *fp;
205
206	if (!nvram_match("mr_enable_x", "1"))
207		return;
208
209	if ((fp = fopen(igmpproxy_conf, "w")) == NULL) {
210		perror(igmpproxy_conf);
211		return;
212	}
213
214	fprintf(fp, "# automagically generated from web settings\n"
215			"quickleave\n\n"
216			"phyint %s upstream\n"
217			"\taltnet %s\n\n"
218			"phyint %s downstream\n\n",
219			wan_ifname,
220			nvram_get("mr_altnet_x") ? : "0.0.0.0/0",
221			nvram_get("lan_ifname") ? : "br0");
222
223	fclose(fp);
224
225	eval("/usr/sbin/igmpproxy", "-c", igmpproxy_conf);
226}
227
228void
229start_lan(void)
230{
231	char *lan_ifname = nvram_safe_get("lan_ifname");
232//	char br0_ifnames[255];
233	char name[80], *next;
234//	char tmpstr[48];
235//	int i, j;
236	int s;
237	struct ifreq ifr;
238
239	dprintf("%s\n", lan_ifname);
240
241#ifdef GUEST_ACCOUNT
242	memset(br0_ifnames,0,sizeof(br0_ifnames));
243
244	nvram_unset("unbridged_ifnames");
245	nvram_unset("br0_ifnames");
246
247	/* If we're a travel router... then we need to make sure we get
248		 the primary wireless interface up before trying to attach slave
249		 interface(s) to the bridge */
250#endif
251
252#ifdef URE
253	if(nvram_match("ure_disable", "0"))
254	{
255		eval("wlconf", nvram_get("wan0_ifname"), "up");
256	}
257#endif
258
259 	/* Bring up bridged interfaces */
260	if (strncmp(lan_ifname, "br", 2) == 0) {
261		eval("brctl", "addbr", lan_ifname);
262		eval("brctl", "setfd", lan_ifname, "0");
263		if (nvram_match("router_disable", "1") || nvram_match("lan_stp", "0"))
264			eval("brctl", "stp", lan_ifname, "dis");
265#ifdef ASUS_EXT
266		foreach(name, nvram_safe_get("lan_ifnames_t"), next) {
267#else
268		foreach(name, nvram_safe_get("lan_ifnames"), next) {
269#endif
270			/* Bring up interface */
271			ifconfig(name, IFUP, NULL, NULL);
272			/* Set the logical bridge address to that of the first interface */
273			if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0)
274				continue;
275			strncpy(ifr.ifr_name, lan_ifname, IFNAMSIZ);
276			if (ioctl(s, SIOCGIFHWADDR, &ifr) == 0 &&
277			    memcmp(ifr.ifr_hwaddr.sa_data, "\0\0\0\0\0\0", ETHER_ADDR_LEN) == 0) {
278				strncpy(ifr.ifr_name, name, IFNAMSIZ);
279				if (ioctl(s, SIOCGIFHWADDR, &ifr) == 0) {
280					strncpy(ifr.ifr_name, lan_ifname, IFNAMSIZ);
281					ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
282					ioctl(s, SIOCSIFHWADDR, &ifr);
283				}
284			}
285			close(s);
286			/* If not a wl i/f then simply add it to the bridge */
287			if (eval("wlconf", name, "up"))
288			{
289#ifdef RT2400_SUPPORT
290				if (strcmp(name, "eth2")==0)
291				{
292					// added by Joey for WL500b + WL127
293					if (nvram_match("wl_channel", "0"))
294						nvram_set("wl_channel", "6");
295
296					sprintf(tmpstr, "mac_address=%s", nvram_safe_get("et0macaddr"));
297					eval("insmod","rt2400.o",tmpstr);
298					eval("brctl","addif",lan_ifname,"ra0");
299					ifconfig("ra0",IFUP,NULL,NULL);
300					nvram_set("nobr","1");
301
302					j = atoi(nvram_safe_get("wl_wdsnum_x"));
303					for(i=1;i<=j;i++)
304					{
305						sprintf(tmpstr, "ra%d", i);							ifconfig(tmpstr, IFUP, NULL, NULL);
306						eval("brctl","addif",lan_ifname,tmpstr);
307					}
308				}
309				else
310#endif
311
312#ifdef GUEST_ACCOUNT
313				{
314					if (eval("brctl", "addif", lan_ifname, name))
315						perror("brctl");
316					else{
317						char buf[255],*ptr;
318						ptr = nvram_get("br0_ifnames");
319						if (ptr)
320							snprintf(buf,sizeof(buf),"%s %s",ptr,name);
321						else
322							strncpy(buf,name,sizeof(buf));
323						nvram_set("br0_ifnames",buf);
324					}
325				}
326#else
327				eval("brctl", "addif", lan_ifname, name);
328#endif
329
330			}
331			else
332			{
333#ifdef GUEST_ACCOUNT
334				char wl_guest[] = "wlXXXXXXXXXX_guest";
335				char wl_vifs[]= "wlXXXXXXXXXX_vifs";
336				char mode[] = "wlXXXXXXXXXX_mode";
337				char *vifs;
338#endif
339
340				/* get the instance number of the wl i/f */
341				char wl_name[] = "wlXXXXXXXXXX_mode";
342				int unit;
343#ifdef ASUS_EXT
344				sync_mac(name, nvram_safe_get("et0macaddr"));
345#endif
346				wl_ioctl(name, WLC_GET_INSTANCE, &unit, sizeof(unit));
347				snprintf(wl_name, sizeof(wl_name), "wl%d_mode", unit);
348
349#ifdef GUEST_ACCOUNT
350				snprintf(wl_guest, sizeof(wl_guest), "wl%d_guest", unit);
351				snprintf(wl_vifs, sizeof(wl_vifs), "wl%d_vifs", unit);
352
353				/* Multi-SSID specific configuration */
354				/* Virtual interfaces are created with  the master interface
355				   by wlconf() . Only copy those that have wlX.Y_guest set*/
356
357				vifs = nvram_get(wl_vifs);
358
359				if (vifs){
360					char buf[255];
361					char name[32];
362					char vif_guest[32];
363					char *ptr=NULL,*next=NULL;
364
365					memset(buf,0,sizeof(buf));
366
367					ptr = nvram_get("unbridged_ifnames");
368
369					if (ptr) snprintf(buf,sizeof(buf),"%s",ptr);
370
371					/*Loop thru wlX_vifs to get the virtual interfaces
372				          wlX.Y_guest must be set for IP to configure it*/
373					foreach(name,vifs,next){
374						snprintf(vif_guest,sizeof(vif_guest),"%s_guest",name);
375						cprintf("vif_guest=%s\n", vif_guest);
376						if (nvram_match(vif_guest,"1")){
377							if(*buf)
378								snprintf(buf,sizeof(buf),"%s %s",buf,name);
379							else
380								strncpy(buf,name,sizeof(buf));
381						}
382					}
383
384					if (*buf) nvram_set("unbridged_ifnames",buf);
385				}
386#endif
387				/* Receive all multicast frames in WET mode */
388				if (nvram_match(wl_name, "wet"))
389				{
390					ifconfig(name, IFUP | IFF_ALLMULTI, NULL, NULL);
391#ifdef GUEST_ACCOUNT
392					/* Enable host DHCP relay */
393					if (nvram_match("lan_dhcp", "1"))
394						wl_iovar_set(name, "wet_host_mac", ifr.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
395#endif
396				}
397
398				/* Do not attach the main wl i/f if in wds mode */
399#ifdef GUEST_ACCOUNT
400				if ( !nvram_match(mode, "wds") && !nvram_match(wl_guest, "1")){
401					char buf[255],*ptr;
402
403					eval("brctl", "addif", lan_ifname, name);
404
405					ptr = nvram_get("br0_ifnames");
406					if (ptr)
407						snprintf(buf,sizeof(buf),"%s %s",ptr,name);
408					else
409						strncpy(buf,name,sizeof(buf));
410					nvram_set("br0_ifnames",buf);
411				}
412#else
413				if (nvram_invmatch(wl_name, "wds"))
414					eval("brctl", "addif",lan_ifname,name);
415#endif
416			}
417		}
418	}
419	/* specific non-bridged lan i/f */
420	else if (strcmp(lan_ifname, "")) {
421		/* Bring up interface */
422		ifconfig(lan_ifname, IFUP, NULL, NULL);
423		/* config wireless i/f */
424		if (!eval("wlconf", lan_ifname, "up")) {
425			char tmp[100], prefix[] = "wanXXXXXXXXXX_";
426			int unit;
427			/* get the instance number of the wl i/f */
428			wl_ioctl(lan_ifname, WLC_GET_INSTANCE, &unit, sizeof(unit));
429			snprintf(prefix, sizeof(prefix), "wl%d_", unit);
430			/* Receive all multicast frames in WET mode */
431			if (nvram_match(strcat_r(prefix, "mode", tmp), "wet"))
432				ifconfig(lan_ifname, IFUP | IFF_ALLMULTI, NULL, NULL);
433		}
434	}
435#ifdef GUEST_ACCOUNT
436	/* build unbridged ifnames NVRAM var from wl_guest list */
437	foreach(name, nvram_safe_get("lan_ifnames"), next) {
438
439		char wl_guest[] = "wlXXXXXXXXXX_guest";
440		int unit;
441
442		if (!eval("wlconf", name, "up")) {
443			wl_ioctl(name, WLC_GET_INSTANCE, &unit, sizeof(unit));
444			snprintf(wl_guest, sizeof(wl_guest), "wl%d_guest", unit);
445
446			if (nvram_match(wl_guest,"1")){
447				char buf[255],*ptr;
448
449				ptr = nvram_get("unbridged_ifnames");
450				if (ptr)
451					snprintf(buf,sizeof(buf),"%s %s",ptr,name);
452				else
453					strncpy(buf,name,sizeof(buf));
454
455				nvram_set("unbridged_ifnames",buf);
456			}
457		}
458	}
459#endif
460	/* Get current LAN hardware address */
461	if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) >= 0) {
462		char eabuf[32];
463		strncpy(ifr.ifr_name, lan_ifname, IFNAMSIZ);
464		if (ioctl(s, SIOCGIFHWADDR, &ifr) == 0)
465			nvram_set("lan_hwaddr", ether_etoa(ifr.ifr_hwaddr.sa_data, eabuf));
466		close(s);
467	}
468
469#ifdef WPA2_WMM
470	/* Set QoS mode */
471	if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) >= 0) {
472		int i, qos;
473		caddr_t ifrdata;
474		struct ethtool_drvinfo info;
475
476		qos = (strcmp(nvram_safe_get("wl_wme"), "on")) ? 0 : 1;
477		for (i = 1; i <= DEV_NUMIFS; i ++) {
478			ifr.ifr_ifindex = i;
479			if (ioctl(s, SIOCGIFNAME, &ifr))
480				continue;
481			if (ioctl(s, SIOCGIFHWADDR, &ifr))
482				continue;
483			if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER)
484				continue;
485			/* get flags */
486			if (ioctl(s, SIOCGIFFLAGS, &ifr))
487				continue;
488			/* if up(wan not up yet at this point) */
489			if (ifr.ifr_flags & IFF_UP) {
490				ifrdata = ifr.ifr_data;
491				memset(&info, 0, sizeof(info));
492				info.cmd = ETHTOOL_GDRVINFO;
493				ifr.ifr_data = (caddr_t)&info;
494				if (ioctl(s, SIOCETHTOOL, &ifr) >= 0) {
495					/* currently only need to set QoS to et devices */
496					if (!strncmp(info.driver, "et", 2)) {
497						ifr.ifr_data = (caddr_t)&qos;
498						ioctl(s, SIOCSETCQOS, &ifr);
499					}
500				}
501				ifr.ifr_data = ifrdata;
502			}
503		}
504	}
505	close(s);
506#endif
507
508#ifdef ASUS_EXT
509#ifndef FLASH2M
510	/*
511	* Configure DHCP connection. The DHCP client will run
512	* 'udhcpc bound'/'udhcpc deconfig' upon finishing IP address
513	* renew and release.
514	*/
515	if (nvram_match("router_disable", "1"))
516	{
517		if (nvram_match("lan_proto_x", "1"))
518		{
519			char *dhcp_argv[] = { "udhcpc",
520					      "-i", lan_ifname,
521					      "-p", "/var/run/udhcpc_lan.pid",
522					      "-s", "/tmp/landhcpc",
523					      NULL
524			};
525			pid_t pid;
526
527
528			/* Bring up and configure LAN interface */
529			ifconfig(lan_ifname, IFUP,
530		 		nvram_safe_get("lan_ipaddr"), nvram_safe_get("lan_netmask"));
531
532			symlink("/sbin/rc", "/tmp/landhcpc");
533
534			/* Start dhcp daemon */
535			_eval(dhcp_argv, NULL, 0, &pid);
536		}
537		else
538		{
539			/* Bring up and configure LAN interface */
540			ifconfig(lan_ifname, IFUP,
541		 		nvram_safe_get("lan_ipaddr"), nvram_safe_get("lan_netmask"));
542			lan_up(lan_ifname);
543
544			update_lan_status(1);
545		}
546	}
547	else
548#endif // end of FLASH2M
549	{
550		/* Bring up and configure LAN interface */
551		ifconfig(lan_ifname, IFUP,
552		 	nvram_safe_get("lan_ipaddr"), nvram_safe_get("lan_netmask"));
553		/* Install lan specific static routes */
554		add_lan_routes(lan_ifname);
555
556		update_lan_status(1);
557	}
558#else
559	/* Bring up and configure LAN interface */
560	ifconfig(lan_ifname, IFUP,
561		 nvram_safe_get("lan_ipaddr"), nvram_safe_get("lan_netmask"));
562
563	/* Install lan specific static routes */
564	add_lan_routes(lan_ifname);
565#endif
566
567#ifdef GUEST_ACCOUNT
568	/* Bring up unbridged LAN interfaces (if they exist)*/
569	{
570	char *interfaces,*ifname, *ptr;
571	char word[64], *next;
572	int index =1 ;
573
574	interfaces = nvram_get("unbridged_ifnames");
575	if (interfaces)
576		foreach(word,interfaces,next){
577
578		 	char interface[32], mask[32];
579		 	int s;
580
581		 	ptr=word;
582			ifname = word;
583			index =  get_ipconfig_index(ifname);
584
585			if ( index < 0) {
586				cprintf("Cannot find index for interface:%s\n",ifname);
587				continue;
588			}
589
590			snprintf(interface,sizeof(interface),"lan%d_ipaddr",index);
591		 	snprintf(mask,sizeof(mask),"lan%d_netmask",index);
592		 	ifconfig(ifname, IFUP,nvram_safe_get(interface), nvram_safe_get(mask));
593
594		 	/* Get Ethernet hardware address. Note this value NOT is committed to NVRAM */
595		 	if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) >= 0) {
596		 		struct ifreq ifr;
597				char macaddr[]="00:00:00:00:00:00";
598				char mac[32];
599
600				memset(&ifr,0,sizeof(ifr));
601				strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
602				if (ioctl(s, SIOCGIFHWADDR, &ifr) == 0){
603					ether_etoa(ifr.ifr_hwaddr.sa_data, macaddr);
604					snprintf(mac,sizeof(mac),"lan%d_hwaddr",index);
605					nvram_set(mac,macaddr);
606				}
607				close(s);
608			}
609
610		}
611	}
612#endif
613
614#ifndef ASUS_EXT
615	/* Start syslogd if either log_ipaddr or log_ram_enable is set */
616	if (nvram_invmatch("log_ipaddr", "") || nvram_match("log_ram_enable", "1")) {
617		char *argv[] = {
618			"syslogd",
619			NULL, 		/* -C */
620			NULL, NULL,	/* -R host */
621			NULL
622		};
623		int pid;
624		int argc = 1;
625
626		if (nvram_match("log_ram_enable", "1")) {
627			argv[argc++] = "-C";
628		}
629		else if (!nvram_match("log_ram_enable", "0")) {
630			nvram_set("log_ram_enable", "0");
631		}
632
633		if (nvram_invmatch("log_ipaddr", "")) {
634			argv[argc++] = "-R";
635			argv[argc++] = nvram_get("log_ipaddr");
636		}
637
638		_eval(argv, NULL, 0, &pid);
639	}
640#endif
641
642	dprintf("%s %s\n",
643		nvram_safe_get("lan_ipaddr"),
644		nvram_safe_get("lan_netmask"));
645}
646
647void
648stop_lan(void)
649{
650	char *lan_ifname = nvram_safe_get("lan_ifname");
651	char name[80], *next;
652
653	dprintf("%s\n", lan_ifname);
654
655	/* Stop the syslogd daemon */
656	eval("killall", "syslogd");
657
658	/* Remove static routes */
659	del_lan_routes(lan_ifname);
660
661	/* Bring down LAN interface */
662	ifconfig(lan_ifname, 0, NULL, NULL);
663
664	/* Bring down bridged interfaces */
665	if (strncmp(lan_ifname, "br", 2) == 0) {
666#ifdef ASUS_EXT
667		foreach(name, nvram_safe_get("lan_ifnames_t"), next) {
668#else
669		foreach(name, nvram_safe_get("lan_ifnames"), next) {
670#endif
671			eval("wlconf", name, "down");
672			ifconfig(name, 0, NULL, NULL);
673			eval("brctl", "delif", lan_ifname, name);
674		}
675		eval("brctl", "delbr", lan_ifname);
676	}
677	/* Bring down specific interface */
678	else if (strcmp(lan_ifname, ""))
679		eval("wlconf", lan_ifname, "down");
680
681	dprintf("done\n");
682}
683
684static int
685wan_prefix(char *ifname, char *prefix)
686{
687	int unit;
688
689	if ((unit = wan_ifunit(ifname)) < 0)
690		return -1;
691
692	sprintf(prefix, "wan%d_", unit);
693	return 0;
694}
695
696static int
697add_wan_routes(char *wan_ifname)
698{
699	char prefix[] = "wanXXXXXXXXXX_";
700
701	/* Figure out nvram variable name prefix for this i/f */
702	if (wan_prefix(wan_ifname, prefix) < 0)
703		return -1;
704
705	return add_routes(prefix, "route", wan_ifname);
706}
707
708static int
709del_wan_routes(char *wan_ifname)
710{
711	char prefix[] = "wanXXXXXXXXXX_";
712
713	/* Figure out nvram variable name prefix for this i/f */
714	if (wan_prefix(wan_ifname, prefix) < 0)
715		return -1;
716
717	return del_routes(prefix, "route", wan_ifname);
718}
719
720int
721wan_valid(char *ifname)
722{
723	char name[80], *next;
724
725	foreach(name, nvram_safe_get("wan_ifnames"), next)
726		if (ifname && !strcmp(ifname, name))
727			return 1;
728
729#ifdef WIRELESS_WAN
730	if (nvram_invmatch("wl_mode_ex", "ap")) {
731		return nvram_match("wl0_ifname", ifname);
732	}
733#endif
734	return 0;
735}
736
737void
738start_wan(void)
739{
740	char *wan_ifname;
741	char *wan_proto;
742	int unit;
743	char tmp[100], prefix[] = "wanXXXXXXXXXX_";
744	char eabuf[32];
745	int s;
746	struct ifreq ifr;
747	pid_t pid;
748
749	/* check if we need to setup WAN */
750	if (nvram_match("router_disable", "1")
751#ifdef BTN_SETUP
752			|| is_ots()/* Cherry Cho unmarked in 2007/2/13. */
753#endif
754	)
755		return;
756
757#ifdef ASUS_EXT
758	update_wan_status(0);
759#endif
760
761	/* start connection independent firewall */
762	start_firewall();
763
764// 2008.08 James. If wan port wasn't plugged, the filter also can be used. {
765	char logaccept[16], logdrop[16], wan_if[8];
766
767	memset(logaccept, 0, 16);
768	memset(logdrop, 0, 16);
769	memset(wan_if, 0, 8);
770
771	/* Determine the log type */
772	if(nvram_match("fw_log_x", "accept") || nvram_match("fw_log_x", "both"))
773		strcpy(logaccept, "logaccept");
774	else
775		strcpy(logaccept, "ACCEPT");
776
777	if(nvram_match("fw_log_x", "drop") || nvram_match("fw_log_x", "both"))
778		strcpy(logdrop, "logdrop");
779	else
780		strcpy(logdrop, "DROP");
781
782	if(nvram_match("wan_proto", "pppoe")
783			|| nvram_match("wan_proto", "pptp")
784			|| nvram_match("wan_proto", "l2tp")
785#ifdef CDMA // HSDPA
786			|| strcmp(nvram_safe_get("hsdpa_product"), "") != 0
787#endif
788			)
789		strcpy(wan_if, "ppp0");
790	else
791		strcpy(wan_if, "eth0");
792
793	/* Filter setting */
794	filter_setting(wan_if, NULL, "br0", NULL, logaccept, logdrop);
795// 2008.08 James. }
796
797	/* Create links */
798	mkdir("/tmp/ppp", 0777);
799#ifdef CDMA
800	mkdir("/tmp/ppp/peers", 0777); // HSDPA
801#endif
802	symlink("/sbin/rc", "/tmp/ppp/ip-up");
803	symlink("/sbin/rc", "/tmp/ppp/ip-down");
804	symlink("/sbin/rc", "/tmp/udhcpc");
805
806	//symlink("/dev/null", "/tmp/ppp/connect-errors");
807
808	/* Start each configured and enabled wan connection and its undelying i/f */
809	for (unit = 0; unit < MAX_NVPARSE; unit ++)
810	{
811#ifdef ASUS_EXT // Only multiple pppoe is allowed
812		if (unit>0 && nvram_invmatch("wan_proto", "pppoe")) break;
813#endif
814
815		snprintf(prefix, sizeof(prefix), "wan%d_", unit);
816
817		/* make sure the connection exists and is enabled */
818		wan_ifname = nvram_get(strcat_r(prefix, "ifname", tmp));
819		if (!wan_ifname)
820			continue;
821		wan_proto = nvram_get(strcat_r(prefix, "proto", tmp));
822		if (!wan_proto || !strcmp(wan_proto, "disabled"))
823			continue;
824
825		/* disable the connection if the i/f is not in wan_ifnames */
826		if (!wan_valid(wan_ifname)) {
827			nvram_set(strcat_r(prefix, "proto", tmp), "disabled");
828			continue;
829		}
830
831		dprintf("%s %s\n\n\n\n\n", wan_ifname, wan_proto);
832
833		/* Set i/f hardware address before bringing it up */
834		if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0)
835			continue;
836
837		strncpy(ifr.ifr_name, wan_ifname, IFNAMSIZ);
838
839		/* Since WAN interface may be already turned up (by vlan.c),
840		   if WAN hardware address is specified (and different than the current one),
841		   we need to make it down for synchronizing hwaddr. */
842		if (ioctl(s, SIOCGIFHWADDR, &ifr)) {
843			close(s);
844			continue;
845		}
846
847		ether_atoe(nvram_safe_get(strcat_r(prefix, "hwaddr", tmp)), eabuf);
848		if (bcmp(eabuf, ifr.ifr_hwaddr.sa_data, ETHER_ADDR_LEN))
849		{
850			/* current hardware address is different than user specified */
851			ifconfig(wan_ifname, 0, NULL, NULL);
852		}
853
854		/* Configure i/f only once, specially for wireless i/f shared by multiple connections */
855		if (ioctl(s, SIOCGIFFLAGS, &ifr)) {
856			close(s);
857			continue;
858		}
859		if (!(ifr.ifr_flags & IFF_UP)) {
860			/* Sync connection nvram address and i/f hardware address */
861			memset(ifr.ifr_hwaddr.sa_data, 0, ETHER_ADDR_LEN);
862
863			if (!nvram_invmatch(strcat_r(prefix, "hwaddr", tmp), "") ||
864			    !ether_atoe(nvram_safe_get(strcat_r(prefix, "hwaddr", tmp)), ifr.ifr_hwaddr.sa_data) ||
865			    !memcmp(ifr.ifr_hwaddr.sa_data, "\0\0\0\0\0\0", ETHER_ADDR_LEN)) {
866				if (ioctl(s, SIOCGIFHWADDR, &ifr)) {
867					close(s);
868					continue;
869				}
870				nvram_set(strcat_r(prefix, "hwaddr", tmp), ether_etoa(ifr.ifr_hwaddr.sa_data, eabuf));
871			}
872			else {
873				ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
874				ioctl(s, SIOCSIFHWADDR, &ifr);
875			}
876
877			/* Bring up i/f */
878			ifconfig(wan_ifname, IFUP, NULL, NULL);
879
880#ifdef URE
881			/* do wireless specific config */
882			if(nvram_match("ure_disable", "1"))
883			{
884				eval("wlconf", wan_ifname, "up");
885			}
886#else
887			/* do wireless specific config */
888			eval("wlconf", wan_ifname, "up");
889#endif
890		}
891
892		close(s);
893
894#ifdef ASUS_EXT
895		if (unit==0)
896		{
897			FILE *fp;
898
899			setup_ethernet(nvram_safe_get("wan_ifname"));
900			start_pppoe_relay(wan_ifname);
901
902			/* Enable Forwarding */
903			if ((fp = fopen("/proc/sys/net/ipv4/ip_forward", "r+"))) 			{
904				fputc('1', fp);
905				fclose(fp);
906			} else
907			{
908				perror("/proc/sys/net/ipv4/ip_forward");
909			}
910		}
911
912		/*
913		* Configure PPPoE connection. The PPPoE client will run
914		* ip-up/ip-down scripts upon link's connect/disconnect.
915		*/
916#ifdef CDMA // HSDPA {
917		if (!nvram_match("hsdpa_product", "") && nvram_match("cdma_down", "1"))
918        	{
919			nvram_set("cdma_down", "0");
920			start_cdma();
921                	update_wan_status(0);
922        	}
923		else
924#endif // HSDPA }
925		if (strcmp(wan_proto, "pppoe") == 0 || strcmp(wan_proto, "pptp") == 0 || strcmp(wan_proto, "l2tp") == 0)
926		{
927			int demand = atoi(nvram_safe_get(strcat_r(prefix, "pppoe_idletime", tmp))) &&
928					strcmp(wan_proto, "l2tp") /* L2TP does not support idling */ ;
929
930			/* update demand option */
931			nvram_set(strcat_r(prefix, "pppoe_demand", tmp), demand ? "1" : "0");
932
933			/* Bring up  WAN interface */
934			ifconfig(wan_ifname, IFUP,
935					nvram_get(strcat_r(prefix, "pppoe_ipaddr", tmp)),
936					nvram_get(strcat_r(prefix, "pppoe_netmask", tmp)));
937
938			/* launch dhcp client and wait for lease forawhile */
939			if (nvram_match(strcat_r(prefix, "pppoe_ipaddr", tmp), "0.0.0.0"))
940			{
941printf("*** when pppoe_ipaddr=0.0.0.0. ***\n");
942				char *wan_hostname = nvram_get(strcat_r(prefix, "hostname", tmp));
943				char *dhcp_argv[] = { "udhcpc",
944									"-i", wan_ifname,
945									"-p", (sprintf(tmp, "/var/run/udhcpc%d.pid", unit), tmp),
946									"-s", "/tmp/udhcpc",
947									"-b",
948									wan_hostname && *wan_hostname ? "-H" : NULL,
949									wan_hostname && *wan_hostname ? wan_hostname : NULL,
950									NULL
951									};
952				/* Start dhcp daemon */
953				_eval(dhcp_argv, NULL, 0, NULL);
954			}
955			else {
956				/* start firewall */
957				start_firewall_ex(nvram_safe_get(strcat_r(prefix, "pppoe_ifname", tmp)),
958						"0.0.0.0", "br0", nvram_safe_get("lan_ipaddr"));
959
960				/* setup static wan routes via physical device */
961				add_routes("wan_", "route", wan_ifname);
962				/* and set default route if specified with metric 1 */
963				if (inet_addr_(nvram_safe_get(strcat_r(prefix, "pppoe_gateway", tmp))) &&
964						!nvram_match("wan_heartbeat_x", ""))
965					route_add(wan_ifname, 2, "0.0.0.0",
966							nvram_safe_get(strcat_r(prefix, "pppoe_gateway", tmp)), "0.0.0.0");
967				/* start multicast router */
968				start_igmpproxy(wan_ifname);
969			}
970
971			/* launch pppoe client daemon */
972			start_pppd(prefix);
973
974			/* ppp interface name is referenced from this point on */
975			wan_ifname = nvram_safe_get(strcat_r(prefix, "pppoe_ifname", tmp));
976
977			/* Pretend that the WAN interface is up */
978			if (nvram_match(strcat_r(prefix, "pppoe_demand", tmp), "1"))
979			{
980				int timeout = 5;
981				/* Wait for pppx to be created */
982				while (ifconfig(wan_ifname, IFUP, NULL, NULL) && timeout--)
983					sleep(1);
984
985				/* Retrieve IP info */
986				if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0)
987					continue;
988				strncpy(ifr.ifr_name, wan_ifname, IFNAMSIZ);
989
990				/* Set temporary IP address */
991				if (ioctl(s, SIOCGIFADDR, &ifr))
992					perror(wan_ifname);
993				nvram_set(strcat_r(prefix, "ipaddr", tmp), inet_ntoa(sin_addr(&ifr.ifr_addr)));
994				nvram_set(strcat_r(prefix, "netmask", tmp), "255.255.255.255");
995
996				/* Set temporary P-t-P address */
997				if (ioctl(s, SIOCGIFDSTADDR, &ifr))
998					perror(wan_ifname);
999				nvram_set(strcat_r(prefix, "gateway", tmp), inet_ntoa(sin_addr(&ifr.ifr_dstaddr)));
1000
1001				close(s);
1002
1003				/*
1004				 * Preset routes so that traffic can be sent to proper pppx even before
1005				 * the link is brought up.
1006				 */
1007				preset_wan_routes(wan_ifname);
1008			}
1009#ifdef ASUS_EXT
1010			nvram_set("wan_ifname_t", wan_ifname);
1011#endif
1012		}
1013#endif
1014		/*
1015		 * Configure DHCP connection. The DHCP client will run
1016		 * 'udhcpc bound'/'udhcpc deconfig' upon finishing IP address
1017		 * renew and release.
1018		 */
1019		else if (!strcmp(wan_proto, "dhcp")
1020#ifdef BIGPOND
1021				|| !strcmp(wan_proto, "bigpond")
1022#endif
1023				) {
1024			char *wan_hostname = nvram_get(strcat_r(prefix, "hostname", tmp));
1025			char *dhcp_argv[] = { "udhcpc",
1026						"-i", wan_ifname,
1027						"-p", (sprintf(tmp, "/var/run/udhcpc%d.pid", unit), tmp),
1028						"-s", "/tmp/udhcpc",
1029						wan_hostname && *wan_hostname ? "-H" : NULL,
1030						wan_hostname && *wan_hostname ? wan_hostname : NULL,
1031						NULL
1032						};
1033			/* Start dhcp daemon */
1034			_eval(dhcp_argv, NULL, 0, &pid);
1035			/* Update wan information for null DNS server */
1036			update_wan_status(1);
1037
1038#ifdef ASUS_EXT
1039			wanmessage("Can not get IP from server");
1040			nvram_set("wan_ifname_t", wan_ifname);
1041#endif
1042		}
1043		/* Configure static IP connection. */
1044		else if (strcmp(wan_proto, "static") == 0) {
1045			/* Assign static IP address to i/f */
1046			ifconfig(wan_ifname, IFUP,
1047					nvram_safe_get(strcat_r(prefix, "ipaddr", tmp)),
1048					nvram_safe_get(strcat_r(prefix, "netmask", tmp)));
1049			/* We are done configuration */
1050			wan_up(wan_ifname);
1051#ifdef ASUS_EXT
1052			nvram_set("wan_ifname_t", wan_ifname);
1053#endif
1054		}
1055
1056#ifndef ASUS_EXT
1057		/* Start connection dependent firewall */
1058		start_firewall2(wan_ifname);
1059#endif
1060
1061		dprintf("%s %s\n",
1062				nvram_safe_get(strcat_r(prefix, "ipaddr", tmp)),
1063				nvram_safe_get(strcat_r(prefix, "netmask", tmp)));
1064	}
1065
1066	/* Report stats */
1067	if (nvram_invmatch("stats_server", "")) {
1068		char *stats_argv[] = { "stats", nvram_get("stats_server"), NULL };
1069		_eval(stats_argv, NULL, 5, NULL);
1070	}
1071}
1072
1073void
1074stop_wan(void)
1075{
1076	char name[80], *next, signal[] = "XXXX";
1077
1078	eval("killall", "stats");
1079	eval("killall", "ntpclient");
1080
1081	/* Shutdown and kill all possible tasks */
1082	eval("killall", "ip-up");
1083	eval("killall", "ip-down");
1084#if 0
1085	snprintf(signal, sizeof(signal), "-%d", SIGHUP);
1086	eval("killall", signal, "pppoecd");
1087	eval("killall", signal, "pppd");
1088	eval("killall", "pppoecd");
1089#endif
1090	eval("killall", "l2tpd");
1091	eval("killall", "pppd");
1092#ifdef CDMA
1093	eval("killall", "chat");
1094#endif
1095	snprintf(signal, sizeof(signal), "-%d", SIGUSR2);
1096	eval("killall", signal, "udhcpc");
1097	eval("killall", "udhcpc");
1098	eval("killall", "igmpproxy");
1099
1100	/* Bring down WAN interfaces */
1101	foreach(name, nvram_safe_get("wan_ifnames"), next)
1102	{
1103		ifconfig(name, 0, NULL, NULL);
1104	}
1105
1106	/* Remove dynamically created links */
1107	unlink("/tmp/udhcpc");
1108
1109	unlink("/tmp/ppp/ip-up");
1110	unlink("/tmp/ppp/ip-down");
1111//	unlink("/tmp/ppp/options");
1112	rmdir("/tmp/ppp");
1113
1114#ifdef ASUS_EXT
1115	update_wan_status(0);
1116#endif
1117	dprintf("done\n");
1118}
1119
1120void
1121stop_wan2(void)
1122{
1123	char name[80], *next, signal[] = "XXXX";
1124
1125	eval("killall", "stats");
1126	eval("killall", "ntpclient");
1127
1128	/* Shutdown and kill all possible tasks */
1129	eval("killall", "ip-up");
1130	eval("killall", "ip-down");
1131	/*snprintf(signal, sizeof(signal), "-%d", SIGHUP);
1132	eval("killall", signal, "pppoecd");
1133	eval("killall", signal, "pppd");//*/
1134	eval("killall", "pppoecd");
1135	eval("killall", "l2tpd");
1136	eval("killall", "pppd");
1137
1138	snprintf(signal, sizeof(signal), "-%d", SIGUSR2);
1139	eval("killall", signal, "udhcpc");
1140	eval("killall", "udhcpc");
1141
1142	/* Remove dynamically created links */
1143	unlink("/tmp/udhcpc");
1144
1145	unlink("/tmp/ppp/ip-up");
1146	unlink("/tmp/ppp/ip-down");
1147	unlink("/tmp/ppp/options");
1148	rmdir("/tmp/ppp");
1149
1150#ifdef ASUS_EXT
1151	if(nvram_invmatch("wan_ifname_t", "")) wan_down(nvram_safe_get("wan_ifname_t"));
1152#endif
1153
1154	dprintf("done\n");
1155}
1156
1157static int
1158update_resolvconf(void)
1159{
1160        FILE *fp;
1161        char word[100], *next;
1162
1163        /* check if auto dns enabled */
1164        if (!nvram_match("wan_dnsenable_x", "1"))
1165                return 0;
1166
1167        if (!(fp = fopen("/tmp/resolv.conf", "w+"))) {
1168                perror("/tmp/resolv.conf");
1169                return errno;
1170        }
1171
1172        foreach(word, (nvram_get("wan0_dns") ? :
1173                nvram_safe_get("wanx_dns")), next)
1174        {
1175                fprintf(fp, "nameserver %s\n", word);
1176        }
1177        fclose(fp);
1178
1179#ifdef ASUS_EXT
1180        stop_dns();
1181        start_dns();
1182#else
1183        /* notify dnsmasq */
1184        snprintf(tmp, sizeof(tmp), "-%d", SIGHUP);
1185        eval("killall", tmp, "dnsmasq");
1186#endif
1187
1188        return 0;
1189}
1190
1191void
1192wan_up(char *wan_ifname)
1193{
1194	char tmp[100], prefix[] = "wanXXXXXXXXXX_";
1195	char *wan_proto, *gateway;
1196
1197	/* Figure out nvram variable name prefix for this i/f */
1198	if (wan_prefix(wan_ifname, prefix) < 0)
1199	{
1200		/* called for dhcp+ppp */
1201		if (!nvram_match("wan0_ifname", wan_ifname))
1202			return;
1203
1204		/* re-start firewall with old ppp0 address or 0.0.0.0 */
1205		start_firewall_ex("ppp0", nvram_safe_get("wan0_ipaddr"),
1206				"br0", nvram_safe_get("lan_ipaddr"));
1207
1208		/* setup static wan routes via physical device */
1209		add_routes("wan_", "route", wan_ifname);
1210		/* and one supplied via DHCP */
1211		add_wanx_routes("wanx_", wan_ifname, 0);
1212
1213		gateway = inet_addr_(nvram_safe_get("wan_gateway")) != INADDR_ANY ?
1214				nvram_get("wan_gateway") : nvram_safe_get("wanx_gateway");
1215
1216		/* and default route with metric 1 */
1217		if (inet_addr_(gateway) != INADDR_ANY)
1218		{
1219			char word[100], *next;
1220
1221			route_add(wan_ifname, 2, "0.0.0.0", gateway, "0.0.0.0");
1222
1223			/* ... and to dns servers as well for demand ppp to work */
1224			if (nvram_match("wan_dnsenable_x", "1"))
1225				foreach(word, nvram_safe_get("wanx_dns"), next)
1226				{
1227					in_addr_t mask = inet_addr(nvram_safe_get("wanx_netmask"));
1228					if ((inet_addr(word) & mask) != (inet_addr(nvram_safe_get("wanx_ipaddr")) & mask))
1229						route_add(wan_ifname, 2, word, gateway, "255.255.255.255");
1230				}
1231		}
1232
1233		/* start multicast router */
1234		start_igmpproxy(wan_ifname);
1235
1236		update_resolvconf();
1237
1238		return;
1239	}
1240
1241	wan_proto = nvram_safe_get(strcat_r(prefix, "proto", tmp));
1242
1243	dprintf("%s %s\n", wan_ifname, wan_proto);
1244
1245	/* Set default route to gateway if specified */
1246	if (nvram_match(strcat_r(prefix, "primary", tmp), "1"))
1247	{
1248		if (strcmp(wan_proto, "dhcp") == 0 || strcmp(wan_proto, "static") == 0)
1249		{
1250			/* the gateway is in the local network */
1251			route_add(wan_ifname, 0, nvram_safe_get(strcat_r(prefix, "gateway", tmp)), NULL, "255.255.255.255");
1252		}
1253		/* default route via default gateway */
1254		route_add(wan_ifname, 0, "0.0.0.0", nvram_safe_get(strcat_r(prefix, "gateway", tmp)), "0.0.0.0");
1255		/* hack: avoid routing cycles, when both peer and server has the same IP */
1256		if (strcmp(wan_proto, "pptp") == 0
1257				|| strcmp(wan_proto, "l2tp") == 0
1258#ifdef CDMA // HSDPA
1259				|| strcmp(nvram_safe_get("hsdpa_product"), "") != 0
1260#endif
1261				) {
1262			/* delete gateway route as it's no longer needed */
1263			route_del(wan_ifname, 0, nvram_safe_get(strcat_r(prefix, "gateway", tmp)), "0.0.0.0", "255.255.255.255");
1264		}
1265	}
1266
1267	/* Install interface dependent static routes */
1268	add_wan_routes(wan_ifname);
1269
1270	/* setup static wan routes via physical device */
1271	if (strcmp(wan_proto, "dhcp") == 0 || strcmp(wan_proto, "static") == 0)
1272	{
1273		nvram_set("wanx_gateway", nvram_safe_get(strcat_r(prefix, "gateway", tmp)));
1274		add_routes("wan_", "route", wan_ifname);
1275	}
1276
1277	/* and one supplied via DHCP */
1278	if (strcmp(wan_proto, "dhcp") == 0)
1279		add_wanx_routes(prefix, wan_ifname, 0);
1280
1281	/* Add dns servers to resolv.conf */
1282	update_resolvconf();
1283
1284	/* Sync time */
1285	//start_ntpc();
1286#ifdef ASUS_EXT
1287	update_wan_status(1);
1288
1289	start_firewall_ex(wan_ifname, nvram_safe_get(strcat_r(prefix, "ipaddr", tmp)),
1290			"br0", nvram_safe_get("lan_ipaddr"));
1291
1292	start_ddns();
1293	stop_upnp();
1294	start_upnp();
1295#endif
1296
1297#ifdef BIGPOND
1298	if (strcmp(wan_proto, "bigpond")==0)
1299	{
1300		stop_bpalogin();
1301		start_bpalogin();
1302	}
1303#endif
1304
1305#ifdef CDMA
1306	if (!nvram_match("hsdpa_product", "")) // HSDPA
1307	{
1308		nvram_set("cdma_down", "2");
1309		nvram_set("got_HSDPA", "1");
1310	}
1311#endif
1312#ifdef QOS
1313	nvram_set("qos_userspec_app", "0");
1314	nvram_set("qos_global_enable", "0");
1315	nvram_set("qos_userdef_enable", "0");
1316	nvram_set("qos_enable", "0");
1317
1318	if(nvram_invmatch("qos_rulenum_x", "0"))
1319		nvram_set("qos_userspec_app", "1");
1320
1321	if(nvram_match("qos_pshack_prio", "1")
1322			|| nvram_match("qos_shortpkt_prio", "1")
1323			|| nvram_match("qos_service_enable", "1")
1324			|| nvram_match("qos_tos_prio", "1")
1325			)
1326		nvram_set("qos_global_enable", "1");
1327
1328	if(nvram_match("qos_userspec_app", "1") || nvram_match("qos_dfragment_enable", "1"))
1329		nvram_set("qos_userdef_enable", "1");
1330
1331	if(nvram_match("qos_global_enable", "1") || nvram_match("qos_userdef_enable", "1"))
1332		nvram_set("qos_enable", "1");
1333	else
1334		nvram_set("qos_enable", "0");
1335
1336	if(nvram_match("qos_enable", "1")){
1337		nvram_set("qos_ubw", "0");
1338		nvram_set("qos_ubw_tmp", "0");
1339
1340		if(nvram_invmatch("qos_manual_ubw", "0") && nvram_invmatch("qos_manual_ubw", "")){
1341			nvram_set("qos_ubw", nvram_get("qos_manual_ubw"));
1342			nvram_set("qos_ubw_tmp", nvram_get("qos_manual_ubw"));
1343		}
1344		else{
1345			qos_get_wan_rate();
1346			nvram_set("qos_ubw_tmp", nvram_get("qos_ubw"));
1347		}
1348
1349		Speedtest_Init();
1350	}
1351#endif
1352
1353	/* start multicast router */
1354	if(!strcmp(wan_proto, "dhcp")
1355#ifdef BIGPOND
1356			|| !strcmp(wan_proto, "bigpond")
1357#endif
1358			|| !strcmp(wan_proto, "static"))
1359	{
1360		start_igmpproxy(wan_ifname);
1361	}
1362
1363	dprintf("done\n");
1364}
1365
1366void
1367wan_down(char *wan_ifname)
1368{
1369	char tmp[100], prefix[] = "wanXXXXXXXXXX_";
1370	char *wan_proto;
1371
1372	/* Figure out nvram variable name prefix for this i/f */
1373	if (wan_prefix(wan_ifname, prefix) < 0)
1374		return;
1375
1376	wan_proto = nvram_safe_get(strcat_r(prefix, "proto", tmp));
1377
1378	dprintf("%s %s\n", wan_ifname, wan_proto);
1379
1380	/* Remove default route to gateway if specified */
1381	if (nvram_match(strcat_r(prefix, "primary", tmp), "1"))
1382		route_del(wan_ifname, 0, "0.0.0.0",
1383			nvram_safe_get(strcat_r(prefix, "gateway", tmp)),
1384			"0.0.0.0");
1385
1386        /* Del route while static IP */
1387        if (strcmp(wan_proto, "static") == 0)
1388                ifconfig(wan_ifname, IFUP, "0.0.0.0", NULL);
1389
1390	/* Remove interface dependent static routes */
1391	del_wan_routes(wan_ifname);
1392
1393        /* Update resolv.conf -- leave as is if no dns servers left for demand to work */
1394        if (*nvram_safe_get("wanx_dns"))
1395                nvram_unset(strcat_r(prefix, "dns", tmp));
1396	update_resolvconf();
1397
1398#ifdef ASUS_EXT
1399	//printf("update wan status\n");
1400	update_wan_status(0);
1401#endif
1402
1403#ifdef BIGPOND
1404	if(!strcmp(wan_proto, "bigpond"))
1405		stop_bpalogin();
1406#endif
1407
1408#ifdef CDMA
1409	if (!nvram_match("hsdpa_product", "")) // HSDPA
1410	{
1411		stop_cdma();
1412csprintf("--- network: CDMA=%s! ---\n", nvram_safe_get("cdma_down"));
1413		if(nvram_invmatch("cdma_down", "99")
1414				&& nvram_invmatch("cdma_down", "3")){
1415			nvram_set("cdma_down", "1");
1416csprintf("--- network: Wrong! ---\n");
1417		}
1418	}
1419#endif
1420
1421	nvram_set("wan_ready", "0");
1422
1423	dprintf("done\n");
1424}
1425
1426#ifdef ASUS_EXT
1427#ifndef FLASH2M
1428void
1429lan_up(char *lan_ifname)
1430{
1431	FILE *fp;
1432	char word[100], *next;
1433	char line[100];
1434
1435	/* Set default route to gateway if specified */
1436	route_add(lan_ifname, 0, "0.0.0.0",
1437			nvram_safe_get("lan_gateway"),
1438			"0.0.0.0");
1439
1440	/* Open resolv.conf to read */
1441	if (!(fp = fopen("/tmp/resolv.conf", "w"))) {
1442		perror("/tmp/resolv.conf");
1443		return;
1444	}
1445
1446	if (nvram_invmatch("lan_gateway", ""))
1447		fprintf(fp, "nameserver %s\n", nvram_safe_get("lan_gateway"));
1448
1449	foreach(word, nvram_safe_get("lan_dns"), next)
1450	{
1451		fprintf(fp, "nameserver %s\n", word);
1452	}
1453	fclose(fp);
1454
1455	/* Sync time */
1456	//start_ntpc();
1457}
1458
1459void
1460lan_down(char *lan_ifname)
1461{
1462	/* Remove default route to gateway if specified */
1463	route_del(lan_ifname, 0, "0.0.0.0",
1464			nvram_safe_get("lan_gateway"),
1465			"0.0.0.0");
1466
1467	/* remove resolv.conf */
1468	unlink("/tmp/resolv.conf");
1469}
1470
1471
1472void
1473lan_up_ex(char *lan_ifname)
1474{
1475	FILE *fp;
1476	char word[100], *next;
1477	char line[100];
1478
1479	/* Set default route to gateway if specified */
1480	route_add(lan_ifname, 0, "0.0.0.0",
1481			nvram_safe_get("lan_gateway_t"),
1482			"0.0.0.0");
1483
1484	/* Open resolv.conf to read */
1485	if (!(fp = fopen("/tmp/resolv.conf", "w"))) {
1486		perror("/tmp/resolv.conf");
1487		return;
1488	}
1489
1490	if (nvram_invmatch("lan_gateway_t", ""))
1491		fprintf(fp, "nameserver %s\n", nvram_safe_get("lan_gateway_t"));
1492
1493	foreach(word, nvram_safe_get("lan_dns_t"), next)
1494	{
1495		fprintf(fp, "nameserver %s\n", word);
1496	}
1497	fclose(fp);
1498
1499	/* Sync time */
1500	//start_ntpc();
1501	//update_lan_status(1);
1502}
1503
1504void
1505lan_down_ex(char *lan_ifname)
1506{
1507	/* Remove default route to gateway if specified */
1508	route_del(lan_ifname, 0, "0.0.0.0",
1509			nvram_safe_get("lan_gateway_t"),
1510			"0.0.0.0");
1511
1512	/* remove resolv.conf */
1513	unlink("/tmp/resolv.conf");
1514
1515	update_lan_status(0);
1516}
1517#endif
1518#endif
1519
1520static int
1521notify_nas(char *type, char *ifname, char *action)
1522{
1523	char *argv[] = {"nas4not", type, ifname, action,
1524			NULL,	/* role */
1525			NULL,	/* crypto */
1526			NULL,	/* auth */
1527			NULL,	/* passphrase */
1528			NULL,	/* ssid */
1529			NULL};
1530	char *str = NULL;
1531	int retries = 10;
1532	char tmp[100], prefix[] = "wlXXXXXXXXXX_";
1533	int unit;
1534	char remote[ETHER_ADDR_LEN];
1535	char ssid[48], pass[80], auth[16], crypto[16], role[8];
1536	int i;
1537
1538	/* the wireless interface must be configured to run NAS */
1539	wl_ioctl(ifname, WLC_GET_INSTANCE, &unit, sizeof(unit));
1540	snprintf(prefix, sizeof(prefix), "wl%d_", unit);
1541	if (nvram_match(strcat_r(prefix, "akm", tmp), "") &&
1542	    nvram_match(strcat_r(prefix, "auth_mode", tmp), "none"))
1543		return 0;
1544
1545	/* find WDS link configuration */
1546	wl_ioctl(ifname, WLC_WDS_GET_REMOTE_HWADDR, remote, ETHER_ADDR_LEN);
1547	for (i = 0; i < MAX_NVPARSE; i ++) {
1548		char mac[ETHER_ADDR_STR_LEN];
1549		uint8 ea[ETHER_ADDR_LEN];
1550
1551		if (get_wds_wsec(unit, i, mac, role, crypto, auth, ssid, pass) &&
1552		    ether_atoe(mac, ea) && !bcmp(ea, remote, ETHER_ADDR_LEN)) {
1553			argv[4] = role;
1554			argv[5] = crypto;
1555			argv[6] = auth;
1556			argv[7] = pass;
1557			argv[8] = ssid;
1558			break;
1559		}
1560	}
1561
1562	/* did not find WDS link configuration, use wireless' */
1563	if (i == MAX_NVPARSE) {
1564		/* role */
1565		argv[4] = "auto";
1566		/* crypto */
1567		argv[5] = nvram_safe_get(strcat_r(prefix, "crypto", tmp));
1568		/* auth mode */
1569		argv[6] = nvram_safe_get(strcat_r(prefix, "akm", tmp));
1570		/* passphrase */
1571		argv[7] = nvram_safe_get(strcat_r(prefix, "wpa_psk", tmp));
1572		/* ssid */
1573		argv[8] = nvram_safe_get(strcat_r(prefix, "ssid", tmp));
1574	}
1575
1576	/* wait till nas is started */
1577	while (retries -- > 0 && !(str = file2str("/tmp/nas.lan.pid")))
1578		sleep(1);
1579	if (str) {
1580		int pid;
1581		free(str);
1582		return _eval(argv, ">/dev/console", 0, &pid);
1583	}
1584	return -1;
1585}
1586
1587int
1588hotplug_net(void)
1589{
1590	char *lan_ifname = nvram_safe_get("lan_ifname");
1591	char *interface, *action;
1592
1593	if (!(interface = getenv("INTERFACE")) ||
1594	    !(action = getenv("ACTION")))
1595		return EINVAL;
1596
1597	if (strncmp(interface, "wds", 3))
1598		return 0;
1599
1600	if (!strcmp(action, "register")) {
1601		/* Bring up the interface and add to the bridge */
1602		ifconfig(interface, IFUP, NULL, NULL);
1603
1604		/* Bridge WDS interfaces */
1605		if (!strncmp(lan_ifname, "br", 2) &&
1606		    eval("brctl", "addif", lan_ifname, interface))
1607		    return 0;
1608
1609		/* Notify NAS of adding the interface */
1610		notify_nas("lan", interface, "up");
1611	}
1612	return 0;
1613}
1614
1615
1616int
1617wan_ifunit(char *wan_ifname)
1618{
1619	int unit;
1620	char tmp[100], prefix[] = "wanXXXXXXXXXX_";
1621
1622	if ((unit = ppp_ifunit(wan_ifname)) >= 0)
1623		return unit;
1624	else {
1625		for (unit = 0; unit < MAX_NVPARSE; unit ++) {
1626			snprintf(prefix, sizeof(prefix), "wan%d_", unit);
1627			if (nvram_match(strcat_r(prefix, "ifname", tmp), wan_ifname) &&
1628			    (nvram_match(strcat_r(prefix, "proto", tmp), "dhcp") ||
1629#ifdef BIGPOND
1630					 nvram_match(strcat_r(prefix, "proto", tmp), "bigpond") ||
1631#endif
1632#ifdef CDMA
1633			     nvram_match(strcat_r(prefix, "proto", tmp), "cdma") ||
1634#endif
1635			     nvram_match(strcat_r(prefix, "proto", tmp), "static")))
1636				return unit;
1637		}
1638	}
1639	return -1;
1640}
1641
1642int
1643preset_wan_routes(char *wan_ifname)
1644{
1645	char tmp[100], prefix[] = "wanXXXXXXXXXX_";
1646
1647
1648	/* Figure out nvram variable name prefix for this i/f */
1649	if (wan_prefix(wan_ifname, prefix) < 0)
1650		return -1;
1651
1652	/* Set default route to gateway if specified */
1653	if (nvram_match(strcat_r(prefix, "primary", tmp), "1"))
1654	{
1655		route_add(wan_ifname, 0, "0.0.0.0", "0.0.0.0", "0.0.0.0");
1656	}
1657
1658	/* Install interface dependent static routes */
1659	add_wan_routes(wan_ifname);
1660	return 0;
1661}
1662
1663int
1664wan_primary_ifunit(void)
1665{
1666	int unit;
1667
1668	for (unit = 0; unit < MAX_NVPARSE; unit ++) {
1669		char tmp[100], prefix[] = "wanXXXXXXXXXX_";
1670		snprintf(prefix, sizeof(prefix), "wan%d_", unit);
1671		if (nvram_match(strcat_r(prefix, "primary", tmp), "1"))
1672			return unit;
1673	}
1674
1675	return 0;
1676}
1677
1678