1/*
2 * Copyright 2004, ASUSTek Inc.
3 * All Rights Reserved.
4 *
5 * THIS SOFTWARE IS OFFERED "AS IS", AND ASUS GRANTS NO WARRANTIES OF ANY
6 * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
7 * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
8 * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
9 *
10 * $Id: common_ex.c,v 1.9 2009/03/12 07:15:02 james26_jang Exp $
11 */
12
13
14#include <stdlib.h>
15#include <stdio.h>
16#include <time.h>
17#include <signal.h>
18#include <string.h>
19#include <bcmnvram.h>
20#include <shutils.h>
21#include <netconf.h>
22#include <wlioctl.h>
23#include <sys/time.h>
24#include <syslog.h>
25#include <stdarg.h>
26#include <arpa/inet.h>
27#include "rc.h"
28
29
30#define XSTR(s) STR(s)
31#define STR(s) #s
32
33static char list[2048];
34
35void update_lan_status(int);
36
37/* remove space in the end of string */
38char *trim_r(char *str)
39{
40	int i;
41
42	i=strlen(str);
43
44	while(i>=1)
45	{
46		if (*(str+i-1) == ' ' || *(str+i-1) == 0x0a || *(str+i-1) == 0x0d) *(str+i-1)=0x0;
47		else break;
48		i--;
49	}
50	return(str);
51}
52
53/* convert mac address format from XXXXXXXXXXXX to XX:XX:XX:XX:XX:XX */
54char *mac_conv(char *mac_name, int idx, char *buf)
55{
56	char *mac, name[32];
57	int i, j;
58
59	if (idx!=-1)
60		sprintf(name, "%s%d", mac_name, idx);
61	else sprintf(name, "%s", mac_name);
62
63	mac = nvram_safe_get(name);
64
65	if (strlen(mac)==0)
66	{
67		buf[0] = 0;
68	}
69	else
70	{
71		j=0;
72		for(i=0; i<12; i++)
73		{
74			if (i!=0&&i%2==0) buf[j++] = ':';
75			buf[j++] = mac[i];
76		}
77		buf[j] = 0;
78	}
79
80	dprintf("mac: %s\n", buf);
81
82	return(buf);
83}
84
85void getsyspara(void)
86{
87	FILE *fp;
88//	char buf[1];
89	unsigned long *imagelen;
90	char dataPtr[4];
91	char verPtr[64];
92	char productid[13];
93	char fwver[12];
94//	int i;
95
96	strcpy(productid, "WLHDD");
97	strcpy(fwver, "0.1.0.1");
98
99	if((fp = fopen("/dev/mtd/1", "rb")) != NULL){
100		if(fseek(fp, 4, SEEK_SET) != 0) goto write_ver;
101		fread(dataPtr, 1, 4, fp);
102		imagelen = (unsigned long *)dataPtr;
103
104		dprintf("image len %x\n", *imagelen);
105		if(fseek(fp, *imagelen-64, SEEK_SET) != 0)
106			goto write_ver;
107
108		dprintf("seek\n");
109		if(!fread(verPtr, 1, 64, fp))
110			goto write_ver;
111
112		dprintf("ver %x %x", verPtr[0], verPtr[1]);
113		strncpy(productid, verPtr+4, 12);
114		productid[12] = 0;
115		sprintf(fwver, "%d.%d.%d.%d", verPtr[0], verPtr[1], verPtr[2], verPtr[3]);
116
117		dprintf("get fw ver: %s\n", productid);
118		fclose(fp);
119	}
120
121write_ver:
122	// its a ugle solution for Product ID
123	if(strstr(productid, "WL500gx"))
124		nvram_set("productid", "WL500g.Deluxe");
125//	else if (strstr(productid, "WL500gp"))
126//		nvram_set("productid", "WL500g.Premium");
127	else
128		nvram_set("productid", trim_r(productid));
129
130	nvram_set("firmver", trim_r(fwver));
131}
132
133void wan_netmask_check(void)
134{
135	unsigned int ip, gw, nm, lip, lnm;
136
137	if (nvram_match("wan0_proto", "static")
138			|| nvram_match("wan0_proto", "pptp")
139			|| nvram_match("wan0_proto", "l2tp"))
140	{
141		ip = inet_addr(nvram_safe_get("wan_ipaddr"));
142		gw = inet_addr(nvram_safe_get("wan_gateway"));
143		nm = inet_addr(nvram_safe_get("wan_netmask"));
144
145		lip = inet_addr(nvram_safe_get("lan_ipaddr"));
146		lnm = inet_addr(nvram_safe_get("lan_netmask"));
147
148		dprintf("ip : %x %x %x\n", ip, gw, nm);
149
150		if (ip==0x0 && (nvram_match("wan0_proto", "pptp") || nvram_match("wan0_proto", "l2tp")))
151			return;
152
153		if (ip==0x0 || (ip&lnm)==(lip&lnm))
154		{
155			nvram_set("wan_ipaddr", "1.1.1.1");
156			nvram_set("wan_netmask", "255.0.0.0");
157			nvram_set("wan0_ipaddr", nvram_safe_get("wan_ipaddr"));
158			nvram_set("wan0_netmask", nvram_safe_get("wan_netmask"));
159		}
160
161		// check netmask here
162		if (gw==0 || gw==0xffffffff || (ip&nm)==(gw&nm))
163		{
164			nvram_set("wan0_netmask", nvram_safe_get("wan_netmask"));
165		}
166		else
167		{
168			for(nm=0xffffffff;nm!=0;nm=(nm>>8))
169			{
170				if ((ip&nm)==(gw&nm)) break;
171			}
172
173			dprintf("nm: %x\n", nm);
174
175			if (nm==0xffffffff) nvram_set("wan0_netmask", "255.255.255.255");
176			else if (nm==0xffffff) nvram_set("wan0_netmask", "255.255.255.0");
177			else if (nm==0xffff) nvram_set("wan0_netmask", "255.255.0.0");
178			else if (nm==0xff) nvram_set("wan0_netmask", "255.0.0.0");
179			else nvram_set("wan0_netmask", "0.0.0.0");
180		}
181
182	}
183}
184
185/* This function is used to map nvram value from asus to Broadcom */
186void convert_asus_values(int skipflag)
187{
188	char tmpstr[32], tmpstr1[32], macbuf[36];
189	char servers[64];
190	char ifnames[36];
191	char sbuf[64];
192	int i, num;
193	char *ptr;
194	FILE *fp;
195printf("*** Start convert_asus_values(%d). ***\n", skipflag);
196
197// 2008.03 James.  {
198	// Fixed the wrong value of nvram. {
199	if(nvram_match("macfilter_enable_x", "disabled"))
200		nvram_set("macfilter_enable_x", "0");
201
202	if(nvram_match("wl_frameburst", "0"))
203		nvram_set("wl_frameburst", "off");
204
205	if(nvram_match("wl_crypto", "0"))
206		nvram_set("wl_crypto", "tkip");
207
208	if(nvram_match("wl_preauth", "1"))
209		nvram_set("wl_preauth", "enabled");
210	else if(nvram_match("wl_preauth", "0"))
211		nvram_set("wl_preauth", "disabled");
212	// Fixed the wrong value of nvram. }
213
214	char *ddns_hostname_x = nvram_safe_get("ddns_hostname_x");
215	char *follow_info = strchr(ddns_hostname_x, '.');
216	char ddns_account[64];
217	int len = 0;
218
219	if(nvram_match("ddns_enable_x", "1") && nvram_match("ddns_server_x", "WWW.ASUS.COM")
220			&& follow_info != NULL && !strcmp(follow_info, ".asuscomm.com")){
221		len = strlen(ddns_hostname_x)-strlen(follow_info);
222
223		memset(ddns_account, 0, 64);
224		strncpy(ddns_account, ddns_hostname_x, len);
225
226		nvram_set("computer_name", ddns_account);
227		nvram_set("computer_nameb", ddns_account);
228	}
229// 2008.03 James. }
230
231	if(!skipflag)
232	{
233		getsyspara();
234		/* convert country code from regulation_domain */
235		convert_country();
236	}
237
238#ifndef CDMA
239	if(nvram_match("wan_proto", "cdma"))
240		nvram_set("wan_proto", "dhcp");
241#endif	// CDMA
242
243#if 0
244#ifndef WL500GPV2
245	{
246		int radio=atoi(nvram_safe_get("wl_radio_power_x"));
247
248		//early_convert_asus_values();
249		if (radio>=13)
250			nvram_set("wl_radio_power_x", "9");
251	}
252#endif	// WL500GPV2
253#endif	// 0
254
255#if 0
256	if(nvram_get("preferred_lang")==NULL){
257		if((nvram_match("regulation_domain","0x47TW"))||(nvram_match("regulation_domain","0X47TW")))
258			nvram_set("preferred_lang","TW");
259		else
260			nvram_set("preferred_lang","EN");
261	}
262	if(nvram_invmatch("temp_lang", ""))
263		nvram_set("preferred_lang", nvram_get("temp_lang"));
264#endif	// 0
265#ifdef DLM
266        if (nvram_match("ftp_lang","")){
267                if((nvram_match("regulation_domain","0x47TW"))||(nvram_match("regulation_domain","0X47TW")))
268                        nvram_set("ftp_lang","TW");
269                else if ((nvram_match("regulation_domain","0x44CN"))||(nvram_match("regulation_domain","0X44CN")))
270                        nvram_set("ftp_lang", "CN");
271                else if ((nvram_match("regulation_domain","0x46KR"))||(nvram_match("regulation_domain","0X46KR")))
272                        nvram_set("ftp_lang", "KR");
273                else
274                        nvram_set("ftp_lang","EN");
275        }
276#endif	// DLM
277
278	if(nvram_invmatch("httpd_die_reboot", ""))
279		nvram_set("httpd_die_reboot", "");
280#if 0
281	{
282		int radio = atoi(nvram_safe_get("wl_radio_power_x"));
283		if (radio < 16)
284			eval("wl", "txpwr1", "-o", "-d", nvram_safe_get("wl_radio_power_x"));
285		else
286			eval("wl", "txpwr1", "-1");
287	}
288#endif	// 0
289
290#ifdef WL500GPV2
291	/* Clean MFG test values when boot */
292	if (nvram_invmatch("asus_mfg", ""))
293		nvram_set("asus_mfg", "");
294	if (nvram_invmatch("asus_mfg_webcam", ""))
295		nvram_set("asus_mfg_webcam", "");
296	if (nvram_invmatch("asus_mfg_flash", ""))
297		nvram_set("asus_mfg_flash", "");
298	if (nvram_invmatch("asus_mfg_printer", ""))
299		nvram_set("asus_mfg_printer", "");
300	if (nvram_invmatch("btn_rst", ""))
301		nvram_set("btn_rst", "");
302	if (nvram_invmatch("btn_ez", ""))
303		nvram_set("btn_ez", "");
304#endif	// WL500GPV2
305
306#ifdef RT2400_SUPPORT
307	write_rt2400_conf();
308#endif	// RT2400_SUPPORT
309
310#ifdef WOB
311	// add for 4712/5350 which have no eeprom
312	//nvram_set("il0macaddr", nvram_safe_get("et0macaddr"));
313	nvram_unset("wl0_hwaddr");
314#endif	// WOB
315
316	if (nvram_match("productid", "WL500b") ||
317	    nvram_match("productid", "WL500bv2"))
318	{
319		nvram_set("wl_gmode", "0");
320	}
321
322	cprintf("read from nvram\n");
323
324	nvram_set("wl_wsc_mode", nvram_safe_get("wsc_mode"));
325	// for WPA/WPA2 - Enterprise
326	nvram_set("wl0_wsc_mode", nvram_safe_get("wl_wsc_mode"));
327
328	/* Wireless Section */
329	nvram_set("wl0_bss_enabled", nvram_safe_get("wl_bss_enabled"));
330
331	/* Country Code */
332	nvram_set("wl0_country_code", nvram_safe_get("wl_country_code"));
333
334	/* GMODE */
335	nvram_set("wl0_gmode", nvram_safe_get("wl_gmode"));
336
337	if (nvram_match("wl_gmode_protection_x", "1"))
338	{
339		//cprintf("set to auto\n");
340		nvram_set("wl_gmode_protection", "auto");
341		nvram_set("wl0_gmode_protection", "auto");
342	}
343	else
344	{
345		//cprintf("set to off");
346		nvram_set("wl_gmode_protection", "off");
347		nvram_set("wl0_gmode_protection", "off");
348	}
349
350	if (nvram_match("wl_wep_x", "0") || nvram_match("wl_auth_mode", "psk"))
351		nvram_set("wl0_wep", "disabled");
352	else nvram_set("wl0_wep", "enabled");
353
354	if (nvram_match("wl_auth_mode", "shared"))
355		nvram_set("wl0_auth", "1");
356	else nvram_set("wl0_auth", "0");
357
358
359#ifdef WPA2_WMM
360// 2008.06 James. {
361	/*if(nvram_match("wl_auth_mode", "wpa")){
362		if(nvram_match("wl_wpa_mode", "4")){
363			if(nvram_match("wl_crypto", "tkip") || nvram_match("wl_crypto", "0")){
364				nvram_set("wl_akm", "wpa");
365				nvram_set("wl0_akm", "wpa");
366			}
367			else if(nvram_match("wl_crypto", "aes")){
368				nvram_set("wl_akm", "wpa2");
369				nvram_set("wl0_akm", "wpa2");
370			}
371			else{
372				nvram_set("wl_akm", "wpa wpa2");
373				nvram_set("wl0_akm", "wpa wpa2");
374			}
375		}
376		else if(nvram_match("wl_wpa_mode", "3")){
377			nvram_set("wl_akm", "wpa");
378			nvram_set("wl0_akm", "wpa");
379		}
380	}
381	else if(nvram_match("wl_auth_mode", "wpa2")){
382		nvram_set("wl_akm", "wpa2");
383		nvram_set("wl0_akm", "wpa2");
384	}
385	else if(nvram_match("wl_auth_mode", "psk")){
386		if(nvram_match("wl_wpa_mode", "0")){
387			if(nvram_match("wl_crypto", "tkip") || nvram_match("wl_crypto", "0")){
388				nvram_set("wl_akm", "psk");
389				nvram_set("wl0_akm", "psk");
390			}
391			else if(nvram_match("wl_crypto", "aes")){
392				nvram_set("wl_akm", "psk2");
393				nvram_set("wl0_akm", "psk2");
394			}
395			else{
396				nvram_set("wl_akm", "psk psk2");
397				nvram_set("wl0_akm", "psk psk2");
398			}
399		}
400		else if(nvram_match("wl_wpa_mode", "1")){
401			nvram_set("wl_akm", "psk");
402			nvram_set("wl0_akm", "psk");
403		}
404		else if(nvram_match("wl_wpa_mode", "2")){
405			nvram_set("wl_akm", "psk2");
406			nvram_set("wl0_akm", "psk2");
407		}
408	}
409	else{
410		nvram_set("wl_akm", "");
411		nvram_set("wl0_akm", "");
412	}//*/
413	if(nvram_match("wl_auth_mode", "psk")){
414		if(nvram_match("wl_wpa_mode", "1")){
415			nvram_set("wl_akm", "psk");
416			nvram_set("wl0_akm", "psk");
417		}
418		else if(nvram_match("wl_wpa_mode", "2")){
419			nvram_set("wl_akm", "psk2");
420			nvram_set("wl0_akm", "psk2");
421		}
422		else{	// wl_wpa_mode == 0
423			nvram_set("wl_akm", "psk"); // according to the official firmware.
424			nvram_set("wl0_akm", "psk psk2");
425		}
426	}
427	else if(nvram_match("wl_auth_mode", "wpa") || nvram_match("wl_auth_mode", "wpa2")){
428		if(nvram_match("wl_auth_mode", "wpa2")){
429			nvram_set("wl_akm", "wpa2");
430			nvram_set("wl0_akm", "wpa2");
431		}
432		else if(nvram_match("wl_wpa_mode", "3")){
433			nvram_set("wl_akm", "wpa");
434			nvram_set("wl0_akm", "wpa");
435		}
436		else{	// wl_wpa_mode == 4
437			nvram_set("wl_akm", "psk");	// according to the official firmware.
438			nvram_set("wl0_akm", "wpa wpa2");
439		}
440	}
441	else{
442		nvram_set("wl_akm", "");
443		nvram_set("wl0_akm", "");
444	}//*/
445// 2008.06 James. }
446	// thanks for Oleg
447	nvram_set("wl0_auth_mode", nvram_match("wl_auth_mode", "radius") ? "radius" : "none");
448
449	nvram_set("wl0_preauth", nvram_safe_get("wl_preauth"));
450	nvram_set("wl0_net_reauth", nvram_safe_get("wl_net_reauth"));
451	nvram_set("wl0_wme", nvram_safe_get("wl_wme"));
452	nvram_set("wl0_wme_no_ack", nvram_safe_get("wl_wme_no_ack"));
453	nvram_set("wl0_wme_sta_bk", nvram_safe_get("wl_wme_sta_bk"));
454	nvram_set("wl0_wme_sta_be", nvram_safe_get("wl_wme_sta_be"));
455	nvram_set("wl0_wme_sta_vi", nvram_safe_get("wl_wme_sta_vi"));
456	nvram_set("wl0_wme_sta_vo", nvram_safe_get("wl_wme_sta_vo"));
457	nvram_set("wl0_wme_ap_bk", nvram_safe_get("wl_wme_ap_bk"));
458	nvram_set("wl0_wme_ap_be", nvram_safe_get("wl_wme_ap_be"));
459	nvram_set("wl0_wme_ap_vi", nvram_safe_get("wl_wme_ap_vi"));
460	nvram_set("wl0_wme_ap_vo", nvram_safe_get("wl_wme_ap_vo"));
461// 2008.06 James. {
462	nvram_set("wl0_wme_txp_bk", nvram_safe_get("wl_wme_txp_bk"));
463	nvram_set("wl0_wme_txp_be", nvram_safe_get("wl_wme_txp_be"));
464	nvram_set("wl0_wme_txp_vi", nvram_safe_get("wl_wme_txp_vi"));
465	nvram_set("wl0_wme_txp_vo", nvram_safe_get("wl_wme_txp_vo"));
466// 2008.06 James. }
467#else	// WPA2_WMM
468	nvram_set("wl0_auth_mode", nvram_safe_get("wl_auth_mode"));
469	nvram_set("wl_akm", "");
470	nvram_set("wl0_akm", "");
471	nvram_set("wl0_wme", "off");
472#endif	// WPA2_WMM
473
474	nvram_set("wl0_ssid", nvram_safe_get("wl_ssid"));
475	nvram_set("wl0_channel", nvram_safe_get("wl_channel"));
476	nvram_set("wl0_country_code", nvram_safe_get("wl_country_code"));
477	nvram_set("wl0_rate", nvram_safe_get("wl_rate"));
478	nvram_set("wl0_mrate", nvram_safe_get("wl_mrate"));
479	nvram_set("wl0_rateset", nvram_safe_get("wl_rateset"));
480	nvram_set("wl0_frag", nvram_safe_get("wl_frag"));
481	nvram_set("wl0_rts", nvram_safe_get("wl_rts"));
482	nvram_set("wl0_dtim", nvram_safe_get("wl_dtim"));
483	nvram_set("wl0_bcn", nvram_safe_get("wl_bcn"));
484	nvram_set("wl0_plcphdr", nvram_safe_get("wl_plcphdr"));
485	nvram_set("wl0_crypto", nvram_safe_get("wl_crypto"));
486	nvram_set("wl0_wpa_psk", nvram_safe_get("wl_wpa_psk"));
487	nvram_set("wl0_key", nvram_safe_get("wl_key"));
488	nvram_set("wl0_key1", nvram_safe_get("wl_key1"));
489	nvram_set("wl0_key2", nvram_safe_get("wl_key2"));
490	nvram_set("wl0_key3", nvram_safe_get("wl_key3"));
491	nvram_set("wl0_key4", nvram_safe_get("wl_key4"));
492	nvram_set("wl0_closed", nvram_safe_get("wl_closed"));
493	nvram_set("wl0_frameburst", nvram_safe_get("wl_frameburst"));
494	nvram_set("wl0_afterburner", nvram_safe_get("wl_afterburner"));
495	nvram_set("wl0_ap_isolate", nvram_safe_get("wl_ap_isolate"));
496	nvram_set("wl0_radio", nvram_safe_get("wl_radio_x"));
497
498	if (nvram_match("wl_wpa_mode", ""))
499		nvram_set("wl_wpa_mode", "0");
500
501
502	nvram_set("wl0_radius_ipaddr", nvram_safe_get("wl_radius_ipaddr"));
503	nvram_set("wl0_radius_port", nvram_safe_get("wl_radius_port"));
504	nvram_set("wl0_radius_key", nvram_safe_get("wl_radius_key"));
505	nvram_set("wl0_wpa_gtk_rekey", nvram_safe_get("wl_wpa_gtk_rekey"));
506
507
508	if (nvram_invmatch("wl_mode_ex", "ap"))
509	{
510		int wepidx=atoi(nvram_safe_get("wl0_key"));
511		char wepname[16];
512
513		sprintf(sbuf, "wl join \"%s\"", nvram_safe_get("wl0_ssid"));
514
515		// key ??
516		if (nvram_match("wl0_auth_mode", "psk"))
517		{
518			sprintf(sbuf, "%s key %s", sbuf, nvram_safe_get("wl0_wpa_psk"));
519		}
520		else if (nvram_match("wl0_wep", "enabled") && wepidx>=1 && wepidx<=4)
521		{
522			sprintf(wepname, "wl0_key%d", wepidx);
523			sprintf(sbuf, "%s key %s", sbuf, nvram_safe_get(wepname));
524		}
525
526		sprintf(sbuf, "%s imode bss", sbuf);
527
528		if (nvram_match("wl_auth_mode", "psk"))
529			sprintf(sbuf, "%s amode wpapsk", sbuf);
530		else if (nvram_match("wl_auth_mode", "shared"))
531			sprintf(sbuf, "%s amode shared", sbuf);
532		else sprintf(sbuf, "%s amode open", sbuf);
533
534		nvram_set("wl0_join", sbuf);
535		/* Station or Ethernet Bridge Mode */
536		nvram_set("wl0_mode", nvram_safe_get("wl_mode_ex"));
537	}
538	else
539	{
540		/* WDS control */
541		if (nvram_match("wl_mode_x", "1")
542				&& nvram_invmatch("wl_radio_x", "0") // 2010.03 James. Fix the reboot issue.
543				){
544			nvram_set("wl_mode", "wds");
545			nvram_set("wl0_mode", "wds");
546		}
547		else{
548			nvram_set("wl_mode", "ap");
549			nvram_set("wl0_mode", "ap");
550		}
551
552		nvram_set("wl0_lazywds", nvram_safe_get("wl_lazywds"));
553	}
554
555	if (nvram_match("wl_wdsapply_x", "1"))
556	{
557		num = atoi(nvram_safe_get("wl_wdsnum_x"));
558		list[0]=0;
559
560#ifdef RT2400_SUPPORT
561		fp = fopen("/tmp/RT2400AP.dat", "a+");
562		if (fp) fprintf(fp, "WdsList=");
563#endif	// RT2400_SUPPORT
564
565		for(i=0;i<num;i++)
566		{
567			sprintf(list, "%s %s", list, mac_conv("wl_wdslist_x", i, macbuf));
568
569#ifdef RT2400_SUPPORT
570			if (fp)
571			{
572				fprintf(fp, "%s;", mac_conv("wl_wdslist_x", i, macbuf));
573			}
574#endif	// RT2400_SUPPORT
575		}
576
577#ifdef RT2400_SUPPORT
578		fprintf(fp, "\n");
579		fclose(fp);
580#endif	// RT2400_SUPPORT
581		dprintf("wds list %s %x\n", list, num);
582
583		nvram_set("wl_wds", list);	// 2008.06 James.
584		nvram_set("wl0_wds", list);
585	}
586	else{
587		nvram_set("wl_wds", "");	// 2008.06 James.
588		nvram_set("wl0_wds", "");
589	}
590
591	/* Mac filter */
592	nvram_set("wl0_macmode", nvram_safe_get("wl_macmode"));
593
594	if (nvram_invmatch("wl_macmode", "disabled"))
595	{
596		num = atoi(nvram_safe_get("wl_macnum_x"));
597		list[0]=0;
598
599#ifdef RT2400_SUPPORT
600		fp = fopen("/tmp/RT2400AP.dat", "a+");
601		if (fp) fprintf(fp, "AclList=");
602#endif	// RT2400_SUPPORT
603
604		for(i=0;i<num;i++)
605		{
606			sprintf(list, "%s %s", list, mac_conv("wl_maclist_x", i, macbuf));
607
608#ifdef RT2400_SUPPORT
609			if (fp) fprintf(fp, "%s;", mac_conv("wl_maclist_x", i, macbuf));
610#endif	// RT2400_SUPPORT
611		}
612
613#ifdef RT2400_SUPPORT
614		fprintf(fp, "\n");
615		fclose(fp);
616#endif	// RT2400_SUPPORT
617		//printf("mac list %s %x\n", list, num);
618
619		nvram_set("wl_maclist", list);
620		nvram_set("wl0_maclist", list);
621	}
622
623
624#ifdef GUEST_ACCOUNT
625	if(!skipflag)
626	{
627	if (nvram_match("wl_guest_enable", "1"))
628	{
629		nvram_set("wl0.1_guest","1");
630		nvram_set("wl0.1_ifname", "wl0.1");
631		nvram_set("wl0.1_mode", "ap");
632		nvram_set("wl0.1_radio", "1");
633		nvram_set("wl0.1_ipconfig_index","1");
634		//nvram_set("unbridged_ifnames", "wl0.1");
635		nvram_set("wl0_vifs", "wl0.1");
636		nvram_set("wl0.1_ssid", nvram_safe_get("wl_guest_ssid_1"));
637		nvram_set("wl0.1_crypto", nvram_safe_get("wl_guest_crypto_1"));
638		nvram_set("wl0.1_wpa_psk", nvram_safe_get("wl_guest_wpa_psk_1"));
639		nvram_set("wl0.1_key", nvram_safe_get("wl_guest_key_1"));
640		nvram_set("wl0.1_key1", nvram_safe_get("wl_guest_key1_1"));
641		nvram_set("wl0.1_key2", nvram_safe_get("wl_guest_key2_1"));
642		nvram_set("wl0.1_key3", nvram_safe_get("wl_guest_key3_1"));
643		nvram_set("wl0.1_key4", nvram_safe_get("wl_guest_key4_1"));
644
645		if (nvram_match("wl_guest_wep_x_1", "0"))
646			nvram_set("wl0.1_wep", "disabled");
647		else nvram_set("wl0.1_wep", "enabled");
648
649		if (nvram_match("wl_guest_auth_mode_1", "shared"))
650			nvram_set("wl0.1_auth", "1");
651		else nvram_set("wl0.1_auth", "0");
652
653		if (nvram_match("wl_guest_auth_mode_1", "wpa"))
654		{
655			nvram_set("wl0.1_akm", "wpa wpa2");
656		}
657		else if (nvram_match("wl_guest_auth_mode_1", "psk"))
658		{
659			nvram_set("wl0.1_akm", "psk psk2");
660		}
661		else
662		{
663			nvram_set("wl0.1_akm", "");
664		}
665
666		nvram_set("wl0.1_auth_mode", nvram_match("wl_guest_auth_mode_1", "radius") ? "radius" : "none");
667		nvram_set("wl0.1_gmode", nvram_safe_get("wl0_gmode"));
668		nvram_set("wl0.1_gmode_protection", nvram_safe_get("wl0_gmode_protection"));
669		nvram_set("wl0.1_rate", nvram_safe_get("wl_rate"));
670		nvram_set("wl0.1_rateset", nvram_safe_get("wl_rateset"));
671		nvram_set("wl0.1_frag", nvram_safe_get("wl_frag"));
672		nvram_set("wl0.1_rts", nvram_safe_get("wl_rts"));
673		nvram_set("wl0.1_dtim", nvram_safe_get("wl_dtim"));
674		nvram_set("wl0.1_bcn", nvram_safe_get("wl_bcn"));
675		nvram_set("wl0.1_plcphdr", nvram_safe_get("wl_plcphdr"));
676		nvram_set("wl0.1_closed", nvram_safe_get("wl_closed"));
677		nvram_set("wl0.1_frameburst", nvram_safe_get("wl_frameburst"));
678		nvram_set("wl0.1_afterburner", nvram_safe_get("wl_afterburner"));
679		nvram_set("wl0.1_ap_isolate", nvram_safe_get("wl_ap_isolate"));
680		nvram_set("wl0.1_radio", nvram_safe_get("wl_radio_x"));
681		nvram_set("wl0.1_preauth", nvram_safe_get("wl_preauth"));
682		nvram_set("wl0.1_net_reauth", nvram_safe_get("wl_net_reauth"));
683		nvram_set("wl0.1_wme", nvram_safe_get("wl_wme"));
684		nvram_set("wl0.1_wme_no_ack", nvram_safe_get("wl_wme_no_ack"));
685		nvram_set("wl0.1_wme_sta_bk", nvram_safe_get("wl_wme_sta_bk"));
686		nvram_set("wl0.1_wme_sta_be", nvram_safe_get("wl_wme_sta_be"));
687		nvram_set("wl0.1_wme_sta_vi", nvram_safe_get("wl_wme_sta_vi"));
688		nvram_set("wl0.1_wme_sta_vo", nvram_safe_get("wl_wme_sta_vo"));
689		nvram_set("wl0.1_wme_ap_bk", nvram_safe_get("wl_wme_ap_bk"));
690		nvram_set("wl0.1_wme_ap_be", nvram_safe_get("wl_wme_ap_be"));
691		nvram_set("wl0.1_wme_ap_vi", nvram_safe_get("wl_wme_ap_vi"));
692		nvram_set("wl0.1_wme_ap_vo", nvram_safe_get("wl_wme_ap_vo"));
693
694		nvram_set("lan1_ifname", "wl0.1");
695
696		if (nvram_match("dhcp1_enable_x", "1"))
697		{
698			nvram_set("lan1_proto", "dhcp");
699		}
700		else
701		{
702			nvram_set("lan1_proto", "static");
703		}
704	}
705	else
706	{
707		nvram_unset("wl0.1_ifname");
708		nvram_unset("wl0.1_guest");
709		nvram_unset("wl0_vifs");
710		nvram_unset("lan1_ifname");
711	}
712}
713#endif
714
715	/* Direct copy value */
716	/* LAN Section */
717	if(nvram_match("dhcp_enable_x", "1"))
718		nvram_set("lan_proto", "dhcp");
719	else
720		nvram_set("lan_proto", "static");
721
722	nvram_set("wan0_proto", nvram_safe_get("wan_proto"));
723	if(nvram_match("x_DHCPClient", "0")){	// 2008.05 James.
724		nvram_set("wan0_ipaddr", nvram_safe_get("wan_ipaddr"));
725		nvram_set("wan0_netmask", nvram_safe_get("wan_netmask"));
726		nvram_set("wan0_gateway", nvram_safe_get("wan_gateway"));
727	}	// 2008.05 James.
728// 2008.09 James. {
729	else if(nvram_match("wan_proto", "pppoe")
730			|| nvram_match("wan_proto", "pptp")
731			|| nvram_match("wan_proto", "l2tp")){
732		nvram_set("wan_ipaddr", "0.0.0.0");
733		nvram_set("wan_netmask", "0.0.0.0");
734		nvram_set("wan_gateway", "0.0.0.0");
735
736// 2009.06 James. {
737		if(!skipflag){
738			nvram_unset("wan0_ipaddr");
739			nvram_unset("wan0_netmask");
740			nvram_unset("wan0_gateway");
741		}
742// 2009.06 James. }
743
744		if(nvram_match("wan_dnsenable_x", "1")){
745			nvram_set("wan_dns1_x", "0.0.0.0");
746			nvram_set("wan_dns2_x", "0.0.0.0");
747		}
748	}
749// 2008.09 James. }
750
751// 2007.10 James. for the procedure of no-reboot rc, and start_wan() will do this. {
752	if(!skipflag){
753		nvram_unset("add_relay_client"); // 2009.12 James. For relaying clients.
754		nvram_unset("system_ready"); // 2009.09 James. For WPS of AP mode.
755		nvram_unset("ntp_ready");	// for notifying detectWAN.
756		nvram_unset("wan_ready");	// for notifying wanduck.
757		nvram_unset("manually_disconnect_wan");	// 2008.07 James.
758		nvram_set("wan_ipaddr_t", "");
759		nvram_set("wan_netmask_t", "");
760		nvram_set("wan_gateway_t", "");
761		nvram_set("wan_dns_t", "");
762		nvram_set("wan_status_t", "Disconnected");
763		nvram_unset("qos_ubw");	// 2009.03 James.
764		nvram_unset("qos_ubw_tmp");	// 2009.03 James.
765		nvram_unset("qos_ubw_status");	// 2009.03 James.
766	}
767// 2007.10 James }
768
769	wan_netmask_check();
770
771	if(nvram_match("wan_proto", "pppoe")
772			|| nvram_match("wan_proto", "pptp")
773			|| nvram_match("wan_proto", "l2tp")
774			){
775		nvram_set("wan0_pppoe_ifname", "ppp0");
776		nvram_set("upnp_wan_proto", "pppoe");
777		nvram_set("wan0_pppoe_username", nvram_safe_get("wan_pppoe_username"));
778		nvram_set("wan0_pppoe_passwd", nvram_safe_get("wan_pppoe_passwd"));
779		nvram_set("wan0_pppoe_idletime", nvram_safe_get("wan_pppoe_idletime"));
780		nvram_set("wan0_pppoe_txonly_x", nvram_safe_get("wan_pppoe_txonly_x"));
781		nvram_set("wan0_pppoe_mtu", nvram_safe_get("wan_pppoe_mtu"));
782		nvram_set("wan0_pppoe_mru", nvram_safe_get("wan_pppoe_mru"));
783		nvram_set("wan0_pppoe_service", nvram_safe_get("wan_pppoe_service"));
784		nvram_set("wan0_pppoe_ac", nvram_safe_get("wan_pppoe_ac"));
785		nvram_set("wan0_pppoe_options_x", nvram_safe_get("wan_pppoe_options_x"));
786		nvram_set("wan0_pptp_options_x", nvram_safe_get("wan_pptp_options_x"));
787#ifdef REMOVE
788		nvram_set("wan0_pppoe_demand", "1");
789		nvram_set("wan0_pppoe_keepalive", "1");
790#endif	// REMOVE
791		nvram_set("wan0_pppoe_ipaddr", nvram_safe_get("wan_ipaddr"));
792		nvram_set("wan0_pppoe_netmask",
793				inet_addr_(nvram_safe_get("wan_ipaddr")) &&
794				inet_addr_(nvram_safe_get("wan_netmask"))?nvram_get("wan_netmask"):NULL);
795		nvram_set("wan0_pppoe_gateway", nvram_get("wan_gateway"));
796
797		/* current interface address (dhcp + firewall) */
798		if(!skipflag)
799			nvram_set("wanx_ipaddr", nvram_safe_get("wan_ipaddr"));
800	}
801// 2007.12 James. {
802	else
803		nvram_unset("upnp_wan_proto");
804// 2007.12 James. }
805
806	nvram_set("wan0_hostname", nvram_safe_get("wan_hostname"));
807
808	if (nvram_invmatch("wan_hwaddr_x", ""))
809	{
810		nvram_set("wan_hwaddr", mac_conv("wan_hwaddr_x", -1, macbuf));
811		nvram_set("wan0_hwaddr", mac_conv("wan_hwaddr_x", -1, macbuf));
812	}
813
814	nvram_set("wan0_dnsenable_x", nvram_safe_get("wan_dnsenable_x"));
815	if(!skipflag){
816		nvram_unset("wan0_dns");
817		nvram_unset("wanx_dns");
818	}
819
820	convert_routes();
821
822	memset(servers, 0, sizeof(servers));
823
824	if (nvram_invmatch("ntp_server0", ""))
825		sprintf(servers, "%s%s ", servers, nvram_safe_get("ntp_server0"));
826	if (nvram_invmatch("ntp_server1", ""))
827		sprintf(servers, "%s%s ", servers, nvram_safe_get("ntp_server1"));
828
829	nvram_set("ntp_servers", servers);
830
831	if (nvram_match("wan_nat_x", "0") && nvram_match("wan_route_x", "IP_Bridged"))
832	{
833		memset(ifnames, 0, 36);
834		sprintf(ifnames, "%s", nvram_safe_get("lan_ifnames"));
835/*#ifndef WL500GPV2
836		sprintf(ifnames, "%s %s", ifnames, nvram_safe_get("wan_ifnames"));
837#endif	// WL500GPV2//*/
838		nvram_set("lan_ifnames_t", ifnames);
839		nvram_set("br0_ifnames", ifnames);	// 2008.06 James.
840		nvram_set("router_disable", "1");
841		nvram_set("vlan_enable", "0");
842	}
843#ifdef WIRELESS_WAN
844	else if (nvram_invmatch("wl_mode_ex", "ap")) // thanks for Oleg
845	{
846		char name[80], *next;
847
848		char *wl_ifname=nvram_safe_get("wl0_ifname");
849
850		/* remove wl_ifname from the ifnames */
851		strcpy(ifnames, nvram_safe_get("wan_ifnames"));
852		foreach(name, nvram_safe_get("lan_ifnames"), next) {
853			if (strcmp(name, wl_ifname)) {
854				sprintf(ifnames, "%s %s", ifnames, name);
855			}
856		}
857		nvram_set("lan_ifnames_t", ifnames);
858		nvram_set("br0_ifnames", ifnames);  // 2008.06 James.
859		nvram_set("router_disable", "0");
860		nvram_set("vlan_enable", "1");
861	}
862#endif	// WIRELESS_WAN
863	else
864	{
865// 2008.06 James. {
866		//nvram_set("lan_ifnames_t", nvram_safe_get("lan_ifnames"));
867		memset(ifnames, 0, sizeof(ifnames));
868		strcpy(ifnames, nvram_safe_get("lan_ifnames"));
869		nvram_set("lan_ifnames_t", ifnames);
870		nvram_set("br0_ifnames", ifnames);
871// 2008.06 James. }
872		nvram_set("router_disable", "0");
873		nvram_set("vlan_enable", "1");
874	}
875#if 0
876	if(nvram_match("vlan_enable", "0")){
877		nvram_set("vlan0ports", "0 1 2 3 4 5*");
878		nvram_set("vlan1ports", "5u");
879	}
880	else{
881		if(nvram_match("productid", "WL500gpv2")){
882			nvram_set("vlan0ports", "0 1 2 3 5*");
883			nvram_set("vlan1ports", "4 5u");
884		}
885		else{
886			nvram_set("vlan0ports", "1 2 3 4 5*");
887			nvram_set("vlan1ports", "0 5u");
888		}
889	}
890#endif
891//	if (nvram_match("ddns_enable_x", "1") && nvram_match("wan_proto", "pppoe"))
892		eval("start_ddns");
893
894
895#ifdef USB_SUPPORT
896	// clean some temp variables
897	if (!skipflag)
898	{
899		nvram_set("swap_on", "0");
900		//nvram_set("usb_device", "");
901//	nvram_set("usb_ftp_device", ""); 	// marked by Jiahao for WL500gP
902		nvram_set("usb_path", "");
903		nvram_set("usb_path1", "");	// WL-520GU
904		nvram_set("usb_path1.1", "");	// WL-500gP V2
905		nvram_set("usb_path1.2", "");	// WL-500gP V2
906		nvram_set("usb_storage_device", "");
907		nvram_set("usb_storage_device_remove", "");
908#ifdef PRINTER_SUPPORT
909		nvram_set("usb_printer_device", "");	// 2008.05 James.
910		nvram_set("usb_path_printer", "");
911#endif // PRINTER_SUPPORT
912#ifdef AUDIO_SUPPORT
913		nvram_set("usb_audio_device", "");
914		nvram_set("usb_path_audio", "");
915#endif // AUDIO_SUPPORT
916#ifdef WEBCAM_SUPPORT
917		nvram_set("usb_web_device", "");	// following lines are added by Jiahao for WL500gP
918		nvram_set("usb_path_webcam", "");
919		nvram_set("usb_webdriver_x", "");
920		nvram_set("usb_web_flag", "");
921#endif // WEBCAM_SUPPORT
922#ifdef CDMA
923		nvram_set("usb_hsdpa_device", "");
924		nvram_set("usb_path_hsdpa", "");
925#endif // CDMA
926		nvram_set("usb_disc0_path0", "");
927		nvram_set("usb_disc0_path1", "");
928		nvram_set("usb_disc0_path2", "");
929		nvram_set("usb_disc0_path3", "");
930		nvram_set("usb_disc0_path4", "");
931		nvram_set("usb_disc0_path5", "");
932		nvram_set("usb_disc0_path6", "");
933		nvram_set("usb_disc1_path0", "");
934		nvram_set("usb_disc1_path1", "");
935		nvram_set("usb_disc1_path2", "");
936		nvram_set("usb_disc1_path3", "");
937		nvram_set("usb_disc1_path4", "");
938		nvram_set("usb_disc1_path5", "");
939		nvram_set("usb_disc1_path6", "");
940		nvram_set("usb_disc0_fs_path0", "");
941		nvram_set("usb_disc0_fs_path1", "");
942		nvram_set("usb_disc0_fs_path2", "");
943		nvram_set("usb_disc0_fs_path3", "");
944		nvram_set("usb_disc0_fs_path4", "");
945		nvram_set("usb_disc0_fs_path5", "");
946		nvram_set("usb_disc0_fs_path6", "");
947		nvram_set("usb_disc1_fs_path0", "");
948		nvram_set("usb_disc1_fs_path1", "");
949		nvram_set("usb_disc1_fs_path2", "");
950		nvram_set("usb_disc1_fs_path3", "");
951		nvram_set("usb_disc1_fs_path4", "");
952		nvram_set("usb_disc1_fs_path5", "");
953		nvram_set("usb_disc1_fs_path6", "");
954		nvram_set("usb_disc0_index", "0");
955		nvram_set("usb_disc1_index", "0");
956		nvram_set("usb_disc0_port", "0");
957		nvram_set("usb_disc1_port", "0");
958		nvram_set("usb_disc0_dev", "");
959		nvram_set("usb_disc1_dev", "");
960// 2008.06 James. {
961		nvram_set("usb_disc0_pid", "");
962		nvram_set("usb_disc0_safely_removed", "0");
963		nvram_set("usb_disc1_pid", "");
964		nvram_set("usb_disc1_safely_removed", "0");
965// 2008.06 James. }
966#ifdef DLM
967// 2007.12 James. {
968		nvram_unset("ftp_running");
969		nvram_unset("samba_running");
970		nvram_unset("dms_running");
971		nvram_unset("apps_running");
972		nvram_unset("apps_installed");//*/
973		nvram_set("eject_from_web", "0");
974		//nvram_set("st_ftp_modex", nvram_get("st_ftp_mode"));
975		//nvram_set("st_samba_modex", nvram_get("st_samba_mode"));
976// 2007.12 James. }
977		nvram_set("st_samba_mode_x", "-1");
978		nvram_set("apps_dl_x", "-1");
979		//nvram_set("apps_dmsx", nvram_get("apps_dms"));  // 2008.07 James.
980		nvram_set("apps_dms_usb_port_x", "-1");
981		nvram_set("apps_dms_usb_port_x2", "-1");
982		nvram_set("apps_status_checked", "0");
983		nvram_set("apps_version", "");	// 2008.09 James. for apps_version.
984		nvram_set("usb_storage_busy", "0");
985		nvram_set("usb_storage_busy2", "0");
986		nvram_set("swapoff_failed", "0");
987#endif	// DLM
988#ifdef WCN
989		nvram_set("wcn_enable_x", nvram_get("wcn_enable"));
990#endif	// WCN
991	}
992	nvram_set("apps_dlx", nvram_get("apps_dl"));  // 2008.08 James.
993	nvram_set("apps_dmsx", nvram_get("apps_dms"));  // 2008.07 James.
994#endif	// USB_SUPPORT
995	nvram_set("no_br", "0");
996
997	if(nvram_invmatch("sp_battle_ips", "0") && !skipflag)
998	{
999		eval("insmod", "ip_nat_starcraft.o");
1000		eval("insmod", "ipt_NETMAP.o");
1001	}
1002
1003	//2005/09/22 insmod FTP module
1004	if (nvram_match("usb_ftpenable_x", "1") && atoi(nvram_get("usb_ftpport_x"))!=21)
1005	{
1006		char ports[32];
1007
1008		sprintf(ports, "ports=21,%d", atoi(nvram_get("usb_ftpport_x")));
1009		eval("insmod", "ip_conntrack_ftp.o", ports);
1010		eval("insmod", "ip_nat_ftp.o", ports);
1011	}
1012	else
1013	{
1014		eval("insmod", "ip_conntrack_ftp.o");
1015		eval("insmod", "ip_nat_ftp.o");
1016	}
1017
1018	update_lan_status(1);
1019
1020#ifdef NOIPTABLES
1021	if (nvram_match("misc_http_x", "1"))
1022	{
1023		if (nvram_invmatch("misc_httpport_x", ""))
1024			nvram_set("http_wanport", nvram_safe_get("misc_httpport_x"));
1025		else nvram_set("http_wanport", "8080");
1026	}
1027	else nvram_set("http_wanport", "");
1028
1029	if (nvram_invmatch("fw_enable_x", "0"))
1030	{
1031		nvram_set("fw_disable", "0");
1032	}
1033	else
1034	{
1035		nvram_set("fw_disable", "1");
1036	}
1037
1038	num = 0;
1039	if (nvram_match("fw_log_x", "accept") ||
1040		nvram_match("fw_log_x", "both"))
1041		num |= 2;
1042
1043	if (nvram_match("fw_log_x", "drop") ||
1044		nvram_match("fw_log_x", "both"))
1045		num |= 1;
1046
1047	sprintf(tmpstr, "%d", num);
1048	nvram_set("log_level", tmpstr);
1049#endif	// NOIPTABLES
1050
1051	// pre mapping
1052	if(nvram_match("time_zone", "KST-9KDT"))
1053		nvram_set("time_zone", "UCT-9_1");
1054	else if(nvram_match("time_zone", "RFT-9RFTDST"))
1055		nvram_set("time_zone", "UCT-9_2");
1056
1057	strcpy(tmpstr, nvram_safe_get("time_zone"));
1058	/* replace . with : */
1059	if ((ptr=strchr(tmpstr, '.'))!=NULL) *ptr = ':';
1060	/* remove *_? */
1061	if ((ptr=strchr(tmpstr, '_'))!=NULL) *ptr = 0x0;
1062
1063	// special mapping
1064	if(nvram_match("time_zone", "JST"))
1065		nvram_set("time_zone_x", "UCT-9");
1066	else if(nvram_match("time_zone", "TST-10TDT"))
1067		nvram_set("time_zone_x", "UCT-10");
1068	else if(nvram_match("time_zone", "CST-9:30CDT"))
1069		nvram_set("time_zone_x", "UCT-9:30");
1070	else nvram_set("time_zone_x", tmpstr);
1071
1072#ifdef CDMA // HSDPA {
1073	if(!skipflag)
1074	{
1075		nvram_set("hsdpa_product", "");
1076		nvram_set("hsdpa_combo", "-1");
1077		nvram_set("cdma_down", "99");
1078		nvram_unset("got_HSDPA");
1079		nvram_unset("hsdpa_ttyUSB_num");
1080	}
1081#endif // HSDPA }
1082
1083	nvram_set("reboot", "");
1084#ifdef WCN
1085	nvram_set("reboot_WCN", "");
1086#endif	// WCN
1087	dprintf("end map\n");
1088printf("*** End convert_asus_values(%d). ***\n", skipflag);
1089}
1090
1091/* This function is used to map nvram value from asus to Broadcom */
1092#if 0
1093void early_convert_asus_values()
1094{
1095	if (nvram_match ("wl_radio_power_x", "12"))
1096	{
1097		nvram_set("pa0maxpwr", "79");
1098		//nvram_set("opo","16");
1099	}
1100	else if (nvram_invmatch ("wl_radio_power_x", "12"))
1101        {
1102                nvram_set("pa0maxpwr", "78");
1103                //nvram_set("opo","8");
1104        }
1105}
1106#endif
1107
1108char *findpattern(char *target, char *pattern)
1109{
1110	char *find;
1111	int len;
1112
1113	//printf("find : %s %s\n", target, pattern);
1114
1115	if ((find=strstr(target, pattern)))
1116	{
1117		len = strlen(pattern);
1118		if (find[len]==';' || find[len]==0)
1119		{
1120			return find;
1121		}
1122	}
1123	return NULL;
1124}
1125
1126void update_lan_status(int isup)
1127{
1128	if (isup)
1129	{
1130		nvram_set("lan_ipaddr_t", nvram_safe_get("lan_ipaddr"));
1131		nvram_set("lan_netmask_t", nvram_safe_get("lan_netmask"));
1132
1133		if (nvram_match("wan_route_x", "IP_Routed"))
1134		{
1135			if (nvram_match("lan_proto", "dhcp"))
1136			{
1137				if (nvram_invmatch("dhcp_gateway_x", ""))
1138					nvram_set("lan_gateway_t", nvram_safe_get("dhcp_gateway_x"));
1139				else
1140					nvram_set("lan_gateway_t", nvram_safe_get("lan_ipaddr"));
1141			}
1142			else nvram_set("lan_gateway_t", nvram_safe_get("lan_ipaddr"));
1143		}
1144		else nvram_set("lan_gateway_t", nvram_safe_get("lan_gateway"));
1145	}
1146	else
1147	{
1148		nvram_set("lan_ipaddr_t", "");
1149		nvram_set("lan_netmask_t", "");
1150		nvram_set("lan_gateway_t", "");
1151	}
1152}
1153
1154
1155void update_wan_status(int isup)
1156{
1157	char *proto;
1158	char dns_str[64];
1159
1160	proto = nvram_safe_get("wan_proto");
1161
1162//printf("update wan status %d %s\n", isup, proto);
1163
1164	if (!strcmp(proto, "static")) nvram_set("wan_proto_t", "Static");
1165	else if (!strcmp(proto, "dhcp")) nvram_set("wan_proto_t", "Automatic IP");
1166	else if (!strcmp(proto, "pppoe")) nvram_set("wan_proto_t", "PPPoE");
1167	else if (!strcmp(proto, "pptp")) nvram_set("wan_proto_t", "PPTP");
1168	else if (!strcmp(proto, "l2tp")) nvram_set("wan_proto_t", "L2TP");
1169#ifdef BIGPOND
1170	else if (!strcmp(proto, "bigpond")) nvram_set("wan_proto_t", "BigPond");
1171#endif
1172#ifdef CDMA // HSDPA {
1173	else if (!nvram_match("hsdpa_product", "")) nvram_set("wan_proto_t", "CDMA");
1174
1175	nvram_set("hsdpa_mode_t", nvram_safe_get("hsdpa_mode"));
1176#endif // HSDPA }
1177
1178
1179	if (!isup)
1180	{
1181		nvram_set("wan_ipaddr_t", "");
1182		nvram_set("wan_netmask_t", "");
1183		nvram_set("wan_gateway_t", "");
1184		nvram_set("wan_dns_t", "");
1185		//nvram_set("wan_ifname_t", "");	// 2008.03 James.
1186		nvram_set("wan_status_t", "Disconnected");
1187	}
1188	else
1189	{
1190		nvram_set("wan_ipaddr_t", nvram_safe_get("wan0_ipaddr"));
1191		nvram_set("wan_netmask_t", nvram_safe_get("wan0_netmask"));
1192		nvram_set("wan_gateway_t", nvram_safe_get("wan0_gateway"));
1193
1194		memset(dns_str, 0, 64);
1195		if(nvram_invmatch("wan_dnsenable_x", "1"))
1196		{
1197			if(nvram_invmatch("wan_dns1_x", ""))
1198				sprintf(dns_str, "%s", nvram_safe_get("wan_dns1_x"));
1199
1200			if(nvram_invmatch("wan_dns2_x", "")){
1201				if(strlen(dns_str) > 0)
1202					sprintf(dns_str, "%s %s", dns_str, nvram_safe_get("wan_dns2_x"));
1203				else
1204					sprintf(dns_str, "%s", nvram_safe_get("wan_dns2_x"));
1205			}
1206
1207			nvram_set("wan_dns_t", dns_str);
1208		}
1209		else
1210			nvram_set("wan_dns_t", nvram_safe_get("wan0_dns"));
1211
1212		nvram_set("wan_status_t", "Connected");
1213	}
1214}
1215
1216/*
1217 * logmessage
1218 *
1219 */
1220void logmessage(char *logheader, char *fmt, ...){
1221	va_list args;
1222	char buf[512];
1223
1224	va_start(args, fmt);
1225
1226	vsnprintf(buf, sizeof(buf), fmt, args);
1227	openlog(logheader, 0, 0);
1228	syslog(0, buf);
1229	closelog();
1230	va_end(args);
1231}
1232
1233
1234/*
1235 * wanmessage
1236 *
1237 */
1238void wanmessage(char *fmt, ...)
1239{
1240  va_list args;
1241  char buf[512];
1242
1243  va_start(args, fmt);
1244  vsnprintf(buf, sizeof(buf), fmt, args);
1245  nvram_set("wan_reason_t", buf);
1246  va_end(args);
1247}
1248
1249/*
1250 * pppstatus
1251 *
1252 */
1253char *pppstatus(char *buf)
1254{
1255   FILE *fp;
1256   char sline[128], *p;
1257
1258   if ((fp=fopen("/tmp/wanstatus.log", "r")) && fgets(sline, sizeof(sline), fp))
1259   {
1260	p = strstr(sline, ",");
1261	strcpy(buf, p+1);
1262   }
1263   else
1264   {
1265	strcpy(buf, "unknown reason");
1266   }
1267   return buf;
1268}
1269
1270
1271/*
1272 * Kills process whose PID is stored in plaintext in pidfile
1273 * @param	pidfile	PID file, signal
1274 * @return	0 on success and errno on failure
1275 */
1276int
1277kill_pidfile_s(char *pidfile, int sig)
1278{
1279	FILE *fp = fopen(pidfile, "r");
1280	char buf[256];
1281	extern int errno;
1282
1283	if(fp && fgets(buf, sizeof(buf), fp)){
1284		pid_t pid = strtoul(buf, NULL, 0);
1285		fclose(fp);
1286		return kill(pid, sig);
1287  }
1288  else
1289		return errno;
1290}
1291
1292#ifdef RT2400_SUPPORT
1293void write_rt2400_conf(void)
1294{
1295
1296	FILE *fp;
1297	char *tmpstr;
1298
1299	// create hostapd.conf
1300	fp=fopen("/tmp/RT2400AP.dat","w");
1301	if (fp==NULL)
1302	{
1303		return;
1304	}
1305
1306
1307	fprintf(fp, "[Default]\n");
1308	fprintf(fp, "SSID=%s\n", nvram_safe_get("wl_ssid"));
1309	if (nvram_match("wl_channel", "0"))
1310		fprintf(fp, "Channel=6\n");
1311	else
1312		fprintf(fp, "Channel=%s\n", nvram_safe_get("wl_channel"));
1313
1314	fprintf(fp, "HIDESSID=%s\n", nvram_safe_get("wl_closed"));
1315
1316	fprintf(fp, "BeaconPeriod=%s\n", nvram_safe_get("wl_bcn"));
1317	fprintf(fp, "RTSThreshold=%s\n", nvram_safe_get("wl_rts"));
1318	fprintf(fp, "FragThreshold=%s\n", nvram_safe_get("wl_frag"));
1319
1320
1321	if (nvram_invmatch("wl_wep_x","0"))
1322	{
1323		fprintf(fp, "DefaultKeyID=%s\n", nvram_safe_get("wl_key"));
1324
1325		fprintf(fp, "Key1Type=0\n");
1326		fprintf(fp, "Key1Str=%s\n", nvram_safe_get("wl_key1"));
1327		fprintf(fp, "Key2Type=0\n");
1328		fprintf(fp, "Key2Str=%s\n", nvram_safe_get("wl_key2"));
1329		fprintf(fp, "Key3Type=0\n");
1330		fprintf(fp, "Key3Str=%s\n", nvram_safe_get("wl_key3"));
1331		fprintf(fp, "Key4Type=0\n");
1332		fprintf(fp, "Key4Str=%s\n", nvram_safe_get("wl_key4"));
1333	}
1334
1335
1336	if (nvram_match("wl_auth_mode","shared"))
1337	{
1338		fprintf(fp, "AuthMode=shared\n");
1339		fprintf(fp, "EncrypType=wep\n");
1340	}
1341	else if (nvram_match("wl_auth_mode","psk"))
1342	{
1343		fprintf(fp, "AuthMode=wpapsk\n");
1344		fprintf(fp, "EncrypType=tkip\n");
1345		fprintf(fp, "WPAPSK=%s\n", nvram_safe_get("wl_wpa_psk"));
1346		fprintf(fp, "RekeyInterval=%s\n", nvram_safe_get("wl_wpa_gtk_rekey"));
1347		fprintf(fp, "RekeyMethod=time\n");
1348	}
1349	else
1350	{
1351		fprintf(fp, "AuthMode=open\n");
1352		if (nvram_invmatch("wl_wep_x","0"))
1353			fprintf(fp, "EncrypType=wep\n");
1354		else fprintf(fp, "EncrypType=none\n");
1355	}
1356
1357	if (nvram_match("wl_macmode", "allow"))
1358	{
1359		fprintf(fp, "AclEnable=1\n");
1360	}
1361	else if (nvram_match("wl_macmode", "deny"))
1362	{
1363		fprintf(fp, "AclEnable=2\n");
1364	}
1365	else fprintf(fp, "AclEnable=0\n");
1366
1367	if (nvram_match("wl_mode_x", "0"))
1368		fprintf(fp, "WdsEnable=0\n");
1369	else fprintf(fp, "WdsEnable=1\n");
1370	fclose(fp);
1371}
1372#endif
1373