source: src/router/services/networking/network.c @ 15851

Last change on this file since 15851 was 15851, checked in by BrainSlayer, 3 years ago

wipe this config, if someone wants to use it unbridged, he should configure the wireless interface manually and select the chillispot interface manually

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