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

Last change on this file since 18853 was 18853, checked in by BrainSlayer, 14 months ago

DIR825-C1 in progress

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