blob: 02d63282960d268ce81865539f8cf8c07fef904a [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001/*
2
3 *
4 */
5
6
7/* System include files */
8#include <string.h>
9#include <sys/types.h>
10#include <sys/stat.h>
11#include <fcntl.h>
12#include <unistd.h>
13#include <string.h>
14#include <stdio.h>
15#include <arpa/inet.h>
16#include <netinet/in.h>
17#include <stdlib.h>
18#include <ctype.h>
19#include <regex.h>
20#include <linux/wireless.h>
21#include <linux/sockios.h>
22#include <sys/ioctl.h>
23
24#include "rtk_arch.h"
25
26char *ConfigFilePath=0;
27
28#define WLAN_FAST_INIT
29#define BR_SHORTCUT
30#define NUM_WLAN_INTERFACE 1
31#define DEFAULT_CONFIG_PATH (WIFI_CONFIG_ROOT_DIR "/wlan0/")
32#define PIN_LEN 8
33typedef enum { BANDMODE2G=0, BANDMODE5G=1, BANDMODEBOTH=2, BANDMODESINGLE=3 } WLANBAND2G5GMODE_TYPE_T;
34typedef enum { AP_MODE=0, CLIENT_MODE=1, WDS_MODE=2, AP_WDS_MODE=3 } WLAN_MODE_T;
35typedef enum { ENCRYPT_DISABLED=0, ENCRYPT_WEP=1, ENCRYPT_WPA=2, ENCRYPT_WPA2=4, ENCRYPT_WPA2_MIXED=6 ,ENCRYPT_WAPI=7} ENCRYPT_T;
36typedef enum { WEP_DISABLED=0, WEP64=1, WEP128=2 } WEP_T;
37typedef enum { KEY_ASCII=0, KEY_HEX=1} KEY_TYPE_T;
38typedef enum { WPA_AUTH_AUTO=1, WPA_AUTH_PSK=2 } WPA_AUTH_T;
39typedef enum { WPA_CIPHER_TKIP=1, WPA_CIPHER_AES=2, WPA_CIPHER_MIXED=3 } WPA_CIPHER_T;
40enum { WSC_AUTH_OPEN=1, WSC_AUTH_WPAPSK=2, WSC_AUTH_SHARED=4, WSC_AUTH_WPA=8, WSC_AUTH_WPA2=0x10, WSC_AUTH_WPA2PSK=0x20, WSC_AUTH_WPA2PSKMIXED=0x22 };
41enum { WSC_ENCRYPT_NONE=1, WSC_ENCRYPT_WEP=2, WSC_ENCRYPT_TKIP=4, WSC_ENCRYPT_AES=8, WSC_ENCRYPT_TKIPAES=12 };
42enum { CONFIG_METHOD_ETH=0x2, CONFIG_METHOD_PIN=0x4, CONFIG_METHOD_PBC=0x80 };
43enum { CONFIG_BY_INTERNAL_REGISTRAR=1, CONFIG_BY_EXTERNAL_REGISTRAR=2};
44/* Constand definitions */
45#define DEC_FORMAT ("%d")
46#define BYTE5_FORMAT ("%02x%02x%02x%02x%02x")
47#define BYTE6_FORMAT ("%02x%02x%02x%02x%02x%02x")
48#define BYTE13_FORMAT ("%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x")
49#define STR_FORMAT ("%s")
50#define HEX_FORMAT ("%02x")
51#define SCHEDULE_FORMAT ("%d,%d,%d,%d")
52#ifdef HOME_GATEWAY
53#define PORTFW_FORMAT ("%s, %d, %d, %d")
54#define PORTFILTER_FORMAT ("%d, %d, %d")
55#define IPFILTER_FORMAT ("%s, %d")
56#define TRIGGERPORT_FORMAT ("%d, %d, %d, %d, %d, %d")
57#endif
58#define MACFILTER_FORMAT ("%02x%02x%02x%02x%02x%02x")
59#define MACFILTER_COLON_FORMAT ("%02x:%02x:%02x:%02x:%02x:%02x")
60#define WDS_FORMAT ("%02x%02x%02x%02x%02x%02x,%d")
61#define DHCPRSVDIP_FORMAT ("%02x%02x%02x%02x%02x%02x,%s,%s")
62#define VLANCONFIG_FORMAT ("%s,%d,%d,%d,%d,%d,%d")
63#ifdef HOME_GATEWAY
64#ifdef VPN_SUPPORT
65//#define IPSECTUNNEL_FORMAT ("%d, %d, %s, %d, %s, %d, %d, %s , %d, %s, %d, %d, %d, %d, %d, %d, %s, %d, %d, %d, %lu, %lu, %d, %s, %s, %s")
66#define IPSECTUNNEL_FORMAT ("%d, %d, %s, %d, %s, %d, %d, %s , %d, %s, %d, %d, %d, %d, %s, %d, %d, %d, %lu, %lu, %d, %s, %s, %s, %d, %s, %s, %d, %d, %s")
67#endif
68#ifdef CONFIG_IPV6
69#define RADVD_FORMAT ("%d, %s, %d, %d, %d, %d, %d, %d, %d, %d ,%d, %d, %s, %d, %d, %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x, %d, %d, %d, %d, %d, %d, %s, %d, %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x, %d, %d, %d, %d, %d, %d, %s, %d")
70#define DNSV6_FORMAT ("%d, %s")
71#define DHCPV6S_FORMAT ("%d, %s, %s, %s, %s")
72#define ADDR6_FORMAT ("%d, %d, %d, %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x, %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x")
73#endif
74#endif
75
76#define SPACE (' ')
77#define EOL ('\n')
78
79#define LOCAL_ADMIN_BIT 0x02
80
81#define READ_MIB_FROM_DEVICE 0
82
83
84
85/* Macro definition */
86static int _is_hex(char c)
87{
88 return (((c >= '0') && (c <= '9')) ||
89 ((c >= 'A') && (c <= 'F')) ||
90 ((c >= 'a') && (c <= 'f')));
91}
92
93static int string_to_hex(char *string, char *key, int len)
94{
95 char tmpBuf[4];
96 int idx, ii=0;
97 for (idx=0; idx<len; idx+=2) {
98 tmpBuf[0] = string[idx];
99 tmpBuf[1] = string[idx+1];
100 tmpBuf[2] = 0;
101 if ( !_is_hex(tmpBuf[0]) || !_is_hex(tmpBuf[1]))
102 return 0;
103
104 key[ii++] = (char) strtol(tmpBuf, (char**)NULL, 16);
105 }
106 return 1;
107}
108
109static void convert_lower(char *str)
110{ int i;
111 int len = strlen(str);
112 for (i=0; i<len; i++)
113 str[i] = tolower(str[i]);
114}
115
116
117
118static void generateWpaConf(char *outputFile, int isWds, char *iface);
119
120static int updateWscConf(char *in, char *out, int genpin, char *iface);
121
122int write_line_to_file(char *filename, int mode, char *line_data);
123int get_file_content(char *file_name, char *dest_buffer);
124//static void getVal2(char *value, char **p1, char **p2);
125
126/////////////////////////////////////////////////////////////////////////////////////////
127#if 0
128static char __inline__ *getVal(char *value, char **p)
129{
130 int len=0;
131
132 while (*value == ' ' ) value++;
133
134 *p = value;
135
136 while (*value && *value!=',') {
137 value++;
138 len++;
139 }
140
141 if ( !len ) {
142 *p = NULL;
143 return NULL;
144 }
145
146 if ( *value == 0)
147 return NULL;
148
149 *value = 0;
150 value++;
151
152 return value;
153}
154#endif
155
156//////////////////////////////////////////////////////////////////////
157static char *get_token(char *data, char *token)
158{
159 char *ptr=data;
160 int len=0, idx=0;
161
162 while (*ptr && *ptr != '\n' ) {
163 if (*ptr == '=') {
164 if (len <= 1)
165 return NULL;
166 memcpy(token, data, len);
167
168 /* delete ending space */
169 for (idx=len-1; idx>=0; idx--) {
170 if (token[idx] != ' ')
171 break;
172 }
173 token[idx+1] = '\0';
174
175 return ptr+1;
176 }
177 len++;
178 ptr++;
179 }
180 return NULL;
181}
182
183//////////////////////////////////////////////////////////////////////
184static int get_value(char *data, char *value)
185{
186 char *ptr=data;
187 int len=0, idx, i;
188
189 while (*ptr && *ptr != '\n' && *ptr != '\r') {
190 len++;
191 ptr++;
192 }
193
194 /* delete leading space */
195 idx = 0;
196 while (len-idx > 0) {
197 if (data[idx] != ' ')
198 break;
199 idx++;
200 }
201 len -= idx;
202
203 /* delete bracing '"' */
204 if (data[idx] == '"') {
205 for (i=idx+len-1; i>idx; i--) {
206 if (data[i] == '"') {
207 idx++;
208 len = i - idx;
209 }
210 break;
211 }
212 }
213
214 if (len > 0) {
215 memcpy(value, &data[idx], len);
216 value[len] = '\0';
217 }
218 return len;
219}
220
221//////////////////////////////////////////////////////////////////////
222/*
223 SSID=WPS05bdad67c1
224 WSC_SSID=WPS05bdad67c1
225 ENCRYPT=6
226 WSC_AUTH=34
227 WPA_AUTH=2
228 WPA_PSK=5cde41ec70fd0dcf75a766
229 PSK_FORMAT=0
230 WSC_PSK=5cde41ec70fd0dcf75a766
231 WPA_CIPHER_SUITE=3
232 WPA2_CIPHER_SUITE=3
233 WSC_ENC=12
234 WSC_CONFIGBYEXTREG=1
235 WSC_CONFIGURED=1
236*/
237static void readFileSetParam(char *file, char *wlanif_name)
238{
239 FILE *fp;
240 char line[200], token[40], value[100], *ptr;
241 char config_path[256]={0};
242 char file_name[40];
243 char full_path[256];
244
245 fp = fopen(file, "r");
246 if (fp == NULL) {
247 printf("read file [%s] failed!\n", file);
248 return;
249 }
250
251 //sprintf(config_path, WIFI_CONFIG_ROOT_DIR "/%s/", wlanif_name);
252 sprintf(config_path, "%s/%s/", ConfigFilePath,wlanif_name);
253
254 while ( fgets(line, 200, fp) ) {
255 ptr = get_token(line, token);
256 if (ptr == NULL)
257 continue;
258 if (get_value(ptr, value)==0)
259 continue;
260 if(strstr(token, "WEP")){
261 if(!strcmp(token, "WEP64_KEY1")){
262 sprintf(file_name, "%s", "wepkey1_64_hex");
263 sprintf(full_path, "%s%s",config_path,file_name);
264 write_line_to_file(full_path, 1,value);
265 }else if(!strcmp(token, "WEP64_KEY2")){
266 sprintf(file_name, "%s", "wepkey2_64_hex");
267 sprintf(full_path, "%s%s",config_path,file_name);
268 write_line_to_file(full_path, 1,value);
269 }else if(!strcmp(token, "WEP64_KEY3")){
270 sprintf(file_name, "%s", "wepkey3_64_hex");
271 sprintf(full_path, "%s%s",config_path,file_name);
272 write_line_to_file(full_path, 1,value);
273 }else if(!strcmp(token, "WEP64_KEY4")){
274 sprintf(file_name, "%s", "wepkey4_64_hex");
275 sprintf(full_path, "%s%s",config_path,file_name);
276 write_line_to_file(full_path, 1,value);
277 }else if(!strcmp(token, "WEP128_KEY1")){
278 sprintf(file_name, "%s", "wepkey1_128_hex");
279 sprintf(full_path, "%s%s",config_path,file_name);
280 write_line_to_file(full_path, 1,value);
281 }else if(!strcmp(token, "WEP128_KEY2")){
282 sprintf(file_name, "%s", "wepkey2_128_hex");
283 sprintf(full_path, "%s%s",config_path,file_name);
284 write_line_to_file(full_path, 1,value);
285 }else if(!strcmp(token, "WEP128_KEY3")){
286 sprintf(file_name, "%s", "wepkey3_128_hex");
287 sprintf(full_path, "%s%s",config_path,file_name);
288 write_line_to_file(full_path, 1,value);
289 }else if(!strcmp(token, "WEP128_KEY4")){
290 sprintf(file_name, "%s", "wepkey4_128_hex");
291 sprintf(full_path, "%s%s",config_path,file_name);
292 write_line_to_file(full_path, 1,value);
293 }else if(!strcmp(token, "WEP")){
294 //set WEP 64 or 128
295 sprintf(file_name, "%s", token);
296 convert_lower(file_name);
297 sprintf(full_path, "%s%s",config_path,file_name);
298 write_line_to_file(full_path, 1,value);
299
300
301 }else if(!strcmp(token, "WEP_KEY_TYPE")){
302 //set HEX key type
303 sprintf(file_name, "%s", token);
304 convert_lower(file_name);
305 sprintf(full_path, "%s%s",config_path,file_name);
306 write_line_to_file(full_path, 1,value);
307
308 }
309 }
310 else{
311 sprintf(file_name, "%s", token);
312 convert_lower(file_name);
313 sprintf(full_path, "%s%s",config_path,file_name);
314 write_line_to_file(full_path, 1,value);
315 }
316 }
317 fclose(fp);
318}
319
320static void write_mib_content_to_file(char *mib_name, char *mib_value)
321{
322 char file_name[40];
323 char full_path[256];
324
325 sprintf(file_name, "%s", mib_name);
326 convert_lower(file_name);
327 sprintf(full_path, "%s%s",DEFAULT_CONFIG_PATH,file_name);
328 write_line_to_file(full_path, 1,mib_value);
329}
330
331static void read_mib_content_from_file(char *mib_name, char *mib_value)
332{
333 char file_name[40];
334 char full_path[256];
335
336 sprintf(file_name, "%s", mib_name);
337 convert_lower(file_name);
338 sprintf(full_path, "%s%s",DEFAULT_CONFIG_PATH,file_name);
339 get_file_content(full_path, mib_value);
340}
341
342//////////////////////////////////////////////////////////////////////
343int main(int argc, char** argv)
344{
345 int argNum=1;
346// int i;
347
348// int wlan_idx=0;
349 if ( argc > 1 ) {
350
351 if( argc==2 && (!strcmp(argv[1], "version")) )
352 {
353 printf("version : %s\n",DAEMON_VERSION);
354 return 0;
355 }
356
357 // for debugging
358#if 0
359 printf("Flash Receive Command: ");
360 for (i=0;i<argc;i++)
361 {
362 printf("%s ", argv[i]);
363 }
364 printf("\n");
365#endif
366
367 if ( !strcmp(argv[argNum], "config_file_path") ) {
368 ConfigFilePath = argv[argNum+1];
369 argNum+=2;
370 }
371 else
372 {
373 ConfigFilePath=WIFI_CONFIG_ROOT_DIR;
374 }
375
376 if ( !strcmp(argv[argNum], "wpa") ) {
377 if ((argNum+2) < argc) {
378 if (memcmp(argv[++argNum], "wlan", 4)) {
379 printf("Miss wlan_interface argument!\n");
380 return 0;
381 }
382 generateWpaConf(argv[argNum+1], 0,argv[argNum+2]);
383 return 0;
384 }
385 else {
386 printf("Miss arguments [wlan_interface config_filename]!\n");
387 return 0;
388 }
389 }
390 // set flash parameters by reading from file
391 else if ( !strcmp(argv[argNum], "-param_file") ) {
392 if ((argNum+2) < argc) {
393 if (memcmp(argv[++argNum], "wlan", 4)) {
394 printf("Miss wlan_interface argument!\n");
395 return 0;
396 }
397#if 0
398 wlan_idx = atoi(&argv[argNum][4]);
399 if (wlan_idx >= NUM_WLAN_INTERFACE) {
400 printf("invalid wlan interface index number!\n");
401 return 0;
402 }
403#endif
404 printf("readFileSetParam %s %s\n",argv[argNum+1],argv[argNum]);
405 readFileSetParam(argv[argNum+1], argv[argNum]);
406 }
407 else
408 printf("Miss arguments [wlan_interface param_file]!\n");
409 return 0;
410 }
411
412 else if ( !strcmp(argv[argNum], "upd-wsc-conf") ) {
413
414 return updateWscConf(argv[argNum+1], argv[argNum+2], 0, argv[argNum+3]);
415 }
416 else if ( !strcmp(argv[argNum], "gen-pin") ) {
417 return updateWscConf(0, 0, 1, argv[argNum+1]);
418 }else if ( !strcmp(argv[argNum], "set") ) {
419 write_mib_content_to_file(argv[argNum+1], argv[argNum+2]);
420 }
421 else if ( !strcmp(argv[argNum], "sethw") )
422 { //flash sethw
423 unsigned char pwr;
424 char mib[100]={0};
425 char mibvalue[256+1]={0};
426
427 argNum++;
428 if (!memcmp((argv[argNum]+9), "TX_POWER", 8)) {
429 sprintf(mib, "%s", (argv[argNum++]+9));
430 while( (argNum < argc) && (argNum < 31) ) {
431 pwr = atoi(argv[argNum++]);
432 sprintf(mibvalue, "%s%02x", mibvalue, pwr);
433 }
434// printf("Flash: write file %s:%s\n", mib, mibvalue);
435 write_mib_content_to_file(mib, mibvalue);
436 }
437 else {
438 while((argNum + 1) < argc)
439 {
440 if((!strcmp(argv[argNum], "HW_WLAN0_WLAN_ADDR")) ) {
441 sprintf(mib, "wlan0_addr");
442 argNum++;
443 }
444 else if (!memcmp(argv[argNum], "HW_WLAN", 7)) {
445 sprintf(mib, "%s", (argv[argNum++]+9));
446 }
447 else {
448 strcpy(mib, argv[argNum++]);
449 }
450 sscanf(argv[argNum++], "%s", mibvalue);
451
452// printf("Flash: write file %s:%s\n", mib, mibvalue);
453 write_mib_content_to_file(mib, mibvalue);
454 }
455 }
456 }
457 else if ( !strcmp(argv[argNum], "gethw") )
458 {
459 char buffer[512];
460 char mib[100]={0};
461
462 argNum++;
463 while(argNum < argc)
464 {
465 if((!strcmp(argv[argNum], "HW_NIC0_ADDR")) || (!strcmp(argv[argNum], "HW_WLAN0_WLAN_ADDR")) ) {
466 sprintf(mib, "wlan0_addr");
467 }
468 else if (!memcmp(argv[argNum], "HW_WLAN", 7)) {
469 sprintf(mib, "%s", (argv[argNum]+9));
470 }
471 else {
472 strcpy(mib, argv[argNum]);
473 }
474
475 read_mib_content_from_file(mib, buffer);
476 printf("%s=%s",argv[argNum],buffer);
477
478 argNum++;
479 }
480 }
481 else
482 {
483 printf("flash: Unknown Command: %s\n", argv[argNum]);
484 }
485
486 }
487
488 return 0;
489}
490#if 0
491////////////////////////////////////////////////////////////////////////////////
492static void getVal2(char *value, char **p1, char **p2)
493{
494 value = getVal(value, p1);
495 if ( value )
496 getVal(value, p2);
497 else
498 *p2 = NULL;
499}
500
501////////////////////////////////////////////////////////////////////////////////
502static int getdir(char *fullname, char *path, int loop)
503{
504 char tmpBuf[100], *p, *p1;
505
506 strcpy(tmpBuf, fullname);
507 path[0] = '\0';
508
509 p1 = tmpBuf;
510 while (1) {
511 if ((p=strchr(p1, '/'))) {
512 if (--loop == 0) {
513 *p = '\0';
514 strcpy(path, tmpBuf);
515 return 0;
516 }
517 p1 = ++p;
518 }
519 else
520 break;
521 }
522 return -1;
523}
524#endif
525////////////////////////////////////////////////////////////////////////////////
526static void __inline__ WRITE_WPA_FILE(int fh, char *buf)
527{
528 if ( write(fh, buf, strlen(buf)) != strlen(buf) ) {
529 printf("Write WPA config file error!\n");
530 close(fh);
531 exit(1);
532 }
533}
534
535#if READ_MIB_FROM_DEVICE
536#else
537////////////////////////////////////////////////////////////////////////////////
538static void generateWpaConf(char *outputFile, int isWds, char *iface)
539{
540 int fh, intVal , auth_val , encrypt, enable1x, wep;
541 char buf1[1024], buf2[1024];
542 char config_path[256]={0};
543 char config_value[100]={0};
544 char tmp1[100], tmpbuf[100];
545 int get_result=0;
546 char default_config_path[256]={0};
547 int repeater_enabled=0;
548#ifdef CONFIG_IEEE80211W
549 int pmf_enabled=0;
550#endif /* CONFIG_IEEE80211W */
551
552 //sprintf(default_config_path, WIFI_CONFIG_ROOT_DIR "/%s/", iface);
553 sprintf(default_config_path, "%s/%s/", ConfigFilePath,iface);
554 fh = open(outputFile, O_RDWR|O_CREAT|O_TRUNC, S_IRGRP|S_IWGRP);
555 if (fh == -1) {
556 printf("Create WPA config file error!\n");
557 return;
558 }
559 if (!isWds) {
560
561 //apmib_get( MIB_WLAN_ENCRYPT, (void *)&encrypt);
562 sprintf(config_path, "%s%s", default_config_path, "encrypt");
563 get_result = get_file_content(config_path, config_value);
564 encrypt = atoi(config_value);
565
566 sprintf(buf2, "encryption = %d\n", encrypt);
567 WRITE_WPA_FILE(fh, buf2);
568
569
570 //apmib_get( MIB_WLAN_SSID, (void *)buf1);
571 if(!strcmp(iface, "wlan0-vxd")){
572 //sprintf(config_path, WIFI_CONFIG_ROOT_DIR "/%s", "repeater_enabled");
573 sprintf(config_path, "%s/%s/", ConfigFilePath,"repeater_ssid");
574 get_result = get_file_content(config_path, config_value);
575 repeater_enabled = atoi(config_value);
576 if(repeater_enabled==1){
577 //sprintf(config_path, WIFI_CONFIG_ROOT_DIR "/%s", "repeater_ssid");
578 sprintf(config_path, "%s/%s/", ConfigFilePath,"repeater_ssid");
579 get_result = get_file_content(config_path, config_value);
580 }else{
581 sprintf(config_path, "%s%s", default_config_path, "ssid");
582 }
583 }else{
584 sprintf(config_path, "%s%s", default_config_path, "ssid");
585 }
586
587 get_result = get_file_content(config_path, config_value);
588 //config_value[strlen(config_value)-1]='\0';
589 sprintf(buf1,"%s", config_value);
590
591 sprintf(buf2, "ssid = \"%s\"\n", buf1);
592 WRITE_WPA_FILE(fh, buf2);
593
594 //apmib_get( MIB_WLAN_ENABLE_1X, (void *)&enable1x);
595 sprintf(config_path, "%s%s", default_config_path, "enable_1x");
596 get_result = get_file_content(config_path, config_value);
597 enable1x = atoi(config_value);
598
599 sprintf(buf2, "enable1x = %d\n", enable1x);
600 WRITE_WPA_FILE(fh, buf2);
601
602 //apmib_get( MIB_WLAN_MAC_AUTH_ENABLED, (void *)&intVal);
603 sprintf(config_path, "%s%s", default_config_path, "mac_auth_enabled");
604 get_result = get_file_content(config_path, config_value);
605 intVal = atoi(config_value);
606
607 sprintf(buf2, "enableMacAuth = %d\n", intVal);
608 WRITE_WPA_FILE(fh, buf2);
609
610 //apmib_get( MIB_WLAN_ENABLE_SUPP_NONWPA, (void *)&intVal);
611 sprintf(config_path, "%s%s", default_config_path, "enable_supp_nonwpa");
612 get_result = get_file_content(config_path, config_value);
613 intVal = atoi(config_value);
614
615
616 if (intVal){
617 //apmib_get( MIB_WLAN_SUPP_NONWPA, (void *)&intVal);
618 sprintf(config_path, "%s%s", default_config_path, "supp_nonwpa");
619 get_result = get_file_content(config_path, config_value);
620 intVal = atoi(config_value);
621 }
622 sprintf(buf2, "supportNonWpaClient = %d\n", intVal);
623 WRITE_WPA_FILE(fh, buf2);
624
625 //apmib_get( MIB_WLAN_WEP, (void *)&wep);
626 sprintf(config_path, "%s%s", default_config_path, "wep");
627 get_result = get_file_content(config_path, config_value);
628 wep = atoi(config_value);
629
630 sprintf(buf2, "wepKey = %d\n", wep);
631 WRITE_WPA_FILE(fh, buf2);
632
633 if ( encrypt==1 && enable1x ) {
634 if (wep == 1) {
635 //apmib_get( MIB_WLAN_WEP64_KEY1, (void *)buf1);
636 sprintf(config_path, "%s%s", default_config_path, "wepkey1_64_asc");
637 if( get_file_content(config_path, config_value)<0 )
638 {
639 strcpy(buf2, "wepGroupKey = \"\"\n");
640 }
641 else
642 {
643 sprintf(tmpbuf, "%s", config_value);
644 memcpy(tmp1, tmpbuf, 5);
645 tmp1[5] = '\0';
646 sprintf(buf2, "wepGroupKey = \"%02x%02x%02x%02x%02x\"\n", tmp1[0],tmp1[1],tmp1[2],tmp1[3],tmp1[4]);
647 }
648 }
649 else {
650 //apmib_get( MIB_WLAN_WEP128_KEY1, (void *)buf1);
651 sprintf(config_path, "%s%s", default_config_path, "wepkey1_128_asc");
652 if( get_file_content(config_path, config_value)<0 )
653 {
654 strcpy(buf2, "wepGroupKey = \"\"\n");
655 }
656 else
657 {
658 sprintf(tmpbuf, "%s", config_value);
659 memcpy(tmp1, tmpbuf, 13);
660 tmp1[13] = '\0';
661 sprintf(buf2, "wepGroupKey = \"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\"\n",
662 tmp1[0],tmp1[1],tmp1[2],tmp1[3],tmp1[4],
663 tmp1[5],tmp1[6],tmp1[7],tmp1[8],tmp1[9],
664 tmp1[10],tmp1[11],tmp1[12]);
665 }
666 }
667 }
668 else
669 strcpy(buf2, "wepGroupKey = \"\"\n");
670 WRITE_WPA_FILE(fh, buf2);
671
672 //apmib_get( MIB_WLAN_WPA_AUTH, (void *)&intVal);
673 sprintf(config_path, "%s%s", default_config_path, "wpa_auth");
674 get_result = get_file_content(config_path, config_value);
675 auth_val = atoi(config_value);
676
677#ifndef CONFIG_IEEE80211W
678 sprintf(buf2, "authentication = %d\n", auth_val );
679 WRITE_WPA_FILE(fh, buf2);
680#else
681 sprintf(config_path, "%s%s", default_config_path, "wpa11w");
682 get_result = get_file_content(config_path, config_value);
683 pmf_enabled = atoi(config_value);
684 sprintf((char *)buf2, "ieee80211w = %d\n", pmf_enabled);
685 WRITE_WPA_FILE(fh, buf2);
686
687 sprintf(config_path, "%s%s", default_config_path, "wpa2EnableSHA256");
688 get_result = get_file_content(config_path, config_value);
689 intVal = atoi(config_value);
690 if (pmf_enabled == 0) {
691 intVal = 0;
692 } else if (pmf_enabled == 1) {
693 if (intVal != 0 && intVal != 1) {
694 intVal = 0;
695 }
696 } else if (pmf_enabled == 2) {
697 intVal = 1;
698 }
699 sprintf((char *)buf2, "sha256 = %d\n", intVal);
700 WRITE_WPA_FILE(fh, buf2);
701
702 if( (intVal==1) && (auth_val==1) )
703 {
704 sprintf(buf2, "authentication = 5\n" );
705 WRITE_WPA_FILE(fh, buf2);
706 }
707 else
708 {
709 sprintf(buf2, "authentication = %d\n", auth_val );
710 WRITE_WPA_FILE(fh, buf2);
711 }
712
713
714#endif
715
716 //apmib_get( MIB_WLAN_WPA_CIPHER_SUITE, (void *)&intVal);
717 sprintf(config_path, "%s%s", default_config_path, "wpa_cipher");
718 get_result = get_file_content(config_path, config_value);
719 intVal = atoi(config_value);
720
721 sprintf(buf2, "unicastCipher = %d\n", intVal);
722 WRITE_WPA_FILE(fh, buf2);
723
724 //apmib_get( MIB_WLAN_WPA2_CIPHER_SUITE, (void *)&intVal);
725 sprintf(config_path, "%s%s", default_config_path, "wpa2_cipher");
726 get_result = get_file_content(config_path, config_value);
727 intVal = atoi(config_value);
728
729 sprintf(buf2, "wpa2UnicastCipher = %d\n", intVal);
730 WRITE_WPA_FILE(fh, buf2);
731
732 //apmib_get( MIB_WLAN_WPA2_PRE_AUTH, (void *)&intVal);
733 sprintf(config_path, "%s%s", default_config_path, "wpa2_pre_auth");
734 get_result = get_file_content(config_path, config_value);
735 intVal = atoi(config_value);
736
737 sprintf(buf2, "enablePreAuth = %d\n", intVal);
738 WRITE_WPA_FILE(fh, buf2);
739
740 //apmib_get( MIB_WLAN_PSK_FORMAT, (void *)&intVal);
741 sprintf(config_path, "%s%s", default_config_path, "psk_format");
742 get_result = get_file_content(config_path, config_value);
743 intVal = atoi(config_value);
744
745 if (intVal==0)
746 sprintf(buf2, "usePassphrase = 1\n");
747 else
748 sprintf(buf2, "usePassphrase = 0\n");
749 WRITE_WPA_FILE(fh, buf2);
750
751 //apmib_get( MIB_WLAN_WPA_PSK, (void *)buf1);
752
753 sprintf(config_path, "%s%s", default_config_path, "wpa_psk");
754 get_result = get_file_content(config_path, config_value);
755 //config_value[strlen(config_value)-1]='\0';
756 sprintf(buf1, "%s", config_value);
757 sprintf(buf2, "psk = \"%s\"\n", buf1);
758 WRITE_WPA_FILE(fh, buf2);
759
760 //apmib_get( MIB_WLAN_WPA_GROUP_REKEY_TIME, (void *)&intVal);
761 sprintf(config_path, "%s%s", default_config_path, "gk_rekey");
762 get_result = get_file_content(config_path, config_value);
763 intVal = atoi(config_value);
764
765 sprintf(buf2, "groupRekeyTime = %d\n", intVal);
766 WRITE_WPA_FILE(fh, buf2);
767
768 //apmib_get( MIB_WLAN_RS_PORT, (void *)&intVal);
769 sprintf(config_path, "%s%s", default_config_path, "rs_port");
770 get_result = get_file_content(config_path, config_value);
771 intVal = atoi(config_value);
772
773 sprintf(buf2, "rsPort = %d\n", intVal);
774 WRITE_WPA_FILE(fh, buf2);
775
776 //apmib_get( MIB_WLAN_RS_IP, (void *)buf1);
777 sprintf(config_path, "%s%s", default_config_path, "rs_ip");
778 get_result = get_file_content(config_path, config_value);
779 //config_value[strlen(config_value)-1]='\0';
780 sprintf(buf1, "%s", config_value);
781 sprintf(buf2, "rsIP = %s\n",buf1);
782 //sprintf(buf2, "rsIP = %s\n", inet_ntoa(*((struct in_addr *)buf1)));
783 WRITE_WPA_FILE(fh, buf2);
784
785 //apmib_get( MIB_WLAN_RS_PASSWORD, (void *)buf1);
786 sprintf(config_path, "%s%s", default_config_path, "rs_password");
787 get_result = get_file_content(config_path, config_value);
788 //config_value[strlen(config_value)-1]='\0';
789 sprintf(buf1, "%s", config_value);
790 sprintf(buf2, "rsPassword = \"%s\"\n", buf1);
791 WRITE_WPA_FILE(fh, buf2);
792
793 sprintf(config_path, "%s%s", default_config_path, "rs2_port");
794 if( get_result = get_file_content(config_path, config_value)>0 )
795 {
796 intVal = atoi(config_value);
797 sprintf(buf2, "rs2Port = %d\n", intVal);
798 WRITE_WPA_FILE(fh, buf2);
799 }
800
801 sprintf(config_path, "%s%s", default_config_path, "rs2_ip");
802 if( get_result = get_file_content(config_path, config_value)>0 )
803 {
804 intVal = atoi(config_value);
805 sprintf(buf2, "rs2IP = %d\n", intVal);
806 WRITE_WPA_FILE(fh, buf2);
807 }
808
809 sprintf(config_path, "%s%s", default_config_path, "rs2_password");
810 if( get_result = get_file_content(config_path, config_value)>0 )
811 {
812 intVal = atoi(config_value);
813 sprintf(buf2, "rs2Password = %d\n", intVal);
814 WRITE_WPA_FILE(fh, buf2);
815 }
816
817 //apmib_get( MIB_WLAN_RS_MAXRETRY, (void *)&intVal);
818 sprintf(config_path, "%s%s", default_config_path, "rs_maxretry");
819 get_result = get_file_content(config_path, config_value);
820 intVal = atoi(config_value);
821
822 sprintf(buf2, "rsMaxReq = %d\n", intVal);
823 WRITE_WPA_FILE(fh, buf2);
824
825
826 // apmib_get( MIB_WLAN_RS_INTERVAL_TIME, (void *)&intVal);
827 sprintf(config_path, "%s%s", default_config_path, "rs_interval_time");
828 get_result = get_file_content(config_path, config_value);
829 intVal = atoi(config_value);
830
831 sprintf(buf2, "rsAWhile = %d\n", intVal);
832 WRITE_WPA_FILE(fh, buf2);
833
834 //apmib_get( MIB_WLAN_ACCOUNT_RS_ENABLED, (void *)&intVal);
835 sprintf(config_path, "%s%s", default_config_path, "account_rs_enabled");
836 get_result = get_file_content(config_path, config_value);
837 intVal = atoi(config_value);
838
839 sprintf(buf2, "accountRsEnabled = %d\n", intVal);
840 WRITE_WPA_FILE(fh, buf2);
841
842 //apmib_get( MIB_WLAN_ACCOUNT_RS_PORT, (void *)&intVal);
843 sprintf(config_path, "%s%s", default_config_path, "account_rs_port");
844 get_result = get_file_content(config_path, config_value);
845 intVal = atoi(config_value);
846
847 sprintf(buf2, "accountRsPort = %d\n", intVal);
848 WRITE_WPA_FILE(fh, buf2);
849
850 //apmib_get( MIB_WLAN_ACCOUNT_RS_IP, (void *)buf1);
851 sprintf(config_path, "%s%s", default_config_path, "account_rs_ip");
852 get_result = get_file_content(config_path, config_value);
853 //config_value[strlen(config_value)-1]='\0';
854 sprintf(buf1, "%s", config_value);
855 sprintf(buf2, "accountRsIP = %s\n", buf1);
856 //sprintf(buf2, "accountRsIP = %s\n", inet_ntoa(*((struct in_addr *)buf1)));
857 WRITE_WPA_FILE(fh, buf2);
858
859 //apmib_get( MIB_WLAN_ACCOUNT_RS_PASSWORD, (void *)buf1);
860 sprintf(config_path, "%s%s", default_config_path, "account_rs_password");
861 get_result = get_file_content(config_path, config_value);
862 //config_value[strlen(config_value)-1]='\0';
863 sprintf(buf1, "%s", config_value);
864 sprintf(buf2, "accountRsPassword = \"%s\"\n", buf1);
865
866 WRITE_WPA_FILE(fh, buf2);
867
868 //apmib_get( MIB_WLAN_ACCOUNT_RS_UPDATE_ENABLED, (void *)&intVal);
869 sprintf(config_path, "%s%s", default_config_path, "account_rs_update_enabled");
870 get_result = get_file_content(config_path, config_value);
871 intVal = atoi(config_value);
872 sprintf(buf2, "accountRsUpdateEnabled = %d\n", intVal);
873 WRITE_WPA_FILE(fh, buf2);
874
875 //apmib_get( MIB_WLAN_ACCOUNT_RS_UPDATE_DELAY, (void *)&intVal);
876 sprintf(config_path, "%s%s", default_config_path, "account_rs_update_delay");
877 get_result = get_file_content(config_path, config_value);
878 intVal = atoi(config_value);
879
880 sprintf(buf2, "accountRsUpdateTime = %d\n", intVal);
881 WRITE_WPA_FILE(fh, buf2);
882
883 //apmib_get( MIB_WLAN_ACCOUNT_RS_MAXRETRY, (void *)&intVal);
884 sprintf(config_path, "%s%s", default_config_path, "account_rs_maxretry");
885 get_result = get_file_content(config_path, config_value);
886 intVal = atoi(config_value);
887
888 sprintf(buf2, "accountRsMaxReq = %d\n", intVal);
889 WRITE_WPA_FILE(fh, buf2);
890
891 //apmib_get( MIB_WLAN_ACCOUNT_RS_INTERVAL_TIME, (void *)&intVal);
892 sprintf(config_path, "%s%s", default_config_path, "account_rs_interval_time");
893 get_result = get_file_content(config_path, config_value);
894 intVal = atoi(config_value);
895
896 sprintf(buf2, "accountRsAWhile = %d\n", intVal);
897 WRITE_WPA_FILE(fh, buf2);
898
899 }
900 close(fh);
901}
902#endif
903
904////////////////////////////////////////////////////////////////////////////////
905#if 0
906void calc_incr(unsigned char *mac, int idx)
907{
908 if( (*mac+idx) == 0x0 )
909 calc_incr(mac-1,1);
910 else
911 *mac += idx;
912}
913
914int get_root_mac(unsigned char *mac)
915{
916 int fd;
917 struct ifreq ifr;
918 unsigned char zero_mac[6]={0}, broadcat_mac[6]={0xff};
919
920 fd = socket(AF_INET, SOCK_DGRAM, 0);
921
922 ifr.ifr_addr.sa_family = AF_INET;
923 strcpy(ifr.ifr_name, "wlan0");
924 if( ioctl(fd, SIOCGIFHWADDR, &ifr) < 0 )
925 return -1;
926
927 close(fd);
928 if( !memcmp(ifr.ifr_hwaddr.sa_data,zero_mac,6) || !memcmp(ifr.ifr_hwaddr.sa_data,broadcat_mac,6) )
929 return -1;
930
931 memcpy(mac,ifr.ifr_hwaddr.sa_data,6);
932 return 0;
933}
934
935#endif
936enum {
937 MODE_AP_UNCONFIG=1, // AP unconfigured (enrollee)
938 MODE_CLIENT_UNCONFIG=2, // client unconfigured (enrollee)
939 MODE_CLIENT_CONFIG=3, // client configured (registrar)
940 MODE_AP_PROXY=4, // AP configured (proxy)
941 MODE_AP_PROXY_REGISTRAR=5, // AP configured (proxy and registrar)
942 MODE_CLIENT_UNCONFIG_REGISTRAR=6 // client unconfigured (registrar)
943};
944
945#define WRITE_WSC_PARAM(dst, tmp, str, val) { \
946 sprintf(tmp, str, val); \
947 memcpy(dst, tmp, strlen(tmp)); \
948 dst += strlen(tmp); \
949}
950
951static void convert_bin_to_str(char *bin, int len, char *out)
952{
953 int i;
954 char tmpbuf[10];
955
956 out[0] = '\0';
957
958 for (i=0; i<len; i++) {
959 sprintf(tmpbuf, "%02x", bin[i]);
960 strcat(out, tmpbuf);
961 }
962}
963
964static void convert_hex_to_ascii(unsigned long code, char *out)
965{
966 *out++ = '0' + ((code / 10000000) % 10);
967 *out++ = '0' + ((code / 1000000) % 10);
968 *out++ = '0' + ((code / 100000) % 10);
969 *out++ = '0' + ((code / 10000) % 10);
970 *out++ = '0' + ((code / 1000) % 10);
971 *out++ = '0' + ((code / 100) % 10);
972 *out++ = '0' + ((code / 10) % 10);
973 *out++ = '0' + ((code / 1) % 10);
974 *out = '\0';
975}
976
977static int compute_pin_checksum(unsigned long int PIN)
978{
979 unsigned long int accum = 0;
980 int digit;
981
982 PIN *= 10;
983 accum += 3 * ((PIN / 10000000) % 10);
984 accum += 1 * ((PIN / 1000000) % 10);
985 accum += 3 * ((PIN / 100000) % 10);
986 accum += 1 * ((PIN / 10000) % 10);
987 accum += 3 * ((PIN / 1000) % 10);
988 accum += 1 * ((PIN / 100) % 10);
989 accum += 3 * ((PIN / 10) % 10);
990
991 digit = (accum % 10);
992 return (10 - digit) % 10;
993}
994int write_line_to_file(char *filename, int mode, char *line_data)
995{
996 char tmpbuf[512];
997 int fh=0;
998
999 if(mode == 1) {/* write line data to file */
1000
1001 fh = open(filename, O_RDWR|O_CREAT|O_TRUNC, S_IWGRP|S_IRGRP);
1002
1003 }else if(mode == 2){/*append line data to file*/
1004
1005 fh = open(filename, O_RDWR|O_APPEND);
1006 }
1007
1008
1009 if (fh < 0) {
1010 fprintf(stderr, "Create %s error!\n", filename);
1011 return 0;
1012 }
1013
1014
1015 sprintf(tmpbuf, "%s", line_data);
1016 write(fh, tmpbuf, strlen(tmpbuf));
1017
1018
1019
1020 close(fh);
1021 return 1;
1022}
1023
1024int get_file_content(char *file_name, char *dest_buffer)
1025{
1026 FILE *fp;
1027 char buf[150];
1028 int check_end_char=0;
1029 fp= fopen(file_name, "r");
1030 if (!fp) {
1031 printf("can't open file:%s!\n",file_name);
1032 dest_buffer[0]=0;
1033 return -1;
1034 }
1035 fgets(buf,150,fp);
1036 fclose(fp);
1037 sprintf(dest_buffer, "%s", buf);
1038 for(check_end_char=0; check_end_char< strlen(dest_buffer);check_end_char++){
1039 if(dest_buffer[check_end_char]==0x0A){
1040 dest_buffer[check_end_char]='\0';
1041 break;
1042 }
1043 }
1044 return 1;
1045}
1046
1047
1048
1049
1050#if READ_MIB_FROM_DEVICE
1051#else
1052static int updateWscConf(char *in, char *out, int genpin, char *wlanif_name)
1053{
1054 int fh;
1055 struct stat status;
1056 char *buf, *ptr;
1057 int intVal, is_client, is_config, is_registrar, len, is_wep=0, wep_key_type=0, wep_transmit_key=0;
1058 char tmpbuf[100], tmp1[256];
1059
1060 int isUpnpEnabled=0, wsc_method = 0, wsc_auth=0, wsc_enc=0;
1061 int wlan_network_type=0, wsc_manual_enabled=0, wlan_encrpty=0, wlan_wep=0;
1062 //char wlan_wep64_key1[100], wlan_wep64_key2[100], wlan_wep64_key3[100], wlan_wep64_key4[100];
1063 //char wlan_wep128_key1[100], wlan_wep128_key2[100], wlan_wep128_key3[100], wlan_wep128_key4[100];
1064#ifdef FOR_DUAL_BAND
1065 char wlan_wpa_psk[100];
1066#endif
1067 char wlan_ssid[100], device_name[100];//, wsc_pin[100];
1068 int wlan_chan_num=0, wsc_config_by_ext_reg=0;
1069// int is_vxdif=0;
1070 char default_config_path[256]={0};
1071 char config_path[256]={0};
1072 char config_value[100]={0};
1073 char tmp_config_value[100]={0};
1074 char tmp_config_path[256]={0};
1075 int get_result=0;
1076
1077 // WPS2DOTX
1078 int wlan0_wlan_disabled=0;
1079 int wlan0_wsc_disabled=0;
1080#ifdef FOR_DUAL_BAND
1081 int wlan1_wlan_disabled=0;
1082 int wlan1_wsc_disabled=0;
1083#endif
1084
1085 // WPS2DOTX
1086
1087 int band_select_5g2g; // 0:2.4g ; 1:5G ; 2:both
1088
1089 printf("\r\n wlanif_name=[%s],__[%s-%u]\r\n",wlanif_name,__FILE__,__LINE__);
1090
1091 //sprintf(default_config_path, WIFI_CONFIG_ROOT_DIR "/%s/", wlanif_name);
1092 sprintf(default_config_path, "%s/%s/", ConfigFilePath,wlanif_name);
1093
1094
1095 printf("default_config_path=%s\n",default_config_path);
1096
1097 //apmib_get(MIB_WLAN_WSC_REGISTRAR_ENABLED, (void *)&is_registrar);
1098 sprintf(config_path, "%s%s", default_config_path, "wsc_registrar_enabled");
1099 get_result = get_file_content(config_path, config_value);
1100 is_registrar = atoi(config_value);
1101
1102 //apmib_get(MIB_WLAN_WSC_UPNP_ENABLED, (void *)&isUpnpEnabled);
1103
1104 sprintf(config_path, "%s%s", default_config_path, "wsc_upnp_enabled");
1105 get_result = get_file_content(config_path, config_value);
1106 isUpnpEnabled = atoi(config_value);
1107
1108
1109 //apmib_get(MIB_WLAN_WSC_METHOD, (void *)&wsc_method);
1110 sprintf(config_path, "%s%s", default_config_path, "wsc_method");
1111 get_result = get_file_content(config_path, config_value);
1112 wsc_method = atoi(config_value);
1113
1114 //apmib_get(MIB_WLAN_NETWORK_TYPE, (void *)&wlan_network_type);
1115 sprintf(config_path, "%s%s", default_config_path, "network_type");
1116 get_result = get_file_content(config_path, config_value);
1117 wlan_network_type = atoi(config_value);
1118
1119 //apmib_get(MIB_WLAN_WSC_MANUAL_ENABLED, (void *)&wsc_manual_enabled);
1120 sprintf(config_path, "%s%s", default_config_path, "wsc_manual_enabled");
1121 get_result = get_file_content(config_path, config_value);
1122 wsc_manual_enabled = atoi(config_value);
1123
1124 //apmib_get(MIB_WLAN_CHANNEL, (void *)&wlan_chan_num);
1125 sprintf(config_path, "%s%s", default_config_path, "channel");
1126 get_result = get_file_content(config_path, config_value);
1127 wlan_chan_num=atoi(config_value);
1128
1129 //apmib_get(MIB_DEVICE_NAME, (void *)&device_name);
1130 //sprintf(config_path, WIFI_CONFIG_ROOT_DIR "/%s", "device_name");
1131 sprintf(config_path, "%s/%s", ConfigFilePath,"device_name");
1132 get_result = get_file_content(config_path, config_value);
1133 //config_value[strlen(config_value)-1]='\0';
1134 sprintf(device_name, "%s", config_value);
1135
1136
1137 //apmib_get(MIB_WLAN_BAND2G5G_SELECT, (void *)&band_select_5g2g);
1138 //sprintf(config_path, WIFI_CONFIG_ROOT_DIR "/%s", "band2g5g_select");
1139 sprintf(config_path, "%s/%s", ConfigFilePath,"band2g5g_select");
1140 get_result = get_file_content(config_path, config_value);
1141 band_select_5g2g=atoi(config_value);
1142
1143
1144
1145
1146
1147 //apmib_get(MIB_WLAN_WSC_AUTH, (void *)&wsc_auth);
1148
1149 sprintf(config_path, "%s%s", default_config_path, "wsc_auth");
1150 get_result = get_file_content(config_path, config_value);
1151 wsc_auth = atoi(config_value);
1152
1153
1154 //apmib_get(MIB_WLAN_WSC_ENC, (void *)&wsc_enc);
1155
1156 sprintf(config_path, "%s%s", default_config_path, "wsc_enc");
1157 get_result = get_file_content(config_path, config_value);
1158 wsc_enc = atoi(config_value);
1159
1160 //apmib_get(MIB_WLAN_WSC_CONFIGURED, (void *)&is_config);
1161 sprintf(config_path, "%s%s", default_config_path, "wsc_configured");
1162 get_result = get_file_content(config_path, config_value);
1163 is_config = atoi(config_value);
1164
1165 //apmib_get(MIB_WLAN_WSC_CONFIGBYEXTREG, (void *)&wsc_config_by_ext_reg);
1166 sprintf(config_path, "%s%s", default_config_path, "wsc_configbyextreg");
1167 get_result = get_file_content(config_path, config_value);
1168 wsc_config_by_ext_reg=atoi(config_value);
1169
1170 //apmib_get(MIB_WLAN_SSID, (void *)&wlan_ssid);
1171
1172 sprintf(config_path, "%s%s", default_config_path, "ssid");
1173 get_result = get_file_content(config_path, config_value);
1174 sprintf(wlan_ssid, "%s", config_value);
1175
1176 //apmib_get(MIB_WLAN_MODE, (void *)&is_client);
1177 sprintf(config_path, "%s%s", default_config_path, "wlan_mode");
1178 get_result = get_file_content(config_path, config_value);
1179 is_client = atoi(config_value);
1180
1181 //apmib_get(MIB_WLAN_ENCRYPT, (void *)&wlan_encrpty);
1182
1183 sprintf(config_path, "%s%s", default_config_path, "encrypt");
1184 get_result = get_file_content(config_path, config_value);
1185 wlan_encrpty = atoi(config_value);
1186
1187 //apmib_get(MIB_WLAN_WEP, (void *)&wlan_wep);
1188 sprintf(config_path, "%s%s", default_config_path, "wep");
1189 get_result = get_file_content(config_path, config_value);
1190 wlan_wep = atoi(config_value);
1191
1192 //apmib_get(MIB_WLAN_WEP_KEY_TYPE, (void *)&wep_key_type);
1193 sprintf(config_path, "%s%s", default_config_path, "wep_key_type");
1194 get_result = get_file_content(config_path, config_value);
1195 wep_key_type = atoi(config_value);
1196
1197 //apmib_get(MIB_WLAN_WEP_DEFAULT_KEY, (void *)&wep_transmit_key);
1198
1199 sprintf(config_path, "%s%s", default_config_path, "wep_default_key");
1200 get_result = get_file_content(config_path, config_value);
1201 wep_transmit_key = atoi(config_value);
1202
1203
1204
1205 //apmib_get(MIB_WLAN_WPA_PSK, (void *)&wlan_wpa_psk);
1206 //apmib_get(MIB_WLAN_WSC_DISABLE, (void *)&wlan0_wsc_disabled); // 1104
1207
1208 sprintf(config_path, "%s%s", default_config_path, "wsc_disabled");
1209 get_result = get_file_content(config_path, config_value);
1210 wlan0_wsc_disabled = atoi(config_value);
1211
1212 //apmib_get(MIB_WLAN_WLAN_DISABLED, (void *)&wlan0_wlan_disabled); // 0908
1213
1214 sprintf(config_path, "%s%s", default_config_path, "wlan_disabled");
1215 get_result = get_file_content(config_path, config_value);
1216 wlan0_wlan_disabled = atoi(config_value);
1217
1218 /* WPS2DOTX ; 2011-0524*/
1219 if(wlan0_wlan_disabled)
1220 wlan0_wsc_disabled=1 ; // if wlan0 interface is disabled ;
1221
1222 sprintf(config_path, "%s%s", default_config_path, "wsc_pin");
1223 get_result = get_file_content(config_path, config_value);
1224
1225 if (genpin || ((get_result != -1) && !memcmp(config_value, "\x0\x0\x0\x0\x0\x0\x0\x0", PIN_LEN))) {
1226 #include <sys/time.h>
1227 struct timeval tod;
1228 unsigned long num;
1229
1230 gettimeofday(&tod , NULL);
1231
1232 sprintf(tmp_config_path, "%s%s", default_config_path, "nic0_addr");
1233 get_result = get_file_content(tmp_config_path, tmp_config_value);
1234 //apmib_get(MIB_HW_NIC0_ADDR, (void *)&tmp1);
1235 if(get_result != -1){
1236 string_to_hex(tmp_config_value,tmp1, 12);
1237
1238 tod.tv_sec += tmp1[4]+tmp1[5];
1239 srand(tod.tv_sec);
1240 num = rand() % 10000000;
1241 num = num*10 + compute_pin_checksum(num);
1242 convert_hex_to_ascii((unsigned long)num, tmpbuf);
1243
1244 //apmib_set(MIB_HW_WSC_PIN, (void *)tmpbuf);
1245 write_line_to_file(config_path, 1,tmpbuf);
1246 //write_line_to_file(WIFI_CONFIG_ROOT_DIR "/wlan1/wsc_pin", 1,tmpbuf);
1247 sprintf( tmp1 , "%s/wlan1/wsc_pin" , ConfigFilePath );
1248 write_line_to_file( tmp1 , 1,tmpbuf);
1249 printf("Generated PIN = %s\n", tmpbuf);
1250 }
1251 if (genpin)
1252 return 0;
1253 }
1254
1255 if (stat(in, &status) < 0) {
1256 printf("stat() error [%s]!\n", in);
1257 return -1;
1258 }
1259
1260 buf = malloc(status.st_size+2048);
1261 if (buf == NULL) {
1262 printf("malloc() error [%d]!\n", (int)status.st_size+2048);
1263 return -1;
1264 }
1265
1266 ptr = buf;
1267
1268 if (is_client == CLIENT_MODE) {
1269 {
1270 if (is_registrar) {
1271 if (!is_config)
1272 intVal = MODE_CLIENT_UNCONFIG_REGISTRAR;
1273 else
1274 intVal = MODE_CLIENT_CONFIG;
1275 }
1276 else
1277 intVal = MODE_CLIENT_UNCONFIG;
1278 }
1279 }
1280 else {
1281 if (!is_config)
1282 intVal = MODE_AP_UNCONFIG;
1283 else
1284 intVal = MODE_AP_PROXY_REGISTRAR;
1285 }
1286 WRITE_WSC_PARAM(ptr, tmpbuf, "mode = %d\n", intVal);
1287
1288
1289 if (is_client == CLIENT_MODE) {
1290 intVal = 0;
1291 }
1292 else
1293 intVal = isUpnpEnabled;
1294
1295 WRITE_WSC_PARAM(ptr, tmpbuf, "upnp = %d\n", intVal);
1296
1297 intVal = 0;
1298 intVal = wsc_method;
1299 //Ethernet(0x2)+Label(0x4)+PushButton(0x80) Bitwise OR
1300 if (intVal == 1) //Pin+Ethernet
1301 intVal = (CONFIG_METHOD_ETH | CONFIG_METHOD_PIN);
1302 else if (intVal == 2) //PBC+Ethernet
1303 intVal = (CONFIG_METHOD_ETH | CONFIG_METHOD_PBC);
1304 if (intVal == 3) //Pin+PBC+Ethernet
1305 intVal = (CONFIG_METHOD_ETH | CONFIG_METHOD_PIN | CONFIG_METHOD_PBC);
1306 WRITE_WSC_PARAM(ptr, tmpbuf, "config_method = %d\n", intVal);
1307
1308 // 1104
1309 WRITE_WSC_PARAM(ptr, tmpbuf, "wlan0_wsc_disabled = %d\n", wlan0_wsc_disabled);
1310
1311 WRITE_WSC_PARAM(ptr, tmpbuf, "auth_type = %d\n", wsc_auth);
1312
1313 WRITE_WSC_PARAM(ptr, tmpbuf, "encrypt_type = %d\n", wsc_enc);
1314
1315 if (wsc_enc == WSC_ENCRYPT_WEP)
1316 is_wep = 1;
1317
1318 if(wlanif_name != NULL && (strcmp(wlanif_name,"wlan0") == 0 || strcmp(wlanif_name,"wlan1") == 0))
1319 {
1320 if (is_client == CLIENT_MODE)
1321 {
1322 if (wlan_network_type == 0)
1323 intVal = 1;
1324 else
1325 intVal = 2;
1326 }
1327 else
1328 intVal = 1;
1329 }
1330 else
1331 intVal = 1;
1332
1333 WRITE_WSC_PARAM(ptr, tmpbuf, "connection_type = %d\n", intVal);
1334
1335 WRITE_WSC_PARAM(ptr, tmpbuf, "manual_config = %d\n", wsc_manual_enabled);
1336
1337
1338 if (is_wep) { // only allow WEP in none-MANUAL mode (configured by external registrar)
1339 if (wlan_encrpty != ENCRYPT_WEP) {
1340 printf("WEP mismatched between WPS and host system\n");
1341 free(buf);
1342 return -1;
1343 }
1344 if (wlan_wep <= WEP_DISABLED || wlan_wep > WEP128) {
1345 printf("WEP encrypt length error\n");
1346 free(buf);
1347 return -1;
1348 }
1349
1350 wep_transmit_key++;
1351 WRITE_WSC_PARAM(ptr, tmpbuf, "wep_transmit_key = %d\n", wep_transmit_key);
1352
1353 /*whatever key type is ASSIC or HEX always use String-By-Hex fromat
1354 ;2011-0419,fixed,need patch with wscd daemon , search 2011-0419*/
1355 if (wlan_wep == WEP64) {
1356 // apmib_get(MIB_WLAN_WEP64_KEY1, (void *)&wlan_wep64_key1);
1357
1358 if (wep_key_type == KEY_ASCII) {
1359 sprintf(config_path, "%s%s", default_config_path, "wepkey1_64_asc");
1360 get_result = get_file_content(config_path, config_value);
1361 sprintf(tmpbuf, "%s", config_value);
1362 memcpy(tmp1, tmpbuf, 5);
1363 tmp1[5] = '\0';
1364 }
1365 else {
1366 sprintf(config_path, "%s%s", default_config_path, "wepkey1_64_hex");
1367 get_result = get_file_content(config_path, config_value);
1368 sprintf(tmp1, "%s", config_value);
1369 }
1370 WRITE_WSC_PARAM(ptr, tmpbuf, "network_key = \"%s\"\n", tmp1);
1371
1372//apmib_get(MIB_WLAN_WEP64_KEY2, (void *)&wlan_wep64_key2);
1373 if (wep_key_type == KEY_ASCII) {
1374 sprintf(config_path, "%s%s", default_config_path, "wepkey2_64_asc");
1375 get_result = get_file_content(config_path, config_value);
1376 sprintf(tmpbuf, "%s", config_value);
1377 memcpy(tmp1, tmpbuf, 5);
1378 tmp1[5] = '\0';
1379 }
1380 else {
1381 sprintf(config_path, "%s%s", default_config_path, "wepkey2_64_hex");
1382 get_result = get_file_content(config_path, config_value);
1383 sprintf(tmp1, "%s", config_value);
1384 }
1385
1386 WRITE_WSC_PARAM(ptr, tmpbuf, "wep_key2 = %s\n", tmp1);
1387
1388//apmib_get(MIB_WLAN_WEP64_KEY3, (void *)&wlan_wep64_key3);
1389
1390 if (wep_key_type == KEY_ASCII) {
1391 sprintf(config_path, "%s%s", default_config_path, "wepkey3_64_asc");
1392 get_result = get_file_content(config_path, config_value);
1393 sprintf(tmpbuf, "%s", config_value);
1394 memcpy(tmp1, tmpbuf, 5);
1395 tmp1[5] = '\0';
1396 }
1397 else {
1398 sprintf(config_path, "%s%s", default_config_path, "wepkey3_64_hex");
1399 get_result = get_file_content(config_path, config_value);
1400 sprintf(tmp1, "%s", config_value);
1401 }
1402
1403 WRITE_WSC_PARAM(ptr, tmpbuf, "wep_key3 = %s\n", tmp1);
1404
1405//apmib_get(MIB_WLAN_WEP64_KEY4, (void *)&wlan_wep64_key4);
1406
1407 if (wep_key_type == KEY_ASCII) {
1408 sprintf(config_path, "%s%s", default_config_path, "wepkey4_64_asc");
1409 get_result = get_file_content(config_path, config_value);
1410 sprintf(tmpbuf, "%s", config_value);
1411 memcpy(tmp1, tmpbuf, 5);
1412 tmp1[5] = '\0';
1413 }
1414 else {
1415 sprintf(config_path, "%s%s", default_config_path, "wepkey4_64_hex");
1416 get_result = get_file_content(config_path, config_value);
1417 sprintf(tmp1, "%s", config_value);
1418 }
1419
1420 WRITE_WSC_PARAM(ptr, tmpbuf, "wep_key4 = %s\n", tmp1);
1421 }
1422 else {
1423 //apmib_get(MIB_WLAN_WEP128_KEY1, (void *)&wlan_wep128_key1);
1424
1425 if (wep_key_type == KEY_ASCII) {
1426 sprintf(config_path, "%s%s", default_config_path, "wepkey1_128_asc");
1427 get_result = get_file_content(config_path, config_value);
1428 sprintf(tmpbuf, "%s", config_value);
1429 memcpy(tmp1, tmpbuf, 13);
1430 tmp1[13] = '\0';
1431 }
1432 else {
1433 sprintf(config_path, "%s%s", default_config_path, "wepkey1_128_hex");
1434 get_result = get_file_content(config_path, config_value);
1435 sprintf(tmp1, "%s", config_value);
1436 }
1437
1438
1439 WRITE_WSC_PARAM(ptr, tmpbuf, "network_key = \"%s\"\n", tmp1);
1440//apmib_get(MIB_WLAN_WEP128_KEY2, (void *)&wlan_wep128_key2);
1441
1442 if (wep_key_type == KEY_ASCII) {
1443 sprintf(config_path, "%s%s", default_config_path, "wepkey2_128_asc");
1444 get_result = get_file_content(config_path, config_value);
1445 sprintf(tmpbuf, "%s", config_value);
1446 memcpy(tmp1, tmpbuf, 13);
1447 tmp1[13] = '\0';
1448 }
1449 else {
1450 sprintf(config_path, "%s%s", default_config_path, "wepkey2_128_hex");
1451 get_result = get_file_content(config_path, config_value);
1452 sprintf(tmp1, "%s", config_value);
1453 }
1454
1455 WRITE_WSC_PARAM(ptr, tmpbuf, "wep_key2 = %s\n", tmp1);
1456//apmib_get(MIB_WLAN_WEP128_KEY3, (void *)&wlan_wep128_key3);
1457
1458 if (wep_key_type == KEY_ASCII) {
1459 sprintf(config_path, "%s%s", default_config_path, "wepkey3_128_asc");
1460 get_result = get_file_content(config_path, config_value);
1461 sprintf(tmpbuf, "%s", config_value);
1462 memcpy(tmp1, tmpbuf, 13);
1463 tmp1[13] = '\0';
1464 }
1465 else {
1466 sprintf(config_path, "%s%s", default_config_path, "wepkey3_128_hex");
1467 get_result = get_file_content(config_path, config_value);
1468 sprintf(tmp1, "%s", config_value);
1469 }
1470
1471 WRITE_WSC_PARAM(ptr, tmpbuf, "wep_key3 = %s\n", tmp1);
1472
1473//apmib_get(MIB_WLAN_WEP128_KEY4, (void *)&wlan_wep128_key4);
1474
1475 if (wep_key_type == KEY_ASCII) {
1476 sprintf(config_path, "%s%s", default_config_path, "wepkey4_128_asc");
1477 get_result = get_file_content(config_path, config_value);
1478 sprintf(tmpbuf, "%s", config_value);
1479 memcpy(tmp1, tmpbuf, 13);
1480 tmp1[13] = '\0';
1481 }
1482 else {
1483 sprintf(config_path, "%s%s", default_config_path, "wepkey4_128_hex");
1484 get_result = get_file_content(config_path, config_value);
1485 sprintf(tmp1, "%s", config_value);
1486 }
1487 WRITE_WSC_PARAM(ptr, tmpbuf, "wep_key4 = %s\n", tmp1);
1488 }
1489 }
1490 else {
1491 sprintf(config_path, "%s%s", default_config_path, "wpa_psk");
1492 get_result = get_file_content(config_path, config_value);
1493 sprintf(tmp1, "%s", config_value);
1494 WRITE_WSC_PARAM(ptr, tmpbuf, "network_key = \"%s\"\n", tmp1);
1495 }
1496
1497 sprintf(config_path, "%s%s", default_config_path, "ssid");
1498 get_result = get_file_content(config_path, config_value);
1499 sprintf(tmp1, "%s", config_value);
1500 WRITE_WSC_PARAM(ptr, tmpbuf, "ssid = \"%s\"\n", tmp1);
1501
1502 sprintf(config_path, "%s%s", default_config_path, "wsc_pin");
1503 get_result = get_file_content(config_path, config_value);
1504 //config_value[strlen(config_value)-1]='\0';
1505 sprintf(tmp1, "%s", config_value);
1506 WRITE_WSC_PARAM(ptr, tmpbuf, "pin_code = %s\n", tmp1);
1507
1508
1509 if (wlan_chan_num > 14)
1510 intVal = 2;
1511 else
1512 intVal = 1;
1513 WRITE_WSC_PARAM(ptr, tmpbuf, "rf_band = %d\n", intVal);
1514
1515#ifdef FOR_DUAL_BAND
1516 /* switch to wlan1 */
1517
1518 //sprintf(default_config_path, WIFI_CONFIG_ROOT_DIR "/%s/", "wlan1");
1519 sprintf(default_config_path, "%s/%s/", ConfigFilePath,"wlan1");
1520
1521 //apmib_get(MIB_WLAN_WSC_AUTH, (void *)&wsc_auth);
1522
1523 sprintf(config_path, "%s%s", default_config_path, "wsc_auth");
1524 get_result = get_file_content(config_path, config_value);
1525 wsc_auth = atoi(config_value);
1526
1527 //apmib_get(MIB_WLAN_WSC_ENC, (void *)&wsc_enc);
1528
1529 sprintf(config_path, "%s%s", default_config_path, "wsc_enc");
1530 get_result = get_file_content(config_path, config_value);
1531 wsc_enc = atoi(config_value);
1532
1533// apmib_get(MIB_WLAN_SSID, (void *)&wlan_ssid);
1534
1535 sprintf(config_path, "%s%s", default_config_path, "ssid");
1536 get_result = get_file_content(config_path, config_value);
1537 sprintf(wlan_ssid, "%s", config_value);
1538
1539
1540// apmib_get(MIB_WLAN_MODE, (void *)&is_client);
1541
1542 sprintf(config_path, "%s%s", default_config_path, "wlan_mode");
1543 get_result = get_file_content(config_path, config_value);
1544 is_client = atoi(config_value);
1545
1546 //apmib_get(MIB_WLAN_ENCRYPT, (void *)&wlan_encrpty);
1547
1548 sprintf(config_path, "%s%s", default_config_path, "encrypt");
1549 get_result = get_file_content(config_path, config_value);
1550 wlan_encrpty = atoi(config_value);
1551
1552
1553 //apmib_get(MIB_WLAN_WEP, (void *)&wlan_wep);
1554
1555 sprintf(config_path, "%s%s", default_config_path, "wep");
1556 get_result = get_file_content(config_path, config_value);
1557 wlan_wep = atoi(config_value);
1558
1559 //apmib_get(MIB_WLAN_WEP_KEY_TYPE, (void *)&wep_key_type);
1560
1561 sprintf(config_path, "%s%s", default_config_path, "wep_key_type");
1562 get_result = get_file_content(config_path, config_value);
1563 wep_key_type = atoi(config_value);
1564
1565 //apmib_get(MIB_WLAN_WEP_DEFAULT_KEY, (void *)&wep_transmit_key);
1566
1567 sprintf(config_path, "%s%s", default_config_path, "wep_default_key");
1568 get_result = get_file_content(config_path, config_value);
1569 wep_transmit_key = atoi(config_value);
1570
1571
1572
1573
1574
1575
1576 //apmib_get(MIB_WLAN_WSC_DISABLE, (void *)&wlan1_wsc_disabled); // 1104
1577
1578 sprintf(config_path, "%s%s", default_config_path, "wsc_disabled");
1579 get_result = get_file_content(config_path, config_value);
1580 wlan1_wsc_disabled = atoi(config_value);
1581
1582
1583 //apmib_get(MIB_WLAN_WLAN_DISABLED, (void *)&wlan1_wlan_disabled); // 0908
1584 sprintf(config_path, "%s%s", default_config_path, "wlan_disabled");
1585 get_result = get_file_content(config_path, config_value);
1586 wlan1_wlan_disabled = atoi(config_value);
1587
1588
1589
1590 if(wlan1_wlan_disabled)
1591 wlan1_wsc_disabled = 1 ; // if wlan1 interface is disabled
1592
1593
1594 WRITE_WSC_PARAM(ptr, tmpbuf, "#=====wlan1 start==========%d\n",intVal);
1595 WRITE_WSC_PARAM(ptr, tmpbuf, "ssid2 = \"%s\"\n",wlan_ssid );
1596 WRITE_WSC_PARAM(ptr, tmpbuf, "auth_type2 = %d\n", wsc_auth);
1597 WRITE_WSC_PARAM(ptr, tmpbuf, "encrypt_type2 = %d\n", wsc_enc);
1598
1599 // 1104
1600 if(band_select_5g2g!=2) // != dual band
1601 {
1602 intVal=1;
1603 WRITE_WSC_PARAM(ptr, tmpbuf, "wlan1_wsc_disabled = %d\n",intVal);
1604 }
1605 else // else see if wlan1 is enabled
1606 {
1607 WRITE_WSC_PARAM(ptr, tmpbuf, "wlan1_wsc_disabled = %d\n", wlan1_wsc_disabled);
1608 }
1609
1610 is_wep = 0;
1611
1612 if (wsc_enc == WSC_ENCRYPT_WEP)
1613 is_wep = 1;
1614
1615 if (is_wep) { // only allow WEP in none-MANUAL mode (configured by external registrar)
1616 if (wlan_encrpty != ENCRYPT_WEP) {
1617 printf("WEP mismatched between WPS and host system\n");
1618 free(buf);
1619 return -1;
1620 }
1621 if (wlan_wep <= WEP_DISABLED || wlan_wep > WEP128) {
1622 printf("WEP encrypt length error\n");
1623 free(buf);
1624 return -1;
1625 }
1626
1627 wep_transmit_key++;
1628 WRITE_WSC_PARAM(ptr, tmpbuf, "wep_transmit_key2 = %d\n", wep_transmit_key);
1629
1630
1631 /*whatever key type is ASSIC or HEX always use String-By-Hex fromat
1632 ;2011-0419,fixed,need patch with wscd daemon , search 2011-0419*/
1633 if (wlan_wep == WEP64) {
1634
1635
1636 //apmib_get(MIB_WLAN_WEP64_KEY1, (void *)&wlan_wep64_key1);
1637
1638 if (wep_key_type == KEY_ASCII) {
1639 sprintf(config_path, "%s%s", default_config_path, "wepkey1_64_asc");
1640 get_result = get_file_content(config_path, config_value);
1641 sprintf(tmpbuf, "%s", config_value);
1642 memcpy(tmp1, tmpbuf, 5);
1643 tmp1[5] = '\0';
1644 }
1645 else {
1646 sprintf(config_path, "%s%s", default_config_path, "wepkey1_64_hex");
1647 get_result = get_file_content(config_path, config_value);
1648 sprintf(tmp1, "%s", config_value);
1649 }
1650
1651
1652 WRITE_WSC_PARAM(ptr, tmpbuf, "network_key2 = \"%s\"\n", tmp1);
1653
1654
1655
1656
1657
1658 //apmib_get(MIB_WLAN_WEP64_KEY2, (void *)&wlan_wep64_key2);
1659 if (wep_key_type == KEY_ASCII) {
1660 sprintf(config_path, "%s%s", default_config_path, "wepkey2_64_asc");
1661 get_result = get_file_content(config_path, config_value);
1662 sprintf(tmpbuf, "%s", config_value);
1663 memcpy(tmp1, tmpbuf, 5);
1664 tmp1[5] = '\0';
1665 }
1666 else {
1667 sprintf(config_path, "%s%s", default_config_path, "wepkey2_64_hex");
1668 get_result = get_file_content(config_path, config_value);
1669 sprintf(tmp1, "%s", config_value);
1670 }
1671
1672 WRITE_WSC_PARAM(ptr, tmpbuf, "wep_key22 = %s\n", tmp1);
1673
1674
1675 //apmib_get(MIB_WLAN_WEP64_KEY3, (void *)&wlan_wep64_key3);
1676 if (wep_key_type == KEY_ASCII) {
1677 sprintf(config_path, "%s%s", default_config_path, "wepkey3_64_asc");
1678 get_result = get_file_content(config_path, config_value);
1679 sprintf(tmpbuf, "%s", config_value);
1680 memcpy(tmp1, tmpbuf, 5);
1681 tmp1[5] = '\0';
1682 }
1683 else {
1684 sprintf(config_path, "%s%s", default_config_path, "wepkey3_64_hex");
1685 get_result = get_file_content(config_path, config_value);
1686 sprintf(tmp1, "%s", config_value);
1687 }
1688 WRITE_WSC_PARAM(ptr, tmpbuf, "wep_key32 = %s\n", tmp1);
1689
1690
1691
1692 //apmib_get(MIB_WLAN_WEP64_KEY4, (void *)&wlan_wep64_key4);
1693 if (wep_key_type == KEY_ASCII) {
1694 sprintf(config_path, "%s%s", default_config_path, "wepkey4_64_asc");
1695 get_result = get_file_content(config_path, config_value);
1696 sprintf(tmpbuf, "%s", config_value);
1697 memcpy(tmp1, tmpbuf, 5);
1698 tmp1[5] = '\0';
1699 }
1700 else {
1701 sprintf(config_path, "%s%s", default_config_path, "wepkey4_64_hex");
1702 get_result = get_file_content(config_path, config_value);
1703 sprintf(tmp1, "%s", config_value);
1704 }
1705 WRITE_WSC_PARAM(ptr, tmpbuf, "wep_key42 = %s\n", tmp1);
1706 }
1707 else {
1708
1709
1710 //apmib_get(MIB_WLAN_WEP128_KEY1, (void *)&wlan_wep128_key1);
1711 if (wep_key_type == KEY_ASCII) {
1712 sprintf(config_path, "%s%s", default_config_path, "wepkey1_128_asc");
1713 get_result = get_file_content(config_path, config_value);
1714 sprintf(tmpbuf, "%s", config_value);
1715 memcpy(tmp1, tmpbuf, 13);
1716 tmp1[13] = '\0';
1717 }
1718 else {
1719 sprintf(config_path, "%s%s", default_config_path, "wepkey1_128_hex");
1720 get_result = get_file_content(config_path, config_value);
1721 sprintf(tmp1, "%s", config_value);
1722 }
1723 WRITE_WSC_PARAM(ptr, tmpbuf, "network_key2 = \"%s\"\n", tmp1);
1724
1725 //apmib_get(MIB_WLAN_WEP128_KEY2, (void *)&wlan_wep128_key2);
1726
1727 if (wep_key_type == KEY_ASCII) {
1728 sprintf(config_path, "%s%s", default_config_path, "wepkey2_128_asc");
1729 get_result = get_file_content(config_path, config_value);
1730 sprintf(tmpbuf, "%s", config_value);
1731 memcpy(tmp1, tmpbuf, 13);
1732 tmp1[13] = '\0';
1733 }
1734 else {
1735 sprintf(config_path, "%s%s", default_config_path, "wepkey2_128_hex");
1736 get_result = get_file_content(config_path, config_value);
1737 sprintf(tmp1, "%s", config_value);
1738 }
1739 WRITE_WSC_PARAM(ptr, tmpbuf, "wep_key22 = %s\n", tmp1);
1740
1741 //apmib_get(MIB_WLAN_WEP128_KEY3, (void *)&wlan_wep128_key3);
1742 if (wep_key_type == KEY_ASCII) {
1743 sprintf(config_path, "%s%s", default_config_path, "wepkey3_128_asc");
1744 get_result = get_file_content(config_path, config_value);
1745 sprintf(tmpbuf, "%s", config_value);
1746 memcpy(tmp1, tmpbuf, 13);
1747 tmp1[13] = '\0';
1748 }
1749 else {
1750 sprintf(config_path, "%s%s", default_config_path, "wepkey3_128_hex");
1751 get_result = get_file_content(config_path, config_value);
1752 sprintf(tmp1, "%s", config_value);
1753 }
1754
1755
1756 WRITE_WSC_PARAM(ptr, tmpbuf, "wep_key32 = %s\n", tmp1);
1757
1758 //apmib_get(MIB_WLAN_WEP128_KEY4, (void *)&wlan_wep128_key4);
1759
1760 if (wep_key_type == KEY_ASCII) {
1761 sprintf(config_path, "%s%s", default_config_path, "wepkey4_128_asc");
1762 get_result = get_file_content(config_path, config_value);
1763 sprintf(tmpbuf, "%s", config_value);
1764 memcpy(tmp1, tmpbuf, 13);
1765 tmp1[13] = '\0';
1766 }
1767 else {
1768 sprintf(config_path, "%s%s", default_config_path, "wepkey4_128_hex");
1769 get_result = get_file_content(config_path, config_value);
1770 sprintf(tmp1, "%s", config_value);
1771 }
1772
1773 WRITE_WSC_PARAM(ptr, tmpbuf, "wep_key42 = %s\n", tmp1);
1774 }
1775 }
1776 else {
1777
1778 //apmib_get(MIB_WLAN_WPA_PSK, (void *)&wlan_wpa_psk);
1779 sprintf(config_path, "%s%s", default_config_path, "wpa_psk");
1780 get_result = get_file_content(config_path, config_value);
1781 sprintf(wlan_wpa_psk, "%s", config_value);
1782 WRITE_WSC_PARAM(ptr, tmpbuf, "network_key2 = \"%s\"\n", wlan_wpa_psk);
1783 }
1784
1785
1786
1787 intVal =2 ;
1788 WRITE_WSC_PARAM(ptr, tmpbuf, "#=====wlan1 end==========%d\n",intVal);
1789
1790 /*sync the PIN code of wlan0 and wlan1*/
1791 //apmib_set(MIB_HW_WSC_PIN, (void *)wsc_pin);
1792
1793
1794
1795
1796#endif // END of FOR_DUAL_BAND
1797 WRITE_WSC_PARAM(ptr, tmpbuf, "device_name = \"%s\"\n", device_name);
1798
1799 WRITE_WSC_PARAM(ptr, tmpbuf, "config_by_ext_reg = %d\n", wsc_config_by_ext_reg);
1800
1801 len = (int)(((long)ptr)-((long)buf));
1802
1803 fh = open(in, O_RDONLY);
1804 if (fh == -1) {
1805 printf("open() error [%s]!\n", in);
1806 return -1;
1807 }
1808
1809 lseek(fh, 0L, SEEK_SET);
1810 if (read(fh, ptr, status.st_size) != status.st_size) {
1811 printf("read() error [%s]!\n", in);
1812 return -1;
1813 }
1814 close(fh);
1815
1816 // search UUID field, replace last 12 char with hw mac address
1817 ptr = strstr(ptr, "uuid =");
1818 if (ptr) {
1819 char tmp2[100];
1820// apmib_get(MIB_HW_NIC0_ADDR, (void *)&tmp1);
1821 sprintf(tmp_config_path, "%s%s", default_config_path, "nic0_addr");
1822 get_result = get_file_content(tmp_config_path, tmp_config_value);
1823 if(get_result != -1){
1824 string_to_hex(tmp_config_value,tmp1, 12);
1825 convert_bin_to_str(tmp1, 6, tmp2);
1826 memcpy(ptr+27, tmp2, 12);
1827 }
1828 }
1829
1830 fh = open(out, O_RDWR|O_CREAT|O_TRUNC, S_IWGRP|S_IRGRP);
1831 if (fh == -1) {
1832 printf("open() error [%s]!\n", out);
1833 return -1;
1834 }
1835
1836 if (write(fh, buf, len+status.st_size) != len+status.st_size ) {
1837 printf("Write() file error [%s]!\n", out);
1838 return -1;
1839 }
1840 close(fh);
1841 free(buf);
1842
1843
1844
1845 return 0;
1846}
1847#endif
1848
1849
1850
1851#if READ_MIB_FROM_DEVICE
1852
1853#define IW_MAX_PRIV_DEF 64
1854int get_priv_info_flag=0;
1855int priv_info_number;
1856
1857int iw_get_priv_info(int skfd, char * ifname, struct iw_priv_args * priv, int maxpriv )
1858{
1859 struct iwreq wrq;
1860 /* Ask the driver */
1861 wrq.u.data.pointer = (caddr_t) priv;
1862 wrq.u.data.length = maxpriv;
1863 wrq.u.data.flags = 0;
1864
1865
1866 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
1867 if( ioctl(skfd, SIOCGIWPRIV , &wrq)<0 )
1868 return(-1);
1869
1870 //if(iw_get_ext(skfd, ifname, SIOCGIWPRIV, &wrq) < 0)
1871 // return(-1);
1872
1873
1874 return(wrq.u.data.length);
1875}
1876
1877 struct iw_priv_args priv[IW_MAX_PRIV_DEF];
1878
1879static int
1880mib_cmd( char * ifname, /* Dev name */
1881 char * cmdname, /* Command name */
1882 char * args, /* Command line args */
1883 char * out_buf,
1884 int out_buf_max_len )
1885{
1886 struct iwreq wrq,*pwrq;
1887 u_char buffer[4096]; /* Only that big in v25 and later */
1888 int i = 0,j; /* Start with first command arg */
1889 int k; /* Index in private description table */
1890 int temp;
1891 int subcmd = 0; /* sub-ioctl index */
1892
1893
1894
1895 int number; /* Max of private ioctl */
1896 int skfd;
1897
1898 pwrq = &wrq;
1899
1900 skfd = socket( AF_INET , SOCK_DGRAM, 0);
1901 if(skfd < 0)
1902 {
1903 printf("open socket failure skfd=%d\n",skfd);
1904 return skfd;
1905 }
1906
1907
1908 if( get_priv_info_flag==0 )
1909 {
1910 memset( priv , 0 , sizeof(priv) );
1911 number = iw_get_priv_info(skfd, ifname, priv, IW_MAX_PRIV_DEF);
1912 if(number <= 0)
1913 {
1914 /* Could skip this message ? */
1915 fprintf(stderr, "%-8.8s no private ioctls.\n\n",ifname);
1916 close(skfd);
1917 return(-1);
1918 }
1919 get_priv_info_flag=1;
1920 priv_info_number = number;
1921
1922 }
1923 else
1924 {
1925 number = priv_info_number;
1926 }
1927
1928 /*for( j=0 ; j<number ; j++ )
1929 {
1930 printf("args[%d]=%08x, set_args=%04x, get_args=%04x, name=%s\n", j , priv[j].cmd , priv[j].set_args , priv[j].get_args , priv[j].name );
1931 }
1932 printf("set_private number=%d\n", number );*/
1933 //printf("private_cmd, args[0]=%s, ifname=%s, cmdname=%s\n",args[0], ifname, cmdname );
1934
1935
1936 /* Search the correct ioctl */
1937 k = -1;
1938 while((++k < number) && strcmp(priv[k].name, cmdname));
1939 if(k == number)
1940 {
1941 fprintf(stderr, "Invalid command : %s\n", cmdname);
1942 close(skfd);
1943 return(-1);
1944 }
1945
1946 /* If we have to set some data */
1947 if((priv[k].set_args & IW_PRIV_TYPE_MASK) && (priv[k].set_args & IW_PRIV_SIZE_MASK))
1948 {
1949 switch(priv[k].set_args & IW_PRIV_TYPE_MASK)
1950 {
1951 case IW_PRIV_TYPE_CHAR:
1952 printf("IW_PRIV_TYPE_CHAR\n");
1953 /* Size of the string to fetch */
1954 pwrq->u.data.length = strlen(args) + 1;
1955 if(pwrq->u.data.length > (priv[k].set_args & IW_PRIV_SIZE_MASK))
1956 pwrq->u.data.length = priv[k].set_args & IW_PRIV_SIZE_MASK;
1957
1958 /* Fetch string */
1959 memcpy(buffer, args, pwrq->u.data.length);
1960 buffer[sizeof(buffer) - 1] = '\0';
1961 i++;
1962 break;
1963
1964 default:
1965 printf("%s %d k=%d number=%d\n",__FUNCTION__,__LINE__,k,number);
1966 fprintf(stderr, "Not yet implemented...\n");
1967 close(skfd);
1968 return(-1);
1969 }
1970
1971 if((priv[k].set_args & IW_PRIV_SIZE_FIXED) && (pwrq->u.data.length != (priv[k].set_args & IW_PRIV_SIZE_MASK)))
1972 {
1973 printf("The command %s need exactly %d argument...\n",cmdname, priv[k].set_args & IW_PRIV_SIZE_MASK);
1974 close(skfd);
1975 return(-1);
1976 }
1977 } /* if args to set */
1978 else
1979 {
1980 printf("ttt4\n");
1981 printf("length=0?\n");
1982 pwrq->u.data.length = 0L;
1983 }
1984
1985
1986 strncpy(pwrq->ifr_name, ifname, IFNAMSIZ);
1987 pwrq->u.data.pointer = (caddr_t) buffer;
1988 pwrq->u.data.flags = subcmd;
1989
1990 printf("cmd=%d ifname=%s subcmd=%08x\n",priv[k].cmd,pwrq->ifr_name,subcmd);
1991 /* Perform the private ioctl */
1992 if(ioctl(skfd, priv[k].cmd , pwrq) < 0)
1993 {
1994 fprintf(stderr, "Interface doesn't accept private ioctl...\n");
1995 close(skfd);
1996 return(-1);
1997 }
1998 //printf("out_buf_max_len=%d\n",out_buf_max_len);
1999
2000 if( pwrq->u.data.length < 4 )
2001 {
2002 memset( out_buf , 0 , 4 );
2003 memcpy( out_buf , pwrq->u.data.pointer , pwrq->u.data.length );
2004 pwrq->u.data.length=4;
2005 }
2006 else
2007 {
2008 if( pwrq->u.data.length > out_buf_max_len )
2009 {
2010 pwrq->u.data.length=out_buf_max_len;
2011 }
2012 memcpy( out_buf , pwrq->u.data.pointer , pwrq->u.data.length );
2013 }
2014
2015 //printf("pwrq->u.data.length=%d\n",pwrq->u.data.length);
2016
2017 printf("data=\n");
2018 for( i=0 ; i<pwrq->u.data.length ; i++ )
2019 {
2020 printf("%02x ",out_buf[i]);
2021 }
2022 printf("\n");
2023
2024 close(skfd);
2025 return pwrq->u.data.length;
2026}
2027
2028
2029static void generateWpaConf(char *outputFile, int isWds, char *iface)
2030{
2031 int fh, intVal, encrypt=0, enable1x, wep,encmode,psk_enable;
2032 char buf1[1024], buf2[1024];
2033 char config_path[256]={0};
2034 char config_value[100]={0};
2035 char tmp1[100], tmpbuf[100];
2036 int get_result=0;
2037 char default_config_path[256]={0};
2038 int repeater_enabled=0;
2039 char buf[128];
2040 int len,i;
2041
2042 //sprintf(default_config_path, WIFI_CONFIG_ROOT_DIR "/%s/", iface);
2043 sprintf(default_config_path, "%s/%s/",ConfigFilePath, iface);
2044 fh = open(outputFile, O_RDWR|O_CREAT|O_TRUNC, S_IRGRP|S_IWGRP);
2045 if (fh == -1) {
2046 printf("Create WPA config file error!\n");
2047 return;
2048 }
2049
2050
2051 if (!isWds)
2052 {
2053 len=mib_cmd( iface , "get_mib" , "psk_enable" , buf , sizeof(buf) );
2054 if( len<0 )
2055 {
2056 printf("ERROR : Execute mib_cmd wepkey1 failure\n");
2057 return;
2058 }
2059 else
2060 {
2061 psk_enable = *(int *)buf;
2062 printf("psk_enable = %d\n", psk_enable );
2063 }
2064
2065
2066
2067 len=mib_cmd( iface , "get_mib" , "encmode" , buf , sizeof(buf) );
2068 if( len<0 )
2069 {
2070 printf("ERROR : Execute mib_cmd failure\n");
2071 return;
2072 }
2073 else
2074 {
2075 encmode = *(int *)buf;
2076 if( encmode==1 || encmode==5 )
2077 {
2078 encrypt = 1;
2079 }
2080 else
2081 {
2082 if( psk_enable==1 )
2083 {
2084 encrypt=2;
2085 }
2086 else if( psk_enable==2 )
2087 {
2088 encrypt=4;
2089 }
2090 else if( psk_enable==3 )
2091 {
2092 encrypt=6;
2093 }
2094 else
2095 {
2096 printf("unknow mode psk_enable=%d encmode=%d",psk_enable,encmode);
2097 }
2098
2099 }
2100 printf("2.encrypt=%d\n",encrypt);
2101 }
2102 sprintf(buf2, "encryption = %d\n", encrypt);
2103 WRITE_WPA_FILE(fh, buf2);
2104
2105
2106 if(!strcmp(iface, "wlan0-vxd"))
2107 {
2108 //sprintf(config_path, WIFI_CONFIG_ROOT_DIR "/%s", "repeater_enabled");
2109 sprintf(config_path, "%s/%s", ConfigFilePath,"repeater_enabled");
2110 get_result = get_file_content(config_path, config_value);
2111 repeater_enabled = atoi(config_value);
2112 if(repeater_enabled==1)
2113 {
2114 //sprintf(config_path, WIFI_CONFIG_ROOT_DIR "/%s", "repeater_ssid");
2115 sprintf(config_path, "%s/%s", ConfigFilePath , "repeater_ssid");
2116 get_result = get_file_content(config_path, config_value);
2117 }
2118 else
2119 {
2120 //sprintf(config_path, "%s%s", default_config_path, "ssid");
2121 goto aa;
2122 }
2123 }
2124 else
2125 {
2126aa:
2127 //sprintf(config_path, "%s%s", default_config_path, "ssid");
2128 len=mib_cmd( iface , "get_mib" , "ssid" , config_value , sizeof(config_value) );
2129 if( len<0 )
2130 {
2131 printf("ERROR : Execute mib_cmd ssid failure\n");
2132 return -1;
2133 }
2134 else
2135 {
2136 config_value[len]=0;
2137 printf("ssid=%s\n",config_value);
2138 }
2139 }
2140 sprintf(buf1,"%s", config_value);
2141 sprintf(buf2, "ssid = \"%s\"\n", buf1);
2142 WRITE_WPA_FILE(fh, buf2);
2143
2144
2145
2146 sprintf(config_path, "%s%s", default_config_path, "enable_1x");
2147 get_result = get_file_content(config_path, config_value);
2148 enable1x = atoi(config_value);
2149 sprintf(buf2, "enable1x = %d\n", enable1x);
2150 WRITE_WPA_FILE(fh, buf2);
2151
2152
2153
2154 sprintf(config_path, "%s%s", default_config_path, "mac_auth_enabled");
2155 get_result = get_file_content(config_path, config_value);
2156 intVal = atoi(config_value);
2157 sprintf(buf2, "enableMacAuth = %d\n", intVal);
2158 WRITE_WPA_FILE(fh, buf2);
2159
2160
2161 sprintf(config_path, "%s%s", default_config_path, "enable_supp_nonwpa");
2162 get_result = get_file_content(config_path, config_value);
2163 intVal = atoi(config_value);
2164 if (intVal){
2165 sprintf(config_path, "%s%s", default_config_path, "supp_nonwpa");
2166 get_result = get_file_content(config_path, config_value);
2167 intVal = atoi(config_value);
2168 }
2169 sprintf(buf2, "supportNonWpaClient = %d\n", intVal);
2170 WRITE_WPA_FILE(fh, buf2);
2171
2172
2173 if( encmode==1 )
2174 {
2175 sprintf(buf1,"1");
2176 }
2177 else if( encmode==5 )
2178 {
2179 sprintf(buf1,"2");
2180 }
2181 else
2182 {
2183 buf1[0]=0;
2184 }
2185 sprintf(buf2, "wepKey = %s\n", buf1);
2186 WRITE_WPA_FILE(fh, buf2);
2187
2188
2189 if ( encrypt==1 && enable1x ) {
2190 len=mib_cmd( iface , "get_mib" , "wepkey1" , buf , sizeof(buf) );
2191 if( len<0 )
2192 {
2193 printf("ERROR : Execute mib_cmd wepkey1 failure\n");
2194 return;
2195 }
2196 else
2197 {
2198 if( encmode==1 )
2199 {
2200 len=5;
2201 }
2202 else
2203 {
2204 len=13;
2205 }
2206
2207 memcpy( tmp1 , buf , len );
2208 tmp1[len] = '\0';
2209 if( len==5 )
2210 {
2211 sprintf(buf2, "wepGroupKey = \"%02x%02x%02x%02x%02x\"\n", tmp1[0],tmp1[1],tmp1[2],tmp1[3],tmp1[4]);
2212 }
2213 else if( len==13 )
2214 {
2215 sprintf(buf2, "wepGroupKey = \"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\"\n",
2216 tmp1[0],tmp1[1],tmp1[2],tmp1[3],tmp1[4],
2217 tmp1[5],tmp1[6],tmp1[7],tmp1[8],tmp1[9],
2218 tmp1[10],tmp1[11],tmp1[12]);
2219 }
2220 else
2221 {
2222 strcpy(buf2, "wepGroupKey = \"\"\n");
2223 printf("ERROR : error wepkey1 len=%d\n",len);
2224 }
2225 }
2226 }
2227 else
2228 strcpy(buf2, "wepGroupKey = \"\"\n");
2229 WRITE_WPA_FILE(fh, buf2);
2230
2231
2232
2233
2234 sprintf(config_path, "%s%s", default_config_path, "wpa_auth");
2235 if( get_file_content(config_path, config_value) < 0 )
2236 {
2237 printf("ERROR : %s %d : can't open %s\n",__FUNCTION__,__LINE__,config_path);
2238 intVal=0;
2239 }
2240 else
2241 {
2242 intVal = atoi(config_value);
2243 printf("1.authentication = %d\n", intVal);
2244 }
2245 sprintf(buf2, "authentication = %d\n", intVal);
2246 WRITE_WPA_FILE(fh, buf2);
2247
2248
2249
2250 len=mib_cmd( iface , "get_mib" , "wpa_cipher" , buf , sizeof(buf) );
2251 if( len<0 )
2252 {
2253 printf("ERROR : Execute mib_cmd wpa_cipher failure\n");
2254 return;
2255 }
2256 else
2257 {
2258 intVal=*(int *)buf;
2259 intVal=*(int *)buf;
2260 if( intVal==2 )
2261 {
2262 intVal=1;
2263 }
2264 else if( intVal==8 )
2265 {
2266 intVal=2;
2267 }
2268 else if( intVal==10 )
2269 {
2270 intVal=3;
2271 }
2272 printf("2.wpa_cipher=%d\n",intVal);
2273 }
2274 sprintf(buf2, "unicastCipher = %d\n", intVal);
2275 WRITE_WPA_FILE(fh, buf2);
2276
2277
2278 len=mib_cmd( iface , "get_mib" , "wpa2_cipher" , buf , sizeof(buf) );
2279 if( len<0 )
2280 {
2281 printf("ERROR : Execute mib_cmd wpa2_cipher failure\n");
2282 return;
2283 }
2284 else
2285 {
2286 intVal=*(int *)buf;
2287 if( intVal==2 )
2288 {
2289 intVal=1;
2290 }
2291 else if( intVal==8 )
2292 {
2293 intVal=2;
2294 }
2295 else if( intVal==10 )
2296 {
2297 intVal=3;
2298 }
2299 printf("2.wpa2_cipher=%d\n",intVal);
2300 }
2301 sprintf(buf2, "wpa2UnicastCipher = %d\n", intVal);
2302 WRITE_WPA_FILE(fh, buf2);
2303
2304
2305 sprintf(config_path, "%s%s", default_config_path, "wpa2_pre_auth");
2306 if( get_file_content(config_path, config_value) < 0 )
2307 {
2308 printf("%s %d\n",__FUNCTION__,__LINE__);
2309 }
2310 else
2311 {
2312 intVal = atoi(config_value);
2313 sprintf(buf2, "enablePreAuth = %d\n", intVal);
2314 printf("%s\n",buf2);
2315 }
2316 WRITE_WPA_FILE(fh, buf2);
2317
2318
2319 sprintf(config_path, "%s%s", default_config_path, "psk_format");
2320 get_result = get_file_content(config_path, config_value);
2321 intVal = atoi(config_value);
2322
2323 if (intVal==0)
2324 sprintf(buf2, "usePassphrase = 1\n");
2325 else
2326 sprintf(buf2, "usePassphrase = 0\n");
2327 WRITE_WPA_FILE(fh, buf2);
2328
2329
2330
2331 sprintf(config_path, "%s%s", default_config_path, "wpa_psk");
2332 get_result = get_file_content(config_path, config_value);
2333 sprintf(buf1, "%s", config_value);
2334
2335
2336 sprintf(buf2, "psk = \"%s\"\n", buf1);
2337 WRITE_WPA_FILE(fh, buf2);
2338
2339
2340
2341 sprintf(config_path, "%s%s", default_config_path, "gk_rekey");
2342 get_result = get_file_content(config_path, config_value);
2343 intVal = atoi(config_value);
2344 sprintf(buf2, "groupRekeyTime = %d\n", intVal);
2345 WRITE_WPA_FILE(fh, buf2);
2346
2347 sprintf(config_path, "%s%s", default_config_path, "rs_port");
2348 get_result = get_file_content(config_path, config_value);
2349 intVal = atoi(config_value);
2350 sprintf(buf2, "rsPort = %d\n", intVal);
2351 WRITE_WPA_FILE(fh, buf2);
2352
2353
2354 sprintf(config_path, "%s%s", default_config_path, "rs_ip");
2355 get_result = get_file_content(config_path, config_value);
2356 sprintf(buf1, "%s", config_value);
2357 sprintf(buf2, "rsIP = %s\n",buf1);
2358 WRITE_WPA_FILE(fh, buf2);
2359
2360
2361 sprintf(config_path, "%s%s", default_config_path, "rs_password");
2362 get_result = get_file_content(config_path, config_value);
2363 sprintf(buf1, "%s", config_value);
2364 sprintf(buf2, "rsPassword = \"%s\"\n", buf1);
2365 WRITE_WPA_FILE(fh, buf2);
2366
2367
2368
2369 sprintf(config_path, "%s%s", default_config_path, "rs_maxretry");
2370 get_result = get_file_content(config_path, config_value);
2371 intVal = atoi(config_value);
2372 sprintf(buf2, "rsMaxReq = %d\n", intVal);
2373 WRITE_WPA_FILE(fh, buf2);
2374
2375
2376 sprintf(config_path, "%s%s", default_config_path, "rs_interval_time");
2377 get_result = get_file_content(config_path, config_value);
2378 intVal = atoi(config_value);
2379 sprintf(buf2, "rsAWhile = %d\n", intVal);
2380 WRITE_WPA_FILE(fh, buf2);
2381
2382
2383 sprintf(config_path, "%s%s", default_config_path, "account_rs_enabled");
2384 get_result = get_file_content(config_path, config_value);
2385 intVal = atoi(config_value);
2386 sprintf(buf2, "accountRsEnabled = %d\n", intVal);
2387 WRITE_WPA_FILE(fh, buf2);
2388
2389
2390 sprintf(config_path, "%s%s", default_config_path, "account_rs_port");
2391 get_result = get_file_content(config_path, config_value);
2392 intVal = atoi(config_value);
2393 sprintf(buf2, "accountRsPort = %d\n", intVal);
2394 WRITE_WPA_FILE(fh, buf2);
2395
2396
2397 sprintf(config_path, "%s%s", default_config_path, "account_rs_ip");
2398 get_result = get_file_content(config_path, config_value);
2399 sprintf(buf1, "%s", config_value);
2400 sprintf(buf2, "accountRsIP = %s\n", buf1);
2401 WRITE_WPA_FILE(fh, buf2);
2402
2403
2404 sprintf(config_path, "%s%s", default_config_path, "account_rs_password");
2405 get_result = get_file_content(config_path, config_value);
2406 sprintf(buf1, "%s", config_value);
2407 sprintf(buf2, "accountRsPassword = \"%s\"\n", buf1);
2408 WRITE_WPA_FILE(fh, buf2);
2409
2410
2411 sprintf(config_path, "%s%s", default_config_path, "account_rs_update_enabled");
2412 get_result = get_file_content(config_path, config_value);
2413 intVal = atoi(config_value);
2414 sprintf(buf2, "accountRsUpdateEnabled = %d\n", intVal);
2415 WRITE_WPA_FILE(fh, buf2);
2416
2417
2418 sprintf(config_path, "%s%s", default_config_path, "account_rs_update_delay");
2419 get_result = get_file_content(config_path, config_value);
2420 intVal = atoi(config_value);
2421 sprintf(buf2, "accountRsUpdateTime = %d\n", intVal);
2422 WRITE_WPA_FILE(fh, buf2);
2423
2424
2425 sprintf(config_path, "%s%s", default_config_path, "account_rs_maxretry");
2426 get_result = get_file_content(config_path, config_value);
2427 intVal = atoi(config_value);
2428 sprintf(buf2, "accountRsMaxReq = %d\n", intVal);
2429 WRITE_WPA_FILE(fh, buf2);
2430
2431
2432 sprintf(config_path, "%s%s", default_config_path, "account_rs_interval_time");
2433 get_result = get_file_content(config_path, config_value);
2434 intVal = atoi(config_value);
2435 sprintf(buf2, "accountRsAWhile = %d\n", intVal);
2436 WRITE_WPA_FILE(fh, buf2);
2437 }
2438}
2439
2440static int updateWscConf(char *in, char *out, int genpin, char *wlanif_name)
2441{
2442 int fh=0,ret=0;
2443 struct stat status;
2444 char *buf=0, *ptr;
2445 int intVal, is_client, is_config, is_registrar, len, is_wep=0, wep_key_type=0, wep_transmit_key=0;
2446 char tmpbuf[100], tmp1[100];
2447 char buf_1[128];
2448
2449 int isUpnpEnabled=0, wsc_method = 0, wsc_auth=0, wsc_enc=0;
2450 int wlan_network_type=0, wsc_manual_enabled=0, wlan_encrpty=0, wlan_wep=0;
2451#ifdef FOR_DUAL_BAND
2452 char wlan_wpa_psk[100];
2453#endif
2454 char wlan_ssid[100], device_name[100];//, wsc_pin[100];
2455 int wlan_chan_num=0, wsc_config_by_ext_reg=0;
2456// int is_vxdif=0;
2457 char default_config_path[256]={0};
2458 char config_path[256]={0};
2459 char config_value[100]={0};
2460 char wsc_pin[100]={0};
2461 char tmp_config_value[100]={0};
2462 char tmp_config_path[256]={0};
2463 int get_result=0,psk_en;
2464
2465 // WPS2DOTX
2466 int wlan0_wlan_disabled=0;
2467 int wlan0_wsc_disabled=0;
2468#ifdef FOR_DUAL_BAND
2469 int wlan1_wlan_disabled=0;
2470 int wlan1_wsc_disabled=0;
2471#endif
2472
2473 // WPS2DOTX
2474
2475 int band_select_5g2g; // 0:2.4g ; 1:5G ; 2:both
2476
2477 printf("\r\n wlanif_name=[%s],__[%s-%u]\r\n",wlanif_name,__FILE__,__LINE__);
2478
2479 //sprintf(default_config_path, WIFI_CONFIG_ROOT_DIR "/%s/", wlanif_name);
2480 sprintf(default_config_path, "%s/%s/", ConfigFilePath,wlanif_name);
2481
2482 //apmib_get(MIB_WLAN_WSC_REGISTRAR_ENABLED, (void *)&is_registrar);
2483 sprintf(config_path, "%s%s", default_config_path, "wsc_registrar_enabled");
2484 if( get_file_content(config_path, config_value) < 0 )
2485 {
2486 return -1;
2487 }
2488 is_registrar = atoi(config_value);
2489
2490 sprintf(config_path, "%s%s", default_config_path, "wsc_upnp_enabled");
2491 if( get_file_content(config_path, config_value) < 0 )
2492 {
2493 return -1;
2494 }
2495 isUpnpEnabled = atoi(config_value);
2496
2497 sprintf(config_path, "%s%s", default_config_path, "wsc_method");
2498 if( get_file_content(config_path, config_value) < 0 )
2499 {
2500 return -1;
2501 }
2502 wsc_method = atoi(config_value);
2503
2504 sprintf(config_path, "%s%s", default_config_path, "network_type");
2505 if( get_file_content(config_path, config_value) < 0 )
2506 {
2507 return -1;
2508 }
2509 wlan_network_type = atoi(config_value);
2510
2511 sprintf(config_path, "%s%s", default_config_path, "wsc_manual_enabled");
2512 if( get_file_content(config_path, config_value) < 0 )
2513 {
2514 return -1;
2515 }
2516 wsc_manual_enabled = atoi(config_value);
2517 printf("1.wlan_chan_num=%08x\n",wlan_chan_num);
2518
2519 //sprintf(config_path, WIFI_CONFIG_ROOT_DIR "/%s", "device_name");
2520 sprintf(config_path, "%s/%s", ConfigFilePath,"device_name");
2521 if( get_file_content(config_path, config_value) < 0 )
2522 {
2523 //return -1;
2524 }
2525 sprintf(device_name, "%s", config_value);
2526
2527
2528 //sprintf(config_path, WIFI_CONFIG_ROOT_DIR "/%s", "band2g5g_select");
2529 sprintf(config_path, "%s/%s", ConfigFilePath,"band2g5g_select");
2530 if( get_file_content(config_path, config_value) < 0 )
2531 {
2532 //return -1;
2533 }
2534 band_select_5g2g=atoi(config_value);
2535
2536 sprintf(config_path, "%s%s", default_config_path, "wsc_configured");
2537 if( get_file_content(config_path, config_value) < 0 )
2538 {
2539 return -1;
2540 }
2541 is_config = atoi(config_value);
2542
2543 sprintf(config_path, "%s%s", default_config_path, "wsc_configbyextreg");
2544 if( get_file_content(config_path, config_value) < 0 )
2545 {
2546 return -1;
2547 }
2548 wsc_config_by_ext_reg=atoi(config_value);
2549
2550 sprintf(config_path, "%s%s", default_config_path, "wsc_disabled");
2551 get_result = get_file_content(config_path, config_value);
2552 wlan0_wsc_disabled = atoi(config_value);
2553
2554 sprintf(config_path, "%s%s", default_config_path, "wlan_disabled");
2555 get_result = get_file_content(config_path, config_value);
2556 wlan0_wlan_disabled = atoi(config_value);
2557
2558 /* WPS2DOTX ; 2011-0524*/
2559 if(wlan0_wlan_disabled)
2560 wlan0_wsc_disabled=1 ; // if wlan0 interface is disabled ;
2561
2562
2563 sprintf(config_path, "%s%s", default_config_path, "channel");
2564 get_result = get_file_content(config_path, config_value);
2565 wlan_chan_num=atoi(config_value);
2566
2567 len=mib_cmd( wlanif_name , "get_mib" , "channel" , buf_1 , sizeof(buf_1) );
2568 if( len<0 )
2569 {
2570 printf("ERROR : Execute mib_cmd failure\n");
2571 return -1;
2572 }
2573 else
2574 {
2575 wlan_chan_num = *(int *)buf_1;
2576 printf("2.wlan_chan_num=%08x\n",wlan_chan_num);
2577 }
2578
2579
2580
2581 len=mib_cmd( wlanif_name , "get_mib" , "psk_enable" , buf_1 , sizeof(buf_1) );
2582 if( len<0 )
2583 {
2584 printf("ERROR : Execute mib_cmd psk_enable failure\n");
2585 return -1;
2586 }
2587 else
2588 {
2589 psk_en = *(int *)buf_1;
2590 switch( psk_en )
2591 {
2592 case 1:
2593 //WPA
2594 wsc_auth=2;
2595 wlan_encrpty=2;
2596 break;
2597
2598 case 2:
2599 //WPA2
2600 wsc_auth=32;
2601 wlan_encrpty=4;
2602 break;
2603
2604 case 3:
2605 //wpa_wpa2_mixed
2606 wsc_auth=34;
2607 wlan_encrpty=6;
2608 break;
2609
2610 default :
2611 printf("ERROR : unknow mode\n");
2612 return -1;
2613 }
2614 printf("2.wsc_auth=%08x wlan_encrpty=%d\n",wsc_auth,wlan_encrpty);
2615 }
2616
2617
2618
2619 if( psk_en==1 )
2620 {
2621 len=mib_cmd( wlanif_name , "get_mib" , "wpa_cipher" , buf_1 , sizeof(buf_1) );
2622 }
2623 else if( psk_en==2 )
2624 {
2625 len=mib_cmd( wlanif_name , "get_mib" , "wpa2_cipher" , buf_1 , sizeof(buf_1) );
2626 }
2627 else if( psk_en==3 )
2628 {
2629 len=mib_cmd( wlanif_name , "get_mib" , "wpa2_cipher" , buf_1 , sizeof(buf_1) );
2630 }
2631 else
2632 {
2633 printf("ERROR : Execute mode psk_en=%d\n",psk_en);
2634 }
2635
2636
2637 if( len<0 )
2638 {
2639 printf("ERROR : Execute mib_cmd failure\n");
2640 //close(fh);
2641 }
2642 else
2643 {
2644 wsc_enc = *(int *)buf_1;
2645 switch( wsc_enc )
2646 {
2647 case 2:
2648 //TKIP
2649 wsc_enc=4;
2650 break;
2651
2652 case 8:
2653 //AES
2654 wsc_enc=8;
2655 break;
2656
2657 case 10:
2658 //TKIP_AES_mixed
2659 wsc_enc=12;
2660 break;
2661
2662 default :
2663 printf("ERROR : unknow mode wsc_enc=%d\n",wsc_enc);
2664 return -1;
2665 }
2666 printf("2.wsc_enc=%08x\n",wsc_enc);
2667 }
2668
2669
2670
2671 len=mib_cmd( wlanif_name , "get_mib" , "ssid" , buf_1 , sizeof(buf_1) );
2672 if( len<0 )
2673 {
2674 printf("ERROR : Execute mib_cmd failure\n");
2675 return -1;
2676 }
2677 else
2678 {
2679 strncpy( wlan_ssid , buf_1 , len );
2680 wlan_ssid[len]=0;
2681 printf("2.wlan_ssid=%s\n",wlan_ssid);
2682 }
2683
2684
2685
2686 len=mib_cmd( wlanif_name , "get_mib" , "opmode" , buf_1 , sizeof(buf_1) );
2687 if( len<0 )
2688 {
2689 printf("ERROR : Execute mib_cmd opmode failure\n");
2690 return -1;
2691 }
2692 else
2693 {
2694 get_result = *(int *)buf_1;
2695 if( get_result==16 )
2696 {
2697 is_client=0;
2698 }
2699 else if( get_result==8 )
2700 {
2701 is_client=1;
2702 }
2703 else
2704 {
2705 printf("ERROR : unknow opmode mode=%d\n",get_result);
2706 ret=-1;
2707 goto exit;
2708 }
2709 printf("2.is_client=%d\n",is_client);
2710 }
2711
2712
2713
2714
2715
2716
2717 len=mib_cmd( wlanif_name , "get_mib" , "encmode" , buf_1 , sizeof(buf_1) );
2718 if( len<0 )
2719 {
2720 printf("ERROR : Execute mib_cmd encmode failure\n");
2721 ret=-1;
2722 goto exit;
2723 }
2724 else
2725 {
2726 wlan_wep = *(int *)buf_1;
2727 //printf("2.wlan_wep=%d\n",wlan_wep);
2728 if( wlan_wep==1 )
2729 {
2730 ;
2731 }
2732 else if( wlan_wep==1 )
2733 {
2734 wlan_wep=5;
2735 }
2736 else
2737 {
2738 wlan_wep=0;
2739 }
2740 printf("2.wlan_wep=%d\n",wlan_wep);
2741 }
2742
2743 sprintf(config_path, "%s%s", default_config_path, "wsc_pin");
2744 if( get_file_content(config_path, wsc_pin ) < 0 )
2745 {
2746 printf("ERROR : oen %s failure\n",config_path);
2747 }
2748 else
2749 {
2750 printf("wsc_pin=%s\n",wsc_pin);
2751 }
2752
2753
2754 if (stat(in, &status) < 0) {
2755 printf("stat() error [%s]!\n", in);
2756 return -1;
2757 }
2758
2759 buf = malloc(status.st_size+2048);
2760 if (buf == NULL) {
2761 printf("malloc() error [%d]!\n", (int)status.st_size+2048);
2762 return -1;
2763 }
2764
2765 ptr = buf;
2766
2767 if (is_client == CLIENT_MODE) {
2768 {
2769 if (is_registrar) {
2770 if (!is_config)
2771 intVal = MODE_CLIENT_UNCONFIG_REGISTRAR;
2772 else
2773 intVal = MODE_CLIENT_CONFIG;
2774 }
2775 else
2776 intVal = MODE_CLIENT_UNCONFIG;
2777 }
2778 }
2779 else {
2780 if (!is_config)
2781 intVal = MODE_AP_UNCONFIG;
2782 else
2783 intVal = MODE_AP_PROXY_REGISTRAR;
2784 }
2785 WRITE_WSC_PARAM(ptr, tmpbuf, "mode = %d\n", intVal);
2786
2787
2788
2789
2790
2791
2792 if (is_client == CLIENT_MODE) {
2793 intVal = 0;
2794 }
2795 else
2796 intVal = isUpnpEnabled;
2797
2798 WRITE_WSC_PARAM(ptr, tmpbuf, "upnp = %d\n", intVal);
2799
2800 intVal = 0;
2801 intVal = wsc_method;
2802 //Ethernet(0x2)+Label(0x4)+PushButton(0x80) Bitwise OR
2803 if (intVal == 1) //Pin+Ethernet
2804 intVal = (CONFIG_METHOD_ETH | CONFIG_METHOD_PIN);
2805 else if (intVal == 2) //PBC+Ethernet
2806 intVal = (CONFIG_METHOD_ETH | CONFIG_METHOD_PBC);
2807 if (intVal == 3) //Pin+PBC+Ethernet
2808 intVal = (CONFIG_METHOD_ETH | CONFIG_METHOD_PIN | CONFIG_METHOD_PBC);
2809 WRITE_WSC_PARAM(ptr, tmpbuf, "config_method = %d\n", intVal);
2810
2811 // 1104
2812 WRITE_WSC_PARAM(ptr, tmpbuf, "wlan0_wsc_disabled = %d\n", wlan0_wsc_disabled);
2813 WRITE_WSC_PARAM(ptr, tmpbuf, "auth_type = %d\n", wsc_auth);
2814 WRITE_WSC_PARAM(ptr, tmpbuf, "encrypt_type = %d\n", wsc_enc);
2815
2816 if (wsc_enc == WSC_ENCRYPT_WEP)
2817 is_wep = 1;
2818
2819 if(wlanif_name != NULL && (strcmp(wlanif_name,"wlan0") == 0 || strcmp(wlanif_name,"wlan1") == 0))
2820 {
2821 if (is_client == CLIENT_MODE)
2822 {
2823 if (wlan_network_type == 0)
2824 intVal = 1;
2825 else
2826 intVal = 2;
2827 }
2828 else
2829 intVal = 1;
2830 }
2831 else
2832 intVal = 1;
2833
2834 WRITE_WSC_PARAM(ptr, tmpbuf, "connection_type = %d\n", intVal);
2835 WRITE_WSC_PARAM(ptr, tmpbuf, "manual_config = %d\n", wsc_manual_enabled);
2836
2837 if (is_wep) { // only allow WEP in none-MANUAL mode (configured by external registrar)
2838 //WPS2.0 don't support WEP
2839 goto exit;
2840 }
2841 else {
2842 len=mib_cmd( wlanif_name , "get_mib" , "passphrase" , buf_1 , sizeof(buf_1) );
2843 if( len<0 )
2844 {
2845 printf("ERROR : Execute mib_cmd passphrase failure\n");
2846 ret=-1;
2847 goto exit;
2848 }
2849 else
2850 {
2851 printf("passphrase=%s\n",buf_1);
2852 }
2853 WRITE_WSC_PARAM( ptr, tmpbuf, "network_key = \"%s\"\n", buf_1 );
2854 }
2855
2856 WRITE_WSC_PARAM(ptr, tmpbuf, "ssid = \"%s\"\n", wlan_ssid);
2857 WRITE_WSC_PARAM(ptr, tmpbuf, "pin_code = %s\n", wsc_pin);
2858
2859
2860 if (wlan_chan_num > 14)
2861 intVal = 2;
2862 else
2863 intVal = 1;
2864
2865 WRITE_WSC_PARAM(ptr, tmpbuf, "rf_band = %d\n", intVal);
2866 WRITE_WSC_PARAM(ptr, tmpbuf, "device_name = \"%s\"\n", device_name);
2867 WRITE_WSC_PARAM(ptr, tmpbuf, "config_by_ext_reg = %d\n", wsc_config_by_ext_reg);
2868
2869 len = (int)(((long)ptr)-((long)buf));
2870
2871 fh = open(in, O_RDONLY);
2872 if (fh == -1) {
2873 printf("open() error [%s]!\n", in);
2874 ret=-1;
2875 goto exit;
2876 }
2877
2878 lseek(fh, 0L, SEEK_SET);
2879 if (read(fh, ptr, status.st_size) != status.st_size) {
2880 printf("read() error [%s]!\n", in);
2881 ret=-1;
2882 goto exit;
2883 }
2884 close(fh);
2885
2886 // search UUID field, replace last 12 char with hw mac address
2887 ptr = strstr(ptr, "uuid =");
2888 if (ptr) {
2889 char tmp2[100];
2890// apmib_get(MIB_HW_NIC0_ADDR, (void *)&tmp1);
2891 sprintf(tmp_config_path, "%s%s", default_config_path, "nic0_addr");
2892 get_result = get_file_content(tmp_config_path, tmp_config_value);
2893 if(get_result != -1){
2894 string_to_hex(tmp_config_value,tmp1, 12);
2895 convert_bin_to_str(tmp1, 6, tmp2);
2896 memcpy(ptr+27, tmp2, 12);
2897 }
2898 }
2899
2900 fh = open(out, O_RDWR|O_CREAT|O_TRUNC, S_IRGRP|S_IWGRP);
2901 if (fh == -1) {
2902 printf("open() error [%s]!\n", out);
2903 goto exit;
2904 }
2905
2906 if (write(fh, buf, len+status.st_size) != len+status.st_size ) {
2907 printf("Write() file error [%s]!\n", out);
2908 ret=-1;
2909 goto exit;
2910 }
2911
2912exit:
2913 if( fh )
2914 close(fh);
2915 if( buf )
2916 free(buf);
2917 return ret;
2918}
2919
2920
2921
2922
2923#endif
2924
2925