source: src/router/services/sysinit/sysinit.c @ 9263

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

litestation and dir 300 mac fixes to solve performance problem

File size: 31.4 KB
Line 
1/*
2 * sysinit.c
3 *
4 * Copyright (C) 2007 Sebastian Gottschall <gottschall@dd-wrt.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 *
20 * $Id:
21 */
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <limits.h>
26#include <time.h>
27#include <unistd.h>
28#include <errno.h>
29#include <syslog.h>
30#include <signal.h>
31#include <string.h>
32#include <termios.h>
33
34#include <sys/klog.h>
35#include <sys/types.h>
36#include <sys/mount.h>
37#include <sys/reboot.h>
38#include <sys/stat.h>
39#include <sys/sysmacros.h>
40#include <sys/time.h>
41#include <sys/utsname.h>
42#include <sys/wait.h>
43#include <dirent.h>
44
45#include <epivers.h>
46#include <bcmnvram.h>
47#include <mtd.h>
48#include <shutils.h>
49#include <rc.h>
50#include <netconf.h>
51#include <nvparse.h>
52#include <bcmdevs.h>
53
54#include <wlutils.h>
55#include <utils.h>
56#include <cyutils.h>
57#include <code_pattern.h>
58#include <cy_conf.h>
59//#include <mkfiles.h>
60#include <typedefs.h>
61#include <bcmnvram.h>
62#include <bcmutils.h>
63#include <shutils.h>
64#include <wlutils.h>
65#include <cy_conf.h>
66#include <cymac.h>
67//#include <ledcontrol.h>
68
69
70#define WL_IOCTL(name, cmd, buf, len) (ret = wl_ioctl((name), (cmd), (buf), (len)))
71
72#define TXPWR_MAX 251
73#define TXPWR_DEFAULT 28
74
75void start_restore_defaults (void);
76static void rc_signal (int sig);
77extern void start_overclocking (void);
78extern int check_cfe_nv (void);
79extern int check_pmon_nv (void);
80static void unset_nvram (void);
81int start_nvram (void);
82
83extern struct nvram_tuple srouter_defaults[];
84
85
86
87int
88endswith (char *str, char *cmp)
89{
90  int cmp_len, str_len, i;
91  cmp_len = strlen (cmp);
92  str_len = strlen (str);
93  if (cmp_len > str_len)
94    return (0);
95  for (i = 0; i < cmp_len; i++)
96    {
97      if (str[(str_len - 1) - i] != cmp[(cmp_len - 1) - i])
98        return (0);
99    }
100  return (1);
101}
102
103
104
105
106#ifdef HAVE_MACBIND
107#include "../../../opt/mac.h"
108#endif
109void
110runStartup (char *folder, char *extension)
111{
112  struct dirent *entry;
113  DIR *directory;
114  unsigned char buf[128];
115  directory = opendir (folder);
116  if (directory == NULL)
117    {
118      return;
119    }
120//list all files in this directory
121  while ((entry = readdir (directory)) != NULL)
122    {
123      if (endswith (entry->d_name, extension))
124        {
125          sprintf (buf, "%s/%s 2>&1 > /dev/null&\n", folder, entry->d_name);
126          //execute script     
127          system2 (buf);
128        }
129    }
130  closedir (directory);
131}
132
133/* SeG dd-wrt addition for module startup scripts */
134int
135start_modules (void)
136{
137  runStartup ("/etc/config", ".startup");
138
139#ifdef HAVE_RB500
140  runStartup ("/usr/local/etc/config", ".startup");     //if available
141#elif HAVE_X86
142  runStartup ("/usr/local/etc/config", ".startup");     //if available
143#else
144  runStartup ("/jffs/etc/config", ".startup");  //if available
145  runStartup ("/mmc/etc/config", ".startup");   //if available
146#endif
147  return 0;
148}
149
150int
151start_wanup (void)
152{
153  runStartup ("/etc/config", ".wanup");
154#ifdef HAVE_RB500
155  runStartup ("/usr/local/etc/config", ".wanup");       //if available
156#elif HAVE_X86
157  runStartup ("/usr/local/etc/config", ".wanup");       //if available
158#else
159  runStartup ("/jffs/etc/config", ".wanup");    //if available
160  runStartup ("/mmc/etc/config", ".wanup");     //if available
161  runStartup ("/tmp/etc/config", ".wanup");     //if available
162#endif
163  return 0;
164}
165
166
167
168
169int
170start_create_rc_startup (void)
171{
172  create_rc_file (RC_STARTUP);
173  return 0;
174}
175
176int
177start_create_rc_shutdown (void)
178{
179  create_rc_file (RC_SHUTDOWN);
180  return 0;
181}
182
183
184int
185create_rc_file (char *name)
186{
187  FILE *fp;
188  char *p = nvram_safe_get (name);
189  char tmp_file[100] = { 0 };
190
191  if ((void *) 0 == name || 0 == p[0])
192    return -1;
193
194  snprintf (tmp_file, 100, "/tmp/.%s", name);
195  unlink (tmp_file);
196
197  fp = fopen (tmp_file, "w");
198  if (fp)
199    {
200      // filter Windows <cr>ud
201      while (*p)
202        {
203          if (*p != 0x0d)
204            fprintf (fp, "%c", *p);
205          p++;
206        }
207    }
208  fclose (fp);
209  chmod (tmp_file, 0700);
210
211  return 0;
212}
213
214static void
215ses_cleanup (void)
216{
217  /* well known event to cleanly initialize state machine */
218  nvram_set ("ses_event", "2");
219
220  /* Delete lethal dynamically generated variables */
221  nvram_unset ("ses_bridge_disable");
222}
223
224static void
225ses_restore_defaults (void)
226{
227  char tmp[100], prefix[] = "wlXXXXXXXXXX_ses_";
228  int i;
229
230  /* Delete dynamically generated variables */
231  for (i = 0; i < MAX_NVPARSE; i++)
232    {
233      sprintf (prefix, "wl%d_ses_", i);
234      nvram_unset (strcat_r (prefix, "ssid", tmp));
235      nvram_unset (strcat_r (prefix, "closed", tmp));
236      nvram_unset (strcat_r (prefix, "wpa_psk", tmp));
237      nvram_unset (strcat_r (prefix, "auth", tmp));
238      nvram_unset (strcat_r (prefix, "wep", tmp));
239      nvram_unset (strcat_r (prefix, "auth_mode", tmp));
240      nvram_unset (strcat_r (prefix, "crypto", tmp));
241      nvram_unset (strcat_r (prefix, "akm", tmp));
242    }
243}
244
245void
246start_restore_defaults (void)
247{
248
249#ifdef HAVE_RB500
250  struct nvram_tuple generic[] = {
251    {"lan_ifname", "br0", 0},
252    {"lan_ifnames",
253     "eth1 eth2 eth3 eth4 eth5 eth6 eth7 eth8 ath0 ath1 ath2 ath3 ath4 ath5",
254     0},
255    {"wan_ifname", "eth0", 0},
256    {"wan_ifnames", "eth0", 0},
257    {0, 0, 0}
258  };
259#elif HAVE_GEMTEK
260  struct nvram_tuple generic[] = {
261    {"lan_ifname", "br0", 0},
262    {"lan_ifnames", "eth1 ath0", 0},
263    {"wan_ifname", "eth0", 0},
264    {"wan_ifnames", "eth0", 0},
265    {0, 0, 0}
266  };
267#elif HAVE_GATEWORX
268  struct nvram_tuple generic[] = {
269    {"lan_ifname", "br0", 0},
270    {"lan_ifnames", "ixp0 ath0 ath1 ath2 ath3",
271     0},
272    {"wan_ifname2", "ixp1", 0},
273    {"wan_ifname", "ixp1", 0},
274    {"wan_ifnames", "ixp1", 0},
275    {0, 0, 0}
276  };
277#elif HAVE_X86
278  struct nvram_tuple generic[] = {
279    {"lan_ifname", "br0", 0},
280#ifdef HAVE_NOWIFI
281    {"lan_ifnames", "eth1 eth2 eth3 eth4 eth5 eth6 eth7 eth8 eth9 eth10", 0},
282#else
283#ifdef HAVE_GW700
284    {"lan_ifnames",
285     "eth0 eth2 eth3 eth4 eth5 eth6 eth7 eth8 eth9 eth10 ath0 ath1 ath2 ath3 ath5 ath6 ath7 ath8",
286     0},
287#else
288    {"lan_ifnames",
289     "eth1 eth2 eth3 eth4 eth5 eth6 eth7 eth8 eth9 eth10 ath0 ath1 ath2 ath3 ath5 ath6 ath7 ath8",
290     0},
291#endif
292#endif
293#ifdef HAVE_GW700
294    {"wan_ifname", "eth1", 0},
295    {"wan_ifname2", "eth1", 0},
296    {"wan_ifnames", "eth1", 0},
297#else
298    {"wan_ifname", "eth0", 0},
299    {"wan_ifname2", "eth0", 0},
300    {"wan_ifnames", "eth0", 0},
301#endif
302    {0, 0, 0}
303  };
304#elif HAVE_XSCALE
305  struct nvram_tuple generic[] = {
306    {"lan_ifname", "br0", 0},
307    {"lan_ifnames",
308     "ixp0.1 ixp0.2 ath0 ath1",
309     0},
310    {"wan_ifname", "ixp1", 0},
311    {"wan_ifnames", "ixp1", 0},
312    {0, 0, 0}
313  };
314#elif HAVE_MAGICBOX
315  struct nvram_tuple generic[] = {
316    {"lan_ifname", "br0", 0},
317    {"lan_ifnames", "eth1 ath0",
318     0},
319    {"wan_ifname", "eth0", 0},
320    {"wan_ifnames", "eth0", 0},
321    {0, 0, 0}
322  };
323#elif HAVE_FONERA
324  struct nvram_tuple generic[] = {
325    {"lan_ifname", "br0", 0},
326    {"lan_ifnames", "vlan0 ath0", 0},
327    {"wan_ifname", "", 0},
328    {"wan_ifnames", "eth0 vlan1", 0},
329    {0, 0, 0}
330  };
331#elif HAVE_LS2
332  struct nvram_tuple generic[] = {
333    {"lan_ifname", "br0", 0},
334    {"lan_ifnames", "vlan2 ath0", 0},
335    {"wan_ifname", "vlan0", 0},
336    {"wan_ifnames", "vlan0", 0},
337    {0, 0, 0}
338  };
339#elif HAVE_LS5
340  struct nvram_tuple generic[] = {
341    {"lan_ifname", "br0", 0},
342    {"lan_ifnames", "ath0", 0},
343    {"wan_ifname", "eth0", 0},
344    {"wan_ifnames", "eth0", 0},
345    {0, 0, 0}
346  };
347#elif HAVE_WHRAG108
348  struct nvram_tuple generic[] = {
349    {"lan_ifname", "br0", 0},
350    {"lan_ifnames", "eth0 ath0 ath1", 0},
351    {"wan_ifname", "eth1", 0},
352    {"wan_ifnames", "eth1", 0},
353    {0, 0, 0}
354  };
355#elif HAVE_PB42
356  struct nvram_tuple generic[] = {
357    {"lan_ifname", "br0", 0},
358    {"lan_ifnames", "eth1 ath0 ath1", 0},
359    {"wan_ifname", "eth0", 0},
360    {"wan_ifnames", "eth0", 0},
361    {0, 0, 0}
362  };
363#elif HAVE_TW6600
364  struct nvram_tuple generic[] = {
365    {"lan_ifname", "br0", 0},
366    {"lan_ifnames", "ath0 ath1", 0},
367    {"wan_ifname", "eth0", 0},
368    {"wan_ifnames", "eth0", 0},
369    {0, 0, 0}
370  };
371#elif HAVE_CA8
372  struct nvram_tuple generic[] = {
373    {"lan_ifname", "br0", 0},
374    {"lan_ifnames", "ath0", 0},
375    {"wan_ifname", "eth0", 0},
376    {"wan_ifnames", "eth0", 0},
377    {0, 0, 0}
378  };
379#else
380  struct nvram_tuple generic[] = {
381    {"lan_ifname", "br0", 0},
382    {"lan_ifnames", "eth0 eth2 eth3 eth4", 0},
383    {"wan_ifname", "eth1", 0},
384    {"wan_ifnames", "eth1", 0},
385    {0, 0, 0}
386  };
387  struct nvram_tuple vlan[] = {
388    {"lan_ifname", "br0", 0},
389    {"lan_ifnames", "vlan0 eth1 eth2 eth3", 0},
390    {"wan_ifname", "vlan1", 0},
391    {"wan_ifnames", "vlan1", 0},
392    {0, 0, 0}
393  };
394
395  struct nvram_tuple wrt350vlan[] = {
396    {"lan_ifname", "br0", 0},
397    {"lan_ifnames", "vlan1 eth0", 0},
398    {"wan_ifname", "vlan2", 0},
399    {"wan_ifnames", "vlan2", 0},
400    {0, 0, 0}
401  };
402
403  struct nvram_tuple wrt600vlan[] = {
404    {"lan_ifname", "br0", 0},
405    {"lan_ifnames", "vlan0 eth0 eth1", 0},
406    {"wan_ifname", "vlan2", 0},
407    {"wan_ifnames", "vlan2", 0},
408    {0, 0, 0}
409  };
410
411  struct nvram_tuple wzr144nhvlan[] = {
412    {"lan_ifname", "br0", 0},
413    {"lan_ifnames", "vlan2 eth0", 0},
414    {"wan_ifname", "vlan1", 0},
415    {"wan_ifnames", "vlan1", 0},
416    {0, 0, 0}
417  };
418
419  struct nvram_tuple generic_2[] = {
420    {"lan_ifname", "br0", 0},
421    {"lan_ifnames", "eth1 eth2", 0},
422    {"wan_ifname", "eth0", 0},
423    {"wan_ifnames", "eth0", 0},
424    {0, 0, 0}
425  };
426
427  struct nvram_tuple generic_3[] = {
428    {"lan_ifname", "br0", 0},
429    {"lan_ifnames", "eth0 eth1", 0},
430    {"wan_ifname", "eth2", 0},
431    {"wan_ifnames", "eth2", 0},
432    {0, 0, 0}
433  };
434#endif
435
436  struct nvram_tuple *linux_overrides;
437  struct nvram_tuple *t, *u;
438  int restore_defaults = 0;
439//      uint boardflags;
440
441  /* Restore defaults if told to.
442
443     Note: an intentional side effect is that when
444     upgrading from a firmware without the
445     sv_restore_defaults var, defaults will
446     also be restored.
447   */
448  char *et0mac = nvram_safe_get ("et0macaddr");
449  char *et1mac = nvram_safe_get ("et1macaddr");
450//  unsigned char mac[20];
451//  if (getRouterBrand () == ROUTER_BUFFALO_WZRG144NH)
452//    {
453//      if (nvram_get ("il0macaddr") == NULL)
454//      {
455//        strcpy (mac, et0mac);
456//        MAC_ADD (mac);
457//        nvram_set ("il0macaddr", mac);
458//      }
459//    }
460
461#ifdef HAVE_RB500
462  linux_overrides = generic;
463  int brand = getRouterBrand ();
464#elif HAVE_XSCALE
465  linux_overrides = generic;
466  int brand = getRouterBrand ();
467  if (nvram_invmatch ("sv_restore_defaults", "0"))      // || nvram_invmatch("os_name", "linux"))
468    {
469      restore_defaults = 1;
470    }
471#elif HAVE_X86
472  linux_overrides = generic;
473  int brand = getRouterBrand ();
474  if (nvram_invmatch ("sv_restore_defaults", "0"))      // || nvram_invmatch("os_name", "linux"))
475    {
476      restore_defaults = 1;
477    }
478#elif HAVE_MAGICBOX
479  linux_overrides = generic;
480  int brand = getRouterBrand ();
481  if (nvram_invmatch ("sv_restore_defaults", "0"))      // || nvram_invmatch("os_name", "linux"))
482    {
483      restore_defaults = 1;
484    }
485#elif HAVE_GATEWORX
486  linux_overrides = generic;
487  int brand = getRouterBrand ();
488  if (nvram_invmatch ("sv_restore_defaults", "0"))      // || nvram_invmatch("os_name", "linux"))
489    {
490      restore_defaults = 1;
491    }
492#elif HAVE_FONERA
493  linux_overrides = generic;
494  int brand = getRouterBrand ();
495  if (nvram_invmatch ("sv_restore_defaults", "0"))      // || nvram_invmatch("os_name", "linux"))
496    {
497      restore_defaults = 1;
498    }
499#elif HAVE_LS2
500  linux_overrides = generic;
501  int brand = getRouterBrand ();
502  if (nvram_invmatch ("sv_restore_defaults", "0"))      // || nvram_invmatch("os_name", "linux"))
503    {
504      restore_defaults = 1;
505    }
506#elif HAVE_LS5
507  linux_overrides = generic;
508  int brand = getRouterBrand ();
509  if (nvram_invmatch ("sv_restore_defaults", "0"))      // || nvram_invmatch("os_name", "linux"))
510    {
511      restore_defaults = 1;
512    }
513#elif HAVE_WHRAG108
514  linux_overrides = generic;
515  int brand = getRouterBrand ();
516  if (nvram_invmatch ("sv_restore_defaults", "0"))      // || nvram_invmatch("os_name", "linux"))
517    {
518      restore_defaults = 1;
519    }
520#elif HAVE_TW6600
521  linux_overrides = generic;
522  int brand = getRouterBrand ();
523  if (nvram_invmatch ("sv_restore_defaults", "0"))      // || nvram_invmatch("os_name", "linux"))
524    {
525      restore_defaults = 1;
526    }
527#elif HAVE_PB42
528  linux_overrides = generic;
529  int brand = getRouterBrand ();
530  if (nvram_invmatch ("sv_restore_defaults", "0"))      // || nvram_invmatch("os_name", "linux"))
531    {
532      restore_defaults = 1;
533    }
534#elif HAVE_CA8
535  linux_overrides = generic;
536  int brand = getRouterBrand ();
537  if (nvram_invmatch ("sv_restore_defaults", "0"))      // || nvram_invmatch("os_name", "linux"))
538    {
539      restore_defaults = 1;
540    }
541#elif HAVE_GEMTEK
542  linux_overrides = generic;
543  int brand = getRouterBrand ();
544#else
545  int brand = getRouterBrand ();
546
547  if (nvram_invmatch ("sv_restore_defaults", "0"))      // || nvram_invmatch("os_name", "linux"))
548    {
549//      nvram_unset("sv_restore_defaults");
550      restore_defaults = 1;
551    }
552  if (nvram_match ("product_name", "INSPECTION"))
553    {
554      nvram_unset ("product_name");
555      restore_defaults = 1;
556    }
557  if (nvram_get ("router_name") == NULL)
558    restore_defaults = 1;
559
560  if (restore_defaults)
561    {
562      cprintf ("Restoring defaults...\n");
563      /* these unsets are important for routers where we can't erase nvram and only software restore defaults */
564      nvram_unset ("wan_to_lan");
565      nvram_unset ("wl_vifs");
566      nvram_unset ("wl0_vifs");
567    }
568
569//    }
570
571/* Delete dynamically generated variables */
572  /* Choose default lan/wan i/f list. */
573  char *ds;
574  switch (brand)
575    {
576#ifndef HAVE_BUFFALO
577    case ROUTER_WRTSL54GS:
578    case ROUTER_WRT150N:
579    case ROUTER_WRT300N:
580    case ROUTER_NETGEAR_WNR834B:
581    case ROUTER_NETGEAR_WNR834BV2:
582    case ROUTER_ASUS_WL500G:
583    case ROUTER_ASUS_WL500W:
584#endif
585    case ROUTER_BUFFALO_WZRG300N:
586    case ROUTER_BUFFALO_WLAH_G54:
587    case ROUTER_BUFFALO_WAPM_HP_AM54G54:
588    case ROUTER_BUFFALO_WZRRSG54:
589      linux_overrides = generic;
590      break;
591#ifndef HAVE_BUFFALO
592    case ROUTER_ASUS_WL500GD:
593    case ROUTER_ASUS_WL550GE:
594      linux_overrides = vlan;
595      break;
596    case ROUTER_WRT350N:
597      linux_overrides = wrt350vlan;
598      break;
599    case ROUTER_WRT600N:
600      linux_overrides = wrt600vlan;
601      break;
602#endif
603    case ROUTER_BUFFALO_WZRG144NH:
604      linux_overrides = wzr144nhvlan;
605      break;
606#ifndef HAVE_BUFFALO
607    case ROUTER_MOTOROLA_WE800G:
608    case ROUTER_WAP54G_V1:
609    case ROUTER_SITECOM_WL105B:
610#endif
611    case ROUTER_BUFFALO_WLI2_TX1_G54:
612    case ROUTER_BUFFALO_WLAG54C:
613      linux_overrides = generic_2;
614      break;
615#ifndef HAVE_BUFFALO
616    case ROUTER_WAP54G_V2:
617    case ROUTER_VIEWSONIC_WAPBR_100:
618    case ROUTER_USR_5430:
619    case ROUTER_BELKIN_F5D7230_V2000:
620    case ROUTER_NETGEAR_WG602_V3:
621#endif
622    case ROUTER_BUFFALO_WLA2G54C:
623    case ROUTER_BUFFALO_WLI_TX4_G54HP:
624      linux_overrides = generic_3;
625      break;
626#ifndef HAVE_BUFFALO
627    case ROUTER_RT480W:
628    case ROUTER_RT210W:
629#endif
630    case ROUTER_BRCM4702_GENERIC:
631      ds = nvram_safe_get ("dhcp_start");
632      if (ds != NULL && strlen (ds) > 3)
633        {
634          fprintf (stderr, "cleaning nvram variables\n");
635          for (t = srouter_defaults; t->name; t++)
636            {
637              nvram_unset (t->name);
638            }
639          restore_defaults = 1;
640        }
641
642/*      ds = nvram_safe_get ("http_passwd");
643      if (ds == NULL || strlen (ds) == 0)       //fix for empty default password
644        {
645          nvram_set ("http_passwd", "admin");
646        }
647      ds = nvram_safe_get ("language");
648      if (ds != NULL && strlen (ds) < 3)
649        {
650          nvram_set ("language", "english");
651        }*/
652      // fall through 
653    default:
654      if (check_vlan_support ())
655        linux_overrides = vlan;
656      else
657        linux_overrides = generic;
658      break;
659    }
660#endif
661/*  int i;
662 *  for (i=0;i<4;i++)
663 *              nvram_set(linux_overrides[i].name,linux_overrides[i].value);
664 */
665
666  /* Restore defaults */
667#ifdef HAVE_FON
668  int reset = 0;
669  char *rev = nvram_safe_get ("fon_revision");
670  if (rev == NULL || strlen (rev) == 0)
671    reset = 1;
672  if (strlen (rev) > 0)
673    {
674      int n = atoi (rev);
675      if (atoi (srouter_defaults[0].value) != n)
676        reset = 1;
677    }
678  if (reset)
679    {
680      for (t = srouter_defaults; t->name; t++)
681        {
682          for (u = linux_overrides; u && u->name; u++)
683            {
684              if (!strcmp (t->name, u->name))
685                {
686                  nvram_set (u->name, u->value);
687                  break;
688                }
689            }
690          if (!u || !u->name)
691            nvram_set (t->name, t->value);
692        }
693    }
694#endif
695#ifdef HAVE_GATEWORX
696  if (restore_defaults)
697    {
698      eval ("erase", "nvram");
699    }
700#elif HAVE_XSCALE
701  if (restore_defaults)
702    eval ("rm", "-f", "/etc/nvram/*");  // delete nvram database
703#endif
704#ifdef HAVE_MAGICBOX
705  if (restore_defaults)
706    {
707      eval ("rm", "-f", "/tmp/nvram/*");        // delete nvram database
708      eval ("rm", "-f", "/tmp/nvram/.lock");    // delete nvram database
709      eval ("erase", "nvram");
710    }
711#endif
712#ifdef HAVE_FONERA
713  if (restore_defaults)
714    {
715      eval ("erase", "nvram");
716    }
717#endif
718#ifdef HAVE_LS2
719  if (restore_defaults)
720    {
721      eval ("erase", "nvram");
722    }
723#endif
724#ifdef HAVE_LS5
725  if (restore_defaults)
726    {
727      eval ("erase", "nvram");
728    }
729#endif
730#ifdef HAVE_WHRAG108
731  if (restore_defaults)
732    {
733      eval ("erase", "nvram");
734    }
735#endif
736#ifdef HAVE_TW6600
737  if (restore_defaults)
738    {
739      eval ("erase", "nvram");
740    }
741#endif
742  int nvcnt = 0;
743//  if (!nvram_match("default_init","1"))
744  {
745    for (t = srouter_defaults; t->name; t++)
746      {
747        if (restore_defaults || !nvram_get (t->name))
748          {
749            for (u = linux_overrides; u && u->name; u++)
750              {
751                if (!strcmp (t->name, u->name))
752                  {
753                    nvcnt++;
754                    nvram_set (u->name, u->value);
755                    break;
756                  }
757              }
758            if (!u || !u->name)
759              {
760                nvcnt++;
761                nvram_set (t->name, t->value);
762              }
763          }
764      }
765  }
766#ifndef HAVE_FON
767  if (restore_defaults)
768    {
769      switch (brand)
770        {
771        case ROUTER_ASUS_WL520G:
772        case ROUTER_ASUS_WL500G_PRE_V2:
773          nvram_set ("vlan0ports", "0 1 2 3 5*");
774          nvram_set ("vlan1ports", "4 5");
775          break;
776        case ROUTER_LINKSYS_WTR54GS:
777          nvram_set ("vlan0ports", "0 5*");
778          nvram_set ("vlan1ports", "1 5");
779          break;
780        case ROUTER_ASUS_WL550GE:
781          nvram_set ("vlan0ports", "1 2 3 4 5*");
782          nvram_set ("vlan1ports", "0 5");
783          break;
784        case ROUTER_MOTOROLA:
785        case ROUTER_WRT54G_V8:
786          nvram_set ("vlan0ports", "3 2 1 0 5*");
787          nvram_set ("vlan1ports", "4 5");
788          break;
789        case ROUTER_LINKSYS_WRH54G:
790        case ROUTER_ASUS_WL500GD:
791          nvram_set ("vlan0ports", "4 3 2 1 5*");
792          nvram_set ("vlan1ports", "0 5");
793          break;
794        }
795      if (nvram_match ("bootnv_ver", "4")
796          || nvram_match ("boardnum", "WAP54GV3_8M_0614"))
797        {
798          nvram_set ("vlan0ports", "3 2 1 0 5*");
799          nvram_set ("vlan1ports", "4 5");
800        }
801#ifdef HAVE_SPUTNIK
802      nvram_set ("lan_ipaddr", "192.168.180.1");
803#elif HAVE_BUFFALO
804      nvram_set ("lan_ipaddr", "192.168.11.1");
805#else
806      nvram_set ("lan_ipaddr", "192.168.1.1");
807#endif
808    }
809#else
810  if (restore_defaults)
811    {
812      nvram_set ("lan_ipaddr", "192.168.10.1");
813    }
814#endif
815#ifdef HAVE_SKYTRON
816  if (restore_defaults)
817    {
818      nvram_set ("lan_ipaddr", "192.168.0.1");
819    }
820#endif
821  if (brand == ROUTER_WRT600N)
822    {
823
824      if (!nvram_get ("vlan0ports") || nvram_match ("vlan0ports", ""))
825        {
826          nvram_set ("vlan0ports", "1 2 3 4 8*");
827        }
828      if (!nvram_get ("vlan2ports") || nvram_match ("vlan2ports", ""))
829        {
830          nvram_set ("vlan2ports", "0 8");
831        }
832
833    }
834  else if (brand == ROUTER_WRT350N)
835    {
836
837      if (!nvram_get ("vlan1ports") || nvram_match ("vlan1ports", ""))
838        {
839          nvram_set ("vlan1ports", "1 2 3 4 8*");
840        }
841      if (!nvram_get ("vlan2ports") || nvram_match ("vlan2ports", ""))
842        {
843          nvram_set ("vlan2ports", "0 8");
844        }
845
846    }
847  else if (brand == ROUTER_BUFFALO_WZRG144NH)
848
849    {
850      if (!nvram_get ("vlan1ports") || nvram_match ("vlan1ports", ""))
851        {
852          nvram_set ("vlan1ports", "4 8");
853        }
854      if (!nvram_get ("vlan2ports") || nvram_match ("vlan2ports", ""))
855        {
856          nvram_set ("vlan2ports", "0 1 2 3 8");
857        }
858
859    }
860  else
861    {
862      if (!nvram_get ("vlan0hwname") || nvram_match ("vlan0hwname", ""))
863        nvram_set ("vlan0hwname", "et0");
864      if (!nvram_get ("vlan1hwname") || nvram_match ("vlan1hwname", ""))
865        nvram_set ("vlan1hwname", "et0");
866
867      switch (brand)
868        {
869        case ROUTER_MOTOROLA:
870        case ROUTER_MOTOROLA_V1:
871        case ROUTER_MOTOROLA_WE800G:
872        case ROUTER_RT210W:
873          if (et0mac != NULL)
874            nvram_set ("et0macaddr", et0mac);
875          if (et1mac != NULL)
876            nvram_set ("et1macaddr", et1mac);
877          break;
878        }
879
880      if (!nvram_get ("vlan0ports") || nvram_match ("vlan0ports", ""))
881        {
882          switch (brand)
883            {
884            case ROUTER_LINKSYS_WTR54GS:
885              nvram_set ("vlan0ports", "0 5*");
886              break;
887            case ROUTER_ASUS_WL500G_PRE:
888              nvram_set ("vlan0ports", "1 2 3 4 5*");
889              break;
890            case ROUTER_MOTOROLA:
891            case ROUTER_WRT54G_V8:
892              nvram_set ("vlan0ports", "3 2 1 0 5*");
893              break;
894            case ROUTER_LINKSYS_WRT55AG:
895            case ROUTER_RT480W:
896            case ROUTER_DELL_TRUEMOBILE_2300_V2:
897            case ROUTER_ASUS_WL520G:
898            case ROUTER_ASUS_WL500G_PRE_V2:
899              nvram_set ("vlan0ports", "0 1 2 3 5*");
900              break;
901            case ROUTER_LINKSYS_WRH54G:
902            case ROUTER_ASUS_WL500GD:
903              nvram_set ("vlan0ports", "4 3 2 1 5*");
904              break;
905            default:
906              if (nvram_match ("bootnv_ver", "4")
907                  || nvram_match ("boardnum", "WAP54GV3_8M_0614"))
908                nvram_set ("vlan0ports", "3 2 1 0 5*");
909              else
910                nvram_set ("vlan0ports", "1 2 3 4 5*");
911              break;
912            }
913        }
914
915      if (!nvram_get ("vlan1ports") || nvram_match ("vlan1ports", ""))
916        {
917          switch (brand)
918            {
919            case ROUTER_LINKSYS_WTR54GS:
920              nvram_set ("vlan1ports", "1 5");
921              break;
922            case ROUTER_ASUS_WL500G_PRE:
923            case ROUTER_LINKSYS_WRH54G:
924              nvram_set ("vlan1ports", "0 5");
925              break;
926            case ROUTER_MOTOROLA:
927            case ROUTER_WRT54G_V8:
928            case ROUTER_LINKSYS_WRT55AG:
929            case ROUTER_RT480W:
930            case ROUTER_DELL_TRUEMOBILE_2300_V2:
931            case ROUTER_ASUS_WL520G:
932            case ROUTER_ASUS_WL500G_PRE_V2:
933              nvram_set ("vlan1ports", "4 5");
934              break;
935            default:
936              if (nvram_match ("bootnv_ver", "4")
937                  || nvram_match ("boardnum", "WAP54GV3_8M_0614"))
938                nvram_set ("vlan1ports", "4 5");
939              else
940                nvram_set ("vlan1ports", "0 5");
941              break;
942            }
943        }
944
945    }
946
947  if (brand == ROUTER_WRT54G || brand == ROUTER_WRT54G1X
948      || brand == ROUTER_LINKSYS_WRT55AG)
949    {
950      if (!nvram_get ("aa0"))
951        nvram_set ("aa0", "3");
952      if (!nvram_get ("ag0"))
953        nvram_set ("ag0", "255");
954      if (!nvram_get ("gpio2"))
955        nvram_set ("gpio2", "adm_eecs");
956      if (!nvram_get ("gpio3"))
957        nvram_set ("gpio3", "adm_eesk");
958      if (!nvram_get ("gpio5"))
959        nvram_set ("gpio5", "adm_eedi");
960      if (!nvram_get ("gpio6"))
961        nvram_set ("gpio6", "adm_rc");
962      if (!nvram_get ("boardrev") || nvram_match ("boardrev", ""))
963        nvram_set ("boardrev", "0x10");
964      if (!nvram_get ("boardflags") || nvram_match ("boardflags", ""))
965        nvram_set ("boardflags", "0x0388");
966      if (!nvram_get ("boardflags2"))
967        nvram_set ("boardflags2", "0");
968    }
969  /* Always set OS defaults */
970  nvram_set ("os_name", "linux");
971  nvram_set ("os_version", EPI_VERSION_STR);
972
973#ifdef HAVE_DDLAN
974  nvram_unset ("cur_rssi");
975  nvram_unset ("cur_noise");
976  nvram_unset ("cur_bssid");
977  nvram_unset ("cur_snr");
978  nvram_set ("cur_state",
979             "<span style=\"background-color: rgb(255, 0, 0);\">Nicht Verbunden</span>");
980#endif
981#ifdef HAVE_SPUTNIK_APD
982  /* Added for Sputnik Agent */
983  nvram_unset ("sputnik_mjid");
984  nvram_unset ("sputnik_rereg");
985#endif
986
987  if (nvram_get ("overclocking") == NULL)
988    nvram_set ("overclocking", nvram_safe_get ("clkfreq"));
989  cprintf ("start overclocking\n");
990  start_overclocking ();
991  cprintf ("done()");
992  if (nvram_get ("http_username") != NULL)
993    {
994      if (nvram_match ("http_username", ""))
995        {
996#ifdef HAVE_POWERNOC
997          nvram_set ("http_username", "bJz7PcC1rCRJQ"); //admin
998#else
999          nvram_set ("http_username", "bJ/GddyoJuiU2"); //root
1000#endif
1001        }
1002    }
1003
1004  cprintf ("check CFE nv\n");
1005  if (check_now_boot () == PMON_BOOT)
1006    check_pmon_nv ();
1007  else
1008    check_cfe_nv ();
1009  cprintf ("restore defaults\n");
1010
1011  /* Commit values */
1012  if (restore_defaults)
1013    {
1014      int i;
1015      unset_nvram ();
1016      nvram_commit ();
1017      cprintf ("done\n");
1018      for (i = 0; i < MAX_NVPARSE; i++)
1019        {
1020          del_wds_wsec (0, i);
1021          del_wds_wsec (1, i);
1022        }
1023    }
1024}
1025
1026
1027
1028
1029
1030
1031/* States */
1032enum
1033{
1034  RESTART,
1035  STOP,
1036  START,
1037  TIMER,
1038  USER,
1039  IDLE,
1040};
1041static int state = START;
1042static int signalled = -1;
1043
1044/* Signal handling */
1045static void
1046rc_signal (int sig)
1047{
1048  if (state == IDLE)
1049    {
1050      if (sig == SIGHUP)
1051        {
1052          printf ("signalling RESTART\n");
1053          signalled = RESTART;
1054        }
1055      else if (sig == SIGUSR2)
1056        {
1057          printf ("signalling START\n");
1058          signalled = START;
1059        }
1060      else if (sig == SIGINT)
1061        {
1062          printf ("signalling STOP\n");
1063          signalled = STOP;
1064        }
1065      else if (sig == SIGALRM)
1066        {
1067          printf ("signalling TIMER\n");
1068          signalled = TIMER;
1069        }
1070      else if (sig == SIGUSR1)
1071        {                       // Receive from WEB
1072          printf ("signalling USER1\n");
1073          signalled = USER;
1074        }
1075
1076    }
1077}
1078
1079/* Timer procedure */
1080int
1081do_timer (void)
1082{
1083  //do_ntp();
1084  return 0;
1085}
1086
1087
1088#define CONVERT_NV(old, new) \
1089        if(nvram_get(old)) \
1090                nvram_set(new, nvram_safe_get(old));
1091
1092int
1093start_nvram (void)
1094{
1095  int i = 0;
1096
1097  /* broadcom 3.11.48.7 change some nvram name */
1098#ifdef HAVE_MSSID
1099  CONVERT_NV ("d11g_channel", "wl_channel");
1100#else
1101  CONVERT_NV ("d11g_channel", "wl0_channel");
1102#endif
1103  CONVERT_NV ("d11g_rateset", "wl_rateset");
1104  CONVERT_NV ("d11g_rts", "wl_rts");
1105  CONVERT_NV ("d11g_bcn", "wl_bcn");
1106  CONVERT_NV ("d11g_mode", "wl_gmode");
1107  CONVERT_NV ("d11g_rate", "wl_rate");
1108  CONVERT_NV ("d11g_frag", "wl_frag");
1109  CONVERT_NV ("d11g_dtim", "wl_dtim");
1110
1111  nvram_unset ("wl0_hwaddr");   // When disbale wireless, we must get null wireless mac */
1112
1113  nvram_set ("wan_get_dns", "");
1114  nvram_set ("filter_id", "1");
1115  nvram_set ("wl_active_add_mac", "0");
1116  nvram_set ("ddns_change", "");
1117  nvram_unset ("action_service");
1118  nvram_set ("wan_get_domain", "");
1119
1120
1121  //if(!nvram_get("wl_macmode1")){
1122  //      if(nvram_match("wl_macmode","disabled"))
1123  //              nvram_set("wl_macmode1","disabled");
1124  //      else
1125  //              nvram_set("wl_macmode1","other");
1126  //}
1127  if (nvram_match ("wl_gmode", "5"))    // Mixed mode had been changed to 5
1128    nvram_set ("wl_gmode", "1");
1129
1130  if (nvram_match ("wl_gmode", "4"))    // G-ONLY mode had been changed to 2, after 1.40.1 for WiFi G certication
1131    nvram_set ("wl_gmode", "2");
1132
1133//      nvram_set("wl_country","Worldwide");    // The country always Worldwide
1134
1135#ifndef AOL_SUPPORT
1136  nvram_set ("aol_block_traffic", "0");
1137  nvram_set ("aol_block_traffic1", "0");
1138  nvram_set ("aol_block_traffic2", "0");
1139#endif
1140  nvram_set ("ping_ip", "");
1141  nvram_set ("ping_times", "");
1142//  nvram_set ("traceroute_ip", "");
1143
1144  nvram_set ("filter_port", "");        // The name have been disbaled from 1.41.3
1145
1146#ifdef HAVE_UPNP
1147  if ((nvram_match ("restore_defaults", "1"))
1148      || (nvram_match ("upnpcas", "1")))
1149    {
1150      nvram_set ("upnp_clear", "1");
1151    }
1152  else
1153    {
1154      char s[32];
1155      char *nv;
1156      for (i = 0; i < MAX_NVPARSE; ++i)
1157        {
1158          sprintf (s, "forward_port%d", i);
1159          if ((nv = nvram_get (s)) != NULL)
1160            {
1161              if (strstr (nv, "msmsgs"))
1162                nvram_unset (s);
1163            }
1164        }
1165    }
1166  nvram_set ("upnp_wan_proto", "");
1167#endif
1168
1169  /* The tkip and aes already are changed to wl_crypto from v3.63.3.0 */
1170  if (nvram_match ("wl_wep", "tkip"))
1171    {
1172      nvram_set ("wl_crypto", "tkip");
1173    }
1174  else if (nvram_match ("wl_wep", "aes"))
1175    {
1176      nvram_set ("wl_crypto", "aes");
1177    }
1178  else if (nvram_match ("wl_wep", "tkip+aes"))
1179    {
1180      nvram_set ("wl_crypto", "tkip+aes");
1181    }
1182
1183  if (nvram_match ("wl_wep", "restricted"))
1184    nvram_set ("wl_wep", "enabled");    // the nas need this value, the "restricted" is no longer need. (20040624 by honor)
1185
1186
1187#ifdef HAVE_SET_BOOT
1188  if (!nvram_match ("boot_wait_web", "0"))
1189    nvram_set ("boot_wait_web", "1");
1190#endif
1191
1192#ifdef HAVE_SSHD
1193  if (!nvram_match ("sshd_web", "0"))
1194    nvram_set ("sshd_web", "1");
1195#endif
1196
1197#ifndef HAVE_MSSID
1198  nvram_set ("wl0_country_code", "JP");
1199  nvram_set ("wl0_country", "Japan");
1200#endif
1201#ifndef HAVE_BUFFALO
1202  if (check_hw_type () == BCM5352E_CHIP)
1203    {
1204      nvram_set ("opo", "0");   // OFDM power reducement in quarter dbm (2 dbm in this case)
1205      nvram_set ("ag0", "0");   // Antenna Gain definition in dbm
1206    }
1207#endif
1208
1209  if (nvram_match ("svqos_port1bw", "full"))
1210    nvram_set ("svqos_port1bw", "FULL");
1211  if (nvram_match ("svqos_port2bw", "full"))
1212    nvram_set ("svqos_port2bw", "FULL");
1213  if (nvram_match ("svqos_port3bw", "full"))
1214    nvram_set ("svqos_port3bw", "FULL");
1215  if (nvram_match ("svqos_port4bw", "full"))
1216    nvram_set ("svqos_port4bw", "FULL");
1217  //dirty fix for WBR2 units
1218
1219
1220  if (strlen (nvram_safe_get ("http_username")) == 0)
1221    {
1222      nvram_set ("http_username", zencrypt ("root"));
1223      nvram_set ("http_passwd", zencrypt ("admin"));
1224    }
1225
1226  //clean old filter_servicesX to free nvram
1227  nvram_unset ("filter_services0");
1228  nvram_unset ("filter_services1");
1229  nvram_unset ("filter_services2");
1230  nvram_unset ("filter_services3");
1231  nvram_unset ("filter_services4");
1232  nvram_unset ("filter_services5");
1233  nvram_unset ("filter_services6");
1234  nvram_unset ("filter_services7");
1235
1236  nvram_unset ("vdsl_state");   //important (this value should never be commited, but if this will fix the vlan7 issue)
1237
1238#ifdef DIST
1239  nvram_set ("dist_type", DIST);
1240#endif
1241
1242  {
1243
1244#ifdef DIST
1245#ifndef HAVE_TW6600
1246#ifdef HAVE_MICRO
1247//if dist_type micro, check styles, and force to elegant if needed
1248    char *style = nvram_safe_get ("router_style");
1249    if (!strstr ("blue cyan elegant green orange red yellow", style))
1250      {
1251        nvram_set ("router_style", "elegant");
1252      }
1253#endif
1254#endif
1255#endif
1256  }
1257
1258#ifdef HAVE_WIVIZ
1259  if (!strlen (nvram_safe_get ("hopseq"))
1260      || !strlen (nvram_safe_get ("hopdwell")))
1261    {
1262#ifdef HAVE_MSSID
1263      char *channel = nvram_safe_get ("wl0_channel");
1264#else
1265      char *channel = nvram_safe_get ("wl_channel");
1266#endif
1267
1268      nvram_set ("hopdwell", "1000");
1269      nvram_set ("hopseq", channel);
1270    }
1271#endif
1272
1273
1274  return 0;
1275}
1276
1277static void
1278unset_nvram (void)
1279{
1280#ifndef MPPPOE_SUPPORT
1281  nvram_safe_unset ("ppp_username_1");
1282  nvram_safe_unset ("ppp_passwd_1");
1283  nvram_safe_unset ("ppp_idletime_1");
1284  nvram_safe_unset ("ppp_demand_1");
1285  nvram_safe_unset ("ppp_redialperiod_1");
1286  nvram_safe_unset ("ppp_service_1");
1287  nvram_safe_unset ("mpppoe_enable");
1288  nvram_safe_unset ("mpppoe_dname");
1289#endif
1290#ifndef HAVE_HTTPS
1291  nvram_safe_unset ("remote_mgt_https");
1292#endif
1293#ifndef HSIAB_SUPPORT
1294  nvram_safe_unset ("hsiab_mode");
1295  nvram_safe_unset ("hsiab_provider");
1296  nvram_safe_unset ("hsiab_device_id");
1297  nvram_safe_unset ("hsiab_device_password");
1298  nvram_safe_unset ("hsiab_admin_url");
1299  nvram_safe_unset ("hsiab_registered");
1300  nvram_safe_unset ("hsiab_configured");
1301  nvram_safe_unset ("hsiab_register_ops");
1302  nvram_safe_unset ("hsiab_session_ops");
1303  nvram_safe_unset ("hsiab_config_ops");
1304  nvram_safe_unset ("hsiab_manual_reg_ops");
1305  nvram_safe_unset ("hsiab_proxy_host");
1306  nvram_safe_unset ("hsiab_proxy_port");
1307  nvram_safe_unset ("hsiab_conf_time");
1308  nvram_safe_unset ("hsiab_stats_time");
1309  nvram_safe_unset ("hsiab_session_time");
1310  nvram_safe_unset ("hsiab_sync");
1311  nvram_safe_unset ("hsiab_config");
1312#endif
1313
1314#ifndef HEARTBEAT_SUPPORT
1315  nvram_safe_unset ("hb_server_ip");
1316  nvram_safe_unset ("hb_server_domain");
1317#endif
1318
1319#ifndef PARENTAL_CONTROL_SUPPORT
1320  nvram_safe_unset ("artemis_enable");
1321  nvram_safe_unset ("artemis_SVCGLOB");
1322  nvram_safe_unset ("artemis_HB_DB");
1323  nvram_safe_unset ("artemis_provisioned");
1324#endif
1325
1326#ifndef WL_STA_SUPPORT
1327//        nvram_safe_unset("wl_ap_ssid");
1328//        nvram_safe_unset("wl_ap_ip");
1329#endif
1330
1331
1332}
Note: See TracBrowser for help on using the repository browser.