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

Last change on this file since 17793 was 17793, checked in by chris, 20 months ago

qos rewrite pppd/radius needs testing

File size: 63.4 KB
Line 
1/*
2 * sysinit.c
3 *
4 * Copyright (C) 2007 Sebastian Gottschall <gottschall@dd-wrt.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 *
20 * $Id:
21 */
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <limits.h>
26#include <time.h>
27#include <unistd.h>
28#include <errno.h>
29#include <syslog.h>
30#include <signal.h>
31#include <string.h>
32#include <termios.h>
33
34#include <sys/klog.h>
35#include <sys/types.h>
36#include <sys/mount.h>
37#include <sys/reboot.h>
38#include <sys/stat.h>
39#include <sys/sysmacros.h>
40#include <sys/time.h>
41#include <sys/utsname.h>
42#include <sys/wait.h>
43#include <dirent.h>
44
45#include <epivers.h>
46#include <bcmnvram.h>
47#include <mtd.h>
48#include <shutils.h>
49#include <rc.h>
50#include <netconf.h>
51#include <nvparse.h>
52#include <bcmdevs.h>
53
54#include <wlutils.h>
55#include <utils.h>
56#include <cyutils.h>
57#include <code_pattern.h>
58#include <cy_conf.h>
59// #include <mkfiles.h>
60#include <typedefs.h>
61#include <bcmnvram.h>
62#include <bcmutils.h>
63#include <shutils.h>
64#include <wlutils.h>
65#include <cy_conf.h>
66#include <cymac.h>
67#include <glob.h>
68// #include <ledcontrol.h>
69
70#define WL_IOCTL(name, cmd, buf, len) (ret = wl_ioctl((name), (cmd), (buf), (len)))
71
72#define TXPWR_MAX 251
73#define TXPWR_DEFAULT 28
74
75void start_restore_defaults(void);
76static void rc_signal(int sig);
77extern void start_overclocking(void);
78extern int check_cfe_nv(void);
79extern int check_pmon_nv(void);
80static void unset_nvram(void);
81void start_nvram(void);
82
83extern struct nvram_tuple *srouter_defaults;
84extern void load_defaults(void);
85extern void free_defaults(void);
86
87extern int usb_add_ufd(void);
88
89extern int f_exists(const char *path);
90extern int getdevicecount(void);
91
92int endswith(char *str, char *cmp)
93{
94        int cmp_len, str_len, i;
95
96        cmp_len = strlen(cmp);
97        str_len = strlen(str);
98        if (cmp_len > str_len)
99                return (0);
100        for (i = 0; i < cmp_len; i++) {
101                if (str[(str_len - 1) - i] != cmp[(cmp_len - 1) - i])
102                        return (0);
103        }
104        return (1);
105}
106
107#ifdef HAVE_MACBIND
108#include "../../../opt/mac.h"
109#endif
110
111void runStartup(char *folder, char *extension)
112{
113        struct dirent **entry;
114        struct stat filestat;
115        DIR *directory;
116        int num, n = 0;
117        char fullname[128];
118
119        directory = opendir(folder);
120        if (directory == NULL) {
121                return;
122        }
123        closedir(directory);
124
125        num = scandir(folder, &entry, 0, alphasort);
126        if (num < 0)
127                return;
128        // list all files in this directory
129        while (n < num) {
130                if (!strcmp(extension, "K**") && strlen(entry[n]->d_name) > 3 && startswith(entry[n]->d_name, "K") && strspn(entry[n]->d_name, "K1234567890") == 3) {   // K* scripts
131                        sprintf(fullname, "%s/%s", folder, entry[n]->d_name);
132                        if (!stat(fullname, &filestat)
133                            && (filestat.st_mode & S_IXUSR))
134                                sysprintf("%s 2>&1 > /dev/null", fullname);
135                        free(entry[n]);
136                        n++;
137                        continue;
138                }
139                if (!strcmp(extension, "S**") && strlen(entry[n]->d_name) > 3 && startswith(entry[n]->d_name, "S") && strspn(entry[n]->d_name, "S1234567890") == 3) {   // S* scripts
140                        sprintf(fullname, "%s/%s", folder, entry[n]->d_name);
141                        if (!stat(fullname, &filestat)
142                            && (filestat.st_mode & S_IXUSR))
143                                sysprintf("%s 2>&1 > /dev/null", fullname);
144                        free(entry[n]);
145                        n++;
146                        continue;
147                }
148                if (endswith(entry[n]->d_name, extension)) {
149#ifdef HAVE_REGISTER
150                        if (!isregistered_real()) {
151                                if (endswith
152                                    (entry[n]->d_name, "wdswatchdog.startup")) {
153                                        free(entry[n]);
154                                        n++;
155                                        continue;
156                                }
157                                if (endswith
158                                    (entry[n]->d_name, "schedulerb.startup")) {
159                                        free(entry[n]);
160                                        n++;
161                                        continue;
162                                }
163                                if (endswith
164                                    (entry[n]->d_name,
165                                     "proxywatchdog.startup")) {
166                                        free(entry[n]);
167                                        n++;
168                                        continue;
169                                }
170                        }
171#endif
172                        sysprintf("%s/%s 2>&1 > /dev/null", folder,
173                                  entry[n]->d_name);
174                        // execute script
175                }
176                free(entry[n]);
177                n++;
178        }
179        free(entry);
180        return;
181}
182
183#if defined(HAVE_BUFFALO) || defined(HAVE_BUFFALO_BL_DEFAULTS)
184
185extern void *getUEnv(char *name);
186
187static void buffalo_defaults(int force)
188{
189        char *pincode = getUEnv("pincode");
190        if (pincode && nvram_get("pincode") == NULL) {
191                nvram_set("pincode", pincode);
192        }
193        if (nvram_get("ath0_akm") == NULL || force) {
194                char *region = getUEnv("region");
195                if (!region
196                    || (strcmp(region, "AP") && strcmp(region, "TW")
197                        && strcmp(region, "RU")
198                        && strcmp(region, "KR")
199                        && strcmp(region, "CH"))) {
200                        {
201                                char *mode_ex =
202                                    getUEnv
203                                    ("DEF-p_wireless_ath0_11bg-authmode_ex");
204                                if (!mode_ex)
205                                        mode_ex =
206                                            getUEnv
207                                            ("DEF-p_wireless_ath00_11bg-authmode_ex");
208                                if (mode_ex && !strcmp(mode_ex, "mixed-psk")) {
209                                        char *mode =
210                                            getUEnv
211                                            ("DEF-p_wireless_ath0_11bg-authmode");
212                                        if (!mode)
213                                                mode =
214                                                    getUEnv
215                                                    ("DEF-p_wireless_ath00_11bg-authmode");
216                                        if (!mode)
217                                                return;
218                                        if (!strcmp(mode, "psk")) {
219                                                nvram_set("ath0_akm",
220                                                          "psk psk2");
221                                                nvram_set("ath0_security_mode",
222                                                          "psk psk2");
223                                        }
224                                        if (!strcmp(mode, "psk2")) {
225                                                nvram_set("ath0_akm",
226                                                          "psk psk2");
227                                                nvram_set("ath0_security_mode",
228                                                          "psk psk2");
229                                        }
230                                } else {
231                                        char *mode =
232                                            getUEnv
233                                            ("DEF-p_wireless_ath0_11bg-authmode");
234                                        if (!mode)
235                                                mode =
236                                                    getUEnv
237                                                    ("DEF-p_wireless_ath00_11bg-authmode");
238                                        if (mode) {
239                                                nvram_set("ath0_akm", mode);
240                                                nvram_set("ath0_security_mode",
241                                                          mode);
242                                        } else
243                                                return;
244                                }
245
246                                char *crypto =
247                                    getUEnv("DEF-p_wireless_ath0_11bg-crypto");
248                                if (!crypto)
249                                        crypto =
250                                            getUEnv
251                                            ("DEF-p_wireless_ath00_11bg-crypto");
252                                if (crypto)
253                                        nvram_set("ath0_crypto", crypto);
254                                char *wpapsk =
255                                    getUEnv("DEF-p_wireless_ath0_11bg-wpapsk");
256                                if (!wpapsk)
257                                        wpapsk =
258                                            getUEnv
259                                            ("DEF-p_wireless_ath00_11bg-wpapsk");
260                                if (wpapsk)
261                                        nvram_set("ath0_wpa_psk", wpapsk);
262                        }
263#ifdef HAVE_WZRHPAG300NH
264                        {
265                                char *mode_ex =
266                                    getUEnv
267                                    ("DEF-p_wireless_ath1_11a-authmode_ex");
268                                if (!mode_ex)
269                                        mode_ex =
270                                            getUEnv
271                                            ("DEF-p_wireless_ath10_11a-authmode_ex");
272                                if (mode_ex && !strcmp(mode_ex, "mixed-psk")) {
273                                        char *mode =
274                                            getUEnv
275                                            ("DEF-p_wireless_ath1_11a-authmode");
276                                        if (!mode)
277                                                mode =
278                                                    getUEnv
279                                                    ("DEF-p_wireless_ath10_11a-authmode");
280                                        if (!mode)
281                                                return;
282                                        if (!strcmp(mode, "psk")) {
283                                                nvram_set("ath1_akm",
284                                                          "psk psk2");
285                                                nvram_set("ath1_security_mode",
286                                                          "psk psk2");
287                                        }
288                                        if (!strcmp(mode, "psk2")) {
289                                                nvram_set("ath1_akm",
290                                                          "psk psk2");
291                                                nvram_set("ath1_security_mode",
292                                                          "psk psk2");
293                                        }
294                                } else {
295                                        char *mode =
296                                            getUEnv
297                                            ("DEF-p_wireless_ath1_11a-authmode");
298                                        if (!mode)
299                                                mode =
300                                                    getUEnv
301                                                    ("DEF-p_wireless_ath10_11a-authmode");
302                                        if (mode) {
303                                                nvram_set("ath1_akm", mode);
304                                                nvram_set("ath1_security_mode",
305                                                          mode);
306                                        } else
307                                                return;
308                                }
309
310                                char *crypto =
311                                    getUEnv("DEF-p_wireless_ath1_11a-crypto");
312                                if (!crypto)
313                                        crypto =
314                                            getUEnv
315                                            ("DEF-p_wireless_ath10_11a-crypto");
316                                if (crypto)
317                                        nvram_set("ath1_crypto", crypto);
318                                char *wpapsk =
319                                    getUEnv("DEF-p_wireless_ath1_11a-wpapsk");
320                                if (!wpapsk)
321                                        wpapsk =
322                                            getUEnv
323                                            ("DEF-p_wireless_ath10_11a-wpapsk");
324                                if (wpapsk)
325                                        nvram_set("ath1_wpa_psk", wpapsk);
326                        }
327                }
328                struct ifreq ifr;
329                int s;
330
331                if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW))) {
332                        char eabuf[32];
333
334                        strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
335                        ioctl(s, SIOCGIFHWADDR, &ifr);
336                        close(s);
337                        unsigned char *edata =
338                            (unsigned char *)ifr.ifr_hwaddr.sa_data;
339                        sprintf(eabuf, "BUFFALO-%02X%02X%02X_G",
340                                edata[3] & 0xff, edata[4] & 0xff,
341                                edata[5] & 0xff);
342                        nvram_set("ath0_ssid", eabuf);
343                        sprintf(eabuf, "BUFFALO-%02X%02X%02X_A",
344                                edata[3] & 0xff, edata[4] & 0xff,
345                                edata[5] & 0xff);
346                        nvram_set("ath1_ssid", eabuf);
347                }
348#else
349                }
350                struct ifreq ifr;
351                int s;
352
353                if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW))) {
354                        char eabuf[32];
355
356                        strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
357                        ioctl(s, SIOCGIFHWADDR, &ifr);
358                        close(s);
359                        unsigned char *edata =
360                            (unsigned char *)ifr.ifr_hwaddr.sa_data;
361                        sprintf(eabuf, "%02X%02X%02X%02X%02X%02X",
362                                edata[0] & 0xff, edata[1] & 0xff,
363                                edata[2] & 0xff, edata[3] & 0xff,
364                                edata[4] & 0xff, edata[5] & 0xff);
365                        nvram_set("ath0_ssid", eabuf);
366                }
367#endif
368
369                region = getUEnv("region");
370                if (region == NULL) {
371                        region = "US";
372                }
373                if (!strcmp(region, "US")) {
374                        nvram_set("ath0_regdomain", "UNITED_STATES");
375                } else if (!strcmp(region, "EU")) {
376                        nvram_set("ath0_regdomain", "GERMANY");
377                } else if (!strcmp(region, "JP")) {
378                        nvram_set("ath0_regdomain", "JAPAN");
379                } else if (!strcmp(region, "AP")) {
380                        nvram_set("ath0_regdomain", "SINGAPORE");
381                } else if (!strcmp(region, "RU")) {
382                        nvram_set("ath0_regdomain", "RUSSIA");
383                } else if (!strcmp(region, "TW")) {
384                        nvram_set("ath0_regdomain", "TAIWAN");
385                } else if (!strcmp(region, "CH")) {
386                        nvram_set("ath0_regdomain", "CHINA");
387                } else if (!strcmp(region, "KR")) {
388                        nvram_set("ath0_regdomain", "KOREA_REPUBLIC");
389                }
390#ifdef HAVE_WZRHPAG300NH
391                if (!strcmp(region, "US")) {
392                        nvram_set("ath1_regdomain", "UNITED_STATES");
393                } else if (!strcmp(region, "EU")) {
394                        nvram_set("ath1_regdomain", "GERMANY");
395                } else if (!strcmp(region, "JP")) {
396                        nvram_set("ath1_regdomain", "JAPAN");
397                } else if (!strcmp(region, "RU")) {
398                        nvram_set("ath1_regdomain", "RUSSIA");
399                } else if (!strcmp(region, "AP")) {
400                        nvram_set("ath1_regdomain", "SINGAPORE");
401                } else if (!strcmp(region, "TW")) {
402                        nvram_set("ath1_regdomain", "TAIWAN");
403                } else if (!strcmp(region, "CH")) {
404                        nvram_set("ath1_regdomain", "CHINA");
405                } else if (!strcmp(region, "KR")) {
406                        nvram_set("ath1_regdomain", "KOREA_REPUBLIC");
407                }
408#endif
409                if (!strcmp(region, "AP") || !strcmp(region, "CH")
410                        || !strcmp(region, "KR")
411                        || !strcmp(region, "TW")
412                        || !strcmp(region, "RU"))
413                        nvram_set("wps_status", "0");
414                else
415                        nvram_set("wps_status", "1");
416#ifdef HAVE_SPOTPASS
417                system("startservice spotpass_defaults");
418#endif
419        }
420}
421#endif
422/*
423 * SeG dd-wrt addition for module startup scripts
424 */
425void start_modules(void)
426{
427        runStartup("/etc/config", ".startup");
428
429#ifdef HAVE_RB500
430        runStartup("/usr/local/etc/config", ".startup");        // if available
431#elif HAVE_X86
432        runStartup("/usr/local/etc/config", ".startup");        // if available
433#else
434        runStartup("/jffs/etc/config", ".startup");     // if available
435        runStartup("/mmc/etc/config", ".startup");      // if available
436#endif
437        return;
438}
439
440void start_wanup(void)
441{
442        runStartup("/etc/config", ".wanup");
443#ifdef HAVE_RB500
444        runStartup("/usr/local/etc/config", ".wanup");  // if available
445#elif HAVE_X86
446        runStartup("/usr/local/etc/config", ".wanup");  // if available
447#else
448        runStartup("/jffs/etc/config", ".wanup");       // if available
449        runStartup("/mmc/etc/config", ".wanup");        // if available
450        runStartup("/tmp/etc/config", ".wanup");        // if available
451#endif
452        return;
453}
454
455void start_run_rc_startup(void)
456{
457        DIR *directory;
458        int count = 36;         // 36 * 5 s = 180s
459
460        create_rc_file(RC_STARTUP);
461
462        if (f_exists("/tmp/.rc_startup"))
463                system("/tmp/.rc_startup");
464
465        while (count > 0) {
466                directory = opendir("/opt/etc/init.d");
467                if (directory == NULL) {
468                        sleep(5);
469                        count--;
470                } else {
471                        closedir(directory);
472                        runStartup("/opt/etc/init.d", "S**");   // if available; run S** startup scripts
473                        return;
474                }
475        }
476}
477
478void start_run_rc_shutdown(void)
479{
480        runStartup("/opt/etc/init.d", "K**");   // if available; run K** shutdown scripts
481        create_rc_file(RC_SHUTDOWN);
482        if (f_exists("/tmp/.rc_shutdown"))
483                system("/tmp/.rc_shutdown");
484        return;
485}
486
487int create_rc_file(char *name)
488{
489        FILE *fp;
490        char *p = nvram_safe_get(name);
491        char tmp_file[100] = { 0 };
492
493        if ((void *)0 == name || 0 == p[0])
494                return -1;
495
496        snprintf(tmp_file, 100, "/tmp/.%s", name);
497        unlink(tmp_file);
498
499        fp = fopen(tmp_file, "w");
500        if (fp) {
501                if (strlen(p) <= 2 || memcmp("#!", p, 2))
502                        fprintf(fp, "#!/bin/sh\n");
503                // filter Windows <cr>ud
504                while (*p) {
505                        if (*p != 0x0d)
506                                fprintf(fp, "%c", *p);
507                        p++;
508                }
509                fclose(fp);
510        }
511        chmod(tmp_file, 0700);
512
513        return 0;
514}
515
516static void ses_cleanup(void)
517{
518        /*
519         * well known event to cleanly initialize state machine
520         */
521        nvram_set("ses_event", "2");
522
523        /*
524         * Delete lethal dynamically generated variables
525         */
526        nvram_unset("ses_bridge_disable");
527}
528
529static void ses_restore_defaults(void)
530{
531        char tmp[100], prefix[] = "wlXXXXXXXXXX_ses_";
532        int i;
533
534        /*
535         * Delete dynamically generated variables
536         */
537        for (i = 0; i < MAX_NVPARSE; i++) {
538                sprintf(prefix, "wl%d_ses_", i);
539                nvram_unset(strcat_r(prefix, "ssid", tmp));
540                nvram_unset(strcat_r(prefix, "closed", tmp));
541                nvram_unset(strcat_r(prefix, "wpa_psk", tmp));
542                nvram_unset(strcat_r(prefix, "auth", tmp));
543                nvram_unset(strcat_r(prefix, "wep", tmp));
544                nvram_unset(strcat_r(prefix, "auth_mode", tmp));
545                nvram_unset(strcat_r(prefix, "crypto", tmp));
546                nvram_unset(strcat_r(prefix, "akm", tmp));
547        }
548}
549
550void start_restore_defaults(void)
551{
552
553#ifdef HAVE_RB500
554        struct nvram_tuple generic[] = {
555                {"lan_ifname", "br0", 0},
556                {"lan_ifnames",
557                 "eth1 eth2 eth3 eth4 eth5 eth6 eth7 eth8 ath0 ath1 ath2 ath3 ath4 ath5",
558                 0},
559                {"wan_ifname", "eth0", 0},
560                {"wan_ifnames", "eth0", 0},
561                {0, 0, 0}
562        };
563#elif HAVE_GEMTEK
564        struct nvram_tuple generic[] = {
565                {"lan_ifname", "br0", 0},
566                {"lan_ifnames", "eth1 ath0", 0},
567                {"wan_ifname", "eth0", 0},
568                {"wan_ifnames", "eth0", 0},
569                {0, 0, 0}
570        };
571#elif HAVE_EAP9550
572        struct nvram_tuple generic[] = {
573                {"lan_ifname", "br0", 0},
574                {"lan_ifnames", "eth2 ra0",
575                 0},
576                {"wan_ifname2", "eth2", 0},
577                {"wan_ifname", "eth2", 0},
578                {"wan_default", "eth2", 0},
579                {"wan_ifnames", "eth2", 0},
580                {0, 0, 0}
581        };
582#elif HAVE_RT2880
583        struct nvram_tuple generic[] = {
584                {"lan_ifname", "br0", 0},
585                {"lan_ifnames", "vlan1 vlan2 ra0",
586                 0},
587                {"wan_ifname2", "vlan2", 0},
588                {"wan_ifname", "vlan2", 0},
589                {"wan_default", "vlan2", 0},
590                {"wan_ifnames", "vlan2", 0},
591                {0, 0, 0}
592        };
593#elif HAVE_GATEWORX
594#if defined(HAVE_XIOCOM) || defined(HAVE_MI424WR)
595        struct nvram_tuple generic[] = {
596                {"lan_ifname", "br0", 0},
597                {"lan_ifnames", "ixp1 ath0 ath1 ath2 ath3",
598                 0},
599                {"wan_ifname2", "ixp0", 0},
600                {"wan_ifname", "ixp0", 0},
601                {"wan_default", "ixp0", 0},
602                {"wan_ifnames", "ixp0", 0},
603                {0, 0, 0}
604        };
605#else
606        struct nvram_tuple generic[] = {
607                {"lan_ifname", "br0", 0},
608                {"lan_ifnames", "ixp0 ath0 ath1 ath2 ath3",
609                 0},
610                {"wan_ifname2", "ixp1", 0},
611                {"wan_ifname", "ixp1", 0},
612                {"wan_default", "ixp1", 0},
613                {"wan_ifnames", "ixp1", 0},
614                {0, 0, 0}
615        };
616#endif
617#elif HAVE_X86
618        struct nvram_tuple generic[] = {
619                {"lan_ifname", "br0", 0},
620#ifdef HAVE_NOWIFI
621                {"lan_ifnames",
622                 "eth1 eth2 eth3 eth4 eth5 eth6 eth7 eth8 eth9 eth10",
623                 0},
624#else
625#ifdef HAVE_GW700
626                {"lan_ifnames",
627                 "eth0 eth2 eth3 eth4 eth5 eth6 eth7 eth8 eth9 eth10 ath0 ath1 ath2 ath3 ath5 ath6 ath7 ath8",
628                 0},
629#else
630                {"lan_ifnames",
631                 "eth1 eth2 eth3 eth4 eth5 eth6 eth7 eth8 eth9 eth10 ath0 ath1 ath2 ath3 ath5 ath6 ath7 ath8",
632                 0},
633#endif
634#endif
635#ifdef HAVE_GW700
636                {"wan_ifname", "eth1", 0},
637                {"wan_ifname2", "eth1", 0},
638                {"wan_ifnames", "eth1", 0},
639#else
640                {"wan_ifname", "eth0", 0},
641                {"wan_ifname2", "eth0", 0},
642                {"wan_ifnames", "eth0", 0},
643#endif
644                {0, 0, 0}
645        };
646#elif HAVE_XSCALE
647        struct nvram_tuple generic[] = {
648                {"lan_ifname", "br0", 0},
649                {"lan_ifnames",
650                 "ixp0.1 ixp0.2 ath0 ath1",
651                 0},
652                {"wan_ifname", "ixp1", 0},
653                {"wan_ifname2", "ixp1", 0},
654                {"wan_ifnames", "ixp1", 0},
655                {"wan_default", "ixp1", 0},
656                {0, 0, 0}
657        };
658#elif HAVE_LAGUNA
659        struct nvram_tuple generic[] = {
660                {"lan_ifname", "br0", 0},
661                {"lan_ifnames",
662                 "eth0 eth1 ath0 ath1 ath2 ath3",
663                 0},
664                {"wan_ifname", "eth0", 0},
665                {"wan_ifname2", "eth0", 0},
666                {"wan_ifnames", "eth0", 0},
667                {"wan_default", "eth0", 0},
668                {0, 0, 0}
669        };
670#elif HAVE_MAGICBOX
671        struct nvram_tuple generic[] = {
672                {"lan_ifname", "br0", 0},
673                {"lan_ifnames", "eth1 ath0",
674                 0},
675                {"wan_ifname", "eth0", 0},
676                {"wan_ifname2", "eth0", 0},
677                {"wan_ifnames", "eth0", 0},
678                {"wan_default", "eth0", 0},
679                {0, 0, 0}
680        };
681#elif HAVE_RB600
682        struct nvram_tuple generic[] = {
683                {"lan_ifname", "br0", 0},
684                {"lan_ifnames",
685                 "eth0 eth1 eth2 eth3 eth4 eth5 eth6 eth7 eth8 ath0 ath1 ath2 ath3 ath4 ath5 ath6 ath7",
686                 0},
687                {"wan_ifname", "eth0", 0},
688                {"wan_ifname2", "eth0", 0},
689                {"wan_ifnames", "eth0", 0},
690                {"wan_default", "eth0", 0},
691                {0, 0, 0}
692        };
693#elif HAVE_FONERA
694        struct nvram_tuple generic[] = {
695                {"lan_ifname", "br0", 0},
696                {"lan_ifnames", "vlan0 ath0", 0},
697                {"wan_ifname", "", 0},
698                {"wan_ifname2", "", 0},
699                {"wan_default", "", 0},
700                {"wan_ifnames", "eth0 vlan1", 0},
701                {0, 0, 0}
702        };
703#elif HAVE_BWRG1000
704        struct nvram_tuple generic[] = {
705                {"lan_ifname", "br0", 0},
706                {"lan_ifnames", "vlan0 vlan2 ath0", 0},
707                {"wan_ifname", "vlan2", 0},
708                {"wan_ifname2", "vlan2", 0},
709                {"wan_ifnames", "vlan2", 0},
710                {"wan_default", "vlan2", 0},
711                {0, 0, 0}
712        };
713#elif HAVE_SOLO51
714        struct nvram_tuple generic[] = {
715                {"lan_ifname", "br0", 0},
716                {"lan_ifnames", "vlan0 vlan2 ath0", 0},
717                {"wan_ifname", "vlan0", 0},
718                {"wan_ifname2", "vlan0", 0},
719                {"wan_ifnames", "vlan0", 0},
720                {"wan_default", "vlan0", 0},
721                {0, 0, 0}
722        };
723#elif HAVE_BS2
724        struct nvram_tuple generic[] = {
725                {"lan_ifname", "br0", 0},
726                {"lan_ifnames", "eth0 ath0 ath1", 0},
727                {"wan_ifname", "eth0", 0},
728                {"wan_ifname2", "eth0", 0},
729                {"wan_ifnames", "eth0", 0},
730                {"wan_default", "eth0", 0},
731                {0, 0, 0}
732        };
733#elif HAVE_NS2
734        struct nvram_tuple generic[] = {
735                {"lan_ifname", "br0", 0},
736                {"lan_ifnames", "eth0 ath0 ath1", 0},
737                {"wan_ifname", "eth0", 0},
738                {"wan_ifname2", "eth0", 0},
739                {"wan_ifnames", "eth0", 0},
740                {"wan_default", "eth0", 0},
741                {0, 0, 0}
742        };
743#elif HAVE_LC2
744        struct nvram_tuple generic[] = {
745                {"lan_ifname", "br0", 0},
746                {"lan_ifnames", "eth0 ath0 ath1", 0},
747                {"wan_ifname", "eth0", 0},
748                {"wan_ifname2", "eth0", 0},
749                {"wan_ifnames", "eth0", 0},
750                {"wan_default", "eth0", 0},
751                {0, 0, 0}
752        };
753#elif HAVE_PICO2
754        struct nvram_tuple generic[] = {
755                {"lan_ifname", "br0", 0},
756                {"lan_ifnames", "eth0 ath0 ath1", 0},
757                {"wan_ifname", "eth0", 0},
758                {"wan_ifname2", "eth0", 0},
759                {"wan_ifnames", "eth0", 0},
760                {"wan_default", "eth0", 0},
761                {0, 0, 0}
762        };
763#elif HAVE_PICO2HP
764        struct nvram_tuple generic[] = {
765                {"lan_ifname", "br0", 0},
766                {"lan_ifnames", "eth0 ath0 ath1", 0},
767                {"wan_ifname", "eth0", 0},
768                {"wan_ifname2", "eth0", 0},
769                {"wan_ifnames", "eth0", 0},
770                {"wan_default", "eth0", 0},
771                {0, 0, 0}
772        };
773#elif HAVE_MS2
774        struct nvram_tuple generic[] = {
775                {"lan_ifname", "br0", 0},
776                {"lan_ifnames", "eth0 ath0 ath1", 0},
777                {"wan_ifname", "eth0", 0},
778                {"wan_ifname2", "eth0", 0},
779                {"wan_ifnames", "eth0", 0},
780                {"wan_default", "eth0", 0},
781                {0, 0, 0}
782        };
783#elif HAVE_BS2HP
784        struct nvram_tuple generic[] = {
785                {"lan_ifname", "br0", 0},
786                {"lan_ifnames", "eth0 ath0 ath1", 0},
787                {"wan_ifname", "eth0", 0},
788                {"wan_ifname2", "eth0", 0},
789                {"wan_ifnames", "eth0", 0},
790                {"wan_default", "eth0", 0},
791                {0, 0, 0}
792        };
793#elif HAVE_LS2
794        struct nvram_tuple generic[] = {
795                {"lan_ifname", "br0", 0},
796                {"lan_ifnames", "vlan0 vlan2 ath0", 0},
797                {"wan_ifname", "vlan0", 0},
798                {"wan_ifname2", "vlan0", 0},
799                {"wan_ifnames", "vlan0", 0},
800                {"wan_default", "vlan0", 0},
801                {0, 0, 0}
802        };
803#elif HAVE_RS
804        struct nvram_tuple generic[] = {
805                {"lan_ifname", "br0", 0},
806                {"lan_ifnames", "eth0 eth1 ath0 ath1 ath2", 0},
807                {"wan_ifname", "eth0", 0},
808                {"wan_ifname2", "eth0", 0},
809                {"wan_ifnames", "eth0", 0},
810                {"wan_default", "eth0", 0},
811                {0, 0, 0}
812        };
813#elif HAVE_WR941
814        struct nvram_tuple generic[] = {
815                {"lan_ifname", "br0", 0},
816                {"lan_ifnames", "vlan0 vlan1 ath0", 0},
817                {"wan_ifname", "vlan1", 0},
818                {"wan_ifname2", "vlan1", 0},
819                {"wan_ifnames", "vlan1", 0},
820                {"wan_default", "vlan1", 0},
821                {0, 0, 0}
822        };
823#elif HAVE_WA901v1
824        struct nvram_tuple generic[] = {
825                {"lan_ifname", "br0", 0},
826                {"lan_ifnames", "eth1 ath0", 0},
827                {"wan_ifname", "eth1", 0},
828                {"wan_ifname2", "eth1", 0},
829                {"wan_ifnames", "eth1", 0},
830                {"wan_default", "eth1", 0},
831                {0, 0, 0}
832        };
833#elif HAVE_WR741
834        struct nvram_tuple generic[] = {
835                {"lan_ifname", "br0", 0},
836                {"lan_ifnames", "eth0 eth1 ath0", 0},
837                {"wan_ifname", "eth0", 0},
838                {"wan_ifname2", "eth0", 0},
839                {"wan_ifnames", "eth0", 0},
840                {"wan_default", "eth0", 0},
841                {0, 0, 0}
842        };
843#elif HAVE_WR1043
844        struct nvram_tuple generic[] = {
845                {"lan_ifname", "br0", 0},
846                {"lan_ifnames", "vlan1 vlan2 ath0", 0},
847                {"wan_ifname", "vlan2", 0},
848                {"wan_ifname2", "vlan2", 0},
849                {"wan_ifnames", "vlan2", 0},
850                {"wan_default", "vlan2", 0},
851                {0, 0, 0}
852        };
853#elif HAVE_AP83
854        struct nvram_tuple generic[] = {
855                {"lan_ifname", "br0", 0},
856                {"lan_ifnames", "eth0 eth1 ath0", 0},
857                {"wan_ifname", "eth1", 0},
858                {"wan_ifname2", "eth1", 0},
859                {"wan_ifnames", "eth1", 0},
860                {"wan_default", "eth1", 0},
861                {0, 0, 0}
862        };
863#elif HAVE_AP94
864        struct nvram_tuple generic[] = {
865                {"lan_ifname", "br0", 0},
866                {"lan_ifnames", "eth0 eth1 ath0", 0},
867                {"wan_ifname", "eth1", 0},
868                {"wan_ifname2", "eth1", 0},
869                {"wan_ifnames", "eth1", 0},
870                {"wan_default", "eth1", 0},
871                {0, 0, 0}
872        };
873#elif HAVE_UBNTM
874        struct nvram_tuple generic[] = {
875                {"lan_ifname", "br0", 0},
876                {"lan_ifnames", "eth0 eth1 ath0", 0},
877                {"wan_ifname", "", 0},
878                {"wan_ifname2", "", 0},
879                {"wan_ifnames", "", 0},
880                {"wan_default", "", 0},
881                {0, 0, 0}
882        };
883#elif HAVE_WHRHPGN
884        struct nvram_tuple generic[] = {
885                {"lan_ifname", "br0", 0},
886                {"lan_ifnames", "eth0 eth1 ath0", 0},
887                {"wan_ifname", "", 0},
888                {"wan_ifname2", "", 0},
889                {"wan_ifnames", "", 0},
890                {"wan_default", "", 0},
891                {0, 0, 0}
892        };
893#elif HAVE_DIR615E
894        struct nvram_tuple generic[] = {
895                {"lan_ifname", "br0", 0},
896                {"lan_ifnames", "eth0 eth1 ath0", 0},
897                {"wan_ifname", "", 0},
898                {"wan_ifname2", "", 0},
899                {"wan_ifnames", "", 0},
900                {"wan_default", "", 0},
901                {0, 0, 0}
902        };
903#elif HAVE_JA76PF
904        struct nvram_tuple generic[] = {
905                {"lan_ifname", "br0", 0},
906                {"lan_ifnames", "eth0 eth1 ath0", 0},
907                {"wan_ifname", "eth1", 0},
908                {"wan_ifname2", "eth1", 0},
909                {"wan_ifnames", "eth1", 0},
910                {"wan_default", "eth1", 0},
911                {0, 0, 0}
912        };
913#elif HAVE_ALFAAP94
914        struct nvram_tuple generic[] = {
915                {"lan_ifname", "br0", 0},
916                {"lan_ifnames", "eth0 eth1 ath0 ath1 ath2", 0},
917                {"wan_ifname", "eth1", 0},
918                {"wan_ifname2", "eth1", 0},
919                {"wan_ifnames", "eth1", 0},
920                {"wan_default", "eth1", 0},
921                {0, 0, 0}
922        };
923#elif HAVE_WZRG450
924        struct nvram_tuple generic[] = {
925                {"lan_ifname", "br0", 0},
926                {"lan_ifnames", "vlan1 ath0", 0},
927                {"wan_ifname", "vlan2", 0},
928                {"wan_ifname2", "vlan2", 0},
929                {"wan_ifnames", "vlan2", 0},
930                {"wan_default", "vlan2", 0},
931                {0, 0, 0}
932        };
933#elif HAVE_WZRHPAG300NH
934        struct nvram_tuple generic[] = {
935                {"lan_ifname", "br0", 0},
936                {"lan_ifnames", "eth0 eth1 ath0 ath1", 0},
937                {"wan_ifname", "eth1", 0},
938                {"wan_ifname2", "eth1", 0},
939                {"wan_ifnames", "eth1", 0},
940                {"wan_default", "eth1", 0},
941                {0, 0, 0}
942        };
943#elif HAVE_JWAP003
944        struct nvram_tuple generic[] = {
945                {"lan_ifname", "br0", 0},
946                {"lan_ifnames", "eth0 eth1 ath0", 0},
947                {"wan_ifname", "eth1", 0},
948                {"wan_ifname2", "eth1", 0},
949                {"wan_ifnames", "eth1", 0},
950                {"wan_default", "eth1", 0},
951                {0, 0, 0}
952        };
953#elif HAVE_LSX
954        struct nvram_tuple generic[] = {
955                {"lan_ifname", "br0", 0},
956                {"lan_ifnames", "eth0 ath0", 0},
957                {"wan_ifname", "", 0},
958                {"wan_ifname2", "", 0},
959                {"wan_ifnames", "", 0},
960                {"wan_default", "", 0},
961                {0, 0, 0}
962        };
963#elif HAVE_DANUBE
964        struct nvram_tuple generic[] = {
965                {"lan_ifname", "br0", 0},
966                {"lan_ifnames", "eth0 ath0", 0},
967                {"wan_ifname", "nas0", 0},
968                {"wan_ifname2", "nas0", 0},
969                {"wan_ifnames", "nas0", 0},
970                {"wan_default", "nas0", 0},
971                {0, 0, 0}
972        };
973#elif HAVE_WBD222
974        struct nvram_tuple generic[] = {
975                {"lan_ifname", "br0", 0},
976                {"lan_ifnames", "eth0 eth1 eth2 ath0 ath1 ath2", 0},
977                {"wan_ifname", "eth0", 0},
978                {"wan_ifname2", "eth0", 0},
979                {"wan_ifnames", "eth0", 0},
980                {"wan_default", "eth0", 0},
981                {0, 0, 0}
982        };
983#elif HAVE_STORM
984        struct nvram_tuple generic[] = {
985                {"lan_ifname", "br0", 0},
986                {"lan_ifnames", "eth0 ath0", 0},
987                {"wan_ifname", "", 0},
988                {"wan_ifname2", "", 0},
989                {"wan_ifnames", "", 0},
990                {"wan_default", "", 0},
991                {0, 0, 0}
992        };
993#elif HAVE_OPENRISC
994        struct nvram_tuple generic[] = {
995                {"lan_ifname", "br0", 0},
996                {"lan_ifnames", "eth0 eth1 eth2 eth3 ath0", 0},
997                {"wan_ifname", "", 0},
998                {"wan_ifname2", "", 0},
999                {"wan_ifnames", "", 0},
1000                {"wan_default", "", 0},
1001                {0, 0, 0}
1002        };
1003#elif HAVE_WP54G
1004        struct nvram_tuple generic[] = {
1005                {"lan_ifname", "br0", 0},
1006                {"lan_ifnames", "eth0 ath0", 0},
1007                {"wan_ifname", "eth1", 0},
1008                {"wan_ifname2", "eth1", 0},
1009                {"wan_ifnames", "eth1", 0},
1010                {"wan_default", "eth1", 0},
1011                {0, 0, 0}
1012        };
1013#elif HAVE_NP28G
1014        struct nvram_tuple generic[] = {
1015                {"lan_ifname", "br0", 0},
1016                {"lan_ifnames", "eth0 ath0", 0},
1017                {"wan_ifname", "eth1", 0},
1018                {"wan_ifname2", "eth1", 0},
1019                {"wan_ifnames", "eth1", 0},
1020                {"wan_default", "eth1", 0},
1021                {0, 0, 0}
1022        };
1023#elif HAVE_ADM5120
1024        struct nvram_tuple generic[] = {
1025                {"lan_ifname", "br0", 0},
1026                {"lan_ifnames", "eth0 ath0", 0},
1027                {"wan_ifname", "", 0},
1028                {"wan_ifname2", "", 0},
1029                {"wan_ifnames", "", 0},
1030                {"wan_default", "", 0},
1031                {0, 0, 0}
1032        };
1033#elif HAVE_LS5
1034        struct nvram_tuple generic[] = {
1035                {"lan_ifname", "br0", 0},
1036                {"lan_ifnames", "ath0", 0},
1037                {"wan_ifname", "eth0", 0},
1038                {"wan_ifname2", "eth0", 0},
1039                {"wan_ifnames", "eth0", 0},
1040                {"wan_default", "eth0", 0},
1041                {0, 0, 0}
1042        };
1043#elif HAVE_WHRAG108
1044        struct nvram_tuple generic[] = {
1045                {"lan_ifname", "br0", 0},
1046                {"lan_ifnames", "eth0 ath0 ath1", 0},
1047                {"wan_ifname2", "eth1", 0},
1048                {"wan_ifname", "eth1", 0},
1049                {"wan_ifnames", "eth1", 0},
1050                {"wan_default", "eth1", 0},
1051                {0, 0, 0}
1052        };
1053#elif HAVE_PB42
1054        struct nvram_tuple generic[] = {
1055                {"lan_ifname", "br0", 0},
1056                {"lan_ifnames", "eth1 ath0 ath1", 0},
1057                {"wan_ifname2", "eth0", 0},
1058                {"wan_ifname", "eth0", 0},
1059                {"wan_ifnames", "eth0", 0},
1060                {"wan_default", "eth0", 0},
1061                {0, 0, 0}
1062        };
1063#elif HAVE_TW6600
1064        struct nvram_tuple generic[] = {
1065                {"lan_ifname", "br0", 0},
1066                {"lan_ifnames", "ath0 ath1", 0},
1067                {"wan_ifname2", "eth0", 0},
1068                {"wan_ifname", "eth0", 0},
1069                {"wan_ifnames", "eth0", 0},
1070                {"wan_default", "eth0", 0},
1071                {0, 0, 0}
1072        };
1073#elif HAVE_CA8PRO
1074        struct nvram_tuple generic[] = {
1075                {"lan_ifname", "br0", 0},
1076                {"lan_ifnames", "vlan0 ath0", 0},
1077                {"wan_ifname", "vlan1", 0},
1078                {"wan_ifname2", "vlan1", 0},
1079                {"wan_ifnames", "vlan1", 0},
1080                {"wan_default", "vlan1", 0},
1081                {0, 0, 0}
1082        };
1083#elif HAVE_CA8
1084        struct nvram_tuple generic[] = {
1085                {"lan_ifname", "br0", 0},
1086                {"lan_ifnames", "ath0", 0},
1087                {"wan_ifname", "eth0", 0},
1088                {"wan_ifname2", "eth0", 0},
1089                {"wan_ifnames", "eth0", 0},
1090                {"wan_default", "eth0", 0},
1091                {0, 0, 0}
1092        };
1093#else
1094        struct nvram_tuple generic[] = {
1095                {"lan_ifname", "br0", 0},
1096                {"lan_ifnames", "eth0 eth2 eth3 eth4", 0},
1097                {"wan_ifname", "eth1", 0},
1098                {"wan_ifname2", "eth1", 0},
1099                {"wan_ifnames", "eth1", 0},
1100                {"wan_default", "eth1", 0},
1101                {0, 0, 0}
1102        };
1103        struct nvram_tuple vlan[] = {
1104                {"lan_ifname", "br0", 0},
1105                {"lan_ifnames", "vlan0 eth1 eth2 eth3", 0},
1106                {"wan_ifname", "vlan1", 0},
1107                {"wan_ifname2", "vlan1", 0},
1108                {"wan_ifnames", "vlan1", 0},
1109                {"wan_default", "vlan1", 0},
1110                {0, 0, 0}
1111        };
1112
1113        struct nvram_tuple wrt350vlan[] = {
1114                {"lan_ifname", "br0", 0},
1115                {"lan_ifnames", "vlan1 eth0", 0},
1116                {"wan_ifname", "vlan2", 0},
1117                {"wan_ifname2", "vlan2", 0},
1118                {"wan_ifnames", "vlan2", 0},
1119                {"wan_default", "vlan2", 0},
1120                {0, 0, 0}
1121        };
1122
1123        struct nvram_tuple wnr3500vlan[] = {
1124                {"lan_ifname", "br0", 0},
1125                {"lan_ifnames", "vlan1 eth1", 0},
1126                {"wan_ifname", "vlan2", 0},
1127                {"wan_ifname2", "vlan2", 0},
1128                {"wan_ifnames", "vlan2", 0},
1129                {"wan_default", "vlan2", 0},
1130                {0, 0, 0}
1131        };
1132
1133        struct nvram_tuple wrt320vlan[] = {
1134                {"lan_ifname", "br0", 0},
1135                {"lan_ifnames", "vlan1 eth1", 0},
1136                {"wan_ifname", "vlan2", 0},
1137                {"wan_ifname2", "vlan2", 0},
1138                {"wan_ifnames", "vlan2", 0},
1139                {"wan_default", "vlan2", 0},
1140                {0, 0, 0}
1141        };
1142
1143        struct nvram_tuple wrt30011vlan[] = {
1144                {"lan_ifname", "br0", 0},
1145                {"lan_ifnames", "vlan0 eth0", 0},
1146                {"wan_ifname", "vlan1", 0},
1147                {"wan_ifname2", "vlan1", 0},
1148                {"wan_ifnames", "vlan1", 0},
1149                {"wan_default", "vlan1", 0},
1150                {0, 0, 0}
1151        };
1152
1153        struct nvram_tuple wrt600vlan[] = {
1154                {"lan_ifname", "br0", 0},
1155                {"lan_ifnames", "vlan0 eth0 eth1", 0},
1156                {"wan_ifname", "vlan2", 0},
1157                {"wan_ifname2", "vlan2", 0},
1158                {"wan_ifnames", "vlan2", 0},
1159                {"wan_default", "vlan2", 0},
1160                {0, 0, 0}
1161        };
1162
1163        struct nvram_tuple wrt60011vlan[] = {
1164                {"lan_ifname", "br0", 0},
1165                {"lan_ifnames", "vlan1 eth0 eth1", 0},
1166                {"wan_ifname", "vlan2", 0},
1167                {"wan_ifname2", "vlan2", 0},
1168                {"wan_ifnames", "vlan2", 0},
1169                {"wan_default", "vlan2", 0},
1170                {0, 0, 0}
1171        };
1172
1173        struct nvram_tuple wrt6102vlan[] = {
1174                {"lan_ifname", "br0", 0},
1175                {"lan_ifnames", "vlan1 eth1 eth2", 0},
1176                {"wan_ifname", "vlan2", 0},
1177                {"wan_ifname2", "vlan2", 0},
1178                {"wan_ifnames", "vlan2", 0},
1179                {"wan_default", "vlan2", 0},
1180                {0, 0, 0}
1181        };
1182
1183        struct nvram_tuple rt53nvlan[] = {
1184                {"lan_ifname", "br0", 0},
1185                {"lan_ifnames", "vlan2 eth1 eth2", 0},
1186                {"wan_ifname", "vlan1", 0},
1187                {"wan_ifname2", "vlan1", 0},
1188                {"wan_ifnames", "vlan1", 0},
1189                {"wan_default", "vlan1", 0},
1190                {0, 0, 0}
1191        };
1192
1193        struct nvram_tuple wzr144nhvlan[] = {
1194                {"lan_ifname", "br0", 0},
1195                {"lan_ifnames", "vlan2 eth0", 0},
1196                {"wan_ifname", "vlan1", 0},
1197                {"wan_ifname2", "vlan1", 0},
1198                {"wan_ifnames", "vlan1", 0},
1199                {"wan_default", "vlan1", 0},
1200                {0, 0, 0}
1201        };
1202
1203        struct nvram_tuple generic_2[] = {
1204                {"lan_ifname", "br0", 0},
1205                {"lan_ifnames", "eth1 eth2", 0},
1206                {"wan_ifname", "eth0", 0},
1207                {"wan_ifname2", "eth0", 0},
1208                {"wan_ifnames", "eth0", 0},
1209                {"wan_default", "eth0", 0},
1210                {0, 0, 0}
1211        };
1212
1213        struct nvram_tuple generic_3[] = {
1214                {"lan_ifname", "br0", 0},
1215                {"lan_ifnames", "eth0 eth1", 0},
1216                {"wan_ifname", "eth2", 0},
1217                {"wan_ifname2", "eth2", 0},
1218                {"wan_ifnames", "eth2", 0},
1219                {"wan_default", "eth2", 0},
1220                {0, 0, 0}
1221        };
1222#endif
1223
1224        struct nvram_tuple *linux_overrides;
1225        struct nvram_tuple *t, *u;
1226        int restore_defaults = 0;
1227
1228        // uint boardflags;
1229
1230        /*
1231         * Restore defaults if told to.
1232         *
1233         * Note: an intentional side effect is that when upgrading from a
1234         * firmware without the sv_restore_defaults var, defaults will also be
1235         * restored.
1236         */
1237        char *et0mac = nvram_safe_get("et0macaddr");
1238        char *et1mac = nvram_safe_get("et1macaddr");
1239
1240        // unsigned char mac[20];
1241        // if (getRouterBrand () == ROUTER_BUFFALO_WZRG144NH)
1242        // {
1243        // if (nvram_get ("il0macaddr") == NULL)
1244        // {
1245        // strcpy (mac, et0mac);
1246        // MAC_ADD (mac);
1247        // nvram_set ("il0macaddr", mac);
1248        // }
1249        // }
1250        load_defaults();
1251#ifdef HAVE_RB500
1252        linux_overrides = generic;
1253        int brand = getRouterBrand();
1254#elif HAVE_XSCALE
1255        linux_overrides = generic;
1256        int brand = getRouterBrand();
1257
1258        if (nvram_invmatch("sv_restore_defaults", "0")) // ||
1259                // nvram_invmatch("os_name",
1260                // "linux"))
1261        {
1262                restore_defaults = 1;
1263        }
1264#elif HAVE_X86
1265        linux_overrides = generic;
1266        int brand = getRouterBrand();
1267
1268        if (nvram_invmatch("sv_restore_defaults", "0")) // ||
1269                // nvram_invmatch("os_name",
1270                // "linux"))
1271        {
1272                restore_defaults = 1;
1273        }
1274#elif HAVE_MAGICBOX
1275        linux_overrides = generic;
1276        int brand = getRouterBrand();
1277
1278        if (nvram_invmatch("sv_restore_defaults", "0")) // ||
1279                // nvram_invmatch("os_name",
1280                // "linux"))
1281        {
1282                restore_defaults = 1;
1283        }
1284#elif HAVE_LAGUNA
1285        linux_overrides = generic;
1286        int brand = getRouterBrand();
1287
1288        if (nvram_invmatch("sv_restore_defaults", "0")) // ||
1289                // nvram_invmatch("os_name",
1290                // "linux"))
1291        {
1292                restore_defaults = 1;
1293        }
1294#elif HAVE_RB600
1295        linux_overrides = generic;
1296        int brand = getRouterBrand();
1297
1298        if (nvram_invmatch("sv_restore_defaults", "0")) // ||
1299                // nvram_invmatch("os_name",
1300                // "linux"))
1301        {
1302                restore_defaults = 1;
1303        }
1304#elif HAVE_GATEWORX
1305        linux_overrides = generic;
1306        int brand = getRouterBrand();
1307
1308        if (nvram_invmatch("sv_restore_defaults", "0")) // ||
1309                // nvram_invmatch("os_name",
1310                // "linux"))
1311        {
1312                restore_defaults = 1;
1313        }
1314#elif HAVE_FONERA
1315        linux_overrides = generic;
1316        int brand = getRouterBrand();
1317
1318        if (nvram_invmatch("sv_restore_defaults", "0")) // ||
1319                // nvram_invmatch("os_name",
1320                // "linux"))
1321        {
1322                restore_defaults = 1;
1323        }
1324#elif HAVE_SOLO51
1325        linux_overrides = generic;
1326        int brand = getRouterBrand();
1327
1328        if (nvram_invmatch("sv_restore_defaults", "0")) // ||
1329                // nvram_invmatch("os_name",
1330                // "linux"))
1331        {
1332                restore_defaults = 1;
1333        }
1334#elif HAVE_RT2880
1335        linux_overrides = generic;
1336        int brand = getRouterBrand();
1337
1338        if (nvram_invmatch("sv_restore_defaults", "0")) // ||
1339                // nvram_invmatch("os_name",
1340                // "linux"))
1341        {
1342                restore_defaults = 1;
1343        }
1344#elif HAVE_LS2
1345        linux_overrides = generic;
1346        int brand = getRouterBrand();
1347
1348        if (nvram_invmatch("sv_restore_defaults", "0")) // ||
1349                // nvram_invmatch("os_name",
1350                // "linux"))
1351        {
1352                restore_defaults = 1;
1353        }
1354#elif HAVE_LS5
1355        linux_overrides = generic;
1356        int brand = getRouterBrand();
1357
1358        if (nvram_invmatch("sv_restore_defaults", "0")) // ||
1359                // nvram_invmatch("os_name",
1360                // "linux"))
1361        {
1362                restore_defaults = 1;
1363        }
1364#elif HAVE_WHRAG108
1365        linux_overrides = generic;
1366        int brand = getRouterBrand();
1367
1368        if (nvram_invmatch("sv_restore_defaults", "0")) // ||
1369                // nvram_invmatch("os_name",
1370                // "linux"))
1371        {
1372                restore_defaults = 1;
1373        }
1374#elif HAVE_TW6600
1375        linux_overrides = generic;
1376        int brand = getRouterBrand();
1377
1378        if (nvram_invmatch("sv_restore_defaults", "0")) // ||
1379                // nvram_invmatch("os_name",
1380                // "linux"))
1381        {
1382                restore_defaults = 1;
1383        }
1384#elif HAVE_PB42
1385        linux_overrides = generic;
1386        int brand = getRouterBrand();
1387
1388        if (nvram_invmatch("sv_restore_defaults", "0")) // ||
1389                // nvram_invmatch("os_name",
1390                // "linux"))
1391        {
1392                restore_defaults = 1;
1393        }
1394#elif HAVE_LSX
1395        linux_overrides = generic;
1396        int brand = getRouterBrand();
1397
1398        if (nvram_invmatch("sv_restore_defaults", "0")) // ||
1399                // nvram_invmatch("os_name",
1400                // "linux"))
1401        {
1402                restore_defaults = 1;
1403        }
1404#elif HAVE_DANUBE
1405        linux_overrides = generic;
1406        int brand = getRouterBrand();
1407
1408        if (nvram_invmatch("sv_restore_defaults", "0")) // ||
1409                // nvram_invmatch("os_name",
1410                // "linux"))
1411        {
1412                restore_defaults = 1;
1413        }
1414#elif HAVE_OPENRISC
1415        linux_overrides = generic;
1416        int brand = getRouterBrand();
1417
1418        if (nvram_invmatch("sv_restore_defaults", "0")) // ||
1419                // nvram_invmatch("os_name",
1420                // "linux"))
1421        {
1422                restore_defaults = 1;
1423        }
1424#elif HAVE_STORM
1425        linux_overrides = generic;
1426        int brand = getRouterBrand();
1427
1428        if (nvram_invmatch("sv_restore_defaults", "0")) // ||
1429                // nvram_invmatch("os_name",
1430                // "linux"))
1431        {
1432                restore_defaults = 1;
1433        }
1434#elif HAVE_ADM5120
1435        linux_overrides = generic;
1436        int brand = getRouterBrand();
1437
1438        if (nvram_invmatch("sv_restore_defaults", "0")) // ||
1439                // nvram_invmatch("os_name",
1440                // "linux"))
1441        {
1442                restore_defaults = 1;
1443        }
1444#elif HAVE_CA8
1445        linux_overrides = generic;
1446        int brand = getRouterBrand();
1447
1448        if (nvram_invmatch("sv_restore_defaults", "0")) // ||
1449                // nvram_invmatch("os_name",
1450                // "linux"))
1451        {
1452                restore_defaults = 1;
1453        }
1454#elif HAVE_GEMTEK
1455        linux_overrides = generic;
1456        int brand = getRouterBrand();
1457#else
1458        int brand = getRouterBrand();
1459
1460        if (nvram_invmatch("sv_restore_defaults", "0")) // ||
1461                // nvram_invmatch("os_name",
1462                // "linux"))
1463        {
1464                // nvram_unset("sv_restore_defaults");
1465                restore_defaults = 1;
1466        }
1467        if (nvram_match("product_name", "INSPECTION")) {
1468                nvram_unset("product_name");
1469                restore_defaults = 1;
1470        }
1471        if (nvram_get("router_name") == NULL)
1472                restore_defaults = 1;
1473
1474        if (restore_defaults) {
1475                cprintf("Restoring defaults...\n");
1476#ifdef HAVE_MICRO
1477                /*
1478                 * adjust ip_conntrack_max based on available memory size
1479                 * some routers that can run micro only have 16MB memory
1480                 */
1481                FILE *fmem = fopen("/proc/meminfo", "r");
1482                char line[128];
1483                unsigned long msize = 0;
1484
1485                if (fmem != NULL) {
1486                        fgets(line, sizeof(line), fmem);        //eat first line
1487                        fgets(line, sizeof(line), fmem);
1488                        if (sscanf(line, "%*s %lu", &msize) == 1) {
1489                                if (msize > (8 * 1024 * 1024)) {
1490                                        nvram_set("ip_conntrack_max", "4096");
1491                                        nvram_set("ip_conntrack_tcp_timeouts",
1492                                                  "3600");
1493                                }
1494                        }
1495                        fclose(fmem);
1496                }
1497#endif
1498                /*
1499                 * these unsets are important for routers where we can't erase nvram
1500                 * and only software restore defaults
1501                 */
1502                nvram_unset("wan_to_lan");
1503                nvram_unset("wl_vifs");
1504                nvram_unset("wl0_vifs");
1505        }
1506        // }
1507
1508        /*
1509         * Delete dynamically generated variables
1510         */
1511        /*
1512         * Choose default lan/wan i/f list.
1513         */
1514        char *ds;
1515
1516        switch (brand) {
1517#ifndef HAVE_BUFFALO
1518        case ROUTER_WRTSL54GS:
1519        case ROUTER_WRT150N:
1520        case ROUTER_WRT160N:
1521        case ROUTER_WRT300N:
1522        case ROUTER_NETGEAR_WNR834B:
1523        case ROUTER_NETGEAR_WNR834BV2:
1524        case ROUTER_NETGEAR_WNDR3300:
1525        case ROUTER_ASUS_WL500G:
1526        case ROUTER_ASUS_WL500W:
1527#endif
1528        case ROUTER_BUFFALO_WZRG300N:
1529        case ROUTER_BUFFALO_WLAH_G54:
1530        case ROUTER_BUFFALO_WAPM_HP_AM54G54:
1531        case ROUTER_BUFFALO_WZRRSG54:
1532        case ROUTER_ASKEY_RT220XD:
1533                linux_overrides = generic;
1534                break;
1535#ifndef HAVE_BUFFALO
1536        case ROUTER_ASUS_WL500GD:
1537        case ROUTER_ASUS_WL550GE:
1538        case ROUTER_BELKIN_F5D7230_V3000:
1539                linux_overrides = vlan;
1540                break;
1541        case ROUTER_NETGEAR_WNR3500L:
1542                linux_overrides = wnr3500vlan;
1543                break;
1544        case ROUTER_WRT160NV3:
1545        case ROUTER_WRT320N:
1546        case ROUTER_WRT310NV2:
1547                linux_overrides = wrt320vlan;
1548                break;
1549        case ROUTER_WRT350N:
1550                linux_overrides = wrt350vlan;
1551                break;
1552        case ROUTER_WRT310N:
1553                linux_overrides = wrt350vlan;
1554                break;
1555        case ROUTER_WRT300NV11:
1556                linux_overrides = wrt30011vlan;
1557                break;
1558        case ROUTER_WRT600N:
1559                if (nvram_match("switch_type", "BCM5395"))
1560                        linux_overrides = wrt60011vlan;
1561                else
1562                        linux_overrides = wrt600vlan;
1563                break;
1564        case ROUTER_WRT610N:
1565                linux_overrides = wrt60011vlan;
1566                break;
1567        case ROUTER_WRT610NV2:
1568        case ROUTER_LINKSYS_E1000V2:
1569        case ROUTER_LINKSYS_E3200:
1570        case ROUTER_LINKSYS_E4200:
1571        case ROUTER_NETGEAR_WNDR4000:
1572        case ROUTER_ASUS_RTN66:
1573                linux_overrides = wrt6102vlan;
1574                break;
1575        case ROUTER_ASUS_RTN53:
1576                linux_overrides = rt53nvlan;
1577                break;
1578#endif
1579        case ROUTER_BUFFALO_WZRG144NH:
1580                linux_overrides = wzr144nhvlan;
1581                break;
1582#ifndef HAVE_BUFFALO
1583        case ROUTER_MOTOROLA_WE800G:
1584        case ROUTER_WAP54G_V1:
1585        case ROUTER_SITECOM_WL105B:
1586#endif
1587        case ROUTER_BUFFALO_WLI2_TX1_G54:
1588        case ROUTER_BUFFALO_WLAG54C:
1589                linux_overrides = generic_2;
1590                break;
1591#ifndef HAVE_BUFFALO
1592        case ROUTER_WAP54G_V2:
1593        case ROUTER_VIEWSONIC_WAPBR_100:
1594        case ROUTER_USR_5430:
1595        case ROUTER_BELKIN_F5D7230_V2000:
1596        case ROUTER_NETGEAR_WG602_V3:
1597        case ROUTER_NETGEAR_WG602_V4:
1598#endif
1599        case ROUTER_BUFFALO_WLA2G54C:
1600        case ROUTER_BUFFALO_WLI_TX4_G54HP:
1601                linux_overrides = generic_3;
1602                break;
1603#ifndef HAVE_BUFFALO
1604        case ROUTER_RT480W:
1605        case ROUTER_RT210W:
1606#endif
1607        case ROUTER_BRCM4702_GENERIC:
1608                ds = nvram_safe_get("dhcp_start");
1609                if (ds != NULL && strlen(ds) > 3) {
1610                        fprintf(stderr, "cleaning nvram variables\n");
1611                        for (t = srouter_defaults; t->name; t++) {
1612                                nvram_unset(t->name);
1613                        }
1614                        restore_defaults = 1;
1615                }
1616
1617                /*
1618                 * ds = nvram_safe_get ("http_passwd"); if (ds == NULL || strlen
1619                 * (ds) == 0) //fix for empty default password { nvram_set
1620                 * ("http_passwd", "admin"); } ds = nvram_safe_get ("language");
1621                 * if (ds != NULL && strlen (ds) < 3) { nvram_set ("language",
1622                 * "english"); }
1623                 */
1624                // fall through
1625        default:
1626                if (check_vlan_support())
1627                        linux_overrides = vlan;
1628                else
1629                        linux_overrides = generic;
1630                break;
1631        }
1632#endif
1633        /*
1634         * int i; for (i=0;i<4;i++)
1635         * nvram_set(linux_overrides[i].name,linux_overrides[i].value);
1636         */
1637
1638        /*
1639         * Restore defaults
1640         */
1641#ifdef HAVE_FON
1642        int reset = 0;
1643        char *rev = nvram_safe_get("fon_revision");
1644
1645        if (rev == NULL || strlen(rev) == 0)
1646                reset = 1;
1647        if (strlen(rev) > 0) {
1648                int n = atoi(rev);
1649
1650                if (atoi(srouter_defaults[0].value) != n)
1651                        reset = 1;
1652        }
1653        if (reset) {
1654                for (t = srouter_defaults; t->name; t++) {
1655                        for (u = linux_overrides; u && u->name; u++) {
1656                                if (!strcmp(t->name, u->name)) {
1657                                        nvram_set(u->name, u->value);
1658                                        break;
1659                                }
1660                        }
1661                        if (!u || !u->name)
1662                                nvram_set(t->name, t->value);
1663                }
1664        }
1665#endif
1666#ifdef HAVE_GATEWORX
1667        if (restore_defaults) {
1668                eval("erase", "nvram");
1669        }
1670#elif HAVE_XSCALE
1671        if (restore_defaults)
1672                eval("rm", "-f", "/etc/nvram/*");       // delete nvram database
1673#endif
1674#ifdef HAVE_MAGICBOX
1675        if (restore_defaults) {
1676                eval("rm", "-f", "/tmp/nvram/*");       // delete nvram database
1677                eval("rm", "-f", "/tmp/nvram/.lock");   // delete nvram database
1678                eval("erase", "nvram");
1679        }
1680#endif
1681#ifdef HAVE_RB600
1682        if (restore_defaults) {
1683                eval("rm", "-f", "/tmp/nvram/*");       // delete nvram database
1684                eval("rm", "-f", "/tmp/nvram/.lock");   // delete nvram database
1685                eval("erase", "nvram");
1686        }
1687#endif
1688#ifdef HAVE_RT2880
1689        if (restore_defaults) {
1690                eval("erase", "nvram");
1691        }
1692#endif
1693#ifdef HAVE_LAGUNA
1694        if (restore_defaults) {
1695                eval("erase", "nvram");
1696        }
1697#endif
1698#ifdef HAVE_FONERA
1699        if (restore_defaults) {
1700                eval("erase", "nvram");
1701        }
1702#endif
1703#ifdef HAVE_SOLO51
1704        if (restore_defaults) {
1705                eval("erase", "nvram");
1706        }
1707#endif
1708#ifdef HAVE_LS2
1709        if (restore_defaults) {
1710                eval("erase", "nvram");
1711        }
1712#endif
1713#ifdef HAVE_LS5
1714        if (restore_defaults) {
1715                eval("erase", "nvram");
1716        }
1717#endif
1718#ifdef HAVE_WHRAG108
1719        if (restore_defaults) {
1720                eval("erase", "nvram");
1721        }
1722#endif
1723#ifdef HAVE_TW6600
1724        if (restore_defaults) {
1725                eval("erase", "nvram");
1726        }
1727#endif
1728        int nvcnt = 0;
1729
1730#ifndef HAVE_MADWIFI
1731        int icnt = get_wl_instances();
1732#else
1733        int icnt = getdevicecount();
1734#endif
1735#if defined(HAVE_BUFFALO) || defined(HAVE_BUFFALO_BL_DEFAULTS)
1736        buffalo_defaults(restore_defaults);
1737#endif
1738        // if (!nvram_match("default_init","1"))
1739        {
1740                for (t = srouter_defaults; t->name; t++) {
1741                        if (restore_defaults || !nvram_get(t->name)) {
1742                                for (u = linux_overrides; u && u->name; u++) {
1743                                        if (!strcmp(t->name, u->name)) {
1744                                                nvcnt++;
1745                                                nvram_set(u->name, u->value);
1746                                                break;
1747                                        }
1748                                }
1749                                if (!u || !u->name) {
1750                                        nvcnt++;
1751                                        nvram_set(t->name, t->value);
1752                                        if (icnt == 1 && startswith(t->name, "wl1_"))   //unset wl1_xx if we have single radio only
1753                                                nvram_unset(t->name);
1754                                }
1755                        }
1756                }
1757        }
1758        free_defaults();
1759        if (strlen(nvram_safe_get("http_username")) == 0) {
1760                nvram_set("http_username", zencrypt("root"));
1761                nvram_set("http_passwd", zencrypt("admin"));
1762        }
1763#ifndef HAVE_FON
1764        if (restore_defaults) {
1765                switch (brand) {
1766                case ROUTER_ASUS_WL520G:
1767                case ROUTER_ASUS_WL500G_PRE_V2:
1768                case ROUTER_BELKIN_F5D7230_V3000:
1769                case ROUTER_USR_5465:
1770                        nvram_set("vlan0ports", "0 1 2 3 5*");
1771                        nvram_set("vlan1ports", "4 5");
1772                        break;
1773                case ROUTER_LINKSYS_WTR54GS:
1774                        nvram_set("vlan0ports", "0 5*");
1775                        nvram_set("vlan1ports", "1 5");
1776                        break;
1777                case ROUTER_ASUS_WL550GE:
1778                        nvram_set("vlan0ports", "1 2 3 4 5*");
1779                        nvram_set("vlan1ports", "0 5");
1780                        break;
1781                case ROUTER_MOTOROLA:
1782                case ROUTER_WRT54G_V8:
1783                case ROUTER_ASUS_RTN12:
1784                        nvram_set("vlan0ports", "3 2 1 0 5*");
1785                        nvram_set("vlan1ports", "4 5");
1786                        break;
1787                case ROUTER_ASUS_RTN12B:
1788                        nvram_set("vlan0ports", "3 2 1 0 5*");
1789                        nvram_set("vlan1ports", "4 5");
1790                        break;
1791                case ROUTER_LINKSYS_WRH54G:
1792                case ROUTER_ASUS_WL500GD:
1793                case ROUTER_BUFFALO_WBR2G54S:
1794                        nvram_set("vlan0ports", "4 3 2 1 5*");
1795                        nvram_set("vlan1ports", "0 5");
1796                        break;
1797                case ROUTER_USR_5461:
1798                        nvram_set("vlan0ports", "4 1 2 3 5*");
1799                        nvram_set("vlan1ports", "0 5");
1800                        break;
1801                default:
1802                        if (nvram_match("boardnum", "WAP54GV3_8M_0614")) {
1803                                nvram_set("vlan0ports", "3 2 1 0 5*");
1804                                nvram_set("vlan1ports", "4 5");
1805                        }
1806                        break;
1807                }
1808#ifdef HAVE_SPUTNIK
1809                nvram_set("lan_ipaddr", "192.168.180.1");
1810#elif HAVE_BUFFALO
1811                nvram_set("lan_ipaddr", "192.168.11.1");
1812#else
1813                nvram_set("lan_ipaddr", "192.168.1.1");
1814#endif
1815        }
1816#else
1817        if (restore_defaults) {
1818                nvram_set("lan_ipaddr", "192.168.10.1");
1819        }
1820#endif
1821#ifdef HAVE_SKYTRON
1822        if (restore_defaults) {
1823                nvram_set("lan_ipaddr", "192.168.0.1");
1824        }
1825#endif
1826        if (brand == ROUTER_WRT600N) {
1827                if (nvram_match("switch_type", "BCM5395") && nvram_match("vlan0ports", "1 2 3 4 8*"))   // fix for WRT600N
1828                        // v1.1 (BCM5395 does
1829                        // not suppport vid
1830                        // 0, so gemtek
1831                        // internally
1832                        // configured vid 1
1833                        // as lan)
1834                {
1835                        nvram_set("vlan0ports", " ");
1836                        nvram_set("vlan1ports", "1 2 3 4 8*");
1837                        nvram_set("vlan2ports", "0 8*");
1838                        nvram_set("vlan0hwname", " ");
1839                        nvram_set("vlan1hwname", "et0");
1840                        nvram_set("landevs", "vlan1 wl0 wl1");
1841                        nvram_set("lan_ifnames", "vlan1 eth0 eth1");
1842                } else {
1843                        if (!nvram_get("vlan0ports")
1844                            || nvram_match("vlan0ports", "")) {
1845                                nvram_set("vlan0ports", "1 2 3 4 8*");
1846                                nvram_set("vlan2ports", "0 8*");
1847                        }
1848                        if (!nvram_get("vlan2ports")
1849                            || nvram_match("vlan2ports", "")) {
1850                                nvram_set("vlan0ports", "1 2 3 4 8*");
1851                                nvram_set("vlan2ports", "0 8*");
1852                        }
1853                }
1854        } else if (brand == ROUTER_WRT610N || brand == ROUTER_WRT610NV2) {
1855                if (!nvram_get("vlan1ports") || nvram_match("vlan1ports", "")) {
1856                        nvram_set("vlan1ports", "1 2 3 4 8*");
1857                        nvram_set("vlan2ports", "0 8");
1858                }
1859                if (!nvram_get("vlan2ports") || nvram_match("vlan2ports", "")) {
1860                        nvram_set("vlan1ports", "1 2 3 4 8*");
1861                        nvram_set("vlan2ports", "0 8");
1862                }
1863        } else if (brand == ROUTER_WRT350N) {
1864
1865                if (!nvram_get("vlan1ports") || nvram_match("vlan1ports", "")) {
1866                        nvram_set("vlan1ports", "1 2 3 4 8*");
1867                        nvram_set("vlan2ports", "0 8");
1868                }
1869                if (!nvram_get("vlan2ports") || nvram_match("vlan2ports", "")) {
1870                        nvram_set("vlan1ports", "1 2 3 4 8*");
1871                        nvram_set("vlan2ports", "0 8");
1872                }
1873
1874        } else if (brand == ROUTER_WRT310N) {
1875
1876                if (!nvram_get("vlan1ports") || nvram_match("vlan1ports", "")) {
1877                        nvram_set("vlan1ports", "1 2 3 4 8*");
1878                        nvram_set("vlan2ports", "0 8");
1879                }
1880                if (!nvram_get("vlan2ports") || nvram_match("vlan2ports", "")) {
1881                        nvram_set("vlan1ports", "1 2 3 4 8*");
1882                        nvram_set("vlan2ports", "0 8");
1883                }
1884
1885        } else if (brand == ROUTER_WRT300NV11) {
1886
1887                if (!nvram_get("vlan0ports") || nvram_match("vlan0ports", "")) {
1888                        nvram_set("vlan0ports", "1 2 3 4 5*");
1889                        nvram_set("vlan1ports", "0 5");
1890                }
1891                if (!nvram_get("vlan1ports") || nvram_match("vlan1ports", "")) {
1892                        nvram_set("vlan0ports", "1 2 3 4 5*");
1893                        nvram_set("vlan1ports", "0 5");
1894                }
1895
1896        } else if (brand == ROUTER_ASUS_RTN66) {
1897
1898                if (!nvram_get("vlan1ports") || nvram_match("vlan1ports", "")) {
1899                        nvram_set("vlan1ports", "1 2 3 4 8*");
1900                        nvram_set("vlan2ports", "0 8u");
1901                }
1902                if (!nvram_get("vlan2ports") || nvram_match("vlan2ports", "")) {
1903                        nvram_set("vlan1ports", "1 2 3 4 8*");
1904                        nvram_set("vlan2ports", "0 8u");
1905                }
1906
1907        } else if (brand == ROUTER_ASUS_RTN53) {
1908
1909                if (!nvram_get("vlan1ports") || nvram_match("vlan1ports", "")) {
1910                        nvram_set("vlan1ports", "0 5");
1911                        nvram_set("vlan2ports", "1 2 3 4 5*");
1912                }
1913                if (!nvram_get("vlan2ports") || nvram_match("vlan2ports", "")) {
1914                        nvram_set("vlan1ports", "0 5");
1915                        nvram_set("vlan2ports", "1 2 3 4 5*");
1916                }
1917
1918        } else if (brand == ROUTER_BUFFALO_WZRG144NH) {
1919                if (!nvram_get("vlan1ports") || nvram_match("vlan1ports", "")) {
1920                        nvram_set("vlan1ports", "4 8");
1921                        nvram_set("vlan2ports", "0 1 2 3 8*");
1922                }
1923                if (!nvram_get("vlan2ports") || nvram_match("vlan2ports", "")) {
1924                        nvram_set("vlan1ports", "4 8");
1925                        nvram_set("vlan2ports", "0 1 2 3 8*");
1926                }
1927
1928        } else {
1929                if (!nvram_get("vlan0hwname") || nvram_match("vlan0hwname", ""))
1930                        nvram_set("vlan0hwname", "et0");
1931                if (!nvram_get("vlan1hwname") || nvram_match("vlan1hwname", ""))
1932                        nvram_set("vlan1hwname", "et0");
1933
1934                switch (brand) {
1935                case ROUTER_MOTOROLA:
1936                case ROUTER_MOTOROLA_V1:
1937                case ROUTER_MOTOROLA_WE800G:
1938                case ROUTER_RT210W:
1939                        if (et0mac != NULL)
1940                                nvram_set("et0macaddr", et0mac);
1941                        if (et1mac != NULL)
1942                                nvram_set("et1macaddr", et1mac);
1943                        break;
1944                }
1945
1946                if (!nvram_get("vlan0ports") || nvram_match("vlan0ports", "")) {
1947                        switch (brand) {
1948                        case ROUTER_NETGEAR_WNR3500L:
1949                        case ROUTER_WRT320N:
1950                                nvram_unset("vlan0hwname");
1951                                break;
1952                        case ROUTER_LINKSYS_WTR54GS:
1953                                nvram_set("vlan0ports", "0 5*");
1954                                break;
1955                        case ROUTER_ASUS_WL500G_PRE:
1956                        case ROUTER_ASUS_WL700GE:
1957                                nvram_set("vlan0ports", "1 2 3 4 5*");
1958                                break;
1959                        case ROUTER_MOTOROLA:
1960                        case ROUTER_WRT54G_V8:
1961                        case ROUTER_BELKIN_F5D7231_V2000:
1962                                nvram_set("vlan0ports", "3 2 1 0 5*");
1963                                break;
1964                        case ROUTER_LINKSYS_WRT55AG:
1965                        case ROUTER_RT480W:
1966                        case ROUTER_DELL_TRUEMOBILE_2300_V2:
1967                        case ROUTER_ASUS_WL520G:
1968                        case ROUTER_ASUS_WL500G_PRE_V2:
1969                        case ROUTER_WRT54G_V81:
1970                        case ROUTER_BELKIN_F5D7230_V3000:
1971                        case ROUTER_USR_5465:
1972                                nvram_set("vlan0ports", "0 1 2 3 5*");
1973                                break;
1974                        case ROUTER_LINKSYS_WRH54G:
1975                        case ROUTER_ASUS_WL500GD:
1976                        case ROUTER_BUFFALO_WBR2G54S:
1977                                nvram_set("vlan0ports", "4 3 2 1 5*");
1978                                break;
1979                        case ROUTER_USR_5461:
1980                                nvram_set("vlan0ports", "4 1 2 3 5*");
1981                                break;
1982                        default:
1983                                if (nvram_match("bootnv_ver", "4")
1984                                    || nvram_match("boardnum",
1985                                                   "WAP54GV3_8M_0614"))
1986                                        nvram_set("vlan0ports", "3 2 1 0 5*");
1987                                else
1988                                        nvram_set("vlan0ports", "1 2 3 4 5*");
1989                                break;
1990                        }
1991                }
1992
1993                if (!nvram_get("vlan1ports") || nvram_match("vlan1ports", "")) {
1994                        switch (brand) {
1995                        case ROUTER_NETGEAR_WNR3500L:
1996                        case ROUTER_WRT320N:
1997                                nvram_set("vlan2ports", "0 8");
1998                                break;
1999                        case ROUTER_LINKSYS_WTR54GS:
2000                                nvram_set("vlan1ports", "1 5");
2001                                break;
2002                        case ROUTER_ASUS_WL500G_PRE:
2003                        case ROUTER_LINKSYS_WRH54G:
2004                        case ROUTER_USR_5461:
2005                                nvram_set("vlan1ports", "0 5");
2006                                break;
2007                        case ROUTER_MOTOROLA:
2008                        case ROUTER_WRT54G_V8:
2009                        case ROUTER_WRT54G_V81:
2010                        case ROUTER_LINKSYS_WRT55AG:
2011                        case ROUTER_RT480W:
2012                        case ROUTER_DELL_TRUEMOBILE_2300_V2:
2013                        case ROUTER_ASUS_WL520G:
2014                        case ROUTER_ASUS_WL500G_PRE_V2:
2015                        case ROUTER_BELKIN_F5D7230_V3000:
2016                        case ROUTER_BELKIN_F5D7231_V2000:
2017                        case ROUTER_USR_5465:
2018                                nvram_set("vlan1ports", "4 5");
2019                                break;
2020                        default:
2021                                if (nvram_match("bootnv_ver", "4")
2022                                    || nvram_match("boardnum",
2023                                                   "WAP54GV3_8M_0614"))
2024                                        nvram_set("vlan1ports", "4 5");
2025                                else
2026                                        nvram_set("vlan1ports", "0 5");
2027                                break;
2028                        }
2029                }
2030
2031        }
2032
2033        if (restore_defaults) { //hack for VLAN page display for some routers: lan is on vlan1, wan is on vlan2
2034                if (strlen(nvram_safe_get("vlan1ports")) == 10) {
2035                        nvram_set("port0vlans", "2");
2036                        nvram_set("port1vlans", "1");
2037                        nvram_set("port2vlans", "1");
2038                        nvram_set("port3vlans", "1");
2039                        nvram_set("port4vlans", "1");
2040                        nvram_set("port5vlans", "1 2 16");
2041                }
2042        }
2043
2044        if (brand == ROUTER_WRT54G || brand == ROUTER_WRT54G1X
2045            || brand == ROUTER_LINKSYS_WRT55AG) {
2046                if (!nvram_get("aa0"))
2047                        nvram_set("aa0", "3");
2048                if (!nvram_get("ag0"))
2049                        nvram_set("ag0", "255");
2050                if (!nvram_get("gpio2"))
2051                        nvram_set("gpio2", "adm_eecs");
2052                if (!nvram_get("gpio3"))
2053                        nvram_set("gpio3", "adm_eesk");
2054                if (!nvram_get("gpio5"))
2055                        nvram_set("gpio5", "adm_eedi");
2056                if (!nvram_get("gpio6"))
2057                        nvram_set("gpio6", "adm_rc");
2058                if (!nvram_get("boardrev") || nvram_match("boardrev", ""))
2059                        nvram_set("boardrev", "0x10");
2060                if (!nvram_get("boardflags") || nvram_match("boardflags", ""))
2061                        nvram_set("boardflags", "0x0388");
2062                if (!nvram_get("boardflags2"))
2063                        nvram_set("boardflags2", "0");
2064        }
2065
2066        if (restore_defaults &&
2067            (brand == ROUTER_ASUS_RTN10 || brand == ROUTER_ASUS_RTN12
2068             || brand == ROUTER_ASUS_RTN10U || brand == ROUTER_ASUS_RTN53
2069             || brand == ROUTER_ASUS_RTN12B || brand == ROUTER_ASUS_RTN16)) {
2070                nvram_set("wl0_txpwr", "17");
2071        }
2072
2073        if (restore_defaults && (brand == ROUTER_LINKSYS_E4200)) {
2074                nvram_set("wl0_txpwr", "100");
2075                nvram_set("wl1_txpwr", "100");
2076        }
2077#ifndef HAVE_BUFFALO
2078        if (restore_defaults && brand == ROUTER_BUFFALO_WHRG54S
2079            && nvram_match("DD_BOARD", "Buffalo WHR-HP-G54")) {
2080                nvram_set("wl0_txpwr", "28");
2081        }
2082#endif
2083        if (restore_defaults && brand == ROUTER_BUFFALO_WLI_TX4_G54HP) {
2084                nvram_set("wl0_txpwr", "28");
2085        }
2086
2087        /*
2088         * Always set OS defaults
2089         */
2090        nvram_set("os_name", "linux");
2091        nvram_set("os_version", EPI_VERSION_STR);
2092
2093#ifdef HAVE_SPUTNIK_APD
2094        /*
2095         * Added for Sputnik Agent
2096         */
2097        nvram_unset("sputnik_mjid");
2098        nvram_unset("sputnik_rereg");
2099#endif
2100#ifdef HAVE_FREERADIUS
2101        nvram_unset("cert_running");
2102#endif
2103        nvram_unset("probe_blacklist");
2104
2105        if (nvram_get("overclocking") == NULL) {
2106                char *clk = nvram_safe_get("clkfreq");
2107                char dup[64];
2108
2109                strcpy(dup, clk);
2110                int j;
2111                for (j = 0; j < strlen(dup); j++)
2112                        if (dup[j] == ',')
2113                                dup[j] = 0;
2114
2115                nvram_set("overclocking", dup);
2116        }
2117        cprintf("start overclocking\n");
2118        start_overclocking();
2119        cprintf("done()");
2120        if (nvram_get("http_username") != NULL) {
2121                if (nvram_match("http_username", "")) {
2122#ifdef HAVE_POWERNOC
2123                        nvram_set("http_username", "bJz7PcC1rCRJQ");    // admin
2124#else
2125                        nvram_set("http_username", "bJ/GddyoJuiU2");    // root
2126#endif
2127                }
2128        }
2129        if (atoi(nvram_safe_get("nvram_ver")) < 3) {
2130                nvram_set("nvram_ver", "3");
2131                nvram_set("block_multicast", "1");
2132        }
2133        nvram_set("gozila_action", "0");
2134        nvram_set("generate_key", "0");
2135        nvram_set("clone_wan_mac", "0");
2136        nvram_unset("flash_active");
2137
2138        cprintf("check CFE nv\n");
2139        if (check_now_boot() == PMON_BOOT)
2140                check_pmon_nv();
2141        else
2142                check_cfe_nv();
2143        cprintf("restore defaults\n");
2144
2145        glob_t globbuf;
2146        char globstring[1024];
2147        char firststyle[32] = "";
2148        int globresult;
2149
2150        sprintf(globstring, "/www/style/*");
2151        globresult = glob(globstring, GLOB_NOSORT, NULL, &globbuf);
2152        int i, found = 0;
2153        for (i = 0; i < globbuf.gl_pathc; i++) {
2154                char *style;
2155                style = strrchr(globbuf.gl_pathv[i], '/') + 1;
2156#ifdef HAVE_PWC
2157                if (strcmp(style, "pwc"))
2158#endif
2159                        if (firststyle[0] == '\0')
2160                                strcpy(firststyle, style);
2161
2162                if (!strcmp(nvram_get("router_style"), style)) {
2163                        found = 1;
2164                }
2165        }
2166        if (found == 0 && firststyle[0] != '\0') {
2167                fprintf(stderr, "[SET ROUTER STYLE] %s\n", firststyle);
2168                nvram_set("router_style", firststyle);
2169                if (!restore_defaults) {
2170                        nvram_commit();
2171                }
2172        }
2173        globfree(&globbuf);
2174
2175        /*
2176         * Commit values
2177         */
2178        if (restore_defaults) {
2179                int i;
2180
2181                unset_nvram();
2182                nvram_commit();
2183                cprintf("done\n");
2184                for (i = 0; i < MAX_NVPARSE; i++) {
2185                        del_wds_wsec(0, i);
2186                        del_wds_wsec(1, i);
2187                }
2188        }
2189}
2190
2191void start_drivers(void)
2192{
2193        /*
2194         * #ifdef HAVE_USB //load usb driver. we will add samba server, ftp
2195         * server and ctorrent support in future modules = "usbcore usb-ohci
2196         * usb-uhci ehci-hcd scsi_mod usb-storage ide-core ide-detect
2197         * ide-disk ide-scsi cdrom ide-cd printer sd_mod sr_mod" foreach
2198         * (module, modules, next) { cprintf ("loading %s\n", module);
2199         * insmod(module); } #endif
2200         */
2201
2202#ifdef HAVE_USB
2203
2204        fprintf(stderr, "[USB] checking...\n");
2205        if (nvram_match("usb_enable", "1")) {
2206                led_control(USB_POWER, LED_ON);
2207                led_control(LED_USB, LED_ON);
2208
2209                cprintf("loading usbcore\n");
2210                insmod("usbcore");
2211
2212                cprintf("loading usb2 module\n");
2213                insmod("ehci-hcd");
2214
2215                cprintf("loading usb-uhci\n");
2216                insmod("usb-uhci");
2217                insmod("uhci-hcd");
2218
2219                cprintf("loading usb-ohci\n");
2220                insmod("usb-ohci");
2221                insmod("ohci-hcd");
2222
2223                insmod("xhci-hcd");
2224#ifdef HAVE_DANUBE
2225                insmod("dwc_otg");      // usb
2226#endif
2227                insmod("usb-libusual");
2228
2229                if (nvram_match("usb_storage", "1")) {
2230                        cprintf("loading scsi_mod\n");
2231                        insmod("scsi_mod");
2232                        insmod("scsi_wait_scan");
2233                        cprintf("loading sd_mod\n");
2234                        insmod("sd_mod");
2235                        cprintf("loading cdrom drivers\n");
2236                        insmod("cdrom");
2237                        insmod("sr_mod");
2238                        cprintf("loading usb-storage\n");
2239                        insmod("usb-storage");
2240                }
2241
2242                if (nvram_match("usb_printer", "1")) {
2243                        cprintf("loading printer\n");
2244                        insmod("printer");
2245                        insmod("usblp");
2246                }
2247#ifdef HAVE_USBIP
2248                if (nvram_match("usb_ip", "1")) {
2249                        cprintf("loading usb over ip drivers\n");
2250                        insmod("usbip_common_mod");
2251                        insmod("usbip");
2252
2253                        insmod("usbip-core");
2254                        insmod("usbip-host");
2255                        eval("usbipd", "-D");
2256                }
2257#endif
2258                mount("devpts", "/proc/bus/usb", "usbfs", MS_MGC_VAL, NULL);
2259//   Mounting is done by hotplug event!         
2260//              if( nvram_match("usb_automnt", "1") && nvram_match("usb_storage", "1")) {
2261//                      printf(stderr, "[USB] check for drives....\n");
2262//                      usb_add_ufd();
2263//              }
2264        } else {
2265                eval("stopservice", "samba3");
2266                eval("stopservice", "ftpsrv");
2267                sysprintf("umount /%s",
2268                          nvram_default_get("usb_mntpoint", "mnt"));
2269                eval("rmmod", "usblp");
2270                eval("rmmod", "printer");
2271                eval("rmmod", "usb-storage");
2272                eval("rmmod", "sr_mod");
2273                eval("rmmod", "cdrom");
2274                eval("rmmod", "sd_mod");
2275                eval("rmmod", "scsi_wait_scan");
2276                eval("rmmod", "scsi_mod");
2277                eval("rmmod", "usb-ohci");
2278                eval("rmmod", "ohci-hcd");
2279                eval("rmmod", "uhci-hcd");
2280                eval("rmmod", "usb-uhci");
2281                eval("rmmod", "ehci-hcd");
2282                eval("rmmod", "usbcore");
2283/* unload filesystems */
2284/* xfs */
2285                eval("rmmod", "xfs");
2286/* fat */
2287                eval("rmmod", "msdos");
2288                eval("rmmod", "vfat");
2289                eval("rmmod", "fat");
2290                eval("rmmod", "nls_utf8");
2291                eval("rmmod", "nls_iso8859-2");
2292                eval("rmmod", "nls_iso8859-1");
2293                eval("rmmod", "nls_cp437");
2294                eval("rmmod", "nls_cp932");
2295                eval("rmmod", "nls_cp936");
2296                eval("rmmod", "nls_cp950");
2297                eval("rmmod", "nls_base");
2298//
2299/* ext3 */
2300#ifdef HAVE_USB_ADVANCED
2301                eval("rmmod", "ext3");
2302                eval("rmmod", "jbd");
2303#endif
2304/* ext2 */
2305                eval("rmmod", "ext2");
2306                eval("rmmod", "mbcache");
2307/* ntfs-3g */
2308#ifdef HAVE_NTFS3G
2309                eval("rmmod", "fuse");
2310#endif
2311
2312                led_control(USB_POWER, LED_OFF);
2313
2314                led_control(LED_USB, LED_OFF);
2315        }
2316#endif
2317
2318}
2319
2320/*
2321 * States
2322 */
2323enum {
2324        RESTART,
2325        STOP,
2326        START,
2327        TIMER,
2328        USER,
2329        IDLE,
2330};
2331static int state = START;
2332static int signalled = -1;
2333
2334/*
2335 * Signal handling
2336 */
2337static void rc_signal(int sig)
2338{
2339        if (state == IDLE) {
2340                if (sig == SIGHUP) {
2341                        printf("signalling RESTART\n");
2342                        signalled = RESTART;
2343                } else if (sig == SIGUSR2) {
2344                        printf("signalling START\n");
2345                        signalled = START;
2346                } else if (sig == SIGINT) {
2347                        printf("signalling STOP\n");
2348                        signalled = STOP;
2349                } else if (sig == SIGALRM) {
2350                        printf("signalling TIMER\n");
2351                        signalled = TIMER;
2352                } else if (sig == SIGUSR1) {    // Receive from WEB
2353                        printf("signalling USER1\n");
2354                        signalled = USER;
2355                }
2356
2357        }
2358}
2359
2360/*
2361 * Timer procedure
2362 */
2363int do_timer(void)
2364{
2365        // do_ntp();
2366        return 0;
2367}
2368
2369#define CONVERT_NV(old, new) \
2370        if(nvram_get(old)) \
2371                nvram_set(new, nvram_safe_get(old));
2372
2373void start_nvram(void)
2374{
2375        int i = 0;
2376
2377        /*
2378         * broadcom 3.11.48.7 change some nvram name
2379         */
2380
2381        nvram_unset("wl0_hwaddr");      // When disbale wireless, we must get
2382        //
2383        // null wireless mac */
2384
2385        nvram_set("wan_get_dns", "");
2386        nvram_set("filter_id", "1");
2387        nvram_set("wl_active_add_mac", "0");
2388        nvram_set("ddns_change", "");
2389        nvram_unset("action_service");
2390        nvram_set("wan_get_domain", "");
2391
2392        // if(!nvram_get("wl_macmode1")){
2393        // if(nvram_match("wl_macmode","disabled"))
2394        // nvram_set("wl_macmode1","disabled");
2395        // else
2396        // nvram_set("wl_macmode1","other");
2397        // }
2398        if (nvram_match("wl_gmode", "5"))       // Mixed mode had been
2399                // changed to 5
2400                nvram_set("wl_gmode", "1");
2401
2402        if (nvram_match("wl_gmode", "4"))       // G-ONLY mode had been
2403                // changed to 2, after 1.40.1
2404                // for WiFi G certication
2405                nvram_set("wl_gmode", "2");
2406
2407        // nvram_set("wl_country","Worldwide"); // The country always Worldwide
2408
2409        nvram_set("ping_ip", "");
2410        nvram_set("ping_times", "");
2411        // nvram_set ("traceroute_ip", "");
2412
2413        nvram_set("filter_port", "");   // The name have been disbaled from
2414        // 1.41.3
2415
2416#ifdef HAVE_UPNP
2417        if ((nvram_match("restore_defaults", "1"))
2418            || (nvram_match("upnpcas", "1"))) {
2419                nvram_set("upnp_clear", "1");
2420        } else {
2421                char s[32];
2422                char *nv;
2423
2424                for (i = 0; i < MAX_NVPARSE; ++i) {
2425                        sprintf(s, "forward_port%d", i);
2426                        if ((nv = nvram_get(s)) != NULL) {
2427                                if (strstr(nv, "msmsgs"))
2428                                        nvram_unset(s);
2429                        }
2430                }
2431        }
2432        nvram_set("upnp_wan_proto", "");
2433#endif
2434
2435        /*
2436         * The tkip and aes already are changed to wl_crypto from v3.63.3.0
2437         */
2438        if (nvram_match("wl_wep", "tkip")) {
2439                nvram_set("wl_crypto", "tkip");
2440        } else if (nvram_match("wl_wep", "aes")) {
2441                nvram_set("wl_crypto", "aes");
2442        } else if (nvram_match("wl_wep", "tkip+aes")) {
2443                nvram_set("wl_crypto", "tkip+aes");
2444        }
2445
2446        if (nvram_match("wl_wep", "restricted"))
2447                nvram_set("wl_wep", "enabled"); // the nas need this value,
2448        // the "restricted" is no
2449        // longer need. (20040624 by
2450        // honor)
2451
2452#ifdef HAVE_SET_BOOT
2453        if (!nvram_match("boot_wait_web", "0"))
2454                nvram_set("boot_wait_web", "1");
2455#endif
2456#ifndef HAVE_BUFFALO
2457        if (check_hw_type() == BCM5352E_CHIP) {
2458                nvram_set("opo", "0");  // OFDM power reducement in quarter
2459                // dbm (2 dbm in this case)
2460                nvram_set("ag0", "0");  // Antenna Gain definition in dbm
2461        }
2462#endif
2463
2464        if (nvram_match("svqos_port1bw", "full"))
2465                nvram_set("svqos_port1bw", "FULL");
2466        if (nvram_match("svqos_port2bw", "full"))
2467                nvram_set("svqos_port2bw", "FULL");
2468        if (nvram_match("svqos_port3bw", "full"))
2469                nvram_set("svqos_port3bw", "FULL");
2470        if (nvram_match("svqos_port4bw", "full"))
2471                nvram_set("svqos_port4bw", "FULL");
2472        // dirty fix for WBR2 units
2473
2474        // clean old filter_servicesX to free nvram
2475        nvram_unset("filter_services0");
2476        nvram_unset("filter_services1");
2477        nvram_unset("filter_services2");
2478        nvram_unset("filter_services3");
2479        nvram_unset("filter_services4");
2480        nvram_unset("filter_services5");
2481        nvram_unset("filter_services6");
2482        nvram_unset("filter_services7");
2483
2484        nvram_unset("vdsl_state");      // important (this value should never
2485        //
2486        // be commited, but if this will fix
2487        // the vlan7 issue)
2488        nvram_unset("fromvdsl");        // important (this value should never be
2489        // commited, but if this will fix the vlan7
2490        // issue)
2491
2492        nvram_unset("do_reboot");       //for GUI, see broadcom.c
2493
2494#ifdef DIST
2495        nvram_set("dist_type", DIST);
2496#endif
2497
2498        {
2499
2500#ifdef DIST
2501#ifndef HAVE_TW6600
2502#ifdef HAVE_MICRO
2503                // if dist_type micro, check styles, and force to elegant if needed
2504
2505#ifdef HAVE_ROUTERSTYLE
2506                char *style = nvram_safe_get("router_style");
2507
2508                if (!strstr("blue cyan elegant green orange red yellow", style))
2509#endif
2510                {
2511                        nvram_set("router_style", "elegant");
2512                }
2513#endif
2514#endif
2515#endif
2516        }
2517
2518#ifdef HAVE_WIVIZ
2519        if (!strlen(nvram_safe_get("hopseq"))
2520            || !strlen(nvram_safe_get("hopdwell"))) {
2521                nvram_set("hopdwell", "1000");
2522                nvram_set("hopseq", "0");
2523        }
2524#endif
2525        nvram_unset("lasthour");
2526#ifdef HAVE_AQOS
2527        //filter hostapd shaping rules
2528       
2529        char *qos_mac = nvram_safe_get("svqos_macs");
2530
2531        if (strlen(qos_mac) > 0) {
2532                char *newqos = malloc(strlen(qos_mac) + 254);
2533                memset(newqos, 0, strlen(qos_mac)+254);
2534
2535                char level[32], level2[32], data[32], type[32], level3[32];
2536
2537                do {
2538                        if (sscanf
2539                            (qos_mac, "%31s %31s %31s %31s %31s |", data, level,
2540                             level2, type, level3) < 5)
2541                                break;
2542
2543                        if(!strcmp(level3, "|"))
2544                                strcpy(level3, "0\0");
2545
2546                        if (strcmp(type, "hostapd") && strcmp(type, "pppd")) {
2547                                if (strlen(newqos) > 0)
2548                                        sprintf(newqos, "%s %s %s %s %s %s |",
2549                                                newqos, data, level, level2,
2550                                                type, level3);
2551                                else
2552                                        sprintf(newqos, "%s %s %s %s %s |", data,
2553                                                level, level2, type, level3);
2554
2555                        }
2556                }
2557                while ((qos_mac = strpbrk(++qos_mac, "|")) && qos_mac++);
2558                nvram_set("svqos_macs", newqos);
2559                free(newqos);
2560        }
2561       
2562        char *qos_ip = nvram_safe_get("svqos_ips");
2563
2564        if (strlen(qos_ip) > 0) {
2565                char *newip = malloc(strlen(qos_ip) + 254);
2566                memset(newip, 0, strlen(qos_ip)+254);
2567               
2568                char data[32], level[32], level2[32], level3[32];
2569               
2570                do {
2571                        if (sscanf(qos_ip, "%31s %31s %31s %31s |", data, level, level2, level3) < 4)
2572                                break;
2573                       
2574                        if (!strcmp(level3, "|"))
2575                                strcpy(level3, "0\0");
2576                       
2577                        if (strlen(newip) > 0)
2578                                sprintf(newip, "%s %s %s %s %s |", newip, data, level, level2, level3);
2579                        else
2580                                sprintf(newip, "%s %s %s %s |", data, level, level2, level3);
2581                }
2582                while ((qos_ip = strpbrk(++qos_ip, "|")) && qos_ip++);
2583                nvram_set("svqos_ips", newip);
2584                free(newip);
2585        }
2586       
2587#endif
2588        return;
2589}
2590
2591static void unset_nvram(void)
2592{
2593#ifndef MPPPOE_SUPPORT
2594        nvram_safe_unset("ppp_username_1");
2595        nvram_safe_unset("ppp_passwd_1");
2596        nvram_safe_unset("ppp_idletime_1");
2597        nvram_safe_unset("ppp_demand_1");
2598        nvram_safe_unset("ppp_redialperiod_1");
2599        nvram_safe_unset("ppp_service_1");
2600        nvram_safe_unset("mpppoe_enable");
2601        nvram_safe_unset("mpppoe_dname");
2602#endif
2603#ifndef HAVE_HTTPS
2604        nvram_safe_unset("remote_mgt_https");
2605#endif
2606#ifndef HSIAB_SUPPORT
2607        nvram_safe_unset("hsiab_mode");
2608        nvram_safe_unset("hsiab_provider");
2609        nvram_safe_unset("hsiab_device_id");
2610        nvram_safe_unset("hsiab_device_password");
2611        nvram_safe_unset("hsiab_admin_url");
2612        nvram_safe_unset("hsiab_registered");
2613        nvram_safe_unset("hsiab_configured");
2614        nvram_safe_unset("hsiab_register_ops");
2615        nvram_safe_unset("hsiab_session_ops");
2616        nvram_safe_unset("hsiab_config_ops");
2617        nvram_safe_unset("hsiab_manual_reg_ops");
2618        nvram_safe_unset("hsiab_proxy_host");
2619        nvram_safe_unset("hsiab_proxy_port");
2620        nvram_safe_unset("hsiab_conf_time");
2621        nvram_safe_unset("hsiab_stats_time");
2622        nvram_safe_unset("hsiab_session_time");
2623        nvram_safe_unset("hsiab_sync");
2624        nvram_safe_unset("hsiab_config");
2625#endif
2626
2627#ifndef HEARTBEAT_SUPPORT
2628        nvram_safe_unset("hb_server_ip");
2629        nvram_safe_unset("hb_server_domain");
2630#endif
2631
2632#ifndef PARENTAL_CONTROL_SUPPORT
2633        nvram_safe_unset("artemis_enable");
2634        nvram_safe_unset("artemis_SVCGLOB");
2635        nvram_safe_unset("artemis_HB_DB");
2636        nvram_safe_unset("artemis_provisioned");
2637#endif
2638
2639#ifndef WL_STA_SUPPORT
2640        // nvram_safe_unset("wl_ap_ssid");
2641        // nvram_safe_unset("wl_ap_ip");
2642#endif
2643
2644#ifdef HAVE_3G
2645        // make sure we dial in mode 5 with 3g first!
2646        nvram_unset("3gnetmodetoggle");
2647#endif
2648
2649}
Note: See TracBrowser for help on using the repository browser.