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

Revision 12365, 97.0 kB (checked in by eko, 5 months ago)

fix wrt610n wireless MACs

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         /*
2528          * Configure WAN interface
2529          */
2530 #ifdef HAVE_3G
2531         if ((strcmp(wan_proto, "3g") == 0)) {
2532                 char *controldevice = get3GControlDevice();
2533
2534                 mkdir("/tmp/ppp", 0777);
2535                 int timeout = 5;
2536
2537                 /* init PIN */
2538                 if (strlen(nvram_safe_get("wan_pin")))
2539                         sysprintf("export COMGTPIN=%s;comgt PIN -d %s\n",
2540                                   nvram_safe_get("wan_pin"), controldevice);
2541                 if (strlen(nvram_safe_get("wan_apn")))
2542                         if (!nvram_match("wan_dial", "2"))
2543                                 sysprintf
2544                                     ("export COMGTAPN=\"%s\";comgt APN -d %s\n",
2545                                      nvram_safe_get("wan_apn"), controldevice);
2546                 // Lets open option file and enter all the parameters.
2547                 fp = fopen("/tmp/ppp/options.pppoe", "w");
2548                 fprintf(fp, "defaultroute\n");
2549                 fprintf(fp, "usepeerdns\n");
2550                 fprintf(fp, "noipdefault\n");
2551                 fprintf(fp, "noauth\n");
2552                 fprintf(fp, "lcp-echo-interval 10\n");
2553                 fprintf(fp, "lcp-echo-failure 3\n");
2554                 if (nvram_match("mtu_enable", "1")) {
2555                         if (atoi(nvram_safe_get("wan_mtu")) > 0) {
2556                                 fprintf(fp, "mtu %s\n",
2557                                         nvram_safe_get("wan_mtu"));
2558                                 fprintf(fp, "mru %s\n",
2559                                         nvram_safe_get("wan_mtu"));
2560                         }
2561
2562                 }
2563                 fprintf(fp, "crtscts\n");
2564                 fprintf(fp, "460800\n");
2565 //      fprintf(fp,"connect \"/usr/sbin/comgt DIAL -d %s\"\n",controldevice);
2566                 char *dial = "*99***1#";
2567                 if (nvram_match("wan_dial", "0"))
2568                         dial = "ATD*99***1#";
2569                 if (nvram_match("wan_dial", "1"))
2570                         dial = "ATD*99#";
2571                 if (nvram_match("wan_dial", "2"))
2572                         dial = "ATDT#777";
2573                 fprintf(fp,
2574                         "connect \"COMGTDIAL='%s' /usr/sbin/comgt DIAL -d %s >/tmp/comgt.out 2>&1\"\n",
2575                         dial, nvram_safe_get("3gdata"));
2576                 if (strlen(nvram_safe_get("ppp_username")))
2577                         fprintf(fp, "user %s\n",
2578                                 nvram_safe_get("ppp_username"));
2579                 if (strlen(nvram_safe_get("ppp_passwd")))
2580                         fprintf(fp, "password %s\n",
2581                                 nvram_safe_get("ppp_passwd"));
2582                 fprintf(fp, "%s\n", nvram_get("3gdata"));
2583
2584                 fclose(fp);
2585
2586                 symlink("/sbin/rc", "/tmp/ppp/ip-up");
2587                 symlink("/sbin/rc", "/tmp/ppp/ip-down");
2588                 unlink("/tmp/ppp/log");
2589
2590                 // Clean pppoe linksys client files - Added by ice-man (Wed Jun 1)
2591                 unlink("/tmp/ppp/connect-log");
2592                 unlink("/tmp/ppp/set-pppoepid");
2593
2594                 stop_dhcpc();
2595 #ifdef HAVE_PPTP
2596                 stop_pptp();
2597 #endif
2598                 eval("pppd", "file", "/tmp/ppp/options.pppoe");
2599
2600                 /*
2601                  * Pretend that the WAN interface is up
2602                  */
2603                 if (nvram_match("ppp_demand", "1")) {
2604                         /*
2605                          * Wait for ppp0 to be created
2606                          */
2607                         while (ifconfig("ppp0", IFUP, NULL, NULL) && timeout--)
2608                                 sleep(1);
2609                         strncpy(ifr.ifr_name, "ppp0", IFNAMSIZ);
2610
2611                         /*
2612                          * Set temporary IP address
2613                          */
2614                         timeout = 3;
2615                         while (ioctl(s, SIOCGIFADDR, &ifr) && timeout--) {
2616                                 perror("ppp0");
2617                                 printf("Wait ppp inteface to init (1) ...\n");
2618                                 sleep(1);
2619                         };
2620                         char client[32];
2621
2622                         nvram_set("wan_ipaddr",
2623                                   inet_ntop(AF_INET, &sin_addr(&ifr.ifr_addr),
2624                                             client, 16));
2625                         nvram_set("wan_netmask", "255.255.255.255");
2626
2627                         /*
2628                          * Set temporary P-t-P address
2629                          */
2630                         timeout = 3;
2631                         while (ioctl(s, SIOCGIFDSTADDR, &ifr) && timeout--) {
2632                                 perror("ppp0");
2633                                 printf("Wait ppp inteface to init (2) ...\n");
2634                                 sleep(1);
2635                         }
2636                         char *peer =
2637                             inet_ntop(AF_INET, &sin_addr(&ifr.ifr_dstaddr),
2638                                       client,
2639                                       16);
2640
2641                         nvram_set("wan_gateway", peer);
2642
2643                         start_wan_done("ppp0");
2644
2645                         // if user press Connect" button from web, we must force to dial
2646                         if (nvram_match("action_service", "start_3g")) {
2647                                 sleep(3);
2648                                 start_force_to_dial();
2649                                 nvram_unset("action_service");
2650                         }
2651                 } else {
2652                         if (status != REDIAL) {
2653                                 start_redial();
2654                         }
2655                 }
2656
2657         } else
2658 #endif
2659 #ifdef HAVE_PPPOE
2660         if ((strcmp(wan_proto, "pppoe") == 0)) {
2661                 char username[80], passwd[80];
2662                 char idletime[20], retry_num[20];
2663
2664                 snprintf(idletime, sizeof(idletime), "%d",
2665                          atoi(nvram_safe_get("ppp_idletime")) * 60);
2666                 snprintf(retry_num, sizeof(retry_num), "%d",
2667                          (atoi(nvram_safe_get("ppp_redialperiod")) / 5) - 1);
2668
2669                 snprintf(username, sizeof(username), "%s",
2670                          nvram_safe_get("ppp_username"));
2671                 snprintf(passwd, sizeof(passwd), "%s",
2672                          nvram_safe_get("ppp_passwd"));
2673
2674                 mkdir("/tmp/ppp", 0777);
2675                 int timeout = 5;
2676
2677                 // Lets open option file and enter all the parameters.
2678                 fp = fopen("/tmp/ppp/options.pppoe", "w");
2679                 // rp-pppoe kernelmode plugin
2680 #if defined(HAVE_ADM5120) && !defined(HAVE_WP54G) && !defined(HAVE_NP28G)
2681                 fprintf(fp, "plugin /lib/rp-pppoe.so");
2682 #else
2683                 fprintf(fp, "plugin /usr/lib/rp-pppoe.so");
2684 #endif
2685                 if (nvram_invmatch("pppoe_service", ""))
2686                         fprintf(fp, " rp_pppoe_service %s",
2687                                 nvram_safe_get("pppoe_service"));
2688                 fprintf(fp, "\n");
2689                 char vlannic[32];
2690                 char tvnic[32];
2691
2692                 if (!strncmp(pppoe_wan_ifname, "vlan", 4)) {
2693                         if (nvram_match("wan_vdsl", "1")) {
2694                                 char *ifn = enable_dtag_vlan(1);
2695
2696                                 if (nvram_match("dtag_vlan8", "1")) {
2697                                         sprintf(vlannic, "%s.0008", ifn);
2698                                         if (!ifexists(vlannic)) {
2699                                                 eval("vconfig", "set_name_type",
2700                                                      "DEV_PLUS_VID");
2701                                                 eval("vconfig", "add", ifn,
2702                                                      "8");
2703                                                 eval("ifconfig", vlannic, "up");
2704                                         }
2705                                         nvram_set("tvnicfrom", vlannic);
2706                                         symlink("/sbin/rc", "/tmp/udhcpc_tv");
2707                                         eval("udhcpc", "-i", vlannic, "-s",
2708                                              "/tmp/udhcpc_tv", "-q");
2709                                 }
2710                                 sprintf(vlannic, "%s.0007", ifn);
2711                                 if (!ifexists(vlannic)) {
2712                                         eval("vconfig", "set_name_type",
2713                                              "DEV_PLUS_VID");
2714                                         eval("vconfig", "add", ifn, "7");
2715                                         eval("ifconfig", vlannic, "up");
2716                                 }
2717
2718                                 fprintf(fp, "nic-%s\n", vlannic);
2719                         } else {
2720                                 char *ifn = enable_dtag_vlan(0);
2721
2722                                 sprintf(vlannic, "%s.0007", ifn);
2723                                 if (ifexists(vlannic))
2724                                         eval("vconfig", "rem", vlannic);
2725                                 sprintf(vlannic, "%s.0008", ifn);
2726                                 if (ifexists(vlannic))
2727                                         eval("vconfig", "rem", vlannic);
2728                                 fprintf(fp, "nic-%s\n", pppoe_wan_ifname);
2729                         }
2730
2731                 } else {
2732                         if (nvram_match("wan_vdsl", "1"))       // Deutsche Telekom
2733                                 // VDSL2 Vlan 7 Tag
2734                         {
2735                                 if (nvram_match("dtag_vlan8", "1")) {
2736                                         sprintf(vlannic, "%s.0008",
2737                                                 pppoe_wan_ifname);
2738                                         if (!ifexists(vlannic)) {
2739                                                 eval("vconfig", "set_name_type",
2740                                                      "DEV_PLUS_VID");
2741                                                 eval("vconfig", "add",
2742                                                      pppoe_wan_ifname, "8");
2743                                                 eval("ifconfig", vlannic, "up");
2744                                                 nvram_set("tvnicfrom", vlannic);
2745                                                 symlink("/sbin/rc",
2746                                                         "/tmp/udhcpc_tv");
2747                                                 eval("udhcpc", "-i", vlannic,
2748                                                      "-s", "/tmp/udhcpc_tv",
2749                                                      "-q");
2750                                         }
2751                                 }
2752                                 sprintf(vlannic, "%s.0007", pppoe_wan_ifname);
2753                                 if (!ifexists(vlannic)) {
2754                                         eval("vconfig", "set_name_type",
2755                                              "DEV_PLUS_VID");
2756                                         eval("vconfig", "add", pppoe_wan_ifname,
2757                                              "7");
2758                                         eval("ifconfig", vlannic, "up");
2759                                 }
2760                                 fprintf(fp, "nic-%s\n", vlannic);
2761                         } else {
2762                                 sprintf(vlannic, "%s.0008", pppoe_wan_ifname);
2763                                 if (ifexists(vlannic))
2764                                         eval("vconfig", "rem", vlannic);
2765                                 sprintf(vlannic, "%s.0007", pppoe_wan_ifname);
2766                                 if (ifexists(vlannic))
2767                                         eval("vconfig", "rem", vlannic);
2768                                 fprintf(fp, "nic-%s\n", pppoe_wan_ifname);
2769                         }
2770                 }
2771
2772                 // Those are default options we use + user/passwd
2773                 // By using user/password options we dont have to deal with chap/pap
2774                 // secrets files.
2775                 if (nvram_match("ppp_compression", "1")) {
2776                         fprintf(fp, "mppc\n");
2777                 } else {
2778                         fprintf(fp, "noccp\n");
2779                         fprintf(fp, "nomppc\n");
2780                 }
2781                 fprintf(fp, "noipdefault\n"
2782                         "noauth\n"
2783                         "defaultroute\n" "noaccomp\n" "nobsdcomp\n"
2784                         "nodeflate\n"
2785                         // "debug\n"
2786                         // "maxfail 0\n"
2787                         // "nocrtscts\n"
2788                         // "sync\n"
2789                         // "local\n"
2790                         // "noixp\n"
2791                         // "lock\n"
2792                         // "noproxyarp\n"
2793                         // "ipcp-accept-local\n"
2794                         // "ipcp-accept-remote\n"
2795                         // "nodetach\n"
2796                         "nopcomp\n");
2797                 // "novj\n"
2798                 // "novjccomp\n");
2799                 if (nvram_invmatch("ppp_mppe", ""))
2800                         fprintf(fp, "%s\n", nvram_safe_get("ppp_mppe"));
2801                 else
2802                         fprintf(fp, "nomppe\n");
2803                 fprintf(fp, "usepeerdns\n"
2804                         "user '%s'\n" "password '%s'\n", username, passwd);
2805
2806                 // This is a tricky one. When used it could improve speed of PPPoE
2807                 // but not all ISP's can support it.
2808                 // default-asyncmap escapes all control characters. By using asyncmap
2809                 // 0 PPPD will not escape any control characters
2810                 // Not all ISP's can handle this. By default use default-asyncmap
2811                 // and if ppp_asyncmap=1 do not escape
2812                 if (nvram_match("ppp_asyncmap", "1"))
2813                         fprintf(fp, "asyncmap 0\n");
2814                 else
2815                         fprintf(fp, "default-asyncmap\n");
2816
2817                 // Allow users some control on PPP interface MTU and MRU
2818                 // If pppoe_ppp_mtu > 0 will set mtu of pppX interface to the value
2819                 // in the nvram variable
2820                 // If pppoe_ppp_mru > 0 will set mru of pppX interface to the value
2821                 // in the nvram variable
2822                 // if none is specified PPPD will autonegotiate the values with ISP
2823                 // (sometimes not desirable)
2824                 // Do not forget this should be at least 8 bytes less then physycal
2825                 // interfaces mtu.
2826
2827                 // if MRU is not Auto force MTU/MRU of interface to value selected by
2828                 // theuser on web page
2829                 if (nvram_match("mtu_enable", "1")) {
2830                         if (atoi(nvram_safe_get("wan_mtu")) > 0) {
2831                                 fprintf(fp, "mtu %s\n",
2832                                         nvram_safe_get("wan_mtu"));
2833                                 fprintf(fp, "mru %s\n",
2834                                         nvram_safe_get("wan_mtu"));
2835                         }
2836
2837                 } else {
2838                         // If MRU set to Auto we still allow custom MTU/MRU settings for
2839                         // expirienced users
2840                         if (nvram_invmatch("pppoe_ppp_mtu", ""))
2841                                 if (atoi(nvram_safe_get("pppoe_ppp_mtu")) > 0)
2842                                         fprintf(fp, "mtu %s\n",
2843                                                 nvram_safe_get
2844                                                 ("pppoe_ppp_mtu"));
2845                         if (nvram_invmatch("pppoe_ppp_mru", ""))
2846                                 if (atoi(nvram_safe_get("pppoe_ppp_mru")) > 0)
2847                                         fprintf(fp, "mru %s\n",
2848                                                 nvram_safe_get
2849                                                 ("pppoe_ppp_mru"));
2850                 }
2851
2852                 // Allow runtime debugging
2853                 if (nvram_match("ppp_debug", "1"))
2854                         fprintf(fp, "debug\n");
2855
2856                 // Demand dial.. This is not pretty.
2857                 // The first problems i see is that if connection is lost it would
2858                 // take PPPoE (idletime * 2) * 3 to notice it.
2859                 // In other words if idle is set to 30 seconds, it would take 30*2*3
2860                 // (180) seconds to detect the lost connection.
2861                 // We have to increase the lcp-echo-interval to idletime*2 so that we
2862                 // do not upset the idletime counter.
2863                 // When not using demand dialing, it only takes 15 seconds to detect
2864                 // the lost connection.
2865                 if (nvram_match("ppp_demand", "1"))
2866                         fprintf(fp, "demand\n"
2867                                 "idle %s\n"
2868                                 "10.112.112.112:10.112.112.113\n"
2869                                 "lcp-echo-interval %d\n"
2870                                 "lcp-echo-failure 3\n"
2871                                 "ipcp-accept-remote\n"
2872                                 "ipcp-accept-local\n"
2873                                 "connect true\n" "ktune\n", idletime,
2874                                 atoi(idletime) * 2);
2875                 else
2876                         fprintf(fp, "persist\n"
2877                                 "lcp-echo-interval 5\n"
2878                                 "lcp-echo-failure 10\n");
2879
2880                 fclose(fp);
2881
2882                 symlink("/sbin/rc", "/tmp/ppp/ip-up");
2883                 symlink("/sbin/rc", "/tmp/ppp/ip-down");
2884                 unlink("/tmp/ppp/log");
2885
2886                 // Clean pppoe linksys client files - Added by ice-man (Wed Jun 1)
2887                 unlink("/tmp/ppp/connect-log");
2888                 unlink("/tmp/ppp/set-pppoepid");
2889
2890                 stop_dhcpc();
2891 #ifdef HAVE_PPTP
2892                 stop_pptp();
2893 #endif
2894                 eval("killall", "pppd");
2895                 eval("pppd", "file", "/tmp/ppp/options.pppoe");
2896
2897                 // This is horrible.
2898                 // What if pppoe recconects with ppp1?
2899
2900                 /*
2901                  * Pretend that the WAN interface is up
2902                  */
2903                 if (nvram_match("ppp_demand", "1")) {
2904                         /*
2905                          * Wait for ppp0 to be created
2906                          */
2907                         while (ifconfig("ppp0", IFUP, NULL, NULL) && timeout--)
2908                                 sleep(1);
2909                         strncpy(ifr.ifr_name, "ppp0", IFNAMSIZ);
2910
2911                         /*
2912                          * Set temporary IP address
2913                          */
2914                         timeout = 3;
2915                         while (ioctl(s, SIOCGIFADDR, &ifr) && timeout--) {
2916                                 perror("ppp0");
2917                                 printf("Wait ppp inteface to init (1) ...\n");
2918                                 sleep(1);
2919                         };
2920                         char client[32];
2921
2922                         nvram_set("wan_ipaddr",
2923                                   inet_ntop(AF_INET, &sin_addr(&ifr.ifr_addr),
2924                                             client, 16));
2925                         nvram_set("wan_netmask", "255.255.255.255");
2926
2927                         /*
2928                          * Set temporary P-t-P address
2929                          */
2930                         timeout = 3;
2931                         while (ioctl(s, SIOCGIFDSTADDR, &ifr) && timeout--) {
2932                                 perror("ppp0");
2933                                 printf("Wait ppp inteface to init (2) ...\n");
2934                                 sleep(1);
2935                         }
2936                         char *peer =
2937                             inet_ntop(AF_INET, &sin_addr(&ifr.ifr_dstaddr),
2938                                       client,
2939                                       16);
2940
2941                         nvram_set("wan_gateway", peer);
2942
2943                         start_wan_done("ppp0");
2944
2945                         // if user press Connect" button from web, we must force to dial
2946                         if (nvram_match("action_service", "start_pppoe")) {
2947                                 sleep(3);
2948                                 start_force_to_dial();
2949                                 nvram_unset("action_service");
2950                         }
2951                 } else {
2952                         if (status != REDIAL) {
2953                                 start_redial();
2954                         }
2955                 }
2956         } else
2957 #endif
2958         if (strcmp(wan_proto, "dhcp") == 0) {
2959                 start_dhcpc(wan_ifname);
2960         }
2961 #ifdef HAVE_PPTP
2962         else if (strcmp(wan_proto, "pptp") == 0) {
2963                 start_pptp(status);
2964         }
2965 #endif
2966 #ifdef HAVE_L2TP
2967         else if (strcmp(wan_proto, "l2tp") == 0) {
2968                 start_dhcpc(wan_ifname);
2969         }
2970 #endif
2971 #ifdef HAVE_HEARTBEAT
2972         else if (strcmp(wan_proto, "heartbeat") == 0) {
2973                 start_dhcpc(wan_ifname);
2974         }
2975 #endif
2976         else {
2977                 ifconfig(wan_ifname, IFUP,
2978                          nvram_safe_get("wan_ipaddr"),
2979                          nvram_safe_get("wan_netmask"));
2980                 start_wan_done(wan_ifname);
2981         }
2982         cprintf("dhcp client ready\n");
2983
2984         /*
2985          * Get current WAN hardware address
2986          */
2987         if (!strncmp(wan_ifname, "ppp", 3))
2988                 strncpy(ifr.ifr_name, nvram_safe_get("wan_ifname"), IFNAMSIZ);
2989         else
2990                 strncpy(ifr.ifr_name, wan_ifname, IFNAMSIZ);
2991         cprintf("get current hardware adress");
2992         {
2993                 if (ioctl(s, SIOCGIFHWADDR, &ifr) == 0) {
2994                         char eabuf[32];
2995
2996                         nvram_set("wan_hwaddr",
2997                                   ether_etoa(ifr.ifr_hwaddr.sa_data, eabuf));
2998                         // fprintf(stderr,"write wan addr
2999                         // %s\n",nvram_safe_get("wan_hwaddr"));
3000                 }
3001
3002         }
3003
3004         close(s);
3005
3006         // set_ip_forward('1');
3007
3008         // ===================================================================================
3009         // Tallest move herei(from "start_lan" function ). Fixed when wireless
3010         // disable, wireless LED dosen't off.
3011         // ===================================================================================
3012         /*
3013          * Disable wireless will cause diag led blink, so we want to stop it.
3014          */
3015
3016         cprintf("diag led control\n");
3017         if ((check_hw_type() == BCM4712_CHIP)
3018             || (check_hw_type() == BCM5325E_CHIP)) {
3019                 // Barry will put disable WLAN here
3020                 if (nvram_match("wl_gmode", "-1")) {
3021                         diag_led(WL, STOP_LED);
3022 #if 0
3023                         eval("wlled", "0 0 1");
3024                         eval("wlled", "0 1 1");
3025 #endif
3026                 }
3027                 diag_led(DIAG, STOP_LED);
3028         }
3029         /*
3030          * Light or go out the DMZ led even if there is no wan ip.
3031          */
3032         if (nvram_match("dmz_enable", "1") && nvram_invmatch("dmz_ipaddr", "")
3033             && nvram_invmatch("dmz_ipaddr", "0"))
3034                 diag_led(DMZ, START_LED);
3035         else
3036                 diag_led(DMZ, STOP_LED);
3037
3038         cprintf("%s %s\n", nvram_safe_get("wan_ipaddr"),
3039                 nvram_safe_get("wan_netmask"));
3040
3041         if (nvram_match("wan_proto", "l2tp")) {
3042                 /*
3043                  * Delete all default routes
3044                  */
3045                 while (route_del(get_wan_face(), 0, NULL, NULL, NULL) == 0) ;
3046         }
3047         cprintf("wep handling\n");
3048         cprintf("disable stp if needed\n");
3049         if (nvram_match("lan_stp", "0")) {
3050 #ifdef HAVE_MICRO
3051                 br_init();
3052 #endif
3053
3054                 br_set_stp_state("br0", 0);
3055
3056 #ifdef HAVE_MICRO
3057                 br_shutdown();
3058 #endif
3059
3060         } else {
3061 #ifdef HAVE_MICRO
3062                 br_init();
3063 #endif
3064
3065                 br_set_stp_state("br0", 1);
3066 #ifdef HAVE_MICRO
3067                 br_shutdown();
3068 #endif
3069
3070         }
3071
3072         cprintf("done()()()\n");
3073 }
3074
3075 void start_wan_boot(void)
3076 {
3077         start_wan(BOOT);
3078 }
3079
3080 void start_wan_redial(void)
3081 {
3082         start_wan(REDIAL);
3083 }
3084
3085 void start_wan_service(void)
3086 {
3087         stop_process_monitor();
3088         stop_ddns();
3089         cprintf("start process monitor\n");
3090         start_process_monitor();
3091         sleep(5);
3092         cprintf("start ddns\n");
3093         start_ddns();
3094 }
3095
3096 void start_wan_done(char *wan_ifname)
3097 {
3098         cprintf("%s %s\n", wan_ifname, nvram_safe_get("wan_proto"));
3099
3100         if (nvram_match("wan_proto", "l2tp")) {
3101                 /*
3102                  * Delete all default routes
3103                  */
3104                 while (route_del
3105                        (nvram_safe_get("wan_ifname"), 0, NULL, NULL,
3106                         NULL) == 0) ;
3107         }
3108
3109         /*
3110          * Delete all default routes
3111          */
3112         while (route_del(wan_ifname, 0, NULL, NULL, NULL) == 0) ;
3113
3114         if ((nvram_match("wan_proto", "pppoe")) && check_wan_link(1)) {
3115                 while (route_del
3116                        (nvram_safe_get("wan_ifname_1"), 0, NULL, NULL,
3117                         NULL) == 0) ;
3118         }
3119
3120         if (nvram_invmatch("wan_proto", "disabled")) {
3121                 int timeout = 5;
3122
3123                 /*
3124                  * Set default route to gateway if specified
3125                  */
3126                 char *gateway = nvram_match("wan_proto",
3127                                             "pptp") ?
3128                     nvram_safe_get("pptp_get_ip") :
3129                     nvram_safe_get("wan_gateway");
3130                 if (strcmp(gateway, "0.0.0.0"))
3131                         while (route_add
3132                                (wan_ifname, 0, "0.0.0.0", gateway, "0.0.0.0")
3133                                && timeout--) {
3134                                 if ((nvram_match("wan_proto", "pppoe"))
3135                                     && nvram_match("ppp_demand", "1")) {
3136                                         printf
3137                                             ("Wait ppp interface to init (3) ...\n");
3138                                         sleep(1);
3139                                 } else
3140                                         break;
3141
3142                         }
3143         }
3144
3145         /*
3146          * Delete all default routes
3147          */
3148 //     while (route_del(wan_ifname, 0, NULL, NULL, NULL) == 0);
3149
3150         /*
3151          * Set default route to gateway if specified
3152          */
3153         /*
3154          * while (strcmp(nvram_safe_get("wan_proto"), "disabled") &&
3155          * route_add(wan_ifname, 0, "0.0.0.0", nvram_safe_get("wan_gateway"),
3156          * "0.0.0.0") && timeout-- ){ if (nvram_match("wan_proto", "pppoe") &&
3157          * nvram_match("ppp_demand", "1")) { printf("Wait ppp interface to init
3158          * (3) ...\n"); sleep(1); } }
3159          */
3160         if (nvram_match("wan_proto", "pptp")) {
3161                 route_del(nvram_safe_get("wan_iface"), 0,
3162                           nvram_safe_get("wan_gateway"), NULL,
3163                           "255.255.255.255");
3164                 route_del(nvram_safe_get("wan_iface"), 0,
3165                           nvram_safe_get("pptp_server_ip"), NULL,
3166                           "255.255.255.255");
3167                 route_add(nvram_safe_get("wan_iface"), 0,
3168                           nvram_safe_get("pptp_get_ip"), NULL,
3169                           "255.255.255.255");
3170         } else if (nvram_match("wan_proto", "l2tp")) {
3171                 route_del(nvram_safe_get("wan_iface"), 0,
3172                           nvram_safe_get("wan_gateway"), NULL,
3173                           "255.255.255.255");
3174                 route_add(nvram_safe_get("wan_iface"), 0,
3175                           nvram_safe_get("l2tp_get_ip"), NULL,
3176                           "255.255.255.255");
3177                 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
3178                 // routing
3179                 // problem
3180                 // in
3181                 // Israel
3182                 // by
3183                 // kanki
3184         }
3185
3186         /*
3187          * save dns to resolv.conf
3188          */
3189         cprintf("dns to resolv\n");
3190         dns_to_resolv();
3191
3192         cprintf("restart dhcp server\n");
3193         /*
3194          * Restart DHCP server
3195          */
3196 #ifdef HAVE_UDHCPD
3197         stop_udhcpd();
3198         start_udhcpd();
3199 #endif
3200         cprintf("restart dns proxy\n");
3201         /*
3202          * Restart DNS proxy stop_dnsmasq (); start_dnsmasq ();
3203          */
3204         cprintf("start firewall\n");
3205         /*
3206          * Start firewall
3207          */
3208         start_firewall();
3209
3210         /*
3211          * Set additional wan static routes if need
3212          */
3213
3214         start_set_routes();
3215         cprintf("routes done\n");
3216         if (nvram_match("wan_proto", "pppoe")
3217             || nvram_match("wan_proto", "pptp")
3218             || nvram_match("wan_proto", "l2tp")
3219             || nvram_match("wan_proto", "3g")) {
3220                 if (nvram_match("ppp_demand", "1")) {   // ntp and ddns will trigger DOD, so we must
3221                         // stop them when wan is unavaile.
3222                         FILE *fp;
3223
3224                         if ((fp = fopen("/tmp/ppp/link", "r"))) {
3225                                 start_wan_service();
3226                                 fclose(fp);
3227                         }
3228                 } else {
3229                         start_wan_service();
3230                 }
3231         } else {
3232                 start_wan_service();
3233         }
3234
3235 #ifdef HAVE_UPNP
3236         stop_upnp();
3237 #endif
3238 #ifdef HAVE_BIRD
3239         stop_zebra();
3240 #endif
3241         // stop_cron ();
3242         stop_wshaper();
3243         cprintf("start zebra\n");
3244 #ifdef HAVE_BIRD
3245         start_zebra();
3246 #endif
3247         cprintf("start upnp\n");
3248 #ifdef HAVE_UPNP
3249         start_upnp();
3250 #endif
3251         // cprintf ("start cron\n");
3252         // start_OAcron ();
3253         cprintf("start wshaper\n");
3254         stop_wland();
3255         start_wshaper();
3256         start_wland();
3257         if (nvram_match("wan_proto", "pptp")) {
3258
3259                 if (nvram_invmatch("pptp_customipup", "")) {
3260
3261                         // We not going to assume that /tmp/ppp is created..
3262                         mkdir("/tmp/ppp", 0700);
3263
3264                         // Create our custom pptp ipup script and change its attributes
3265                         nvram2file("pptp_customipup",
3266                                    "/tmp/ppp/sh_pptp_customipup");
3267                         chmod("/tmp/ppp/sh_pptp_customipup", 0744);
3268
3269                         // Execute our custom ipup script
3270                         system2("/tmp/ppp/sh_pptp_customipup");
3271
3272                 }
3273         }
3274         cprintf("std on\n");
3275 #ifdef HAVE_MICRO
3276         br_init();
3277 #endif
3278
3279         if (nvram_match("lan_stp", "0"))
3280                 br_set_stp_state(nvram_safe_get("lan_ifname"), 0);
3281         else
3282                 br_set_stp_state(nvram_safe_get("lan_ifname"), 1);
3283 #ifdef HAVE_MICRO
3284         br_shutdown();
3285 #endif
3286
3287         cprintf("check wan link\n");
3288         if (check_wan_link(0))
3289                 SET_LED(GOT_IP);
3290         else if ((!check_wan_link(0)) && nvram_match("wan_proto", "auto")) {
3291                 SET_LED(GET_IP_ERROR);
3292         }
3293         /*
3294          * check ip addresses for validity
3295          */
3296         uint32 wanip;
3297         uint32 wannm;
3298
3299         inet_aton(nvram_safe_get("wan_ipaddr"), (struct in_addr *)&wanip);
3300         inet_aton(nvram_safe_get("wan_netmask"), (struct in_addr *)&wannm);
3301         uint32 lanip;
3302         uint32 lannm;
3303
3304         inet_aton(nvram_safe_get("lan_ipaddr"), (struct in_addr *)&lanip);
3305         inet_aton(nvram_safe_get("lan_netmask"), (struct in_addr *)&lannm);
3306
3307         if (wanip != 0 && nvram_match("wan_ipaddr", "0.0.0.0")
3308             && !nvram_match("wan_proto", "disabled")) {
3309                 int iperror = 0;
3310
3311                 if ((wanip & wannm) == (lanip & wannm))
3312                         iperror = 1;
3313                 if ((lanip & lannm) == (wanip & lannm))
3314                         iperror = 1;
3315                 if (iperror)
3316                         eval("ledtool", "5");   // blink 5 times the 3 time interval
3317         }
3318         /*
3319          * end
3320          */
3321
3322         cprintf("running custom DD-WRT ipup scripts\n");
3323         runStartup("/etc/config", ".ipup");
3324 #ifdef HAVE_RB500
3325         runStartup("/usr/local/etc/config", ".ipup");
3326 #else
3327         runStartup("/jffs/etc/config", ".ipup");
3328         runStartup("/mmc/etc/config", ".ipup");
3329 #endif
3330         cprintf("trigger gpio");
3331
3332         led_control(LED_CONNECTED, LED_ON);
3333
3334         if (!nvram_match("wan_proto", "disabled"))
3335                 dd_syslog(LOG_INFO, "WAN is up. IP: %s\n",
3336                           nvram_safe_get("wan_ipaddr"));
3337
3338         float sys_uptime;
3339         FILE *up;
3340
3341         up = fopen("/proc/uptime", "r");
3342         fscanf(up, "%f", &sys_uptime);
3343         fclose(up);
3344
3345         up = fopen("/tmp/.wanuptime", "w");
3346         fprintf(up, "%f", sys_uptime);
3347         fclose(up);
3348
3349         cprintf("done\n");
3350
3351         nvram_set("wan_iface", nvram_safe_get("wan_ifname"));
3352
3353 #ifdef HAVE_OPENVPN
3354         cprintf("starting openvpn\n");
3355         stop_openvpn();
3356         start_openvpn();
3357         cprintf("done\n");
3358
3359 #endif
3360 #ifdef HAVE_OPENVPN
3361         stop_openvpnserverwan();
3362         start_openvpnserverwan();
3363 #endif
3364 #ifdef HAVE_DHCPFORWARD
3365         stop_dhcpfwd();
3366         start_dhcpfwd();
3367 #endif
3368         nvram_set("wanup", "1");
3369 #ifdef HAVE_MILKFISH
3370         if (nvram_match("milkfish_enabled", "1")) {
3371                 cprintf("starting milkfish netup script\n");
3372                 eval("/etc/config/milkfish.netup");
3373         }
3374 #endif
3375 #ifdef HAVE_SPUTNIK_APD
3376         stop_sputnik();
3377         start_sputnik();
3378 #endif
3379
3380 #ifdef HAVE_FON
3381 #ifdef HAVE_MICRO
3382         br_init();
3383 #endif
3384
3385         if (nvram_match("wl0_mode", "apsta")) {
3386                 br_del_interface(nvram_safe_get("lan_ifname"), "wl0.1");
3387                 ifconfig("wl0.1", IFUP | IFF_ALLMULTI, "0.0.0.0", NULL);
3388         } else if (nvram_match("wl0_mode", "ap")) {
3389                 br_del_interface(nvram_safe_get("lan_ifname"), get_wdev());
3390                 ifconfig(get_wdev(), IFUP | IFF_ALLMULTI, "0.0.0.0", NULL);
3391         }
3392 #ifdef HAVE_CHILLI
3393         stop_chilli();
3394         start_chilli();
3395 #endif
3396 #else
3397         if (nvram_match("fon_enable", "1")
3398             || (nvram_match("chilli_nowifibridge", "1")
3399                 && nvram_match("chilli_enable", "1"))) {
3400                 if (nvram_match("wl0_mode", "apsta")) {
3401                         br_del_interface(nvram_safe_get("lan_ifname"), "wl0.1");
3402                         ifconfig("wl0.1", IFUP | IFF_ALLMULTI, "0.0.0.0", NULL);
3403                 } else if (nvram_match("wl0_mode", "ap")) {
3404                         br_del_interface(nvram_safe_get("lan_ifname"),
3405                                          get_wdev());
3406                         ifconfig(get_wdev(), IFUP | IFF_ALLMULTI, "0.0.0.0",
3407                                  NULL);
3408                 }
3409 #ifdef HAVE_CHILLI
3410                 stop_chilli();
3411                 start_chilli();
3412 #endif
3413         }
3414 #endif
3415 #ifdef HAVE_MICRO
3416         br_shutdown();
3417 #endif
3418 #if defined(HAVE_MADWIFI) || defined(HAVE_RT2880)
3419 #ifndef HAVE_NOWIFI
3420         start_hostapdwan();
3421 #endif
3422 #endif
3423         cprintf("start igmp proxy\n");
3424 #ifdef HAVE_MULTICAST
3425         if (!nvram_match("dtag_vlan8", "1"))
3426                 stop_igmp_proxy();
3427         start_igmp_proxy();
3428 #endif
3429         cprintf("ready\n");
3430         start_anchorfree();
3431         start_anchorfreednat();
3432 #ifdef HAVE_MADWIFI
3433         start_duallink();
3434 #endif
3435 }
3436
3437 void stop_wan(void)
3438 {
3439         char *wan_ifname = get_wan_face();
3440
3441         nvram_set("wanup", "0");
3442
3443         led_control(LED_CONNECTED, LED_OFF);
3444         unlink("/tmp/.wanuptime");
3445
3446         cprintf("%s %s\n", wan_ifname, nvram_safe_get("wan_proto"));
3447 #ifdef HAVE_OPENVPN
3448         stop_openvpnserverwan();
3449         stop_openvpn();
3450 #endif
3451 #ifdef HAVE_DHCPFORWARD
3452         stop_dhcpfwd();
3453 #endif
3454         /*
3455          * Stop firewall
3456          */
3457         stop_firewall();
3458         /*
3459          * Kill any WAN client daemons or callbacks
3460          */
3461 #ifdef HAVE_PPPOE
3462         stop_pppoe();
3463 #endif
3464 #ifdef HAVE_L2TP
3465         stop_l2tp();
3466 #endif
3467         stop_dhcpc();
3468 #ifdef HAVE_HEARTBEAT
3469         stop_heartbeat();
3470 #endif
3471 #ifdef HAVE_PPTP
3472         stop_pptp();
3473 #endif
3474 #ifdef HAVE_SPUTNIK_APD
3475         stop_sputnik();
3476 #endif
3477         stop_ntpc();
3478         stop_redial();
3479         nvram_set("wan_get_dns", "");
3480
3481         // Reset pppd's pppX interface
3482         nvram_set("pppd_pppifname", "");
3483
3484         /*
3485          * Bring down WAN interfaces
3486          */
3487         ifconfig(wan_ifname, 0, NULL, NULL);
3488         eval("ifconfig", wan_ifname, "down");   // to allow for MAC clone to
3489         // take effect
3490 #ifdef HAVE_PPP
3491 #endif
3492 #ifndef HAVE_FON
3493         if (nvram_match("fon_enable", "1")
3494             || (nvram_match("chilli_nowifibridge", "1")
3495                 && nvram_match("chilli_enable", "1")))
3496 #endif
3497         {
3498 #ifdef HAVE_MICRO
3499                 br_init();
3500 #endif
3501
3502                 br_add_interface(getBridge(get_wdev()), get_wdev());
3503 #ifdef HAVE_MICRO
3504                 br_shutdown();
3505 #endif
3506
3507         }
3508
3509         cprintf("done\n");
3510 }
3511
3512 void start_set_routes(void)
3513 {
3514         char word[80], *tmp;
3515         char *ipaddr, *netmask, *gateway, *metric, *ifname;
3516
3517         if (!nvram_match("lan_gateway", "0.0.0.0")) {
3518                 eval("route", "del", "default");
3519                 eval("route", "add", "default", "gw",
3520                      nvram_safe_get("lan_gateway"));
3521         }
3522         char *defgateway;
3523
3524         if (nvram_match("wan_proto", "pptp"))
3525                 defgateway = nvram_safe_get("pptp_get_ip");
3526         else if (nvram_match("wan_proto", "l2tp"))
3527                 defgateway = nvram_safe_get("l2tp_get_ip");
3528         else
3529                 defgateway = nvram_safe_get("wan_gateway");
3530         if (strcmp(defgateway, "0.0.0.0")
3531             && !nvram_match("wan_proto", "disabled")) {
3532                 eval("route", "del", "default");
3533                 eval("route", "add", "default", "gw", defgateway);
3534         }
3535         foreach(word, nvram_safe_get("static_route"), tmp) {
3536                 netmask = word;
3537                 ipaddr = strsep(&netmask, ":");
3538                 if (!ipaddr || !netmask)
3539                         continue;
3540                 gateway = netmask;
3541                 netmask = strsep(&gateway, ":");
3542                 if (!netmask || !gateway)
3543                         continue;
3544                 metric = gateway;
3545                 gateway = strsep(&metric, ":");
3546                 if (!gateway || !metric)
3547                         continue;
3548                 ifname = metric;
3549                 metric = strsep(&ifname, ":");
3550                 if (!metric || !ifname)
3551                         continue;
3552                 if (!strcmp(ipaddr, "0.0.0.0") && !strcmp(gateway, "0.0.0.0"))
3553                         continue;
3554                 if (!strcmp(ipaddr, "0.0.0.0")) {
3555                         eval("route", "del", "default");
3556                         eval("route", "add", "default", "gw", gateway);
3557                 } else if (!strcmp(ifname, "any")) {
3558                         eval("route", "add", "-net", ipaddr, "netmask", netmask,
3559                              "gw", gateway, "metric", metric);
3560                 } else
3561                         route_add(ifname, atoi(metric) + 1, ipaddr, gateway,
3562                                   netmask);
3563         }
3564         if (f_exists("/tmp/tvrouting"))
3565         system("sh /tmp/tvrouting");
3566 }
3567
3568 #if !defined(HAVE_MADWIFI) && !defined(HAVE_RT2880)
3569 static int notify_nas(char *type, char *ifname, char *action)
3570 {
3571         char *argv[] = { "nas4not", type, ifname, action,
3572                 NULL,           /* role */
3573                 NULL,           /* crypto */
3574                 NULL,           /* auth */
3575                 NULL,           /* passphrase */
3576                 NULL,           /* ssid */
3577                 NULL
3578         };
3579         char *str = NULL;
3580         int retries = 10;
3581         char tmp[100], prefix[] = "wlXXXXXXXXXX_";
3582         int unit;
3583         char remote[ETHER_ADDR_LEN];
3584         char ssid[48], pass[80], auth[16], crypto[16], role[8];
3585         int i;
3586
3587         /*
3588          * the wireless interface must be configured to run NAS
3589          */
3590         wl_ioctl(ifname, WLC_GET_INSTANCE, &unit, sizeof(unit));
3591         snprintf(prefix, sizeof(prefix), "wl%d_", unit);
3592         if (nvram_match(strcat_r(prefix, "akm", tmp), "") &&
3593             nvram_match(strcat_r(prefix, "auth_mode", tmp), "none"))
3594                 return 0;
3595
3596         while (retries-- > 0 && !(str = file2str("/tmp/nas.wl0lan.pid")))
3597                 sleep(1);
3598         if (!str) {
3599                 return -1;
3600         }
3601         free(str);
3602         sleep(3);
3603         /*
3604          * find WDS link configuration
3605          */
3606         wl_ioctl(ifname, WLC_WDS_GET_REMOTE_HWADDR, remote, ETHER_ADDR_LEN);
3607         for (i = 0; i < MAX_NVPARSE; i++) {
3608                 char mac[ETHER_ADDR_STR_LEN];
3609                 uint8 ea[ETHER_ADDR_LEN];
3610
3611                 if (get_wds_wsec(unit, i, mac, role, crypto, auth, ssid, pass)
3612                     && ether_atoe(mac, ea)
3613                     && !bcmp(ea, remote, ETHER_ADDR_LEN)) {
3614                         argv[4] = role;
3615                         argv[5] = crypto;
3616                         argv[6] = auth;
3617                         argv[7] = pass;
3618                         argv[8] = ssid;
3619                         break;
3620                 }
3621         }
3622
3623         /*
3624          * did not find WDS link configuration, use wireless'
3625          */
3626         if (i == MAX_NVPARSE) {
3627                 /*
3628                  * role
3629                  */
3630                 argv[4] = "auto";
3631                 /*
3632                  * crypto
3633                  */
3634                 argv[5] = nvram_safe_get(strcat_r(prefix, "crypto", tmp));
3635                 /*
3636                  * auth mode
3637                  */
3638                 argv[6] = nvram_safe_get(strcat_r(prefix, "akm", tmp));
3639                 /*
3640                  * passphrase
3641                  */
3642                 argv[7] = nvram_safe_get(strcat_r(prefix, "wpa_psk", tmp));
3643                 /*
3644                  * ssid
3645                  */
3646                 argv[8] = nvram_safe_get(strcat_r(prefix, "ssid", tmp));
3647         }
3648         int pid;
3649
3650         return _evalpid(argv, ">/dev/console", 0, &pid);
3651 }
3652 #endif
3653 /*
3654  * static int notify_nas(char *type, char *ifname, char *action) { char
3655  * *argv[] = {"nas4not", type, ifname, action, NULL, NULL, NULL, NULL,
3656  * NULL, NULL}; char *str = NULL; int retries = 10; char tmp[100], prefix[]
3657  * = "wlXXXXXXXXXX_"; int unit; char remote[ETHER_ADDR_LEN]; char ssid[48],
3658  * pass[80], auth[16], crypto[16], role[8]; int i;
3659  *
3660  * wl_ioctl(ifname, WLC_GET_INSTANCE, &unit, sizeof(unit)); snprintf(prefix,
3661  * sizeof(prefix), "wl%d_", unit); #ifdef WPA2_WMM if
3662  * (nvram_match(strcat_r(prefix, "akm", tmp), "") &&
3663  * nvram_match(strcat_r(prefix, "auth_mode", tmp), "none")) #else if
3664  * (nvram_match(strcat_r(prefix, "auth_mode", tmp), "open") ||
3665  * nvram_match(strcat_r(prefix, "auth_mode", tmp), "shared")) #endif return
3666  * 0;
3667  *
3668  * wl_ioctl(ifname, WLC_WDS_GET_REMOTE_HWADDR, remote, ETHER_ADDR_LEN); for
3669  * (i = 0; i < MAX_NVPARSE; i ++) { char mac[ETHER_ADDR_STR_LEN]; uint8
3670  * ea[ETHER_ADDR_LEN];
3671  *
3672  * if (get_wds_wsec(unit, i, mac, role, crypto, auth, ssid, pass) &&
3673  * ether_atoe(mac, ea) && !bcmp(ea, remote, ETHER_ADDR_LEN)) { argv[4] =
3674  * role; argv[5] = crypto; argv[6] = auth; argv[7] = pass; argv[8] = ssid;
3675  * break; } }
3676  *
3677  * if (i == MAX_NVPARSE) {
3678  *
3679  * argv[4] = "auto";
3680  *
3681  * argv[5] = nvram_safe_get(strcat_r(prefix, "crypto", tmp));
3682  *
3683  * #ifdef WPA2_WMM argv[6] = nvram_safe_get(strcat_r(prefix, "akm", tmp));
3684  * #else argv[6] = nvram_safe_get(strcat_r(prefix, "auth_mode", tmp)); #endif
3685  *
3686  * argv[7] = nvram_safe_get(strcat_r(prefix, "wpa_psk", tmp));
3687  *
3688  * argv[8] = nvram_safe_get(strcat_r(prefix, "ssid", tmp)); }
3689  *
3690  * while (retries -- > 0 && !(str = file2str("/tmp/nas.lan.pid"))) sleep(1);
3691  * if (str) { int pid; free(str); return _eval(argv, ">/dev/console", 0,
3692  * &pid); } return -1;
3693  *
3694  * }
3695  */
3696 void start_hotplug_net(void)
3697 {
3698 #ifdef HAVE_MADWIFI
3699         char *interface, *action;
3700
3701         interface = getenv("INTERFACE");
3702         if (!interface)
3703                 return;
3704         action = getenv("ACTION");
3705         if (!action)
3706                 return;
3707         // sysprintf("echo \"Hotplug %s=%s\" > /dev/console\n",action,interface);
3708         if (strncmp(interface, "ath", 3))
3709                 return;
3710
3711         // try to parse
3712         char ifname[32];
3713
3714         memset(ifname, 0, 32);
3715         int index = indexof(interface, '.');
3716
3717         if (index == -1)
3718                 return;
3719         strncpy(ifname, interface + index + 1, strlen(interface) - (index + 1));
3720         if (strncmp(ifname, "sta", 3)) {
3721                 return;
3722         }
3723         char nr[32];
3724
3725         memset(nr, 0, 32);
3726         strcpy(nr, ((unsigned char *)&ifname[0]) + 3);
3727         memset(ifname, 0, 32);
3728         strncpy(ifname, interface, index);
3729         char bridged[32];
3730
3731         sprintf(bridged, "%s_bridged", ifname);
3732
3733         if (!strcmp(action, "add")) {
3734                 fprintf(stderr, "adding WDS %s\n", interface);
3735
3736                 eval("ifconfig", interface, "up");
3737                 if (nvram_match(bridged, "1"))
3738                         br_add_interface(getBridge(ifname), interface);
3739         }
3740         if (!strcmp(action, "remove")) {
3741                 fprintf(stderr, "removing WDS %s\n", interface);
3742                 eval("ifconfig", interface, "down");
3743                 if (nvram_match(bridged, "1"))
3744                         br_del_interface(getBridge(ifname), interface);
3745         }
3746         return;
3747 #else
3748
3749         // char *lan_ifname = nvram_safe_get("lan_ifname");
3750         char *interface, *action;
3751
3752         if (!(interface = getenv("INTERFACE"))