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

Last change on this file since 11162 was 11162, checked in by BrainSlayer, 4 years ago

use vlan1 instead of eth0 for wan

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