/
wfa_cs.c
4249 lines (3490 loc) · 123 KB
/
wfa_cs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
Copyright (c) 2016 Wi-Fi Alliance. All Rights Reserved
Permission to use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted, provided that the above copyright notice and this permission
notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH
THE USE OR PERFORMANCE OF THIS SOFTWARE.
******************************************************************************/
/*
* File: wfa_cs.c -- configuration and setup
* This file contains all implementation for the dut setup and control
* functions, such as network interfaces, ip address and wireless specific
* setup with its supplicant.
*
* The current implementation is to show how these functions
* should be defined in order to support the Agent Control/Test Manager
* control commands. To simplify the current work and avoid any GPL licenses,
* the functions mostly invoke shell commands by calling linux system call,
* system("<commands>").
*
* It depends on the differnt device and platform, vendors can choice their
* own ways to interact its systems, supplicants and process these commands
* such as using the native APIs.
*
*
*/
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <linux/types.h>
#include <linux/socket.h>
#include <poll.h>
#include "wfa_portall.h"
#include "wfa_debug.h"
#include "wfa_ver.h"
#include "wfa_main.h"
#include "wfa_types.h"
#include "wfa_ca.h"
#include "wfa_tlv.h"
#include "wfa_sock.h"
#include "wfa_tg.h"
#include "wfa_cmds.h"
#include "wfa_rsp.h"
#include "wfa_utils.h"
#ifdef WFA_WMM_PS_EXT
#include "wfa_wmmps.h"
#endif
#define CERTIFICATES_PATH "/etc/wpa_supplicant"
/* Some device may only support UDP ECHO, activate this line */
//#define WFA_PING_UDP_ECHO_ONLY 1
#define WFA_ENABLED 1
extern unsigned short wfa_defined_debug;
int wfaExecuteCLI(char *CLI);
/* Since the two definitions are used all over the CA function */
char gCmdStr[WFA_CMD_STR_SZ];
dutCmdResponse_t gGenericResp;
int wfaTGSetPrio(int sockfd, int tgClass);
void create_apts_msg(int msg, unsigned int txbuf[],int id);
int sret = 0;
extern char e2eResults[];
FILE *e2efp = NULL;
int chk_ret_status()
{
char *ret = getenv(WFA_RET_ENV);
if(*ret == '1')
return WFA_SUCCESS;
else
return WFA_FAILURE;
}
/*
* agtCmdProcGetVersion(): response "ca_get_version" command to controller
* input: cmd --- not used
* valLen -- not used
* output: parms -- a buffer to store the version info response.
*/
int agtCmdProcGetVersion(int len, BYTE *parms, int *respLen, BYTE *respBuf)
{
dutCmdResponse_t *getverResp = &gGenericResp;
DPRINT_INFO(WFA_OUT, "entering agtCmdProcGetVersion ...\n");
getverResp->status = STATUS_COMPLETE;
wSTRNCPY(getverResp->cmdru.version, WFA_SYSTEM_VER, WFA_VERNAM_LEN);
wfaEncodeTLV(WFA_GET_VERSION_RESP_TLV, sizeof(dutCmdResponse_t), (BYTE *)getverResp, respBuf);
*respLen = WFA_TLV_HDR_LEN + sizeof(dutCmdResponse_t);
return WFA_SUCCESS;
}
/*
* wfaStaAssociate():
* The function is to force the station wireless I/F to re/associate
* with the AP.
*/
int wfaStaAssociate(int len, BYTE *caCmdBuf, int *respLen, BYTE *respBuf)
{
dutCommand_t *assoc = (dutCommand_t *)caCmdBuf;
char *ifname = assoc->intf;
dutCmdResponse_t *staAssocResp = &gGenericResp;
DPRINT_INFO(WFA_OUT, "entering wfaStaAssociate ...\n");
/*
* if bssid appears, station should associate with the specific
* BSSID AP at its initial association.
* If it is different to the current associating AP, it will be forced to
* roam the new AP
*/
if(assoc->cmdsu.assoc.bssid[0] != '\0')
{
/* if (the first association) */
/* just do initial association to the BSSID */
/* else (station already associate to an AP) */
/* Do forced roaming */
}
else
{
/* use 'ifconfig' command to bring down the interface (linux specific) */
//sprintf(gCmdStr, "ifconfig %s down", ifname);
//sret = system(gCmdStr);
/* use 'ifconfig' command to bring up the interface (linux specific) */
//sprintf(gCmdStr, "ifconfig %s up", ifname);
//sret = system(gCmdStr);
/*
* use 'wpa_cli' command to force a 802.11 re/associate
* (wpa_supplicant specific)
*/
sprintf(gCmdStr, "wpa_cli -i%s reassociate", ifname);
sret = system(gCmdStr);
}
/*
* Then report back to control PC for completion.
* This does not have failed/error status. The result only tells
* a completion.
*/
staAssocResp->status = STATUS_COMPLETE;
wfaEncodeTLV(WFA_STA_ASSOCIATE_RESP_TLV, 4, (BYTE *)staAssocResp, respBuf);
*respLen = WFA_TLV_HDR_LEN + 4;
return WFA_SUCCESS;
}
/*
* wfaStaReAssociate():
* The function is to force the station wireless I/F to re/associate
* with the AP.
*/
int wfaStaReAssociate(int len, BYTE *caCmdBuf, int *respLen, BYTE *respBuf)
{
dutCommand_t *assoc = (dutCommand_t *)caCmdBuf;
char *ifname = assoc->intf;
dutCmdResponse_t *staAssocResp = &gGenericResp;
DPRINT_INFO(WFA_OUT, "entering wfaStaAssociate ...\n");
/*
* if bssid appears, station should associate with the specific
* BSSID AP at its initial association.
* If it is different to the current associating AP, it will be forced to
* roam the new AP
*/
if(assoc->cmdsu.assoc.bssid[0] != '\0')
{
/* if (the first association) */
/* just do initial association to the BSSID */
/* else (station already associate to an AP) */
/* Do forced roaming */
}
else
{
/* use 'ifconfig' command to bring down the interface (linux specific) */
sprintf(gCmdStr, "ifconfig %s down", ifname);
sret = system(gCmdStr);
/* use 'ifconfig' command to bring up the interface (linux specific) */
sprintf(gCmdStr, "ifconfig %s up", ifname);
/*
* use 'wpa_cli' command to force a 802.11 re/associate
* (wpa_supplicant specific)
*/
sprintf(gCmdStr, "wpa_cli -i%s reassociate", ifname);
sret = system(gCmdStr);
}
/*
* Then report back to control PC for completion.
* This does not have failed/error status. The result only tells
* a completion.
*/
staAssocResp->status = STATUS_COMPLETE;
wfaEncodeTLV(WFA_STA_ASSOCIATE_RESP_TLV, 4, (BYTE *)staAssocResp, respBuf);
*respLen = WFA_TLV_HDR_LEN + 4;
return WFA_SUCCESS;
}
/*
* wfaStaIsConnected():
* The function is to check whether the station's wireless I/F has
* already connected to an AP.
*/
int wfaStaIsConnected(int len, BYTE *caCmdBuf, int *respLen, BYTE *respBuf)
{
dutCommand_t *connStat = (dutCommand_t *)caCmdBuf;
dutCmdResponse_t *staConnectResp = &gGenericResp;
char *ifname = connStat->intf;
FILE *tmpfile = NULL;
char result[32];
DPRINT_INFO(WFA_OUT, "Entering isConnected ...\n");
#ifdef WFA_NEW_CLI_FORMAT
sprintf(gCmdStr, "wfa_chkconnect %s\n", ifname);
sret = system(gCmdStr);
if(chk_ret_status() == WFA_SUCCESS)
staConnectResp->cmdru.connected = 1;
else
staConnectResp->cmdru.connected = 0;
#else
/*
* use 'wpa_cli' command to check the interface status
* none, scanning or complete (wpa_supplicant specific)
*/
sprintf(gCmdStr, "/sbin/wpa_cli -i%s status | grep ^wpa_state= | cut -f2- -d= > /tmp/.isConnected", ifname);
sret = system(gCmdStr);
/*
* the status is saved in a file. Open the file and check it.
*/
tmpfile = fopen("/tmp/.isConnected", "r+");
if(tmpfile == NULL)
{
staConnectResp->status = STATUS_ERROR;
wfaEncodeTLV(WFA_STA_IS_CONNECTED_RESP_TLV, 4, (BYTE *)staConnectResp, respBuf);
*respLen = WFA_TLV_HDR_LEN + 4;
DPRINT_ERR(WFA_ERR, "file open failed\n");
return WFA_FAILURE;
}
sret = fscanf(tmpfile, "%s", (char *)result);
if(strncmp(result, "COMPLETED", 9) == 0)
staConnectResp->cmdru.connected = 1;
else
staConnectResp->cmdru.connected = 0;
#endif
/*
* Report back the status: Complete or Failed.
*/
staConnectResp->status = STATUS_COMPLETE;
wfaEncodeTLV(WFA_STA_IS_CONNECTED_RESP_TLV, sizeof(dutCmdResponse_t), (BYTE *)staConnectResp, respBuf);
*respLen = WFA_TLV_HDR_LEN + sizeof(dutCmdResponse_t);
return WFA_SUCCESS;
}
/*
* wfaStaGetIpConfig():
* This function is to retriev the ip info including
* 1. dhcp enable
* 2. ip address
* 3. mask
* 4. primary-dns
* 5. secondary-dns
*
* The current implementation is to use a script to find these information
* and store them in a file.
*/
int wfaStaGetIpConfig(int len, BYTE *caCmdBuf, int *respLen, BYTE *respBuf)
{
int slen, ret, i = 0;
dutCommand_t *getIpConf = (dutCommand_t *)caCmdBuf;
dutCmdResponse_t *ipconfigResp = &gGenericResp;
char *ifname = getIpConf->intf;
caStaGetIpConfigResp_t *ifinfo = &ipconfigResp->cmdru.getIfconfig;
FILE *tmpfd;
char string[256];
char *str;
/*
* check a script file (the current implementation specific)
*/
ret = access("/usr/local/sbin/getipconfig.sh", F_OK);
if(ret == -1)
{
ipconfigResp->status = STATUS_ERROR;
wfaEncodeTLV(WFA_STA_GET_IP_CONFIG_RESP_TLV, 4, (BYTE *)ipconfigResp, respBuf);
*respLen = WFA_TLV_HDR_LEN + 4;
DPRINT_ERR(WFA_ERR, "file not exist\n");
return WFA_FAILURE;
}
strcpy(ifinfo->dns[0], "0");
strcpy(ifinfo->dns[1], "0");
/*
* Run the script file "getipconfig.sh" to check the ip status
* (current implementation specific).
* note: "getipconfig.sh" is only defined for the current implementation
*/
sprintf(gCmdStr, "getipconfig.sh /tmp/ipconfig.txt %s\n", ifname);
sret = system(gCmdStr);
/* open the output result and scan/retrieve the info */
tmpfd = fopen("/tmp/ipconfig.txt", "r+");
if(tmpfd == NULL)
{
ipconfigResp->status = STATUS_ERROR;
wfaEncodeTLV(WFA_STA_GET_IP_CONFIG_RESP_TLV, 4, (BYTE *)ipconfigResp, respBuf);
*respLen = WFA_TLV_HDR_LEN + 4;
DPRINT_ERR(WFA_ERR, "file open failed\n");
return WFA_FAILURE;
}
for(;;)
{
if(fgets(string, 256, tmpfd) == NULL)
break;
/* check dhcp enabled */
if(strncmp(string, "dhcpcli", 7) ==0)
{
str = strtok(string, "=");
str = strtok(NULL, "=");
if(str != NULL)
ifinfo->isDhcp = 1;
else
ifinfo->isDhcp = 0;
}
/* find out the ip address */
if(strncmp(string, "ipaddr", 6) == 0)
{
str = strtok(string, "=");
str = strtok(NULL, " ");
if(str != NULL)
{
wSTRNCPY(ifinfo->ipaddr, str, 15);
ifinfo->ipaddr[15]='\0';
}
else
wSTRNCPY(ifinfo->ipaddr, "none", 15);
}
/* check the mask */
if(strncmp(string, "mask", 4) == 0)
{
char ttstr[16];
char *ttp = ttstr;
str = strtok_r(string, "=", &ttp);
if(*ttp != '\0')
{
strcpy(ifinfo->mask, ttp);
slen = strlen(ifinfo->mask);
ifinfo->mask[slen-1] = '\0';
}
else
strcpy(ifinfo->mask, "none");
}
/* find out the dns server ip address */
if(strncmp(string, "nameserv", 8) == 0)
{
char ttstr[16];
char *ttp = ttstr;
str = strtok_r(string, " ", &ttp);
if(str != NULL && i < 2)
{
strcpy(ifinfo->dns[i], ttp);
slen = strlen(ifinfo->dns[i]);
ifinfo->dns[i][slen-1] = '\0';
}
else
strcpy(ifinfo->dns[i], "none");
i++;
}
}
/*
* Report back the results
*/
ipconfigResp->status = STATUS_COMPLETE;
wfaEncodeTLV(WFA_STA_GET_IP_CONFIG_RESP_TLV, sizeof(dutCmdResponse_t), (BYTE *)ipconfigResp, respBuf);
*respLen = WFA_TLV_HDR_LEN + sizeof(dutCmdResponse_t);
#if 0
DPRINT_INFO(WFA_OUT, "%i %i %s %s %s %s %i\n", ipconfigResp->status,
ifinfo->isDhcp, ifinfo->ipaddr, ifinfo->mask,
ifinfo->dns[0], ifinfo->dns[1], *respLen);
#endif
fclose(tmpfd);
return WFA_SUCCESS;
}
/*
* wfaStaSetIpConfig():
* The function is to set the ip configuration to a wireless I/F.
* 1. IP address
* 2. Mac address
* 3. default gateway
* 4. dns nameserver (pri and sec).
*/
int wfaStaSetIpConfig(int len, BYTE *caCmdBuf, int *respLen, BYTE *respBuf)
{
dutCommand_t *setIpConf = (dutCommand_t *)caCmdBuf;
caStaSetIpConfig_t *ipconfig = &setIpConf->cmdsu.ipconfig;
dutCmdResponse_t *staSetIpResp = &gGenericResp;
DPRINT_INFO(WFA_OUT, "entering wfaStaSetIpConfig ...\n");
/*
* Use command 'ifconfig' to configure the interface ip address, mask.
* (Linux specific).
*/
sprintf(gCmdStr, "/sbin/ifconfig %s %s netmask %s > /dev/null 2>&1 ", ipconfig->intf, ipconfig->ipaddr, ipconfig->mask);
sret = system(gCmdStr);
/* use command 'route add' to set set gatewway (linux specific) */
if(ipconfig->defGateway[0] != '\0')
{
sprintf(gCmdStr, "/sbin/route add default gw %s > /dev/null 2>&1", ipconfig->defGateway);
sret = system(gCmdStr);
}
/* set dns (linux specific) */
sprintf(gCmdStr, "cp /etc/resolv.conf /tmp/resolv.conf.bk");
sret = system(gCmdStr);
sprintf(gCmdStr, "echo nameserv %s > /etc/resolv.conf", ipconfig->pri_dns);
sret = system(gCmdStr);
sprintf(gCmdStr, "echo nameserv %s >> /etc/resolv.conf", ipconfig->sec_dns);
sret = system(gCmdStr);
/*
* report status
*/
staSetIpResp->status = STATUS_COMPLETE;
wfaEncodeTLV(WFA_STA_SET_IP_CONFIG_RESP_TLV, 4, (BYTE *)staSetIpResp, respBuf);
*respLen = WFA_TLV_HDR_LEN + 4;
return WFA_SUCCESS;
}
/*
* wfaStaVerifyIpConnection():
* The function is to verify if the station has IP connection with an AP by
* send ICMP/pings to the AP.
*/
int wfaStaVerifyIpConnection(int len, BYTE *caCmdBuf, int *respLen, BYTE *respBuf)
{
dutCommand_t *verip = (dutCommand_t *)caCmdBuf;
dutCmdResponse_t *verifyIpResp = &gGenericResp;
#ifndef WFA_PING_UDP_ECHO_ONLY
char strout[64], *pcnt;
FILE *tmpfile;
DPRINT_INFO(WFA_OUT, "Entering wfaStaVerifyIpConnection ...\n");
/* set timeout value in case not set */
if(verip->cmdsu.verifyIp.timeout <= 0)
{
verip->cmdsu.verifyIp.timeout = 10;
}
/* execute the ping command and pipe the result to a tmp file */
sprintf(gCmdStr, "ping %s -c 3 -W %u | grep loss | cut -f3 -d, 1>& /tmp/pingout.txt", verip->cmdsu.verifyIp.dipaddr, verip->cmdsu.verifyIp.timeout);
sret = system(gCmdStr);
/* scan/check the output */
tmpfile = fopen("/tmp/pingout.txt", "r+");
if(tmpfile == NULL)
{
verifyIpResp->status = STATUS_ERROR;
wfaEncodeTLV(WFA_STA_VERIFY_IP_CONNECTION_RESP_TLV, 4, (BYTE *)verifyIpResp, respBuf);
*respLen = WFA_TLV_HDR_LEN + 4;
DPRINT_ERR(WFA_ERR, "file open failed\n");
return WFA_FAILURE;
}
verifyIpResp->status = STATUS_COMPLETE;
if(fscanf(tmpfile, "%s", strout) == EOF)
verifyIpResp->cmdru.connected = 0;
else
{
pcnt = strtok(strout, "%");
/* if the loss rate is 100%, not able to connect */
if(atoi(pcnt) == 100)
verifyIpResp->cmdru.connected = 0;
else
verifyIpResp->cmdru.connected = 1;
}
fclose(tmpfile);
#else
int btSockfd;
struct pollfd fds[2];
int timeout = 2000;
char anyBuf[64];
struct sockaddr_in toAddr;
int done = 1, cnt = 0, ret, nbytes;
verifyIpResp->status = STATUS_COMPLETE;
verifyIpResp->cmdru.connected = 0;
btSockfd = wfaCreateUDPSock("127.0.0.1", WFA_UDP_ECHO_PORT);
if(btSockfd == -1)
{
verifyIpResp->status = STATUS_ERROR;
wfaEncodeTLV(WFA_STA_VERIFY_IP_CONNECTION_RESP_TLV, 4, (BYTE *)verifyIpResp, respBuf);
*respLen = WFA_TLV_HDR_LEN + 4;
return WFA_FAILURE;;
}
toAddr.sin_family = AF_INET;
toAddr.sin_addr.s_addr = inet_addr(verip->cmdsu.verifyIp.dipaddr);
toAddr.sin_port = htons(WFA_UDP_ECHO_PORT);
while(done)
{
wfaTrafficSendTo(btSockfd, (char *)anyBuf, 64, (struct sockaddr *)&toAddr);
cnt++;
fds[0].fd = btSockfd;
fds[0].events = POLLIN | POLLOUT;
ret = poll(fds, 1, timeout);
switch(ret)
{
case 0:
/* it is time out, count a packet lost*/
break;
case -1:
/* it is an error */
default:
{
switch(fds[0].revents)
{
case POLLIN:
case POLLPRI:
case POLLOUT:
nbytes = wfaTrafficRecv(btSockfd, (char *)anyBuf, (struct sockaddr *)&toAddr);
if(nbytes != 0)
verifyIpResp->cmdru.connected = 1;
done = 0;
break;
default:
/* errors but not care */
;
}
}
}
if(cnt == 3)
{
done = 0;
}
}
#endif
wfaEncodeTLV(WFA_STA_VERIFY_IP_CONNECTION_RESP_TLV, sizeof(dutCmdResponse_t), (BYTE *)verifyIpResp, respBuf);
*respLen = WFA_TLV_HDR_LEN + sizeof(dutCmdResponse_t);
return WFA_SUCCESS;
}
/*
* wfaStaGetMacAddress()
* This function is to retrieve the MAC address of a wireless I/F.
*/
int wfaStaGetMacAddress(int len, BYTE *caCmdBuf, int *respLen, BYTE *respBuf)
{
dutCommand_t *getMac = (dutCommand_t *)caCmdBuf;
dutCmdResponse_t *getmacResp = &gGenericResp;
char *str;
char *ifname = getMac->intf;
FILE *tmpfd;
char string[257];
DPRINT_INFO(WFA_OUT, "Entering wfaStaGetMacAddress ...\n");
/*
* run the script "getipconfig.sh" to find out the mac
*/
sprintf(gCmdStr, "ifconfig %s > /tmp/ipconfig.txt ", ifname);
sret = system(gCmdStr);
tmpfd = fopen("/tmp/ipconfig.txt", "r+");
if(tmpfd == NULL)
{
getmacResp->status = STATUS_ERROR;
wfaEncodeTLV(WFA_STA_GET_MAC_ADDRESS_RESP_TLV, 4, (BYTE *)getmacResp, respBuf);
*respLen = WFA_TLV_HDR_LEN + 4;
DPRINT_ERR(WFA_ERR, "file open failed\n");
return WFA_FAILURE;
}
if(fgets((char *)&string[0], 256, tmpfd) == NULL)
{
getmacResp->status = STATUS_ERROR;
}
str = strtok(string, " ");
while(str && ((strcmp(str,"HWaddr")) != 0))
{
str = strtok(NULL, " ");
}
/* get mac */
if(str)
{
str = strtok(NULL, " ");
strcpy(getmacResp->cmdru.mac, str);
getmacResp->status = STATUS_COMPLETE;
}
wfaEncodeTLV(WFA_STA_GET_MAC_ADDRESS_RESP_TLV, sizeof(dutCmdResponse_t), (BYTE *)getmacResp, respBuf);
*respLen = WFA_TLV_HDR_LEN + sizeof(dutCmdResponse_t);
fclose(tmpfd);
return WFA_SUCCESS;
}
/*
* wfaStaGetStats():
* The function is to retrieve the statistics of the I/F's layer 2 txFrames,
* rxFrames, txMulticast, rxMulticast, fcsErrors/crc, and txRetries.
* Currently there is not definition how to use these info.
*/
int wfaStaGetStats(int len, BYTE *caCmdBuf, int *respLen, BYTE *respBuf)
{
dutCmdResponse_t *statsResp = &gGenericResp;
/* this is never used, you can skip this call */
statsResp->status = STATUS_ERROR;
wfaEncodeTLV(WFA_STA_GET_STATS_RESP_TLV, sizeof(dutCmdResponse_t), (BYTE *)statsResp, respBuf);
*respLen = WFA_TLV_HDR_LEN + sizeof(dutCmdResponse_t);
return WFA_SUCCESS;
}
/*
* wfaSetEncryption():
* The function is to set the wireless interface with WEP or none.
*
* Since WEP is optional test, current function is only used for
* resetting the Security to NONE/Plaintext (OPEN). To test WEP,
* this function should be replaced by the next one (wfaSetEncryption1())
*
* Input parameters:
* 1. I/F
* 2. ssid
* 3. encpType - wep or none
* Optional:
* 4. key1
* 5. key2
* 6. key3
* 7. key4
* 8. activeKey Index
*/
int wfaSetEncryption1(int len, BYTE *caCmdBuf, int *respLen, BYTE *respBuf)
{
caStaSetEncryption_t *setEncryp = (caStaSetEncryption_t *)caCmdBuf;
dutCmdResponse_t *setEncrypResp = &gGenericResp;
/*
* disable the network first
*/
sprintf(gCmdStr, "wpa_cli -i %s disable_network 0", setEncryp->intf);
sret = system(gCmdStr);
/*
* set SSID
*/
sprintf(gCmdStr, "wpa_cli -i %s set_network 0 ssid '\"%s\"'", setEncryp->intf, setEncryp->ssid);
sret = system(gCmdStr);
/*
* Tell the supplicant for infrastructure mode (1)
*/
sprintf(gCmdStr, "wpa_cli -i %s set_network 0 mode 0", setEncryp->intf);
sret = system(gCmdStr);
/*
* set Key management to NONE (NO WPA) for plaintext or WEP
*/
sprintf(gCmdStr, "wpa_cli -i %s set_network 0 key_mgmt NONE", setEncryp->intf);
sret = system(gCmdStr);
sprintf(gCmdStr, "wpa_cli -i %s enable_network 0", setEncryp->intf);
sret = system(gCmdStr);
setEncrypResp->status = STATUS_COMPLETE;
wfaEncodeTLV(WFA_STA_SET_ENCRYPTION_RESP_TLV, 4, (BYTE *)setEncrypResp, respBuf);
*respLen = WFA_TLV_HDR_LEN + 4;
return WFA_SUCCESS;
}
/*
* Since WEP is optional, this function could be used to replace
* wfaSetEncryption() if necessary.
*/
int wfaSetEncryption(int len, BYTE *caCmdBuf, int *respLen, BYTE *respBuf)
{
caStaSetEncryption_t *setEncryp = (caStaSetEncryption_t *)caCmdBuf;
dutCmdResponse_t *setEncrypResp = &gGenericResp;
int i;
/*
* disable the network first
*/
sprintf(gCmdStr, "wpa_cli -i %s disable_network 0", setEncryp->intf);
sret = system(gCmdStr);
/*
* set SSID
*/
sprintf(gCmdStr, "wpa_cli -i %s set_network 0 ssid '\"%s\"'", setEncryp->intf, setEncryp->ssid);
sret = system(gCmdStr);
/*
* Tell the supplicant for infrastructure mode (1)
*/
sprintf(gCmdStr, "wpa_cli -i %s set_network 0 mode 0", setEncryp->intf);
sret = system(gCmdStr);
/*
* set Key management to NONE (NO WPA) for plaintext or WEP
*/
sprintf(gCmdStr, "wpa_cli -i %s set_network 0 key_mgmt NONE", setEncryp->intf);
sret = system(gCmdStr);
/* set keys */
if(setEncryp->encpType == 1)
{
for(i=0; i<4; i++)
{
if(setEncryp->keys[i][0] != '\0')
{
sprintf(gCmdStr, "wpa_cli -i %s set_network 0 wep_key%i %s",
setEncryp->intf, i, setEncryp->keys[i]);
sret = system(gCmdStr);
}
}
/* set active key */
i = setEncryp->activeKeyIdx;
if(setEncryp->keys[i][0] != '\0')
{
sprintf(gCmdStr, "wpa_cli -i %s set_network 0 wep_tx_keyidx %i",
setEncryp->intf, setEncryp->activeKeyIdx);
sret = system(gCmdStr);
}
}
else /* clearly remove the keys -- reported by p.schwann */
{
for(i = 0; i < 4; i++)
{
sprintf(gCmdStr, "wpa_cli -i %s set_network 0 wep_key%i \"\"", setEncryp->intf, i);
sret = system(gCmdStr);
}
}
sprintf(gCmdStr, "wpa_cli -i %s enable_network 0", setEncryp->intf);
sret = system(gCmdStr);
setEncrypResp->status = STATUS_COMPLETE;
wfaEncodeTLV(WFA_STA_SET_ENCRYPTION_RESP_TLV, 4, (BYTE *)setEncrypResp, respBuf);
*respLen = WFA_TLV_HDR_LEN + 4;
return WFA_SUCCESS;
}
int wfaStaSetSecurity(int len, BYTE *caCmdBuf, int *respLen, BYTE *respBuf)
{
int ret = WFA_SUCCESS;
return ret;
}
/*
* wfaStaSetEapTLS():
* This is to set
* 1. ssid
* 2. encrypType - tkip or aes-ccmp
* 3. keyManagementType - wpa or wpa2
* 4. trustedRootCA
* 5. clientCertificate
*/
int wfaStaSetEapTLS(int len, BYTE *caCmdBuf, int *respLen, BYTE *respBuf)
{
caStaSetEapTLS_t *setTLS = (caStaSetEapTLS_t *)caCmdBuf;
char *ifname = setTLS->intf;
dutCmdResponse_t *setEapTlsResp = &gGenericResp;
DPRINT_INFO(WFA_OUT, "Entering wfaStaSetEapTLS ...\n");
/*
* need to store the trustedROOTCA and clientCertificate into a file first.
*/
#ifdef WFA_NEW_CLI_FORMAT
sprintf(gCmdStr, "wfa_set_eaptls -i %s %s %s %s", ifname, setTLS->ssid, setTLS->trustedRootCA, setTLS->clientCertificate);
sret = system(gCmdStr);
#else
sprintf(gCmdStr, "wpa_cli -i %s disable_network 0", ifname);
sret = system(gCmdStr);
/* ssid */
sprintf(gCmdStr, "wpa_cli -i %s set_network 0 ssid '\"%s\"'", ifname, setTLS->ssid);
sret = system(gCmdStr);
/* key management */
if(strcasecmp(setTLS->keyMgmtType, "wpa2-sha256") == 0)
{
}
else if(strcasecmp(setTLS->keyMgmtType, "wpa2-eap") == 0)
{
}
else if(strcasecmp(setTLS->keyMgmtType, "wpa2-ft") == 0)
{
}
else if(strcasecmp(setTLS->keyMgmtType, "wpa") == 0)
{
sprintf(gCmdStr, "wpa_cli -i %s set_network 0 key_mgmt WPA-EAP", ifname);
}
else if(strcasecmp(setTLS->keyMgmtType, "wpa2") == 0)
{
// to take all and device to pick any one supported.
}
else
{
// ??
}
sret = system(gCmdStr);
/* protocol WPA */
sprintf(gCmdStr, "wpa_cli -i %s set_network 0 proto WPA", ifname);
sret = system(gCmdStr);
sprintf(gCmdStr, "wpa_cli -i %s set_network 0 eap TLS", ifname);
sret = system(gCmdStr);
sprintf(gCmdStr, "wpa_cli -i %s set_network 0 ca_cert '\"%s\"'", ifname, setTLS->trustedRootCA);
sret = system(gCmdStr);
sprintf(gCmdStr, "wpa_cli -i %s set_network 0 identity '\"wifi-user@wifilabs.local\"'", ifname);
sret = system(gCmdStr);
sprintf(gCmdStr, "wpa_cli -i %s set_network 0 private_key '\"%s/%s\"'", ifname, CERTIFICATES_PATH, setTLS->clientCertificate);
sret = system(gCmdStr);
sprintf(gCmdStr, "wpa_cli -i %s set_network 0 private_key_passwd '\"wifi\"'", ifname);
sret = system(gCmdStr);
sprintf(gCmdStr, "wpa_cli -i %s enable_network 0", ifname);
sret = system(gCmdStr);
#endif
setEapTlsResp->status = STATUS_COMPLETE;
wfaEncodeTLV(WFA_STA_SET_EAPTLS_RESP_TLV, 4, (BYTE *)setEapTlsResp, respBuf);
*respLen = WFA_TLV_HDR_LEN + 4;
return WFA_SUCCESS;
}
/*
* The function is to set
* 1. ssid
* 2. passPhrase
* 3. keyMangementType - wpa/wpa2
* 4. encrypType - tkip or aes-ccmp
*/
int wfaStaSetPSK(int len, BYTE *caCmdBuf, int *respLen, BYTE *respBuf)
{
/*Incompleted function*/
dutCmdResponse_t *setPskResp = &gGenericResp;
#ifndef WFA_PC_CONSOLE
caStaSetPSK_t *setPSK = (caStaSetPSK_t *)caCmdBuf;
#ifdef WFA_NEW_CLI_FORMAT
sprintf(gCmdStr, "wfa_set_psk %s %s %s", setPSK->intf, setPSK->ssid, setPSK->passphrase);
sret = system(gCmdStr);
#else
sprintf(gCmdStr, "wpa_cli -i %s set_network 0 ssid '\"%s\"'", setPSK->intf, setPSK->ssid);
sret = system(gCmdStr);
if(strcasecmp(setPSK->keyMgmtType, "wpa2-sha256") == 0)
sprintf(gCmdStr, "wpa_cli -i %s set_network 0 key_mgmt WPA2-SHA256", setPSK->intf);
else if(strcasecmp(setPSK->keyMgmtType, "wpa2") == 0)
{
// take all and device to pick it supported.
}
else if(strcasecmp(setPSK->keyMgmtType, "wpa2-psk") == 0)
{
}
else if(strcasecmp(setPSK->keyMgmtType, "wpa2-ft") == 0)
{
}
else if (strcasecmp(setPSK->keyMgmtType, "wpa2-wpa-psk") == 0)
{
}
else
sprintf(gCmdStr, "wpa_cli -i %s set_network 0 key_mgmt WPA-PSK", setPSK->intf);
sret = system(gCmdStr);
sprintf(gCmdStr, "wpa_cli -i %s set_network 0 psk '\"%s\"'", setPSK->intf, setPSK->passphrase);
sret = system(gCmdStr);
sprintf(gCmdStr, "wpa_cli -i %s enable_network 0", setPSK->intf);
sret = system(gCmdStr);
/* if PMF enable */
if(setPSK->pmf == WFA_ENABLED || setPSK->pmf == WFA_OPTIONAL)
{
}
else if(setPSK->pmf == WFA_REQUIRED)
{
}
else if(setPSK->pmf == WFA_F_REQUIRED)
{
}
else if(setPSK->pmf == WFA_F_DISABLED)
{
}
else
{
/* Disable PMF */
}