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

Last change on this file since 9305 was 9305, checked in by BrainSlayer, 5 years ago

less stack consumption

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