root/src/router/services/networking/network.c

Revision 12432, 97.1 kB (checked in by BrainSlayer, 5 months ago)

hso support

Line 
1 /*
2  * network.c
3  *
4  * Copyright (C) 2007 Sebastian Gottschall <gottschall@dd-wrt.com>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * $Id:
21  */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <errno.h>
26 #include <syslog.h>
27 #include <ctype.h>
28 #include <string.h>
29 #include <signal.h>
30 #include <unistd.h>
31 #include <math.h>
32 #include <sys/stat.h>
33 #include <sys/ioctl.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <net/if.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
39 #include <net/if_arp.h>
40 #include <sys/sysinfo.h>
41
42 typedef u_int64_t u64;
43 typedef u_int32_t u32;
44 typedef u_int16_t u16;
45 typedef u_int8_t u8;
46
47 typedef u_int64_t __u64;
48 typedef u_int32_t __u32;
49 typedef u_int16_t __u16;
50 typedef u_int8_t __u8;
51
52 #include <limits.h>
53 #include <unistd.h>
54 #include <stdlib.h>
55 #include <stdio.h>
56 #include <stdint.h>
57 #include <fcntl.h>
58 #include <errno.h>
59 #include <error.h>
60 #include <time.h>
61 #include <sys/ioctl.h>
62 #include <sys/types.h>
63 #include <sys/param.h>
64 #include <sys/mount.h>
65 #include <sys/stat.h>
66 #include <sys/reboot.h>
67 #include <sys/sysinfo.h>
68
69 #include <string.h>
70 #include <linux/version.h>
71
72 #include <linux/sockios.h>
73 #include <linux/ethtool.h>
74 // #include <libbridge.h>
75
76 #include <bcmnvram.h>
77 #include <netconf.h>
78 #include <shutils.h>
79 #include <code_pattern.h>
80 #include <wlutils.h>
81 #include <utils.h>
82 #include <rc.h>
83 #include "ledcontrol.h"
84 #include <cy_conf.h>
85 #include <cymac.h>
86 #include <bcmutils.h>
87 #include <nvparse.h>
88 #include <etsockio.h>
89 #include <bcmparams.h>
90 #include <services.h>
91
92 extern int br_add_bridge(const char *brname);
93 extern int br_del_bridge(const char *brname);
94 extern int br_add_interface(const char *br, const char *dev);
95 extern int br_del_interface(const char *br, const char *dev);
96 extern int br_set_stp_state(const char *br, int stp_state);
97 void start_set_routes(void);
98
99 #define PTABLE_MAGIC 0xbadc0ded
100 #define PTABLE_SLT1 1
101 #define PTABLE_SLT2 2
102 #define PTABLE_ACKW 3
103 #define PTABLE_ADHM 4
104 #define PTABLE_END 0xffffffff
105
106 /*
107  * phy types
108  */
109 #define PHY_TYPE_A              0
110 #define PHY_TYPE_B              1
111 #define PHY_TYPE_G              2
112 #define PHY_TYPE_NULL           0xf
113
114 #define WL_IOCTL(name, cmd, buf, len) (wl_ioctl((name), (cmd), (buf), (len)))
115
116 #define TXPWR_MAX 251
117 #define TXPWR_DEFAULT 70
118
119 #define IFUP (IFF_UP | IFF_RUNNING | IFF_BROADCAST | IFF_MULTICAST)
120 #define sin_addr(s) (((struct sockaddr_in *)(s))->sin_addr)
121 /*
122  * configure loopback interface
123  */
124 void config_loopback(void)
125 {
126         /*
127          * Bring up loopback interface
128          */
129         ifconfig("lo", IFUP, "127.0.0.1", "255.0.0.0");
130
131         /*
132          * Add to routing table
133          */
134         route_add("lo", 0, "127.0.0.0", "0.0.0.0", "255.0.0.0");
135 }
136
137 char *getMacAddr(char *ifname, char *mac)
138 {
139         unsigned char hwbuff[16];
140         int i = wl_hwaddr(ifname, hwbuff);
141
142         if (i < 0)
143                 return NULL;
144         sprintf(mac, "%02X:%02X:%02X:%02X:%02X:%02X", hwbuff[0], hwbuff[1],
145                 hwbuff[2], hwbuff[3], hwbuff[4], hwbuff[5]);
146         return mac;
147 }
148
149 static unsigned long ptable[128];
150 static unsigned long kmem_offset;
151 static inline void wlc_get_mem_offset(void)
152 {
153         FILE *f;
154         char s[64];
155
156         /*
157          * yes, i'm lazy ;)
158          */
159         f = popen("grep '\\[wl]' /proc/ksyms | sort", "r");
160         if (fgets(s, 64, f) == 0) {
161                 return;
162         }
163         pclose(f);
164
165         s[8] = 0;
166         kmem_offset = strtoul(s, NULL, 16);
167
168         /*
169          * sanity check
170          */
171         if (kmem_offset < 0xc0000000)
172                 kmem_offset = 0;
173         return;
174 }
175
176 static int ptable_init(void)
177 {
178         struct stat statbuf;
179         int fd;
180
181         if (ptable[0] == PTABLE_MAGIC)
182                 return 0;
183
184         if ((fd = open("/etc/patchtable.bin", O_RDONLY)) < 0)
185                 return -1;
186
187         if (fstat(fd, &statbuf) < 0)
188                 goto failed;
189
190         if (statbuf.st_size < 512)
191                 goto failed;
192
193         // if (lseek(fd, statbuf.st_size - 512, SEEK_SET) < 0) {
194         // perror("lseek");
195         // goto failed;
196         // }
197
198         if (read(fd, ptable, 512) < 512)
199                 goto failed;
200
201         if (ptable[0] != PTABLE_MAGIC)
202                 goto failed;
203
204         close(fd);
205
206         wlc_get_mem_offset();
207         if (kmem_offset == 0)
208                 return -1;
209
210         return 0;
211
212 failed:
213         close(fd);
214
215         return -1;
216 }
217
218 static inline unsigned long wlc_kmem_read(unsigned long offset)
219 {
220         int fd;
221         unsigned long ret;
222
223         if ((fd = open("/dev/kmem", O_RDONLY)) < 0)
224                 return -1;
225
226         lseek(fd, 0x70000000, SEEK_SET);
227         lseek(fd, (kmem_offset - 0x70000000) + offset, SEEK_CUR);
228         read(fd, &ret, 4);
229         close(fd);
230
231         return ret;
232 }
233
234 static inline void wlc_kmem_write(unsigned long offset, unsigned long value)
235 {
236         int fd;
237
238         if ((fd = open("/dev/kmem", O_WRONLY)) < 0)
239                 return;
240
241         lseek(fd, 0x70000000, SEEK_SET);
242         lseek(fd, (kmem_offset - 0x70000000) + offset, SEEK_CUR);
243         write(fd, &value, 4);
244         close(fd);
245 }
246
247 static int wlc_patcher_getval(unsigned long key, unsigned long *val)
248 {
249         unsigned long *pt = &ptable[1];
250         unsigned long tmp;
251
252         if (ptable_init() < 0) {
253                 fprintf(stderr, "Could not load the ptable\n");
254                 return -1;
255         }
256
257         while (*pt != PTABLE_END) {
258                 if (*pt == key) {
259                         tmp = wlc_kmem_read(pt[1]);
260
261                         if (tmp == pt[2])
262                                 *val = 0xffffffff;
263                         else
264                                 *val = tmp;
265
266                         return 0;
267                 }
268                 pt += 3;
269         }
270
271         return -1;
272 }
273
274 static int wlc_patcher_setval(unsigned long key, unsigned long val)
275 {
276         unsigned long *pt = &ptable[1];
277
278         if (ptable_init() < 0) {
279                 fprintf(stderr, "Could not load the ptable\n");
280                 return -1;
281         }
282
283         if (val != 0xffffffff)
284                 val = (pt[2] & ~(0xffff)) | (val & 0xffff);
285
286         while (*pt != PTABLE_END) {
287                 if (*pt == key) {
288                         if (val == 0xffffffff)  /* default */
289                                 val = pt[2];
290
291                         wlc_kmem_write(pt[1], val);
292                 }
293                 pt += 3;
294         }
295
296         return 0;
297 }
298
299 /*
300  * static int get_wlc_slottime(wlc_param param, void *data, void *value) {
301  * int *val = (int *) value; int ret = 0;
302  *
303  * ret = wlc_patcher_getval(PTABLE_SLT1, (unsigned long *) val); if (*val !=
304  * 0xffffffff) *val &= 0xffff; } return ret; }
305  */
306 static int set_wlc_slottime(int value)
307 {
308         int ret = 0;
309
310         wlc_patcher_setval(PTABLE_SLT1, value);
311         wlc_patcher_setval(PTABLE_SLT2, ((value == -1) ? value : value + 510));
312         return ret;
313 }
314
315 static int wlc_noack(int value)
316 {
317         int ret = 0;
318
319         // if ((param & PARAM_MODE) == SET) {
320         wlc_patcher_setval(PTABLE_ACKW, (value ? 1 : 0));
321         // } else if ((param & PARAM_MODE) == GET) {
322         // ret = wlc_patcher_getval(PTABLE_ACKW, (unsigned long *) val);
323         // *val &= 0xffff;
324         // *val = (*val ? 1 : 0);
325         // }
326
327         return ret;
328 }
329
330 #ifndef HAVE_MADWIFI
331 #ifndef HAVE_RT2880
332 static int notify_nas(char *type, char *ifname, char *action);
333 #endif
334 #endif
335
336 void start_dhcpc(char *wan_ifname)
337 {
338         pid_t pid;
339         char *wan_hostname = nvram_get("wan_hostname");
340         char *vendorclass = nvram_get("dhcpc_vendorclass");
341         char *requestip = nvram_get("dhcpc_requestip");
342
343         symlink("/sbin/rc", "/tmp/udhcpc");
344
345         nvram_set("wan_get_dns", "");
346         killall("udhcpc", SIGTERM);
347
348         char *dhcp_argv[] = { "udhcpc",
349                 "-i", wan_ifname,
350                 "-p", "/var/run/udhcpc.pid",
351                 "-s", "/tmp/udhcpc",
352                 NULL, NULL,
353                 NULL, NULL,
354                 NULL, NULL,
355                 NULL
356         };
357
358         int i = 7;
359
360         if (vendorclass != NULL && strlen(vendorclass) > 0) {
361                 dhcp_argv[i] = "-V";
362                 i++;
363                 dhcp_argv[i] = vendorclass;
364                 i++;
365         }
366
367         if (requestip != NULL && strlen(requestip) > 0) {
368                 dhcp_argv[i] = "-r";
369                 i++;
370                 dhcp_argv[i] = requestip;
371                 i++;
372         }
373
374         if (wan_hostname != NULL && strlen(wan_hostname) > 0) {
375                 dhcp_argv[i] = "-H";
376                 i++;
377                 dhcp_argv[i] = wan_hostname;
378                 i++;
379         }
380
381         _evalpid(dhcp_argv, NULL, 0, &pid);
382
383 }
384
385 /*
386  * Enable WET DHCP relay for ethernet clients
387  */
388 static int enable_dhcprelay(char *ifname)
389 {
390         char name[80], *next;
391
392         dprintf("%s\n", ifname);
393
394         /*
395          * WET interface is meaningful only in bridged environment
396          */
397         if (strncmp(ifname, "br", 2) == 0) {
398                 foreach(name, nvram_safe_get("lan_ifnames"), next) {
399
400                         char mode[] = "wlXXXXXXXXXX_mode";
401                         int unit;
402
403                         /*
404                          * make sure the interface is indeed of wl
405                          */
406                         if (wl_probe(name))
407                                 continue;
408
409                         /*
410                          * get the instance number of the wl i/f
411                          */
412                         wl_ioctl(name, WLC_GET_INSTANCE, &unit, sizeof(unit));
413                         snprintf(mode, sizeof(mode), "wl%d_mode", unit);
414
415                         /*
416                          * enable DHCP relay, there should be only one WET i/f
417                          */
418                         if (nvram_match(mode, "wet")
419                             || nvram_match(mode, "apstawet")) {
420                                 uint32 ip;
421
422                                 inet_aton(nvram_safe_get("lan_ipaddr"),
423                                           (struct in_addr *)&ip);
424                                 if (wl_iovar_setint(name, "wet_host_ipv4", ip))
425                                         perror("wet_host_ipv4");
426                                 break;
427                         }
428                 }
429         }
430         return 0;
431 }
432
433 static int wlconf_up(char *name)
434 {
435
436         int phytype, gmode, val, ret;
437
438 #if defined(HAVE_MADWIFI) || defined(HAVE_RT2880)
439         return -1;
440 #endif
441         if (!strncmp(name, "vlan", 4))
442                 return -1;
443         if (!strncmp(name, "br", 2))
444                 return -1;
445 #ifdef HAVE_ONLYCLIENT
446         if (nvram_match("wl_mode", "ap")) {
447                 cprintf("this version does only support the client mode\n");
448                 nvram_set("wl_mode", "sta");
449                 nvram_commit();
450         }
451 #endif
452         int instance = get_wl_instance(name);
453
454         if (instance == -1)
455                 return -1;      // no wireless device
456         if (nvram_nmatch("infra", "wl%d_mode", instance)) {
457                 nvram_nset("0", "wl%d_infra", instance);
458         } else {
459                 nvram_nset("1", "wl%d_infra", instance);
460         }
461         ret = eval("wlconf", name, "up");
462         /*
463          * eval("wl","radio","off"); eval("wl","atten","0","0","60");
464          * eval("wl","lrl","16"); eval("wl","srl","16");
465          * eval("wl","interference","0"); eval("wl","radio","on");
466          */
467         gmode = atoi(nvram_nget("wl%d_gmode", instance));
468
469         /*
470          * Get current phy type
471          */
472         WL_IOCTL(name, WLC_GET_PHYTYPE, &phytype, sizeof(phytype));
473
474         // set preamble type for b cards
475         if (phytype == PHY_TYPE_B || gmode == 0) {
476                 if (nvram_nmatch("long", "wl%d_plcphdr", instance))
477                         val = WLC_PLCP_LONG;
478                 else if (nvram_nmatch("short", "wl%d_plcphdr", instance))
479                         val = WLC_PLCP_SHORT;
480                 else
481                         val = WLC_PLCP_AUTO;
482                 WL_IOCTL(name, WLC_SET_PLCPHDR, &val, sizeof(val));
483         }
484         // adjust txpwr and txant
485         val = atoi(nvram_nget("wl%d_txpwr", instance));
486         if (val < 1 || val > TXPWR_MAX)
487                 val = TXPWR_DEFAULT;
488         char pwr[8];
489         sprintf(pwr, "%d", val);
490         eval("wl", "-i", name, "txpwr1", "-m", "-o", pwr);
491         /*
492          * Set txant
493          */
494         val = atoi(nvram_nget("wl%d_txant", instance));
495         if (val < 0 || val > 3 || val == 2)
496                 val = 3;
497         WL_IOCTL(name, WLC_SET_TXANT, &val, sizeof(val));
498
499         /*
500          * if (nvram_match ("boardtype", "bcm94710dev")) { if (val == 0) val = 1;
501          * if (val == 1) val = 0; }
502          */
503         val = atoi(nvram_nget("wl%d_antdiv", instance));
504         WL_IOCTL(name, WLC_SET_ANTDIV, &val, sizeof(val));
505
506         /*
507          * search for "afterburner" string
508          */
509         char *afterburner = nvram_nget("wl%d_afterburner", instance);
510
511         if (!strcmp(afterburner, "on"))
512                 eval("wl", "-i", name, "afterburner_override", "1");
513         else if (!strcmp(afterburner, "off"))
514                 eval("wl", "-i", name, "afterburner_override", "0");
515         else                    // auto
516                 eval("wl", "-i", name, "afterburner_override", "-1");
517
518         char *shortslot = nvram_nget("wl%d_shortslot", instance);
519
520         if (!strcmp(shortslot, "long"))
521                 eval("wl", "-i", name, "shortslot_override", "0");
522         else if (!strcmp(shortslot, "short"))
523                 eval("wl", "-i", name, "shortslot_override", "1");
524         else                    // auto
525                 eval("wl", "-i", name, "shortslot_override", "-1");
526
527         // Set ACK Timing. Thx to Nbd
528         char *v;
529
530         if ((v = nvram_nget("wl%d_distance", instance))) {
531                 rw_reg_t reg;
532                 uint32 shm;
533
534                 val = atoi(v);
535                 if (val == 0) {
536 #ifdef HAVE_ACK
537                         eval("wl", "-i", name, "noack", "1");
538 #endif
539                         // wlc_noack (0);
540                         return 0;
541                 } else {
542 #ifdef HAVE_ACK
543                         eval("wl", "-i", name, "noack", "0");
544 #endif
545                         // wlc_noack (1);
546                 }
547
548                 val = 9 + (val / 150) + ((val % 150) ? 1 : 0);
549 #ifdef HAVE_ACK
550                 char strv[32];
551
552                 sprintf(strv, "%d", val);
553                 eval("wl", "-i", name, "acktiming", strv);
554 #endif
555         }
556
557         /*
558          * if (nvram_match("wl0_mode","sta") || nvram_match("wl0_mode","infra"))
559          * { val = 0; WL_IOCTL(name, WLC_SET_WET, &val, sizeof(val)); if
560          * (nvram_match("wl_mode", "infra")){ val = 0; WL_IOCTL(name,
561          * WLC_SET_INFRA, &val, sizeof(val)); } else{ val = 1; WL_IOCTL(name,
562          * WLC_SET_INFRA, &val, sizeof(val)); } }
563          */
564
565         if (nvram_nmatch("infra", "wl%d_mode", instance)) {
566                 eval("wl", "-i", name, "infra", "0");
567                 eval("wl", "-i", name, "ssid",
568                      nvram_nget("wl%d_ssid", instance));
569         }
570 #if !defined(HAVE_MADWIFI) && !defined(HAVE_RT2880)
571         eval("wl", "-i", name, "vlan_mode", "0");
572         char ifinst[32];
573
574         sprintf(ifinst, "wl%d", instance);
575         set_vifsmac(ifinst);
576 #endif
577         return ret;
578 }
579
580 int isClient(void)
581 {
582         if (getSTA())
583                 return 1;
584         return 0;
585
586 }
587
588 void start_wlconf(void)
589 {
590         int cnt = get_wl_instances();
591         int c;
592
593         for (c = 0; c < cnt; c++) {
594                 if (!nvram_nmatch("disabled", "wl%d_net_mode", c))
595                         wlconf_up(get_wl_instance_name(c));
596         }
597 }
598
599 // #ifdef HAVE_PORTSETUP
600 #ifdef HAVE_RT2880
601 #define IFMAP(a) getRADev(a)
602 #else
603 #define IFMAP(a) (a)
604 #endif
605
606 static void do_portsetup(char *lan, char *ifname)
607 {
608         char var[64];
609         char var2[64];
610
611         sprintf(var, "%s_bridged", IFMAP(ifname));
612         if (nvram_default_match(var, "1", "1")) {
613                 br_add_interface(getBridge(IFMAP(ifname)), IFMAP(ifname));
614         } else {
615                 ifconfig(ifname, IFUP, nvram_nget("%s_ipaddr", IFMAP(ifname)),
616                          nvram_nget("%s_netmask", ifname));
617         }
618
619 }
620
621 // #endif
622
623 #define PORTSETUPWAN(a) if (strlen(a)>0 && strlen(nvram_safe_get ("wan_ifname2"))>0) \
624             { \
625                 nvram_set ("wan_ifname", nvram_safe_get ("wan_ifname2")); \
626                 nvram_set ("wan_ifnames", nvram_safe_get ("wan_ifname2"));\
627             } \
628           else \
629             { \
630                 nvram_set ("wan_ifname",a); \
631                 nvram_set ("wan_ifnames",a); \
632             }
633
634  /*
635   * add wan ifname to lan_ifnames if we use fullswitch
636   */
637 void set_fullswitch(void)
638 {
639         char wanifname[8];
640         char lanifnames[128];
641
642         strcpy(wanifname, nvram_safe_get("wan_ifname"));
643         strcpy(lanifnames, nvram_safe_get("lan_ifnames"));
644
645         if (nvram_match("fullswitch", "1")
646             && (getSTA() || getWET()
647                 || nvram_match("wan_proto", "disabled"))) {
648                 if (!nvram_match("fullswitch_set", "1")) {
649                         nvram_set("lan_default", lanifnames);
650                         nvram_set("fullswitch_set", "1");
651                 }
652                 sprintf(lanifnames, "%s %s", nvram_safe_get("lan_default"),
653                         nvram_safe_get("wan_default"));
654                 strcpy(wanifname, "");
655         } else {
656                 if (nvram_match("fullswitch_set", "1")) {
657                         strcpy(lanifnames, nvram_safe_get("lan_default"));
658                         nvram_unset("lan_default");
659                         strcpy(wanifname, nvram_safe_get("wan_default"));
660                         nvram_unset("fullswitch_set");
661                 }
662         }
663
664         nvram_set("lan_ifnames", lanifnames);
665         nvram_set("wan_ifname", wanifname);
666         nvram_set("wan_ifnames", wanifname);
667         nvram_set("pppoe_wan_ifname", wanifname);
668         nvram_set("pppoe_ifname", wanifname);
669
670         return;
671 }
672
673 void reset_hwaddr(char *ifname)
674 {
675         struct ifreq ifr;
676         char eabuf[32];
677         int s;
678         if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0)
679                 return;
680         /*
681          * Get current LAN hardware address
682          */
683
684         strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
685         if (ioctl(s, SIOCGIFHWADDR, &ifr) == 0) {
686                 nvram_set("lan_hwaddr",
687                           ether_etoa(ifr.ifr_hwaddr.sa_data, eabuf));
688                 if (getRouterBrand() == ROUTER_DLINK_DIR320) {
689                         if (strlen(nvram_safe_get("et0macaddr")) == 12) {
690                                 char wlmac[32];
691
692                                 strcpy(wlmac, nvram_safe_get("wl0_hwaddr"));
693                                 MAC_SUB(wlmac);
694                                 nvram_set("et0macaddr", wlmac);
695                                 nvram_unset("lan_hwaddr");
696                                 nvram_unset("wan_hwaddr");
697                                 // fis dlink quirk, by restarting system. utils.c will
698                                 // automaticly assign the et0macaddr then
699                                 nvram_commit();
700                                 eval("event", "5", "1", "15");
701                         }
702                 }
703 #ifdef HAVE_RB500
704                 nvram_set("et0macaddr", nvram_safe_get("lan_hwaddr"));
705 #endif
706 #ifdef HAVE_XSCALE
707 #ifndef HAVE_GATEWORX
708                 nvram_set("et0macaddr", nvram_safe_get("lan_hwaddr"));
709 #endif
710 #endif
711 #ifdef HAVE_MAGICBOX
712                 nvram_set("et0macaddr", nvram_safe_get("lan_hwaddr"));
713 #endif
714 #ifdef HAVE_FONERA
715                 nvram_set("et0macaddr", nvram_safe_get("lan_hwaddr"));
716 #endif
717 #ifdef HAVE_RT2880
718                 nvram_set("et0macaddr", nvram_safe_get("lan_hwaddr"));
719 #endif
720 #ifdef HAVE_LS2
721                 nvram_set("et0macaddr", nvram_safe_get("lan_hwaddr"));
722 #endif
723 #ifdef HAVE_LS5
724                 nvram_set("et0macaddr", nvram_safe_get("lan_hwaddr"));
725 #endif
726 #ifdef HAVE_WHRAG108
727                 nvram_set("et0macaddr", nvram_safe_get("lan_hwaddr"));
728 #endif
729 #ifdef HAVE_PB42
730                 nvram_set("et0macaddr", nvram_safe_get("lan_hwaddr"));
731 #endif
732 #ifdef HAVE_LSX
733                 nvram_set("et0macaddr", nvram_safe_get("lan_hwaddr"));
734 #endif
735 #ifdef HAVE_DANUBE
736                 nvram_set("et0macaddr", nvram_safe_get("lan_hwaddr"));
737 #endif
738 #ifdef HAVE_STORM
739                 nvram_set("et0macaddr", nvram_safe_get("lan_hwaddr"));
740 #endif
741 #ifdef HAVE_ADM5120
742                 nvram_set("et0macaddr", nvram_safe_get("lan_hwaddr"));
743 #endif
744 #ifdef HAVE_TW6600
745                 nvram_set("et0macaddr", nvram_safe_get("lan_hwaddr"));
746 #endif
747 #ifdef HAVE_CA8
748                 nvram_set("et0macaddr", nvram_safe_get("lan_hwaddr"));
749 #endif
750         }
751
752         close(s);
753
754 }
755
756 #ifdef HAVE_3G
757 #define CANBRIDGE() (nvram_match( "wan_proto", "disabled" ) ||  nvram_match( "wan_proto", "3g" ))
758 #else
759 #define CANBRIDGE() nvram_match( "wan_proto", "disabled" )
760 #endif
761
762 void start_lan(void)
763 {
764         if (strlen(nvram_safe_get("wan_default")) > 0) {
765                 PORTSETUPWAN(nvram_safe_get("wan_default"));    // setup
766                 // default
767                 // wan ports,
768                 // or
769                 // reassign
770                 // wan if
771                 // required
772                 // by network
773                 // setup
774                 set_fullswitch();       // for broadcom - add wan to switch ...
775         }
776         struct ifreq ifr;
777         unsigned char mac[20];
778         int s;
779         char eabuf[32];
780
781         if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0)
782                 return;
783
784 #ifdef HAVE_RB500
785         if (getSTA() || getWET() || CANBRIDGE()) {
786                 nvram_set("lan_ifname", "br0");
787                 nvram_set("lan_ifnames",
788                           "eth0 eth1 eth2 eth3 eth4 eth5 eth6 eth7 eth8 ath0 ath1 ath2 ath3 ath4 ath5");
789                 PORTSETUPWAN("");
790         } else {
791                 nvram_set("lan_ifname", "br0");
792                 nvram_set("lan_ifnames",
793                           "eth1 eth2 eth3 eth4 eth5 eth6 eth7 eth8 ath0 ath1 ath2 ath3 ath4 ath5");
794                 PORTSETUPWAN("eth0");
795         }
796
797         strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
798         ioctl(s, SIOCGIFHWADDR, &ifr);
799         nvram_set("et0macaddr", ether_etoa(ifr.ifr_hwaddr.sa_data, eabuf));
800 #endif
801
802 #ifdef HAVE_MAGICBOX
803         if (getSTA() || getWET() || CANBRIDGE()) {
804                 nvram_set("lan_ifname", "br0");
805                 nvram_set("lan_ifnames", "eth0 eth1 ath0");
806                 PORTSETUPWAN("");
807         } else {
808                 nvram_set("lan_ifname", "br0");
809                 nvram_set("lan_ifnames", "eth1 ath0");
810                 PORTSETUPWAN("eth0");
811         }
812
813         strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
814         ioctl(s, SIOCGIFHWADDR, &ifr);
815         nvram_set("et0macaddr", ether_etoa(ifr.ifr_hwaddr.sa_data, eabuf));
816         strcpy(mac, nvram_safe_get("et0macaddr"));
817         MAC_ADD(mac);
818         ether_atoe(mac, ifr.ifr_hwaddr.sa_data);
819         ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
820         strncpy(ifr.ifr_name, "eth1", IFNAMSIZ);
821         ioctl(s, SIOCSIFHWADDR, &ifr);
822 #endif
823 #if defined(HAVE_FONERA) && !defined(HAVE_DIR300) && !defined(HAVE_WRT54G2) && !defined(HAVE_MR3202A)
824         if (getRouterBrand() == ROUTER_BOARD_FONERA2200) {
825                 if (getSTA() || getWET() || CANBRIDGE()) {
826                         nvram_set("lan_ifname", "br0");
827                         nvram_set("lan_ifnames", "vlan0 vlan1 ath0");
828                         PORTSETUPWAN("");
829                 } else {
830                         nvram_set("lan_ifname", "br0");
831                         nvram_set("lan_ifnames", "vlan1 vlan0 ath0");
832                         PORTSETUPWAN("vlan1");
833                 }
834         } else {
835                 if (getSTA() || getWET()
836                     || CANBRIDGE()) {
837                         nvram_set("lan_ifname", "br0");
838                         nvram_set("lan_ifnames", "eth0 ath0");
839                         PORTSETUPWAN("");
840                 } else {
841                         nvram_set("lan_ifname", "br0");
842                         nvram_set("lan_ifnames", "eth0 ath0");
843                         PORTSETUPWAN("eth0");
844                 }
845         }
846         strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
847         ioctl(s, SIOCGIFHWADDR, &ifr);
848         nvram_set("et0macaddr", ether_etoa(ifr.ifr_hwaddr.sa_data, eabuf));
849         strcpy(mac, nvram_safe_get("et0macaddr"));
850 #endif
851 #ifdef HAVE_WRT54G2
852         if (getSTA() || getWET() || CANBRIDGE()) {
853                 nvram_set("lan_ifname", "br0");
854                 nvram_set("lan_ifnames", "vlan1 vlan2 ath0");
855                 PORTSETUPWAN("");
856         } else {
857                 nvram_set("lan_ifname", "br0");
858                 nvram_set("lan_ifnames", "vlan1 vlan2 ath0");
859                 PORTSETUPWAN("vlan2");
860         }
861         strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
862         ioctl(s, SIOCGIFHWADDR, &ifr);
863         nvram_set("et0macaddr", ether_etoa(ifr.ifr_hwaddr.sa_data, eabuf));
864         strcpy(mac, nvram_safe_get("et0macaddr"));
865 #endif
866 #ifdef HAVE_DIR300
867         if (getSTA() || getWET() || CANBRIDGE()) {
868                 nvram_set("lan_ifname", "br0");
869                 nvram_set("lan_ifnames", "vlan0 vlan2 ath0");
870                 PORTSETUPWAN("");
871         } else {
872                 nvram_set("lan_ifname", "br0");
873                 nvram_set("lan_ifnames", "vlan0 vlan2 ath0");
874                 PORTSETUPWAN("vlan2");
875         }
876         strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
877         ioctl(s, SIOCGIFHWADDR, &ifr);
878         nvram_set("et0macaddr", ether_etoa(ifr.ifr_hwaddr.sa_data, eabuf));
879         strcpy(mac, nvram_safe_get("et0macaddr"));
880 #endif
881 #ifdef HAVE_RS
882         if (getSTA() || getWET() || CANBRIDGE()) {
883                 nvram_set("lan_ifname", "br0");
884                 nvram_set("lan_ifnames", "eth0 eth1 ath0 ath1 ath2");
885                 PORTSETUPWAN("");
886         } else {
887                 nvram_set("lan_ifname", "br0");
888                 nvram_set("lan_ifnames", "eth0 eth1 ath0 ath1 ath2");
889                 PORTSETUPWAN("eth0");
890         }
891         strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
892         ioctl(s, SIOCGIFHWADDR, &ifr);
893         nvram_set("et0macaddr", ether_etoa(ifr.ifr_hwaddr.sa_data, eabuf));
894         strcpy(mac, nvram_safe_get("et0macaddr"));
895 #elif HAVE_LSX
896         if (getSTA() || getWET() || CANBRIDGE()) {
897                 nvram_set("lan_ifname", "br0");
898                 nvram_set("lan_ifnames", "eth0 ath0");
899                 PORTSETUPWAN("");
900         } else {
901                 nvram_set("lan_ifname", "br0");
902                 nvram_set("lan_ifnames", "eth0 ath0");
903                 PORTSETUPWAN("eth0");
904         }
905         strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
906         ioctl(s, SIOCGIFHWADDR, &ifr);
907         nvram_set("et0macaddr", ether_etoa(ifr.ifr_hwaddr.sa_data, eabuf));
908         strcpy(mac, nvram_safe_get("et0macaddr"));
909 #endif
910 #ifdef HAVE_DANUBE
911         if (getSTA() || getWET() || CANBRIDGE()) {
912                 nvram_set("lan_ifname", "br0");
913                 nvram_set("lan_ifnames", "eth0 ath0");
914                 PORTSETUPWAN("");
915         } else {
916                 nvram_set("lan_ifname", "br0");
917                 nvram_set("lan_ifnames", "eth0 ath0");
918                 PORTSETUPWAN("eth0");
919         }
920         strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
921         ioctl(s, SIOCGIFHWADDR, &ifr);
922         nvram_set("et0macaddr", ether_etoa(ifr.ifr_hwaddr.sa_data, eabuf));
923         strcpy(mac, nvram_safe_get("et0macaddr"));
924 #endif
925 #ifdef HAVE_RT2880
926         if (getRouterBrand() == ROUTER_BOARD_ECB9750)   // lets load
927         {
928                 if (getSTA() || getWET()
929                     || CANBRIDGE()) {
930                         nvram_set("lan_ifname", "br0");
931                         nvram_set("lan_ifnames", "eth2 ra0");
932                         PORTSETUPWAN("");
933                 } else {
934                         nvram_set("lan_ifname", "br0");
935                         nvram_set("lan_ifnames", "eth2 ra0");
936                         PORTSETUPWAN("eth2");
937                 }
938         } else {
939                 if (getSTA() || getWET()
940                     || CANBRIDGE()) {
941                         nvram_set("lan_ifname", "br0");
942                         nvram_set("lan_ifnames", "vlan1 vlan2 ra0");
943                         PORTSETUPWAN("");
944                 } else {
945                         nvram_set("lan_ifname", "br0");
946                         nvram_set("lan_ifnames", "vlan1 vlan2 ra0");
947                         PORTSETUPWAN("vlan2");
948                 }
949         }
950         strncpy(ifr.ifr_name, "eth2", IFNAMSIZ);
951         ioctl(s, SIOCGIFHWADDR, &ifr);
952         nvram_set("et0macaddr", ether_etoa(ifr.ifr_hwaddr.sa_data, eabuf));
953         strcpy(mac, nvram_safe_get("et0macaddr"));
954 #endif
955 #ifdef HAVE_STORM
956         if (getSTA() || getWET() || CANBRIDGE()) {
957                 nvram_set("lan_ifname", "br0");
958                 nvram_set("lan_ifnames", "eth0 ath0");
959                 PORTSETUPWAN("");
960         } else {
961                 nvram_set("lan_ifname", "br0");
962                 nvram_set("lan_ifnames", "eth0 ath0");
963                 PORTSETUPWAN("eth0");
964         }
965         strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
966         ioctl(s, SIOCGIFHWADDR, &ifr);
967         nvram_set("et0macaddr", ether_etoa(ifr.ifr_hwaddr.sa_data, eabuf));
968         strcpy(mac, nvram_safe_get("et0macaddr"));
969 #endif
970 #ifdef HAVE_ADM5120
971
972         if (getRouterBrand() == ROUTER_BOARD_WP54G
973             || getRouterBrand() == ROUTER_BOARD_NP28G) {
974                 if (getSTA() || getWET()
975                     || CANBRIDGE()) {
976                         nvram_set("lan_ifname", "br0");
977                         nvram_set("lan_ifnames", "eth0 eth1 ath0");
978                         PORTSETUPWAN("");
979                 } else {
980                         nvram_set("lan_ifname", "br0");
981                         nvram_set("lan_ifnames", "eth0 eth1 ath0");
982                         PORTSETUPWAN("eth1");
983                 }
984         } else {
985
986                 if (getSTA() || getWET()
987                     || CANBRIDGE()) {
988                         nvram_set("lan_ifname", "br0");
989                         nvram_set("lan_ifnames", "eth0 ath0");
990                         PORTSETUPWAN("");
991                 } else {
992                         nvram_set("lan_ifname", "br0");
993                         nvram_set("lan_ifnames", "eth0 ath0");
994                         PORTSETUPWAN("eth0");
995                 }
996         }
997
998         strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
999         ioctl(s, SIOCGIFHWADDR, &ifr);
1000         nvram_set("et0macaddr", ether_etoa(ifr.ifr_hwaddr.sa_data, eabuf));
1001         strcpy(mac, nvram_safe_get("et0macaddr"));
1002 #endif
1003 #ifdef HAVE_MR3202A
1004         if (getSTA() || getWET() || CANBRIDGE()) {
1005                 nvram_set("lan_ifname", "br0");
1006                 nvram_set("lan_ifnames", "vlan1 vlan2 ath0");
1007                 PORTSETUPWAN("");
1008         } else {
1009                 nvram_set("lan_ifname", "br0");
1010                 nvram_set("lan_ifnames", "vlan1 vlan2 ath0");
1011                 PORTSETUPWAN("vlan2");
1012         }
1013         strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
1014         ioctl(s, SIOCGIFHWADDR, &ifr);
1015         nvram_set("et0macaddr", ether_etoa(ifr.ifr_hwaddr.sa_data, eabuf));
1016         strcpy(mac, nvram_safe_get("et0macaddr"));
1017 #endif
1018 #ifdef HAVE_LS2
1019         if (getSTA() || getWET() || CANBRIDGE()) {
1020 #if defined(HAVE_NS2) || defined(HAVE_BS2) || defined(HAVE_LC2) || defined(HAVE_BS2HP) || defined(HAVE_MS2) || defined(HAVE_PICO2) || defined(HAVE_PICO2HP)
1021                 nvram_set("lan_ifname", "br0");
1022                 nvram_set("lan_ifnames", "eth0 ath0");
1023                 PORTSETUPWAN("");
1024 #else
1025                 nvram_set("lan_ifname", "br0");
1026                 nvram_set("lan_ifnames", "vlan0 vlan2 ath0");
1027                 PORTSETUPWAN("");
1028 #endif
1029         } else {
1030 #if defined(HAVE_NS2) || defined(HAVE_BS2) || defined(HAVE_LC2) || defined(HAVE_BS2HP) || defined(HAVE_MS2) || defined(HAVE_PICO2) || defined(HAVE_PICO2HP)
1031                 nvram_set("lan_ifname", "br0");
1032                 nvram_set("lan_ifnames", "eth0 ath0");
1033                 PORTSETUPWAN("eth0");
1034 #else
1035                 nvram_set("lan_ifname", "br0");
1036 #ifdef HAVE_BWRG1000
1037                 nvram_set("lan_ifnames", "vlan0 vlan2 ath0");
1038                 PORTSETUPWAN("vlan2");
1039 #else
1040                 nvram_set("lan_ifnames", "vlan0 vlan2 ath0");
1041                 PORTSETUPWAN("vlan0");
1042 #endif
1043 #endif
1044         }
1045
1046         strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
1047         ioctl(s, SIOCGIFHWADDR, &ifr);
1048         nvram_set("et0macaddr", ether_etoa(ifr.ifr_hwaddr.sa_data, eabuf));
1049         strcpy(mac, nvram_safe_get("et0macaddr"));
1050 #endif
1051 #ifdef HAVE_LS5
1052         if (getSTA() || getWET() || CANBRIDGE()) {
1053                 nvram_set("lan_ifname", "br0");
1054                 nvram_set("lan_ifnames", "eth0 ath0");
1055                 PORTSETUPWAN("");
1056         } else {
1057                 nvram_set("lan_ifname", "br0");
1058                 nvram_set("lan_ifnames", "eth0 ath0");
1059                 PORTSETUPWAN("eth0");
1060         }
1061
1062         strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
1063         ioctl(s, SIOCGIFHWADDR, &ifr);
1064         nvram_set("et0macaddr", ether_etoa(ifr.ifr_hwaddr.sa_data, eabuf));
1065         strcpy(mac, nvram_safe_get("et0macaddr"));
1066 #endif
1067 #ifdef HAVE_TW6600
1068         if (getSTA() || getWET() || CANBRIDGE()) {
1069                 nvram_set("lan_ifname", "br0");
1070                 nvram_set("lan_ifnames", "eth0 ath0 ath1");
1071                 PORTSETUPWAN("");
1072         } else {
1073                 nvram_set("lan_ifname", "br0");
1074                 nvram_set("lan_ifnames", "eth0 ath0 ath1");
1075                 PORTSETUPWAN("eth0");
1076         }
1077
1078         strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
1079         ioctl(s, SIOCGIFHWADDR, &ifr);
1080         nvram_set("et0macaddr", ether_etoa(ifr.ifr_hwaddr.sa_data, eabuf));
1081         strcpy(mac, nvram_safe_get("et0macaddr"));
1082 #endif
1083 #ifdef HAVE_PB42
1084         if (getSTA() || getWET() || CANBRIDGE()) {
1085                 nvram_set("lan_ifname", "br0");
1086                 nvram_set("lan_ifnames", "eth0 eth1 ath0 ath1");
1087                 PORTSETUPWAN("");
1088         } else {
1089                 nvram_set("lan_ifname", "br0");
1090                 nvram_set("lan_ifnames", "eth0 eth1 ath0 ath1");
1091                 PORTSETUPWAN("eth0");
1092         }
1093
1094         strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
1095         ioctl(s, SIOCGIFHWADDR, &ifr);
1096         nvram_set("et0macaddr", ether_etoa(ifr.ifr_hwaddr.sa_data, eabuf));
1097         strcpy(mac, nvram_safe_get("et0macaddr"));
1098 #endif
1099 #ifdef HAVE_WHRAG108
1100         if (getSTA() || getWET() || CANBRIDGE()) {
1101                 nvram_set("lan_ifname", "br0");
1102                 nvram_set("lan_ifnames", "eth0 eth1 ath0 ath1");
1103                 PORTSETUPWAN("");
1104         } else {
1105                 nvram_set("lan_ifname", "br0");
1106                 nvram_set("lan_ifnames", "eth0 eth1 ath0 ath1");
1107                 PORTSETUPWAN("eth1");
1108         }
1109
1110         strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
1111         ioctl(s, SIOCGIFHWADDR, &ifr);
1112         nvram_set("et0macaddr", ether_etoa(ifr.ifr_hwaddr.sa_data, eabuf));
1113         strcpy(mac, nvram_safe_get("et0macaddr"));
1114 #endif
1115 #ifdef HAVE_CA8
1116         if (getSTA() || getWET() || CANBRIDGE()) {
1117                 if (getRouterBrand() == ROUTER_BOARD_CA8PRO) {
1118                         nvram_set("lan_ifname", "br0");
1119                         nvram_set("lan_ifnames", "vlan0 vlan1 ath0");
1120                         PORTSETUPWAN("");
1121                 } else if (getRouterBrand() == ROUTER_BOARD_RCAA01) {
1122                         nvram_set("lan_ifname", "br0");
1123                         nvram_set("lan_ifnames", "vlan0 vlan1 ath0 ath1");
1124                         PORTSETUPWAN("");
1125                 } else if (getRouterBrand() == ROUTER_BOARD_RDAT81) {
1126                         nvram_set("lan_ifname", "br0");
1127                         nvram_set("lan_ifnames", "eth0 ath0 ath1");
1128                         PORTSETUPWAN("");
1129                 } else {
1130                         nvram_set("lan_ifname", "br0");
1131                         nvram_set("lan_ifnames", "eth0 ath0");
1132                         PORTSETUPWAN("");
1133                 }
1134         } else {
1135                 if (getRouterBrand() == ROUTER_BOARD_CA8PRO) {
1136                         nvram_set("lan_ifname", "br0");
1137                         nvram_set("lan_ifnames", "vlan0 vlan1 ath0");
1138                         PORTSETUPWAN("vlan1");
1139                 } else if (getRouterBrand() == ROUTER_BOARD_RCAA01) {
1140                         nvram_set("lan_ifname", "br0");
1141                         nvram_set("lan_ifnames", "vlan0 vlan1 ath0 ath1");
1142                         PORTSETUPWAN("vlan1");
1143                 } else if (getRouterBrand() == ROUTER_BOARD_RDAT81) {
1144                         nvram_set("lan_ifname", "br0");
1145                         nvram_set("lan_ifnames", "eth0 ath0 ath1");
1146                         PORTSETUPWAN("eth0");
1147                 } else {
1148                         nvram_set("lan_ifname", "br0");
1149                         nvram_set("lan_ifnames", "eth0 ath0");
1150                         PORTSETUPWAN("eth0");
1151                 }
1152         }
1153
1154         strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
1155         ioctl(s, SIOCGIFHWADDR, &ifr);
1156         nvram_set("et0macaddr", ether_etoa(ifr.ifr_hwaddr.sa_data, eabuf));
1157         strcpy(mac, nvram_safe_get("et0macaddr"));
1158 #endif
1159 #ifdef HAVE_GATEWORX
1160         if (getSTA() || getWET() || CANBRIDGE()) {
1161                 if (getRouterBrand() == ROUTER_BOARD_GATEWORX_SWAP) {
1162                         nvram_set("lan_ifname", "br0");
1163                         if (nvram_match("intel_eth", "1"))
1164                                 nvram_set("lan_ifnames",
1165                                           "ixp0 eth0 eth1 ath0 ath1 ath2 ath3 ofdm");
1166                         else
1167                                 nvram_set("lan_ifnames",
1168                                           "ixp0 ath0 ath1 ath2 ath3 ofdm");
1169                         PORTSETUPWAN("");
1170                 } else if (getRouterBrand() == ROUTER_BOARD_GATEWORX_GW2345) {
1171                         nvram_set("lan_ifname", "br0");
1172                         if (nvram_match("intel_eth", "1"))
1173                                 nvram_set("lan_ifnames",
1174                                           "ixp0 ixp1 eth0 eth1 ath0 ath1 ath2 ath3 ofdm");
1175                         else
1176                                 nvram_set("lan_ifnames",
1177                                           "ixp0 ixp1 ath0 ath1 ath2 ath3 ofdm");
1178                         PORTSETUPWAN("");
1179                 } else {
1180                         nvram_set("lan_ifname", "br0");
1181                         if (nvram_match("intel_eth", "1"))
1182                                 nvram_set("lan_ifnames",
1183                                           "ixp0 ixp1 eth0 eth1 ath0 ath1 ath2 ath3 ofdm");
1184                         else
1185                                 nvram_set("lan_ifnames",
1186                                           "ixp0 ixp1 ath0 ath1 ath2 ath3 ofdm");
1187                         PORTSETUPWAN("");
1188                 }
1189         } else {
1190                 if (getRouterBrand() == ROUTER_BOARD_GATEWORX_SWAP) {
1191                         nvram_set("lan_ifname", "br0");
1192                         if (nvram_match("intel_eth", "1"))
1193                                 nvram_set("lan_ifnames",
1194                                           "eth0 eth1 ixp0 ath0 ath1 ath2 ath3 ofdm");
1195                         else
1196                                 nvram_set("lan_ifnames",
1197                                           "ixp0 ath0 ath1 ath2 ath3 ofdm");
1198                         PORTSETUPWAN("ixp0");
1199                 } else if (getRouterBrand() == ROUTER_BOARD_GATEWORX_GW2345) {
1200                         nvram_set("lan_ifname", "br0");
1201                         if (nvram_match("intel_eth", "1"))
1202                                 nvram_set("lan_ifnames",
1203                                           "eth0 eth1 ixp0 ixp1 ath0 ath1 ath2 ath3 ofdm");
1204                         else
1205                                 nvram_set("lan_ifnames",
1206                                           "ixp0 ixp1 ath0 ath1 ath2 ath3 ofdm");
1207                         PORTSETUPWAN("ixp1");
1208                 } else {
1209 #ifdef HAVE_XIOCOM
1210                         nvram_set("lan_ifname", "br0");
1211                         if (nvram_match("intel_eth", "1"))
1212                                 nvram_set("lan_ifnames",
1213                                           "eth0 eth1 ixp0 ixp1 ath0 ath1 ath2 ath3 ofdm");
1214                         else
1215                                 nvram_set("lan_ifnames",
1216                                           "ixp0 ixp1 ath0 ath1 ath2 ath3 ofdm");
1217
1218                         PORTSETUPWAN("ixp0");
1219
1220 #else
1221                         nvram_set("lan_ifname", "br0");
1222                         if (nvram_match("intel_eth", "1"))
1223                                 nvram_set("lan_ifnames",
1224                                           "eth0 eth1 ixp0 ixp1 ath0 ath1 ath2 ath3 ofdm");
1225                         else
1226                                 nvram_set("lan_ifnames",
1227                                           "ixp0 ixp1 ath0 ath1 ath2 ath3 ofdm");
1228
1229                         PORTSETUPWAN("ixp1");
1230 #endif
1231                 }
1232         }
1233         strncpy(ifr.ifr_name, "ixp0", IFNAMSIZ);
1234         if (ioctl(s, SIOCGIFHWADDR, &ifr) != 0) {
1235                 strncpy(ifr.ifr_name, "ixp0", IFNAMSIZ);
1236                 ioctl(s, SIOCGIFHWADDR, &ifr);
1237         }
1238         nvram_set("et0macaddr", ether_etoa(ifr.ifr_hwaddr.sa_data, eabuf));
1239         /*
1240          * strncpy (ifr.ifr_name, "ixp1", IFNAMSIZ); ioctl (s, SIOCGIFHWADDR,
1241          * &ifr); nvram_set ("et0macaddr", ether_etoa (ifr.ifr_hwaddr.sa_data,
1242          * eabuf)); strcpy (mac, nvram_safe_get ("et0macaddr")); MAC_ADD (mac);
1243          * ether_atoe (mac, ifr.ifr_hwaddr.sa_data); ifr.ifr_hwaddr.sa_family =
1244          * ARPHRD_ETHER; strncpy (ifr.ifr_name, "eth1", IFNAMSIZ); ioctl (s,
1245          * SIOCSIFHWADDR, &ifr);
1246          */
1247 #endif
1248 #ifdef HAVE_X86
1249         if (getSTA() || getWET()) {
1250                 nvram_set("lan_ifname", "br0");
1251 #ifdef HAVE_NOWIFI
1252                 nvram_set("lan_ifnames",
1253                           "eth0 eth1 eth2 eth3 eth4 eth5 eth6 eth7 eth8 eth9 eth10");
1254 #else
1255                 if (nvram_match("wifi_bonding", "1"))
1256                         nvram_set("lan_ifnames",
1257                                   "eth0 eth1 eth2 eth3 eth4 eth5 eth6 eth7 eth8 eth9 eth10 bond0");
1258                 else
1259                         nvram_set("lan_ifnames",
1260                                   "eth0 eth1 eth2 eth3 eth4 eth5 eth6 eth7 eth8 eth9 eth10 ath0 ath1 ath2 ath3 ath4 ath5 ath6 ath7 ath8");
1261
1262 #endif
1263                 PORTSETUPWAN("");
1264         } else if (CANBRIDGE()) {
1265                 nvram_set("lan_ifname", "br0");
1266 #ifdef HAVE_NOWIFI
1267                 nvram_set("lan_ifnames",
1268                           "eth0 eth1 eth2 eth3 eth4 eth5 eth6 eth7 eth8 eth9 eth10");
1269 #else
1270                 if (nvram_match("wifi_bonding", "1"))
1271                         nvram_set("lan_ifnames",
1272                                   "eth0 eth1 eth2 eth3 eth4 eth5 eth6 eth7 eth8 eth9 eth10 bond0");
1273                 else
1274                         nvram_set("lan_ifnames",
1275                                   "eth0 eth1 eth2 eth3 eth4 eth5 eth6 eth7 eth8 eth9 eth10 ath0 ath1 ath2 ath3 ath4 ath5 ath6 ath7 ath8");
1276 #endif
1277                 PORTSETUPWAN("");
1278         } else {
1279                 nvram_set("lan_ifname", "br0");
1280 #ifdef HAVE_NOWIFI
1281                 nvram_set("lan_ifnames",
1282                           "eth0 eth1 eth2 eth3 eth4 eth5 eth6 eth7 eth8 eth9 eth10");
1283 #else
1284                 if (nvram_match("wifi_bonding", "1"))
1285                         nvram_set("lan_ifnames",
1286                                   "eth0 eth1 eth2 eth3 eth4 eth5 eth6 eth7 eth8 eth9 eth10 bond0");
1287                 else
1288                         nvram_set("lan_ifnames",
1289                                   "eth0 eth1 eth2 eth3 eth4 eth5 eth6 eth7 eth8 eth9 eth10 ath0 ath1 ath2 ath3 ath4 ath5 ath6 ath7 ath8");
1290 #endif
1291 #ifdef HAVE_GW700
1292                 PORTSETUPWAN("eth1");
1293 #else
1294                 PORTSETUPWAN("eth0");
1295 #endif
1296
1297         }
1298         strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
1299         ioctl(s, SIOCGIFHWADDR, &ifr);
1300         nvram_set("et0macaddr", ether_etoa(ifr.ifr_hwaddr.sa_data, eabuf));
1301
1302         /*
1303          * strncpy (ifr.ifr_name, "ixp1", IFNAMSIZ); ioctl (s, SIOCGIFHWADDR,
1304          * &ifr); nvram_set ("et0macaddr", ether_etoa (ifr.ifr_hwaddr.sa_data,
1305          * eabuf)); strcpy (mac, nvram_safe_get ("et0macaddr")); MAC_ADD (mac);
1306          * ether_atoe (mac, ifr.ifr_hwaddr.sa_data); ifr.ifr_hwaddr.sa_family =
1307          * ARPHRD_ETHER; strncpy (ifr.ifr_name, "eth1", IFNAMSIZ); ioctl (s,
1308          * SIOCSIFHWADDR, &ifr);
1309          */
1310 #endif
1311         char *lan_ifname = strdup(nvram_safe_get("lan_ifname"));
1312         char *wan_ifname = strdup(nvram_safe_get("wan_ifname"));
1313         char *lan_ifnames = strdup(nvram_safe_get("lan_ifnames"));
1314         char name[80], *next, *svbuf;
1315         char realname[80];
1316         char wl_face[10];
1317
1318         strcpy(lan_ifname, nvram_safe_get("lan_ifname"));
1319         strcpy(wan_ifname, nvram_safe_get("wan_ifname"));
1320         strcpy(lan_ifnames, nvram_safe_get("lan_ifnames"));
1321
1322         cprintf("%s\n", lan_ifname);
1323
1324         // If running in client-mode, remove old WAN-configuration
1325         if (getSTA()) {
1326                 // #ifdef HAVE_SKYTRON
1327                 // ifconfig(wan_ifname,IFUP,"172.16.1.1","255.255.255.0");
1328                 // #else
1329                 ifconfig(wan_ifname, IFUP, "0.0.0.0", NULL);
1330                 // #endif
1331
1332         }
1333         // find wireless interface
1334         diag_led(DIAG, STOP_LED);       // stop that blinking
1335         strcpy(wl_face, get_wdev());
1336 #if defined(HAVE_MADWIFI) || defined(HAVE_RT2880)
1337 #ifndef HAVE_NOWIFI
1338         deconfigure_wifi();
1339 #endif
1340 #else
1341         eval("wlconf", wl_face, "down");
1342 #endif
1343 #ifdef HAVE_WAVESAT
1344         deconfigure_wimax();
1345 #endif
1346
1347         /*
1348          * you gotta bring it down before you can set its MAC
1349          */
1350         cprintf("configure wl_face %s\n", wl_face);
1351         ifconfig(wl_face, 0, 0, 0);
1352 #if !defined(HAVE_MADWIFI) && !defined(HAVE_RT2880)
1353
1354         if (nvram_match("mac_clone_enable", "1") &&
1355             nvram_invmatch("def_whwaddr", "00:00:00:00:00:00") &&
1356             nvram_invmatch("def_whwaddr", "")) {
1357                 ether_atoe(nvram_safe_get("def_whwaddr"),
1358                            ifr.ifr_hwaddr.sa_data);
1359
1360         } else {
1361                 getWirelessMac(mac);
1362
1363                 ether_atoe(mac, ifr.ifr_hwaddr.sa_data);
1364
1365                 if (nvram_match("wl0_hwaddr", "") || !nvram_get("wl0_hwaddr")) {
1366                         nvram_set("wl0_hwaddr", mac);
1367                         nvram_commit();
1368                 }
1369         }
1370         /*
1371          * Write wireless mac
1372          */
1373         cprintf("Write wireless mac\n");
1374         ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
1375         strncpy(ifr.ifr_name, wl_face, IFNAMSIZ);
1376
1377         eval("wl", "-i", wl_face, "down");
1378         if (ioctl(s, SIOCSIFHWADDR, &ifr) == -1)
1379                 perror("Write wireless mac fail : ");
1380         else
1381                 cprintf("Write wireless mac successfully\n");
1382         eval("wl", "-i", wl_face, "up");
1383         start_config_macs(wl_face);
1384 #endif
1385         if (getSTA()) {
1386                 unsigned char mac[20];
1387
1388                 getWANMac(mac);
1389
1390                 nvram_set("wan_hwaddr", mac);
1391         }
1392
1393         cprintf("wl_face up %s\n", wl_face);
1394         ifconfig(wl_face, IFUP, 0, 0);
1395 #ifdef HAVE_MICRO
1396         br_init();
1397 #endif
1398         /*
1399          * Bring up bridged interface
1400          */
1401 #ifdef HAVE_EAD
1402         char eadline[64];
1403
1404         memset(eadline, 0, 64);
1405 #endif
1406
1407         if (strncmp(lan_ifname, "br0", 3) == 0) {
1408                 br_add_bridge(lan_ifname);
1409                 if (nvram_match("lan_stp", "0"))
1410                         br_set_stp_state(lan_ifname, 0);
1411                 else
1412                         br_set_stp_state(lan_ifname, 1);
1413 #ifdef HAVE_MICRO
1414                 br_set_bridge_forward_delay(lan_ifname, 1);
1415 #else
1416                 br_set_bridge_forward_delay(lan_ifname, 1);
1417 #endif
1418 #ifdef HAVE_EAD
1419                 eval("killall", "-9", "ead");
1420 #endif
1421                 foreach(name, lan_ifnames, next) {
1422                         if (!ifexists(name))
1423                                 continue;
1424 #ifdef HAVE_EAD
1425 #if defined(HAVE_RT2880) || defined(HAVE_MADWIFI)
1426                         if (strncmp(name, "ath", 3) && strncmp(name, "ra", 2))
1427 #else
1428                         if (wl_probe(name))
1429 #endif
1430                                 sprintf(eadline, "%s%s %s ", eadline, "-d",
1431                                         name);
1432 #endif
1433                         if (nvram_match("wan_ifname", name))
1434                                 continue;
1435 #if defined(HAVE_MADWIFI) && !defined(HAVE_RB500) && !defined(HAVE_XSCALE) && !defined(HAVE_MAGICBOX) && !defined(HAVE_FONERA) && !defined(HAVE_WHRAG108) && !defined(HAVE_X86) && !defined(HAVE_LS2) && !defined(HAVE_LS5) && !defined(HAVE_CA8) && !defined(HAVE_TW6600) && !defined(HAVE_PB42) && !defined(HAVE_LSX) && !defined(HAVE_DANUBE) && !defined(HAVE_STORM) && !defined(HAVE_ADM5120) && !defined(HAVE_RT2880)
1436                         if (!strcmp(name, "eth2")) {
1437                                 strcpy(realname, "ath0");
1438                         } else
1439 #endif
1440                                 strcpy(realname, name);
1441
1442                         cprintf("name=[%s] lan_ifname=[%s]\n", realname,
1443                                 lan_ifname);
1444
1445                         /*
1446                          * Bring up interface
1447                          */
1448                         if (ifconfig(realname, IFUP, "0.0.0.0", NULL))
1449                                 continue;
1450
1451                         // set proper mtu
1452                         eval("ifconfig", realname, "mtu", getMTU(realname));
1453
1454                         /*
1455                          * Set the logical bridge address to that of the first interface
1456                          */
1457
1458 #if !defined(HAVE_MADWIFI) && !defined(HAVE_RT2880)
1459                         strncpy(ifr.ifr_name, lan_ifname, IFNAMSIZ);
1460                         if (ioctl(s, SIOCGIFHWADDR, &ifr) == 0 &&
1461                             memcmp(ifr.ifr_hwaddr.sa_data, "\0\0\0\0\0\0",
1462                                    ETHER_ADDR_LEN) == 0
1463                             && strcmp(wl_face, realname) == 0) {
1464                                 strncpy(ifr.ifr_name, realname, IFNAMSIZ);
1465                                 if (ioctl(s, SIOCGIFHWADDR, &ifr) == 0) {
1466                                         strncpy(ifr.ifr_name, lan_ifname,
1467                                                 IFNAMSIZ);
1468                                         ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
1469                                         ioctl(s, SIOCSIFHWADDR, &ifr);
1470                                         cprintf("=====> set %s hwaddr to %s\n",
1471                                                 lan_ifname, realname);
1472                                 } else
1473                                         perror(lan_ifname);
1474                         } else
1475                                 perror(lan_ifname);
1476 #endif
1477                         /*
1478                          * If not a wl i/f then simply add it to the bridge
1479                          */
1480 #if !defined(HAVE_MADWIFI) && !defined(HAVE_RT2880)
1481                         if (wlconf_up(name)) {
1482                                 // #ifdef HAVE_PORTSETUP
1483                                 do_portsetup(lan_ifname, name);
1484                                 // #else
1485                                 // br_add_interface (getBridge (name), name);
1486                                 // #endif
1487                         } else {
1488
1489                                 if (nvram_match("mac_clone_enable", "1") &&
1490                                     nvram_invmatch("def_whwaddr",
1491                                                    "00:00:00:00:00:00")
1492                                     && nvram_invmatch("def_whwaddr", "")) {
1493                                         ether_atoe(nvram_safe_get
1494                                                    ("def_whwaddr"),
1495                                                    ifr.ifr_hwaddr.sa_data);
1496
1497                                 } else {
1498                                         getWirelessMac(mac);
1499
1500                                         ether_atoe(mac, ifr.ifr_hwaddr.sa_data);
1501                                         int instance = get_wl_instance(name);
1502
1503                                         if (instance == -1)
1504                                                 continue;       // no wireless device
1505                                         if (nvram_nmatch
1506                                             ("", "wl%d_hwaddr", instance)
1507                                             || !nvram_nget("wl%d_hwaddr",
1508                                                            instance)) {
1509                                                 nvram_nset(mac, "wl%d_hwaddr",
1510                                                            instance);
1511                                                 nvram_commit();
1512                                                 ifr.ifr_hwaddr.sa_family =
1513                                                     ARPHRD_ETHER;
1514                                                 strncpy(ifr.ifr_name, name,
1515                                                         IFNAMSIZ);
1516
1517                                                 eval("wl", "-i", name, "down");
1518                                                 if (ioctl
1519                                                     (s, SIOCSIFHWADDR,
1520                                                      &ifr) == -1)
1521                                                         perror
1522                                                             ("Write wireless mac fail : ");
1523                                                 else
1524                                                         cprintf
1525                                                             ("Write wireless mac successfully\n");
1526                                                 eval("wl", "-i", name, "up");
1527                                                 start_config_macs(name);
1528                                         }
1529                                 }
1530
1531 #else
1532                         cprintf("configure %s\n", name);
1533                         if (strcmp(name, "wl0"))        // check if the interface is a
1534                                 // buffalo wireless
1535                         {
1536                                 do_portsetup(lan_ifname, name);
1537                         } else {
1538
1539 #endif
1540                                 /*
1541                                  * get the instance number of the wl i/f
1542                                  */
1543                                 char wl_name[] = "wlXXXXXXXXXX_mode";
1544                                 int unit;
1545
1546 #if defined(HAVE_MADWIFI) || defined(HAVE_RT2880)
1547                                 unit = 0;
1548 #else
1549                                 wl_ioctl(name, WLC_GET_INSTANCE, &unit,
1550                                          sizeof(unit));
1551 #endif
1552                                 snprintf(wl_name, sizeof(wl_name), "wl%d_mode",
1553                                          unit);
1554                                 /*
1555                                  * Do not attach the main wl i/f if in wds or client/adhoc
1556                                  */
1557
1558                                 led_control(LED_BRIDGE, LED_OFF);
1559                                 if (nvram_match(wl_name, "wet")
1560                                     || nvram_match(wl_name, "apstawet")) {
1561                                         ifconfig(name, IFUP | IFF_ALLMULTI, NULL, NULL);        // from
1562                                         // up
1563                                         br_add_interface(getBridge(IFMAP(name)),
1564                                                          name);
1565                                         led_control(LED_BRIDGE, LED_ON);
1566                                         /* Enable host DHCP relay */
1567                                         if (nvram_match("lan_dhcp", "1")) {
1568                                                 wl_iovar_set(name,
1569                                                              "wet_host_mac",
1570                                                              ifr.
1571                                                              ifr_hwaddr.sa_data,
1572                                                              ETHER_ADDR_LEN);
1573                                         }
1574                                         /* Enable WET DHCP relay if requested */
1575                                         if (nvram_match("dhcp_relay", "1"))     // seems to fix some dhcp problems, also Netgear does it this way
1576                                         {
1577                                                 enable_dhcprelay(lan_ifname);
1578                                         }
1579                                         do_mssid(name);
1580                                 }
1581 #ifdef HAVE_WAVESAT
1582                                 if (nvram_match(wl_name, "bridge")) {
1583                                         ifconfig(name, IFUP | IFF_ALLMULTI, NULL, NULL);        // from
1584                                         // up
1585                                         br_add_interface(getBridge(IFMAP(name)),
1586                                                          name);
1587                                         led_control(LED_BRIDGE, LED_ON);
1588                                 }
1589 #endif
1590
1591                                 if (nvram_match(wl_name, "ap")) {
1592
1593                                         do_portsetup(lan_ifname, name);
1594                                         // br_add_interface (getBridge (name), name); //eval
1595                                         // ("brctl", "addif", lan_ifname, name);
1596                                         do_mssid(name);
1597                                 }
1598                                 if (nvram_match(wl_name, "apsta")) {
1599 #if !defined(HAVE_MADWIFI) && !defined(HAVE_RT2880)
1600                                         // eval ("wl", "ap", "0");
1601                                         eval("wl", "-i", name, "ap", "0");
1602                                         // eval ("wl", "infra", "1");
1603                                         eval("wl", "-i", name, "infra", "1");
1604                                         wl_ioctl(wl_name, WLC_SCAN, svbuf,
1605                                                  sizeof(svbuf));
1606                                         wlconf_up(name);
1607 #endif
1608                                         // eval("wlconf", name, "up");
1609                                         ifconfig(name, IFUP | IFF_ALLMULTI,
1610                                                  NULL, NULL);
1611 #if !defined(HAVE_MADWIFI) && !defined(HAVE_RT2880)
1612                                         // eval ("wl", "ap", "0");
1613                                         eval("wl", "-i", name, "ap", "0");
1614                                         // eval ("wl", "ssid", nvram_get ("wl0_ssid"));
1615                                         eval("wl", "-i", name, "ssid",
1616                                              nvram_nget("wl%d_ssid",
1617                                                         get_wl_instance(name)));
1618                                         // eval ("brctl", "addif", lan_ifname, name);
1619 #ifndef HAVE_FON
1620                                         if (nvram_match("fon_enable", "0"))
1621                                                 do_mssid(name);
1622 #endif
1623 #endif
1624                                 }
1625
1626                                 /*
1627                                  * if client/wet mode, turn off ap mode et al
1628                                  */
1629                                 if (nvram_match(wl_name, "infra")) {
1630 #if !defined(HAVE_MADWIFI) && !defined(HAVE_RT2880)
1631                                         // eval ("wl", "ap", "0");
1632                                         eval("wl", "-i", name, "ap", "0");
1633                                         // eval ("wl", "infra", "0");
1634                                         eval("wl", "-i", name, "infra", "0");
1635                                         wl_ioctl(wl_name, WLC_SCAN, svbuf,
1636                                                  sizeof(svbuf));
1637                                         wlconf_up(name);
1638 #endif
1639                                         // eval ("wl", "infra", "0");
1640                                         eval("wl", "-i", name, "infra", "0");
1641                                         // eval ("wl", "ssid", nvram_safe_get ("wl0_ssid"));
1642                                         ifconfig(name, IFUP | IFF_ALLMULTI,
1643                                                  NULL, NULL);
1644                                         eval("wl", "-i", name, "ssid",
1645                                              nvram_nget("wl%d_ssid",
1646                                                         get_wl_instance(name)));
1647                                         do_portsetup(lan_ifname, name);
1648                                 }
1649
1650                                 if (nvram_match(wl_name, "sta")) {
1651 #if !defined(HAVE_MADWIFI) && !defined(HAVE_RT2880)
1652                                         // eval ("wl", "ap", "0");
1653                                         eval("wl", "-i", name, "ap", "0");
1654                                         // eval ("wl", "infra", "1");
1655                                         eval("wl", "-i", name, "infra", "1");
1656                                         wlconf_up(name);
1657                                         wl_ioctl(name, WLC_SCAN, svbuf,
1658                                                  sizeof(svbuf));
1659 #endif
1660                                         // eval("wlconf", name, "up");
1661                                         ifconfig(name, IFUP | IFF_ALLMULTI,
1662                                                  NULL, NULL);
1663 #if !defined(HAVE_MADWIFI) && !defined(HAVE_RT2880)
1664                                         // eval ("wl", "ap", "0");
1665                                         eval("wl", "-i", name, "ap", "0");
1666                                         // eval ("wl", "ssid", nvram_get ("wl0_ssid"));
1667                                         eval("wl", "-i", name, "ssid",
1668                                              nvram_nget("wl%d_ssid",
1669                                                         get_wl_instance(name)));
1670 #endif
1671                                 }
1672 #ifdef HAVE_WAVESAT
1673                                 if (nvram_match(wl_name, "router")) {
1674
1675                                         do_portsetup(lan_ifname, name);
1676                                         // br_add_interface (getBridge (name), name); //eval
1677                                         // ("brctl", "addif", lan_ifname, name);
1678                                         do_mssid(name);
1679                                 }
1680 #endif
1681
1682                         }
1683
1684                 }
1685         }
1686
1687         free(lan_ifname);
1688         free(wan_ifname);
1689         free(lan_ifnames);
1690 #ifdef HAVE_EAD
1691         if (strlen(eadline) > 0)
1692                 sysprintf("ead %s -B", eadline);
1693 #endif
1694 #if defined(HAVE_MADWIFI) || defined(HAVE_RT2880)
1695 #ifndef HAVE_NOWIFI
1696         if (nvram_match("mac_clone_enable", "1") &&
1697             nvram_invmatch("def_hwaddr", "00:00:00:00:00:00") &&
1698             nvram_invmatch("def_hwaddr", "")) {
1699                 ether_atoe(nvram_safe_get("def_whwaddr"),
1700                            ifr.ifr_hwaddr.sa_data);
1701                 ifr.ifr_hwaddr.sa_family = ARPHRD_IEEE80211;
1702                 char *ifs = getSTA();
1703                 char *wifi = NULL;
1704
1705                 if (ifs)
1706                         wifi = getWifi(ifs);
1707                 if (wifi) {
1708                         strncpy(ifr.ifr_name, wifi, IFNAMSIZ);
1709                         eval("ifconfig", wifi, "down");
1710                         if (ioctl(s, SIOCSIFHWADDR, &ifr) == -1)
1711                                 perror("Write wireless mac fail : ");
1712                         else
1713                                 cprintf("Write wireless mac successfully\n");
1714                         eval("ifconfig", wifi, "up");
1715                 }
1716         }
1717         if (nvram_match("mac_clone_enable", "1") &&
1718             nvram_invmatch("def_whwaddr", "00:00:00:00:00:00") &&
1719             nvram_invmatch("def_whwaddr", "")) {
1720                 ether_atoe(nvram_safe_get("def_whwaddr"),
1721                            ifr.ifr_hwaddr.sa_data);
1722                 ifr.ifr_hwaddr.sa_family = ARPHRD_IEEE80211;
1723                 char *ifs = getWDSSTA();
1724                 char *wifi = NULL;
1725
1726                 if (!ifs)
1727                         ifs = getWET();
1728                 if (ifs)
1729                         wifi = getWifi(ifs);
1730                 if (wifi) {
1731                         strncpy(ifr.ifr_name, wifi, IFNAMSIZ);
1732                         eval("ifconfig", wifi, "down");
1733                         if (ioctl(s, SIOCSIFHWADDR, &ifr) == -1)
1734                                 perror("Write wireless mac fail : ");
1735                         else
1736                                 cprintf("Write wireless mac successfully\n");
1737                         eval("ifconfig", wifi, "up");
1738                 }
1739         }
1740         configure_wifi();
1741 #endif
1742 #endif
1743 #ifdef HAVE_WAVESAT
1744         configure_wimax();
1745 #endif
1746         lan_ifname = strdup(nvram_safe_get("lan_ifname"));
1747         lan_ifnames = strdup(nvram_safe_get("lan_ifnames"));
1748
1749         /*
1750          * specific non-bridged lan i/f
1751          */
1752         if (strcmp(lan_ifname, "")) {   // FIXME
1753                 /*
1754                  * Bring up interface
1755                  */
1756                 ifconfig(lan_ifname, IFUP, NULL, NULL);
1757 #if !defined(HAVE_MADWIFI) && !defined(HAVE_RT2880)
1758                 /*
1759                  * config wireless i/f
1760                  */
1761                 if (!wlconf_up(lan_ifname)) {
1762                         char tmp[100], prefix[] = "wanXXXXXXXXXX_";
1763                         int unit;
1764
1765                         /*
1766                          * get the instance number of the wl i/f
1767                          */
1768                         wl_ioctl(lan_ifname, WLC_GET_INSTANCE, &unit,
1769                                  sizeof(unit));
1770                         snprintf(prefix, sizeof(prefix), "wl%d_", unit);
1771                         /*
1772                          * Receive all multicast frames in WET mode
1773                          */
1774                         if (nvram_match(strcat_r(prefix, "mode", tmp), "sta"))
1775                                 ifconfig(lan_ifname, IFUP | IFF_ALLMULTI, NULL,
1776                                          NULL);
1777                         if (nvram_match(strcat_r(prefix, "mode", tmp), "apsta"))
1778                                 ifconfig(lan_ifname, IFUP | IFF_ALLMULTI, NULL,
1779                                          NULL);
1780
1781                 }
1782 #endif
1783
1784         }
1785
1786         /*
1787          * Bring up and configure LAN interface
1788          */
1789         ifconfig(lan_ifname, IFUP, nvram_safe_get("lan_ipaddr"),
1790                  nvram_safe_get("lan_netmask"));
1791
1792         char staticlan[32];
1793
1794         sprintf(staticlan, "%s:0", lan_ifname);
1795 #if defined(HAVE_FONERA) || defined(HAVE_CA8) && !defined(HAVE_MR3202A)
1796         if (getRouterBrand() != ROUTER_BOARD_FONERA2200
1797             && getRouterBrand() != ROUTER_BOARD_CA8PRO
1798             && getRouterBrand() != ROUTER_BOARD_RCAA01)
1799                 if (nvram_match("ath0_mode", "sta")
1800                     || nvram_match("ath0_mode", "wdssta")
1801                     || nvram_match("ath0_mode", "wet")
1802                     || CANBRIDGE()) {
1803 #endif
1804
1805                         eval("ifconfig", "eth0:0", "down");
1806                         // add fallback ip
1807                         eval("ifconfig", staticlan, "169.254.255.1", "netmask",
1808                              "255.255.0.0");
1809
1810 #if defined(HAVE_FONERA) || defined(HAVE_CA8) && !defined(HAVE_MR3202A)
1811                 } else
1812                         eval("ifconfig", staticlan, "0.0.0.0", "down");
1813 #endif
1814         close(s);
1815         reset_hwaddr(lan_ifname);
1816
1817         cprintf("%s %s\n",
1818                 nvram_safe_get("lan_ipaddr"), nvram_safe_get("lan_netmask"));
1819
1820 #ifndef HAVE_MADWIFI
1821 #ifndef HAVE_RT2880
1822         int cnt = get_wl_instances();
1823         int c;
1824
1825         for (c = 0; c < cnt; c++) {
1826 #ifdef HAVE_MADWIFI
1827                 char br1enable[32];
1828                 char br1ipaddr[32];
1829                 char br1netmask[32];
1830
1831                 sprintf(br1enable, "ath%d_br1_enable", c);
1832                 sprintf(br1ipaddr, "ath%d_br1_ipaddr", c);
1833                 sprintf(br1netmask, "ath%d_br1_netmask", c);
1834 #else
1835                 char br1enable[32];
1836                 char br1ipaddr[32];
1837                 char br1netmask[32];
1838
1839                 sprintf(br1enable, "wl%d_br1_enable", c);
1840                 sprintf(br1ipaddr, "wl%d_br1_ipaddr", c);
1841                 sprintf(br1netmask, "wl%d_br1_netmask", c);
1842 #endif
1843                 if (nvram_get(br1enable) == NULL)
1844                         nvram_set(br1enable, "0");
1845                 if (nvram_get(br1ipaddr) == NULL)
1846                         nvram_set(br1ipaddr, "0.0.0.0");
1847                 if (nvram_get(br1netmask) == NULL)
1848                         nvram_set(br1netmask, "255.255.255.0");
1849                 if (nvram_match(br1enable, "1")) {
1850                         ifconfig("br1", 0, 0, 0);
1851
1852                         // eval ("ifconfig", "br1", "down");
1853                         br_del_bridge("br1");
1854                         br_add_bridge("br1");
1855
1856                         if (nvram_match("lan_stp", "0"))
1857                                 br_set_stp_state("br1", 0);     // eval ("brctl", "stp",
1858                         // "br1", "off");
1859                         else
1860                                 br_set_stp_state("br1", 1);     // eval ("brctl", "stp",
1861                         // "br1", "off");
1862                         br_set_bridge_forward_delay("br1", 1);
1863
1864                         /*
1865                          * Bring up and configure br1 interface
1866                          */
1867                         if (nvram_invmatch(br1ipaddr, "0.0.0.0")) {
1868                                 ifconfig("br1", IFUP, nvram_safe_get(br1ipaddr),
1869                                          nvram_safe_get(br1netmask));
1870
1871                                 if (nvram_match("lan_stp", "0"))
1872                                         br_set_stp_state("br1", 0);     // eval ("brctl",
1873                                 // "stp", "br1",
1874                                 // "off");
1875                                 else
1876                                         br_set_stp_state("br1", 1);     // eval ("brctl",
1877                                 // "stp", "br1",
1878                                 // "off");
1879
1880                                 sleep(2);
1881 #if !defined(HAVE_MADWIFI) && !defined(HAVE_RT2880)
1882                                 notify_nas("lan", "br1", "up");
1883 #endif
1884                         }
1885
1886                 }
1887         }
1888         /*
1889          * Sveasoft - Bring up and configure wds interfaces
1890          */
1891         /*
1892          * logic - if separate ip defined bring it up
1893          */
1894         /*
1895          * else if flagged for br1 and br1 is enabled add to br1
1896          */
1897         /*
1898          * else add it to the br0 bridge
1899          */
1900         for (c = 0; c < cnt; c++) {
1901
1902                 for (s = 1; s <= MAX_WDS_DEVS; s++) {
1903                         char wdsvarname[32] = { 0 };
1904                         char wdsdevname[32] = { 0 };
1905                         char *dev;
1906
1907 #ifdef HAVE_MADWIFI
1908                         char br1enable[32];
1909
1910                         sprintf(wdsvarname, "ath%d_wds%d_enable", c, s);
1911                         sprintf(wdsdevname, "ath%d_wds%d_if", c, s);
1912                         sprintf(br1enable, "ath%d_br1_enable", c);
1913                         if (nvram_get(wdsvarname) == NULL)
1914                                 nvram_set(wdsvarname, "0");
1915 #else
1916                         char br1enable[32];
1917
1918                         sprintf(wdsvarname, "wl%d_wds%d_enable", c, s);
1919                         sprintf(wdsdevname, "wl%d_wds%d_if", c, s);
1920                         sprintf(br1enable, "wl%d_br1_enable", c);
1921                         if (nvram_get(wdsvarname) == NULL)
1922                                 nvram_set(wdsvarname, "0");
1923 #endif
1924                         dev = nvram_safe_get(wdsdevname);
1925                         if (strlen(dev) == 0)
1926                                 continue;
1927 #ifdef HAVE_RT2880
1928                         dev = getWDSDev(dev);
1929 #endif
1930                         ifconfig(dev, 0, 0, 0);
1931
1932                         // eval ("ifconfig", dev, "down");
1933                         if (nvram_match(wdsvarname, "1")) {
1934                                 char *wdsip;
1935                                 char *wdsnm;
1936                                 char wdsbc[32] = { 0 };
1937 #ifdef HAVE_MADWIFI
1938                                 wdsip = nvram_nget("ath%d_wds%d_ipaddr", c, s);
1939                                 wdsnm = nvram_nget("ath%d_wds%d_netmask", c, s);
1940 #else
1941                                 wdsip = nvram_nget("wl%d_wds%d_ipaddr", c, s);
1942                                 wdsnm = nvram_nget("wl%d_wds%d_netmask", c, s);
1943 #endif
1944
1945                                 snprintf(wdsbc, 31, "%s", wdsip);
1946                                 get_broadcast(wdsbc, wdsnm);
1947                                 eval("ifconfig", dev, wdsip, "broadcast",
1948                                      wdsbc, "netmask", wdsnm, "up");
1949                         } else if (nvram_match(wdsvarname, "2")
1950                                    && nvram_match(br1enable, "1")) {
1951                                 eval("ifconfig", dev, "up");
1952                                 sleep(1);
1953                                 br_add_interface("br1", dev);
1954                         } else if (nvram_match(wdsvarname, "3")) {
1955                                 ifconfig(dev, IFUP, 0, 0);
1956                                 sleep(1);
1957                                 br_add_interface(getBridge(dev), dev);
1958                         }
1959                 }
1960         }
1961 #endif
1962 #endif
1963 #ifdef HAVE_XSCALE
1964 #define HAVE_RB500
1965 #endif
1966 #ifdef HAVE_PB42
1967 #define HAVE_RB500
1968 #endif
1969 #ifdef HAVE_LSX
1970 #define HAVE_RB500
1971 #endif
1972 #ifdef HAVE_DANUBE
1973 #define HAVE_RB500
1974 #endif
1975 #ifdef HAVE_STORM
1976 #define HAVE_RB500
1977 #endif
1978 #ifdef HAVE_ADM5120
1979 #define HAVE_RB500
1980 #endif
1981 #ifdef HAVE_MAGICBOX
1982 #define HAVE_RB500
1983 #endif
1984 #ifdef HAVE_RT2880
1985 #define HAVE_RB500
1986 #endif
1987 #ifdef HAVE_FONERA
1988 #define HAVE_RB500
1989 #endif
1990 #ifdef HAVE_LS2
1991 #define HAVE_RB500
1992 #endif
1993 #ifdef HAVE_LS5
1994 #define HAVE_RB500
1995 #endif
1996 #ifdef HAVE_WHRAG108
1997 #define HAVE_RB500
1998 #endif
1999 #ifdef HAVE_TW6600
2000 #define HAVE_RB500
2001 #endif
2002 #ifdef HAVE_CA8
2003 #define HAVE_RB500
2004 #endif
2005 #ifndef HAVE_RB500
2006         /*
2007          * Set QoS mode
2008          */
2009         if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) >= 0) {
2010                 int i, qos;
2011                 caddr_t ifrdata;
2012                 struct ethtool_drvinfo info;
2013
2014                 qos = (strcmp(nvram_safe_get("wl_wme"), "on")) ? 0 : 1;
2015                 for (i = 1; i <= DEV_NUMIFS; i++) {
2016                         ifr.ifr_ifindex = i;
2017                         if (ioctl(s, SIOCGIFNAME, &ifr))
2018                                 continue;
2019                         if (ioctl(s, SIOCGIFHWADDR, &ifr))
2020                                 continue;
2021                         if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER)
2022                                 continue;
2023                         /*
2024                          * get flags
2025                          */
2026                         if (ioctl(s, SIOCGIFFLAGS, &ifr))
2027                                 continue;
2028                         /*
2029                          * if up(wan not up yet at this point)
2030                          */
2031                         if (ifr.ifr_flags & IFF_UP) {
2032                                 ifrdata = ifr.ifr_data;
2033                                 memset(&info, 0, sizeof(info));
2034                                 info.cmd = ETHTOOL_GDRVINFO;
2035                                 ifr.ifr_data = (caddr_t) & info;
2036                                 if (ioctl(s, SIOCETHTOOL, &ifr) >= 0) {
2037                                         /*
2038                                          * currently only need to set QoS to et devices
2039                                          */
2040                                         if (!strncmp(info.driver, "et", 2)) {
2041                                                 ifr.ifr_data = (caddr_t) & qos;
2042                                                 ioctl(s, SIOCSETCQOS, &ifr);
2043                                         }
2044                                 }
2045                                 ifr.ifr_data = ifrdata;
2046                         }
2047                 }
2048                 close(s);
2049         }
2050 #undef HAVE_RB500
2051 #endif
2052         /*
2053          * Sveasoft - set default IP gateway defined
2054          */
2055         if (strcmp(nvram_safe_get("lan_gateway"), "0.0.0.0"))
2056                 eval("ip", "ro", "add", "default", "via",
2057                      nvram_safe_get("lan_gateway"), "dev", "br0");
2058
2059 #if !defined(HAVE_MADWIFI) && !defined(HAVE_RT2880)
2060         for (c = 0; c < cnt; c++) {
2061                 eval("wl", "-i", get_wl_instance_name(c), "vlan_mode", "0");
2062         }
2063 #endif
2064         /*
2065          * Bring up local host interface
2066          */
2067         config_loopback();
2068
2069         /*
2070          * Set additional lan static routes if need
2071          */
2072         start_set_routes();
2073 #if !defined(HAVE_MADWIFI) && !defined(HAVE_RT2880)
2074         for (c = 0; c < cnt; c++) {
2075                 eval("wl", "-i", get_wl_instance_name(c), "radio",
2076                      nvram_nmatch("disabled", "wl%d_net_mode",
2077                                   c) ? "off" : "on");
2078         }
2079 #endif
2080         /*
2081          * Disable wireless will cause diag led blink, so we want to stop it.
2082          */
2083         if (check_hw_type() == BCM4712_CHIP) {
2084                 diag_led(DIAG, STOP_LED);
2085                 /*
2086                  * Light or go out the DMZ led even if there is no wan ip.
2087                  */
2088                 if (nvram_invmatch("dmz_ipaddr", "")
2089                     && nvram_invmatch("dmz_ipaddr", "0"))
2090                         diag_led(DMZ, START_LED);
2091                 else
2092                         diag_led(DMZ, STOP_LED);
2093         }
2094
2095         if (nvram_match("lan_stp", "0"))
2096                 br_set_stp_state("br0", 0);
2097         else
2098                 br_set_stp_state("br0", 1);
2099
2100         free(lan_ifnames);
2101         free(lan_ifname);
2102         // eval ("rm", "/tmp/hosts");
2103         addHost("localhost", "127.0.0.1");
2104         if (strlen(nvram_safe_get("wan_hostname")) > 0)
2105                 addHost(nvram_safe_get("wan_hostname"),
2106                         nvram_safe_get("lan_ipaddr"));
2107         else if (strlen(nvram_safe_get("router_name")) > 0)
2108                 addHost(nvram_safe_get("router_name"),
2109                         nvram_safe_get("lan_ipaddr"));
2110 #ifdef HAVE_MICRO
2111         br_shutdown();
2112 #endif
2113 }
2114
2115 void stop_lan(void)
2116 {
2117         char *lan_ifname = nvram_safe_get("lan_ifname");
2118         char name[80], *next;
2119
2120         cprintf("%s\n", lan_ifname);
2121         /*
2122          * Bring down LAN interface
2123          */
2124         ifconfig(lan_ifname, 0, NULL, NULL);
2125 #ifdef HAVE_MICRO
2126         br_init();
2127 #endif
2128
2129 #if !defined(HAVE_MADWIFI) && !defined(HAVE_RT2880)
2130         br_del_interface(lan_ifname, "wl0.1");
2131         ifconfig("wl0.1", 0, NULL, NULL);
2132         br_del_interface(lan_ifname, "wl0.2");
2133         ifconfig("wl0.2", 0, NULL, NULL);
2134         br_del_interface(lan_ifname, "wl0.3");
2135         ifconfig("wl0.3", 0, NULL, NULL);
2136         br_del_interface(lan_ifname, "wl1.1");
2137         ifconfig("wl1.1", 0, NULL, NULL);
2138         br_del_interface(lan_ifname, "wl1.2");
2139         ifconfig("wl1.2", 0, NULL, NULL);
2140         br_del_interface(lan_ifname, "wl1.3");
2141         ifconfig("wl1.3", 0, NULL, NULL);
2142 #endif
2143         /*
2144          * Bring down bridged interfaces
2145          */
2146         if (strncmp(lan_ifname, "br", 2) == 0) {
2147                 foreach(name, nvram_safe_get("lan_ifnames"), next) {
2148                         if (nvram_match("wan_ifname", name))
2149                                 continue;
2150                         if (!ifexists(name))
2151                                 continue;
2152 #if !defined(HAVE_MADWIFI) && !defined(HAVE_RT2880)
2153                         eval("wlconf", name, "down");
2154 #endif
2155                         ifconfig(name, 0, NULL, NULL);
2156                         br_del_interface(lan_ifname, name);
2157                 }
2158                 br_del_bridge(lan_ifname);
2159         }
2160         /*
2161          * Bring down specific interface
2162          */
2163 #if !defined(HAVE_MADWIFI) && !defined(HAVE_RT2880)
2164         else if (strcmp(lan_ifname, ""))
2165                 eval("wlconf", lan_ifname, "down");
2166 #endif
2167 #ifdef HAVE_MICRO
2168         br_shutdown();
2169 #endif
2170
2171         cprintf("done\n");
2172 }
2173
2174 #define sin_addr(s) (((struct sockaddr_in *)(s))->sin_addr)
2175
2176 int wan_valid(char *ifname)
2177 {
2178         char name[80], *next;
2179
2180         foreach(name, nvram_safe_get("wan_ifnames"), next)
2181             if (ifname && !strcmp(ifname, name))
2182                 return 1;
2183
2184         if (getSTA() && !strcmp(getSTA(), ifname))
2185                 return 1;
2186
2187         return 0;
2188 }
2189
2190 void start_force_to_dial(void);
2191
2192 void start_wan(int status)
2193 {
2194         FILE *fp;
2195         char *wan_ifname = get_wan_face();
2196         char *wan_proto = nvram_safe_get("wan_proto");
2197         int s;
2198         struct ifreq ifr;
2199         if (isClient()) {
2200                 char *ifn = getSTA();
2201                 int count = 10;
2202                 while ((count--) > 0)   // wait until wan is available (10 sek max)
2203                 {
2204                         if (ifexists(ifn)) {
2205                                 break;
2206                         }
2207                         sleep(1);
2208                 }
2209         }
2210
2211         eval("ifconfig", nvram_safe_get("wan_ifname"), "allmulti", "promisc");
2212
2213 #ifdef HAVE_PPPOE
2214 #ifdef HAVE_RB500
2215         char *pppoe_wan_ifname = nvram_invmatch("pppoe_wan_ifname",
2216                                                 "") ?
2217             nvram_safe_get("pppoe_wan_ifname") : nvram_safe_get("wan_ifname");
2218 #else
2219
2220 #ifdef HAVE_XSCALE
2221         char *pppoe_wan_ifname = nvram_invmatch("pppoe_wan_ifname",
2222                                                 "") ?
2223             nvram_safe_get("pppoe_wan_ifname") : "ixp1";
2224 #elif HAVE_MAGICBOX
2225         char *pppoe_wan_ifname = nvram_invmatch("pppoe_wan_ifname",
2226                                                 "") ?
2227             nvram_safe_get("pppoe_wan_ifname") : "eth0";
2228 #elif HAVE_WRT54G2
2229         char *pppoe_wan_ifname = nvram_invmatch("pppoe_wan_ifname",
2230                                                 "") ?
2231             nvram_safe_get("pppoe_wan_ifname") : "vlan1";
2232 #elif HAVE_DIR300
2233         char *pppoe_wan_ifname = nvram_invmatch("pppoe_wan_ifname",
2234                                                 "") ?
2235             nvram_safe_get("pppoe_wan_ifname") : "vlan2";
2236 #elif HAVE_BWRG1000
2237         char *pppoe_wan_ifname = nvram_invmatch("pppoe_wan_ifname",
2238                                                 "") ?
2239             nvram_safe_get("pppoe_wan_ifname") : "vlan2";
2240 #elif HAVE_ECB9750
2241         char *pppoe_wan_ifname = nvram_invmatch("pppoe_wan_ifname",
2242                                                 "") ?
2243             nvram_safe_get("pppoe_wan_ifname") : "eth2";
2244 #elif HAVE_RT2880
2245         char *pppoe_wan_ifname = nvram_invmatch("pppoe_wan_ifname",
2246                                                 "") ?
2247             nvram_safe_get("pppoe_wan_ifname") : "vlan2";
2248 #elif HAVE_MR3202A
2249         char *pppoe_wan_ifname = nvram_invmatch("pppoe_wan_ifname",
2250                                                 "") ?
2251             nvram_safe_get("pppoe_wan_ifname") : "vlan2";
2252 #elif HAVE_FONERA
2253         char *pppoe_wan_ifname = NULL;
2254
2255         if (getRouterBrand() == ROUTER_BOARD_FONERA2200)
2256                 pppoe_wan_ifname =
2257                     nvram_invmatch("pppoe_wan_ifname",
2258                                    "") ? nvram_safe_get("pppoe_wan_ifname") :
2259                     "vlan1";
2260         else
2261                 pppoe_wan_ifname =
2262                     nvram_invmatch("pppoe_wan_ifname",
2263                                    "") ? nvram_safe_get("pppoe_wan_ifname") :
2264                     "eth0";
2265 #elif HAVE_NS2
2266         char *pppoe_wan_ifname = nvram_invmatch("pppoe_wan_ifname",
2267                                                 "") ?
2268             nvram_safe_get("pppoe_wan_ifname") : "eth0";
2269 #elif HAVE_BS2
2270         char *pppoe_wan_ifname = nvram_invmatch("pppoe_wan_ifname",
2271                                                 "") ?
2272             nvram_safe_get("pppoe_wan_ifname") : "eth0";
2273 #elif HAVE_PICO2
2274         char *pppoe_wan_ifname = nvram_invmatch("pppoe_wan_ifname",
2275                                                 "") ?
2276             nvram_safe_get("pppoe_wan_ifname") : "eth0";
2277 #elif HAVE_PICO2HP
2278         char *pppoe_wan_ifname = nvram_invmatch("pppoe_wan_ifname",
2279                                                 "") ?
2280             nvram_safe_get("pppoe_wan_ifname") : "eth0";
2281 #elif HAVE_MS2
2282         char *pppoe_wan_ifname = nvram_invmatch("pppoe_wan_ifname",
2283                                                 "") ?
2284             nvram_safe_get("pppoe_wan_ifname") : "eth0";
2285 #elif HAVE_BS2HP
2286         char *pppoe_wan_ifname = nvram_invmatch("pppoe_wan_ifname",
2287                                                 "") ?
2288             nvram_safe_get("pppoe_wan_ifname") : "eth0";
2289 #elif HAVE_LC2
2290         char *pppoe_wan_ifname = nvram_invmatch("pppoe_wan_ifname",
2291                                                 "") ?
2292             nvram_safe_get("pppoe_wan_ifname") : "eth0";
2293 #elif HAVE_LS2
2294         char *pppoe_wan_ifname = nvram_invmatch("pppoe_wan_ifname",
2295                                                 "") ?
2296             nvram_safe_get("pppoe_wan_ifname") : nvram_safe_get("wan_ifname");
2297 #elif HAVE_LSX
2298         char *pppoe_wan_ifname = nvram_invmatch("pppoe_wan_ifname",
2299                                                 "") ?
2300             nvram_safe_get("pppoe_wan_ifname") : "eth0";
2301 #elif HAVE_DANUBE
2302         char *pppoe_wan_ifname = nvram_invmatch("pppoe_wan_ifname",
2303                                                 "") ?
2304             nvram_safe_get("pppoe_wan_ifname") : "eth0";
2305 #elif HAVE_STORM
2306         char *pppoe_wan_ifname = nvram_invmatch("pppoe_wan_ifname",
2307                                                 "") ?
2308             nvram_safe_get("pppoe_wan_ifname") : "eth0";
2309 #elif HAVE_ADM5120
2310         char *pppoe_wan_ifname = nvram_invmatch("pppoe_wan_ifname",
2311                                                 "") ?
2312             nvram_safe_get("pppoe_wan_ifname") : "eth0";
2313 #elif HAVE_LS5
2314         char *pppoe_wan_ifname = nvram_invmatch("pppoe_wan_ifname",
2315                                                 "") ?
2316             nvram_safe_get("pppoe_wan_ifname") : "eth0";
2317 #elif HAVE_WHRAG108
2318         char *pppoe_wan_ifname = nvram_invmatch("pppoe_wan_ifname",
2319                                                 "") ?
2320             nvram_safe_get("pppoe_wan_ifname") : "eth1";
2321 #elif HAVE_PB42
2322         char *pppoe_wan_ifname = nvram_invmatch("pppoe_wan_ifname",
2323                                                 "") ?
2324             nvram_safe_get("pppoe_wan_ifname") : "eth0";
2325 #elif HAVE_TW6600
2326         char *pppoe_wan_ifname = nvram_invmatch("pppoe_wan_ifname",
2327                                                 "") ?
2328             nvram_safe_get("pppoe_wan_ifname") : "eth0";
2329 #elif HAVE_RDAT81
2330         char *pppoe_wan_ifname = nvram_invmatch("pppoe_wan_ifname",
2331                                                 "") ?
2332             nvram_safe_get("pppoe_wan_ifname") : "eth0";
2333 #elif HAVE_RCAA01
2334         char *pppoe_wan_ifname = nvram_invmatch("pppoe_wan_ifname",
2335                                                 "") ?
2336             nvram_safe_get("pppoe_wan_ifname") : "vlan1";
2337 #elif HAVE_CA8PRO
2338         char *pppoe_wan_ifname = nvram_invmatch("pppoe_wan_ifname",
2339                                                 "") ?
2340             nvram_safe_get("pppoe_wan_ifname") : "vlan1";
2341 #elif HAVE_CA8
2342         char *pppoe_wan_ifname = nvram_invmatch("pppoe_wan_ifname",
2343                                                 "") ?
2344             nvram_safe_get("pppoe_wan_ifname") : "eth0";
2345 #elif HAVE_X86
2346         char *pppoe_wan_ifname = nvram_invmatch("pppoe_wan_ifname",
2347                                                 "") ?
2348             nvram_safe_get("pppoe_wan_ifname") : nvram_safe_get("wan_ifname");
2349 #else
2350         char *pppoe_wan_ifname = nvram_invmatch("pppoe_wan_ifname",
2351                                                 "") ?
2352             nvram_safe_get("pppoe_wan_ifname") : "vlan1";
2353 #endif
2354 #ifdef HAVE_MULTICAST
2355         if (!nvram_match("dtag_vlan8", "1"))
2356                 stop_igmp_proxy();
2357 #endif
2358
2359 #endif
2360 #if !defined(HAVE_MADWIFI) && !defined(HAVE_RT2880)
2361         if (getWET()) {
2362                 dns_to_resolv();
2363                 return;
2364         }
2365         if (isClient()) {
2366                 pppoe_wan_ifname = getSTA();
2367         }
2368 #else
2369         if (isClient()) {
2370                 pppoe_wan_ifname = getSTA();
2371                 int count = 10;
2372                 while ((count--) > 0) {
2373
2374                 }
2375         }
2376 #endif
2377 #endif
2378         // fprintf(stderr,"%s %s\n", wan_ifname, wan_proto);
2379
2380         if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0)
2381                 return;
2382         /*
2383          * Check PPPoE version, RP or linksys
2384          */
2385 #ifdef HAVE_PPPOE
2386         if (nvram_match("wan_proto", "pppoe"))
2387                 strncpy(ifr.ifr_name, pppoe_wan_ifname, IFNAMSIZ);
2388         else
2389 #endif
2390                 strncpy(ifr.ifr_name, wan_ifname, IFNAMSIZ);
2391
2392         /*
2393          * Set WAN hardware address before bringing interface up
2394          */
2395         memset(ifr.ifr_hwaddr.sa_data, 0, ETHER_ADDR_LEN);
2396
2397         ifconfig(wan_ifname, 0, NULL, NULL);
2398 #if defined(HAVE_FONERA) || defined(HAVE_CA8) && !defined(HAVE_MR3202A)
2399         if (getRouterBrand() != ROUTER_BOARD_FONERA2200
2400             && getRouterBrand() != ROUTER_BOARD_CA8PRO) {
2401                 char staticlan[32];
2402
2403                 sprintf(staticlan, "%s:0", wan_ifname);
2404                 if (!nvram_match("ath0_mode", "sta")
2405                     && !nvram_match("ath0_mode", "wdssta")
2406                     && !nvram_match("ath0_mode", "wet")
2407                     && !CANBRIDGE()) {
2408                         eval("ifconfig", "br0:0", "down");
2409                         eval("ifconfig", staticlan, "169.254.255.1", "netmask",
2410                              "255.255.0.0");
2411                 } else
2412                         eval("ifconfig", staticlan, "0.0.0.0", "down");
2413         }
2414 #endif
2415
2416         // fprintf(stderr,"%s %s\n", wan_ifname, wan_proto);
2417         char *wlifname = getSTA();
2418
2419         if (!wlifname) {
2420                 wlifname = getWET();
2421         }
2422
2423         unsigned char mac[20];
2424
2425         if (nvram_match("mac_clone_enable", "1") &&
2426             nvram_invmatch("def_hwaddr", "00:00:00:00:00:00") &&
2427             nvram_invmatch("def_hwaddr", "")) {
2428                 ether_atoe(nvram_safe_get("def_hwaddr"),
2429                            ifr.ifr_hwaddr.sa_data);
2430         } else {
2431
2432                 if (wlifname && !strcmp(wan_ifname, wlifname))  // sta mode
2433                 {
2434                         getWirelessMac(mac);
2435                         ether_atoe(mac, ifr.ifr_hwaddr.sa_data);
2436                 } else {
2437 #ifdef HAVE_X86
2438                         ioctl(s, SIOCGIFHWADDR, &ifr);
2439 #else
2440                         getWANMac(mac);
2441                         ether_atoe(mac, ifr.ifr_hwaddr.sa_data);
2442 #endif
2443                 }
2444         }
2445
2446         if (memcmp(ifr.ifr_hwaddr.sa_data, "\0\0\0\0\0\0", ETHER_ADDR_LEN)) {
2447                 ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
2448 #if !defined(HAVE_MADWIFI) && !defined(HAVE_RT2880)
2449
2450                 if (wlifname && !strcmp(wan_ifname, wlifname))
2451                         eval("wl", "-i", wan_ifname, "down");
2452                 ioctl(s, SIOCSIFHWADDR, &ifr);
2453 #else
2454                 if (!wlifname) {
2455                         ioctl(s, SIOCSIFHWADDR, &ifr);
2456 //          eval("ifconfig",wan_ifname,"promisc"); // set wan to promisc, since we now have usually different mac addresses on vlans
2457                 }
2458 #endif
2459 #if !defined(HAVE_MADWIFI) && !defined(HAVE_RT2880)
2460                 if (wlifname && !strcmp(wan_ifname, wlifname)) {
2461                         eval("wl", "-i", wan_ifname, "up");
2462                         start_config_macs(wan_ifname);
2463                 }
2464 #endif
2465                 cprintf("Write WAN mac successfully\n");
2466         } else
2467                 perror("Write WAN mac fail : \n");
2468         // fprintf(stderr,"%s %s\n", wan_ifname, wan_proto);
2469
2470         /*
2471          * Set MTU
2472          */
2473         init_mtu(wan_proto);    // add by honor 2002/12/27
2474         // fprintf(stderr,"%s %s\n", wan_ifname, wan_proto);
2475
2476         // Set our Interface to the right MTU
2477 #ifdef HAVE_PPPOE
2478         if (nvram_match("wan_proto", "pppoe")) {
2479                 ifr.ifr_mtu = atoi(getMTU(wan_ifname)); // default ethernet frame size
2480         } else
2481 #endif
2482         {
2483                 int mtu = atoi(nvram_safe_get("wan_mtu"));
2484                 if (mtu == 1500)
2485                         mtu = atoi(getMTU(wan_ifname));
2486                 ifr.ifr_mtu = mtu;
2487         }
2488         // fprintf(stderr,"set mtu for %s to %d\n",ifr.ifr_name,ifr.ifr_mtu);
2489         ioctl(s, SIOCSIFMTU, &ifr);
2490
2491         if (strcmp(wan_proto, "disabled") == 0) {
2492                 start_wan_done(wan_ifname);
2493                 return;
2494         }
2495
2496         /*
2497          * Bring up WAN interface
2498          */
2499 #ifdef HAVE_PPPOE
2500         /*
2501          * AhMan March 19 2005
2502          */
2503         /*
2504          * ice-man March 19 2005
2505          */
2506         /*
2507          * pppoe_wan interface must be up in order to use any pppoe client
2508          */
2509         if (strcmp(wan_proto, "pppoe") == 0)
2510                 ifconfig(pppoe_wan_ifname, IFUP, NULL, NULL);
2511         else {
2512                 ifconfig(wan_ifname, IFUP, NULL, NULL);
2513         }
2514 #else
2515
2516         ifconfig(wan_ifname, IFUP, NULL, NULL);
2517         if (nvram_match("wl0_mode", "infra")) {
2518                 eval("wl", "infra", "0");
2519                 eval("wl", "ssid", nvram_safe_get("wl0_ssid"));
2520         }
2521 #endif
2522         set_host_domain_name();
2523
2524         // Remove the current value of pppd_pppifname
2525         nvram_set("pppd_pppifname", "");
2526
2527 #ifdef HAVE_3G
2528         nvram_set("3gdata", "");
2529 #endif
2530         /*
2531          * Configure WAN interface
2532          */
2533 #ifdef HAVE_3G
2534         if ((strcmp(wan_proto, "3g") == 0)) {
2535                 stop_dhcpc();
2536 #ifdef HAVE_PPTP
2537                 stop_pptp();
2538 #endif
2539                 mkdir("/tmp/ppp", 0777);
2540                 symlink("/sbin/rc", "/tmp/ppp/ip-up");
2541                 symlink("/sbin/rc", "/tmp/ppp/ip-down");
2542                 unlink("/tmp/ppp/log");
2543                 unlink("/tmp/ppp/connect-log");
2544                 unlink("/tmp/ppp/set-pppoepid");
2545                 char *controldevice = get3GControlDevice();
2546                 int timeout = 5;
2547                 if (controldevice && !strcmp(controldevice,"hso"))
2548                     {
2549                    
2550                    
2551                     }else{
2552
2553
2554                 /* init PIN */
2555                 if (strlen(nvram_safe_get("wan_pin")))
2556                         sysprintf("export COMGTPIN=%s;comgt PIN -d %s\n",
2557                                   nvram_safe_get("wan_pin"), controldevice);
2558                 if (strlen(nvram_safe_get("wan_apn")))
2559                         if (!nvram_match("wan_dial", "2"))
2560                                 sysprintf
2561                                     ("export COMGTAPN=\"%s\";comgt APN -d %s\n",
2562                                      nvram_safe_get("wan_apn"), controldevice);
2563                 // Lets open option file and enter all the parameters.
2564                 fp = fopen("/tmp/ppp/options.pppoe", "w");
2565                 fprintf(fp, "defaultroute\n");
2566                 fprintf(fp, "usepeerdns\n");
2567                 fprintf(fp, "noipdefault\n");
2568                 fprintf(fp, "noauth\n");
2569                 fprintf(fp, "ipcp-max-failure 30\n");
2570                 fprintf(fp, "lcp-echo-interval 10\n");
2571                 fprintf(fp, "lcp-echo-failure 3\n");
2572                 if (nvram_match("mtu_enable", "1")) {
2573                         if (atoi(nvram_safe_get("wan_mtu")) > 0) {
2574                                 fprintf(fp, "mtu %s\n",
2575                                         nvram_safe_get("wan_mtu"));
2576                                 fprintf(fp, "mru %s\n",
2577                                         nvram_safe_get("wan_mtu"));
2578                         }
2579
2580                 }
2581                 fprintf(fp, "crtscts\n");
2582                 fprintf(fp, "460800\n");
2583 //      fprintf(fp,"connect \"/usr/sbin/comgt DIAL -d %s\"\n",controldevice);
2584                 char *dial = "*99***1#";
2585                 if (nvram_match("wan_dial", "0"))
2586                         dial = "ATD*99***1#";
2587                 if (nvram_match("wan_dial", "1"))
2588                         dial = "ATD*99#";
2589                 if (nvram_match("wan_dial", "2"))
2590                         dial = "ATDT#777";
2591                 fprintf(fp,
2592                         "connect \"COMGTDIAL='%s' /usr/sbin/comgt DIAL -d %s >/tmp/comgt.out 2>&1\"\n",
2593                         dial, nvram_safe_get("3gdata"));
2594                 if (strlen(nvram_safe_get("ppp_username")))
2595                         fprintf(fp, "user %s\n",
2596                                 nvram_safe_get("ppp_username"));
2597                 if (strlen(nvram_safe_get("ppp_passwd")))
2598                         fprintf(fp, "password %s\n",
2599                                 nvram_safe_get("ppp_passwd"));
2600                 fprintf(fp, "%s\n", nvram_get("3gdata"));
2601
2602                 fclose(fp);
2603
2604
2605
2606                 eval("pppd", "file", "/tmp/ppp/options.pppoe");
2607
2608                 /*
2609                  * Pretend that the WAN interface is up
2610                  */
2611                 if (nvram_match("ppp_demand", "1")) {
2612                         /*
2613                          * Wait for ppp0 to be created
2614                          */
2615                         while (ifconfig("ppp0", IFUP, NULL, NULL) && timeout--)
2616                                 sleep(1);
2617                         strncpy(ifr.ifr_name, "ppp0", IFNAMSIZ);
2618
2619                         /*
2620                          * Set temporary IP address
2621                          */
2622                         timeout = 3;
2623                         while (ioctl(s, SIOCGIFADDR, &ifr) && timeout--) {
2624                                 perror("ppp0");
2625                                 printf("Wait ppp inteface to init (1) ...\n");
2626                                 sleep(1);
2627                         };
2628                         char client[32];
2629
2630                         nvram_set("wan_ipaddr",
2631                                   inet_ntop(AF_INET, &sin_addr(&ifr.ifr_addr),
2632                                             client, 16));
2633                         nvram_set("wan_netmask", "255.255.255.255");
2634
2635                         /*
2636                          * Set temporary P-t-P address
2637                          */
2638                         timeout = 3;
2639                         while (ioctl(s, SIOCGIFDSTADDR, &ifr) && timeout--) {
2640                                 perror("ppp0");
2641                                 printf("Wait ppp inteface to init (2) ...\n");
2642                                 sleep(1);
2643                         }
2644                         char *peer =
2645                             inet_ntop(AF_INET, &sin_addr(&ifr.ifr_dstaddr),
2646                                       client,
2647                                       16);
2648
2649                         nvram_set("wan_gateway", peer);
2650
2651                         start_wan_done("ppp0");
2652
2653                         // if user press Connect" button from web, we must force to dial
2654                         if (nvram_match("action_service", "start_3g")) {
2655                                 sleep(3);
2656                                 start_force_to_dial();
2657                                 nvram_unset("action_service");
2658                         }
2659                 } else {
2660                         if (status != REDIAL) {
2661                                 start_redial();
2662                         }
2663                 }
2664                 }
2665
2666         } else
2667 #endif
2668 #ifdef HAVE_PPPOE
2669         if ((strcmp(wan_proto, "pppoe") == 0)) {
2670                 char username[80], passwd[80];
2671                 char idletime[20], retry_num[20];
2672
2673                 snprintf(idletime, sizeof(idletime), "%d",
2674                          atoi(nvram_safe_get("ppp_idletime")) * 60);
2675                 snprintf(retry_num, sizeof(retry_num), "%d",
2676                          (atoi(nvram_safe_get("ppp_redialperiod")) / 5) - 1);
2677
2678                 snprintf(username, sizeof(username), "%s",
2679                          nvram_safe_get("ppp_username"));
2680                 snprintf(passwd, sizeof(passwd), "%s",
2681                          nvram_safe_get("ppp_passwd"));
2682
2683                 mkdir("/tmp/ppp", 0777);
2684                 int timeout = 5;
2685
2686                 // Lets open option file and enter all the parameters.
2687                 fp = fopen("/tmp/ppp/options.pppoe", "w");
2688                 // rp-pppoe kernelmode plugin
2689 #if defined(HAVE_ADM5120) && !defined(HAVE_WP54G) && !defined(HAVE_NP28G)
2690                 fprintf(fp, "plugin /lib/rp-pppoe.so");
2691 #else
2692                 fprintf(fp, "plugin /usr/lib/rp-pppoe.so");
2693 #endif
2694                 if (nvram_invmatch("pppoe_service", ""))
2695                         fprintf(fp, " rp_pppoe_service %s",
2696                                 nvram_safe_get("pppoe_service"));
2697                 fprintf(fp, "\n");
2698                 char vlannic[32];
2699                 char tvnic[32];
2700
2701                 if (!strncmp(pppoe_wan_ifname, "vlan", 4)) {
2702                         if (nvram_match("wan_vdsl", "1")) {
2703                                 char *ifn = enable_dtag_vlan(1);
2704
2705                                 if (nvram_match("dtag_vlan8", "1")) {
2706                                         sprintf(vlannic, "%s.0008", ifn);
2707                                         if (!ifexists(vlannic)) {
2708                                                 eval("vconfig", "set_name_type",
2709                                                      "DEV_PLUS_VID");
2710                                                 eval("vconfig", "add", ifn,
2711                                                      "8");
2712                                                 eval("ifconfig", vlannic, "up");
2713                                         }
2714                                         nvram_set("tvnicfrom", vlannic);
2715                                         symlink("/sbin/rc", "/tmp/udhcpc_tv");
2716                                         eval("udhcpc", "-i", vlannic, "-s",
2717                                              "/tmp/udhcpc_tv", "-q");
2718                                 }
2719                                 sprintf(vlannic, "%s.0007", ifn);
2720                                 if (!ifexists(vlannic)) {
2721                                         eval("vconfig", "set_name_type",
2722                                              "DEV_PLUS_VID");
2723                                         eval("vconfig", "add", ifn, "7");
2724                                         eval("ifconfig", vlannic, "up");
2725                                 }
2726
2727                                 fprintf(fp, "nic-%s\n", vlannic);
2728                         } else {
2729                                 char *ifn = enable_dtag_vlan(0);
2730
2731                                 sprintf(vlannic, "%s.0007", ifn);
2732                                 if (ifexists(vlannic))
2733                                         eval("vconfig", "rem", vlannic);
2734                                 sprintf(vlannic, "%s.0008", ifn);
2735                                 if (ifexists(vlannic))
2736                                         eval("vconfig", "rem", vlannic);
2737                                 fprintf(fp, "nic-%s\n", pppoe_wan_ifname);
2738                         }
2739
2740                 } else {
2741                         if (nvram_match("wan_vdsl", "1"))       // Deutsche Telekom
2742                                 // VDSL2 Vlan 7 Tag
2743                         {
2744                                 if (nvram_match("dtag_vlan8", "1")) {
2745                                         sprintf(vlannic, "%s.0008",
2746                                                 pppoe_wan_ifname);
2747                                         if (!ifexists(vlannic)) {
2748                                                 eval("vconfig", "set_name_type",
2749                                                      "DEV_PLUS_VID");
2750                                                 eval("vconfig", "add",
2751                                                      pppoe_wan_ifname, "8");
2752                                                 eval("ifconfig", vlannic, "up");
2753                                                 nvram_set("tvnicfrom", vlannic);
2754                                                 symlink("/sbin/rc",
2755                                                         "/tmp/udhcpc_tv");
2756                                                 eval("udhcpc", "-i", vlannic,
2757                                                      "-s", "/tmp/udhcpc_tv",
2758                                                      "-q");
2759                                         }
2760                                 }
2761                                 sprintf(vlannic, "%s.0007", pppoe_wan_ifname);
2762                                 if (!ifexists(vlannic)) {
2763                                         eval("vconfig", "set_name_type",
2764                                              "DEV_PLUS_VID");
2765                                         eval("vconfig", "add", pppoe_wan_ifname,
2766                                              "7");
2767                                         eval("ifconfig", vlannic, "up");
2768                                 }
2769                                 fprintf(fp, "nic-%s\n", vlannic);
2770                         } else {
2771                                 sprintf(vlannic, "%s.0008", pppoe_wan_ifname);
2772                                 if (ifexists(vlannic))
2773                                         eval("vconfig", "rem", vlannic);
2774                                 sprintf(vlannic, "%s.0007", pppoe_wan_ifname);
2775                                 if (ifexists(vlannic))
2776                                         eval("vconfig", "rem", vlannic);
2777                                 fprintf(fp, "nic-%s\n", pppoe_wan_ifname);
2778                         }
2779                 }
2780
2781                 // Those are default options we use + user/passwd
2782                 // By using user/password options we dont have to deal with chap/pap
2783                 // secrets files.
2784                 if (nvram_match("ppp_compression", "1")) {
2785                         fprintf(fp, "mppc\n");
2786                 } else {
2787                         fprintf(fp, "noccp\n");
2788                         fprintf(fp, "nomppc\n");
2789                 }
2790                 fprintf(fp, "noipdefault\n"
2791                         "noauth\n"
2792                         "defaultroute\n" "noaccomp\n" "nobsdcomp\n"
2793                         "nodeflate\n"
2794                         // "debug\n"
2795                         // "maxfail 0\n"
2796                         // "nocrtscts\n"
2797                         // "sync\n"
2798                         // "local\n"
2799                         // "noixp\n"
2800                         // "lock\n"
2801                         // "noproxyarp\n"
2802                         // "ipcp-accept-local\n"
2803                         // "ipcp-accept-remote\n"
2804                         // "nodetach\n"
2805                         "nopcomp\n");
2806                 // "novj\n"
2807                 // "novjccomp\n");
2808                 if (nvram_invmatch("ppp_mppe", ""))
2809                         fprintf(fp, "%s\n", nvram_safe_get("ppp_mppe"));
2810                 else
2811                         fprintf(fp, "nomppe\n");
2812                 fprintf(fp, "usepeerdns\n"
2813                         "user '%s'\n" "password '%s'\n", username, passwd);
2814
2815                 // This is a tricky one. When used it could improve speed of PPPoE
2816                 // but not all ISP's can support it.
2817                 // default-asyncmap escapes all control characters. By using asyncmap
2818                 // 0 PPPD will not escape any control characters
2819                 // Not all ISP's can handle this. By default use default-asyncmap
2820                 // and if ppp_asyncmap=1 do not escape
2821                 if (nvram_match("ppp_asyncmap", "1"))
2822                         fprintf(fp, "asyncmap 0\n");
2823                 else
2824                         fprintf(fp, "default-asyncmap\n");
2825
2826                 // Allow users some control on PPP interface MTU and MRU
2827                 // If pppoe_ppp_mtu > 0 will set mtu of pppX interface to the value
2828                 // in the nvram variable
2829                 // If pppoe_ppp_mru > 0 will set mru of pppX interface to the value
2830                 // in the nvram variable
2831                 // if none is specified PPPD will autonegotiate the values with ISP
2832                 // (sometimes not desirable)
2833                 // Do not forget this should be at least 8 bytes less then physycal
2834                 // interfaces mtu.
2835
2836                 // if MRU is not Auto force MTU/MRU of interface to value selected by
2837                 // theuser on web page
2838                 if (nvram_match("mtu_enable", "1")) {
2839                         if (atoi(nvram_safe_get("wan_mtu")) > 0) {
2840                                 fprintf(fp, "mtu %s\n",
2841                                         nvram_safe_get("wan_mtu"));
2842                                 fprintf(fp, "mru %s\n",
2843                                         nvram_safe_get("wan_mtu"));
2844                         }
2845
2846                 } else {
2847                         // If MRU set to Auto we still allow custom MTU/MRU settings for
2848                         // expirienced users
2849                         if (nvram_invmatch("pppoe_ppp_mtu", ""))
2850                                 if (atoi(nvram_safe_get("pppoe_ppp_mtu")) > 0)
2851                                         fprintf(fp, "mtu %s\n",
2852                                                 nvram_safe_get
2853                                                 ("pppoe_ppp_mtu"));
2854                         if (nvram_invmatch("pppoe_ppp_mru", ""))
2855                                 if (atoi(nvram_safe_get("pppoe_ppp_mru")) > 0)
2856                                         fprintf(fp, "mru %s\n",
2857                                                 nvram_safe_get
2858                                                 ("pppoe_ppp_mru"));
2859                 }
2860
2861                 // Allow runtime debugging
2862                 if (nvram_match("ppp_debug", "1"))
2863                         fprintf(fp, "debug\n");
2864
2865                 // Demand dial.. This is not pretty.
2866                 // The first problems i see is that if connection is lost it would
2867                 // take PPPoE (idletime * 2) * 3 to notice it.
2868                 // In other words if idle is set to 30 seconds, it would take 30*2*3
2869                 // (180) seconds to detect the lost connection.
2870                 // We have to increase the lcp-echo-interval to idletime*2 so that we
2871                 // do not upset the idletime counter.
2872                 // When not using demand dialing, it only takes 15 seconds to detect
2873                 // the lost connection.
2874                 if (nvram_match("ppp_demand", "1"))
2875                         fprintf(fp, "demand\n"
2876                                 "idle %s\n"
2877                                 "10.112.112.112:10.112.112.113\n"
2878                                 "lcp-echo-interval %d\n"
2879                                 "lcp-echo-failure 3\n"
2880                                 "ipcp-accept-remote\n"
2881                                 "ipcp-accept-local\n"
2882                                 "connect true\n" "ktune\n", idletime,
2883                                 atoi(idletime) * 2);
2884                 else
2885                         fprintf(fp, "persist\n"
2886                                 "lcp-echo-interval 5\n"
2887                                 "lcp-echo-failure 10\n");
2888
2889                 fclose(fp);
2890
2891                 symlink("/sbin/rc", "/tmp/ppp/ip-up");
2892                 symlink("/sbin/rc", "/tmp/ppp/ip-down");
2893                 unlink("/tmp/ppp/log");
2894
2895                 // Clean pppoe linksys client files - Added by ice-man (Wed Jun 1)
2896                 unlink("/tmp/ppp/connect-log");
2897                 unlink("/tmp/ppp/set-pppoepid");
2898
2899                 stop_dhcpc();
2900 #ifdef HAVE_PPTP
2901                 stop_pptp();
2902 #endif
2903                 eval("killall", "pppd");
2904                 eval("pppd", "file", "/tmp/ppp/options.pppoe");
2905
2906                 // This is horrible.
2907                 // What if pppoe recconects with ppp1?
2908
2909                 /*
2910                  * Pretend that the WAN interface is up
2911                  */
2912                 if (nvram_match("ppp_demand", "1")) {
2913                         /*
2914                          * Wait for ppp0 to be created
2915                          */
2916                         while (ifconfig("ppp0", IFUP, NULL, NULL) && timeout--)
2917                                 sleep(1);
2918                         strncpy(ifr.ifr_name, "ppp0", IFNAMSIZ);
2919
2920                         /*
2921                          * Set temporary IP address
2922                          */
2923                         timeout = 3;
2924                         while (ioctl(s, SIOCGIFADDR, &ifr) && timeout--) {
2925                                 perror("ppp0");
2926                                 printf("Wait ppp inteface to init (1) ...\n");
2927                                 sleep(1);
2928                         };
2929                         char client[32];
2930
2931                         nvram_set("wan_ipaddr",
2932                                   inet_ntop(AF_INET, &sin_addr(&ifr.ifr_addr),
2933                                             client, 16));
2934                         nvram_set("wan_netmask", "255.255.255.255");
2935
2936                         /*
2937                          * Set temporary P-t-P address
2938                          */
2939                         timeout = 3;
2940                         while (ioctl(s, SIOCGIFDSTADDR, &ifr) && timeout--) {
2941                                 perror("ppp0");
2942                                 printf("Wait ppp inteface to init (2) ...\n");
2943                                 sleep(1);
2944                         }
2945                         char *peer =
2946                             inet_ntop(AF_INET, &sin_addr(&ifr.ifr_dstaddr),
2947                                       client,
2948                                       16);
2949
2950                         nvram_set("wan_gateway", peer);
2951
2952                         start_wan_done("ppp0");
2953
2954                         // if user press Connect" button from web, we must force to dial
2955                         if (nvram_match("action_service", "start_pppoe")) {
2956                                 sleep(3);
2957                                 start_force_to_dial();
2958                                 nvram_unset("action_service");
2959                         }
2960                 } else {
2961                         if (status != REDIAL) {
2962                                 start_redial();
2963                         }
2964                 }
2965         } else
2966 #endif
2967         if (strcmp(wan_proto, "dhcp") == 0) {
2968                 start_dhcpc(wan_ifname);
2969         }
2970 #ifdef HAVE_PPTP
2971         else if (strcmp(wan_proto, "pptp") == 0) {
2972                 start_pptp(status);
2973         }
2974 #endif
2975 #ifdef HAVE_L2TP
2976         else if (strcmp(wan_proto, "l2tp") == 0) {
2977                 start_dhcpc(wan_ifname);
2978         }
2979 #endif
2980 #ifdef HAVE_HEARTBEAT
2981         else if (strcmp(wan_proto, "heartbeat") == 0) {
2982                 start_dhcpc(wan_ifname);
2983         }
2984 #endif
2985         else {
2986                 ifconfig(wan_ifname, IFUP,
2987                          nvram_safe_get("wan_ipaddr"),
2988                          nvram_safe_get("wan_netmask"));
2989                 start_wan_done(wan_ifname);
2990         }
2991         cprintf("dhcp client ready\n");
2992
2993         /*
2994          * Get current WAN hardware address
2995          */
2996         if (!strncmp(wan_ifname, "ppp", 3))
2997                 strncpy(ifr.ifr_name, nvram_safe_get("wan_ifname"), IFNAMSIZ);
2998         else
2999                 strncpy(ifr.ifr_name, wan_ifname, IFNAMSIZ);
3000         cprintf("get current hardware adress");
3001         {
3002                 if (ioctl(s, SIOCGIFHWADDR, &ifr) == 0) {
3003                         char eabuf[32];
3004
3005                         nvram_set("wan_hwaddr",
3006                                   ether_etoa(ifr.ifr_hwaddr.sa_data, eabuf));
3007                         // fprintf(stderr,"write wan addr
3008                         // %s\n",nvram_safe_get("wan_hwaddr"));
3009                 }
3010
3011         }
3012
3013         close(s);
3014
3015         // set_ip_forward('1');
3016
3017         // ===================================================================================
3018         // Tallest move herei(from "start_lan" function ). Fixed when wireless
3019         // disable, wireless LED dosen't off.
3020         // ===================================================================================
3021         /*
3022          * Disable wireless will cause diag led blink, so we want to stop it.
3023          */
3024
3025         cprintf("diag led control\n");
3026         if ((check_hw_type() == BCM4712_CHIP)
3027             || (check_hw_type() == BCM5325E_CHIP)) {
3028                 // Barry will put disable WLAN here
3029                 if (nvram_match("wl_gmode", "-1")) {
3030                         diag_led(WL, STOP_LED);
3031 #if 0
3032                         eval("wlled", "0 0 1");
3033                         eval("wlled", "0 1 1");
3034 #endif
3035                 }
3036                 diag_led(DIAG, STOP_LED);
3037         }
3038         /*
3039          * Light or go out the DMZ led even if there is no wan ip.
3040          */
3041         if (nvram_match("dmz_enable", "1") && nvram_invmatch("dmz_ipaddr", "")
3042             && nvram_invmatch("dmz_ipaddr", "0"))
3043                 diag_led(DMZ, START_LED);
3044         else
3045                 diag_led(DMZ, STOP_LED);
3046
3047         cprintf("%s %s\n", nvram_safe_get("wan_ipaddr"),
3048                 nvram_safe_get("wan_netmask"));
3049
3050         if (nvram_match("wan_proto", "l2tp")) {
3051                 /*
3052                  * Delete all default routes
3053                  */
3054                 while (route_del(get_wan_face(), 0, NULL, NULL, NULL) == 0) ;
3055         }
3056         cprintf("wep handling\n");
3057         cprintf("disable stp if needed\n");
3058         if (nvram_match("lan_stp", "0")) {
3059 #ifdef HAVE_MICRO
3060                 br_init();
3061 #endif
3062
3063                 br_set_stp_state("br0", 0);
3064
3065 #ifdef HAVE_MICRO
3066                 br_shutdown();
3067 #endif
3068
3069         } else {
3070 #ifdef HAVE_MICRO
3071                 br_init();
3072 #endif
3073
3074                 br_set_stp_state("br0", 1);
3075 #ifdef HAVE_MICRO
3076                 br_shutdown();
3077 #endif
3078
3079         }
3080
3081         cprintf("done()()()\n");
3082 }
3083
3084 void start_wan_boot(void)
3085 {
3086         start_wan(BOOT);
3087 }
3088
3089 void start_wan_redial(void)
3090 {
3091         start_wan(REDIAL);
3092 }
3093
3094 void start_wan_service(void)
3095 {
3096         stop_process_monitor();
3097         stop_ddns();
3098         cprintf("start process monitor\n");
3099         start_process_monitor();
3100         sleep(5);
3101         cprintf("start ddns\n");
3102         start_ddns();
3103 }
3104
3105 void start_wan_done(char *wan_ifname)
3106 {
3107         cprintf("%s %s\n", wan_ifname, nvram_safe_get("wan_proto"));
3108
3109         if (nvram_match("wan_proto", "l2tp")) {
3110                 /*
3111                  * Delete all default routes
3112                  */
3113                 while (route_del
3114                        (nvram_safe_get("wan_ifname"), 0, NULL, NULL,
3115                         NULL) == 0) ;
3116         }
3117
3118         /*
3119          * Delete all default routes
3120          */
3121         while (route_del(wan_ifname, 0, NULL, NULL, NULL) == 0) ;
3122
3123         if ((nvram_match("wan_proto", "pppoe")) && check_wan_link(1)) {
3124                 while (route_del
3125                        (nvram_safe_get("wan_ifname_1"), 0, NULL, NULL,
3126                         NULL) == 0) ;
3127         }
3128
3129         if (nvram_invmatch("wan_proto", "disabled")) {
3130                 int timeout = 5;
3131
3132                 /*
3133                  * Set default route to gateway if specified
3134                  */
3135                 char *gateway = nvram_match("wan_proto",
3136                                             "pptp") ?
3137                     nvram_safe_get("pptp_get_ip") :
3138                     nvram_safe_get("wan_gateway");
3139                 if (strcmp(gateway, "0.0.0.0"))
3140                         while (route_add
3141                                (wan_ifname, 0, "0.0.0.0", gateway, "0.0.0.0")
3142                                && timeout--) {
3143                                 if ((nvram_match("wan_proto", "pppoe"))
3144                                     && nvram_match("ppp_demand", "1")) {
3145                                         printf
3146                                             ("Wait ppp interface to init (3) ...\n");
3147                                         sleep(1);
3148                                 } else
3149                                         break;
3150
3151                         }
3152         }
3153
3154         /*
3155          * Delete all default routes
3156          */
3157 //     while (route_del(wan_ifname, 0, NULL, NULL, NULL) == 0);
3158
3159         /*
3160          * Set default route to gateway if specified
3161          */
3162         /*
3163          * while (strcmp(nvram_safe_get("wan_proto"), "disabled") &&
3164          * route_add(wan_ifname, 0, "0.0.0.0", nvram_safe_get("wan_gateway"),
3165          * "0.0.0.0") && timeout-- ){ if (nvram_match("wan_proto", "pppoe") &&
3166          * nvram_match("ppp_demand", "1")) { printf("Wait ppp interface to init
3167          * (3) ...\n"); sleep(1); } }
3168          */
3169         if (nvram_match("wan_proto", "pptp")) {
3170                 route_del(nvram_safe_get("wan_iface"), 0,
3171                           nvram_safe_get("wan_gateway"), NULL,
3172                           "255.255.255.255");
3173                 route_del(nvram_safe_get("wan_iface"), 0,
3174                           nvram_safe_get("pptp_server_ip"), NULL,
3175                           "255.255.255.255");
3176                 route_add(nvram_safe_get("wan_iface"), 0,
3177                           nvram_safe_get("pptp_get_ip"), NULL,
3178                           "255.255.255.255");
3179         } else if (nvram_match("wan_proto", "l2tp")) {
3180                 route_del(nvram_safe_get("wan_iface"), 0,
3181                           nvram_safe_get("wan_gateway"), NULL,
3182                           "255.255.255.255");
3183                 route_add(nvram_safe_get("wan_iface"), 0,
3184                           nvram_safe_get("l2tp_get_ip"), NULL,
3185                           "255.255.255.255");
3186                 route_add(nvram_safe_get("wan_ifname"), 0, nvram_safe_get("l2tp_server_ip"), nvram_safe_get("wan_gateway_buf"), "255.255.255.255");     // fixed
3187                 // routing
3188                 // problem
3189                 // in
3190                 // Israel
3191                 // by
3192                 // kanki
3193         }
3194
3195         /*
3196          * save dns to resolv.conf
3197          */
3198         cprintf("dns to resolv\n");
3199         dns_to_resolv();
3200
3201         cprintf("restart dhcp server\n");
3202         /*
3203          * Restart DHCP server
3204          */
3205 #ifdef HAVE_UDHCPD
3206         stop_udhcpd();
3207         start_udhcpd();
3208 #endif
3209         cprintf("restart dns proxy\n");
3210         /*
3211          * Restart DNS proxy stop_dnsmasq (); start_dnsmasq ();
3212          */
3213         cprintf("start firewall\n");
3214         /*
3215          * Start firewall
3216          */
3217         start_firewall();
3218
3219         /*
3220          * Set additional wan static routes if need
3221          */
3222
3223         start_set_routes();
3224         cprintf("routes done\n");
3225         if (nvram_match("wan_proto", "pppoe")
3226             || nvram_match("wan_proto", "pptp")
3227             || nvram_match("wan_proto", "l2tp")
3228             || nvram_match("wan_proto", "3g")) {
3229                 if (nvram_match("ppp_demand", "1")) {   // ntp and ddns will trigger DOD, so we must
3230                         // stop them when wan is unavaile.
3231                         FILE *fp;
3232
3233                         if ((fp = fopen("/tmp/ppp/link", "r"))) {
3234                                 start_wan_service();
3235                                 fclose(fp);
3236                         }
3237                 } else {
3238                         start_wan_service();
3239                 }
3240         } else {
3241                 start_wan_service();
3242         }
3243
3244 #ifdef HAVE_UPNP
3245         stop_upnp();
3246 #endif
3247 #ifdef HAVE_BIRD
3248         stop_zebra();
3249 #endif
3250         // stop_cron ();
3251         stop_wshaper();
3252         cprintf("start zebra\n");
3253 #ifdef HAVE_BIRD
3254         start_zebra();
3255 #endif
3256         cprintf("start upnp\n");
3257 #ifdef HAVE_UPNP
3258         start_upnp();
3259 #endif
3260         // cprintf ("start cron\n");
3261         // start_OAcron ();
3262         cprintf("start wshaper\n");
3263         stop_wland();
3264         start_wshaper();
3265         start_wland();
3266         if (nvram_match("wan_proto", "pptp")) {
3267
3268                 if (nvram_invmatch("pptp_customipup", "")) {
3269
3270                         // We not going to assume that /tmp/ppp is created..
3271                         mkdir("/tmp/ppp", 0700);
3272
3273                         // Create our custom pptp ipup script and change its attributes
3274                         nvram2file("pptp_customipup",
3275                                    "/tmp/ppp/sh_pptp_customipup");
3276                         chmod("/tmp/ppp/sh_pptp_customipup", 0744);
3277
3278                         // Execute our custom ipup script
3279                         system2("/tmp/ppp/sh_pptp_customipup");
3280
3281                 }
3282         }
3283         cprintf("std on\n");
3284 #ifdef HAVE_MICRO
3285         br_init();
3286 #endif
3287
3288         if (nvram_match("lan_stp", "0"))
3289                 br_set_stp_state(nvram_safe_get("lan_ifname"), 0);
3290         else
3291                 br_set_stp_state(nvram_safe_get("lan_ifname"), 1);
3292 #ifdef HAVE_MICRO
3293         br_shutdown();
3294 #endif
3295
3296         cprintf("check wan link\n");
3297         if (check_wan_link(0))
3298                 SET_LED(GOT_IP);
3299         else if ((!check_wan_link(0)) && nvram_match("wan_proto", "auto")) {
3300                 SET_LED(GET_IP_ERROR);
3301         }
3302         /*
3303          * check ip addresses for validity
3304          */
3305         uint32 wanip;
3306         uint32 wannm;
3307
3308         inet_aton(nvram_safe_get("wan_ipaddr"), (struct in_addr *)&wanip);
3309         inet_aton(nvram_safe_get("wan_netmask"), (struct in_addr *)&wannm);
3310         uint32 lanip;
3311         uint32 lannm;
3312
3313         inet_aton(nvram_safe_get("lan_ipaddr"), (struct in_addr *)&lanip);
3314         inet_aton(nvram_safe_get("lan_netmask"), (struct in_addr *)&lannm);
3315
3316         if (wanip != 0 && nvram_match("wan_ipaddr", "0.0.0.0")
3317             && !nvram_match("wan_proto", "disabled")) {
3318                 int iperror = 0;
3319
3320                 if ((wanip & wannm) == (lanip & wannm))
3321                         iperror = 1;
3322                 if ((lanip & lannm) == (wanip & lannm))
3323                         iperror = 1;
3324                 if (iperror)
3325                         eval("ledtool", "5");   // blink 5 times the 3 time interval
3326         }
3327         /*
3328          * end
3329          */
3330
3331         cprintf("running custom DD-WRT ipup scripts\n");
3332         runStartup("/etc/config", ".ipup");
3333 #ifdef HAVE_RB500
3334         runStartup("/usr/local/etc/config", ".ipup");
3335 #else
3336         runStartup("/jffs/etc/config", ".ipup");
3337         runStartup("/mmc/etc/config", ".ipup");
3338 #endif
3339         cprintf("trigger gpio");
3340
3341         led_control(LED_CONNECTED, LED_ON);
3342
3343         if (!nvram_match("wan_proto", "disabled"))
3344                 dd_syslog(LOG_INFO, "WAN is up. IP: %s\n",
3345                           nvram_safe_get("wan_ipaddr"));
3346
3347         float sys_uptime;
3348         FILE *up;
3349
3350         up = fopen("/proc/uptime", "r");
3351         fscanf(up, "%f", &sys_uptime);
3352         fclose(up);
3353
3354         up = fopen("/tmp/.wanuptime", "w");
3355         fprintf(up, "%f", sys_uptime);
3356         fclose(up);
3357
3358         cprintf("done\n");
3359
3360         nvram_set("wan_iface", nvram_safe_get("wan_ifname"));
3361
3362 #ifdef HAVE_OPENVPN
3363         cprintf("starting openvpn\n");
3364         stop_openvpn();
3365         start_openvpn();
3366         cprintf("done\n");
3367
3368 #endif
3369 #ifdef HAVE_OPENVPN
3370         stop_openvpnserverwan();
3371         start_openvpnserverwan();
3372 #endif
3373 #ifdef HAVE_DHCPFORWARD
3374         stop_dhcpfwd();
3375         start_dhcpfwd();
3376 #endif
3377         nvram_set("wanup", "1");
3378 #ifdef HAVE_MILKFISH
3379         if (nvram_match("milkfish_enabled", "1")) {
3380                 cprintf("starting milkfish netup script\n");
3381                 eval("/etc/config/milkfish.netup");
3382         }
3383 #endif
3384 #ifdef HAVE_SPUTNIK_APD
3385         stop_sputnik();
3386         start_sputnik();
3387 #endif
3388
3389 #ifdef HAVE_FON
3390 #ifdef HAVE_MICRO
3391         br_init();
3392 #endif
3393
3394         if (nvram_match("wl0_mode", "apsta")) {
3395                 br_del_interface(nvram_safe_get("lan_ifname"), "wl0.1");
3396                 ifconfig("wl0.1", IFUP | IFF_ALLMULTI, "0.0.0.0", NULL);
3397         } else if (nvram_match("wl0_mode", "ap")) {
3398                 br_del_interface(nvram_safe_get("lan_ifname"), get_wdev());
3399                 ifconfig(get_wdev(), IFUP | IFF_ALLMULTI, "0.0.0.0", NULL);
3400         }
3401 #ifdef HAVE_CHILLI
3402         stop_chilli();
3403         start_chilli();
3404 #endif
3405 #else
3406         if (nvram_match("fon_enable", "1")
3407             || (nvram_match("chilli_nowifibridge", "1")
3408                 && nvram_match("chilli_enable", "1"))) {
3409                 if (nvram_match("wl0_mode", "apsta")) {
3410                         br_del_interface(nvram_safe_get("lan_ifname"), "wl0.1");
3411                         ifconfig("wl0.1", IFUP | IFF_ALLMULTI, "0.0.0.0", NULL);
3412                 } else if (nvram_match("wl0_mode", "ap")) {
3413                         br_del_interface(nvram_safe_get("lan_ifname"),
3414                                          get_wdev());
3415                         ifconfig(get_wdev(), IFUP | IFF_ALLMULTI, "0.0.0.0",
3416                                  NULL);
3417                 }
3418 #ifdef HAVE_CHILLI
3419                 stop_chilli();
3420                 start_chilli();
3421 #endif
3422         }
3423 #endif
3424 #ifdef HAVE_MICRO
3425         br_shutdown();
3426 #endif
3427 #if defined(HAVE_MADWIFI) || defined(HAVE_RT2880)
3428 #ifndef HAVE_NOWIFI
3429         start_hostapdwan();
3430 #endif
3431 #endif
3432         cprintf("start igmp proxy\n");
3433 #ifdef HAVE_MULTICAST
3434         if (!nvram_match("dtag_vlan8", "1"))
3435                 stop_igmp_proxy();
3436         start_igmp_proxy();
3437 #endif
3438         cprintf("ready\n");
3439         start_anchorfree();
3440         start_anchorfreednat();
3441 #ifdef HAVE_MADWIFI
3442         start_duallink();
3443 #endif
3444 }
3445
3446 void stop_wan(void)
3447 {
3448         char *wan_ifname = get_wan_face();
3449
3450         nvram_set("wanup", "0");
3451
3452         led_control(LED_CONNECTED, LED_OFF);
3453         unlink("/tmp/.wanuptime");
3454
3455         cprintf("%s %s\n", wan_ifname, nvram_safe_get("wan_proto"));
3456 #ifdef HAVE_OPENVPN
3457         stop_openvpnserverwan();
3458         stop_openvpn();
3459 #endif
3460 #ifdef HAVE_DHCPFORWARD
3461         stop_dhcpfwd();
3462 #endif
3463         /*
3464          * Stop firewall
3465          */
3466         stop_firewall();
3467         /*
3468          * Kill any WAN client daemons or callbacks
3469          */
3470 #ifdef HAVE_PPPOE
3471         stop_pppoe();
3472 #endif
3473 #ifdef HAVE_L2TP
3474         stop_l2tp();
3475 #endif
3476         stop_dhcpc();
3477 #ifdef HAVE_HEARTBEAT
3478         stop_heartbeat();
3479 #endif
3480 #ifdef HAVE_PPTP
3481         stop_pptp();
3482 #endif
3483 #ifdef HAVE_SPUTNIK_APD
3484         stop_sputnik();
3485 #endif
3486         stop_ntpc();
3487         stop_redial();
3488         nvram_set("wan_get_dns", "");
3489
3490         // Reset pppd's pppX interface
3491         nvram_set("pppd_pppifname", "");
3492
3493         /*
3494          * Bring down WAN interfaces
3495          */
3496         ifconfig(wan_ifname, 0, NULL, NULL);
3497         eval("ifconfig", wan_ifname, "down");   // to allow for MAC clone to
3498         // take effect
3499 #ifdef HAVE_PPP
3500 #endif
3501 #ifndef HAVE_FON
3502         if (nvram_match("fon_enable", "1")
3503             || (nvram_match("chilli_nowifibridge", "1")
3504                 && nvram_match("chilli_enable", "1")))
3505 #endif
3506         {
3507 #ifdef HAVE_MICRO
3508                 br_init();
3509 #endif
3510
3511                 br_add_interface(getBridge(get_wdev()), get_wdev());
3512 #ifdef HAVE_MICRO
3513                 br_shutdown();
3514 #endif
3515
3516         }
3517
3518         cprintf("done\n");
3519 }
3520
3521 void start_set_routes(void)
3522 {
3523         char word[80], *tmp;
3524         char *ipaddr, *netmask, *gateway, *metric, *ifname;
3525
3526         if (!nvram_match("lan_gateway", "0.0.0.0")) {
3527                 eval("route", "del", "default");
3528                 eval("route", "add", "default", "gw",
3529                      nvram_safe_get("lan_gateway"));
3530         }
3531         char *defgateway;
3532
3533         if (nvram_match("wan_proto", "pptp"))
3534                 defgateway = nvram_safe_get("pptp_get_ip");
3535         else if (nvram_match("wan_proto", "l2tp"))
3536                 defgateway = nvram_safe_get("l2tp_get_ip");
3537         else
3538                 defgateway = nvram_safe_get("wan_gateway");
3539         if (strcmp(defgateway, "0.0.0.0")
3540             && !nvram_match("wan_proto", "disabled")) {
3541                 eval("route", "del", "default");
3542                 eval("route", "add", "default", "gw", defgateway);
3543         }
3544         foreach(word, nvram_safe_get("static_route"), tmp) {
3545                 netmask = word;
3546                 ipaddr = strsep(&netmask, ":");
3547                 if (!ipaddr || !netmask)
3548                         continue;
3549                 gateway = netmask;
3550                 netmask = strsep(&gateway, ":");
3551                 if (!netmask || !gateway)
3552                         continue;
3553                 metric = gateway;
3554                 gateway = strsep(&metric, ":");
3555                 if (!gateway || !metric)
3556                         continue;
3557                 ifname = metric;
3558                 metric = strsep(&ifname, ":");
3559                 if (!metric || !ifname)
3560                         continue;
3561                 if (!strcmp(ipaddr, "0.0.0.0") && !strcmp(gateway, "0.0.0.0"))
3562                         continue;
3563                 if (!strcmp(ipaddr, "0.0.0.0")) {
3564                         eval("route", "del", "default");
3565                         eval("route", "add", "default", "gw", gateway);
3566                 } else if (!strcmp(ifname, "any")) {
3567                         eval("route", "add", "-net", ipaddr, "netmask", netmask,
3568                              "gw", gateway, "metric", metric);
3569                 } else
3570                         route_add(ifname, atoi(metric) + 1, ipaddr, gateway,
3571                                   netmask);
3572         }
3573         if (f_exists("/tmp/tvrouting"))
3574         system("sh /tmp/tvrouting");
3575 }
3576
3577 #if !defined(HAVE_MADWIFI) && !defined(HAVE_RT2880)
3578 static int notify_nas(char *type, char *ifname, char *action)
3579 {
3580         char *argv[] = { "nas4not", type, ifname, action,
3581                 NULL,           /* role */
3582                 NULL,           /* crypto */
3583                 NULL,           /* auth */
3584                 NULL,           /* passphrase */
3585                 NULL,           /* ssid */
3586                 NULL
3587         };
3588         char *str = NULL;
3589         int retries = 10;
3590         char tmp[100], prefix[] = "wlXXXXXXXXXX_";
3591         int unit;
3592         char remote[ETHER_ADDR_LEN];
3593         char ssid[48], pass[80], auth[16], crypto[16], role[8];
3594         int i;
3595
3596         /*
3597          * the wireless interface must be configured to run NAS
3598          */
3599         wl_ioctl(ifname, WLC_GET_INSTANCE, &unit, sizeof(unit));
3600         snprintf(prefix, sizeof(prefix), "wl%d_", unit);
3601         if (nvram_match(strcat_r(prefix, "akm", tmp), "") &&
3602             nvram_match(strcat_r(prefix, "auth_mode", tmp), "none"))
3603                 return 0;
3604
3605         while (retries-- > 0 && !(str = file2str("/tmp/nas.wl0lan.pid")))
3606                 sleep(1);
3607         if (!str) {
3608                 return -1;
3609         }
3610         free(str);
3611         sleep(3);
3612         /*
3613          * find WDS link configuration
3614          */
3615         wl_ioctl(ifname, WLC_WDS_GET_REMOTE_HWADDR, remote, ETHER_ADDR_LEN);
3616         for (i = 0; i < MAX_NVPARSE; i++) {
3617                 char mac[ETHER_ADDR_STR_LEN];
3618                 uint8 ea[ETHER_ADDR_LEN];
3619
3620                 if (get_wds_wsec(unit, i, mac, role, crypto, auth, ssid, pass)
3621                     && ether_atoe(mac, ea)
3622                     && !bcmp(ea, remote, ETHER_ADDR_LEN)) {
3623                         argv[4] = role;
3624                         argv[5] = crypto;
3625                         argv[6] = auth;
3626                         argv[7] = pass;
3627                         argv[8] = ssid;
3628                         break;
3629                 }
3630         }
3631
3632         /*
3633          * did not find WDS link configuration, use wireless'
3634          */
3635         if (i == MAX_NVPARSE) {
3636                 /*
3637                  * role
3638                  */
3639                 argv[4] = "auto";
3640                 /*
3641                  * crypto
3642                  */
3643                 argv[5] = nvram_safe_get(strcat_r(prefix, "crypto", tmp));
3644                 /*
3645                  * auth mode
3646                  */
3647                 argv[6] = nvram_safe_get(strcat_r(prefix, "akm", tmp));
3648                 /*
3649                  * passphrase
3650                  */
3651                 argv[7] = nvram_safe_get(strcat_r(prefix, "wpa_psk", tmp));
3652                 /*
3653                  * ssid
3654                  */
3655                 argv[8] = nvram_safe_get(strcat_r(prefix, "ssid", tmp));
3656         }
3657         int pid;
3658
3659         return _evalpid(argv, ">/dev/console", 0, &pid);
3660 }
3661 #endif
3662 /*
3663  * static int notify_nas(char *type, char *ifname, char *action) { char
3664  * *argv[] = {"nas4not", type, ifname, action, NULL, NULL, NULL, NULL,
3665  * NULL, NULL}; char *str = NULL; int retries = 10; char tmp[100], prefix[]
3666  * = "wlXXXXXXXXXX_"; int unit; char remote[ETHER_ADDR_LEN]; char ssid[48],
3667  * pass[80], auth[16], crypto[16], role[8]; int i;
3668  *
3669  * wl_ioctl(ifname, WLC_GET_INSTANCE, &unit, sizeof(unit)); snprintf(prefix,
3670  * sizeof(prefix), "wl%d_", unit); #ifdef WPA2_WMM if
3671  * (nvram_match(strcat_r(prefix, "akm", tmp), "") &&
3672  * nvram_match(strcat_r(prefix, "auth_mode", tmp), "none")) #else if
3673  * (nvram_match(strcat_r(prefix, "auth_mode", tmp), "open") ||
3674  * nvram_match(strcat_r(prefix, "auth_mode", tmp), "shared")) #endif return
3675  * 0;
3676  *
3677  * wl_ioctl(ifname, WLC_WDS_GET_REMOTE_HWADDR, remote, ETHER_ADDR_LEN); for
3678  * (i = 0; i < MAX_NVPARSE; i ++) { char mac[ETHER_ADDR_STR_LEN]; uint8
3679  * ea[ETHER_ADDR_LEN];
3680  *
3681  * if (get_wds_wsec(unit, i, mac, role, crypto, auth, ssid, pass) &&
3682  * ether_atoe(mac, ea) && !bcmp(ea, remote, ETHER_ADDR_LEN)) { argv[4] =
3683  * role; argv[5] = crypto; argv[6] = auth; argv[7] = pass; argv[8] = ssid;
3684  * break; } }
3685  *
3686  * if (i == MAX_NVPARSE) {
3687  *
3688  * argv[4] = "auto";
3689  *
3690  * argv[5] = nvram_safe_get(strcat_r(prefix, "crypto", tmp));
3691  *
3692  * #ifdef WPA2_WMM argv[6] = nvram_safe_get(strcat_r(prefix, "akm", tmp));
3693  * #else argv[6] = nvram_safe_get(strcat_r(prefix, "auth_mode", tmp)); #endif
3694  *
3695  * argv[7] = nvram_safe_get(strcat_r(prefix, "wpa_psk", tmp));
3696  *
3697  * argv[8] = nvram_safe_get(strcat_r(prefix, "ssid", tmp)); }
3698  *
3699  * while (retries -- > 0 && !(str = file2str("/tmp/nas.lan.pid"))) sleep(1);
3700  * if (str) { int pid; free(str); return _eval(argv, ">/dev/console", 0,
3701  * &pid); } return -1;
3702  *
3703  * }
3704  */
3705 void start_hotplug_net(void)
3706 {
3707 #ifdef HAVE_MADWIFI
3708         char *interface, *action;
3709
3710         interface = getenv("INTERFACE");
3711         if (!interface)
3712                 return;
3713         action = getenv("ACTION");
3714         if (!action)
3715                 return;
3716         // sysprintf("echo \"Hotplug %s=%s\" > /dev/console\n",action,interface);
3717         if (strncmp(interface, "ath", 3))
3718                 return;
3719
3720         // try to parse
3721         char ifname[32];
3722
3723         memset(ifname, 0, 32);
3724         int index = indexof(interface, '.');
3725
3726         if (index == -1)
3727                 return;
3728         strncpy(ifname, interface + index + 1, strlen(interface) - (index + 1));
3729         if (strncmp(ifname, "sta", 3)) {
3730                 return;
3731         }
3732         char nr[32];
3733
3734         memset(nr, 0, 32);
3735         strcpy(nr, ((unsigned char *)&ifname[0]) + 3);
3736         memset(ifname, 0, 32);
3737         strncpy(ifname, interface, index);
3738         char bridged[32];
3739
3740         sprintf(bridged, "%s_bridged", ifname);
3741
3742         if (!strcmp(action, "add")) {
3743                 fprintf(stderr, "adding WDS %s\n", interface);
3744
3745                 eval("ifconfig", interface, "up");
3746                 if (nvram_match(bridged, "1"))
3747                         br_add_interface(getBridge(ifname), interface);
3748         }
3749         if (!strcmp(action, "remove")) {
3750                 fprintf(stderr, "removing WDS %s\n", interface);
3751                 eval("ifconfig", interface, "down");
3752