1/*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License as
4 * published by the Free Software Foundation; either version 2 of
5 * the License, or (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
15 * MA 02111-1307 USA
16 */
17
18#include <rc.h>
19#ifdef RTCONFIG_QCA
20#include <stdio.h>
21#include <fcntl.h>		//      for restore175C() from Ralink src
22#include <qca.h>
23#include <asm/byteorder.h>
24#include <bcmnvram.h>
25//#include <linux/ethtool.h>
26#include <linux/sockios.h>
27#include <net/if_arp.h>
28#include <shutils.h>
29#include <sys/signal.h>
30#include <sys/types.h>
31#include <sys/stat.h>
32#include <dirent.h>
33#include <sys/mount.h>
34#include <net/if.h>
35#include <linux/mii.h>
36//#include <linux/if.h>
37#include <iwlib.h>
38//#include <wps.h>
39//#include <stapriv.h>
40#include <shared.h>
41#include "flash_mtd.h"
42#include "ate.h"
43
44#define MAX_FRW 64
45#define MACSIZE 12
46
47#define	DEFAULT_SSID_2G	"ASUS"
48#define	DEFAULT_SSID_5G	"ASUS_5G"
49
50#define RTKSWITCH_DEV  "/dev/rtkswitch"
51
52#define LED_CONTROL(led, flag) ralink_gpio_write_bit(led, flag)
53
54#ifdef RTCONFIG_WIRELESSREPEATER
55char *wlc_nvname(char *keyword);
56#endif
57
58#if defined(RTCONFIG_QCA)|| defined(RTAC52U) || defined(RTAC51U)
59#define VHT_SUPPORT		/* 11AC */
60#endif
61
62int g_wsc_configured = 0;
63int g_isEnrollee[MAX_NR_WL_IF] = { 0, };
64
65int getCountryRegion5G(const char *countryCode, int *warning);
66
67char *get_wscd_pidfile(void)
68{
69	static char tmpstr[32] = "/var/run/wscd.pid.";
70	char wif[8];
71
72	__get_wlifname(nvram_get_int("wps_band"), 0, wif);
73	sprintf(tmpstr, "/var/run/wscd.pid.%s", wif);
74	return tmpstr;
75}
76
77char *get_wscd_pidfile_band(int wps_band)
78{
79	static char tmpstr[32] = "/var/run/wscd.pid.";
80	char wif[8];
81
82	__get_wlifname(wps_band, 0, wif);
83	sprintf(tmpstr, "/var/run/wscd.pid.%s", wif);
84	return tmpstr;
85}
86
87int get_wifname_num(char *name)
88{
89	if (strcmp(WIF_5G, name) == 0)
90		return 1;
91	else if (strcmp(WIF_2G, name) == 0)
92		return 0;
93	else
94		return -1;
95}
96
97const char *get_wifname(int band)
98{
99	if (band)
100		return WIF_5G;
101	else
102		return WIF_2G;
103}
104
105const char *get_wpsifname(void)
106{
107	int wps_band = nvram_get_int("wps_band");
108
109	if (wps_band)
110		return WIF_5G;
111	else
112		return WIF_2G;
113}
114
115#if 0
116char *get_non_wpsifname()
117{
118	int wps_band = nvram_get_int("wps_band");
119
120	if (wps_band)
121		return WIF_2G;
122	else
123		return WIF_5G;
124}
125#endif
126
127static unsigned char nibble_hex(char *c)
128{
129	int val;
130	char tmpstr[3];
131
132	tmpstr[2] = '\0';
133	memcpy(tmpstr, c, 2);
134	val = strtoul(tmpstr, NULL, 16);
135	return val;
136}
137
138static int atoh(const char *a, unsigned char *e)
139{
140	char *c = (char *)a;
141	int i = 0;
142
143	memset(e, 0, MAX_FRW);
144	for (i = 0; i < MAX_FRW; ++i, c += 3) {
145		if (!isxdigit(*c) || !isxdigit(*(c + 1)) || isxdigit(*(c + 2)))	// should be "AA:BB:CC:DD:..."
146			break;
147		e[i] = (unsigned char)nibble_hex(c);
148	}
149
150	return i;
151}
152
153char *htoa(const unsigned char *e, char *a, int len)
154{
155	char *c = a;
156	int i;
157
158	for (i = 0; i < len; i++) {
159		if (i)
160			*c++ = ':';
161		c += sprintf(c, "%02X", e[i] & 0xff);
162	}
163	return a;
164}
165
166int FREAD(unsigned int addr_sa, int len)
167{
168	unsigned char buffer[MAX_FRW];
169	char buffer_h[128];
170	memset(buffer, 0, sizeof(buffer));
171	memset(buffer_h, 0, sizeof(buffer_h));
172
173	if (FRead(buffer, addr_sa, len) < 0)
174		dbg("FREAD: Out of scope\n");
175	else {
176		if (len > MAX_FRW)
177			len = MAX_FRW;
178		htoa(buffer, buffer_h, len);
179		puts(buffer_h);
180	}
181	return 0;
182}
183
184/*
185 * 	write str_hex to offset da
186 *	console input:	FWRITE 0x45000 00:11:22:33:44:55:66:77
187 *	console output:	00:11:22:33:44:55:66:77
188 *
189 */
190int FWRITE(const char *da, const char *str_hex)
191{
192	unsigned char ee[MAX_FRW];
193	unsigned int addr_da;
194	int len;
195
196	addr_da = strtoul(da, NULL, 16);
197	if (addr_da && (len = atoh(str_hex, ee))) {
198		FWrite(ee, addr_da, len);
199		FREAD(addr_da, len);
200	}
201	return 0;
202}
203
204//End of new ATE Command
205//Ren.B
206int check_macmode(const char *str)
207{
208
209	if ((!str) || (!strcmp(str, "")) || (!strcmp(str, "disabled"))) {
210		return 0;
211	}
212
213	if (strcmp(str, "allow") == 0) {
214		return 1;
215	}
216
217	if (strcmp(str, "deny") == 0) {
218		return 2;
219	}
220	return 0;
221}
222
223//Ren.E
224
225//Ren.B
226void gen_macmode(int mac_filter[], int band, char *prefix)
227{
228	char temp[128];
229
230	sprintf(temp,"%smacmode", prefix);
231	mac_filter[0] = check_macmode(nvram_get(temp));
232	_dprintf("mac_filter[0] = %d\n", mac_filter[0]);
233}
234
235//Ren.E
236
237static inline void __choose_mrate(char *prefix, int *mcast_phy, int *mcast_mcs, int *rate)
238{
239	int phy = 3, mcs = 7;	/* HTMIX 65/150Mbps */
240	*rate=150000;
241	char tmp[128];
242
243	if (ipv6_enabled() && nvram_get_int(ipv6_nvname("ipv6_radvd"))) {
244		if (!strncmp(prefix, "wl0", 3)) {
245			phy = 2;
246			mcs = 2;	/* 2G: OFDM 12Mbps */
247			*rate=12000;
248		} else {
249			phy = 3;
250			mcs = 1;	/* 5G: HTMIX 13/30Mbps */
251			*rate=30000;
252		}
253	}
254
255	if (nvram_match(strcat_r(prefix, "nmode_x", tmp), "2") ||	/* legacy mode */
256	    strstr(nvram_safe_get(strcat_r(prefix, "crypto", tmp)), "tkip")) {	/* tkip */
257		/* In such case, choose OFDM instead of HTMIX */
258		phy = 2;
259		mcs = 4;	/* OFDM 24Mbps */
260		*rate=24000;
261	}
262
263	*mcast_phy = phy;
264	*mcast_mcs = mcs;
265}
266
267int bw40_channel_check(int band,char *ext)
268{
269   	int ch;
270 	if(!band)
271 	   	ch=nvram_get_int("wl0_channel");
272	else
273 	   	ch=nvram_get_int("wl1_channel");
274	if(ch)
275	{
276	  if(!band) //2.4G
277	  {
278		if((ch==1) ||(ch==2) ||(ch==3)||(ch==4))
279		{
280			if(!strcmp(ext,"MINUS"))
281		 	{
282				dbG("stage 1: a  mismatch between %s mode and ch %d => fix mode\n",ext,ch);
283				sprintf(ext,"PLUS");
284			}
285
286		}
287		else if(ch>=8)
288		{
289			if(!strcmp(ext,"PLUS"))
290		 	{
291				dbG("stage 2: a  mismatch between %s mode and ch %d => fix mode\n",ext,ch);
292				sprintf(ext,"MINUS");
293			}
294		}
295		//ch5,6,7:both
296	  }
297	  else //5G
298	  {
299		  if ((ch == 36) || (ch == 44) || (ch == 52) || (ch == 60) || (ch == 100)
300		     || (ch == 108) ||(ch == 116) || (ch == 124) || (ch == 132) || (ch == 149) || (ch ==157))
301		  {
302			if(!strcmp(ext,"MINUS"))
303		 	{
304				dbG("stage 1: a  mismatch between %s mode and ch %d => fix mode\n",ext,ch);
305				sprintf(ext,"PLUS");
306			}
307
308		  }
309		  else if ((ch == 40) || (ch == 48) || (ch == 56) || (ch == 64) || (ch == 104) || (ch == 112) ||
310		         (ch == 120) || (ch == 128) || (ch == 136) || (ch == 153) ||(ch == 161))
311	  	  {
312
313			if(!strcmp(ext,"PLUS"))
314		 	{
315				dbG("stage 2: a  mismatch between %s mode and ch %d => fix mode\n",ext,ch);
316				sprintf(ext,"MINUS");
317			}
318
319		  }
320
321	  }
322
323	}
324	return 1; //pass
325}
326
327
328#define MAX_NO_GUEST 3
329int gen_ath_config(int band, int is_iNIC,int subnet)
330{
331#ifdef RTCONFIG_WIRELESSREPEATER
332	FILE *fp4 = NULL;
333	char path4[50];
334#endif
335	FILE *fp, *fp2, *fp3, *fp5;
336	char *str = NULL;
337	char *str2 = NULL;
338	int i;
339	char list[2048];
340	char wds_mac[4][30];
341	int wds_keyidx;
342	char wds_key[50];
343	int flag_8021x = 0;
344	int warning = 0;
345//	int ChannelNumMax_2G = 11;
346	char tmp[128], prefix[] = "wlXXXXXXX_" ,athfix[]="athXXXX_",tmpfix[]="wlXXXXX_";
347	char temp[128], prefix_mssid[] = "wlXXXXXXXXXX_mssid_";
348	char tmpstr[128];
349	int j;
350	char *nv, *nvp, *b;
351	int mcast_phy = 0, mcast_mcs = 0;
352	int mac_filter[MAX_NO_MSSID];
353	char t_mode[30],t_bw[10],t_ext[10],mode_cmd[100];
354	int bg_prot,ban;
355	int sw_mode = nvram_get_int("sw_mode");
356	int wlc_band = nvram_get_int("wlc_band");
357	int wpapsk;
358	int val,rate,caps;
359	char wif[10], vphy[10];
360	char path1[50],path2[50],path3[50];
361	char path5[sizeof(NAWDS_SH_FMT) + 6] = "";
362	int rep_mode, nmode, shortgi, stbc;
363	char *uuid = nvram_safe_get("uuid");
364#if defined(RTCONFIG_WIFI_QCA9990_QCA9990) || defined(RTCONFIG_WIFI_QCA9994_QCA9994)
365	int sumimo, mumimo, ldpc = 1, tqam, tqam_intop;
366#endif
367#ifdef RTCONFIG_QCA_TW_AUTO_BAND4
368	unsigned char CC[3];
369#endif
370
371	rep_mode=0;
372	bg_prot=0;
373	ban=0;
374	memset(mode_cmd,0,sizeof(mode_cmd));
375	memset(t_mode,0,sizeof(t_mode));
376	memset(t_bw,0,sizeof(t_bw));
377	memset(t_ext,0,sizeof(t_ext));
378
379	__get_wlifname(band, subnet, wif);
380	strcpy(vphy, get_vphyifname(band));
381
382	sprintf(path1,"/etc/Wireless/conf/hostapd_%s.conf",wif);
383	sprintf(path2,"/etc/Wireless/sh/postwifi_%s.sh",wif);
384	sprintf(path3,"/etc/Wireless/sh/prewifi_%s.sh",wif);
385	system("mkdir -p /etc/Wireless/conf");
386	system("mkdir -p /etc/Wireless/sh");
387
388	_dprintf("gen qca config\n");
389	if (!(fp = fopen(path1, "w+")))
390		return 0;
391	if (!(fp2 = fopen(path2, "w+")))
392		return 0;
393	if (!(fp3 = fopen(path3, "w+")))
394		return 0;
395
396#ifdef RTCONFIG_WIRELESSREPEATER
397	if (sw_mode == SW_MODE_REPEATER && wlc_band == band && nvram_invmatch("wlc_ssid", "")&& subnet==0)
398	{
399	   	rep_mode=1;
400		sprintf(path4,"/etc/Wireless/conf/wpa_supplicant-sta%d.conf",band);
401		if (!(fp4 = fopen(path4, "w+")))
402			return 0;
403	}
404#endif
405
406
407	fprintf(fp, "#The word of \"Default\" must not be removed\n");
408	fprintf(fp, "#Default\n");
409
410	if(subnet==0)
411		snprintf(prefix, sizeof(prefix), "wl%d_", band);
412	else
413	{
414	   	j=0;
415	   	for(i=1;i<=MAX_NO_GUEST;i++)
416		{
417			sprintf(prefix_mssid, "wl%d.%d_", band, i);
418			if (nvram_match(strcat_r(prefix_mssid, "bss_enabled", temp), "1"))
419			{
420			   	j++;
421				if(j==subnet)
422					snprintf(prefix, sizeof(prefix), "wl%d.%d_", band,i);
423			}
424
425		}
426	}
427
428#ifdef RTCONFIG_WIRELESSREPEATER
429	if(rep_mode==1)
430	    snprintf(prefix, sizeof(prefix), "wl%d.1_", band);
431
432	if (sw_mode == SW_MODE_REPEATER && wlc_band == band && nvram_invmatch("wlc_ssid", "") && subnet ==0)
433	{
434		int flag_wep = 0;
435		int p;
436	    	snprintf(prefix_mssid, sizeof(prefix_mssid), "wl%d_", band);
437	// convert wlc_xxx to wlX_ according to wlc_band == band
438		nvram_set("ure_disable", "0");
439
440		nvram_set("wl_ssid", nvram_safe_get("wlc_ssid"));
441		nvram_set(strcat_r(prefix_mssid, "ssid", tmp), nvram_safe_get("wlc_ssid"));
442		nvram_set(strcat_r(prefix_mssid, "auth_mode_x", tmp), nvram_safe_get("wlc_auth_mode"));
443
444		nvram_set(strcat_r(prefix_mssid, "wep_x", tmp), nvram_safe_get("wlc_wep"));
445
446		nvram_set(strcat_r(prefix_mssid, "key", tmp), nvram_safe_get("wlc_key"));
447		for(p = 1; p <= 4; p++)
448		{
449			char prekey[16];
450			snprintf(prekey, sizeof(prekey), "key%d", p);
451			if(nvram_get_int("wlc_key")==p)
452				nvram_set(strcat_r(prefix_mssid, prekey, tmp), nvram_safe_get("wlc_wep_key"));
453		}
454
455		nvram_set(strcat_r(prefix_mssid, "crypto", tmp), nvram_safe_get("wlc_crypto"));
456		nvram_set(strcat_r(prefix_mssid, "wpa_psk", tmp), nvram_safe_get("wlc_wpa_psk"));
457		nvram_set(strcat_r(prefix_mssid, "bw", tmp), nvram_safe_get("wlc_nbw_cap"));
458
459
460		fprintf(fp4,
461		      "ctrl_interface=/var/run/wpa_supplicant-sta%d\n"
462		      "network={\n"
463		      		"ssid=\"%s\"\n",
464		      band,nvram_safe_get(strcat_r(prefix_mssid, "ssid", tmp)));
465
466		str = nvram_safe_get("wlc_auth_mode");
467		if (str && strlen(str))
468		{
469			if (!strcmp(str, "open") && nvram_match("wlc_wep", "0"))
470			{
471				fprintf(fp4,"       key_mgmt=NONE\n");    //open/none
472			}
473			else if (!strcmp(str, "open"))
474			{
475				flag_wep = 1;
476				fprintf(fp4,"       key_mgmt=NONE\n"); //open
477				fprintf(fp4,"       auth_alg=OPEN\n");
478			}
479			else if (!strcmp(str, "shared"))
480			{
481				flag_wep = 1;
482				fprintf(fp4,"       key_mgmt=NONE\n"); //shared
483				fprintf(fp4,"       auth_alg=SHARED\n");
484			}
485			else if (!strcmp(str, "psk") || !strcmp(str, "psk2"))
486			{
487			   	fprintf(fp4,"       key_mgmt=WPA-PSK\n");
488#if 0
489				fprintf(fp4,"       proto=RSN\n");
490#else
491				if (!strcmp(str, "psk"))
492				 	fprintf(fp4,"       proto=WPA\n");  //wpapsk
493				else
494				 	fprintf(fp4,"       proto=RSN\n");  //wpa2psk
495#endif
496				//EncrypType
497				if (nvram_match("wlc_crypto", "tkip"))
498				{
499					fprintf(fp4, "       pairwise=TKIP\n");
500					fprintf(fp4, "       group=TKIP\n");
501				}
502				else if (nvram_match("wlc_crypto", "aes"))
503				{
504					fprintf(fp4, "       pairwise=CCMP TKIP\n");
505					fprintf(fp4, "       group=CCMP TKIP\n");
506				}
507
508				//key
509				fprintf(fp4, "       psk=\"%s\"\n",nvram_safe_get("wlc_wpa_psk"));
510			}
511			else
512				fprintf(fp4,"       key_mgmt=NONE\n");    //open/none
513		}
514		else
515			fprintf(fp4,"       key_mgmt=NONE\n");   //open/none
516
517		//EncrypType
518		if (flag_wep)
519		{
520			for(p = 1 ; p <= 4; p++)
521			{
522				if(nvram_get_int("wlc_key")==p)
523				{
524				   	if((strlen(nvram_safe_get("wlc_wep_key"))==5)||(strlen(nvram_safe_get("wlc_wep_key"))==13))
525					{
526
527						fprintf(fp4, "       wep_tx_keyidx=%d\n",p-1);
528					   	fprintf(fp4, "       wep_key%d=\"%s\"\n",p-1,nvram_safe_get("wlc_wep_key"));
529
530					}
531					else if((strlen(nvram_safe_get("wlc_wep_key"))==10)||(strlen(nvram_safe_get("wlc_wep_key"))==26))
532					{
533						fprintf(fp4, "       wep_tx_keyidx=%d\n",p-1);
534					   	fprintf(fp4, "       wep_key%d=%s\n",p-1,nvram_safe_get("wlc_wep_key"));
535					}
536					else
537					{
538						fprintf(fp4, "       wep_tx_keyidx=%d\n",p-1);
539					   	fprintf(fp4, "       wep_key%d=0\n",p-1);
540					}
541
542				}
543			}
544
545		}
546		fprintf(fp4, "}\n");
547	}
548#endif	/* RTCONFIG_WIRELESSREPEATER */
549
550
551	fprintf(fp, "interface=%s\n",wif);
552
553	fprintf(fp, "ctrl_interface=/var/run/hostapd\n");
554	fprintf(fp, "dump_file=/tmp/hostapd.dump\n");
555	fprintf(fp, "driver=atheros\n");
556	fprintf(fp, "bridge=br0\n");
557	fprintf(fp, "logger_syslog=-1\n");
558	fprintf(fp, "logger_syslog_level=2\n");
559	fprintf(fp, "logger_stdout=-1\n");
560	fprintf(fp, "logger_stdout_level=2\n");
561	fprintf(fp, "ctrl_interface_group=0\n");
562	fprintf(fp, "max_num_sta=255\n");
563	fprintf(fp, "macaddr_acl=0\n");
564	fprintf(fp, "ignore_broadcast_ssid=0\n");
565
566
567	fprintf(fp, "eapol_key_index_workaround=0\n");
568	flag_8021x=0;
569
570	str = nvram_safe_get(strcat_r(prefix, "auth_mode_x", tmp));
571	if (str && (!strcmp(str, "radius") || !strcmp(str, "wpa")
572	    ||!strcmp(str, "wpa2")|| !strcmp(str, "wpawpa2")))
573	{
574	        flag_8021x=1;
575		fprintf(fp, "ieee8021x=1\n");
576		fprintf(fp, "eap_server=0\n");
577	}
578     	else
579        {
580		fprintf(fp, "ieee8021x=0\n");
581		fprintf(fp, "eap_server=1\n");
582	}
583
584
585/*
586	//CountryRegion
587	if(subnet==0)
588	{
589		str = nvram_safe_get(strcat_r(prefix, "country_code", tmp));
590		if (str && strlen(str)) {
591			int region;
592			region = getCountryRegion2G(str);
593			//printf(fp, "country_code=%d\n", region);
594			if(region==5) //default DB=US
595				fprintf(fp, "country_code=US\n");
596			else
597				fprintf(fp, "country_code=%s\n", str);
598			ChannelNumMax_2G = getChannelNumMax2G(region);
599		} else {
600			warning = 1;
601			//fprintf(fp, "country_code=%d\n", 5);
602			fprintf(fp, "country_code=US\n");
603		}
604	}
605*/
606/*
607	//CountryRegion for A band
608	str = nvram_safe_get(strcat_r(prefix, "country_code", tmp));
609	if (str && strlen(str)) {
610		int region;
611		region = getCountryRegion5G(str, &warning);
612		fprintf(fp, "CountryRegionABand=%d\n", region);
613	} else {
614		warning = 3;
615		fprintf(fp, "CountryRegionABand=%d\n", 7);
616	}
617
618	//CountryCode
619	str = nvram_safe_get(strcat_r(prefix, "country_code", tmp));
620	if (str && strlen(str)) {
621		fprintf(fp, "CountryCode=%s\n", str);
622	} else {
623		warning = 4;
624		fprintf(fp, "CountryCode=DB\n");
625	}
626*/
627	//SSID Num. [MSSID Only]
628
629	fprintf(fp,"ssid=%s\n",nvram_safe_get(strcat_r(prefix, "ssid", tmp)));
630
631	if(band)
632		fprintf(fp, "wmm_enabled=1\n");
633
634	//if(subnet==0)
635	{
636
637		snprintf(tmpfix, sizeof(tmpfix), "wl%d_", band);
638		//Network Mode
639		str = nvram_safe_get(strcat_r(tmpfix, "nmode_x", tmp));
640		//5G
641		if (band) {
642			if (str && strlen(str)) {
643				if (atoi(str) == 0)	// Auto
644				{
645#if defined(VHT_SUPPORT)
646					sprintf(t_mode,"iwpriv %s mode 11ACV" ,wif);
647#else
648					fprintf(fp, "hw_mode=a\n");
649#endif
650				} else if (atoi(str) == 1)	// N + AC
651				{
652#if defined(VHT_SUPPORT)
653					sprintf(t_mode,"iwpriv %s mode 11ACV" ,wif);
654#else
655					fprintf(fp, "hw_mode=a\n");
656#endif
657				} else if (atoi(str) == 2)	// Legacy, A & N mode
658				{
659					sprintf(t_mode,"iwpriv %s mode 11NA" ,wif);
660					//ban=1;
661				}
662				else	// A,N
663				{
664#if defined(VHT_SUPPORT)
665					sprintf(t_mode,"iwpriv %s mode 11ACV" ,wif);
666#else
667					fprintf(fp, "hw_mode=a\n");
668#endif
669				}
670			} else {
671				warning = 6;
672				fprintf(fp, "hw_mode=a\n");
673			}
674
675		}
676		else //2.4G
677		{
678			if (str && strlen(str)) {
679				if (atoi(str) == 0)	// B,G,N
680				{
681					sprintf(t_mode,"iwpriv %s mode 11NG",wif);
682			     		bg_prot=1;
683				}
684				else if (atoi(str) == 2)	// B,G
685				{
686					sprintf(t_mode,"iwpriv %s mode 11G",wif);
687				     	bg_prot=1;
688					ban=1;
689				}
690				else if (atoi(str) == 1)	// N
691					sprintf(t_mode,"iwpriv %s mode 11NG",wif);
692				else	// B,G,N
693				{
694					sprintf(t_mode,"iwpriv %s mode 11NG",wif);
695				     	bg_prot=1;
696				}
697			} else {
698				warning = 7;
699				sprintf(t_mode,"iwpriv %s 11NG",wif);
700				bg_prot=1;
701			}
702		}
703
704
705	} //subnet
706
707	nmode = nvram_get_int(strcat_r(tmpfix, "nmode_x", tmp));
708#if defined(RTCONFIG_WIFI_QCA9990_QCA9990) || defined(RTCONFIG_WIFI_QCA9994_QCA9994)
709	// 2.4GHz 256 QAM
710	if (!band) {
711		/* 256-QAM can't be enabled, if HT mode is not enabled. */
712		tqam = (nmode == 0 || nmode == 1)? !!nvram_get_int(strcat_r(tmpfix, "turbo_qam", tmp)) : 0;
713		fprintf(fp2, "iwpriv %s vht_11ng %d\n", wif, tqam);
714		tqam_intop = tqam? !!nvram_get_int(strcat_r(tmpfix, "turbo_qam_brcm_intop", tmp)) : 0;
715		fprintf(fp2, "iwpriv %s 11ngvhtintop %d\n", wif, tqam_intop);
716	}
717#endif
718
719	// Short-GI
720	shortgi = (nmode == 0 || nmode == 1)? !!nvram_get_int(strcat_r(tmpfix, "HT_GI", tmp)) : 0;
721	fprintf(fp2, "iwpriv %s shortgi %d\n", wif, shortgi);
722
723	// STBC
724	stbc = (nmode == 0 || nmode == 1)? !!nvram_get_int(strcat_r(tmpfix, "HT_STBC", tmp)) : 0;
725	fprintf(fp2, "iwpriv %s tx_stbc %d\n", wif, stbc);
726	fprintf(fp2, "iwpriv %s rx_stbc %d\n", wif, stbc);
727
728#if defined(RTCONFIG_WIFI_QCA9990_QCA9990) || defined(RTCONFIG_WIFI_QCA9994_QCA9994)
729	// TX BeamForming, must be set before association with the station.
730	sumimo = (nmode == 0 || nmode == 1)? !!nvram_get_int(strcat_r(tmpfix, "txbf", tmp)) : 0;
731	mumimo = (nmode == 0 || nmode == 1)? !!(band && nvram_get_int(strcat_r(tmpfix, "mumimo", tmp))) : 0;
732	if (repeater_mode() || mediabridge_mode()) {
733		/* STA */
734		fprintf(fp2, "iwpriv %s vhtsubfer %d\n", wif, 0);	/* Single-user beam former */
735		fprintf(fp2, "iwpriv %s vhtsubfee %d\n", wif, sumimo);	/* Single-user beam formee */
736		fprintf(fp2, "iwpriv %s vhtmubfer %d\n", wif, 0);	/* Multiple-user beam former */
737		fprintf(fp2, "iwpriv %s vhtmubfee %d\n", wif, mumimo);	/* Multiple-user beam formee */
738	} else {
739		/* AP */
740		fprintf(fp2, "iwpriv %s vhtsubfer %d\n", wif, sumimo);	/* Single-user beam former */
741		fprintf(fp2, "iwpriv %s vhtsubfee %d\n", wif, 0);	/* Single-user beam formee */
742		fprintf(fp2, "iwpriv %s vhtmubfer %d\n", wif, mumimo);	/* Multiple-user beam former */
743		fprintf(fp2, "iwpriv %s vhtmubfee %d\n", wif, 0);	/* Multiple-user beam formee */
744	}
745	fprintf(fp2, "iwpriv %s implicitbf 0\n", wif);
746
747#ifdef RTCONFIG_OPTIMIZE_XBOX
748	// LDPC
749	if (nvram_match(strcat_r(prefix, "optimizexbox", tmp), "1"))
750		ldpc = 0;
751	fprintf(fp2, "iwpriv %s ldpc %d\n", wif, ldpc);
752#endif
753#endif
754
755	//fprintf(fp2,"ifconfig %s up\n",wif);
756	fprintf(fp2,"iwpriv %s hide_ssid %d\n",wif,nvram_get_int(strcat_r(prefix, "closed", tmp)));
757	if (!nvram_get_int(strcat_r(prefix, "closed", tmp))) {
758		int n;
759		char nv[33], buf[128];
760
761		snprintf(nv, sizeof(nv), "%s", nvram_safe_get(strcat_r(prefix, "ssid",tmp)));
762		//replace SSID each char to "\char"
763		memset(buf, 0x0, sizeof(buf));
764		for (n = 0; n < strlen(nv); n++)
765			sprintf(buf, "%s\\%c", buf, nv[n]);
766		fprintf(fp2, "iwconfig %s essid %s\n", wif, buf);
767	}
768
769	if(subnet==0 && rep_mode==0 )
770	{
771		//BGProtection
772		str = nvram_safe_get(strcat_r(prefix, "gmode_protection", tmp));
773		if (str && strlen(str)) {
774			if (!strcmp(str, "auto") && (bg_prot)) //only for 2.4G
775				fprintf(fp2, "iwpriv %s protmode 1\n", wif);
776			else
777				fprintf(fp2, "iwpriv %s protmode 0\n", wif);
778		} else {
779			warning = 13;
780			fprintf(fp2, "iwpriv %s protmode 0\n", wif);
781		}
782
783
784		//TxPreamble
785		str = nvram_safe_get(strcat_r(prefix, "plcphdr", tmp));
786		if (str && strcmp(str, "long") == 0)
787			fprintf(fp2, "iwpriv %s shpreamble 0\n",wif);
788		else if (str && strcmp(str, "short") == 0)
789			fprintf(fp2, "iwpriv %s shpreamble 1\n",wif);
790		else
791			fprintf(fp2, "iwpriv %s shpreamble 0\n",wif);
792
793
794		//RTSThreshold  Default=2347
795		str = nvram_safe_get(strcat_r(prefix, "rts", tmp));
796		if (str && strlen(str))
797			fprintf(fp2, "iwconfig %s rts %d\n",wif,atoi(str));
798		else {
799			warning = 14;
800			fprintf(fp2, "iwconfig %s rts 2347\n", wif);
801		}
802
803
804		//DTIM Period
805		str = nvram_safe_get(strcat_r(prefix, "dtim", tmp));
806		if (str && strlen(str))
807			fprintf(fp2, "iwpriv %s dtim_period %d\n", wif,atoi(str));
808		else {
809			warning = 11;
810			fprintf(fp2, "iwpriv %s dtim_period 1\n", wif);
811		}
812
813		//BeaconPeriod
814		str = nvram_safe_get(strcat_r(prefix, "bcn", tmp));
815		if (str && strlen(str)) {
816			if (atoi(str) > 1000 || atoi(str) < 20) {
817				nvram_set(strcat_r(prefix, "bcn", tmp), "100");
818				fprintf(fp2, "iwpriv %s bintval 100\n",wif);
819			} else
820				fprintf(fp2, "iwpriv %s bintval %d\n",wif,atoi(str));
821		} else {
822			warning = 10;
823			fprintf(fp2, "iwpriv %s bintval 100\n",wif);
824		}
825
826		//APSDCapable
827		str = nvram_safe_get(strcat_r(prefix, "wme_apsd", tmp));
828		if (str && strlen(str))
829			fprintf(fp2, "iwpriv %s uapsd %d\n", wif,strcmp(str, "off") ? 1 : 0);
830		else {
831			warning = 18;
832			fprintf(fp2, "iwpriv %s uapsd 1\n",wif);
833		}
834
835		//TxBurst
836		str = nvram_safe_get(strcat_r(prefix, "frameburst", tmp));
837		if (str && strlen(str))
838			fprintf(fp2, "iwpriv %s burst %d\n", vphy, strcmp(str, "off") ? 1 : 0);
839		else {
840			warning = 16;
841			fprintf(fp2, "iwpriv %s burst 1\n", vphy);
842		}
843
844	}
845
846
847	if(!band) //2.4G
848		   fprintf(fp2,"iwpriv %s ap_bridge %d\n",wif,nvram_get_int("wl0_ap_isolate")?0:1);
849	else
850		   fprintf(fp2,"iwpriv %s ap_bridge %d\n",wif,nvram_get_int("wl1_ap_isolate")?0:1);
851
852
853
854
855	//AuthMode
856	memset(tmpstr, 0x0, sizeof(tmpstr));
857
858
859	str = nvram_safe_get(strcat_r(prefix, "auth_mode_x", tmp));
860
861	if (str && strlen(str)) {
862		if (!strcmp(str, "open"))
863			fprintf(fp, "auth_algs=1\n");
864		else if (!strcmp(str, "shared"))
865			fprintf(fp, "auth_algs=2\n");
866		else  //wpa/wpa2/wpa-auto-enterprise:wpa/wpa2/wpawpa2
867		      //wpa/wpa2/wpa-auto-personal:psk/psk2/pskpsk2
868			fprintf(fp, "auth_algs=1\n");
869	 }
870	 else
871	 {
872			warning = 24;
873			fprintf(fp, "auth_algs=1\n");
874	 }
875
876	//EncrypType
877	memset(tmpstr, 0x0, sizeof(tmpstr));
878
879
880	str = nvram_safe_get(strcat_r(prefix, "wpa_gtk_rekey", tmp));
881	if (str && strlen(str))
882		fprintf(fp, "wpa_group_rekey=%d\n",atoi(str));
883
884
885	sprintf(prefix_mssid, "%s", prefix);
886
887	if ((nvram_match(strcat_r(prefix_mssid, "auth_mode_x", temp), "open")
888		&& nvram_match(strcat_r(prefix_mssid, "wep_x", temp),"0")))
889			fprintf(fp, "#wpa_pairwise=\n");
890	else if ((nvram_match(strcat_r(prefix_mssid, "auth_mode_x", temp), "open")
891		&&
892		 nvram_invmatch(strcat_r(prefix_mssid, "wep_x", temp),"0"))
893		||
894		nvram_match(strcat_r(prefix_mssid, "auth_mode_x", temp), "shared")
895		||
896		nvram_match(strcat_r(prefix_mssid, "auth_mode_x", temp),"radius"))
897	   	{
898		   	//wep
899			if (nvram_match(strcat_r(prefix_mssid, "auth_mode_x", temp), "shared"))
900				fprintf(fp2, "iwpriv %s authmode 2\n", wif);
901			else
902				fprintf(fp2, "iwpriv %s authmode 1\n", wif);
903
904		   	str = nvram_safe_get(strcat_r(prefix_mssid, "key", temp));
905			sprintf(tmpstr, "%skey%s", prefix_mssid, str);
906			fprintf(fp2,"iwconfig %s key [%s]\n",wif,str); //key index
907
908			if(strlen(nvram_safe_get(tmpstr))==10 || strlen(nvram_safe_get(tmpstr))==26)
909				fprintf(fp2,"iwconfig %s key %s\n",wif,nvram_safe_get(tmpstr));
910			else if(strlen(nvram_safe_get(tmpstr))==5 || strlen(nvram_safe_get(tmpstr))==13)
911		   	   	fprintf(fp2,"iwconfig %s key \"s:%s\"\n",wif,nvram_safe_get(tmpstr));
912			else
913				fprintf(fp, "#wpa_pairwise=\n");
914		}
915		else if (nvram_match(strcat_r(prefix_mssid, "crypto", temp), "tkip"))
916		{
917		   	 if(flag_8021x)
918			 	fprintf(fp, "wpa_key_mgmt=WPA-EAP\n");
919			 else
920			 	fprintf(fp, "wpa_key_mgmt=WPA-PSK\n");
921			 fprintf(fp, "wpa_strict_rekey=1\n");
922			 fprintf(fp, "eapol_version=2\n");
923			 fprintf(fp, "wpa_pairwise=TKIP\n");
924		}
925		else if (nvram_match(strcat_r(prefix_mssid, "crypto", temp), "aes"))
926		{
927		   	 if(flag_8021x)
928			 	fprintf(fp, "wpa_key_mgmt=WPA-EAP\n");
929			 else
930			 	fprintf(fp, "wpa_key_mgmt=WPA-PSK\n");
931			 fprintf(fp, "wpa_strict_rekey=1\n");
932			 fprintf(fp, "eapol_version=2\n");
933	   	   	fprintf(fp, "wpa_pairwise=CCMP\n");
934		}
935		else if (nvram_match(strcat_r(prefix_mssid, "crypto", temp), "tkip+aes"))
936		{
937		   	 if(flag_8021x)
938			 	fprintf(fp, "wpa_key_mgmt=WPA-EAP\n");
939			 else
940			 	fprintf(fp, "wpa_key_mgmt=WPA-PSK\n");
941			 fprintf(fp, "wpa_strict_rekey=1\n");
942			 fprintf(fp, "eapol_version=2\n");
943	  	   	 fprintf(fp, "wpa_pairwise=TKIP CCMP\n");
944		}
945		else
946		{
947			warning = 25;
948			fprintf(fp, "#wpa_pairwise=\n");
949		}
950
951	wpapsk=0;
952	if ((nvram_match(strcat_r(prefix_mssid, "auth_mode_x", temp), "open")
953	     && nvram_match(strcat_r(prefix_mssid, "wep_x", temp),"0")))
954			fprintf(fp, "wpa=0\n");
955	else if ((nvram_match(strcat_r(prefix_mssid, "auth_mode_x", temp), "open")
956		  &&
957		  nvram_invmatch(strcat_r(prefix_mssid, "wep_x", temp), "0"))
958	          ||
959		  nvram_match(strcat_r(prefix_mssid, "auth_mode_x", temp),"shared")
960		  ||
961		nvram_match(strcat_r(prefix_mssid, "auth_mode_x", temp),"radius"))
962			fprintf(fp, "wpa=0\n");
963	else if (nvram_match(strcat_r(prefix_mssid, "crypto", temp), "tkip"))
964	{
965			wpapsk=1;
966			fprintf(fp, "wpa=1\n");
967	}
968	else if (nvram_match(strcat_r(prefix_mssid, "crypto", temp), "aes"))
969	{
970   			wpapsk=2;
971	 		fprintf(fp, "wpa=2\n");
972	}
973	else if (nvram_match(strcat_r(prefix_mssid, "crypto", temp), "tkip+aes"))
974	{
975	   		wpapsk=3;
976	 	  	fprintf(fp, "wpa=3\n");
977	}
978	else
979	{
980			warning = 25;
981			fprintf(fp, "wpa=0\n");
982	}
983
984	if(wpapsk!=0)
985	{
986	   	if(!flag_8021x)
987		{
988			char nv[65];
989
990			snprintf(nv, sizeof(nv), "%s", nvram_safe_get(strcat_r(prefix_mssid, "wpa_psk",temp)));
991			if (strlen(nv) == 64)
992				sprintf(tmpstr, "wpa_psk=%s\n", nv);
993			else
994				sprintf(tmpstr, "wpa_passphrase=%s\n", nv);
995			fprintf(fp, "%s", tmpstr);
996		}
997	}
998
999	//if(subnet==0)
1000	{
1001		if(!ban)
1002		{
1003			//HT_BW
1004			str = nvram_safe_get(strcat_r(tmpfix, "bw", tmp));
1005			if (sw_mode == SW_MODE_REPEATER && wlc_band == band)
1006				sprintf(t_bw,"HT40");
1007			else if (atoi(str) > 0)
1008			{
1009	   			if(strstr(t_mode,"11ACV") && (atoi(str)==3 || atoi(str)==1)) //80 BW or auto BW
1010					sprintf(t_bw,"HT80");
1011				else
1012				{
1013					sprintf(t_bw,"HT40");
1014					//ext ch
1015					str = nvram_safe_get(strcat_r(tmpfix, "nctrlsb", tmp));
1016					if(!strcmp(str,"lower"))
1017						sprintf(t_ext,"PLUS");
1018					else
1019						sprintf(t_ext,"MINUS");
1020
1021					bw40_channel_check(band,t_ext);
1022				}
1023			}
1024			else {
1025				//warning = 34;
1026				sprintf(t_bw,"HT20");
1027			}
1028		}
1029
1030		sprintf(mode_cmd,"%s%s%s",t_mode,t_bw,t_ext);
1031		fprintf(fp3,"%s\n",mode_cmd);
1032
1033		if(band) //only 5G
1034		{
1035#if defined(RTCONFIG_WIFI_QCA9557_QCA9882) || defined(RTCONFIG_QCA953X) || defined(RTCONFIG_QCA956X)
1036			if(subnet==0)
1037	  			fprintf(fp3,"iwpriv wifi1 enable_ol_stats %d\n",nvram_get_int("traffic_5g")==1?1:0);
1038#endif
1039		}
1040	}
1041
1042	val=0;
1043	if(!band)
1044	  	val=nvram_get_int("wl0_channel");
1045	else
1046		val=nvram_get_int("wl1_channel");
1047
1048
1049#ifdef RTCONFIG_QCA_TW_AUTO_BAND4
1050	if(band) //5G, flush block-channel list
1051		fprintf(fp3, "wifitool %s block_acs_channel 0\n",wif);
1052#endif
1053	if(val)
1054	{
1055		fprintf(fp3, "iwpriv wifi%d dcs_enable 0\n",band);	//not to scan and change to other channels
1056		fprintf(fp3, "iwconfig %s channel %d\n",wif,val);
1057	}
1058	else if(subnet==0)
1059	{
1060#ifdef RTCONFIG_QCA_TW_AUTO_BAND4
1061		if(band) //5G
1062		{
1063			memset(CC, 0, sizeof(CC));
1064	        	FRead(CC, OFFSET_COUNTRY_CODE, 2);
1065			//for TW, acs but skip 5G band1 & band2
1066	                if(!strcmp(CC,"TW")
1067#if defined(RTCONFIG_TCODE)
1068			  || !strncmp(nvram_safe_get("territory_code"), "TW", 2)
1069
1070#endif
1071			)
1072				fprintf(fp3, "wifitool %s block_acs_channel 36,40,44,48,52,56,60,64\n",wif);
1073		}
1074#endif
1075	   	fprintf(fp3, "iwconfig %s channel auto\n",wif);
1076	}
1077	if(!band && strstr(t_mode, "11N") != NULL) //only 2.4G && N mode is used
1078		fprintf(fp3,"iwpriv %s disablecoext %d\n",wif,nvram_get_int(strcat_r(tmpfix, "bw", tmp))==2?1:0);	// when N mode is used
1079
1080	if(rep_mode)
1081	   goto next;
1082
1083	//AccessPolicy0
1084	gen_macmode(mac_filter, band, prefix);	//Ren
1085	__get_wlifname(band, subnet, athfix);
1086	str = nvram_safe_get(strcat_r(prefix, "macmode", tmp));
1087	if (str && strlen(str)) {
1088		fprintf(fp2, "iwpriv %s maccmd 3\n", athfix); //clear acl list
1089		fprintf(fp2, "iwpriv %s maccmd %d\n", athfix, mac_filter[0]);
1090	} else {
1091		warning = 47;
1092		fprintf(fp2, "iwpriv %s maccmd 0\n",athfix); //disable acl
1093	}
1094
1095	list[0] = 0;
1096	list[1] = 0;
1097	if (nvram_invmatch(strcat_r(prefix, "macmode", tmp), "disabled")) {
1098		nv = nvp = strdup(nvram_safe_get(strcat_r(prefix, "maclist_x", tmp)));
1099		if (nv) {
1100			while ((b = strsep(&nvp, "<")) != NULL) {
1101				if (strlen(b) == 0)
1102					continue;
1103				fprintf(fp2,"iwpriv %s addmac %s\n",athfix,b);
1104			}
1105			free(nv);
1106		}
1107	}
1108
1109
1110	int WdsEnable=0;
1111        int WdsEncrypType=0;
1112	if(subnet==0)
1113	{
1114		sprintf(path5, NAWDS_SH_FMT, wif);
1115		unlink(path5);
1116
1117		fprintf(fp2,"wlanconfig %s nawds mode 0\n",wif);
1118        	//WDS Enable
1119		if (sw_mode != SW_MODE_REPEATER
1120		    && !nvram_match(strcat_r(prefix, "mode_x", tmp), "0")) {
1121
1122			if (!(fp5 = fopen(path5, "w+"))) {
1123				dbg("%s: open %s fail!\n", __func__, path5);
1124			} else {
1125				//WdsEnable
1126				str = nvram_safe_get(strcat_r(prefix, "mode_x", tmp));
1127				if (str && strlen(str)) {
1128					if ((nvram_match
1129					     (strcat_r(prefix, "auth_mode_x", tmp), "open")
1130					     ||
1131					     (nvram_match
1132					      (strcat_r(prefix, "auth_mode_x", tmp), "psk2")
1133					      && nvram_match(strcat_r(prefix, "crypto", tmp),
1134							     "aes")))
1135					 ) {
1136						if (atoi(str) == 0)
1137							WdsEnable=0;
1138						else if (atoi(str) == 1)
1139							WdsEnable=2;
1140						else if (atoi(str) == 2) {
1141							if (nvram_match(strcat_r(prefix, "wdsapply_x", tmp), "0"))
1142							WdsEnable=4;
1143						else
1144							WdsEnable=3;
1145						}
1146					 } else
1147						WdsEnable=0;
1148				} else {
1149					warning = 49;
1150					WdsEnable=0;
1151				}
1152				//WdsEncrypType
1153				if (nvram_match(strcat_r(prefix, "auth_mode_x", tmp), "open")
1154				    && nvram_match(strcat_r(prefix, "wep_x", tmp), "0"))
1155					     WdsEncrypType=0; //none
1156				else if (nvram_match
1157					 (strcat_r(prefix, "auth_mode_x", tmp), "open")
1158					 && nvram_invmatch(strcat_r(prefix, "wep_x", tmp), "0"))
1159					     WdsEncrypType=1; //wep
1160				else if (nvram_match
1161					 (strcat_r(prefix, "auth_mode_x", tmp), "psk2")
1162					 && nvram_match(strcat_r(prefix, "crypto", tmp), "aes"))
1163					     WdsEncrypType=2; //aes
1164				else
1165					     WdsEncrypType=0; //none
1166
1167				i=0;
1168
1169				wds_keyidx=0;
1170				memset(wds_key,0,sizeof(wds_key));
1171				memset(wds_mac,0,sizeof(wds_mac));
1172				if ((nvram_match(strcat_r(prefix, "mode_x", tmp), "1")
1173				     || (nvram_match(strcat_r(prefix, "mode_x", tmp), "2")
1174					 && nvram_match(strcat_r(prefix, "wdsapply_x", tmp),
1175							"1")))
1176				 &&
1177				  (nvram_match(strcat_r(prefix, "auth_mode_x", tmp), "open")
1178				 ||
1179				  (nvram_match(strcat_r(prefix, "auth_mode_x", tmp), "psk2")
1180				&& nvram_match(strcat_r(prefix, "crypto", tmp), "aes")))
1181				 ) {
1182					nv = nvp =
1183					    strdup(nvram_safe_get
1184						   (strcat_r(prefix, "wdslist", tmp)));
1185					if (nv) {
1186						while ((b = strsep(&nvp, "<")) != NULL) {
1187							if (strlen(b) == 0)
1188								continue;
1189							sprintf(wds_mac[i],"%s",b);
1190							i++;
1191						}
1192						free(nv);
1193					}
1194				}
1195
1196				if (nvram_match
1197					(strcat_r(prefix, "auth_mode_x", tmp), "open")
1198					&& nvram_invmatch(strcat_r(prefix, "wep_x", tmp),"0")) {
1199					wds_keyidx=nvram_get_int(strcat_r(prefix, "key", tmp));
1200					//sprintf(list, "wl%d_key%s", band, nvram_safe_get(strcat_r(prefix, "key", tmp)));
1201					str = strcat_r(prefix, "key", tmp);
1202					str2 = nvram_safe_get(str);
1203					sprintf(list, "%s%s", str, str2);
1204					sprintf(wds_key,"%s",nvram_safe_get(list));
1205				} else
1206				    if (nvram_match
1207					(strcat_r(prefix, "auth_mode_x", tmp), "psk2")
1208					&& nvram_match(strcat_r(prefix, "crypto", tmp),
1209						       "aes")) {
1210					wds_keyidx=nvram_get_int(strcat_r(prefix, "wpa_psk", tmp));
1211				    }
1212
1213#if 0//debug
1214				dbg("wds_keyidx: %d\n",wds_keyidx);
1215				dbg("wds_key: %s\n",wds_key);
1216				dbg("wds_mac: %s | %s | %s |%s \n",wds_mac[0],wds_mac[1],wds_mac[2],wds_mac[3]);
1217#endif
1218
1219				//5G:0x97/0x77, 2.4G:0x07
1220				if (band) {
1221					if (nvram_get_int("wl1_wds_vht") == 1) {
1222						if (strstr(mode_cmd,"VHT80") != NULL)
1223							caps = 0x97;
1224						else
1225							caps = 0x77;
1226					}
1227					else
1228						caps = 0x3;
1229				}
1230				else
1231					caps = 0x07;
1232
1233				fprintf(fp2,"iwpriv %s wds 1\n", wif);
1234				fprintf(fp2,"wlanconfig %s nawds override 1\n",wif);
1235				fprintf(fp2,"wlanconfig %s nawds defcaps 0x%x\n", wif, caps);
1236				if(WdsEnable!=2 || nvram_match(strcat_r(prefix, "wdsapply_x", tmp),"1"))
1237					fprintf(fp2,"wlanconfig %s nawds mode %d\n",wif,(WdsEnable==2)?4:3);
1238
1239				/* To increase WDS compatibility, if WDS VHT is not enabled, fallback to 11A. */
1240				if (band && !nvram_match("wl1_wds_vht", "1"))
1241					caps = 0;
1242				for(i=0;i<4;i++)
1243					if(strlen(wds_mac[i]) && nvram_match(strcat_r(prefix, "wdsapply_x", tmp),"1"))
1244						fprintf(fp5, "wlanconfig %s nawds add-repeater %s 0x%x\n", wif, wds_mac[i], caps);
1245
1246				if(WdsEncrypType==0)
1247					dbg("WDS:open/none\n");
1248				else  if(WdsEncrypType==1)
1249				{
1250					dbg("WDS:open/wep\n");
1251					fprintf(fp2,"iwconfig %s key [%d]\n",wif,wds_keyidx);
1252
1253					if(strlen(wds_key)==10 || strlen(wds_key)==26)
1254						fprintf(fp2,"iwconfig %s key %s\n",wif,wds_key);
1255					else if(strlen(wds_key)==5 || strlen(wds_key)==13)
1256						fprintf(fp2,"iwconfig %s key \"s:%s\"\n",wif,wds_key);
1257				}
1258				else
1259					dbg("WDS:unknown\n");
1260
1261				fclose(fp5);
1262			}
1263		}
1264	}
1265	if(flag_8021x)
1266	{
1267		//radius server
1268		if (!strcmp(nvram_safe_get(strcat_r(tmpfix, "radius_ipaddr", tmp)), ""))
1269			fprintf(fp, "auth_server_addr=169.254.1.1\n");
1270		else
1271			fprintf(fp, "auth_server_addr=%s\n",
1272				nvram_safe_get(strcat_r(tmpfix, "radius_ipaddr", tmp)));
1273
1274		//radius port
1275		str = nvram_safe_get(strcat_r(tmpfix, "radius_port", tmp));
1276		if (str && strlen(str))
1277			fprintf(fp, "auth_server_port=%d\n", atoi(str));
1278		else {
1279			warning = 50;
1280			fprintf(fp, "auth_server_port=1812\n");
1281		}
1282
1283		//radius key
1284
1285		str = nvram_safe_get(strcat_r(tmpfix, "radius_key", tmp));
1286		if (str && strlen(str))
1287			fprintf(fp, "auth_server_shared_secret=%s\n",
1288				nvram_safe_get(strcat_r(tmpfix, "radius_key", tmp)));
1289		else
1290		   	fprintf(fp,"#auth_server_shared_secret=\n");
1291	}
1292
1293
1294	//if(subnet==0) //for ath0 & ath1
1295	{
1296		//RadioOn
1297		str = nvram_safe_get(strcat_r(tmpfix, "radio", tmp));
1298		if (str && strlen(str)) {
1299			char *updown = atoi(str)? "up" : "down";
1300			fprintf(fp2, "ifconfig %s %s\n", vphy, updown);
1301			fprintf(fp2, "ifconfig %s %s\n", wif, updown);
1302
1303			/* Connect to peer WDS AP after VAP up */
1304			if (atoi(str) && !subnet && f_exists(path5))
1305				fprintf(fp2, "%s\n", path5);
1306		}
1307		//igmp
1308		fprintf(fp2, "iwpriv %s mcastenhance %d\n",wif,
1309			nvram_get_int(strcat_r(tmpfix, "igs", tmp)) ? 2 : 0);
1310
1311		i = nvram_get_int(strcat_r(tmpfix, "mrate_x", tmp));
1312next_mrate:
1313		switch (i++) {
1314			default:
1315			case 0:		/* Driver default setting: Disable, means automatic rate instead of fixed rate
1316				 * Please refer to #ifdef MCAST_RATE_SPECIFIC section in
1317				 * file linuxxxx/drivers/net/wireless/rtxxxx/common/mlme.c
1318				 */
1319				break;
1320			case 1:		/* Legacy CCK 1Mbps */
1321				fprintf(fp2, "iwpriv %s mcast_rate 1000\n",wif);
1322				mcast_phy = 1;
1323				break;
1324			case 2:		/* Legacy CCK 2Mbps */
1325				fprintf(fp2, "iwpriv %s mcast_rate 2000\n",wif);
1326				mcast_phy = 1;
1327				break;
1328			case 3:		/* Legacy CCK 5.5Mbps */
1329				fprintf(fp2, "iwpriv %s mcast_rate 5500\n",wif);
1330				mcast_phy = 1;
1331				break;
1332			case 4:		/* Legacy OFDM 6Mbps */
1333				fprintf(fp2, "iwpriv %s mcast_rate 6000\n",wif);
1334				break;
1335			case 5:		/* Legacy OFDM 9Mbps */
1336				fprintf(fp2, "iwpriv %s mcast_rate 9000\n",wif);
1337				break;
1338			case 6:		/* Legacy CCK 11Mbps */
1339				fprintf(fp2, "iwpriv %s mcast_rate 11000\n",wif);
1340				mcast_phy = 1;
1341				break;
1342			case 7:		/* Legacy OFDM 12Mbps */
1343				fprintf(fp2, "iwpriv %s mcast_rate 12000\n",wif);
1344				break;
1345			case 8:		/* Legacy OFDM 18Mbps */
1346				fprintf(fp2, "iwpriv %s mcast_rate 18000\n",wif);
1347				break;
1348			case 9:		/* Legacy OFDM 24Mbps */
1349				fprintf(fp2, "iwpriv %s mcast_rate 24000\n",wif);
1350				break;
1351			case 10:		/* Legacy OFDM 36Mbps */
1352				fprintf(fp2, "iwpriv %s mcast_rate 36000\n",wif);
1353				break;
1354			case 11:		/* Legacy OFDM 48Mbps */
1355				fprintf(fp2, "iwpriv %s mcast_rate 48000\n",wif);
1356				break;
1357			case 12:		/* Legacy OFDM 54Mbps */
1358				fprintf(fp2, "iwpriv %s mcast_rate 54000\n",wif);
1359				break;
1360			case 13:		/* HTMIX 130/300Mbps 2S */
1361				fprintf(fp2, "iwpriv %s mcast_rate 300000\n",wif);
1362				break;
1363			case 14:		/* HTMIX 6.5/15Mbps */
1364				fprintf(fp2, "iwpriv %s mcast_rate 15000\n",wif);
1365				break;
1366			case 15:		/* HTMIX 13/30Mbps */
1367				fprintf(fp2, "iwpriv %s mcast_rate 30000\n",wif);
1368				break;
1369			case 16:		/* HTMIX 19.5/45Mbps */
1370				fprintf(fp2, "iwpriv %s mcast_rate 45000\n",wif);
1371				break;
1372			case 17:		/* HTMIX 13/30Mbps 2S */
1373				fprintf(fp2, "iwpriv %s mcast_rate 30000\n",wif);
1374				break;
1375			case 18:		/* HTMIX 26/60Mbps 2S */
1376				fprintf(fp2, "iwpriv %s mcast_rate 60000\n",wif);
1377				break;
1378			case 19:		/* HTMIX 39/90Mbps 2S */
1379				fprintf(fp2, "iwpriv %s mcast_rate 90000\n",wif);
1380				break;
1381			case 20:
1382				/* Choose multicast rate base on mode, encryption type, and IPv6 is enabled or not. */
1383				__choose_mrate(tmpfix, &mcast_phy, &mcast_mcs, &rate);
1384				fprintf(fp2, "iwpriv %s mcast_rate %d\n",wif,rate);
1385				break;
1386			}
1387		/* No CCK for 5Ghz band */
1388		if (band && mcast_phy == 1)
1389			goto next_mrate;
1390	}
1391
1392#ifdef RTCONFIG_WPS
1393	fprintf(fp, "# Wi-Fi Protected Setup (WPS)\n");
1394
1395	if (!subnet && nvram_get_int("wps_enable")) {
1396		if (nvram_match("w_Setting", "0")) {
1397			fprintf(fp, "wps_state=1\n");
1398		} else {
1399			fprintf(fp, "wps_state=2\n");
1400			fprintf(fp, "ap_setup_locked=1\n");
1401		}
1402		if (uuid && strlen(uuid) == 36)
1403			fprintf(fp, "uuid=%s\n", uuid);
1404	} else {
1405		/* Turn off WPS on guest network. */
1406		fprintf(fp, "wps_state=0\n");
1407	}
1408
1409	fprintf(fp, "wps_independent=1\n");
1410	fprintf(fp, "device_name=ASUS Router\n");
1411	fprintf(fp, "manufacturer=ASUSTek Computer Inc.\n");
1412	fprintf(fp, "model_name=%s\n", nvram_safe_get("productid"));
1413	fprintf(fp, "model_number=123\n");	/* FIXME */
1414	fprintf(fp, "serial_number=12345\n");	/* FIXME */
1415	fprintf(fp, "device_type=6-0050F204-1\n");
1416	fprintf(fp, "config_methods=push_button display virtual_display virtual_push_button physical_push_button label\n");
1417	fprintf(fp, "pbc_in_m1=1\n");
1418	fprintf(fp, "ap_pin=%s\n", nvram_safe_get("secret_code"));
1419	fprintf(fp, "upnp_iface=br0\n");
1420	fprintf(fp, "friendly_name=WPS Access Point\n");
1421	fprintf(fp, "manufacturer_url=http://www.asus.com\n");
1422	fprintf(fp, "model_description=ASUS Router\n");
1423	fprintf(fp, "model_url=http://www.asus.com\n");
1424	//fprintf(fp, "wps_rf_bands=ag\n"); /* according to spec */
1425	fprintf(fp, "wps_rf_bands=%c\n",band?'a':'g');
1426
1427	fprintf(fp, "ieee80211w=0\n");
1428#endif
1429next:
1430	fclose(fp);
1431	fclose(fp2);
1432	fclose(fp3);
1433#ifdef RTCONFIG_WIRELESSREPEATER
1434	if (sw_mode == SW_MODE_REPEATER && wlc_band == band && nvram_invmatch("wlc_ssid", "")&& subnet==0)
1435		fclose(fp4);
1436#endif
1437	chmod(path2, 0777);	/* postwifi_athX.sh */
1438	chmod(path3, 0777);	/* prewifi_athX.sh */
1439	chmod(path5, 0777);	/* nawds_athX.sh */
1440
1441	(void) warning;
1442	return 0;
1443}
1444
1445/************************ CONSTANTS & MACROS ************************/
1446
1447/*
1448 * Constants fof WE-9->15
1449 */
1450#define IW15_MAX_FREQUENCIES	16
1451#define IW15_MAX_BITRATES	8
1452#define IW15_MAX_TXPOWER	8
1453#define IW15_MAX_ENCODING_SIZES	8
1454#define IW15_MAX_SPY		8
1455#define IW15_MAX_AP		8
1456
1457/****************************** TYPES ******************************/
1458
1459/*
1460 *	Struct iw_range up to WE-15
1461 */
1462struct iw15_range {
1463	__u32 throughput;
1464	__u32 min_nwid;
1465	__u32 max_nwid;
1466	__u16 num_channels;
1467	__u8 num_frequency;
1468	struct iw_freq freq[IW15_MAX_FREQUENCIES];
1469	__s32 sensitivity;
1470	struct iw_quality max_qual;
1471	__u8 num_bitrates;
1472	__s32 bitrate[IW15_MAX_BITRATES];
1473	__s32 min_rts;
1474	__s32 max_rts;
1475	__s32 min_frag;
1476	__s32 max_frag;
1477	__s32 min_pmp;
1478	__s32 max_pmp;
1479	__s32 min_pmt;
1480	__s32 max_pmt;
1481	__u16 pmp_flags;
1482	__u16 pmt_flags;
1483	__u16 pm_capa;
1484	__u16 encoding_size[IW15_MAX_ENCODING_SIZES];
1485	__u8 num_encoding_sizes;
1486	__u8 max_encoding_tokens;
1487	__u16 txpower_capa;
1488	__u8 num_txpower;
1489	__s32 txpower[IW15_MAX_TXPOWER];
1490	__u8 we_version_compiled;
1491	__u8 we_version_source;
1492	__u16 retry_capa;
1493	__u16 retry_flags;
1494	__u16 r_time_flags;
1495	__s32 min_retry;
1496	__s32 max_retry;
1497	__s32 min_r_time;
1498	__s32 max_r_time;
1499	struct iw_quality avg_qual;
1500};
1501
1502/*
1503 * Union for all the versions of iwrange.
1504 * Fortunately, I mostly only add fields at the end, and big-bang
1505 * reorganisations are few.
1506 */
1507union iw_range_raw {
1508	struct iw15_range range15;	/* WE 9->15 */
1509	struct iw_range range;	/* WE 16->current */
1510};
1511
1512/*
1513 * Offsets in iw_range struct
1514 */
1515#define iwr15_off(f)	( ((char *) &(((struct iw15_range *) NULL)->f)) - \
1516			  (char *) NULL)
1517#define iwr_off(f)	( ((char *) &(((struct iw_range *) NULL)->f)) - \
1518			  (char *) NULL)
1519
1520/* Disable runtime version warning in ralink_get_range_info() */
1521int iw_ignore_version_sp = 0;
1522
1523/*
1524int getSiteSurvey(int band, char *ofile)
1525=> TBD. implement it if we want to support media bridge or repeater mode
1526*/
1527
1528int __need_to_start_wps_band(char *prefix)
1529{
1530	char *p, tmp[128];
1531
1532	if (!prefix || *prefix == '\0')
1533		return 0;
1534
1535	p = nvram_safe_get(strcat_r(prefix, "auth_mode_x", tmp));
1536	if ((!strcmp(p, "open")
1537	     && !nvram_match(strcat_r(prefix, "wep_x", tmp), "0"))
1538	    || !strcmp(p, "shared") || !strcmp(p, "psk") || !strcmp(p, "wpa")
1539	    || !strcmp(p, "wpa2") || !strcmp(p, "wpawpa2")
1540	    || !strcmp(p, "radius")
1541	    || nvram_match(strcat_r(prefix, "radio", tmp), "0")
1542	    || !((nvram_get_int("sw_mode") == SW_MODE_ROUTER)
1543		 || (nvram_get_int("sw_mode") == SW_MODE_AP)))
1544		return 0;
1545
1546	return 1;
1547}
1548
1549int need_to_start_wps_band(int wps_band)
1550{
1551	int ret = 1;
1552	char prefix[] = "wlXXXXXXXXXX_";
1553
1554	switch (wps_band) {
1555	case 0:		/* fall through */
1556	case 1:
1557		snprintf(prefix, sizeof(prefix), "wl%d_", wps_band);
1558		ret = __need_to_start_wps_band(prefix);
1559		break;
1560	default:
1561		ret = 0;
1562	}
1563
1564	return ret;
1565	return 1;	/* FIXME */
1566}
1567
1568int wps_pin(int pincode)
1569{
1570	int i;
1571	char word[256], *next, ifnames[128];
1572	int wps_band = nvram_get_int("wps_band"), multiband = get_wps_multiband();
1573
1574	i = 0;
1575	strcpy(ifnames, nvram_safe_get("wl_ifnames"));
1576	foreach(word, ifnames, next) {
1577		if (i >= MAX_NR_WL_IF)
1578			break;
1579		if (!multiband && wps_band != i) {
1580			++i;
1581			continue;
1582		}
1583
1584		if (!need_to_start_wps_band(i)) {
1585			++i;
1586			continue;
1587		}
1588//              dbg("WPS: PIN\n");
1589
1590		if (pincode == 0) {
1591			;
1592		} else {
1593			doSystem("hostapd_cli -i%s wps_pin any %08d", get_wifname(i), pincode);
1594		}
1595
1596		++i;
1597	}
1598
1599	return 0;
1600}
1601
1602static int __wps_pbc(const int multiband)
1603{
1604	int i;
1605	char word[256], *next, ifnames[128];
1606	int wps_band = nvram_get_int("wps_band");
1607
1608	i = 0;
1609	strcpy(ifnames, nvram_safe_get("wl_ifnames"));
1610	foreach(word, ifnames, next) {
1611		if (i >= MAX_NR_WL_IF)
1612			break;
1613		if (!multiband && wps_band != i) {
1614			++i;
1615			continue;
1616		}
1617
1618		if (!need_to_start_wps_band(i)) {
1619			++i;
1620			continue;
1621		}
1622//              dbg("WPS: PBC\n");
1623		g_isEnrollee[i] = 1;
1624		eval("hostapd_cli", "-i", (char*)get_wifname(i), "wps_pbc");
1625		eval("hostapd_cli", "-i", (char*)get_wifname(i), "wps_ap_pin", "disable");
1626
1627		++i;
1628	}
1629
1630	return 0;
1631}
1632
1633int wps_pbc(void)
1634{
1635	return __wps_pbc(get_wps_multiband());
1636}
1637
1638int wps_pbc_both(void)
1639{
1640#if defined(RTCONFIG_WPSMULTIBAND)
1641	return __wps_pbc(1);
1642#endif
1643}
1644
1645extern void wl_default_wps(int unit);
1646
1647void __wps_oob(const int multiband)
1648{
1649	int i, wps_band = nvram_get_int("wps_band");
1650	char word[256], *next;
1651	char ifnames[128];
1652
1653	if (nvram_match("lan_ipaddr", ""))
1654		return;
1655
1656	i = 0;
1657	strcpy(ifnames, nvram_safe_get("wl_ifnames"));
1658	foreach(word, ifnames, next) {
1659		if (i >= MAX_NR_WL_IF)
1660			break;
1661		if (!multiband && wps_band != i) {
1662			++i;
1663			continue;
1664		}
1665
1666		nvram_set("w_Setting", "0");
1667		wl_default_wps(i);
1668
1669		qca_wif_up(word);
1670		g_isEnrollee[i] = 0;
1671
1672		++i;
1673	}
1674
1675#ifdef RTCONFIG_TCODE
1676	restore_defaults_wifi(0);
1677#endif
1678	nvram_commit();
1679
1680	gen_qca_wifi_cfgs();
1681}
1682
1683void wps_oob(void)
1684{
1685	__wps_oob(get_wps_multiband());
1686}
1687
1688void wps_oob_both(void)
1689{
1690#if defined(RTCONFIG_WPSMULTIBAND)
1691	__wps_oob(1);
1692#else
1693	wps_oob();
1694#endif /* RTCONFIG_WPSMULTIBAND */
1695}
1696
1697void start_wsc(void)
1698{
1699	int i;
1700	char *wps_sta_pin = nvram_safe_get("wps_sta_pin");
1701	char word[256], *next, ifnames[128];
1702	int wps_band = nvram_get_int("wps_band"), multiband = get_wps_multiband();
1703
1704	if (nvram_match("lan_ipaddr", ""))
1705		return;
1706
1707	i = 0;
1708	strcpy(ifnames, nvram_safe_get("wl_ifnames"));
1709	foreach(word, ifnames, next) {
1710		if (i >= MAX_NR_WL_IF)
1711			break;
1712		if (!multiband && wps_band != i) {
1713			++i;
1714			continue;
1715		}
1716
1717		if (!need_to_start_wps_band(i)) {
1718			++i;
1719			continue;
1720		}
1721
1722		dbg("%s: start wsc(%d)\n", __func__, i);
1723		doSystem("hostapd_cli -i%s wps_cancel", get_wifname(i));	// WPS disabled
1724
1725		if (strlen(wps_sta_pin) && strcmp(wps_sta_pin, "00000000")
1726		    && (wl_wpsPincheck(wps_sta_pin) == 0)) {
1727			dbg("WPS: PIN\n");	// PIN method
1728			g_isEnrollee[i] = 0;
1729			doSystem("hostapd_cli -i%s wps_pin any %s", get_wifname(i), wps_sta_pin);
1730		} else {
1731			dbg("WPS: PBC\n");	// PBC method
1732			g_isEnrollee[i] = 1;
1733			eval("hostapd_cli", "-i", (char*)get_wifname(i), "wps_pbc");
1734			eval("hostapd_cli", "-i", (char*)get_wifname(i), "wps_ap_pin", "disable");
1735		}
1736
1737		++i;
1738	}
1739}
1740
1741static void __stop_wsc(int multiband)
1742{
1743	int i;
1744	char word[256], *next, ifnames[128];
1745	int wps_band = nvram_get_int("wps_band");
1746
1747	i = 0;
1748	strcpy(ifnames, nvram_safe_get("wl_ifnames"));
1749	foreach(word, ifnames, next) {
1750		if (i >= MAX_NR_WL_IF)
1751			break;
1752		if (!multiband && wps_band != i) {
1753			++i;
1754			continue;
1755		}
1756		if (!need_to_start_wps_band(i)) {
1757			++i;
1758			continue;
1759		}
1760
1761		doSystem("hostapd_cli -i%s wps_cancel", get_wifname(i));	// WPS disabled
1762
1763		++i;
1764	}
1765}
1766
1767void stop_wsc(void)
1768{
1769	__stop_wsc(get_wps_multiband());
1770}
1771
1772void stop_wsc_both(void)
1773{
1774#if defined(RTCONFIG_WPSMULTIBAND)
1775	__stop_wsc(1);
1776#endif
1777}
1778
1779#ifdef RTCONFIG_WPS_ENROLLEE
1780void start_wsc_enrollee(void)
1781{
1782	int i;
1783	char word[256], *next, ifnames[128];
1784	char conf[64];
1785	FILE *fp;
1786
1787	i = 0;
1788	strcpy(ifnames, nvram_safe_get("wl_ifnames"));
1789	foreach(word, ifnames, next) {
1790		if (i >= MAX_NR_WL_IF)
1791			break;
1792
1793		dbg("%s: start wsc enrollee(%d)\n", __func__, i);
1794
1795		if (nvram_get_int("sw_mode") == SW_MODE_ROUTER
1796				|| nvram_get_int("sw_mode") == SW_MODE_AP) {
1797			sprintf(conf, "/etc/Wireless/conf/wpa_supplicant-sta%d.conf", i);
1798			if ((fp = fopen(conf, "w+")) < 0) {
1799				_dprintf("%s: Can't open %s\n", __func__, conf);
1800				continue;
1801			}
1802			fprintf(fp, "ctrl_interface=/var/run/wpa_supplicant\n");
1803			fprintf(fp, "update_config=1\n");
1804			fclose(fp);
1805
1806			doSystem("wlanconfig sta%d create wlandev wifi%d wlanmode sta nosbeacon", i, i);
1807			sleep(1);
1808			doSystem("ifconfig sta%d up", i);
1809			doSystem("wpa_supplicant -B -P /var/run/wifi-sta%d.pid -D athr -i sta%d -b br0 -c /etc/Wireless/conf/wpa_supplicant-sta%d.conf", i, i, i);
1810		}
1811
1812		doSystem("wpa_cli -i sta%d wps_pbc", i);
1813		i++;
1814	}
1815}
1816
1817void stop_wsc_enrollee(void)
1818{
1819	int i;
1820	char word[256], *next, ifnames[128];
1821	char fpath[32];
1822
1823	i = 0;
1824	strcpy(ifnames, nvram_safe_get("wl_ifnames"));
1825	foreach(word, ifnames, next) {
1826		if (i >= MAX_NR_WL_IF)
1827			break;
1828
1829		doSystem("wpa_cli -i sta%d wps_cancel", i);
1830
1831		if (nvram_get_int("sw_mode") == SW_MODE_ROUTER
1832				|| nvram_get_int("sw_mode") == SW_MODE_AP) {
1833			sprintf(fpath, "/var/run/wifi-sta%d.pid", i);
1834			kill_pidfile_tk(fpath);
1835			unlink(fpath);
1836			sprintf(fpath, "/etc/Wireless/conf/wpa_supplicant-sta%d.conf", i);
1837			unlink(fpath);
1838
1839			doSystem("ifconfig sta%d down", i);
1840			doSystem("wlanconfig sta%d destroy", i);
1841		}
1842
1843		i++;
1844	}
1845}
1846
1847#ifdef RTCONFIG_WIFI_CLONE
1848void wifi_clone(int unit)
1849{
1850	char buf[512];
1851	FILE *fp;
1852	int len;
1853	char *pt1, *pt2;
1854	char tmp[128], prefix[] = "wlXXXXXXXXXX_";
1855
1856	sprintf(buf, "/etc/Wireless/conf/wpa_supplicant-sta%d.conf", unit);
1857	snprintf(prefix, sizeof(prefix), "wl%d_", unit);
1858	fp = fopen(buf, "r");
1859	if (fp) {
1860		memset(buf, 0, sizeof(buf));
1861		len = fread(buf, 1, sizeof(buf), fp);
1862		pclose(fp);
1863		if (len > 1) {
1864			buf[len-1] = '\0';
1865			//SSID
1866			pt1 = strstr(buf, "ssid=\"");
1867			if (pt1) {
1868				pt2 = pt1 + strlen("ssid=\"");
1869				pt1 = strstr(pt2, "\"");
1870				if (pt1) {
1871					*pt1 = '\0';
1872					chomp(pt2);
1873					nvram_set(strcat_r(prefix, "ssid", tmp), pt2);
1874				}
1875			}
1876					nvram_set(strcat_r(prefix, "crypto", tmp), "aes");
1877			//PSK
1878			pt2 = pt1 + 1;
1879			pt1 = strstr(pt2, "psk=\"");
1880			if (pt1) {	//WPA2-PSK
1881				pt2 = pt1 + strlen("psk=\"");
1882				pt1 = strstr(pt2, "\"");
1883				if (pt1) {
1884					*pt1 = '\0';
1885					chomp(pt2);
1886					nvram_set(strcat_r(prefix, "wpa_psk", tmp), pt2);
1887					nvram_set(strcat_r(prefix, "auth_mode_x", tmp), "psk2");
1888					nvram_set(strcat_r(prefix, "crypto", tmp), "aes");
1889				}
1890			}
1891			else {		//OPEN
1892				nvram_set(strcat_r(prefix, "auth_mode_x", tmp), "open");
1893				nvram_set(strcat_r(prefix, "wep_x", tmp), "0");
1894			}
1895			nvram_set("x_Setting", "1");
1896			nvram_commit();
1897		}
1898	}
1899}
1900#endif
1901
1902char *getWscStatus_enrollee(int unit)
1903{
1904	char buf[512];
1905	FILE *fp;
1906	int len;
1907	char *pt1, *pt2;
1908
1909	sprintf(buf, "wpa_cli -i sta%d status", unit);
1910	fp = popen(buf, "r");
1911	if (fp) {
1912		memset(buf, 0, sizeof(buf));
1913		len = fread(buf, 1, sizeof(buf), fp);
1914		pclose(fp);
1915		if (len > 1) {
1916			buf[len-1] = '\0';
1917			pt1 = strstr(buf, "wpa_state=");
1918			if (pt1) {
1919				pt2 = pt1 + strlen("wpa_state=");
1920				pt1 = strstr(pt2, "address=");
1921				if (pt1) {
1922					*pt1 = '\0';
1923					chomp(pt2);
1924				}
1925				return pt2;
1926			}
1927		}
1928	}
1929
1930	return "";
1931}
1932#endif
1933
1934char *getWscStatus(int unit)
1935{
1936	char buf[512];
1937	FILE *fp;
1938	int len;
1939	char *pt1,*pt2;
1940
1941	sprintf(buf, "hostapd_cli -i%s wps_get_status", get_wifname(unit));
1942	fp = popen(buf, "r");
1943	if (fp) {
1944		memset(buf, 0, sizeof(buf));
1945		len = fread(buf, 1, sizeof(buf), fp);
1946		pclose(fp);
1947		if (len > 1) {
1948			buf[len-1] = '\0';
1949			pt1 = strstr(buf, "Last WPS result: ");
1950			if (pt1) {
1951				pt2 = pt1 + strlen("Last WPS result: ");
1952				pt1 = strstr(pt2, "Peer Address: ");
1953				if (pt1) {
1954					*pt1 = '\0';
1955					chomp(pt2);
1956				}
1957				return pt2;
1958			}
1959		}
1960	}
1961
1962	return "";	/* FIXME */
1963}
1964
1965void wsc_user_commit(void)
1966{
1967}
1968
1969
1970void Get_fail_log(char *buf, int size, unsigned int offset)
1971{
1972	struct FAIL_LOG fail_log, *log = &fail_log;
1973	char *p = buf;
1974	int x, y;
1975
1976	memset(buf, 0, size);
1977	FRead((char *)&fail_log, offset, sizeof(fail_log));
1978	if (log->num == 0 || log->num > FAIL_LOG_MAX) {
1979		return;
1980	}
1981	for (x = 0; x < (FAIL_LOG_MAX >> 3); x++) {
1982		for (y = 0; log->bits[x] != 0 && y < 7; y++) {
1983			if (log->bits[x] & (1 << y)) {
1984				p += snprintf(p, size - (p - buf), "%d,",
1985					      (x << 3) + y);
1986			}
1987		}
1988	}
1989}
1990
1991
1992void ate_commit_bootlog(char *err_code)
1993{
1994	unsigned char fail_buffer[OFFSET_SERIAL_NUMBER - OFFSET_FAIL_RET];
1995
1996	nvram_set("Ate_power_on_off_enable", err_code);
1997	nvram_commit();
1998
1999	memset(fail_buffer, 0, sizeof(fail_buffer));
2000	strncpy(fail_buffer, err_code,
2001		OFFSET_FAIL_BOOT_LOG - OFFSET_FAIL_RET - 1);
2002	Gen_fail_log(nvram_get("Ate_reboot_log"),
2003		     nvram_get_int("Ate_boot_check"),
2004		     (struct FAIL_LOG *)&fail_buffer[OFFSET_FAIL_BOOT_LOG -
2005						     OFFSET_FAIL_RET]);
2006	Gen_fail_log(nvram_get("Ate_dev_log"), nvram_get_int("Ate_boot_check"),
2007		     (struct FAIL_LOG *)&fail_buffer[OFFSET_FAIL_DEV_LOG -
2008						     OFFSET_FAIL_RET]);
2009
2010	FWrite(fail_buffer, OFFSET_FAIL_RET, sizeof(fail_buffer));
2011}
2012#endif  //RTCONFIG_QCA
2013
2014#ifdef RTCONFIG_USER_LOW_RSSI
2015typedef struct _WLANCONFIG_LIST {
2016         char addr[18];
2017         unsigned int aid;
2018         unsigned int chan;
2019         char txrate[6];
2020         char rxrate[6];
2021         unsigned int rssi;
2022         unsigned int idle;
2023         unsigned int txseq;
2024         unsigned int rcseq;
2025         char caps[12];
2026         char acaps[10];
2027         char erp[7];
2028         char state_maxrate[20];
2029         char wps[4];
2030         char rsn[4];
2031         char wme[4];
2032         char mode[31];
2033} WLANCONFIG_LIST;
2034
2035void rssi_check_unit(int unit)
2036{
2037	#define STA_LOW_RSSI_PATH "/tmp/low_rssi"
2038   	int rssi_th;
2039	FILE *fp;
2040	char line_buf[300],cmd[300],tmp[128],wif[8]; // max 14x
2041	char prefix[] = "wlXXXXXXXXXX_";
2042	WLANCONFIG_LIST *result;
2043
2044	snprintf(prefix, sizeof(prefix), "wl%d_", unit);
2045	if (!(rssi_th= nvram_get_int(strcat_r(prefix, "user_rssi", tmp))))
2046		return;
2047
2048	result=malloc(sizeof(WLANCONFIG_LIST));
2049	memset(result, 0, sizeof(WLANCONFIG_LIST));
2050	__get_wlifname(unit, 0, wif);
2051	doSystem("wlanconfig %s list > %s", wif, STA_LOW_RSSI_PATH);
2052	fp = fopen(STA_LOW_RSSI_PATH, "r");
2053		if (fp) {
2054			//fseek(fp, 131, SEEK_SET);	// ignore header
2055			fgets(line_buf, sizeof(line_buf), fp); // ignore header
2056			while ( fgets(line_buf, sizeof(line_buf), fp) ) {
2057				sscanf(line_buf, "%s%u%u%s%s%u%u%u%u%s%s%s%s%s%s%s%s",
2058							result->addr,
2059							&result->aid,
2060							&result->chan,
2061							result->txrate,
2062							result->rxrate,
2063							&result->rssi,
2064							&result->idle,
2065							&result->txseq,
2066							&result->rcseq,
2067							result->caps,
2068							result->acaps,
2069							result->erp,
2070							result->state_maxrate,
2071							result->wps,
2072							result->rsn,
2073							result->wme,
2074							result->mode);
2075
2076#if 0
2077				dbg("[%s][%u][%u][%s][%s][%u][%u][%u][%u][%s][%s][%s][%s][%s][%s][%s]\n",
2078					result->addr,
2079					result->aid,
2080					result->chan,
2081					result->txrate,
2082					result->rxrate,
2083					result->rssi,
2084					result->idle,
2085					result->txseq,
2086					result->rcseq,
2087					result->caps,
2088					result->acaps,
2089					result->erp,
2090					result->state_maxrate,
2091					result->wps,
2092					result->rsn,
2093					result->wme);
2094#endif
2095				if(rssi_th>-result->rssi)
2096				{
2097				    	memset(cmd,0,sizeof(cmd));
2098					sprintf(cmd,"iwpriv %s kickmac %s", wif, result->addr);
2099					doSystem(cmd);
2100					dbg("=====>Roaming with %s:Disconnect Station: %s  RSSI: %d\n",
2101						wif, result->addr,-result->rssi);
2102				}
2103			}
2104			free(result);
2105			fclose(fp);
2106			unlink(STA_LOW_RSSI_PATH);
2107		}
2108}
2109#endif
2110
2111void platform_start_ate_mode(void)
2112{
2113	int model = get_model();
2114
2115	switch (model) {
2116#if defined(RTAC55U) || defined(RTAC55UHP)
2117	case MODEL_RTAC55U:
2118	case MODEL_RTAC55UHP:
2119		gpio_dir(13, GPIO_DIR_OUT);	/* Configure 2G LED as GPIO */
2120#ifndef RTCONFIG_ATEUSB3_FORCE
2121		// this way is unstable
2122		if(nvram_get_int("usb_usb3") == 0) {
2123			eval("ejusb", "-1");
2124			modprobe_r(USBOHCI_MOD);
2125			modprobe_r(USB20_MOD);
2126#ifdef USB30_MOD
2127			modprobe_r(USB30_MOD);
2128#endif
2129			nvram_set("xhci_ports", "2-1");
2130			nvram_set("ehci_ports", "1-1 3-1");
2131			nvram_set("ohci_ports", "1-1 4-1");
2132			modprobe(USB20_MOD);
2133			modprobe(USBOHCI_MOD);
2134#ifdef USB30_MOD
2135			modprobe(USB30_MOD, "u3intf=1");
2136#endif
2137		}
2138#endif
2139		break;
2140#endif	/* RTAC55U | RTAC55UHP */
2141
2142#ifdef RT4GAC55U
2143	case MODEL_RT4GAC55U:
2144		break;
2145#endif	/* RT4GAC55U */
2146
2147	default:
2148		_dprintf("%s: model %d\n", __func__, model);
2149	}
2150}
2151
2152
2153#define target 9
2154char str[target][40]={"Address:","ESSID:","Frequency:","Quality=","Encryption key:","IE:","Authentication Suites","Pairwise Ciphers","phy_mode="};
2155int
2156getSiteSurvey(int band,char* ofile)
2157{
2158   	int apCount=0;
2159	char header[128];
2160	char tmp[128], prefix[] = "wlXXXXXXXXXX_";
2161	char cmd[300];
2162	FILE *fp,*ofp;
2163	char buf[target][200],set_flag[target];
2164	int i;
2165	char *pt1,*pt2;
2166	char a1[10],a2[10];
2167	char ssid_str[256];
2168	char ch[4],ssid[33],address[18],enc[9],auth[16],sig[9],wmode[8];
2169	int  lock;
2170	char ure_mac[18];
2171	int wl_authorized = 0;
2172//////
2173	int is_ready;
2174	char temp1[200];
2175	char prefix_header[]="Cell xx - Address:";
2176/////
2177	dbG("site survey...\n");
2178	lock = file_lock("sitesurvey");
2179	system("rm -f /tmp/apscan_wlist");
2180	snprintf(prefix, sizeof(prefix), "wl%d_", band);
2181	sprintf(cmd,"iwlist %s scanning >> /tmp/apscan_wlist",nvram_safe_get(strcat_r(prefix, "ifname", tmp)));
2182	ifconfig(nvram_safe_get(strcat_r(prefix, "ifname", tmp)), IFUP, NULL, NULL);
2183	system(cmd);
2184	file_unlock(lock);
2185
2186	if((fp= fopen("/tmp/apscan_wlist", "r"))==NULL)
2187	   return 0;
2188
2189	memset(header, 0, sizeof(header));
2190	sprintf(header, "%-4s%-33s%-18s%-9s%-16s%-9s%-8s\n", "Ch", "SSID", "BSSID", "Enc", "Auth", "Siganl(%)", "W-Mode");
2191
2192	dbg("\n%s", header);
2193
2194	if ((ofp = fopen(ofile, "a")) == NULL)
2195	{
2196	   fclose(fp);
2197	   return 0;
2198	}
2199
2200	apCount=1;
2201	while(1)
2202	{
2203	   	is_ready=0;
2204		memset(set_flag,0,sizeof(set_flag));
2205		memset(buf,0,sizeof(buf));
2206		memset(temp1,0,sizeof(temp1));
2207		snprintf(prefix_header, sizeof(prefix_header), "Cell %02d - Address:",apCount);
2208
2209  		if(feof(fp))
2210		   break;
2211
2212		while(fgets(temp1,sizeof(temp1),fp))
2213		{
2214			if(strstr(temp1,prefix_header)!=NULL)
2215			{
2216				if(is_ready)
2217				{
2218					fseek(fp,-sizeof(temp1), SEEK_CUR);
2219					break;
2220				}
2221				else
2222			   	{
2223					is_ready=1;
2224					snprintf(prefix_header, sizeof(prefix_header),"Cell %02d - Address:",apCount+1);
2225				}
2226			}
2227			if(is_ready)
2228	   		{
2229				for(i=0;i<target;i++)
2230				{
2231					if(strstr(temp1,str[i])!=NULL && set_flag[i]==0)
2232				  	{
2233						set_flag[i]=1;
2234					     	memcpy(buf[i],temp1,sizeof(temp1));
2235						break;
2236					}
2237				}
2238			}
2239
2240		}
2241
2242
2243		dbg("\napCount=%d\n",apCount);
2244		apCount++;
2245
2246		//ch
2247	        pt1 = strstr(buf[2], "Channel ");
2248		if(pt1)
2249		{
2250
2251			pt2 = strstr(pt1,")");
2252		   	memset(ch,0,sizeof(ch));
2253			strncpy(ch,pt1+strlen("Channel "),pt2-pt1-strlen("Channel "));
2254		}
2255
2256		//ssid
2257	        pt1 = strstr(buf[1], "ESSID:");
2258		if(pt1)
2259		{
2260		   	memset(ssid,0,sizeof(ssid));
2261			strncpy(ssid,pt1+strlen("ESSID:")+1,strlen(buf[1])-2-(pt1+strlen("ESSID:")+1-buf[1]));
2262		}
2263
2264
2265		//bssid
2266	        pt1 = strstr(buf[0], "Address: ");
2267		if(pt1)
2268		{
2269		   	memset(address,0,sizeof(address));
2270			strncpy(address,pt1+strlen("Address: "),strlen(buf[0])-(pt1+strlen("Address: ")-buf[0])-1);
2271		}
2272
2273
2274		//enc
2275		pt1=strstr(buf[4],"Encryption key:");
2276		if(pt1)
2277		{
2278			if(strstr(pt1+strlen("Encryption key:"),"on"))
2279			{
2280				pt2=strstr(buf[7],"Pairwise Ciphers");
2281				if(pt2)
2282	  			{
2283					if(strstr(pt2,"CCMP TKIP") || strstr(pt2,"TKIP CCMP"))
2284				   		sprintf(enc,"TKIP+AES");
2285					else if(strstr(pt2,"CCMP"))
2286					   	sprintf(enc,"AES");
2287					else
2288					   	sprintf(enc,"TKIP");
2289				}
2290				else
2291					sprintf(enc,"WEP");
2292			}
2293			else
2294				sprintf(enc,"NONE");
2295		}
2296
2297
2298		//auth
2299		memset(auth,0,sizeof(auth));
2300		pt1=strstr(buf[5],"IE:");
2301		if(pt1 && strstr(buf[5],"Unknown")==NULL)
2302		{
2303			if(strstr(pt1+strlen("IE:"),"WPA2")!=NULL)
2304		   		sprintf(auth,"WPA2-");
2305			else if(strstr(pt1+strlen("IE:"),"WPA")!=NULL)
2306		   		sprintf(auth,"WPA-");
2307
2308			pt2=strstr(buf[6],"Authentication Suites");
2309			if(pt2)
2310	  		{
2311				if(strstr(pt2+strlen("Authentication Suites"),"PSK")!=NULL)
2312			   		strcat(auth,"Personal");
2313				else //802.1x
2314				   	strcat(auth,"Enterprise");
2315			}
2316		}
2317		else
2318		   	sprintf(auth,"Open System");
2319
2320		//sig
2321	        pt1 = strstr(buf[3], "Quality=");
2322		pt2 = strstr(pt1,"/");
2323		if(pt1 && pt2)
2324		{
2325			memset(sig,0,sizeof(sig));
2326			memset(a1,0,sizeof(a1));
2327			memset(a2,0,sizeof(a2));
2328			strncpy(a1,pt1+strlen("Quality="),pt2-pt1-strlen("Quality="));
2329			strncpy(a2,pt2+1,strstr(pt2," ")-(pt2+1));
2330			sprintf(sig,"%d",100*(atoi(a1)+6)/(atoi(a2)+6));
2331
2332		}
2333
2334		//wmode
2335		memset(wmode,0,sizeof(wmode));
2336		pt1=strstr(buf[8],"phy_mode=");
2337		if(pt1)
2338		{
2339
2340			if((pt2=strstr(pt1+strlen("phy_mode="),"IEEE80211_MODE_11AC_VHT"))!=NULL)
2341		   	   	sprintf(wmode,"ac");
2342			else if((pt2=strstr(pt1+strlen("phy_mode="),"IEEE80211_MODE_11A"))!=NULL
2343			        || (pt2=strstr(pt1+strlen("phy_mode="),"IEEE80211_MODE_TURBO_A"))!=NULL)
2344				sprintf(wmode,"a");
2345			else if((pt2=strstr(pt1+strlen("phy_mode="),"IEEE80211_MODE_11B"))!=NULL)
2346				sprintf(wmode,"b");
2347			else if((pt2=strstr(pt1+strlen("phy_mode="),"IEEE80211_MODE_11G"))!=NULL
2348			        || (pt2=strstr(pt1+strlen("phy_mode="),"IEEE80211_MODE_TURBO_G"))!=NULL)
2349				sprintf(wmode,"bg");
2350			else if((pt2=strstr(pt1+strlen("phy_mode="),"IEEE80211_MODE_11NA"))!=NULL)
2351		   	   	sprintf(wmode,"an");
2352			else if(strstr(pt1+strlen("phy_mode="),"IEEE80211_MODE_11NG"))
2353		   	   	sprintf(wmode,"bgn");
2354		}
2355		else
2356		   	sprintf(wmode,"unknown");
2357
2358#if 1
2359		dbg("%-4s%-33s%-18s%-9s%-16s%-9s%-8s\n",ch,ssid,address,enc,auth,sig,wmode);
2360#endif
2361
2362//////
2363		if(atoi(ch)<0)
2364			fprintf(ofp, "\"ERR_BNAD\",");
2365		else if(atoi(ch)>0 && atoi(ch)<14)
2366			fprintf(ofp, "\"2G\",");
2367		else if(atoi(ch)>14 && atoi(ch)<166)
2368			fprintf(ofp, "\"5G\",");
2369		else
2370			fprintf(ofp, "\"ERR_BNAD\",");
2371
2372
2373		memset(ssid_str, 0, sizeof(ssid_str));
2374		char_to_ascii(ssid_str, trim_r(ssid));
2375
2376		if(strlen(ssid)==0)
2377			fprintf(ofp, "\"\",");
2378		else
2379			fprintf(ofp, "\"%s\",", ssid_str);
2380
2381		fprintf(ofp, "\"%d\",", atoi(ch));
2382
2383		fprintf(ofp, "\"%s\",",auth);
2384
2385		fprintf(ofp, "\"%s\",", enc);
2386
2387		fprintf(ofp, "\"%d\",", atoi(sig));
2388
2389		fprintf(ofp, "\"%s\",", address);
2390
2391		fprintf(ofp, "\"%s\",", wmode);
2392
2393#ifdef RTCONFIG_WIRELESSREPEATER
2394		//memset(ure_mac, 0x0, 18);
2395		//sprintf(ure_mac, "%02X:%02X:%02X:%02X:%02X:%02X",xxxx);
2396		if (strcmp(nvram_safe_get(wlc_nvname("ssid")), ssid)){
2397			if (strcmp(ssid, ""))
2398				fprintf(ofp, "\"%s\"", "0");				// none
2399			else if (!strcmp(ure_mac, address)){
2400				// hidden AP (null SSID)
2401				if (strstr(nvram_safe_get(wlc_nvname("akm")), "psk")!=NULL){
2402					if (wl_authorized){
2403						// in profile, connected
2404						fprintf(ofp, "\"%s\"", "4");
2405					}else{
2406						// in profile, connecting
2407						fprintf(ofp, "\"%s\"", "5");
2408					}
2409				}else{
2410					// in profile, connected
2411					fprintf(ofp, "\"%s\"", "4");
2412				}
2413			}else{
2414				// hidden AP (null SSID)
2415				fprintf(ofp, "\"%s\"", "0");				// none
2416			}
2417		}else if (!strcmp(nvram_safe_get(wlc_nvname("ssid")), ssid)){
2418			if (!strlen(ure_mac)){
2419				// in profile, disconnected
2420				fprintf(ofp, "\"%s\",", "1");
2421			}else if (!strcmp(ure_mac, address)){
2422				if (strstr(nvram_safe_get(wlc_nvname("akm")), "psk")!=NULL){
2423					if (wl_authorized){
2424						// in profile, connected
2425						fprintf(ofp, "\"%s\"", "2");
2426					}else{
2427						// in profile, connecting
2428						fprintf(ofp, "\"%s\"", "3");
2429					}
2430				}else{
2431					// in profile, connected
2432					fprintf(ofp, "\"%s\"", "2");
2433				}
2434			}else{
2435				fprintf(ofp, "\"%s\"", "0");				// impossible...
2436			}
2437		}else{
2438			// wl0_ssid is empty
2439			fprintf(ofp, "\"%s\"", "0");
2440		}
2441#else
2442		fprintf(ofp, "\"%s\"", "0");
2443#endif
2444		fprintf(ofp, "\n");
2445
2446//////
2447
2448	}
2449
2450	fclose(fp);
2451	fclose(ofp);
2452	return 1;
2453}
2454
2455
2456#ifdef RTCONFIG_WIRELESSREPEATER
2457char *wlc_nvname(char *keyword)
2458{
2459	return(wl_nvname(keyword, nvram_get_int("wlc_band"), -1));
2460}
2461#endif
2462
2463char *getStaMAC(void)
2464{
2465	char buf[512];
2466	FILE *fp;
2467	int len,unit;
2468	char *pt1,*pt2;
2469	unit=nvram_get_int("wlc_band");
2470
2471	sprintf(buf, "ifconfig sta%d", unit);
2472
2473	fp = popen(buf, "r");
2474	if (fp) {
2475		memset(buf, 0, sizeof(buf));
2476		len = fread(buf, 1, sizeof(buf), fp);
2477		pclose(fp);
2478		if (len > 1) {
2479			buf[len-1] = '\0';
2480			pt1 = strstr(buf, "HWaddr ");
2481			if (pt1)
2482			{
2483				pt2 = pt1 + strlen("HWaddr ");
2484				chomp(pt2);
2485				return pt2;
2486			}
2487		}
2488	}
2489	return NULL;
2490}
2491
2492
2493unsigned int getPapState(int unit)
2494{
2495	char buf[8192];
2496	FILE *fp;
2497	int len;
2498	char *pt1, *pt2;
2499	sprintf(buf, "iwconfig sta%d", unit);
2500	fp = popen(buf, "r");
2501	if (fp) {
2502		memset(buf, 0, sizeof(buf));
2503		len = fread(buf, 1, sizeof(buf), fp);
2504		pclose(fp);
2505		if (len > 1) {
2506			buf[len-1] = '\0';
2507			pt1 = strstr(buf, "Access Point:");
2508			if (pt1) {
2509				pt2 = pt1 + strlen("Access Point:");
2510				pt1 = strstr(pt2, "Not-Associated");
2511				if (pt1)
2512				{
2513					sprintf(buf, "ifconfig | grep sta%d", unit);
2514				     	fp = popen(buf, "r");
2515					if(fp)
2516				   	{
2517						 memset(buf, 0, sizeof(buf));
2518						 len = fread(buf, 1, sizeof(buf), fp);
2519						 pclose(fp);
2520						 if(len>=1)
2521						    return 0;
2522						 else
2523						    return 3;
2524					}
2525				     	else
2526				   		return 0; //init
2527				}
2528				else
2529				   	return 2; //connect and auth ?????
2530
2531				}
2532			}
2533		}
2534
2535	return 3; // stop
2536}
2537
2538// TODO: wlcconnect_main
2539// TODO: wlcconnect_main
2540//	wireless ap monitor to connect to ap
2541//	when wlc_list, then connect to it according to priority
2542#define FIND_CHANNEL_INTERVAL	15
2543int wlcconnect_core(void)
2544{
2545   int unit,ret;
2546   unit=nvram_get_int("wlc_band");
2547   ret=getPapState(unit);
2548   if(ret!=2) //connected
2549   	dbG("check..wlconnect=%d \n",ret);
2550   return ret;
2551}
2552
2553
2554int wlcscan_core(char *ofile, char *wif)
2555{
2556	int ret,count;
2557
2558	count=0;
2559
2560	while((ret=getSiteSurvey(get_wifname_num(wif),ofile)==0)&& count++ < 2)
2561	{
2562		 dbg("[rc] set scan results command failed, retry %d\n", count);
2563		 sleep(1);
2564	}
2565
2566	return 0;
2567}
2568
2569