-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathwpa_helpers.c
1024 lines (859 loc) · 23.3 KB
/
wpa_helpers.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
/*
* Sigma Control API DUT (station/AP)
* Copyright (c) 2010-2011, Atheros Communications, Inc.
* Copyright (c) 2011-2014, 2016, Qualcomm Atheros, Inc.
* Copyright (c) 2018-2021, The Linux Foundation
* All Rights Reserved.
* Licensed under the Clear BSD license. See README for more details.
*/
#include "sigma_dut.h"
#include <sys/stat.h>
#include "wpa_ctrl.h"
#include "wpa_helpers.h"
#define DEFAULT_HAPD_CTRL_PATH "/var/run/hostapd/"
extern char *sigma_wpas_ctrl;
extern char *client_socket_path;
extern char *sigma_hapd_ctrl;
const char * get_main_ifname(struct sigma_dut *dut)
{
enum driver_type drv = get_driver_type(dut);
enum openwrt_driver_type openwrt_drv = get_openwrt_driver_type();
if (dut->main_ifname) {
if (dut->use_5g && dut->main_ifname_5g)
return dut->main_ifname_5g;
if (!dut->use_5g && dut->main_ifname_2g)
return dut->main_ifname_2g;
return dut->main_ifname;
}
if (drv == DRIVER_ATHEROS || openwrt_drv == OPENWRT_DRIVER_ATHEROS) {
if (if_nametoindex("ath2") > 0)
return "ath2";
else if (if_nametoindex("ath1") > 0)
return "ath1";
else
return "ath0";
}
if (if_nametoindex("p2p0") > 0)
return "p2p0";
if (if_nametoindex("wlan1") > 0) {
struct stat s;
if (stat("/sys/module/mac80211", &s) == 0 &&
if_nametoindex("wlan0")) {
/*
* Likely a dual-radio AP device; use wlan0 for STA/P2P
* operations.
*/
return "wlan0";
}
return "wlan1";
}
if (if_nametoindex("wlan0") > 0)
return "wlan0";
return "unknown";
}
const char * get_station_ifname(struct sigma_dut *dut)
{
if (dut->station_ifname) {
if (dut->use_5g && dut->station_ifname_5g)
return dut->station_ifname_5g;
if (!dut->use_5g && dut->station_ifname_2g)
return dut->station_ifname_2g;
return dut->station_ifname;
}
/*
* If we have both wlan0 and wlan1, assume the first one is the station
* interface.
*/
if (if_nametoindex("wlan1") > 0 && if_nametoindex("wlan0") > 0)
return "wlan0";
if (if_nametoindex("ath0") > 0)
return "ath0";
/* If nothing else matches, hope for best and guess.. */
return "wlan0";
}
const char * get_p2p_ifname(struct sigma_dut *dut, const char *primary_ifname)
{
if (strcmp(get_station_ifname(dut), primary_ifname) != 0)
return primary_ifname;
if (dut->p2p_ifname)
return dut->p2p_ifname;
return get_station_ifname(dut);
}
void dut_ifc_reset(struct sigma_dut *dut)
{
char buf[256];
const char *ifc = get_station_ifname(dut);
snprintf(buf, sizeof(buf), "ifconfig %s down", ifc);
run_system(dut, buf);
snprintf(buf, sizeof(buf), "ifconfig %s up", ifc);
run_system(dut, buf);
}
int wpa_ctrl_command(const char *path, const char *ifname, const char *cmd)
{
struct wpa_ctrl *ctrl;
char buf[128];
size_t len;
snprintf(buf, sizeof(buf), "%s%s", path, ifname);
ctrl = wpa_ctrl_open2(buf, client_socket_path);
if (ctrl == NULL) {
printf("wpa_command: wpa_ctrl_open2(%s) failed\n", buf);
return -1;
}
len = sizeof(buf);
if (wpa_ctrl_request(ctrl, cmd, strlen(cmd), buf, &len, NULL) < 0) {
printf("wpa_command: wpa_ctrl_request failed\n");
wpa_ctrl_close(ctrl);
return -1;
}
wpa_ctrl_close(ctrl);
buf[len] = '\0';
if (strncmp(buf, "FAIL", 4) == 0) {
printf("wpa_command: Command failed (FAIL received)\n");
return -1;
}
return 0;
}
int wpa_command(const char *ifname, const char *cmd)
{
printf("wpa_command(ifname='%s', cmd='%s')\n", ifname, cmd);
return wpa_ctrl_command(sigma_wpas_ctrl, ifname, cmd);
}
int hapd_command(const char *ifname, const char *cmd)
{
const char *path = sigma_hapd_ctrl ? sigma_hapd_ctrl :
DEFAULT_HAPD_CTRL_PATH;
printf("hapd_command(ifname='%s', cmd='%s')\n", ifname, cmd);
return wpa_ctrl_command(path, ifname, cmd);
}
int wpa_ctrl_command_resp(const char *path, const char *ifname,
const char *cmd, char *resp, size_t resp_size)
{
struct wpa_ctrl *ctrl;
char buf[128];
size_t len;
snprintf(buf, sizeof(buf), "%s%s", path, ifname);
ctrl = wpa_ctrl_open2(buf, client_socket_path);
if (ctrl == NULL) {
printf("wpa_command: wpa_ctrl_open2(%s) failed\n", buf);
return -1;
}
len = resp_size;
if (wpa_ctrl_request(ctrl, cmd, strlen(cmd), resp, &len, NULL) < 0) {
printf("wpa_command: wpa_ctrl_request failed\n");
wpa_ctrl_close(ctrl);
return -1;
}
wpa_ctrl_close(ctrl);
resp[len] = '\0';
return 0;
}
int wpa_command_resp(const char *ifname, const char *cmd,
char *resp, size_t resp_size)
{
printf("wpa_command(ifname='%s', cmd='%s')\n", ifname, cmd);
return wpa_ctrl_command_resp(sigma_wpas_ctrl, ifname, cmd,
resp, resp_size);
}
int hapd_command_resp(const char *ifname, const char *cmd,
char *resp, size_t resp_size)
{
const char *path = sigma_hapd_ctrl ? sigma_hapd_ctrl :
DEFAULT_HAPD_CTRL_PATH;
printf("hapd_command(ifname='%s', cmd='%s')\n", ifname, cmd);
return wpa_ctrl_command_resp(path, ifname, cmd, resp, resp_size);
}
struct wpa_ctrl * open_wpa_ctrl_mon(const char *ctrl_path, const char *ifname)
{
struct wpa_ctrl *ctrl;
char path[256];
snprintf(path, sizeof(path), "%s%s", ctrl_path, ifname);
ctrl = wpa_ctrl_open2(path, client_socket_path);
if (ctrl == NULL)
return NULL;
if (wpa_ctrl_attach(ctrl) < 0) {
wpa_ctrl_close(ctrl);
return NULL;
}
return ctrl;
}
struct wpa_ctrl * open_wpa_mon(const char *ifname)
{
return open_wpa_ctrl_mon(sigma_wpas_ctrl, ifname);
}
struct wpa_ctrl * open_hapd_mon(const char *ifname)
{
const char *path = sigma_hapd_ctrl ?
sigma_hapd_ctrl : DEFAULT_HAPD_CTRL_PATH;
return open_wpa_ctrl_mon(path, ifname);
}
int get_wpa_cli_events_timeout(struct sigma_dut *dut, struct wpa_ctrl *mon,
const char **events, char *buf, size_t buf_size,
unsigned int timeout)
{
int fd, ret;
fd_set rfd;
char *pos;
struct timeval tv;
time_t start, now;
int i;
for (i = 0; events[i]; i++) {
sigma_dut_print(dut, DUT_MSG_DEBUG,
"Waiting for wpa_cli event: %s", events[i]);
}
fd = wpa_ctrl_get_fd(mon);
if (fd < 0)
return -1;
if (timeout)
time(&start);
while (1) {
size_t len;
FD_ZERO(&rfd);
FD_SET(fd, &rfd);
if (timeout) {
time(&now);
if ((unsigned int) (now - start) >= timeout)
tv.tv_sec = 1;
else
tv.tv_sec = timeout -
(unsigned int) (now - start) + 1;
tv.tv_usec = 0;
}
ret = select(fd + 1, &rfd, NULL, NULL, timeout ? &tv : NULL);
if (ret == 0) {
sigma_dut_print(dut, DUT_MSG_INFO, "Timeout on "
"waiting for events");
return -1;
}
if (ret < 0) {
sigma_dut_print(dut, DUT_MSG_INFO, "select: %s",
strerror(errno));
return -1;
}
len = buf_size;
if (wpa_ctrl_recv(mon, buf, &len) < 0) {
sigma_dut_print(dut, DUT_MSG_ERROR, "Failure while "
"waiting for events");
return -1;
}
if (len == buf_size)
len--;
buf[len] = '\0';
pos = strchr(buf, '>');
if (pos) {
for (i = 0; events[i]; i++) {
if (strncmp(pos + 1, events[i],
strlen(events[i])) == 0)
return 0; /* Event found */
}
}
if (!timeout)
continue;
time(&now);
if ((unsigned int) (now - start) > timeout) {
sigma_dut_print(dut, DUT_MSG_INFO, "Timeout on "
"waiting for event");
return -1;
}
}
}
int get_wpa_cli_events(struct sigma_dut *dut, struct wpa_ctrl *mon,
const char **events, char *buf, size_t buf_size)
{
return get_wpa_cli_events_timeout(dut, mon, events, buf, buf_size,
dut->default_timeout);
}
int get_wpa_cli_event2(struct sigma_dut *dut, struct wpa_ctrl *mon,
const char *event, const char *event2,
char *buf, size_t buf_size)
{
const char *events[3] = { event, event2, NULL };
return get_wpa_cli_events(dut, mon, events, buf, buf_size);
}
int get_wpa_cli_event(struct sigma_dut *dut, struct wpa_ctrl *mon,
const char *event, char *buf, size_t buf_size)
{
return get_wpa_cli_event2(dut, mon, event, NULL, buf, buf_size);
}
/*
* signal_poll cmd output sample
* RSSI=-51
* LINKSPEED=866
* NOISE=-101
* FREQUENCY=5180
* AVG_RSSI=-50
*/
int get_wpa_signal_poll(struct sigma_dut *dut, const char *ifname,
const char *field, char *obuf, size_t obuf_size)
{
struct wpa_ctrl *ctrl;
char buf[4096];
char *pos, *end;
size_t len, flen;
snprintf(buf, sizeof(buf), "%s%s", sigma_wpas_ctrl, ifname);
ctrl = wpa_ctrl_open2(buf, client_socket_path);
if (!ctrl) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"Failed to connect to wpa_supplicant");
return -1;
}
len = sizeof(buf);
if (wpa_ctrl_request(ctrl, "SIGNAL_POLL", 11, buf, &len, NULL) < 0) {
wpa_ctrl_close(ctrl);
sigma_dut_print(dut, DUT_MSG_ERROR, "ctrl request failed");
return -1;
}
buf[len] = '\0';
wpa_ctrl_close(ctrl);
flen = strlen(field);
pos = buf;
while (pos + flen < buf + len) {
if (pos > buf) {
if (*pos != '\n') {
pos++;
continue;
}
pos++;
}
if (strncmp(pos, field, flen) != 0 || pos[flen] != '=') {
pos++;
continue;
}
pos += flen + 1;
end = strchr(pos, '\n');
if (!end) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"Could not find signal poll field '%s' - end is NULL",
field);
return -1;
}
*end++ = '\0';
if (end - pos > (int) obuf_size) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"signal poll out buffer is too small");
return -1;
}
memcpy(obuf, pos, end - pos);
return 0;
}
sigma_dut_print(dut, DUT_MSG_ERROR, "signal poll param not found");
return -1;
}
int get_wpa_ssid_bssid(struct sigma_dut *dut, const char *ifname,
char *buf, size_t buf_size)
{
struct wpa_ctrl *ctrl;
char buf_local[4096];
char *network, *ssid, *bssid;
size_t buf_size_local;
unsigned int count = 0;
int len, res;
char *save_ptr_network = NULL;
ctrl = open_wpa_mon(ifname);
if (!ctrl) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"Failed to connect to wpa_supplicant");
return -1;
}
wpa_command(ifname, "BSS_FLUSH");
if (wpa_command(ifname, "SCAN TYPE=ONLY")) {
wpa_ctrl_detach(ctrl);
wpa_ctrl_close(ctrl);
sigma_dut_print(dut, DUT_MSG_ERROR, "SCAN command failed");
return -1;
}
res = get_wpa_cli_event(dut, ctrl, "CTRL-EVENT-SCAN-RESULTS",
buf_local, sizeof(buf_local));
wpa_ctrl_detach(ctrl);
buf_size_local = sizeof(buf_local);
if (res < 0 || wpa_ctrl_request(ctrl, "BSS RANGE=ALL MASK=0x1002", 25,
buf_local, &buf_size_local, NULL) < 0) {
wpa_ctrl_close(ctrl);
sigma_dut_print(dut, DUT_MSG_ERROR, "BSS ctrl request failed");
return -1;
}
buf_local[buf_size_local] = '\0';
wpa_ctrl_close(ctrl);
/* Below is BSS RANGE=ALL MASK=0x1002 command sample output which is
* parsed to get the BSSID and SSID parameters.
* Even number of lines, first line BSSID of network 1, second line SSID
* of network 1, ...
*
* bssid=xx:xx:xx:xx:xx:x1
* ssid=SSID1
* bssid=xx:xx:xx:xx:xx:x2
* ssid=SSID2
*/
network = strtok_r(buf_local, "\n", &save_ptr_network);
while (network) {
sigma_dut_print(dut, DUT_MSG_DEBUG, "BSSID: %s", network);
bssid = NULL;
if (!strtok_r(network, "=", &bssid)) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"Invalid BSS result: BSSID not found");
return -1;
}
network = strtok_r(NULL, "\n", &save_ptr_network);
if (network) {
sigma_dut_print(dut, DUT_MSG_DEBUG, "SSID: %s",
network);
ssid = NULL;
if (!strtok_r(network, "=", &ssid)) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"Invalid BSS result: SSID is null");
return -1;
}
} else {
sigma_dut_print(dut, DUT_MSG_ERROR,
"Invalid BSS result: SSID not found");
return -1;
}
/* Skip comma for first entry */
count++;
len = snprintf(buf, buf_size, "%sSSID%d,%s,BSSID%d,%s",
count > 1 ? "," : "",
count, ssid, count, bssid);
if (len < 0 || (size_t) len >= buf_size) {
buf[0] = '\0';
return 0;
}
buf_size -= len;
buf += len;
network = strtok_r(NULL, "\n", &save_ptr_network);
}
return 0;
}
static int get_wpa_ctrl_mlo_status(const char *path, const char *ifname,
const char *cmd, char *obuf,
size_t obuf_size)
{
struct wpa_ctrl *ctrl;
char buf[4096];
size_t len;
int res;
res = snprintf(buf, sizeof(buf), "%s%s", path, ifname);
if (res < 0 || res >= sizeof(buf))
return -1;
ctrl = wpa_ctrl_open2(buf, client_socket_path);
if (!ctrl)
return -1;
len = sizeof(buf);
if (wpa_ctrl_request(ctrl, cmd, strlen(cmd), buf, &len, NULL) < 0) {
wpa_ctrl_close(ctrl);
return -1;
}
wpa_ctrl_close(ctrl);
buf[len] = '\0';
if (len >= obuf_size)
return -1;
memcpy(obuf, buf, len + 1);
return 0;
}
static int get_wpa_mlo_status(const char *ifname, char *obuf, size_t obuf_size)
{
return get_wpa_ctrl_mlo_status(sigma_wpas_ctrl, ifname, "MLO_STATUS",
obuf, obuf_size);
}
int get_mlo_link_mac_ap_link(struct sigma_dut *dut, const char *ifname,
const char *ap_link_addr,
char *obuf, size_t obuf_size)
{
char buf[4096];
char *param;
size_t flen, flen2;
int ap_link_match = 0;
char *save_ptr = NULL;
if (get_wpa_mlo_status(ifname, buf, sizeof(buf))) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"Failed to get MLO status");
return -1;
}
flen = strlen("ap_link_addr");
flen2 = strlen("sta_link_addr");
param = strtok_r(buf, "\n", &save_ptr);
while (param) {
if (strncasecmp(param, "ap_link_addr", flen) == 0 &&
strlen(param) > flen + 1 &&
strncasecmp(¶m[flen + 1], ap_link_addr, 18) == 0)
ap_link_match = 1;
if (ap_link_match) {
if (strncasecmp(param, "sta_link_addr", flen2) == 0 &&
strlen(param) > flen2 + 1) {
sigma_dut_print(dut, DUT_MSG_DEBUG,
"STA link addr %s",
¶m[flen2 + 1]);
strlcpy(obuf, ¶m[flen2 + 1], obuf_size);
return 0;
}
}
param = strtok_r(NULL, "\n", &save_ptr);
}
if (!ap_link_match)
sigma_dut_print(dut, DUT_MSG_ERROR,
"AP link address not found");
return -1;
}
int get_mlo_link_id_link_mac(struct sigma_dut *dut, const char *ifname,
const char *link_addr,
char *obuf, size_t obuf_size)
{
char buf[4096];
char *param;
size_t flen, flen2;
char *saveptr = NULL;
if (get_wpa_mlo_status(ifname, buf, sizeof(buf))) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"Failed to get MLO Status");
return -1;
}
flen = strlen("sta_link_addr");
flen2 = strlen("link_id");
param = strtok_r(buf, "\n", &saveptr);
while (param) {
if (strncasecmp(param, "link_id", flen2) == 0)
strlcpy(obuf, ¶m[flen2 + 1], obuf_size);
if (strncasecmp(param, "sta_link_addr", flen) == 0) {
if (strncasecmp(¶m[flen + 1], link_addr, 18) == 0) {
sigma_dut_print(dut, DUT_MSG_INFO,
"MLO link id for STA link MAC is %s",
¶m[flen2 + 1]);
return 0;
}
}
param = strtok_r(NULL, "\n", &saveptr);
}
sigma_dut_print(dut, DUT_MSG_ERROR, "link id not found");
return -1;
}
int get_connected_mlo_link_ids(struct sigma_dut *dut, const char *ifname)
{
char buf[4096];
char *param;
size_t flen;
char *saveptr = NULL;
int links_bitmask = 0;
if (get_wpa_status(ifname, "wpa_state", buf, sizeof(buf)) < 0 ||
strncmp(buf, "COMPLETED", 9) != 0) {
sigma_dut_print(dut, DUT_MSG_DEBUG, "%s: Not connected",
__func__);
return 0;
}
if (get_wpa_mlo_status(ifname, buf, sizeof(buf))) {
sigma_dut_print(dut, DUT_MSG_DEBUG, "%s: Non-MLO connection",
__func__);
return 0;
}
flen = strlen("link_id");
param = strtok_r(buf, "\n", &saveptr);
while (param) {
if (strncasecmp(param, "link_id", flen) == 0) {
int link_id = atoi(¶m[flen + 1]);
sigma_dut_print(dut, DUT_MSG_DEBUG,
"Found connected link ID %d", link_id);
links_bitmask |= BIT(link_id);
}
param = strtok_r(NULL, "\n", &saveptr);
}
return links_bitmask;
}
static int get_wpa_ctrl_status_field(const char *path, const char *ifname,
const char *cmd, const char *field,
char *obuf, size_t obuf_size)
{
struct wpa_ctrl *ctrl;
char buf[4096];
char *pos, *end;
size_t len, flen;
snprintf(buf, sizeof(buf), "%s%s", path, ifname);
ctrl = wpa_ctrl_open2(buf, client_socket_path);
if (ctrl == NULL)
return -1;
len = sizeof(buf);
if (wpa_ctrl_request(ctrl, cmd, strlen(cmd), buf, &len, NULL) < 0) {
wpa_ctrl_close(ctrl);
return -1;
}
wpa_ctrl_close(ctrl);
buf[len] = '\0';
flen = strlen(field);
pos = buf;
while (pos + flen < buf + len) {
if (pos > buf) {
if (*pos != '\n') {
pos++;
continue;
}
pos++;
}
if (strncmp(pos, field, flen) != 0 || pos[flen] != '=') {
pos++;
continue;
}
pos += flen + 1;
end = strchr(pos, '\n');
if (end == NULL)
return -1;
*end++ = '\0';
if (end - pos > (int) obuf_size)
return -1;
memcpy(obuf, pos, end - pos);
return 0;
}
return -1;
}
static int get_hapd_status(const char *ifname, const char *field, char *obuf,
size_t obuf_size)
{
const char *path = sigma_hapd_ctrl ?
sigma_hapd_ctrl : DEFAULT_HAPD_CTRL_PATH;
return get_wpa_ctrl_status_field(path, ifname, "STATUS",
field, obuf, obuf_size);
}
int ap_get_mlo_link_id(struct sigma_dut *dut, const char *ifname)
{
char buf[4096];
int link_id = -1;
if (get_hapd_status(ifname, "mld_link_id[0]", buf, sizeof(buf)) < 0) {
sigma_dut_print(dut, DUT_MSG_DEBUG, "%s: MLD Link ID not found",
__func__);
return -1;
}
link_id = atoi(buf);
sigma_dut_print(dut, DUT_MSG_DEBUG,
"%s: Found AP link ID %d", __func__, link_id);
return link_id;
}
int get_wpa_status(const char *ifname, const char *field, char *obuf,
size_t obuf_size)
{
return get_wpa_ctrl_status_field(sigma_wpas_ctrl, ifname, "STATUS",
field, obuf, obuf_size);
}
int get_hapd_config(const char *ifname, const char *field, char *obuf,
size_t obuf_size)
{
const char *path = sigma_hapd_ctrl ?
sigma_hapd_ctrl : DEFAULT_HAPD_CTRL_PATH;
return get_wpa_ctrl_status_field(path, ifname, "GET_CONFIG",
field, obuf, obuf_size);
}
int wait_ip_addr(struct sigma_dut *dut, const char *ifname, int timeout)
{
char ip[30];
int count = timeout;
while (count > 0) {
sigma_dut_print(dut, DUT_MSG_DEBUG, "%s: ifname='%s' - %d "
"seconds remaining",
__func__, ifname, count);
count--;
if (get_wpa_status(ifname, "ip_address", ip, sizeof(ip)) == 0
&& strlen(ip) > 0) {
sigma_dut_print(dut, DUT_MSG_INFO, "IP address "
"found: '%s'", ip);
return 0;
}
sleep(1);
}
sigma_dut_print(dut, DUT_MSG_INFO, "%s: Could not get IP address for "
"ifname='%s'", __func__, ifname);
return -1;
}
void remove_wpa_networks(const char *ifname)
{
char buf[4096];
char cmd[256];
char *pos;
if (wpa_command_resp(ifname, "LIST_NETWORKS", buf, sizeof(buf)) < 0)
return;
/* Skip the first line (header) */
pos = strchr(buf, '\n');
if (pos == NULL)
return;
pos++;
while (pos && pos[0]) {
int id = atoi(pos);
snprintf(cmd, sizeof(cmd), "REMOVE_NETWORK %d", id);
wpa_command(ifname, cmd);
pos = strchr(pos, '\n');
if (pos)
pos++;
}
}
int add_network(const char *ifname)
{
char res[30];
if (wpa_command_resp(ifname, "ADD_NETWORK", res, sizeof(res)) < 0)
return -1;
return atoi(res);
}
int set_network(const char *ifname, int id, const char *field,
const char *value)
{
char buf[200];
snprintf(buf, sizeof(buf), "SET_NETWORK %d %s %s", id, field, value);
return wpa_command(ifname, buf);
}
int set_network_quoted(const char *ifname, int id, const char *field,
const char *value)
{
char buf[200];
snprintf(buf, sizeof(buf), "SET_NETWORK %d %s \"%s\"",
id, field, value);
return wpa_command(ifname, buf);
}
int add_cred(const char *ifname)
{
char res[30];
if (wpa_command_resp(ifname, "ADD_CRED", res, sizeof(res)) < 0)
return -1;
return atoi(res);
}
int set_cred(const char *ifname, int id, const char *field, const char *value)
{
char buf[200];
snprintf(buf, sizeof(buf), "SET_CRED %d %s %s", id, field, value);
return wpa_command(ifname, buf);
}
int set_cred_quoted(const char *ifname, int id, const char *field,
const char *value)
{
char buf[200];
snprintf(buf, sizeof(buf), "SET_CRED %d %s \"%s\"",
id, field, value);
return wpa_command(ifname, buf);
}
const char * concat_sigma_tmpdir(struct sigma_dut *dut, const char *src,
char *dst, size_t len)
{
snprintf(dst, len, "%s%s", dut->sigma_tmpdir, src);
return dst;
}
int start_sta_mode(struct sigma_dut *dut)
{
FILE *f;
char buf[256];
char sta_conf_path[100];
const char *ifname;
char *tmp, *pos;
if (dut->mode == SIGMA_MODE_STATION) {
if ((dut->use_5g && dut->sta_2g_started) ||
(!dut->use_5g && dut->sta_5g_started)) {
stop_sta_mode(dut);
sleep(1);
} else {
return 0;
}
}
if (dut->mode == SIGMA_MODE_AP) {
if (system("killall hostapd") == 0) {
int i;
/* Wait some time to allow hostapd to complete cleanup
* before starting a new process */
for (i = 0; i < 10; i++) {
usleep(500000);
if (system("pidof hostapd") != 0)
break;
}
}
}
if (dut->mode == SIGMA_MODE_SNIFFER && dut->sniffer_ifname) {
snprintf(buf, sizeof(buf), "ifconfig %s down",
dut->sniffer_ifname);
if (system(buf) != 0) {
sigma_dut_print(dut, DUT_MSG_INFO,
"Failed to run '%s'", buf);
}
snprintf(buf, sizeof(buf), "iw dev %s set type station",
dut->sniffer_ifname);
if (system(buf) != 0) {
sigma_dut_print(dut, DUT_MSG_INFO,
"Failed to run '%s'", buf);
}
}
dut->mode = SIGMA_MODE_STATION;
ifname = get_main_ifname(dut);
if (wpa_command(ifname, "PING") == 0)
return 0; /* wpa_supplicant is already running */
/* Start wpa_supplicant */
f = fopen(concat_sigma_tmpdir(dut, "/sigma_dut-sta.conf", sta_conf_path,
sizeof(sta_conf_path)), "w");
if (f == NULL)
return -1;
tmp = strdup(sigma_wpas_ctrl);
if (tmp == NULL) {
fclose(f);
return -1;
}
pos = tmp;
while (pos[0] != '\0' && pos[1] != '\0')
pos++;
if (*pos == '/')
*pos = '\0';
fprintf(f, "ctrl_interface=%s\n", tmp);
free(tmp);
fprintf(f, "device_name=Test client\n");
fprintf(f, "device_type=1-0050F204-1\n");
if (is_60g_sigma_dut(dut)) {
fprintf(f, "eapol_version=2\n");
fprintf(f,
"config_methods=display push_button keypad virtual_display physical_display virtual_push_button\n");
}
fclose(f);
#ifdef __QNXNTO__
snprintf(buf, sizeof(buf),
"wpa_supplicant -Dqca -i%s -B %s%s%s -c %s/sigma_dut-sta.conf",
ifname,
dut->wpa_supplicant_debug_log ? "-K -t -ddd " : "",
(dut->wpa_supplicant_debug_log &&
dut->wpa_supplicant_debug_log[0]) ? "-f " : "",
dut->wpa_supplicant_debug_log ?
dut->wpa_supplicant_debug_log : "",
dut->sigma_tmpdir);
#else /*__QNXNTO__*/
snprintf(buf, sizeof(buf),
"%swpa_supplicant -Dnl80211 -i%s -B %s%s%s -c %s/sigma_dut-sta.conf",
file_exists("wpa_supplicant") ? "./" : "",
ifname,
dut->wpa_supplicant_debug_log ? "-K -t -ddd " : "",
(dut->wpa_supplicant_debug_log &&
dut->wpa_supplicant_debug_log[0]) ? "-f " : "",
dut->wpa_supplicant_debug_log ?
dut->wpa_supplicant_debug_log : "",
dut->sigma_tmpdir);
#endif /*__QNXNTO__*/
if (system(buf) != 0) {
sigma_dut_print(dut, DUT_MSG_INFO, "Failed to run '%s'", buf);
return -1;
}
sleep(1);
if (wpa_command(ifname, "PING")) {
sigma_dut_print(dut, DUT_MSG_INFO, "Failed to communicate "
"with wpa_supplicant");
return -1;
}
if (dut->use_5g)
dut->sta_5g_started = 1;
else
dut->sta_2g_started = 1;
return 0;
}