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

Last change on this file since 7436 was 7436, checked in by eko, 6 years ago

wl radio change

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