-
Notifications
You must be signed in to change notification settings - Fork 26
/
sta.c
19585 lines (16837 loc) · 486 KB
/
sta.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-2017, 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/ioctl.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <ctype.h>
#ifdef __linux__
#include <regex.h>
#include <dirent.h>
#include <sys/time.h>
#include <netpacket/packet.h>
#include <linux/if_ether.h>
#ifdef ANDROID
#include <cutils/properties.h>
#include <android/log.h>
#include "keystore_get.h"
#else /* ANDROID */
#include <ifaddrs.h>
#endif /* ANDROID */
#include <netdb.h>
#endif /* __linux__ */
#ifdef __QNXNTO__
#include <net/if_dl.h>
#endif /* __QNXNTO__ */
#include "wpa_ctrl.h"
#include "wpa_helpers.h"
#include "miracast.h"
#include "qca-vendor_copy.h"
#include "nl80211_copy.h"
/* Temporary files for sta_send_addba */
#define VI_QOS_TMP_FILE "/tmp/vi-qos.tmp"
#define VI_QOS_FILE "/tmp/vi-qos.txt"
#define VI_QOS_REFFILE "/etc/vi-qos.txt"
/*
* MTU for Ethernet need to take into account 8-byte SNAP header
* to be added when encapsulating Ethernet frame into 802.11
*/
#ifndef IEEE80211_MAX_DATA_LEN_DMG
#define IEEE80211_MAX_DATA_LEN_DMG 7920
#endif
#ifndef IEEE80211_SNAP_LEN_DMG
#define IEEE80211_SNAP_LEN_DMG 8
#endif
#define NON_PREF_CH_LIST_SIZE 100
#define NEIGHBOR_REPORT_SIZE 1000
#define DEFAULT_NEIGHBOR_BSSID_INFO "17"
#define DEFAULT_NEIGHBOR_PHY_TYPE "1"
#define WIL_DEFAULT_BI 100
/* default remain on channel time for transmitting frames (milliseconds) */
#define WIL_TRANSMIT_FRAME_DEFAULT_ROC 500
#define IEEE80211_P2P_ATTR_DEVICE_ID 3
#define IEEE80211_P2P_ATTR_GROUP_ID 15
/* describes tagged bytes in template frame file */
struct template_frame_tag {
int num;
int offset;
size_t len;
};
extern char *sigma_wpas_ctrl;
extern char *sigma_cert_path;
extern enum driver_type wifi_chip_type;
extern char *sigma_radio_ifname[];
#ifdef __linux__
#define WIL_WMI_MAX_PAYLOAD 248
#define WIL_WMI_ESE_CFG_CMDID 0xa01
#define WIL_WMI_BF_TRIG_CMDID 0x83a
#define WIL_WMI_UNIT_TEST_CMDID 0x900
#define WIL_WMI_P2P_CFG_CMDID 0x910
#define WIL_WMI_START_LISTEN_CMDID 0x914
#define WIL_WMI_DISCOVERY_STOP_CMDID 0x917
struct wil_wmi_header {
uint8_t mid;
uint8_t reserved;
uint16_t cmd;
uint32_t ts;
} __attribute__((packed));
enum wil_wmi_bf_trig_type {
WIL_WMI_SLS,
WIL_WMI_BRP_RX,
WIL_WMI_BRP_TX,
};
struct wil_wmi_bf_trig_cmd {
/* enum wil_wmi_bf_trig_type */
uint32_t bf_type;
/* cid when type == WMI_BRP_RX */
uint32_t sta_id;
uint32_t reserved;
/* mac address when type = WIL_WMI_SLS */
uint8_t dest_mac[6];
} __attribute__((packed));
enum wil_wmi_sched_scheme_advertisment {
WIL_WMI_ADVERTISE_ESE_DISABLED,
WIL_WMI_ADVERTISE_ESE_IN_BEACON,
WIL_WMI_ADVERTISE_ESE_IN_ANNOUNCE_FRAME,
};
enum wil_wmi_ese_slot_type {
WIL_WMI_ESE_SP,
WIL_WMI_ESE_CBAP,
WIL_WMI_ESE_ANNOUNCE_NO_ACK,
};
struct wil_wmi_ese_slot {
/* offset from start of BI in microseconds */
uint32_t tbtt_offset;
uint8_t flags;
/* enum wil_wmi_ese_slot_type */
uint8_t slot_type;
/* duration in microseconds */
uint16_t duration;
/* frame exchange sequence duration, microseconds */
uint16_t tx_op;
/* time between 2 blocks for periodic allocation(microseconds) */
uint16_t period;
/* number of blocks in periodic allocation */
uint8_t num_blocks;
/* for semi-active allocations */
uint8_t idle_period;
uint8_t src_aid;
uint8_t dst_aid;
uint32_t reserved;
} __attribute__((packed));
#define WIL_WMI_MAX_ESE_SLOTS 4
struct wil_wmi_ese_cfg {
uint8_t serial_num;
/* wil_wmi_sched_scheme_advertisment */
uint8_t ese_advertisment;
uint16_t flags;
uint8_t num_allocs;
uint8_t reserved[3];
uint64_t start_tbtt;
/* allocations list */
struct wil_wmi_ese_slot slots[WIL_WMI_MAX_ESE_SLOTS];
} __attribute__((packed));
#define WIL_WMI_UT_FORCE_MCS 6
struct wil_wmi_force_mcs {
/* WIL_WMI_UT_HW_SYSAPI */
uint16_t module_id;
/* WIL_WMI_UT_FORCE_MCS */
uint16_t subtype_id;
/* cid (ignored in oob_mode, affects all stations) */
uint32_t cid;
/* 1 to force MCS, 0 to restore default behavior */
uint32_t force_enable;
/* MCS index, 0-12 */
uint32_t mcs;
} __attribute__((packed));
#define WIL_WMI_UT_HW_SYSAPI 10
#define WIL_WMI_UT_FORCE_RSN_IE 0x29
struct wil_wmi_force_rsn_ie {
/* WIL_WMI_UT_HW_SYSAPI */
uint16_t module_id;
/* WIL_WMI_UT_FORCE_RSN_IE */
uint16_t subtype_id;
/* 0 = no change, 1 = remove if exists, 2 = add if does not exist */
uint32_t state;
} __attribute__((packed));
enum wil_wmi_discovery_mode {
WMI_DISCOVERY_MODE_NON_OFFLOAD,
WMI_DISCOVERY_MODE_OFFLOAD,
WMI_DISCOVERY_MODE_PEER2PEER,
};
struct wil_wmi_p2p_cfg_cmd {
/* enum wil_wmi_discovery_mode */
uint8_t discovery_mode;
/* 0-based (wireless channel - 1) */
uint8_t channel;
/* set to WIL_DEFAULT_BI */
uint16_t bcon_interval;
} __attribute__((packed));
#endif /* __linux__ */
static int get_key_mgmt_capa(struct sigma_dut *dut);
#ifdef ANDROID
static int add_ipv6_rule(struct sigma_dut *dut, const char *ifname);
#define ANDROID_KEYSTORE_GET 'g'
#define ANDROID_KEYSTORE_GET_PUBKEY 'b'
static int android_keystore_get(char cmd, const char *key, unsigned char *val)
{
/* Android 4.3 changed keystore design, so need to use keystore_get() */
#ifndef KEYSTORE_MESSAGE_SIZE
#define KEYSTORE_MESSAGE_SIZE 65535
#endif /* KEYSTORE_MESSAGE_SIZE */
ssize_t len;
uint8_t *value = NULL;
__android_log_print(ANDROID_LOG_DEBUG, "sigma_dut",
"keystore command '%c' key '%s' --> keystore_get",
cmd, key);
len = keystore_get(key, strlen(key), &value);
if (len < 0) {
__android_log_print(ANDROID_LOG_DEBUG, "sigma_dut",
"keystore_get() failed");
return -1;
}
if (len > KEYSTORE_MESSAGE_SIZE)
len = KEYSTORE_MESSAGE_SIZE;
memcpy(val, value, len);
free(value);
return len;
}
#endif /* ANDROID */
#ifdef NL80211_SUPPORT
static int nl80211_sta_set_power_save(struct sigma_dut *dut,
const char *intf,
enum nl80211_ps_state ps_state)
{
struct nl_msg *msg;
int ifindex, ret;
ifindex = if_nametoindex(intf);
if (ifindex == 0) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"%s: Index for interface %s not found",
__func__, intf);
return -1;
}
msg = nl80211_drv_msg(dut, dut->nl_ctx, ifindex, 0,
NL80211_CMD_SET_POWER_SAVE);
if (!msg) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"%s: err in creating nl80211 msg", __func__);
return -1;
}
if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state)) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"%s: err in populating nl80211 msg", __func__);
nlmsg_free(msg);
return -1;
}
ret = send_and_recv_msgs(dut, dut->nl_ctx, msg, NULL, NULL);
if (ret) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"%s: err in send_and_recv_msgs, ret=%d (%s)",
__func__, ret, strerror(-ret));
return -1;
}
return 0;
}
#endif /* NL80211_SUPPORT */
static int set_power_save_wcn(struct sigma_dut *dut, const char *intf, int ps)
{
char buf[100];
#ifdef NL80211_SUPPORT
enum nl80211_ps_state ps_state;
ps_state = ps == 1 ? NL80211_PS_ENABLED : NL80211_PS_DISABLED;
if (nl80211_sta_set_power_save(dut, intf, ps_state) == 0)
return 0;
#endif /* NL80211_SUPPORT */
snprintf(buf, sizeof(buf), "iwpriv %s setPower %d", intf, ps);
if (system(buf) != 0) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"iwpriv setPower %d failed", ps);
return -1;
}
return 0;
}
int set_ps(const char *intf, struct sigma_dut *dut, int enabled)
{
#ifdef __linux__
char buf[100];
if (wifi_chip_type == DRIVER_WCN) {
if (enabled) {
if (set_power_save_wcn(dut, intf, 1) < 0) {
snprintf(buf, sizeof(buf),
"iwpriv wlan0 dump 906");
if (system(buf) != 0)
goto set_power_save;
}
} else {
if (set_power_save_wcn(dut, intf, 2) < 0) {
snprintf(buf, sizeof(buf),
"iwpriv wlan0 dump 905");
if (system(buf) != 0)
goto set_power_save;
snprintf(buf, sizeof(buf),
"iwpriv wlan0 dump 912");
if (system(buf) != 0)
goto set_power_save;
}
}
return 0;
}
set_power_save:
snprintf(buf, sizeof(buf), "./iw dev %s set power_save %s",
intf, enabled ? "on" : "off");
if (system(buf) != 0) {
snprintf(buf, sizeof(buf), "iw dev %s set power_save %s",
intf, enabled ? "on" : "off");
if (system(buf) != 0) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"Failed to set power save %s",
enabled ? "on" : "off");
return -1;
}
}
return 0;
#else /* __linux__ */
return -1;
#endif /* __linux__ */
}
#ifdef __linux__
static int wil6210_get_debugfs_dir(struct sigma_dut *dut, char *path,
size_t len)
{
DIR *dir, *wil_dir;
struct dirent *entry;
int ret = -1;
const char *root_path = "/sys/kernel/debug/ieee80211";
dir = opendir(root_path);
if (!dir)
return -2;
while ((entry = readdir(dir))) {
if (strcmp(entry->d_name, ".") == 0 ||
strcmp(entry->d_name, "..") == 0)
continue;
if (snprintf(path, len, "%s/%s/wil6210",
root_path, entry->d_name) >= (int) len) {
ret = -3;
break;
}
wil_dir = opendir(path);
if (wil_dir) {
closedir(wil_dir);
ret = 0;
break;
}
}
closedir(dir);
return ret;
}
static int wil6210_wmi_send(struct sigma_dut *dut, uint16_t command,
void *payload, uint16_t length)
{
struct {
struct wil_wmi_header hdr;
char payload[WIL_WMI_MAX_PAYLOAD];
} __attribute__((packed)) cmd;
char buf[128], fname[128];
size_t towrite, written;
FILE *f;
int res;
if (length > WIL_WMI_MAX_PAYLOAD) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"payload too large(%u, max %u)",
length, WIL_WMI_MAX_PAYLOAD);
return -1;
}
memset(&cmd.hdr, 0, sizeof(cmd.hdr));
cmd.hdr.cmd = command;
memcpy(cmd.payload, payload, length);
if (wil6210_get_debugfs_dir(dut, buf, sizeof(buf))) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"failed to get wil6210 debugfs dir");
return -1;
}
res = snprintf(fname, sizeof(fname), "%s/wmi_send", buf);
if (res < 0 || res >= sizeof(fname))
return -1;
f = fopen(fname, "wb");
if (!f) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"failed to open: %s", fname);
return -1;
}
towrite = sizeof(cmd.hdr) + length;
written = fwrite(&cmd, 1, towrite, f);
fclose(f);
if (written != towrite) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"failed to send wmi %u", command);
return -1;
}
return 0;
}
static int wil6210_get_sta_info_field(struct sigma_dut *dut, const char *bssid,
const char *pattern, unsigned int *field)
{
char buf[128], fname[128];
FILE *f;
regex_t re;
regmatch_t m[2];
int rc, ret = -1, res;
if (wil6210_get_debugfs_dir(dut, buf, sizeof(buf))) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"failed to get wil6210 debugfs dir");
return -1;
}
res = snprintf(fname, sizeof(fname), "%s/stations", buf);
if (res < 0 || res >= sizeof(fname))
return -1;
f = fopen(fname, "r");
if (!f) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"failed to open: %s", fname);
return -1;
}
if (regcomp(&re, pattern, REG_EXTENDED)) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"regcomp failed: %s", pattern);
goto out;
}
/*
* find the entry for the mac address
* line is of the form: [n] 11:22:33:44:55:66 state AID aid
*/
while (fgets(buf, sizeof(buf), f)) {
if (strcasestr(buf, bssid)) {
/* extract the field (CID/AID/state) */
rc = regexec(&re, buf, 2, m, 0);
if (!rc && (m[1].rm_so >= 0)) {
buf[m[1].rm_eo] = 0;
*field = atoi(&buf[m[1].rm_so]);
ret = 0;
break;
}
}
}
regfree(&re);
if (ret)
sigma_dut_print(dut, DUT_MSG_ERROR,
"could not extract field");
out:
fclose(f);
return ret;
}
static int wil6210_get_cid(struct sigma_dut *dut, const char *bssid,
unsigned int *cid)
{
const char *pattern = "\\[([0-9]+)\\]";
return wil6210_get_sta_info_field(dut, bssid, pattern, cid);
}
static int wil6210_send_brp_rx(struct sigma_dut *dut, const char *mac,
int l_rx)
{
struct wil_wmi_bf_trig_cmd cmd;
unsigned int cid;
memset(&cmd, 0, sizeof(cmd));
if (wil6210_get_cid(dut, mac, &cid))
return -1;
cmd.bf_type = WIL_WMI_BRP_RX;
cmd.sta_id = cid;
/* training length (l_rx) is ignored, FW always uses length 16 */
return wil6210_wmi_send(dut, WIL_WMI_BF_TRIG_CMDID,
&cmd, sizeof(cmd));
}
static int wil6210_send_sls(struct sigma_dut *dut, const char *mac)
{
struct wil_wmi_bf_trig_cmd cmd;
memset(&cmd, 0, sizeof(cmd));
if (parse_mac_address(dut, mac, (unsigned char *)&cmd.dest_mac))
return -1;
cmd.bf_type = WIL_WMI_SLS;
return wil6210_wmi_send(dut, WIL_WMI_BF_TRIG_CMDID,
&cmd, sizeof(cmd));
}
int wil6210_set_ese(struct sigma_dut *dut, int count,
struct sigma_ese_alloc *allocs)
{
struct wil_wmi_ese_cfg cmd = { };
int i;
if (count == 0 || count > WIL_WMI_MAX_ESE_SLOTS)
return -1;
if (dut->ap_bcnint <= 0) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"invalid beacon interval(%d), check test",
dut->ap_bcnint);
return -1;
}
cmd.ese_advertisment = WIL_WMI_ADVERTISE_ESE_IN_BEACON;
cmd.flags = 0x1d;
cmd.num_allocs = count;
for (i = 0; i < count; i++) {
/*
* Convert percent from BI (BI specified in milliseconds)
* to absolute duration in microseconds.
*/
cmd.slots[i].duration =
(allocs[i].percent_bi * dut->ap_bcnint * 1000) / 100;
switch (allocs[i].type) {
case ESE_CBAP:
cmd.slots[i].slot_type = WIL_WMI_ESE_CBAP;
break;
case ESE_SP:
cmd.slots[i].slot_type = WIL_WMI_ESE_SP;
break;
default:
sigma_dut_print(dut, DUT_MSG_ERROR,
"invalid slot type(%d) at index %d",
allocs[i].type, i);
return -1;
}
cmd.slots[i].src_aid = allocs[i].src_aid;
cmd.slots[i].dst_aid = allocs[i].dst_aid;
sigma_dut_print(dut, DUT_MSG_INFO,
"slot %d, duration %u, type %d, srcAID %u dstAID %u",
i, cmd.slots[i].duration,
cmd.slots[i].slot_type, cmd.slots[i].src_aid,
cmd.slots[i].dst_aid);
}
return wil6210_wmi_send(dut, WIL_WMI_ESE_CFG_CMDID, &cmd, sizeof(cmd));
}
int wil6210_set_force_mcs(struct sigma_dut *dut, int force, int mcs)
{
struct wil_wmi_force_mcs cmd = { };
cmd.module_id = WIL_WMI_UT_HW_SYSAPI;
cmd.subtype_id = WIL_WMI_UT_FORCE_MCS;
cmd.force_enable = (uint32_t) force;
cmd.mcs = (uint32_t) mcs;
return wil6210_wmi_send(dut, WIL_WMI_UNIT_TEST_CMDID,
&cmd, sizeof(cmd));
}
static int wil6210_force_rsn_ie(struct sigma_dut *dut, int state)
{
struct wil_wmi_force_rsn_ie cmd = { };
cmd.module_id = WIL_WMI_UT_HW_SYSAPI;
cmd.subtype_id = WIL_WMI_UT_FORCE_RSN_IE;
cmd.state = (uint32_t) state;
return wil6210_wmi_send(dut, WIL_WMI_UNIT_TEST_CMDID,
&cmd, sizeof(cmd));
}
/*
* this function is also used to configure generic remain-on-channel
*/
static int wil6210_p2p_cfg(struct sigma_dut *dut, int freq)
{
struct wil_wmi_p2p_cfg_cmd cmd = { };
int channel = freq_to_channel(freq);
if (channel < 0)
return -1;
cmd.discovery_mode = WMI_DISCOVERY_MODE_NON_OFFLOAD;
cmd.channel = channel - 1;
cmd.bcon_interval = WIL_DEFAULT_BI;
cmd.discovery_mode = WMI_DISCOVERY_MODE_PEER2PEER;
return wil6210_wmi_send(dut, WIL_WMI_P2P_CFG_CMDID,
&cmd, sizeof(cmd));
}
static int wil6210_remain_on_channel(struct sigma_dut *dut, int freq)
{
int ret = wil6210_p2p_cfg(dut, freq);
if (ret)
return ret;
ret = wil6210_wmi_send(dut, WIL_WMI_START_LISTEN_CMDID, NULL, 0);
if (!ret) {
/*
* wait a bit to allow FW to setup the radio
* especially important if we switch channels
*/
usleep(500000);
}
return ret;
}
static int wil6210_stop_discovery(struct sigma_dut *dut)
{
return wil6210_wmi_send(dut, WIL_WMI_DISCOVERY_STOP_CMDID, NULL, 0);
}
static int wil6210_transmit_frame(struct sigma_dut *dut, int freq,
int wait_duration,
const char *frame, size_t frame_len)
{
char buf[128], fname[128];
FILE *f;
int res = 0, r;
size_t written;
if (wil6210_get_debugfs_dir(dut, buf, sizeof(buf))) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"failed to get wil6210 debugfs dir");
return -1;
}
r = snprintf(fname, sizeof(fname), "%s/tx_mgmt", buf);
if (r < 0 || r >= sizeof(fname))
return -1;
if (wil6210_remain_on_channel(dut, freq)) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"failed to listen on channel");
return -1;
}
f = fopen(fname, "wb");
if (!f) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"failed to open: %s", fname);
res = -1;
goto out_stop;
}
written = fwrite(frame, 1, frame_len, f);
fclose(f);
if (written != frame_len) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"failed to transmit frame (got %zd, expected %zd)",
written, frame_len);
res = -1;
goto out_stop;
}
usleep(wait_duration * 1000);
out_stop:
wil6210_stop_discovery(dut);
return res;
}
static int find_template_frame_tag(struct template_frame_tag *tags,
int total_tags, int tag_num)
{
int i;
for (i = 0; i < total_tags; i++) {
if (tag_num == tags[i].num)
return i;
}
return -1;
}
static int replace_p2p_attribute(struct sigma_dut *dut, char *buf, size_t len,
int id, const char *value, size_t val_len)
{
struct wfa_p2p_attribute *attr = (struct wfa_p2p_attribute *) buf;
if (len < 3 + val_len) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"not enough space to replace P2P attribute");
return -1;
}
if (attr->len != val_len) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"attribute length mismatch (need %zu have %hu)",
val_len, attr->len);
return -1;
}
if (attr->id != id) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"incorrect attribute id (expected %d actual %d)",
id, attr->id);
return -1;
}
memcpy(attr->variable, value, val_len);
return 0;
}
static int parse_template_frame_file(struct sigma_dut *dut, const char *fname,
char *buf, size_t *length,
struct template_frame_tag *tags,
size_t *num_tags)
{
char line[512];
FILE *f;
size_t offset = 0, tag_index = 0;
int num, index;
int in_tag = 0, tag_num = 0, tag_offset = 0;
if (*length < sizeof(struct ieee80211_hdr_3addr)) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"supplied buffer is too small");
return -1;
}
f = fopen(fname, "r");
if (!f) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"failed to open template file %s", fname);
return -1;
}
/*
* template file format: lines beginning with # are comments and
* ignored.
* It is possible to tag bytes in the frame to make it easy
* to replace fields in the template, espcially if they appear
* in variable-sized sections (such as IEs)
* This is done by a line beginning with $NUM where NUM is an integer
* tag number. It can be followed by space(s) and comment.
* The next line is considered the tagged bytes. The parser will fill
* the tag number, offset and length of the tagged bytes.
* rest of the lines contain frame bytes as sequence of hex digits,
* 2 digits for each byte. Spaces are allowed between bytes.
* On bytes lines only hex digits and spaces are allowed
*/
while (!feof(f)) {
if (!fgets(line, sizeof(line), f))
break;
index = 0;
while (isspace((unsigned char) line[index]))
index++;
if (!line[index] || line[index] == '#')
continue;
if (line[index] == '$') {
if (tags) {
index++;
tag_num = strtol(&line[index], NULL, 0);
tag_offset = offset;
in_tag = 1;
}
continue;
}
while (line[index]) {
if (isspace((unsigned char) line[index])) {
index++;
continue;
}
num = hex_byte(&line[index]);
if (num < 0)
break;
buf[offset++] = num;
if (offset == *length)
goto out;
index += 2;
}
if (in_tag) {
if (tag_index < *num_tags) {
tags[tag_index].num = tag_num;
tags[tag_index].offset = tag_offset;
tags[tag_index].len = offset - tag_offset;
tag_index++;
} else {
sigma_dut_print(dut, DUT_MSG_INFO,
"too many tags, tag ignored");
}
in_tag = 0;
}
}
if (num_tags)
*num_tags = tag_index;
out:
fclose(f);
if (offset < sizeof(struct ieee80211_hdr_3addr)) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"template frame is too small");
return -1;
}
*length = offset;
return 0;
}
#endif /* __linux__ */
static void static_ip_file(int proto, const char *addr, const char *mask,
const char *gw)
{
if (proto) {
FILE *f = fopen("static-ip", "w");
if (f) {
fprintf(f, "%d %s %s %s\n", proto, addr,
mask ? mask : "N/A",
gw ? gw : "N/A");
fclose(f);
}
} else {
unlink("static-ip");
}
}
static int send_neighbor_request(struct sigma_dut *dut, const char *intf,
const char *ssid)
{
char buf[100];
int ret = 0;
/* Use wpa_supplicant to send neighbor report request */
snprintf(buf, sizeof(buf), "NEIGHBOR_REP_REQUEST ssid=\"%s\"",
ssid);
ret = wpa_command(intf, buf);
if (ret == 0) {
sigma_dut_print(dut, DUT_MSG_INFO,
"Neighbor report request sent through wpa_command");
return 0;
}
#ifdef __linux__
sigma_dut_print(dut, DUT_MSG_INFO,
"wpa_command failed, ret:%d; fall back to iwpriv", ret);
snprintf(buf, sizeof(buf), "iwpriv %s neighbor %s",
intf, ssid);
sigma_dut_print(dut, DUT_MSG_INFO, "Request: %s", buf);
if (system(buf) != 0) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"iwpriv neighbor request failed");
return -1;
}
sigma_dut_print(dut, DUT_MSG_INFO, "iwpriv neighbor request send");
return 0;
#else /* __linux__ */
return -1;
#endif /* __linux__ */
}
static int send_trans_mgmt_query(struct sigma_dut *dut, const char *intf,
struct sigma_cmd *cmd)
{
const char *val;
int reason_code = 0;
char buf[1024];
/*
* In the earlier builds we used WNM_QUERY and in later
* builds used WNM_BSS_QUERY.
*/
val = get_param(cmd, "BTMQuery_Reason_Code");
if (val)
reason_code = atoi(val);
val = get_param(cmd, "Cand_List");
if (val && atoi(val) == 1 && dut->btm_query_cand_list) {
snprintf(buf, sizeof(buf), "WNM_BSS_QUERY %d%s", reason_code,
dut->btm_query_cand_list);
free(dut->btm_query_cand_list);
dut->btm_query_cand_list = NULL;
} else {
snprintf(buf, sizeof(buf), "WNM_BSS_QUERY %d", reason_code);
}
if (wpa_command(intf, buf) != 0) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"transition management query failed");
return -1;
}
sigma_dut_print(dut, DUT_MSG_DEBUG,
"transition management query sent");
return 0;
}
int is_ip_addr(const char *str)
{
const char *pos = str;
struct in_addr addr;
while (*pos) {
if (*pos != '.' && (*pos < '0' || *pos > '9'))
return 0;
pos++;
}
return inet_aton(str, &addr);
}
int get_ip_config(struct sigma_dut *dut, const char *ifname, char *buf,
size_t buf_len)
{
char tmp[256];
char ip[16], mask[15], dns[16], sec_dns[16];
int is_dhcp = 0;
int s;
#ifdef ANDROID
char prop[PROPERTY_VALUE_MAX];
#else /* ANDROID */
FILE *f;
#ifdef __linux__
const char *str_ps;
#endif /* __linux__ */
#endif /* ANDROID */
ip[0] = '\0';
mask[0] = '\0';
dns[0] = '\0';
sec_dns[0] = '\0';
s = socket(PF_INET, SOCK_DGRAM, 0);
if (s >= 0) {
struct ifreq ifr;
struct sockaddr_in saddr;