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

Last change on this file since 15978 was 15978, checked in by BrainSlayer, 2 years ago

experimental danube port

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