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

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

not required anymore

File size: 38.9 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#define WL_IOCTL(name, cmd, buf, len) (ret = wl_ioctl((name), (cmd), (buf), (len)))
70
71#define TXPWR_MAX 251
72#define TXPWR_DEFAULT 28
73
74void start_restore_defaults( void );
75static void rc_signal( int sig );
76extern void start_overclocking( void );
77extern int check_cfe_nv( void );
78extern int check_pmon_nv( void );
79static void unset_nvram( void );
80int start_nvram( void );
81
82extern struct nvram_tuple srouter_defaults[];
83
84int endswith( char *str, char *cmp )
85{
86    int cmp_len, str_len, i;
87
88    cmp_len = strlen( cmp );
89    str_len = strlen( str );
90    if( cmp_len > str_len )
91        return ( 0 );
92    for( i = 0; i < cmp_len; i++ )
93    {
94        if( str[( str_len - 1 ) - i] != cmp[( cmp_len - 1 ) - i] )
95            return ( 0 );
96    }
97    return ( 1 );
98}
99
100#ifdef HAVE_MACBIND
101#include "../../../opt/mac.h"
102#endif
103void runStartup( char *folder, char *extension )
104{
105    struct dirent *entry;
106    DIR *directory;
107
108    directory = opendir( folder );
109    if( directory == NULL )
110    {
111        return;
112    }
113    // list all files in this directory
114    while( ( entry = readdir( directory ) ) != NULL )
115    {
116        if( endswith( entry->d_name, extension ) )
117        {
118#ifdef HAVE_REGISTER
119            if( !isregistered_real(  ) )
120            {
121                if( endswith( entry->d_name, "wdswatchdog.startup" ) )
122                    continue;
123                if( endswith( entry->d_name, "schedulerb.startup" ) )
124                    continue;
125                if( endswith( entry->d_name, "proxywatchdog.startup" ) )
126                    continue;
127            }
128#endif
129            sysprintf( "%s/%s 2>&1 > /dev/null&\n", folder, entry->d_name );
130            // execute script
131        }
132    }
133    closedir( directory );
134}
135
136/*
137 * SeG dd-wrt addition for module startup scripts
138 */
139int start_modules( void )
140{
141    runStartup( "/etc/config", ".startup" );
142
143#ifdef HAVE_RB500
144    runStartup( "/usr/local/etc/config", ".startup" );  // if available
145#elif HAVE_X86
146    runStartup( "/usr/local/etc/config", ".startup" );  // if available
147#else
148    runStartup( "/jffs/etc/config", ".startup" );       // if available
149    runStartup( "/mmc/etc/config", ".startup" );        // if available
150#endif
151    return 0;
152}
153
154int start_wanup( void )
155{
156    runStartup( "/etc/config", ".wanup" );
157#ifdef HAVE_RB500
158    runStartup( "/usr/local/etc/config", ".wanup" );    // if available
159#elif HAVE_X86
160    runStartup( "/usr/local/etc/config", ".wanup" );    // if available
161#else
162    runStartup( "/jffs/etc/config", ".wanup" ); // if available
163    runStartup( "/mmc/etc/config", ".wanup" );  // if available
164    runStartup( "/tmp/etc/config", ".wanup" );  // if available
165#endif
166    return 0;
167}
168
169int start_create_rc_startup( void )
170{
171    create_rc_file( RC_STARTUP );
172    return 0;
173}
174
175int start_create_rc_shutdown( void )
176{
177    create_rc_file( RC_SHUTDOWN );
178    return 0;
179}
180
181int create_rc_file( char *name )
182{
183    FILE *fp;
184    char *p = nvram_safe_get( name );
185    char tmp_file[100] = { 0 };
186
187    if( ( void * )0 == name || 0 == p[0] )
188        return -1;
189
190    snprintf( tmp_file, 100, "/tmp/.%s", name );
191    unlink( tmp_file );
192
193    fp = fopen( tmp_file, "w" );
194    if( fp )
195    {
196        // filter Windows <cr>ud
197        while( *p )
198        {
199            if( *p != 0x0d )
200                fprintf( fp, "%c", *p );
201            p++;
202        }
203    }
204    fclose( fp );
205    chmod( tmp_file, 0700 );
206
207    return 0;
208}
209
210static void ses_cleanup( void )
211{
212    /*
213     * well known event to cleanly initialize state machine
214     */
215    nvram_set( "ses_event", "2" );
216
217    /*
218     * Delete lethal dynamically generated variables
219     */
220    nvram_unset( "ses_bridge_disable" );
221}
222
223static void ses_restore_defaults( void )
224{
225    char tmp[100], prefix[] = "wlXXXXXXXXXX_ses_";
226    int i;
227
228    /*
229     * Delete dynamically generated variables
230     */
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 start_restore_defaults( void )
246{
247
248#ifdef HAVE_RB500
249    struct nvram_tuple generic[] = {
250        {"lan_ifname", "br0", 0},
251        {"lan_ifnames",
252         "eth1 eth2 eth3 eth4 eth5 eth6 eth7 eth8 ath0 ath1 ath2 ath3 ath4 ath5",
253         0},
254        {"wan_ifname", "eth0", 0},
255        {"wan_ifnames", "eth0", 0},
256        {0, 0, 0}
257    };
258#elif HAVE_GEMTEK
259    struct nvram_tuple generic[] = {
260        {"lan_ifname", "br0", 0},
261        {"lan_ifnames", "eth1 ath0", 0},
262        {"wan_ifname", "eth0", 0},
263        {"wan_ifnames", "eth0", 0},
264        {0, 0, 0}
265    };
266#elif HAVE_GATEWORX
267    struct nvram_tuple generic[] = {
268        {"lan_ifname", "br0", 0},
269        {"lan_ifnames", "ixp0 ath0 ath1 ath2 ath3",
270         0},
271        {"wan_ifname2", "ixp1", 0},
272        {"wan_ifname", "ixp1", 0},
273        {"wan_default", "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",
282         0},
283#else
284#ifdef HAVE_GW700
285        {"lan_ifnames",
286         "eth0 eth2 eth3 eth4 eth5 eth6 eth7 eth8 eth9 eth10 ath0 ath1 ath2 ath3 ath5 ath6 ath7 ath8",
287         0},
288#else
289        {"lan_ifnames",
290         "eth1 eth2 eth3 eth4 eth5 eth6 eth7 eth8 eth9 eth10 ath0 ath1 ath2 ath3 ath5 ath6 ath7 ath8",
291         0},
292#endif
293#endif
294#ifdef HAVE_GW700
295        {"wan_ifname", "eth1", 0},
296        {"wan_ifname2", "eth1", 0},
297        {"wan_ifnames", "eth1", 0},
298#else
299        {"wan_ifname", "eth0", 0},
300        {"wan_ifname2", "eth0", 0},
301        {"wan_ifnames", "eth0", 0},
302#endif
303        {0, 0, 0}
304    };
305#elif HAVE_XSCALE
306    struct nvram_tuple generic[] = {
307        {"lan_ifname", "br0", 0},
308        {"lan_ifnames",
309         "ixp0.1 ixp0.2 ath0 ath1",
310         0},
311        {"wan_ifname", "ixp1", 0},
312        {"wan_ifname2", "ixp1", 0},
313        {"wan_ifnames", "ixp1", 0},
314        {"wan_default", "ixp1", 0},
315        {0, 0, 0}
316    };
317#elif HAVE_MAGICBOX
318    struct nvram_tuple generic[] = {
319        {"lan_ifname", "br0", 0},
320        {"lan_ifnames", "eth1 ath0",
321         0},
322        {"wan_ifname", "eth0", 0},
323        {"wan_ifname2", "eth0", 0},
324        {"wan_ifnames", "eth0", 0},
325        {"wan_default", "eth0", 0},
326        {0, 0, 0}
327    };
328#elif HAVE_FONERA
329    struct nvram_tuple generic[] = {
330        {"lan_ifname", "br0", 0},
331        {"lan_ifnames", "vlan0 ath0", 0},
332        {"wan_ifname", "", 0},
333        {"wan_ifname2", "", 0},
334        {"wan_default", "", 0},
335        {"wan_ifnames", "eth0 vlan1", 0},
336        {0, 0, 0}
337    };
338#elif HAVE_LS2
339    struct nvram_tuple generic[] = {
340        {"lan_ifname", "br0", 0},
341        {"lan_ifnames", "vlan2 ath0", 0},
342        {"wan_ifname", "vlan0", 0},
343        {"wan_ifname2", "vlan0", 0},
344        {"wan_ifnames", "vlan0", 0},
345        {"wan_default", "vlan0", 0},
346        {0, 0, 0}
347    };
348#elif HAVE_LSX
349    struct nvram_tuple generic[] = {
350        {"lan_ifname", "br0", 0},
351        {"lan_ifnames", "eth0 ath0", 0},
352        {"wan_ifname", "", 0},
353        {"wan_ifname2", "", 0},
354        {"wan_ifnames", "", 0},
355        {"wan_default", "", 0},
356        {0, 0, 0}
357    };
358#elif HAVE_DANUBE
359    struct nvram_tuple generic[] = {
360        {"lan_ifname", "br0", 0},
361        {"lan_ifnames", "eth0 ath0", 0},
362        {"wan_ifname", "", 0},
363        {"wan_ifname2", "", 0},
364        {"wan_ifnames", "", 0},
365        {"wan_default", "", 0},
366        {0, 0, 0}
367    };
368#elif HAVE_STORM
369    struct nvram_tuple generic[] = {
370        {"lan_ifname", "br0", 0},
371        {"lan_ifnames", "eth0 ath0", 0},
372        {"wan_ifname", "", 0},
373        {"wan_ifname2", "", 0},
374        {"wan_ifnames", "", 0},
375        {"wan_default", "", 0},
376        {0, 0, 0}
377    };
378#elif HAVE_ADM5120
379    struct nvram_tuple generic[] = {
380        {"lan_ifname", "br0", 0},
381        {"lan_ifnames", "eth0 ath0", 0},
382        {"wan_ifname", "", 0},
383        {"wan_ifname2", "", 0},
384        {"wan_ifnames", "", 0},
385        {"wan_default", "", 0},
386        {0, 0, 0}
387    };
388#elif HAVE_LS5
389    struct nvram_tuple generic[] = {
390        {"lan_ifname", "br0", 0},
391        {"lan_ifnames", "ath0", 0},
392        {"wan_ifname", "eth0", 0},
393        {"wan_ifname2", "eth0", 0},
394        {"wan_ifnames", "eth0", 0},
395        {"wan_default", "eth0", 0},
396        {0, 0, 0}
397    };
398#elif HAVE_WHRAG108
399    struct nvram_tuple generic[] = {
400        {"lan_ifname", "br0", 0},
401        {"lan_ifnames", "eth0 ath0 ath1", 0},
402        {"wan_ifname2", "eth1", 0},
403        {"wan_ifname", "eth1", 0},
404        {"wan_ifnames", "eth1", 0},
405        {"wan_default", "eth1", 0},
406        {0, 0, 0}
407    };
408#elif HAVE_PB42
409    struct nvram_tuple generic[] = {
410        {"lan_ifname", "br0", 0},
411        {"lan_ifnames", "eth1 ath0 ath1", 0},
412        {"wan_ifname2", "eth0", 0},
413        {"wan_ifname", "eth0", 0},
414        {"wan_ifnames", "eth0", 0},
415        {"wan_default", "eth0", 0},
416        {0, 0, 0}
417    };
418#elif HAVE_TW6600
419    struct nvram_tuple generic[] = {
420        {"lan_ifname", "br0", 0},
421        {"lan_ifnames", "ath0 ath1", 0},
422        {"wan_ifname2", "eth0", 0},
423        {"wan_ifname", "eth0", 0},
424        {"wan_ifnames", "eth0", 0},
425        {"wan_default", "eth0", 0},
426        {0, 0, 0}
427    };
428#elif HAVE_CA8
429    struct nvram_tuple generic[] = {
430        {"lan_ifname", "br0", 0},
431        {"lan_ifnames", "ath0", 0},
432        {"wan_ifname", "eth0", 0},
433        {"wan_ifname2", "eth0", 0},
434        {"wan_ifnames", "eth0", 0},
435        {"wan_default", "eth0", 0},
436        {0, 0, 0}
437    };
438#else
439    struct nvram_tuple generic[] = {
440        {"lan_ifname", "br0", 0},
441        {"lan_ifnames", "eth0 eth2 eth3 eth4", 0},
442        {"wan_ifname", "eth1", 0},
443        {"wan_ifname2", "eth1", 0},
444        {"wan_ifnames", "eth1", 0},
445        {"wan_default", "eth1", 0},
446        {0, 0, 0}
447    };
448    struct nvram_tuple vlan[] = {
449        {"lan_ifname", "br0", 0},
450        {"lan_ifnames", "vlan0 eth1 eth2 eth3", 0},
451        {"wan_ifname", "vlan1", 0},
452        {"wan_ifname2", "vlan1", 0},
453        {"wan_ifnames", "vlan1", 0},
454        {"wan_default", "vlan1", 0},
455        {0, 0, 0}
456    };
457
458    struct nvram_tuple wrt350vlan[] = {
459        {"lan_ifname", "br0", 0},
460        {"lan_ifnames", "vlan1 eth0", 0},
461        {"wan_ifname", "vlan2", 0},
462        {"wan_ifname2", "vlan2", 0},
463        {"wan_ifnames", "vlan2", 0},
464        {"wan_default", "vlan2", 0},
465        {0, 0, 0}
466    };
467
468    struct nvram_tuple wrt30011vlan[] = {
469        {"lan_ifname", "br0", 0},
470        {"lan_ifnames", "vlan0 eth0", 0},
471        {"wan_ifname", "vlan1", 0},
472        {"wan_ifname2", "vlan1", 0},
473        {"wan_ifnames", "vlan1", 0},
474        {"wan_default", "vlan1", 0},
475        {0, 0, 0}
476    };
477
478    struct nvram_tuple wrt600vlan[] = {
479        {"lan_ifname", "br0", 0},
480        {"lan_ifnames", "vlan0 eth0 eth1", 0},
481        {"wan_ifname", "vlan2", 0},
482        {"wan_ifname2", "vlan2", 0},
483        {"wan_ifnames", "vlan2", 0},
484        {"wan_default", "vlan2", 0},
485        {0, 0, 0}
486    };
487
488    struct nvram_tuple wrt60011vlan[] = {
489        {"lan_ifname", "br0", 0},
490        {"lan_ifnames", "vlan1 eth0 eth1", 0},
491        {"wan_ifname", "vlan2", 0},
492        {"wan_ifname2", "vlan2", 0},
493        {"wan_ifnames", "vlan2", 0},
494        {"wan_default", "vlan2", 0},
495        {0, 0, 0}
496    };
497
498    struct nvram_tuple wzr144nhvlan[] = {
499        {"lan_ifname", "br0", 0},
500        {"lan_ifnames", "vlan2 eth0", 0},
501        {"wan_ifname", "vlan1", 0},
502        {"wan_ifname2", "vlan1", 0},
503        {"wan_ifnames", "vlan1", 0},
504        {"wan_default", "vlan1", 0},
505        {0, 0, 0}
506    };
507
508    struct nvram_tuple generic_2[] = {
509        {"lan_ifname", "br0", 0},
510        {"lan_ifnames", "eth1 eth2", 0},
511        {"wan_ifname", "eth0", 0},
512        {"wan_ifname2", "eth0", 0},
513        {"wan_ifnames", "eth0", 0},
514        {"wan_default", "eth0", 0},
515        {0, 0, 0}
516    };
517
518    struct nvram_tuple generic_3[] = {
519        {"lan_ifname", "br0", 0},
520        {"lan_ifnames", "eth0 eth1", 0},
521        {"wan_ifname", "eth2", 0},
522        {"wan_ifname2", "eth2", 0},
523        {"wan_ifnames", "eth2", 0},
524        {"wan_default", "eth2", 0},
525        {0, 0, 0}
526    };
527#endif
528
529    struct nvram_tuple *linux_overrides;
530    struct nvram_tuple *t, *u;
531    int restore_defaults = 0;
532
533    // uint boardflags;
534
535    /*
536     * Restore defaults if told to.
537     *
538     * Note: an intentional side effect is that when upgrading from a
539     * firmware without the sv_restore_defaults var, defaults will also be
540     * restored.
541     */
542    char *et0mac = nvram_safe_get( "et0macaddr" );
543    char *et1mac = nvram_safe_get( "et1macaddr" );
544
545    // unsigned char mac[20];
546    // if (getRouterBrand () == ROUTER_BUFFALO_WZRG144NH)
547    // {
548    // if (nvram_get ("il0macaddr") == NULL)
549    // {
550    // strcpy (mac, et0mac);
551    // MAC_ADD (mac);
552    // nvram_set ("il0macaddr", mac);
553    // }
554    // }
555
556#ifdef HAVE_RB500
557    linux_overrides = generic;
558    int brand = getRouterBrand(  );
559#elif HAVE_XSCALE
560    linux_overrides = generic;
561    int brand = getRouterBrand(  );
562
563    if( nvram_invmatch( "sv_restore_defaults", "0" ) )  // ||
564        // nvram_invmatch("os_name",
565        // "linux"))
566    {
567        restore_defaults = 1;
568    }
569#elif HAVE_X86
570    linux_overrides = generic;
571    int brand = getRouterBrand(  );
572
573    if( nvram_invmatch( "sv_restore_defaults", "0" ) )  // ||
574        // nvram_invmatch("os_name",
575        // "linux"))
576    {
577        restore_defaults = 1;
578    }
579#elif HAVE_MAGICBOX
580    linux_overrides = generic;
581    int brand = getRouterBrand(  );
582
583    if( nvram_invmatch( "sv_restore_defaults", "0" ) )  // ||
584        // nvram_invmatch("os_name",
585        // "linux"))
586    {
587        restore_defaults = 1;
588    }
589#elif HAVE_GATEWORX
590    linux_overrides = generic;
591    int brand = getRouterBrand(  );
592
593    if( nvram_invmatch( "sv_restore_defaults", "0" ) )  // ||
594        // nvram_invmatch("os_name",
595        // "linux"))
596    {
597        restore_defaults = 1;
598    }
599#elif HAVE_FONERA
600    linux_overrides = generic;
601    int brand = getRouterBrand(  );
602
603    if( nvram_invmatch( "sv_restore_defaults", "0" ) )  // ||
604        // nvram_invmatch("os_name",
605        // "linux"))
606    {
607        restore_defaults = 1;
608    }
609#elif HAVE_LS2
610    linux_overrides = generic;
611    int brand = getRouterBrand(  );
612
613    if( nvram_invmatch( "sv_restore_defaults", "0" ) )  // ||
614        // nvram_invmatch("os_name",
615        // "linux"))
616    {
617        restore_defaults = 1;
618    }
619#elif HAVE_LS5
620    linux_overrides = generic;
621    int brand = getRouterBrand(  );
622
623    if( nvram_invmatch( "sv_restore_defaults", "0" ) )  // ||
624        // nvram_invmatch("os_name",
625        // "linux"))
626    {
627        restore_defaults = 1;
628    }
629#elif HAVE_WHRAG108
630    linux_overrides = generic;
631    int brand = getRouterBrand(  );
632
633    if( nvram_invmatch( "sv_restore_defaults", "0" ) )  // ||
634        // nvram_invmatch("os_name",
635        // "linux"))
636    {
637        restore_defaults = 1;
638    }
639#elif HAVE_TW6600
640    linux_overrides = generic;
641    int brand = getRouterBrand(  );
642
643    if( nvram_invmatch( "sv_restore_defaults", "0" ) )  // ||
644        // nvram_invmatch("os_name",
645        // "linux"))
646    {
647        restore_defaults = 1;
648    }
649#elif HAVE_PB42
650    linux_overrides = generic;
651    int brand = getRouterBrand(  );
652
653    if( nvram_invmatch( "sv_restore_defaults", "0" ) )  // ||
654        // nvram_invmatch("os_name",
655        // "linux"))
656    {
657        restore_defaults = 1;
658    }
659#elif HAVE_LSX
660    linux_overrides = generic;
661    int brand = getRouterBrand(  );
662
663    if( nvram_invmatch( "sv_restore_defaults", "0" ) )  // ||
664        // nvram_invmatch("os_name",
665        // "linux"))
666    {
667        restore_defaults = 1;
668    }
669#elif HAVE_DANUBE
670    linux_overrides = generic;
671    int brand = getRouterBrand(  );
672
673    if( nvram_invmatch( "sv_restore_defaults", "0" ) )  // ||
674        // nvram_invmatch("os_name",
675        // "linux"))
676    {
677        restore_defaults = 1;
678    }
679#elif HAVE_STORM
680    linux_overrides = generic;
681    int brand = getRouterBrand(  );
682
683    if( nvram_invmatch( "sv_restore_defaults", "0" ) )  // ||
684        // nvram_invmatch("os_name",
685        // "linux"))
686    {
687        restore_defaults = 1;
688    }
689#elif HAVE_ADM5120
690    linux_overrides = generic;
691    int brand = getRouterBrand(  );
692
693    if( nvram_invmatch( "sv_restore_defaults", "0" ) )  // ||
694        // nvram_invmatch("os_name",
695        // "linux"))
696    {
697        restore_defaults = 1;
698    }
699#elif HAVE_CA8
700    linux_overrides = generic;
701    int brand = getRouterBrand(  );
702
703    if( nvram_invmatch( "sv_restore_defaults", "0" ) )  // ||
704        // nvram_invmatch("os_name",
705        // "linux"))
706    {
707        restore_defaults = 1;
708    }
709#elif HAVE_GEMTEK
710    linux_overrides = generic;
711    int brand = getRouterBrand(  );
712#else
713    int brand = getRouterBrand(  );
714
715    if( nvram_invmatch( "sv_restore_defaults", "0" ) )  // ||
716        // nvram_invmatch("os_name",
717        // "linux"))
718    {
719        // nvram_unset("sv_restore_defaults");
720        restore_defaults = 1;
721    }
722    if( nvram_match( "product_name", "INSPECTION" ) )
723    {
724        nvram_unset( "product_name" );
725        restore_defaults = 1;
726    }
727    if( nvram_get( "router_name" ) == NULL )
728        restore_defaults = 1;
729
730    if( restore_defaults )
731    {
732        cprintf( "Restoring defaults...\n" );
733        /*
734         * these unsets are important for routers where we can't erase nvram
735         * and only software restore defaults
736         */
737        nvram_unset( "wan_to_lan" );
738        nvram_unset( "wl_vifs" );
739        nvram_unset( "wl0_vifs" );
740    }
741
742    // }
743
744    /*
745     * Delete dynamically generated variables
746     */
747    /*
748     * Choose default lan/wan i/f list.
749     */
750    char *ds;
751
752    switch ( brand )
753    {
754#ifndef HAVE_BUFFALO
755        case ROUTER_WRTSL54GS:
756        case ROUTER_WRT150N:
757        case ROUTER_WRT160N:
758        case ROUTER_WRT300N:
759        case ROUTER_NETGEAR_WNR834B:
760        case ROUTER_NETGEAR_WNR834BV2:
761        case ROUTER_NETGEAR_WNDR3300:
762        case ROUTER_ASUS_WL500G:
763        case ROUTER_ASUS_WL500W:
764#endif
765        case ROUTER_BUFFALO_WZRG300N:
766        case ROUTER_BUFFALO_WLAH_G54:
767        case ROUTER_BUFFALO_WAPM_HP_AM54G54:
768        case ROUTER_BUFFALO_WZRRSG54:
769        case ROUTER_ASKEY_RT220XD:
770            linux_overrides = generic;
771            break;
772#ifndef HAVE_BUFFALO
773        case ROUTER_ASUS_WL500GD:
774        case ROUTER_ASUS_WL550GE:
775        case ROUTER_BELKIN_F5D7230_V3000:
776            linux_overrides = vlan;
777            break;
778        case ROUTER_WRT350N:
779            linux_overrides = wrt350vlan;
780            break;
781        case ROUTER_WRT310N:
782            linux_overrides = wrt350vlan;
783            break;
784        case ROUTER_WRT300NV11:
785            linux_overrides = wrt30011vlan;
786            break;
787        case ROUTER_WRT600N:
788            if( nvram_match( "switch_type", "BCM5395" ) )
789                linux_overrides = wrt60011vlan;
790            else
791                linux_overrides = wrt600vlan;
792            break;
793        case ROUTER_WRT610N:
794            linux_overrides = wrt60011vlan;
795            break;
796#endif
797        case ROUTER_BUFFALO_WZRG144NH:
798            linux_overrides = wzr144nhvlan;
799            break;
800#ifndef HAVE_BUFFALO
801        case ROUTER_MOTOROLA_WE800G:
802        case ROUTER_WAP54G_V1:
803        case ROUTER_SITECOM_WL105B:
804#endif
805        case ROUTER_BUFFALO_WLI2_TX1_G54:
806        case ROUTER_BUFFALO_WLAG54C:
807            linux_overrides = generic_2;
808            break;
809#ifndef HAVE_BUFFALO
810        case ROUTER_WAP54G_V2:
811        case ROUTER_VIEWSONIC_WAPBR_100:
812        case ROUTER_USR_5430:
813        case ROUTER_BELKIN_F5D7230_V2000:
814        case ROUTER_NETGEAR_WG602_V3:
815        case ROUTER_NETGEAR_WG602_V4:
816#endif
817        case ROUTER_BUFFALO_WLA2G54C:
818        case ROUTER_BUFFALO_WLI_TX4_G54HP:
819            linux_overrides = generic_3;
820            break;
821#ifndef HAVE_BUFFALO
822        case ROUTER_RT480W:
823        case ROUTER_RT210W:
824#endif
825        case ROUTER_BRCM4702_GENERIC:
826            ds = nvram_safe_get( "dhcp_start" );
827            if( ds != NULL && strlen( ds ) > 3 )
828            {
829                fprintf( stderr, "cleaning nvram variables\n" );
830                for( t = srouter_defaults; t->name; t++ )
831                {
832                    nvram_unset( t->name );
833                }
834                restore_defaults = 1;
835            }
836
837            /*
838             * ds = nvram_safe_get ("http_passwd"); if (ds == NULL || strlen
839             * (ds) == 0) //fix for empty default password { nvram_set
840             * ("http_passwd", "admin"); } ds = nvram_safe_get ("language");
841             * if (ds != NULL && strlen (ds) < 3) { nvram_set ("language",
842             * "english"); }
843             */
844            // fall through
845        default:
846            if( check_vlan_support(  ) )
847                linux_overrides = vlan;
848            else
849                linux_overrides = generic;
850            break;
851    }
852#endif
853    /*
854     * int i; for (i=0;i<4;i++)
855     * nvram_set(linux_overrides[i].name,linux_overrides[i].value);
856     */
857
858    /*
859     * Restore defaults
860     */
861#ifdef HAVE_FON
862    int reset = 0;
863    char *rev = nvram_safe_get( "fon_revision" );
864
865    if( rev == NULL || strlen( rev ) == 0 )
866        reset = 1;
867    if( strlen( rev ) > 0 )
868    {
869        int n = atoi( rev );
870
871        if( atoi( srouter_defaults[0].value ) != n )
872            reset = 1;
873    }
874    if( reset )
875    {
876        for( t = srouter_defaults; t->name; t++ )
877        {
878            for( u = linux_overrides; u && u->name; u++ )
879            {
880                if( !strcmp( t->name, u->name ) )
881                {
882                    nvram_set( u->name, u->value );
883                    break;
884                }
885            }
886            if( !u || !u->name )
887                nvram_set( t->name, t->value );
888        }
889    }
890#endif
891#ifdef HAVE_GATEWORX
892    if( restore_defaults )
893    {
894        eval( "erase", "nvram" );
895    }
896#elif HAVE_XSCALE
897    if( restore_defaults )
898        eval( "rm", "-f", "/etc/nvram/*" );     // delete nvram database
899#endif
900#ifdef HAVE_MAGICBOX
901    if( restore_defaults )
902    {
903        eval( "rm", "-f", "/tmp/nvram/*" );     // delete nvram database
904        eval( "rm", "-f", "/tmp/nvram/.lock" ); // delete nvram database
905        eval( "erase", "nvram" );
906    }
907#endif
908#ifdef HAVE_FONERA
909    if( restore_defaults )
910    {
911        eval( "erase", "nvram" );
912    }
913#endif
914#ifdef HAVE_LS2
915    if( restore_defaults )
916    {
917        eval( "erase", "nvram" );
918    }
919#endif
920#ifdef HAVE_LS5
921    if( restore_defaults )
922    {
923        eval( "erase", "nvram" );
924    }
925#endif
926#ifdef HAVE_WHRAG108
927    if( restore_defaults )
928    {
929        eval( "erase", "nvram" );
930    }
931#endif
932#ifdef HAVE_TW6600
933    if( restore_defaults )
934    {
935        eval( "erase", "nvram" );
936    }
937#endif
938    int nvcnt = 0;
939
940    // if (!nvram_match("default_init","1"))
941    {
942        for( t = srouter_defaults; t->name; t++ )
943        {
944            if( restore_defaults || !nvram_get( t->name ) )
945            {
946                for( u = linux_overrides; u && u->name; u++ )
947                {
948                    if( !strcmp( t->name, u->name ) )
949                    {
950                        nvcnt++;
951                        nvram_set( u->name, u->value );
952                        break;
953                    }
954                }
955                if( !u || !u->name )
956                {
957                    nvcnt++;
958                    nvram_set( t->name, t->value );
959                }
960            }
961        }
962    }
963    if( strlen( nvram_safe_get( "http_username" ) ) == 0 )
964    {
965        nvram_set( "http_username", zencrypt( "root" ) );
966        nvram_set( "http_passwd", zencrypt( "admin" ) );
967    }
968#ifndef HAVE_FON
969    if( restore_defaults )
970    {
971        switch ( brand )
972        {
973            case ROUTER_ASUS_WL520G:
974            case ROUTER_ASUS_WL500G_PRE_V2:
975            case ROUTER_BELKIN_F5D7230_V3000:
976                nvram_set( "vlan0ports", "0 1 2 3 5*" );
977                nvram_set( "vlan1ports", "4 5" );
978                break;
979            case ROUTER_LINKSYS_WTR54GS:
980                nvram_set( "vlan0ports", "0 5*" );
981                nvram_set( "vlan1ports", "1 5" );
982                break;
983            case ROUTER_ASUS_WL550GE:
984                nvram_set( "vlan0ports", "1 2 3 4 5*" );
985                nvram_set( "vlan1ports", "0 5" );
986                break;
987            case ROUTER_MOTOROLA:
988            case ROUTER_WRT54G_V8:
989                nvram_set( "vlan0ports", "3 2 1 0 5*" );
990                nvram_set( "vlan1ports", "4 5" );
991                break;
992            case ROUTER_LINKSYS_WRH54G:
993            case ROUTER_ASUS_WL500GD:
994            case ROUTER_BUFFALO_WBR2G54S:
995                nvram_set( "vlan0ports", "4 3 2 1 5*" );
996                nvram_set( "vlan1ports", "0 5" );
997                break;
998            case ROUTER_USR_5461:
999                nvram_set( "vlan0ports", "4 1 2 3 5*" );
1000                nvram_set( "vlan1ports", "0 5" );
1001                break;
1002            default:
1003                if( nvram_match( "boardnum", "WAP54GV3_8M_0614" ) )
1004                {
1005                    nvram_set( "vlan0ports", "3 2 1 0 5*" );
1006                    nvram_set( "vlan1ports", "4 5" );
1007                }
1008                break;
1009        }
1010#ifdef HAVE_SPUTNIK
1011        nvram_set( "lan_ipaddr", "192.168.180.1" );
1012#elif HAVE_BUFFALO
1013        nvram_set( "lan_ipaddr", "192.168.11.1" );
1014#else
1015        nvram_set( "lan_ipaddr", "192.168.1.1" );
1016#endif
1017    }
1018#else
1019    if( restore_defaults )
1020    {
1021        nvram_set( "lan_ipaddr", "192.168.10.1" );
1022    }
1023#endif
1024#ifdef HAVE_SKYTRON
1025    if( restore_defaults )
1026    {
1027        nvram_set( "lan_ipaddr", "192.168.0.1" );
1028    }
1029#endif
1030    if( brand == ROUTER_WRT600N )
1031    {
1032        if( nvram_match( "switch_type", "BCM5395" ) )   // fix for WRT600N
1033            // v1.1 (BCM5395 does
1034            // not suppport vid
1035            // 0, so gemtek
1036            // internally
1037            // configured vid 1
1038            // as lan)
1039        {
1040            nvram_set( "vlan0ports", " " );
1041            nvram_set( "vlan1ports", "1 2 3 4 8*" );
1042            nvram_set( "vlan2ports", "0 8*" );
1043            nvram_set( "vlan0hwname", " " );
1044            nvram_set( "vlan1hwname", "et0" );
1045            nvram_set( "landevs", "vlan1 wl0 wl1" );
1046            nvram_set( "lan_ifnames", "vlan1 eth0 eth1" );
1047        }
1048        else
1049        {
1050            if( !nvram_get( "vlan0ports" )
1051                || nvram_match( "vlan0ports", "" ) )
1052            {
1053                nvram_set( "vlan0ports", "1 2 3 4 8*" );
1054                nvram_set( "vlan2ports", "0 8*" );
1055            }
1056            if( !nvram_get( "vlan2ports" )
1057                || nvram_match( "vlan2ports", "" ) )
1058            {
1059                nvram_set( "vlan0ports", "1 2 3 4 8*" );
1060                nvram_set( "vlan2ports", "0 8*" );
1061            }
1062        }
1063    }
1064    else if( brand == ROUTER_WRT610N )
1065    {
1066        if( !nvram_get( "vlan1ports" ) || nvram_match( "vlan1ports", "" ) )
1067        {
1068            nvram_set( "vlan1ports", "1 2 3 4 8*" );
1069            nvram_set( "vlan2ports", "0 8" );
1070        }
1071        if( !nvram_get( "vlan2ports" ) || nvram_match( "vlan2ports", "" ) )
1072        {
1073            nvram_set( "vlan1ports", "1 2 3 4 8*" );
1074            nvram_set( "vlan2ports", "0 8" );
1075        }
1076    }
1077    else if( brand == ROUTER_WRT350N )
1078    {
1079
1080        if( !nvram_get( "vlan1ports" ) || nvram_match( "vlan1ports", "" ) )
1081        {
1082            nvram_set( "vlan1ports", "1 2 3 4 8*" );
1083            nvram_set( "vlan2ports", "0 8" );
1084        }
1085        if( !nvram_get( "vlan2ports" ) || nvram_match( "vlan2ports", "" ) )
1086        {
1087            nvram_set( "vlan1ports", "1 2 3 4 8*" );
1088            nvram_set( "vlan2ports", "0 8" );
1089        }
1090
1091    }
1092    else if( brand == ROUTER_WRT310N )
1093    {
1094
1095        if( !nvram_get( "vlan1ports" ) || nvram_match( "vlan1ports", "" ) )
1096        {
1097            nvram_set( "vlan1ports", "1 2 3 4 8*" );
1098            nvram_set( "vlan2ports", "0 8" );
1099        }
1100        if( !nvram_get( "vlan2ports" ) || nvram_match( "vlan2ports", "" ) )
1101        {
1102            nvram_set( "vlan1ports", "1 2 3 4 8*" );
1103            nvram_set( "vlan2ports", "0 8" );
1104        }
1105
1106    }
1107    else if( brand == ROUTER_WRT300NV11 )
1108    {
1109
1110        if( !nvram_get( "vlan0ports" ) || nvram_match( "vlan0ports", "" ) )
1111        {
1112            nvram_set( "vlan0ports", "1 2 3 4 5*" );
1113            nvram_set( "vlan1ports", "0 5" );
1114        }
1115        if( !nvram_get( "vlan1ports" ) || nvram_match( "vlan1ports", "" ) )
1116        {
1117            nvram_set( "vlan0ports", "1 2 3 4 5*" );
1118            nvram_set( "vlan1ports", "0 5" );
1119        }
1120
1121    }
1122    else if( brand == ROUTER_BUFFALO_WZRG144NH )
1123
1124    {
1125        if( !nvram_get( "vlan1ports" ) || nvram_match( "vlan1ports", "" ) )
1126        {
1127            nvram_set( "vlan1ports", "4 8" );
1128            nvram_set( "vlan2ports", "0 1 2 3 8*" );
1129        }
1130        if( !nvram_get( "vlan2ports" ) || nvram_match( "vlan2ports", "" ) )
1131        {
1132            nvram_set( "vlan1ports", "4 8" );
1133            nvram_set( "vlan2ports", "0 1 2 3 8*" );
1134        }
1135
1136    }
1137    else
1138    {
1139        if( !nvram_get( "vlan0hwname" ) || nvram_match( "vlan0hwname", "" ) )
1140            nvram_set( "vlan0hwname", "et0" );
1141        if( !nvram_get( "vlan1hwname" ) || nvram_match( "vlan1hwname", "" ) )
1142            nvram_set( "vlan1hwname", "et0" );
1143
1144        switch ( brand )
1145        {
1146            case ROUTER_MOTOROLA:
1147            case ROUTER_MOTOROLA_V1:
1148            case ROUTER_MOTOROLA_WE800G:
1149            case ROUTER_RT210W:
1150                if( et0mac != NULL )
1151                    nvram_set( "et0macaddr", et0mac );
1152                if( et1mac != NULL )
1153                    nvram_set( "et1macaddr", et1mac );
1154                break;
1155        }
1156
1157        if( !nvram_get( "vlan0ports" ) || nvram_match( "vlan0ports", "" ) )
1158        {
1159            switch ( brand )
1160            {
1161                case ROUTER_LINKSYS_WTR54GS:
1162                    nvram_set( "vlan0ports", "0 5*" );
1163                    break;
1164                case ROUTER_ASUS_WL500G_PRE:
1165                    nvram_set( "vlan0ports", "1 2 3 4 5*" );
1166                    break;
1167                case ROUTER_MOTOROLA:
1168                case ROUTER_WRT54G_V8:
1169                    nvram_set( "vlan0ports", "3 2 1 0 5*" );
1170                    break;
1171                case ROUTER_LINKSYS_WRT55AG:
1172                case ROUTER_RT480W:
1173                case ROUTER_DELL_TRUEMOBILE_2300_V2:
1174                case ROUTER_ASUS_WL520G:
1175                case ROUTER_ASUS_WL500G_PRE_V2:
1176                case ROUTER_WRT54G_V81:
1177                case ROUTER_BELKIN_F5D7230_V3000:
1178                    nvram_set( "vlan0ports", "0 1 2 3 5*" );
1179                    break;
1180                case ROUTER_LINKSYS_WRH54G:
1181                case ROUTER_ASUS_WL500GD:
1182                case ROUTER_BUFFALO_WBR2G54S:
1183                    nvram_set( "vlan0ports", "4 3 2 1 5*" );
1184                    break;
1185                case ROUTER_USR_5461:
1186                    nvram_set( "vlan0ports", "4 1 2 3 5*" );
1187                    break;
1188                default:
1189                    if( nvram_match( "bootnv_ver", "4" )
1190                        || nvram_match( "boardnum", "WAP54GV3_8M_0614" ) )
1191                        nvram_set( "vlan0ports", "3 2 1 0 5*" );
1192                    else
1193                        nvram_set( "vlan0ports", "1 2 3 4 5*" );
1194                    break;
1195            }
1196        }
1197
1198        if( !nvram_get( "vlan1ports" ) || nvram_match( "vlan1ports", "" ) )
1199        {
1200            switch ( brand )
1201            {
1202                case ROUTER_LINKSYS_WTR54GS:
1203                    nvram_set( "vlan1ports", "1 5" );
1204                    break;
1205                case ROUTER_ASUS_WL500G_PRE:
1206                case ROUTER_LINKSYS_WRH54G:
1207                case ROUTER_USR_5461:
1208                    nvram_set( "vlan1ports", "0 5" );
1209                    break;
1210                case ROUTER_MOTOROLA:
1211                case ROUTER_WRT54G_V8:
1212                case ROUTER_WRT54G_V81:
1213                case ROUTER_LINKSYS_WRT55AG:
1214                case ROUTER_RT480W:
1215                case ROUTER_DELL_TRUEMOBILE_2300_V2:
1216                case ROUTER_ASUS_WL520G:
1217                case ROUTER_ASUS_WL500G_PRE_V2:
1218                case ROUTER_BELKIN_F5D7230_V3000:
1219                    nvram_set( "vlan1ports", "4 5" );
1220                    break;
1221                default:
1222                    if( nvram_match( "bootnv_ver", "4" )
1223                        || nvram_match( "boardnum", "WAP54GV3_8M_0614" ) )
1224                        nvram_set( "vlan1ports", "4 5" );
1225                    else
1226                        nvram_set( "vlan1ports", "0 5" );
1227                    break;
1228            }
1229        }
1230
1231    }
1232    if( brand == ROUTER_WRT54G || brand == ROUTER_WRT54G1X
1233        || brand == ROUTER_LINKSYS_WRT55AG )
1234    {
1235        if( !nvram_get( "aa0" ) )
1236            nvram_set( "aa0", "3" );
1237        if( !nvram_get( "ag0" ) )
1238            nvram_set( "ag0", "255" );
1239        if( !nvram_get( "gpio2" ) )
1240            nvram_set( "gpio2", "adm_eecs" );
1241        if( !nvram_get( "gpio3" ) )
1242            nvram_set( "gpio3", "adm_eesk" );
1243        if( !nvram_get( "gpio5" ) )
1244            nvram_set( "gpio5", "adm_eedi" );
1245        if( !nvram_get( "gpio6" ) )
1246            nvram_set( "gpio6", "adm_rc" );
1247        if( !nvram_get( "boardrev" ) || nvram_match( "boardrev", "" ) )
1248            nvram_set( "boardrev", "0x10" );
1249        if( !nvram_get( "boardflags" ) || nvram_match( "boardflags", "" ) )
1250            nvram_set( "boardflags", "0x0388" );
1251        if( !nvram_get( "boardflags2" ) )
1252            nvram_set( "boardflags2", "0" );
1253    }
1254    /*
1255     * Always set OS defaults
1256     */
1257    nvram_set( "os_name", "linux" );
1258    nvram_set( "os_version", EPI_VERSION_STR );
1259
1260#ifdef HAVE_SPUTNIK_APD
1261    /*
1262     * Added for Sputnik Agent
1263     */
1264    nvram_unset( "sputnik_mjid" );
1265    nvram_unset( "sputnik_rereg" );
1266#endif
1267    nvram_unset( "probe_blacklist" );
1268
1269    if( nvram_get( "overclocking" ) == NULL )
1270        nvram_set( "overclocking", nvram_safe_get( "clkfreq" ) );
1271    cprintf( "start overclocking\n" );
1272    start_overclocking(  );
1273    cprintf( "done()" );
1274    if( nvram_get( "http_username" ) != NULL )
1275    {
1276        if( nvram_match( "http_username", "" ) )
1277        {
1278#ifdef HAVE_POWERNOC
1279            nvram_set( "http_username", "bJz7PcC1rCRJQ" );      // admin
1280#else
1281            nvram_set( "http_username", "bJ/GddyoJuiU2" );      // root
1282#endif
1283        }
1284    }
1285    if( atoi( nvram_safe_get( "nvram_ver" ) ) < 3 )
1286    {
1287        nvram_set( "nvram_ver", "3" );
1288        nvram_set( "block_multicast", "1" );
1289    }
1290    nvram_set( "gozila_action", "0" );
1291    nvram_set( "generate_key", "0" );
1292    nvram_set( "clone_wan_mac", "0" );
1293
1294    cprintf( "check CFE nv\n" );
1295    if( check_now_boot(  ) == PMON_BOOT )
1296        check_pmon_nv(  );
1297    else
1298        check_cfe_nv(  );
1299    cprintf( "restore defaults\n" );
1300
1301    /*
1302     * Commit values
1303     */
1304    if( restore_defaults )
1305    {
1306        int i;
1307
1308        unset_nvram(  );
1309        nvram_commit(  );
1310        cprintf( "done\n" );
1311        for( i = 0; i < MAX_NVPARSE; i++ )
1312        {
1313            del_wds_wsec( 0, i );
1314            del_wds_wsec( 1, i );
1315        }
1316    }
1317}
1318
1319/*
1320 * States
1321 */
1322enum
1323{
1324    RESTART,
1325    STOP,
1326    START,
1327    TIMER,
1328    USER,
1329    IDLE,
1330};
1331static int state = START;
1332static int signalled = -1;
1333
1334/*
1335 * Signal handling
1336 */
1337static void rc_signal( int sig )
1338{
1339    if( state == IDLE )
1340    {
1341        if( sig == SIGHUP )
1342        {
1343            printf( "signalling RESTART\n" );
1344            signalled = RESTART;
1345        }
1346        else if( sig == SIGUSR2 )
1347        {
1348            printf( "signalling START\n" );
1349            signalled = START;
1350        }
1351        else if( sig == SIGINT )
1352        {
1353            printf( "signalling STOP\n" );
1354            signalled = STOP;
1355        }
1356        else if( sig == SIGALRM )
1357        {
1358            printf( "signalling TIMER\n" );
1359            signalled = TIMER;
1360        }
1361        else if( sig == SIGUSR1 )
1362        {                       // Receive from WEB
1363            printf( "signalling USER1\n" );
1364            signalled = USER;
1365        }
1366
1367    }
1368}
1369
1370/*
1371 * Timer procedure
1372 */
1373int do_timer( void )
1374{
1375    // do_ntp();
1376    return 0;
1377}
1378
1379#define CONVERT_NV(old, new) \
1380        if(nvram_get(old)) \
1381                nvram_set(new, nvram_safe_get(old));
1382
1383int start_nvram( void )
1384{
1385    int i = 0;
1386
1387    /*
1388     * broadcom 3.11.48.7 change some nvram name
1389     */
1390#ifdef HAVE_MSSID
1391    CONVERT_NV( "d11g_channel", "wl_channel" );
1392#else
1393    CONVERT_NV( "d11g_channel", "wl0_channel" );
1394#endif
1395    CONVERT_NV( "d11g_rateset", "wl_rateset" );
1396    CONVERT_NV( "d11g_rts", "wl_rts" );
1397    CONVERT_NV( "d11g_bcn", "wl_bcn" );
1398    CONVERT_NV( "d11g_mode", "wl_gmode" );
1399    CONVERT_NV( "d11g_rate", "wl_rate" );
1400    CONVERT_NV( "d11g_frag", "wl_frag" );
1401    CONVERT_NV( "d11g_dtim", "wl_dtim" );
1402
1403    nvram_unset( "wl0_hwaddr" );        // When disbale wireless, we must get
1404    //
1405    // null wireless mac */
1406
1407    nvram_set( "wan_get_dns", "" );
1408    nvram_set( "filter_id", "1" );
1409    nvram_set( "wl_active_add_mac", "0" );
1410    nvram_set( "ddns_change", "" );
1411    nvram_unset( "action_service" );
1412    nvram_set( "wan_get_domain", "" );
1413
1414    // if(!nvram_get("wl_macmode1")){
1415    // if(nvram_match("wl_macmode","disabled"))
1416    // nvram_set("wl_macmode1","disabled");
1417    // else
1418    // nvram_set("wl_macmode1","other");
1419    // }
1420    if( nvram_match( "wl_gmode", "5" ) )        // Mixed mode had been
1421        // changed to 5
1422        nvram_set( "wl_gmode", "1" );
1423
1424    if( nvram_match( "wl_gmode", "4" ) )        // G-ONLY mode had been
1425        // changed to 2, after 1.40.1
1426        // for WiFi G certication
1427        nvram_set( "wl_gmode", "2" );
1428
1429    // nvram_set("wl_country","Worldwide"); // The country always Worldwide
1430
1431#ifndef AOL_SUPPORT
1432    nvram_set( "aol_block_traffic", "0" );
1433    nvram_set( "aol_block_traffic1", "0" );
1434    nvram_set( "aol_block_traffic2", "0" );
1435#endif
1436    nvram_set( "ping_ip", "" );
1437    nvram_set( "ping_times", "" );
1438    // nvram_set ("traceroute_ip", "");
1439
1440    nvram_set( "filter_port", "" );     // The name have been disbaled from
1441    // 1.41.3
1442
1443#ifdef HAVE_UPNP
1444    if( ( nvram_match( "restore_defaults", "1" ) )
1445        || ( nvram_match( "upnpcas", "1" ) ) )
1446    {
1447        nvram_set( "upnp_clear", "1" );
1448    }
1449    else
1450    {
1451        char s[32];
1452        char *nv;
1453
1454        for( i = 0; i < MAX_NVPARSE; ++i )
1455        {
1456            sprintf( s, "forward_port%d", i );
1457            if( ( nv = nvram_get( s ) ) != NULL )
1458            {
1459                if( strstr( nv, "msmsgs" ) )
1460                    nvram_unset( s );
1461            }
1462        }
1463    }
1464    nvram_set( "upnp_wan_proto", "" );
1465#endif
1466
1467    /*
1468     * The tkip and aes already are changed to wl_crypto from v3.63.3.0
1469     */
1470    if( nvram_match( "wl_wep", "tkip" ) )
1471    {
1472        nvram_set( "wl_crypto", "tkip" );
1473    }
1474    else if( nvram_match( "wl_wep", "aes" ) )
1475    {
1476        nvram_set( "wl_crypto", "aes" );
1477    }
1478    else if( nvram_match( "wl_wep", "tkip+aes" ) )
1479    {
1480        nvram_set( "wl_crypto", "tkip+aes" );
1481    }
1482
1483    if( nvram_match( "wl_wep", "restricted" ) )
1484        nvram_set( "wl_wep", "enabled" );       // the nas need this value,
1485    // the "restricted" is no
1486    // longer need. (20040624 by
1487    // honor)
1488
1489#ifdef HAVE_SET_BOOT
1490    if( !nvram_match( "boot_wait_web", "0" ) )
1491        nvram_set( "boot_wait_web", "1" );
1492#endif
1493#ifndef HAVE_BUFFALO
1494    if( check_hw_type(  ) == BCM5352E_CHIP )
1495    {
1496        nvram_set( "opo", "0" );        // OFDM power reducement in quarter
1497        // dbm (2 dbm in this case)
1498        nvram_set( "ag0", "0" );        // Antenna Gain definition in dbm
1499    }
1500#endif
1501
1502    if( nvram_match( "svqos_port1bw", "full" ) )
1503        nvram_set( "svqos_port1bw", "FULL" );
1504    if( nvram_match( "svqos_port2bw", "full" ) )
1505        nvram_set( "svqos_port2bw", "FULL" );
1506    if( nvram_match( "svqos_port3bw", "full" ) )
1507        nvram_set( "svqos_port3bw", "FULL" );
1508    if( nvram_match( "svqos_port4bw", "full" ) )
1509        nvram_set( "svqos_port4bw", "FULL" );
1510    // dirty fix for WBR2 units
1511
1512    // clean old filter_servicesX to free nvram
1513    nvram_unset( "filter_services0" );
1514    nvram_unset( "filter_services1" );
1515    nvram_unset( "filter_services2" );
1516    nvram_unset( "filter_services3" );
1517    nvram_unset( "filter_services4" );
1518    nvram_unset( "filter_services5" );
1519    nvram_unset( "filter_services6" );
1520    nvram_unset( "filter_services7" );
1521
1522    nvram_unset( "vdsl_state" );        // important (this value should never
1523    //
1524    // be commited, but if this will fix
1525    // the vlan7 issue)
1526    nvram_unset( "fromvdsl" );  // important (this value should never be
1527    // commited, but if this will fix the vlan7
1528    // issue)
1529
1530    nvram_unset( "do_reboot" ); //for GUI, see broadcom.c
1531
1532#ifdef DIST
1533    nvram_set( "dist_type", DIST );
1534#endif
1535
1536    {
1537
1538#ifdef DIST
1539#ifndef HAVE_TW6600
1540#ifdef HAVE_MICRO
1541        // if dist_type micro, check styles, and force to elegant if needed
1542
1543#ifdef HAVE_ROUTERSTYLE
1544        char *style = nvram_safe_get( "router_style" );
1545
1546        if( !strstr( "blue cyan elegant green orange red yellow", style ) )
1547#endif
1548        {
1549            nvram_set( "router_style", "elegant" );
1550        }
1551#endif
1552#endif
1553#endif
1554    }
1555
1556#ifdef HAVE_SSHD
1557    nvram_unset( "remote_mgt_telnet" );
1558    nvram_unset( "telnet_wanport" );
1559#else
1560    nvram_unset( "remote_mgt_ssh" );
1561    nvram_unset( "sshd_wanport" );
1562#endif
1563
1564#ifdef HAVE_WIVIZ
1565    if( !strlen( nvram_safe_get( "hopseq" ) )
1566        || !strlen( nvram_safe_get( "hopdwell" ) ) )
1567    {
1568#ifdef HAVE_MSSID
1569        char *channel = nvram_safe_get( "wl0_channel" );
1570#else
1571        char *channel = nvram_safe_get( "wl_channel" );
1572#endif
1573
1574        nvram_set( "hopdwell", "1000" );
1575        nvram_set( "hopseq", channel );
1576    }
1577#endif
1578    nvram_unset( "lasthour" );
1579#ifdef HAVE_AQOS
1580    //filter hostapd shaping rules
1581    char *qos_mac = nvram_safe_get( "svqos_macs" );
1582
1583    if( strlen( qos_mac ) > 0 )
1584    {
1585        char *newqos = malloc( strlen( qos_mac ) );
1586
1587        memset( newqos, 0, strlen( qos_mac ) );
1588        char level[32], level2[32], data[32], type[32];
1589
1590        do
1591        {
1592            if( sscanf
1593                ( qos_mac, "%31s %31s %31s %31s |", data, level, level2,
1594                  type ) < 4 )
1595                break;
1596            if( strcmp( type, "hostapd" ) )
1597            {
1598                sprintf( newqos, "%s %s %s %s %s |", newqos, data, level,
1599                         level2, type );
1600            }
1601        }
1602        while( ( qos_mac = strpbrk( ++qos_mac, "|" ) ) && qos_mac++ );
1603        nvram_set( "svqos_macs", newqos );
1604        free( newqos );
1605    }
1606#endif
1607    return 0;
1608}
1609
1610static void unset_nvram( void )
1611{
1612#ifndef MPPPOE_SUPPORT
1613    nvram_safe_unset( "ppp_username_1" );
1614    nvram_safe_unset( "ppp_passwd_1" );
1615    nvram_safe_unset( "ppp_idletime_1" );
1616    nvram_safe_unset( "ppp_demand_1" );
1617    nvram_safe_unset( "ppp_redialperiod_1" );
1618    nvram_safe_unset( "ppp_service_1" );
1619    nvram_safe_unset( "mpppoe_enable" );
1620    nvram_safe_unset( "mpppoe_dname" );
1621#endif
1622#ifndef HAVE_HTTPS
1623    nvram_safe_unset( "remote_mgt_https" );
1624#endif
1625#ifndef HSIAB_SUPPORT
1626    nvram_safe_unset( "hsiab_mode" );
1627    nvram_safe_unset( "hsiab_provider" );
1628    nvram_safe_unset( "hsiab_device_id" );
1629    nvram_safe_unset( "hsiab_device_password" );
1630    nvram_safe_unset( "hsiab_admin_url" );
1631    nvram_safe_unset( "hsiab_registered" );
1632    nvram_safe_unset( "hsiab_configured" );
1633    nvram_safe_unset( "hsiab_register_ops" );
1634    nvram_safe_unset( "hsiab_session_ops" );
1635    nvram_safe_unset( "hsiab_config_ops" );
1636    nvram_safe_unset( "hsiab_manual_reg_ops" );
1637    nvram_safe_unset( "hsiab_proxy_host" );
1638    nvram_safe_unset( "hsiab_proxy_port" );
1639    nvram_safe_unset( "hsiab_conf_time" );
1640    nvram_safe_unset( "hsiab_stats_time" );
1641    nvram_safe_unset( "hsiab_session_time" );
1642    nvram_safe_unset( "hsiab_sync" );
1643    nvram_safe_unset( "hsiab_config" );
1644#endif
1645
1646#ifndef HEARTBEAT_SUPPORT
1647    nvram_safe_unset( "hb_server_ip" );
1648    nvram_safe_unset( "hb_server_domain" );
1649#endif
1650
1651#ifndef PARENTAL_CONTROL_SUPPORT
1652    nvram_safe_unset( "artemis_enable" );
1653    nvram_safe_unset( "artemis_SVCGLOB" );
1654    nvram_safe_unset( "artemis_HB_DB" );
1655    nvram_safe_unset( "artemis_provisioned" );
1656#endif
1657
1658#ifndef WL_STA_SUPPORT
1659    // nvram_safe_unset("wl_ap_ssid");
1660    // nvram_safe_unset("wl_ap_ip");
1661#endif
1662
1663}
Note: See TracBrowser for help on using the repository browser.