blob: ac5cfb1c9be6c0c8c6bdd493fccf37db8afbce93 [file] [log] [blame]
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001/*
2 * Sigma Control API DUT (station/AP)
3 * Copyright (c) 2010-2011, Atheros Communications, Inc.
Jouni Malinen9d7e31d2017-12-22 18:55:04 +02004 * Copyright (c) 2011-2017, Qualcomm Atheros, Inc.
Jouni Malinenc12ea4a2018-01-05 21:07:10 +02005 * Copyright (c) 2018, The Linux Foundation
Jouni Malinencd4e3c32015-10-29 12:39:56 +02006 * All Rights Reserved.
7 * Licensed under the Clear BSD license. See README for more details.
8 */
9
10#include "sigma_dut.h"
11#include <sys/ioctl.h>
12#include <sys/stat.h>
13#ifdef __linux__
Lior Davidcc88b562017-01-03 18:52:09 +020014#include <regex.h>
15#include <dirent.h>
Jouni Malinencd4e3c32015-10-29 12:39:56 +020016#include <sys/time.h>
17#include <netpacket/packet.h>
18#include <linux/if_ether.h>
19#ifdef ANDROID
20#include <cutils/properties.h>
21#include <android/log.h>
22#include "keystore_get.h"
23#else /* ANDROID */
24#include <ifaddrs.h>
25#endif /* ANDROID */
26#include <netdb.h>
27#endif /* __linux__ */
28#ifdef __QNXNTO__
29#include <net/if_dl.h>
30#endif /* __QNXNTO__ */
31#include "wpa_ctrl.h"
32#include "wpa_helpers.h"
Amarnath Hullur Subramanyam9c381f52017-03-17 00:04:41 -070033#include "miracast.h"
Jouni Malinencd4e3c32015-10-29 12:39:56 +020034
35/* Temporary files for sta_send_addba */
36#define VI_QOS_TMP_FILE "/tmp/vi-qos.tmp"
37#define VI_QOS_FILE "/tmp/vi-qos.txt"
38#define VI_QOS_REFFILE "/etc/vi-qos.txt"
39
40/*
41 * MTU for Ethernet need to take into account 8-byte SNAP header
42 * to be added when encapsulating Ethernet frame into 802.11
43 */
44#ifndef IEEE80211_MAX_DATA_LEN_DMG
45#define IEEE80211_MAX_DATA_LEN_DMG 7920
46#endif
47#ifndef IEEE80211_SNAP_LEN_DMG
48#define IEEE80211_SNAP_LEN_DMG 8
49#endif
50
Ashwini Patil00402582017-04-13 12:29:39 +053051#define NON_PREF_CH_LIST_SIZE 100
Ashwini Patil5acd7382017-04-13 15:55:04 +053052#define NEIGHBOR_REPORT_SIZE 1000
53#define DEFAULT_NEIGHBOR_BSSID_INFO "17"
54#define DEFAULT_NEIGHBOR_PHY_TYPE "1"
Ashwini Patil00402582017-04-13 12:29:39 +053055
Jouni Malinencd4e3c32015-10-29 12:39:56 +020056extern char *sigma_wpas_ctrl;
57extern char *sigma_cert_path;
58extern enum driver_type wifi_chip_type;
59extern char *sigma_radio_ifname[];
60
Lior David0fe101e2017-03-09 16:09:50 +020061#ifdef __linux__
62#define WIL_WMI_MAX_PAYLOAD 248
63#define WIL_WMI_BF_TRIG_CMDID 0x83a
64
65struct wil_wmi_header {
66 uint8_t mid;
67 uint8_t reserved;
68 uint16_t cmd;
69 uint32_t ts;
70} __attribute__((packed));
71
72enum wil_wmi_bf_trig_type {
73 WIL_WMI_SLS,
74 WIL_WMI_BRP_RX,
75 WIL_WMI_BRP_TX,
76};
77
78struct wil_wmi_bf_trig_cmd {
79 /* enum wil_wmi_bf_trig_type */
80 uint32_t bf_type;
81 /* cid when type == WMI_BRP_RX */
82 uint32_t sta_id;
83 uint32_t reserved;
84 /* mac address when type = WIL_WMI_SLS */
85 uint8_t dest_mac[6];
86} __attribute__((packed));
87#endif /* __linux__ */
Jouni Malinencd4e3c32015-10-29 12:39:56 +020088
89#ifdef ANDROID
90
91static int add_ipv6_rule(struct sigma_dut *dut, const char *ifname);
92
93#define ANDROID_KEYSTORE_GET 'g'
94#define ANDROID_KEYSTORE_GET_PUBKEY 'b'
95
96static int android_keystore_get(char cmd, const char *key, unsigned char *val)
97{
Jouni Malinencd4e3c32015-10-29 12:39:56 +020098 /* Android 4.3 changed keystore design, so need to use keystore_get() */
99#ifndef KEYSTORE_MESSAGE_SIZE
100#define KEYSTORE_MESSAGE_SIZE 65535
101#endif /* KEYSTORE_MESSAGE_SIZE */
102
103 ssize_t len;
104 uint8_t *value = NULL;
105
106 __android_log_print(ANDROID_LOG_DEBUG, "sigma_dut",
107 "keystore command '%c' key '%s' --> keystore_get",
108 cmd, key);
109
110 len = keystore_get(key, strlen(key), &value);
111 if (len < 0) {
112 __android_log_print(ANDROID_LOG_DEBUG, "sigma_dut",
113 "keystore_get() failed");
114 return -1;
115 }
116
117 if (len > KEYSTORE_MESSAGE_SIZE)
118 len = KEYSTORE_MESSAGE_SIZE;
119 memcpy(val, value, len);
120 free(value);
121 return len;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200122}
123#endif /* ANDROID */
124
125
126int set_ps(const char *intf, struct sigma_dut *dut, int enabled)
127{
128#ifdef __linux__
129 char buf[100];
130
131 if (wifi_chip_type == DRIVER_WCN) {
132 if (enabled) {
133 snprintf(buf, sizeof(buf), "iwpriv wlan0 dump 906");
Pradeep Reddy POTTETI625b3702016-09-20 17:09:58 +0530134 if (system(buf) != 0)
135 goto set_power_save;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200136 } else {
137 snprintf(buf, sizeof(buf), "iwpriv wlan0 dump 905");
Pradeep Reddy POTTETI625b3702016-09-20 17:09:58 +0530138 if (system(buf) != 0)
139 goto set_power_save;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200140 snprintf(buf, sizeof(buf), "iwpriv wlan0 dump 912");
Pradeep Reddy POTTETI625b3702016-09-20 17:09:58 +0530141 if (system(buf) != 0)
142 goto set_power_save;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200143 }
144
145 return 0;
146 }
147
Pradeep Reddy POTTETI625b3702016-09-20 17:09:58 +0530148set_power_save:
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200149 snprintf(buf, sizeof(buf), "./iw dev %s set power_save %s",
150 intf, enabled ? "on" : "off");
151 if (system(buf) != 0) {
152 snprintf(buf, sizeof(buf), "iw dev %s set power_save %s",
153 intf, enabled ? "on" : "off");
Pradeep Reddy POTTETI625b3702016-09-20 17:09:58 +0530154 if (system(buf) != 0) {
155 sigma_dut_print(dut, DUT_MSG_ERROR,
156 "Failed to set power save %s",
157 enabled ? "on" : "off");
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200158 return -1;
Pradeep Reddy POTTETI625b3702016-09-20 17:09:58 +0530159 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200160 }
161
162 return 0;
163#else /* __linux__ */
164 return -1;
165#endif /* __linux__ */
166}
167
168
Lior Davidcc88b562017-01-03 18:52:09 +0200169#ifdef __linux__
Lior David0fe101e2017-03-09 16:09:50 +0200170
Lior Davidcc88b562017-01-03 18:52:09 +0200171static int wil6210_get_debugfs_dir(struct sigma_dut *dut, char *path,
172 size_t len)
173{
174 DIR *dir, *wil_dir;
175 struct dirent *entry;
176 int ret = -1;
177 const char *root_path = "/sys/kernel/debug/ieee80211";
178
179 dir = opendir(root_path);
180 if (!dir)
181 return -2;
182
183 while ((entry = readdir(dir))) {
184 if (strcmp(entry->d_name, ".") == 0 ||
185 strcmp(entry->d_name, "..") == 0)
186 continue;
187
188 if (snprintf(path, len, "%s/%s/wil6210",
189 root_path, entry->d_name) >= (int) len) {
190 ret = -3;
191 break;
192 }
193
194 wil_dir = opendir(path);
195 if (wil_dir) {
196 closedir(wil_dir);
197 ret = 0;
198 break;
199 }
200 }
201
202 closedir(dir);
203 return ret;
204}
Lior David0fe101e2017-03-09 16:09:50 +0200205
206
207static int wil6210_wmi_send(struct sigma_dut *dut, uint16_t command,
208 void *payload, uint16_t length)
209{
210 struct {
211 struct wil_wmi_header hdr;
212 char payload[WIL_WMI_MAX_PAYLOAD];
213 } __attribute__((packed)) cmd;
214 char buf[128], fname[128];
215 size_t towrite, written;
216 FILE *f;
217
218 if (length > WIL_WMI_MAX_PAYLOAD) {
219 sigma_dut_print(dut, DUT_MSG_ERROR,
220 "payload too large(%u, max %u)",
221 length, WIL_WMI_MAX_PAYLOAD);
222 return -1;
223 }
224
225 memset(&cmd.hdr, 0, sizeof(cmd.hdr));
226 cmd.hdr.cmd = command;
227 memcpy(cmd.payload, payload, length);
228
229 if (wil6210_get_debugfs_dir(dut, buf, sizeof(buf))) {
230 sigma_dut_print(dut, DUT_MSG_ERROR,
231 "failed to get wil6210 debugfs dir");
232 return -1;
233 }
234
235 snprintf(fname, sizeof(fname), "%s/wmi_send", buf);
236 f = fopen(fname, "wb");
237 if (!f) {
238 sigma_dut_print(dut, DUT_MSG_ERROR,
239 "failed to open: %s", fname);
240 return -1;
241 }
242
243 towrite = sizeof(cmd.hdr) + length;
244 written = fwrite(&cmd, 1, towrite, f);
245 fclose(f);
246 if (written != towrite) {
247 sigma_dut_print(dut, DUT_MSG_ERROR,
248 "failed to send wmi %u", command);
249 return -1;
250 }
251
252 return 0;
253}
254
255
256static int wil6210_get_sta_info_field(struct sigma_dut *dut, const char *bssid,
257 const char *pattern, unsigned int *field)
258{
259 char buf[128], fname[128];
260 FILE *f;
261 regex_t re;
262 regmatch_t m[2];
263 int rc, ret = -1;
264
265 if (wil6210_get_debugfs_dir(dut, buf, sizeof(buf))) {
266 sigma_dut_print(dut, DUT_MSG_ERROR,
267 "failed to get wil6210 debugfs dir");
268 return -1;
269 }
270
271 snprintf(fname, sizeof(fname), "%s/stations", buf);
272 f = fopen(fname, "r");
273 if (!f) {
274 sigma_dut_print(dut, DUT_MSG_ERROR,
275 "failed to open: %s", fname);
276 return -1;
277 }
278
279 if (regcomp(&re, pattern, REG_EXTENDED)) {
280 sigma_dut_print(dut, DUT_MSG_ERROR,
281 "regcomp failed: %s", pattern);
282 goto out;
283 }
284
285 /*
286 * find the entry for the mac address
287 * line is of the form: [n] 11:22:33:44:55:66 state AID aid
288 */
289 while (fgets(buf, sizeof(buf), f)) {
290 if (strcasestr(buf, bssid)) {
291 /* extract the field (CID/AID/state) */
292 rc = regexec(&re, buf, 2, m, 0);
293 if (!rc && (m[1].rm_so >= 0)) {
294 buf[m[1].rm_eo] = 0;
295 *field = atoi(&buf[m[1].rm_so]);
296 ret = 0;
297 break;
298 }
299 }
300 }
301
302 regfree(&re);
303 if (ret)
304 sigma_dut_print(dut, DUT_MSG_ERROR,
305 "could not extract field");
306
307out:
308 fclose(f);
309
310 return ret;
311}
312
313
314static int wil6210_get_cid(struct sigma_dut *dut, const char *bssid,
315 unsigned int *cid)
316{
317 const char *pattern = "\\[([0-9]+)\\]";
318
319 return wil6210_get_sta_info_field(dut, bssid, pattern, cid);
320}
321
322
323static int wil6210_send_brp_rx(struct sigma_dut *dut, const char *mac,
324 int l_rx)
325{
Rakesh Sunki556237d2017-03-30 14:49:31 -0700326 struct wil_wmi_bf_trig_cmd cmd;
Lior David0fe101e2017-03-09 16:09:50 +0200327 unsigned int cid;
328
Rakesh Sunki556237d2017-03-30 14:49:31 -0700329 memset(&cmd, 0, sizeof(cmd));
330
Lior David0fe101e2017-03-09 16:09:50 +0200331 if (wil6210_get_cid(dut, mac, &cid))
332 return -1;
333
334 cmd.bf_type = WIL_WMI_BRP_RX;
335 cmd.sta_id = cid;
336 /* training length (l_rx) is ignored, FW always uses length 16 */
337 return wil6210_wmi_send(dut, WIL_WMI_BF_TRIG_CMDID,
338 &cmd, sizeof(cmd));
339}
340
341
342static int wil6210_send_sls(struct sigma_dut *dut, const char *mac)
343{
Rakesh Sunki556237d2017-03-30 14:49:31 -0700344 struct wil_wmi_bf_trig_cmd cmd;
345
346 memset(&cmd, 0, sizeof(cmd));
Lior David0fe101e2017-03-09 16:09:50 +0200347
348 if (parse_mac_address(dut, mac, (unsigned char *)&cmd.dest_mac))
349 return -1;
350
351 cmd.bf_type = WIL_WMI_SLS;
352 return wil6210_wmi_send(dut, WIL_WMI_BF_TRIG_CMDID,
353 &cmd, sizeof(cmd));
354}
355
Lior Davidcc88b562017-01-03 18:52:09 +0200356#endif /* __linux__ */
357
358
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200359static void static_ip_file(int proto, const char *addr, const char *mask,
360 const char *gw)
361{
362 if (proto) {
363 FILE *f = fopen("static-ip", "w");
364 if (f) {
365 fprintf(f, "%d %s %s %s\n", proto, addr,
366 mask ? mask : "N/A",
367 gw ? gw : "N/A");
368 fclose(f);
369 }
370 } else {
371 unlink("static-ip");
372 }
373}
374
375
376static int send_neighbor_request(struct sigma_dut *dut, const char *intf,
377 const char *ssid)
378{
379#ifdef __linux__
380 char buf[100];
381
382 snprintf(buf, sizeof(buf), "iwpriv %s neighbor %s",
383 intf, ssid);
384 sigma_dut_print(dut, DUT_MSG_INFO, "Request: %s", buf);
385
386 if (system(buf) != 0) {
387 sigma_dut_print(dut, DUT_MSG_ERROR,
388 "iwpriv neighbor request failed");
389 return -1;
390 }
391
392 sigma_dut_print(dut, DUT_MSG_INFO, "iwpriv neighbor request send");
393
394 return 0;
395#else /* __linux__ */
396 return -1;
397#endif /* __linux__ */
398}
399
400
401static int send_trans_mgmt_query(struct sigma_dut *dut, const char *intf,
Ashwini Patil5acd7382017-04-13 15:55:04 +0530402 struct sigma_cmd *cmd)
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200403{
Ashwini Patil5acd7382017-04-13 15:55:04 +0530404 const char *val;
405 int reason_code = 0;
406 char buf[1024];
407
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200408 /*
409 * In the earlier builds we used WNM_QUERY and in later
410 * builds used WNM_BSS_QUERY.
411 */
412
Ashwini Patil5acd7382017-04-13 15:55:04 +0530413 val = get_param(cmd, "BTMQuery_Reason_Code");
414 if (val)
415 reason_code = atoi(val);
416
417 val = get_param(cmd, "Cand_List");
418 if (val && atoi(val) == 1 && dut->btm_query_cand_list) {
419 snprintf(buf, sizeof(buf), "WNM_BSS_QUERY %d%s", reason_code,
420 dut->btm_query_cand_list);
421 free(dut->btm_query_cand_list);
422 dut->btm_query_cand_list = NULL;
423 } else {
424 snprintf(buf, sizeof(buf), "WNM_BSS_QUERY %d", reason_code);
425 }
426
427 if (wpa_command(intf, buf) != 0) {
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200428 sigma_dut_print(dut, DUT_MSG_ERROR,
429 "transition management query failed");
430 return -1;
431 }
432
433 sigma_dut_print(dut, DUT_MSG_DEBUG,
434 "transition management query sent");
435
436 return 0;
437}
438
439
440int is_ip_addr(const char *str)
441{
442 const char *pos = str;
443 struct in_addr addr;
444
445 while (*pos) {
446 if (*pos != '.' && (*pos < '0' || *pos > '9'))
447 return 0;
448 pos++;
449 }
450
451 return inet_aton(str, &addr);
452}
453
454
455int is_ipv6_addr(const char *str)
456{
457 struct sockaddr_in6 addr;
458
459 return inet_pton(AF_INET6, str, &(addr.sin6_addr));
460}
461
462
463int get_ip_config(struct sigma_dut *dut, const char *ifname, char *buf,
464 size_t buf_len)
465{
466 char tmp[256], *pos, *pos2;
467 FILE *f;
468 char ip[16], mask[15], dns[16], sec_dns[16];
Sarvepalli, Rajesh Babua76c6442016-03-18 20:34:26 +0530469 const char *str_ps;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200470 int is_dhcp = 0;
471 int s;
472#ifdef ANDROID
473 char prop[PROPERTY_VALUE_MAX];
474#endif /* ANDROID */
475
476 ip[0] = '\0';
477 mask[0] = '\0';
478 dns[0] = '\0';
479 sec_dns[0] = '\0';
480
481 s = socket(PF_INET, SOCK_DGRAM, 0);
482 if (s >= 0) {
483 struct ifreq ifr;
484 struct sockaddr_in saddr;
485
486 memset(&ifr, 0, sizeof(ifr));
Peng Xub8fc5cc2017-05-10 17:27:28 -0700487 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200488 if (ioctl(s, SIOCGIFADDR, &ifr) < 0) {
489 sigma_dut_print(dut, DUT_MSG_INFO, "Failed to get "
490 "%s IP address: %s",
491 ifname, strerror(errno));
492 } else {
493 memcpy(&saddr, &ifr.ifr_addr,
494 sizeof(struct sockaddr_in));
Peng Xub8fc5cc2017-05-10 17:27:28 -0700495 strlcpy(ip, inet_ntoa(saddr.sin_addr), sizeof(ip));
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200496 }
497
498 if (ioctl(s, SIOCGIFNETMASK, &ifr) == 0) {
499 memcpy(&saddr, &ifr.ifr_addr,
500 sizeof(struct sockaddr_in));
Peng Xub8fc5cc2017-05-10 17:27:28 -0700501 strlcpy(mask, inet_ntoa(saddr.sin_addr), sizeof(mask));
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200502 }
503 close(s);
504 }
505
506#ifdef ANDROID
507 snprintf(tmp, sizeof(tmp), "dhcp.%s.pid", ifname);
508 if (property_get(tmp, prop, NULL) != 0 && atoi(prop) > 0) {
509 snprintf(tmp, sizeof(tmp), "dhcp.%s.result", ifname);
510 if (property_get(tmp, prop, NULL) != 0 &&
511 strcmp(prop, "ok") == 0) {
512 snprintf(tmp, sizeof(tmp), "dhcp.%s.ipaddress",
513 ifname);
514 if (property_get(tmp, prop, NULL) != 0 &&
515 strcmp(ip, prop) == 0)
516 is_dhcp = 1;
517 }
518 }
519
520 snprintf(tmp, sizeof(tmp), "dhcp.%s.dns1", ifname);
Peng Xub8fc5cc2017-05-10 17:27:28 -0700521 if (property_get(tmp, prop, NULL) != 0)
522 strlcpy(dns, prop, sizeof(dns));
523 else if (property_get("net.dns1", prop, NULL) != 0)
524 strlcpy(dns, prop, sizeof(dns));
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200525
526 snprintf(tmp, sizeof(tmp), "dhcp.%s.dns2", ifname);
Peng Xub8fc5cc2017-05-10 17:27:28 -0700527 if (property_get(tmp, prop, NULL) != 0)
528 strlcpy(sec_dns, prop, sizeof(sec_dns));
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200529#else /* ANDROID */
530#ifdef __linux__
Sarvepalli, Rajesh Babua76c6442016-03-18 20:34:26 +0530531 if (get_driver_type() == DRIVER_OPENWRT)
532 str_ps = "ps -w";
533 else
534 str_ps = "ps ax";
535 snprintf(tmp, sizeof(tmp),
536 "%s | grep dhclient | grep -v grep | grep -q %s",
537 str_ps, ifname);
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200538 if (system(tmp) == 0)
539 is_dhcp = 1;
540 else {
Sarvepalli, Rajesh Babua76c6442016-03-18 20:34:26 +0530541 snprintf(tmp, sizeof(tmp),
542 "%s | grep udhcpc | grep -v grep | grep -q %s",
543 str_ps, ifname);
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200544 if (system(tmp) == 0)
545 is_dhcp = 1;
546 else {
Sarvepalli, Rajesh Babua76c6442016-03-18 20:34:26 +0530547 snprintf(tmp, sizeof(tmp),
548 "%s | grep dhcpcd | grep -v grep | grep -q %s",
549 str_ps, ifname);
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200550 if (system(tmp) == 0)
551 is_dhcp = 1;
552 }
553 }
554#endif /* __linux__ */
555
556 f = fopen("/etc/resolv.conf", "r");
557 if (f) {
558 while (fgets(tmp, sizeof(tmp), f)) {
559 if (strncmp(tmp, "nameserver", 10) != 0)
560 continue;
561 pos = tmp + 10;
562 while (*pos == ' ' || *pos == '\t')
563 pos++;
564 pos2 = pos;
565 while (*pos2) {
566 if (*pos2 == '\n' || *pos2 == '\r') {
567 *pos2 = '\0';
568 break;
569 }
570 pos2++;
571 }
Peng Xub8fc5cc2017-05-10 17:27:28 -0700572 if (!dns[0])
573 strlcpy(dns, pos, sizeof(dns));
574 else if (!sec_dns[0])
575 strlcpy(sec_dns, pos, sizeof(sec_dns));
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200576 }
577 fclose(f);
578 }
579#endif /* ANDROID */
580
581 snprintf(buf, buf_len, "dhcp,%d,ip,%s,mask,%s,primary-dns,%s",
582 is_dhcp, ip, mask, dns);
583 buf[buf_len - 1] = '\0';
584
585 return 0;
586}
587
588
589
590
591int get_ipv6_config(struct sigma_dut *dut, const char *ifname, char *buf,
592 size_t buf_len)
593{
594#ifdef __linux__
595#ifdef ANDROID
596 char cmd[200], result[1000], *pos, *end;
597 FILE *f;
598 size_t len;
599
600 snprintf(cmd, sizeof(cmd), "ip addr show dev %s scope global", ifname);
601 f = popen(cmd, "r");
602 if (f == NULL)
603 return -1;
604 len = fread(result, 1, sizeof(result) - 1, f);
605 pclose(f);
606 if (len == 0)
607 return -1;
608 result[len] = '\0';
609 sigma_dut_print(dut, DUT_MSG_DEBUG, "%s result: %s\n", cmd, result);
610
611 pos = strstr(result, "inet6 ");
612 if (pos == NULL)
613 return -1;
614 pos += 6;
615 end = strchr(pos, ' ');
616 if (end)
617 *end = '\0';
618 end = strchr(pos, '/');
619 if (end)
620 *end = '\0';
621 snprintf(buf, buf_len, "ip,%s", pos);
622 buf[buf_len - 1] = '\0';
623 return 0;
624#else /* ANDROID */
625 struct ifaddrs *ifaddr, *ifa;
626 int res, found = 0;
627 char host[NI_MAXHOST];
628
629 if (getifaddrs(&ifaddr) < 0) {
630 perror("getifaddrs");
631 return -1;
632 }
633
634 for (ifa = ifaddr; ifa; ifa = ifa->ifa_next) {
635 if (strcasecmp(ifname, ifa->ifa_name) != 0)
636 continue;
637 if (ifa->ifa_addr == NULL ||
638 ifa->ifa_addr->sa_family != AF_INET6)
639 continue;
640
641 res = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in6),
642 host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
643 if (res != 0) {
644 sigma_dut_print(dut, DUT_MSG_DEBUG, "getnameinfo: %s",
645 gai_strerror(res));
646 continue;
647 }
648 if (strncmp(host, "fe80::", 6) == 0)
649 continue; /* skip link-local */
650
651 sigma_dut_print(dut, DUT_MSG_DEBUG, "ifaddr: %s", host);
652 found = 1;
653 break;
654 }
655
656 freeifaddrs(ifaddr);
657
658 if (found) {
659 char *pos;
660 pos = strchr(host, '%');
661 if (pos)
662 *pos = '\0';
663 snprintf(buf, buf_len, "ip,%s", host);
664 buf[buf_len - 1] = '\0';
665 return 0;
666 }
667
668#endif /* ANDROID */
669#endif /* __linux__ */
670 return -1;
671}
672
673
674static int cmd_sta_get_ip_config(struct sigma_dut *dut,
675 struct sigma_conn *conn,
676 struct sigma_cmd *cmd)
677{
678 const char *intf = get_param(cmd, "Interface");
679 const char *ifname;
680 char buf[200];
681 const char *val;
682 int type = 1;
683
684 if (intf == NULL)
685 return -1;
686
687 if (strcmp(intf, get_main_ifname()) == 0)
688 ifname = get_station_ifname();
689 else
690 ifname = intf;
691
692 /*
693 * UCC may assume the IP address to be available immediately after
694 * association without trying to run sta_get_ip_config multiple times.
695 * Sigma CAPI does not specify this command as a block command that
696 * would wait for the address to become available, but to pass tests
697 * more reliably, it looks like such a wait may be needed here.
698 */
699 if (wait_ip_addr(dut, ifname, 15) < 0) {
700 sigma_dut_print(dut, DUT_MSG_INFO, "Could not get IP address "
701 "for sta_get_ip_config");
702 /*
703 * Try to continue anyway since many UCC tests do not really
704 * care about the return value from here..
705 */
706 }
707
708 val = get_param(cmd, "Type");
709 if (val)
710 type = atoi(val);
711 if (type == 2 || dut->last_set_ip_config_ipv6) {
712 int i;
713
714 /*
715 * Since we do not have proper wait for IPv6 addresses, use a
716 * fixed two second delay here as a workaround for UCC script
717 * assuming IPv6 address is available when this command returns.
718 * Some scripts did not use Type,2 properly for IPv6, so include
719 * also the cases where the previous sta_set_ip_config indicated
720 * use of IPv6.
721 */
722 sigma_dut_print(dut, DUT_MSG_INFO, "Wait up to extra ten seconds in sta_get_ip_config for IPv6 address");
723 for (i = 0; i < 10; i++) {
724 sleep(1);
725 if (get_ipv6_config(dut, ifname, buf, sizeof(buf)) == 0)
726 {
727 sigma_dut_print(dut, DUT_MSG_INFO, "Found IPv6 address");
728 send_resp(dut, conn, SIGMA_COMPLETE, buf);
729#ifdef ANDROID
730 sigma_dut_print(dut, DUT_MSG_INFO,
731 "Adding IPv6 rule on Android");
732 add_ipv6_rule(dut, intf);
733#endif /* ANDROID */
734
735 return 0;
736 }
737 }
738 }
739 if (type == 1) {
740 if (get_ip_config(dut, ifname, buf, sizeof(buf)) < 0)
741 return -2;
742 } else if (type == 2) {
743 if (get_ipv6_config(dut, ifname, buf, sizeof(buf)) < 0)
744 return -2;
745 } else {
746 send_resp(dut, conn, SIGMA_ERROR,
747 "errorCode,Unsupported address type");
748 return 0;
749 }
750
751 send_resp(dut, conn, SIGMA_COMPLETE, buf);
752 return 0;
753}
754
755
756static void kill_dhcp_client(struct sigma_dut *dut, const char *ifname)
757{
758#ifdef __linux__
759 char buf[200];
760 char path[128];
761 struct stat s;
762
763#ifdef ANDROID
764 snprintf(path, sizeof(path), "/data/misc/dhcp/dhcpcd-%s.pid", ifname);
765#else /* ANDROID */
766 snprintf(path, sizeof(path), "/var/run/dhclient-%s.pid", ifname);
767#endif /* ANDROID */
768 if (stat(path, &s) == 0) {
769 snprintf(buf, sizeof(buf), "kill `cat %s`", path);
770 sigma_dut_print(dut, DUT_MSG_INFO,
771 "Kill previous DHCP client: %s", buf);
772 if (system(buf) != 0)
773 sigma_dut_print(dut, DUT_MSG_INFO,
774 "Failed to kill DHCP client");
775 unlink(path);
776 sleep(1);
777 } else {
778 snprintf(path, sizeof(path), "/var/run/dhcpcd-%s.pid", ifname);
779
780 if (stat(path, &s) == 0) {
781 snprintf(buf, sizeof(buf), "kill `cat %s`", path);
782 sigma_dut_print(dut, DUT_MSG_INFO,
783 "Kill previous DHCP client: %s", buf);
784 if (system(buf) != 0)
785 sigma_dut_print(dut, DUT_MSG_INFO,
786 "Failed to kill DHCP client");
787 unlink(path);
788 sleep(1);
789 }
790 }
791#endif /* __linux__ */
792}
793
794
795static int start_dhcp_client(struct sigma_dut *dut, const char *ifname)
796{
797#ifdef __linux__
798 char buf[200];
799
800#ifdef ANDROID
Purushottam Kushwaha46d64262016-08-23 17:57:53 +0530801 if (access("/system/bin/dhcpcd", F_OK) != -1) {
802 snprintf(buf, sizeof(buf),
803 "/system/bin/dhcpcd -b %s", ifname);
804 } else if (access("/system/bin/dhcptool", F_OK) != -1) {
805 snprintf(buf, sizeof(buf), "/system/bin/dhcptool %s &", ifname);
806 } else {
807 sigma_dut_print(dut, DUT_MSG_ERROR,
808 "DHCP client program missing");
809 return 0;
810 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200811#else /* ANDROID */
812 snprintf(buf, sizeof(buf),
813 "dhclient -nw -pf /var/run/dhclient-%s.pid %s",
814 ifname, ifname);
815#endif /* ANDROID */
816 sigma_dut_print(dut, DUT_MSG_INFO, "Start DHCP client: %s", buf);
817 if (system(buf) != 0) {
818 snprintf(buf, sizeof(buf), "dhcpcd -t 0 %s &", ifname);
819 if (system(buf) != 0) {
820 sigma_dut_print(dut, DUT_MSG_INFO,
821 "Failed to start DHCP client");
822#ifndef ANDROID
823 return -1;
824#endif /* ANDROID */
825 }
826 }
827#endif /* __linux__ */
828
829 return 0;
830}
831
832
833static int clear_ip_addr(struct sigma_dut *dut, const char *ifname)
834{
835#ifdef __linux__
836 char buf[200];
837
838 snprintf(buf, sizeof(buf), "ip addr flush dev %s", ifname);
839 if (system(buf) != 0) {
840 sigma_dut_print(dut, DUT_MSG_INFO,
841 "Failed to clear IP addresses");
842 return -1;
843 }
844#endif /* __linux__ */
845
846 return 0;
847}
848
849
850#ifdef ANDROID
851static int add_ipv6_rule(struct sigma_dut *dut, const char *ifname)
852{
853 char cmd[200], *result, *pos;
854 FILE *fp;
Pradeep Reddy POTTETIf58a1fe2016-10-13 17:22:03 +0530855 int tableid;
856 size_t len, result_len = 1000;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200857
858 snprintf(cmd, sizeof(cmd), "ip -6 route list table all | grep %s",
859 ifname);
860 fp = popen(cmd, "r");
861 if (fp == NULL)
862 return -1;
863
864 result = malloc(result_len);
Pradeep Reddy POTTETI673d85c2016-07-26 19:08:07 +0530865 if (result == NULL) {
866 fclose(fp);
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200867 return -1;
Pradeep Reddy POTTETI673d85c2016-07-26 19:08:07 +0530868 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200869
Pradeep Reddy POTTETIf58a1fe2016-10-13 17:22:03 +0530870 len = fread(result, 1, result_len - 1, fp);
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200871 fclose(fp);
872
873 if (len == 0) {
874 free(result);
875 return -1;
876 }
Pradeep Reddy POTTETIf58a1fe2016-10-13 17:22:03 +0530877 result[len] = '\0';
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200878
879 pos = strstr(result, "table ");
880 if (pos == NULL) {
881 free(result);
882 return -1;
883 }
884
885 pos += strlen("table ");
886 tableid = atoi(pos);
887 if (tableid != 0) {
888 if (system("ip -6 rule del prio 22000") != 0) {
889 /* ignore any error */
890 }
891 snprintf(cmd, sizeof(cmd),
892 "ip -6 rule add from all lookup %d prio 22000",
893 tableid);
894 if (system(cmd) != 0) {
895 sigma_dut_print(dut, DUT_MSG_INFO,
896 "Failed to run %s", cmd);
897 free(result);
898 return -1;
899 }
900 } else {
901 sigma_dut_print(dut, DUT_MSG_INFO,
902 "No Valid Table Id found %s", pos);
903 free(result);
904 return -1;
905 }
906 free(result);
907
908 return 0;
909}
910#endif /* ANDROID */
911
912
Ankita Bajaj1bde7942018-01-09 19:15:01 +0530913int set_ipv4_addr(struct sigma_dut *dut, const char *ifname,
914 const char *ip, const char *mask)
915{
916 char buf[200];
917
918 snprintf(buf, sizeof(buf), "ifconfig %s %s netmask %s",
919 ifname, ip, mask);
920 return system(buf) == 0;
921}
922
923
924int set_ipv4_gw(struct sigma_dut *dut, const char *gw)
925{
926 char buf[200];
927
928 if (!is_ip_addr(gw)) {
929 sigma_dut_print(dut, DUT_MSG_DEBUG, "Invalid gw addr - %s", gw);
930 return -1;
931 }
932
933 snprintf(buf, sizeof(buf), "route add default gw %s", gw);
934 if (!dut->no_ip_addr_set && system(buf) != 0) {
935 snprintf(buf, sizeof(buf), "ip ro re default via %s",
936 gw);
937 if (system(buf) != 0)
938 return 0;
939 }
940
941 return 1;
942}
943
944
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200945static int cmd_sta_set_ip_config(struct sigma_dut *dut,
946 struct sigma_conn *conn,
947 struct sigma_cmd *cmd)
948{
949 const char *intf = get_param(cmd, "Interface");
950 const char *ifname;
951 char buf[200];
952 const char *val, *ip, *mask, *gw;
953 int type = 1;
954
955 if (intf == NULL)
956 return -1;
957
958 if (strcmp(intf, get_main_ifname()) == 0)
959 ifname = get_station_ifname();
960 else
961 ifname = intf;
962
963 if (if_nametoindex(ifname) == 0) {
964 send_resp(dut, conn, SIGMA_ERROR,
965 "ErrorCode,Unknown interface");
966 return 0;
967 }
968
969 val = get_param(cmd, "Type");
970 if (val) {
971 type = atoi(val);
Ankita Bajaj1bde7942018-01-09 19:15:01 +0530972 if (type < 1 || type > 3) {
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200973 send_resp(dut, conn, SIGMA_ERROR,
974 "ErrorCode,Unsupported address type");
975 return 0;
976 }
977 }
978
979 dut->last_set_ip_config_ipv6 = 0;
980
981 val = get_param(cmd, "dhcp");
982 if (val && (strcmp(val, "1") == 0 || strcasecmp(val, "true") == 0)) {
983 static_ip_file(0, NULL, NULL, NULL);
984#ifdef __linux__
985 if (type == 2) {
986 dut->last_set_ip_config_ipv6 = 1;
987 sigma_dut_print(dut, DUT_MSG_INFO, "Using IPv6 "
988 "stateless address autoconfiguration");
989#ifdef ANDROID
990 /*
991 * This sleep is required as the assignment in case of
992 * Android is taking time and is done by the kernel.
993 * The subsequent ping for IPv6 is impacting HS20 test
994 * case.
995 */
996 sleep(2);
997 add_ipv6_rule(dut, intf);
998#endif /* ANDROID */
999 /* Assume this happens by default */
1000 return 1;
1001 }
Ankita Bajaj1bde7942018-01-09 19:15:01 +05301002 if (type != 3) {
1003 kill_dhcp_client(dut, ifname);
1004 if (start_dhcp_client(dut, ifname) < 0)
1005 return -2;
1006 } else {
1007 sigma_dut_print(dut, DUT_MSG_DEBUG,
1008 "Using FILS HLP DHCPv4 Rapid Commit");
1009 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001010
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001011 return 1;
1012#endif /* __linux__ */
1013 return -2;
1014 }
1015
1016 ip = get_param(cmd, "ip");
Pradeep Reddy POTTETIb18c5652016-01-18 12:45:37 +05301017 if (!ip) {
1018 send_resp(dut, conn, SIGMA_INVALID,
1019 "ErrorCode,Missing IP address");
1020 return 0;
1021 }
1022
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001023 mask = get_param(cmd, "mask");
Pradeep Reddy POTTETIb18c5652016-01-18 12:45:37 +05301024 if (!mask) {
1025 send_resp(dut, conn, SIGMA_INVALID,
1026 "ErrorCode,Missing subnet mask");
1027 return 0;
1028 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001029
1030 if (type == 2) {
1031 int net = atoi(mask);
1032
1033 if ((net < 0 && net > 64) || !is_ipv6_addr(ip))
1034 return -1;
1035
1036 if (dut->no_ip_addr_set) {
1037 snprintf(buf, sizeof(buf),
1038 "sysctl net.ipv6.conf.%s.disable_ipv6=1",
1039 ifname);
1040 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
1041 if (system(buf) != 0) {
1042 sigma_dut_print(dut, DUT_MSG_DEBUG,
1043 "Failed to disable IPv6 address before association");
1044 }
1045 } else {
1046 snprintf(buf, sizeof(buf),
1047 "ip -6 addr del %s/%s dev %s",
1048 ip, mask, ifname);
1049 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
1050 if (system(buf) != 0) {
1051 /*
1052 * This command may fail if the address being
1053 * deleted does not exist. Inaction here is
1054 * intentional.
1055 */
1056 }
1057
1058 snprintf(buf, sizeof(buf),
1059 "ip -6 addr add %s/%s dev %s",
1060 ip, mask, ifname);
1061 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
1062 if (system(buf) != 0) {
1063 send_resp(dut, conn, SIGMA_ERROR,
1064 "ErrorCode,Failed to set IPv6 address");
1065 return 0;
1066 }
1067 }
1068
1069 dut->last_set_ip_config_ipv6 = 1;
1070 static_ip_file(6, ip, mask, NULL);
1071 return 1;
1072 } else if (type == 1) {
Pradeep Reddy POTTETIb18c5652016-01-18 12:45:37 +05301073 if (!is_ip_addr(ip) || !is_ip_addr(mask))
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001074 return -1;
1075 }
1076
1077 kill_dhcp_client(dut, ifname);
1078
1079 if (!dut->no_ip_addr_set) {
Ankita Bajaj1bde7942018-01-09 19:15:01 +05301080 if (!set_ipv4_addr(dut, ifname, ip, mask)) {
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001081 send_resp(dut, conn, SIGMA_ERROR,
1082 "ErrorCode,Failed to set IP address");
1083 return 0;
1084 }
1085 }
1086
1087 gw = get_param(cmd, "defaultGateway");
1088 if (gw) {
Ankita Bajaj1bde7942018-01-09 19:15:01 +05301089 if (set_ipv4_gw(dut, gw) < 1) {
1090 send_resp(dut, conn, SIGMA_ERROR,
1091 "ErrorCode,Failed to set default gateway");
1092 return 0;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001093 }
1094 }
1095
1096 val = get_param(cmd, "primary-dns");
1097 if (val) {
1098 /* TODO */
1099 sigma_dut_print(dut, DUT_MSG_INFO, "Ignored primary-dns %s "
1100 "setting", val);
1101 }
1102
1103 val = get_param(cmd, "secondary-dns");
1104 if (val) {
1105 /* TODO */
1106 sigma_dut_print(dut, DUT_MSG_INFO, "Ignored secondary-dns %s "
1107 "setting", val);
1108 }
1109
1110 static_ip_file(4, ip, mask, gw);
1111
1112 return 1;
1113}
1114
1115
1116static int cmd_sta_get_info(struct sigma_dut *dut, struct sigma_conn *conn,
1117 struct sigma_cmd *cmd)
1118{
1119 /* const char *intf = get_param(cmd, "Interface"); */
1120 /* TODO: could report more details here */
1121 send_resp(dut, conn, SIGMA_COMPLETE, "vendor,Atheros");
1122 return 0;
1123}
1124
1125
1126static int cmd_sta_get_mac_address(struct sigma_dut *dut,
1127 struct sigma_conn *conn,
1128 struct sigma_cmd *cmd)
1129{
1130 /* const char *intf = get_param(cmd, "Interface"); */
1131 char addr[20], resp[50];
1132
Ankita Bajaj0d5825b2017-10-25 16:20:17 +05301133 if (dut->dev_role == DEVROLE_STA_CFON)
1134 return sta_cfon_get_mac_address(dut, conn, cmd);
1135
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001136 if (get_wpa_status(get_station_ifname(), "address", addr, sizeof(addr))
1137 < 0)
1138 return -2;
1139
1140 snprintf(resp, sizeof(resp), "mac,%s", addr);
1141 send_resp(dut, conn, SIGMA_COMPLETE, resp);
1142 return 0;
1143}
1144
1145
1146static int cmd_sta_is_connected(struct sigma_dut *dut, struct sigma_conn *conn,
1147 struct sigma_cmd *cmd)
1148{
1149 /* const char *intf = get_param(cmd, "Interface"); */
1150 int connected = 0;
1151 char result[32];
1152 if (get_wpa_status(get_station_ifname(), "wpa_state", result,
1153 sizeof(result)) < 0) {
1154 sigma_dut_print(dut, DUT_MSG_INFO, "Could not get interface "
1155 "%s status", get_station_ifname());
1156 return -2;
1157 }
1158
1159 sigma_dut_print(dut, DUT_MSG_DEBUG, "wpa_state=%s", result);
1160 if (strncmp(result, "COMPLETED", 9) == 0)
1161 connected = 1;
1162
1163 if (connected)
1164 send_resp(dut, conn, SIGMA_COMPLETE, "connected,1");
1165 else
1166 send_resp(dut, conn, SIGMA_COMPLETE, "connected,0");
1167
1168 return 0;
1169}
1170
1171
1172static int cmd_sta_verify_ip_connection(struct sigma_dut *dut,
1173 struct sigma_conn *conn,
1174 struct sigma_cmd *cmd)
1175{
1176 /* const char *intf = get_param(cmd, "Interface"); */
1177 const char *dst, *timeout;
1178 int wait_time = 90;
1179 char buf[100];
1180 int res;
1181
1182 dst = get_param(cmd, "destination");
1183 if (dst == NULL || !is_ip_addr(dst))
1184 return -1;
1185
1186 timeout = get_param(cmd, "timeout");
1187 if (timeout) {
1188 wait_time = atoi(timeout);
1189 if (wait_time < 1)
1190 wait_time = 1;
1191 }
1192
1193 /* TODO: force renewal of IP lease if DHCP is enabled */
1194
1195 snprintf(buf, sizeof(buf), "ping %s -c 3 -W %d", dst, wait_time);
1196 res = system(buf);
1197 sigma_dut_print(dut, DUT_MSG_DEBUG, "ping returned: %d", res);
1198 if (res == 0)
1199 send_resp(dut, conn, SIGMA_COMPLETE, "connected,1");
1200 else if (res == 256)
1201 send_resp(dut, conn, SIGMA_COMPLETE, "connected,0");
1202 else
1203 return -2;
1204
1205 return 0;
1206}
1207
1208
1209static int cmd_sta_get_bssid(struct sigma_dut *dut, struct sigma_conn *conn,
1210 struct sigma_cmd *cmd)
1211{
1212 /* const char *intf = get_param(cmd, "Interface"); */
1213 char bssid[20], resp[50];
1214
1215 if (get_wpa_status(get_station_ifname(), "bssid", bssid, sizeof(bssid))
1216 < 0)
Peng Xub8fc5cc2017-05-10 17:27:28 -07001217 strlcpy(bssid, "00:00:00:00:00:00", sizeof(bssid));
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001218
1219 snprintf(resp, sizeof(resp), "bssid,%s", bssid);
1220 send_resp(dut, conn, SIGMA_COMPLETE, resp);
1221 return 0;
1222}
1223
1224
1225#ifdef __SAMSUNG__
1226static int add_use_network(const char *ifname)
1227{
1228 char buf[100];
1229
1230 snprintf(buf, sizeof(buf), "USE_NETWORK ON");
1231 wpa_command(ifname, buf);
1232 return 0;
1233}
1234#endif /* __SAMSUNG__ */
1235
1236
1237static int add_network_common(struct sigma_dut *dut, struct sigma_conn *conn,
1238 const char *ifname, struct sigma_cmd *cmd)
1239{
1240 const char *ssid = get_param(cmd, "ssid");
1241 int id;
1242 const char *val;
1243
1244 if (ssid == NULL)
1245 return -1;
1246
1247 start_sta_mode(dut);
1248
1249#ifdef __SAMSUNG__
1250 add_use_network(ifname);
1251#endif /* __SAMSUNG__ */
1252
1253 id = add_network(ifname);
1254 if (id < 0)
1255 return -2;
1256 sigma_dut_print(dut, DUT_MSG_DEBUG, "Adding network %d", id);
1257
1258 if (set_network_quoted(ifname, id, "ssid", ssid) < 0)
1259 return -2;
1260
1261 dut->infra_network_id = id;
1262 snprintf(dut->infra_ssid, sizeof(dut->infra_ssid), "%s", ssid);
1263
1264 val = get_param(cmd, "program");
1265 if (!val)
1266 val = get_param(cmd, "prog");
1267 if (val && strcasecmp(val, "hs2") == 0) {
1268 char buf[100];
1269 snprintf(buf, sizeof(buf), "ENABLE_NETWORK %d no-connect", id);
1270 wpa_command(ifname, buf);
1271
1272 val = get_param(cmd, "prefer");
1273 if (val && atoi(val) > 0)
1274 set_network(ifname, id, "priority", "1");
1275 }
1276
1277 return id;
1278}
1279
1280
1281static int cmd_sta_set_encryption(struct sigma_dut *dut,
1282 struct sigma_conn *conn,
1283 struct sigma_cmd *cmd)
1284{
1285 const char *intf = get_param(cmd, "Interface");
1286 const char *ssid = get_param(cmd, "ssid");
1287 const char *type = get_param(cmd, "encpType");
1288 const char *ifname;
1289 char buf[200];
1290 int id;
1291
1292 if (intf == NULL || ssid == NULL)
1293 return -1;
1294
1295 if (strcmp(intf, get_main_ifname()) == 0)
1296 ifname = get_station_ifname();
1297 else
1298 ifname = intf;
1299
1300 id = add_network_common(dut, conn, ifname, cmd);
1301 if (id < 0)
1302 return id;
1303
1304 if (set_network(ifname, id, "key_mgmt", "NONE") < 0)
1305 return -2;
1306
1307 if (type && strcasecmp(type, "wep") == 0) {
1308 const char *val;
1309 int i;
1310
1311 val = get_param(cmd, "activeKey");
1312 if (val) {
1313 int keyid;
1314 keyid = atoi(val);
1315 if (keyid < 1 || keyid > 4)
1316 return -1;
1317 snprintf(buf, sizeof(buf), "%d", keyid - 1);
1318 if (set_network(ifname, id, "wep_tx_keyidx", buf) < 0)
1319 return -2;
1320 }
1321
1322 for (i = 0; i < 4; i++) {
1323 snprintf(buf, sizeof(buf), "key%d", i + 1);
1324 val = get_param(cmd, buf);
1325 if (val == NULL)
1326 continue;
1327 snprintf(buf, sizeof(buf), "wep_key%d", i);
1328 if (set_network(ifname, id, buf, val) < 0)
1329 return -2;
1330 }
1331 }
1332
1333 return 1;
1334}
1335
1336
1337static int set_wpa_common(struct sigma_dut *dut, struct sigma_conn *conn,
1338 const char *ifname, struct sigma_cmd *cmd)
1339{
1340 const char *val;
1341 int id;
Jouni Malinenad395a22017-09-01 21:13:46 +03001342 int cipher_set = 0;
Jouni Malinen47dcc952017-10-09 16:43:24 +03001343 int owe;
Sunil Duttc75a1e62018-01-11 20:47:50 +05301344 int suite_b = 0;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001345
1346 id = add_network_common(dut, conn, ifname, cmd);
1347 if (id < 0)
1348 return id;
1349
Jouni Malinen47dcc952017-10-09 16:43:24 +03001350 val = get_param(cmd, "Type");
1351 owe = val && strcasecmp(val, "OWE") == 0;
1352
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001353 val = get_param(cmd, "keyMgmtType");
Jouni Malinen47dcc952017-10-09 16:43:24 +03001354 if (!val && owe)
1355 val = "OWE";
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001356 if (val == NULL) {
1357 send_resp(dut, conn, SIGMA_INVALID, "errorCode,Missing keyMgmtType");
1358 return 0;
1359 }
1360 if (strcasecmp(val, "wpa") == 0 ||
1361 strcasecmp(val, "wpa-psk") == 0) {
1362 if (set_network(ifname, id, "proto", "WPA") < 0)
1363 return -2;
1364 } else if (strcasecmp(val, "wpa2") == 0 ||
1365 strcasecmp(val, "wpa2-psk") == 0 ||
1366 strcasecmp(val, "wpa2-ft") == 0 ||
1367 strcasecmp(val, "wpa2-sha256") == 0) {
1368 if (set_network(ifname, id, "proto", "WPA2") < 0)
1369 return -2;
Pradeep Reddy POTTETI6d04b3b2016-11-15 14:51:26 +05301370 } else if (strcasecmp(val, "wpa2-wpa-psk") == 0 ||
1371 strcasecmp(val, "wpa2-wpa-ent") == 0) {
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001372 if (set_network(ifname, id, "proto", "WPA WPA2") < 0)
1373 return -2;
Jouni Malinenad395a22017-09-01 21:13:46 +03001374 } else if (strcasecmp(val, "SuiteB") == 0) {
Sunil Duttc75a1e62018-01-11 20:47:50 +05301375 suite_b = 1;
Jouni Malinenad395a22017-09-01 21:13:46 +03001376 if (set_network(ifname, id, "proto", "WPA2") < 0)
1377 return -2;
Jouni Malinen47dcc952017-10-09 16:43:24 +03001378 } else if (strcasecmp(val, "OWE") == 0) {
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001379 } else {
1380 send_resp(dut, conn, SIGMA_INVALID, "errorCode,Unrecognized keyMgmtType value");
1381 return 0;
1382 }
1383
1384 val = get_param(cmd, "encpType");
Jouni Malinenad395a22017-09-01 21:13:46 +03001385 if (val) {
1386 cipher_set = 1;
1387 if (strcasecmp(val, "tkip") == 0) {
1388 if (set_network(ifname, id, "pairwise", "TKIP") < 0)
1389 return -2;
1390 } else if (strcasecmp(val, "aes-ccmp") == 0) {
1391 if (set_network(ifname, id, "pairwise", "CCMP") < 0)
1392 return -2;
1393 } else if (strcasecmp(val, "aes-ccmp-tkip") == 0) {
1394 if (set_network(ifname, id, "pairwise",
1395 "CCMP TKIP") < 0)
1396 return -2;
1397 } else if (strcasecmp(val, "aes-gcmp") == 0) {
1398 if (set_network(ifname, id, "pairwise", "GCMP") < 0)
1399 return -2;
1400 if (set_network(ifname, id, "group", "GCMP") < 0)
1401 return -2;
1402 } else {
1403 send_resp(dut, conn, SIGMA_ERROR,
1404 "errorCode,Unrecognized encpType value");
1405 return 0;
1406 }
1407 }
1408
1409 val = get_param(cmd, "PairwiseCipher");
1410 if (val) {
1411 cipher_set = 1;
1412 /* TODO: Support space separated list */
1413 if (strcasecmp(val, "AES-GCMP-256") == 0) {
1414 if (set_network(ifname, id, "pairwise", "GCMP-256") < 0)
1415 return -2;
1416 } else if (strcasecmp(val, "AES-CCMP-256") == 0) {
1417 if (set_network(ifname, id, "pairwise",
1418 "CCMP-256") < 0)
1419 return -2;
1420 } else if (strcasecmp(val, "AES-GCMP-128") == 0) {
1421 if (set_network(ifname, id, "pairwise", "GCMP") < 0)
1422 return -2;
1423 } else if (strcasecmp(val, "AES-CCMP-128") == 0) {
1424 if (set_network(ifname, id, "pairwise", "CCMP") < 0)
1425 return -2;
1426 } else {
1427 send_resp(dut, conn, SIGMA_ERROR,
1428 "errorCode,Unrecognized PairwiseCipher value");
1429 return 0;
1430 }
1431 }
1432
Jouni Malinen47dcc952017-10-09 16:43:24 +03001433 if (!cipher_set && !owe) {
Jouni Malinenad395a22017-09-01 21:13:46 +03001434 send_resp(dut, conn, SIGMA_ERROR,
1435 "errorCode,Missing encpType and PairwiseCipher");
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001436 return 0;
1437 }
Jouni Malinenad395a22017-09-01 21:13:46 +03001438
1439 val = get_param(cmd, "GroupCipher");
1440 if (val) {
1441 if (strcasecmp(val, "AES-GCMP-256") == 0) {
1442 if (set_network(ifname, id, "group", "GCMP-256") < 0)
1443 return -2;
1444 } else if (strcasecmp(val, "AES-CCMP-256") == 0) {
1445 if (set_network(ifname, id, "group", "CCMP-256") < 0)
1446 return -2;
1447 } else if (strcasecmp(val, "AES-GCMP-128") == 0) {
1448 if (set_network(ifname, id, "group", "GCMP") < 0)
1449 return -2;
1450 } else if (strcasecmp(val, "AES-CCMP-128") == 0) {
1451 if (set_network(ifname, id, "group", "CCMP") < 0)
1452 return -2;
1453 } else {
1454 send_resp(dut, conn, SIGMA_ERROR,
1455 "errorCode,Unrecognized GroupCipher value");
1456 return 0;
1457 }
1458 }
1459
Jouni Malinen7b239522017-09-14 21:37:18 +03001460 val = get_param(cmd, "GroupMgntCipher");
Jouni Malinenad395a22017-09-01 21:13:46 +03001461 if (val) {
Jouni Malinene8898cb2017-09-26 17:55:26 +03001462 const char *cipher;
1463
1464 if (strcasecmp(val, "BIP-GMAC-256") == 0) {
1465 cipher = "BIP-GMAC-256";
1466 } else if (strcasecmp(val, "BIP-CMAC-256") == 0) {
1467 cipher = "BIP-CMAC-256";
1468 } else if (strcasecmp(val, "BIP-GMAC-128") == 0) {
1469 cipher = "BIP-GMAC-128";
1470 } else if (strcasecmp(val, "BIP-CMAC-128") == 0) {
1471 cipher = "AES-128-CMAC";
1472 } else {
1473 send_resp(dut, conn, SIGMA_INVALID,
1474 "errorCode,Unsupported GroupMgntCipher");
1475 return 0;
1476 }
1477 if (set_network(ifname, id, "group_mgmt", cipher) < 0) {
1478 send_resp(dut, conn, SIGMA_INVALID,
1479 "errorCode,Failed to set GroupMgntCipher");
1480 return 0;
1481 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001482 }
1483
1484 dut->sta_pmf = STA_PMF_DISABLED;
vamsi krishnaf39bc1e2017-08-23 17:37:53 +05301485
1486 if (dut->program == PROGRAM_OCE) {
1487 dut->sta_pmf = STA_PMF_OPTIONAL;
1488 if (set_network(ifname, id, "ieee80211w", "1") < 0)
1489 return -2;
1490 }
1491
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001492 val = get_param(cmd, "PMF");
1493 if (val) {
1494 if (strcasecmp(val, "Required") == 0 ||
1495 strcasecmp(val, "Forced_Required") == 0) {
1496 dut->sta_pmf = STA_PMF_REQUIRED;
1497 if (set_network(ifname, id, "ieee80211w", "2") < 0)
1498 return -2;
1499 } else if (strcasecmp(val, "Optional") == 0) {
1500 dut->sta_pmf = STA_PMF_OPTIONAL;
1501 if (set_network(ifname, id, "ieee80211w", "1") < 0)
1502 return -2;
1503 } else if (strcasecmp(val, "Disabled") == 0 ||
1504 strcasecmp(val, "Forced_Disabled") == 0) {
1505 dut->sta_pmf = STA_PMF_DISABLED;
1506 } else {
1507 send_resp(dut, conn, SIGMA_INVALID, "errorCode,Unrecognized PMF value");
1508 return 0;
1509 }
Sunil Duttc75a1e62018-01-11 20:47:50 +05301510 } else if (owe || suite_b) {
Jouni Malinen1287cd72018-01-04 17:08:01 +02001511 dut->sta_pmf = STA_PMF_REQUIRED;
1512 if (set_network(ifname, id, "ieee80211w", "2") < 0)
1513 return -2;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001514 }
1515
1516 return id;
1517}
1518
1519
1520static int cmd_sta_set_psk(struct sigma_dut *dut, struct sigma_conn *conn,
1521 struct sigma_cmd *cmd)
1522{
1523 const char *intf = get_param(cmd, "Interface");
Jouni Malinen992a81e2017-08-22 13:57:47 +03001524 const char *type = get_param(cmd, "Type");
Jouni Malinen1287cd72018-01-04 17:08:01 +02001525 const char *pmf = get_param(cmd, "PMF");
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001526 const char *ifname, *val, *alg;
1527 int id;
1528
1529 if (intf == NULL)
1530 return -1;
1531
1532 if (strcmp(intf, get_main_ifname()) == 0)
1533 ifname = get_station_ifname();
1534 else
1535 ifname = intf;
1536
1537 id = set_wpa_common(dut, conn, ifname, cmd);
1538 if (id < 0)
1539 return id;
1540
1541 val = get_param(cmd, "keyMgmtType");
1542 alg = get_param(cmd, "micAlg");
1543
Jouni Malinen992a81e2017-08-22 13:57:47 +03001544 if (type && strcasecmp(type, "SAE") == 0) {
1545 if (val && strcasecmp(val, "wpa2-ft") == 0) {
1546 if (set_network(ifname, id, "key_mgmt", "FT-SAE") < 0)
1547 return -2;
1548 } else {
1549 if (set_network(ifname, id, "key_mgmt", "SAE") < 0)
1550 return -2;
1551 }
1552 if (wpa_command(ifname, "SET sae_groups ") != 0) {
1553 sigma_dut_print(dut, DUT_MSG_ERROR,
1554 "Failed to clear sae_groups to default");
1555 return -2;
1556 }
Jouni Malinen1287cd72018-01-04 17:08:01 +02001557 if (!pmf) {
1558 dut->sta_pmf = STA_PMF_REQUIRED;
1559 if (set_network(ifname, id, "ieee80211w", "2") < 0)
1560 return -2;
1561 }
Jouni Malinen0ab50f42017-08-31 01:34:59 +03001562 } else if (type && strcasecmp(type, "PSK-SAE") == 0) {
1563 if (val && strcasecmp(val, "wpa2-ft") == 0) {
1564 if (set_network(ifname, id, "key_mgmt",
1565 "FT-SAE FT-PSK") < 0)
1566 return -2;
1567 } else {
1568 if (set_network(ifname, id, "key_mgmt",
1569 "SAE WPA-PSK") < 0)
1570 return -2;
1571 }
1572 if (wpa_command(ifname, "SET sae_groups ") != 0) {
1573 sigma_dut_print(dut, DUT_MSG_ERROR,
1574 "Failed to clear sae_groups to default");
1575 return -2;
1576 }
Jouni Malinen1287cd72018-01-04 17:08:01 +02001577 if (!pmf) {
1578 dut->sta_pmf = STA_PMF_OPTIONAL;
1579 if (set_network(ifname, id, "ieee80211w", "1") < 0)
1580 return -2;
1581 }
Jouni Malinen992a81e2017-08-22 13:57:47 +03001582 } else if (alg && strcasecmp(alg, "SHA-256") == 0) {
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001583 if (set_network(ifname, id, "key_mgmt", "WPA-PSK-SHA256") < 0)
1584 return -2;
1585 } else if (alg && strcasecmp(alg, "SHA-1") == 0) {
1586 if (set_network(ifname, id, "key_mgmt", "WPA-PSK") < 0)
1587 return -2;
Ashwini Patil6dbf7b02017-03-20 13:42:11 +05301588 } else if (val && strcasecmp(val, "wpa2-ft") == 0) {
1589 if (set_network(ifname, id, "key_mgmt", "FT-PSK") < 0)
1590 return -2;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001591 } else if ((val && strcasecmp(val, "wpa2-sha256") == 0) ||
1592 dut->sta_pmf == STA_PMF_REQUIRED) {
1593 if (set_network(ifname, id, "key_mgmt",
1594 "WPA-PSK WPA-PSK-SHA256") < 0)
1595 return -2;
1596 } else if (dut->sta_pmf == STA_PMF_OPTIONAL) {
1597 if (set_network(ifname, id, "key_mgmt",
1598 "WPA-PSK WPA-PSK-SHA256") < 0)
1599 return -2;
1600 } else {
1601 if (set_network(ifname, id, "key_mgmt", "WPA-PSK") < 0)
1602 return -2;
1603 }
1604
1605 val = get_param(cmd, "passPhrase");
1606 if (val == NULL)
1607 return -1;
Jouni Malinen2126f422017-10-11 23:24:33 +03001608 if (type && strcasecmp(type, "SAE") == 0) {
1609 if (set_network_quoted(ifname, id, "sae_password", val) < 0)
1610 return -2;
1611 } else {
1612 if (set_network_quoted(ifname, id, "psk", val) < 0)
1613 return -2;
1614 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001615
Jouni Malinen992a81e2017-08-22 13:57:47 +03001616 val = get_param(cmd, "ECGroupID");
1617 if (val) {
1618 char buf[50];
1619
1620 snprintf(buf, sizeof(buf), "SET sae_groups %u", atoi(val));
1621 if (wpa_command(ifname, buf) != 0) {
1622 sigma_dut_print(dut, DUT_MSG_ERROR,
1623 "Failed to clear sae_groups");
1624 return -2;
1625 }
1626 }
1627
Jouni Malinen68143132017-09-02 02:34:08 +03001628 val = get_param(cmd, "InvalidSAEElement");
1629 if (val) {
1630 free(dut->sae_commit_override);
1631 dut->sae_commit_override = strdup(val);
1632 }
1633
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001634 return 1;
1635}
1636
1637
1638static int set_eap_common(struct sigma_dut *dut, struct sigma_conn *conn,
Bala Krishna Bhamidipati73d7af02016-03-24 12:27:56 +05301639 const char *ifname, int username_identity,
1640 struct sigma_cmd *cmd)
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001641{
vamsi krishnaf39bc1e2017-08-23 17:37:53 +05301642 const char *val, *alg, *akm;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001643 int id;
1644 char buf[200];
1645#ifdef ANDROID
1646 unsigned char kvalue[KEYSTORE_MESSAGE_SIZE];
1647 int length;
1648#endif /* ANDROID */
1649
1650 id = set_wpa_common(dut, conn, ifname, cmd);
1651 if (id < 0)
1652 return id;
1653
1654 val = get_param(cmd, "keyMgmtType");
1655 alg = get_param(cmd, "micAlg");
vamsi krishnaf39bc1e2017-08-23 17:37:53 +05301656 akm = get_param(cmd, "AKMSuiteType");
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001657
Jouni Malinenad395a22017-09-01 21:13:46 +03001658 if (val && strcasecmp(val, "SuiteB") == 0) {
1659 if (set_network(ifname, id, "key_mgmt", "WPA-EAP-SUITE-B-192") <
1660 0)
1661 return -2;
1662 } else if (alg && strcasecmp(alg, "SHA-256") == 0) {
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001663 if (set_network(ifname, id, "key_mgmt", "WPA-EAP-SHA256") < 0)
1664 return -2;
1665 } else if (alg && strcasecmp(alg, "SHA-1") == 0) {
1666 if (set_network(ifname, id, "key_mgmt", "WPA-EAP") < 0)
1667 return -2;
1668 } else if (val && strcasecmp(val, "wpa2-ft") == 0) {
1669 if (set_network(ifname, id, "key_mgmt", "FT-EAP") < 0)
1670 return -2;
1671 } else if ((val && strcasecmp(val, "wpa2-sha256") == 0) ||
1672 dut->sta_pmf == STA_PMF_REQUIRED) {
1673 if (set_network(ifname, id, "key_mgmt",
1674 "WPA-EAP WPA-EAP-SHA256") < 0)
1675 return -2;
vamsi krishnaf39bc1e2017-08-23 17:37:53 +05301676 } else if (akm && atoi(akm) == 14) {
1677 if (dut->sta_pmf == STA_PMF_OPTIONAL ||
1678 dut->sta_pmf == STA_PMF_REQUIRED) {
1679 if (set_network(ifname, id, "key_mgmt",
1680 "WPA-EAP-SHA256 FILS-SHA256") < 0)
1681 return -2;
1682 } else {
1683 if (set_network(ifname, id, "key_mgmt",
1684 "WPA-EAP FILS-SHA256") < 0)
1685 return -2;
1686 }
1687
1688 if (set_network(ifname, id, "erp", "1") < 0)
1689 return -2;
1690 } else if (akm && atoi(akm) == 15) {
1691 if (dut->sta_pmf == STA_PMF_OPTIONAL ||
1692 dut->sta_pmf == STA_PMF_REQUIRED) {
1693 if (set_network(ifname, id, "key_mgmt",
1694 "WPA-EAP-SHA256 FILS-SHA384") < 0)
1695 return -2;
1696 } else {
1697 if (set_network(ifname, id, "key_mgmt",
1698 "WPA-EAP FILS-SHA384") < 0)
1699 return -2;
1700 }
1701
1702 if (set_network(ifname, id, "erp", "1") < 0)
1703 return -2;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001704 } else if (dut->sta_pmf == STA_PMF_OPTIONAL) {
1705 if (set_network(ifname, id, "key_mgmt",
1706 "WPA-EAP WPA-EAP-SHA256") < 0)
1707 return -2;
1708 } else {
1709 if (set_network(ifname, id, "key_mgmt", "WPA-EAP") < 0)
1710 return -2;
1711 }
1712
1713 val = get_param(cmd, "trustedRootCA");
1714 if (val) {
1715#ifdef ANDROID
1716 snprintf(buf, sizeof(buf), "CACERT_%s", val);
1717 length = android_keystore_get(ANDROID_KEYSTORE_GET, buf,
1718 kvalue);
1719 if (length > 0) {
1720 sigma_dut_print(dut, DUT_MSG_INFO,
1721 "Use Android keystore [%s]", buf);
1722 snprintf(buf, sizeof(buf), "keystore://CACERT_%s",
1723 val);
1724 goto ca_cert_selected;
1725 }
1726#endif /* ANDROID */
1727
1728 snprintf(buf, sizeof(buf), "%s/%s", sigma_cert_path, val);
1729#ifdef __linux__
1730 if (!file_exists(buf)) {
1731 char msg[300];
1732 snprintf(msg, sizeof(msg), "ErrorCode,trustedRootCA "
1733 "file (%s) not found", buf);
1734 send_resp(dut, conn, SIGMA_ERROR, msg);
1735 return -3;
1736 }
1737#endif /* __linux__ */
1738#ifdef ANDROID
1739ca_cert_selected:
1740#endif /* ANDROID */
1741 if (set_network_quoted(ifname, id, "ca_cert", buf) < 0)
1742 return -2;
1743 }
1744
Bala Krishna Bhamidipati73d7af02016-03-24 12:27:56 +05301745 if (username_identity) {
1746 val = get_param(cmd, "username");
1747 if (val) {
1748 if (set_network_quoted(ifname, id, "identity", val) < 0)
1749 return -2;
1750 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001751
Bala Krishna Bhamidipati73d7af02016-03-24 12:27:56 +05301752 val = get_param(cmd, "password");
1753 if (val) {
1754 if (set_network_quoted(ifname, id, "password", val) < 0)
1755 return -2;
1756 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001757 }
1758
1759 return id;
1760}
1761
1762
Jouni Malinen5eabb2a2017-10-03 18:17:30 +03001763static int set_tls_cipher(const char *ifname, int id, const char *cipher)
1764{
1765 const char *val;
1766
1767 if (!cipher)
1768 return 0;
1769
1770 if (strcasecmp(cipher, "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384") == 0)
1771 val = "ECDHE-ECDSA-AES256-GCM-SHA384";
1772 else if (strcasecmp(cipher,
1773 "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384") == 0)
1774 val = "ECDHE-RSA-AES256-GCM-SHA384";
1775 else if (strcasecmp(cipher, "TLS_DHE_RSA_WITH_AES_256_GCM_SHA384") == 0)
1776 val = "DHE-RSA-AES256-GCM-SHA384";
1777 else if (strcasecmp(cipher,
1778 "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256") == 0)
1779 val = "ECDHE-ECDSA-AES128-GCM-SHA256";
1780 else
1781 return -1;
1782
1783 /* Need to clear phase1="tls_suiteb=1" to allow cipher enforcement */
1784 set_network_quoted(ifname, id, "phase1", "");
1785
1786 return set_network_quoted(ifname, id, "openssl_ciphers", val);
1787}
1788
1789
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001790static int cmd_sta_set_eaptls(struct sigma_dut *dut, struct sigma_conn *conn,
1791 struct sigma_cmd *cmd)
1792{
1793 const char *intf = get_param(cmd, "Interface");
1794 const char *ifname, *val;
1795 int id;
1796 char buf[200];
1797#ifdef ANDROID
1798 unsigned char kvalue[KEYSTORE_MESSAGE_SIZE];
1799 int length;
1800 int jb_or_newer = 0;
1801 char prop[PROPERTY_VALUE_MAX];
1802#endif /* ANDROID */
1803
1804 if (intf == NULL)
1805 return -1;
1806
1807 if (strcmp(intf, get_main_ifname()) == 0)
1808 ifname = get_station_ifname();
1809 else
1810 ifname = intf;
1811
Bala Krishna Bhamidipati73d7af02016-03-24 12:27:56 +05301812 id = set_eap_common(dut, conn, ifname, 1, cmd);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001813 if (id < 0)
1814 return id;
1815
1816 if (set_network(ifname, id, "eap", "TLS") < 0)
1817 return -2;
1818
Pradeep Reddy POTTETI9f6c2132016-05-05 16:28:19 +05301819 if (!get_param(cmd, "username") &&
1820 set_network_quoted(ifname, id, "identity",
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001821 "wifi-user@wifilabs.local") < 0)
1822 return -2;
1823
1824 val = get_param(cmd, "clientCertificate");
1825 if (val == NULL)
1826 return -1;
1827#ifdef ANDROID
1828 snprintf(buf, sizeof(buf), "USRPKEY_%s", val);
1829 length = android_keystore_get(ANDROID_KEYSTORE_GET, buf, kvalue);
1830 if (length < 0) {
1831 /*
1832 * JB started reporting keystore type mismatches, so retry with
1833 * the GET_PUBKEY command if the generic GET fails.
1834 */
1835 length = android_keystore_get(ANDROID_KEYSTORE_GET_PUBKEY,
1836 buf, kvalue);
1837 }
1838
1839 if (property_get("ro.build.version.release", prop, NULL) != 0) {
1840 sigma_dut_print(dut, DUT_MSG_DEBUG, "Android release %s", prop);
1841 if (strncmp(prop, "4.0", 3) != 0)
1842 jb_or_newer = 1;
1843 } else
1844 jb_or_newer = 1; /* assume newer */
1845
1846 if (jb_or_newer && length > 0) {
1847 sigma_dut_print(dut, DUT_MSG_INFO,
1848 "Use Android keystore [%s]", buf);
1849 if (set_network(ifname, id, "engine", "1") < 0)
1850 return -2;
1851 if (set_network_quoted(ifname, id, "engine_id", "keystore") < 0)
1852 return -2;
1853 snprintf(buf, sizeof(buf), "USRPKEY_%s", val);
1854 if (set_network_quoted(ifname, id, "key_id", buf) < 0)
1855 return -2;
1856 snprintf(buf, sizeof(buf), "keystore://USRCERT_%s", val);
1857 if (set_network_quoted(ifname, id, "client_cert", buf) < 0)
1858 return -2;
1859 return 1;
1860 } else if (length > 0) {
1861 sigma_dut_print(dut, DUT_MSG_INFO,
1862 "Use Android keystore [%s]", buf);
1863 snprintf(buf, sizeof(buf), "keystore://USRPKEY_%s", val);
1864 if (set_network_quoted(ifname, id, "private_key", buf) < 0)
1865 return -2;
1866 snprintf(buf, sizeof(buf), "keystore://USRCERT_%s", val);
1867 if (set_network_quoted(ifname, id, "client_cert", buf) < 0)
1868 return -2;
1869 return 1;
1870 }
1871#endif /* ANDROID */
1872
1873 snprintf(buf, sizeof(buf), "%s/%s", sigma_cert_path, val);
1874#ifdef __linux__
1875 if (!file_exists(buf)) {
1876 char msg[300];
1877 snprintf(msg, sizeof(msg), "ErrorCode,clientCertificate file "
1878 "(%s) not found", buf);
1879 send_resp(dut, conn, SIGMA_ERROR, msg);
1880 return -3;
1881 }
1882#endif /* __linux__ */
1883 if (set_network_quoted(ifname, id, "private_key", buf) < 0)
1884 return -2;
1885 if (set_network_quoted(ifname, id, "client_cert", buf) < 0)
1886 return -2;
1887
1888 if (set_network_quoted(ifname, id, "private_key_passwd", "wifi") < 0)
1889 return -2;
1890
Jouni Malinen5eabb2a2017-10-03 18:17:30 +03001891 val = get_param(cmd, "keyMgmtType");
1892 if (val && strcasecmp(val, "SuiteB") == 0) {
1893 val = get_param(cmd, "CertType");
1894 if (val && strcasecmp(val, "RSA") == 0) {
1895 if (set_network_quoted(ifname, id, "phase1",
1896 "tls_suiteb=1") < 0)
1897 return -2;
1898 } else {
1899 if (set_network_quoted(ifname, id, "openssl_ciphers",
1900 "SUITEB192") < 0)
1901 return -2;
1902 }
1903
1904 val = get_param(cmd, "TLSCipher");
1905 if (set_tls_cipher(ifname, id, val) < 0) {
1906 send_resp(dut, conn, SIGMA_ERROR,
1907 "ErrorCode,Unsupported TLSCipher value");
1908 return -3;
1909 }
1910 }
1911
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001912 return 1;
1913}
1914
1915
1916static int cmd_sta_set_eapttls(struct sigma_dut *dut, struct sigma_conn *conn,
1917 struct sigma_cmd *cmd)
1918{
1919 const char *intf = get_param(cmd, "Interface");
1920 const char *ifname;
1921 int id;
1922
1923 if (intf == NULL)
1924 return -1;
1925
1926 if (strcmp(intf, get_main_ifname()) == 0)
1927 ifname = get_station_ifname();
1928 else
1929 ifname = intf;
1930
Bala Krishna Bhamidipati73d7af02016-03-24 12:27:56 +05301931 id = set_eap_common(dut, conn, ifname, 1, cmd);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001932 if (id < 0)
1933 return id;
1934
1935 if (set_network(ifname, id, "eap", "TTLS") < 0) {
1936 send_resp(dut, conn, SIGMA_ERROR,
1937 "errorCode,Failed to set TTLS method");
1938 return 0;
1939 }
1940
1941 if (set_network_quoted(ifname, id, "phase2", "auth=MSCHAPV2") < 0) {
1942 send_resp(dut, conn, SIGMA_ERROR,
1943 "errorCode,Failed to set MSCHAPv2 for TTLS Phase 2");
1944 return 0;
1945 }
1946
1947 return 1;
1948}
1949
1950
1951static int cmd_sta_set_eapsim(struct sigma_dut *dut, struct sigma_conn *conn,
1952 struct sigma_cmd *cmd)
1953{
1954 const char *intf = get_param(cmd, "Interface");
1955 const char *ifname;
1956 int id;
1957
1958 if (intf == NULL)
1959 return -1;
1960
1961 if (strcmp(intf, get_main_ifname()) == 0)
1962 ifname = get_station_ifname();
1963 else
1964 ifname = intf;
1965
Bala Krishna Bhamidipati73d7af02016-03-24 12:27:56 +05301966 id = set_eap_common(dut, conn, ifname, !dut->sim_no_username, cmd);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001967 if (id < 0)
1968 return id;
1969
1970 if (set_network(ifname, id, "eap", "SIM") < 0)
1971 return -2;
1972
1973 return 1;
1974}
1975
1976
1977static int cmd_sta_set_peap(struct sigma_dut *dut, struct sigma_conn *conn,
1978 struct sigma_cmd *cmd)
1979{
1980 const char *intf = get_param(cmd, "Interface");
1981 const char *ifname, *val;
1982 int id;
1983 char buf[100];
1984
1985 if (intf == NULL)
1986 return -1;
1987
1988 if (strcmp(intf, get_main_ifname()) == 0)
1989 ifname = get_station_ifname();
1990 else
1991 ifname = intf;
1992
Bala Krishna Bhamidipati73d7af02016-03-24 12:27:56 +05301993 id = set_eap_common(dut, conn, ifname, 1, cmd);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001994 if (id < 0)
1995 return id;
1996
1997 if (set_network(ifname, id, "eap", "PEAP") < 0)
1998 return -2;
1999
2000 val = get_param(cmd, "innerEAP");
2001 if (val) {
2002 if (strcasecmp(val, "MSCHAPv2") == 0) {
2003 if (set_network_quoted(ifname, id, "phase2",
2004 "auth=MSCHAPV2") < 0)
2005 return -2;
2006 } else if (strcasecmp(val, "GTC") == 0) {
2007 if (set_network_quoted(ifname, id, "phase2",
2008 "auth=GTC") < 0)
2009 return -2;
2010 } else
2011 return -1;
2012 }
2013
2014 val = get_param(cmd, "peapVersion");
2015 if (val) {
2016 int ver = atoi(val);
2017 if (ver < 0 || ver > 1)
2018 return -1;
2019 snprintf(buf, sizeof(buf), "peapver=%d", ver);
2020 if (set_network_quoted(ifname, id, "phase1", buf) < 0)
2021 return -2;
2022 }
2023
2024 return 1;
2025}
2026
2027
2028static int cmd_sta_set_eapfast(struct sigma_dut *dut, struct sigma_conn *conn,
2029 struct sigma_cmd *cmd)
2030{
2031 const char *intf = get_param(cmd, "Interface");
2032 const char *ifname, *val;
2033 int id;
2034 char buf[100];
2035
2036 if (intf == NULL)
2037 return -1;
2038
2039 if (strcmp(intf, get_main_ifname()) == 0)
2040 ifname = get_station_ifname();
2041 else
2042 ifname = intf;
2043
Bala Krishna Bhamidipati73d7af02016-03-24 12:27:56 +05302044 id = set_eap_common(dut, conn, ifname, 1, cmd);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02002045 if (id < 0)
2046 return id;
2047
2048 if (set_network(ifname, id, "eap", "FAST") < 0)
2049 return -2;
2050
2051 val = get_param(cmd, "innerEAP");
2052 if (val) {
2053 if (strcasecmp(val, "MSCHAPV2") == 0) {
2054 if (set_network_quoted(ifname, id, "phase2",
2055 "auth=MSCHAPV2") < 0)
2056 return -2;
2057 } else if (strcasecmp(val, "GTC") == 0) {
2058 if (set_network_quoted(ifname, id, "phase2",
2059 "auth=GTC") < 0)
2060 return -2;
2061 } else
2062 return -1;
2063 }
2064
2065 val = get_param(cmd, "validateServer");
2066 if (val) {
2067 /* TODO */
2068 sigma_dut_print(dut, DUT_MSG_INFO, "Ignored EAP-FAST "
2069 "validateServer=%s", val);
2070 }
2071
2072 val = get_param(cmd, "pacFile");
2073 if (val) {
2074 snprintf(buf, sizeof(buf), "blob://%s", val);
2075 if (set_network_quoted(ifname, id, "pac_file", buf) < 0)
2076 return -2;
2077 }
2078
2079 if (set_network_quoted(ifname, id, "phase1", "fast_provisioning=2") <
2080 0)
2081 return -2;
2082
2083 return 1;
2084}
2085
2086
2087static int cmd_sta_set_eapaka(struct sigma_dut *dut, struct sigma_conn *conn,
2088 struct sigma_cmd *cmd)
2089{
2090 const char *intf = get_param(cmd, "Interface");
2091 const char *ifname;
2092 int id;
2093
2094 if (intf == NULL)
2095 return -1;
2096
2097 if (strcmp(intf, get_main_ifname()) == 0)
2098 ifname = get_station_ifname();
2099 else
2100 ifname = intf;
2101
Bala Krishna Bhamidipati73d7af02016-03-24 12:27:56 +05302102 id = set_eap_common(dut, conn, ifname, !dut->sim_no_username, cmd);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02002103 if (id < 0)
2104 return id;
2105
2106 if (set_network(ifname, id, "eap", "AKA") < 0)
2107 return -2;
2108
2109 return 1;
2110}
2111
2112
2113static int cmd_sta_set_eapakaprime(struct sigma_dut *dut,
2114 struct sigma_conn *conn,
2115 struct sigma_cmd *cmd)
2116{
2117 const char *intf = get_param(cmd, "Interface");
2118 const char *ifname;
2119 int id;
2120
2121 if (intf == NULL)
2122 return -1;
2123
2124 if (strcmp(intf, get_main_ifname()) == 0)
2125 ifname = get_station_ifname();
2126 else
2127 ifname = intf;
2128
Bala Krishna Bhamidipati73d7af02016-03-24 12:27:56 +05302129 id = set_eap_common(dut, conn, ifname, !dut->sim_no_username, cmd);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02002130 if (id < 0)
2131 return id;
2132
2133 if (set_network(ifname, id, "eap", "AKA'") < 0)
2134 return -2;
2135
2136 return 1;
2137}
2138
2139
2140static int sta_set_open(struct sigma_dut *dut, struct sigma_conn *conn,
2141 struct sigma_cmd *cmd)
2142{
2143 const char *intf = get_param(cmd, "Interface");
2144 const char *ifname;
2145 int id;
2146
2147 if (strcmp(intf, get_main_ifname()) == 0)
2148 ifname = get_station_ifname();
2149 else
2150 ifname = intf;
2151
2152 id = add_network_common(dut, conn, ifname, cmd);
2153 if (id < 0)
2154 return id;
2155
2156 if (set_network(ifname, id, "key_mgmt", "NONE") < 0)
2157 return -2;
2158
2159 return 1;
2160}
2161
2162
Jouni Malinen47dcc952017-10-09 16:43:24 +03002163static int sta_set_owe(struct sigma_dut *dut, struct sigma_conn *conn,
2164 struct sigma_cmd *cmd)
2165{
2166 const char *intf = get_param(cmd, "Interface");
2167 const char *ifname, *val;
2168 int id;
2169
2170 if (intf == NULL)
2171 return -1;
2172
2173 if (strcmp(intf, get_main_ifname()) == 0)
2174 ifname = get_station_ifname();
2175 else
2176 ifname = intf;
2177
2178 id = set_wpa_common(dut, conn, ifname, cmd);
2179 if (id < 0)
2180 return id;
2181
2182 if (set_network(ifname, id, "key_mgmt", "OWE") < 0)
2183 return -2;
2184
2185 val = get_param(cmd, "ECGroupID");
Jouni Malinenfac9cad2017-10-10 18:35:55 +03002186 if (val && strcmp(val, "0") == 0) {
2187 if (wpa_command(ifname,
2188 "VENDOR_ELEM_ADD 13 ff23200000783590fb7440e03d5b3b33911f86affdcc6b4411b707846ac4ff08ddc8831ccd") != 0) {
2189 sigma_dut_print(dut, DUT_MSG_ERROR,
2190 "Failed to set OWE DH Param element override");
2191 return -2;
2192 }
2193 } else if (val && set_network(ifname, id, "owe_group", val) < 0) {
Jouni Malinen47dcc952017-10-09 16:43:24 +03002194 sigma_dut_print(dut, DUT_MSG_ERROR,
2195 "Failed to clear owe_group");
2196 return -2;
2197 }
2198
2199 return 1;
2200}
2201
2202
Jouni Malinencd4e3c32015-10-29 12:39:56 +02002203static int cmd_sta_set_security(struct sigma_dut *dut, struct sigma_conn *conn,
2204 struct sigma_cmd *cmd)
2205{
2206 const char *type = get_param(cmd, "Type");
2207
2208 if (type == NULL) {
2209 send_resp(dut, conn, SIGMA_ERROR,
2210 "ErrorCode,Missing Type argument");
2211 return 0;
2212 }
2213
2214 if (strcasecmp(type, "OPEN") == 0)
2215 return sta_set_open(dut, conn, cmd);
Jouni Malinen47dcc952017-10-09 16:43:24 +03002216 if (strcasecmp(type, "OWE") == 0)
2217 return sta_set_owe(dut, conn, cmd);
Jouni Malinen992a81e2017-08-22 13:57:47 +03002218 if (strcasecmp(type, "PSK") == 0 ||
Jouni Malinen0ab50f42017-08-31 01:34:59 +03002219 strcasecmp(type, "PSK-SAE") == 0 ||
Jouni Malinen992a81e2017-08-22 13:57:47 +03002220 strcasecmp(type, "SAE") == 0)
Jouni Malinencd4e3c32015-10-29 12:39:56 +02002221 return cmd_sta_set_psk(dut, conn, cmd);
2222 if (strcasecmp(type, "EAPTLS") == 0)
2223 return cmd_sta_set_eaptls(dut, conn, cmd);
2224 if (strcasecmp(type, "EAPTTLS") == 0)
2225 return cmd_sta_set_eapttls(dut, conn, cmd);
2226 if (strcasecmp(type, "EAPPEAP") == 0)
2227 return cmd_sta_set_peap(dut, conn, cmd);
2228 if (strcasecmp(type, "EAPSIM") == 0)
2229 return cmd_sta_set_eapsim(dut, conn, cmd);
2230 if (strcasecmp(type, "EAPFAST") == 0)
2231 return cmd_sta_set_eapfast(dut, conn, cmd);
2232 if (strcasecmp(type, "EAPAKA") == 0)
2233 return cmd_sta_set_eapaka(dut, conn, cmd);
2234 if (strcasecmp(type, "EAPAKAPRIME") == 0)
2235 return cmd_sta_set_eapakaprime(dut, conn, cmd);
Amarnath Hullur Subramanyam81b11cd2018-01-30 19:07:17 -08002236 if (strcasecmp(type, "wep") == 0)
2237 return cmd_sta_set_encryption(dut, conn, cmd);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02002238
2239 send_resp(dut, conn, SIGMA_ERROR,
2240 "ErrorCode,Unsupported Type value");
2241 return 0;
2242}
2243
2244
2245int ath6kl_client_uapsd(struct sigma_dut *dut, const char *intf, int uapsd)
2246{
2247#ifdef __linux__
2248 /* special handling for ath6kl */
2249 char path[128], fname[128], *pos;
2250 ssize_t res;
2251 FILE *f;
2252
2253 snprintf(path, sizeof(path), "/sys/class/net/%s/phy80211", intf);
2254 res = readlink(path, path, sizeof(path));
2255 if (res < 0)
2256 return 0; /* not ath6kl */
2257
2258 if (res >= (int) sizeof(path))
2259 res = sizeof(path) - 1;
2260 path[res] = '\0';
2261 pos = strrchr(path, '/');
2262 if (pos == NULL)
2263 pos = path;
2264 else
2265 pos++;
2266 snprintf(fname, sizeof(fname),
2267 "/sys/kernel/debug/ieee80211/%s/ath6kl/"
2268 "create_qos", pos);
2269 if (!file_exists(fname))
2270 return 0; /* not ath6kl */
2271
2272 if (uapsd) {
2273 f = fopen(fname, "w");
2274 if (f == NULL)
2275 return -1;
2276
2277 sigma_dut_print(dut, DUT_MSG_DEBUG, "Use ath6kl create_qos");
2278 fprintf(f, "4 2 2 1 2 9999999 9999999 9999999 7777777 0 4 "
2279 "45000 200 56789000 56789000 5678900 0 0 9999999 "
2280 "20000 0\n");
2281 fclose(f);
2282 } else {
2283 snprintf(fname, sizeof(fname),
2284 "/sys/kernel/debug/ieee80211/%s/ath6kl/"
2285 "delete_qos", pos);
2286
2287 f = fopen(fname, "w");
2288 if (f == NULL)
2289 return -1;
2290
2291 sigma_dut_print(dut, DUT_MSG_DEBUG, "Use ath6kl delete_qos");
2292 fprintf(f, "2 4\n");
2293 fclose(f);
2294 }
2295#endif /* __linux__ */
2296
2297 return 0;
2298}
2299
2300
2301static int cmd_sta_set_uapsd(struct sigma_dut *dut, struct sigma_conn *conn,
2302 struct sigma_cmd *cmd)
2303{
2304 const char *intf = get_param(cmd, "Interface");
2305 /* const char *ssid = get_param(cmd, "ssid"); */
2306 const char *val;
2307 int max_sp_len = 4;
2308 int ac_be = 1, ac_bk = 1, ac_vi = 1, ac_vo = 1;
2309 char buf[100];
2310 int ret1, ret2;
2311
2312 val = get_param(cmd, "maxSPLength");
2313 if (val) {
2314 max_sp_len = atoi(val);
2315 if (max_sp_len != 0 && max_sp_len != 1 && max_sp_len != 2 &&
2316 max_sp_len != 4)
2317 return -1;
2318 }
2319
2320 val = get_param(cmd, "acBE");
2321 if (val)
2322 ac_be = atoi(val);
2323
2324 val = get_param(cmd, "acBK");
2325 if (val)
2326 ac_bk = atoi(val);
2327
2328 val = get_param(cmd, "acVI");
2329 if (val)
2330 ac_vi = atoi(val);
2331
2332 val = get_param(cmd, "acVO");
2333 if (val)
2334 ac_vo = atoi(val);
2335
2336 dut->client_uapsd = ac_be || ac_bk || ac_vi || ac_vo;
2337
2338 snprintf(buf, sizeof(buf), "P2P_SET client_apsd %d,%d,%d,%d;%d",
2339 ac_be, ac_bk, ac_vi, ac_vo, max_sp_len);
2340 ret1 = wpa_command(intf, buf);
2341
2342 snprintf(buf, sizeof(buf), "SET uapsd %d,%d,%d,%d;%d",
2343 ac_be, ac_bk, ac_vi, ac_vo, max_sp_len);
2344 ret2 = wpa_command(intf, buf);
2345
2346 if (ret1 && ret2) {
2347 sigma_dut_print(dut, DUT_MSG_INFO, "Failed to set client mode "
2348 "UAPSD parameters.");
2349 return -2;
2350 }
2351
2352 if (ath6kl_client_uapsd(dut, intf, dut->client_uapsd) < 0) {
2353 send_resp(dut, conn, SIGMA_ERROR,
2354 "ErrorCode,Failed to set ath6kl QoS parameters");
2355 return 0;
2356 }
2357
2358 return 1;
2359}
2360
2361
2362static int cmd_sta_set_wmm(struct sigma_dut *dut, struct sigma_conn *conn,
2363 struct sigma_cmd *cmd)
2364{
2365 char buf[1000];
2366 const char *intf = get_param(cmd, "Interface");
2367 const char *grp = get_param(cmd, "Group");
2368 const char *act = get_param(cmd, "Action");
2369 const char *tid = get_param(cmd, "Tid");
2370 const char *dir = get_param(cmd, "Direction");
2371 const char *psb = get_param(cmd, "Psb");
2372 const char *up = get_param(cmd, "Up");
2373 const char *fixed = get_param(cmd, "Fixed");
2374 const char *size = get_param(cmd, "Size");
2375 const char *msize = get_param(cmd, "Maxsize");
2376 const char *minsi = get_param(cmd, "Min_srvc_intrvl");
2377 const char *maxsi = get_param(cmd, "Max_srvc_intrvl");
2378 const char *inact = get_param(cmd, "Inactivity");
2379 const char *sus = get_param(cmd, "Suspension");
2380 const char *mindr = get_param(cmd, "Mindatarate");
2381 const char *meandr = get_param(cmd, "Meandatarate");
2382 const char *peakdr = get_param(cmd, "Peakdatarate");
2383 const char *phyrate = get_param(cmd, "Phyrate");
2384 const char *burstsize = get_param(cmd, "Burstsize");
2385 const char *sba = get_param(cmd, "Sba");
2386 int direction;
2387 int handle;
Peng Xu93319622017-10-04 17:58:16 -07002388 float sba_fv = 0;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02002389 int fixed_int;
2390 int psb_ts;
2391
2392 if (intf == NULL || grp == NULL || act == NULL )
2393 return -1;
2394
2395 if (strcasecmp(act, "addts") == 0) {
2396 if (tid == NULL || dir == NULL || psb == NULL ||
2397 up == NULL || fixed == NULL || size == NULL)
2398 return -1;
2399
2400 /*
2401 * Note: Sigma CAPI spec lists uplink, downlink, and bidi as the
2402 * possible values, but WMM-AC and V-E test scripts use "UP,
2403 * "DOWN", and "BIDI".
2404 */
2405 if (strcasecmp(dir, "uplink") == 0 ||
2406 strcasecmp(dir, "up") == 0) {
2407 direction = 0;
2408 } else if (strcasecmp(dir, "downlink") == 0 ||
2409 strcasecmp(dir, "down") == 0) {
2410 direction = 1;
2411 } else if (strcasecmp(dir, "bidi") == 0) {
2412 direction = 2;
2413 } else {
2414 sigma_dut_print(dut, DUT_MSG_ERROR,
2415 "Direction %s not supported", dir);
2416 return -1;
2417 }
2418
2419 if (strcasecmp(psb, "legacy") == 0) {
2420 psb_ts = 0;
2421 } else if (strcasecmp(psb, "uapsd") == 0) {
2422 psb_ts = 1;
2423 } else {
2424 sigma_dut_print(dut, DUT_MSG_ERROR,
2425 "PSB %s not supported", psb);
2426 return -1;
2427 }
2428
2429 if (atoi(tid) < 0 || atoi(tid) > 7) {
2430 sigma_dut_print(dut, DUT_MSG_ERROR,
2431 "TID %s not supported", tid);
2432 return -1;
2433 }
2434
2435 if (strcasecmp(fixed, "true") == 0) {
2436 fixed_int = 1;
2437 } else {
2438 fixed_int = 0;
2439 }
2440
Peng Xu93319622017-10-04 17:58:16 -07002441 if (sba)
2442 sba_fv = atof(sba);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02002443
2444 dut->dialog_token++;
2445 handle = 7000 + dut->dialog_token;
2446
2447 /*
2448 * size: convert to hex
2449 * maxsi: convert to hex
2450 * mindr: convert to hex
2451 * meandr: convert to hex
2452 * peakdr: convert to hex
2453 * burstsize: convert to hex
2454 * phyrate: convert to hex
2455 * sba: convert to hex with modification
2456 * minsi: convert to integer
2457 * sus: convert to integer
2458 * inact: convert to integer
2459 * maxsi: convert to integer
2460 */
2461
2462 /*
2463 * The Nominal MSDU Size field is 2 octets long and contains an
2464 * unsigned integer that specifies the nominal size, in octets,
2465 * of MSDUs belonging to the traffic under this traffic
2466 * specification and is defined in Figure 16. If the Fixed
2467 * subfield is set to 1, then the size of the MSDU is fixed and
2468 * is indicated by the Size Subfield. If the Fixed subfield is
2469 * set to 0, then the size of the MSDU might not be fixed and
2470 * the Size indicates the nominal MSDU size.
2471 *
2472 * The Surplus Bandwidth Allowance Factor field is 2 octets long
2473 * and specifies the excess allocation of time (and bandwidth)
2474 * over and above the stated rates required to transport an MSDU
2475 * belonging to the traffic in this TSPEC. This field is
2476 * represented as an unsigned binary number with an implicit
2477 * binary point after the leftmost 3 bits. For example, an SBA
2478 * of 1.75 is represented as 0x3800. This field is included to
2479 * account for retransmissions. As such, the value of this field
2480 * must be greater than unity.
2481 */
2482
2483 snprintf(buf, sizeof(buf),
2484 "iwpriv %s addTspec %d %s %d %d %s 0x%X"
2485 " 0x%X 0x%X 0x%X"
2486 " 0x%X 0x%X 0x%X"
2487 " 0x%X %d %d %d %d"
2488 " %d %d",
2489 intf, handle, tid, direction, psb_ts, up,
2490 (unsigned int) ((fixed_int << 15) | atoi(size)),
2491 msize ? atoi(msize) : 0,
2492 mindr ? atoi(mindr) : 0,
2493 meandr ? atoi(meandr) : 0,
2494 peakdr ? atoi(peakdr) : 0,
2495 burstsize ? atoi(burstsize) : 0,
2496 phyrate ? atoi(phyrate) : 0,
2497 sba ? ((unsigned int) (((int) sba_fv << 13) |
2498 (int)((sba_fv - (int) sba_fv) *
2499 8192))) : 0,
2500 minsi ? atoi(minsi) : 0,
2501 sus ? atoi(sus) : 0,
2502 0, 0,
2503 inact ? atoi(inact) : 0,
2504 maxsi ? atoi(maxsi) : 0);
2505
2506 if (system(buf) != 0) {
2507 sigma_dut_print(dut, DUT_MSG_ERROR,
2508 "iwpriv addtspec request failed");
2509 send_resp(dut, conn, SIGMA_ERROR,
2510 "errorCode,Failed to execute addTspec command");
2511 return 0;
2512 }
2513
2514 sigma_dut_print(dut, DUT_MSG_INFO,
2515 "iwpriv addtspec request send");
2516
2517 /* Mapping handle to a TID */
2518 dut->tid_to_handle[atoi(tid)] = handle;
2519 } else if (strcasecmp(act, "delts") == 0) {
2520 if (tid == NULL)
2521 return -1;
2522
2523 if (atoi(tid) < 0 || atoi(tid) > 7) {
2524 sigma_dut_print(dut, DUT_MSG_ERROR,
2525 "TID %s not supported", tid);
2526 send_resp(dut, conn, SIGMA_ERROR,
2527 "errorCode,Unsupported TID");
2528 return 0;
2529 }
2530
2531 handle = dut->tid_to_handle[atoi(tid)];
2532
2533 if (handle < 7000 || handle > 7255) {
2534 /* Invalid handle ie no mapping for that TID */
2535 sigma_dut_print(dut, DUT_MSG_ERROR,
2536 "handle-> %d not found", handle);
2537 }
2538
2539 snprintf(buf, sizeof(buf), "iwpriv %s delTspec %d",
2540 intf, handle);
2541
2542 if (system(buf) != 0) {
2543 sigma_dut_print(dut, DUT_MSG_ERROR,
2544 "iwpriv deltspec request failed");
2545 send_resp(dut, conn, SIGMA_ERROR,
2546 "errorCode,Failed to execute delTspec command");
2547 return 0;
2548 }
2549
2550 sigma_dut_print(dut, DUT_MSG_INFO,
2551 "iwpriv deltspec request send");
2552
2553 dut->tid_to_handle[atoi(tid)] = 0;
2554 } else {
2555 sigma_dut_print(dut, DUT_MSG_ERROR,
2556 "Action type %s not supported", act);
2557 send_resp(dut, conn, SIGMA_ERROR,
2558 "errorCode,Unsupported Action");
2559 return 0;
2560 }
2561
2562 return 1;
2563}
2564
2565
vamsi krishna52e16f92017-08-29 12:37:34 +05302566static int find_network(struct sigma_dut *dut, const char *ssid)
2567{
2568 char list[4096];
2569 char *pos;
2570
2571 sigma_dut_print(dut, DUT_MSG_DEBUG,
2572 "Search for profile based on SSID: '%s'", ssid);
2573 if (wpa_command_resp(get_station_ifname(), "LIST_NETWORKS",
2574 list, sizeof(list)) < 0)
2575 return -1;
2576 pos = strstr(list, ssid);
2577 if (!pos || pos == list || pos[-1] != '\t' || pos[strlen(ssid)] != '\t')
2578 return -1;
2579
2580 while (pos > list && pos[-1] != '\n')
2581 pos--;
2582 dut->infra_network_id = atoi(pos);
2583 snprintf(dut->infra_ssid, sizeof(dut->infra_ssid), "%s", ssid);
2584 return 0;
2585}
2586
2587
Sunil Dutt44595082018-02-12 19:41:45 +05302588#ifdef NL80211_SUPPORT
2589static int sta_config_rsnie(struct sigma_dut *dut, int val)
2590{
2591 struct nl_msg *msg;
2592 int ret;
2593 struct nlattr *params;
2594 int ifindex;
2595
2596 ifindex = if_nametoindex("wlan0");
2597 if (!(msg = nl80211_drv_msg(dut, dut->nl_ctx, ifindex, 0,
2598 NL80211_CMD_VENDOR)) ||
2599 nla_put_u32(msg, NL80211_ATTR_IFINDEX, ifindex) ||
2600 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
2601 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
2602 QCA_NL80211_VENDOR_SUBCMD_SET_WIFI_CONFIGURATION) ||
2603 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
2604 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_CONFIG_RSN_IE, val)) {
2605 sigma_dut_print(dut, DUT_MSG_ERROR,
2606 "%s: err in adding vendor_cmd and vendor_data",
2607 __func__);
2608 nlmsg_free(msg);
2609 return -1;
2610 }
2611 nla_nest_end(msg, params);
2612
2613 ret = send_and_recv_msgs(dut, dut->nl_ctx, msg, NULL, NULL);
2614 if (ret) {
2615 sigma_dut_print(dut, DUT_MSG_ERROR,
2616 "%s: err in send_and_recv_msgs, ret=%d",
2617 __func__, ret);
2618 return ret;
2619 }
2620
2621 return 0;
2622}
2623#endif /* NL80211_SUPPORT */
2624
2625
Jouni Malinencd4e3c32015-10-29 12:39:56 +02002626static int cmd_sta_associate(struct sigma_dut *dut, struct sigma_conn *conn,
2627 struct sigma_cmd *cmd)
2628{
2629 /* const char *intf = get_param(cmd, "Interface"); */
2630 const char *ssid = get_param(cmd, "ssid");
2631 const char *wps_param = get_param(cmd, "WPS");
2632 const char *bssid = get_param(cmd, "bssid");
Jouni Malinen46a19b62017-06-23 14:31:27 +03002633 const char *chan = get_param(cmd, "channel");
Jouni Malinencd4e3c32015-10-29 12:39:56 +02002634 int wps = 0;
Jouni Malinen3c367e82017-06-23 17:01:47 +03002635 char buf[1000], extra[50];
Jouni Malinencd4e3c32015-10-29 12:39:56 +02002636
2637 if (ssid == NULL)
2638 return -1;
2639
Jouni Malinen3c367e82017-06-23 17:01:47 +03002640 if (dut->rsne_override) {
Sunil Dutt44595082018-02-12 19:41:45 +05302641#ifdef NL80211_SUPPORT
2642 if (get_driver_type() == DRIVER_WCN) {
2643 sta_config_rsnie(dut, 1);
2644 dut->config_rsnie = 1;
2645 }
2646#endif /* NL80211_SUPPORT */
Jouni Malinen3c367e82017-06-23 17:01:47 +03002647 snprintf(buf, sizeof(buf), "TEST_ASSOC_IE %s",
2648 dut->rsne_override);
2649 if (wpa_command(get_station_ifname(), buf) < 0) {
2650 send_resp(dut, conn, SIGMA_ERROR,
2651 "ErrorCode,Failed to set DEV_CONFIGURE_IE RSNE override");
2652 return 0;
2653 }
2654 }
2655
Jouni Malinen68143132017-09-02 02:34:08 +03002656 if (dut->sae_commit_override) {
2657 snprintf(buf, sizeof(buf), "SET sae_commit_override %s",
2658 dut->sae_commit_override);
2659 if (wpa_command(get_station_ifname(), buf) < 0) {
2660 send_resp(dut, conn, SIGMA_ERROR,
2661 "ErrorCode,Failed to set SAE commit override");
2662 return 0;
2663 }
2664 }
Ankita Bajaj1bde7942018-01-09 19:15:01 +05302665#ifdef ANDROID
2666 if (dut->fils_hlp)
2667 process_fils_hlp(dut);
2668#endif /* ANDROID */
Jouni Malinen68143132017-09-02 02:34:08 +03002669
Jouni Malinencd4e3c32015-10-29 12:39:56 +02002670 if (wps_param &&
2671 (strcmp(wps_param, "1") == 0 || strcasecmp(wps_param, "On") == 0))
2672 wps = 1;
2673
2674 if (wps) {
2675 if (dut->wps_method == WFA_CS_WPS_NOT_READY) {
2676 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,WPS "
2677 "parameters not yet set");
2678 return 0;
2679 }
2680 if (dut->wps_method == WFA_CS_WPS_PBC) {
2681 if (wpa_command(get_station_ifname(), "WPS_PBC") < 0)
2682 return -2;
2683 } else {
2684 snprintf(buf, sizeof(buf), "WPS_PIN any %s",
2685 dut->wps_pin);
2686 if (wpa_command(get_station_ifname(), buf) < 0)
2687 return -2;
2688 }
2689 } else {
vamsi krishna52e16f92017-08-29 12:37:34 +05302690 if (strcmp(ssid, dut->infra_ssid) == 0) {
2691 sigma_dut_print(dut, DUT_MSG_DEBUG,
2692 "sta_associate for the most recently added network");
2693 } else if (find_network(dut, ssid) < 0) {
2694 sigma_dut_print(dut, DUT_MSG_DEBUG,
2695 "sta_associate for a previously stored network profile");
2696 send_resp(dut, conn, SIGMA_ERROR,
2697 "ErrorCode,Profile not found");
Jouni Malinencd4e3c32015-10-29 12:39:56 +02002698 return 0;
2699 }
2700
2701 if (bssid &&
2702 set_network(get_station_ifname(), dut->infra_network_id,
2703 "bssid", bssid) < 0) {
2704 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,"
2705 "Invalid bssid argument");
2706 return 0;
2707 }
2708
Jouni Malinen46a19b62017-06-23 14:31:27 +03002709 extra[0] = '\0';
2710 if (chan)
2711 snprintf(extra, sizeof(extra), " freq=%u",
2712 channel_to_freq(atoi(chan)));
2713 snprintf(buf, sizeof(buf), "SELECT_NETWORK %d%s",
2714 dut->infra_network_id, extra);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02002715 if (wpa_command(get_station_ifname(), buf) < 0) {
2716 sigma_dut_print(dut, DUT_MSG_INFO, "Failed to select "
2717 "network id %d on %s",
2718 dut->infra_network_id,
2719 get_station_ifname());
2720 return -2;
2721 }
2722 }
2723
2724 return 1;
2725}
2726
2727
2728static int run_hs20_osu(struct sigma_dut *dut, const char *params)
2729{
2730 char buf[500], cmd[200];
2731 int res;
2732
2733 /* Use hs20-osu-client file at the current dir, if found; otherwise use
2734 * default path */
2735 res = snprintf(cmd, sizeof(cmd),
2736 "%s -w \"%s\" -r hs20-osu-client.res %s%s -dddKt -f Logs/hs20-osu-client.txt",
2737 file_exists("./hs20-osu-client") ?
2738 "./hs20-osu-client" : "hs20-osu-client",
2739 sigma_wpas_ctrl,
2740 dut->summary_log ? "-s " : "",
2741 dut->summary_log ? dut->summary_log : "");
2742 if (res < 0 || res >= (int) sizeof(cmd))
2743 return -1;
2744
2745 res = snprintf(buf, sizeof(buf), "%s %s", cmd, params);
2746 if (res < 0 || res >= (int) sizeof(buf))
2747 return -1;
2748 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
2749
2750 if (system(buf) != 0) {
2751 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to run: %s", buf);
2752 return -1;
2753 }
2754 sigma_dut_print(dut, DUT_MSG_DEBUG,
2755 "Completed hs20-osu-client operation");
2756
2757 return 0;
2758}
2759
2760
2761static int download_ppsmo(struct sigma_dut *dut,
2762 struct sigma_conn *conn,
2763 const char *intf,
2764 struct sigma_cmd *cmd)
2765{
2766 const char *name, *path, *val;
2767 char url[500], buf[600], fbuf[100];
2768 char *fqdn = NULL;
2769
2770 name = get_param(cmd, "FileName");
2771 path = get_param(cmd, "FilePath");
2772 if (name == NULL || path == NULL)
2773 return -1;
2774
2775 if (strcasecmp(path, "VendorSpecific") == 0) {
2776 snprintf(url, sizeof(url), "PPS/%s", name);
2777 sigma_dut_print(dut, DUT_MSG_INFO, "Use pre-configured PPS MO "
2778 "from the device (%s)", url);
2779 if (!file_exists(url)) {
2780 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Requested "
2781 "PPS MO file does not exist");
2782 return 0;
2783 }
2784 snprintf(buf, sizeof(buf), "cp %s pps-tnds.xml", url);
2785 if (system(buf) != 0) {
2786 send_resp(dut, conn, SIGMA_ERROR,
2787 "errorCode,Failed to copy PPS MO");
2788 return 0;
2789 }
2790 } else if (strncasecmp(path, "http:", 5) != 0 &&
2791 strncasecmp(path, "https:", 6) != 0) {
2792 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,"
2793 "Unsupported FilePath value");
2794 return 0;
2795 } else {
2796 snprintf(url, sizeof(url), "%s/%s", path, name);
2797 sigma_dut_print(dut, DUT_MSG_INFO, "Downloading PPS MO from %s",
2798 url);
2799 snprintf(buf, sizeof(buf), "wget -T 10 -t 3 -O pps-tnds.xml '%s'", url);
2800 remove("pps-tnds.xml");
2801 if (system(buf) != 0) {
2802 send_resp(dut, conn, SIGMA_ERROR,
2803 "errorCode,Failed to download PPS MO");
2804 return 0;
2805 }
2806 }
2807
2808 if (run_hs20_osu(dut, "from_tnds pps-tnds.xml pps.xml") < 0) {
2809 send_resp(dut, conn, SIGMA_ERROR,
2810 "errorCode,Failed to parse downloaded PPSMO");
2811 return 0;
2812 }
2813 unlink("pps-tnds.xml");
2814
2815 val = get_param(cmd, "managementTreeURI");
2816 if (val) {
2817 const char *pos, *end;
2818 sigma_dut_print(dut, DUT_MSG_DEBUG, "managementTreeURI: %s",
2819 val);
2820 if (strncmp(val, "./Wi-Fi/", 8) != 0) {
2821 send_resp(dut, conn, SIGMA_ERROR,
2822 "errorCode,Invalid managementTreeURI prefix");
2823 return 0;
2824 }
2825 pos = val + 8;
2826 end = strchr(pos, '/');
2827 if (end == NULL ||
2828 strcmp(end, "/PerProviderSubscription") != 0) {
2829 send_resp(dut, conn, SIGMA_ERROR,
2830 "errorCode,Invalid managementTreeURI postfix");
2831 return 0;
2832 }
2833 if (end - pos >= (int) sizeof(fbuf)) {
2834 send_resp(dut, conn, SIGMA_ERROR,
2835 "errorCode,Too long FQDN in managementTreeURI");
2836 return 0;
2837 }
2838 memcpy(fbuf, pos, end - pos);
2839 fbuf[end - pos] = '\0';
2840 fqdn = fbuf;
2841 sigma_dut_print(dut, DUT_MSG_INFO,
2842 "FQDN from managementTreeURI: %s", fqdn);
2843 } else if (run_hs20_osu(dut, "get_fqdn pps.xml") == 0) {
2844 FILE *f = fopen("pps-fqdn", "r");
2845 if (f) {
2846 if (fgets(fbuf, sizeof(fbuf), f)) {
2847 fbuf[sizeof(fbuf) - 1] = '\0';
2848 fqdn = fbuf;
2849 sigma_dut_print(dut, DUT_MSG_DEBUG,
2850 "Use FQDN %s", fqdn);
2851 }
2852 fclose(f);
2853 }
2854 }
2855
2856 if (fqdn == NULL) {
2857 send_resp(dut, conn, SIGMA_ERROR,
2858 "errorCode,No FQDN specified");
2859 return 0;
2860 }
2861
2862 mkdir("SP", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
2863 snprintf(buf, sizeof(buf), "SP/%s", fqdn);
2864 mkdir(buf, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
2865
2866 snprintf(buf, sizeof(buf), "SP/%s/pps.xml", fqdn);
2867 if (rename("pps.xml", buf) < 0) {
2868 send_resp(dut, conn, SIGMA_ERROR,
2869 "errorCode,Could not move PPS MO");
2870 return 0;
2871 }
2872
2873 if (strcasecmp(path, "VendorSpecific") == 0) {
2874 snprintf(buf, sizeof(buf), "cp Certs/ca.pem SP/%s/ca.pem",
2875 fqdn);
2876 if (system(buf)) {
2877 send_resp(dut, conn, SIGMA_ERROR,
2878 "errorCode,Failed to copy OSU CA cert");
2879 return 0;
2880 }
2881
2882 snprintf(buf, sizeof(buf),
2883 "cp Certs/aaa-ca.pem SP/%s/aaa-ca.pem",
2884 fqdn);
2885 if (system(buf)) {
2886 send_resp(dut, conn, SIGMA_ERROR,
2887 "errorCode,Failed to copy AAA CA cert");
2888 return 0;
2889 }
2890 } else {
2891 snprintf(buf, sizeof(buf),
2892 "dl_osu_ca SP/%s/pps.xml SP/%s/ca.pem",
2893 fqdn, fqdn);
2894 if (run_hs20_osu(dut, buf) < 0) {
2895 send_resp(dut, conn, SIGMA_ERROR,
2896 "errorCode,Failed to download OSU CA cert");
2897 return 0;
2898 }
2899
2900 snprintf(buf, sizeof(buf),
2901 "dl_aaa_ca SP/%s/pps.xml SP/%s/aaa-ca.pem",
2902 fqdn, fqdn);
2903 if (run_hs20_osu(dut, buf) < 0) {
2904 sigma_dut_print(dut, DUT_MSG_INFO,
2905 "Failed to download AAA CA cert");
2906 }
2907 }
2908
2909 if (file_exists("next-client-cert.pem")) {
2910 snprintf(buf, sizeof(buf), "SP/%s/client-cert.pem", fqdn);
2911 if (rename("next-client-cert.pem", buf) < 0) {
2912 send_resp(dut, conn, SIGMA_ERROR,
2913 "errorCode,Could not move client certificate");
2914 return 0;
2915 }
2916 }
2917
2918 if (file_exists("next-client-key.pem")) {
2919 snprintf(buf, sizeof(buf), "SP/%s/client-key.pem", fqdn);
2920 if (rename("next-client-key.pem", buf) < 0) {
2921 send_resp(dut, conn, SIGMA_ERROR,
2922 "errorCode,Could not move client key");
2923 return 0;
2924 }
2925 }
2926
2927 snprintf(buf, sizeof(buf), "set_pps SP/%s/pps.xml", fqdn);
2928 if (run_hs20_osu(dut, buf) < 0) {
2929 send_resp(dut, conn, SIGMA_ERROR,
2930 "errorCode,Failed to configure credential from "
2931 "PPSMO");
2932 return 0;
2933 }
2934
2935 return 1;
2936}
2937
2938
2939static int download_cert(struct sigma_dut *dut,
2940 struct sigma_conn *conn,
2941 const char *intf,
2942 struct sigma_cmd *cmd)
2943{
2944 const char *name, *path;
2945 char url[500], buf[600];
2946
2947 name = get_param(cmd, "FileName");
2948 path = get_param(cmd, "FilePath");
2949 if (name == NULL || path == NULL)
2950 return -1;
2951
2952 if (strcasecmp(path, "VendorSpecific") == 0) {
2953 snprintf(url, sizeof(url), "Certs/%s-cert.pem", name);
2954 sigma_dut_print(dut, DUT_MSG_INFO, "Use pre-configured client "
2955 "certificate from the device (%s)", url);
2956 if (!file_exists(url)) {
2957 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Requested "
2958 "certificate file does not exist");
2959 return 0;
2960 }
2961 snprintf(buf, sizeof(buf), "cp %s next-client-cert.pem", url);
2962 if (system(buf) != 0) {
2963 send_resp(dut, conn, SIGMA_ERROR,
2964 "errorCode,Failed to copy client "
2965 "certificate");
2966 return 0;
2967 }
2968
2969 snprintf(url, sizeof(url), "Certs/%s-key.pem", name);
2970 sigma_dut_print(dut, DUT_MSG_INFO, "Use pre-configured client "
2971 "private key from the device (%s)", url);
2972 if (!file_exists(url)) {
2973 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Requested "
2974 "private key file does not exist");
2975 return 0;
2976 }
2977 snprintf(buf, sizeof(buf), "cp %s next-client-key.pem", url);
2978 if (system(buf) != 0) {
2979 send_resp(dut, conn, SIGMA_ERROR,
2980 "errorCode,Failed to copy client key");
2981 return 0;
2982 }
2983 } else if (strncasecmp(path, "http:", 5) != 0 &&
2984 strncasecmp(path, "https:", 6) != 0) {
2985 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,"
2986 "Unsupported FilePath value");
2987 return 0;
2988 } else {
2989 snprintf(url, sizeof(url), "%s/%s.pem", path, name);
2990 sigma_dut_print(dut, DUT_MSG_INFO, "Downloading client "
2991 "certificate/key from %s", url);
2992 snprintf(buf, sizeof(buf),
2993 "wget -T 10 -t 3 -O next-client-cert.pem '%s'", url);
2994 if (system(buf) != 0) {
2995 send_resp(dut, conn, SIGMA_ERROR,
2996 "errorCode,Failed to download client "
2997 "certificate");
2998 return 0;
2999 }
3000
3001 if (system("cp next-client-cert.pem next-client-key.pem") != 0)
3002 {
3003 send_resp(dut, conn, SIGMA_ERROR,
3004 "errorCode,Failed to copy client key");
3005 return 0;
3006 }
3007 }
3008
3009 return 1;
3010}
3011
3012
3013static int cmd_sta_preset_testparameters_hs2_r2(struct sigma_dut *dut,
3014 struct sigma_conn *conn,
3015 const char *intf,
3016 struct sigma_cmd *cmd)
3017{
3018 const char *val;
3019
3020 val = get_param(cmd, "FileType");
3021 if (val && strcasecmp(val, "PPSMO") == 0)
3022 return download_ppsmo(dut, conn, intf, cmd);
3023 if (val && strcasecmp(val, "CERT") == 0)
3024 return download_cert(dut, conn, intf, cmd);
3025 if (val) {
3026 send_resp(dut, conn, SIGMA_ERROR,
3027 "ErrorCode,Unsupported FileType");
3028 return 0;
3029 }
3030
3031 return 1;
3032}
3033
3034
Ankita Bajaja2cb5672017-10-25 16:08:28 +05303035static int cmd_sta_preset_testparameters_oce(struct sigma_dut *dut,
3036 struct sigma_conn *conn,
3037 const char *intf,
3038 struct sigma_cmd *cmd)
3039{
3040 const char *val;
Ankita Bajaj1bde7942018-01-09 19:15:01 +05303041 char buf[1000];
3042 char text[20];
3043 unsigned char addr[ETH_ALEN];
Ankita Bajaja2cb5672017-10-25 16:08:28 +05303044
3045 val = get_param(cmd, "OCESupport");
3046 if (val && strcasecmp(val, "Disable") == 0) {
3047 if (wpa_command(intf, "SET oce 0") < 0) {
3048 send_resp(dut, conn, SIGMA_ERROR,
3049 "ErrorCode,Failed to disable OCE");
3050 return 0;
3051 }
3052 } else if (val && strcasecmp(val, "Enable") == 0) {
3053 if (wpa_command(intf, "SET oce 1") < 0) {
3054 send_resp(dut, conn, SIGMA_ERROR,
3055 "ErrorCode,Failed to enable OCE");
3056 return 0;
3057 }
3058 }
3059
vamsi krishnaa2799492017-12-05 14:28:01 +05303060 val = get_param(cmd, "FILScap");
3061 if (val && (atoi(val) == 1)) {
3062 if (wpa_command(intf, "SET disable_fils 0") < 0) {
3063 send_resp(dut, conn, SIGMA_ERROR,
3064 "ErrorCode,Failed to enable FILS");
3065 return 0;
3066 }
3067 } else if (val && (atoi(val) == 0)) {
3068 if (wpa_command(intf, "SET disable_fils 1") < 0) {
3069 send_resp(dut, conn, SIGMA_ERROR,
3070 "ErrorCode,Failed to disable FILS");
3071 return 0;
3072 }
3073 }
3074
Ankita Bajaj1bde7942018-01-09 19:15:01 +05303075 val = get_param(cmd, "FILSHLP");
3076 if (val && strcasecmp(val, "Enable") == 0) {
3077 if (get_wpa_status(get_station_ifname(), "address", text,
3078 sizeof(text)) < 0)
3079 return -2;
3080 hwaddr_aton(text, addr);
3081 snprintf(buf, sizeof(buf),
3082 "FILS_HLP_REQ_ADD ff:ff:ff:ff:ff:ff "
3083 "080045100140000040004011399e00000000ffffffff00440043"
3084 "012cb30001010600fd4f46410000000000000000000000000000"
3085 "000000000000"
3086 "%02x%02x%02x%02x%02x%02x"
3087 "0000000000000000000000000000000000000000000000000000"
3088 "0000000000000000000000000000000000000000000000000000"
3089 "0000000000000000000000000000000000000000000000000000"
3090 "0000000000000000000000000000000000000000000000000000"
3091 "0000000000000000000000000000000000000000000000000000"
3092 "0000000000000000000000000000000000000000000000000000"
3093 "0000000000000000000000000000000000000000000000000000"
3094 "0000000000000000000000000000000000000000638253633501"
3095 "013d0701000af549d29b390205dc3c12616e64726f69642d6468"
3096 "63702d382e302e30370a0103060f1a1c333a3b2b5000ff00",
3097 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
3098 if (wpa_command(intf, buf)) {
3099 send_resp(dut, conn, SIGMA_ERROR,
3100 "ErrorCode,Failed to add HLP");
3101 return 0;
3102 }
3103 dut->fils_hlp = 1;
3104 }
3105
Ankita Bajaja2cb5672017-10-25 16:08:28 +05303106 return 1;
3107}
3108
3109
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003110static void ath_sta_set_noack(struct sigma_dut *dut, const char *intf,
3111 const char *val)
3112{
3113 int counter = 0;
3114 char token[50];
3115 char *result;
3116 char buf[100];
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05303117 char *saveptr;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003118
Peng Xub8fc5cc2017-05-10 17:27:28 -07003119 strlcpy(token, val, sizeof(token));
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003120 token[sizeof(token) - 1] = '\0';
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05303121 result = strtok_r(token, ":", &saveptr);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003122 while (result) {
3123 if (strcmp(result, "disable") == 0) {
3124 snprintf(buf, sizeof(buf),
3125 "iwpriv %s noackpolicy %d 1 0",
3126 intf, counter);
3127 } else {
3128 snprintf(buf, sizeof(buf),
3129 "iwpriv %s noackpolicy %d 1 1",
3130 intf, counter);
3131 }
3132 if (system(buf) != 0) {
3133 sigma_dut_print(dut, DUT_MSG_ERROR,
3134 "iwpriv noackpolicy failed");
3135 }
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05303136 result = strtok_r(NULL, ":", &saveptr);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003137 counter++;
3138 }
3139}
3140
3141
3142static void ath_sta_set_rts(struct sigma_dut *dut, const char *intf,
3143 const char *val)
3144{
3145 char buf[100];
3146
3147 snprintf(buf, sizeof(buf), "iwconfig %s rts %s", intf, val);
3148 if (system(buf) != 0) {
3149 sigma_dut_print(dut, DUT_MSG_ERROR, "iwconfig RTS failed");
3150 }
3151}
3152
3153
3154static void ath_sta_set_wmm(struct sigma_dut *dut, const char *intf,
3155 const char *val)
3156{
3157 char buf[100];
3158
3159 if (strcasecmp(val, "off") == 0) {
3160 snprintf(buf, sizeof(buf), "iwpriv %s wmm 0", intf);
3161 if (system(buf) != 0) {
3162 sigma_dut_print(dut, DUT_MSG_ERROR,
3163 "Failed to turn off WMM");
3164 }
3165 }
3166}
3167
3168
Amarnath Hullur Subramanyam75214d22018-02-04 19:17:11 -08003169static int wcn_sta_set_wmm(struct sigma_dut *dut, const char *intf,
3170 const char *val)
3171{
3172#ifdef NL80211_SUPPORT
3173 struct nl_msg *msg;
3174 int ret = 0;
3175 struct nlattr *params;
3176 int ifindex;
3177 int wmmenable = 1;
3178
3179 if (val &&
3180 (strcasecmp(val, "off") == 0 || strcmp(val, "0") == 0))
3181 wmmenable = 0;
3182
3183 ifindex = if_nametoindex(intf);
3184 if (ifindex == 0) {
3185 sigma_dut_print(dut, DUT_MSG_ERROR,
3186 "%s: Index for interface %s failed",
3187 __func__, intf);
3188 return -1;
3189 }
3190
3191 if (!(msg = nl80211_drv_msg(dut, dut->nl_ctx, ifindex, 0,
3192 NL80211_CMD_VENDOR)) ||
3193 nla_put_u32(msg, NL80211_ATTR_IFINDEX, ifindex) ||
3194 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
3195 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
3196 QCA_NL80211_VENDOR_SUBCMD_WIFI_TEST_CONFIGURATION) ||
3197 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
3198 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_WIFI_TEST_CONFIG_WMM_ENABLE,
3199 wmmenable)) {
3200 sigma_dut_print(dut, DUT_MSG_ERROR,
3201 "%s: err in adding vendor_cmd and vendor_data",
3202 __func__);
3203 nlmsg_free(msg);
3204 return -1;
3205 }
3206 nla_nest_end(msg, params);
3207
3208 ret = send_and_recv_msgs(dut, dut->nl_ctx, msg, NULL, NULL);
3209 if (ret) {
3210 sigma_dut_print(dut, DUT_MSG_ERROR,
3211 "%s: err in send_and_recv_msgs, ret=%d",
3212 __func__, ret);
3213 }
3214 return ret;
3215#else /* NL80211_SUPPORT */
3216 sigma_dut_print(dut, DUT_MSG_ERROR,
3217 "WMM cannot be changed without NL80211_SUPPORT defined");
3218 return -1;
3219#endif /* NL80211_SUPPORT */
3220}
3221
3222
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003223static void ath_sta_set_sgi(struct sigma_dut *dut, const char *intf,
3224 const char *val)
3225{
3226 char buf[100];
3227 int sgi20;
3228
3229 sgi20 = strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0;
3230
3231 snprintf(buf, sizeof(buf), "iwpriv %s shortgi %d", intf, sgi20);
3232 if (system(buf) != 0)
3233 sigma_dut_print(dut, DUT_MSG_ERROR, "iwpriv shortgi failed");
3234}
3235
3236
3237static void ath_sta_set_11nrates(struct sigma_dut *dut, const char *intf,
3238 const char *val)
3239{
3240 char buf[100];
Pradeep Reddy POTTETI67376b72016-10-25 20:08:17 +05303241 int rate_code, v;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003242
3243 /* Disable Tx Beam forming when using a fixed rate */
3244 ath_disable_txbf(dut, intf);
3245
Pradeep Reddy POTTETI67376b72016-10-25 20:08:17 +05303246 v = atoi(val);
3247 if (v < 0 || v > 32) {
3248 sigma_dut_print(dut, DUT_MSG_ERROR,
3249 "Invalid Fixed MCS rate: %d", v);
3250 return;
3251 }
3252 rate_code = 0x80 + v;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003253
3254 snprintf(buf, sizeof(buf), "iwpriv %s set11NRates 0x%x",
3255 intf, rate_code);
3256 if (system(buf) != 0) {
3257 sigma_dut_print(dut, DUT_MSG_ERROR,
3258 "iwpriv set11NRates failed");
3259 }
3260
3261 /* Channel width gets messed up, fix this */
3262 snprintf(buf, sizeof(buf), "iwpriv %s chwidth %d", intf, dut->chwidth);
3263 if (system(buf) != 0)
3264 sigma_dut_print(dut, DUT_MSG_ERROR, "iwpriv chwidth failed");
3265}
3266
3267
Amarnath Hullur Subramanyamd5bb5732018-02-22 15:50:38 -08003268static void iwpriv_sta_set_amsdu(struct sigma_dut *dut, const char *intf,
3269 const char *val)
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003270{
3271 char buf[60];
3272
3273 if (strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0)
3274 snprintf(buf, sizeof(buf), "iwpriv %s amsdu 2", intf);
3275 else
3276 snprintf(buf, sizeof(buf), "iwpriv %s amsdu 1", intf);
3277
3278 if (system(buf) != 0)
3279 sigma_dut_print(dut, DUT_MSG_ERROR, "iwpriv amsdu failed");
3280}
3281
3282
Deepak Dhamdhere80356cb2016-03-28 16:48:32 -07003283static int iwpriv_sta_set_ampdu(struct sigma_dut *dut, const char *intf,
3284 int ampdu)
3285{
3286 char buf[60];
3287
3288 snprintf(buf, sizeof(buf), "iwpriv %s ampdu %d", intf, ampdu);
3289 if (system(buf) != 0) {
3290 sigma_dut_print(dut, DUT_MSG_ERROR, "iwpriv ampdu failed");
3291 return -1;
3292 }
3293
3294 return 0;
3295}
3296
3297
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003298static void ath_sta_set_stbc(struct sigma_dut *dut, const char *intf,
3299 const char *val)
3300{
3301 char buf[60];
3302
3303 snprintf(buf, sizeof(buf), "iwpriv %s tx_stbc %s", intf, val);
3304 if (system(buf) != 0) {
3305 sigma_dut_print(dut, DUT_MSG_ERROR,
3306 "iwpriv tx_stbc failed");
3307 }
3308
3309 snprintf(buf, sizeof(buf), "iwpriv %s rx_stbc %s", intf, val);
3310 if (system(buf) != 0) {
3311 sigma_dut_print(dut, DUT_MSG_ERROR,
3312 "iwpriv rx_stbc failed");
3313 }
3314}
3315
3316
3317static int wcn_sta_set_cts_width(struct sigma_dut *dut, const char *intf,
3318 const char *val)
3319{
3320 char buf[60];
3321
Peng Xucc317ed2017-05-18 16:44:37 -07003322 if (strcmp(val, "160") == 0) {
3323 snprintf(buf, sizeof(buf), "iwpriv %s cts_cbw 5", intf);
3324 } else if (strcmp(val, "80") == 0) {
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003325 snprintf(buf, sizeof(buf), "iwpriv %s cts_cbw 3", intf);
3326 } else if (strcmp(val, "40") == 0) {
3327 snprintf(buf, sizeof(buf), "iwpriv %s cts_cbw 2", intf);
3328 } else if (strcmp(val, "20") == 0) {
3329 snprintf(buf, sizeof(buf), "iwpriv %s cts_cbw 1", intf);
3330 } else if (strcasecmp(val, "Auto") == 0) {
3331 buf[0] = '\0';
3332 } else {
3333 sigma_dut_print(dut, DUT_MSG_ERROR,
3334 "WIDTH/CTS_WIDTH value not supported");
3335 return -1;
3336 }
3337
3338 if (buf[0] != '\0' && system(buf) != 0) {
3339 sigma_dut_print(dut, DUT_MSG_ERROR,
3340 "Failed to set WIDTH/CTS_WIDTH");
3341 return -1;
3342 }
3343
3344 return 0;
3345}
3346
3347
3348int ath_set_width(struct sigma_dut *dut, struct sigma_conn *conn,
3349 const char *intf, const char *val)
3350{
3351 char buf[60];
3352
3353 if (strcasecmp(val, "Auto") == 0) {
3354 snprintf(buf, sizeof(buf), "iwpriv %s chwidth 0", intf);
3355 dut->chwidth = 0;
3356 } else if (strcasecmp(val, "20") == 0) {
3357 snprintf(buf, sizeof(buf), "iwpriv %s chwidth 0", intf);
3358 dut->chwidth = 0;
3359 } else if (strcasecmp(val, "40") == 0) {
3360 snprintf(buf, sizeof(buf), "iwpriv %s chwidth 1", intf);
3361 dut->chwidth = 1;
3362 } else if (strcasecmp(val, "80") == 0) {
3363 snprintf(buf, sizeof(buf), "iwpriv %s chwidth 2", intf);
3364 dut->chwidth = 2;
3365 } else if (strcasecmp(val, "160") == 0) {
3366 snprintf(buf, sizeof(buf), "iwpriv %s chwidth 3", intf);
3367 dut->chwidth = 3;
3368 } else {
3369 send_resp(dut, conn, SIGMA_ERROR,
3370 "ErrorCode,WIDTH not supported");
3371 return -1;
3372 }
3373
3374 if (system(buf) != 0) {
3375 sigma_dut_print(dut, DUT_MSG_ERROR,
3376 "iwpriv chwidth failed");
3377 }
3378
3379 return 0;
3380}
3381
3382
3383static int wcn_sta_set_sp_stream(struct sigma_dut *dut, const char *intf,
3384 const char *val)
3385{
3386 char buf[60];
3387
Amarnath Hullur Subramanyamd5374fa2018-02-25 19:00:24 -08003388 if (strcmp(val, "1SS") == 0 || strcmp(val, "1") == 0) {
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003389 snprintf(buf, sizeof(buf), "iwpriv %s nss 1", intf);
Amarnath Hullur Subramanyamd5374fa2018-02-25 19:00:24 -08003390 } else if (strcmp(val, "2SS") == 0 || strcmp(val, "2") == 0) {
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003391 snprintf(buf, sizeof(buf), "iwpriv %s nss 2", intf);
3392 } else {
3393 sigma_dut_print(dut, DUT_MSG_ERROR,
3394 "SP_STREAM value not supported");
3395 return -1;
3396 }
3397
3398 if (system(buf) != 0) {
3399 sigma_dut_print(dut, DUT_MSG_ERROR,
3400 "Failed to set SP_STREAM");
3401 return -1;
3402 }
3403
3404 return 0;
3405}
3406
3407
Pradeep Reddy POTTETI4a1f6b32016-11-23 13:15:21 +05303408static void wcn_sta_set_stbc(struct sigma_dut *dut, const char *intf,
3409 const char *val)
3410{
3411 char buf[60];
3412
3413 snprintf(buf, sizeof(buf), "iwpriv %s tx_stbc %s", intf, val);
3414 if (system(buf) != 0)
3415 sigma_dut_print(dut, DUT_MSG_ERROR, "iwpriv tx_stbc failed");
3416
3417 snprintf(buf, sizeof(buf), "iwpriv %s rx_stbc %s", intf, val);
3418 if (system(buf) != 0)
3419 sigma_dut_print(dut, DUT_MSG_ERROR, "iwpriv rx_stbc failed");
3420}
3421
3422
Ashwini Patil68d02cd2017-01-10 15:39:16 +05303423static int mbo_set_cellular_data_capa(struct sigma_dut *dut,
3424 struct sigma_conn *conn,
3425 const char *intf, int capa)
3426{
3427 char buf[32];
3428
3429 if (capa > 0 && capa < 4) {
3430 snprintf(buf, sizeof(buf), "SET mbo_cell_capa %d", capa);
3431 if (wpa_command(intf, buf) < 0) {
3432 send_resp(dut, conn, SIGMA_ERROR,
3433 "ErrorCode, Failed to set cellular data capability");
3434 return 0;
3435 }
3436 return 1;
3437 }
3438
3439 sigma_dut_print(dut, DUT_MSG_ERROR,
3440 "Invalid Cellular data capability: %d", capa);
3441 send_resp(dut, conn, SIGMA_INVALID,
3442 "ErrorCode,Invalid cellular data capability");
3443 return 0;
3444}
3445
3446
Ashwini Patil9183fdb2017-04-13 16:58:25 +05303447static int mbo_set_roaming(struct sigma_dut *dut, struct sigma_conn *conn,
3448 const char *intf, const char *val)
3449{
3450 if (strcasecmp(val, "Disable") == 0) {
3451 if (wpa_command(intf, "SET roaming 0") < 0) {
3452 send_resp(dut, conn, SIGMA_ERROR,
3453 "ErrorCode,Failed to disable roaming");
3454 return 0;
3455 }
3456 return 1;
3457 }
3458
3459 if (strcasecmp(val, "Enable") == 0) {
3460 if (wpa_command(intf, "SET roaming 1") < 0) {
3461 send_resp(dut, conn, SIGMA_ERROR,
3462 "ErrorCode,Failed to enable roaming");
3463 return 0;
3464 }
3465 return 1;
3466 }
3467
3468 sigma_dut_print(dut, DUT_MSG_ERROR,
3469 "Invalid value provided for roaming: %s", val);
3470 send_resp(dut, conn, SIGMA_INVALID,
3471 "ErrorCode,Unknown value provided for Roaming");
3472 return 0;
3473}
3474
3475
Ashwini Patila75de5a2017-04-13 16:35:05 +05303476static int mbo_set_assoc_disallow(struct sigma_dut *dut,
3477 struct sigma_conn *conn,
3478 const char *intf, const char *val)
3479{
3480 if (strcasecmp(val, "Disable") == 0) {
3481 if (wpa_command(intf, "SET ignore_assoc_disallow 1") < 0) {
3482 send_resp(dut, conn, SIGMA_ERROR,
3483 "ErrorCode,Failed to disable Assoc_disallow");
3484 return 0;
3485 }
3486 return 1;
3487 }
3488
3489 if (strcasecmp(val, "Enable") == 0) {
3490 if (wpa_command(intf, "SET ignore_assoc_disallow 0") < 0) {
3491 send_resp(dut, conn, SIGMA_ERROR,
3492 "ErrorCode,Failed to enable Assoc_disallow");
3493 return 0;
3494 }
3495 return 1;
3496 }
3497
3498 sigma_dut_print(dut, DUT_MSG_ERROR,
3499 "Invalid value provided for Assoc_disallow: %s", val);
3500 send_resp(dut, conn, SIGMA_INVALID,
3501 "ErrorCode,Unknown value provided for Assoc_disallow");
3502 return 0;
3503}
3504
3505
Ashwini Patilc63161e2017-04-13 16:30:23 +05303506static int mbo_set_bss_trans_req(struct sigma_dut *dut, struct sigma_conn *conn,
3507 const char *intf, const char *val)
3508{
3509 if (strcasecmp(val, "Reject") == 0) {
3510 if (wpa_command(intf, "SET reject_btm_req_reason 1") < 0) {
3511 send_resp(dut, conn, SIGMA_ERROR,
3512 "ErrorCode,Failed to Reject BTM Request");
3513 return 0;
3514 }
3515 return 1;
3516 }
3517
3518 if (strcasecmp(val, "Accept") == 0) {
3519 if (wpa_command(intf, "SET reject_btm_req_reason 0") < 0) {
3520 send_resp(dut, conn, SIGMA_ERROR,
3521 "ErrorCode,Failed to Accept BTM Request");
3522 return 0;
3523 }
3524 return 1;
3525 }
3526
3527 sigma_dut_print(dut, DUT_MSG_ERROR,
3528 "Invalid value provided for BSS_Transition: %s", val);
3529 send_resp(dut, conn, SIGMA_INVALID,
3530 "ErrorCode,Unknown value provided for BSS_Transition");
3531 return 0;
3532}
3533
3534
Ashwini Patil00402582017-04-13 12:29:39 +05303535static int mbo_set_non_pref_ch_list(struct sigma_dut *dut,
3536 struct sigma_conn *conn,
3537 const char *intf,
3538 struct sigma_cmd *cmd)
3539{
3540 const char *ch, *pref, *op_class, *reason;
3541 char buf[120];
3542 int len, ret;
3543
3544 pref = get_param(cmd, "Ch_Pref");
3545 if (!pref)
3546 return 1;
3547
3548 if (strcasecmp(pref, "clear") == 0) {
3549 free(dut->non_pref_ch_list);
3550 dut->non_pref_ch_list = NULL;
3551 } else {
3552 op_class = get_param(cmd, "Ch_Op_Class");
3553 if (!op_class) {
3554 send_resp(dut, conn, SIGMA_INVALID,
3555 "ErrorCode,Ch_Op_Class not provided");
3556 return 0;
3557 }
3558
3559 ch = get_param(cmd, "Ch_Pref_Num");
3560 if (!ch) {
3561 send_resp(dut, conn, SIGMA_INVALID,
3562 "ErrorCode,Ch_Pref_Num not provided");
3563 return 0;
3564 }
3565
3566 reason = get_param(cmd, "Ch_Reason_Code");
3567 if (!reason) {
3568 send_resp(dut, conn, SIGMA_INVALID,
3569 "ErrorCode,Ch_Reason_Code not provided");
3570 return 0;
3571 }
3572
3573 if (!dut->non_pref_ch_list) {
3574 dut->non_pref_ch_list =
3575 calloc(1, NON_PREF_CH_LIST_SIZE);
3576 if (!dut->non_pref_ch_list) {
3577 send_resp(dut, conn, SIGMA_ERROR,
3578 "ErrorCode,Failed to allocate memory for non_pref_ch_list");
3579 return 0;
3580 }
3581 }
3582 len = strlen(dut->non_pref_ch_list);
3583 ret = snprintf(dut->non_pref_ch_list + len,
3584 NON_PREF_CH_LIST_SIZE - len,
3585 " %s:%s:%s:%s", op_class, ch, pref, reason);
3586 if (ret > 0 && ret < NON_PREF_CH_LIST_SIZE - len) {
3587 sigma_dut_print(dut, DUT_MSG_DEBUG, "non_pref_list: %s",
3588 dut->non_pref_ch_list);
3589 } else {
3590 sigma_dut_print(dut, DUT_MSG_ERROR,
3591 "snprintf failed for non_pref_list, ret = %d",
3592 ret);
3593 send_resp(dut, conn, SIGMA_ERROR,
3594 "ErrorCode,snprintf failed");
3595 free(dut->non_pref_ch_list);
3596 dut->non_pref_ch_list = NULL;
3597 return 0;
3598 }
3599 }
3600
3601 ret = snprintf(buf, sizeof(buf), "SET non_pref_chan%s",
3602 dut->non_pref_ch_list ? dut->non_pref_ch_list : " ");
3603 if (ret < 0 || ret >= (int) sizeof(buf)) {
3604 sigma_dut_print(dut, DUT_MSG_DEBUG,
3605 "snprintf failed for set non_pref_chan, ret: %d",
3606 ret);
3607 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,snprint failed");
3608 return 0;
3609 }
3610
3611 if (wpa_command(intf, buf) < 0) {
3612 send_resp(dut, conn, SIGMA_ERROR,
3613 "ErrorCode,Failed to set non-preferred channel list");
3614 return 0;
3615 }
3616
3617 return 1;
3618}
3619
3620
Amarnath Hullur Subramanyam474a17d2018-02-22 18:45:54 -08003621#ifdef NL80211_SUPPORT
3622static int sta_set_he_fragmentation(struct sigma_dut *dut, const char *intf,
3623 enum he_fragmentation_val frag)
3624{
3625 struct nl_msg *msg;
3626 int ret = 0;
3627 struct nlattr *params;
3628 int ifindex;
3629
3630 ifindex = if_nametoindex(intf);
3631 if (ifindex == 0) {
3632 sigma_dut_print(dut, DUT_MSG_ERROR,
3633 "%s: Index for interface %s failed",
3634 __func__, intf);
3635 return -1;
3636 }
3637
3638 if (!(msg = nl80211_drv_msg(dut, dut->nl_ctx, ifindex, 0,
3639 NL80211_CMD_VENDOR)) ||
3640 nla_put_u32(msg, NL80211_ATTR_IFINDEX, ifindex) ||
3641 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
3642 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
3643 QCA_NL80211_VENDOR_SUBCMD_WIFI_TEST_CONFIGURATION) ||
3644 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
3645 nla_put_u8(msg,
3646 QCA_WLAN_VENDOR_ATTR_WIFI_TEST_CONFIG_HE_FRAGMENTATION,
3647 frag)) {
3648 sigma_dut_print(dut, DUT_MSG_ERROR,
3649 "%s: err in adding vendor_cmd and vendor_data",
3650 __func__);
3651 nlmsg_free(msg);
3652 return -1;
3653 }
3654 nla_nest_end(msg, params);
3655
3656 ret = send_and_recv_msgs(dut, dut->nl_ctx, msg, NULL, NULL);
3657 if (ret) {
3658 sigma_dut_print(dut, DUT_MSG_ERROR,
3659 "%s: err in send_and_recv_msgs, ret=%d",
3660 __func__, ret);
3661 }
3662 return ret;
3663}
3664#endif /* NL80211_SUPPORT */
3665
3666
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003667static int cmd_sta_preset_testparameters(struct sigma_dut *dut,
3668 struct sigma_conn *conn,
3669 struct sigma_cmd *cmd)
3670{
3671 const char *intf = get_param(cmd, "Interface");
3672 const char *val;
3673
3674 val = get_param(cmd, "Program");
Peng Xue9fa7952017-05-09 15:59:49 -07003675 if (val && strcasecmp(val, "HS2-R2") == 0)
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003676 return cmd_sta_preset_testparameters_hs2_r2(dut, conn, intf,
3677 cmd);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003678
priyadharshini gowthamand66913a2016-07-29 15:11:17 -07003679 if (val && strcasecmp(val, "LOC") == 0)
3680 return loc_cmd_sta_preset_testparameters(dut, conn, cmd);
3681
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003682#ifdef ANDROID_NAN
3683 if (val && strcasecmp(val, "NAN") == 0)
3684 return nan_cmd_sta_preset_testparameters(dut, conn, cmd);
3685#endif /* ANDROID_NAN */
Amarnath Hullur Subramanyam9c381f52017-03-17 00:04:41 -07003686#ifdef MIRACAST
3687 if (val && (strcasecmp(val, "WFD") == 0 ||
3688 strcasecmp(val, "DisplayR2") == 0))
3689 return miracast_preset_testparameters(dut, conn, cmd);
3690#endif /* MIRACAST */
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003691
Ashwini Patil68d02cd2017-01-10 15:39:16 +05303692 if (val && strcasecmp(val, "MBO") == 0) {
3693 val = get_param(cmd, "Cellular_Data_Cap");
3694 if (val &&
3695 mbo_set_cellular_data_capa(dut, conn, intf, atoi(val)) == 0)
3696 return 0;
Ashwini Patil00402582017-04-13 12:29:39 +05303697
3698 val = get_param(cmd, "Ch_Pref");
3699 if (val && mbo_set_non_pref_ch_list(dut, conn, intf, cmd) == 0)
3700 return 0;
3701
Ashwini Patilc63161e2017-04-13 16:30:23 +05303702 val = get_param(cmd, "BSS_Transition");
3703 if (val && mbo_set_bss_trans_req(dut, conn, intf, val) == 0)
3704 return 0;
3705
Ashwini Patila75de5a2017-04-13 16:35:05 +05303706 val = get_param(cmd, "Assoc_Disallow");
3707 if (val && mbo_set_assoc_disallow(dut, conn, intf, val) == 0)
3708 return 0;
3709
Ashwini Patil9183fdb2017-04-13 16:58:25 +05303710 val = get_param(cmd, "Roaming");
3711 if (val && mbo_set_roaming(dut, conn, intf, val) == 0)
3712 return 0;
3713
Ashwini Patil68d02cd2017-01-10 15:39:16 +05303714 return 1;
3715 }
3716
Ankita Bajaja2cb5672017-10-25 16:08:28 +05303717 if (val && strcasecmp(val, "OCE") == 0)
3718 return cmd_sta_preset_testparameters_oce(dut, conn, intf, cmd);
3719
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003720#if 0
3721 val = get_param(cmd, "Supplicant");
3722 if (val && strcasecmp(val, "Default") != 0) {
3723 send_resp(dut, conn, SIGMA_ERROR,
3724 "ErrorCode,Only default(Vendor) supplicant "
3725 "supported");
3726 return 0;
3727 }
3728#endif
3729
3730 val = get_param(cmd, "RTS");
3731 if (val) {
3732 switch (get_driver_type()) {
3733 case DRIVER_ATHEROS:
3734 ath_sta_set_rts(dut, intf, val);
3735 break;
3736 default:
3737#if 0
3738 send_resp(dut, conn, SIGMA_ERROR,
3739 "ErrorCode,Setting RTS not supported");
3740 return 0;
3741#else
3742 sigma_dut_print(dut, DUT_MSG_DEBUG,
3743 "Setting RTS not supported");
3744 break;
3745#endif
3746 }
3747 }
3748
3749#if 0
3750 val = get_param(cmd, "FRGMNT");
3751 if (val) {
3752 /* TODO */
3753 send_resp(dut, conn, SIGMA_ERROR,
3754 "ErrorCode,Setting FRGMNT not supported");
3755 return 0;
3756 }
3757#endif
3758
3759#if 0
3760 val = get_param(cmd, "Preamble");
3761 if (val) {
3762 /* TODO: Long/Short */
3763 send_resp(dut, conn, SIGMA_ERROR,
3764 "ErrorCode,Setting Preamble not supported");
3765 return 0;
3766 }
3767#endif
3768
3769 val = get_param(cmd, "Mode");
3770 if (val) {
3771 if (strcmp(val, "11b") == 0 ||
3772 strcmp(val, "11g") == 0 ||
3773 strcmp(val, "11a") == 0 ||
3774 strcmp(val, "11n") == 0 ||
3775 strcmp(val, "11ng") == 0 ||
3776 strcmp(val, "11nl") == 0 ||
3777 strcmp(val, "11nl(nabg)") == 0 ||
3778 strcmp(val, "AC") == 0 ||
3779 strcmp(val, "11AC") == 0 ||
3780 strcmp(val, "11ac") == 0 ||
3781 strcmp(val, "11na") == 0 ||
Amarnath Hullur Subramanyamb0db2712018-01-30 19:40:35 -08003782 strcmp(val, "11an") == 0 ||
3783 strcmp(val, "11ax") == 0) {
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003784 /* STA supports all modes by default */
3785 } else {
3786 send_resp(dut, conn, SIGMA_ERROR,
3787 "ErrorCode,Setting Mode not supported");
3788 return 0;
3789 }
Amarnath Hullur Subramanyam97d0e532018-01-31 02:53:02 -08003790
3791 /* Change the mode only in case of testbed for HE program
3792 * and for 11a and 11g modes only. */
3793 if (dut->program == PROGRAM_HE &&
3794 dut->device_type == STA_testbed) {
3795 int phymode;
3796 char buf[60];
3797
3798 if (strcmp(val, "11a") == 0) {
3799 phymode = 1;
3800 } else if (strcmp (val, "11g") == 0) {
3801 phymode = 3;
3802 } else {
3803 sigma_dut_print(dut, DUT_MSG_DEBUG,
3804 "Ignoring mode change for mode: %s",
3805 val);
3806 phymode = -1;
3807 }
3808 if (phymode != -1) {
3809 snprintf(buf, sizeof(buf),
3810 "iwpriv %s setphymode %d",
3811 intf, phymode);
3812 if (system(buf) != 0) {
3813 sigma_dut_print(dut, DUT_MSG_ERROR,
3814 "iwpriv setting of phymode failed");
3815 }
3816 }
3817 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003818 }
3819
3820 val = get_param(cmd, "wmm");
3821 if (val) {
3822 switch (get_driver_type()) {
3823 case DRIVER_ATHEROS:
3824 ath_sta_set_wmm(dut, intf, val);
3825 break;
Amarnath Hullur Subramanyam75214d22018-02-04 19:17:11 -08003826 case DRIVER_WCN:
3827 wcn_sta_set_wmm(dut, intf, val);
3828 break;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003829 default:
3830 sigma_dut_print(dut, DUT_MSG_DEBUG,
3831 "Setting wmm not supported");
3832 break;
3833 }
3834 }
3835
3836 val = get_param(cmd, "Powersave");
3837 if (val) {
3838 if (strcmp(val, "0") == 0 || strcasecmp(val, "off") == 0) {
3839 if (wpa_command(get_station_ifname(),
3840 "P2P_SET ps 0") < 0)
3841 return -2;
3842 /* Make sure test modes are disabled */
3843 wpa_command(get_station_ifname(), "P2P_SET ps 98");
3844 wpa_command(get_station_ifname(), "P2P_SET ps 96");
3845 } else if (strcmp(val, "1") == 0 ||
3846 strcasecmp(val, "PSPoll") == 0 ||
3847 strcasecmp(val, "on") == 0) {
3848 /* Disable default power save mode */
3849 wpa_command(get_station_ifname(), "P2P_SET ps 0");
3850 /* Enable PS-Poll test mode */
3851 if (wpa_command(get_station_ifname(),
3852 "P2P_SET ps 97") < 0 ||
3853 wpa_command(get_station_ifname(),
3854 "P2P_SET ps 99") < 0)
3855 return -2;
3856 } else if (strcmp(val, "2") == 0 ||
3857 strcasecmp(val, "Fast") == 0) {
3858 /* TODO */
3859 send_resp(dut, conn, SIGMA_ERROR,
3860 "ErrorCode,Powersave=Fast not supported");
3861 return 0;
3862 } else if (strcmp(val, "3") == 0 ||
3863 strcasecmp(val, "PSNonPoll") == 0) {
3864 /* Make sure test modes are disabled */
3865 wpa_command(get_station_ifname(), "P2P_SET ps 98");
3866 wpa_command(get_station_ifname(), "P2P_SET ps 96");
3867
3868 /* Enable default power save mode */
3869 if (wpa_command(get_station_ifname(),
3870 "P2P_SET ps 1") < 0)
3871 return -2;
3872 } else
3873 return -1;
3874 }
3875
3876 val = get_param(cmd, "NoAck");
3877 if (val) {
3878 switch (get_driver_type()) {
3879 case DRIVER_ATHEROS:
3880 ath_sta_set_noack(dut, intf, val);
3881 break;
3882 default:
3883 send_resp(dut, conn, SIGMA_ERROR,
3884 "ErrorCode,Setting NoAck not supported");
3885 return 0;
3886 }
3887 }
3888
3889 val = get_param(cmd, "IgnoreChswitchProhibit");
3890 if (val) {
3891 /* TODO: Enabled/disabled */
3892 if (strcasecmp(val, "Enabled") == 0) {
3893 send_resp(dut, conn, SIGMA_ERROR,
3894 "ErrorCode,Enabling IgnoreChswitchProhibit "
3895 "not supported");
3896 return 0;
3897 }
3898 }
3899
3900 val = get_param(cmd, "TDLS");
3901 if (val) {
3902 if (strcasecmp(val, "Disabled") == 0) {
3903 if (wpa_command(intf, "SET tdls_disabled 1")) {
3904 send_resp(dut, conn, SIGMA_ERROR,
3905 "ErrorCode,Failed to disable TDLS");
3906 return 0;
3907 }
3908 } else if (strcasecmp(val, "Enabled") == 0) {
3909 if (wpa_command(intf, "SET tdls_disabled 0")) {
3910 send_resp(dut, conn, SIGMA_ERROR,
3911 "ErrorCode,Failed to enable TDLS");
3912 return 0;
3913 }
3914 } else {
3915 send_resp(dut, conn, SIGMA_ERROR,
3916 "ErrorCode,Unsupported TDLS value");
3917 return 0;
3918 }
3919 }
3920
3921 val = get_param(cmd, "TDLSmode");
3922 if (val) {
3923 if (strcasecmp(val, "Default") == 0) {
3924 wpa_command(intf, "SET tdls_testing 0");
3925 } else if (strcasecmp(val, "APProhibit") == 0) {
3926 if (wpa_command(intf, "SET tdls_testing 0x400")) {
3927 send_resp(dut, conn, SIGMA_ERROR,
3928 "ErrorCode,Failed to enable ignore "
3929 "APProhibit TDLS mode");
3930 return 0;
3931 }
3932 } else if (strcasecmp(val, "HiLoMac") == 0) {
3933 /* STA should respond with TDLS setup req for a TDLS
3934 * setup req */
3935 if (wpa_command(intf, "SET tdls_testing 0x80")) {
3936 send_resp(dut, conn, SIGMA_ERROR,
3937 "ErrorCode,Failed to enable HiLoMac "
3938 "TDLS mode");
3939 return 0;
3940 }
3941 } else if (strcasecmp(val, "WeakSecurity") == 0) {
3942 /*
3943 * Since all security modes are enabled by default when
3944 * Sigma control is used, there is no need to do
3945 * anything here.
3946 */
3947 } else if (strcasecmp(val, "ExistLink") == 0) {
3948 /*
3949 * Since we allow new TDLS Setup Request even if there
3950 * is an existing link, nothing needs to be done for
3951 * this.
3952 */
3953 } else {
3954 /* TODO:
3955 * ExistLink: STA should send TDLS setup req even if
3956 * direct link already exists
3957 */
3958 send_resp(dut, conn, SIGMA_ERROR,
3959 "ErrorCode,Unsupported TDLSmode value");
3960 return 0;
3961 }
3962 }
3963
3964 val = get_param(cmd, "FakePubKey");
3965 if (val && atoi(val) && wpa_command(intf, "SET wps_corrupt_pkhash 1")) {
3966 send_resp(dut, conn, SIGMA_ERROR,
3967 "ErrorCode,Failed to enable FakePubKey");
3968 return 0;
3969 }
3970
Amarnath Hullur Subramanyamae1042b2018-02-22 21:52:52 -08003971#ifdef NL80211_SUPPORT
3972 val = get_param(cmd, "FrgmntSupport");
3973 if (val) {
3974 if (strcasecmp(val, "Enable") == 0) {
3975 if (sta_set_he_fragmentation(dut, intf,
3976 HE_FRAG_LEVEL1)) {
3977 send_resp(dut, conn, SIGMA_ERROR,
3978 "ErrorCode,Failed to enable HE Fragmentation");
3979 return 0;
3980 }
3981 } else if (strcasecmp(val, "Disable") == 0) {
3982 if (sta_set_he_fragmentation(dut, intf,
3983 HE_FRAG_DISABLE)) {
3984 send_resp(dut, conn, SIGMA_ERROR,
3985 "ErrorCode,Failed to disable HE Fragmentation");
3986 return 0;
3987 }
3988 }
3989 }
3990#endif /* NL80211_SUPPORT */
3991
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003992 return 1;
3993}
3994
3995
3996static const char * ath_get_radio_name(const char *radio_name)
3997{
3998 if (radio_name == NULL)
3999 return "wifi0";
4000 if (strcmp(radio_name, "wifi1") == 0)
4001 return "wifi1";
4002 if (strcmp(radio_name, "wifi2") == 0)
4003 return "wifi2";
4004 return "wifi0";
4005}
4006
4007
4008static void ath_sta_set_txsp_stream(struct sigma_dut *dut, const char *intf,
4009 const char *val)
4010{
4011 char buf[60];
4012 unsigned int vht_mcsmap = 0;
4013 int txchainmask = 0;
4014 const char *basedev = ath_get_radio_name(sigma_radio_ifname[0]);
4015
4016 if (strcasecmp(val, "1") == 0 || strcasecmp(val, "1SS") == 0) {
4017 if (dut->testbed_flag_txsp == 1) {
4018 vht_mcsmap = 0xfffc;
4019 dut->testbed_flag_txsp = 0;
4020 } else {
4021 vht_mcsmap = 0xfffe;
4022 }
4023 txchainmask = 1;
4024 } else if (strcasecmp(val, "2") == 0 || strcasecmp(val, "2SS") == 0) {
4025 if (dut->testbed_flag_txsp == 1) {
4026 vht_mcsmap = 0xfff0;
4027 dut->testbed_flag_txsp = 0;
4028 } else {
4029 vht_mcsmap = 0xfffa;
4030 }
4031 txchainmask = 3;
4032 } else if (strcasecmp(val, "3") == 0 || strcasecmp(val, "3SS") == 0) {
4033 if (dut->testbed_flag_txsp == 1) {
4034 vht_mcsmap = 0xffc0;
4035 dut->testbed_flag_txsp = 0;
4036 } else {
4037 vht_mcsmap = 0xffea;
4038 }
4039 txchainmask = 7;
4040 } else if (strcasecmp(val, "4") == 0 || strcasecmp(val, "4SS") == 0) {
4041 if (dut->testbed_flag_txsp == 1) {
4042 vht_mcsmap = 0xff00;
4043 dut->testbed_flag_txsp = 0;
4044 } else {
4045 vht_mcsmap = 0xffaa;
4046 }
4047 txchainmask = 15;
4048 } else {
4049 if (dut->testbed_flag_txsp == 1) {
4050 vht_mcsmap = 0xffc0;
4051 dut->testbed_flag_txsp = 0;
4052 } else {
4053 vht_mcsmap = 0xffea;
4054 }
4055 }
4056
4057 if (txchainmask) {
4058 snprintf(buf, sizeof(buf), "iwpriv %s txchainmask %d",
4059 basedev, txchainmask);
4060 if (system(buf) != 0) {
4061 sigma_dut_print(dut, DUT_MSG_ERROR,
4062 "iwpriv txchainmask failed");
4063 }
4064 }
4065
4066 snprintf(buf, sizeof(buf), "iwpriv %s vht_mcsmap 0x%04x",
4067 intf, vht_mcsmap);
4068 if (system(buf) != 0) {
4069 sigma_dut_print(dut, DUT_MSG_ERROR,
4070 "iwpriv %s vht_mcsmap 0x%04x failed",
4071 intf, vht_mcsmap);
4072 }
4073}
4074
4075
4076static void ath_sta_set_rxsp_stream(struct sigma_dut *dut, const char *intf,
4077 const char *val)
4078{
4079 char buf[60];
4080 unsigned int vht_mcsmap = 0;
4081 int rxchainmask = 0;
4082 const char *basedev = ath_get_radio_name(sigma_radio_ifname[0]);
4083
4084 if (strcasecmp(val, "1") == 0 || strcasecmp(val, "1SS") == 0) {
4085 if (dut->testbed_flag_rxsp == 1) {
4086 vht_mcsmap = 0xfffc;
4087 dut->testbed_flag_rxsp = 0;
4088 } else {
4089 vht_mcsmap = 0xfffe;
4090 }
4091 rxchainmask = 1;
4092 } else if (strcasecmp(val, "2") == 0 || strcasecmp(val, "2SS") == 0) {
4093 if (dut->testbed_flag_rxsp == 1) {
4094 vht_mcsmap = 0xfff0;
4095 dut->testbed_flag_rxsp = 0;
4096 } else {
4097 vht_mcsmap = 0xfffa;
4098 }
4099 rxchainmask = 3;
4100 } else if (strcasecmp(val, "3") == 0 || strcasecmp(val, "3SS") == 0) {
4101 if (dut->testbed_flag_rxsp == 1) {
4102 vht_mcsmap = 0xffc0;
4103 dut->testbed_flag_rxsp = 0;
4104 } else {
4105 vht_mcsmap = 0xffea;
4106 }
4107 rxchainmask = 7;
4108 } else if (strcasecmp(val, "4") == 0 || strcasecmp(val, "4SS") == 0) {
4109 if (dut->testbed_flag_rxsp == 1) {
4110 vht_mcsmap = 0xff00;
4111 dut->testbed_flag_rxsp = 0;
4112 } else {
4113 vht_mcsmap = 0xffaa;
4114 }
4115 rxchainmask = 15;
4116 } else {
4117 if (dut->testbed_flag_rxsp == 1) {
4118 vht_mcsmap = 0xffc0;
4119 dut->testbed_flag_rxsp = 0;
4120 } else {
4121 vht_mcsmap = 0xffea;
4122 }
4123 }
4124
4125 if (rxchainmask) {
4126 snprintf(buf, sizeof(buf), "iwpriv %s rxchainmask %d",
4127 basedev, rxchainmask);
4128 if (system(buf) != 0) {
4129 sigma_dut_print(dut, DUT_MSG_ERROR,
4130 "iwpriv rxchainmask failed");
4131 }
4132 }
4133
4134 snprintf(buf, sizeof(buf), "iwpriv %s vht_mcsmap 0x%04x",
4135 intf, vht_mcsmap);
4136 if (system(buf) != 0) {
4137 sigma_dut_print(dut, DUT_MSG_ERROR,
4138 "iwpriv %s vht_mcsmap 0x%04x",
4139 intf, vht_mcsmap);
4140 }
4141}
4142
4143
4144void ath_set_zero_crc(struct sigma_dut *dut, const char *val)
4145{
4146 if (strcasecmp(val, "enable") == 0) {
4147 if (system("athdiag --set --address=0x2a204 --and=0xbfffffff")
4148 != 0) {
4149 sigma_dut_print(dut, DUT_MSG_ERROR,
4150 "Disable BB_VHTSIGB_CRC_CALC failed");
4151 }
4152
4153 if (system("athdiag --set --address=0x2a204 --or=0x80000000")
4154 != 0) {
4155 sigma_dut_print(dut, DUT_MSG_ERROR,
4156 "Enable FORCE_VHT_SIGB_CRC_VALUE_ZERO failed");
4157 }
4158 } else {
4159 if (system("athdiag --set --address=0x2a204 --and=0x7fffffff")
4160 != 0) {
4161 sigma_dut_print(dut, DUT_MSG_ERROR,
4162 "Disable FORCE_VHT_SIGB_CRC_VALUE_ZERO failed");
4163 }
4164
4165 if (system("athdiag --set --address=0x2a204 --or=0x40000000")
4166 != 0) {
4167 sigma_dut_print(dut, DUT_MSG_ERROR,
4168 "Enable BB_VHTSIGB_CRC_CALC failed");
4169 }
4170 }
4171}
4172
4173
Amarnath Hullur Subramanyamebfe6b62018-01-31 03:04:17 -08004174static int wcn_sta_set_width(struct sigma_dut *dut, const char *intf,
4175 const char *val)
4176{
4177 char buf[60];
4178
4179 if (strcmp(val, "20") == 0) {
4180 snprintf(buf, sizeof(buf), "iwpriv %s chwidth 0", intf);
4181 dut->chwidth = 0;
4182 } else if (strcmp(val, "40") == 0) {
4183 snprintf(buf, sizeof(buf), "iwpriv %s chwidth 1", intf);
4184 dut->chwidth = 1;
4185 } else if (strcmp(val, "80") == 0) {
4186 snprintf(buf, sizeof(buf), "iwpriv %s chwidth 2", intf);
4187 dut->chwidth = 2;
4188 } else if (strcmp(val, "Auto") == 0) {
4189 buf[0] = '\0';
4190 } else {
4191 sigma_dut_print(dut, DUT_MSG_ERROR, "WIDTH %s not supported",
4192 val);
4193 return -1;
4194 }
4195
4196 if (buf[0] != '\0' && system(buf) != 0) {
4197 sigma_dut_print(dut, DUT_MSG_ERROR, "iwpriv chwidth failed");
4198 return -1;
4199 }
4200
4201 return 0;
4202}
4203
4204
Amarnath Hullur Subramanyamd8a9db92018-02-02 18:53:14 -08004205static int nlvendor_sta_set_addba_reject(struct sigma_dut *dut,
4206 const char *intf, int addbareject)
4207{
4208#ifdef NL80211_SUPPORT
4209 struct nl_msg *msg;
4210 int ret = 0;
4211 struct nlattr *params;
4212 int ifindex;
4213
4214 ifindex = if_nametoindex(intf);
4215 if (ifindex == 0) {
4216 sigma_dut_print(dut, DUT_MSG_ERROR,
4217 "%s: Index for interface %s failed",
4218 __func__, intf);
4219 return -1;
4220 }
4221
4222 if (!(msg = nl80211_drv_msg(dut, dut->nl_ctx, ifindex, 0,
4223 NL80211_CMD_VENDOR)) ||
4224 nla_put_u32(msg, NL80211_ATTR_IFINDEX, ifindex) ||
4225 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
4226 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
4227 QCA_NL80211_VENDOR_SUBCMD_WIFI_TEST_CONFIGURATION) ||
4228 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
4229 nla_put_u8(msg,
4230 QCA_WLAN_VENDOR_ATTR_WIFI_TEST_CONFIG_ACCEPT_ADDBA_REQ,
4231 !addbareject)) {
4232 sigma_dut_print(dut, DUT_MSG_ERROR,
4233 "%s: err in adding vendor_cmd and vendor_data",
4234 __func__);
4235 nlmsg_free(msg);
4236 return -1;
4237 }
4238 nla_nest_end(msg, params);
4239
4240 ret = send_and_recv_msgs(dut, dut->nl_ctx, msg, NULL, NULL);
4241 if (ret) {
4242 sigma_dut_print(dut, DUT_MSG_ERROR,
4243 "%s: err in send_and_recv_msgs, ret=%d",
4244 __func__, ret);
4245 }
4246 return ret;
4247#else /* NL80211_SUPPORT */
4248 sigma_dut_print(dut, DUT_MSG_ERROR,
4249 "ADDBA_REJECT cannot be set without NL80211_SUPPORT defined");
4250 return -1;
4251#endif /* NL80211_SUPPORT */
4252}
4253
4254
4255static int sta_set_addba_reject(struct sigma_dut *dut, const char *intf,
4256 int addbareject)
4257{
4258 int ret;
4259
4260 switch (get_driver_type()) {
4261 case DRIVER_WCN:
4262 ret = nlvendor_sta_set_addba_reject(dut, intf, addbareject);
4263 if (ret) {
4264 sigma_dut_print(dut, DUT_MSG_ERROR,
4265 "nlvendor_sta_set_addba_reject failed, ret:%d",
4266 ret);
4267 return ret;
4268 }
4269 break;
4270 default:
4271 sigma_dut_print(dut, DUT_MSG_ERROR,
4272 "errorCode,Unsupported ADDBA_REJECT with the current driver");
4273 ret = -1;
4274 break;
4275 }
4276
4277 return ret;
4278}
4279
4280
Amarnath Hullur Subramanyam9745b3c2018-02-04 21:27:57 -08004281static int nlvendor_disable_addba(struct sigma_dut *dut, const char *intf)
4282{
4283#ifdef NL80211_SUPPORT
4284 struct nl_msg *msg;
4285 int ret = 0;
4286 struct nlattr *params;
4287 int ifindex;
4288
4289 ifindex = if_nametoindex(intf);
4290 if (ifindex == 0) {
4291 sigma_dut_print(dut, DUT_MSG_ERROR,
4292 "%s: Index for interface %s failed",
4293 __func__, intf);
4294 return -1;
4295 }
4296
4297 if (!(msg = nl80211_drv_msg(dut, dut->nl_ctx, ifindex, 0,
4298 NL80211_CMD_VENDOR)) ||
4299 nla_put_u32(msg, NL80211_ATTR_IFINDEX, ifindex) ||
4300 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
4301 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
4302 QCA_NL80211_VENDOR_SUBCMD_WIFI_TEST_CONFIGURATION) ||
4303 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
4304 nla_put_u8(msg,
4305 QCA_WLAN_VENDOR_ATTR_WIFI_TEST_CONFIG_SEND_ADDBA_REQ,
4306 0)) {
4307 sigma_dut_print(dut, DUT_MSG_ERROR,
4308 "%s: err in adding vendor_cmd and vendor_data",
4309 __func__);
4310 nlmsg_free(msg);
4311 return -1;
4312 }
4313 nla_nest_end(msg, params);
4314
4315 ret = send_and_recv_msgs(dut, dut->nl_ctx, msg, NULL, NULL);
4316 if (ret) {
4317 sigma_dut_print(dut, DUT_MSG_ERROR,
4318 "%s: err in send_and_recv_msgs, ret=%d",
4319 __func__, ret);
4320 }
4321 return ret;
4322#else /* NL80211_SUPPORT */
4323 sigma_dut_print(dut, DUT_MSG_ERROR,
4324 "Disable addba not possible without NL80211_SUPPORT defined");
4325 return -1;
4326#endif /* NL80211_SUPPORT */
4327}
4328
4329
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004330static int cmd_sta_set_wireless_common(const char *intf, struct sigma_dut *dut,
4331 struct sigma_conn *conn,
4332 struct sigma_cmd *cmd)
4333{
4334 const char *val;
Amarnath Hullur Subramanyamd8a9db92018-02-02 18:53:14 -08004335 int ampdu = -1, addbareject = -1;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004336 char buf[30];
4337
4338 val = get_param(cmd, "40_INTOLERANT");
4339 if (val) {
4340 if (strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0) {
4341 /* TODO: iwpriv ht40intol through wpa_supplicant */
4342 send_resp(dut, conn, SIGMA_ERROR,
4343 "ErrorCode,40_INTOLERANT not supported");
4344 return 0;
4345 }
4346 }
4347
4348 val = get_param(cmd, "ADDBA_REJECT");
4349 if (val) {
4350 if (strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0) {
4351 /* reject any ADDBA with status "decline" */
4352 ampdu = 0;
Amarnath Hullur Subramanyamd8a9db92018-02-02 18:53:14 -08004353 addbareject = 1;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004354 } else {
4355 /* accept ADDBA */
4356 ampdu = 1;
Amarnath Hullur Subramanyamd8a9db92018-02-02 18:53:14 -08004357 addbareject = 0;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004358 }
4359 }
4360
Amarnath Hullur Subramanyamd8a9db92018-02-02 18:53:14 -08004361 if (addbareject >= 0 &&
4362 sta_set_addba_reject(dut, intf, addbareject) < 0) {
4363 send_resp(dut, conn, SIGMA_ERROR,
4364 "ErrorCode,set addba_reject failed");
4365 return 0;
4366 }
4367
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004368 val = get_param(cmd, "AMPDU");
4369 if (val) {
4370 if (strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0) {
4371 /* enable AMPDU Aggregation */
4372 if (ampdu == 0) {
4373 send_resp(dut, conn, SIGMA_ERROR,
4374 "ErrorCode,Mismatch in "
4375 "addba_reject/ampdu - "
4376 "not supported");
4377 return 0;
4378 }
4379 ampdu = 1;
4380 } else {
4381 /* disable AMPDU Aggregation */
4382 if (ampdu == 1) {
4383 send_resp(dut, conn, SIGMA_ERROR,
4384 "ErrorCode,Mismatch in "
4385 "addba_reject/ampdu - "
4386 "not supported");
4387 return 0;
4388 }
4389 ampdu = 0;
4390 }
4391 }
4392
4393 if (ampdu >= 0) {
Amarnath Hullur Subramanyam9745b3c2018-02-04 21:27:57 -08004394 int ret;
4395
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004396 sigma_dut_print(dut, DUT_MSG_DEBUG, "%s A-MPDU aggregation",
4397 ampdu ? "Enabling" : "Disabling");
4398 snprintf(buf, sizeof(buf), "SET ampdu %d", ampdu);
Deepak Dhamdhere80356cb2016-03-28 16:48:32 -07004399 if (wpa_command(intf, buf) < 0 &&
4400 iwpriv_sta_set_ampdu(dut, intf, ampdu) < 0) {
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004401 send_resp(dut, conn, SIGMA_ERROR,
4402 "ErrorCode,set aggr failed");
4403 return 0;
4404 }
Amarnath Hullur Subramanyam9745b3c2018-02-04 21:27:57 -08004405
4406 if (ampdu == 0) {
4407 /* Disable sending of addba using nl vendor command */
4408 ret = nlvendor_disable_addba(dut, intf);
4409 if (ret) {
4410 sigma_dut_print(dut, DUT_MSG_ERROR,
4411 "Failed to disable addba, ret:%d",
4412 ret);
4413 }
4414 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004415 }
4416
4417 val = get_param(cmd, "AMSDU");
4418 if (val) {
4419 switch (get_driver_type()) {
4420 case DRIVER_ATHEROS:
Amarnath Hullur Subramanyamd5bb5732018-02-22 15:50:38 -08004421 case DRIVER_WCN:
4422 iwpriv_sta_set_amsdu(dut, intf, val);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004423 break;
4424 default:
4425 if (strcmp(val, "1") == 0 ||
4426 strcasecmp(val, "Enable") == 0) {
4427 /* Enable AMSDU Aggregation */
4428 send_resp(dut, conn, SIGMA_ERROR,
4429 "ErrorCode,AMSDU aggregation not supported");
4430 return 0;
4431 }
4432 break;
4433 }
4434 }
4435
4436 val = get_param(cmd, "STBC_RX");
4437 if (val) {
4438 switch (get_driver_type()) {
4439 case DRIVER_ATHEROS:
4440 ath_sta_set_stbc(dut, intf, val);
4441 break;
Pradeep Reddy POTTETI4a1f6b32016-11-23 13:15:21 +05304442 case DRIVER_WCN:
4443 wcn_sta_set_stbc(dut, intf, val);
4444 break;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004445 default:
4446 send_resp(dut, conn, SIGMA_ERROR,
4447 "ErrorCode,STBC_RX not supported");
4448 return 0;
4449 }
4450 }
4451
4452 val = get_param(cmd, "WIDTH");
4453 if (val) {
4454 switch (get_driver_type()) {
4455 case DRIVER_WCN:
Amarnath Hullur Subramanyamebfe6b62018-01-31 03:04:17 -08004456 if (wcn_sta_set_width(dut, intf, val) < 0) {
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004457 send_resp(dut, conn, SIGMA_ERROR,
4458 "ErrorCode,Failed to set WIDTH");
4459 return 0;
4460 }
4461 break;
4462 case DRIVER_ATHEROS:
4463 if (ath_set_width(dut, conn, intf, val) < 0)
4464 return 0;
4465 break;
4466 default:
4467 sigma_dut_print(dut, DUT_MSG_ERROR,
4468 "Setting WIDTH not supported");
4469 break;
4470 }
4471 }
4472
4473 val = get_param(cmd, "SMPS");
4474 if (val) {
4475 /* TODO: Dynamic/0, Static/1, No Limit/2 */
4476 send_resp(dut, conn, SIGMA_ERROR,
4477 "ErrorCode,SMPS not supported");
4478 return 0;
4479 }
4480
4481 val = get_param(cmd, "TXSP_STREAM");
4482 if (val) {
4483 switch (get_driver_type()) {
4484 case DRIVER_WCN:
4485 if (wcn_sta_set_sp_stream(dut, intf, val) < 0) {
4486 send_resp(dut, conn, SIGMA_ERROR,
4487 "ErrorCode,Failed to set TXSP_STREAM");
4488 return 0;
4489 }
4490 break;
4491 case DRIVER_ATHEROS:
4492 ath_sta_set_txsp_stream(dut, intf, val);
4493 break;
4494 default:
4495 sigma_dut_print(dut, DUT_MSG_ERROR,
4496 "Setting TXSP_STREAM not supported");
4497 break;
4498 }
4499 }
4500
4501 val = get_param(cmd, "RXSP_STREAM");
4502 if (val) {
4503 switch (get_driver_type()) {
4504 case DRIVER_WCN:
4505 if (wcn_sta_set_sp_stream(dut, intf, val) < 0) {
4506 send_resp(dut, conn, SIGMA_ERROR,
4507 "ErrorCode,Failed to set RXSP_STREAM");
4508 return 0;
4509 }
4510 break;
4511 case DRIVER_ATHEROS:
4512 ath_sta_set_rxsp_stream(dut, intf, val);
4513 break;
4514 default:
4515 sigma_dut_print(dut, DUT_MSG_ERROR,
4516 "Setting RXSP_STREAM not supported");
4517 break;
4518 }
4519 }
4520
4521 val = get_param(cmd, "DYN_BW_SGNL");
4522 if (val) {
Priyadharshini Gowthaman818afef2015-11-09 13:28:15 -08004523 switch (get_driver_type()) {
4524 case DRIVER_WCN:
Peng Xuc59afd32016-11-21 15:01:11 -08004525 if (strcasecmp(val, "enable") == 0) {
4526 snprintf(buf, sizeof(buf),
4527 "iwpriv %s cwmenable 1", intf);
4528 if (system(buf) != 0) {
4529 sigma_dut_print(dut, DUT_MSG_ERROR,
4530 "iwpriv cwmenable 1 failed");
4531 return 0;
4532 }
4533 } else if (strcasecmp(val, "disable") == 0) {
4534 snprintf(buf, sizeof(buf),
4535 "iwpriv %s cwmenable 0", intf);
4536 if (system(buf) != 0) {
4537 sigma_dut_print(dut, DUT_MSG_ERROR,
4538 "iwpriv cwmenable 0 failed");
4539 return 0;
4540 }
4541 } else {
4542 sigma_dut_print(dut, DUT_MSG_ERROR,
4543 "Unsupported DYN_BW_SGL");
4544 }
4545
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004546 snprintf(buf, sizeof(buf), "iwpriv %s cts_cbw 3", intf);
4547 if (system(buf) != 0) {
4548 sigma_dut_print(dut, DUT_MSG_ERROR,
4549 "Failed to set cts_cbw in DYN_BW_SGNL");
4550 return 0;
4551 }
Priyadharshini Gowthaman818afef2015-11-09 13:28:15 -08004552 break;
4553 case DRIVER_ATHEROS:
priyadharshini gowthamane5e25172015-12-08 14:53:48 -08004554 novap_reset(dut, intf);
Priyadharshini Gowthaman818afef2015-11-09 13:28:15 -08004555 ath_config_dyn_bw_sig(dut, intf, val);
4556 break;
4557 default:
4558 sigma_dut_print(dut, DUT_MSG_ERROR,
4559 "Failed to set DYN_BW_SGNL");
4560 break;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004561 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004562 }
4563
4564 val = get_param(cmd, "RTS_FORCE");
4565 if (val) {
priyadharshini gowthamane5e25172015-12-08 14:53:48 -08004566 novap_reset(dut, intf);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004567 if (strcasecmp(val, "Enable") == 0) {
4568 snprintf(buf, sizeof(buf), "iwconfig %s rts 64", intf);
Priyadharshini Gowthamanabdb2122015-11-17 11:52:19 +02004569 if (system(buf) != 0) {
4570 sigma_dut_print(dut, DUT_MSG_ERROR,
4571 "Failed to set RTS_FORCE 64");
4572 }
priyadharshini gowthaman270870e2015-12-09 10:10:23 -08004573 snprintf(buf, sizeof(buf),
4574 "wifitool %s beeliner_fw_test 100 1", intf);
4575 if (system(buf) != 0) {
4576 sigma_dut_print(dut, DUT_MSG_ERROR,
4577 "wifitool beeliner_fw_test 100 1 failed");
4578 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004579 } else if (strcasecmp(val, "Disable") == 0) {
4580 snprintf(buf, sizeof(buf), "iwconfig %s rts 2347",
4581 intf);
Priyadharshini Gowthamanabdb2122015-11-17 11:52:19 +02004582 if (system(buf) != 0) {
4583 sigma_dut_print(dut, DUT_MSG_ERROR,
4584 "Failed to set RTS_FORCE 2347");
4585 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004586 } else {
4587 send_resp(dut, conn, SIGMA_ERROR,
4588 "ErrorCode,RTS_FORCE value not supported");
4589 return 0;
4590 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004591 }
4592
4593 val = get_param(cmd, "CTS_WIDTH");
4594 if (val) {
4595 switch (get_driver_type()) {
4596 case DRIVER_WCN:
4597 if (wcn_sta_set_cts_width(dut, intf, val) < 0) {
4598 send_resp(dut, conn, SIGMA_ERROR,
4599 "ErrorCode,Failed to set CTS_WIDTH");
4600 return 0;
4601 }
4602 break;
4603 case DRIVER_ATHEROS:
4604 ath_set_cts_width(dut, intf, val);
4605 break;
4606 default:
4607 sigma_dut_print(dut, DUT_MSG_ERROR,
4608 "Setting CTS_WIDTH not supported");
4609 break;
4610 }
4611 }
4612
4613 val = get_param(cmd, "BW_SGNL");
4614 if (val) {
4615 if (strcasecmp(val, "Enable") == 0) {
4616 snprintf(buf, sizeof(buf), "iwpriv %s cwmenable 1",
4617 intf);
4618 } else if (strcasecmp(val, "Disable") == 0) {
4619 /* TODO: Disable */
4620 buf[0] = '\0';
4621 } else {
4622 send_resp(dut, conn, SIGMA_ERROR,
4623 "ErrorCode,BW_SGNL value not supported");
4624 return 0;
4625 }
4626
4627 if (buf[0] != '\0' && system(buf) != 0) {
4628 sigma_dut_print(dut, DUT_MSG_ERROR,
4629 "Failed to set BW_SGNL");
4630 }
4631 }
4632
4633 val = get_param(cmd, "Band");
4634 if (val) {
4635 if (strcmp(val, "2.4") == 0 || strcmp(val, "5") == 0) {
4636 /* STA supports all bands by default */
4637 } else {
4638 send_resp(dut, conn, SIGMA_ERROR,
4639 "ErrorCode,Unsupported Band");
4640 return 0;
4641 }
4642 }
4643
4644 val = get_param(cmd, "zero_crc");
4645 if (val) {
4646 switch (get_driver_type()) {
4647 case DRIVER_ATHEROS:
4648 ath_set_zero_crc(dut, val);
4649 break;
4650 default:
4651 break;
4652 }
4653 }
4654
4655 return 1;
4656}
4657
4658
4659static int sta_set_60g_common(struct sigma_dut *dut, struct sigma_conn *conn,
4660 struct sigma_cmd *cmd)
4661{
4662 const char *val;
4663 char buf[100];
4664
4665 val = get_param(cmd, "MSDUSize");
4666 if (val) {
4667 int mtu;
4668
4669 dut->amsdu_size = atoi(val);
4670 if (dut->amsdu_size > IEEE80211_MAX_DATA_LEN_DMG ||
4671 dut->amsdu_size < IEEE80211_SNAP_LEN_DMG) {
4672 sigma_dut_print(dut, DUT_MSG_ERROR,
4673 "MSDUSize %d is above max %d or below min %d",
4674 dut->amsdu_size,
4675 IEEE80211_MAX_DATA_LEN_DMG,
4676 IEEE80211_SNAP_LEN_DMG);
4677 dut->amsdu_size = 0;
4678 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
4679 }
4680
4681 mtu = dut->amsdu_size - IEEE80211_SNAP_LEN_DMG;
4682 sigma_dut_print(dut, DUT_MSG_DEBUG,
4683 "Setting amsdu_size to %d", mtu);
4684 snprintf(buf, sizeof(buf), "ifconfig %s mtu %d",
4685 get_station_ifname(), mtu);
4686
4687 if (system(buf) != 0) {
4688 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to set %s",
4689 buf);
4690 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
4691 }
4692 }
4693
4694 val = get_param(cmd, "BAckRcvBuf");
4695 if (val) {
4696 dut->back_rcv_buf = atoi(val);
4697 if (dut->back_rcv_buf == 0) {
4698 sigma_dut_print(dut, DUT_MSG_ERROR,
4699 "Failed to convert %s or value is 0",
4700 val);
4701 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
4702 }
4703
4704 sigma_dut_print(dut, DUT_MSG_DEBUG,
4705 "Setting BAckRcvBuf to %s", val);
4706 }
4707
4708 return SIGMA_DUT_SUCCESS_CALLER_SEND_STATUS;
4709}
4710
4711
4712static int sta_pcp_start(struct sigma_dut *dut, struct sigma_conn *conn,
4713 struct sigma_cmd *cmd)
4714{
4715 int net_id;
4716 char *ifname;
4717 const char *val;
4718 char buf[100];
4719
4720 dut->mode = SIGMA_MODE_STATION;
4721 ifname = get_main_ifname();
4722 if (wpa_command(ifname, "PING") != 0) {
4723 sigma_dut_print(dut, DUT_MSG_ERROR, "Supplicant not running");
4724 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
4725 }
4726
4727 wpa_command(ifname, "FLUSH");
4728 net_id = add_network_common(dut, conn, ifname, cmd);
4729 if (net_id < 0) {
4730 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to add network");
4731 return net_id;
4732 }
4733
4734 /* TODO: mode=2 for the AP; in the future, replace for mode PCP */
4735 if (set_network(ifname, net_id, "mode", "2") < 0) {
4736 sigma_dut_print(dut, DUT_MSG_ERROR,
4737 "Failed to set supplicant network mode");
4738 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
4739 }
4740
4741 sigma_dut_print(dut, DUT_MSG_DEBUG,
4742 "Supplicant set network with mode 2");
4743
4744 val = get_param(cmd, "Security");
4745 if (val && strcasecmp(val, "OPEN") == 0) {
4746 dut->ap_key_mgmt = AP_OPEN;
4747 if (set_network(ifname, net_id, "key_mgmt", "NONE") < 0) {
4748 sigma_dut_print(dut, DUT_MSG_ERROR,
4749 "Failed to set supplicant to %s security",
4750 val);
4751 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
4752 }
4753 } else if (val && strcasecmp(val, "WPA2-PSK") == 0) {
4754 dut->ap_key_mgmt = AP_WPA2_PSK;
4755 if (set_network(ifname, net_id, "key_mgmt", "WPA-PSK") < 0) {
4756 sigma_dut_print(dut, DUT_MSG_ERROR,
4757 "Failed to set supplicant to %s security",
4758 val);
4759 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
4760 }
4761
4762 if (set_network(ifname, net_id, "proto", "RSN") < 0) {
4763 sigma_dut_print(dut, DUT_MSG_ERROR,
4764 "Failed to set supplicant to proto RSN");
4765 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
4766 }
4767 } else if (val) {
4768 sigma_dut_print(dut, DUT_MSG_ERROR,
4769 "Requested Security %s is not supported on 60GHz",
4770 val);
4771 return SIGMA_DUT_INVALID_CALLER_SEND_STATUS;
4772 }
4773
4774 val = get_param(cmd, "Encrypt");
4775 if (val && strcasecmp(val, "AES-GCMP") == 0) {
4776 if (set_network(ifname, net_id, "pairwise", "GCMP") < 0) {
4777 sigma_dut_print(dut, DUT_MSG_ERROR,
4778 "Failed to set supplicant to pairwise GCMP");
4779 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
4780 }
4781 if (set_network(ifname, net_id, "group", "GCMP") < 0) {
4782 sigma_dut_print(dut, DUT_MSG_ERROR,
4783 "Failed to set supplicant to group GCMP");
4784 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
4785 }
4786 } else if (val) {
4787 sigma_dut_print(dut, DUT_MSG_ERROR,
4788 "Requested Encrypt %s is not supported on 60 GHz",
4789 val);
4790 return SIGMA_DUT_INVALID_CALLER_SEND_STATUS;
4791 }
4792
4793 val = get_param(cmd, "PSK");
4794 if (val && set_network_quoted(ifname, net_id, "psk", val) < 0) {
4795 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to set psk %s",
4796 val);
4797 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
4798 }
4799
4800 /* Convert 60G channel to freq */
4801 switch (dut->ap_channel) {
4802 case 1:
4803 val = "58320";
4804 break;
4805 case 2:
4806 val = "60480";
4807 break;
4808 case 3:
4809 val = "62640";
4810 break;
4811 default:
4812 sigma_dut_print(dut, DUT_MSG_ERROR,
4813 "Failed to configure channel %d. Not supported",
4814 dut->ap_channel);
4815 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
4816 }
4817
4818 if (set_network(ifname, net_id, "frequency", val) < 0) {
4819 sigma_dut_print(dut, DUT_MSG_ERROR,
4820 "Failed to set supplicant network frequency");
4821 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
4822 }
4823
4824 sigma_dut_print(dut, DUT_MSG_DEBUG,
4825 "Supplicant set network with frequency");
4826
4827 snprintf(buf, sizeof(buf), "SELECT_NETWORK %d", net_id);
4828 if (wpa_command(ifname, buf) < 0) {
4829 sigma_dut_print(dut, DUT_MSG_INFO,
4830 "Failed to select network id %d on %s",
4831 net_id, ifname);
4832 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
4833 }
4834
4835 sigma_dut_print(dut, DUT_MSG_DEBUG, "Selected network");
4836
4837 return SIGMA_DUT_SUCCESS_CALLER_SEND_STATUS;
4838}
4839
4840
Lior David67543f52017-01-03 19:04:22 +02004841static int wil6210_set_abft_len(struct sigma_dut *dut, int abft_len)
4842{
4843 char buf[128], fname[128];
4844 FILE *f;
4845
4846 if (wil6210_get_debugfs_dir(dut, buf, sizeof(buf))) {
4847 sigma_dut_print(dut, DUT_MSG_ERROR,
4848 "failed to get wil6210 debugfs dir");
4849 return -1;
4850 }
4851
4852 snprintf(fname, sizeof(fname), "%s/abft_len", buf);
4853 f = fopen(fname, "w");
4854 if (!f) {
4855 sigma_dut_print(dut, DUT_MSG_ERROR,
4856 "failed to open: %s", fname);
4857 return -1;
4858 }
4859
4860 fprintf(f, "%d\n", abft_len);
4861 fclose(f);
4862
4863 return 0;
4864}
4865
4866
4867static int sta_set_60g_abft_len(struct sigma_dut *dut, struct sigma_conn *conn,
4868 int abft_len)
4869{
4870 switch (get_driver_type()) {
4871 case DRIVER_WIL6210:
4872 return wil6210_set_abft_len(dut, abft_len);
4873 default:
4874 sigma_dut_print(dut, DUT_MSG_ERROR,
4875 "set abft_len not supported");
4876 return -1;
4877 }
4878}
4879
4880
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004881static int sta_set_60g_pcp(struct sigma_dut *dut, struct sigma_conn *conn,
4882 struct sigma_cmd *cmd)
4883{
4884 const char *val;
Lior David67543f52017-01-03 19:04:22 +02004885 unsigned int abft_len = 1; /* default is one slot */
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004886
4887 if (dut->dev_role != DEVROLE_PCP) {
4888 send_resp(dut, conn, SIGMA_INVALID,
4889 "ErrorCode,Invalid DevRole");
4890 return 0;
4891 }
4892
4893 val = get_param(cmd, "SSID");
4894 if (val) {
4895 if (strlen(val) > sizeof(dut->ap_ssid) - 1) {
4896 send_resp(dut, conn, SIGMA_INVALID,
4897 "ErrorCode,Invalid SSID");
4898 return -1;
4899 }
4900
Peng Xub8fc5cc2017-05-10 17:27:28 -07004901 strlcpy(dut->ap_ssid, val, sizeof(dut->ap_ssid));
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004902 }
4903
4904 val = get_param(cmd, "CHANNEL");
4905 if (val) {
4906 const char *pos;
4907
4908 dut->ap_channel = atoi(val);
4909 pos = strchr(val, ';');
4910 if (pos) {
4911 pos++;
4912 dut->ap_channel_1 = atoi(pos);
4913 }
4914 }
4915
4916 switch (dut->ap_channel) {
4917 case 1:
4918 case 2:
4919 case 3:
4920 break;
4921 default:
4922 sigma_dut_print(dut, DUT_MSG_ERROR,
4923 "Channel %d is not supported", dut->ap_channel);
4924 send_resp(dut, conn, SIGMA_ERROR,
4925 "Requested channel is not supported");
4926 return -1;
4927 }
4928
4929 val = get_param(cmd, "BCNINT");
4930 if (val)
4931 dut->ap_bcnint = atoi(val);
4932
4933
4934 val = get_param(cmd, "ExtSchIE");
4935 if (val) {
4936 send_resp(dut, conn, SIGMA_ERROR,
4937 "ErrorCode,ExtSchIE is not supported yet");
4938 return -1;
4939 }
4940
4941 val = get_param(cmd, "AllocType");
4942 if (val) {
4943 send_resp(dut, conn, SIGMA_ERROR,
4944 "ErrorCode,AllocType is not supported yet");
4945 return -1;
4946 }
4947
4948 val = get_param(cmd, "PercentBI");
4949 if (val) {
4950 send_resp(dut, conn, SIGMA_ERROR,
4951 "ErrorCode,PercentBI is not supported yet");
4952 return -1;
4953 }
4954
4955 val = get_param(cmd, "CBAPOnly");
4956 if (val) {
4957 send_resp(dut, conn, SIGMA_ERROR,
4958 "ErrorCode,CBAPOnly is not supported yet");
4959 return -1;
4960 }
4961
4962 val = get_param(cmd, "AMPDU");
4963 if (val) {
4964 if (strcasecmp(val, "Enable") == 0)
4965 dut->ap_ampdu = 1;
4966 else if (strcasecmp(val, "Disable") == 0)
4967 dut->ap_ampdu = 2;
4968 else {
4969 send_resp(dut, conn, SIGMA_ERROR,
4970 "ErrorCode,AMPDU value is not Enable nor Disabled");
4971 return -1;
4972 }
4973 }
4974
4975 val = get_param(cmd, "AMSDU");
4976 if (val) {
4977 if (strcasecmp(val, "Enable") == 0)
4978 dut->ap_amsdu = 1;
4979 else if (strcasecmp(val, "Disable") == 0)
4980 dut->ap_amsdu = 2;
4981 }
4982
4983 val = get_param(cmd, "NumMSDU");
4984 if (val) {
4985 send_resp(dut, conn, SIGMA_ERROR,
4986 "ErrorCode, NumMSDU is not supported yet");
4987 return -1;
4988 }
4989
4990 val = get_param(cmd, "ABFTLRang");
4991 if (val) {
4992 sigma_dut_print(dut, DUT_MSG_DEBUG,
Lior David67543f52017-01-03 19:04:22 +02004993 "ABFTLRang parameter %s", val);
4994 if (strcmp(val, "Gt1") == 0)
4995 abft_len = 2; /* 2 slots in this case */
4996 }
4997
4998 if (sta_set_60g_abft_len(dut, conn, abft_len)) {
4999 send_resp(dut, conn, SIGMA_ERROR,
5000 "ErrorCode, Can't set ABFT length");
5001 return -1;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005002 }
5003
5004 if (sta_pcp_start(dut, conn, cmd) < 0) {
5005 send_resp(dut, conn, SIGMA_ERROR,
5006 "ErrorCode, Can't start PCP role");
5007 return -1;
5008 }
5009
5010 return sta_set_60g_common(dut, conn, cmd);
5011}
5012
5013
5014static int sta_set_60g_sta(struct sigma_dut *dut, struct sigma_conn *conn,
5015 struct sigma_cmd *cmd)
5016{
5017 const char *val = get_param(cmd, "DiscoveryMode");
5018
5019 if (dut->dev_role != DEVROLE_STA) {
5020 send_resp(dut, conn, SIGMA_INVALID,
5021 "ErrorCode,Invalid DevRole");
5022 return 0;
5023 }
5024
5025 if (val) {
5026 sigma_dut_print(dut, DUT_MSG_DEBUG, "Discovery: %s", val);
5027 /* Ignore Discovery mode till Driver expose API. */
5028#if 0
5029 if (strcasecmp(val, "1") == 0) {
5030 send_resp(dut, conn, SIGMA_INVALID,
5031 "ErrorCode,DiscoveryMode 1 not supported");
5032 return 0;
5033 }
5034
5035 if (strcasecmp(val, "0") == 0) {
5036 /* OK */
5037 } else {
5038 send_resp(dut, conn, SIGMA_INVALID,
5039 "ErrorCode,DiscoveryMode not supported");
5040 return 0;
5041 }
5042#endif
5043 }
5044
5045 if (start_sta_mode(dut) != 0)
5046 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
5047 return sta_set_60g_common(dut, conn, cmd);
5048}
5049
5050
5051static int cmd_sta_disconnect(struct sigma_dut *dut, struct sigma_conn *conn,
5052 struct sigma_cmd *cmd)
5053{
5054 const char *intf = get_param(cmd, "Interface");
Jouni Malinened77e672018-01-10 16:45:13 +02005055 const char *val = get_param(cmd, "maintain_profile");
vamsi krishnad605c422017-09-20 14:56:31 +05305056
Jouni Malinened77e672018-01-10 16:45:13 +02005057 if (dut->program == PROGRAM_OCE ||
Amarnath Hullur Subramanyamebeda9e2018-01-31 03:21:48 -08005058 dut->program == PROGRAM_HE ||
Jouni Malinened77e672018-01-10 16:45:13 +02005059 (val && atoi(val) == 1)) {
vamsi krishnad605c422017-09-20 14:56:31 +05305060 wpa_command(intf, "DISCONNECT");
5061 return 1;
5062 }
5063
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005064 disconnect_station(dut);
5065 /* Try to ignore old scan results to avoid HS 2.0R2 test case failures
5066 * due to cached results. */
5067 wpa_command(intf, "SET ignore_old_scan_res 1");
5068 wpa_command(intf, "BSS_FLUSH");
5069 return 1;
5070}
5071
5072
5073static int cmd_sta_reassoc(struct sigma_dut *dut, struct sigma_conn *conn,
5074 struct sigma_cmd *cmd)
5075{
5076 const char *intf = get_param(cmd, "Interface");
5077 const char *bssid = get_param(cmd, "bssid");
5078 const char *val = get_param(cmd, "CHANNEL");
5079 struct wpa_ctrl *ctrl;
Srinivas Dasari0ebedb12018-02-14 17:03:51 +05305080 char buf[1000];
Sunil Duttd30ce092018-01-11 23:56:29 +05305081 char result[32];
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005082 int res;
5083 int chan = 0;
Ashwini Patil467efef2017-05-25 12:18:27 +05305084 int status = 0;
Sunil Duttd30ce092018-01-11 23:56:29 +05305085 int fastreassoc = 1;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005086
5087 if (bssid == NULL) {
5088 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Missing bssid "
5089 "argument");
5090 return 0;
5091 }
5092
5093 if (val)
5094 chan = atoi(val);
5095
5096 if (wifi_chip_type != DRIVER_WCN && wifi_chip_type != DRIVER_AR6003) {
5097 /* The current network may be from sta_associate or
5098 * sta_hs2_associate
5099 */
5100 if (set_network(intf, dut->infra_network_id, "bssid", bssid) <
5101 0 ||
5102 set_network(intf, 0, "bssid", bssid) < 0)
5103 return -2;
5104 }
5105
5106 ctrl = open_wpa_mon(intf);
5107 if (ctrl == NULL) {
5108 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to open "
5109 "wpa_supplicant monitor connection");
5110 return -1;
5111 }
5112
Sunil Duttd30ce092018-01-11 23:56:29 +05305113 if (get_wpa_status(get_station_ifname(), "wpa_state", result,
5114 sizeof(result)) < 0 ||
5115 strncmp(result, "COMPLETED", 9) != 0) {
5116 sigma_dut_print(dut, DUT_MSG_DEBUG,
5117 "sta_reassoc: Not connected");
5118 fastreassoc = 0;
5119 }
5120
Srinivas Dasari0ebedb12018-02-14 17:03:51 +05305121 if (dut->rsne_override) {
5122#ifdef NL80211_SUPPORT
5123 if (get_driver_type() == DRIVER_WCN && dut->config_rsnie == 0) {
5124 sta_config_rsnie(dut, 1);
5125 dut->config_rsnie = 1;
5126 }
5127#endif /* NL80211_SUPPORT */
5128 snprintf(buf, sizeof(buf), "TEST_ASSOC_IE %s",
5129 dut->rsne_override);
5130 if (wpa_command(intf, buf) < 0) {
5131 send_resp(dut, conn, SIGMA_ERROR,
5132 "ErrorCode,Failed to set DEV_CONFIGURE_IE RSNE override");
5133 return 0;
5134 }
5135 }
5136
Sunil Duttd30ce092018-01-11 23:56:29 +05305137 if (wifi_chip_type == DRIVER_WCN && fastreassoc) {
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005138#ifdef ANDROID
Ashwini Patil4c8158f2017-05-25 12:49:21 +05305139 if (chan) {
5140 unsigned int freq;
5141
5142 freq = channel_to_freq(chan);
5143 if (!freq) {
5144 sigma_dut_print(dut, DUT_MSG_ERROR,
5145 "Invalid channel number provided: %d",
5146 chan);
5147 send_resp(dut, conn, SIGMA_INVALID,
5148 "ErrorCode,Invalid channel number");
5149 goto close_mon_conn;
5150 }
5151 res = snprintf(buf, sizeof(buf),
5152 "SCAN TYPE=ONLY freq=%d", freq);
5153 } else {
5154 res = snprintf(buf, sizeof(buf), "SCAN TYPE=ONLY");
5155 }
5156 if (res < 0 || res >= (int) sizeof(buf)) {
5157 send_resp(dut, conn, SIGMA_ERROR,
5158 "ErrorCode,snprintf failed");
5159 goto close_mon_conn;
5160 }
5161 if (wpa_command(intf, buf) < 0) {
5162 sigma_dut_print(dut, DUT_MSG_INFO,
5163 "Failed to start scan");
5164 send_resp(dut, conn, SIGMA_ERROR,
5165 "ErrorCode,scan failed");
5166 goto close_mon_conn;
5167 }
5168
5169 res = get_wpa_cli_event(dut, ctrl, "CTRL-EVENT-SCAN-RESULTS",
5170 buf, sizeof(buf));
5171 if (res < 0) {
5172 sigma_dut_print(dut, DUT_MSG_INFO,
5173 "Scan did not complete");
5174 send_resp(dut, conn, SIGMA_ERROR,
5175 "ErrorCode,scan did not complete");
5176 goto close_mon_conn;
5177 }
5178
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005179 if (set_network(intf, dut->infra_network_id, "bssid", "any")
5180 < 0) {
5181 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to set "
5182 "bssid to any during FASTREASSOC");
Ashwini Patil467efef2017-05-25 12:18:27 +05305183 status = -2;
5184 goto close_mon_conn;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005185 }
5186 res = snprintf(buf, sizeof(buf), "DRIVER FASTREASSOC %s %d",
5187 bssid, chan);
5188 if (res > 0 && res < (int) sizeof(buf))
5189 res = wpa_command(intf, buf);
5190
5191 if (res < 0 || res >= (int) sizeof(buf)) {
5192 send_resp(dut, conn, SIGMA_ERROR,
5193 "errorCode,Failed to run DRIVER FASTREASSOC");
Ashwini Patil467efef2017-05-25 12:18:27 +05305194 goto close_mon_conn;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005195 }
5196#else /* ANDROID */
5197 sigma_dut_print(dut, DUT_MSG_DEBUG,
5198 "Reassoc using iwpriv - skip chan=%d info",
5199 chan);
5200 snprintf(buf, sizeof(buf), "iwpriv %s reassoc", intf);
5201 if (system(buf) != 0) {
5202 sigma_dut_print(dut, DUT_MSG_ERROR, "%s failed", buf);
Ashwini Patil467efef2017-05-25 12:18:27 +05305203 status = -2;
5204 goto close_mon_conn;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005205 }
5206#endif /* ANDROID */
5207 sigma_dut_print(dut, DUT_MSG_INFO,
5208 "sta_reassoc: Run %s successful", buf);
5209 } else if (wpa_command(intf, "REASSOCIATE")) {
5210 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to "
5211 "request reassociation");
Ashwini Patil467efef2017-05-25 12:18:27 +05305212 goto close_mon_conn;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005213 }
5214
5215 res = get_wpa_cli_event(dut, ctrl, "CTRL-EVENT-CONNECTED",
5216 buf, sizeof(buf));
Ashwini Patil467efef2017-05-25 12:18:27 +05305217 if (res < 0) {
5218 sigma_dut_print(dut, DUT_MSG_INFO, "Connection did not complete");
5219 status = -1;
5220 goto close_mon_conn;
5221 }
5222 status = 1;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005223
Ashwini Patil467efef2017-05-25 12:18:27 +05305224close_mon_conn:
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005225 wpa_ctrl_detach(ctrl);
5226 wpa_ctrl_close(ctrl);
Ashwini Patil467efef2017-05-25 12:18:27 +05305227 return status;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005228}
5229
5230
5231static void hs2_clear_credentials(const char *intf)
5232{
5233 wpa_command(intf, "REMOVE_CRED all");
5234}
5235
5236
Lior Davidcc88b562017-01-03 18:52:09 +02005237#ifdef __linux__
5238static int wil6210_get_aid(struct sigma_dut *dut, const char *bssid,
5239 unsigned int *aid)
5240{
Lior David0fe101e2017-03-09 16:09:50 +02005241 const char *pattern = "AID[ \t]+([0-9]+)";
Lior Davidcc88b562017-01-03 18:52:09 +02005242
Lior David0fe101e2017-03-09 16:09:50 +02005243 return wil6210_get_sta_info_field(dut, bssid, pattern, aid);
Lior Davidcc88b562017-01-03 18:52:09 +02005244}
5245#endif /* __linux__ */
5246
5247
5248static int sta_get_aid_60g(struct sigma_dut *dut, const char *bssid,
5249 unsigned int *aid)
5250{
5251 switch (get_driver_type()) {
5252#ifdef __linux__
5253 case DRIVER_WIL6210:
5254 return wil6210_get_aid(dut, bssid, aid);
5255#endif /* __linux__ */
5256 default:
5257 sigma_dut_print(dut, DUT_MSG_ERROR, "get AID not supported");
5258 return -1;
5259 }
5260}
5261
5262
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005263static int sta_get_parameter_60g(struct sigma_dut *dut, struct sigma_conn *conn,
5264 struct sigma_cmd *cmd)
5265{
5266 char buf[MAX_CMD_LEN];
5267 char bss_list[MAX_CMD_LEN];
5268 const char *parameter = get_param(cmd, "Parameter");
5269
5270 if (parameter == NULL)
5271 return -1;
5272
Lior Davidcc88b562017-01-03 18:52:09 +02005273 if (strcasecmp(parameter, "AID") == 0) {
5274 unsigned int aid = 0;
5275 char bssid[20];
5276
5277 if (get_wpa_status(get_station_ifname(), "bssid",
5278 bssid, sizeof(bssid)) < 0) {
5279 sigma_dut_print(dut, DUT_MSG_ERROR,
5280 "could not get bssid");
5281 return -2;
5282 }
5283
5284 if (sta_get_aid_60g(dut, bssid, &aid))
5285 return -2;
5286
5287 snprintf(buf, sizeof(buf), "aid,%d", aid);
5288 sigma_dut_print(dut, DUT_MSG_INFO, "%s", buf);
5289 send_resp(dut, conn, SIGMA_COMPLETE, buf);
5290 return 0;
5291 }
5292
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005293 if (strcasecmp(parameter, "DiscoveredDevList") == 0) {
5294 char *bss_line;
5295 char *bss_id = NULL;
5296 const char *ifname = get_param(cmd, "Interface");
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05305297 char *saveptr;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005298
5299 if (ifname == NULL) {
5300 sigma_dut_print(dut, DUT_MSG_INFO,
5301 "For get DiscoveredDevList need Interface name.");
5302 return -1;
5303 }
5304
5305 /*
5306 * Use "BSS RANGE=ALL MASK=0x2" which provides a list
5307 * of BSSIDs in "bssid=<BSSID>\n"
5308 */
5309 if (wpa_command_resp(ifname, "BSS RANGE=ALL MASK=0x2",
5310 bss_list,
5311 sizeof(bss_list)) < 0) {
5312 sigma_dut_print(dut, DUT_MSG_ERROR,
5313 "Failed to get bss list");
5314 return -1;
5315 }
5316
5317 sigma_dut_print(dut, DUT_MSG_DEBUG,
5318 "bss list for ifname:%s is:%s",
5319 ifname, bss_list);
5320
5321 snprintf(buf, sizeof(buf), "DeviceList");
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05305322 bss_line = strtok_r(bss_list, "\n", &saveptr);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005323 while (bss_line) {
5324 if (sscanf(bss_line, "bssid=%ms", &bss_id) > 0 &&
5325 bss_id) {
5326 int len;
5327
5328 len = snprintf(buf + strlen(buf),
5329 sizeof(buf) - strlen(buf),
5330 ",%s", bss_id);
5331 free(bss_id);
5332 bss_id = NULL;
5333 if (len < 0) {
5334 sigma_dut_print(dut,
5335 DUT_MSG_ERROR,
5336 "Failed to read BSSID");
5337 send_resp(dut, conn, SIGMA_ERROR,
5338 "ErrorCode,Failed to read BSS ID");
5339 return 0;
5340 }
5341
5342 if ((size_t) len >= sizeof(buf) - strlen(buf)) {
5343 sigma_dut_print(dut,
5344 DUT_MSG_ERROR,
5345 "Response buf too small for list");
5346 send_resp(dut, conn,
5347 SIGMA_ERROR,
5348 "ErrorCode,Response buf too small for list");
5349 return 0;
5350 }
5351 }
5352
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05305353 bss_line = strtok_r(NULL, "\n", &saveptr);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005354 }
5355
5356 sigma_dut_print(dut, DUT_MSG_INFO, "DiscoveredDevList is %s",
5357 buf);
5358 send_resp(dut, conn, SIGMA_COMPLETE, buf);
5359 return 0;
5360 }
5361
5362 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Unsupported parameter");
5363 return 0;
5364}
5365
5366
5367static int cmd_sta_get_parameter(struct sigma_dut *dut, struct sigma_conn *conn,
5368 struct sigma_cmd *cmd)
5369{
5370 const char *program = get_param(cmd, "Program");
5371
5372 if (program == NULL)
5373 return -1;
5374
5375 if (strcasecmp(program, "P2PNFC") == 0)
5376 return p2p_cmd_sta_get_parameter(dut, conn, cmd);
5377
5378 if (strcasecmp(program, "60ghz") == 0)
5379 return sta_get_parameter_60g(dut, conn, cmd);
5380
5381#ifdef ANDROID_NAN
5382 if (strcasecmp(program, "NAN") == 0)
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -07005383 return nan_cmd_sta_get_parameter(dut, conn, cmd);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005384#endif /* ANDROID_NAN */
5385
Amarnath Hullur Subramanyam9c381f52017-03-17 00:04:41 -07005386#ifdef MIRACAST
5387 if (strcasecmp(program, "WFD") == 0 ||
5388 strcasecmp(program, "DisplayR2") == 0)
5389 return miracast_cmd_sta_get_parameter(dut, conn, cmd);
5390#endif /* MIRACAST */
5391
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005392 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Unsupported parameter");
5393 return 0;
5394}
5395
5396
5397static void sta_reset_default_ath(struct sigma_dut *dut, const char *intf,
5398 const char *type)
5399{
5400 char buf[100];
5401
5402 if (dut->program == PROGRAM_VHT) {
5403 snprintf(buf, sizeof(buf), "iwpriv %s chwidth 2", intf);
5404 if (system(buf) != 0) {
5405 sigma_dut_print(dut, DUT_MSG_ERROR,
5406 "iwpriv %s chwidth failed", intf);
5407 }
5408
5409 snprintf(buf, sizeof(buf), "iwpriv %s mode 11ACVHT80", intf);
5410 if (system(buf) != 0) {
5411 sigma_dut_print(dut, DUT_MSG_ERROR,
5412 "iwpriv %s mode 11ACVHT80 failed",
5413 intf);
5414 }
5415
5416 snprintf(buf, sizeof(buf), "iwpriv %s vhtmcs -1", intf);
5417 if (system(buf) != 0) {
5418 sigma_dut_print(dut, DUT_MSG_ERROR,
5419 "iwpriv %s vhtmcs -1 failed", intf);
5420 }
5421 }
5422
5423 if (dut->program == PROGRAM_HT) {
5424 snprintf(buf, sizeof(buf), "iwpriv %s chwidth 0", intf);
5425 if (system(buf) != 0) {
5426 sigma_dut_print(dut, DUT_MSG_ERROR,
5427 "iwpriv %s chwidth failed", intf);
5428 }
5429
5430 snprintf(buf, sizeof(buf), "iwpriv %s mode 11naht40", intf);
5431 if (system(buf) != 0) {
5432 sigma_dut_print(dut, DUT_MSG_ERROR,
5433 "iwpriv %s mode 11naht40 failed",
5434 intf);
5435 }
5436
5437 snprintf(buf, sizeof(buf), "iwpriv %s set11NRates 0", intf);
5438 if (system(buf) != 0) {
5439 sigma_dut_print(dut, DUT_MSG_ERROR,
5440 "iwpriv set11NRates failed");
5441 }
5442 }
5443
5444 if (dut->program == PROGRAM_VHT || dut->program == PROGRAM_HT) {
5445 snprintf(buf, sizeof(buf), "iwpriv %s powersave 0", intf);
5446 if (system(buf) != 0) {
5447 sigma_dut_print(dut, DUT_MSG_ERROR,
5448 "disabling powersave failed");
5449 }
5450
5451 /* Reset CTS width */
5452 snprintf(buf, sizeof(buf), "wifitool %s beeliner_fw_test 54 0",
5453 intf);
5454 if (system(buf) != 0) {
5455 sigma_dut_print(dut, DUT_MSG_ERROR,
5456 "wifitool %s beeliner_fw_test 54 0 failed",
5457 intf);
5458 }
5459
5460 /* Enable Dynamic Bandwidth signalling by default */
5461 snprintf(buf, sizeof(buf), "iwpriv %s cwmenable 1", intf);
5462 if (system(buf) != 0) {
5463 sigma_dut_print(dut, DUT_MSG_ERROR,
5464 "iwpriv %s cwmenable 1 failed", intf);
5465 }
5466
5467 snprintf(buf, sizeof(buf), "iwconfig %s rts 2347", intf);
5468 if (system(buf) != 0) {
5469 sigma_dut_print(dut, DUT_MSG_ERROR,
5470 "iwpriv rts failed");
5471 }
5472 }
5473
5474 if (type && strcasecmp(type, "Testbed") == 0) {
5475 dut->testbed_flag_txsp = 1;
5476 dut->testbed_flag_rxsp = 1;
5477 /* STA has to set spatial stream to 2 per Appendix H */
5478 snprintf(buf, sizeof(buf), "iwpriv %s vht_mcsmap 0xfff0", intf);
5479 if (system(buf) != 0) {
5480 sigma_dut_print(dut, DUT_MSG_ERROR,
5481 "iwpriv vht_mcsmap failed");
5482 }
5483
5484 /* Disable LDPC per Appendix H */
5485 snprintf(buf, sizeof(buf), "iwpriv %s ldpc 0", intf);
5486 if (system(buf) != 0) {
5487 sigma_dut_print(dut, DUT_MSG_ERROR,
5488 "iwpriv %s ldpc 0 failed", intf);
5489 }
5490
5491 snprintf(buf, sizeof(buf), "iwpriv %s amsdu 1", intf);
5492 if (system(buf) != 0) {
5493 sigma_dut_print(dut, DUT_MSG_ERROR,
5494 "iwpriv amsdu failed");
5495 }
5496
5497 /* TODO: Disable STBC 2x1 transmit and receive */
5498 snprintf(buf, sizeof(buf), "iwpriv %s tx_stbc 0", intf);
5499 if (system(buf) != 0) {
5500 sigma_dut_print(dut, DUT_MSG_ERROR,
5501 "Disable tx_stbc 0 failed");
5502 }
5503
5504 snprintf(buf, sizeof(buf), "iwpriv %s rx_stbc 0", intf);
5505 if (system(buf) != 0) {
5506 sigma_dut_print(dut, DUT_MSG_ERROR,
5507 "Disable rx_stbc 0 failed");
5508 }
5509
5510 /* STA has to disable Short GI per Appendix H */
5511 snprintf(buf, sizeof(buf), "iwpriv %s shortgi 0", intf);
5512 if (system(buf) != 0) {
5513 sigma_dut_print(dut, DUT_MSG_ERROR,
5514 "iwpriv %s shortgi 0 failed", intf);
5515 }
5516 }
5517
5518 if (type && strcasecmp(type, "DUT") == 0) {
5519 snprintf(buf, sizeof(buf), "iwpriv %s nss 3", intf);
5520 if (system(buf) != 0) {
5521 sigma_dut_print(dut, DUT_MSG_ERROR,
5522 "iwpriv %s nss 3 failed", intf);
5523 }
5524
5525 snprintf(buf, sizeof(buf), "iwpriv %s shortgi 1", intf);
5526 if (system(buf) != 0) {
5527 sigma_dut_print(dut, DUT_MSG_ERROR,
5528 "iwpriv %s shortgi 1 failed", intf);
5529 }
5530 }
5531}
5532
5533
Amarnath Hullur Subramanyam2538acc2018-02-02 16:15:25 -08005534#ifdef NL80211_SUPPORT
5535static int sta_set_he_mcs(struct sigma_dut *dut, const char *intf,
5536 enum he_mcs_config mcs)
5537{
5538 struct nl_msg *msg;
5539 int ret = 0;
5540 struct nlattr *params;
5541 int ifindex;
5542
5543 ifindex = if_nametoindex(intf);
5544 if (ifindex == 0) {
5545 sigma_dut_print(dut, DUT_MSG_ERROR,
5546 "%s: Index for interface %s failed",
5547 __func__, intf);
5548 return -1;
5549 }
5550
5551 if (!(msg = nl80211_drv_msg(dut, dut->nl_ctx, ifindex, 0,
5552 NL80211_CMD_VENDOR)) ||
5553 nla_put_u32(msg, NL80211_ATTR_IFINDEX, ifindex) ||
5554 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
5555 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
5556 QCA_NL80211_VENDOR_SUBCMD_WIFI_TEST_CONFIGURATION) ||
5557 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
5558 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_WIFI_TEST_CONFIG_HE_MCS,
5559 mcs)) {
5560 sigma_dut_print(dut, DUT_MSG_ERROR,
5561 "%s: err in adding vendor_cmd and vendor_data",
5562 __func__);
5563 nlmsg_free(msg);
5564 return -1;
5565 }
5566 nla_nest_end(msg, params);
5567
5568 ret = send_and_recv_msgs(dut, dut->nl_ctx, msg, NULL, NULL);
5569 if (ret) {
5570 sigma_dut_print(dut, DUT_MSG_ERROR,
5571 "%s: err in send_and_recv_msgs, ret=%d",
5572 __func__, ret);
5573 }
5574 return ret;
5575}
5576#endif /* NL80211_SUPPORT */
5577
5578
Amarnath Hullur Subramanyam4622a212018-02-23 12:12:14 -08005579static int sta_set_heconfig_and_wep_tkip(struct sigma_dut *dut,
5580 const char *intf, int enable)
5581{
5582#ifdef NL80211_SUPPORT
5583 struct nl_msg *msg;
5584 int ret = 0;
5585 struct nlattr *params;
5586 int ifindex;
5587
5588 ifindex = if_nametoindex(intf);
5589 if (ifindex == 0) {
5590 sigma_dut_print(dut, DUT_MSG_ERROR,
5591 "%s: Index for interface %s failed",
5592 __func__, intf);
5593 return -1;
5594 }
5595
5596 if (!(msg = nl80211_drv_msg(dut, dut->nl_ctx, ifindex, 0,
5597 NL80211_CMD_VENDOR)) ||
5598 nla_put_u32(msg, NL80211_ATTR_IFINDEX, ifindex) ||
5599 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
5600 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
5601 QCA_NL80211_VENDOR_SUBCMD_WIFI_TEST_CONFIGURATION) ||
5602 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
5603 nla_put_u8(msg,
5604 QCA_WLAN_VENDOR_ATTR_WIFI_TEST_CONFIG_WEP_TKIP_IN_HE,
5605 enable)) {
5606 sigma_dut_print(dut, DUT_MSG_ERROR,
5607 "%s: err in adding vendor_cmd and vendor_data",
5608 __func__);
5609 nlmsg_free(msg);
5610 return -1;
5611 }
5612 nla_nest_end(msg, params);
5613
5614 ret = send_and_recv_msgs(dut, dut->nl_ctx, msg, NULL, NULL);
5615 if (ret) {
5616 sigma_dut_print(dut, DUT_MSG_ERROR,
5617 "%s: err in send_and_recv_msgs, ret=%d",
5618 __func__, ret);
5619 }
5620 return ret;
5621#else /* NL80211_SUPPORT */
5622 sigma_dut_print(dut, DUT_MSG_ERROR,
5623 "HE config enablement cannot be changed without NL80211_SUPPORT defined");
5624 return -1;
5625#endif /* NL80211_SUPPORT */
5626}
5627
5628
Amarnath Hullur Subramanyam58f2a6e2018-01-31 03:36:00 -08005629static void sta_reset_default_wcn(struct sigma_dut *dut, const char *intf,
5630 const char *type)
5631{
5632 char buf[60];
5633
5634 if (dut->program == PROGRAM_HE) {
5635 /* resetting phymode to auto in case of HE program */
5636 snprintf(buf, sizeof(buf), "iwpriv %s setphymode 0", intf);
5637 if (system(buf) != 0) {
5638 sigma_dut_print(dut, DUT_MSG_ERROR,
5639 "iwpriv %s setphymode failed", intf);
5640 }
5641
5642 /* remove all network profiles */
5643 remove_wpa_networks(intf);
Amarnath Hullur Subramanyam2538acc2018-02-02 16:15:25 -08005644
5645 /* Set nss to 1 and MCS 0-7 in case of testbed */
5646 if (type && strcasecmp(type, "Testbed") == 0) {
5647#ifdef NL80211_SUPPORT
5648 int ret;
5649#endif /* NL80211_SUPPORT */
5650
5651 snprintf(buf, sizeof(buf), "iwpriv %s nss 1", intf);
5652 if (system(buf) != 0) {
5653 sigma_dut_print(dut, DUT_MSG_ERROR,
5654 "iwpriv %s nss failed", intf);
5655 }
5656
5657#ifdef NL80211_SUPPORT
5658 ret = sta_set_he_mcs(dut, intf, HE_80_MCS0_7);
5659 if (ret) {
5660 sigma_dut_print(dut, DUT_MSG_ERROR,
5661 "Setting of MCS failed, ret:%d",
5662 ret);
5663 }
5664#endif /* NL80211_SUPPORT */
Amarnath Hullur Subramanyamc67621d2018-02-04 23:18:01 -08005665
5666 /* Disable STBC as default */
5667 wcn_sta_set_stbc(dut, intf, "0");
Amarnath Hullur Subramanyamd5bb5732018-02-22 15:50:38 -08005668
5669 /* Disable AMSDU as default */
5670 iwpriv_sta_set_amsdu(dut, intf, "0");
Amarnath Hullur Subramanyam474a17d2018-02-22 18:45:54 -08005671
5672#ifdef NL80211_SUPPORT
5673 /* HE fragmentation default off */
5674 if (sta_set_he_fragmentation(dut, intf,
5675 HE_FRAG_DISABLE)) {
5676 sigma_dut_print(dut, DUT_MSG_ERROR,
5677 "Setting of HE fragmentation failed");
5678 }
5679#endif /* NL80211_SUPPORT */
Amarnath Hullur Subramanyam4622a212018-02-23 12:12:14 -08005680
5681 /* Enable WEP/TKIP with HE capability in testbed */
5682 if (sta_set_heconfig_and_wep_tkip(dut, intf, 1)) {
5683 sigma_dut_print(dut, DUT_MSG_ERROR,
5684 "Enabling HE config with WEP/TKIP failed");
5685 }
Amarnath Hullur Subramanyam2538acc2018-02-02 16:15:25 -08005686 }
Amarnath Hullur Subramanyam58f2a6e2018-01-31 03:36:00 -08005687 }
5688}
5689
5690
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005691static int cmd_sta_reset_default(struct sigma_dut *dut,
5692 struct sigma_conn *conn,
5693 struct sigma_cmd *cmd)
5694{
5695 int cmd_sta_p2p_reset(struct sigma_dut *dut, struct sigma_conn *conn,
5696 struct sigma_cmd *cmd);
5697 const char *intf = get_param(cmd, "Interface");
5698 const char *type;
Amarnath Hullur Subramanyam9c381f52017-03-17 00:04:41 -07005699 const char *program = get_param(cmd, "program");
Ankita Bajaj0d5825b2017-10-25 16:20:17 +05305700 const char *dev_role = get_param(cmd, "DevRole");
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005701
Amarnath Hullur Subramanyam9c381f52017-03-17 00:04:41 -07005702 if (!program)
5703 program = get_param(cmd, "prog");
5704 dut->program = sigma_program_to_enum(program);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005705 dut->device_type = STA_unknown;
5706 type = get_param(cmd, "type");
5707 if (type && strcasecmp(type, "Testbed") == 0)
5708 dut->device_type = STA_testbed;
5709 if (type && strcasecmp(type, "DUT") == 0)
5710 dut->device_type = STA_dut;
5711
5712 if (dut->program == PROGRAM_TDLS) {
5713 /* Clear TDLS testing mode */
5714 wpa_command(intf, "SET tdls_disabled 0");
5715 wpa_command(intf, "SET tdls_testing 0");
5716 dut->no_tpk_expiration = 0;
Pradeep Reddy POTTETI8ce2a232016-10-28 12:17:32 +05305717 if (get_driver_type() == DRIVER_WCN) {
5718 /* Enable the WCN driver in TDLS Explicit trigger mode
5719 */
5720 wpa_command(intf, "SET tdls_external_control 0");
5721 wpa_command(intf, "SET tdls_trigger_control 0");
5722 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005723 }
5724
Amarnath Hullur Subramanyam9c381f52017-03-17 00:04:41 -07005725#ifdef MIRACAST
5726 if (dut->program == PROGRAM_WFD ||
5727 dut->program == PROGRAM_DISPLAYR2)
5728 miracast_sta_reset_default(dut, conn, cmd);
5729#endif /* MIRACAST */
5730
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005731 switch (get_driver_type()) {
5732 case DRIVER_ATHEROS:
5733 sta_reset_default_ath(dut, intf, type);
5734 break;
Amarnath Hullur Subramanyam58f2a6e2018-01-31 03:36:00 -08005735 case DRIVER_WCN:
5736 sta_reset_default_wcn(dut, intf, type);
5737 break;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005738 default:
5739 break;
5740 }
5741
5742#ifdef ANDROID_NAN
5743 if (dut->program == PROGRAM_NAN)
5744 nan_cmd_sta_reset_default(dut, conn, cmd);
5745#endif /* ANDROID_NAN */
5746
5747 if (dut->program == PROGRAM_HS2_R2) {
5748 unlink("SP/wi-fi.org/pps.xml");
5749 if (system("rm -r SP/*") != 0) {
5750 }
5751 unlink("next-client-cert.pem");
5752 unlink("next-client-key.pem");
5753 }
5754
5755 if (dut->program == PROGRAM_60GHZ) {
5756 const char *dev_role = get_param(cmd, "DevRole");
5757
5758 if (!dev_role) {
5759 send_resp(dut, conn, SIGMA_ERROR,
5760 "errorCode,Missing DevRole argument");
5761 return 0;
5762 }
5763
5764 if (strcasecmp(dev_role, "STA") == 0)
5765 dut->dev_role = DEVROLE_STA;
5766 else if (strcasecmp(dev_role, "PCP") == 0)
5767 dut->dev_role = DEVROLE_PCP;
5768 else {
5769 send_resp(dut, conn, SIGMA_ERROR,
5770 "errorCode,Unknown DevRole");
5771 return 0;
5772 }
5773
5774 if (dut->device_type == STA_unknown) {
5775 sigma_dut_print(dut, DUT_MSG_ERROR,
5776 "Device type is not STA testbed or DUT");
5777 send_resp(dut, conn, SIGMA_ERROR,
5778 "errorCode,Unknown device type");
5779 return 0;
5780 }
5781 }
5782
5783 wpa_command(intf, "WPS_ER_STOP");
5784 wpa_command(intf, "FLUSH");
vamsi krishnaf39bc1e2017-08-23 17:37:53 +05305785 wpa_command(intf, "ERP_FLUSH");
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005786 wpa_command(intf, "SET radio_disabled 0");
5787
5788 if (dut->tmp_mac_addr && dut->set_macaddr) {
5789 dut->tmp_mac_addr = 0;
5790 if (system(dut->set_macaddr) != 0) {
5791 sigma_dut_print(dut, DUT_MSG_INFO, "Failed to clear "
5792 "temporary MAC address");
5793 }
5794 }
5795
5796 set_ps(intf, dut, 0);
5797
5798 if (dut->program == PROGRAM_HS2 || dut->program == PROGRAM_HS2_R2) {
5799 wpa_command(intf, "SET interworking 1");
5800 wpa_command(intf, "SET hs20 1");
5801 }
5802
Deepak Dhamdhere0fe0e452017-12-18 14:52:09 -08005803 if (dut->program == PROGRAM_HS2_R2 ||
5804 dut->program == PROGRAM_OCE) {
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005805 wpa_command(intf, "SET pmf 1");
5806 } else {
5807 wpa_command(intf, "SET pmf 0");
5808 }
5809
5810 hs2_clear_credentials(intf);
5811 wpa_command(intf, "SET hessid 00:00:00:00:00:00");
5812 wpa_command(intf, "SET access_network_type 15");
5813
5814 static_ip_file(0, NULL, NULL, NULL);
5815 kill_dhcp_client(dut, intf);
5816 clear_ip_addr(dut, intf);
5817
5818 dut->er_oper_performed = 0;
5819 dut->er_oper_bssid[0] = '\0';
5820
priyadharshini gowthamanad6cbba2016-10-04 10:39:58 -07005821 if (dut->program == PROGRAM_LOC) {
5822 /* Disable Interworking by default */
5823 wpa_command(get_station_ifname(), "SET interworking 0");
5824 }
5825
Ashwini Patil00402582017-04-13 12:29:39 +05305826 if (dut->program == PROGRAM_MBO) {
5827 free(dut->non_pref_ch_list);
5828 dut->non_pref_ch_list = NULL;
Ashwini Patil5acd7382017-04-13 15:55:04 +05305829 free(dut->btm_query_cand_list);
5830 dut->btm_query_cand_list = NULL;
Ashwini Patilc63161e2017-04-13 16:30:23 +05305831 wpa_command(intf, "SET reject_btm_req_reason 0");
Ashwini Patila75de5a2017-04-13 16:35:05 +05305832 wpa_command(intf, "SET ignore_assoc_disallow 0");
Ashwini Patild174f2c2017-04-13 16:49:46 +05305833 wpa_command(intf, "SET gas_address3 0");
Ashwini Patil9183fdb2017-04-13 16:58:25 +05305834 wpa_command(intf, "SET roaming 1");
Ashwini Patil00402582017-04-13 12:29:39 +05305835 }
5836
Jouni Malinen3c367e82017-06-23 17:01:47 +03005837 free(dut->rsne_override);
5838 dut->rsne_override = NULL;
5839
Jouni Malinen68143132017-09-02 02:34:08 +03005840 free(dut->sae_commit_override);
5841 dut->sae_commit_override = NULL;
5842
Jouni Malinend86e5822017-08-29 03:55:32 +03005843 dut->dpp_conf_id = -1;
Jouni Malinenb1dd21f2017-11-13 19:14:29 +02005844 free(dut->dpp_peer_uri);
5845 dut->dpp_peer_uri = NULL;
Jouni Malinen63d50412017-11-24 11:55:38 +02005846 dut->dpp_local_bootstrap = -1;
Jouni Malinen5011fb52017-12-05 21:00:15 +02005847 wpa_command(intf, "SET dpp_config_processing 2");
Jouni Malinend86e5822017-08-29 03:55:32 +03005848
Jouni Malinenfac9cad2017-10-10 18:35:55 +03005849 wpa_command(intf, "VENDOR_ELEM_REMOVE 13 *");
5850
vamsi krishnaa2799492017-12-05 14:28:01 +05305851 if (dut->program == PROGRAM_OCE) {
Ankita Bajaja2cb5672017-10-25 16:08:28 +05305852 wpa_command(intf, "SET oce 1");
vamsi krishnaa2799492017-12-05 14:28:01 +05305853 wpa_command(intf, "SET disable_fils 0");
Ankita Bajaj1bde7942018-01-09 19:15:01 +05305854 wpa_command(intf, "FILS_HLP_REQ_FLUSH");
5855 dut->fils_hlp = 0;
5856#ifdef ANDROID
5857 hlp_thread_cleanup(dut);
5858#endif /* ANDROID */
vamsi krishnaa2799492017-12-05 14:28:01 +05305859 }
Ankita Bajaja2cb5672017-10-25 16:08:28 +05305860
Sunil Dutt076081f2018-02-05 19:45:50 +05305861#ifdef NL80211_SUPPORT
Sunil Dutt44595082018-02-12 19:41:45 +05305862 if (get_driver_type() == DRIVER_WCN &&
5863 dut->config_rsnie == 1) {
5864 dut->config_rsnie = 0;
5865 sta_config_rsnie(dut, 0);
Sunil Dutt076081f2018-02-05 19:45:50 +05305866 }
5867#endif /* NL80211_SUPPORT */
5868
Sunil Duttfebf8a82018-02-09 18:50:13 +05305869 if (dev_role && strcasecmp(dev_role, "STA-CFON") == 0) {
5870 dut->dev_role = DEVROLE_STA_CFON;
5871 return sta_cfon_reset_default(dut, conn, cmd);
5872 }
5873
5874 if (dut->program != PROGRAM_VHT)
5875 return cmd_sta_p2p_reset(dut, conn, cmd);
5876
Priyadharshini Gowthamana7dfd492015-11-09 14:34:08 -08005877 return 1;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005878}
5879
5880
5881static int cmd_sta_get_events(struct sigma_dut *dut, struct sigma_conn *conn,
5882 struct sigma_cmd *cmd)
5883{
5884 const char *program = get_param(cmd, "Program");
5885
5886 if (program == NULL)
5887 return -1;
5888#ifdef ANDROID_NAN
5889 if (strcasecmp(program, "NAN") == 0)
5890 return nan_cmd_sta_get_events(dut, conn, cmd);
5891#endif /* ANDROID_NAN */
5892 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Unsupported parameter");
5893 return 0;
5894}
5895
5896
5897static int cmd_sta_exec_action(struct sigma_dut *dut, struct sigma_conn *conn,
5898 struct sigma_cmd *cmd)
5899{
5900 const char *program = get_param(cmd, "Prog");
5901
5902 if (program == NULL)
5903 return -1;
5904#ifdef ANDROID_NAN
5905 if (strcasecmp(program, "NAN") == 0)
5906 return nan_cmd_sta_exec_action(dut, conn, cmd);
5907#endif /* ANDROID_NAN */
priyadharshini gowthamand66913a2016-07-29 15:11:17 -07005908 if (strcasecmp(program, "Loc") == 0)
5909 return loc_cmd_sta_exec_action(dut, conn, cmd);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005910 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Unsupported parameter");
5911 return 0;
5912}
5913
5914
5915static int cmd_sta_set_11n(struct sigma_dut *dut, struct sigma_conn *conn,
5916 struct sigma_cmd *cmd)
5917{
5918 const char *intf = get_param(cmd, "Interface");
5919 const char *val, *mcs32, *rate;
5920
5921 val = get_param(cmd, "GREENFIELD");
5922 if (val) {
5923 if (strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0) {
5924 /* Enable GD */
5925 send_resp(dut, conn, SIGMA_ERROR,
5926 "ErrorCode,GF not supported");
5927 return 0;
5928 }
5929 }
5930
5931 val = get_param(cmd, "SGI20");
5932 if (val) {
5933 switch (get_driver_type()) {
5934 case DRIVER_ATHEROS:
5935 ath_sta_set_sgi(dut, intf, val);
5936 break;
5937 default:
5938 send_resp(dut, conn, SIGMA_ERROR,
5939 "ErrorCode,SGI20 not supported");
5940 return 0;
5941 }
5942 }
5943
5944 mcs32 = get_param(cmd, "MCS32"); /* HT Duplicate Mode Enable/Disable */
5945 rate = get_param(cmd, "MCS_FIXEDRATE"); /* Fixed MCS rate (0..31) */
5946 if (mcs32 && rate) {
5947 /* TODO */
5948 send_resp(dut, conn, SIGMA_ERROR,
5949 "ErrorCode,MCS32,MCS_FIXEDRATE not supported");
5950 return 0;
5951 } else if (mcs32 && !rate) {
5952 /* TODO */
5953 send_resp(dut, conn, SIGMA_ERROR,
5954 "ErrorCode,MCS32 not supported");
5955 return 0;
5956 } else if (!mcs32 && rate) {
5957 switch (get_driver_type()) {
5958 case DRIVER_ATHEROS:
priyadharshini gowthamane5e25172015-12-08 14:53:48 -08005959 novap_reset(dut, intf);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005960 ath_sta_set_11nrates(dut, intf, rate);
5961 break;
5962 default:
5963 send_resp(dut, conn, SIGMA_ERROR,
5964 "ErrorCode,MCS32_FIXEDRATE not supported");
5965 return 0;
5966 }
5967 }
5968
5969 return cmd_sta_set_wireless_common(intf, dut, conn, cmd);
5970}
5971
5972
5973static int cmd_sta_set_wireless_vht(struct sigma_dut *dut,
5974 struct sigma_conn *conn,
5975 struct sigma_cmd *cmd)
5976{
5977 const char *intf = get_param(cmd, "Interface");
5978 const char *val;
5979 char buf[30];
5980 int tkip = -1;
5981 int wep = -1;
5982
5983 val = get_param(cmd, "SGI80");
5984 if (val) {
5985 int sgi80;
5986
5987 sgi80 = strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0;
5988 snprintf(buf, sizeof(buf), "iwpriv %s shortgi %d", intf, sgi80);
5989 if (system(buf) != 0) {
5990 sigma_dut_print(dut, DUT_MSG_ERROR,
5991 "iwpriv shortgi failed");
5992 }
5993 }
5994
5995 val = get_param(cmd, "TxBF");
5996 if (val && (strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0)) {
5997 snprintf(buf, sizeof(buf), "iwpriv %s vhtsubfee 1", intf);
5998 if (system(buf) != 0) {
5999 sigma_dut_print(dut, DUT_MSG_ERROR,
6000 "iwpriv vhtsubfee failed");
6001 }
6002 snprintf(buf, sizeof(buf), "iwpriv %s vhtsubfer 1", intf);
6003 if (system(buf) != 0) {
6004 sigma_dut_print(dut, DUT_MSG_ERROR,
6005 "iwpriv vhtsubfer failed");
6006 }
6007 }
6008
6009 val = get_param(cmd, "MU_TxBF");
6010 if (val && (strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0)) {
6011 switch (get_driver_type()) {
6012 case DRIVER_ATHEROS:
6013 ath_sta_set_txsp_stream(dut, intf, "1SS");
6014 ath_sta_set_rxsp_stream(dut, intf, "1SS");
6015 case DRIVER_WCN:
6016 if (wcn_sta_set_sp_stream(dut, intf, "1SS") < 0) {
6017 send_resp(dut, conn, SIGMA_ERROR,
6018 "ErrorCode,Failed to set RX/TXSP_STREAM");
6019 return 0;
6020 }
6021 default:
6022 sigma_dut_print(dut, DUT_MSG_ERROR,
6023 "Setting SP_STREAM not supported");
6024 break;
6025 }
6026 snprintf(buf, sizeof(buf), "iwpriv %s vhtmubfee 1", intf);
6027 if (system(buf) != 0) {
6028 sigma_dut_print(dut, DUT_MSG_ERROR,
6029 "iwpriv vhtmubfee failed");
6030 }
6031 snprintf(buf, sizeof(buf), "iwpriv %s vhtmubfer 1", intf);
6032 if (system(buf) != 0) {
6033 sigma_dut_print(dut, DUT_MSG_ERROR,
6034 "iwpriv vhtmubfer failed");
6035 }
6036 }
6037
6038 val = get_param(cmd, "LDPC");
6039 if (val) {
6040 int ldpc;
6041
6042 ldpc = strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0;
6043 snprintf(buf, sizeof(buf), "iwpriv %s ldpc %d", intf, ldpc);
6044 if (system(buf) != 0) {
6045 sigma_dut_print(dut, DUT_MSG_ERROR,
6046 "iwpriv ldpc failed");
6047 }
6048 }
6049
Amarnath Hullur Subramanyam7bae60e2018-01-31 03:46:50 -08006050 val = get_param(cmd, "BCC");
6051 if (val) {
6052 int bcc;
6053
6054 bcc = strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0;
6055 /* use LDPC iwpriv itself to set bcc coding, bcc coding
6056 * is mutually exclusive to bcc */
6057 snprintf(buf, sizeof(buf), "iwpriv %s ldpc %d", intf, !bcc);
6058 if (system(buf) != 0) {
6059 sigma_dut_print(dut, DUT_MSG_ERROR,
6060 "Enabling/Disabling of BCC failed");
6061 }
6062 }
6063
Jouni Malinencd4e3c32015-10-29 12:39:56 +02006064 val = get_param(cmd, "opt_md_notif_ie");
6065 if (val) {
6066 char *result = NULL;
6067 char delim[] = ";";
6068 char token[30];
6069 int value, config_val = 0;
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05306070 char *saveptr;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02006071
Peng Xub8fc5cc2017-05-10 17:27:28 -07006072 strlcpy(token, val, sizeof(token));
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05306073 result = strtok_r(token, delim, &saveptr);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02006074
6075 /* Extract the NSS information */
6076 if (result) {
6077 value = atoi(result);
6078 switch (value) {
6079 case 1:
6080 config_val = 1;
6081 break;
6082 case 2:
6083 config_val = 3;
6084 break;
6085 case 3:
6086 config_val = 7;
6087 break;
6088 case 4:
6089 config_val = 15;
6090 break;
6091 default:
6092 config_val = 3;
6093 break;
6094 }
6095
6096 snprintf(buf, sizeof(buf), "iwpriv %s rxchainmask %d",
6097 intf, config_val);
6098 if (system(buf) != 0) {
6099 sigma_dut_print(dut, DUT_MSG_ERROR,
6100 "iwpriv rxchainmask failed");
6101 }
6102
6103 snprintf(buf, sizeof(buf), "iwpriv %s txchainmask %d",
6104 intf, config_val);
6105 if (system(buf) != 0) {
6106 sigma_dut_print(dut, DUT_MSG_ERROR,
6107 "iwpriv txchainmask failed");
6108 }
6109 }
6110
6111 /* Extract the channel width information */
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05306112 result = strtok_r(NULL, delim, &saveptr);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02006113 if (result) {
6114 value = atoi(result);
6115 switch (value) {
6116 case 20:
6117 config_val = 0;
6118 break;
6119 case 40:
6120 config_val = 1;
6121 break;
6122 case 80:
6123 config_val = 2;
6124 break;
6125 case 160:
6126 config_val = 3;
6127 break;
6128 default:
6129 config_val = 2;
6130 break;
6131 }
6132
6133 dut->chwidth = config_val;
6134
6135 snprintf(buf, sizeof(buf), "iwpriv %s chwidth %d",
6136 intf, config_val);
6137 if (system(buf) != 0) {
6138 sigma_dut_print(dut, DUT_MSG_ERROR,
6139 "iwpriv chwidth failed");
6140 }
6141 }
6142
6143 snprintf(buf, sizeof(buf), "iwpriv %s opmode_notify 1", intf);
6144 if (system(buf) != 0) {
6145 sigma_dut_print(dut, DUT_MSG_ERROR,
6146 "iwpriv opmode_notify failed");
6147 }
6148 }
6149
6150 val = get_param(cmd, "nss_mcs_cap");
6151 if (val) {
6152 int nss, mcs;
6153 char token[20];
6154 char *result = NULL;
6155 unsigned int vht_mcsmap = 0;
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05306156 char *saveptr;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02006157
Peng Xub8fc5cc2017-05-10 17:27:28 -07006158 strlcpy(token, val, sizeof(token));
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05306159 result = strtok_r(token, ";", &saveptr);
Pradeep Reddy POTTETIcd649a22016-01-29 12:55:59 +05306160 if (!result) {
6161 sigma_dut_print(dut, DUT_MSG_ERROR,
6162 "VHT NSS not specified");
6163 return 0;
6164 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02006165 nss = atoi(result);
6166
6167 snprintf(buf, sizeof(buf), "iwpriv %s nss %d", intf, nss);
6168 if (system(buf) != 0) {
6169 sigma_dut_print(dut, DUT_MSG_ERROR,
6170 "iwpriv nss failed");
6171 }
6172
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05306173 result = strtok_r(NULL, ";", &saveptr);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02006174 if (result == NULL) {
6175 sigma_dut_print(dut, DUT_MSG_ERROR,
6176 "VHTMCS NOT SPECIFIED!");
6177 return 0;
6178 }
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05306179 result = strtok_r(result, "-", &saveptr);
6180 result = strtok_r(NULL, "-", &saveptr);
Pradeep Reddy POTTETIcd649a22016-01-29 12:55:59 +05306181 if (!result) {
6182 sigma_dut_print(dut, DUT_MSG_ERROR,
6183 "VHT MCS not specified");
6184 return 0;
6185 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02006186 mcs = atoi(result);
6187
6188 snprintf(buf, sizeof(buf), "iwpriv %s vhtmcs %d", intf, mcs);
6189 if (system(buf) != 0) {
6190 sigma_dut_print(dut, DUT_MSG_ERROR,
6191 "iwpriv mcs failed");
6192 }
6193
6194 switch (nss) {
6195 case 1:
6196 switch (mcs) {
6197 case 7:
6198 vht_mcsmap = 0xfffc;
6199 break;
6200 case 8:
6201 vht_mcsmap = 0xfffd;
6202 break;
6203 case 9:
6204 vht_mcsmap = 0xfffe;
6205 break;
6206 default:
6207 vht_mcsmap = 0xfffe;
6208 break;
6209 }
6210 break;
6211 case 2:
6212 switch (mcs) {
6213 case 7:
6214 vht_mcsmap = 0xfff0;
6215 break;
6216 case 8:
6217 vht_mcsmap = 0xfff5;
6218 break;
6219 case 9:
6220 vht_mcsmap = 0xfffa;
6221 break;
6222 default:
6223 vht_mcsmap = 0xfffa;
6224 break;
6225 }
6226 break;
6227 case 3:
6228 switch (mcs) {
6229 case 7:
6230 vht_mcsmap = 0xffc0;
6231 break;
6232 case 8:
6233 vht_mcsmap = 0xffd5;
6234 break;
6235 case 9:
6236 vht_mcsmap = 0xffea;
6237 break;
6238 default:
6239 vht_mcsmap = 0xffea;
6240 break;
6241 }
6242 break;
6243 default:
6244 vht_mcsmap = 0xffea;
6245 break;
6246 }
6247 snprintf(buf, sizeof(buf), "iwpriv %s vht_mcsmap 0x%04x",
6248 intf, vht_mcsmap);
6249 if (system(buf) != 0) {
6250 sigma_dut_print(dut, DUT_MSG_ERROR,
6251 "iwpriv vht_mcsmap failed");
6252 }
6253 }
6254
6255 /* UNSUPPORTED: val = get_param(cmd, "Tx_lgi_rate"); */
6256
6257 val = get_param(cmd, "Vht_tkip");
6258 if (val)
6259 tkip = strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0;
6260
6261 val = get_param(cmd, "Vht_wep");
6262 if (val)
6263 wep = strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0;
6264
6265 if (tkip != -1 || wep != -1) {
6266 if ((tkip == 1 && wep != 0) || (wep == 1 && tkip != 0)) {
6267 snprintf(buf, sizeof(buf), "iwpriv %s htweptkip 1",
6268 intf);
6269 } else if ((tkip == 0 && wep != 1) || (wep == 0 && tkip != 1)) {
6270 snprintf(buf, sizeof(buf), "iwpriv %s htweptkip 0",
6271 intf);
6272 } else {
6273 sigma_dut_print(dut, DUT_MSG_ERROR,
6274 "ErrorCode,mixed mode of VHT TKIP/WEP not supported");
6275 return 0;
6276 }
6277
6278 if (system(buf) != 0) {
6279 sigma_dut_print(dut, DUT_MSG_ERROR,
6280 "iwpriv htweptkip failed");
6281 }
6282 }
6283
6284 val = get_param(cmd, "txBandwidth");
6285 if (val) {
6286 switch (get_driver_type()) {
Amarnath Hullur Subramanyam4f860292018-01-31 03:49:35 -08006287 case DRIVER_WCN:
6288 if (wcn_sta_set_width(dut, intf, val) < 0) {
6289 send_resp(dut, conn, SIGMA_ERROR,
6290 "ErrorCode,Failed to set txBandwidth");
6291 return 0;
6292 }
6293 break;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02006294 case DRIVER_ATHEROS:
6295 if (ath_set_width(dut, conn, intf, val) < 0) {
6296 send_resp(dut, conn, SIGMA_ERROR,
6297 "ErrorCode,Failed to set txBandwidth");
6298 return 0;
6299 }
6300 break;
6301 default:
6302 sigma_dut_print(dut, DUT_MSG_ERROR,
6303 "Setting txBandwidth not supported");
6304 break;
6305 }
6306 }
6307
6308 return cmd_sta_set_wireless_common(intf, dut, conn, cmd);
6309}
6310
6311
6312static int sta_set_wireless_60g(struct sigma_dut *dut,
6313 struct sigma_conn *conn,
6314 struct sigma_cmd *cmd)
6315{
6316 const char *dev_role = get_param(cmd, "DevRole");
6317
6318 if (!dev_role) {
6319 send_resp(dut, conn, SIGMA_INVALID,
6320 "ErrorCode,DevRole not specified");
6321 return 0;
6322 }
6323
6324 if (strcasecmp(dev_role, "PCP") == 0)
6325 return sta_set_60g_pcp(dut, conn, cmd);
6326 if (strcasecmp(dev_role, "STA") == 0)
6327 return sta_set_60g_sta(dut, conn, cmd);
6328 send_resp(dut, conn, SIGMA_INVALID,
6329 "ErrorCode,DevRole not supported");
6330 return 0;
6331}
6332
6333
Ankita Bajaj0d5825b2017-10-25 16:20:17 +05306334static int sta_set_wireless_oce(struct sigma_dut *dut, struct sigma_conn *conn,
6335 struct sigma_cmd *cmd)
6336{
6337 int status;
6338 const char *intf = get_param(cmd, "Interface");
6339 const char *val = get_param(cmd, "DevRole");
6340
6341 if (val && strcasecmp(val, "STA-CFON") == 0) {
6342 status = sta_cfon_set_wireless(dut, conn, cmd);
6343 if (status)
6344 return status;
6345 }
6346 return cmd_sta_set_wireless_common(intf, dut, conn, cmd);
6347}
6348
6349
Jouni Malinencd4e3c32015-10-29 12:39:56 +02006350static int cmd_sta_set_wireless(struct sigma_dut *dut, struct sigma_conn *conn,
6351 struct sigma_cmd *cmd)
6352{
6353 const char *val;
6354
6355 val = get_param(cmd, "Program");
6356 if (val) {
6357 if (strcasecmp(val, "11n") == 0)
6358 return cmd_sta_set_11n(dut, conn, cmd);
Amarnath Hullur Subramanyam4f860292018-01-31 03:49:35 -08006359 if (strcasecmp(val, "VHT") == 0 || strcasecmp(val, "HE") == 0)
Jouni Malinencd4e3c32015-10-29 12:39:56 +02006360 return cmd_sta_set_wireless_vht(dut, conn, cmd);
6361 if (strcasecmp(val, "60ghz") == 0)
6362 return sta_set_wireless_60g(dut, conn, cmd);
Ankita Bajaj0d5825b2017-10-25 16:20:17 +05306363 if (strcasecmp(val, "OCE") == 0)
6364 return sta_set_wireless_oce(dut, conn, cmd);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02006365 send_resp(dut, conn, SIGMA_ERROR,
6366 "ErrorCode,Program value not supported");
6367 } else {
6368 send_resp(dut, conn, SIGMA_ERROR,
6369 "ErrorCode,Program argument not available");
6370 }
6371
6372 return 0;
6373}
6374
6375
6376static void ath_sta_inject_frame(struct sigma_dut *dut, const char *intf,
6377 int tid)
6378{
6379 char buf[100];
6380 int tid_to_dscp [] = { 0x00, 0x20, 0x40, 0x60, 0x80, 0xa0, 0xc0, 0xe0 };
6381
Pradeep Reddy POTTETId31d1322016-10-13 17:22:03 +05306382 if (tid < 0 ||
6383 tid >= (int) (sizeof(tid_to_dscp) / sizeof(tid_to_dscp[0]))) {
6384 sigma_dut_print(dut, DUT_MSG_ERROR, "Unsupported TID: %d", tid);
6385 return;
6386 }
6387
Jouni Malinencd4e3c32015-10-29 12:39:56 +02006388 /*
6389 * Two ways to ensure that addba request with a
6390 * non zero TID could be sent out. EV 117296
6391 */
6392 snprintf(buf, sizeof(buf),
6393 "ping -c 8 -Q %d `arp -a | grep wlan0 | awk '{print $2}' | tr -d '()'`",
6394 tid);
6395 if (system(buf) != 0) {
6396 sigma_dut_print(dut, DUT_MSG_ERROR,
6397 "Ping did not send out");
6398 }
6399
6400 snprintf(buf, sizeof(buf),
6401 "iwconfig %s | grep Access | awk '{print $6}' > %s",
6402 intf, VI_QOS_TMP_FILE);
6403 if (system(buf) != 0)
6404 return;
6405
6406 snprintf(buf, sizeof(buf),
6407 "ifconfig %s | grep HWaddr | cut -b 39-56 >> %s",
6408 intf, VI_QOS_TMP_FILE);
6409 if (system(buf) != 0)
6410 sigma_dut_print(dut, DUT_MSG_ERROR, "HWaddr matching failed");
6411
6412 snprintf(buf,sizeof(buf), "sed -n '3,$p' %s >> %s",
6413 VI_QOS_REFFILE, VI_QOS_TMP_FILE);
6414 if (system(buf) != 0) {
6415 sigma_dut_print(dut, DUT_MSG_ERROR,
6416 "VI_QOS_TEMP_FILE generation error failed");
6417 }
6418 snprintf(buf, sizeof(buf), "sed '5 c %x' %s > %s",
6419 tid_to_dscp[tid], VI_QOS_TMP_FILE, VI_QOS_FILE);
6420 if (system(buf) != 0) {
6421 sigma_dut_print(dut, DUT_MSG_ERROR,
6422 "VI_QOS_FILE generation failed");
6423 }
6424
6425 snprintf(buf, sizeof(buf), "sed '5 c %x' %s > %s",
6426 tid_to_dscp[tid], VI_QOS_TMP_FILE, VI_QOS_FILE);
6427 if (system(buf) != 0) {
6428 sigma_dut_print(dut, DUT_MSG_ERROR,
6429 "VI_QOS_FILE generation failed");
6430 }
6431
6432 snprintf(buf, sizeof(buf), "ethinject %s %s", intf, VI_QOS_FILE);
6433 if (system(buf) != 0) {
6434 }
6435}
6436
6437
6438static int ath_sta_send_addba(struct sigma_dut *dut, struct sigma_conn *conn,
6439 struct sigma_cmd *cmd)
6440{
6441 const char *intf = get_param(cmd, "Interface");
6442 const char *val;
6443 int tid = 0;
6444 char buf[100];
6445
6446 val = get_param(cmd, "TID");
6447 if (val) {
6448 tid = atoi(val);
6449 if (tid)
6450 ath_sta_inject_frame(dut, intf, tid);
6451 }
6452
6453 /* Command sequence for ADDBA request on Peregrine based devices */
6454 snprintf(buf, sizeof(buf), "iwpriv %s setaddbaoper 1", intf);
6455 if (system(buf) != 0) {
6456 sigma_dut_print(dut, DUT_MSG_ERROR,
6457 "iwpriv setaddbaoper failed");
6458 }
6459
6460 snprintf(buf, sizeof(buf), "wifitool %s senddelba 1 %d 1 4", intf, tid);
6461 if (system(buf) != 0) {
6462 sigma_dut_print(dut, DUT_MSG_ERROR,
6463 "wifitool senddelba failed");
6464 }
6465
6466 snprintf(buf, sizeof(buf), "wifitool %s sendaddba 1 %d 64", intf, tid);
6467 if (system(buf) != 0) {
6468 sigma_dut_print(dut, DUT_MSG_ERROR,
6469 "wifitool sendaddba failed");
6470 }
6471
6472 /* UNSUPPORTED: val = get_param(cmd, "Dest_mac"); */
6473
6474 return 1;
6475}
6476
6477
Lior David9981b512017-01-20 13:16:40 +02006478#ifdef __linux__
6479
6480static int wil6210_send_addba(struct sigma_dut *dut, const char *dest_mac,
6481 int agg_size)
6482{
6483 char dir[128], buf[128];
6484 FILE *f;
6485 regex_t re;
6486 regmatch_t m[2];
6487 int rc, ret = -1, vring_id, found;
6488
6489 if (wil6210_get_debugfs_dir(dut, dir, sizeof(dir))) {
6490 sigma_dut_print(dut, DUT_MSG_ERROR,
6491 "failed to get wil6210 debugfs dir");
6492 return -1;
6493 }
6494
6495 snprintf(buf, sizeof(buf), "%s/vrings", dir);
6496 f = fopen(buf, "r");
6497 if (!f) {
6498 sigma_dut_print(dut, DUT_MSG_ERROR, "failed to open: %s", buf);
6499 return -1;
6500 }
6501
6502 if (regcomp(&re, "VRING tx_[ \t]*([0-9]+)", REG_EXTENDED)) {
6503 sigma_dut_print(dut, DUT_MSG_ERROR, "regcomp failed");
6504 goto out;
6505 }
6506
6507 /* find TX VRING for the mac address */
6508 found = 0;
6509 while (fgets(buf, sizeof(buf), f)) {
6510 if (strcasestr(buf, dest_mac)) {
6511 found = 1;
6512 break;
6513 }
6514 }
6515
6516 if (!found) {
6517 sigma_dut_print(dut, DUT_MSG_ERROR,
6518 "no TX VRING for %s", dest_mac);
6519 goto out;
6520 }
6521
6522 /* extract VRING ID, "VRING tx_<id> = {" */
6523 if (!fgets(buf, sizeof(buf), f)) {
6524 sigma_dut_print(dut, DUT_MSG_ERROR,
6525 "no VRING start line for %s", dest_mac);
6526 goto out;
6527 }
6528
6529 rc = regexec(&re, buf, 2, m, 0);
6530 regfree(&re);
6531 if (rc || m[1].rm_so < 0) {
6532 sigma_dut_print(dut, DUT_MSG_ERROR,
6533 "no VRING TX ID for %s", dest_mac);
6534 goto out;
6535 }
6536 buf[m[1].rm_eo] = 0;
6537 vring_id = atoi(&buf[m[1].rm_so]);
6538
6539 /* send the addba command */
6540 fclose(f);
6541 snprintf(buf, sizeof(buf), "%s/back", dir);
6542 f = fopen(buf, "w");
6543 if (!f) {
6544 sigma_dut_print(dut, DUT_MSG_ERROR,
6545 "failed to open: %s", buf);
6546 return -1;
6547 }
6548
6549 fprintf(f, "add %d %d\n", vring_id, agg_size);
6550
6551 ret = 0;
6552
6553out:
6554 fclose(f);
6555
6556 return ret;
6557}
6558
6559
Jouni Malinencd4e3c32015-10-29 12:39:56 +02006560static int send_addba_60g(struct sigma_dut *dut, struct sigma_conn *conn,
6561 struct sigma_cmd *cmd)
6562{
6563 const char *val;
6564 int tid = 0;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02006565
6566 val = get_param(cmd, "TID");
6567 if (val) {
6568 tid = atoi(val);
6569 if (tid != 0) {
6570 sigma_dut_print(dut, DUT_MSG_ERROR,
6571 "Ignore TID %d for send_addba use TID 0 for 60g since only 0 required on TX",
6572 tid);
6573 }
6574 }
6575
6576 val = get_param(cmd, "Dest_mac");
6577 if (!val) {
6578 sigma_dut_print(dut, DUT_MSG_ERROR,
6579 "Currently not supporting addba for 60G without Dest_mac");
6580 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
6581 }
6582
Lior David9981b512017-01-20 13:16:40 +02006583 if (wil6210_send_addba(dut, val, dut->back_rcv_buf))
Jouni Malinencd4e3c32015-10-29 12:39:56 +02006584 return -1;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02006585
6586 return 1;
6587}
6588
Lior David9981b512017-01-20 13:16:40 +02006589#endif /* __linux__ */
6590
Jouni Malinencd4e3c32015-10-29 12:39:56 +02006591
6592static int cmd_sta_send_addba(struct sigma_dut *dut, struct sigma_conn *conn,
6593 struct sigma_cmd *cmd)
6594{
6595 switch (get_driver_type()) {
6596 case DRIVER_ATHEROS:
6597 return ath_sta_send_addba(dut, conn, cmd);
Lior David9981b512017-01-20 13:16:40 +02006598#ifdef __linux__
Jouni Malinencd4e3c32015-10-29 12:39:56 +02006599 case DRIVER_WIL6210:
6600 return send_addba_60g(dut, conn, cmd);
Lior David9981b512017-01-20 13:16:40 +02006601#endif /* __linux__ */
Jouni Malinencd4e3c32015-10-29 12:39:56 +02006602 default:
6603 /*
6604 * There is no driver specific implementation for other drivers.
6605 * Ignore the command and report COMPLETE since the following
6606 * throughput test operation will end up sending ADDBA anyway.
6607 */
6608 return 1;
6609 }
6610}
6611
6612
6613int inject_eth_frame(int s, const void *data, size_t len,
6614 unsigned short ethtype, char *dst, char *src)
6615{
6616 struct iovec iov[4] = {
6617 {
6618 .iov_base = dst,
6619 .iov_len = ETH_ALEN,
6620 },
6621 {
6622 .iov_base = src,
6623 .iov_len = ETH_ALEN,
6624 },
6625 {
6626 .iov_base = &ethtype,
6627 .iov_len = sizeof(unsigned short),
6628 },
6629 {
6630 .iov_base = (void *) data,
6631 .iov_len = len,
6632 }
6633 };
6634 struct msghdr msg = {
6635 .msg_name = NULL,
6636 .msg_namelen = 0,
6637 .msg_iov = iov,
6638 .msg_iovlen = 4,
6639 .msg_control = NULL,
6640 .msg_controllen = 0,
6641 .msg_flags = 0,
6642 };
6643
6644 return sendmsg(s, &msg, 0);
6645}
6646
6647#if defined(__linux__) || defined(__QNXNTO__)
6648
6649int inject_frame(int s, const void *data, size_t len, int encrypt)
6650{
6651#define IEEE80211_RADIOTAP_F_WEP 0x04
6652#define IEEE80211_RADIOTAP_F_FRAG 0x08
6653 unsigned char rtap_hdr[] = {
6654 0x00, 0x00, /* radiotap version */
6655 0x0e, 0x00, /* radiotap length */
6656 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
6657 IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
6658 0x00, /* padding */
6659 0x00, 0x00, /* RX and TX flags to indicate that */
6660 0x00, 0x00, /* this is the injected frame directly */
6661 };
6662 struct iovec iov[2] = {
6663 {
6664 .iov_base = &rtap_hdr,
6665 .iov_len = sizeof(rtap_hdr),
6666 },
6667 {
6668 .iov_base = (void *) data,
6669 .iov_len = len,
6670 }
6671 };
6672 struct msghdr msg = {
6673 .msg_name = NULL,
6674 .msg_namelen = 0,
6675 .msg_iov = iov,
6676 .msg_iovlen = 2,
6677 .msg_control = NULL,
6678 .msg_controllen = 0,
6679 .msg_flags = 0,
6680 };
6681
6682 if (encrypt)
6683 rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
6684
6685 return sendmsg(s, &msg, 0);
6686}
6687
6688
6689int open_monitor(const char *ifname)
6690{
6691#ifdef __QNXNTO__
6692 struct sockaddr_dl ll;
6693 int s;
6694
6695 memset(&ll, 0, sizeof(ll));
6696 ll.sdl_family = AF_LINK;
6697 ll.sdl_index = if_nametoindex(ifname);
6698 if (ll.sdl_index == 0) {
6699 perror("if_nametoindex");
6700 return -1;
6701 }
6702 s = socket(PF_INET, SOCK_RAW, 0);
6703#else /* __QNXNTO__ */
6704 struct sockaddr_ll ll;
6705 int s;
6706
6707 memset(&ll, 0, sizeof(ll));
6708 ll.sll_family = AF_PACKET;
6709 ll.sll_ifindex = if_nametoindex(ifname);
6710 if (ll.sll_ifindex == 0) {
6711 perror("if_nametoindex");
6712 return -1;
6713 }
6714 s = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
6715#endif /* __QNXNTO__ */
6716 if (s < 0) {
6717 perror("socket[PF_PACKET,SOCK_RAW]");
6718 return -1;
6719 }
6720
6721 if (bind(s, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
6722 perror("monitor socket bind");
6723 close(s);
6724 return -1;
6725 }
6726
6727 return s;
6728}
6729
6730
6731static int hex2num(char c)
6732{
6733 if (c >= '0' && c <= '9')
6734 return c - '0';
6735 if (c >= 'a' && c <= 'f')
6736 return c - 'a' + 10;
6737 if (c >= 'A' && c <= 'F')
6738 return c - 'A' + 10;
6739 return -1;
6740}
6741
6742
6743int hwaddr_aton(const char *txt, unsigned char *addr)
6744{
6745 int i;
6746
6747 for (i = 0; i < 6; i++) {
6748 int a, b;
6749
6750 a = hex2num(*txt++);
6751 if (a < 0)
6752 return -1;
6753 b = hex2num(*txt++);
6754 if (b < 0)
6755 return -1;
6756 *addr++ = (a << 4) | b;
6757 if (i < 5 && *txt++ != ':')
6758 return -1;
6759 }
6760
6761 return 0;
6762}
6763
6764#endif /* defined(__linux__) || defined(__QNXNTO__) */
6765
6766enum send_frame_type {
6767 DISASSOC, DEAUTH, SAQUERY, AUTH, ASSOCREQ, REASSOCREQ, DLS_REQ
6768};
6769enum send_frame_protection {
6770 CORRECT_KEY, INCORRECT_KEY, UNPROTECTED
6771};
6772
6773
6774static int sta_inject_frame(struct sigma_dut *dut, struct sigma_conn *conn,
6775 enum send_frame_type frame,
6776 enum send_frame_protection protected,
6777 const char *dest)
6778{
6779#ifdef __linux__
6780 unsigned char buf[1000], *pos;
6781 int s, res;
6782 char bssid[20], addr[20];
6783 char result[32], ssid[100];
6784 size_t ssid_len;
6785
6786 if (get_wpa_status(get_station_ifname(), "wpa_state", result,
6787 sizeof(result)) < 0 ||
6788 strncmp(result, "COMPLETED", 9) != 0) {
6789 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Not connected");
6790 return 0;
6791 }
6792
6793 if (get_wpa_status(get_station_ifname(), "bssid", bssid, sizeof(bssid))
6794 < 0) {
6795 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not get "
6796 "current BSSID");
6797 return 0;
6798 }
6799
6800 if (get_wpa_status(get_station_ifname(), "address", addr, sizeof(addr))
6801 < 0) {
6802 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not get "
6803 "own MAC address");
6804 return 0;
6805 }
6806
6807 if (get_wpa_status(get_station_ifname(), "ssid", ssid, sizeof(ssid))
6808 < 0) {
6809 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not get "
6810 "current SSID");
6811 return 0;
6812 }
6813 ssid_len = strlen(ssid);
6814
6815 pos = buf;
6816
6817 /* Frame Control */
6818 switch (frame) {
6819 case DISASSOC:
6820 *pos++ = 0xa0;
6821 break;
6822 case DEAUTH:
6823 *pos++ = 0xc0;
6824 break;
6825 case SAQUERY:
6826 *pos++ = 0xd0;
6827 break;
6828 case AUTH:
6829 *pos++ = 0xb0;
6830 break;
6831 case ASSOCREQ:
6832 *pos++ = 0x00;
6833 break;
6834 case REASSOCREQ:
6835 *pos++ = 0x20;
6836 break;
6837 case DLS_REQ:
6838 *pos++ = 0xd0;
6839 break;
6840 }
6841
6842 if (protected == INCORRECT_KEY)
6843 *pos++ = 0x40; /* Set Protected field to 1 */
6844 else
6845 *pos++ = 0x00;
6846
6847 /* Duration */
6848 *pos++ = 0x00;
6849 *pos++ = 0x00;
6850
6851 /* addr1 = DA (current AP) */
6852 hwaddr_aton(bssid, pos);
6853 pos += 6;
6854 /* addr2 = SA (own address) */
6855 hwaddr_aton(addr, pos);
6856 pos += 6;
6857 /* addr3 = BSSID (current AP) */
6858 hwaddr_aton(bssid, pos);
6859 pos += 6;
6860
6861 /* Seq# (to be filled by driver/mac80211) */
6862 *pos++ = 0x00;
6863 *pos++ = 0x00;
6864
6865 if (protected == INCORRECT_KEY) {
6866 /* CCMP parameters */
6867 memcpy(pos, "\x61\x01\x00\x20\x00\x10\x00\x00", 8);
6868 pos += 8;
6869 }
6870
6871 if (protected == INCORRECT_KEY) {
6872 switch (frame) {
6873 case DEAUTH:
6874 /* Reason code (encrypted) */
6875 memcpy(pos, "\xa7\x39", 2);
6876 pos += 2;
6877 break;
6878 case DISASSOC:
6879 /* Reason code (encrypted) */
6880 memcpy(pos, "\xa7\x39", 2);
6881 pos += 2;
6882 break;
6883 case SAQUERY:
6884 /* Category|Action|TransID (encrypted) */
6885 memcpy(pos, "\x6f\xbd\xe9\x4d", 4);
6886 pos += 4;
6887 break;
6888 default:
6889 return -1;
6890 }
6891
6892 /* CCMP MIC */
6893 memcpy(pos, "\xc8\xd8\x3b\x06\x5d\xb7\x25\x68", 8);
6894 pos += 8;
6895 } else {
6896 switch (frame) {
6897 case DEAUTH:
6898 /* reason code = 8 */
6899 *pos++ = 0x08;
6900 *pos++ = 0x00;
6901 break;
6902 case DISASSOC:
6903 /* reason code = 8 */
6904 *pos++ = 0x08;
6905 *pos++ = 0x00;
6906 break;
6907 case SAQUERY:
6908 /* Category - SA Query */
6909 *pos++ = 0x08;
6910 /* SA query Action - Request */
6911 *pos++ = 0x00;
6912 /* Transaction ID */
6913 *pos++ = 0x12;
6914 *pos++ = 0x34;
6915 break;
6916 case AUTH:
6917 /* Auth Alg (Open) */
6918 *pos++ = 0x00;
6919 *pos++ = 0x00;
6920 /* Seq# */
6921 *pos++ = 0x01;
6922 *pos++ = 0x00;
6923 /* Status code */
6924 *pos++ = 0x00;
6925 *pos++ = 0x00;
6926 break;
6927 case ASSOCREQ:
6928 /* Capability Information */
6929 *pos++ = 0x31;
6930 *pos++ = 0x04;
6931 /* Listen Interval */
6932 *pos++ = 0x0a;
6933 *pos++ = 0x00;
6934 /* SSID */
6935 *pos++ = 0x00;
6936 *pos++ = ssid_len;
6937 memcpy(pos, ssid, ssid_len);
6938 pos += ssid_len;
6939 /* Supported Rates */
6940 memcpy(pos, "\x01\x08\x02\x04\x0b\x16\x0c\x12\x18\x24",
6941 10);
6942 pos += 10;
6943 /* Extended Supported Rates */
6944 memcpy(pos, "\x32\x04\x30\x48\x60\x6c", 6);
6945 pos += 6;
6946 /* RSN */
6947 memcpy(pos, "\x30\x1a\x01\x00\x00\x0f\xac\x04\x01\x00"
6948 "\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x02\xc0"
6949 "\x00\x00\x00\x00\x0f\xac\x06", 28);
6950 pos += 28;
6951 break;
6952 case REASSOCREQ:
6953 /* Capability Information */
6954 *pos++ = 0x31;
6955 *pos++ = 0x04;
6956 /* Listen Interval */
6957 *pos++ = 0x0a;
6958 *pos++ = 0x00;
6959 /* Current AP */
6960 hwaddr_aton(bssid, pos);
6961 pos += 6;
6962 /* SSID */
6963 *pos++ = 0x00;
6964 *pos++ = ssid_len;
6965 memcpy(pos, ssid, ssid_len);
6966 pos += ssid_len;
6967 /* Supported Rates */
6968 memcpy(pos, "\x01\x08\x02\x04\x0b\x16\x0c\x12\x18\x24",
6969 10);
6970 pos += 10;
6971 /* Extended Supported Rates */
6972 memcpy(pos, "\x32\x04\x30\x48\x60\x6c", 6);
6973 pos += 6;
6974 /* RSN */
6975 memcpy(pos, "\x30\x1a\x01\x00\x00\x0f\xac\x04\x01\x00"
6976 "\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x02\xc0"
6977 "\x00\x00\x00\x00\x0f\xac\x06", 28);
6978 pos += 28;
6979 break;
6980 case DLS_REQ:
6981 /* Category - DLS */
6982 *pos++ = 0x02;
6983 /* DLS Action - Request */
6984 *pos++ = 0x00;
6985 /* Destination MACAddress */
6986 if (dest)
6987 hwaddr_aton(dest, pos);
6988 else
6989 memset(pos, 0, 6);
6990 pos += 6;
6991 /* Source MACAddress */
6992 hwaddr_aton(addr, pos);
6993 pos += 6;
6994 /* Capability Information */
6995 *pos++ = 0x10; /* Privacy */
6996 *pos++ = 0x06; /* QoS */
6997 /* DLS Timeout Value */
6998 *pos++ = 0x00;
6999 *pos++ = 0x01;
7000 /* Supported rates */
7001 *pos++ = 0x01;
7002 *pos++ = 0x08;
7003 *pos++ = 0x0c; /* 6 Mbps */
7004 *pos++ = 0x12; /* 9 Mbps */
7005 *pos++ = 0x18; /* 12 Mbps */
7006 *pos++ = 0x24; /* 18 Mbps */
7007 *pos++ = 0x30; /* 24 Mbps */
7008 *pos++ = 0x48; /* 36 Mbps */
7009 *pos++ = 0x60; /* 48 Mbps */
7010 *pos++ = 0x6c; /* 54 Mbps */
7011 /* TODO: Extended Supported Rates */
7012 /* TODO: HT Capabilities */
7013 break;
7014 }
7015 }
7016
7017 s = open_monitor("sigmadut");
7018 if (s < 0) {
7019 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to open "
7020 "monitor socket");
7021 return 0;
7022 }
7023
7024 res = inject_frame(s, buf, pos - buf, protected == CORRECT_KEY);
7025 if (res < 0) {
7026 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to "
7027 "inject frame");
Pradeep Reddy POTTETI673d85c2016-07-26 19:08:07 +05307028 close(s);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007029 return 0;
7030 }
7031 if (res < pos - buf) {
7032 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Only partial "
7033 "frame sent");
Pradeep Reddy POTTETI673d85c2016-07-26 19:08:07 +05307034 close(s);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007035 return 0;
7036 }
7037
7038 close(s);
7039
7040 return 1;
7041#else /* __linux__ */
7042 send_resp(dut, conn, SIGMA_ERROR, "errorCode,sta_send_frame not "
7043 "yet supported");
7044 return 0;
7045#endif /* __linux__ */
7046}
7047
7048
7049static int cmd_sta_send_frame_tdls(struct sigma_dut *dut,
7050 struct sigma_conn *conn,
7051 struct sigma_cmd *cmd)
7052{
7053 const char *intf = get_param(cmd, "Interface");
7054 const char *sta, *val;
7055 unsigned char addr[ETH_ALEN];
7056 char buf[100];
7057
7058 sta = get_param(cmd, "peer");
7059 if (sta == NULL)
7060 sta = get_param(cmd, "station");
7061 if (sta == NULL) {
7062 send_resp(dut, conn, SIGMA_ERROR,
7063 "ErrorCode,Missing peer address");
7064 return 0;
7065 }
7066 if (hwaddr_aton(sta, addr) < 0) {
7067 send_resp(dut, conn, SIGMA_ERROR,
7068 "ErrorCode,Invalid peer address");
7069 return 0;
7070 }
7071
7072 val = get_param(cmd, "type");
7073 if (val == NULL)
7074 return -1;
7075
7076 if (strcasecmp(val, "DISCOVERY") == 0) {
7077 snprintf(buf, sizeof(buf), "TDLS_DISCOVER %s", sta);
7078 if (wpa_command(intf, buf) < 0) {
7079 send_resp(dut, conn, SIGMA_ERROR,
7080 "ErrorCode,Failed to send TDLS discovery");
7081 return 0;
7082 }
7083 return 1;
7084 }
7085
7086 if (strcasecmp(val, "SETUP") == 0) {
7087 int status = 0, timeout = 0;
7088
7089 val = get_param(cmd, "Status");
7090 if (val)
7091 status = atoi(val);
7092
7093 val = get_param(cmd, "Timeout");
7094 if (val)
7095 timeout = atoi(val);
7096
7097 if (status != 0 && status != 37) {
7098 send_resp(dut, conn, SIGMA_ERROR,
7099 "ErrorCode,Unsupported status value");
7100 return 0;
7101 }
7102
7103 if (timeout != 0 && timeout != 301) {
7104 send_resp(dut, conn, SIGMA_ERROR,
7105 "ErrorCode,Unsupported timeout value");
7106 return 0;
7107 }
7108
7109 if (status && timeout) {
7110 send_resp(dut, conn, SIGMA_ERROR,
7111 "ErrorCode,Unsupported timeout+status "
7112 "combination");
7113 return 0;
7114 }
7115
7116 if (status == 37 &&
7117 wpa_command(intf, "SET tdls_testing 0x200")) {
7118 send_resp(dut, conn, SIGMA_ERROR,
7119 "ErrorCode,Failed to enable "
7120 "decline setup response test mode");
7121 return 0;
7122 }
7123
7124 if (timeout == 301) {
7125 int res;
7126 if (dut->no_tpk_expiration)
7127 res = wpa_command(intf,
7128 "SET tdls_testing 0x108");
7129 else
7130 res = wpa_command(intf,
7131 "SET tdls_testing 0x8");
7132 if (res) {
7133 send_resp(dut, conn, SIGMA_ERROR,
7134 "ErrorCode,Failed to set short TPK "
7135 "lifetime");
7136 return 0;
7137 }
7138 }
7139
7140 snprintf(buf, sizeof(buf), "TDLS_SETUP %s", sta);
7141 if (wpa_command(intf, buf) < 0) {
7142 send_resp(dut, conn, SIGMA_ERROR,
7143 "ErrorCode,Failed to send TDLS setup");
7144 return 0;
7145 }
7146 return 1;
7147 }
7148
7149 if (strcasecmp(val, "TEARDOWN") == 0) {
7150 snprintf(buf, sizeof(buf), "TDLS_TEARDOWN %s", sta);
7151 if (wpa_command(intf, buf) < 0) {
7152 send_resp(dut, conn, SIGMA_ERROR,
7153 "ErrorCode,Failed to send TDLS teardown");
7154 return 0;
7155 }
7156 return 1;
7157 }
7158
7159 send_resp(dut, conn, SIGMA_ERROR,
7160 "ErrorCode,Unsupported TDLS frame");
7161 return 0;
7162}
7163
7164
7165static int sta_ap_known(const char *ifname, const char *bssid)
7166{
7167 char buf[4096];
7168
7169 snprintf(buf, sizeof(buf), "BSS %s", bssid);
7170 if (wpa_command_resp(ifname, buf, buf, sizeof(buf)) < 0)
7171 return 0;
7172 if (strncmp(buf, "id=", 3) != 0)
7173 return 0;
7174 return 1;
7175}
7176
7177
7178static int sta_scan_ap(struct sigma_dut *dut, const char *ifname,
7179 const char *bssid)
7180{
7181 int res;
7182 struct wpa_ctrl *ctrl;
7183 char buf[256];
7184
7185 if (sta_ap_known(ifname, bssid))
7186 return 0;
7187 sigma_dut_print(dut, DUT_MSG_DEBUG,
7188 "AP not in BSS table - start scan");
7189
7190 ctrl = open_wpa_mon(ifname);
7191 if (ctrl == NULL) {
7192 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to open "
7193 "wpa_supplicant monitor connection");
7194 return -1;
7195 }
7196
7197 if (wpa_command(ifname, "SCAN") < 0) {
7198 sigma_dut_print(dut, DUT_MSG_INFO, "Failed to start scan");
7199 wpa_ctrl_detach(ctrl);
7200 wpa_ctrl_close(ctrl);
7201 return -1;
7202 }
7203
7204 res = get_wpa_cli_event(dut, ctrl, "CTRL-EVENT-SCAN-RESULTS",
7205 buf, sizeof(buf));
7206
7207 wpa_ctrl_detach(ctrl);
7208 wpa_ctrl_close(ctrl);
7209
7210 if (res < 0) {
7211 sigma_dut_print(dut, DUT_MSG_INFO, "Scan did not complete");
7212 return -1;
7213 }
7214
7215 if (sta_ap_known(ifname, bssid))
7216 return 0;
7217 sigma_dut_print(dut, DUT_MSG_INFO, "AP not in BSS table");
7218 return -1;
7219}
7220
7221
7222static int cmd_sta_send_frame_hs2_neighadv(struct sigma_dut *dut,
7223 struct sigma_conn *conn,
7224 struct sigma_cmd *cmd,
7225 const char *intf)
7226{
7227 char buf[200];
7228
7229 snprintf(buf, sizeof(buf), "ndsend 2001:DB8::1 %s", intf);
7230 if (system(buf) != 0) {
7231 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Failed to run "
7232 "ndsend");
7233 return 0;
7234 }
7235
7236 return 1;
7237}
7238
7239
7240static int cmd_sta_send_frame_hs2_neighsolreq(struct sigma_dut *dut,
7241 struct sigma_conn *conn,
7242 struct sigma_cmd *cmd,
7243 const char *intf)
7244{
7245 char buf[200];
7246 const char *ip = get_param(cmd, "SenderIP");
7247
Peng Xu26b356d2017-10-04 17:58:16 -07007248 if (!ip)
7249 return 0;
7250
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007251 snprintf(buf, sizeof(buf), "ndisc6 -nm %s %s -r 4", ip, intf);
7252 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
7253 if (system(buf) == 0) {
7254 sigma_dut_print(dut, DUT_MSG_INFO,
7255 "Neighbor Solicitation got a response "
7256 "for %s@%s", ip, intf);
7257 }
7258
7259 return 1;
7260}
7261
7262
7263static int cmd_sta_send_frame_hs2_arpprobe(struct sigma_dut *dut,
7264 struct sigma_conn *conn,
7265 struct sigma_cmd *cmd,
7266 const char *ifname)
7267{
7268 char buf[200];
7269 const char *ip = get_param(cmd, "SenderIP");
7270
7271 if (ip == NULL) {
7272 send_resp(dut, conn, SIGMA_ERROR,
7273 "ErrorCode,Missing SenderIP parameter");
7274 return 0;
7275 }
7276 snprintf(buf, sizeof(buf), "arping -I %s -D %s -c 4", ifname, ip);
7277 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
7278 if (system(buf) != 0) {
7279 sigma_dut_print(dut, DUT_MSG_INFO, "arping DAD got a response "
7280 "for %s@%s", ip, ifname);
7281 }
7282
7283 return 1;
7284}
7285
7286
7287static int cmd_sta_send_frame_hs2_arpannounce(struct sigma_dut *dut,
7288 struct sigma_conn *conn,
7289 struct sigma_cmd *cmd,
7290 const char *ifname)
7291{
7292 char buf[200];
7293 char ip[16];
7294 int s;
Peng Xub3756882017-10-04 14:39:09 -07007295 struct ifreq ifr;
7296 struct sockaddr_in saddr;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007297
7298 s = socket(PF_INET, SOCK_DGRAM, 0);
Peng Xub3756882017-10-04 14:39:09 -07007299 if (s < 0) {
7300 perror("socket");
7301 return -1;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007302 }
7303
Peng Xub3756882017-10-04 14:39:09 -07007304 memset(&ifr, 0, sizeof(ifr));
7305 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
7306 if (ioctl(s, SIOCGIFADDR, &ifr) < 0) {
7307 sigma_dut_print(dut, DUT_MSG_INFO,
7308 "Failed to get %s IP address: %s",
7309 ifname, strerror(errno));
7310 close(s);
7311 return -1;
7312 }
7313 close(s);
7314
7315 memcpy(&saddr, &ifr.ifr_addr, sizeof(struct sockaddr_in));
7316 strlcpy(ip, inet_ntoa(saddr.sin_addr), sizeof(ip));
7317
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007318 snprintf(buf, sizeof(buf), "arping -I %s -s %s %s -c 4", ifname, ip,
7319 ip);
7320 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
7321 if (system(buf) != 0) {
7322 }
7323
7324 return 1;
7325}
7326
7327
7328static int cmd_sta_send_frame_hs2_arpreply(struct sigma_dut *dut,
7329 struct sigma_conn *conn,
7330 struct sigma_cmd *cmd,
7331 const char *ifname)
7332{
7333 char buf[200], addr[20];
7334 char dst[ETH_ALEN], src[ETH_ALEN];
7335 short ethtype = htons(ETH_P_ARP);
7336 char *pos;
7337 int s, res;
7338 const char *val;
7339 struct sockaddr_in taddr;
7340
7341 val = get_param(cmd, "dest");
7342 if (val)
7343 hwaddr_aton(val, (unsigned char *) dst);
7344
7345 val = get_param(cmd, "DestIP");
7346 if (val)
7347 inet_aton(val, &taddr.sin_addr);
Peng Xu151c9e12017-10-04 14:39:09 -07007348 else
7349 return -2;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007350
7351 if (get_wpa_status(get_station_ifname(), "address", addr,
7352 sizeof(addr)) < 0)
7353 return -2;
7354 hwaddr_aton(addr, (unsigned char *) src);
7355
7356 pos = buf;
7357 *pos++ = 0x00;
7358 *pos++ = 0x01;
7359 *pos++ = 0x08;
7360 *pos++ = 0x00;
7361 *pos++ = 0x06;
7362 *pos++ = 0x04;
7363 *pos++ = 0x00;
7364 *pos++ = 0x02;
7365 memcpy(pos, src, ETH_ALEN);
7366 pos += ETH_ALEN;
7367 memcpy(pos, &taddr.sin_addr, 4);
7368 pos += 4;
7369 memcpy(pos, dst, ETH_ALEN);
7370 pos += ETH_ALEN;
7371 memcpy(pos, &taddr.sin_addr, 4);
7372 pos += 4;
7373
7374 s = open_monitor(get_station_ifname());
7375 if (s < 0) {
7376 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to open "
7377 "monitor socket");
7378 return 0;
7379 }
7380
7381 res = inject_eth_frame(s, buf, pos - buf, ethtype, dst, src);
7382 if (res < 0) {
7383 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to "
7384 "inject frame");
Pradeep Reddy POTTETI673d85c2016-07-26 19:08:07 +05307385 close(s);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007386 return 0;
7387 }
7388
7389 close(s);
7390
7391 return 1;
7392}
7393
7394
7395static int cmd_sta_send_frame_hs2_dls_req(struct sigma_dut *dut,
7396 struct sigma_conn *conn,
7397 struct sigma_cmd *cmd,
7398 const char *intf, const char *dest)
7399{
7400 char buf[100];
7401
7402 if (if_nametoindex("sigmadut") == 0) {
7403 snprintf(buf, sizeof(buf),
7404 "iw dev %s interface add sigmadut type monitor",
7405 get_station_ifname());
7406 if (system(buf) != 0 ||
7407 if_nametoindex("sigmadut") == 0) {
7408 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to add "
7409 "monitor interface with '%s'", buf);
7410 return -2;
7411 }
7412 }
7413
7414 if (system("ifconfig sigmadut up") != 0) {
7415 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to set "
7416 "monitor interface up");
7417 return -2;
7418 }
7419
7420 return sta_inject_frame(dut, conn, DLS_REQ, UNPROTECTED, dest);
7421}
7422
7423
7424static int cmd_sta_send_frame_hs2(struct sigma_dut *dut,
7425 struct sigma_conn *conn,
7426 struct sigma_cmd *cmd)
7427{
7428 const char *intf = get_param(cmd, "Interface");
7429 const char *dest = get_param(cmd, "Dest");
7430 const char *type = get_param(cmd, "FrameName");
7431 const char *val;
7432 char buf[200], *pos, *end;
7433 int count, count2;
7434
7435 if (type == NULL)
7436 type = get_param(cmd, "Type");
7437
7438 if (intf == NULL || dest == NULL || type == NULL)
7439 return -1;
7440
7441 if (strcasecmp(type, "NeighAdv") == 0)
7442 return cmd_sta_send_frame_hs2_neighadv(dut, conn, cmd, intf);
7443
7444 if (strcasecmp(type, "NeighSolicitReq") == 0)
7445 return cmd_sta_send_frame_hs2_neighsolreq(dut, conn, cmd, intf);
7446
7447 if (strcasecmp(type, "ARPProbe") == 0)
7448 return cmd_sta_send_frame_hs2_arpprobe(dut, conn, cmd, intf);
7449
7450 if (strcasecmp(type, "ARPAnnounce") == 0)
7451 return cmd_sta_send_frame_hs2_arpannounce(dut, conn, cmd, intf);
7452
7453 if (strcasecmp(type, "ARPReply") == 0)
7454 return cmd_sta_send_frame_hs2_arpreply(dut, conn, cmd, intf);
7455
7456 if (strcasecmp(type, "DLS-request") == 0 ||
7457 strcasecmp(type, "DLSrequest") == 0)
7458 return cmd_sta_send_frame_hs2_dls_req(dut, conn, cmd, intf,
7459 dest);
7460
7461 if (strcasecmp(type, "ANQPQuery") != 0 &&
7462 strcasecmp(type, "Query") != 0) {
7463 send_resp(dut, conn, SIGMA_ERROR,
7464 "ErrorCode,Unsupported HS 2.0 send frame type");
7465 return 0;
7466 }
7467
7468 if (sta_scan_ap(dut, intf, dest) < 0) {
7469 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Could not find "
7470 "the requested AP");
7471 return 0;
7472 }
7473
7474 pos = buf;
7475 end = buf + sizeof(buf);
7476 count = 0;
7477 pos += snprintf(pos, end - pos, "ANQP_GET %s ", dest);
7478
7479 val = get_param(cmd, "ANQP_CAP_LIST");
7480 if (val && atoi(val)) {
7481 pos += snprintf(pos, end - pos, "%s257", count > 0 ? "," : "");
7482 count++;
7483 }
7484
7485 val = get_param(cmd, "VENUE_NAME");
7486 if (val && atoi(val)) {
7487 pos += snprintf(pos, end - pos, "%s258", count > 0 ? "," : "");
7488 count++;
7489 }
7490
7491 val = get_param(cmd, "NETWORK_AUTH_TYPE");
7492 if (val && atoi(val)) {
7493 pos += snprintf(pos, end - pos, "%s260", count > 0 ? "," : "");
7494 count++;
7495 }
7496
7497 val = get_param(cmd, "ROAMING_CONS");
7498 if (val && atoi(val)) {
7499 pos += snprintf(pos, end - pos, "%s261", count > 0 ? "," : "");
7500 count++;
7501 }
7502
7503 val = get_param(cmd, "IP_ADDR_TYPE_AVAILABILITY");
7504 if (val && atoi(val)) {
7505 pos += snprintf(pos, end - pos, "%s262", count > 0 ? "," : "");
7506 count++;
7507 }
7508
7509 val = get_param(cmd, "NAI_REALM_LIST");
7510 if (val && atoi(val)) {
7511 pos += snprintf(pos, end - pos, "%s263", count > 0 ? "," : "");
7512 count++;
7513 }
7514
7515 val = get_param(cmd, "3GPP_INFO");
7516 if (val && atoi(val)) {
7517 pos += snprintf(pos, end - pos, "%s264", count > 0 ? "," : "");
7518 count++;
7519 }
7520
7521 val = get_param(cmd, "DOMAIN_LIST");
7522 if (val && atoi(val)) {
7523 pos += snprintf(pos, end - pos, "%s268", count > 0 ? "," : "");
7524 count++;
7525 }
7526
7527 if (count && wpa_command(intf, buf)) {
7528 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,ANQP_GET failed");
7529 return 0;
7530 }
7531
7532 pos = buf;
7533 end = buf + sizeof(buf);
7534 count2 = 0;
7535 pos += snprintf(pos, end - pos, "HS20_ANQP_GET %s ", dest);
7536
7537 val = get_param(cmd, "HS_CAP_LIST");
7538 if (val && atoi(val)) {
7539 pos += snprintf(pos, end - pos, "%s2", count2 > 0 ? "," : "");
7540 count2++;
7541 }
7542
7543 val = get_param(cmd, "OPER_NAME");
7544 if (val && atoi(val)) {
7545 pos += snprintf(pos, end - pos, "%s3", count2 > 0 ? "," : "");
7546 count2++;
7547 }
7548
7549 val = get_param(cmd, "WAN_METRICS");
7550 if (!val)
7551 val = get_param(cmd, "WAN_MAT");
7552 if (!val)
7553 val = get_param(cmd, "WAN_MET");
7554 if (val && atoi(val)) {
7555 pos += snprintf(pos, end - pos, "%s4", count2 > 0 ? "," : "");
7556 count2++;
7557 }
7558
7559 val = get_param(cmd, "CONNECTION_CAPABILITY");
7560 if (val && atoi(val)) {
7561 pos += snprintf(pos, end - pos, "%s5", count2 > 0 ? "," : "");
7562 count2++;
7563 }
7564
7565 val = get_param(cmd, "OP_CLASS");
7566 if (val && atoi(val)) {
7567 pos += snprintf(pos, end - pos, "%s7", count2 > 0 ? "," : "");
7568 count2++;
7569 }
7570
7571 val = get_param(cmd, "OSU_PROVIDER_LIST");
7572 if (val && atoi(val)) {
7573 pos += snprintf(pos, end - pos, "%s8", count2 > 0 ? "," : "");
7574 count2++;
7575 }
7576
7577 if (count && count2) {
7578 sigma_dut_print(dut, DUT_MSG_DEBUG, "Wait before sending out "
7579 "second query");
7580 sleep(1);
7581 }
7582
7583 if (count2 && wpa_command(intf, buf)) {
7584 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,HS20_ANQP_GET "
7585 "failed");
7586 return 0;
7587 }
7588
7589 val = get_param(cmd, "NAI_HOME_REALM_LIST");
7590 if (val) {
7591 if (count || count2) {
7592 sigma_dut_print(dut, DUT_MSG_DEBUG, "Wait before "
7593 "sending out second query");
7594 sleep(1);
7595 }
7596
7597 if (strcmp(val, "1") == 0)
7598 val = "mail.example.com";
7599 snprintf(buf, end - pos,
7600 "HS20_GET_NAI_HOME_REALM_LIST %s realm=%s",
7601 dest, val);
7602 if (wpa_command(intf, buf)) {
7603 send_resp(dut, conn, SIGMA_ERROR,
7604 "ErrorCode,HS20_GET_NAI_HOME_REALM_LIST "
7605 "failed");
7606 return 0;
7607 }
7608 }
7609
7610 val = get_param(cmd, "ICON_REQUEST");
7611 if (val) {
7612 if (count || count2) {
7613 sigma_dut_print(dut, DUT_MSG_DEBUG, "Wait before "
7614 "sending out second query");
7615 sleep(1);
7616 }
7617
7618 snprintf(buf, end - pos,
7619 "HS20_ICON_REQUEST %s %s", dest, val);
7620 if (wpa_command(intf, buf)) {
7621 send_resp(dut, conn, SIGMA_ERROR,
7622 "ErrorCode,HS20_ICON_REQUEST failed");
7623 return 0;
7624 }
7625 }
7626
7627 return 1;
7628}
7629
7630
7631static int ath_sta_send_frame_vht(struct sigma_dut *dut,
7632 struct sigma_conn *conn,
7633 struct sigma_cmd *cmd)
7634{
7635 const char *val;
7636 char *ifname;
7637 char buf[100];
7638 int chwidth, nss;
7639
7640 val = get_param(cmd, "framename");
7641 if (!val)
7642 return -1;
7643 sigma_dut_print(dut, DUT_MSG_DEBUG, "framename is %s", val);
7644
7645 /* Command sequence to generate Op mode notification */
7646 if (val && strcasecmp(val, "Op_md_notif_frm") == 0) {
7647 ifname = get_station_ifname();
7648
7649 /* Disable STBC */
7650 snprintf(buf, sizeof(buf),
7651 "iwpriv %s tx_stbc 0", ifname);
7652 if (system(buf) != 0) {
7653 sigma_dut_print(dut, DUT_MSG_ERROR,
7654 "iwpriv tx_stbc 0 failed!");
7655 }
7656
7657 /* Extract Channel width */
7658 val = get_param(cmd, "Channel_width");
7659 if (val) {
7660 switch (atoi(val)) {
7661 case 20:
7662 chwidth = 0;
7663 break;
7664 case 40:
7665 chwidth = 1;
7666 break;
7667 case 80:
7668 chwidth = 2;
7669 break;
7670 case 160:
7671 chwidth = 3;
7672 break;
7673 default:
7674 chwidth = 2;
7675 break;
7676 }
7677
7678 snprintf(buf, sizeof(buf), "iwpriv %s chwidth %d",
7679 ifname, chwidth);
7680 if (system(buf) != 0) {
7681 sigma_dut_print(dut, DUT_MSG_ERROR,
7682 "iwpriv chwidth failed!");
7683 }
7684 }
7685
7686 /* Extract NSS */
7687 val = get_param(cmd, "NSS");
7688 if (val) {
7689 switch (atoi(val)) {
7690 case 1:
7691 nss = 1;
7692 break;
7693 case 2:
7694 nss = 3;
7695 break;
7696 case 3:
7697 nss = 7;
7698 break;
7699 default:
7700 /* We do not support NSS > 3 */
7701 nss = 3;
7702 break;
7703 }
7704 snprintf(buf, sizeof(buf),
7705 "iwpriv %s rxchainmask %d", ifname, nss);
7706 if (system(buf) != 0) {
7707 sigma_dut_print(dut, DUT_MSG_ERROR,
7708 "iwpriv rxchainmask failed!");
7709 }
7710 }
7711
7712 /* Opmode notify */
7713 snprintf(buf, sizeof(buf), "iwpriv %s opmode_notify 1", ifname);
7714 if (system(buf) != 0) {
7715 sigma_dut_print(dut, DUT_MSG_ERROR,
7716 "iwpriv opmode_notify failed!");
7717 } else {
7718 sigma_dut_print(dut, DUT_MSG_INFO,
7719 "Sent out the notify frame!");
7720 }
7721 }
7722
7723 return 1;
7724}
7725
7726
7727static int cmd_sta_send_frame_vht(struct sigma_dut *dut,
7728 struct sigma_conn *conn,
7729 struct sigma_cmd *cmd)
7730{
7731 switch (get_driver_type()) {
7732 case DRIVER_ATHEROS:
7733 return ath_sta_send_frame_vht(dut, conn, cmd);
7734 default:
7735 send_resp(dut, conn, SIGMA_ERROR,
7736 "errorCode,Unsupported sta_set_frame(VHT) with the current driver");
7737 return 0;
7738 }
7739}
7740
7741
Lior David0fe101e2017-03-09 16:09:50 +02007742#ifdef __linux__
7743int wil6210_send_frame_60g(struct sigma_dut *dut, struct sigma_conn *conn,
7744 struct sigma_cmd *cmd)
7745{
7746 const char *frame_name = get_param(cmd, "framename");
7747 const char *mac = get_param(cmd, "dest_mac");
7748
7749 if (!frame_name || !mac) {
7750 sigma_dut_print(dut, DUT_MSG_ERROR,
7751 "framename and dest_mac must be provided");
7752 return -1;
7753 }
7754
7755 if (strcasecmp(frame_name, "brp") == 0) {
7756 const char *l_rx = get_param(cmd, "L-RX");
7757 int l_rx_i;
7758
7759 if (!l_rx) {
7760 sigma_dut_print(dut, DUT_MSG_ERROR,
7761 "L-RX must be provided");
7762 return -1;
7763 }
7764 l_rx_i = atoi(l_rx);
7765
7766 sigma_dut_print(dut, DUT_MSG_INFO,
7767 "dev_send_frame: BRP-RX, dest_mac %s, L-RX %s",
7768 mac, l_rx);
7769 if (l_rx_i != 16) {
7770 sigma_dut_print(dut, DUT_MSG_ERROR,
7771 "unsupported L-RX: %s", l_rx);
7772 return -1;
7773 }
7774
7775 if (wil6210_send_brp_rx(dut, mac, l_rx_i))
7776 return -1;
7777 } else if (strcasecmp(frame_name, "ssw") == 0) {
7778 sigma_dut_print(dut, DUT_MSG_INFO,
7779 "dev_send_frame: SLS, dest_mac %s", mac);
7780 if (wil6210_send_sls(dut, mac))
7781 return -1;
7782 } else {
7783 sigma_dut_print(dut, DUT_MSG_ERROR,
7784 "unsupported frame type: %s", frame_name);
7785 return -1;
7786 }
7787
7788 return 1;
7789}
7790#endif /* __linux__ */
7791
7792
7793static int cmd_sta_send_frame_60g(struct sigma_dut *dut,
7794 struct sigma_conn *conn,
7795 struct sigma_cmd *cmd)
7796{
7797 switch (get_driver_type()) {
7798#ifdef __linux__
7799 case DRIVER_WIL6210:
7800 return wil6210_send_frame_60g(dut, conn, cmd);
7801#endif /* __linux__ */
7802 default:
7803 send_resp(dut, conn, SIGMA_ERROR,
7804 "errorCode,Unsupported sta_set_frame(60G) with the current driver");
7805 return 0;
7806 }
7807}
7808
7809
Ashwini Patildb59b3c2017-04-13 15:19:23 +05307810static int mbo_send_anqp_query(struct sigma_dut *dut, struct sigma_conn *conn,
7811 const char *intf, struct sigma_cmd *cmd)
7812{
7813 const char *val, *addr;
7814 char buf[100];
7815
7816 addr = get_param(cmd, "DestMac");
7817 if (!addr) {
7818 send_resp(dut, conn, SIGMA_INVALID,
7819 "ErrorCode,AP MAC address is missing");
7820 return 0;
7821 }
7822
7823 val = get_param(cmd, "ANQPQuery_ID");
7824 if (!val) {
7825 send_resp(dut, conn, SIGMA_INVALID,
7826 "ErrorCode,Missing ANQPQuery_ID");
7827 return 0;
7828 }
7829
7830 if (strcasecmp(val, "NeighborReportReq") == 0) {
7831 snprintf(buf, sizeof(buf), "ANQP_GET %s 272", addr);
7832 } else if (strcasecmp(val, "QueryListWithCellPref") == 0) {
7833 snprintf(buf, sizeof(buf), "ANQP_GET %s 272,mbo:2", addr);
7834 } else {
7835 sigma_dut_print(dut, DUT_MSG_ERROR, "Invalid ANQPQuery_ID: %s",
7836 val);
7837 send_resp(dut, conn, SIGMA_INVALID,
7838 "ErrorCode,Invalid ANQPQuery_ID");
7839 return 0;
7840 }
7841
Ashwini Patild174f2c2017-04-13 16:49:46 +05307842 /* Set gas_address3 field to IEEE 802.11-2012 standard compliant form
7843 * (Address3 = Wildcard BSSID when sent to not-associated AP;
7844 * if associated, AP BSSID).
7845 */
7846 if (wpa_command(intf, "SET gas_address3 1") < 0) {
7847 send_resp(dut, conn, SIGMA_ERROR,
7848 "ErrorCode,Failed to set gas_address3");
7849 return 0;
7850 }
7851
Ashwini Patildb59b3c2017-04-13 15:19:23 +05307852 if (wpa_command(intf, buf) < 0) {
7853 send_resp(dut, conn, SIGMA_ERROR,
7854 "ErrorCode,Failed to send ANQP query");
7855 return 0;
7856 }
7857
7858 return 1;
7859}
7860
7861
7862static int mbo_cmd_sta_send_frame(struct sigma_dut *dut,
7863 struct sigma_conn *conn,
7864 const char *intf,
7865 struct sigma_cmd *cmd)
7866{
7867 const char *val = get_param(cmd, "FrameName");
7868
7869 if (val && strcasecmp(val, "ANQPQuery") == 0)
7870 return mbo_send_anqp_query(dut, conn, intf, cmd);
7871
7872 return 2;
7873}
7874
7875
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007876int cmd_sta_send_frame(struct sigma_dut *dut, struct sigma_conn *conn,
7877 struct sigma_cmd *cmd)
7878{
7879 const char *intf = get_param(cmd, "Interface");
7880 const char *val;
7881 enum send_frame_type frame;
7882 enum send_frame_protection protected;
7883 char buf[100];
7884 unsigned char addr[ETH_ALEN];
7885 int res;
7886
7887 val = get_param(cmd, "program");
7888 if (val == NULL)
7889 val = get_param(cmd, "frame");
7890 if (val && strcasecmp(val, "TDLS") == 0)
7891 return cmd_sta_send_frame_tdls(dut, conn, cmd);
7892 if (val && (strcasecmp(val, "HS2") == 0 ||
7893 strcasecmp(val, "HS2-R2") == 0))
7894 return cmd_sta_send_frame_hs2(dut, conn, cmd);
7895 if (val && strcasecmp(val, "VHT") == 0)
7896 return cmd_sta_send_frame_vht(dut, conn, cmd);
priyadharshini gowthamand66913a2016-07-29 15:11:17 -07007897 if (val && strcasecmp(val, "LOC") == 0)
7898 return loc_cmd_sta_send_frame(dut, conn, cmd);
Lior David0fe101e2017-03-09 16:09:50 +02007899 if (val && strcasecmp(val, "60GHz") == 0)
7900 return cmd_sta_send_frame_60g(dut, conn, cmd);
Ashwini Patildb59b3c2017-04-13 15:19:23 +05307901 if (val && strcasecmp(val, "MBO") == 0) {
7902 res = mbo_cmd_sta_send_frame(dut, conn, intf, cmd);
7903 if (res != 2)
7904 return res;
7905 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007906
7907 val = get_param(cmd, "TD_DISC");
7908 if (val) {
7909 if (hwaddr_aton(val, addr) < 0)
7910 return -1;
7911 snprintf(buf, sizeof(buf), "TDLS_DISCOVER %s", val);
7912 if (wpa_command(intf, buf) < 0) {
7913 send_resp(dut, conn, SIGMA_ERROR,
7914 "ErrorCode,Failed to send TDLS discovery");
7915 return 0;
7916 }
7917 return 1;
7918 }
7919
7920 val = get_param(cmd, "TD_Setup");
7921 if (val) {
7922 if (hwaddr_aton(val, addr) < 0)
7923 return -1;
7924 snprintf(buf, sizeof(buf), "TDLS_SETUP %s", val);
7925 if (wpa_command(intf, buf) < 0) {
7926 send_resp(dut, conn, SIGMA_ERROR,
7927 "ErrorCode,Failed to start TDLS setup");
7928 return 0;
7929 }
7930 return 1;
7931 }
7932
7933 val = get_param(cmd, "TD_TearDown");
7934 if (val) {
7935 if (hwaddr_aton(val, addr) < 0)
7936 return -1;
7937 snprintf(buf, sizeof(buf), "TDLS_TEARDOWN %s", val);
7938 if (wpa_command(intf, buf) < 0) {
7939 send_resp(dut, conn, SIGMA_ERROR,
7940 "ErrorCode,Failed to tear down TDLS link");
7941 return 0;
7942 }
7943 return 1;
7944 }
7945
7946 val = get_param(cmd, "TD_ChannelSwitch");
7947 if (val) {
7948 /* TODO */
7949 send_resp(dut, conn, SIGMA_ERROR,
7950 "ErrorCode,TD_ChannelSwitch not yet supported");
7951 return 0;
7952 }
7953
7954 val = get_param(cmd, "TD_NF");
7955 if (val) {
7956 /* TODO */
7957 send_resp(dut, conn, SIGMA_ERROR,
7958 "ErrorCode,TD_NF not yet supported");
7959 return 0;
7960 }
7961
7962 val = get_param(cmd, "PMFFrameType");
7963 if (val == NULL)
7964 val = get_param(cmd, "FrameName");
7965 if (val == NULL)
7966 val = get_param(cmd, "Type");
7967 if (val == NULL)
7968 return -1;
7969 if (strcasecmp(val, "disassoc") == 0)
7970 frame = DISASSOC;
7971 else if (strcasecmp(val, "deauth") == 0)
7972 frame = DEAUTH;
7973 else if (strcasecmp(val, "saquery") == 0)
7974 frame = SAQUERY;
7975 else if (strcasecmp(val, "auth") == 0)
7976 frame = AUTH;
7977 else if (strcasecmp(val, "assocreq") == 0)
7978 frame = ASSOCREQ;
7979 else if (strcasecmp(val, "reassocreq") == 0)
7980 frame = REASSOCREQ;
7981 else if (strcasecmp(val, "neigreq") == 0) {
7982 sigma_dut_print(dut, DUT_MSG_INFO, "Got neighbor request");
7983
7984 val = get_param(cmd, "ssid");
7985 if (val == NULL)
7986 return -1;
7987
7988 res = send_neighbor_request(dut, intf, val);
7989 if (res) {
7990 send_resp(dut, conn, SIGMA_ERROR, "errorCode,"
7991 "Failed to send neighbor report request");
7992 return 0;
7993 }
7994
7995 return 1;
Ashwini Patil5acd7382017-04-13 15:55:04 +05307996 } else if (strcasecmp(val, "transmgmtquery") == 0 ||
7997 strcasecmp(val, "BTMQuery") == 0) {
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007998 sigma_dut_print(dut, DUT_MSG_DEBUG,
7999 "Got Transition Management Query");
8000
Ashwini Patil5acd7382017-04-13 15:55:04 +05308001 res = send_trans_mgmt_query(dut, intf, cmd);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02008002 if (res) {
8003 send_resp(dut, conn, SIGMA_ERROR, "errorCode,"
8004 "Failed to send Transition Management Query");
8005 return 0;
8006 }
8007
8008 return 1;
8009 } else {
8010 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Unsupported "
8011 "PMFFrameType");
8012 return 0;
8013 }
8014
8015 val = get_param(cmd, "PMFProtected");
8016 if (val == NULL)
8017 val = get_param(cmd, "Protected");
8018 if (val == NULL)
8019 return -1;
8020 if (strcasecmp(val, "Correct-key") == 0 ||
8021 strcasecmp(val, "CorrectKey") == 0)
8022 protected = CORRECT_KEY;
8023 else if (strcasecmp(val, "IncorrectKey") == 0)
8024 protected = INCORRECT_KEY;
8025 else if (strcasecmp(val, "Unprotected") == 0)
8026 protected = UNPROTECTED;
8027 else {
8028 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Unsupported "
8029 "PMFProtected");
8030 return 0;
8031 }
8032
8033 if (protected != UNPROTECTED &&
8034 (frame == AUTH || frame == ASSOCREQ || frame == REASSOCREQ)) {
8035 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Impossible "
8036 "PMFProtected for auth/assocreq/reassocreq");
8037 return 0;
8038 }
8039
8040 if (if_nametoindex("sigmadut") == 0) {
8041 snprintf(buf, sizeof(buf),
8042 "iw dev %s interface add sigmadut type monitor",
8043 get_station_ifname());
8044 if (system(buf) != 0 ||
8045 if_nametoindex("sigmadut") == 0) {
8046 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to add "
8047 "monitor interface with '%s'", buf);
8048 return -2;
8049 }
8050 }
8051
8052 if (system("ifconfig sigmadut up") != 0) {
8053 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to set "
8054 "monitor interface up");
8055 return -2;
8056 }
8057
8058 return sta_inject_frame(dut, conn, frame, protected, NULL);
8059}
8060
8061
8062static int cmd_sta_set_parameter_hs2(struct sigma_dut *dut,
8063 struct sigma_conn *conn,
8064 struct sigma_cmd *cmd,
8065 const char *ifname)
8066{
8067 char buf[200];
8068 const char *val;
8069
8070 val = get_param(cmd, "ClearARP");
8071 if (val && atoi(val) == 1) {
8072 snprintf(buf, sizeof(buf), "ip neigh flush dev %s", ifname);
8073 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
8074 if (system(buf) != 0) {
8075 send_resp(dut, conn, SIGMA_ERROR,
8076 "errorCode,Failed to clear ARP cache");
8077 return 0;
8078 }
8079 }
8080
8081 return 1;
8082}
8083
8084
8085int cmd_sta_set_parameter(struct sigma_dut *dut, struct sigma_conn *conn,
8086 struct sigma_cmd *cmd)
8087{
8088 const char *intf = get_param(cmd, "Interface");
8089 const char *val;
8090
8091 if (intf == NULL)
8092 return -1;
8093
8094 val = get_param(cmd, "program");
8095 if (val && (strcasecmp(val, "HS2") == 0 ||
8096 strcasecmp(val, "HS2-R2") == 0))
8097 return cmd_sta_set_parameter_hs2(dut, conn, cmd, intf);
8098
8099 return -1;
8100}
8101
8102
8103static int cmd_sta_set_macaddr(struct sigma_dut *dut, struct sigma_conn *conn,
8104 struct sigma_cmd *cmd)
8105{
8106 const char *intf = get_param(cmd, "Interface");
8107 const char *mac = get_param(cmd, "MAC");
8108
8109 if (intf == NULL || mac == NULL)
8110 return -1;
8111
8112 sigma_dut_print(dut, DUT_MSG_INFO, "Change local MAC address for "
8113 "interface %s to %s", intf, mac);
8114
8115 if (dut->set_macaddr) {
8116 char buf[128];
8117 int res;
8118 if (strcasecmp(mac, "default") == 0) {
8119 res = snprintf(buf, sizeof(buf), "%s",
8120 dut->set_macaddr);
8121 dut->tmp_mac_addr = 0;
8122 } else {
8123 res = snprintf(buf, sizeof(buf), "%s %s",
8124 dut->set_macaddr, mac);
8125 dut->tmp_mac_addr = 1;
8126 }
8127 if (res < 0 || res >= (int) sizeof(buf))
8128 return -1;
8129 if (system(buf) != 0) {
8130 send_resp(dut, conn, SIGMA_ERROR,
8131 "errorCode,Failed to set MAC "
8132 "address");
8133 return 0;
8134 }
8135 return 1;
8136 }
8137
8138 if (strcasecmp(mac, "default") == 0)
8139 return 1;
8140
8141 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Unsupported "
8142 "command");
8143 return 0;
8144}
8145
8146
8147static int iwpriv_tdlsoffchnmode(struct sigma_dut *dut,
8148 struct sigma_conn *conn, const char *intf,
8149 int val)
8150{
8151 char buf[200];
8152 int res;
8153
8154 res = snprintf(buf, sizeof(buf), "iwpriv %s tdlsoffchnmode %d",
8155 intf, val);
8156 if (res < 0 || res >= (int) sizeof(buf))
8157 return -1;
8158 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
8159 if (system(buf) != 0) {
8160 send_resp(dut, conn, SIGMA_ERROR,
8161 "errorCode,Failed to configure offchannel mode");
8162 return 0;
8163 }
8164
8165 return 1;
8166}
8167
8168
Jouni Malinencd4e3c32015-10-29 12:39:56 +02008169static int off_chan_val(enum sec_ch_offset off)
8170{
8171 switch (off) {
8172 case SEC_CH_NO:
8173 return 0;
8174 case SEC_CH_40ABOVE:
8175 return 40;
8176 case SEC_CH_40BELOW:
8177 return -40;
8178 }
8179
8180 return 0;
8181}
8182
8183
8184static int iwpriv_set_offchan(struct sigma_dut *dut, struct sigma_conn *conn,
8185 const char *intf, int off_ch_num,
8186 enum sec_ch_offset sec)
8187{
8188 char buf[200];
8189 int res;
8190
8191 res = snprintf(buf, sizeof(buf), "iwpriv %s tdlsoffchan %d",
8192 intf, off_ch_num);
8193 if (res < 0 || res >= (int) sizeof(buf))
8194 return -1;
8195 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
8196 if (system(buf) != 0) {
8197 send_resp(dut, conn, SIGMA_ERROR,
8198 "errorCode,Failed to set offchan");
8199 return 0;
8200 }
8201
8202 res = snprintf(buf, sizeof(buf), "iwpriv %s tdlsecchnoffst %d",
8203 intf, off_chan_val(sec));
8204 if (res < 0 || res >= (int) sizeof(buf))
8205 return -1;
8206 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
8207 if (system(buf) != 0) {
8208 send_resp(dut, conn, SIGMA_ERROR,
8209 "errorCode,Failed to set sec chan offset");
8210 return 0;
8211 }
8212
8213 return 1;
8214}
8215
8216
8217static int tdls_set_offchannel_offset(struct sigma_dut *dut,
8218 struct sigma_conn *conn,
8219 const char *intf, int off_ch_num,
8220 enum sec_ch_offset sec)
8221{
8222 char buf[200];
8223 int res;
8224
8225 res = snprintf(buf, sizeof(buf), "DRIVER TDLSOFFCHANNEL %d",
8226 off_ch_num);
8227 if (res < 0 || res >= (int) sizeof(buf))
8228 return -1;
8229 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
8230
8231 if (wpa_command(intf, buf) < 0) {
8232 send_resp(dut, conn, SIGMA_ERROR,
8233 "ErrorCode,Failed to set offchan");
8234 return 0;
8235 }
8236 res = snprintf(buf, sizeof(buf), "DRIVER TDLSSECONDARYCHANNELOFFSET %d",
8237 off_chan_val(sec));
8238 if (res < 0 || res >= (int) sizeof(buf))
8239 return -1;
8240
8241 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
8242
8243 if (wpa_command(intf, buf) < 0) {
8244 send_resp(dut, conn, SIGMA_ERROR,
8245 "ErrorCode,Failed to set sec chan offset");
8246 return 0;
8247 }
8248
8249 return 1;
8250}
8251
8252
8253static int tdls_set_offchannel_mode(struct sigma_dut *dut,
8254 struct sigma_conn *conn,
8255 const char *intf, int val)
8256{
8257 char buf[200];
8258 int res;
8259
8260 res = snprintf(buf, sizeof(buf), "DRIVER TDLSOFFCHANNELMODE %d",
8261 val);
8262 if (res < 0 || res >= (int) sizeof(buf))
8263 return -1;
8264 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
8265
8266 if (wpa_command(intf, buf) < 0) {
8267 send_resp(dut, conn, SIGMA_ERROR,
8268 "ErrorCode,Failed to configure offchannel mode");
8269 return 0;
8270 }
8271
8272 return 1;
8273}
8274
8275
8276static int cmd_sta_set_rfeature_tdls(const char *intf, struct sigma_dut *dut,
8277 struct sigma_conn *conn,
8278 struct sigma_cmd *cmd)
8279{
8280 const char *val;
8281 enum {
8282 CHSM_NOT_SET,
8283 CHSM_ENABLE,
8284 CHSM_DISABLE,
8285 CHSM_REJREQ,
8286 CHSM_UNSOLRESP
8287 } chsm = CHSM_NOT_SET;
8288 int off_ch_num = -1;
8289 enum sec_ch_offset sec_ch = SEC_CH_NO;
8290 int res;
8291
8292 val = get_param(cmd, "Uapsd");
8293 if (val) {
8294 char buf[100];
8295 if (strcasecmp(val, "Enable") == 0)
8296 snprintf(buf, sizeof(buf), "SET ps 99");
8297 else if (strcasecmp(val, "Disable") == 0)
8298 snprintf(buf, sizeof(buf), "SET ps 98");
8299 else {
8300 send_resp(dut, conn, SIGMA_ERROR, "errorCode,"
8301 "Unsupported uapsd parameter value");
8302 return 0;
8303 }
8304 if (wpa_command(intf, buf)) {
8305 send_resp(dut, conn, SIGMA_ERROR,
8306 "ErrorCode,Failed to change U-APSD "
8307 "powersave mode");
8308 return 0;
8309 }
8310 }
8311
8312 val = get_param(cmd, "TPKTIMER");
8313 if (val && strcasecmp(val, "DISABLE") == 0) {
8314 if (wpa_command(intf, "SET tdls_testing 0x100")) {
8315 send_resp(dut, conn, SIGMA_ERROR,
8316 "ErrorCode,Failed to enable no TPK "
8317 "expiration test mode");
8318 return 0;
8319 }
8320 dut->no_tpk_expiration = 1;
8321 }
8322
8323 val = get_param(cmd, "ChSwitchMode");
8324 if (val) {
8325 if (strcasecmp(val, "Enable") == 0 ||
8326 strcasecmp(val, "Initiate") == 0)
8327 chsm = CHSM_ENABLE;
8328 else if (strcasecmp(val, "Disable") == 0 ||
8329 strcasecmp(val, "passive") == 0)
8330 chsm = CHSM_DISABLE;
8331 else if (strcasecmp(val, "RejReq") == 0)
8332 chsm = CHSM_REJREQ;
8333 else if (strcasecmp(val, "UnSolResp") == 0)
8334 chsm = CHSM_UNSOLRESP;
8335 else {
8336 send_resp(dut, conn, SIGMA_ERROR,
8337 "ErrorCode,Unknown ChSwitchMode value");
8338 return 0;
8339 }
8340 }
8341
8342 val = get_param(cmd, "OffChNum");
8343 if (val) {
8344 off_ch_num = atoi(val);
8345 if (off_ch_num == 0) {
8346 send_resp(dut, conn, SIGMA_ERROR,
8347 "ErrorCode,Invalid OffChNum");
8348 return 0;
8349 }
8350 }
8351
8352 val = get_param(cmd, "SecChOffset");
8353 if (val) {
8354 if (strcmp(val, "20") == 0)
8355 sec_ch = SEC_CH_NO;
8356 else if (strcasecmp(val, "40above") == 0)
8357 sec_ch = SEC_CH_40ABOVE;
8358 else if (strcasecmp(val, "40below") == 0)
8359 sec_ch = SEC_CH_40BELOW;
8360 else {
8361 send_resp(dut, conn, SIGMA_ERROR,
8362 "ErrorCode,Unknown SecChOffset value");
8363 return 0;
8364 }
8365 }
8366
8367 if (chsm == CHSM_NOT_SET) {
8368 /* no offchannel changes requested */
8369 return 1;
8370 }
8371
8372 if (strcmp(intf, get_main_ifname()) != 0 &&
8373 strcmp(intf, get_station_ifname()) != 0) {
8374 send_resp(dut, conn, SIGMA_ERROR,
8375 "ErrorCode,Unknown interface");
8376 return 0;
8377 }
8378
8379 switch (chsm) {
8380 case CHSM_NOT_SET:
Jouni Malinen280f5ba2016-08-29 21:33:10 +03008381 res = 1;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02008382 break;
8383 case CHSM_ENABLE:
8384 if (off_ch_num < 0) {
8385 send_resp(dut, conn, SIGMA_ERROR,
8386 "ErrorCode,Missing OffChNum argument");
8387 return 0;
8388 }
8389 if (wifi_chip_type == DRIVER_WCN) {
8390 res = tdls_set_offchannel_offset(dut, conn, intf,
8391 off_ch_num, sec_ch);
8392 } else {
8393 res = iwpriv_set_offchan(dut, conn, intf, off_ch_num,
8394 sec_ch);
8395 }
8396 if (res != 1)
8397 return res;
8398 if (wifi_chip_type == DRIVER_WCN)
8399 res = tdls_set_offchannel_mode(dut, conn, intf, 1);
8400 else
8401 res = iwpriv_tdlsoffchnmode(dut, conn, intf, 1);
8402 break;
8403 case CHSM_DISABLE:
8404 if (wifi_chip_type == DRIVER_WCN)
8405 res = tdls_set_offchannel_mode(dut, conn, intf, 2);
8406 else
8407 res = iwpriv_tdlsoffchnmode(dut, conn, intf, 2);
8408 break;
8409 case CHSM_REJREQ:
8410 if (wifi_chip_type == DRIVER_WCN)
8411 res = tdls_set_offchannel_mode(dut, conn, intf, 3);
8412 else
8413 res = iwpriv_tdlsoffchnmode(dut, conn, intf, 3);
8414 break;
8415 case CHSM_UNSOLRESP:
8416 if (off_ch_num < 0) {
8417 send_resp(dut, conn, SIGMA_ERROR,
8418 "ErrorCode,Missing OffChNum argument");
8419 return 0;
8420 }
8421 if (wifi_chip_type == DRIVER_WCN) {
8422 res = tdls_set_offchannel_offset(dut, conn, intf,
8423 off_ch_num, sec_ch);
8424 } else {
8425 res = iwpriv_set_offchan(dut, conn, intf, off_ch_num,
8426 sec_ch);
8427 }
8428 if (res != 1)
8429 return res;
8430 if (wifi_chip_type == DRIVER_WCN)
8431 res = tdls_set_offchannel_mode(dut, conn, intf, 4);
8432 else
8433 res = iwpriv_tdlsoffchnmode(dut, conn, intf, 4);
8434 break;
8435 }
8436
8437 return res;
8438}
8439
8440
8441static int ath_sta_set_rfeature_vht(const char *intf, struct sigma_dut *dut,
8442 struct sigma_conn *conn,
8443 struct sigma_cmd *cmd)
8444{
8445 const char *val;
8446 char *token, *result;
8447
priyadharshini gowthamane5e25172015-12-08 14:53:48 -08008448 novap_reset(dut, intf);
8449
Jouni Malinencd4e3c32015-10-29 12:39:56 +02008450 val = get_param(cmd, "nss_mcs_opt");
8451 if (val) {
8452 /* String (nss_operating_mode; mcs_operating_mode) */
8453 int nss, mcs;
8454 char buf[50];
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05308455 char *saveptr;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02008456
8457 token = strdup(val);
8458 if (!token)
8459 return 0;
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05308460 result = strtok_r(token, ";", &saveptr);
Pradeep Reddy POTTETI41b8c542016-06-15 16:09:46 +05308461 if (!result) {
8462 sigma_dut_print(dut, DUT_MSG_ERROR,
8463 "VHT NSS not specified");
8464 goto failed;
8465 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02008466 if (strcasecmp(result, "def") != 0) {
8467 nss = atoi(result);
8468 if (nss == 4)
8469 ath_disable_txbf(dut, intf);
8470 snprintf(buf, sizeof(buf), "iwpriv %s nss %d",
8471 intf, nss);
8472 if (system(buf) != 0) {
8473 sigma_dut_print(dut, DUT_MSG_ERROR,
8474 "iwpriv nss failed");
8475 goto failed;
8476 }
8477 }
8478
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05308479 result = strtok_r(NULL, ";", &saveptr);
Pradeep Reddy POTTETI41b8c542016-06-15 16:09:46 +05308480 if (!result) {
8481 sigma_dut_print(dut, DUT_MSG_ERROR,
8482 "VHT MCS not specified");
8483 goto failed;
8484 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02008485 if (strcasecmp(result, "def") == 0) {
8486 snprintf(buf, sizeof(buf), "iwpriv %s set11NRates 0",
8487 intf);
8488 if (system(buf) != 0) {
8489 sigma_dut_print(dut, DUT_MSG_ERROR,
8490 "iwpriv set11NRates failed");
8491 goto failed;
8492 }
8493
8494 } else {
8495 mcs = atoi(result);
8496 snprintf(buf, sizeof(buf), "iwpriv %s vhtmcs %d",
8497 intf, mcs);
8498 if (system(buf) != 0) {
8499 sigma_dut_print(dut, DUT_MSG_ERROR,
8500 "iwpriv vhtmcs failed");
8501 goto failed;
8502 }
8503 }
8504 /* Channel width gets messed up, fix this */
8505 snprintf(buf, sizeof(buf), "iwpriv %s chwidth %d",
8506 intf, dut->chwidth);
8507 if (system(buf) != 0) {
8508 sigma_dut_print(dut, DUT_MSG_ERROR,
8509 "iwpriv chwidth failed");
8510 }
8511 }
8512
8513 return 1;
8514failed:
8515 free(token);
8516 return 0;
8517}
8518
8519
8520static int cmd_sta_set_rfeature_vht(const char *intf, struct sigma_dut *dut,
8521 struct sigma_conn *conn,
8522 struct sigma_cmd *cmd)
8523{
8524 switch (get_driver_type()) {
8525 case DRIVER_ATHEROS:
8526 return ath_sta_set_rfeature_vht(intf, dut, conn, cmd);
8527 default:
8528 send_resp(dut, conn, SIGMA_ERROR,
8529 "errorCode,Unsupported sta_set_rfeature(VHT) with the current driver");
8530 return 0;
8531 }
8532}
8533
8534
Amarnath Hullur Subramanyam42c25a02018-01-31 04:02:27 -08008535static int wcn_sta_set_rfeature_he(const char *intf, struct sigma_dut *dut,
8536 struct sigma_conn *conn,
8537 struct sigma_cmd *cmd)
8538{
8539 const char *val;
8540 char *token = NULL, *result;
8541 char buf[60];
8542
8543 val = get_param(cmd, "nss_mcs_opt");
8544 if (val) {
8545 /* String (nss_operating_mode; mcs_operating_mode) */
8546 int nss, mcs, ratecode;
8547 char *saveptr;
8548
8549 token = strdup(val);
8550 if (!token)
8551 return -2;
8552
8553 result = strtok_r(token, ";", &saveptr);
8554 if (!result) {
8555 sigma_dut_print(dut, DUT_MSG_ERROR,
8556 "HE NSS not specified");
8557 goto failed;
8558 }
8559 nss = 1;
8560 if (strcasecmp(result, "def") != 0)
8561 nss = atoi(result);
8562
8563 result = strtok_r(NULL, ";", &saveptr);
8564 if (!result) {
8565 sigma_dut_print(dut, DUT_MSG_ERROR,
8566 "HE MCS not specified");
8567 goto failed;
8568 }
8569 mcs = 7;
8570 if (strcasecmp(result, "def") != 0)
8571 mcs = atoi(result);
8572
8573 ratecode = 0x400; /* for nss:1 MCS 0 */
8574 if (nss == 2) {
8575 ratecode = 0x420; /* for nss:2 MCS 0 */
8576 } else if (nss > 2) {
8577 sigma_dut_print(dut, DUT_MSG_ERROR,
8578 "HE NSS %d not supported", nss);
8579 goto failed;
8580 }
8581
8582 /* Add the MCS to the ratecode */
8583 if (mcs >= 0 && mcs <= 11) {
8584 ratecode += mcs;
8585 } else {
8586 sigma_dut_print(dut, DUT_MSG_ERROR,
8587 "HE MCS %d not supported", mcs);
8588 goto failed;
8589 }
8590 snprintf(buf, sizeof(buf), "iwpriv %s set_11ax_rate 0x%03x",
8591 intf, ratecode);
8592 if (system(buf) != 0) {
8593 sigma_dut_print(dut, DUT_MSG_ERROR,
8594 "iwpriv setting of 11ax rates failed");
8595 goto failed;
8596 }
8597 free(token);
8598 }
8599
8600 val = get_param(cmd, "GI");
8601 if (val) {
8602 if (strcmp(val, "0.8") == 0) {
8603 snprintf(buf, sizeof(buf), "iwpriv %s shortgi 0", intf);
8604 } else if (strcmp(val, "1.6") == 0) {
8605 snprintf(buf, sizeof(buf), "iwpriv %s shortgi 2", intf);
8606 } else if (strcmp(val, "3.2") == 0) {
8607 snprintf(buf, sizeof(buf), "iwpriv %s shortgi 3", intf);
8608 } else {
8609 send_resp(dut, conn, SIGMA_ERROR,
8610 "errorCode,GI value not supported");
8611 return 0;
8612 }
8613 if (system(buf) != 0) {
8614 send_resp(dut, conn, SIGMA_ERROR,
8615 "errorCode,Failed to set shortgi");
8616 return 0;
8617 }
8618 }
8619
8620 return 1;
8621
8622failed:
8623 free(token);
8624 return -2;
8625}
8626
8627
8628static int cmd_sta_set_rfeature_he(const char *intf, struct sigma_dut *dut,
8629 struct sigma_conn *conn,
8630 struct sigma_cmd *cmd)
8631{
8632 switch (get_driver_type()) {
8633 case DRIVER_WCN:
8634 return wcn_sta_set_rfeature_he(intf, dut, conn, cmd);
8635 default:
8636 send_resp(dut, conn, SIGMA_ERROR,
8637 "errorCode,Unsupported sta_set_rfeature(HE) with the current driver");
8638 return 0;
8639 }
8640}
8641
8642
Ashwini Patil5acd7382017-04-13 15:55:04 +05308643static int btm_query_candidate_list(struct sigma_dut *dut,
8644 struct sigma_conn *conn,
8645 struct sigma_cmd *cmd)
8646{
8647 const char *bssid, *info, *op_class, *ch, *phy_type, *pref;
8648 int len, ret;
8649 char buf[10];
8650
8651 /*
8652 * Neighbor Report elements format:
8653 * neighbor=<BSSID>,<BSSID Information>,<Operating Class>,
8654 * <Channel Number>,<PHY Type>[,<hexdump of Optional Subelements>]
8655 * eg: neighbor=aa:bb:cc:dd:ee:ff,17,81,6,1,030101
8656 */
8657
8658 bssid = get_param(cmd, "Nebor_BSSID");
8659 if (!bssid) {
8660 send_resp(dut, conn, SIGMA_INVALID,
8661 "errorCode,Nebor_BSSID is missing");
8662 return 0;
8663 }
8664
8665 info = get_param(cmd, "Nebor_Bssid_Info");
8666 if (!info) {
8667 sigma_dut_print(dut, DUT_MSG_INFO,
8668 "Using default value for Nebor_Bssid_Info: %s",
8669 DEFAULT_NEIGHBOR_BSSID_INFO);
8670 info = DEFAULT_NEIGHBOR_BSSID_INFO;
8671 }
8672
8673 op_class = get_param(cmd, "Nebor_Op_Class");
8674 if (!op_class) {
8675 send_resp(dut, conn, SIGMA_INVALID,
8676 "errorCode,Nebor_Op_Class is missing");
8677 return 0;
8678 }
8679
8680 ch = get_param(cmd, "Nebor_Op_Ch");
8681 if (!ch) {
8682 send_resp(dut, conn, SIGMA_INVALID,
8683 "errorCode,Nebor_Op_Ch is missing");
8684 return 0;
8685 }
8686
8687 phy_type = get_param(cmd, "Nebor_Phy_Type");
8688 if (!phy_type) {
8689 sigma_dut_print(dut, DUT_MSG_INFO,
8690 "Using default value for Nebor_Phy_Type: %s",
8691 DEFAULT_NEIGHBOR_PHY_TYPE);
8692 phy_type = DEFAULT_NEIGHBOR_PHY_TYPE;
8693 }
8694
8695 /* Parse optional subelements */
8696 buf[0] = '\0';
8697 pref = get_param(cmd, "Nebor_Pref");
8698 if (pref) {
8699 /* hexdump for preferrence subelement */
8700 ret = snprintf(buf, sizeof(buf), ",0301%02x", atoi(pref));
8701 if (ret < 0 || ret >= (int) sizeof(buf)) {
8702 sigma_dut_print(dut, DUT_MSG_ERROR,
8703 "snprintf failed for optional subelement ret: %d",
8704 ret);
8705 send_resp(dut, conn, SIGMA_ERROR,
8706 "errorCode,snprintf failed for subelement");
8707 return 0;
8708 }
8709 }
8710
8711 if (!dut->btm_query_cand_list) {
8712 dut->btm_query_cand_list = calloc(1, NEIGHBOR_REPORT_SIZE);
8713 if (!dut->btm_query_cand_list) {
8714 send_resp(dut, conn, SIGMA_ERROR,
8715 "errorCode,Failed to allocate memory for btm_query_cand_list");
8716 return 0;
8717 }
8718 }
8719
8720 len = strlen(dut->btm_query_cand_list);
8721 ret = snprintf(dut->btm_query_cand_list + len,
8722 NEIGHBOR_REPORT_SIZE - len, " neighbor=%s,%s,%s,%s,%s%s",
8723 bssid, info, op_class, ch, phy_type, buf);
8724 if (ret < 0 || ret >= NEIGHBOR_REPORT_SIZE - len) {
8725 sigma_dut_print(dut, DUT_MSG_ERROR,
8726 "snprintf failed for neighbor report list ret: %d",
8727 ret);
8728 send_resp(dut, conn, SIGMA_ERROR,
8729 "errorCode,snprintf failed for neighbor report");
8730 free(dut->btm_query_cand_list);
8731 dut->btm_query_cand_list = NULL;
8732 return 0;
8733 }
8734
8735 return 1;
8736}
8737
8738
Jouni Malinencd4e3c32015-10-29 12:39:56 +02008739static int cmd_sta_set_rfeature(struct sigma_dut *dut, struct sigma_conn *conn,
8740 struct sigma_cmd *cmd)
8741{
8742 const char *intf = get_param(cmd, "Interface");
8743 const char *prog = get_param(cmd, "Prog");
Ashwini Patil68d02cd2017-01-10 15:39:16 +05308744 const char *val;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02008745
8746 if (intf == NULL || prog == NULL)
8747 return -1;
8748
Ashwini Patil5acd7382017-04-13 15:55:04 +05308749 /* BSS Transition candidate list for BTM query */
8750 val = get_param(cmd, "Nebor_BSSID");
8751 if (val && btm_query_candidate_list(dut, conn, cmd) == 0)
8752 return 0;
8753
Jouni Malinencd4e3c32015-10-29 12:39:56 +02008754 if (strcasecmp(prog, "TDLS") == 0)
8755 return cmd_sta_set_rfeature_tdls(intf, dut, conn, cmd);
8756
8757 if (strcasecmp(prog, "VHT") == 0)
8758 return cmd_sta_set_rfeature_vht(intf, dut, conn, cmd);
8759
Amarnath Hullur Subramanyam42c25a02018-01-31 04:02:27 -08008760 if (strcasecmp(prog, "HE") == 0)
8761 return cmd_sta_set_rfeature_he(intf, dut, conn, cmd);
8762
Ashwini Patil68d02cd2017-01-10 15:39:16 +05308763 if (strcasecmp(prog, "MBO") == 0) {
8764 val = get_param(cmd, "Cellular_Data_Cap");
8765 if (val &&
8766 mbo_set_cellular_data_capa(dut, conn, intf, atoi(val)) == 0)
8767 return 0;
Ashwini Patil00402582017-04-13 12:29:39 +05308768
8769 val = get_param(cmd, "Ch_Pref");
8770 if (val && mbo_set_non_pref_ch_list(dut, conn, intf, cmd) == 0)
8771 return 0;
8772
Ashwini Patil68d02cd2017-01-10 15:39:16 +05308773 return 1;
8774 }
8775
Jouni Malinencd4e3c32015-10-29 12:39:56 +02008776 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Unsupported Prog");
8777 return 0;
8778}
8779
8780
8781static int cmd_sta_set_radio(struct sigma_dut *dut, struct sigma_conn *conn,
8782 struct sigma_cmd *cmd)
8783{
8784 const char *intf = get_param(cmd, "Interface");
8785 const char *mode = get_param(cmd, "Mode");
8786 int res;
8787
8788 if (intf == NULL || mode == NULL)
8789 return -1;
8790
8791 if (strcasecmp(mode, "On") == 0)
8792 res = wpa_command(intf, "SET radio_disabled 0");
8793 else if (strcasecmp(mode, "Off") == 0)
8794 res = wpa_command(intf, "SET radio_disabled 1");
8795 else
8796 return -1;
8797
8798 if (res) {
8799 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to change "
8800 "radio mode");
8801 return 0;
8802 }
8803
8804 return 1;
8805}
8806
8807
8808static int cmd_sta_set_pwrsave(struct sigma_dut *dut, struct sigma_conn *conn,
8809 struct sigma_cmd *cmd)
8810{
8811 const char *intf = get_param(cmd, "Interface");
8812 const char *mode = get_param(cmd, "Mode");
8813 int res;
8814
8815 if (intf == NULL || mode == NULL)
8816 return -1;
8817
8818 if (strcasecmp(mode, "On") == 0)
8819 res = set_ps(intf, dut, 1);
8820 else if (strcasecmp(mode, "Off") == 0)
8821 res = set_ps(intf, dut, 0);
8822 else
8823 return -1;
8824
8825 if (res) {
8826 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to change "
8827 "power save mode");
8828 return 0;
8829 }
8830
8831 return 1;
8832}
8833
8834
8835static int cmd_sta_bssid_pool(struct sigma_dut *dut, struct sigma_conn *conn,
8836 struct sigma_cmd *cmd)
8837{
8838 const char *intf = get_param(cmd, "Interface");
8839 const char *val, *bssid;
8840 int res;
8841 char *buf;
8842 size_t buf_len;
8843
8844 val = get_param(cmd, "BSSID_FILTER");
8845 if (val == NULL)
8846 return -1;
8847
8848 bssid = get_param(cmd, "BSSID_List");
8849 if (atoi(val) == 0 || bssid == NULL) {
8850 /* Disable BSSID filter */
8851 if (wpa_command(intf, "SET bssid_filter ")) {
8852 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed "
8853 "to disable BSSID filter");
8854 return 0;
8855 }
8856
8857 return 1;
8858 }
8859
8860 buf_len = 100 + strlen(bssid);
8861 buf = malloc(buf_len);
8862 if (buf == NULL)
8863 return -1;
8864
8865 snprintf(buf, buf_len, "SET bssid_filter %s", bssid);
8866 res = wpa_command(intf, buf);
8867 free(buf);
8868 if (res) {
8869 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to enable "
8870 "BSSID filter");
8871 return 0;
8872 }
8873
8874 return 1;
8875}
8876
8877
8878static int cmd_sta_reset_parm(struct sigma_dut *dut, struct sigma_conn *conn,
8879 struct sigma_cmd *cmd)
8880{
8881 const char *intf = get_param(cmd, "Interface");
8882 const char *val;
8883
8884 /* TODO: ARP */
8885
8886 val = get_param(cmd, "HS2_CACHE_PROFILE");
8887 if (val && strcasecmp(val, "All") == 0)
8888 hs2_clear_credentials(intf);
8889
8890 return 1;
8891}
8892
8893
8894static int cmd_sta_get_key(struct sigma_dut *dut, struct sigma_conn *conn,
8895 struct sigma_cmd *cmd)
8896{
8897 const char *intf = get_param(cmd, "Interface");
8898 const char *key_type = get_param(cmd, "KeyType");
8899 char buf[100], resp[200];
8900
8901 if (key_type == NULL)
8902 return -1;
8903
8904 if (strcasecmp(key_type, "GTK") == 0) {
8905 if (wpa_command_resp(intf, "GET gtk", buf, sizeof(buf)) < 0 ||
8906 strncmp(buf, "FAIL", 4) == 0) {
8907 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could "
8908 "not fetch current GTK");
8909 return 0;
8910 }
8911 snprintf(resp, sizeof(resp), "KeyValue,%s", buf);
8912 send_resp(dut, conn, SIGMA_COMPLETE, resp);
8913 return 0;
8914 } else {
8915 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Unsupported "
8916 "KeyType");
8917 return 0;
8918 }
8919
8920 return 1;
8921}
8922
8923
8924static int hs2_set_policy(struct sigma_dut *dut)
8925{
8926#ifdef ANDROID
8927 system("ip rule del prio 23000");
8928 if (system("ip rule add from all lookup main prio 23000") != 0) {
8929 sigma_dut_print(dut, DUT_MSG_ERROR,
8930 "Failed to run:ip rule add from all lookup main prio");
8931 return -1;
8932 }
8933 if (system("ip route flush cache") != 0) {
8934 sigma_dut_print(dut, DUT_MSG_ERROR,
8935 "Failed to run ip route flush cache");
8936 return -1;
8937 }
8938 return 1;
8939#else /* ANDROID */
8940 return 0;
8941#endif /* ANDROID */
8942}
8943
8944
8945static int cmd_sta_hs2_associate(struct sigma_dut *dut,
8946 struct sigma_conn *conn,
8947 struct sigma_cmd *cmd)
8948{
8949 const char *intf = get_param(cmd, "Interface");
8950 const char *val = get_param(cmd, "Ignore_blacklist");
8951 struct wpa_ctrl *ctrl;
8952 int res;
8953 char bssid[20], ssid[40], resp[100], buf[100], blacklisted[100];
8954 int tries = 0;
8955 int ignore_blacklist = 0;
8956 const char *events[] = {
8957 "CTRL-EVENT-CONNECTED",
8958 "INTERWORKING-BLACKLISTED",
8959 "INTERWORKING-NO-MATCH",
8960 NULL
8961 };
8962
8963 start_sta_mode(dut);
8964
8965 blacklisted[0] = '\0';
8966 if (val && atoi(val))
8967 ignore_blacklist = 1;
8968
8969try_again:
8970 ctrl = open_wpa_mon(intf);
8971 if (ctrl == NULL) {
8972 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to open "
8973 "wpa_supplicant monitor connection");
8974 return -2;
8975 }
8976
8977 tries++;
8978 if (wpa_command(intf, "INTERWORKING_SELECT auto")) {
8979 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to start "
8980 "Interworking connection");
8981 wpa_ctrl_detach(ctrl);
8982 wpa_ctrl_close(ctrl);
8983 return 0;
8984 }
8985
8986 buf[0] = '\0';
8987 while (1) {
8988 char *pos;
8989 res = get_wpa_cli_events(dut, ctrl, events, buf, sizeof(buf));
8990 pos = strstr(buf, "INTERWORKING-BLACKLISTED");
8991 if (!pos)
8992 break;
8993 pos += 25;
8994 sigma_dut_print(dut, DUT_MSG_DEBUG, "Found blacklisted AP: %s",
8995 pos);
8996 if (!blacklisted[0])
8997 memcpy(blacklisted, pos, strlen(pos) + 1);
8998 }
8999
9000 if (ignore_blacklist && blacklisted[0]) {
9001 char *end;
9002 end = strchr(blacklisted, ' ');
9003 if (end)
9004 *end = '\0';
9005 sigma_dut_print(dut, DUT_MSG_DEBUG, "Try to connect to a blacklisted network: %s",
9006 blacklisted);
9007 snprintf(buf, sizeof(buf), "INTERWORKING_CONNECT %s",
9008 blacklisted);
9009 if (wpa_command(intf, buf)) {
9010 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to start Interworking connection to blacklisted network");
9011 wpa_ctrl_detach(ctrl);
9012 wpa_ctrl_close(ctrl);
9013 return 0;
9014 }
9015 res = get_wpa_cli_event(dut, ctrl, "CTRL-EVENT-CONNECTED",
9016 buf, sizeof(buf));
9017 }
9018
9019 wpa_ctrl_detach(ctrl);
9020 wpa_ctrl_close(ctrl);
9021
9022 if (res < 0) {
9023 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Could not "
9024 "connect");
9025 return 0;
9026 }
9027
9028 if (strstr(buf, "INTERWORKING-NO-MATCH") ||
9029 strstr(buf, "INTERWORKING-BLACKLISTED")) {
9030 if (tries < 2) {
9031 sigma_dut_print(dut, DUT_MSG_INFO, "No match found - try again to verify no APs were missed in the scan");
9032 goto try_again;
9033 }
9034 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,No network with "
9035 "matching credentials found");
9036 return 0;
9037 }
9038
9039 if (get_wpa_status(intf, "bssid", bssid, sizeof(bssid)) < 0 ||
9040 get_wpa_status(intf, "ssid", ssid, sizeof(ssid)) < 0) {
9041 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Could not "
9042 "get current BSSID/SSID");
9043 return 0;
9044 }
9045
9046 snprintf(resp, sizeof(resp), "SSID,%s,BSSID,%s", ssid, bssid);
9047 send_resp(dut, conn, SIGMA_COMPLETE, resp);
9048 hs2_set_policy(dut);
9049 return 0;
9050}
9051
9052
9053static int sta_add_credential_uname_pwd(struct sigma_dut *dut,
9054 struct sigma_conn *conn,
9055 const char *ifname,
9056 struct sigma_cmd *cmd)
9057{
9058 const char *val;
9059 int id;
9060
9061 id = add_cred(ifname);
9062 if (id < 0)
9063 return -2;
9064 sigma_dut_print(dut, DUT_MSG_DEBUG, "Adding credential %d", id);
9065
9066 val = get_param(cmd, "prefer");
9067 if (val && atoi(val) > 0)
9068 set_cred(ifname, id, "priority", "1");
9069
9070 val = get_param(cmd, "REALM");
9071 if (val && set_cred_quoted(ifname, id, "realm", val) < 0) {
9072 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not set "
9073 "realm");
9074 return 0;
9075 }
9076
9077 val = get_param(cmd, "HOME_FQDN");
9078 if (val && set_cred_quoted(ifname, id, "domain", val) < 0) {
9079 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not set "
9080 "home_fqdn");
9081 return 0;
9082 }
9083
9084 val = get_param(cmd, "Username");
9085 if (val && set_cred_quoted(ifname, id, "username", val) < 0) {
9086 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not set "
9087 "username");
9088 return 0;
9089 }
9090
9091 val = get_param(cmd, "Password");
9092 if (val && set_cred_quoted(ifname, id, "password", val) < 0) {
9093 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not set "
9094 "password");
9095 return 0;
9096 }
9097
9098 val = get_param(cmd, "ROOT_CA");
9099 if (val) {
9100 char fname[200];
9101 snprintf(fname, sizeof(fname), "%s/%s", sigma_cert_path, val);
9102#ifdef __linux__
9103 if (!file_exists(fname)) {
9104 char msg[300];
9105 snprintf(msg, sizeof(msg), "ErrorCode,ROOT_CA "
9106 "file (%s) not found", fname);
9107 send_resp(dut, conn, SIGMA_ERROR, msg);
9108 return 0;
9109 }
9110#endif /* __linux__ */
9111 if (set_cred_quoted(ifname, id, "ca_cert", fname) < 0) {
9112 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could "
9113 "not set root CA");
9114 return 0;
9115 }
9116 }
9117
9118 return 1;
9119}
9120
9121
9122static int update_devdetail_imsi(struct sigma_dut *dut, const char *imsi)
9123{
9124 FILE *in, *out;
9125 char buf[500];
9126 int found = 0;
9127
9128 in = fopen("devdetail.xml", "r");
9129 if (in == NULL)
9130 return -1;
9131 out = fopen("devdetail.xml.tmp", "w");
9132 if (out == NULL) {
9133 fclose(in);
9134 return -1;
9135 }
9136
9137 while (fgets(buf, sizeof(buf), in)) {
9138 char *pos = strstr(buf, "<IMSI>");
9139 if (pos) {
9140 sigma_dut_print(dut, DUT_MSG_INFO, "Updated DevDetail IMSI to %s",
9141 imsi);
9142 pos += 6;
9143 *pos = '\0';
9144 fprintf(out, "%s%s</IMSI>\n", buf, imsi);
9145 found++;
9146 } else {
9147 fprintf(out, "%s", buf);
9148 }
9149 }
9150
9151 fclose(out);
9152 fclose(in);
9153 if (found)
9154 rename("devdetail.xml.tmp", "devdetail.xml");
9155 else
9156 unlink("devdetail.xml.tmp");
9157
9158 return 0;
9159}
9160
9161
9162static int sta_add_credential_sim(struct sigma_dut *dut,
9163 struct sigma_conn *conn,
9164 const char *ifname, struct sigma_cmd *cmd)
9165{
9166 const char *val, *imsi = NULL;
9167 int id;
9168 char buf[200];
9169 int res;
9170 const char *pos;
9171 size_t mnc_len;
9172 char plmn_mcc[4];
9173 char plmn_mnc[4];
9174
9175 id = add_cred(ifname);
9176 if (id < 0)
9177 return -2;
9178 sigma_dut_print(dut, DUT_MSG_DEBUG, "Adding credential %d", id);
9179
9180 val = get_param(cmd, "prefer");
9181 if (val && atoi(val) > 0)
9182 set_cred(ifname, id, "priority", "1");
9183
9184 val = get_param(cmd, "PLMN_MCC");
9185 if (val == NULL) {
9186 send_resp(dut, conn, SIGMA_ERROR,
9187 "errorCode,Missing PLMN_MCC");
9188 return 0;
9189 }
9190 if (strlen(val) != 3) {
9191 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Invalid MCC");
9192 return 0;
9193 }
9194 snprintf(plmn_mcc, sizeof(plmn_mcc), "%s", val);
9195
9196 val = get_param(cmd, "PLMN_MNC");
9197 if (val == NULL) {
9198 send_resp(dut, conn, SIGMA_ERROR,
9199 "errorCode,Missing PLMN_MNC");
9200 return 0;
9201 }
9202 if (strlen(val) != 2 && strlen(val) != 3) {
9203 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Invalid MNC");
9204 return 0;
9205 }
9206 snprintf(plmn_mnc, sizeof(plmn_mnc), "%s", val);
9207
9208 val = get_param(cmd, "IMSI");
9209 if (val == NULL) {
9210 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Missing SIM "
9211 "IMSI");
9212 return 0;
9213 }
9214
9215 imsi = pos = val;
9216
9217 if (strncmp(plmn_mcc, pos, 3) != 0) {
9218 send_resp(dut, conn, SIGMA_ERROR, "errorCode,MCC mismatch");
9219 return 0;
9220 }
9221 pos += 3;
9222
9223 mnc_len = strlen(plmn_mnc);
9224 if (mnc_len < 2) {
9225 send_resp(dut, conn, SIGMA_ERROR, "errorCode,MNC not set");
9226 return 0;
9227 }
9228
9229 if (strncmp(plmn_mnc, pos, mnc_len) != 0) {
9230 send_resp(dut, conn, SIGMA_ERROR, "errorCode,MNC mismatch");
9231 return 0;
9232 }
9233 pos += mnc_len;
9234
9235 res = snprintf(buf, sizeof(buf), "%s%s-%s",plmn_mcc, plmn_mnc, pos);
9236 if (res < 0 || res >= (int) sizeof(buf))
9237 return -1;
9238 if (set_cred_quoted(ifname, id, "imsi", buf) < 0) {
9239 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could "
9240 "not set IMSI");
9241 return 0;
9242 }
9243
9244 val = get_param(cmd, "Password");
9245 if (val && set_cred_quoted(ifname, id, "milenage", val) < 0) {
9246 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could "
9247 "not set password");
9248 return 0;
9249 }
9250
9251 if (dut->program == PROGRAM_HS2_R2) {
9252 /*
9253 * Set provisioning_sp for the test cases where SIM/USIM
9254 * provisioning is used.
9255 */
9256 if (val && set_cred_quoted(ifname, id, "provisioning_sp",
9257 "wi-fi.org") < 0) {
9258 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could "
9259 "not set provisioning_sp");
9260 return 0;
9261 }
9262
9263 update_devdetail_imsi(dut, imsi);
9264 }
9265
9266 return 1;
9267}
9268
9269
9270static int sta_add_credential_cert(struct sigma_dut *dut,
9271 struct sigma_conn *conn,
9272 const char *ifname,
9273 struct sigma_cmd *cmd)
9274{
9275 const char *val;
9276 int id;
9277
9278 id = add_cred(ifname);
9279 if (id < 0)
9280 return -2;
9281 sigma_dut_print(dut, DUT_MSG_DEBUG, "Adding credential %d", id);
9282
9283 val = get_param(cmd, "prefer");
9284 if (val && atoi(val) > 0)
9285 set_cred(ifname, id, "priority", "1");
9286
9287 val = get_param(cmd, "REALM");
9288 if (val && set_cred_quoted(ifname, id, "realm", val) < 0) {
9289 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not set "
9290 "realm");
9291 return 0;
9292 }
9293
9294 val = get_param(cmd, "HOME_FQDN");
9295 if (val && set_cred_quoted(ifname, id, "domain", val) < 0) {
9296 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not set "
9297 "home_fqdn");
9298 return 0;
9299 }
9300
9301 val = get_param(cmd, "Username");
9302 if (val && set_cred_quoted(ifname, id, "username", val) < 0) {
9303 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not set "
9304 "username");
9305 return 0;
9306 }
9307
9308 val = get_param(cmd, "clientCertificate");
9309 if (val) {
9310 char fname[200];
9311 snprintf(fname, sizeof(fname), "%s/%s", sigma_cert_path, val);
9312#ifdef __linux__
9313 if (!file_exists(fname)) {
9314 char msg[300];
9315 snprintf(msg, sizeof(msg),
9316 "ErrorCode,clientCertificate "
9317 "file (%s) not found", fname);
9318 send_resp(dut, conn, SIGMA_ERROR, msg);
9319 return 0;
9320 }
9321#endif /* __linux__ */
9322 if (set_cred_quoted(ifname, id, "client_cert", fname) < 0) {
9323 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could "
9324 "not set client_cert");
9325 return 0;
9326 }
9327 if (set_cred_quoted(ifname, id, "private_key", fname) < 0) {
9328 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could "
9329 "not set private_key");
9330 return 0;
9331 }
9332 }
9333
9334 val = get_param(cmd, "ROOT_CA");
9335 if (val) {
9336 char fname[200];
9337 snprintf(fname, sizeof(fname), "%s/%s", sigma_cert_path, val);
9338#ifdef __linux__
9339 if (!file_exists(fname)) {
9340 char msg[300];
9341 snprintf(msg, sizeof(msg), "ErrorCode,ROOT_CA "
9342 "file (%s) not found", fname);
9343 send_resp(dut, conn, SIGMA_ERROR, msg);
9344 return 0;
9345 }
9346#endif /* __linux__ */
9347 if (set_cred_quoted(ifname, id, "ca_cert", fname) < 0) {
9348 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could "
9349 "not set root CA");
9350 return 0;
9351 }
9352 }
9353
9354 return 1;
9355}
9356
9357
9358static int cmd_sta_add_credential(struct sigma_dut *dut,
9359 struct sigma_conn *conn,
9360 struct sigma_cmd *cmd)
9361{
9362 const char *intf = get_param(cmd, "Interface");
9363 const char *type;
9364
9365 start_sta_mode(dut);
9366
9367 type = get_param(cmd, "Type");
9368 if (!type)
9369 return -1;
9370
9371 if (strcasecmp(type, "uname_pwd") == 0)
9372 return sta_add_credential_uname_pwd(dut, conn, intf, cmd);
9373
9374 if (strcasecmp(type, "sim") == 0)
9375 return sta_add_credential_sim(dut, conn, intf, cmd);
9376
9377 if (strcasecmp(type, "cert") == 0)
9378 return sta_add_credential_cert(dut, conn, intf, cmd);
9379
9380 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Unsupported credential "
9381 "type");
9382 return 0;
9383}
9384
9385
9386static int cmd_sta_scan(struct sigma_dut *dut, struct sigma_conn *conn,
9387 struct sigma_cmd *cmd)
9388{
9389 const char *intf = get_param(cmd, "Interface");
vamsi krishna89ad8c62017-09-19 12:51:18 +05309390 const char *val, *bssid, *ssid;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02009391 char buf[100];
vamsi krishna89ad8c62017-09-19 12:51:18 +05309392 char ssid_hex[65];
Jouni Malinencd4e3c32015-10-29 12:39:56 +02009393 int res;
9394
9395 val = get_param(cmd, "HESSID");
9396 if (val) {
9397 res = snprintf(buf, sizeof(buf), "SET hessid %s", val);
9398 if (res < 0 || res >= (int) sizeof(buf))
9399 return -1;
9400 wpa_command(intf, buf);
9401 }
9402
9403 val = get_param(cmd, "ACCS_NET_TYPE");
9404 if (val) {
9405 res = snprintf(buf, sizeof(buf), "SET access_network_type %s",
9406 val);
9407 if (res < 0 || res >= (int) sizeof(buf))
9408 return -1;
9409 wpa_command(intf, buf);
9410 }
9411
vamsi krishna89ad8c62017-09-19 12:51:18 +05309412 bssid = get_param(cmd, "Bssid");
9413 ssid = get_param(cmd, "Ssid");
9414
9415 if (ssid) {
9416 if (2 * strlen(ssid) >= sizeof(ssid_hex)) {
9417 send_resp(dut, conn, SIGMA_ERROR,
9418 "ErrorCode,Too long SSID");
9419 return 0;
9420 }
9421 ascii2hexstr(ssid, ssid_hex);
9422 }
9423
9424 res = snprintf(buf, sizeof(buf), "SCAN%s%s%s%s",
9425 bssid ? " bssid=": "",
9426 bssid ? bssid : "",
9427 ssid ? " ssid " : "",
9428 ssid ? ssid_hex : "");
9429 if (res < 0 || res >= (int) sizeof(buf))
9430 return -1;
9431
9432 if (wpa_command(intf, buf)) {
Jouni Malinencd4e3c32015-10-29 12:39:56 +02009433 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not start "
9434 "scan");
9435 return 0;
9436 }
9437
9438 return 1;
9439}
9440
9441
Jouni Malinen5e5d43d2018-01-10 17:29:33 +02009442static int cmd_sta_scan_bss(struct sigma_dut *dut, struct sigma_conn *conn,
9443 struct sigma_cmd *cmd)
9444{
9445 const char *intf = get_param(cmd, "Interface");
9446 const char *bssid;
9447 char buf[4096], *pos;
9448 int freq, chan;
9449 char *ssid;
9450 char resp[100];
9451 int res;
9452 struct wpa_ctrl *ctrl;
9453
9454 bssid = get_param(cmd, "BSSID");
9455 if (!bssid) {
9456 send_resp(dut, conn, SIGMA_INVALID,
9457 "errorCode,BSSID argument is missing");
9458 return 0;
9459 }
9460
9461 ctrl = open_wpa_mon(intf);
9462 if (!ctrl) {
9463 sigma_dut_print(dut, DUT_MSG_ERROR,
9464 "Failed to open wpa_supplicant monitor connection");
9465 return -1;
9466 }
9467
9468 if (wpa_command(intf, "SCAN TYPE=ONLY")) {
9469 send_resp(dut, conn, SIGMA_ERROR,
9470 "errorCode,Could not start scan");
9471 wpa_ctrl_detach(ctrl);
9472 wpa_ctrl_close(ctrl);
9473 return 0;
9474 }
9475
9476 res = get_wpa_cli_event(dut, ctrl, "CTRL-EVENT-SCAN-RESULTS",
9477 buf, sizeof(buf));
9478
9479 wpa_ctrl_detach(ctrl);
9480 wpa_ctrl_close(ctrl);
9481
9482 if (res < 0) {
9483 send_resp(dut, conn, SIGMA_ERROR,
9484 "errorCode,Scan did not complete");
9485 return 0;
9486 }
9487
9488 snprintf(buf, sizeof(buf), "BSS %s", bssid);
9489 if (wpa_command_resp(intf, buf, buf, sizeof(buf)) < 0 ||
9490 strncmp(buf, "id=", 3) != 0) {
9491 send_resp(dut, conn, SIGMA_ERROR,
9492 "errorCode,Specified BSSID not found");
9493 return 0;
9494 }
9495
9496 pos = strstr(buf, "\nfreq=");
9497 if (!pos) {
9498 send_resp(dut, conn, SIGMA_ERROR,
9499 "errorCode,Channel not found");
9500 return 0;
9501 }
9502 freq = atoi(pos + 6);
9503 chan = freq_to_channel(freq);
9504
9505 pos = strstr(buf, "\nssid=");
9506 if (!pos) {
9507 send_resp(dut, conn, SIGMA_ERROR,
9508 "errorCode,SSID not found");
9509 return 0;
9510 }
9511 ssid = pos + 6;
9512 pos = strchr(ssid, '\n');
9513 if (pos)
9514 *pos = '\0';
9515 snprintf(resp, sizeof(resp), "ssid,%s,bsschannel,%d", ssid, chan);
9516 send_resp(dut, conn, SIGMA_COMPLETE, resp);
9517 return 0;
9518}
9519
9520
Jouni Malinencd4e3c32015-10-29 12:39:56 +02009521static int cmd_sta_set_systime(struct sigma_dut *dut, struct sigma_conn *conn,
9522 struct sigma_cmd *cmd)
9523{
9524#ifdef __linux__
9525 struct timeval tv;
9526 struct tm tm;
9527 time_t t;
9528 const char *val;
Pradeep Reddy POTTETI429c69e2016-10-13 17:22:03 +05309529 int v;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02009530
9531 wpa_command(get_station_ifname(), "PMKSA_FLUSH");
9532
9533 memset(&tm, 0, sizeof(tm));
9534 val = get_param(cmd, "seconds");
9535 if (val)
9536 tm.tm_sec = atoi(val);
9537 val = get_param(cmd, "minutes");
9538 if (val)
9539 tm.tm_min = atoi(val);
9540 val = get_param(cmd, "hours");
9541 if (val)
9542 tm.tm_hour = atoi(val);
9543 val = get_param(cmd, "date");
9544 if (val)
9545 tm.tm_mday = atoi(val);
9546 val = get_param(cmd, "month");
Pradeep Reddy POTTETI429c69e2016-10-13 17:22:03 +05309547 if (val) {
9548 v = atoi(val);
9549 if (v < 1 || v > 12) {
9550 send_resp(dut, conn, SIGMA_INVALID,
9551 "errorCode,Invalid month");
9552 return 0;
9553 }
9554 tm.tm_mon = v - 1;
9555 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02009556 val = get_param(cmd, "year");
9557 if (val) {
9558 int year = atoi(val);
9559#ifdef ANDROID
9560 if (year > 2035)
9561 year = 2035; /* years beyond 2035 not supported */
9562#endif /* ANDROID */
9563 tm.tm_year = year - 1900;
9564 }
9565 t = mktime(&tm);
9566 if (t == (time_t) -1) {
9567 send_resp(dut, conn, SIGMA_ERROR,
9568 "errorCode,Invalid date or time");
9569 return 0;
9570 }
9571
9572 memset(&tv, 0, sizeof(tv));
9573 tv.tv_sec = t;
9574
9575 if (settimeofday(&tv, NULL) < 0) {
9576 sigma_dut_print(dut, DUT_MSG_INFO, "settimeofday failed: %s",
9577 strerror(errno));
9578 send_resp(dut, conn, SIGMA_ERROR,
9579 "errorCode,Failed to set time");
9580 return 0;
9581 }
9582
9583 return 1;
9584#endif /* __linux__ */
9585
9586 return -1;
9587}
9588
9589
9590static int cmd_sta_osu(struct sigma_dut *dut, struct sigma_conn *conn,
9591 struct sigma_cmd *cmd)
9592{
9593 const char *intf = get_param(cmd, "Interface");
9594 const char *name, *val;
9595 int prod_ess_assoc = 1;
9596 char buf[200], bssid[100], ssid[100];
9597 int res;
9598 struct wpa_ctrl *ctrl;
9599
9600 name = get_param(cmd, "osuFriendlyName");
9601
9602 val = get_param(cmd, "ProdESSAssoc");
9603 if (val)
9604 prod_ess_assoc = atoi(val);
9605
9606 kill_dhcp_client(dut, intf);
9607 if (start_dhcp_client(dut, intf) < 0)
9608 return -2;
9609
9610 sigma_dut_print(dut, DUT_MSG_DEBUG, "Trigger OSU");
9611 mkdir("Logs", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
9612 res = snprintf(buf, sizeof(buf),
9613 "%s %s%s%s signup osu-ca.pem",
9614 prod_ess_assoc ? "" : "-N",
9615 name ? "-O'" : "", name ? name : "",
9616 name ? "'" : "");
9617
Kanchanapally, Vidyullatha12b66762015-12-31 16:46:42 +05309618 hs2_set_policy(dut);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02009619 if (run_hs20_osu(dut, buf) < 0) {
9620 FILE *f;
9621
9622 sigma_dut_print(dut, DUT_MSG_INFO, "Failed to complete OSU");
9623
9624 f = fopen("hs20-osu-client.res", "r");
9625 if (f) {
9626 char resp[400], res[300], *pos;
9627 if (!fgets(res, sizeof(res), f))
9628 res[0] = '\0';
9629 pos = strchr(res, '\n');
9630 if (pos)
9631 *pos = '\0';
9632 fclose(f);
9633 sigma_dut_summary(dut, "hs20-osu-client provisioning failed: %s",
9634 res);
9635 snprintf(resp, sizeof(resp), "notify-send '%s'", res);
9636 if (system(resp) != 0) {
9637 }
9638 snprintf(resp, sizeof(resp),
9639 "SSID,,BSSID,,failureReason,%s", res);
9640 send_resp(dut, conn, SIGMA_COMPLETE, resp);
9641 return 0;
9642 }
9643
9644 send_resp(dut, conn, SIGMA_COMPLETE, "SSID,,BSSID,");
9645 return 0;
9646 }
9647
9648 if (!prod_ess_assoc)
9649 goto report;
9650
9651 ctrl = open_wpa_mon(intf);
9652 if (ctrl == NULL) {
9653 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to open "
9654 "wpa_supplicant monitor connection");
9655 return -1;
9656 }
9657
9658 res = get_wpa_cli_event(dut, ctrl, "CTRL-EVENT-CONNECTED",
9659 buf, sizeof(buf));
9660
9661 wpa_ctrl_detach(ctrl);
9662 wpa_ctrl_close(ctrl);
9663
9664 if (res < 0) {
9665 sigma_dut_print(dut, DUT_MSG_INFO, "Failed to connect to "
9666 "network after OSU");
9667 send_resp(dut, conn, SIGMA_COMPLETE, "SSID,,BSSID,");
9668 return 0;
9669 }
9670
9671report:
9672 if (get_wpa_status(intf, "bssid", bssid, sizeof(bssid)) < 0 ||
9673 get_wpa_status(intf, "ssid", ssid, sizeof(ssid)) < 0) {
9674 sigma_dut_print(dut, DUT_MSG_INFO, "Failed to get BSSID/SSID");
9675 send_resp(dut, conn, SIGMA_COMPLETE, "SSID,,BSSID,");
9676 return 0;
9677 }
9678
9679 snprintf(buf, sizeof(buf), "SSID,%s,BSSID,%s", ssid, bssid);
9680 send_resp(dut, conn, SIGMA_COMPLETE, buf);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02009681 return 0;
9682}
9683
9684
9685static int cmd_sta_policy_update(struct sigma_dut *dut, struct sigma_conn *conn,
9686 struct sigma_cmd *cmd)
9687{
9688 const char *val;
9689 int timeout = 120;
9690
9691 val = get_param(cmd, "PolicyUpdate");
9692 if (val == NULL || atoi(val) == 0)
9693 return 1; /* No operation requested */
9694
9695 val = get_param(cmd, "Timeout");
9696 if (val)
9697 timeout = atoi(val);
9698
9699 if (timeout) {
9700 /* TODO: time out the command and return
9701 * PolicyUpdateStatus,TIMEOUT if needed. */
9702 }
9703
9704 sigma_dut_print(dut, DUT_MSG_DEBUG, "Trigger policy update");
9705 mkdir("Logs", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
9706 if (run_hs20_osu(dut, "pol_upd fqdn=wi-fi.org") < 0) {
9707 send_resp(dut, conn, SIGMA_COMPLETE, "PolicyUpdateStatus,FAIL");
9708 return 0;
9709 }
9710
9711 send_resp(dut, conn, SIGMA_COMPLETE, "PolicyUpdateStatus,SUCCESS");
9712 return 0;
9713}
9714
9715
9716static int cmd_sta_er_config(struct sigma_dut *dut, struct sigma_conn *conn,
9717 struct sigma_cmd *cmd)
9718{
9719 struct wpa_ctrl *ctrl;
9720 const char *intf = get_param(cmd, "Interface");
9721 const char *bssid = get_param(cmd, "Bssid");
9722 const char *ssid = get_param(cmd, "SSID");
9723 const char *security = get_param(cmd, "Security");
9724 const char *passphrase = get_param(cmd, "Passphrase");
9725 const char *pin = get_param(cmd, "PIN");
9726 char buf[1000];
9727 char ssid_hex[200], passphrase_hex[200];
9728 const char *keymgmt, *cipher;
9729
9730 if (intf == NULL)
9731 intf = get_main_ifname();
9732
9733 if (!bssid) {
9734 send_resp(dut, conn, SIGMA_ERROR,
9735 "ErrorCode,Missing Bssid argument");
9736 return 0;
9737 }
9738
9739 if (!ssid) {
9740 send_resp(dut, conn, SIGMA_ERROR,
9741 "ErrorCode,Missing SSID argument");
9742 return 0;
9743 }
9744
9745 if (!security) {
9746 send_resp(dut, conn, SIGMA_ERROR,
9747 "ErrorCode,Missing Security argument");
9748 return 0;
9749 }
9750
9751 if (!passphrase) {
9752 send_resp(dut, conn, SIGMA_ERROR,
9753 "ErrorCode,Missing Passphrase argument");
9754 return 0;
9755 }
9756
9757 if (!pin) {
9758 send_resp(dut, conn, SIGMA_ERROR,
9759 "ErrorCode,Missing PIN argument");
9760 return 0;
9761 }
9762
vamsi krishna8c9c1562017-05-12 15:51:46 +05309763 if (2 * strlen(ssid) >= sizeof(ssid_hex) ||
9764 2 * strlen(passphrase) >= sizeof(passphrase_hex)) {
Jouni Malinencd4e3c32015-10-29 12:39:56 +02009765 send_resp(dut, conn, SIGMA_ERROR,
9766 "ErrorCode,Too long SSID/passphrase");
9767 return 0;
9768 }
9769
9770 ctrl = open_wpa_mon(intf);
9771 if (ctrl == NULL) {
9772 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to open "
9773 "wpa_supplicant monitor connection");
9774 return -2;
9775 }
9776
9777 if (strcasecmp(security, "wpa2-psk") == 0) {
9778 keymgmt = "WPA2PSK";
9779 cipher = "CCMP";
9780 } else {
9781 wpa_ctrl_detach(ctrl);
9782 wpa_ctrl_close(ctrl);
9783 send_resp(dut, conn, SIGMA_ERROR,
9784 "ErrorCode,Unsupported Security value");
9785 return 0;
9786 }
9787
9788 ascii2hexstr(ssid, ssid_hex);
9789 ascii2hexstr(passphrase, passphrase_hex);
9790 snprintf(buf, sizeof(buf), "WPS_REG %s %s %s %s %s %s",
9791 bssid, pin, ssid_hex, keymgmt, cipher, passphrase_hex);
9792
9793 if (wpa_command(intf, buf) < 0) {
9794 wpa_ctrl_detach(ctrl);
9795 wpa_ctrl_close(ctrl);
9796 send_resp(dut, conn, SIGMA_ERROR,
9797 "ErrorCode,Failed to start registrar");
9798 return 0;
9799 }
9800
9801 snprintf(dut->er_oper_bssid, sizeof(dut->er_oper_bssid), "%s", bssid);
9802 dut->er_oper_performed = 1;
9803
9804 return wps_connection_event(dut, conn, ctrl, intf, 0);
9805}
9806
9807
9808static int cmd_sta_wps_connect_pw_token(struct sigma_dut *dut,
9809 struct sigma_conn *conn,
9810 struct sigma_cmd *cmd)
9811{
9812 struct wpa_ctrl *ctrl;
9813 const char *intf = get_param(cmd, "Interface");
9814 const char *bssid = get_param(cmd, "Bssid");
9815 char buf[100];
9816
9817 if (!bssid) {
9818 send_resp(dut, conn, SIGMA_ERROR,
9819 "ErrorCode,Missing Bssid argument");
9820 return 0;
9821 }
9822
9823 ctrl = open_wpa_mon(intf);
9824 if (ctrl == NULL) {
9825 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to open "
9826 "wpa_supplicant monitor connection");
9827 return -2;
9828 }
9829
9830 snprintf(buf, sizeof(buf), "WPS_NFC %s", bssid);
9831
9832 if (wpa_command(intf, buf) < 0) {
9833 wpa_ctrl_detach(ctrl);
9834 wpa_ctrl_close(ctrl);
9835 send_resp(dut, conn, SIGMA_ERROR,
9836 "ErrorCode,Failed to start registrar");
9837 return 0;
9838 }
9839
9840 return wps_connection_event(dut, conn, ctrl, intf, 0);
9841}
9842
9843
vamsi krishna9b144002017-09-20 13:28:13 +05309844static int cmd_start_wps_registration(struct sigma_dut *dut,
9845 struct sigma_conn *conn,
9846 struct sigma_cmd *cmd)
9847{
9848 struct wpa_ctrl *ctrl;
9849 const char *intf = get_param(cmd, "Interface");
9850 const char *role, *method;
9851 int res;
9852 char buf[256];
9853 const char *events[] = {
9854 "CTRL-EVENT-CONNECTED",
9855 "WPS-OVERLAP-DETECTED",
9856 "WPS-TIMEOUT",
9857 "WPS-FAIL",
9858 NULL
9859 };
9860
9861 ctrl = open_wpa_mon(intf);
9862 if (!ctrl) {
9863 sigma_dut_print(dut, DUT_MSG_ERROR,
9864 "Failed to open wpa_supplicant monitor connection");
9865 return -2;
9866 }
9867
9868 role = get_param(cmd, "WpsRole");
9869 if (!role) {
9870 send_resp(dut, conn, SIGMA_INVALID,
9871 "ErrorCode,WpsRole not provided");
9872 goto fail;
9873 }
9874
9875 if (strcasecmp(role, "Enrollee") == 0) {
9876 method = get_param(cmd, "WpsConfigMethod");
9877 if (!method) {
9878 send_resp(dut, conn, SIGMA_INVALID,
9879 "ErrorCode,WpsConfigMethod not provided");
9880 goto fail;
9881 }
9882 if (strcasecmp(method, "PBC") == 0) {
9883 if (wpa_command(intf, "WPS_PBC") < 0) {
9884 send_resp(dut, conn, SIGMA_ERROR,
9885 "ErrorCode,Failed to enable PBC");
9886 goto fail;
9887 }
9888 } else {
9889 /* TODO: PIN method */
9890 send_resp(dut, conn, SIGMA_ERROR,
9891 "ErrorCode,Unsupported WpsConfigMethod value");
9892 goto fail;
9893 }
9894 res = get_wpa_cli_events(dut, ctrl, events, buf, sizeof(buf));
9895 if (res < 0) {
9896 send_resp(dut, conn, SIGMA_ERROR,
9897 "ErrorCode,WPS connection did not complete");
9898 goto fail;
9899 }
9900 if (strstr(buf, "WPS-TIMEOUT")) {
9901 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,NoPeer");
9902 } else if (strstr(buf, "WPS-OVERLAP-DETECTED")) {
9903 send_resp(dut, conn, SIGMA_ERROR,
9904 "ErrorCode,OverlapSession");
9905 } else if (strstr(buf, "CTRL-EVENT-CONNECTED")) {
9906 send_resp(dut, conn, SIGMA_COMPLETE, "Successful");
9907 } else {
9908 send_resp(dut, conn, SIGMA_ERROR,
9909 "ErrorCode,WPS operation failed");
9910 }
9911 } else {
9912 /* TODO: Registrar role */
9913 send_resp(dut, conn, SIGMA_ERROR,
9914 "ErrorCode,Unsupported WpsRole value");
9915 }
9916
9917fail:
9918 wpa_ctrl_detach(ctrl);
9919 wpa_ctrl_close(ctrl);
9920 return 0;
9921}
9922
9923
Jouni Malinencd4e3c32015-10-29 12:39:56 +02009924static int req_intf(struct sigma_cmd *cmd)
9925{
9926 return get_param(cmd, "interface") == NULL ? -1 : 0;
9927}
9928
9929
9930void sta_register_cmds(void)
9931{
9932 sigma_dut_reg_cmd("sta_get_ip_config", req_intf,
9933 cmd_sta_get_ip_config);
9934 sigma_dut_reg_cmd("sta_set_ip_config", req_intf,
9935 cmd_sta_set_ip_config);
9936 sigma_dut_reg_cmd("sta_get_info", req_intf, cmd_sta_get_info);
9937 sigma_dut_reg_cmd("sta_get_mac_address", req_intf,
9938 cmd_sta_get_mac_address);
9939 sigma_dut_reg_cmd("sta_is_connected", req_intf, cmd_sta_is_connected);
9940 sigma_dut_reg_cmd("sta_verify_ip_connection", req_intf,
9941 cmd_sta_verify_ip_connection);
9942 sigma_dut_reg_cmd("sta_get_bssid", req_intf, cmd_sta_get_bssid);
9943 sigma_dut_reg_cmd("sta_set_encryption", req_intf,
9944 cmd_sta_set_encryption);
9945 sigma_dut_reg_cmd("sta_set_psk", req_intf, cmd_sta_set_psk);
9946 sigma_dut_reg_cmd("sta_set_eaptls", req_intf, cmd_sta_set_eaptls);
9947 sigma_dut_reg_cmd("sta_set_eapttls", req_intf, cmd_sta_set_eapttls);
9948 sigma_dut_reg_cmd("sta_set_eapsim", req_intf, cmd_sta_set_eapsim);
9949 sigma_dut_reg_cmd("sta_set_peap", req_intf, cmd_sta_set_peap);
9950 sigma_dut_reg_cmd("sta_set_eapfast", req_intf, cmd_sta_set_eapfast);
9951 sigma_dut_reg_cmd("sta_set_eapaka", req_intf, cmd_sta_set_eapaka);
9952 sigma_dut_reg_cmd("sta_set_eapakaprime", req_intf,
9953 cmd_sta_set_eapakaprime);
9954 sigma_dut_reg_cmd("sta_set_security", req_intf, cmd_sta_set_security);
9955 sigma_dut_reg_cmd("sta_set_uapsd", req_intf, cmd_sta_set_uapsd);
9956 /* TODO: sta_set_ibss */
9957 /* TODO: sta_set_mode */
9958 sigma_dut_reg_cmd("sta_set_wmm", req_intf, cmd_sta_set_wmm);
9959 sigma_dut_reg_cmd("sta_associate", req_intf, cmd_sta_associate);
9960 /* TODO: sta_up_load */
9961 sigma_dut_reg_cmd("sta_preset_testparameters", req_intf,
9962 cmd_sta_preset_testparameters);
9963 /* TODO: sta_set_system */
9964 sigma_dut_reg_cmd("sta_set_11n", req_intf, cmd_sta_set_11n);
9965 /* TODO: sta_set_rifs_test */
9966 sigma_dut_reg_cmd("sta_set_wireless", req_intf, cmd_sta_set_wireless);
9967 sigma_dut_reg_cmd("sta_send_addba", req_intf, cmd_sta_send_addba);
9968 /* TODO: sta_send_coexist_mgmt */
9969 sigma_dut_reg_cmd("sta_disconnect", req_intf, cmd_sta_disconnect);
9970 sigma_dut_reg_cmd("sta_reassoc", req_intf, cmd_sta_reassoc);
9971 sigma_dut_reg_cmd("sta_reassociate", req_intf, cmd_sta_reassoc);
9972 sigma_dut_reg_cmd("sta_reset_default", req_intf,
9973 cmd_sta_reset_default);
9974 sigma_dut_reg_cmd("sta_send_frame", req_intf, cmd_sta_send_frame);
9975 sigma_dut_reg_cmd("sta_set_macaddr", req_intf, cmd_sta_set_macaddr);
9976 sigma_dut_reg_cmd("sta_set_rfeature", req_intf, cmd_sta_set_rfeature);
9977 sigma_dut_reg_cmd("sta_set_radio", req_intf, cmd_sta_set_radio);
9978 sigma_dut_reg_cmd("sta_set_pwrsave", req_intf, cmd_sta_set_pwrsave);
9979 sigma_dut_reg_cmd("sta_bssid_pool", req_intf, cmd_sta_bssid_pool);
9980 sigma_dut_reg_cmd("sta_reset_parm", req_intf, cmd_sta_reset_parm);
9981 sigma_dut_reg_cmd("sta_get_key", req_intf, cmd_sta_get_key);
9982 sigma_dut_reg_cmd("sta_hs2_associate", req_intf,
9983 cmd_sta_hs2_associate);
9984 sigma_dut_reg_cmd("sta_add_credential", req_intf,
9985 cmd_sta_add_credential);
9986 sigma_dut_reg_cmd("sta_scan", req_intf, cmd_sta_scan);
Jouni Malinen5e5d43d2018-01-10 17:29:33 +02009987 sigma_dut_reg_cmd("sta_scan_bss", req_intf, cmd_sta_scan_bss);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02009988 sigma_dut_reg_cmd("sta_set_systime", NULL, cmd_sta_set_systime);
9989 sigma_dut_reg_cmd("sta_osu", req_intf, cmd_sta_osu);
9990 sigma_dut_reg_cmd("sta_policy_update", req_intf, cmd_sta_policy_update);
9991 sigma_dut_reg_cmd("sta_er_config", NULL, cmd_sta_er_config);
9992 sigma_dut_reg_cmd("sta_wps_connect_pw_token", req_intf,
9993 cmd_sta_wps_connect_pw_token);
9994 sigma_dut_reg_cmd("sta_exec_action", req_intf, cmd_sta_exec_action);
9995 sigma_dut_reg_cmd("sta_get_events", req_intf, cmd_sta_get_events);
9996 sigma_dut_reg_cmd("sta_get_parameter", req_intf, cmd_sta_get_parameter);
vamsi krishna9b144002017-09-20 13:28:13 +05309997 sigma_dut_reg_cmd("start_wps_registration", req_intf,
9998 cmd_start_wps_registration);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02009999}