blob: ff8cca39fab5bfe7bf0dd67e80558a234d252e56 [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.
4 * Copyright (c) 2011-2015, Qualcomm Atheros, Inc.
5 * All Rights Reserved.
6 * Licensed under the Clear BSD license. See README for more details.
7 */
8
9#include "sigma_dut.h"
10#include <sys/ioctl.h>
11#include <sys/stat.h>
12#ifdef __linux__
Lior Davidcc88b562017-01-03 18:52:09 +020013#include <regex.h>
14#include <dirent.h>
Jouni Malinencd4e3c32015-10-29 12:39:56 +020015#include <sys/time.h>
16#include <netpacket/packet.h>
17#include <linux/if_ether.h>
18#ifdef ANDROID
19#include <cutils/properties.h>
20#include <android/log.h>
21#include "keystore_get.h"
22#else /* ANDROID */
23#include <ifaddrs.h>
24#endif /* ANDROID */
25#include <netdb.h>
26#endif /* __linux__ */
27#ifdef __QNXNTO__
28#include <net/if_dl.h>
29#endif /* __QNXNTO__ */
30#include "wpa_ctrl.h"
31#include "wpa_helpers.h"
Amarnath Hullur Subramanyam9c381f52017-03-17 00:04:41 -070032#include "miracast.h"
Jouni Malinencd4e3c32015-10-29 12:39:56 +020033
34/* Temporary files for sta_send_addba */
35#define VI_QOS_TMP_FILE "/tmp/vi-qos.tmp"
36#define VI_QOS_FILE "/tmp/vi-qos.txt"
37#define VI_QOS_REFFILE "/etc/vi-qos.txt"
38
39/*
40 * MTU for Ethernet need to take into account 8-byte SNAP header
41 * to be added when encapsulating Ethernet frame into 802.11
42 */
43#ifndef IEEE80211_MAX_DATA_LEN_DMG
44#define IEEE80211_MAX_DATA_LEN_DMG 7920
45#endif
46#ifndef IEEE80211_SNAP_LEN_DMG
47#define IEEE80211_SNAP_LEN_DMG 8
48#endif
49
Ashwini Patil00402582017-04-13 12:29:39 +053050#define NON_PREF_CH_LIST_SIZE 100
Ashwini Patil5acd7382017-04-13 15:55:04 +053051#define NEIGHBOR_REPORT_SIZE 1000
52#define DEFAULT_NEIGHBOR_BSSID_INFO "17"
53#define DEFAULT_NEIGHBOR_PHY_TYPE "1"
Ashwini Patil00402582017-04-13 12:29:39 +053054
Jouni Malinencd4e3c32015-10-29 12:39:56 +020055extern char *sigma_wpas_ctrl;
56extern char *sigma_cert_path;
57extern enum driver_type wifi_chip_type;
58extern char *sigma_radio_ifname[];
59
Lior David0fe101e2017-03-09 16:09:50 +020060#ifdef __linux__
61#define WIL_WMI_MAX_PAYLOAD 248
62#define WIL_WMI_BF_TRIG_CMDID 0x83a
63
64struct wil_wmi_header {
65 uint8_t mid;
66 uint8_t reserved;
67 uint16_t cmd;
68 uint32_t ts;
69} __attribute__((packed));
70
71enum wil_wmi_bf_trig_type {
72 WIL_WMI_SLS,
73 WIL_WMI_BRP_RX,
74 WIL_WMI_BRP_TX,
75};
76
77struct wil_wmi_bf_trig_cmd {
78 /* enum wil_wmi_bf_trig_type */
79 uint32_t bf_type;
80 /* cid when type == WMI_BRP_RX */
81 uint32_t sta_id;
82 uint32_t reserved;
83 /* mac address when type = WIL_WMI_SLS */
84 uint8_t dest_mac[6];
85} __attribute__((packed));
86#endif /* __linux__ */
Jouni Malinencd4e3c32015-10-29 12:39:56 +020087
88#ifdef ANDROID
89
90static int add_ipv6_rule(struct sigma_dut *dut, const char *ifname);
91
92#define ANDROID_KEYSTORE_GET 'g'
93#define ANDROID_KEYSTORE_GET_PUBKEY 'b'
94
95static int android_keystore_get(char cmd, const char *key, unsigned char *val)
96{
Jouni Malinencd4e3c32015-10-29 12:39:56 +020097 /* Android 4.3 changed keystore design, so need to use keystore_get() */
98#ifndef KEYSTORE_MESSAGE_SIZE
99#define KEYSTORE_MESSAGE_SIZE 65535
100#endif /* KEYSTORE_MESSAGE_SIZE */
101
102 ssize_t len;
103 uint8_t *value = NULL;
104
105 __android_log_print(ANDROID_LOG_DEBUG, "sigma_dut",
106 "keystore command '%c' key '%s' --> keystore_get",
107 cmd, key);
108
109 len = keystore_get(key, strlen(key), &value);
110 if (len < 0) {
111 __android_log_print(ANDROID_LOG_DEBUG, "sigma_dut",
112 "keystore_get() failed");
113 return -1;
114 }
115
116 if (len > KEYSTORE_MESSAGE_SIZE)
117 len = KEYSTORE_MESSAGE_SIZE;
118 memcpy(val, value, len);
119 free(value);
120 return len;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200121}
122#endif /* ANDROID */
123
124
125int set_ps(const char *intf, struct sigma_dut *dut, int enabled)
126{
127#ifdef __linux__
128 char buf[100];
129
130 if (wifi_chip_type == DRIVER_WCN) {
131 if (enabled) {
132 snprintf(buf, sizeof(buf), "iwpriv wlan0 dump 906");
Pradeep Reddy POTTETI625b3702016-09-20 17:09:58 +0530133 if (system(buf) != 0)
134 goto set_power_save;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200135 } else {
136 snprintf(buf, sizeof(buf), "iwpriv wlan0 dump 905");
Pradeep Reddy POTTETI625b3702016-09-20 17:09:58 +0530137 if (system(buf) != 0)
138 goto set_power_save;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200139 snprintf(buf, sizeof(buf), "iwpriv wlan0 dump 912");
Pradeep Reddy POTTETI625b3702016-09-20 17:09:58 +0530140 if (system(buf) != 0)
141 goto set_power_save;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200142 }
143
144 return 0;
145 }
146
Pradeep Reddy POTTETI625b3702016-09-20 17:09:58 +0530147set_power_save:
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200148 snprintf(buf, sizeof(buf), "./iw dev %s set power_save %s",
149 intf, enabled ? "on" : "off");
150 if (system(buf) != 0) {
151 snprintf(buf, sizeof(buf), "iw dev %s set power_save %s",
152 intf, enabled ? "on" : "off");
Pradeep Reddy POTTETI625b3702016-09-20 17:09:58 +0530153 if (system(buf) != 0) {
154 sigma_dut_print(dut, DUT_MSG_ERROR,
155 "Failed to set power save %s",
156 enabled ? "on" : "off");
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200157 return -1;
Pradeep Reddy POTTETI625b3702016-09-20 17:09:58 +0530158 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200159 }
160
161 return 0;
162#else /* __linux__ */
163 return -1;
164#endif /* __linux__ */
165}
166
167
Lior Davidcc88b562017-01-03 18:52:09 +0200168#ifdef __linux__
Lior David0fe101e2017-03-09 16:09:50 +0200169
Lior Davidcc88b562017-01-03 18:52:09 +0200170static int wil6210_get_debugfs_dir(struct sigma_dut *dut, char *path,
171 size_t len)
172{
173 DIR *dir, *wil_dir;
174 struct dirent *entry;
175 int ret = -1;
176 const char *root_path = "/sys/kernel/debug/ieee80211";
177
178 dir = opendir(root_path);
179 if (!dir)
180 return -2;
181
182 while ((entry = readdir(dir))) {
183 if (strcmp(entry->d_name, ".") == 0 ||
184 strcmp(entry->d_name, "..") == 0)
185 continue;
186
187 if (snprintf(path, len, "%s/%s/wil6210",
188 root_path, entry->d_name) >= (int) len) {
189 ret = -3;
190 break;
191 }
192
193 wil_dir = opendir(path);
194 if (wil_dir) {
195 closedir(wil_dir);
196 ret = 0;
197 break;
198 }
199 }
200
201 closedir(dir);
202 return ret;
203}
Lior David0fe101e2017-03-09 16:09:50 +0200204
205
206static int wil6210_wmi_send(struct sigma_dut *dut, uint16_t command,
207 void *payload, uint16_t length)
208{
209 struct {
210 struct wil_wmi_header hdr;
211 char payload[WIL_WMI_MAX_PAYLOAD];
212 } __attribute__((packed)) cmd;
213 char buf[128], fname[128];
214 size_t towrite, written;
215 FILE *f;
216
217 if (length > WIL_WMI_MAX_PAYLOAD) {
218 sigma_dut_print(dut, DUT_MSG_ERROR,
219 "payload too large(%u, max %u)",
220 length, WIL_WMI_MAX_PAYLOAD);
221 return -1;
222 }
223
224 memset(&cmd.hdr, 0, sizeof(cmd.hdr));
225 cmd.hdr.cmd = command;
226 memcpy(cmd.payload, payload, length);
227
228 if (wil6210_get_debugfs_dir(dut, buf, sizeof(buf))) {
229 sigma_dut_print(dut, DUT_MSG_ERROR,
230 "failed to get wil6210 debugfs dir");
231 return -1;
232 }
233
234 snprintf(fname, sizeof(fname), "%s/wmi_send", buf);
235 f = fopen(fname, "wb");
236 if (!f) {
237 sigma_dut_print(dut, DUT_MSG_ERROR,
238 "failed to open: %s", fname);
239 return -1;
240 }
241
242 towrite = sizeof(cmd.hdr) + length;
243 written = fwrite(&cmd, 1, towrite, f);
244 fclose(f);
245 if (written != towrite) {
246 sigma_dut_print(dut, DUT_MSG_ERROR,
247 "failed to send wmi %u", command);
248 return -1;
249 }
250
251 return 0;
252}
253
254
255static int wil6210_get_sta_info_field(struct sigma_dut *dut, const char *bssid,
256 const char *pattern, unsigned int *field)
257{
258 char buf[128], fname[128];
259 FILE *f;
260 regex_t re;
261 regmatch_t m[2];
262 int rc, ret = -1;
263
264 if (wil6210_get_debugfs_dir(dut, buf, sizeof(buf))) {
265 sigma_dut_print(dut, DUT_MSG_ERROR,
266 "failed to get wil6210 debugfs dir");
267 return -1;
268 }
269
270 snprintf(fname, sizeof(fname), "%s/stations", buf);
271 f = fopen(fname, "r");
272 if (!f) {
273 sigma_dut_print(dut, DUT_MSG_ERROR,
274 "failed to open: %s", fname);
275 return -1;
276 }
277
278 if (regcomp(&re, pattern, REG_EXTENDED)) {
279 sigma_dut_print(dut, DUT_MSG_ERROR,
280 "regcomp failed: %s", pattern);
281 goto out;
282 }
283
284 /*
285 * find the entry for the mac address
286 * line is of the form: [n] 11:22:33:44:55:66 state AID aid
287 */
288 while (fgets(buf, sizeof(buf), f)) {
289 if (strcasestr(buf, bssid)) {
290 /* extract the field (CID/AID/state) */
291 rc = regexec(&re, buf, 2, m, 0);
292 if (!rc && (m[1].rm_so >= 0)) {
293 buf[m[1].rm_eo] = 0;
294 *field = atoi(&buf[m[1].rm_so]);
295 ret = 0;
296 break;
297 }
298 }
299 }
300
301 regfree(&re);
302 if (ret)
303 sigma_dut_print(dut, DUT_MSG_ERROR,
304 "could not extract field");
305
306out:
307 fclose(f);
308
309 return ret;
310}
311
312
313static int wil6210_get_cid(struct sigma_dut *dut, const char *bssid,
314 unsigned int *cid)
315{
316 const char *pattern = "\\[([0-9]+)\\]";
317
318 return wil6210_get_sta_info_field(dut, bssid, pattern, cid);
319}
320
321
322static int wil6210_send_brp_rx(struct sigma_dut *dut, const char *mac,
323 int l_rx)
324{
Rakesh Sunki556237d2017-03-30 14:49:31 -0700325 struct wil_wmi_bf_trig_cmd cmd;
Lior David0fe101e2017-03-09 16:09:50 +0200326 unsigned int cid;
327
Rakesh Sunki556237d2017-03-30 14:49:31 -0700328 memset(&cmd, 0, sizeof(cmd));
329
Lior David0fe101e2017-03-09 16:09:50 +0200330 if (wil6210_get_cid(dut, mac, &cid))
331 return -1;
332
333 cmd.bf_type = WIL_WMI_BRP_RX;
334 cmd.sta_id = cid;
335 /* training length (l_rx) is ignored, FW always uses length 16 */
336 return wil6210_wmi_send(dut, WIL_WMI_BF_TRIG_CMDID,
337 &cmd, sizeof(cmd));
338}
339
340
341static int wil6210_send_sls(struct sigma_dut *dut, const char *mac)
342{
Rakesh Sunki556237d2017-03-30 14:49:31 -0700343 struct wil_wmi_bf_trig_cmd cmd;
344
345 memset(&cmd, 0, sizeof(cmd));
Lior David0fe101e2017-03-09 16:09:50 +0200346
347 if (parse_mac_address(dut, mac, (unsigned char *)&cmd.dest_mac))
348 return -1;
349
350 cmd.bf_type = WIL_WMI_SLS;
351 return wil6210_wmi_send(dut, WIL_WMI_BF_TRIG_CMDID,
352 &cmd, sizeof(cmd));
353}
354
Lior Davidcc88b562017-01-03 18:52:09 +0200355#endif /* __linux__ */
356
357
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200358static void static_ip_file(int proto, const char *addr, const char *mask,
359 const char *gw)
360{
361 if (proto) {
362 FILE *f = fopen("static-ip", "w");
363 if (f) {
364 fprintf(f, "%d %s %s %s\n", proto, addr,
365 mask ? mask : "N/A",
366 gw ? gw : "N/A");
367 fclose(f);
368 }
369 } else {
370 unlink("static-ip");
371 }
372}
373
374
375static int send_neighbor_request(struct sigma_dut *dut, const char *intf,
376 const char *ssid)
377{
378#ifdef __linux__
379 char buf[100];
380
381 snprintf(buf, sizeof(buf), "iwpriv %s neighbor %s",
382 intf, ssid);
383 sigma_dut_print(dut, DUT_MSG_INFO, "Request: %s", buf);
384
385 if (system(buf) != 0) {
386 sigma_dut_print(dut, DUT_MSG_ERROR,
387 "iwpriv neighbor request failed");
388 return -1;
389 }
390
391 sigma_dut_print(dut, DUT_MSG_INFO, "iwpriv neighbor request send");
392
393 return 0;
394#else /* __linux__ */
395 return -1;
396#endif /* __linux__ */
397}
398
399
400static int send_trans_mgmt_query(struct sigma_dut *dut, const char *intf,
Ashwini Patil5acd7382017-04-13 15:55:04 +0530401 struct sigma_cmd *cmd)
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200402{
Ashwini Patil5acd7382017-04-13 15:55:04 +0530403 const char *val;
404 int reason_code = 0;
405 char buf[1024];
406
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200407 /*
408 * In the earlier builds we used WNM_QUERY and in later
409 * builds used WNM_BSS_QUERY.
410 */
411
Ashwini Patil5acd7382017-04-13 15:55:04 +0530412 val = get_param(cmd, "BTMQuery_Reason_Code");
413 if (val)
414 reason_code = atoi(val);
415
416 val = get_param(cmd, "Cand_List");
417 if (val && atoi(val) == 1 && dut->btm_query_cand_list) {
418 snprintf(buf, sizeof(buf), "WNM_BSS_QUERY %d%s", reason_code,
419 dut->btm_query_cand_list);
420 free(dut->btm_query_cand_list);
421 dut->btm_query_cand_list = NULL;
422 } else {
423 snprintf(buf, sizeof(buf), "WNM_BSS_QUERY %d", reason_code);
424 }
425
426 if (wpa_command(intf, buf) != 0) {
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200427 sigma_dut_print(dut, DUT_MSG_ERROR,
428 "transition management query failed");
429 return -1;
430 }
431
432 sigma_dut_print(dut, DUT_MSG_DEBUG,
433 "transition management query sent");
434
435 return 0;
436}
437
438
439int is_ip_addr(const char *str)
440{
441 const char *pos = str;
442 struct in_addr addr;
443
444 while (*pos) {
445 if (*pos != '.' && (*pos < '0' || *pos > '9'))
446 return 0;
447 pos++;
448 }
449
450 return inet_aton(str, &addr);
451}
452
453
454int is_ipv6_addr(const char *str)
455{
456 struct sockaddr_in6 addr;
457
458 return inet_pton(AF_INET6, str, &(addr.sin6_addr));
459}
460
461
462int get_ip_config(struct sigma_dut *dut, const char *ifname, char *buf,
463 size_t buf_len)
464{
465 char tmp[256], *pos, *pos2;
466 FILE *f;
467 char ip[16], mask[15], dns[16], sec_dns[16];
Sarvepalli, Rajesh Babua76c6442016-03-18 20:34:26 +0530468 const char *str_ps;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200469 int is_dhcp = 0;
470 int s;
471#ifdef ANDROID
472 char prop[PROPERTY_VALUE_MAX];
473#endif /* ANDROID */
474
475 ip[0] = '\0';
476 mask[0] = '\0';
477 dns[0] = '\0';
478 sec_dns[0] = '\0';
479
480 s = socket(PF_INET, SOCK_DGRAM, 0);
481 if (s >= 0) {
482 struct ifreq ifr;
483 struct sockaddr_in saddr;
484
485 memset(&ifr, 0, sizeof(ifr));
Peng Xub8fc5cc2017-05-10 17:27:28 -0700486 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200487 if (ioctl(s, SIOCGIFADDR, &ifr) < 0) {
488 sigma_dut_print(dut, DUT_MSG_INFO, "Failed to get "
489 "%s IP address: %s",
490 ifname, strerror(errno));
491 } else {
492 memcpy(&saddr, &ifr.ifr_addr,
493 sizeof(struct sockaddr_in));
Peng Xub8fc5cc2017-05-10 17:27:28 -0700494 strlcpy(ip, inet_ntoa(saddr.sin_addr), sizeof(ip));
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200495 }
496
497 if (ioctl(s, SIOCGIFNETMASK, &ifr) == 0) {
498 memcpy(&saddr, &ifr.ifr_addr,
499 sizeof(struct sockaddr_in));
Peng Xub8fc5cc2017-05-10 17:27:28 -0700500 strlcpy(mask, inet_ntoa(saddr.sin_addr), sizeof(mask));
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200501 }
502 close(s);
503 }
504
505#ifdef ANDROID
506 snprintf(tmp, sizeof(tmp), "dhcp.%s.pid", ifname);
507 if (property_get(tmp, prop, NULL) != 0 && atoi(prop) > 0) {
508 snprintf(tmp, sizeof(tmp), "dhcp.%s.result", ifname);
509 if (property_get(tmp, prop, NULL) != 0 &&
510 strcmp(prop, "ok") == 0) {
511 snprintf(tmp, sizeof(tmp), "dhcp.%s.ipaddress",
512 ifname);
513 if (property_get(tmp, prop, NULL) != 0 &&
514 strcmp(ip, prop) == 0)
515 is_dhcp = 1;
516 }
517 }
518
519 snprintf(tmp, sizeof(tmp), "dhcp.%s.dns1", ifname);
Peng Xub8fc5cc2017-05-10 17:27:28 -0700520 if (property_get(tmp, prop, NULL) != 0)
521 strlcpy(dns, prop, sizeof(dns));
522 else if (property_get("net.dns1", prop, NULL) != 0)
523 strlcpy(dns, prop, sizeof(dns));
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200524
525 snprintf(tmp, sizeof(tmp), "dhcp.%s.dns2", ifname);
Peng Xub8fc5cc2017-05-10 17:27:28 -0700526 if (property_get(tmp, prop, NULL) != 0)
527 strlcpy(sec_dns, prop, sizeof(sec_dns));
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200528#else /* ANDROID */
529#ifdef __linux__
Sarvepalli, Rajesh Babua76c6442016-03-18 20:34:26 +0530530 if (get_driver_type() == DRIVER_OPENWRT)
531 str_ps = "ps -w";
532 else
533 str_ps = "ps ax";
534 snprintf(tmp, sizeof(tmp),
535 "%s | grep dhclient | grep -v grep | grep -q %s",
536 str_ps, ifname);
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200537 if (system(tmp) == 0)
538 is_dhcp = 1;
539 else {
Sarvepalli, Rajesh Babua76c6442016-03-18 20:34:26 +0530540 snprintf(tmp, sizeof(tmp),
541 "%s | grep udhcpc | grep -v grep | grep -q %s",
542 str_ps, ifname);
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200543 if (system(tmp) == 0)
544 is_dhcp = 1;
545 else {
Sarvepalli, Rajesh Babua76c6442016-03-18 20:34:26 +0530546 snprintf(tmp, sizeof(tmp),
547 "%s | grep dhcpcd | grep -v grep | grep -q %s",
548 str_ps, ifname);
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200549 if (system(tmp) == 0)
550 is_dhcp = 1;
551 }
552 }
553#endif /* __linux__ */
554
555 f = fopen("/etc/resolv.conf", "r");
556 if (f) {
557 while (fgets(tmp, sizeof(tmp), f)) {
558 if (strncmp(tmp, "nameserver", 10) != 0)
559 continue;
560 pos = tmp + 10;
561 while (*pos == ' ' || *pos == '\t')
562 pos++;
563 pos2 = pos;
564 while (*pos2) {
565 if (*pos2 == '\n' || *pos2 == '\r') {
566 *pos2 = '\0';
567 break;
568 }
569 pos2++;
570 }
Peng Xub8fc5cc2017-05-10 17:27:28 -0700571 if (!dns[0])
572 strlcpy(dns, pos, sizeof(dns));
573 else if (!sec_dns[0])
574 strlcpy(sec_dns, pos, sizeof(sec_dns));
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200575 }
576 fclose(f);
577 }
578#endif /* ANDROID */
579
580 snprintf(buf, buf_len, "dhcp,%d,ip,%s,mask,%s,primary-dns,%s",
581 is_dhcp, ip, mask, dns);
582 buf[buf_len - 1] = '\0';
583
584 return 0;
585}
586
587
588
589
590int get_ipv6_config(struct sigma_dut *dut, const char *ifname, char *buf,
591 size_t buf_len)
592{
593#ifdef __linux__
594#ifdef ANDROID
595 char cmd[200], result[1000], *pos, *end;
596 FILE *f;
597 size_t len;
598
599 snprintf(cmd, sizeof(cmd), "ip addr show dev %s scope global", ifname);
600 f = popen(cmd, "r");
601 if (f == NULL)
602 return -1;
603 len = fread(result, 1, sizeof(result) - 1, f);
604 pclose(f);
605 if (len == 0)
606 return -1;
607 result[len] = '\0';
608 sigma_dut_print(dut, DUT_MSG_DEBUG, "%s result: %s\n", cmd, result);
609
610 pos = strstr(result, "inet6 ");
611 if (pos == NULL)
612 return -1;
613 pos += 6;
614 end = strchr(pos, ' ');
615 if (end)
616 *end = '\0';
617 end = strchr(pos, '/');
618 if (end)
619 *end = '\0';
620 snprintf(buf, buf_len, "ip,%s", pos);
621 buf[buf_len - 1] = '\0';
622 return 0;
623#else /* ANDROID */
624 struct ifaddrs *ifaddr, *ifa;
625 int res, found = 0;
626 char host[NI_MAXHOST];
627
628 if (getifaddrs(&ifaddr) < 0) {
629 perror("getifaddrs");
630 return -1;
631 }
632
633 for (ifa = ifaddr; ifa; ifa = ifa->ifa_next) {
634 if (strcasecmp(ifname, ifa->ifa_name) != 0)
635 continue;
636 if (ifa->ifa_addr == NULL ||
637 ifa->ifa_addr->sa_family != AF_INET6)
638 continue;
639
640 res = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in6),
641 host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
642 if (res != 0) {
643 sigma_dut_print(dut, DUT_MSG_DEBUG, "getnameinfo: %s",
644 gai_strerror(res));
645 continue;
646 }
647 if (strncmp(host, "fe80::", 6) == 0)
648 continue; /* skip link-local */
649
650 sigma_dut_print(dut, DUT_MSG_DEBUG, "ifaddr: %s", host);
651 found = 1;
652 break;
653 }
654
655 freeifaddrs(ifaddr);
656
657 if (found) {
658 char *pos;
659 pos = strchr(host, '%');
660 if (pos)
661 *pos = '\0';
662 snprintf(buf, buf_len, "ip,%s", host);
663 buf[buf_len - 1] = '\0';
664 return 0;
665 }
666
667#endif /* ANDROID */
668#endif /* __linux__ */
669 return -1;
670}
671
672
673static int cmd_sta_get_ip_config(struct sigma_dut *dut,
674 struct sigma_conn *conn,
675 struct sigma_cmd *cmd)
676{
677 const char *intf = get_param(cmd, "Interface");
678 const char *ifname;
679 char buf[200];
680 const char *val;
681 int type = 1;
682
683 if (intf == NULL)
684 return -1;
685
686 if (strcmp(intf, get_main_ifname()) == 0)
687 ifname = get_station_ifname();
688 else
689 ifname = intf;
690
691 /*
692 * UCC may assume the IP address to be available immediately after
693 * association without trying to run sta_get_ip_config multiple times.
694 * Sigma CAPI does not specify this command as a block command that
695 * would wait for the address to become available, but to pass tests
696 * more reliably, it looks like such a wait may be needed here.
697 */
698 if (wait_ip_addr(dut, ifname, 15) < 0) {
699 sigma_dut_print(dut, DUT_MSG_INFO, "Could not get IP address "
700 "for sta_get_ip_config");
701 /*
702 * Try to continue anyway since many UCC tests do not really
703 * care about the return value from here..
704 */
705 }
706
707 val = get_param(cmd, "Type");
708 if (val)
709 type = atoi(val);
710 if (type == 2 || dut->last_set_ip_config_ipv6) {
711 int i;
712
713 /*
714 * Since we do not have proper wait for IPv6 addresses, use a
715 * fixed two second delay here as a workaround for UCC script
716 * assuming IPv6 address is available when this command returns.
717 * Some scripts did not use Type,2 properly for IPv6, so include
718 * also the cases where the previous sta_set_ip_config indicated
719 * use of IPv6.
720 */
721 sigma_dut_print(dut, DUT_MSG_INFO, "Wait up to extra ten seconds in sta_get_ip_config for IPv6 address");
722 for (i = 0; i < 10; i++) {
723 sleep(1);
724 if (get_ipv6_config(dut, ifname, buf, sizeof(buf)) == 0)
725 {
726 sigma_dut_print(dut, DUT_MSG_INFO, "Found IPv6 address");
727 send_resp(dut, conn, SIGMA_COMPLETE, buf);
728#ifdef ANDROID
729 sigma_dut_print(dut, DUT_MSG_INFO,
730 "Adding IPv6 rule on Android");
731 add_ipv6_rule(dut, intf);
732#endif /* ANDROID */
733
734 return 0;
735 }
736 }
737 }
738 if (type == 1) {
739 if (get_ip_config(dut, ifname, buf, sizeof(buf)) < 0)
740 return -2;
741 } else if (type == 2) {
742 if (get_ipv6_config(dut, ifname, buf, sizeof(buf)) < 0)
743 return -2;
744 } else {
745 send_resp(dut, conn, SIGMA_ERROR,
746 "errorCode,Unsupported address type");
747 return 0;
748 }
749
750 send_resp(dut, conn, SIGMA_COMPLETE, buf);
751 return 0;
752}
753
754
755static void kill_dhcp_client(struct sigma_dut *dut, const char *ifname)
756{
757#ifdef __linux__
758 char buf[200];
759 char path[128];
760 struct stat s;
761
762#ifdef ANDROID
763 snprintf(path, sizeof(path), "/data/misc/dhcp/dhcpcd-%s.pid", ifname);
764#else /* ANDROID */
765 snprintf(path, sizeof(path), "/var/run/dhclient-%s.pid", ifname);
766#endif /* ANDROID */
767 if (stat(path, &s) == 0) {
768 snprintf(buf, sizeof(buf), "kill `cat %s`", path);
769 sigma_dut_print(dut, DUT_MSG_INFO,
770 "Kill previous DHCP client: %s", buf);
771 if (system(buf) != 0)
772 sigma_dut_print(dut, DUT_MSG_INFO,
773 "Failed to kill DHCP client");
774 unlink(path);
775 sleep(1);
776 } else {
777 snprintf(path, sizeof(path), "/var/run/dhcpcd-%s.pid", ifname);
778
779 if (stat(path, &s) == 0) {
780 snprintf(buf, sizeof(buf), "kill `cat %s`", path);
781 sigma_dut_print(dut, DUT_MSG_INFO,
782 "Kill previous DHCP client: %s", buf);
783 if (system(buf) != 0)
784 sigma_dut_print(dut, DUT_MSG_INFO,
785 "Failed to kill DHCP client");
786 unlink(path);
787 sleep(1);
788 }
789 }
790#endif /* __linux__ */
791}
792
793
794static int start_dhcp_client(struct sigma_dut *dut, const char *ifname)
795{
796#ifdef __linux__
797 char buf[200];
798
799#ifdef ANDROID
Purushottam Kushwaha46d64262016-08-23 17:57:53 +0530800 if (access("/system/bin/dhcpcd", F_OK) != -1) {
801 snprintf(buf, sizeof(buf),
802 "/system/bin/dhcpcd -b %s", ifname);
803 } else if (access("/system/bin/dhcptool", F_OK) != -1) {
804 snprintf(buf, sizeof(buf), "/system/bin/dhcptool %s &", ifname);
805 } else {
806 sigma_dut_print(dut, DUT_MSG_ERROR,
807 "DHCP client program missing");
808 return 0;
809 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200810#else /* ANDROID */
811 snprintf(buf, sizeof(buf),
812 "dhclient -nw -pf /var/run/dhclient-%s.pid %s",
813 ifname, ifname);
814#endif /* ANDROID */
815 sigma_dut_print(dut, DUT_MSG_INFO, "Start DHCP client: %s", buf);
816 if (system(buf) != 0) {
817 snprintf(buf, sizeof(buf), "dhcpcd -t 0 %s &", ifname);
818 if (system(buf) != 0) {
819 sigma_dut_print(dut, DUT_MSG_INFO,
820 "Failed to start DHCP client");
821#ifndef ANDROID
822 return -1;
823#endif /* ANDROID */
824 }
825 }
826#endif /* __linux__ */
827
828 return 0;
829}
830
831
832static int clear_ip_addr(struct sigma_dut *dut, const char *ifname)
833{
834#ifdef __linux__
835 char buf[200];
836
837 snprintf(buf, sizeof(buf), "ip addr flush dev %s", ifname);
838 if (system(buf) != 0) {
839 sigma_dut_print(dut, DUT_MSG_INFO,
840 "Failed to clear IP addresses");
841 return -1;
842 }
843#endif /* __linux__ */
844
845 return 0;
846}
847
848
849#ifdef ANDROID
850static int add_ipv6_rule(struct sigma_dut *dut, const char *ifname)
851{
852 char cmd[200], *result, *pos;
853 FILE *fp;
Pradeep Reddy POTTETIf58a1fe2016-10-13 17:22:03 +0530854 int tableid;
855 size_t len, result_len = 1000;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200856
857 snprintf(cmd, sizeof(cmd), "ip -6 route list table all | grep %s",
858 ifname);
859 fp = popen(cmd, "r");
860 if (fp == NULL)
861 return -1;
862
863 result = malloc(result_len);
Pradeep Reddy POTTETI673d85c2016-07-26 19:08:07 +0530864 if (result == NULL) {
865 fclose(fp);
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200866 return -1;
Pradeep Reddy POTTETI673d85c2016-07-26 19:08:07 +0530867 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200868
Pradeep Reddy POTTETIf58a1fe2016-10-13 17:22:03 +0530869 len = fread(result, 1, result_len - 1, fp);
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200870 fclose(fp);
871
872 if (len == 0) {
873 free(result);
874 return -1;
875 }
Pradeep Reddy POTTETIf58a1fe2016-10-13 17:22:03 +0530876 result[len] = '\0';
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200877
878 pos = strstr(result, "table ");
879 if (pos == NULL) {
880 free(result);
881 return -1;
882 }
883
884 pos += strlen("table ");
885 tableid = atoi(pos);
886 if (tableid != 0) {
887 if (system("ip -6 rule del prio 22000") != 0) {
888 /* ignore any error */
889 }
890 snprintf(cmd, sizeof(cmd),
891 "ip -6 rule add from all lookup %d prio 22000",
892 tableid);
893 if (system(cmd) != 0) {
894 sigma_dut_print(dut, DUT_MSG_INFO,
895 "Failed to run %s", cmd);
896 free(result);
897 return -1;
898 }
899 } else {
900 sigma_dut_print(dut, DUT_MSG_INFO,
901 "No Valid Table Id found %s", pos);
902 free(result);
903 return -1;
904 }
905 free(result);
906
907 return 0;
908}
909#endif /* ANDROID */
910
911
912static int cmd_sta_set_ip_config(struct sigma_dut *dut,
913 struct sigma_conn *conn,
914 struct sigma_cmd *cmd)
915{
916 const char *intf = get_param(cmd, "Interface");
917 const char *ifname;
918 char buf[200];
919 const char *val, *ip, *mask, *gw;
920 int type = 1;
921
922 if (intf == NULL)
923 return -1;
924
925 if (strcmp(intf, get_main_ifname()) == 0)
926 ifname = get_station_ifname();
927 else
928 ifname = intf;
929
930 if (if_nametoindex(ifname) == 0) {
931 send_resp(dut, conn, SIGMA_ERROR,
932 "ErrorCode,Unknown interface");
933 return 0;
934 }
935
936 val = get_param(cmd, "Type");
937 if (val) {
938 type = atoi(val);
939 if (type != 1 && type != 2) {
940 send_resp(dut, conn, SIGMA_ERROR,
941 "ErrorCode,Unsupported address type");
942 return 0;
943 }
944 }
945
946 dut->last_set_ip_config_ipv6 = 0;
947
948 val = get_param(cmd, "dhcp");
949 if (val && (strcmp(val, "1") == 0 || strcasecmp(val, "true") == 0)) {
950 static_ip_file(0, NULL, NULL, NULL);
951#ifdef __linux__
952 if (type == 2) {
953 dut->last_set_ip_config_ipv6 = 1;
954 sigma_dut_print(dut, DUT_MSG_INFO, "Using IPv6 "
955 "stateless address autoconfiguration");
956#ifdef ANDROID
957 /*
958 * This sleep is required as the assignment in case of
959 * Android is taking time and is done by the kernel.
960 * The subsequent ping for IPv6 is impacting HS20 test
961 * case.
962 */
963 sleep(2);
964 add_ipv6_rule(dut, intf);
965#endif /* ANDROID */
966 /* Assume this happens by default */
967 return 1;
968 }
969
970 kill_dhcp_client(dut, ifname);
971 if (start_dhcp_client(dut, ifname) < 0)
972 return -2;
973 return 1;
974#endif /* __linux__ */
975 return -2;
976 }
977
978 ip = get_param(cmd, "ip");
Pradeep Reddy POTTETIb18c5652016-01-18 12:45:37 +0530979 if (!ip) {
980 send_resp(dut, conn, SIGMA_INVALID,
981 "ErrorCode,Missing IP address");
982 return 0;
983 }
984
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200985 mask = get_param(cmd, "mask");
Pradeep Reddy POTTETIb18c5652016-01-18 12:45:37 +0530986 if (!mask) {
987 send_resp(dut, conn, SIGMA_INVALID,
988 "ErrorCode,Missing subnet mask");
989 return 0;
990 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200991
992 if (type == 2) {
993 int net = atoi(mask);
994
995 if ((net < 0 && net > 64) || !is_ipv6_addr(ip))
996 return -1;
997
998 if (dut->no_ip_addr_set) {
999 snprintf(buf, sizeof(buf),
1000 "sysctl net.ipv6.conf.%s.disable_ipv6=1",
1001 ifname);
1002 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
1003 if (system(buf) != 0) {
1004 sigma_dut_print(dut, DUT_MSG_DEBUG,
1005 "Failed to disable IPv6 address before association");
1006 }
1007 } else {
1008 snprintf(buf, sizeof(buf),
1009 "ip -6 addr del %s/%s dev %s",
1010 ip, mask, ifname);
1011 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
1012 if (system(buf) != 0) {
1013 /*
1014 * This command may fail if the address being
1015 * deleted does not exist. Inaction here is
1016 * intentional.
1017 */
1018 }
1019
1020 snprintf(buf, sizeof(buf),
1021 "ip -6 addr add %s/%s dev %s",
1022 ip, mask, ifname);
1023 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
1024 if (system(buf) != 0) {
1025 send_resp(dut, conn, SIGMA_ERROR,
1026 "ErrorCode,Failed to set IPv6 address");
1027 return 0;
1028 }
1029 }
1030
1031 dut->last_set_ip_config_ipv6 = 1;
1032 static_ip_file(6, ip, mask, NULL);
1033 return 1;
1034 } else if (type == 1) {
Pradeep Reddy POTTETIb18c5652016-01-18 12:45:37 +05301035 if (!is_ip_addr(ip) || !is_ip_addr(mask))
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001036 return -1;
1037 }
1038
1039 kill_dhcp_client(dut, ifname);
1040
1041 if (!dut->no_ip_addr_set) {
1042 snprintf(buf, sizeof(buf), "ifconfig %s %s netmask %s",
1043 ifname, ip, mask);
1044 if (system(buf) != 0) {
1045 send_resp(dut, conn, SIGMA_ERROR,
1046 "ErrorCode,Failed to set IP address");
1047 return 0;
1048 }
1049 }
1050
1051 gw = get_param(cmd, "defaultGateway");
1052 if (gw) {
1053 if (!is_ip_addr(gw))
1054 return -1;
1055 snprintf(buf, sizeof(buf), "route add default gw %s", gw);
1056 if (!dut->no_ip_addr_set && system(buf) != 0) {
1057 snprintf(buf, sizeof(buf), "ip ro re default via %s",
1058 gw);
1059 if (system(buf) != 0) {
1060 send_resp(dut, conn, SIGMA_ERROR,
1061 "ErrorCode,Failed "
1062 "to set default gateway");
1063 return 0;
1064 }
1065 }
1066 }
1067
1068 val = get_param(cmd, "primary-dns");
1069 if (val) {
1070 /* TODO */
1071 sigma_dut_print(dut, DUT_MSG_INFO, "Ignored primary-dns %s "
1072 "setting", val);
1073 }
1074
1075 val = get_param(cmd, "secondary-dns");
1076 if (val) {
1077 /* TODO */
1078 sigma_dut_print(dut, DUT_MSG_INFO, "Ignored secondary-dns %s "
1079 "setting", val);
1080 }
1081
1082 static_ip_file(4, ip, mask, gw);
1083
1084 return 1;
1085}
1086
1087
1088static int cmd_sta_get_info(struct sigma_dut *dut, struct sigma_conn *conn,
1089 struct sigma_cmd *cmd)
1090{
1091 /* const char *intf = get_param(cmd, "Interface"); */
1092 /* TODO: could report more details here */
1093 send_resp(dut, conn, SIGMA_COMPLETE, "vendor,Atheros");
1094 return 0;
1095}
1096
1097
1098static int cmd_sta_get_mac_address(struct sigma_dut *dut,
1099 struct sigma_conn *conn,
1100 struct sigma_cmd *cmd)
1101{
1102 /* const char *intf = get_param(cmd, "Interface"); */
1103 char addr[20], resp[50];
1104
1105 if (get_wpa_status(get_station_ifname(), "address", addr, sizeof(addr))
1106 < 0)
1107 return -2;
1108
1109 snprintf(resp, sizeof(resp), "mac,%s", addr);
1110 send_resp(dut, conn, SIGMA_COMPLETE, resp);
1111 return 0;
1112}
1113
1114
1115static int cmd_sta_is_connected(struct sigma_dut *dut, struct sigma_conn *conn,
1116 struct sigma_cmd *cmd)
1117{
1118 /* const char *intf = get_param(cmd, "Interface"); */
1119 int connected = 0;
1120 char result[32];
1121 if (get_wpa_status(get_station_ifname(), "wpa_state", result,
1122 sizeof(result)) < 0) {
1123 sigma_dut_print(dut, DUT_MSG_INFO, "Could not get interface "
1124 "%s status", get_station_ifname());
1125 return -2;
1126 }
1127
1128 sigma_dut_print(dut, DUT_MSG_DEBUG, "wpa_state=%s", result);
1129 if (strncmp(result, "COMPLETED", 9) == 0)
1130 connected = 1;
1131
1132 if (connected)
1133 send_resp(dut, conn, SIGMA_COMPLETE, "connected,1");
1134 else
1135 send_resp(dut, conn, SIGMA_COMPLETE, "connected,0");
1136
1137 return 0;
1138}
1139
1140
1141static int cmd_sta_verify_ip_connection(struct sigma_dut *dut,
1142 struct sigma_conn *conn,
1143 struct sigma_cmd *cmd)
1144{
1145 /* const char *intf = get_param(cmd, "Interface"); */
1146 const char *dst, *timeout;
1147 int wait_time = 90;
1148 char buf[100];
1149 int res;
1150
1151 dst = get_param(cmd, "destination");
1152 if (dst == NULL || !is_ip_addr(dst))
1153 return -1;
1154
1155 timeout = get_param(cmd, "timeout");
1156 if (timeout) {
1157 wait_time = atoi(timeout);
1158 if (wait_time < 1)
1159 wait_time = 1;
1160 }
1161
1162 /* TODO: force renewal of IP lease if DHCP is enabled */
1163
1164 snprintf(buf, sizeof(buf), "ping %s -c 3 -W %d", dst, wait_time);
1165 res = system(buf);
1166 sigma_dut_print(dut, DUT_MSG_DEBUG, "ping returned: %d", res);
1167 if (res == 0)
1168 send_resp(dut, conn, SIGMA_COMPLETE, "connected,1");
1169 else if (res == 256)
1170 send_resp(dut, conn, SIGMA_COMPLETE, "connected,0");
1171 else
1172 return -2;
1173
1174 return 0;
1175}
1176
1177
1178static int cmd_sta_get_bssid(struct sigma_dut *dut, struct sigma_conn *conn,
1179 struct sigma_cmd *cmd)
1180{
1181 /* const char *intf = get_param(cmd, "Interface"); */
1182 char bssid[20], resp[50];
1183
1184 if (get_wpa_status(get_station_ifname(), "bssid", bssid, sizeof(bssid))
1185 < 0)
Peng Xub8fc5cc2017-05-10 17:27:28 -07001186 strlcpy(bssid, "00:00:00:00:00:00", sizeof(bssid));
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001187
1188 snprintf(resp, sizeof(resp), "bssid,%s", bssid);
1189 send_resp(dut, conn, SIGMA_COMPLETE, resp);
1190 return 0;
1191}
1192
1193
1194#ifdef __SAMSUNG__
1195static int add_use_network(const char *ifname)
1196{
1197 char buf[100];
1198
1199 snprintf(buf, sizeof(buf), "USE_NETWORK ON");
1200 wpa_command(ifname, buf);
1201 return 0;
1202}
1203#endif /* __SAMSUNG__ */
1204
1205
1206static int add_network_common(struct sigma_dut *dut, struct sigma_conn *conn,
1207 const char *ifname, struct sigma_cmd *cmd)
1208{
1209 const char *ssid = get_param(cmd, "ssid");
1210 int id;
1211 const char *val;
1212
1213 if (ssid == NULL)
1214 return -1;
1215
1216 start_sta_mode(dut);
1217
1218#ifdef __SAMSUNG__
1219 add_use_network(ifname);
1220#endif /* __SAMSUNG__ */
1221
1222 id = add_network(ifname);
1223 if (id < 0)
1224 return -2;
1225 sigma_dut_print(dut, DUT_MSG_DEBUG, "Adding network %d", id);
1226
1227 if (set_network_quoted(ifname, id, "ssid", ssid) < 0)
1228 return -2;
1229
1230 dut->infra_network_id = id;
1231 snprintf(dut->infra_ssid, sizeof(dut->infra_ssid), "%s", ssid);
1232
1233 val = get_param(cmd, "program");
1234 if (!val)
1235 val = get_param(cmd, "prog");
1236 if (val && strcasecmp(val, "hs2") == 0) {
1237 char buf[100];
1238 snprintf(buf, sizeof(buf), "ENABLE_NETWORK %d no-connect", id);
1239 wpa_command(ifname, buf);
1240
1241 val = get_param(cmd, "prefer");
1242 if (val && atoi(val) > 0)
1243 set_network(ifname, id, "priority", "1");
1244 }
1245
1246 return id;
1247}
1248
1249
1250static int cmd_sta_set_encryption(struct sigma_dut *dut,
1251 struct sigma_conn *conn,
1252 struct sigma_cmd *cmd)
1253{
1254 const char *intf = get_param(cmd, "Interface");
1255 const char *ssid = get_param(cmd, "ssid");
1256 const char *type = get_param(cmd, "encpType");
1257 const char *ifname;
1258 char buf[200];
1259 int id;
1260
1261 if (intf == NULL || ssid == NULL)
1262 return -1;
1263
1264 if (strcmp(intf, get_main_ifname()) == 0)
1265 ifname = get_station_ifname();
1266 else
1267 ifname = intf;
1268
1269 id = add_network_common(dut, conn, ifname, cmd);
1270 if (id < 0)
1271 return id;
1272
1273 if (set_network(ifname, id, "key_mgmt", "NONE") < 0)
1274 return -2;
1275
1276 if (type && strcasecmp(type, "wep") == 0) {
1277 const char *val;
1278 int i;
1279
1280 val = get_param(cmd, "activeKey");
1281 if (val) {
1282 int keyid;
1283 keyid = atoi(val);
1284 if (keyid < 1 || keyid > 4)
1285 return -1;
1286 snprintf(buf, sizeof(buf), "%d", keyid - 1);
1287 if (set_network(ifname, id, "wep_tx_keyidx", buf) < 0)
1288 return -2;
1289 }
1290
1291 for (i = 0; i < 4; i++) {
1292 snprintf(buf, sizeof(buf), "key%d", i + 1);
1293 val = get_param(cmd, buf);
1294 if (val == NULL)
1295 continue;
1296 snprintf(buf, sizeof(buf), "wep_key%d", i);
1297 if (set_network(ifname, id, buf, val) < 0)
1298 return -2;
1299 }
1300 }
1301
1302 return 1;
1303}
1304
1305
1306static int set_wpa_common(struct sigma_dut *dut, struct sigma_conn *conn,
1307 const char *ifname, struct sigma_cmd *cmd)
1308{
1309 const char *val;
1310 int id;
Jouni Malinenad395a22017-09-01 21:13:46 +03001311 int cipher_set = 0;
Jouni Malinen47dcc952017-10-09 16:43:24 +03001312 int owe;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001313
1314 id = add_network_common(dut, conn, ifname, cmd);
1315 if (id < 0)
1316 return id;
1317
Jouni Malinen47dcc952017-10-09 16:43:24 +03001318 val = get_param(cmd, "Type");
1319 owe = val && strcasecmp(val, "OWE") == 0;
1320
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001321 val = get_param(cmd, "keyMgmtType");
Jouni Malinen47dcc952017-10-09 16:43:24 +03001322 if (!val && owe)
1323 val = "OWE";
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001324 if (val == NULL) {
1325 send_resp(dut, conn, SIGMA_INVALID, "errorCode,Missing keyMgmtType");
1326 return 0;
1327 }
1328 if (strcasecmp(val, "wpa") == 0 ||
1329 strcasecmp(val, "wpa-psk") == 0) {
1330 if (set_network(ifname, id, "proto", "WPA") < 0)
1331 return -2;
1332 } else if (strcasecmp(val, "wpa2") == 0 ||
1333 strcasecmp(val, "wpa2-psk") == 0 ||
1334 strcasecmp(val, "wpa2-ft") == 0 ||
1335 strcasecmp(val, "wpa2-sha256") == 0) {
1336 if (set_network(ifname, id, "proto", "WPA2") < 0)
1337 return -2;
Pradeep Reddy POTTETI6d04b3b2016-11-15 14:51:26 +05301338 } else if (strcasecmp(val, "wpa2-wpa-psk") == 0 ||
1339 strcasecmp(val, "wpa2-wpa-ent") == 0) {
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001340 if (set_network(ifname, id, "proto", "WPA WPA2") < 0)
1341 return -2;
Jouni Malinenad395a22017-09-01 21:13:46 +03001342 } else if (strcasecmp(val, "SuiteB") == 0) {
1343 if (set_network(ifname, id, "proto", "WPA2") < 0)
1344 return -2;
Jouni Malinen47dcc952017-10-09 16:43:24 +03001345 } else if (strcasecmp(val, "OWE") == 0) {
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001346 } else {
1347 send_resp(dut, conn, SIGMA_INVALID, "errorCode,Unrecognized keyMgmtType value");
1348 return 0;
1349 }
1350
1351 val = get_param(cmd, "encpType");
Jouni Malinenad395a22017-09-01 21:13:46 +03001352 if (val) {
1353 cipher_set = 1;
1354 if (strcasecmp(val, "tkip") == 0) {
1355 if (set_network(ifname, id, "pairwise", "TKIP") < 0)
1356 return -2;
1357 } else if (strcasecmp(val, "aes-ccmp") == 0) {
1358 if (set_network(ifname, id, "pairwise", "CCMP") < 0)
1359 return -2;
1360 } else if (strcasecmp(val, "aes-ccmp-tkip") == 0) {
1361 if (set_network(ifname, id, "pairwise",
1362 "CCMP TKIP") < 0)
1363 return -2;
1364 } else if (strcasecmp(val, "aes-gcmp") == 0) {
1365 if (set_network(ifname, id, "pairwise", "GCMP") < 0)
1366 return -2;
1367 if (set_network(ifname, id, "group", "GCMP") < 0)
1368 return -2;
1369 } else {
1370 send_resp(dut, conn, SIGMA_ERROR,
1371 "errorCode,Unrecognized encpType value");
1372 return 0;
1373 }
1374 }
1375
1376 val = get_param(cmd, "PairwiseCipher");
1377 if (val) {
1378 cipher_set = 1;
1379 /* TODO: Support space separated list */
1380 if (strcasecmp(val, "AES-GCMP-256") == 0) {
1381 if (set_network(ifname, id, "pairwise", "GCMP-256") < 0)
1382 return -2;
1383 } else if (strcasecmp(val, "AES-CCMP-256") == 0) {
1384 if (set_network(ifname, id, "pairwise",
1385 "CCMP-256") < 0)
1386 return -2;
1387 } else if (strcasecmp(val, "AES-GCMP-128") == 0) {
1388 if (set_network(ifname, id, "pairwise", "GCMP") < 0)
1389 return -2;
1390 } else if (strcasecmp(val, "AES-CCMP-128") == 0) {
1391 if (set_network(ifname, id, "pairwise", "CCMP") < 0)
1392 return -2;
1393 } else {
1394 send_resp(dut, conn, SIGMA_ERROR,
1395 "errorCode,Unrecognized PairwiseCipher value");
1396 return 0;
1397 }
1398 }
1399
Jouni Malinen47dcc952017-10-09 16:43:24 +03001400 if (!cipher_set && !owe) {
Jouni Malinenad395a22017-09-01 21:13:46 +03001401 send_resp(dut, conn, SIGMA_ERROR,
1402 "errorCode,Missing encpType and PairwiseCipher");
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001403 return 0;
1404 }
Jouni Malinenad395a22017-09-01 21:13:46 +03001405
1406 val = get_param(cmd, "GroupCipher");
1407 if (val) {
1408 if (strcasecmp(val, "AES-GCMP-256") == 0) {
1409 if (set_network(ifname, id, "group", "GCMP-256") < 0)
1410 return -2;
1411 } else if (strcasecmp(val, "AES-CCMP-256") == 0) {
1412 if (set_network(ifname, id, "group", "CCMP-256") < 0)
1413 return -2;
1414 } else if (strcasecmp(val, "AES-GCMP-128") == 0) {
1415 if (set_network(ifname, id, "group", "GCMP") < 0)
1416 return -2;
1417 } else if (strcasecmp(val, "AES-CCMP-128") == 0) {
1418 if (set_network(ifname, id, "group", "CCMP") < 0)
1419 return -2;
1420 } else {
1421 send_resp(dut, conn, SIGMA_ERROR,
1422 "errorCode,Unrecognized GroupCipher value");
1423 return 0;
1424 }
1425 }
1426
Jouni Malinen7b239522017-09-14 21:37:18 +03001427 val = get_param(cmd, "GroupMgntCipher");
Jouni Malinenad395a22017-09-01 21:13:46 +03001428 if (val) {
Jouni Malinene8898cb2017-09-26 17:55:26 +03001429 const char *cipher;
1430
1431 if (strcasecmp(val, "BIP-GMAC-256") == 0) {
1432 cipher = "BIP-GMAC-256";
1433 } else if (strcasecmp(val, "BIP-CMAC-256") == 0) {
1434 cipher = "BIP-CMAC-256";
1435 } else if (strcasecmp(val, "BIP-GMAC-128") == 0) {
1436 cipher = "BIP-GMAC-128";
1437 } else if (strcasecmp(val, "BIP-CMAC-128") == 0) {
1438 cipher = "AES-128-CMAC";
1439 } else {
1440 send_resp(dut, conn, SIGMA_INVALID,
1441 "errorCode,Unsupported GroupMgntCipher");
1442 return 0;
1443 }
1444 if (set_network(ifname, id, "group_mgmt", cipher) < 0) {
1445 send_resp(dut, conn, SIGMA_INVALID,
1446 "errorCode,Failed to set GroupMgntCipher");
1447 return 0;
1448 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001449 }
1450
1451 dut->sta_pmf = STA_PMF_DISABLED;
1452 val = get_param(cmd, "PMF");
1453 if (val) {
1454 if (strcasecmp(val, "Required") == 0 ||
1455 strcasecmp(val, "Forced_Required") == 0) {
1456 dut->sta_pmf = STA_PMF_REQUIRED;
1457 if (set_network(ifname, id, "ieee80211w", "2") < 0)
1458 return -2;
1459 } else if (strcasecmp(val, "Optional") == 0) {
1460 dut->sta_pmf = STA_PMF_OPTIONAL;
1461 if (set_network(ifname, id, "ieee80211w", "1") < 0)
1462 return -2;
1463 } else if (strcasecmp(val, "Disabled") == 0 ||
1464 strcasecmp(val, "Forced_Disabled") == 0) {
1465 dut->sta_pmf = STA_PMF_DISABLED;
1466 } else {
1467 send_resp(dut, conn, SIGMA_INVALID, "errorCode,Unrecognized PMF value");
1468 return 0;
1469 }
1470 }
1471
1472 return id;
1473}
1474
1475
1476static int cmd_sta_set_psk(struct sigma_dut *dut, struct sigma_conn *conn,
1477 struct sigma_cmd *cmd)
1478{
1479 const char *intf = get_param(cmd, "Interface");
Jouni Malinen992a81e2017-08-22 13:57:47 +03001480 const char *type = get_param(cmd, "Type");
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001481 const char *ifname, *val, *alg;
1482 int id;
1483
1484 if (intf == NULL)
1485 return -1;
1486
1487 if (strcmp(intf, get_main_ifname()) == 0)
1488 ifname = get_station_ifname();
1489 else
1490 ifname = intf;
1491
1492 id = set_wpa_common(dut, conn, ifname, cmd);
1493 if (id < 0)
1494 return id;
1495
1496 val = get_param(cmd, "keyMgmtType");
1497 alg = get_param(cmd, "micAlg");
1498
Jouni Malinen992a81e2017-08-22 13:57:47 +03001499 if (type && strcasecmp(type, "SAE") == 0) {
1500 if (val && strcasecmp(val, "wpa2-ft") == 0) {
1501 if (set_network(ifname, id, "key_mgmt", "FT-SAE") < 0)
1502 return -2;
1503 } else {
1504 if (set_network(ifname, id, "key_mgmt", "SAE") < 0)
1505 return -2;
1506 }
1507 if (wpa_command(ifname, "SET sae_groups ") != 0) {
1508 sigma_dut_print(dut, DUT_MSG_ERROR,
1509 "Failed to clear sae_groups to default");
1510 return -2;
1511 }
Jouni Malinen0ab50f42017-08-31 01:34:59 +03001512 } else if (type && strcasecmp(type, "PSK-SAE") == 0) {
1513 if (val && strcasecmp(val, "wpa2-ft") == 0) {
1514 if (set_network(ifname, id, "key_mgmt",
1515 "FT-SAE FT-PSK") < 0)
1516 return -2;
1517 } else {
1518 if (set_network(ifname, id, "key_mgmt",
1519 "SAE WPA-PSK") < 0)
1520 return -2;
1521 }
1522 if (wpa_command(ifname, "SET sae_groups ") != 0) {
1523 sigma_dut_print(dut, DUT_MSG_ERROR,
1524 "Failed to clear sae_groups to default");
1525 return -2;
1526 }
Jouni Malinen992a81e2017-08-22 13:57:47 +03001527 } else if (alg && strcasecmp(alg, "SHA-256") == 0) {
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001528 if (set_network(ifname, id, "key_mgmt", "WPA-PSK-SHA256") < 0)
1529 return -2;
1530 } else if (alg && strcasecmp(alg, "SHA-1") == 0) {
1531 if (set_network(ifname, id, "key_mgmt", "WPA-PSK") < 0)
1532 return -2;
Ashwini Patil6dbf7b02017-03-20 13:42:11 +05301533 } else if (val && strcasecmp(val, "wpa2-ft") == 0) {
1534 if (set_network(ifname, id, "key_mgmt", "FT-PSK") < 0)
1535 return -2;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001536 } else if ((val && strcasecmp(val, "wpa2-sha256") == 0) ||
1537 dut->sta_pmf == STA_PMF_REQUIRED) {
1538 if (set_network(ifname, id, "key_mgmt",
1539 "WPA-PSK WPA-PSK-SHA256") < 0)
1540 return -2;
1541 } else if (dut->sta_pmf == STA_PMF_OPTIONAL) {
1542 if (set_network(ifname, id, "key_mgmt",
1543 "WPA-PSK WPA-PSK-SHA256") < 0)
1544 return -2;
1545 } else {
1546 if (set_network(ifname, id, "key_mgmt", "WPA-PSK") < 0)
1547 return -2;
1548 }
1549
1550 val = get_param(cmd, "passPhrase");
1551 if (val == NULL)
1552 return -1;
Jouni Malinen2126f422017-10-11 23:24:33 +03001553 if (type && strcasecmp(type, "SAE") == 0) {
1554 if (set_network_quoted(ifname, id, "sae_password", val) < 0)
1555 return -2;
1556 } else {
1557 if (set_network_quoted(ifname, id, "psk", val) < 0)
1558 return -2;
1559 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001560
Jouni Malinen992a81e2017-08-22 13:57:47 +03001561 val = get_param(cmd, "ECGroupID");
1562 if (val) {
1563 char buf[50];
1564
1565 snprintf(buf, sizeof(buf), "SET sae_groups %u", atoi(val));
1566 if (wpa_command(ifname, buf) != 0) {
1567 sigma_dut_print(dut, DUT_MSG_ERROR,
1568 "Failed to clear sae_groups");
1569 return -2;
1570 }
1571 }
1572
Jouni Malinen68143132017-09-02 02:34:08 +03001573 val = get_param(cmd, "InvalidSAEElement");
1574 if (val) {
1575 free(dut->sae_commit_override);
1576 dut->sae_commit_override = strdup(val);
1577 }
1578
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001579 return 1;
1580}
1581
1582
1583static int set_eap_common(struct sigma_dut *dut, struct sigma_conn *conn,
Bala Krishna Bhamidipati73d7af02016-03-24 12:27:56 +05301584 const char *ifname, int username_identity,
1585 struct sigma_cmd *cmd)
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001586{
1587 const char *val, *alg;
1588 int id;
1589 char buf[200];
1590#ifdef ANDROID
1591 unsigned char kvalue[KEYSTORE_MESSAGE_SIZE];
1592 int length;
1593#endif /* ANDROID */
1594
1595 id = set_wpa_common(dut, conn, ifname, cmd);
1596 if (id < 0)
1597 return id;
1598
1599 val = get_param(cmd, "keyMgmtType");
1600 alg = get_param(cmd, "micAlg");
1601
Jouni Malinenad395a22017-09-01 21:13:46 +03001602 if (val && strcasecmp(val, "SuiteB") == 0) {
1603 if (set_network(ifname, id, "key_mgmt", "WPA-EAP-SUITE-B-192") <
1604 0)
1605 return -2;
1606 } else if (alg && strcasecmp(alg, "SHA-256") == 0) {
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001607 if (set_network(ifname, id, "key_mgmt", "WPA-EAP-SHA256") < 0)
1608 return -2;
1609 } else if (alg && strcasecmp(alg, "SHA-1") == 0) {
1610 if (set_network(ifname, id, "key_mgmt", "WPA-EAP") < 0)
1611 return -2;
1612 } else if (val && strcasecmp(val, "wpa2-ft") == 0) {
1613 if (set_network(ifname, id, "key_mgmt", "FT-EAP") < 0)
1614 return -2;
1615 } else if ((val && strcasecmp(val, "wpa2-sha256") == 0) ||
1616 dut->sta_pmf == STA_PMF_REQUIRED) {
1617 if (set_network(ifname, id, "key_mgmt",
1618 "WPA-EAP WPA-EAP-SHA256") < 0)
1619 return -2;
1620 } else if (dut->sta_pmf == STA_PMF_OPTIONAL) {
1621 if (set_network(ifname, id, "key_mgmt",
1622 "WPA-EAP WPA-EAP-SHA256") < 0)
1623 return -2;
1624 } else {
1625 if (set_network(ifname, id, "key_mgmt", "WPA-EAP") < 0)
1626 return -2;
1627 }
1628
1629 val = get_param(cmd, "trustedRootCA");
1630 if (val) {
1631#ifdef ANDROID
1632 snprintf(buf, sizeof(buf), "CACERT_%s", val);
1633 length = android_keystore_get(ANDROID_KEYSTORE_GET, buf,
1634 kvalue);
1635 if (length > 0) {
1636 sigma_dut_print(dut, DUT_MSG_INFO,
1637 "Use Android keystore [%s]", buf);
1638 snprintf(buf, sizeof(buf), "keystore://CACERT_%s",
1639 val);
1640 goto ca_cert_selected;
1641 }
1642#endif /* ANDROID */
1643
1644 snprintf(buf, sizeof(buf), "%s/%s", sigma_cert_path, val);
1645#ifdef __linux__
1646 if (!file_exists(buf)) {
1647 char msg[300];
1648 snprintf(msg, sizeof(msg), "ErrorCode,trustedRootCA "
1649 "file (%s) not found", buf);
1650 send_resp(dut, conn, SIGMA_ERROR, msg);
1651 return -3;
1652 }
1653#endif /* __linux__ */
1654#ifdef ANDROID
1655ca_cert_selected:
1656#endif /* ANDROID */
1657 if (set_network_quoted(ifname, id, "ca_cert", buf) < 0)
1658 return -2;
1659 }
1660
Bala Krishna Bhamidipati73d7af02016-03-24 12:27:56 +05301661 if (username_identity) {
1662 val = get_param(cmd, "username");
1663 if (val) {
1664 if (set_network_quoted(ifname, id, "identity", val) < 0)
1665 return -2;
1666 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001667
Bala Krishna Bhamidipati73d7af02016-03-24 12:27:56 +05301668 val = get_param(cmd, "password");
1669 if (val) {
1670 if (set_network_quoted(ifname, id, "password", val) < 0)
1671 return -2;
1672 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001673 }
1674
1675 return id;
1676}
1677
1678
Jouni Malinen5eabb2a2017-10-03 18:17:30 +03001679static int set_tls_cipher(const char *ifname, int id, const char *cipher)
1680{
1681 const char *val;
1682
1683 if (!cipher)
1684 return 0;
1685
1686 if (strcasecmp(cipher, "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384") == 0)
1687 val = "ECDHE-ECDSA-AES256-GCM-SHA384";
1688 else if (strcasecmp(cipher,
1689 "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384") == 0)
1690 val = "ECDHE-RSA-AES256-GCM-SHA384";
1691 else if (strcasecmp(cipher, "TLS_DHE_RSA_WITH_AES_256_GCM_SHA384") == 0)
1692 val = "DHE-RSA-AES256-GCM-SHA384";
1693 else if (strcasecmp(cipher,
1694 "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256") == 0)
1695 val = "ECDHE-ECDSA-AES128-GCM-SHA256";
1696 else
1697 return -1;
1698
1699 /* Need to clear phase1="tls_suiteb=1" to allow cipher enforcement */
1700 set_network_quoted(ifname, id, "phase1", "");
1701
1702 return set_network_quoted(ifname, id, "openssl_ciphers", val);
1703}
1704
1705
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001706static int cmd_sta_set_eaptls(struct sigma_dut *dut, struct sigma_conn *conn,
1707 struct sigma_cmd *cmd)
1708{
1709 const char *intf = get_param(cmd, "Interface");
1710 const char *ifname, *val;
1711 int id;
1712 char buf[200];
1713#ifdef ANDROID
1714 unsigned char kvalue[KEYSTORE_MESSAGE_SIZE];
1715 int length;
1716 int jb_or_newer = 0;
1717 char prop[PROPERTY_VALUE_MAX];
1718#endif /* ANDROID */
1719
1720 if (intf == NULL)
1721 return -1;
1722
1723 if (strcmp(intf, get_main_ifname()) == 0)
1724 ifname = get_station_ifname();
1725 else
1726 ifname = intf;
1727
Bala Krishna Bhamidipati73d7af02016-03-24 12:27:56 +05301728 id = set_eap_common(dut, conn, ifname, 1, cmd);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001729 if (id < 0)
1730 return id;
1731
1732 if (set_network(ifname, id, "eap", "TLS") < 0)
1733 return -2;
1734
Pradeep Reddy POTTETI9f6c2132016-05-05 16:28:19 +05301735 if (!get_param(cmd, "username") &&
1736 set_network_quoted(ifname, id, "identity",
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001737 "wifi-user@wifilabs.local") < 0)
1738 return -2;
1739
1740 val = get_param(cmd, "clientCertificate");
1741 if (val == NULL)
1742 return -1;
1743#ifdef ANDROID
1744 snprintf(buf, sizeof(buf), "USRPKEY_%s", val);
1745 length = android_keystore_get(ANDROID_KEYSTORE_GET, buf, kvalue);
1746 if (length < 0) {
1747 /*
1748 * JB started reporting keystore type mismatches, so retry with
1749 * the GET_PUBKEY command if the generic GET fails.
1750 */
1751 length = android_keystore_get(ANDROID_KEYSTORE_GET_PUBKEY,
1752 buf, kvalue);
1753 }
1754
1755 if (property_get("ro.build.version.release", prop, NULL) != 0) {
1756 sigma_dut_print(dut, DUT_MSG_DEBUG, "Android release %s", prop);
1757 if (strncmp(prop, "4.0", 3) != 0)
1758 jb_or_newer = 1;
1759 } else
1760 jb_or_newer = 1; /* assume newer */
1761
1762 if (jb_or_newer && length > 0) {
1763 sigma_dut_print(dut, DUT_MSG_INFO,
1764 "Use Android keystore [%s]", buf);
1765 if (set_network(ifname, id, "engine", "1") < 0)
1766 return -2;
1767 if (set_network_quoted(ifname, id, "engine_id", "keystore") < 0)
1768 return -2;
1769 snprintf(buf, sizeof(buf), "USRPKEY_%s", val);
1770 if (set_network_quoted(ifname, id, "key_id", buf) < 0)
1771 return -2;
1772 snprintf(buf, sizeof(buf), "keystore://USRCERT_%s", val);
1773 if (set_network_quoted(ifname, id, "client_cert", buf) < 0)
1774 return -2;
1775 return 1;
1776 } else if (length > 0) {
1777 sigma_dut_print(dut, DUT_MSG_INFO,
1778 "Use Android keystore [%s]", buf);
1779 snprintf(buf, sizeof(buf), "keystore://USRPKEY_%s", val);
1780 if (set_network_quoted(ifname, id, "private_key", buf) < 0)
1781 return -2;
1782 snprintf(buf, sizeof(buf), "keystore://USRCERT_%s", val);
1783 if (set_network_quoted(ifname, id, "client_cert", buf) < 0)
1784 return -2;
1785 return 1;
1786 }
1787#endif /* ANDROID */
1788
1789 snprintf(buf, sizeof(buf), "%s/%s", sigma_cert_path, val);
1790#ifdef __linux__
1791 if (!file_exists(buf)) {
1792 char msg[300];
1793 snprintf(msg, sizeof(msg), "ErrorCode,clientCertificate file "
1794 "(%s) not found", buf);
1795 send_resp(dut, conn, SIGMA_ERROR, msg);
1796 return -3;
1797 }
1798#endif /* __linux__ */
1799 if (set_network_quoted(ifname, id, "private_key", buf) < 0)
1800 return -2;
1801 if (set_network_quoted(ifname, id, "client_cert", buf) < 0)
1802 return -2;
1803
1804 if (set_network_quoted(ifname, id, "private_key_passwd", "wifi") < 0)
1805 return -2;
1806
Jouni Malinen5eabb2a2017-10-03 18:17:30 +03001807 val = get_param(cmd, "keyMgmtType");
1808 if (val && strcasecmp(val, "SuiteB") == 0) {
1809 val = get_param(cmd, "CertType");
1810 if (val && strcasecmp(val, "RSA") == 0) {
1811 if (set_network_quoted(ifname, id, "phase1",
1812 "tls_suiteb=1") < 0)
1813 return -2;
1814 } else {
1815 if (set_network_quoted(ifname, id, "openssl_ciphers",
1816 "SUITEB192") < 0)
1817 return -2;
1818 }
1819
1820 val = get_param(cmd, "TLSCipher");
1821 if (set_tls_cipher(ifname, id, val) < 0) {
1822 send_resp(dut, conn, SIGMA_ERROR,
1823 "ErrorCode,Unsupported TLSCipher value");
1824 return -3;
1825 }
1826 }
1827
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001828 return 1;
1829}
1830
1831
1832static int cmd_sta_set_eapttls(struct sigma_dut *dut, struct sigma_conn *conn,
1833 struct sigma_cmd *cmd)
1834{
1835 const char *intf = get_param(cmd, "Interface");
1836 const char *ifname;
1837 int id;
1838
1839 if (intf == NULL)
1840 return -1;
1841
1842 if (strcmp(intf, get_main_ifname()) == 0)
1843 ifname = get_station_ifname();
1844 else
1845 ifname = intf;
1846
Bala Krishna Bhamidipati73d7af02016-03-24 12:27:56 +05301847 id = set_eap_common(dut, conn, ifname, 1, cmd);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001848 if (id < 0)
1849 return id;
1850
1851 if (set_network(ifname, id, "eap", "TTLS") < 0) {
1852 send_resp(dut, conn, SIGMA_ERROR,
1853 "errorCode,Failed to set TTLS method");
1854 return 0;
1855 }
1856
1857 if (set_network_quoted(ifname, id, "phase2", "auth=MSCHAPV2") < 0) {
1858 send_resp(dut, conn, SIGMA_ERROR,
1859 "errorCode,Failed to set MSCHAPv2 for TTLS Phase 2");
1860 return 0;
1861 }
1862
1863 return 1;
1864}
1865
1866
1867static int cmd_sta_set_eapsim(struct sigma_dut *dut, struct sigma_conn *conn,
1868 struct sigma_cmd *cmd)
1869{
1870 const char *intf = get_param(cmd, "Interface");
1871 const char *ifname;
1872 int id;
1873
1874 if (intf == NULL)
1875 return -1;
1876
1877 if (strcmp(intf, get_main_ifname()) == 0)
1878 ifname = get_station_ifname();
1879 else
1880 ifname = intf;
1881
Bala Krishna Bhamidipati73d7af02016-03-24 12:27:56 +05301882 id = set_eap_common(dut, conn, ifname, !dut->sim_no_username, cmd);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001883 if (id < 0)
1884 return id;
1885
1886 if (set_network(ifname, id, "eap", "SIM") < 0)
1887 return -2;
1888
1889 return 1;
1890}
1891
1892
1893static int cmd_sta_set_peap(struct sigma_dut *dut, struct sigma_conn *conn,
1894 struct sigma_cmd *cmd)
1895{
1896 const char *intf = get_param(cmd, "Interface");
1897 const char *ifname, *val;
1898 int id;
1899 char buf[100];
1900
1901 if (intf == NULL)
1902 return -1;
1903
1904 if (strcmp(intf, get_main_ifname()) == 0)
1905 ifname = get_station_ifname();
1906 else
1907 ifname = intf;
1908
Bala Krishna Bhamidipati73d7af02016-03-24 12:27:56 +05301909 id = set_eap_common(dut, conn, ifname, 1, cmd);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001910 if (id < 0)
1911 return id;
1912
1913 if (set_network(ifname, id, "eap", "PEAP") < 0)
1914 return -2;
1915
1916 val = get_param(cmd, "innerEAP");
1917 if (val) {
1918 if (strcasecmp(val, "MSCHAPv2") == 0) {
1919 if (set_network_quoted(ifname, id, "phase2",
1920 "auth=MSCHAPV2") < 0)
1921 return -2;
1922 } else if (strcasecmp(val, "GTC") == 0) {
1923 if (set_network_quoted(ifname, id, "phase2",
1924 "auth=GTC") < 0)
1925 return -2;
1926 } else
1927 return -1;
1928 }
1929
1930 val = get_param(cmd, "peapVersion");
1931 if (val) {
1932 int ver = atoi(val);
1933 if (ver < 0 || ver > 1)
1934 return -1;
1935 snprintf(buf, sizeof(buf), "peapver=%d", ver);
1936 if (set_network_quoted(ifname, id, "phase1", buf) < 0)
1937 return -2;
1938 }
1939
1940 return 1;
1941}
1942
1943
1944static int cmd_sta_set_eapfast(struct sigma_dut *dut, struct sigma_conn *conn,
1945 struct sigma_cmd *cmd)
1946{
1947 const char *intf = get_param(cmd, "Interface");
1948 const char *ifname, *val;
1949 int id;
1950 char buf[100];
1951
1952 if (intf == NULL)
1953 return -1;
1954
1955 if (strcmp(intf, get_main_ifname()) == 0)
1956 ifname = get_station_ifname();
1957 else
1958 ifname = intf;
1959
Bala Krishna Bhamidipati73d7af02016-03-24 12:27:56 +05301960 id = set_eap_common(dut, conn, ifname, 1, cmd);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001961 if (id < 0)
1962 return id;
1963
1964 if (set_network(ifname, id, "eap", "FAST") < 0)
1965 return -2;
1966
1967 val = get_param(cmd, "innerEAP");
1968 if (val) {
1969 if (strcasecmp(val, "MSCHAPV2") == 0) {
1970 if (set_network_quoted(ifname, id, "phase2",
1971 "auth=MSCHAPV2") < 0)
1972 return -2;
1973 } else if (strcasecmp(val, "GTC") == 0) {
1974 if (set_network_quoted(ifname, id, "phase2",
1975 "auth=GTC") < 0)
1976 return -2;
1977 } else
1978 return -1;
1979 }
1980
1981 val = get_param(cmd, "validateServer");
1982 if (val) {
1983 /* TODO */
1984 sigma_dut_print(dut, DUT_MSG_INFO, "Ignored EAP-FAST "
1985 "validateServer=%s", val);
1986 }
1987
1988 val = get_param(cmd, "pacFile");
1989 if (val) {
1990 snprintf(buf, sizeof(buf), "blob://%s", val);
1991 if (set_network_quoted(ifname, id, "pac_file", buf) < 0)
1992 return -2;
1993 }
1994
1995 if (set_network_quoted(ifname, id, "phase1", "fast_provisioning=2") <
1996 0)
1997 return -2;
1998
1999 return 1;
2000}
2001
2002
2003static int cmd_sta_set_eapaka(struct sigma_dut *dut, struct sigma_conn *conn,
2004 struct sigma_cmd *cmd)
2005{
2006 const char *intf = get_param(cmd, "Interface");
2007 const char *ifname;
2008 int id;
2009
2010 if (intf == NULL)
2011 return -1;
2012
2013 if (strcmp(intf, get_main_ifname()) == 0)
2014 ifname = get_station_ifname();
2015 else
2016 ifname = intf;
2017
Bala Krishna Bhamidipati73d7af02016-03-24 12:27:56 +05302018 id = set_eap_common(dut, conn, ifname, !dut->sim_no_username, cmd);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02002019 if (id < 0)
2020 return id;
2021
2022 if (set_network(ifname, id, "eap", "AKA") < 0)
2023 return -2;
2024
2025 return 1;
2026}
2027
2028
2029static int cmd_sta_set_eapakaprime(struct sigma_dut *dut,
2030 struct sigma_conn *conn,
2031 struct sigma_cmd *cmd)
2032{
2033 const char *intf = get_param(cmd, "Interface");
2034 const char *ifname;
2035 int id;
2036
2037 if (intf == NULL)
2038 return -1;
2039
2040 if (strcmp(intf, get_main_ifname()) == 0)
2041 ifname = get_station_ifname();
2042 else
2043 ifname = intf;
2044
Bala Krishna Bhamidipati73d7af02016-03-24 12:27:56 +05302045 id = set_eap_common(dut, conn, ifname, !dut->sim_no_username, cmd);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02002046 if (id < 0)
2047 return id;
2048
2049 if (set_network(ifname, id, "eap", "AKA'") < 0)
2050 return -2;
2051
2052 return 1;
2053}
2054
2055
2056static int sta_set_open(struct sigma_dut *dut, struct sigma_conn *conn,
2057 struct sigma_cmd *cmd)
2058{
2059 const char *intf = get_param(cmd, "Interface");
2060 const char *ifname;
2061 int id;
2062
2063 if (strcmp(intf, get_main_ifname()) == 0)
2064 ifname = get_station_ifname();
2065 else
2066 ifname = intf;
2067
2068 id = add_network_common(dut, conn, ifname, cmd);
2069 if (id < 0)
2070 return id;
2071
2072 if (set_network(ifname, id, "key_mgmt", "NONE") < 0)
2073 return -2;
2074
2075 return 1;
2076}
2077
2078
Jouni Malinen47dcc952017-10-09 16:43:24 +03002079static int sta_set_owe(struct sigma_dut *dut, struct sigma_conn *conn,
2080 struct sigma_cmd *cmd)
2081{
2082 const char *intf = get_param(cmd, "Interface");
2083 const char *ifname, *val;
2084 int id;
2085
2086 if (intf == NULL)
2087 return -1;
2088
2089 if (strcmp(intf, get_main_ifname()) == 0)
2090 ifname = get_station_ifname();
2091 else
2092 ifname = intf;
2093
2094 id = set_wpa_common(dut, conn, ifname, cmd);
2095 if (id < 0)
2096 return id;
2097
2098 if (set_network(ifname, id, "key_mgmt", "OWE") < 0)
2099 return -2;
2100
2101 val = get_param(cmd, "ECGroupID");
Jouni Malinenfac9cad2017-10-10 18:35:55 +03002102 if (val && strcmp(val, "0") == 0) {
2103 if (wpa_command(ifname,
2104 "VENDOR_ELEM_ADD 13 ff23200000783590fb7440e03d5b3b33911f86affdcc6b4411b707846ac4ff08ddc8831ccd") != 0) {
2105 sigma_dut_print(dut, DUT_MSG_ERROR,
2106 "Failed to set OWE DH Param element override");
2107 return -2;
2108 }
2109 } else if (val && set_network(ifname, id, "owe_group", val) < 0) {
Jouni Malinen47dcc952017-10-09 16:43:24 +03002110 sigma_dut_print(dut, DUT_MSG_ERROR,
2111 "Failed to clear owe_group");
2112 return -2;
2113 }
2114
2115 return 1;
2116}
2117
2118
Jouni Malinencd4e3c32015-10-29 12:39:56 +02002119static int cmd_sta_set_security(struct sigma_dut *dut, struct sigma_conn *conn,
2120 struct sigma_cmd *cmd)
2121{
2122 const char *type = get_param(cmd, "Type");
2123
2124 if (type == NULL) {
2125 send_resp(dut, conn, SIGMA_ERROR,
2126 "ErrorCode,Missing Type argument");
2127 return 0;
2128 }
2129
2130 if (strcasecmp(type, "OPEN") == 0)
2131 return sta_set_open(dut, conn, cmd);
Jouni Malinen47dcc952017-10-09 16:43:24 +03002132 if (strcasecmp(type, "OWE") == 0)
2133 return sta_set_owe(dut, conn, cmd);
Jouni Malinen992a81e2017-08-22 13:57:47 +03002134 if (strcasecmp(type, "PSK") == 0 ||
Jouni Malinen0ab50f42017-08-31 01:34:59 +03002135 strcasecmp(type, "PSK-SAE") == 0 ||
Jouni Malinen992a81e2017-08-22 13:57:47 +03002136 strcasecmp(type, "SAE") == 0)
Jouni Malinencd4e3c32015-10-29 12:39:56 +02002137 return cmd_sta_set_psk(dut, conn, cmd);
2138 if (strcasecmp(type, "EAPTLS") == 0)
2139 return cmd_sta_set_eaptls(dut, conn, cmd);
2140 if (strcasecmp(type, "EAPTTLS") == 0)
2141 return cmd_sta_set_eapttls(dut, conn, cmd);
2142 if (strcasecmp(type, "EAPPEAP") == 0)
2143 return cmd_sta_set_peap(dut, conn, cmd);
2144 if (strcasecmp(type, "EAPSIM") == 0)
2145 return cmd_sta_set_eapsim(dut, conn, cmd);
2146 if (strcasecmp(type, "EAPFAST") == 0)
2147 return cmd_sta_set_eapfast(dut, conn, cmd);
2148 if (strcasecmp(type, "EAPAKA") == 0)
2149 return cmd_sta_set_eapaka(dut, conn, cmd);
2150 if (strcasecmp(type, "EAPAKAPRIME") == 0)
2151 return cmd_sta_set_eapakaprime(dut, conn, cmd);
2152
2153 send_resp(dut, conn, SIGMA_ERROR,
2154 "ErrorCode,Unsupported Type value");
2155 return 0;
2156}
2157
2158
2159int ath6kl_client_uapsd(struct sigma_dut *dut, const char *intf, int uapsd)
2160{
2161#ifdef __linux__
2162 /* special handling for ath6kl */
2163 char path[128], fname[128], *pos;
2164 ssize_t res;
2165 FILE *f;
2166
2167 snprintf(path, sizeof(path), "/sys/class/net/%s/phy80211", intf);
2168 res = readlink(path, path, sizeof(path));
2169 if (res < 0)
2170 return 0; /* not ath6kl */
2171
2172 if (res >= (int) sizeof(path))
2173 res = sizeof(path) - 1;
2174 path[res] = '\0';
2175 pos = strrchr(path, '/');
2176 if (pos == NULL)
2177 pos = path;
2178 else
2179 pos++;
2180 snprintf(fname, sizeof(fname),
2181 "/sys/kernel/debug/ieee80211/%s/ath6kl/"
2182 "create_qos", pos);
2183 if (!file_exists(fname))
2184 return 0; /* not ath6kl */
2185
2186 if (uapsd) {
2187 f = fopen(fname, "w");
2188 if (f == NULL)
2189 return -1;
2190
2191 sigma_dut_print(dut, DUT_MSG_DEBUG, "Use ath6kl create_qos");
2192 fprintf(f, "4 2 2 1 2 9999999 9999999 9999999 7777777 0 4 "
2193 "45000 200 56789000 56789000 5678900 0 0 9999999 "
2194 "20000 0\n");
2195 fclose(f);
2196 } else {
2197 snprintf(fname, sizeof(fname),
2198 "/sys/kernel/debug/ieee80211/%s/ath6kl/"
2199 "delete_qos", pos);
2200
2201 f = fopen(fname, "w");
2202 if (f == NULL)
2203 return -1;
2204
2205 sigma_dut_print(dut, DUT_MSG_DEBUG, "Use ath6kl delete_qos");
2206 fprintf(f, "2 4\n");
2207 fclose(f);
2208 }
2209#endif /* __linux__ */
2210
2211 return 0;
2212}
2213
2214
2215static int cmd_sta_set_uapsd(struct sigma_dut *dut, struct sigma_conn *conn,
2216 struct sigma_cmd *cmd)
2217{
2218 const char *intf = get_param(cmd, "Interface");
2219 /* const char *ssid = get_param(cmd, "ssid"); */
2220 const char *val;
2221 int max_sp_len = 4;
2222 int ac_be = 1, ac_bk = 1, ac_vi = 1, ac_vo = 1;
2223 char buf[100];
2224 int ret1, ret2;
2225
2226 val = get_param(cmd, "maxSPLength");
2227 if (val) {
2228 max_sp_len = atoi(val);
2229 if (max_sp_len != 0 && max_sp_len != 1 && max_sp_len != 2 &&
2230 max_sp_len != 4)
2231 return -1;
2232 }
2233
2234 val = get_param(cmd, "acBE");
2235 if (val)
2236 ac_be = atoi(val);
2237
2238 val = get_param(cmd, "acBK");
2239 if (val)
2240 ac_bk = atoi(val);
2241
2242 val = get_param(cmd, "acVI");
2243 if (val)
2244 ac_vi = atoi(val);
2245
2246 val = get_param(cmd, "acVO");
2247 if (val)
2248 ac_vo = atoi(val);
2249
2250 dut->client_uapsd = ac_be || ac_bk || ac_vi || ac_vo;
2251
2252 snprintf(buf, sizeof(buf), "P2P_SET client_apsd %d,%d,%d,%d;%d",
2253 ac_be, ac_bk, ac_vi, ac_vo, max_sp_len);
2254 ret1 = wpa_command(intf, buf);
2255
2256 snprintf(buf, sizeof(buf), "SET uapsd %d,%d,%d,%d;%d",
2257 ac_be, ac_bk, ac_vi, ac_vo, max_sp_len);
2258 ret2 = wpa_command(intf, buf);
2259
2260 if (ret1 && ret2) {
2261 sigma_dut_print(dut, DUT_MSG_INFO, "Failed to set client mode "
2262 "UAPSD parameters.");
2263 return -2;
2264 }
2265
2266 if (ath6kl_client_uapsd(dut, intf, dut->client_uapsd) < 0) {
2267 send_resp(dut, conn, SIGMA_ERROR,
2268 "ErrorCode,Failed to set ath6kl QoS parameters");
2269 return 0;
2270 }
2271
2272 return 1;
2273}
2274
2275
2276static int cmd_sta_set_wmm(struct sigma_dut *dut, struct sigma_conn *conn,
2277 struct sigma_cmd *cmd)
2278{
2279 char buf[1000];
2280 const char *intf = get_param(cmd, "Interface");
2281 const char *grp = get_param(cmd, "Group");
2282 const char *act = get_param(cmd, "Action");
2283 const char *tid = get_param(cmd, "Tid");
2284 const char *dir = get_param(cmd, "Direction");
2285 const char *psb = get_param(cmd, "Psb");
2286 const char *up = get_param(cmd, "Up");
2287 const char *fixed = get_param(cmd, "Fixed");
2288 const char *size = get_param(cmd, "Size");
2289 const char *msize = get_param(cmd, "Maxsize");
2290 const char *minsi = get_param(cmd, "Min_srvc_intrvl");
2291 const char *maxsi = get_param(cmd, "Max_srvc_intrvl");
2292 const char *inact = get_param(cmd, "Inactivity");
2293 const char *sus = get_param(cmd, "Suspension");
2294 const char *mindr = get_param(cmd, "Mindatarate");
2295 const char *meandr = get_param(cmd, "Meandatarate");
2296 const char *peakdr = get_param(cmd, "Peakdatarate");
2297 const char *phyrate = get_param(cmd, "Phyrate");
2298 const char *burstsize = get_param(cmd, "Burstsize");
2299 const char *sba = get_param(cmd, "Sba");
2300 int direction;
2301 int handle;
Peng Xu93319622017-10-04 17:58:16 -07002302 float sba_fv = 0;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02002303 int fixed_int;
2304 int psb_ts;
2305
2306 if (intf == NULL || grp == NULL || act == NULL )
2307 return -1;
2308
2309 if (strcasecmp(act, "addts") == 0) {
2310 if (tid == NULL || dir == NULL || psb == NULL ||
2311 up == NULL || fixed == NULL || size == NULL)
2312 return -1;
2313
2314 /*
2315 * Note: Sigma CAPI spec lists uplink, downlink, and bidi as the
2316 * possible values, but WMM-AC and V-E test scripts use "UP,
2317 * "DOWN", and "BIDI".
2318 */
2319 if (strcasecmp(dir, "uplink") == 0 ||
2320 strcasecmp(dir, "up") == 0) {
2321 direction = 0;
2322 } else if (strcasecmp(dir, "downlink") == 0 ||
2323 strcasecmp(dir, "down") == 0) {
2324 direction = 1;
2325 } else if (strcasecmp(dir, "bidi") == 0) {
2326 direction = 2;
2327 } else {
2328 sigma_dut_print(dut, DUT_MSG_ERROR,
2329 "Direction %s not supported", dir);
2330 return -1;
2331 }
2332
2333 if (strcasecmp(psb, "legacy") == 0) {
2334 psb_ts = 0;
2335 } else if (strcasecmp(psb, "uapsd") == 0) {
2336 psb_ts = 1;
2337 } else {
2338 sigma_dut_print(dut, DUT_MSG_ERROR,
2339 "PSB %s not supported", psb);
2340 return -1;
2341 }
2342
2343 if (atoi(tid) < 0 || atoi(tid) > 7) {
2344 sigma_dut_print(dut, DUT_MSG_ERROR,
2345 "TID %s not supported", tid);
2346 return -1;
2347 }
2348
2349 if (strcasecmp(fixed, "true") == 0) {
2350 fixed_int = 1;
2351 } else {
2352 fixed_int = 0;
2353 }
2354
Peng Xu93319622017-10-04 17:58:16 -07002355 if (sba)
2356 sba_fv = atof(sba);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02002357
2358 dut->dialog_token++;
2359 handle = 7000 + dut->dialog_token;
2360
2361 /*
2362 * size: convert to hex
2363 * maxsi: convert to hex
2364 * mindr: convert to hex
2365 * meandr: convert to hex
2366 * peakdr: convert to hex
2367 * burstsize: convert to hex
2368 * phyrate: convert to hex
2369 * sba: convert to hex with modification
2370 * minsi: convert to integer
2371 * sus: convert to integer
2372 * inact: convert to integer
2373 * maxsi: convert to integer
2374 */
2375
2376 /*
2377 * The Nominal MSDU Size field is 2 octets long and contains an
2378 * unsigned integer that specifies the nominal size, in octets,
2379 * of MSDUs belonging to the traffic under this traffic
2380 * specification and is defined in Figure 16. If the Fixed
2381 * subfield is set to 1, then the size of the MSDU is fixed and
2382 * is indicated by the Size Subfield. If the Fixed subfield is
2383 * set to 0, then the size of the MSDU might not be fixed and
2384 * the Size indicates the nominal MSDU size.
2385 *
2386 * The Surplus Bandwidth Allowance Factor field is 2 octets long
2387 * and specifies the excess allocation of time (and bandwidth)
2388 * over and above the stated rates required to transport an MSDU
2389 * belonging to the traffic in this TSPEC. This field is
2390 * represented as an unsigned binary number with an implicit
2391 * binary point after the leftmost 3 bits. For example, an SBA
2392 * of 1.75 is represented as 0x3800. This field is included to
2393 * account for retransmissions. As such, the value of this field
2394 * must be greater than unity.
2395 */
2396
2397 snprintf(buf, sizeof(buf),
2398 "iwpriv %s addTspec %d %s %d %d %s 0x%X"
2399 " 0x%X 0x%X 0x%X"
2400 " 0x%X 0x%X 0x%X"
2401 " 0x%X %d %d %d %d"
2402 " %d %d",
2403 intf, handle, tid, direction, psb_ts, up,
2404 (unsigned int) ((fixed_int << 15) | atoi(size)),
2405 msize ? atoi(msize) : 0,
2406 mindr ? atoi(mindr) : 0,
2407 meandr ? atoi(meandr) : 0,
2408 peakdr ? atoi(peakdr) : 0,
2409 burstsize ? atoi(burstsize) : 0,
2410 phyrate ? atoi(phyrate) : 0,
2411 sba ? ((unsigned int) (((int) sba_fv << 13) |
2412 (int)((sba_fv - (int) sba_fv) *
2413 8192))) : 0,
2414 minsi ? atoi(minsi) : 0,
2415 sus ? atoi(sus) : 0,
2416 0, 0,
2417 inact ? atoi(inact) : 0,
2418 maxsi ? atoi(maxsi) : 0);
2419
2420 if (system(buf) != 0) {
2421 sigma_dut_print(dut, DUT_MSG_ERROR,
2422 "iwpriv addtspec request failed");
2423 send_resp(dut, conn, SIGMA_ERROR,
2424 "errorCode,Failed to execute addTspec command");
2425 return 0;
2426 }
2427
2428 sigma_dut_print(dut, DUT_MSG_INFO,
2429 "iwpriv addtspec request send");
2430
2431 /* Mapping handle to a TID */
2432 dut->tid_to_handle[atoi(tid)] = handle;
2433 } else if (strcasecmp(act, "delts") == 0) {
2434 if (tid == NULL)
2435 return -1;
2436
2437 if (atoi(tid) < 0 || atoi(tid) > 7) {
2438 sigma_dut_print(dut, DUT_MSG_ERROR,
2439 "TID %s not supported", tid);
2440 send_resp(dut, conn, SIGMA_ERROR,
2441 "errorCode,Unsupported TID");
2442 return 0;
2443 }
2444
2445 handle = dut->tid_to_handle[atoi(tid)];
2446
2447 if (handle < 7000 || handle > 7255) {
2448 /* Invalid handle ie no mapping for that TID */
2449 sigma_dut_print(dut, DUT_MSG_ERROR,
2450 "handle-> %d not found", handle);
2451 }
2452
2453 snprintf(buf, sizeof(buf), "iwpriv %s delTspec %d",
2454 intf, handle);
2455
2456 if (system(buf) != 0) {
2457 sigma_dut_print(dut, DUT_MSG_ERROR,
2458 "iwpriv deltspec request failed");
2459 send_resp(dut, conn, SIGMA_ERROR,
2460 "errorCode,Failed to execute delTspec command");
2461 return 0;
2462 }
2463
2464 sigma_dut_print(dut, DUT_MSG_INFO,
2465 "iwpriv deltspec request send");
2466
2467 dut->tid_to_handle[atoi(tid)] = 0;
2468 } else {
2469 sigma_dut_print(dut, DUT_MSG_ERROR,
2470 "Action type %s not supported", act);
2471 send_resp(dut, conn, SIGMA_ERROR,
2472 "errorCode,Unsupported Action");
2473 return 0;
2474 }
2475
2476 return 1;
2477}
2478
2479
vamsi krishna52e16f92017-08-29 12:37:34 +05302480static int find_network(struct sigma_dut *dut, const char *ssid)
2481{
2482 char list[4096];
2483 char *pos;
2484
2485 sigma_dut_print(dut, DUT_MSG_DEBUG,
2486 "Search for profile based on SSID: '%s'", ssid);
2487 if (wpa_command_resp(get_station_ifname(), "LIST_NETWORKS",
2488 list, sizeof(list)) < 0)
2489 return -1;
2490 pos = strstr(list, ssid);
2491 if (!pos || pos == list || pos[-1] != '\t' || pos[strlen(ssid)] != '\t')
2492 return -1;
2493
2494 while (pos > list && pos[-1] != '\n')
2495 pos--;
2496 dut->infra_network_id = atoi(pos);
2497 snprintf(dut->infra_ssid, sizeof(dut->infra_ssid), "%s", ssid);
2498 return 0;
2499}
2500
2501
Jouni Malinencd4e3c32015-10-29 12:39:56 +02002502static int cmd_sta_associate(struct sigma_dut *dut, struct sigma_conn *conn,
2503 struct sigma_cmd *cmd)
2504{
2505 /* const char *intf = get_param(cmd, "Interface"); */
2506 const char *ssid = get_param(cmd, "ssid");
2507 const char *wps_param = get_param(cmd, "WPS");
2508 const char *bssid = get_param(cmd, "bssid");
Jouni Malinen46a19b62017-06-23 14:31:27 +03002509 const char *chan = get_param(cmd, "channel");
Jouni Malinencd4e3c32015-10-29 12:39:56 +02002510 int wps = 0;
Jouni Malinen3c367e82017-06-23 17:01:47 +03002511 char buf[1000], extra[50];
Jouni Malinencd4e3c32015-10-29 12:39:56 +02002512
2513 if (ssid == NULL)
2514 return -1;
2515
Jouni Malinen3c367e82017-06-23 17:01:47 +03002516 if (dut->rsne_override) {
2517 snprintf(buf, sizeof(buf), "TEST_ASSOC_IE %s",
2518 dut->rsne_override);
2519 if (wpa_command(get_station_ifname(), buf) < 0) {
2520 send_resp(dut, conn, SIGMA_ERROR,
2521 "ErrorCode,Failed to set DEV_CONFIGURE_IE RSNE override");
2522 return 0;
2523 }
2524 }
2525
Jouni Malinen68143132017-09-02 02:34:08 +03002526 if (dut->sae_commit_override) {
2527 snprintf(buf, sizeof(buf), "SET sae_commit_override %s",
2528 dut->sae_commit_override);
2529 if (wpa_command(get_station_ifname(), buf) < 0) {
2530 send_resp(dut, conn, SIGMA_ERROR,
2531 "ErrorCode,Failed to set SAE commit override");
2532 return 0;
2533 }
2534 }
2535
Jouni Malinencd4e3c32015-10-29 12:39:56 +02002536 if (wps_param &&
2537 (strcmp(wps_param, "1") == 0 || strcasecmp(wps_param, "On") == 0))
2538 wps = 1;
2539
2540 if (wps) {
2541 if (dut->wps_method == WFA_CS_WPS_NOT_READY) {
2542 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,WPS "
2543 "parameters not yet set");
2544 return 0;
2545 }
2546 if (dut->wps_method == WFA_CS_WPS_PBC) {
2547 if (wpa_command(get_station_ifname(), "WPS_PBC") < 0)
2548 return -2;
2549 } else {
2550 snprintf(buf, sizeof(buf), "WPS_PIN any %s",
2551 dut->wps_pin);
2552 if (wpa_command(get_station_ifname(), buf) < 0)
2553 return -2;
2554 }
2555 } else {
vamsi krishna52e16f92017-08-29 12:37:34 +05302556 if (strcmp(ssid, dut->infra_ssid) == 0) {
2557 sigma_dut_print(dut, DUT_MSG_DEBUG,
2558 "sta_associate for the most recently added network");
2559 } else if (find_network(dut, ssid) < 0) {
2560 sigma_dut_print(dut, DUT_MSG_DEBUG,
2561 "sta_associate for a previously stored network profile");
2562 send_resp(dut, conn, SIGMA_ERROR,
2563 "ErrorCode,Profile not found");
Jouni Malinencd4e3c32015-10-29 12:39:56 +02002564 return 0;
2565 }
2566
2567 if (bssid &&
2568 set_network(get_station_ifname(), dut->infra_network_id,
2569 "bssid", bssid) < 0) {
2570 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,"
2571 "Invalid bssid argument");
2572 return 0;
2573 }
2574
Jouni Malinen46a19b62017-06-23 14:31:27 +03002575 extra[0] = '\0';
2576 if (chan)
2577 snprintf(extra, sizeof(extra), " freq=%u",
2578 channel_to_freq(atoi(chan)));
2579 snprintf(buf, sizeof(buf), "SELECT_NETWORK %d%s",
2580 dut->infra_network_id, extra);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02002581 if (wpa_command(get_station_ifname(), buf) < 0) {
2582 sigma_dut_print(dut, DUT_MSG_INFO, "Failed to select "
2583 "network id %d on %s",
2584 dut->infra_network_id,
2585 get_station_ifname());
2586 return -2;
2587 }
2588 }
2589
2590 return 1;
2591}
2592
2593
2594static int run_hs20_osu(struct sigma_dut *dut, const char *params)
2595{
2596 char buf[500], cmd[200];
2597 int res;
2598
2599 /* Use hs20-osu-client file at the current dir, if found; otherwise use
2600 * default path */
2601 res = snprintf(cmd, sizeof(cmd),
2602 "%s -w \"%s\" -r hs20-osu-client.res %s%s -dddKt -f Logs/hs20-osu-client.txt",
2603 file_exists("./hs20-osu-client") ?
2604 "./hs20-osu-client" : "hs20-osu-client",
2605 sigma_wpas_ctrl,
2606 dut->summary_log ? "-s " : "",
2607 dut->summary_log ? dut->summary_log : "");
2608 if (res < 0 || res >= (int) sizeof(cmd))
2609 return -1;
2610
2611 res = snprintf(buf, sizeof(buf), "%s %s", cmd, params);
2612 if (res < 0 || res >= (int) sizeof(buf))
2613 return -1;
2614 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
2615
2616 if (system(buf) != 0) {
2617 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to run: %s", buf);
2618 return -1;
2619 }
2620 sigma_dut_print(dut, DUT_MSG_DEBUG,
2621 "Completed hs20-osu-client operation");
2622
2623 return 0;
2624}
2625
2626
2627static int download_ppsmo(struct sigma_dut *dut,
2628 struct sigma_conn *conn,
2629 const char *intf,
2630 struct sigma_cmd *cmd)
2631{
2632 const char *name, *path, *val;
2633 char url[500], buf[600], fbuf[100];
2634 char *fqdn = NULL;
2635
2636 name = get_param(cmd, "FileName");
2637 path = get_param(cmd, "FilePath");
2638 if (name == NULL || path == NULL)
2639 return -1;
2640
2641 if (strcasecmp(path, "VendorSpecific") == 0) {
2642 snprintf(url, sizeof(url), "PPS/%s", name);
2643 sigma_dut_print(dut, DUT_MSG_INFO, "Use pre-configured PPS MO "
2644 "from the device (%s)", url);
2645 if (!file_exists(url)) {
2646 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Requested "
2647 "PPS MO file does not exist");
2648 return 0;
2649 }
2650 snprintf(buf, sizeof(buf), "cp %s pps-tnds.xml", url);
2651 if (system(buf) != 0) {
2652 send_resp(dut, conn, SIGMA_ERROR,
2653 "errorCode,Failed to copy PPS MO");
2654 return 0;
2655 }
2656 } else if (strncasecmp(path, "http:", 5) != 0 &&
2657 strncasecmp(path, "https:", 6) != 0) {
2658 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,"
2659 "Unsupported FilePath value");
2660 return 0;
2661 } else {
2662 snprintf(url, sizeof(url), "%s/%s", path, name);
2663 sigma_dut_print(dut, DUT_MSG_INFO, "Downloading PPS MO from %s",
2664 url);
2665 snprintf(buf, sizeof(buf), "wget -T 10 -t 3 -O pps-tnds.xml '%s'", url);
2666 remove("pps-tnds.xml");
2667 if (system(buf) != 0) {
2668 send_resp(dut, conn, SIGMA_ERROR,
2669 "errorCode,Failed to download PPS MO");
2670 return 0;
2671 }
2672 }
2673
2674 if (run_hs20_osu(dut, "from_tnds pps-tnds.xml pps.xml") < 0) {
2675 send_resp(dut, conn, SIGMA_ERROR,
2676 "errorCode,Failed to parse downloaded PPSMO");
2677 return 0;
2678 }
2679 unlink("pps-tnds.xml");
2680
2681 val = get_param(cmd, "managementTreeURI");
2682 if (val) {
2683 const char *pos, *end;
2684 sigma_dut_print(dut, DUT_MSG_DEBUG, "managementTreeURI: %s",
2685 val);
2686 if (strncmp(val, "./Wi-Fi/", 8) != 0) {
2687 send_resp(dut, conn, SIGMA_ERROR,
2688 "errorCode,Invalid managementTreeURI prefix");
2689 return 0;
2690 }
2691 pos = val + 8;
2692 end = strchr(pos, '/');
2693 if (end == NULL ||
2694 strcmp(end, "/PerProviderSubscription") != 0) {
2695 send_resp(dut, conn, SIGMA_ERROR,
2696 "errorCode,Invalid managementTreeURI postfix");
2697 return 0;
2698 }
2699 if (end - pos >= (int) sizeof(fbuf)) {
2700 send_resp(dut, conn, SIGMA_ERROR,
2701 "errorCode,Too long FQDN in managementTreeURI");
2702 return 0;
2703 }
2704 memcpy(fbuf, pos, end - pos);
2705 fbuf[end - pos] = '\0';
2706 fqdn = fbuf;
2707 sigma_dut_print(dut, DUT_MSG_INFO,
2708 "FQDN from managementTreeURI: %s", fqdn);
2709 } else if (run_hs20_osu(dut, "get_fqdn pps.xml") == 0) {
2710 FILE *f = fopen("pps-fqdn", "r");
2711 if (f) {
2712 if (fgets(fbuf, sizeof(fbuf), f)) {
2713 fbuf[sizeof(fbuf) - 1] = '\0';
2714 fqdn = fbuf;
2715 sigma_dut_print(dut, DUT_MSG_DEBUG,
2716 "Use FQDN %s", fqdn);
2717 }
2718 fclose(f);
2719 }
2720 }
2721
2722 if (fqdn == NULL) {
2723 send_resp(dut, conn, SIGMA_ERROR,
2724 "errorCode,No FQDN specified");
2725 return 0;
2726 }
2727
2728 mkdir("SP", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
2729 snprintf(buf, sizeof(buf), "SP/%s", fqdn);
2730 mkdir(buf, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
2731
2732 snprintf(buf, sizeof(buf), "SP/%s/pps.xml", fqdn);
2733 if (rename("pps.xml", buf) < 0) {
2734 send_resp(dut, conn, SIGMA_ERROR,
2735 "errorCode,Could not move PPS MO");
2736 return 0;
2737 }
2738
2739 if (strcasecmp(path, "VendorSpecific") == 0) {
2740 snprintf(buf, sizeof(buf), "cp Certs/ca.pem SP/%s/ca.pem",
2741 fqdn);
2742 if (system(buf)) {
2743 send_resp(dut, conn, SIGMA_ERROR,
2744 "errorCode,Failed to copy OSU CA cert");
2745 return 0;
2746 }
2747
2748 snprintf(buf, sizeof(buf),
2749 "cp Certs/aaa-ca.pem SP/%s/aaa-ca.pem",
2750 fqdn);
2751 if (system(buf)) {
2752 send_resp(dut, conn, SIGMA_ERROR,
2753 "errorCode,Failed to copy AAA CA cert");
2754 return 0;
2755 }
2756 } else {
2757 snprintf(buf, sizeof(buf),
2758 "dl_osu_ca SP/%s/pps.xml SP/%s/ca.pem",
2759 fqdn, fqdn);
2760 if (run_hs20_osu(dut, buf) < 0) {
2761 send_resp(dut, conn, SIGMA_ERROR,
2762 "errorCode,Failed to download OSU CA cert");
2763 return 0;
2764 }
2765
2766 snprintf(buf, sizeof(buf),
2767 "dl_aaa_ca SP/%s/pps.xml SP/%s/aaa-ca.pem",
2768 fqdn, fqdn);
2769 if (run_hs20_osu(dut, buf) < 0) {
2770 sigma_dut_print(dut, DUT_MSG_INFO,
2771 "Failed to download AAA CA cert");
2772 }
2773 }
2774
2775 if (file_exists("next-client-cert.pem")) {
2776 snprintf(buf, sizeof(buf), "SP/%s/client-cert.pem", fqdn);
2777 if (rename("next-client-cert.pem", buf) < 0) {
2778 send_resp(dut, conn, SIGMA_ERROR,
2779 "errorCode,Could not move client certificate");
2780 return 0;
2781 }
2782 }
2783
2784 if (file_exists("next-client-key.pem")) {
2785 snprintf(buf, sizeof(buf), "SP/%s/client-key.pem", fqdn);
2786 if (rename("next-client-key.pem", buf) < 0) {
2787 send_resp(dut, conn, SIGMA_ERROR,
2788 "errorCode,Could not move client key");
2789 return 0;
2790 }
2791 }
2792
2793 snprintf(buf, sizeof(buf), "set_pps SP/%s/pps.xml", fqdn);
2794 if (run_hs20_osu(dut, buf) < 0) {
2795 send_resp(dut, conn, SIGMA_ERROR,
2796 "errorCode,Failed to configure credential from "
2797 "PPSMO");
2798 return 0;
2799 }
2800
2801 return 1;
2802}
2803
2804
2805static int download_cert(struct sigma_dut *dut,
2806 struct sigma_conn *conn,
2807 const char *intf,
2808 struct sigma_cmd *cmd)
2809{
2810 const char *name, *path;
2811 char url[500], buf[600];
2812
2813 name = get_param(cmd, "FileName");
2814 path = get_param(cmd, "FilePath");
2815 if (name == NULL || path == NULL)
2816 return -1;
2817
2818 if (strcasecmp(path, "VendorSpecific") == 0) {
2819 snprintf(url, sizeof(url), "Certs/%s-cert.pem", name);
2820 sigma_dut_print(dut, DUT_MSG_INFO, "Use pre-configured client "
2821 "certificate from the device (%s)", url);
2822 if (!file_exists(url)) {
2823 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Requested "
2824 "certificate file does not exist");
2825 return 0;
2826 }
2827 snprintf(buf, sizeof(buf), "cp %s next-client-cert.pem", url);
2828 if (system(buf) != 0) {
2829 send_resp(dut, conn, SIGMA_ERROR,
2830 "errorCode,Failed to copy client "
2831 "certificate");
2832 return 0;
2833 }
2834
2835 snprintf(url, sizeof(url), "Certs/%s-key.pem", name);
2836 sigma_dut_print(dut, DUT_MSG_INFO, "Use pre-configured client "
2837 "private key from the device (%s)", url);
2838 if (!file_exists(url)) {
2839 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Requested "
2840 "private key file does not exist");
2841 return 0;
2842 }
2843 snprintf(buf, sizeof(buf), "cp %s next-client-key.pem", url);
2844 if (system(buf) != 0) {
2845 send_resp(dut, conn, SIGMA_ERROR,
2846 "errorCode,Failed to copy client key");
2847 return 0;
2848 }
2849 } else if (strncasecmp(path, "http:", 5) != 0 &&
2850 strncasecmp(path, "https:", 6) != 0) {
2851 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,"
2852 "Unsupported FilePath value");
2853 return 0;
2854 } else {
2855 snprintf(url, sizeof(url), "%s/%s.pem", path, name);
2856 sigma_dut_print(dut, DUT_MSG_INFO, "Downloading client "
2857 "certificate/key from %s", url);
2858 snprintf(buf, sizeof(buf),
2859 "wget -T 10 -t 3 -O next-client-cert.pem '%s'", url);
2860 if (system(buf) != 0) {
2861 send_resp(dut, conn, SIGMA_ERROR,
2862 "errorCode,Failed to download client "
2863 "certificate");
2864 return 0;
2865 }
2866
2867 if (system("cp next-client-cert.pem next-client-key.pem") != 0)
2868 {
2869 send_resp(dut, conn, SIGMA_ERROR,
2870 "errorCode,Failed to copy client key");
2871 return 0;
2872 }
2873 }
2874
2875 return 1;
2876}
2877
2878
2879static int cmd_sta_preset_testparameters_hs2_r2(struct sigma_dut *dut,
2880 struct sigma_conn *conn,
2881 const char *intf,
2882 struct sigma_cmd *cmd)
2883{
2884 const char *val;
2885
2886 val = get_param(cmd, "FileType");
2887 if (val && strcasecmp(val, "PPSMO") == 0)
2888 return download_ppsmo(dut, conn, intf, cmd);
2889 if (val && strcasecmp(val, "CERT") == 0)
2890 return download_cert(dut, conn, intf, cmd);
2891 if (val) {
2892 send_resp(dut, conn, SIGMA_ERROR,
2893 "ErrorCode,Unsupported FileType");
2894 return 0;
2895 }
2896
2897 return 1;
2898}
2899
2900
Ankita Bajaja2cb5672017-10-25 16:08:28 +05302901static int cmd_sta_preset_testparameters_oce(struct sigma_dut *dut,
2902 struct sigma_conn *conn,
2903 const char *intf,
2904 struct sigma_cmd *cmd)
2905{
2906 const char *val;
2907
2908 val = get_param(cmd, "OCESupport");
2909 if (val && strcasecmp(val, "Disable") == 0) {
2910 if (wpa_command(intf, "SET oce 0") < 0) {
2911 send_resp(dut, conn, SIGMA_ERROR,
2912 "ErrorCode,Failed to disable OCE");
2913 return 0;
2914 }
2915 } else if (val && strcasecmp(val, "Enable") == 0) {
2916 if (wpa_command(intf, "SET oce 1") < 0) {
2917 send_resp(dut, conn, SIGMA_ERROR,
2918 "ErrorCode,Failed to enable OCE");
2919 return 0;
2920 }
2921 }
2922
2923 return 1;
2924}
2925
2926
Jouni Malinencd4e3c32015-10-29 12:39:56 +02002927static void ath_sta_set_noack(struct sigma_dut *dut, const char *intf,
2928 const char *val)
2929{
2930 int counter = 0;
2931 char token[50];
2932 char *result;
2933 char buf[100];
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05302934 char *saveptr;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02002935
Peng Xub8fc5cc2017-05-10 17:27:28 -07002936 strlcpy(token, val, sizeof(token));
Jouni Malinencd4e3c32015-10-29 12:39:56 +02002937 token[sizeof(token) - 1] = '\0';
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05302938 result = strtok_r(token, ":", &saveptr);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02002939 while (result) {
2940 if (strcmp(result, "disable") == 0) {
2941 snprintf(buf, sizeof(buf),
2942 "iwpriv %s noackpolicy %d 1 0",
2943 intf, counter);
2944 } else {
2945 snprintf(buf, sizeof(buf),
2946 "iwpriv %s noackpolicy %d 1 1",
2947 intf, counter);
2948 }
2949 if (system(buf) != 0) {
2950 sigma_dut_print(dut, DUT_MSG_ERROR,
2951 "iwpriv noackpolicy failed");
2952 }
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05302953 result = strtok_r(NULL, ":", &saveptr);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02002954 counter++;
2955 }
2956}
2957
2958
2959static void ath_sta_set_rts(struct sigma_dut *dut, const char *intf,
2960 const char *val)
2961{
2962 char buf[100];
2963
2964 snprintf(buf, sizeof(buf), "iwconfig %s rts %s", intf, val);
2965 if (system(buf) != 0) {
2966 sigma_dut_print(dut, DUT_MSG_ERROR, "iwconfig RTS failed");
2967 }
2968}
2969
2970
2971static void ath_sta_set_wmm(struct sigma_dut *dut, const char *intf,
2972 const char *val)
2973{
2974 char buf[100];
2975
2976 if (strcasecmp(val, "off") == 0) {
2977 snprintf(buf, sizeof(buf), "iwpriv %s wmm 0", intf);
2978 if (system(buf) != 0) {
2979 sigma_dut_print(dut, DUT_MSG_ERROR,
2980 "Failed to turn off WMM");
2981 }
2982 }
2983}
2984
2985
2986static void ath_sta_set_sgi(struct sigma_dut *dut, const char *intf,
2987 const char *val)
2988{
2989 char buf[100];
2990 int sgi20;
2991
2992 sgi20 = strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0;
2993
2994 snprintf(buf, sizeof(buf), "iwpriv %s shortgi %d", intf, sgi20);
2995 if (system(buf) != 0)
2996 sigma_dut_print(dut, DUT_MSG_ERROR, "iwpriv shortgi failed");
2997}
2998
2999
3000static void ath_sta_set_11nrates(struct sigma_dut *dut, const char *intf,
3001 const char *val)
3002{
3003 char buf[100];
Pradeep Reddy POTTETI67376b72016-10-25 20:08:17 +05303004 int rate_code, v;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003005
3006 /* Disable Tx Beam forming when using a fixed rate */
3007 ath_disable_txbf(dut, intf);
3008
Pradeep Reddy POTTETI67376b72016-10-25 20:08:17 +05303009 v = atoi(val);
3010 if (v < 0 || v > 32) {
3011 sigma_dut_print(dut, DUT_MSG_ERROR,
3012 "Invalid Fixed MCS rate: %d", v);
3013 return;
3014 }
3015 rate_code = 0x80 + v;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003016
3017 snprintf(buf, sizeof(buf), "iwpriv %s set11NRates 0x%x",
3018 intf, rate_code);
3019 if (system(buf) != 0) {
3020 sigma_dut_print(dut, DUT_MSG_ERROR,
3021 "iwpriv set11NRates failed");
3022 }
3023
3024 /* Channel width gets messed up, fix this */
3025 snprintf(buf, sizeof(buf), "iwpriv %s chwidth %d", intf, dut->chwidth);
3026 if (system(buf) != 0)
3027 sigma_dut_print(dut, DUT_MSG_ERROR, "iwpriv chwidth failed");
3028}
3029
3030
3031static void ath_sta_set_amsdu(struct sigma_dut *dut, const char *intf,
3032 const char *val)
3033{
3034 char buf[60];
3035
3036 if (strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0)
3037 snprintf(buf, sizeof(buf), "iwpriv %s amsdu 2", intf);
3038 else
3039 snprintf(buf, sizeof(buf), "iwpriv %s amsdu 1", intf);
3040
3041 if (system(buf) != 0)
3042 sigma_dut_print(dut, DUT_MSG_ERROR, "iwpriv amsdu failed");
3043}
3044
3045
Deepak Dhamdhere80356cb2016-03-28 16:48:32 -07003046static int iwpriv_sta_set_ampdu(struct sigma_dut *dut, const char *intf,
3047 int ampdu)
3048{
3049 char buf[60];
3050
3051 snprintf(buf, sizeof(buf), "iwpriv %s ampdu %d", intf, ampdu);
3052 if (system(buf) != 0) {
3053 sigma_dut_print(dut, DUT_MSG_ERROR, "iwpriv ampdu failed");
3054 return -1;
3055 }
3056
3057 return 0;
3058}
3059
3060
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003061static void ath_sta_set_stbc(struct sigma_dut *dut, const char *intf,
3062 const char *val)
3063{
3064 char buf[60];
3065
3066 snprintf(buf, sizeof(buf), "iwpriv %s tx_stbc %s", intf, val);
3067 if (system(buf) != 0) {
3068 sigma_dut_print(dut, DUT_MSG_ERROR,
3069 "iwpriv tx_stbc failed");
3070 }
3071
3072 snprintf(buf, sizeof(buf), "iwpriv %s rx_stbc %s", intf, val);
3073 if (system(buf) != 0) {
3074 sigma_dut_print(dut, DUT_MSG_ERROR,
3075 "iwpriv rx_stbc failed");
3076 }
3077}
3078
3079
3080static int wcn_sta_set_cts_width(struct sigma_dut *dut, const char *intf,
3081 const char *val)
3082{
3083 char buf[60];
3084
Peng Xucc317ed2017-05-18 16:44:37 -07003085 if (strcmp(val, "160") == 0) {
3086 snprintf(buf, sizeof(buf), "iwpriv %s cts_cbw 5", intf);
3087 } else if (strcmp(val, "80") == 0) {
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003088 snprintf(buf, sizeof(buf), "iwpriv %s cts_cbw 3", intf);
3089 } else if (strcmp(val, "40") == 0) {
3090 snprintf(buf, sizeof(buf), "iwpriv %s cts_cbw 2", intf);
3091 } else if (strcmp(val, "20") == 0) {
3092 snprintf(buf, sizeof(buf), "iwpriv %s cts_cbw 1", intf);
3093 } else if (strcasecmp(val, "Auto") == 0) {
3094 buf[0] = '\0';
3095 } else {
3096 sigma_dut_print(dut, DUT_MSG_ERROR,
3097 "WIDTH/CTS_WIDTH value not supported");
3098 return -1;
3099 }
3100
3101 if (buf[0] != '\0' && system(buf) != 0) {
3102 sigma_dut_print(dut, DUT_MSG_ERROR,
3103 "Failed to set WIDTH/CTS_WIDTH");
3104 return -1;
3105 }
3106
3107 return 0;
3108}
3109
3110
3111int ath_set_width(struct sigma_dut *dut, struct sigma_conn *conn,
3112 const char *intf, const char *val)
3113{
3114 char buf[60];
3115
3116 if (strcasecmp(val, "Auto") == 0) {
3117 snprintf(buf, sizeof(buf), "iwpriv %s chwidth 0", intf);
3118 dut->chwidth = 0;
3119 } else if (strcasecmp(val, "20") == 0) {
3120 snprintf(buf, sizeof(buf), "iwpriv %s chwidth 0", intf);
3121 dut->chwidth = 0;
3122 } else if (strcasecmp(val, "40") == 0) {
3123 snprintf(buf, sizeof(buf), "iwpriv %s chwidth 1", intf);
3124 dut->chwidth = 1;
3125 } else if (strcasecmp(val, "80") == 0) {
3126 snprintf(buf, sizeof(buf), "iwpriv %s chwidth 2", intf);
3127 dut->chwidth = 2;
3128 } else if (strcasecmp(val, "160") == 0) {
3129 snprintf(buf, sizeof(buf), "iwpriv %s chwidth 3", intf);
3130 dut->chwidth = 3;
3131 } else {
3132 send_resp(dut, conn, SIGMA_ERROR,
3133 "ErrorCode,WIDTH not supported");
3134 return -1;
3135 }
3136
3137 if (system(buf) != 0) {
3138 sigma_dut_print(dut, DUT_MSG_ERROR,
3139 "iwpriv chwidth failed");
3140 }
3141
3142 return 0;
3143}
3144
3145
3146static int wcn_sta_set_sp_stream(struct sigma_dut *dut, const char *intf,
3147 const char *val)
3148{
3149 char buf[60];
3150
3151 if (strcmp(val, "1SS") == 0) {
3152 snprintf(buf, sizeof(buf), "iwpriv %s nss 1", intf);
3153 } else if (strcmp(val, "2SS") == 0) {
3154 snprintf(buf, sizeof(buf), "iwpriv %s nss 2", intf);
3155 } else {
3156 sigma_dut_print(dut, DUT_MSG_ERROR,
3157 "SP_STREAM value not supported");
3158 return -1;
3159 }
3160
3161 if (system(buf) != 0) {
3162 sigma_dut_print(dut, DUT_MSG_ERROR,
3163 "Failed to set SP_STREAM");
3164 return -1;
3165 }
3166
3167 return 0;
3168}
3169
3170
Pradeep Reddy POTTETI4a1f6b32016-11-23 13:15:21 +05303171static void wcn_sta_set_stbc(struct sigma_dut *dut, const char *intf,
3172 const char *val)
3173{
3174 char buf[60];
3175
3176 snprintf(buf, sizeof(buf), "iwpriv %s tx_stbc %s", intf, val);
3177 if (system(buf) != 0)
3178 sigma_dut_print(dut, DUT_MSG_ERROR, "iwpriv tx_stbc failed");
3179
3180 snprintf(buf, sizeof(buf), "iwpriv %s rx_stbc %s", intf, val);
3181 if (system(buf) != 0)
3182 sigma_dut_print(dut, DUT_MSG_ERROR, "iwpriv rx_stbc failed");
3183}
3184
3185
Ashwini Patil68d02cd2017-01-10 15:39:16 +05303186static int mbo_set_cellular_data_capa(struct sigma_dut *dut,
3187 struct sigma_conn *conn,
3188 const char *intf, int capa)
3189{
3190 char buf[32];
3191
3192 if (capa > 0 && capa < 4) {
3193 snprintf(buf, sizeof(buf), "SET mbo_cell_capa %d", capa);
3194 if (wpa_command(intf, buf) < 0) {
3195 send_resp(dut, conn, SIGMA_ERROR,
3196 "ErrorCode, Failed to set cellular data capability");
3197 return 0;
3198 }
3199 return 1;
3200 }
3201
3202 sigma_dut_print(dut, DUT_MSG_ERROR,
3203 "Invalid Cellular data capability: %d", capa);
3204 send_resp(dut, conn, SIGMA_INVALID,
3205 "ErrorCode,Invalid cellular data capability");
3206 return 0;
3207}
3208
3209
Ashwini Patil9183fdb2017-04-13 16:58:25 +05303210static int mbo_set_roaming(struct sigma_dut *dut, struct sigma_conn *conn,
3211 const char *intf, const char *val)
3212{
3213 if (strcasecmp(val, "Disable") == 0) {
3214 if (wpa_command(intf, "SET roaming 0") < 0) {
3215 send_resp(dut, conn, SIGMA_ERROR,
3216 "ErrorCode,Failed to disable roaming");
3217 return 0;
3218 }
3219 return 1;
3220 }
3221
3222 if (strcasecmp(val, "Enable") == 0) {
3223 if (wpa_command(intf, "SET roaming 1") < 0) {
3224 send_resp(dut, conn, SIGMA_ERROR,
3225 "ErrorCode,Failed to enable roaming");
3226 return 0;
3227 }
3228 return 1;
3229 }
3230
3231 sigma_dut_print(dut, DUT_MSG_ERROR,
3232 "Invalid value provided for roaming: %s", val);
3233 send_resp(dut, conn, SIGMA_INVALID,
3234 "ErrorCode,Unknown value provided for Roaming");
3235 return 0;
3236}
3237
3238
Ashwini Patila75de5a2017-04-13 16:35:05 +05303239static int mbo_set_assoc_disallow(struct sigma_dut *dut,
3240 struct sigma_conn *conn,
3241 const char *intf, const char *val)
3242{
3243 if (strcasecmp(val, "Disable") == 0) {
3244 if (wpa_command(intf, "SET ignore_assoc_disallow 1") < 0) {
3245 send_resp(dut, conn, SIGMA_ERROR,
3246 "ErrorCode,Failed to disable Assoc_disallow");
3247 return 0;
3248 }
3249 return 1;
3250 }
3251
3252 if (strcasecmp(val, "Enable") == 0) {
3253 if (wpa_command(intf, "SET ignore_assoc_disallow 0") < 0) {
3254 send_resp(dut, conn, SIGMA_ERROR,
3255 "ErrorCode,Failed to enable Assoc_disallow");
3256 return 0;
3257 }
3258 return 1;
3259 }
3260
3261 sigma_dut_print(dut, DUT_MSG_ERROR,
3262 "Invalid value provided for Assoc_disallow: %s", val);
3263 send_resp(dut, conn, SIGMA_INVALID,
3264 "ErrorCode,Unknown value provided for Assoc_disallow");
3265 return 0;
3266}
3267
3268
Ashwini Patilc63161e2017-04-13 16:30:23 +05303269static int mbo_set_bss_trans_req(struct sigma_dut *dut, struct sigma_conn *conn,
3270 const char *intf, const char *val)
3271{
3272 if (strcasecmp(val, "Reject") == 0) {
3273 if (wpa_command(intf, "SET reject_btm_req_reason 1") < 0) {
3274 send_resp(dut, conn, SIGMA_ERROR,
3275 "ErrorCode,Failed to Reject BTM Request");
3276 return 0;
3277 }
3278 return 1;
3279 }
3280
3281 if (strcasecmp(val, "Accept") == 0) {
3282 if (wpa_command(intf, "SET reject_btm_req_reason 0") < 0) {
3283 send_resp(dut, conn, SIGMA_ERROR,
3284 "ErrorCode,Failed to Accept BTM Request");
3285 return 0;
3286 }
3287 return 1;
3288 }
3289
3290 sigma_dut_print(dut, DUT_MSG_ERROR,
3291 "Invalid value provided for BSS_Transition: %s", val);
3292 send_resp(dut, conn, SIGMA_INVALID,
3293 "ErrorCode,Unknown value provided for BSS_Transition");
3294 return 0;
3295}
3296
3297
Ashwini Patil00402582017-04-13 12:29:39 +05303298static int mbo_set_non_pref_ch_list(struct sigma_dut *dut,
3299 struct sigma_conn *conn,
3300 const char *intf,
3301 struct sigma_cmd *cmd)
3302{
3303 const char *ch, *pref, *op_class, *reason;
3304 char buf[120];
3305 int len, ret;
3306
3307 pref = get_param(cmd, "Ch_Pref");
3308 if (!pref)
3309 return 1;
3310
3311 if (strcasecmp(pref, "clear") == 0) {
3312 free(dut->non_pref_ch_list);
3313 dut->non_pref_ch_list = NULL;
3314 } else {
3315 op_class = get_param(cmd, "Ch_Op_Class");
3316 if (!op_class) {
3317 send_resp(dut, conn, SIGMA_INVALID,
3318 "ErrorCode,Ch_Op_Class not provided");
3319 return 0;
3320 }
3321
3322 ch = get_param(cmd, "Ch_Pref_Num");
3323 if (!ch) {
3324 send_resp(dut, conn, SIGMA_INVALID,
3325 "ErrorCode,Ch_Pref_Num not provided");
3326 return 0;
3327 }
3328
3329 reason = get_param(cmd, "Ch_Reason_Code");
3330 if (!reason) {
3331 send_resp(dut, conn, SIGMA_INVALID,
3332 "ErrorCode,Ch_Reason_Code not provided");
3333 return 0;
3334 }
3335
3336 if (!dut->non_pref_ch_list) {
3337 dut->non_pref_ch_list =
3338 calloc(1, NON_PREF_CH_LIST_SIZE);
3339 if (!dut->non_pref_ch_list) {
3340 send_resp(dut, conn, SIGMA_ERROR,
3341 "ErrorCode,Failed to allocate memory for non_pref_ch_list");
3342 return 0;
3343 }
3344 }
3345 len = strlen(dut->non_pref_ch_list);
3346 ret = snprintf(dut->non_pref_ch_list + len,
3347 NON_PREF_CH_LIST_SIZE - len,
3348 " %s:%s:%s:%s", op_class, ch, pref, reason);
3349 if (ret > 0 && ret < NON_PREF_CH_LIST_SIZE - len) {
3350 sigma_dut_print(dut, DUT_MSG_DEBUG, "non_pref_list: %s",
3351 dut->non_pref_ch_list);
3352 } else {
3353 sigma_dut_print(dut, DUT_MSG_ERROR,
3354 "snprintf failed for non_pref_list, ret = %d",
3355 ret);
3356 send_resp(dut, conn, SIGMA_ERROR,
3357 "ErrorCode,snprintf failed");
3358 free(dut->non_pref_ch_list);
3359 dut->non_pref_ch_list = NULL;
3360 return 0;
3361 }
3362 }
3363
3364 ret = snprintf(buf, sizeof(buf), "SET non_pref_chan%s",
3365 dut->non_pref_ch_list ? dut->non_pref_ch_list : " ");
3366 if (ret < 0 || ret >= (int) sizeof(buf)) {
3367 sigma_dut_print(dut, DUT_MSG_DEBUG,
3368 "snprintf failed for set non_pref_chan, ret: %d",
3369 ret);
3370 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,snprint failed");
3371 return 0;
3372 }
3373
3374 if (wpa_command(intf, buf) < 0) {
3375 send_resp(dut, conn, SIGMA_ERROR,
3376 "ErrorCode,Failed to set non-preferred channel list");
3377 return 0;
3378 }
3379
3380 return 1;
3381}
3382
3383
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003384static int cmd_sta_preset_testparameters(struct sigma_dut *dut,
3385 struct sigma_conn *conn,
3386 struct sigma_cmd *cmd)
3387{
3388 const char *intf = get_param(cmd, "Interface");
3389 const char *val;
3390
3391 val = get_param(cmd, "Program");
Peng Xue9fa7952017-05-09 15:59:49 -07003392 if (val && strcasecmp(val, "HS2-R2") == 0)
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003393 return cmd_sta_preset_testparameters_hs2_r2(dut, conn, intf,
3394 cmd);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003395
priyadharshini gowthamand66913a2016-07-29 15:11:17 -07003396 if (val && strcasecmp(val, "LOC") == 0)
3397 return loc_cmd_sta_preset_testparameters(dut, conn, cmd);
3398
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003399#ifdef ANDROID_NAN
3400 if (val && strcasecmp(val, "NAN") == 0)
3401 return nan_cmd_sta_preset_testparameters(dut, conn, cmd);
3402#endif /* ANDROID_NAN */
Amarnath Hullur Subramanyam9c381f52017-03-17 00:04:41 -07003403#ifdef MIRACAST
3404 if (val && (strcasecmp(val, "WFD") == 0 ||
3405 strcasecmp(val, "DisplayR2") == 0))
3406 return miracast_preset_testparameters(dut, conn, cmd);
3407#endif /* MIRACAST */
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003408
Ashwini Patil68d02cd2017-01-10 15:39:16 +05303409 if (val && strcasecmp(val, "MBO") == 0) {
3410 val = get_param(cmd, "Cellular_Data_Cap");
3411 if (val &&
3412 mbo_set_cellular_data_capa(dut, conn, intf, atoi(val)) == 0)
3413 return 0;
Ashwini Patil00402582017-04-13 12:29:39 +05303414
3415 val = get_param(cmd, "Ch_Pref");
3416 if (val && mbo_set_non_pref_ch_list(dut, conn, intf, cmd) == 0)
3417 return 0;
3418
Ashwini Patilc63161e2017-04-13 16:30:23 +05303419 val = get_param(cmd, "BSS_Transition");
3420 if (val && mbo_set_bss_trans_req(dut, conn, intf, val) == 0)
3421 return 0;
3422
Ashwini Patila75de5a2017-04-13 16:35:05 +05303423 val = get_param(cmd, "Assoc_Disallow");
3424 if (val && mbo_set_assoc_disallow(dut, conn, intf, val) == 0)
3425 return 0;
3426
Ashwini Patil9183fdb2017-04-13 16:58:25 +05303427 val = get_param(cmd, "Roaming");
3428 if (val && mbo_set_roaming(dut, conn, intf, val) == 0)
3429 return 0;
3430
Ashwini Patil68d02cd2017-01-10 15:39:16 +05303431 return 1;
3432 }
3433
Ankita Bajaja2cb5672017-10-25 16:08:28 +05303434 if (val && strcasecmp(val, "OCE") == 0)
3435 return cmd_sta_preset_testparameters_oce(dut, conn, intf, cmd);
3436
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003437#if 0
3438 val = get_param(cmd, "Supplicant");
3439 if (val && strcasecmp(val, "Default") != 0) {
3440 send_resp(dut, conn, SIGMA_ERROR,
3441 "ErrorCode,Only default(Vendor) supplicant "
3442 "supported");
3443 return 0;
3444 }
3445#endif
3446
3447 val = get_param(cmd, "RTS");
3448 if (val) {
3449 switch (get_driver_type()) {
3450 case DRIVER_ATHEROS:
3451 ath_sta_set_rts(dut, intf, val);
3452 break;
3453 default:
3454#if 0
3455 send_resp(dut, conn, SIGMA_ERROR,
3456 "ErrorCode,Setting RTS not supported");
3457 return 0;
3458#else
3459 sigma_dut_print(dut, DUT_MSG_DEBUG,
3460 "Setting RTS not supported");
3461 break;
3462#endif
3463 }
3464 }
3465
3466#if 0
3467 val = get_param(cmd, "FRGMNT");
3468 if (val) {
3469 /* TODO */
3470 send_resp(dut, conn, SIGMA_ERROR,
3471 "ErrorCode,Setting FRGMNT not supported");
3472 return 0;
3473 }
3474#endif
3475
3476#if 0
3477 val = get_param(cmd, "Preamble");
3478 if (val) {
3479 /* TODO: Long/Short */
3480 send_resp(dut, conn, SIGMA_ERROR,
3481 "ErrorCode,Setting Preamble not supported");
3482 return 0;
3483 }
3484#endif
3485
3486 val = get_param(cmd, "Mode");
3487 if (val) {
3488 if (strcmp(val, "11b") == 0 ||
3489 strcmp(val, "11g") == 0 ||
3490 strcmp(val, "11a") == 0 ||
3491 strcmp(val, "11n") == 0 ||
3492 strcmp(val, "11ng") == 0 ||
3493 strcmp(val, "11nl") == 0 ||
3494 strcmp(val, "11nl(nabg)") == 0 ||
3495 strcmp(val, "AC") == 0 ||
3496 strcmp(val, "11AC") == 0 ||
3497 strcmp(val, "11ac") == 0 ||
3498 strcmp(val, "11na") == 0 ||
3499 strcmp(val, "11an") == 0) {
3500 /* STA supports all modes by default */
3501 } else {
3502 send_resp(dut, conn, SIGMA_ERROR,
3503 "ErrorCode,Setting Mode not supported");
3504 return 0;
3505 }
3506 }
3507
3508 val = get_param(cmd, "wmm");
3509 if (val) {
3510 switch (get_driver_type()) {
3511 case DRIVER_ATHEROS:
3512 ath_sta_set_wmm(dut, intf, val);
3513 break;
3514 default:
3515 sigma_dut_print(dut, DUT_MSG_DEBUG,
3516 "Setting wmm not supported");
3517 break;
3518 }
3519 }
3520
3521 val = get_param(cmd, "Powersave");
3522 if (val) {
3523 if (strcmp(val, "0") == 0 || strcasecmp(val, "off") == 0) {
3524 if (wpa_command(get_station_ifname(),
3525 "P2P_SET ps 0") < 0)
3526 return -2;
3527 /* Make sure test modes are disabled */
3528 wpa_command(get_station_ifname(), "P2P_SET ps 98");
3529 wpa_command(get_station_ifname(), "P2P_SET ps 96");
3530 } else if (strcmp(val, "1") == 0 ||
3531 strcasecmp(val, "PSPoll") == 0 ||
3532 strcasecmp(val, "on") == 0) {
3533 /* Disable default power save mode */
3534 wpa_command(get_station_ifname(), "P2P_SET ps 0");
3535 /* Enable PS-Poll test mode */
3536 if (wpa_command(get_station_ifname(),
3537 "P2P_SET ps 97") < 0 ||
3538 wpa_command(get_station_ifname(),
3539 "P2P_SET ps 99") < 0)
3540 return -2;
3541 } else if (strcmp(val, "2") == 0 ||
3542 strcasecmp(val, "Fast") == 0) {
3543 /* TODO */
3544 send_resp(dut, conn, SIGMA_ERROR,
3545 "ErrorCode,Powersave=Fast not supported");
3546 return 0;
3547 } else if (strcmp(val, "3") == 0 ||
3548 strcasecmp(val, "PSNonPoll") == 0) {
3549 /* Make sure test modes are disabled */
3550 wpa_command(get_station_ifname(), "P2P_SET ps 98");
3551 wpa_command(get_station_ifname(), "P2P_SET ps 96");
3552
3553 /* Enable default power save mode */
3554 if (wpa_command(get_station_ifname(),
3555 "P2P_SET ps 1") < 0)
3556 return -2;
3557 } else
3558 return -1;
3559 }
3560
3561 val = get_param(cmd, "NoAck");
3562 if (val) {
3563 switch (get_driver_type()) {
3564 case DRIVER_ATHEROS:
3565 ath_sta_set_noack(dut, intf, val);
3566 break;
3567 default:
3568 send_resp(dut, conn, SIGMA_ERROR,
3569 "ErrorCode,Setting NoAck not supported");
3570 return 0;
3571 }
3572 }
3573
3574 val = get_param(cmd, "IgnoreChswitchProhibit");
3575 if (val) {
3576 /* TODO: Enabled/disabled */
3577 if (strcasecmp(val, "Enabled") == 0) {
3578 send_resp(dut, conn, SIGMA_ERROR,
3579 "ErrorCode,Enabling IgnoreChswitchProhibit "
3580 "not supported");
3581 return 0;
3582 }
3583 }
3584
3585 val = get_param(cmd, "TDLS");
3586 if (val) {
3587 if (strcasecmp(val, "Disabled") == 0) {
3588 if (wpa_command(intf, "SET tdls_disabled 1")) {
3589 send_resp(dut, conn, SIGMA_ERROR,
3590 "ErrorCode,Failed to disable TDLS");
3591 return 0;
3592 }
3593 } else if (strcasecmp(val, "Enabled") == 0) {
3594 if (wpa_command(intf, "SET tdls_disabled 0")) {
3595 send_resp(dut, conn, SIGMA_ERROR,
3596 "ErrorCode,Failed to enable TDLS");
3597 return 0;
3598 }
3599 } else {
3600 send_resp(dut, conn, SIGMA_ERROR,
3601 "ErrorCode,Unsupported TDLS value");
3602 return 0;
3603 }
3604 }
3605
3606 val = get_param(cmd, "TDLSmode");
3607 if (val) {
3608 if (strcasecmp(val, "Default") == 0) {
3609 wpa_command(intf, "SET tdls_testing 0");
3610 } else if (strcasecmp(val, "APProhibit") == 0) {
3611 if (wpa_command(intf, "SET tdls_testing 0x400")) {
3612 send_resp(dut, conn, SIGMA_ERROR,
3613 "ErrorCode,Failed to enable ignore "
3614 "APProhibit TDLS mode");
3615 return 0;
3616 }
3617 } else if (strcasecmp(val, "HiLoMac") == 0) {
3618 /* STA should respond with TDLS setup req for a TDLS
3619 * setup req */
3620 if (wpa_command(intf, "SET tdls_testing 0x80")) {
3621 send_resp(dut, conn, SIGMA_ERROR,
3622 "ErrorCode,Failed to enable HiLoMac "
3623 "TDLS mode");
3624 return 0;
3625 }
3626 } else if (strcasecmp(val, "WeakSecurity") == 0) {
3627 /*
3628 * Since all security modes are enabled by default when
3629 * Sigma control is used, there is no need to do
3630 * anything here.
3631 */
3632 } else if (strcasecmp(val, "ExistLink") == 0) {
3633 /*
3634 * Since we allow new TDLS Setup Request even if there
3635 * is an existing link, nothing needs to be done for
3636 * this.
3637 */
3638 } else {
3639 /* TODO:
3640 * ExistLink: STA should send TDLS setup req even if
3641 * direct link already exists
3642 */
3643 send_resp(dut, conn, SIGMA_ERROR,
3644 "ErrorCode,Unsupported TDLSmode value");
3645 return 0;
3646 }
3647 }
3648
3649 val = get_param(cmd, "FakePubKey");
3650 if (val && atoi(val) && wpa_command(intf, "SET wps_corrupt_pkhash 1")) {
3651 send_resp(dut, conn, SIGMA_ERROR,
3652 "ErrorCode,Failed to enable FakePubKey");
3653 return 0;
3654 }
3655
3656 return 1;
3657}
3658
3659
3660static const char * ath_get_radio_name(const char *radio_name)
3661{
3662 if (radio_name == NULL)
3663 return "wifi0";
3664 if (strcmp(radio_name, "wifi1") == 0)
3665 return "wifi1";
3666 if (strcmp(radio_name, "wifi2") == 0)
3667 return "wifi2";
3668 return "wifi0";
3669}
3670
3671
3672static void ath_sta_set_txsp_stream(struct sigma_dut *dut, const char *intf,
3673 const char *val)
3674{
3675 char buf[60];
3676 unsigned int vht_mcsmap = 0;
3677 int txchainmask = 0;
3678 const char *basedev = ath_get_radio_name(sigma_radio_ifname[0]);
3679
3680 if (strcasecmp(val, "1") == 0 || strcasecmp(val, "1SS") == 0) {
3681 if (dut->testbed_flag_txsp == 1) {
3682 vht_mcsmap = 0xfffc;
3683 dut->testbed_flag_txsp = 0;
3684 } else {
3685 vht_mcsmap = 0xfffe;
3686 }
3687 txchainmask = 1;
3688 } else if (strcasecmp(val, "2") == 0 || strcasecmp(val, "2SS") == 0) {
3689 if (dut->testbed_flag_txsp == 1) {
3690 vht_mcsmap = 0xfff0;
3691 dut->testbed_flag_txsp = 0;
3692 } else {
3693 vht_mcsmap = 0xfffa;
3694 }
3695 txchainmask = 3;
3696 } else if (strcasecmp(val, "3") == 0 || strcasecmp(val, "3SS") == 0) {
3697 if (dut->testbed_flag_txsp == 1) {
3698 vht_mcsmap = 0xffc0;
3699 dut->testbed_flag_txsp = 0;
3700 } else {
3701 vht_mcsmap = 0xffea;
3702 }
3703 txchainmask = 7;
3704 } else if (strcasecmp(val, "4") == 0 || strcasecmp(val, "4SS") == 0) {
3705 if (dut->testbed_flag_txsp == 1) {
3706 vht_mcsmap = 0xff00;
3707 dut->testbed_flag_txsp = 0;
3708 } else {
3709 vht_mcsmap = 0xffaa;
3710 }
3711 txchainmask = 15;
3712 } else {
3713 if (dut->testbed_flag_txsp == 1) {
3714 vht_mcsmap = 0xffc0;
3715 dut->testbed_flag_txsp = 0;
3716 } else {
3717 vht_mcsmap = 0xffea;
3718 }
3719 }
3720
3721 if (txchainmask) {
3722 snprintf(buf, sizeof(buf), "iwpriv %s txchainmask %d",
3723 basedev, txchainmask);
3724 if (system(buf) != 0) {
3725 sigma_dut_print(dut, DUT_MSG_ERROR,
3726 "iwpriv txchainmask failed");
3727 }
3728 }
3729
3730 snprintf(buf, sizeof(buf), "iwpriv %s vht_mcsmap 0x%04x",
3731 intf, vht_mcsmap);
3732 if (system(buf) != 0) {
3733 sigma_dut_print(dut, DUT_MSG_ERROR,
3734 "iwpriv %s vht_mcsmap 0x%04x failed",
3735 intf, vht_mcsmap);
3736 }
3737}
3738
3739
3740static void ath_sta_set_rxsp_stream(struct sigma_dut *dut, const char *intf,
3741 const char *val)
3742{
3743 char buf[60];
3744 unsigned int vht_mcsmap = 0;
3745 int rxchainmask = 0;
3746 const char *basedev = ath_get_radio_name(sigma_radio_ifname[0]);
3747
3748 if (strcasecmp(val, "1") == 0 || strcasecmp(val, "1SS") == 0) {
3749 if (dut->testbed_flag_rxsp == 1) {
3750 vht_mcsmap = 0xfffc;
3751 dut->testbed_flag_rxsp = 0;
3752 } else {
3753 vht_mcsmap = 0xfffe;
3754 }
3755 rxchainmask = 1;
3756 } else if (strcasecmp(val, "2") == 0 || strcasecmp(val, "2SS") == 0) {
3757 if (dut->testbed_flag_rxsp == 1) {
3758 vht_mcsmap = 0xfff0;
3759 dut->testbed_flag_rxsp = 0;
3760 } else {
3761 vht_mcsmap = 0xfffa;
3762 }
3763 rxchainmask = 3;
3764 } else if (strcasecmp(val, "3") == 0 || strcasecmp(val, "3SS") == 0) {
3765 if (dut->testbed_flag_rxsp == 1) {
3766 vht_mcsmap = 0xffc0;
3767 dut->testbed_flag_rxsp = 0;
3768 } else {
3769 vht_mcsmap = 0xffea;
3770 }
3771 rxchainmask = 7;
3772 } else if (strcasecmp(val, "4") == 0 || strcasecmp(val, "4SS") == 0) {
3773 if (dut->testbed_flag_rxsp == 1) {
3774 vht_mcsmap = 0xff00;
3775 dut->testbed_flag_rxsp = 0;
3776 } else {
3777 vht_mcsmap = 0xffaa;
3778 }
3779 rxchainmask = 15;
3780 } else {
3781 if (dut->testbed_flag_rxsp == 1) {
3782 vht_mcsmap = 0xffc0;
3783 dut->testbed_flag_rxsp = 0;
3784 } else {
3785 vht_mcsmap = 0xffea;
3786 }
3787 }
3788
3789 if (rxchainmask) {
3790 snprintf(buf, sizeof(buf), "iwpriv %s rxchainmask %d",
3791 basedev, rxchainmask);
3792 if (system(buf) != 0) {
3793 sigma_dut_print(dut, DUT_MSG_ERROR,
3794 "iwpriv rxchainmask failed");
3795 }
3796 }
3797
3798 snprintf(buf, sizeof(buf), "iwpriv %s vht_mcsmap 0x%04x",
3799 intf, vht_mcsmap);
3800 if (system(buf) != 0) {
3801 sigma_dut_print(dut, DUT_MSG_ERROR,
3802 "iwpriv %s vht_mcsmap 0x%04x",
3803 intf, vht_mcsmap);
3804 }
3805}
3806
3807
3808void ath_set_zero_crc(struct sigma_dut *dut, const char *val)
3809{
3810 if (strcasecmp(val, "enable") == 0) {
3811 if (system("athdiag --set --address=0x2a204 --and=0xbfffffff")
3812 != 0) {
3813 sigma_dut_print(dut, DUT_MSG_ERROR,
3814 "Disable BB_VHTSIGB_CRC_CALC failed");
3815 }
3816
3817 if (system("athdiag --set --address=0x2a204 --or=0x80000000")
3818 != 0) {
3819 sigma_dut_print(dut, DUT_MSG_ERROR,
3820 "Enable FORCE_VHT_SIGB_CRC_VALUE_ZERO failed");
3821 }
3822 } else {
3823 if (system("athdiag --set --address=0x2a204 --and=0x7fffffff")
3824 != 0) {
3825 sigma_dut_print(dut, DUT_MSG_ERROR,
3826 "Disable FORCE_VHT_SIGB_CRC_VALUE_ZERO failed");
3827 }
3828
3829 if (system("athdiag --set --address=0x2a204 --or=0x40000000")
3830 != 0) {
3831 sigma_dut_print(dut, DUT_MSG_ERROR,
3832 "Enable BB_VHTSIGB_CRC_CALC failed");
3833 }
3834 }
3835}
3836
3837
3838static int cmd_sta_set_wireless_common(const char *intf, struct sigma_dut *dut,
3839 struct sigma_conn *conn,
3840 struct sigma_cmd *cmd)
3841{
3842 const char *val;
3843 int ampdu = -1;
3844 char buf[30];
3845
3846 val = get_param(cmd, "40_INTOLERANT");
3847 if (val) {
3848 if (strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0) {
3849 /* TODO: iwpriv ht40intol through wpa_supplicant */
3850 send_resp(dut, conn, SIGMA_ERROR,
3851 "ErrorCode,40_INTOLERANT not supported");
3852 return 0;
3853 }
3854 }
3855
3856 val = get_param(cmd, "ADDBA_REJECT");
3857 if (val) {
3858 if (strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0) {
3859 /* reject any ADDBA with status "decline" */
3860 ampdu = 0;
3861 } else {
3862 /* accept ADDBA */
3863 ampdu = 1;
3864 }
3865 }
3866
3867 val = get_param(cmd, "AMPDU");
3868 if (val) {
3869 if (strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0) {
3870 /* enable AMPDU Aggregation */
3871 if (ampdu == 0) {
3872 send_resp(dut, conn, SIGMA_ERROR,
3873 "ErrorCode,Mismatch in "
3874 "addba_reject/ampdu - "
3875 "not supported");
3876 return 0;
3877 }
3878 ampdu = 1;
3879 } else {
3880 /* disable AMPDU Aggregation */
3881 if (ampdu == 1) {
3882 send_resp(dut, conn, SIGMA_ERROR,
3883 "ErrorCode,Mismatch in "
3884 "addba_reject/ampdu - "
3885 "not supported");
3886 return 0;
3887 }
3888 ampdu = 0;
3889 }
3890 }
3891
3892 if (ampdu >= 0) {
3893 sigma_dut_print(dut, DUT_MSG_DEBUG, "%s A-MPDU aggregation",
3894 ampdu ? "Enabling" : "Disabling");
3895 snprintf(buf, sizeof(buf), "SET ampdu %d", ampdu);
Deepak Dhamdhere80356cb2016-03-28 16:48:32 -07003896 if (wpa_command(intf, buf) < 0 &&
3897 iwpriv_sta_set_ampdu(dut, intf, ampdu) < 0) {
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003898 send_resp(dut, conn, SIGMA_ERROR,
3899 "ErrorCode,set aggr failed");
3900 return 0;
3901 }
3902 }
3903
3904 val = get_param(cmd, "AMSDU");
3905 if (val) {
3906 switch (get_driver_type()) {
3907 case DRIVER_ATHEROS:
3908 ath_sta_set_amsdu(dut, intf, val);
3909 break;
3910 default:
3911 if (strcmp(val, "1") == 0 ||
3912 strcasecmp(val, "Enable") == 0) {
3913 /* Enable AMSDU Aggregation */
3914 send_resp(dut, conn, SIGMA_ERROR,
3915 "ErrorCode,AMSDU aggregation not supported");
3916 return 0;
3917 }
3918 break;
3919 }
3920 }
3921
3922 val = get_param(cmd, "STBC_RX");
3923 if (val) {
3924 switch (get_driver_type()) {
3925 case DRIVER_ATHEROS:
3926 ath_sta_set_stbc(dut, intf, val);
3927 break;
Pradeep Reddy POTTETI4a1f6b32016-11-23 13:15:21 +05303928 case DRIVER_WCN:
3929 wcn_sta_set_stbc(dut, intf, val);
3930 break;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003931 default:
3932 send_resp(dut, conn, SIGMA_ERROR,
3933 "ErrorCode,STBC_RX not supported");
3934 return 0;
3935 }
3936 }
3937
3938 val = get_param(cmd, "WIDTH");
3939 if (val) {
3940 switch (get_driver_type()) {
3941 case DRIVER_WCN:
3942 if (wcn_sta_set_cts_width(dut, intf, val) < 0) {
3943 send_resp(dut, conn, SIGMA_ERROR,
3944 "ErrorCode,Failed to set WIDTH");
3945 return 0;
3946 }
3947 break;
3948 case DRIVER_ATHEROS:
3949 if (ath_set_width(dut, conn, intf, val) < 0)
3950 return 0;
3951 break;
3952 default:
3953 sigma_dut_print(dut, DUT_MSG_ERROR,
3954 "Setting WIDTH not supported");
3955 break;
3956 }
3957 }
3958
3959 val = get_param(cmd, "SMPS");
3960 if (val) {
3961 /* TODO: Dynamic/0, Static/1, No Limit/2 */
3962 send_resp(dut, conn, SIGMA_ERROR,
3963 "ErrorCode,SMPS not supported");
3964 return 0;
3965 }
3966
3967 val = get_param(cmd, "TXSP_STREAM");
3968 if (val) {
3969 switch (get_driver_type()) {
3970 case DRIVER_WCN:
3971 if (wcn_sta_set_sp_stream(dut, intf, val) < 0) {
3972 send_resp(dut, conn, SIGMA_ERROR,
3973 "ErrorCode,Failed to set TXSP_STREAM");
3974 return 0;
3975 }
3976 break;
3977 case DRIVER_ATHEROS:
3978 ath_sta_set_txsp_stream(dut, intf, val);
3979 break;
3980 default:
3981 sigma_dut_print(dut, DUT_MSG_ERROR,
3982 "Setting TXSP_STREAM not supported");
3983 break;
3984 }
3985 }
3986
3987 val = get_param(cmd, "RXSP_STREAM");
3988 if (val) {
3989 switch (get_driver_type()) {
3990 case DRIVER_WCN:
3991 if (wcn_sta_set_sp_stream(dut, intf, val) < 0) {
3992 send_resp(dut, conn, SIGMA_ERROR,
3993 "ErrorCode,Failed to set RXSP_STREAM");
3994 return 0;
3995 }
3996 break;
3997 case DRIVER_ATHEROS:
3998 ath_sta_set_rxsp_stream(dut, intf, val);
3999 break;
4000 default:
4001 sigma_dut_print(dut, DUT_MSG_ERROR,
4002 "Setting RXSP_STREAM not supported");
4003 break;
4004 }
4005 }
4006
4007 val = get_param(cmd, "DYN_BW_SGNL");
4008 if (val) {
Priyadharshini Gowthaman818afef2015-11-09 13:28:15 -08004009 switch (get_driver_type()) {
4010 case DRIVER_WCN:
Peng Xuc59afd32016-11-21 15:01:11 -08004011 if (strcasecmp(val, "enable") == 0) {
4012 snprintf(buf, sizeof(buf),
4013 "iwpriv %s cwmenable 1", intf);
4014 if (system(buf) != 0) {
4015 sigma_dut_print(dut, DUT_MSG_ERROR,
4016 "iwpriv cwmenable 1 failed");
4017 return 0;
4018 }
4019 } else if (strcasecmp(val, "disable") == 0) {
4020 snprintf(buf, sizeof(buf),
4021 "iwpriv %s cwmenable 0", intf);
4022 if (system(buf) != 0) {
4023 sigma_dut_print(dut, DUT_MSG_ERROR,
4024 "iwpriv cwmenable 0 failed");
4025 return 0;
4026 }
4027 } else {
4028 sigma_dut_print(dut, DUT_MSG_ERROR,
4029 "Unsupported DYN_BW_SGL");
4030 }
4031
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004032 snprintf(buf, sizeof(buf), "iwpriv %s cts_cbw 3", intf);
4033 if (system(buf) != 0) {
4034 sigma_dut_print(dut, DUT_MSG_ERROR,
4035 "Failed to set cts_cbw in DYN_BW_SGNL");
4036 return 0;
4037 }
Priyadharshini Gowthaman818afef2015-11-09 13:28:15 -08004038 break;
4039 case DRIVER_ATHEROS:
priyadharshini gowthamane5e25172015-12-08 14:53:48 -08004040 novap_reset(dut, intf);
Priyadharshini Gowthaman818afef2015-11-09 13:28:15 -08004041 ath_config_dyn_bw_sig(dut, intf, val);
4042 break;
4043 default:
4044 sigma_dut_print(dut, DUT_MSG_ERROR,
4045 "Failed to set DYN_BW_SGNL");
4046 break;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004047 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004048 }
4049
4050 val = get_param(cmd, "RTS_FORCE");
4051 if (val) {
priyadharshini gowthamane5e25172015-12-08 14:53:48 -08004052 novap_reset(dut, intf);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004053 if (strcasecmp(val, "Enable") == 0) {
4054 snprintf(buf, sizeof(buf), "iwconfig %s rts 64", intf);
Priyadharshini Gowthamanabdb2122015-11-17 11:52:19 +02004055 if (system(buf) != 0) {
4056 sigma_dut_print(dut, DUT_MSG_ERROR,
4057 "Failed to set RTS_FORCE 64");
4058 }
priyadharshini gowthaman270870e2015-12-09 10:10:23 -08004059 snprintf(buf, sizeof(buf),
4060 "wifitool %s beeliner_fw_test 100 1", intf);
4061 if (system(buf) != 0) {
4062 sigma_dut_print(dut, DUT_MSG_ERROR,
4063 "wifitool beeliner_fw_test 100 1 failed");
4064 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004065 } else if (strcasecmp(val, "Disable") == 0) {
4066 snprintf(buf, sizeof(buf), "iwconfig %s rts 2347",
4067 intf);
Priyadharshini Gowthamanabdb2122015-11-17 11:52:19 +02004068 if (system(buf) != 0) {
4069 sigma_dut_print(dut, DUT_MSG_ERROR,
4070 "Failed to set RTS_FORCE 2347");
4071 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004072 } else {
4073 send_resp(dut, conn, SIGMA_ERROR,
4074 "ErrorCode,RTS_FORCE value not supported");
4075 return 0;
4076 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004077 }
4078
4079 val = get_param(cmd, "CTS_WIDTH");
4080 if (val) {
4081 switch (get_driver_type()) {
4082 case DRIVER_WCN:
4083 if (wcn_sta_set_cts_width(dut, intf, val) < 0) {
4084 send_resp(dut, conn, SIGMA_ERROR,
4085 "ErrorCode,Failed to set CTS_WIDTH");
4086 return 0;
4087 }
4088 break;
4089 case DRIVER_ATHEROS:
4090 ath_set_cts_width(dut, intf, val);
4091 break;
4092 default:
4093 sigma_dut_print(dut, DUT_MSG_ERROR,
4094 "Setting CTS_WIDTH not supported");
4095 break;
4096 }
4097 }
4098
4099 val = get_param(cmd, "BW_SGNL");
4100 if (val) {
4101 if (strcasecmp(val, "Enable") == 0) {
4102 snprintf(buf, sizeof(buf), "iwpriv %s cwmenable 1",
4103 intf);
4104 } else if (strcasecmp(val, "Disable") == 0) {
4105 /* TODO: Disable */
4106 buf[0] = '\0';
4107 } else {
4108 send_resp(dut, conn, SIGMA_ERROR,
4109 "ErrorCode,BW_SGNL value not supported");
4110 return 0;
4111 }
4112
4113 if (buf[0] != '\0' && system(buf) != 0) {
4114 sigma_dut_print(dut, DUT_MSG_ERROR,
4115 "Failed to set BW_SGNL");
4116 }
4117 }
4118
4119 val = get_param(cmd, "Band");
4120 if (val) {
4121 if (strcmp(val, "2.4") == 0 || strcmp(val, "5") == 0) {
4122 /* STA supports all bands by default */
4123 } else {
4124 send_resp(dut, conn, SIGMA_ERROR,
4125 "ErrorCode,Unsupported Band");
4126 return 0;
4127 }
4128 }
4129
4130 val = get_param(cmd, "zero_crc");
4131 if (val) {
4132 switch (get_driver_type()) {
4133 case DRIVER_ATHEROS:
4134 ath_set_zero_crc(dut, val);
4135 break;
4136 default:
4137 break;
4138 }
4139 }
4140
4141 return 1;
4142}
4143
4144
4145static int sta_set_60g_common(struct sigma_dut *dut, struct sigma_conn *conn,
4146 struct sigma_cmd *cmd)
4147{
4148 const char *val;
4149 char buf[100];
4150
4151 val = get_param(cmd, "MSDUSize");
4152 if (val) {
4153 int mtu;
4154
4155 dut->amsdu_size = atoi(val);
4156 if (dut->amsdu_size > IEEE80211_MAX_DATA_LEN_DMG ||
4157 dut->amsdu_size < IEEE80211_SNAP_LEN_DMG) {
4158 sigma_dut_print(dut, DUT_MSG_ERROR,
4159 "MSDUSize %d is above max %d or below min %d",
4160 dut->amsdu_size,
4161 IEEE80211_MAX_DATA_LEN_DMG,
4162 IEEE80211_SNAP_LEN_DMG);
4163 dut->amsdu_size = 0;
4164 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
4165 }
4166
4167 mtu = dut->amsdu_size - IEEE80211_SNAP_LEN_DMG;
4168 sigma_dut_print(dut, DUT_MSG_DEBUG,
4169 "Setting amsdu_size to %d", mtu);
4170 snprintf(buf, sizeof(buf), "ifconfig %s mtu %d",
4171 get_station_ifname(), mtu);
4172
4173 if (system(buf) != 0) {
4174 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to set %s",
4175 buf);
4176 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
4177 }
4178 }
4179
4180 val = get_param(cmd, "BAckRcvBuf");
4181 if (val) {
4182 dut->back_rcv_buf = atoi(val);
4183 if (dut->back_rcv_buf == 0) {
4184 sigma_dut_print(dut, DUT_MSG_ERROR,
4185 "Failed to convert %s or value is 0",
4186 val);
4187 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
4188 }
4189
4190 sigma_dut_print(dut, DUT_MSG_DEBUG,
4191 "Setting BAckRcvBuf to %s", val);
4192 }
4193
4194 return SIGMA_DUT_SUCCESS_CALLER_SEND_STATUS;
4195}
4196
4197
4198static int sta_pcp_start(struct sigma_dut *dut, struct sigma_conn *conn,
4199 struct sigma_cmd *cmd)
4200{
4201 int net_id;
4202 char *ifname;
4203 const char *val;
4204 char buf[100];
4205
4206 dut->mode = SIGMA_MODE_STATION;
4207 ifname = get_main_ifname();
4208 if (wpa_command(ifname, "PING") != 0) {
4209 sigma_dut_print(dut, DUT_MSG_ERROR, "Supplicant not running");
4210 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
4211 }
4212
4213 wpa_command(ifname, "FLUSH");
4214 net_id = add_network_common(dut, conn, ifname, cmd);
4215 if (net_id < 0) {
4216 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to add network");
4217 return net_id;
4218 }
4219
4220 /* TODO: mode=2 for the AP; in the future, replace for mode PCP */
4221 if (set_network(ifname, net_id, "mode", "2") < 0) {
4222 sigma_dut_print(dut, DUT_MSG_ERROR,
4223 "Failed to set supplicant network mode");
4224 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
4225 }
4226
4227 sigma_dut_print(dut, DUT_MSG_DEBUG,
4228 "Supplicant set network with mode 2");
4229
4230 val = get_param(cmd, "Security");
4231 if (val && strcasecmp(val, "OPEN") == 0) {
4232 dut->ap_key_mgmt = AP_OPEN;
4233 if (set_network(ifname, net_id, "key_mgmt", "NONE") < 0) {
4234 sigma_dut_print(dut, DUT_MSG_ERROR,
4235 "Failed to set supplicant to %s security",
4236 val);
4237 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
4238 }
4239 } else if (val && strcasecmp(val, "WPA2-PSK") == 0) {
4240 dut->ap_key_mgmt = AP_WPA2_PSK;
4241 if (set_network(ifname, net_id, "key_mgmt", "WPA-PSK") < 0) {
4242 sigma_dut_print(dut, DUT_MSG_ERROR,
4243 "Failed to set supplicant to %s security",
4244 val);
4245 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
4246 }
4247
4248 if (set_network(ifname, net_id, "proto", "RSN") < 0) {
4249 sigma_dut_print(dut, DUT_MSG_ERROR,
4250 "Failed to set supplicant to proto RSN");
4251 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
4252 }
4253 } else if (val) {
4254 sigma_dut_print(dut, DUT_MSG_ERROR,
4255 "Requested Security %s is not supported on 60GHz",
4256 val);
4257 return SIGMA_DUT_INVALID_CALLER_SEND_STATUS;
4258 }
4259
4260 val = get_param(cmd, "Encrypt");
4261 if (val && strcasecmp(val, "AES-GCMP") == 0) {
4262 if (set_network(ifname, net_id, "pairwise", "GCMP") < 0) {
4263 sigma_dut_print(dut, DUT_MSG_ERROR,
4264 "Failed to set supplicant to pairwise GCMP");
4265 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
4266 }
4267 if (set_network(ifname, net_id, "group", "GCMP") < 0) {
4268 sigma_dut_print(dut, DUT_MSG_ERROR,
4269 "Failed to set supplicant to group GCMP");
4270 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
4271 }
4272 } else if (val) {
4273 sigma_dut_print(dut, DUT_MSG_ERROR,
4274 "Requested Encrypt %s is not supported on 60 GHz",
4275 val);
4276 return SIGMA_DUT_INVALID_CALLER_SEND_STATUS;
4277 }
4278
4279 val = get_param(cmd, "PSK");
4280 if (val && set_network_quoted(ifname, net_id, "psk", val) < 0) {
4281 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to set psk %s",
4282 val);
4283 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
4284 }
4285
4286 /* Convert 60G channel to freq */
4287 switch (dut->ap_channel) {
4288 case 1:
4289 val = "58320";
4290 break;
4291 case 2:
4292 val = "60480";
4293 break;
4294 case 3:
4295 val = "62640";
4296 break;
4297 default:
4298 sigma_dut_print(dut, DUT_MSG_ERROR,
4299 "Failed to configure channel %d. Not supported",
4300 dut->ap_channel);
4301 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
4302 }
4303
4304 if (set_network(ifname, net_id, "frequency", val) < 0) {
4305 sigma_dut_print(dut, DUT_MSG_ERROR,
4306 "Failed to set supplicant network frequency");
4307 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
4308 }
4309
4310 sigma_dut_print(dut, DUT_MSG_DEBUG,
4311 "Supplicant set network with frequency");
4312
4313 snprintf(buf, sizeof(buf), "SELECT_NETWORK %d", net_id);
4314 if (wpa_command(ifname, buf) < 0) {
4315 sigma_dut_print(dut, DUT_MSG_INFO,
4316 "Failed to select network id %d on %s",
4317 net_id, ifname);
4318 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
4319 }
4320
4321 sigma_dut_print(dut, DUT_MSG_DEBUG, "Selected network");
4322
4323 return SIGMA_DUT_SUCCESS_CALLER_SEND_STATUS;
4324}
4325
4326
Lior David67543f52017-01-03 19:04:22 +02004327static int wil6210_set_abft_len(struct sigma_dut *dut, int abft_len)
4328{
4329 char buf[128], fname[128];
4330 FILE *f;
4331
4332 if (wil6210_get_debugfs_dir(dut, buf, sizeof(buf))) {
4333 sigma_dut_print(dut, DUT_MSG_ERROR,
4334 "failed to get wil6210 debugfs dir");
4335 return -1;
4336 }
4337
4338 snprintf(fname, sizeof(fname), "%s/abft_len", buf);
4339 f = fopen(fname, "w");
4340 if (!f) {
4341 sigma_dut_print(dut, DUT_MSG_ERROR,
4342 "failed to open: %s", fname);
4343 return -1;
4344 }
4345
4346 fprintf(f, "%d\n", abft_len);
4347 fclose(f);
4348
4349 return 0;
4350}
4351
4352
4353static int sta_set_60g_abft_len(struct sigma_dut *dut, struct sigma_conn *conn,
4354 int abft_len)
4355{
4356 switch (get_driver_type()) {
4357 case DRIVER_WIL6210:
4358 return wil6210_set_abft_len(dut, abft_len);
4359 default:
4360 sigma_dut_print(dut, DUT_MSG_ERROR,
4361 "set abft_len not supported");
4362 return -1;
4363 }
4364}
4365
4366
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004367static int sta_set_60g_pcp(struct sigma_dut *dut, struct sigma_conn *conn,
4368 struct sigma_cmd *cmd)
4369{
4370 const char *val;
Lior David67543f52017-01-03 19:04:22 +02004371 unsigned int abft_len = 1; /* default is one slot */
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004372
4373 if (dut->dev_role != DEVROLE_PCP) {
4374 send_resp(dut, conn, SIGMA_INVALID,
4375 "ErrorCode,Invalid DevRole");
4376 return 0;
4377 }
4378
4379 val = get_param(cmd, "SSID");
4380 if (val) {
4381 if (strlen(val) > sizeof(dut->ap_ssid) - 1) {
4382 send_resp(dut, conn, SIGMA_INVALID,
4383 "ErrorCode,Invalid SSID");
4384 return -1;
4385 }
4386
Peng Xub8fc5cc2017-05-10 17:27:28 -07004387 strlcpy(dut->ap_ssid, val, sizeof(dut->ap_ssid));
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004388 }
4389
4390 val = get_param(cmd, "CHANNEL");
4391 if (val) {
4392 const char *pos;
4393
4394 dut->ap_channel = atoi(val);
4395 pos = strchr(val, ';');
4396 if (pos) {
4397 pos++;
4398 dut->ap_channel_1 = atoi(pos);
4399 }
4400 }
4401
4402 switch (dut->ap_channel) {
4403 case 1:
4404 case 2:
4405 case 3:
4406 break;
4407 default:
4408 sigma_dut_print(dut, DUT_MSG_ERROR,
4409 "Channel %d is not supported", dut->ap_channel);
4410 send_resp(dut, conn, SIGMA_ERROR,
4411 "Requested channel is not supported");
4412 return -1;
4413 }
4414
4415 val = get_param(cmd, "BCNINT");
4416 if (val)
4417 dut->ap_bcnint = atoi(val);
4418
4419
4420 val = get_param(cmd, "ExtSchIE");
4421 if (val) {
4422 send_resp(dut, conn, SIGMA_ERROR,
4423 "ErrorCode,ExtSchIE is not supported yet");
4424 return -1;
4425 }
4426
4427 val = get_param(cmd, "AllocType");
4428 if (val) {
4429 send_resp(dut, conn, SIGMA_ERROR,
4430 "ErrorCode,AllocType is not supported yet");
4431 return -1;
4432 }
4433
4434 val = get_param(cmd, "PercentBI");
4435 if (val) {
4436 send_resp(dut, conn, SIGMA_ERROR,
4437 "ErrorCode,PercentBI is not supported yet");
4438 return -1;
4439 }
4440
4441 val = get_param(cmd, "CBAPOnly");
4442 if (val) {
4443 send_resp(dut, conn, SIGMA_ERROR,
4444 "ErrorCode,CBAPOnly is not supported yet");
4445 return -1;
4446 }
4447
4448 val = get_param(cmd, "AMPDU");
4449 if (val) {
4450 if (strcasecmp(val, "Enable") == 0)
4451 dut->ap_ampdu = 1;
4452 else if (strcasecmp(val, "Disable") == 0)
4453 dut->ap_ampdu = 2;
4454 else {
4455 send_resp(dut, conn, SIGMA_ERROR,
4456 "ErrorCode,AMPDU value is not Enable nor Disabled");
4457 return -1;
4458 }
4459 }
4460
4461 val = get_param(cmd, "AMSDU");
4462 if (val) {
4463 if (strcasecmp(val, "Enable") == 0)
4464 dut->ap_amsdu = 1;
4465 else if (strcasecmp(val, "Disable") == 0)
4466 dut->ap_amsdu = 2;
4467 }
4468
4469 val = get_param(cmd, "NumMSDU");
4470 if (val) {
4471 send_resp(dut, conn, SIGMA_ERROR,
4472 "ErrorCode, NumMSDU is not supported yet");
4473 return -1;
4474 }
4475
4476 val = get_param(cmd, "ABFTLRang");
4477 if (val) {
4478 sigma_dut_print(dut, DUT_MSG_DEBUG,
Lior David67543f52017-01-03 19:04:22 +02004479 "ABFTLRang parameter %s", val);
4480 if (strcmp(val, "Gt1") == 0)
4481 abft_len = 2; /* 2 slots in this case */
4482 }
4483
4484 if (sta_set_60g_abft_len(dut, conn, abft_len)) {
4485 send_resp(dut, conn, SIGMA_ERROR,
4486 "ErrorCode, Can't set ABFT length");
4487 return -1;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004488 }
4489
4490 if (sta_pcp_start(dut, conn, cmd) < 0) {
4491 send_resp(dut, conn, SIGMA_ERROR,
4492 "ErrorCode, Can't start PCP role");
4493 return -1;
4494 }
4495
4496 return sta_set_60g_common(dut, conn, cmd);
4497}
4498
4499
4500static int sta_set_60g_sta(struct sigma_dut *dut, struct sigma_conn *conn,
4501 struct sigma_cmd *cmd)
4502{
4503 const char *val = get_param(cmd, "DiscoveryMode");
4504
4505 if (dut->dev_role != DEVROLE_STA) {
4506 send_resp(dut, conn, SIGMA_INVALID,
4507 "ErrorCode,Invalid DevRole");
4508 return 0;
4509 }
4510
4511 if (val) {
4512 sigma_dut_print(dut, DUT_MSG_DEBUG, "Discovery: %s", val);
4513 /* Ignore Discovery mode till Driver expose API. */
4514#if 0
4515 if (strcasecmp(val, "1") == 0) {
4516 send_resp(dut, conn, SIGMA_INVALID,
4517 "ErrorCode,DiscoveryMode 1 not supported");
4518 return 0;
4519 }
4520
4521 if (strcasecmp(val, "0") == 0) {
4522 /* OK */
4523 } else {
4524 send_resp(dut, conn, SIGMA_INVALID,
4525 "ErrorCode,DiscoveryMode not supported");
4526 return 0;
4527 }
4528#endif
4529 }
4530
4531 if (start_sta_mode(dut) != 0)
4532 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
4533 return sta_set_60g_common(dut, conn, cmd);
4534}
4535
4536
4537static int cmd_sta_disconnect(struct sigma_dut *dut, struct sigma_conn *conn,
4538 struct sigma_cmd *cmd)
4539{
4540 const char *intf = get_param(cmd, "Interface");
4541 disconnect_station(dut);
4542 /* Try to ignore old scan results to avoid HS 2.0R2 test case failures
4543 * due to cached results. */
4544 wpa_command(intf, "SET ignore_old_scan_res 1");
4545 wpa_command(intf, "BSS_FLUSH");
4546 return 1;
4547}
4548
4549
4550static int cmd_sta_reassoc(struct sigma_dut *dut, struct sigma_conn *conn,
4551 struct sigma_cmd *cmd)
4552{
4553 const char *intf = get_param(cmd, "Interface");
4554 const char *bssid = get_param(cmd, "bssid");
4555 const char *val = get_param(cmd, "CHANNEL");
4556 struct wpa_ctrl *ctrl;
4557 char buf[100];
4558 int res;
4559 int chan = 0;
Ashwini Patil467efef2017-05-25 12:18:27 +05304560 int status = 0;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004561
4562 if (bssid == NULL) {
4563 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Missing bssid "
4564 "argument");
4565 return 0;
4566 }
4567
4568 if (val)
4569 chan = atoi(val);
4570
4571 if (wifi_chip_type != DRIVER_WCN && wifi_chip_type != DRIVER_AR6003) {
4572 /* The current network may be from sta_associate or
4573 * sta_hs2_associate
4574 */
4575 if (set_network(intf, dut->infra_network_id, "bssid", bssid) <
4576 0 ||
4577 set_network(intf, 0, "bssid", bssid) < 0)
4578 return -2;
4579 }
4580
4581 ctrl = open_wpa_mon(intf);
4582 if (ctrl == NULL) {
4583 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to open "
4584 "wpa_supplicant monitor connection");
4585 return -1;
4586 }
4587
4588 if (wifi_chip_type == DRIVER_WCN) {
4589#ifdef ANDROID
Ashwini Patil4c8158f2017-05-25 12:49:21 +05304590 if (chan) {
4591 unsigned int freq;
4592
4593 freq = channel_to_freq(chan);
4594 if (!freq) {
4595 sigma_dut_print(dut, DUT_MSG_ERROR,
4596 "Invalid channel number provided: %d",
4597 chan);
4598 send_resp(dut, conn, SIGMA_INVALID,
4599 "ErrorCode,Invalid channel number");
4600 goto close_mon_conn;
4601 }
4602 res = snprintf(buf, sizeof(buf),
4603 "SCAN TYPE=ONLY freq=%d", freq);
4604 } else {
4605 res = snprintf(buf, sizeof(buf), "SCAN TYPE=ONLY");
4606 }
4607 if (res < 0 || res >= (int) sizeof(buf)) {
4608 send_resp(dut, conn, SIGMA_ERROR,
4609 "ErrorCode,snprintf failed");
4610 goto close_mon_conn;
4611 }
4612 if (wpa_command(intf, buf) < 0) {
4613 sigma_dut_print(dut, DUT_MSG_INFO,
4614 "Failed to start scan");
4615 send_resp(dut, conn, SIGMA_ERROR,
4616 "ErrorCode,scan failed");
4617 goto close_mon_conn;
4618 }
4619
4620 res = get_wpa_cli_event(dut, ctrl, "CTRL-EVENT-SCAN-RESULTS",
4621 buf, sizeof(buf));
4622 if (res < 0) {
4623 sigma_dut_print(dut, DUT_MSG_INFO,
4624 "Scan did not complete");
4625 send_resp(dut, conn, SIGMA_ERROR,
4626 "ErrorCode,scan did not complete");
4627 goto close_mon_conn;
4628 }
4629
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004630 if (set_network(intf, dut->infra_network_id, "bssid", "any")
4631 < 0) {
4632 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to set "
4633 "bssid to any during FASTREASSOC");
Ashwini Patil467efef2017-05-25 12:18:27 +05304634 status = -2;
4635 goto close_mon_conn;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004636 }
4637 res = snprintf(buf, sizeof(buf), "DRIVER FASTREASSOC %s %d",
4638 bssid, chan);
4639 if (res > 0 && res < (int) sizeof(buf))
4640 res = wpa_command(intf, buf);
4641
4642 if (res < 0 || res >= (int) sizeof(buf)) {
4643 send_resp(dut, conn, SIGMA_ERROR,
4644 "errorCode,Failed to run DRIVER FASTREASSOC");
Ashwini Patil467efef2017-05-25 12:18:27 +05304645 goto close_mon_conn;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004646 }
4647#else /* ANDROID */
4648 sigma_dut_print(dut, DUT_MSG_DEBUG,
4649 "Reassoc using iwpriv - skip chan=%d info",
4650 chan);
4651 snprintf(buf, sizeof(buf), "iwpriv %s reassoc", intf);
4652 if (system(buf) != 0) {
4653 sigma_dut_print(dut, DUT_MSG_ERROR, "%s failed", buf);
Ashwini Patil467efef2017-05-25 12:18:27 +05304654 status = -2;
4655 goto close_mon_conn;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004656 }
4657#endif /* ANDROID */
4658 sigma_dut_print(dut, DUT_MSG_INFO,
4659 "sta_reassoc: Run %s successful", buf);
4660 } else if (wpa_command(intf, "REASSOCIATE")) {
4661 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to "
4662 "request reassociation");
Ashwini Patil467efef2017-05-25 12:18:27 +05304663 goto close_mon_conn;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004664 }
4665
4666 res = get_wpa_cli_event(dut, ctrl, "CTRL-EVENT-CONNECTED",
4667 buf, sizeof(buf));
Ashwini Patil467efef2017-05-25 12:18:27 +05304668 if (res < 0) {
4669 sigma_dut_print(dut, DUT_MSG_INFO, "Connection did not complete");
4670 status = -1;
4671 goto close_mon_conn;
4672 }
4673 status = 1;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004674
Ashwini Patil467efef2017-05-25 12:18:27 +05304675close_mon_conn:
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004676 wpa_ctrl_detach(ctrl);
4677 wpa_ctrl_close(ctrl);
Ashwini Patil467efef2017-05-25 12:18:27 +05304678 return status;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004679}
4680
4681
4682static void hs2_clear_credentials(const char *intf)
4683{
4684 wpa_command(intf, "REMOVE_CRED all");
4685}
4686
4687
Lior Davidcc88b562017-01-03 18:52:09 +02004688#ifdef __linux__
4689static int wil6210_get_aid(struct sigma_dut *dut, const char *bssid,
4690 unsigned int *aid)
4691{
Lior David0fe101e2017-03-09 16:09:50 +02004692 const char *pattern = "AID[ \t]+([0-9]+)";
Lior Davidcc88b562017-01-03 18:52:09 +02004693
Lior David0fe101e2017-03-09 16:09:50 +02004694 return wil6210_get_sta_info_field(dut, bssid, pattern, aid);
Lior Davidcc88b562017-01-03 18:52:09 +02004695}
4696#endif /* __linux__ */
4697
4698
4699static int sta_get_aid_60g(struct sigma_dut *dut, const char *bssid,
4700 unsigned int *aid)
4701{
4702 switch (get_driver_type()) {
4703#ifdef __linux__
4704 case DRIVER_WIL6210:
4705 return wil6210_get_aid(dut, bssid, aid);
4706#endif /* __linux__ */
4707 default:
4708 sigma_dut_print(dut, DUT_MSG_ERROR, "get AID not supported");
4709 return -1;
4710 }
4711}
4712
4713
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004714static int sta_get_parameter_60g(struct sigma_dut *dut, struct sigma_conn *conn,
4715 struct sigma_cmd *cmd)
4716{
4717 char buf[MAX_CMD_LEN];
4718 char bss_list[MAX_CMD_LEN];
4719 const char *parameter = get_param(cmd, "Parameter");
4720
4721 if (parameter == NULL)
4722 return -1;
4723
Lior Davidcc88b562017-01-03 18:52:09 +02004724 if (strcasecmp(parameter, "AID") == 0) {
4725 unsigned int aid = 0;
4726 char bssid[20];
4727
4728 if (get_wpa_status(get_station_ifname(), "bssid",
4729 bssid, sizeof(bssid)) < 0) {
4730 sigma_dut_print(dut, DUT_MSG_ERROR,
4731 "could not get bssid");
4732 return -2;
4733 }
4734
4735 if (sta_get_aid_60g(dut, bssid, &aid))
4736 return -2;
4737
4738 snprintf(buf, sizeof(buf), "aid,%d", aid);
4739 sigma_dut_print(dut, DUT_MSG_INFO, "%s", buf);
4740 send_resp(dut, conn, SIGMA_COMPLETE, buf);
4741 return 0;
4742 }
4743
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004744 if (strcasecmp(parameter, "DiscoveredDevList") == 0) {
4745 char *bss_line;
4746 char *bss_id = NULL;
4747 const char *ifname = get_param(cmd, "Interface");
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05304748 char *saveptr;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004749
4750 if (ifname == NULL) {
4751 sigma_dut_print(dut, DUT_MSG_INFO,
4752 "For get DiscoveredDevList need Interface name.");
4753 return -1;
4754 }
4755
4756 /*
4757 * Use "BSS RANGE=ALL MASK=0x2" which provides a list
4758 * of BSSIDs in "bssid=<BSSID>\n"
4759 */
4760 if (wpa_command_resp(ifname, "BSS RANGE=ALL MASK=0x2",
4761 bss_list,
4762 sizeof(bss_list)) < 0) {
4763 sigma_dut_print(dut, DUT_MSG_ERROR,
4764 "Failed to get bss list");
4765 return -1;
4766 }
4767
4768 sigma_dut_print(dut, DUT_MSG_DEBUG,
4769 "bss list for ifname:%s is:%s",
4770 ifname, bss_list);
4771
4772 snprintf(buf, sizeof(buf), "DeviceList");
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05304773 bss_line = strtok_r(bss_list, "\n", &saveptr);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004774 while (bss_line) {
4775 if (sscanf(bss_line, "bssid=%ms", &bss_id) > 0 &&
4776 bss_id) {
4777 int len;
4778
4779 len = snprintf(buf + strlen(buf),
4780 sizeof(buf) - strlen(buf),
4781 ",%s", bss_id);
4782 free(bss_id);
4783 bss_id = NULL;
4784 if (len < 0) {
4785 sigma_dut_print(dut,
4786 DUT_MSG_ERROR,
4787 "Failed to read BSSID");
4788 send_resp(dut, conn, SIGMA_ERROR,
4789 "ErrorCode,Failed to read BSS ID");
4790 return 0;
4791 }
4792
4793 if ((size_t) len >= sizeof(buf) - strlen(buf)) {
4794 sigma_dut_print(dut,
4795 DUT_MSG_ERROR,
4796 "Response buf too small for list");
4797 send_resp(dut, conn,
4798 SIGMA_ERROR,
4799 "ErrorCode,Response buf too small for list");
4800 return 0;
4801 }
4802 }
4803
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05304804 bss_line = strtok_r(NULL, "\n", &saveptr);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004805 }
4806
4807 sigma_dut_print(dut, DUT_MSG_INFO, "DiscoveredDevList is %s",
4808 buf);
4809 send_resp(dut, conn, SIGMA_COMPLETE, buf);
4810 return 0;
4811 }
4812
4813 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Unsupported parameter");
4814 return 0;
4815}
4816
4817
4818static int cmd_sta_get_parameter(struct sigma_dut *dut, struct sigma_conn *conn,
4819 struct sigma_cmd *cmd)
4820{
4821 const char *program = get_param(cmd, "Program");
4822
4823 if (program == NULL)
4824 return -1;
4825
4826 if (strcasecmp(program, "P2PNFC") == 0)
4827 return p2p_cmd_sta_get_parameter(dut, conn, cmd);
4828
4829 if (strcasecmp(program, "60ghz") == 0)
4830 return sta_get_parameter_60g(dut, conn, cmd);
4831
4832#ifdef ANDROID_NAN
4833 if (strcasecmp(program, "NAN") == 0)
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -07004834 return nan_cmd_sta_get_parameter(dut, conn, cmd);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004835#endif /* ANDROID_NAN */
4836
Amarnath Hullur Subramanyam9c381f52017-03-17 00:04:41 -07004837#ifdef MIRACAST
4838 if (strcasecmp(program, "WFD") == 0 ||
4839 strcasecmp(program, "DisplayR2") == 0)
4840 return miracast_cmd_sta_get_parameter(dut, conn, cmd);
4841#endif /* MIRACAST */
4842
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004843 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Unsupported parameter");
4844 return 0;
4845}
4846
4847
4848static void sta_reset_default_ath(struct sigma_dut *dut, const char *intf,
4849 const char *type)
4850{
4851 char buf[100];
4852
4853 if (dut->program == PROGRAM_VHT) {
4854 snprintf(buf, sizeof(buf), "iwpriv %s chwidth 2", intf);
4855 if (system(buf) != 0) {
4856 sigma_dut_print(dut, DUT_MSG_ERROR,
4857 "iwpriv %s chwidth failed", intf);
4858 }
4859
4860 snprintf(buf, sizeof(buf), "iwpriv %s mode 11ACVHT80", intf);
4861 if (system(buf) != 0) {
4862 sigma_dut_print(dut, DUT_MSG_ERROR,
4863 "iwpriv %s mode 11ACVHT80 failed",
4864 intf);
4865 }
4866
4867 snprintf(buf, sizeof(buf), "iwpriv %s vhtmcs -1", intf);
4868 if (system(buf) != 0) {
4869 sigma_dut_print(dut, DUT_MSG_ERROR,
4870 "iwpriv %s vhtmcs -1 failed", intf);
4871 }
4872 }
4873
4874 if (dut->program == PROGRAM_HT) {
4875 snprintf(buf, sizeof(buf), "iwpriv %s chwidth 0", intf);
4876 if (system(buf) != 0) {
4877 sigma_dut_print(dut, DUT_MSG_ERROR,
4878 "iwpriv %s chwidth failed", intf);
4879 }
4880
4881 snprintf(buf, sizeof(buf), "iwpriv %s mode 11naht40", intf);
4882 if (system(buf) != 0) {
4883 sigma_dut_print(dut, DUT_MSG_ERROR,
4884 "iwpriv %s mode 11naht40 failed",
4885 intf);
4886 }
4887
4888 snprintf(buf, sizeof(buf), "iwpriv %s set11NRates 0", intf);
4889 if (system(buf) != 0) {
4890 sigma_dut_print(dut, DUT_MSG_ERROR,
4891 "iwpriv set11NRates failed");
4892 }
4893 }
4894
4895 if (dut->program == PROGRAM_VHT || dut->program == PROGRAM_HT) {
4896 snprintf(buf, sizeof(buf), "iwpriv %s powersave 0", intf);
4897 if (system(buf) != 0) {
4898 sigma_dut_print(dut, DUT_MSG_ERROR,
4899 "disabling powersave failed");
4900 }
4901
4902 /* Reset CTS width */
4903 snprintf(buf, sizeof(buf), "wifitool %s beeliner_fw_test 54 0",
4904 intf);
4905 if (system(buf) != 0) {
4906 sigma_dut_print(dut, DUT_MSG_ERROR,
4907 "wifitool %s beeliner_fw_test 54 0 failed",
4908 intf);
4909 }
4910
4911 /* Enable Dynamic Bandwidth signalling by default */
4912 snprintf(buf, sizeof(buf), "iwpriv %s cwmenable 1", intf);
4913 if (system(buf) != 0) {
4914 sigma_dut_print(dut, DUT_MSG_ERROR,
4915 "iwpriv %s cwmenable 1 failed", intf);
4916 }
4917
4918 snprintf(buf, sizeof(buf), "iwconfig %s rts 2347", intf);
4919 if (system(buf) != 0) {
4920 sigma_dut_print(dut, DUT_MSG_ERROR,
4921 "iwpriv rts failed");
4922 }
4923 }
4924
4925 if (type && strcasecmp(type, "Testbed") == 0) {
4926 dut->testbed_flag_txsp = 1;
4927 dut->testbed_flag_rxsp = 1;
4928 /* STA has to set spatial stream to 2 per Appendix H */
4929 snprintf(buf, sizeof(buf), "iwpriv %s vht_mcsmap 0xfff0", intf);
4930 if (system(buf) != 0) {
4931 sigma_dut_print(dut, DUT_MSG_ERROR,
4932 "iwpriv vht_mcsmap failed");
4933 }
4934
4935 /* Disable LDPC per Appendix H */
4936 snprintf(buf, sizeof(buf), "iwpriv %s ldpc 0", intf);
4937 if (system(buf) != 0) {
4938 sigma_dut_print(dut, DUT_MSG_ERROR,
4939 "iwpriv %s ldpc 0 failed", intf);
4940 }
4941
4942 snprintf(buf, sizeof(buf), "iwpriv %s amsdu 1", intf);
4943 if (system(buf) != 0) {
4944 sigma_dut_print(dut, DUT_MSG_ERROR,
4945 "iwpriv amsdu failed");
4946 }
4947
4948 /* TODO: Disable STBC 2x1 transmit and receive */
4949 snprintf(buf, sizeof(buf), "iwpriv %s tx_stbc 0", intf);
4950 if (system(buf) != 0) {
4951 sigma_dut_print(dut, DUT_MSG_ERROR,
4952 "Disable tx_stbc 0 failed");
4953 }
4954
4955 snprintf(buf, sizeof(buf), "iwpriv %s rx_stbc 0", intf);
4956 if (system(buf) != 0) {
4957 sigma_dut_print(dut, DUT_MSG_ERROR,
4958 "Disable rx_stbc 0 failed");
4959 }
4960
4961 /* STA has to disable Short GI per Appendix H */
4962 snprintf(buf, sizeof(buf), "iwpriv %s shortgi 0", intf);
4963 if (system(buf) != 0) {
4964 sigma_dut_print(dut, DUT_MSG_ERROR,
4965 "iwpriv %s shortgi 0 failed", intf);
4966 }
4967 }
4968
4969 if (type && strcasecmp(type, "DUT") == 0) {
4970 snprintf(buf, sizeof(buf), "iwpriv %s nss 3", intf);
4971 if (system(buf) != 0) {
4972 sigma_dut_print(dut, DUT_MSG_ERROR,
4973 "iwpriv %s nss 3 failed", intf);
4974 }
4975
4976 snprintf(buf, sizeof(buf), "iwpriv %s shortgi 1", intf);
4977 if (system(buf) != 0) {
4978 sigma_dut_print(dut, DUT_MSG_ERROR,
4979 "iwpriv %s shortgi 1 failed", intf);
4980 }
4981 }
4982}
4983
4984
4985static int cmd_sta_reset_default(struct sigma_dut *dut,
4986 struct sigma_conn *conn,
4987 struct sigma_cmd *cmd)
4988{
4989 int cmd_sta_p2p_reset(struct sigma_dut *dut, struct sigma_conn *conn,
4990 struct sigma_cmd *cmd);
4991 const char *intf = get_param(cmd, "Interface");
4992 const char *type;
Amarnath Hullur Subramanyam9c381f52017-03-17 00:04:41 -07004993 const char *program = get_param(cmd, "program");
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004994
Amarnath Hullur Subramanyam9c381f52017-03-17 00:04:41 -07004995 if (!program)
4996 program = get_param(cmd, "prog");
4997 dut->program = sigma_program_to_enum(program);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004998 dut->device_type = STA_unknown;
4999 type = get_param(cmd, "type");
5000 if (type && strcasecmp(type, "Testbed") == 0)
5001 dut->device_type = STA_testbed;
5002 if (type && strcasecmp(type, "DUT") == 0)
5003 dut->device_type = STA_dut;
5004
5005 if (dut->program == PROGRAM_TDLS) {
5006 /* Clear TDLS testing mode */
5007 wpa_command(intf, "SET tdls_disabled 0");
5008 wpa_command(intf, "SET tdls_testing 0");
5009 dut->no_tpk_expiration = 0;
Pradeep Reddy POTTETI8ce2a232016-10-28 12:17:32 +05305010 if (get_driver_type() == DRIVER_WCN) {
5011 /* Enable the WCN driver in TDLS Explicit trigger mode
5012 */
5013 wpa_command(intf, "SET tdls_external_control 0");
5014 wpa_command(intf, "SET tdls_trigger_control 0");
5015 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005016 }
5017
Amarnath Hullur Subramanyam9c381f52017-03-17 00:04:41 -07005018#ifdef MIRACAST
5019 if (dut->program == PROGRAM_WFD ||
5020 dut->program == PROGRAM_DISPLAYR2)
5021 miracast_sta_reset_default(dut, conn, cmd);
5022#endif /* MIRACAST */
5023
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005024 switch (get_driver_type()) {
5025 case DRIVER_ATHEROS:
5026 sta_reset_default_ath(dut, intf, type);
5027 break;
5028 default:
5029 break;
5030 }
5031
5032#ifdef ANDROID_NAN
5033 if (dut->program == PROGRAM_NAN)
5034 nan_cmd_sta_reset_default(dut, conn, cmd);
5035#endif /* ANDROID_NAN */
5036
5037 if (dut->program == PROGRAM_HS2_R2) {
5038 unlink("SP/wi-fi.org/pps.xml");
5039 if (system("rm -r SP/*") != 0) {
5040 }
5041 unlink("next-client-cert.pem");
5042 unlink("next-client-key.pem");
5043 }
5044
5045 if (dut->program == PROGRAM_60GHZ) {
5046 const char *dev_role = get_param(cmd, "DevRole");
5047
5048 if (!dev_role) {
5049 send_resp(dut, conn, SIGMA_ERROR,
5050 "errorCode,Missing DevRole argument");
5051 return 0;
5052 }
5053
5054 if (strcasecmp(dev_role, "STA") == 0)
5055 dut->dev_role = DEVROLE_STA;
5056 else if (strcasecmp(dev_role, "PCP") == 0)
5057 dut->dev_role = DEVROLE_PCP;
5058 else {
5059 send_resp(dut, conn, SIGMA_ERROR,
5060 "errorCode,Unknown DevRole");
5061 return 0;
5062 }
5063
5064 if (dut->device_type == STA_unknown) {
5065 sigma_dut_print(dut, DUT_MSG_ERROR,
5066 "Device type is not STA testbed or DUT");
5067 send_resp(dut, conn, SIGMA_ERROR,
5068 "errorCode,Unknown device type");
5069 return 0;
5070 }
5071 }
5072
5073 wpa_command(intf, "WPS_ER_STOP");
5074 wpa_command(intf, "FLUSH");
5075 wpa_command(intf, "SET radio_disabled 0");
5076
5077 if (dut->tmp_mac_addr && dut->set_macaddr) {
5078 dut->tmp_mac_addr = 0;
5079 if (system(dut->set_macaddr) != 0) {
5080 sigma_dut_print(dut, DUT_MSG_INFO, "Failed to clear "
5081 "temporary MAC address");
5082 }
5083 }
5084
5085 set_ps(intf, dut, 0);
5086
5087 if (dut->program == PROGRAM_HS2 || dut->program == PROGRAM_HS2_R2) {
5088 wpa_command(intf, "SET interworking 1");
5089 wpa_command(intf, "SET hs20 1");
5090 }
5091
5092 if (dut->program == PROGRAM_HS2_R2) {
5093 wpa_command(intf, "SET pmf 1");
5094 } else {
5095 wpa_command(intf, "SET pmf 0");
5096 }
5097
5098 hs2_clear_credentials(intf);
5099 wpa_command(intf, "SET hessid 00:00:00:00:00:00");
5100 wpa_command(intf, "SET access_network_type 15");
5101
5102 static_ip_file(0, NULL, NULL, NULL);
5103 kill_dhcp_client(dut, intf);
5104 clear_ip_addr(dut, intf);
5105
5106 dut->er_oper_performed = 0;
5107 dut->er_oper_bssid[0] = '\0';
5108
priyadharshini gowthamanad6cbba2016-10-04 10:39:58 -07005109 if (dut->program == PROGRAM_LOC) {
5110 /* Disable Interworking by default */
5111 wpa_command(get_station_ifname(), "SET interworking 0");
5112 }
5113
Ashwini Patil00402582017-04-13 12:29:39 +05305114 if (dut->program == PROGRAM_MBO) {
5115 free(dut->non_pref_ch_list);
5116 dut->non_pref_ch_list = NULL;
Ashwini Patil5acd7382017-04-13 15:55:04 +05305117 free(dut->btm_query_cand_list);
5118 dut->btm_query_cand_list = NULL;
Ashwini Patilc63161e2017-04-13 16:30:23 +05305119 wpa_command(intf, "SET reject_btm_req_reason 0");
Ashwini Patila75de5a2017-04-13 16:35:05 +05305120 wpa_command(intf, "SET ignore_assoc_disallow 0");
Ashwini Patild174f2c2017-04-13 16:49:46 +05305121 wpa_command(intf, "SET gas_address3 0");
Ashwini Patil9183fdb2017-04-13 16:58:25 +05305122 wpa_command(intf, "SET roaming 1");
Ashwini Patil00402582017-04-13 12:29:39 +05305123 }
5124
Jouni Malinen3c367e82017-06-23 17:01:47 +03005125 free(dut->rsne_override);
5126 dut->rsne_override = NULL;
5127
Jouni Malinen68143132017-09-02 02:34:08 +03005128 free(dut->sae_commit_override);
5129 dut->sae_commit_override = NULL;
5130
Jouni Malinend86e5822017-08-29 03:55:32 +03005131 dut->dpp_conf_id = -1;
Jouni Malinenb1dd21f2017-11-13 19:14:29 +02005132 free(dut->dpp_peer_uri);
5133 dut->dpp_peer_uri = NULL;
Jouni Malinen63d50412017-11-24 11:55:38 +02005134 dut->dpp_local_bootstrap = -1;
Jouni Malinend86e5822017-08-29 03:55:32 +03005135
Jouni Malinenfac9cad2017-10-10 18:35:55 +03005136 wpa_command(intf, "VENDOR_ELEM_REMOVE 13 *");
5137
Ankita Bajaja2cb5672017-10-25 16:08:28 +05305138 if (dut->program == PROGRAM_OCE)
5139 wpa_command(intf, "SET oce 1");
5140
Priyadharshini Gowthamana7dfd492015-11-09 14:34:08 -08005141 if (dut->program != PROGRAM_VHT)
5142 return cmd_sta_p2p_reset(dut, conn, cmd);
5143 return 1;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005144}
5145
5146
5147static int cmd_sta_get_events(struct sigma_dut *dut, struct sigma_conn *conn,
5148 struct sigma_cmd *cmd)
5149{
5150 const char *program = get_param(cmd, "Program");
5151
5152 if (program == NULL)
5153 return -1;
5154#ifdef ANDROID_NAN
5155 if (strcasecmp(program, "NAN") == 0)
5156 return nan_cmd_sta_get_events(dut, conn, cmd);
5157#endif /* ANDROID_NAN */
5158 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Unsupported parameter");
5159 return 0;
5160}
5161
5162
5163static int cmd_sta_exec_action(struct sigma_dut *dut, struct sigma_conn *conn,
5164 struct sigma_cmd *cmd)
5165{
5166 const char *program = get_param(cmd, "Prog");
5167
5168 if (program == NULL)
5169 return -1;
5170#ifdef ANDROID_NAN
5171 if (strcasecmp(program, "NAN") == 0)
5172 return nan_cmd_sta_exec_action(dut, conn, cmd);
5173#endif /* ANDROID_NAN */
priyadharshini gowthamand66913a2016-07-29 15:11:17 -07005174 if (strcasecmp(program, "Loc") == 0)
5175 return loc_cmd_sta_exec_action(dut, conn, cmd);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005176 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Unsupported parameter");
5177 return 0;
5178}
5179
5180
5181static int cmd_sta_set_11n(struct sigma_dut *dut, struct sigma_conn *conn,
5182 struct sigma_cmd *cmd)
5183{
5184 const char *intf = get_param(cmd, "Interface");
5185 const char *val, *mcs32, *rate;
5186
5187 val = get_param(cmd, "GREENFIELD");
5188 if (val) {
5189 if (strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0) {
5190 /* Enable GD */
5191 send_resp(dut, conn, SIGMA_ERROR,
5192 "ErrorCode,GF not supported");
5193 return 0;
5194 }
5195 }
5196
5197 val = get_param(cmd, "SGI20");
5198 if (val) {
5199 switch (get_driver_type()) {
5200 case DRIVER_ATHEROS:
5201 ath_sta_set_sgi(dut, intf, val);
5202 break;
5203 default:
5204 send_resp(dut, conn, SIGMA_ERROR,
5205 "ErrorCode,SGI20 not supported");
5206 return 0;
5207 }
5208 }
5209
5210 mcs32 = get_param(cmd, "MCS32"); /* HT Duplicate Mode Enable/Disable */
5211 rate = get_param(cmd, "MCS_FIXEDRATE"); /* Fixed MCS rate (0..31) */
5212 if (mcs32 && rate) {
5213 /* TODO */
5214 send_resp(dut, conn, SIGMA_ERROR,
5215 "ErrorCode,MCS32,MCS_FIXEDRATE not supported");
5216 return 0;
5217 } else if (mcs32 && !rate) {
5218 /* TODO */
5219 send_resp(dut, conn, SIGMA_ERROR,
5220 "ErrorCode,MCS32 not supported");
5221 return 0;
5222 } else if (!mcs32 && rate) {
5223 switch (get_driver_type()) {
5224 case DRIVER_ATHEROS:
priyadharshini gowthamane5e25172015-12-08 14:53:48 -08005225 novap_reset(dut, intf);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005226 ath_sta_set_11nrates(dut, intf, rate);
5227 break;
5228 default:
5229 send_resp(dut, conn, SIGMA_ERROR,
5230 "ErrorCode,MCS32_FIXEDRATE not supported");
5231 return 0;
5232 }
5233 }
5234
5235 return cmd_sta_set_wireless_common(intf, dut, conn, cmd);
5236}
5237
5238
5239static int cmd_sta_set_wireless_vht(struct sigma_dut *dut,
5240 struct sigma_conn *conn,
5241 struct sigma_cmd *cmd)
5242{
5243 const char *intf = get_param(cmd, "Interface");
5244 const char *val;
5245 char buf[30];
5246 int tkip = -1;
5247 int wep = -1;
5248
5249 val = get_param(cmd, "SGI80");
5250 if (val) {
5251 int sgi80;
5252
5253 sgi80 = strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0;
5254 snprintf(buf, sizeof(buf), "iwpriv %s shortgi %d", intf, sgi80);
5255 if (system(buf) != 0) {
5256 sigma_dut_print(dut, DUT_MSG_ERROR,
5257 "iwpriv shortgi failed");
5258 }
5259 }
5260
5261 val = get_param(cmd, "TxBF");
5262 if (val && (strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0)) {
5263 snprintf(buf, sizeof(buf), "iwpriv %s vhtsubfee 1", intf);
5264 if (system(buf) != 0) {
5265 sigma_dut_print(dut, DUT_MSG_ERROR,
5266 "iwpriv vhtsubfee failed");
5267 }
5268 snprintf(buf, sizeof(buf), "iwpriv %s vhtsubfer 1", intf);
5269 if (system(buf) != 0) {
5270 sigma_dut_print(dut, DUT_MSG_ERROR,
5271 "iwpriv vhtsubfer failed");
5272 }
5273 }
5274
5275 val = get_param(cmd, "MU_TxBF");
5276 if (val && (strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0)) {
5277 switch (get_driver_type()) {
5278 case DRIVER_ATHEROS:
5279 ath_sta_set_txsp_stream(dut, intf, "1SS");
5280 ath_sta_set_rxsp_stream(dut, intf, "1SS");
5281 case DRIVER_WCN:
5282 if (wcn_sta_set_sp_stream(dut, intf, "1SS") < 0) {
5283 send_resp(dut, conn, SIGMA_ERROR,
5284 "ErrorCode,Failed to set RX/TXSP_STREAM");
5285 return 0;
5286 }
5287 default:
5288 sigma_dut_print(dut, DUT_MSG_ERROR,
5289 "Setting SP_STREAM not supported");
5290 break;
5291 }
5292 snprintf(buf, sizeof(buf), "iwpriv %s vhtmubfee 1", intf);
5293 if (system(buf) != 0) {
5294 sigma_dut_print(dut, DUT_MSG_ERROR,
5295 "iwpriv vhtmubfee failed");
5296 }
5297 snprintf(buf, sizeof(buf), "iwpriv %s vhtmubfer 1", intf);
5298 if (system(buf) != 0) {
5299 sigma_dut_print(dut, DUT_MSG_ERROR,
5300 "iwpriv vhtmubfer failed");
5301 }
5302 }
5303
5304 val = get_param(cmd, "LDPC");
5305 if (val) {
5306 int ldpc;
5307
5308 ldpc = strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0;
5309 snprintf(buf, sizeof(buf), "iwpriv %s ldpc %d", intf, ldpc);
5310 if (system(buf) != 0) {
5311 sigma_dut_print(dut, DUT_MSG_ERROR,
5312 "iwpriv ldpc failed");
5313 }
5314 }
5315
5316 val = get_param(cmd, "opt_md_notif_ie");
5317 if (val) {
5318 char *result = NULL;
5319 char delim[] = ";";
5320 char token[30];
5321 int value, config_val = 0;
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05305322 char *saveptr;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005323
Peng Xub8fc5cc2017-05-10 17:27:28 -07005324 strlcpy(token, val, sizeof(token));
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05305325 result = strtok_r(token, delim, &saveptr);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005326
5327 /* Extract the NSS information */
5328 if (result) {
5329 value = atoi(result);
5330 switch (value) {
5331 case 1:
5332 config_val = 1;
5333 break;
5334 case 2:
5335 config_val = 3;
5336 break;
5337 case 3:
5338 config_val = 7;
5339 break;
5340 case 4:
5341 config_val = 15;
5342 break;
5343 default:
5344 config_val = 3;
5345 break;
5346 }
5347
5348 snprintf(buf, sizeof(buf), "iwpriv %s rxchainmask %d",
5349 intf, config_val);
5350 if (system(buf) != 0) {
5351 sigma_dut_print(dut, DUT_MSG_ERROR,
5352 "iwpriv rxchainmask failed");
5353 }
5354
5355 snprintf(buf, sizeof(buf), "iwpriv %s txchainmask %d",
5356 intf, config_val);
5357 if (system(buf) != 0) {
5358 sigma_dut_print(dut, DUT_MSG_ERROR,
5359 "iwpriv txchainmask failed");
5360 }
5361 }
5362
5363 /* Extract the channel width information */
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05305364 result = strtok_r(NULL, delim, &saveptr);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005365 if (result) {
5366 value = atoi(result);
5367 switch (value) {
5368 case 20:
5369 config_val = 0;
5370 break;
5371 case 40:
5372 config_val = 1;
5373 break;
5374 case 80:
5375 config_val = 2;
5376 break;
5377 case 160:
5378 config_val = 3;
5379 break;
5380 default:
5381 config_val = 2;
5382 break;
5383 }
5384
5385 dut->chwidth = config_val;
5386
5387 snprintf(buf, sizeof(buf), "iwpriv %s chwidth %d",
5388 intf, config_val);
5389 if (system(buf) != 0) {
5390 sigma_dut_print(dut, DUT_MSG_ERROR,
5391 "iwpriv chwidth failed");
5392 }
5393 }
5394
5395 snprintf(buf, sizeof(buf), "iwpriv %s opmode_notify 1", intf);
5396 if (system(buf) != 0) {
5397 sigma_dut_print(dut, DUT_MSG_ERROR,
5398 "iwpriv opmode_notify failed");
5399 }
5400 }
5401
5402 val = get_param(cmd, "nss_mcs_cap");
5403 if (val) {
5404 int nss, mcs;
5405 char token[20];
5406 char *result = NULL;
5407 unsigned int vht_mcsmap = 0;
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05305408 char *saveptr;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005409
Peng Xub8fc5cc2017-05-10 17:27:28 -07005410 strlcpy(token, val, sizeof(token));
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05305411 result = strtok_r(token, ";", &saveptr);
Pradeep Reddy POTTETIcd649a22016-01-29 12:55:59 +05305412 if (!result) {
5413 sigma_dut_print(dut, DUT_MSG_ERROR,
5414 "VHT NSS not specified");
5415 return 0;
5416 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005417 nss = atoi(result);
5418
5419 snprintf(buf, sizeof(buf), "iwpriv %s nss %d", intf, nss);
5420 if (system(buf) != 0) {
5421 sigma_dut_print(dut, DUT_MSG_ERROR,
5422 "iwpriv nss failed");
5423 }
5424
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05305425 result = strtok_r(NULL, ";", &saveptr);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005426 if (result == NULL) {
5427 sigma_dut_print(dut, DUT_MSG_ERROR,
5428 "VHTMCS NOT SPECIFIED!");
5429 return 0;
5430 }
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05305431 result = strtok_r(result, "-", &saveptr);
5432 result = strtok_r(NULL, "-", &saveptr);
Pradeep Reddy POTTETIcd649a22016-01-29 12:55:59 +05305433 if (!result) {
5434 sigma_dut_print(dut, DUT_MSG_ERROR,
5435 "VHT MCS not specified");
5436 return 0;
5437 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005438 mcs = atoi(result);
5439
5440 snprintf(buf, sizeof(buf), "iwpriv %s vhtmcs %d", intf, mcs);
5441 if (system(buf) != 0) {
5442 sigma_dut_print(dut, DUT_MSG_ERROR,
5443 "iwpriv mcs failed");
5444 }
5445
5446 switch (nss) {
5447 case 1:
5448 switch (mcs) {
5449 case 7:
5450 vht_mcsmap = 0xfffc;
5451 break;
5452 case 8:
5453 vht_mcsmap = 0xfffd;
5454 break;
5455 case 9:
5456 vht_mcsmap = 0xfffe;
5457 break;
5458 default:
5459 vht_mcsmap = 0xfffe;
5460 break;
5461 }
5462 break;
5463 case 2:
5464 switch (mcs) {
5465 case 7:
5466 vht_mcsmap = 0xfff0;
5467 break;
5468 case 8:
5469 vht_mcsmap = 0xfff5;
5470 break;
5471 case 9:
5472 vht_mcsmap = 0xfffa;
5473 break;
5474 default:
5475 vht_mcsmap = 0xfffa;
5476 break;
5477 }
5478 break;
5479 case 3:
5480 switch (mcs) {
5481 case 7:
5482 vht_mcsmap = 0xffc0;
5483 break;
5484 case 8:
5485 vht_mcsmap = 0xffd5;
5486 break;
5487 case 9:
5488 vht_mcsmap = 0xffea;
5489 break;
5490 default:
5491 vht_mcsmap = 0xffea;
5492 break;
5493 }
5494 break;
5495 default:
5496 vht_mcsmap = 0xffea;
5497 break;
5498 }
5499 snprintf(buf, sizeof(buf), "iwpriv %s vht_mcsmap 0x%04x",
5500 intf, vht_mcsmap);
5501 if (system(buf) != 0) {
5502 sigma_dut_print(dut, DUT_MSG_ERROR,
5503 "iwpriv vht_mcsmap failed");
5504 }
5505 }
5506
5507 /* UNSUPPORTED: val = get_param(cmd, "Tx_lgi_rate"); */
5508
5509 val = get_param(cmd, "Vht_tkip");
5510 if (val)
5511 tkip = strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0;
5512
5513 val = get_param(cmd, "Vht_wep");
5514 if (val)
5515 wep = strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0;
5516
5517 if (tkip != -1 || wep != -1) {
5518 if ((tkip == 1 && wep != 0) || (wep == 1 && tkip != 0)) {
5519 snprintf(buf, sizeof(buf), "iwpriv %s htweptkip 1",
5520 intf);
5521 } else if ((tkip == 0 && wep != 1) || (wep == 0 && tkip != 1)) {
5522 snprintf(buf, sizeof(buf), "iwpriv %s htweptkip 0",
5523 intf);
5524 } else {
5525 sigma_dut_print(dut, DUT_MSG_ERROR,
5526 "ErrorCode,mixed mode of VHT TKIP/WEP not supported");
5527 return 0;
5528 }
5529
5530 if (system(buf) != 0) {
5531 sigma_dut_print(dut, DUT_MSG_ERROR,
5532 "iwpriv htweptkip failed");
5533 }
5534 }
5535
5536 val = get_param(cmd, "txBandwidth");
5537 if (val) {
5538 switch (get_driver_type()) {
5539 case DRIVER_ATHEROS:
5540 if (ath_set_width(dut, conn, intf, val) < 0) {
5541 send_resp(dut, conn, SIGMA_ERROR,
5542 "ErrorCode,Failed to set txBandwidth");
5543 return 0;
5544 }
5545 break;
5546 default:
5547 sigma_dut_print(dut, DUT_MSG_ERROR,
5548 "Setting txBandwidth not supported");
5549 break;
5550 }
5551 }
5552
5553 return cmd_sta_set_wireless_common(intf, dut, conn, cmd);
5554}
5555
5556
5557static int sta_set_wireless_60g(struct sigma_dut *dut,
5558 struct sigma_conn *conn,
5559 struct sigma_cmd *cmd)
5560{
5561 const char *dev_role = get_param(cmd, "DevRole");
5562
5563 if (!dev_role) {
5564 send_resp(dut, conn, SIGMA_INVALID,
5565 "ErrorCode,DevRole not specified");
5566 return 0;
5567 }
5568
5569 if (strcasecmp(dev_role, "PCP") == 0)
5570 return sta_set_60g_pcp(dut, conn, cmd);
5571 if (strcasecmp(dev_role, "STA") == 0)
5572 return sta_set_60g_sta(dut, conn, cmd);
5573 send_resp(dut, conn, SIGMA_INVALID,
5574 "ErrorCode,DevRole not supported");
5575 return 0;
5576}
5577
5578
5579static int cmd_sta_set_wireless(struct sigma_dut *dut, struct sigma_conn *conn,
5580 struct sigma_cmd *cmd)
5581{
5582 const char *val;
5583
5584 val = get_param(cmd, "Program");
5585 if (val) {
5586 if (strcasecmp(val, "11n") == 0)
5587 return cmd_sta_set_11n(dut, conn, cmd);
5588 if (strcasecmp(val, "VHT") == 0)
5589 return cmd_sta_set_wireless_vht(dut, conn, cmd);
5590 if (strcasecmp(val, "60ghz") == 0)
5591 return sta_set_wireless_60g(dut, conn, cmd);
5592 send_resp(dut, conn, SIGMA_ERROR,
5593 "ErrorCode,Program value not supported");
5594 } else {
5595 send_resp(dut, conn, SIGMA_ERROR,
5596 "ErrorCode,Program argument not available");
5597 }
5598
5599 return 0;
5600}
5601
5602
5603static void ath_sta_inject_frame(struct sigma_dut *dut, const char *intf,
5604 int tid)
5605{
5606 char buf[100];
5607 int tid_to_dscp [] = { 0x00, 0x20, 0x40, 0x60, 0x80, 0xa0, 0xc0, 0xe0 };
5608
Pradeep Reddy POTTETId31d1322016-10-13 17:22:03 +05305609 if (tid < 0 ||
5610 tid >= (int) (sizeof(tid_to_dscp) / sizeof(tid_to_dscp[0]))) {
5611 sigma_dut_print(dut, DUT_MSG_ERROR, "Unsupported TID: %d", tid);
5612 return;
5613 }
5614
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005615 /*
5616 * Two ways to ensure that addba request with a
5617 * non zero TID could be sent out. EV 117296
5618 */
5619 snprintf(buf, sizeof(buf),
5620 "ping -c 8 -Q %d `arp -a | grep wlan0 | awk '{print $2}' | tr -d '()'`",
5621 tid);
5622 if (system(buf) != 0) {
5623 sigma_dut_print(dut, DUT_MSG_ERROR,
5624 "Ping did not send out");
5625 }
5626
5627 snprintf(buf, sizeof(buf),
5628 "iwconfig %s | grep Access | awk '{print $6}' > %s",
5629 intf, VI_QOS_TMP_FILE);
5630 if (system(buf) != 0)
5631 return;
5632
5633 snprintf(buf, sizeof(buf),
5634 "ifconfig %s | grep HWaddr | cut -b 39-56 >> %s",
5635 intf, VI_QOS_TMP_FILE);
5636 if (system(buf) != 0)
5637 sigma_dut_print(dut, DUT_MSG_ERROR, "HWaddr matching failed");
5638
5639 snprintf(buf,sizeof(buf), "sed -n '3,$p' %s >> %s",
5640 VI_QOS_REFFILE, VI_QOS_TMP_FILE);
5641 if (system(buf) != 0) {
5642 sigma_dut_print(dut, DUT_MSG_ERROR,
5643 "VI_QOS_TEMP_FILE generation error failed");
5644 }
5645 snprintf(buf, sizeof(buf), "sed '5 c %x' %s > %s",
5646 tid_to_dscp[tid], VI_QOS_TMP_FILE, VI_QOS_FILE);
5647 if (system(buf) != 0) {
5648 sigma_dut_print(dut, DUT_MSG_ERROR,
5649 "VI_QOS_FILE generation failed");
5650 }
5651
5652 snprintf(buf, sizeof(buf), "sed '5 c %x' %s > %s",
5653 tid_to_dscp[tid], VI_QOS_TMP_FILE, VI_QOS_FILE);
5654 if (system(buf) != 0) {
5655 sigma_dut_print(dut, DUT_MSG_ERROR,
5656 "VI_QOS_FILE generation failed");
5657 }
5658
5659 snprintf(buf, sizeof(buf), "ethinject %s %s", intf, VI_QOS_FILE);
5660 if (system(buf) != 0) {
5661 }
5662}
5663
5664
5665static int ath_sta_send_addba(struct sigma_dut *dut, struct sigma_conn *conn,
5666 struct sigma_cmd *cmd)
5667{
5668 const char *intf = get_param(cmd, "Interface");
5669 const char *val;
5670 int tid = 0;
5671 char buf[100];
5672
5673 val = get_param(cmd, "TID");
5674 if (val) {
5675 tid = atoi(val);
5676 if (tid)
5677 ath_sta_inject_frame(dut, intf, tid);
5678 }
5679
5680 /* Command sequence for ADDBA request on Peregrine based devices */
5681 snprintf(buf, sizeof(buf), "iwpriv %s setaddbaoper 1", intf);
5682 if (system(buf) != 0) {
5683 sigma_dut_print(dut, DUT_MSG_ERROR,
5684 "iwpriv setaddbaoper failed");
5685 }
5686
5687 snprintf(buf, sizeof(buf), "wifitool %s senddelba 1 %d 1 4", intf, tid);
5688 if (system(buf) != 0) {
5689 sigma_dut_print(dut, DUT_MSG_ERROR,
5690 "wifitool senddelba failed");
5691 }
5692
5693 snprintf(buf, sizeof(buf), "wifitool %s sendaddba 1 %d 64", intf, tid);
5694 if (system(buf) != 0) {
5695 sigma_dut_print(dut, DUT_MSG_ERROR,
5696 "wifitool sendaddba failed");
5697 }
5698
5699 /* UNSUPPORTED: val = get_param(cmd, "Dest_mac"); */
5700
5701 return 1;
5702}
5703
5704
Lior David9981b512017-01-20 13:16:40 +02005705#ifdef __linux__
5706
5707static int wil6210_send_addba(struct sigma_dut *dut, const char *dest_mac,
5708 int agg_size)
5709{
5710 char dir[128], buf[128];
5711 FILE *f;
5712 regex_t re;
5713 regmatch_t m[2];
5714 int rc, ret = -1, vring_id, found;
5715
5716 if (wil6210_get_debugfs_dir(dut, dir, sizeof(dir))) {
5717 sigma_dut_print(dut, DUT_MSG_ERROR,
5718 "failed to get wil6210 debugfs dir");
5719 return -1;
5720 }
5721
5722 snprintf(buf, sizeof(buf), "%s/vrings", dir);
5723 f = fopen(buf, "r");
5724 if (!f) {
5725 sigma_dut_print(dut, DUT_MSG_ERROR, "failed to open: %s", buf);
5726 return -1;
5727 }
5728
5729 if (regcomp(&re, "VRING tx_[ \t]*([0-9]+)", REG_EXTENDED)) {
5730 sigma_dut_print(dut, DUT_MSG_ERROR, "regcomp failed");
5731 goto out;
5732 }
5733
5734 /* find TX VRING for the mac address */
5735 found = 0;
5736 while (fgets(buf, sizeof(buf), f)) {
5737 if (strcasestr(buf, dest_mac)) {
5738 found = 1;
5739 break;
5740 }
5741 }
5742
5743 if (!found) {
5744 sigma_dut_print(dut, DUT_MSG_ERROR,
5745 "no TX VRING for %s", dest_mac);
5746 goto out;
5747 }
5748
5749 /* extract VRING ID, "VRING tx_<id> = {" */
5750 if (!fgets(buf, sizeof(buf), f)) {
5751 sigma_dut_print(dut, DUT_MSG_ERROR,
5752 "no VRING start line for %s", dest_mac);
5753 goto out;
5754 }
5755
5756 rc = regexec(&re, buf, 2, m, 0);
5757 regfree(&re);
5758 if (rc || m[1].rm_so < 0) {
5759 sigma_dut_print(dut, DUT_MSG_ERROR,
5760 "no VRING TX ID for %s", dest_mac);
5761 goto out;
5762 }
5763 buf[m[1].rm_eo] = 0;
5764 vring_id = atoi(&buf[m[1].rm_so]);
5765
5766 /* send the addba command */
5767 fclose(f);
5768 snprintf(buf, sizeof(buf), "%s/back", dir);
5769 f = fopen(buf, "w");
5770 if (!f) {
5771 sigma_dut_print(dut, DUT_MSG_ERROR,
5772 "failed to open: %s", buf);
5773 return -1;
5774 }
5775
5776 fprintf(f, "add %d %d\n", vring_id, agg_size);
5777
5778 ret = 0;
5779
5780out:
5781 fclose(f);
5782
5783 return ret;
5784}
5785
5786
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005787static int send_addba_60g(struct sigma_dut *dut, struct sigma_conn *conn,
5788 struct sigma_cmd *cmd)
5789{
5790 const char *val;
5791 int tid = 0;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005792
5793 val = get_param(cmd, "TID");
5794 if (val) {
5795 tid = atoi(val);
5796 if (tid != 0) {
5797 sigma_dut_print(dut, DUT_MSG_ERROR,
5798 "Ignore TID %d for send_addba use TID 0 for 60g since only 0 required on TX",
5799 tid);
5800 }
5801 }
5802
5803 val = get_param(cmd, "Dest_mac");
5804 if (!val) {
5805 sigma_dut_print(dut, DUT_MSG_ERROR,
5806 "Currently not supporting addba for 60G without Dest_mac");
5807 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
5808 }
5809
Lior David9981b512017-01-20 13:16:40 +02005810 if (wil6210_send_addba(dut, val, dut->back_rcv_buf))
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005811 return -1;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005812
5813 return 1;
5814}
5815
Lior David9981b512017-01-20 13:16:40 +02005816#endif /* __linux__ */
5817
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005818
5819static int cmd_sta_send_addba(struct sigma_dut *dut, struct sigma_conn *conn,
5820 struct sigma_cmd *cmd)
5821{
5822 switch (get_driver_type()) {
5823 case DRIVER_ATHEROS:
5824 return ath_sta_send_addba(dut, conn, cmd);
Lior David9981b512017-01-20 13:16:40 +02005825#ifdef __linux__
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005826 case DRIVER_WIL6210:
5827 return send_addba_60g(dut, conn, cmd);
Lior David9981b512017-01-20 13:16:40 +02005828#endif /* __linux__ */
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005829 default:
5830 /*
5831 * There is no driver specific implementation for other drivers.
5832 * Ignore the command and report COMPLETE since the following
5833 * throughput test operation will end up sending ADDBA anyway.
5834 */
5835 return 1;
5836 }
5837}
5838
5839
5840int inject_eth_frame(int s, const void *data, size_t len,
5841 unsigned short ethtype, char *dst, char *src)
5842{
5843 struct iovec iov[4] = {
5844 {
5845 .iov_base = dst,
5846 .iov_len = ETH_ALEN,
5847 },
5848 {
5849 .iov_base = src,
5850 .iov_len = ETH_ALEN,
5851 },
5852 {
5853 .iov_base = &ethtype,
5854 .iov_len = sizeof(unsigned short),
5855 },
5856 {
5857 .iov_base = (void *) data,
5858 .iov_len = len,
5859 }
5860 };
5861 struct msghdr msg = {
5862 .msg_name = NULL,
5863 .msg_namelen = 0,
5864 .msg_iov = iov,
5865 .msg_iovlen = 4,
5866 .msg_control = NULL,
5867 .msg_controllen = 0,
5868 .msg_flags = 0,
5869 };
5870
5871 return sendmsg(s, &msg, 0);
5872}
5873
5874#if defined(__linux__) || defined(__QNXNTO__)
5875
5876int inject_frame(int s, const void *data, size_t len, int encrypt)
5877{
5878#define IEEE80211_RADIOTAP_F_WEP 0x04
5879#define IEEE80211_RADIOTAP_F_FRAG 0x08
5880 unsigned char rtap_hdr[] = {
5881 0x00, 0x00, /* radiotap version */
5882 0x0e, 0x00, /* radiotap length */
5883 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
5884 IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
5885 0x00, /* padding */
5886 0x00, 0x00, /* RX and TX flags to indicate that */
5887 0x00, 0x00, /* this is the injected frame directly */
5888 };
5889 struct iovec iov[2] = {
5890 {
5891 .iov_base = &rtap_hdr,
5892 .iov_len = sizeof(rtap_hdr),
5893 },
5894 {
5895 .iov_base = (void *) data,
5896 .iov_len = len,
5897 }
5898 };
5899 struct msghdr msg = {
5900 .msg_name = NULL,
5901 .msg_namelen = 0,
5902 .msg_iov = iov,
5903 .msg_iovlen = 2,
5904 .msg_control = NULL,
5905 .msg_controllen = 0,
5906 .msg_flags = 0,
5907 };
5908
5909 if (encrypt)
5910 rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
5911
5912 return sendmsg(s, &msg, 0);
5913}
5914
5915
5916int open_monitor(const char *ifname)
5917{
5918#ifdef __QNXNTO__
5919 struct sockaddr_dl ll;
5920 int s;
5921
5922 memset(&ll, 0, sizeof(ll));
5923 ll.sdl_family = AF_LINK;
5924 ll.sdl_index = if_nametoindex(ifname);
5925 if (ll.sdl_index == 0) {
5926 perror("if_nametoindex");
5927 return -1;
5928 }
5929 s = socket(PF_INET, SOCK_RAW, 0);
5930#else /* __QNXNTO__ */
5931 struct sockaddr_ll ll;
5932 int s;
5933
5934 memset(&ll, 0, sizeof(ll));
5935 ll.sll_family = AF_PACKET;
5936 ll.sll_ifindex = if_nametoindex(ifname);
5937 if (ll.sll_ifindex == 0) {
5938 perror("if_nametoindex");
5939 return -1;
5940 }
5941 s = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
5942#endif /* __QNXNTO__ */
5943 if (s < 0) {
5944 perror("socket[PF_PACKET,SOCK_RAW]");
5945 return -1;
5946 }
5947
5948 if (bind(s, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
5949 perror("monitor socket bind");
5950 close(s);
5951 return -1;
5952 }
5953
5954 return s;
5955}
5956
5957
5958static int hex2num(char c)
5959{
5960 if (c >= '0' && c <= '9')
5961 return c - '0';
5962 if (c >= 'a' && c <= 'f')
5963 return c - 'a' + 10;
5964 if (c >= 'A' && c <= 'F')
5965 return c - 'A' + 10;
5966 return -1;
5967}
5968
5969
5970int hwaddr_aton(const char *txt, unsigned char *addr)
5971{
5972 int i;
5973
5974 for (i = 0; i < 6; i++) {
5975 int a, b;
5976
5977 a = hex2num(*txt++);
5978 if (a < 0)
5979 return -1;
5980 b = hex2num(*txt++);
5981 if (b < 0)
5982 return -1;
5983 *addr++ = (a << 4) | b;
5984 if (i < 5 && *txt++ != ':')
5985 return -1;
5986 }
5987
5988 return 0;
5989}
5990
5991#endif /* defined(__linux__) || defined(__QNXNTO__) */
5992
5993enum send_frame_type {
5994 DISASSOC, DEAUTH, SAQUERY, AUTH, ASSOCREQ, REASSOCREQ, DLS_REQ
5995};
5996enum send_frame_protection {
5997 CORRECT_KEY, INCORRECT_KEY, UNPROTECTED
5998};
5999
6000
6001static int sta_inject_frame(struct sigma_dut *dut, struct sigma_conn *conn,
6002 enum send_frame_type frame,
6003 enum send_frame_protection protected,
6004 const char *dest)
6005{
6006#ifdef __linux__
6007 unsigned char buf[1000], *pos;
6008 int s, res;
6009 char bssid[20], addr[20];
6010 char result[32], ssid[100];
6011 size_t ssid_len;
6012
6013 if (get_wpa_status(get_station_ifname(), "wpa_state", result,
6014 sizeof(result)) < 0 ||
6015 strncmp(result, "COMPLETED", 9) != 0) {
6016 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Not connected");
6017 return 0;
6018 }
6019
6020 if (get_wpa_status(get_station_ifname(), "bssid", bssid, sizeof(bssid))
6021 < 0) {
6022 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not get "
6023 "current BSSID");
6024 return 0;
6025 }
6026
6027 if (get_wpa_status(get_station_ifname(), "address", addr, sizeof(addr))
6028 < 0) {
6029 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not get "
6030 "own MAC address");
6031 return 0;
6032 }
6033
6034 if (get_wpa_status(get_station_ifname(), "ssid", ssid, sizeof(ssid))
6035 < 0) {
6036 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not get "
6037 "current SSID");
6038 return 0;
6039 }
6040 ssid_len = strlen(ssid);
6041
6042 pos = buf;
6043
6044 /* Frame Control */
6045 switch (frame) {
6046 case DISASSOC:
6047 *pos++ = 0xa0;
6048 break;
6049 case DEAUTH:
6050 *pos++ = 0xc0;
6051 break;
6052 case SAQUERY:
6053 *pos++ = 0xd0;
6054 break;
6055 case AUTH:
6056 *pos++ = 0xb0;
6057 break;
6058 case ASSOCREQ:
6059 *pos++ = 0x00;
6060 break;
6061 case REASSOCREQ:
6062 *pos++ = 0x20;
6063 break;
6064 case DLS_REQ:
6065 *pos++ = 0xd0;
6066 break;
6067 }
6068
6069 if (protected == INCORRECT_KEY)
6070 *pos++ = 0x40; /* Set Protected field to 1 */
6071 else
6072 *pos++ = 0x00;
6073
6074 /* Duration */
6075 *pos++ = 0x00;
6076 *pos++ = 0x00;
6077
6078 /* addr1 = DA (current AP) */
6079 hwaddr_aton(bssid, pos);
6080 pos += 6;
6081 /* addr2 = SA (own address) */
6082 hwaddr_aton(addr, pos);
6083 pos += 6;
6084 /* addr3 = BSSID (current AP) */
6085 hwaddr_aton(bssid, pos);
6086 pos += 6;
6087
6088 /* Seq# (to be filled by driver/mac80211) */
6089 *pos++ = 0x00;
6090 *pos++ = 0x00;
6091
6092 if (protected == INCORRECT_KEY) {
6093 /* CCMP parameters */
6094 memcpy(pos, "\x61\x01\x00\x20\x00\x10\x00\x00", 8);
6095 pos += 8;
6096 }
6097
6098 if (protected == INCORRECT_KEY) {
6099 switch (frame) {
6100 case DEAUTH:
6101 /* Reason code (encrypted) */
6102 memcpy(pos, "\xa7\x39", 2);
6103 pos += 2;
6104 break;
6105 case DISASSOC:
6106 /* Reason code (encrypted) */
6107 memcpy(pos, "\xa7\x39", 2);
6108 pos += 2;
6109 break;
6110 case SAQUERY:
6111 /* Category|Action|TransID (encrypted) */
6112 memcpy(pos, "\x6f\xbd\xe9\x4d", 4);
6113 pos += 4;
6114 break;
6115 default:
6116 return -1;
6117 }
6118
6119 /* CCMP MIC */
6120 memcpy(pos, "\xc8\xd8\x3b\x06\x5d\xb7\x25\x68", 8);
6121 pos += 8;
6122 } else {
6123 switch (frame) {
6124 case DEAUTH:
6125 /* reason code = 8 */
6126 *pos++ = 0x08;
6127 *pos++ = 0x00;
6128 break;
6129 case DISASSOC:
6130 /* reason code = 8 */
6131 *pos++ = 0x08;
6132 *pos++ = 0x00;
6133 break;
6134 case SAQUERY:
6135 /* Category - SA Query */
6136 *pos++ = 0x08;
6137 /* SA query Action - Request */
6138 *pos++ = 0x00;
6139 /* Transaction ID */
6140 *pos++ = 0x12;
6141 *pos++ = 0x34;
6142 break;
6143 case AUTH:
6144 /* Auth Alg (Open) */
6145 *pos++ = 0x00;
6146 *pos++ = 0x00;
6147 /* Seq# */
6148 *pos++ = 0x01;
6149 *pos++ = 0x00;
6150 /* Status code */
6151 *pos++ = 0x00;
6152 *pos++ = 0x00;
6153 break;
6154 case ASSOCREQ:
6155 /* Capability Information */
6156 *pos++ = 0x31;
6157 *pos++ = 0x04;
6158 /* Listen Interval */
6159 *pos++ = 0x0a;
6160 *pos++ = 0x00;
6161 /* SSID */
6162 *pos++ = 0x00;
6163 *pos++ = ssid_len;
6164 memcpy(pos, ssid, ssid_len);
6165 pos += ssid_len;
6166 /* Supported Rates */
6167 memcpy(pos, "\x01\x08\x02\x04\x0b\x16\x0c\x12\x18\x24",
6168 10);
6169 pos += 10;
6170 /* Extended Supported Rates */
6171 memcpy(pos, "\x32\x04\x30\x48\x60\x6c", 6);
6172 pos += 6;
6173 /* RSN */
6174 memcpy(pos, "\x30\x1a\x01\x00\x00\x0f\xac\x04\x01\x00"
6175 "\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x02\xc0"
6176 "\x00\x00\x00\x00\x0f\xac\x06", 28);
6177 pos += 28;
6178 break;
6179 case REASSOCREQ:
6180 /* Capability Information */
6181 *pos++ = 0x31;
6182 *pos++ = 0x04;
6183 /* Listen Interval */
6184 *pos++ = 0x0a;
6185 *pos++ = 0x00;
6186 /* Current AP */
6187 hwaddr_aton(bssid, pos);
6188 pos += 6;
6189 /* SSID */
6190 *pos++ = 0x00;
6191 *pos++ = ssid_len;
6192 memcpy(pos, ssid, ssid_len);
6193 pos += ssid_len;
6194 /* Supported Rates */
6195 memcpy(pos, "\x01\x08\x02\x04\x0b\x16\x0c\x12\x18\x24",
6196 10);
6197 pos += 10;
6198 /* Extended Supported Rates */
6199 memcpy(pos, "\x32\x04\x30\x48\x60\x6c", 6);
6200 pos += 6;
6201 /* RSN */
6202 memcpy(pos, "\x30\x1a\x01\x00\x00\x0f\xac\x04\x01\x00"
6203 "\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x02\xc0"
6204 "\x00\x00\x00\x00\x0f\xac\x06", 28);
6205 pos += 28;
6206 break;
6207 case DLS_REQ:
6208 /* Category - DLS */
6209 *pos++ = 0x02;
6210 /* DLS Action - Request */
6211 *pos++ = 0x00;
6212 /* Destination MACAddress */
6213 if (dest)
6214 hwaddr_aton(dest, pos);
6215 else
6216 memset(pos, 0, 6);
6217 pos += 6;
6218 /* Source MACAddress */
6219 hwaddr_aton(addr, pos);
6220 pos += 6;
6221 /* Capability Information */
6222 *pos++ = 0x10; /* Privacy */
6223 *pos++ = 0x06; /* QoS */
6224 /* DLS Timeout Value */
6225 *pos++ = 0x00;
6226 *pos++ = 0x01;
6227 /* Supported rates */
6228 *pos++ = 0x01;
6229 *pos++ = 0x08;
6230 *pos++ = 0x0c; /* 6 Mbps */
6231 *pos++ = 0x12; /* 9 Mbps */
6232 *pos++ = 0x18; /* 12 Mbps */
6233 *pos++ = 0x24; /* 18 Mbps */
6234 *pos++ = 0x30; /* 24 Mbps */
6235 *pos++ = 0x48; /* 36 Mbps */
6236 *pos++ = 0x60; /* 48 Mbps */
6237 *pos++ = 0x6c; /* 54 Mbps */
6238 /* TODO: Extended Supported Rates */
6239 /* TODO: HT Capabilities */
6240 break;
6241 }
6242 }
6243
6244 s = open_monitor("sigmadut");
6245 if (s < 0) {
6246 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to open "
6247 "monitor socket");
6248 return 0;
6249 }
6250
6251 res = inject_frame(s, buf, pos - buf, protected == CORRECT_KEY);
6252 if (res < 0) {
6253 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to "
6254 "inject frame");
Pradeep Reddy POTTETI673d85c2016-07-26 19:08:07 +05306255 close(s);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02006256 return 0;
6257 }
6258 if (res < pos - buf) {
6259 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Only partial "
6260 "frame sent");
Pradeep Reddy POTTETI673d85c2016-07-26 19:08:07 +05306261 close(s);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02006262 return 0;
6263 }
6264
6265 close(s);
6266
6267 return 1;
6268#else /* __linux__ */
6269 send_resp(dut, conn, SIGMA_ERROR, "errorCode,sta_send_frame not "
6270 "yet supported");
6271 return 0;
6272#endif /* __linux__ */
6273}
6274
6275
6276static int cmd_sta_send_frame_tdls(struct sigma_dut *dut,
6277 struct sigma_conn *conn,
6278 struct sigma_cmd *cmd)
6279{
6280 const char *intf = get_param(cmd, "Interface");
6281 const char *sta, *val;
6282 unsigned char addr[ETH_ALEN];
6283 char buf[100];
6284
6285 sta = get_param(cmd, "peer");
6286 if (sta == NULL)
6287 sta = get_param(cmd, "station");
6288 if (sta == NULL) {
6289 send_resp(dut, conn, SIGMA_ERROR,
6290 "ErrorCode,Missing peer address");
6291 return 0;
6292 }
6293 if (hwaddr_aton(sta, addr) < 0) {
6294 send_resp(dut, conn, SIGMA_ERROR,
6295 "ErrorCode,Invalid peer address");
6296 return 0;
6297 }
6298
6299 val = get_param(cmd, "type");
6300 if (val == NULL)
6301 return -1;
6302
6303 if (strcasecmp(val, "DISCOVERY") == 0) {
6304 snprintf(buf, sizeof(buf), "TDLS_DISCOVER %s", sta);
6305 if (wpa_command(intf, buf) < 0) {
6306 send_resp(dut, conn, SIGMA_ERROR,
6307 "ErrorCode,Failed to send TDLS discovery");
6308 return 0;
6309 }
6310 return 1;
6311 }
6312
6313 if (strcasecmp(val, "SETUP") == 0) {
6314 int status = 0, timeout = 0;
6315
6316 val = get_param(cmd, "Status");
6317 if (val)
6318 status = atoi(val);
6319
6320 val = get_param(cmd, "Timeout");
6321 if (val)
6322 timeout = atoi(val);
6323
6324 if (status != 0 && status != 37) {
6325 send_resp(dut, conn, SIGMA_ERROR,
6326 "ErrorCode,Unsupported status value");
6327 return 0;
6328 }
6329
6330 if (timeout != 0 && timeout != 301) {
6331 send_resp(dut, conn, SIGMA_ERROR,
6332 "ErrorCode,Unsupported timeout value");
6333 return 0;
6334 }
6335
6336 if (status && timeout) {
6337 send_resp(dut, conn, SIGMA_ERROR,
6338 "ErrorCode,Unsupported timeout+status "
6339 "combination");
6340 return 0;
6341 }
6342
6343 if (status == 37 &&
6344 wpa_command(intf, "SET tdls_testing 0x200")) {
6345 send_resp(dut, conn, SIGMA_ERROR,
6346 "ErrorCode,Failed to enable "
6347 "decline setup response test mode");
6348 return 0;
6349 }
6350
6351 if (timeout == 301) {
6352 int res;
6353 if (dut->no_tpk_expiration)
6354 res = wpa_command(intf,
6355 "SET tdls_testing 0x108");
6356 else
6357 res = wpa_command(intf,
6358 "SET tdls_testing 0x8");
6359 if (res) {
6360 send_resp(dut, conn, SIGMA_ERROR,
6361 "ErrorCode,Failed to set short TPK "
6362 "lifetime");
6363 return 0;
6364 }
6365 }
6366
6367 snprintf(buf, sizeof(buf), "TDLS_SETUP %s", sta);
6368 if (wpa_command(intf, buf) < 0) {
6369 send_resp(dut, conn, SIGMA_ERROR,
6370 "ErrorCode,Failed to send TDLS setup");
6371 return 0;
6372 }
6373 return 1;
6374 }
6375
6376 if (strcasecmp(val, "TEARDOWN") == 0) {
6377 snprintf(buf, sizeof(buf), "TDLS_TEARDOWN %s", sta);
6378 if (wpa_command(intf, buf) < 0) {
6379 send_resp(dut, conn, SIGMA_ERROR,
6380 "ErrorCode,Failed to send TDLS teardown");
6381 return 0;
6382 }
6383 return 1;
6384 }
6385
6386 send_resp(dut, conn, SIGMA_ERROR,
6387 "ErrorCode,Unsupported TDLS frame");
6388 return 0;
6389}
6390
6391
6392static int sta_ap_known(const char *ifname, const char *bssid)
6393{
6394 char buf[4096];
6395
6396 snprintf(buf, sizeof(buf), "BSS %s", bssid);
6397 if (wpa_command_resp(ifname, buf, buf, sizeof(buf)) < 0)
6398 return 0;
6399 if (strncmp(buf, "id=", 3) != 0)
6400 return 0;
6401 return 1;
6402}
6403
6404
6405static int sta_scan_ap(struct sigma_dut *dut, const char *ifname,
6406 const char *bssid)
6407{
6408 int res;
6409 struct wpa_ctrl *ctrl;
6410 char buf[256];
6411
6412 if (sta_ap_known(ifname, bssid))
6413 return 0;
6414 sigma_dut_print(dut, DUT_MSG_DEBUG,
6415 "AP not in BSS table - start scan");
6416
6417 ctrl = open_wpa_mon(ifname);
6418 if (ctrl == NULL) {
6419 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to open "
6420 "wpa_supplicant monitor connection");
6421 return -1;
6422 }
6423
6424 if (wpa_command(ifname, "SCAN") < 0) {
6425 sigma_dut_print(dut, DUT_MSG_INFO, "Failed to start scan");
6426 wpa_ctrl_detach(ctrl);
6427 wpa_ctrl_close(ctrl);
6428 return -1;
6429 }
6430
6431 res = get_wpa_cli_event(dut, ctrl, "CTRL-EVENT-SCAN-RESULTS",
6432 buf, sizeof(buf));
6433
6434 wpa_ctrl_detach(ctrl);
6435 wpa_ctrl_close(ctrl);
6436
6437 if (res < 0) {
6438 sigma_dut_print(dut, DUT_MSG_INFO, "Scan did not complete");
6439 return -1;
6440 }
6441
6442 if (sta_ap_known(ifname, bssid))
6443 return 0;
6444 sigma_dut_print(dut, DUT_MSG_INFO, "AP not in BSS table");
6445 return -1;
6446}
6447
6448
6449static int cmd_sta_send_frame_hs2_neighadv(struct sigma_dut *dut,
6450 struct sigma_conn *conn,
6451 struct sigma_cmd *cmd,
6452 const char *intf)
6453{
6454 char buf[200];
6455
6456 snprintf(buf, sizeof(buf), "ndsend 2001:DB8::1 %s", intf);
6457 if (system(buf) != 0) {
6458 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Failed to run "
6459 "ndsend");
6460 return 0;
6461 }
6462
6463 return 1;
6464}
6465
6466
6467static int cmd_sta_send_frame_hs2_neighsolreq(struct sigma_dut *dut,
6468 struct sigma_conn *conn,
6469 struct sigma_cmd *cmd,
6470 const char *intf)
6471{
6472 char buf[200];
6473 const char *ip = get_param(cmd, "SenderIP");
6474
Peng Xu26b356d2017-10-04 17:58:16 -07006475 if (!ip)
6476 return 0;
6477
Jouni Malinencd4e3c32015-10-29 12:39:56 +02006478 snprintf(buf, sizeof(buf), "ndisc6 -nm %s %s -r 4", ip, intf);
6479 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
6480 if (system(buf) == 0) {
6481 sigma_dut_print(dut, DUT_MSG_INFO,
6482 "Neighbor Solicitation got a response "
6483 "for %s@%s", ip, intf);
6484 }
6485
6486 return 1;
6487}
6488
6489
6490static int cmd_sta_send_frame_hs2_arpprobe(struct sigma_dut *dut,
6491 struct sigma_conn *conn,
6492 struct sigma_cmd *cmd,
6493 const char *ifname)
6494{
6495 char buf[200];
6496 const char *ip = get_param(cmd, "SenderIP");
6497
6498 if (ip == NULL) {
6499 send_resp(dut, conn, SIGMA_ERROR,
6500 "ErrorCode,Missing SenderIP parameter");
6501 return 0;
6502 }
6503 snprintf(buf, sizeof(buf), "arping -I %s -D %s -c 4", ifname, ip);
6504 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
6505 if (system(buf) != 0) {
6506 sigma_dut_print(dut, DUT_MSG_INFO, "arping DAD got a response "
6507 "for %s@%s", ip, ifname);
6508 }
6509
6510 return 1;
6511}
6512
6513
6514static int cmd_sta_send_frame_hs2_arpannounce(struct sigma_dut *dut,
6515 struct sigma_conn *conn,
6516 struct sigma_cmd *cmd,
6517 const char *ifname)
6518{
6519 char buf[200];
6520 char ip[16];
6521 int s;
Peng Xub3756882017-10-04 14:39:09 -07006522 struct ifreq ifr;
6523 struct sockaddr_in saddr;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02006524
6525 s = socket(PF_INET, SOCK_DGRAM, 0);
Peng Xub3756882017-10-04 14:39:09 -07006526 if (s < 0) {
6527 perror("socket");
6528 return -1;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02006529 }
6530
Peng Xub3756882017-10-04 14:39:09 -07006531 memset(&ifr, 0, sizeof(ifr));
6532 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
6533 if (ioctl(s, SIOCGIFADDR, &ifr) < 0) {
6534 sigma_dut_print(dut, DUT_MSG_INFO,
6535 "Failed to get %s IP address: %s",
6536 ifname, strerror(errno));
6537 close(s);
6538 return -1;
6539 }
6540 close(s);
6541
6542 memcpy(&saddr, &ifr.ifr_addr, sizeof(struct sockaddr_in));
6543 strlcpy(ip, inet_ntoa(saddr.sin_addr), sizeof(ip));
6544
Jouni Malinencd4e3c32015-10-29 12:39:56 +02006545 snprintf(buf, sizeof(buf), "arping -I %s -s %s %s -c 4", ifname, ip,
6546 ip);
6547 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
6548 if (system(buf) != 0) {
6549 }
6550
6551 return 1;
6552}
6553
6554
6555static int cmd_sta_send_frame_hs2_arpreply(struct sigma_dut *dut,
6556 struct sigma_conn *conn,
6557 struct sigma_cmd *cmd,
6558 const char *ifname)
6559{
6560 char buf[200], addr[20];
6561 char dst[ETH_ALEN], src[ETH_ALEN];
6562 short ethtype = htons(ETH_P_ARP);
6563 char *pos;
6564 int s, res;
6565 const char *val;
6566 struct sockaddr_in taddr;
6567
6568 val = get_param(cmd, "dest");
6569 if (val)
6570 hwaddr_aton(val, (unsigned char *) dst);
6571
6572 val = get_param(cmd, "DestIP");
6573 if (val)
6574 inet_aton(val, &taddr.sin_addr);
Peng Xu151c9e12017-10-04 14:39:09 -07006575 else
6576 return -2;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02006577
6578 if (get_wpa_status(get_station_ifname(), "address", addr,
6579 sizeof(addr)) < 0)
6580 return -2;
6581 hwaddr_aton(addr, (unsigned char *) src);
6582
6583 pos = buf;
6584 *pos++ = 0x00;
6585 *pos++ = 0x01;
6586 *pos++ = 0x08;
6587 *pos++ = 0x00;
6588 *pos++ = 0x06;
6589 *pos++ = 0x04;
6590 *pos++ = 0x00;
6591 *pos++ = 0x02;
6592 memcpy(pos, src, ETH_ALEN);
6593 pos += ETH_ALEN;
6594 memcpy(pos, &taddr.sin_addr, 4);
6595 pos += 4;
6596 memcpy(pos, dst, ETH_ALEN);
6597 pos += ETH_ALEN;
6598 memcpy(pos, &taddr.sin_addr, 4);
6599 pos += 4;
6600
6601 s = open_monitor(get_station_ifname());
6602 if (s < 0) {
6603 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to open "
6604 "monitor socket");
6605 return 0;
6606 }
6607
6608 res = inject_eth_frame(s, buf, pos - buf, ethtype, dst, src);
6609 if (res < 0) {
6610 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to "
6611 "inject frame");
Pradeep Reddy POTTETI673d85c2016-07-26 19:08:07 +05306612 close(s);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02006613 return 0;
6614 }
6615
6616 close(s);
6617
6618 return 1;
6619}
6620
6621
6622static int cmd_sta_send_frame_hs2_dls_req(struct sigma_dut *dut,
6623 struct sigma_conn *conn,
6624 struct sigma_cmd *cmd,
6625 const char *intf, const char *dest)
6626{
6627 char buf[100];
6628
6629 if (if_nametoindex("sigmadut") == 0) {
6630 snprintf(buf, sizeof(buf),
6631 "iw dev %s interface add sigmadut type monitor",
6632 get_station_ifname());
6633 if (system(buf) != 0 ||
6634 if_nametoindex("sigmadut") == 0) {
6635 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to add "
6636 "monitor interface with '%s'", buf);
6637 return -2;
6638 }
6639 }
6640
6641 if (system("ifconfig sigmadut up") != 0) {
6642 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to set "
6643 "monitor interface up");
6644 return -2;
6645 }
6646
6647 return sta_inject_frame(dut, conn, DLS_REQ, UNPROTECTED, dest);
6648}
6649
6650
6651static int cmd_sta_send_frame_hs2(struct sigma_dut *dut,
6652 struct sigma_conn *conn,
6653 struct sigma_cmd *cmd)
6654{
6655 const char *intf = get_param(cmd, "Interface");
6656 const char *dest = get_param(cmd, "Dest");
6657 const char *type = get_param(cmd, "FrameName");
6658 const char *val;
6659 char buf[200], *pos, *end;
6660 int count, count2;
6661
6662 if (type == NULL)
6663 type = get_param(cmd, "Type");
6664
6665 if (intf == NULL || dest == NULL || type == NULL)
6666 return -1;
6667
6668 if (strcasecmp(type, "NeighAdv") == 0)
6669 return cmd_sta_send_frame_hs2_neighadv(dut, conn, cmd, intf);
6670
6671 if (strcasecmp(type, "NeighSolicitReq") == 0)
6672 return cmd_sta_send_frame_hs2_neighsolreq(dut, conn, cmd, intf);
6673
6674 if (strcasecmp(type, "ARPProbe") == 0)
6675 return cmd_sta_send_frame_hs2_arpprobe(dut, conn, cmd, intf);
6676
6677 if (strcasecmp(type, "ARPAnnounce") == 0)
6678 return cmd_sta_send_frame_hs2_arpannounce(dut, conn, cmd, intf);
6679
6680 if (strcasecmp(type, "ARPReply") == 0)
6681 return cmd_sta_send_frame_hs2_arpreply(dut, conn, cmd, intf);
6682
6683 if (strcasecmp(type, "DLS-request") == 0 ||
6684 strcasecmp(type, "DLSrequest") == 0)
6685 return cmd_sta_send_frame_hs2_dls_req(dut, conn, cmd, intf,
6686 dest);
6687
6688 if (strcasecmp(type, "ANQPQuery") != 0 &&
6689 strcasecmp(type, "Query") != 0) {
6690 send_resp(dut, conn, SIGMA_ERROR,
6691 "ErrorCode,Unsupported HS 2.0 send frame type");
6692 return 0;
6693 }
6694
6695 if (sta_scan_ap(dut, intf, dest) < 0) {
6696 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Could not find "
6697 "the requested AP");
6698 return 0;
6699 }
6700
6701 pos = buf;
6702 end = buf + sizeof(buf);
6703 count = 0;
6704 pos += snprintf(pos, end - pos, "ANQP_GET %s ", dest);
6705
6706 val = get_param(cmd, "ANQP_CAP_LIST");
6707 if (val && atoi(val)) {
6708 pos += snprintf(pos, end - pos, "%s257", count > 0 ? "," : "");
6709 count++;
6710 }
6711
6712 val = get_param(cmd, "VENUE_NAME");
6713 if (val && atoi(val)) {
6714 pos += snprintf(pos, end - pos, "%s258", count > 0 ? "," : "");
6715 count++;
6716 }
6717
6718 val = get_param(cmd, "NETWORK_AUTH_TYPE");
6719 if (val && atoi(val)) {
6720 pos += snprintf(pos, end - pos, "%s260", count > 0 ? "," : "");
6721 count++;
6722 }
6723
6724 val = get_param(cmd, "ROAMING_CONS");
6725 if (val && atoi(val)) {
6726 pos += snprintf(pos, end - pos, "%s261", count > 0 ? "," : "");
6727 count++;
6728 }
6729
6730 val = get_param(cmd, "IP_ADDR_TYPE_AVAILABILITY");
6731 if (val && atoi(val)) {
6732 pos += snprintf(pos, end - pos, "%s262", count > 0 ? "," : "");
6733 count++;
6734 }
6735
6736 val = get_param(cmd, "NAI_REALM_LIST");
6737 if (val && atoi(val)) {
6738 pos += snprintf(pos, end - pos, "%s263", count > 0 ? "," : "");
6739 count++;
6740 }
6741
6742 val = get_param(cmd, "3GPP_INFO");
6743 if (val && atoi(val)) {
6744 pos += snprintf(pos, end - pos, "%s264", count > 0 ? "," : "");
6745 count++;
6746 }
6747
6748 val = get_param(cmd, "DOMAIN_LIST");
6749 if (val && atoi(val)) {
6750 pos += snprintf(pos, end - pos, "%s268", count > 0 ? "," : "");
6751 count++;
6752 }
6753
6754 if (count && wpa_command(intf, buf)) {
6755 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,ANQP_GET failed");
6756 return 0;
6757 }
6758
6759 pos = buf;
6760 end = buf + sizeof(buf);
6761 count2 = 0;
6762 pos += snprintf(pos, end - pos, "HS20_ANQP_GET %s ", dest);
6763
6764 val = get_param(cmd, "HS_CAP_LIST");
6765 if (val && atoi(val)) {
6766 pos += snprintf(pos, end - pos, "%s2", count2 > 0 ? "," : "");
6767 count2++;
6768 }
6769
6770 val = get_param(cmd, "OPER_NAME");
6771 if (val && atoi(val)) {
6772 pos += snprintf(pos, end - pos, "%s3", count2 > 0 ? "," : "");
6773 count2++;
6774 }
6775
6776 val = get_param(cmd, "WAN_METRICS");
6777 if (!val)
6778 val = get_param(cmd, "WAN_MAT");
6779 if (!val)
6780 val = get_param(cmd, "WAN_MET");
6781 if (val && atoi(val)) {
6782 pos += snprintf(pos, end - pos, "%s4", count2 > 0 ? "," : "");
6783 count2++;
6784 }
6785
6786 val = get_param(cmd, "CONNECTION_CAPABILITY");
6787 if (val && atoi(val)) {
6788 pos += snprintf(pos, end - pos, "%s5", count2 > 0 ? "," : "");
6789 count2++;
6790 }
6791
6792 val = get_param(cmd, "OP_CLASS");
6793 if (val && atoi(val)) {
6794 pos += snprintf(pos, end - pos, "%s7", count2 > 0 ? "," : "");
6795 count2++;
6796 }
6797
6798 val = get_param(cmd, "OSU_PROVIDER_LIST");
6799 if (val && atoi(val)) {
6800 pos += snprintf(pos, end - pos, "%s8", count2 > 0 ? "," : "");
6801 count2++;
6802 }
6803
6804 if (count && count2) {
6805 sigma_dut_print(dut, DUT_MSG_DEBUG, "Wait before sending out "
6806 "second query");
6807 sleep(1);
6808 }
6809
6810 if (count2 && wpa_command(intf, buf)) {
6811 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,HS20_ANQP_GET "
6812 "failed");
6813 return 0;
6814 }
6815
6816 val = get_param(cmd, "NAI_HOME_REALM_LIST");
6817 if (val) {
6818 if (count || count2) {
6819 sigma_dut_print(dut, DUT_MSG_DEBUG, "Wait before "
6820 "sending out second query");
6821 sleep(1);
6822 }
6823
6824 if (strcmp(val, "1") == 0)
6825 val = "mail.example.com";
6826 snprintf(buf, end - pos,
6827 "HS20_GET_NAI_HOME_REALM_LIST %s realm=%s",
6828 dest, val);
6829 if (wpa_command(intf, buf)) {
6830 send_resp(dut, conn, SIGMA_ERROR,
6831 "ErrorCode,HS20_GET_NAI_HOME_REALM_LIST "
6832 "failed");
6833 return 0;
6834 }
6835 }
6836
6837 val = get_param(cmd, "ICON_REQUEST");
6838 if (val) {
6839 if (count || count2) {
6840 sigma_dut_print(dut, DUT_MSG_DEBUG, "Wait before "
6841 "sending out second query");
6842 sleep(1);
6843 }
6844
6845 snprintf(buf, end - pos,
6846 "HS20_ICON_REQUEST %s %s", dest, val);
6847 if (wpa_command(intf, buf)) {
6848 send_resp(dut, conn, SIGMA_ERROR,
6849 "ErrorCode,HS20_ICON_REQUEST failed");
6850 return 0;
6851 }
6852 }
6853
6854 return 1;
6855}
6856
6857
6858static int ath_sta_send_frame_vht(struct sigma_dut *dut,
6859 struct sigma_conn *conn,
6860 struct sigma_cmd *cmd)
6861{
6862 const char *val;
6863 char *ifname;
6864 char buf[100];
6865 int chwidth, nss;
6866
6867 val = get_param(cmd, "framename");
6868 if (!val)
6869 return -1;
6870 sigma_dut_print(dut, DUT_MSG_DEBUG, "framename is %s", val);
6871
6872 /* Command sequence to generate Op mode notification */
6873 if (val && strcasecmp(val, "Op_md_notif_frm") == 0) {
6874 ifname = get_station_ifname();
6875
6876 /* Disable STBC */
6877 snprintf(buf, sizeof(buf),
6878 "iwpriv %s tx_stbc 0", ifname);
6879 if (system(buf) != 0) {
6880 sigma_dut_print(dut, DUT_MSG_ERROR,
6881 "iwpriv tx_stbc 0 failed!");
6882 }
6883
6884 /* Extract Channel width */
6885 val = get_param(cmd, "Channel_width");
6886 if (val) {
6887 switch (atoi(val)) {
6888 case 20:
6889 chwidth = 0;
6890 break;
6891 case 40:
6892 chwidth = 1;
6893 break;
6894 case 80:
6895 chwidth = 2;
6896 break;
6897 case 160:
6898 chwidth = 3;
6899 break;
6900 default:
6901 chwidth = 2;
6902 break;
6903 }
6904
6905 snprintf(buf, sizeof(buf), "iwpriv %s chwidth %d",
6906 ifname, chwidth);
6907 if (system(buf) != 0) {
6908 sigma_dut_print(dut, DUT_MSG_ERROR,
6909 "iwpriv chwidth failed!");
6910 }
6911 }
6912
6913 /* Extract NSS */
6914 val = get_param(cmd, "NSS");
6915 if (val) {
6916 switch (atoi(val)) {
6917 case 1:
6918 nss = 1;
6919 break;
6920 case 2:
6921 nss = 3;
6922 break;
6923 case 3:
6924 nss = 7;
6925 break;
6926 default:
6927 /* We do not support NSS > 3 */
6928 nss = 3;
6929 break;
6930 }
6931 snprintf(buf, sizeof(buf),
6932 "iwpriv %s rxchainmask %d", ifname, nss);
6933 if (system(buf) != 0) {
6934 sigma_dut_print(dut, DUT_MSG_ERROR,
6935 "iwpriv rxchainmask failed!");
6936 }
6937 }
6938
6939 /* Opmode notify */
6940 snprintf(buf, sizeof(buf), "iwpriv %s opmode_notify 1", ifname);
6941 if (system(buf) != 0) {
6942 sigma_dut_print(dut, DUT_MSG_ERROR,
6943 "iwpriv opmode_notify failed!");
6944 } else {
6945 sigma_dut_print(dut, DUT_MSG_INFO,
6946 "Sent out the notify frame!");
6947 }
6948 }
6949
6950 return 1;
6951}
6952
6953
6954static int cmd_sta_send_frame_vht(struct sigma_dut *dut,
6955 struct sigma_conn *conn,
6956 struct sigma_cmd *cmd)
6957{
6958 switch (get_driver_type()) {
6959 case DRIVER_ATHEROS:
6960 return ath_sta_send_frame_vht(dut, conn, cmd);
6961 default:
6962 send_resp(dut, conn, SIGMA_ERROR,
6963 "errorCode,Unsupported sta_set_frame(VHT) with the current driver");
6964 return 0;
6965 }
6966}
6967
6968
Lior David0fe101e2017-03-09 16:09:50 +02006969#ifdef __linux__
6970int wil6210_send_frame_60g(struct sigma_dut *dut, struct sigma_conn *conn,
6971 struct sigma_cmd *cmd)
6972{
6973 const char *frame_name = get_param(cmd, "framename");
6974 const char *mac = get_param(cmd, "dest_mac");
6975
6976 if (!frame_name || !mac) {
6977 sigma_dut_print(dut, DUT_MSG_ERROR,
6978 "framename and dest_mac must be provided");
6979 return -1;
6980 }
6981
6982 if (strcasecmp(frame_name, "brp") == 0) {
6983 const char *l_rx = get_param(cmd, "L-RX");
6984 int l_rx_i;
6985
6986 if (!l_rx) {
6987 sigma_dut_print(dut, DUT_MSG_ERROR,
6988 "L-RX must be provided");
6989 return -1;
6990 }
6991 l_rx_i = atoi(l_rx);
6992
6993 sigma_dut_print(dut, DUT_MSG_INFO,
6994 "dev_send_frame: BRP-RX, dest_mac %s, L-RX %s",
6995 mac, l_rx);
6996 if (l_rx_i != 16) {
6997 sigma_dut_print(dut, DUT_MSG_ERROR,
6998 "unsupported L-RX: %s", l_rx);
6999 return -1;
7000 }
7001
7002 if (wil6210_send_brp_rx(dut, mac, l_rx_i))
7003 return -1;
7004 } else if (strcasecmp(frame_name, "ssw") == 0) {
7005 sigma_dut_print(dut, DUT_MSG_INFO,
7006 "dev_send_frame: SLS, dest_mac %s", mac);
7007 if (wil6210_send_sls(dut, mac))
7008 return -1;
7009 } else {
7010 sigma_dut_print(dut, DUT_MSG_ERROR,
7011 "unsupported frame type: %s", frame_name);
7012 return -1;
7013 }
7014
7015 return 1;
7016}
7017#endif /* __linux__ */
7018
7019
7020static int cmd_sta_send_frame_60g(struct sigma_dut *dut,
7021 struct sigma_conn *conn,
7022 struct sigma_cmd *cmd)
7023{
7024 switch (get_driver_type()) {
7025#ifdef __linux__
7026 case DRIVER_WIL6210:
7027 return wil6210_send_frame_60g(dut, conn, cmd);
7028#endif /* __linux__ */
7029 default:
7030 send_resp(dut, conn, SIGMA_ERROR,
7031 "errorCode,Unsupported sta_set_frame(60G) with the current driver");
7032 return 0;
7033 }
7034}
7035
7036
Ashwini Patildb59b3c2017-04-13 15:19:23 +05307037static int mbo_send_anqp_query(struct sigma_dut *dut, struct sigma_conn *conn,
7038 const char *intf, struct sigma_cmd *cmd)
7039{
7040 const char *val, *addr;
7041 char buf[100];
7042
7043 addr = get_param(cmd, "DestMac");
7044 if (!addr) {
7045 send_resp(dut, conn, SIGMA_INVALID,
7046 "ErrorCode,AP MAC address is missing");
7047 return 0;
7048 }
7049
7050 val = get_param(cmd, "ANQPQuery_ID");
7051 if (!val) {
7052 send_resp(dut, conn, SIGMA_INVALID,
7053 "ErrorCode,Missing ANQPQuery_ID");
7054 return 0;
7055 }
7056
7057 if (strcasecmp(val, "NeighborReportReq") == 0) {
7058 snprintf(buf, sizeof(buf), "ANQP_GET %s 272", addr);
7059 } else if (strcasecmp(val, "QueryListWithCellPref") == 0) {
7060 snprintf(buf, sizeof(buf), "ANQP_GET %s 272,mbo:2", addr);
7061 } else {
7062 sigma_dut_print(dut, DUT_MSG_ERROR, "Invalid ANQPQuery_ID: %s",
7063 val);
7064 send_resp(dut, conn, SIGMA_INVALID,
7065 "ErrorCode,Invalid ANQPQuery_ID");
7066 return 0;
7067 }
7068
Ashwini Patild174f2c2017-04-13 16:49:46 +05307069 /* Set gas_address3 field to IEEE 802.11-2012 standard compliant form
7070 * (Address3 = Wildcard BSSID when sent to not-associated AP;
7071 * if associated, AP BSSID).
7072 */
7073 if (wpa_command(intf, "SET gas_address3 1") < 0) {
7074 send_resp(dut, conn, SIGMA_ERROR,
7075 "ErrorCode,Failed to set gas_address3");
7076 return 0;
7077 }
7078
Ashwini Patildb59b3c2017-04-13 15:19:23 +05307079 if (wpa_command(intf, buf) < 0) {
7080 send_resp(dut, conn, SIGMA_ERROR,
7081 "ErrorCode,Failed to send ANQP query");
7082 return 0;
7083 }
7084
7085 return 1;
7086}
7087
7088
7089static int mbo_cmd_sta_send_frame(struct sigma_dut *dut,
7090 struct sigma_conn *conn,
7091 const char *intf,
7092 struct sigma_cmd *cmd)
7093{
7094 const char *val = get_param(cmd, "FrameName");
7095
7096 if (val && strcasecmp(val, "ANQPQuery") == 0)
7097 return mbo_send_anqp_query(dut, conn, intf, cmd);
7098
7099 return 2;
7100}
7101
7102
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007103int cmd_sta_send_frame(struct sigma_dut *dut, struct sigma_conn *conn,
7104 struct sigma_cmd *cmd)
7105{
7106 const char *intf = get_param(cmd, "Interface");
7107 const char *val;
7108 enum send_frame_type frame;
7109 enum send_frame_protection protected;
7110 char buf[100];
7111 unsigned char addr[ETH_ALEN];
7112 int res;
7113
7114 val = get_param(cmd, "program");
7115 if (val == NULL)
7116 val = get_param(cmd, "frame");
7117 if (val && strcasecmp(val, "TDLS") == 0)
7118 return cmd_sta_send_frame_tdls(dut, conn, cmd);
7119 if (val && (strcasecmp(val, "HS2") == 0 ||
7120 strcasecmp(val, "HS2-R2") == 0))
7121 return cmd_sta_send_frame_hs2(dut, conn, cmd);
7122 if (val && strcasecmp(val, "VHT") == 0)
7123 return cmd_sta_send_frame_vht(dut, conn, cmd);
priyadharshini gowthamand66913a2016-07-29 15:11:17 -07007124 if (val && strcasecmp(val, "LOC") == 0)
7125 return loc_cmd_sta_send_frame(dut, conn, cmd);
Lior David0fe101e2017-03-09 16:09:50 +02007126 if (val && strcasecmp(val, "60GHz") == 0)
7127 return cmd_sta_send_frame_60g(dut, conn, cmd);
Ashwini Patildb59b3c2017-04-13 15:19:23 +05307128 if (val && strcasecmp(val, "MBO") == 0) {
7129 res = mbo_cmd_sta_send_frame(dut, conn, intf, cmd);
7130 if (res != 2)
7131 return res;
7132 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007133
7134 val = get_param(cmd, "TD_DISC");
7135 if (val) {
7136 if (hwaddr_aton(val, addr) < 0)
7137 return -1;
7138 snprintf(buf, sizeof(buf), "TDLS_DISCOVER %s", val);
7139 if (wpa_command(intf, buf) < 0) {
7140 send_resp(dut, conn, SIGMA_ERROR,
7141 "ErrorCode,Failed to send TDLS discovery");
7142 return 0;
7143 }
7144 return 1;
7145 }
7146
7147 val = get_param(cmd, "TD_Setup");
7148 if (val) {
7149 if (hwaddr_aton(val, addr) < 0)
7150 return -1;
7151 snprintf(buf, sizeof(buf), "TDLS_SETUP %s", val);
7152 if (wpa_command(intf, buf) < 0) {
7153 send_resp(dut, conn, SIGMA_ERROR,
7154 "ErrorCode,Failed to start TDLS setup");
7155 return 0;
7156 }
7157 return 1;
7158 }
7159
7160 val = get_param(cmd, "TD_TearDown");
7161 if (val) {
7162 if (hwaddr_aton(val, addr) < 0)
7163 return -1;
7164 snprintf(buf, sizeof(buf), "TDLS_TEARDOWN %s", val);
7165 if (wpa_command(intf, buf) < 0) {
7166 send_resp(dut, conn, SIGMA_ERROR,
7167 "ErrorCode,Failed to tear down TDLS link");
7168 return 0;
7169 }
7170 return 1;
7171 }
7172
7173 val = get_param(cmd, "TD_ChannelSwitch");
7174 if (val) {
7175 /* TODO */
7176 send_resp(dut, conn, SIGMA_ERROR,
7177 "ErrorCode,TD_ChannelSwitch not yet supported");
7178 return 0;
7179 }
7180
7181 val = get_param(cmd, "TD_NF");
7182 if (val) {
7183 /* TODO */
7184 send_resp(dut, conn, SIGMA_ERROR,
7185 "ErrorCode,TD_NF not yet supported");
7186 return 0;
7187 }
7188
7189 val = get_param(cmd, "PMFFrameType");
7190 if (val == NULL)
7191 val = get_param(cmd, "FrameName");
7192 if (val == NULL)
7193 val = get_param(cmd, "Type");
7194 if (val == NULL)
7195 return -1;
7196 if (strcasecmp(val, "disassoc") == 0)
7197 frame = DISASSOC;
7198 else if (strcasecmp(val, "deauth") == 0)
7199 frame = DEAUTH;
7200 else if (strcasecmp(val, "saquery") == 0)
7201 frame = SAQUERY;
7202 else if (strcasecmp(val, "auth") == 0)
7203 frame = AUTH;
7204 else if (strcasecmp(val, "assocreq") == 0)
7205 frame = ASSOCREQ;
7206 else if (strcasecmp(val, "reassocreq") == 0)
7207 frame = REASSOCREQ;
7208 else if (strcasecmp(val, "neigreq") == 0) {
7209 sigma_dut_print(dut, DUT_MSG_INFO, "Got neighbor request");
7210
7211 val = get_param(cmd, "ssid");
7212 if (val == NULL)
7213 return -1;
7214
7215 res = send_neighbor_request(dut, intf, val);
7216 if (res) {
7217 send_resp(dut, conn, SIGMA_ERROR, "errorCode,"
7218 "Failed to send neighbor report request");
7219 return 0;
7220 }
7221
7222 return 1;
Ashwini Patil5acd7382017-04-13 15:55:04 +05307223 } else if (strcasecmp(val, "transmgmtquery") == 0 ||
7224 strcasecmp(val, "BTMQuery") == 0) {
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007225 sigma_dut_print(dut, DUT_MSG_DEBUG,
7226 "Got Transition Management Query");
7227
Ashwini Patil5acd7382017-04-13 15:55:04 +05307228 res = send_trans_mgmt_query(dut, intf, cmd);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007229 if (res) {
7230 send_resp(dut, conn, SIGMA_ERROR, "errorCode,"
7231 "Failed to send Transition Management Query");
7232 return 0;
7233 }
7234
7235 return 1;
7236 } else {
7237 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Unsupported "
7238 "PMFFrameType");
7239 return 0;
7240 }
7241
7242 val = get_param(cmd, "PMFProtected");
7243 if (val == NULL)
7244 val = get_param(cmd, "Protected");
7245 if (val == NULL)
7246 return -1;
7247 if (strcasecmp(val, "Correct-key") == 0 ||
7248 strcasecmp(val, "CorrectKey") == 0)
7249 protected = CORRECT_KEY;
7250 else if (strcasecmp(val, "IncorrectKey") == 0)
7251 protected = INCORRECT_KEY;
7252 else if (strcasecmp(val, "Unprotected") == 0)
7253 protected = UNPROTECTED;
7254 else {
7255 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Unsupported "
7256 "PMFProtected");
7257 return 0;
7258 }
7259
7260 if (protected != UNPROTECTED &&
7261 (frame == AUTH || frame == ASSOCREQ || frame == REASSOCREQ)) {
7262 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Impossible "
7263 "PMFProtected for auth/assocreq/reassocreq");
7264 return 0;
7265 }
7266
7267 if (if_nametoindex("sigmadut") == 0) {
7268 snprintf(buf, sizeof(buf),
7269 "iw dev %s interface add sigmadut type monitor",
7270 get_station_ifname());
7271 if (system(buf) != 0 ||
7272 if_nametoindex("sigmadut") == 0) {
7273 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to add "
7274 "monitor interface with '%s'", buf);
7275 return -2;
7276 }
7277 }
7278
7279 if (system("ifconfig sigmadut up") != 0) {
7280 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to set "
7281 "monitor interface up");
7282 return -2;
7283 }
7284
7285 return sta_inject_frame(dut, conn, frame, protected, NULL);
7286}
7287
7288
7289static int cmd_sta_set_parameter_hs2(struct sigma_dut *dut,
7290 struct sigma_conn *conn,
7291 struct sigma_cmd *cmd,
7292 const char *ifname)
7293{
7294 char buf[200];
7295 const char *val;
7296
7297 val = get_param(cmd, "ClearARP");
7298 if (val && atoi(val) == 1) {
7299 snprintf(buf, sizeof(buf), "ip neigh flush dev %s", ifname);
7300 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
7301 if (system(buf) != 0) {
7302 send_resp(dut, conn, SIGMA_ERROR,
7303 "errorCode,Failed to clear ARP cache");
7304 return 0;
7305 }
7306 }
7307
7308 return 1;
7309}
7310
7311
7312int cmd_sta_set_parameter(struct sigma_dut *dut, struct sigma_conn *conn,
7313 struct sigma_cmd *cmd)
7314{
7315 const char *intf = get_param(cmd, "Interface");
7316 const char *val;
7317
7318 if (intf == NULL)
7319 return -1;
7320
7321 val = get_param(cmd, "program");
7322 if (val && (strcasecmp(val, "HS2") == 0 ||
7323 strcasecmp(val, "HS2-R2") == 0))
7324 return cmd_sta_set_parameter_hs2(dut, conn, cmd, intf);
7325
7326 return -1;
7327}
7328
7329
7330static int cmd_sta_set_macaddr(struct sigma_dut *dut, struct sigma_conn *conn,
7331 struct sigma_cmd *cmd)
7332{
7333 const char *intf = get_param(cmd, "Interface");
7334 const char *mac = get_param(cmd, "MAC");
7335
7336 if (intf == NULL || mac == NULL)
7337 return -1;
7338
7339 sigma_dut_print(dut, DUT_MSG_INFO, "Change local MAC address for "
7340 "interface %s to %s", intf, mac);
7341
7342 if (dut->set_macaddr) {
7343 char buf[128];
7344 int res;
7345 if (strcasecmp(mac, "default") == 0) {
7346 res = snprintf(buf, sizeof(buf), "%s",
7347 dut->set_macaddr);
7348 dut->tmp_mac_addr = 0;
7349 } else {
7350 res = snprintf(buf, sizeof(buf), "%s %s",
7351 dut->set_macaddr, mac);
7352 dut->tmp_mac_addr = 1;
7353 }
7354 if (res < 0 || res >= (int) sizeof(buf))
7355 return -1;
7356 if (system(buf) != 0) {
7357 send_resp(dut, conn, SIGMA_ERROR,
7358 "errorCode,Failed to set MAC "
7359 "address");
7360 return 0;
7361 }
7362 return 1;
7363 }
7364
7365 if (strcasecmp(mac, "default") == 0)
7366 return 1;
7367
7368 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Unsupported "
7369 "command");
7370 return 0;
7371}
7372
7373
7374static int iwpriv_tdlsoffchnmode(struct sigma_dut *dut,
7375 struct sigma_conn *conn, const char *intf,
7376 int val)
7377{
7378 char buf[200];
7379 int res;
7380
7381 res = snprintf(buf, sizeof(buf), "iwpriv %s tdlsoffchnmode %d",
7382 intf, val);
7383 if (res < 0 || res >= (int) sizeof(buf))
7384 return -1;
7385 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
7386 if (system(buf) != 0) {
7387 send_resp(dut, conn, SIGMA_ERROR,
7388 "errorCode,Failed to configure offchannel mode");
7389 return 0;
7390 }
7391
7392 return 1;
7393}
7394
7395
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007396static int off_chan_val(enum sec_ch_offset off)
7397{
7398 switch (off) {
7399 case SEC_CH_NO:
7400 return 0;
7401 case SEC_CH_40ABOVE:
7402 return 40;
7403 case SEC_CH_40BELOW:
7404 return -40;
7405 }
7406
7407 return 0;
7408}
7409
7410
7411static int iwpriv_set_offchan(struct sigma_dut *dut, struct sigma_conn *conn,
7412 const char *intf, int off_ch_num,
7413 enum sec_ch_offset sec)
7414{
7415 char buf[200];
7416 int res;
7417
7418 res = snprintf(buf, sizeof(buf), "iwpriv %s tdlsoffchan %d",
7419 intf, off_ch_num);
7420 if (res < 0 || res >= (int) sizeof(buf))
7421 return -1;
7422 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
7423 if (system(buf) != 0) {
7424 send_resp(dut, conn, SIGMA_ERROR,
7425 "errorCode,Failed to set offchan");
7426 return 0;
7427 }
7428
7429 res = snprintf(buf, sizeof(buf), "iwpriv %s tdlsecchnoffst %d",
7430 intf, off_chan_val(sec));
7431 if (res < 0 || res >= (int) sizeof(buf))
7432 return -1;
7433 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
7434 if (system(buf) != 0) {
7435 send_resp(dut, conn, SIGMA_ERROR,
7436 "errorCode,Failed to set sec chan offset");
7437 return 0;
7438 }
7439
7440 return 1;
7441}
7442
7443
7444static int tdls_set_offchannel_offset(struct sigma_dut *dut,
7445 struct sigma_conn *conn,
7446 const char *intf, int off_ch_num,
7447 enum sec_ch_offset sec)
7448{
7449 char buf[200];
7450 int res;
7451
7452 res = snprintf(buf, sizeof(buf), "DRIVER TDLSOFFCHANNEL %d",
7453 off_ch_num);
7454 if (res < 0 || res >= (int) sizeof(buf))
7455 return -1;
7456 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
7457
7458 if (wpa_command(intf, buf) < 0) {
7459 send_resp(dut, conn, SIGMA_ERROR,
7460 "ErrorCode,Failed to set offchan");
7461 return 0;
7462 }
7463 res = snprintf(buf, sizeof(buf), "DRIVER TDLSSECONDARYCHANNELOFFSET %d",
7464 off_chan_val(sec));
7465 if (res < 0 || res >= (int) sizeof(buf))
7466 return -1;
7467
7468 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
7469
7470 if (wpa_command(intf, buf) < 0) {
7471 send_resp(dut, conn, SIGMA_ERROR,
7472 "ErrorCode,Failed to set sec chan offset");
7473 return 0;
7474 }
7475
7476 return 1;
7477}
7478
7479
7480static int tdls_set_offchannel_mode(struct sigma_dut *dut,
7481 struct sigma_conn *conn,
7482 const char *intf, int val)
7483{
7484 char buf[200];
7485 int res;
7486
7487 res = snprintf(buf, sizeof(buf), "DRIVER TDLSOFFCHANNELMODE %d",
7488 val);
7489 if (res < 0 || res >= (int) sizeof(buf))
7490 return -1;
7491 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
7492
7493 if (wpa_command(intf, buf) < 0) {
7494 send_resp(dut, conn, SIGMA_ERROR,
7495 "ErrorCode,Failed to configure offchannel mode");
7496 return 0;
7497 }
7498
7499 return 1;
7500}
7501
7502
7503static int cmd_sta_set_rfeature_tdls(const char *intf, struct sigma_dut *dut,
7504 struct sigma_conn *conn,
7505 struct sigma_cmd *cmd)
7506{
7507 const char *val;
7508 enum {
7509 CHSM_NOT_SET,
7510 CHSM_ENABLE,
7511 CHSM_DISABLE,
7512 CHSM_REJREQ,
7513 CHSM_UNSOLRESP
7514 } chsm = CHSM_NOT_SET;
7515 int off_ch_num = -1;
7516 enum sec_ch_offset sec_ch = SEC_CH_NO;
7517 int res;
7518
7519 val = get_param(cmd, "Uapsd");
7520 if (val) {
7521 char buf[100];
7522 if (strcasecmp(val, "Enable") == 0)
7523 snprintf(buf, sizeof(buf), "SET ps 99");
7524 else if (strcasecmp(val, "Disable") == 0)
7525 snprintf(buf, sizeof(buf), "SET ps 98");
7526 else {
7527 send_resp(dut, conn, SIGMA_ERROR, "errorCode,"
7528 "Unsupported uapsd parameter value");
7529 return 0;
7530 }
7531 if (wpa_command(intf, buf)) {
7532 send_resp(dut, conn, SIGMA_ERROR,
7533 "ErrorCode,Failed to change U-APSD "
7534 "powersave mode");
7535 return 0;
7536 }
7537 }
7538
7539 val = get_param(cmd, "TPKTIMER");
7540 if (val && strcasecmp(val, "DISABLE") == 0) {
7541 if (wpa_command(intf, "SET tdls_testing 0x100")) {
7542 send_resp(dut, conn, SIGMA_ERROR,
7543 "ErrorCode,Failed to enable no TPK "
7544 "expiration test mode");
7545 return 0;
7546 }
7547 dut->no_tpk_expiration = 1;
7548 }
7549
7550 val = get_param(cmd, "ChSwitchMode");
7551 if (val) {
7552 if (strcasecmp(val, "Enable") == 0 ||
7553 strcasecmp(val, "Initiate") == 0)
7554 chsm = CHSM_ENABLE;
7555 else if (strcasecmp(val, "Disable") == 0 ||
7556 strcasecmp(val, "passive") == 0)
7557 chsm = CHSM_DISABLE;
7558 else if (strcasecmp(val, "RejReq") == 0)
7559 chsm = CHSM_REJREQ;
7560 else if (strcasecmp(val, "UnSolResp") == 0)
7561 chsm = CHSM_UNSOLRESP;
7562 else {
7563 send_resp(dut, conn, SIGMA_ERROR,
7564 "ErrorCode,Unknown ChSwitchMode value");
7565 return 0;
7566 }
7567 }
7568
7569 val = get_param(cmd, "OffChNum");
7570 if (val) {
7571 off_ch_num = atoi(val);
7572 if (off_ch_num == 0) {
7573 send_resp(dut, conn, SIGMA_ERROR,
7574 "ErrorCode,Invalid OffChNum");
7575 return 0;
7576 }
7577 }
7578
7579 val = get_param(cmd, "SecChOffset");
7580 if (val) {
7581 if (strcmp(val, "20") == 0)
7582 sec_ch = SEC_CH_NO;
7583 else if (strcasecmp(val, "40above") == 0)
7584 sec_ch = SEC_CH_40ABOVE;
7585 else if (strcasecmp(val, "40below") == 0)
7586 sec_ch = SEC_CH_40BELOW;
7587 else {
7588 send_resp(dut, conn, SIGMA_ERROR,
7589 "ErrorCode,Unknown SecChOffset value");
7590 return 0;
7591 }
7592 }
7593
7594 if (chsm == CHSM_NOT_SET) {
7595 /* no offchannel changes requested */
7596 return 1;
7597 }
7598
7599 if (strcmp(intf, get_main_ifname()) != 0 &&
7600 strcmp(intf, get_station_ifname()) != 0) {
7601 send_resp(dut, conn, SIGMA_ERROR,
7602 "ErrorCode,Unknown interface");
7603 return 0;
7604 }
7605
7606 switch (chsm) {
7607 case CHSM_NOT_SET:
Jouni Malinen280f5ba2016-08-29 21:33:10 +03007608 res = 1;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007609 break;
7610 case CHSM_ENABLE:
7611 if (off_ch_num < 0) {
7612 send_resp(dut, conn, SIGMA_ERROR,
7613 "ErrorCode,Missing OffChNum argument");
7614 return 0;
7615 }
7616 if (wifi_chip_type == DRIVER_WCN) {
7617 res = tdls_set_offchannel_offset(dut, conn, intf,
7618 off_ch_num, sec_ch);
7619 } else {
7620 res = iwpriv_set_offchan(dut, conn, intf, off_ch_num,
7621 sec_ch);
7622 }
7623 if (res != 1)
7624 return res;
7625 if (wifi_chip_type == DRIVER_WCN)
7626 res = tdls_set_offchannel_mode(dut, conn, intf, 1);
7627 else
7628 res = iwpriv_tdlsoffchnmode(dut, conn, intf, 1);
7629 break;
7630 case CHSM_DISABLE:
7631 if (wifi_chip_type == DRIVER_WCN)
7632 res = tdls_set_offchannel_mode(dut, conn, intf, 2);
7633 else
7634 res = iwpriv_tdlsoffchnmode(dut, conn, intf, 2);
7635 break;
7636 case CHSM_REJREQ:
7637 if (wifi_chip_type == DRIVER_WCN)
7638 res = tdls_set_offchannel_mode(dut, conn, intf, 3);
7639 else
7640 res = iwpriv_tdlsoffchnmode(dut, conn, intf, 3);
7641 break;
7642 case CHSM_UNSOLRESP:
7643 if (off_ch_num < 0) {
7644 send_resp(dut, conn, SIGMA_ERROR,
7645 "ErrorCode,Missing OffChNum argument");
7646 return 0;
7647 }
7648 if (wifi_chip_type == DRIVER_WCN) {
7649 res = tdls_set_offchannel_offset(dut, conn, intf,
7650 off_ch_num, sec_ch);
7651 } else {
7652 res = iwpriv_set_offchan(dut, conn, intf, off_ch_num,
7653 sec_ch);
7654 }
7655 if (res != 1)
7656 return res;
7657 if (wifi_chip_type == DRIVER_WCN)
7658 res = tdls_set_offchannel_mode(dut, conn, intf, 4);
7659 else
7660 res = iwpriv_tdlsoffchnmode(dut, conn, intf, 4);
7661 break;
7662 }
7663
7664 return res;
7665}
7666
7667
7668static int ath_sta_set_rfeature_vht(const char *intf, struct sigma_dut *dut,
7669 struct sigma_conn *conn,
7670 struct sigma_cmd *cmd)
7671{
7672 const char *val;
7673 char *token, *result;
7674
priyadharshini gowthamane5e25172015-12-08 14:53:48 -08007675 novap_reset(dut, intf);
7676
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007677 val = get_param(cmd, "nss_mcs_opt");
7678 if (val) {
7679 /* String (nss_operating_mode; mcs_operating_mode) */
7680 int nss, mcs;
7681 char buf[50];
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05307682 char *saveptr;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007683
7684 token = strdup(val);
7685 if (!token)
7686 return 0;
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05307687 result = strtok_r(token, ";", &saveptr);
Pradeep Reddy POTTETI41b8c542016-06-15 16:09:46 +05307688 if (!result) {
7689 sigma_dut_print(dut, DUT_MSG_ERROR,
7690 "VHT NSS not specified");
7691 goto failed;
7692 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007693 if (strcasecmp(result, "def") != 0) {
7694 nss = atoi(result);
7695 if (nss == 4)
7696 ath_disable_txbf(dut, intf);
7697 snprintf(buf, sizeof(buf), "iwpriv %s nss %d",
7698 intf, nss);
7699 if (system(buf) != 0) {
7700 sigma_dut_print(dut, DUT_MSG_ERROR,
7701 "iwpriv nss failed");
7702 goto failed;
7703 }
7704 }
7705
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05307706 result = strtok_r(NULL, ";", &saveptr);
Pradeep Reddy POTTETI41b8c542016-06-15 16:09:46 +05307707 if (!result) {
7708 sigma_dut_print(dut, DUT_MSG_ERROR,
7709 "VHT MCS not specified");
7710 goto failed;
7711 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007712 if (strcasecmp(result, "def") == 0) {
7713 snprintf(buf, sizeof(buf), "iwpriv %s set11NRates 0",
7714 intf);
7715 if (system(buf) != 0) {
7716 sigma_dut_print(dut, DUT_MSG_ERROR,
7717 "iwpriv set11NRates failed");
7718 goto failed;
7719 }
7720
7721 } else {
7722 mcs = atoi(result);
7723 snprintf(buf, sizeof(buf), "iwpriv %s vhtmcs %d",
7724 intf, mcs);
7725 if (system(buf) != 0) {
7726 sigma_dut_print(dut, DUT_MSG_ERROR,
7727 "iwpriv vhtmcs failed");
7728 goto failed;
7729 }
7730 }
7731 /* Channel width gets messed up, fix this */
7732 snprintf(buf, sizeof(buf), "iwpriv %s chwidth %d",
7733 intf, dut->chwidth);
7734 if (system(buf) != 0) {
7735 sigma_dut_print(dut, DUT_MSG_ERROR,
7736 "iwpriv chwidth failed");
7737 }
7738 }
7739
7740 return 1;
7741failed:
7742 free(token);
7743 return 0;
7744}
7745
7746
7747static int cmd_sta_set_rfeature_vht(const char *intf, struct sigma_dut *dut,
7748 struct sigma_conn *conn,
7749 struct sigma_cmd *cmd)
7750{
7751 switch (get_driver_type()) {
7752 case DRIVER_ATHEROS:
7753 return ath_sta_set_rfeature_vht(intf, dut, conn, cmd);
7754 default:
7755 send_resp(dut, conn, SIGMA_ERROR,
7756 "errorCode,Unsupported sta_set_rfeature(VHT) with the current driver");
7757 return 0;
7758 }
7759}
7760
7761
Ashwini Patil5acd7382017-04-13 15:55:04 +05307762static int btm_query_candidate_list(struct sigma_dut *dut,
7763 struct sigma_conn *conn,
7764 struct sigma_cmd *cmd)
7765{
7766 const char *bssid, *info, *op_class, *ch, *phy_type, *pref;
7767 int len, ret;
7768 char buf[10];
7769
7770 /*
7771 * Neighbor Report elements format:
7772 * neighbor=<BSSID>,<BSSID Information>,<Operating Class>,
7773 * <Channel Number>,<PHY Type>[,<hexdump of Optional Subelements>]
7774 * eg: neighbor=aa:bb:cc:dd:ee:ff,17,81,6,1,030101
7775 */
7776
7777 bssid = get_param(cmd, "Nebor_BSSID");
7778 if (!bssid) {
7779 send_resp(dut, conn, SIGMA_INVALID,
7780 "errorCode,Nebor_BSSID is missing");
7781 return 0;
7782 }
7783
7784 info = get_param(cmd, "Nebor_Bssid_Info");
7785 if (!info) {
7786 sigma_dut_print(dut, DUT_MSG_INFO,
7787 "Using default value for Nebor_Bssid_Info: %s",
7788 DEFAULT_NEIGHBOR_BSSID_INFO);
7789 info = DEFAULT_NEIGHBOR_BSSID_INFO;
7790 }
7791
7792 op_class = get_param(cmd, "Nebor_Op_Class");
7793 if (!op_class) {
7794 send_resp(dut, conn, SIGMA_INVALID,
7795 "errorCode,Nebor_Op_Class is missing");
7796 return 0;
7797 }
7798
7799 ch = get_param(cmd, "Nebor_Op_Ch");
7800 if (!ch) {
7801 send_resp(dut, conn, SIGMA_INVALID,
7802 "errorCode,Nebor_Op_Ch is missing");
7803 return 0;
7804 }
7805
7806 phy_type = get_param(cmd, "Nebor_Phy_Type");
7807 if (!phy_type) {
7808 sigma_dut_print(dut, DUT_MSG_INFO,
7809 "Using default value for Nebor_Phy_Type: %s",
7810 DEFAULT_NEIGHBOR_PHY_TYPE);
7811 phy_type = DEFAULT_NEIGHBOR_PHY_TYPE;
7812 }
7813
7814 /* Parse optional subelements */
7815 buf[0] = '\0';
7816 pref = get_param(cmd, "Nebor_Pref");
7817 if (pref) {
7818 /* hexdump for preferrence subelement */
7819 ret = snprintf(buf, sizeof(buf), ",0301%02x", atoi(pref));
7820 if (ret < 0 || ret >= (int) sizeof(buf)) {
7821 sigma_dut_print(dut, DUT_MSG_ERROR,
7822 "snprintf failed for optional subelement ret: %d",
7823 ret);
7824 send_resp(dut, conn, SIGMA_ERROR,
7825 "errorCode,snprintf failed for subelement");
7826 return 0;
7827 }
7828 }
7829
7830 if (!dut->btm_query_cand_list) {
7831 dut->btm_query_cand_list = calloc(1, NEIGHBOR_REPORT_SIZE);
7832 if (!dut->btm_query_cand_list) {
7833 send_resp(dut, conn, SIGMA_ERROR,
7834 "errorCode,Failed to allocate memory for btm_query_cand_list");
7835 return 0;
7836 }
7837 }
7838
7839 len = strlen(dut->btm_query_cand_list);
7840 ret = snprintf(dut->btm_query_cand_list + len,
7841 NEIGHBOR_REPORT_SIZE - len, " neighbor=%s,%s,%s,%s,%s%s",
7842 bssid, info, op_class, ch, phy_type, buf);
7843 if (ret < 0 || ret >= NEIGHBOR_REPORT_SIZE - len) {
7844 sigma_dut_print(dut, DUT_MSG_ERROR,
7845 "snprintf failed for neighbor report list ret: %d",
7846 ret);
7847 send_resp(dut, conn, SIGMA_ERROR,
7848 "errorCode,snprintf failed for neighbor report");
7849 free(dut->btm_query_cand_list);
7850 dut->btm_query_cand_list = NULL;
7851 return 0;
7852 }
7853
7854 return 1;
7855}
7856
7857
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007858static int cmd_sta_set_rfeature(struct sigma_dut *dut, struct sigma_conn *conn,
7859 struct sigma_cmd *cmd)
7860{
7861 const char *intf = get_param(cmd, "Interface");
7862 const char *prog = get_param(cmd, "Prog");
Ashwini Patil68d02cd2017-01-10 15:39:16 +05307863 const char *val;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007864
7865 if (intf == NULL || prog == NULL)
7866 return -1;
7867
Ashwini Patil5acd7382017-04-13 15:55:04 +05307868 /* BSS Transition candidate list for BTM query */
7869 val = get_param(cmd, "Nebor_BSSID");
7870 if (val && btm_query_candidate_list(dut, conn, cmd) == 0)
7871 return 0;
7872
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007873 if (strcasecmp(prog, "TDLS") == 0)
7874 return cmd_sta_set_rfeature_tdls(intf, dut, conn, cmd);
7875
7876 if (strcasecmp(prog, "VHT") == 0)
7877 return cmd_sta_set_rfeature_vht(intf, dut, conn, cmd);
7878
Ashwini Patil68d02cd2017-01-10 15:39:16 +05307879 if (strcasecmp(prog, "MBO") == 0) {
7880 val = get_param(cmd, "Cellular_Data_Cap");
7881 if (val &&
7882 mbo_set_cellular_data_capa(dut, conn, intf, atoi(val)) == 0)
7883 return 0;
Ashwini Patil00402582017-04-13 12:29:39 +05307884
7885 val = get_param(cmd, "Ch_Pref");
7886 if (val && mbo_set_non_pref_ch_list(dut, conn, intf, cmd) == 0)
7887 return 0;
7888
Ashwini Patil68d02cd2017-01-10 15:39:16 +05307889 return 1;
7890 }
7891
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007892 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Unsupported Prog");
7893 return 0;
7894}
7895
7896
7897static int cmd_sta_set_radio(struct sigma_dut *dut, struct sigma_conn *conn,
7898 struct sigma_cmd *cmd)
7899{
7900 const char *intf = get_param(cmd, "Interface");
7901 const char *mode = get_param(cmd, "Mode");
7902 int res;
7903
7904 if (intf == NULL || mode == NULL)
7905 return -1;
7906
7907 if (strcasecmp(mode, "On") == 0)
7908 res = wpa_command(intf, "SET radio_disabled 0");
7909 else if (strcasecmp(mode, "Off") == 0)
7910 res = wpa_command(intf, "SET radio_disabled 1");
7911 else
7912 return -1;
7913
7914 if (res) {
7915 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to change "
7916 "radio mode");
7917 return 0;
7918 }
7919
7920 return 1;
7921}
7922
7923
7924static int cmd_sta_set_pwrsave(struct sigma_dut *dut, struct sigma_conn *conn,
7925 struct sigma_cmd *cmd)
7926{
7927 const char *intf = get_param(cmd, "Interface");
7928 const char *mode = get_param(cmd, "Mode");
7929 int res;
7930
7931 if (intf == NULL || mode == NULL)
7932 return -1;
7933
7934 if (strcasecmp(mode, "On") == 0)
7935 res = set_ps(intf, dut, 1);
7936 else if (strcasecmp(mode, "Off") == 0)
7937 res = set_ps(intf, dut, 0);
7938 else
7939 return -1;
7940
7941 if (res) {
7942 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to change "
7943 "power save mode");
7944 return 0;
7945 }
7946
7947 return 1;
7948}
7949
7950
7951static int cmd_sta_bssid_pool(struct sigma_dut *dut, struct sigma_conn *conn,
7952 struct sigma_cmd *cmd)
7953{
7954 const char *intf = get_param(cmd, "Interface");
7955 const char *val, *bssid;
7956 int res;
7957 char *buf;
7958 size_t buf_len;
7959
7960 val = get_param(cmd, "BSSID_FILTER");
7961 if (val == NULL)
7962 return -1;
7963
7964 bssid = get_param(cmd, "BSSID_List");
7965 if (atoi(val) == 0 || bssid == NULL) {
7966 /* Disable BSSID filter */
7967 if (wpa_command(intf, "SET bssid_filter ")) {
7968 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed "
7969 "to disable BSSID filter");
7970 return 0;
7971 }
7972
7973 return 1;
7974 }
7975
7976 buf_len = 100 + strlen(bssid);
7977 buf = malloc(buf_len);
7978 if (buf == NULL)
7979 return -1;
7980
7981 snprintf(buf, buf_len, "SET bssid_filter %s", bssid);
7982 res = wpa_command(intf, buf);
7983 free(buf);
7984 if (res) {
7985 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to enable "
7986 "BSSID filter");
7987 return 0;
7988 }
7989
7990 return 1;
7991}
7992
7993
7994static int cmd_sta_reset_parm(struct sigma_dut *dut, struct sigma_conn *conn,
7995 struct sigma_cmd *cmd)
7996{
7997 const char *intf = get_param(cmd, "Interface");
7998 const char *val;
7999
8000 /* TODO: ARP */
8001
8002 val = get_param(cmd, "HS2_CACHE_PROFILE");
8003 if (val && strcasecmp(val, "All") == 0)
8004 hs2_clear_credentials(intf);
8005
8006 return 1;
8007}
8008
8009
8010static int cmd_sta_get_key(struct sigma_dut *dut, struct sigma_conn *conn,
8011 struct sigma_cmd *cmd)
8012{
8013 const char *intf = get_param(cmd, "Interface");
8014 const char *key_type = get_param(cmd, "KeyType");
8015 char buf[100], resp[200];
8016
8017 if (key_type == NULL)
8018 return -1;
8019
8020 if (strcasecmp(key_type, "GTK") == 0) {
8021 if (wpa_command_resp(intf, "GET gtk", buf, sizeof(buf)) < 0 ||
8022 strncmp(buf, "FAIL", 4) == 0) {
8023 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could "
8024 "not fetch current GTK");
8025 return 0;
8026 }
8027 snprintf(resp, sizeof(resp), "KeyValue,%s", buf);
8028 send_resp(dut, conn, SIGMA_COMPLETE, resp);
8029 return 0;
8030 } else {
8031 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Unsupported "
8032 "KeyType");
8033 return 0;
8034 }
8035
8036 return 1;
8037}
8038
8039
8040static int hs2_set_policy(struct sigma_dut *dut)
8041{
8042#ifdef ANDROID
8043 system("ip rule del prio 23000");
8044 if (system("ip rule add from all lookup main prio 23000") != 0) {
8045 sigma_dut_print(dut, DUT_MSG_ERROR,
8046 "Failed to run:ip rule add from all lookup main prio");
8047 return -1;
8048 }
8049 if (system("ip route flush cache") != 0) {
8050 sigma_dut_print(dut, DUT_MSG_ERROR,
8051 "Failed to run ip route flush cache");
8052 return -1;
8053 }
8054 return 1;
8055#else /* ANDROID */
8056 return 0;
8057#endif /* ANDROID */
8058}
8059
8060
8061static int cmd_sta_hs2_associate(struct sigma_dut *dut,
8062 struct sigma_conn *conn,
8063 struct sigma_cmd *cmd)
8064{
8065 const char *intf = get_param(cmd, "Interface");
8066 const char *val = get_param(cmd, "Ignore_blacklist");
8067 struct wpa_ctrl *ctrl;
8068 int res;
8069 char bssid[20], ssid[40], resp[100], buf[100], blacklisted[100];
8070 int tries = 0;
8071 int ignore_blacklist = 0;
8072 const char *events[] = {
8073 "CTRL-EVENT-CONNECTED",
8074 "INTERWORKING-BLACKLISTED",
8075 "INTERWORKING-NO-MATCH",
8076 NULL
8077 };
8078
8079 start_sta_mode(dut);
8080
8081 blacklisted[0] = '\0';
8082 if (val && atoi(val))
8083 ignore_blacklist = 1;
8084
8085try_again:
8086 ctrl = open_wpa_mon(intf);
8087 if (ctrl == NULL) {
8088 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to open "
8089 "wpa_supplicant monitor connection");
8090 return -2;
8091 }
8092
8093 tries++;
8094 if (wpa_command(intf, "INTERWORKING_SELECT auto")) {
8095 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to start "
8096 "Interworking connection");
8097 wpa_ctrl_detach(ctrl);
8098 wpa_ctrl_close(ctrl);
8099 return 0;
8100 }
8101
8102 buf[0] = '\0';
8103 while (1) {
8104 char *pos;
8105 res = get_wpa_cli_events(dut, ctrl, events, buf, sizeof(buf));
8106 pos = strstr(buf, "INTERWORKING-BLACKLISTED");
8107 if (!pos)
8108 break;
8109 pos += 25;
8110 sigma_dut_print(dut, DUT_MSG_DEBUG, "Found blacklisted AP: %s",
8111 pos);
8112 if (!blacklisted[0])
8113 memcpy(blacklisted, pos, strlen(pos) + 1);
8114 }
8115
8116 if (ignore_blacklist && blacklisted[0]) {
8117 char *end;
8118 end = strchr(blacklisted, ' ');
8119 if (end)
8120 *end = '\0';
8121 sigma_dut_print(dut, DUT_MSG_DEBUG, "Try to connect to a blacklisted network: %s",
8122 blacklisted);
8123 snprintf(buf, sizeof(buf), "INTERWORKING_CONNECT %s",
8124 blacklisted);
8125 if (wpa_command(intf, buf)) {
8126 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to start Interworking connection to blacklisted network");
8127 wpa_ctrl_detach(ctrl);
8128 wpa_ctrl_close(ctrl);
8129 return 0;
8130 }
8131 res = get_wpa_cli_event(dut, ctrl, "CTRL-EVENT-CONNECTED",
8132 buf, sizeof(buf));
8133 }
8134
8135 wpa_ctrl_detach(ctrl);
8136 wpa_ctrl_close(ctrl);
8137
8138 if (res < 0) {
8139 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Could not "
8140 "connect");
8141 return 0;
8142 }
8143
8144 if (strstr(buf, "INTERWORKING-NO-MATCH") ||
8145 strstr(buf, "INTERWORKING-BLACKLISTED")) {
8146 if (tries < 2) {
8147 sigma_dut_print(dut, DUT_MSG_INFO, "No match found - try again to verify no APs were missed in the scan");
8148 goto try_again;
8149 }
8150 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,No network with "
8151 "matching credentials found");
8152 return 0;
8153 }
8154
8155 if (get_wpa_status(intf, "bssid", bssid, sizeof(bssid)) < 0 ||
8156 get_wpa_status(intf, "ssid", ssid, sizeof(ssid)) < 0) {
8157 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Could not "
8158 "get current BSSID/SSID");
8159 return 0;
8160 }
8161
8162 snprintf(resp, sizeof(resp), "SSID,%s,BSSID,%s", ssid, bssid);
8163 send_resp(dut, conn, SIGMA_COMPLETE, resp);
8164 hs2_set_policy(dut);
8165 return 0;
8166}
8167
8168
8169static int sta_add_credential_uname_pwd(struct sigma_dut *dut,
8170 struct sigma_conn *conn,
8171 const char *ifname,
8172 struct sigma_cmd *cmd)
8173{
8174 const char *val;
8175 int id;
8176
8177 id = add_cred(ifname);
8178 if (id < 0)
8179 return -2;
8180 sigma_dut_print(dut, DUT_MSG_DEBUG, "Adding credential %d", id);
8181
8182 val = get_param(cmd, "prefer");
8183 if (val && atoi(val) > 0)
8184 set_cred(ifname, id, "priority", "1");
8185
8186 val = get_param(cmd, "REALM");
8187 if (val && set_cred_quoted(ifname, id, "realm", val) < 0) {
8188 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not set "
8189 "realm");
8190 return 0;
8191 }
8192
8193 val = get_param(cmd, "HOME_FQDN");
8194 if (val && set_cred_quoted(ifname, id, "domain", val) < 0) {
8195 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not set "
8196 "home_fqdn");
8197 return 0;
8198 }
8199
8200 val = get_param(cmd, "Username");
8201 if (val && set_cred_quoted(ifname, id, "username", val) < 0) {
8202 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not set "
8203 "username");
8204 return 0;
8205 }
8206
8207 val = get_param(cmd, "Password");
8208 if (val && set_cred_quoted(ifname, id, "password", val) < 0) {
8209 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not set "
8210 "password");
8211 return 0;
8212 }
8213
8214 val = get_param(cmd, "ROOT_CA");
8215 if (val) {
8216 char fname[200];
8217 snprintf(fname, sizeof(fname), "%s/%s", sigma_cert_path, val);
8218#ifdef __linux__
8219 if (!file_exists(fname)) {
8220 char msg[300];
8221 snprintf(msg, sizeof(msg), "ErrorCode,ROOT_CA "
8222 "file (%s) not found", fname);
8223 send_resp(dut, conn, SIGMA_ERROR, msg);
8224 return 0;
8225 }
8226#endif /* __linux__ */
8227 if (set_cred_quoted(ifname, id, "ca_cert", fname) < 0) {
8228 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could "
8229 "not set root CA");
8230 return 0;
8231 }
8232 }
8233
8234 return 1;
8235}
8236
8237
8238static int update_devdetail_imsi(struct sigma_dut *dut, const char *imsi)
8239{
8240 FILE *in, *out;
8241 char buf[500];
8242 int found = 0;
8243
8244 in = fopen("devdetail.xml", "r");
8245 if (in == NULL)
8246 return -1;
8247 out = fopen("devdetail.xml.tmp", "w");
8248 if (out == NULL) {
8249 fclose(in);
8250 return -1;
8251 }
8252
8253 while (fgets(buf, sizeof(buf), in)) {
8254 char *pos = strstr(buf, "<IMSI>");
8255 if (pos) {
8256 sigma_dut_print(dut, DUT_MSG_INFO, "Updated DevDetail IMSI to %s",
8257 imsi);
8258 pos += 6;
8259 *pos = '\0';
8260 fprintf(out, "%s%s</IMSI>\n", buf, imsi);
8261 found++;
8262 } else {
8263 fprintf(out, "%s", buf);
8264 }
8265 }
8266
8267 fclose(out);
8268 fclose(in);
8269 if (found)
8270 rename("devdetail.xml.tmp", "devdetail.xml");
8271 else
8272 unlink("devdetail.xml.tmp");
8273
8274 return 0;
8275}
8276
8277
8278static int sta_add_credential_sim(struct sigma_dut *dut,
8279 struct sigma_conn *conn,
8280 const char *ifname, struct sigma_cmd *cmd)
8281{
8282 const char *val, *imsi = NULL;
8283 int id;
8284 char buf[200];
8285 int res;
8286 const char *pos;
8287 size_t mnc_len;
8288 char plmn_mcc[4];
8289 char plmn_mnc[4];
8290
8291 id = add_cred(ifname);
8292 if (id < 0)
8293 return -2;
8294 sigma_dut_print(dut, DUT_MSG_DEBUG, "Adding credential %d", id);
8295
8296 val = get_param(cmd, "prefer");
8297 if (val && atoi(val) > 0)
8298 set_cred(ifname, id, "priority", "1");
8299
8300 val = get_param(cmd, "PLMN_MCC");
8301 if (val == NULL) {
8302 send_resp(dut, conn, SIGMA_ERROR,
8303 "errorCode,Missing PLMN_MCC");
8304 return 0;
8305 }
8306 if (strlen(val) != 3) {
8307 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Invalid MCC");
8308 return 0;
8309 }
8310 snprintf(plmn_mcc, sizeof(plmn_mcc), "%s", val);
8311
8312 val = get_param(cmd, "PLMN_MNC");
8313 if (val == NULL) {
8314 send_resp(dut, conn, SIGMA_ERROR,
8315 "errorCode,Missing PLMN_MNC");
8316 return 0;
8317 }
8318 if (strlen(val) != 2 && strlen(val) != 3) {
8319 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Invalid MNC");
8320 return 0;
8321 }
8322 snprintf(plmn_mnc, sizeof(plmn_mnc), "%s", val);
8323
8324 val = get_param(cmd, "IMSI");
8325 if (val == NULL) {
8326 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Missing SIM "
8327 "IMSI");
8328 return 0;
8329 }
8330
8331 imsi = pos = val;
8332
8333 if (strncmp(plmn_mcc, pos, 3) != 0) {
8334 send_resp(dut, conn, SIGMA_ERROR, "errorCode,MCC mismatch");
8335 return 0;
8336 }
8337 pos += 3;
8338
8339 mnc_len = strlen(plmn_mnc);
8340 if (mnc_len < 2) {
8341 send_resp(dut, conn, SIGMA_ERROR, "errorCode,MNC not set");
8342 return 0;
8343 }
8344
8345 if (strncmp(plmn_mnc, pos, mnc_len) != 0) {
8346 send_resp(dut, conn, SIGMA_ERROR, "errorCode,MNC mismatch");
8347 return 0;
8348 }
8349 pos += mnc_len;
8350
8351 res = snprintf(buf, sizeof(buf), "%s%s-%s",plmn_mcc, plmn_mnc, pos);
8352 if (res < 0 || res >= (int) sizeof(buf))
8353 return -1;
8354 if (set_cred_quoted(ifname, id, "imsi", buf) < 0) {
8355 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could "
8356 "not set IMSI");
8357 return 0;
8358 }
8359
8360 val = get_param(cmd, "Password");
8361 if (val && set_cred_quoted(ifname, id, "milenage", val) < 0) {
8362 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could "
8363 "not set password");
8364 return 0;
8365 }
8366
8367 if (dut->program == PROGRAM_HS2_R2) {
8368 /*
8369 * Set provisioning_sp for the test cases where SIM/USIM
8370 * provisioning is used.
8371 */
8372 if (val && set_cred_quoted(ifname, id, "provisioning_sp",
8373 "wi-fi.org") < 0) {
8374 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could "
8375 "not set provisioning_sp");
8376 return 0;
8377 }
8378
8379 update_devdetail_imsi(dut, imsi);
8380 }
8381
8382 return 1;
8383}
8384
8385
8386static int sta_add_credential_cert(struct sigma_dut *dut,
8387 struct sigma_conn *conn,
8388 const char *ifname,
8389 struct sigma_cmd *cmd)
8390{
8391 const char *val;
8392 int id;
8393
8394 id = add_cred(ifname);
8395 if (id < 0)
8396 return -2;
8397 sigma_dut_print(dut, DUT_MSG_DEBUG, "Adding credential %d", id);
8398
8399 val = get_param(cmd, "prefer");
8400 if (val && atoi(val) > 0)
8401 set_cred(ifname, id, "priority", "1");
8402
8403 val = get_param(cmd, "REALM");
8404 if (val && set_cred_quoted(ifname, id, "realm", val) < 0) {
8405 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not set "
8406 "realm");
8407 return 0;
8408 }
8409
8410 val = get_param(cmd, "HOME_FQDN");
8411 if (val && set_cred_quoted(ifname, id, "domain", val) < 0) {
8412 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not set "
8413 "home_fqdn");
8414 return 0;
8415 }
8416
8417 val = get_param(cmd, "Username");
8418 if (val && set_cred_quoted(ifname, id, "username", val) < 0) {
8419 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not set "
8420 "username");
8421 return 0;
8422 }
8423
8424 val = get_param(cmd, "clientCertificate");
8425 if (val) {
8426 char fname[200];
8427 snprintf(fname, sizeof(fname), "%s/%s", sigma_cert_path, val);
8428#ifdef __linux__
8429 if (!file_exists(fname)) {
8430 char msg[300];
8431 snprintf(msg, sizeof(msg),
8432 "ErrorCode,clientCertificate "
8433 "file (%s) not found", fname);
8434 send_resp(dut, conn, SIGMA_ERROR, msg);
8435 return 0;
8436 }
8437#endif /* __linux__ */
8438 if (set_cred_quoted(ifname, id, "client_cert", fname) < 0) {
8439 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could "
8440 "not set client_cert");
8441 return 0;
8442 }
8443 if (set_cred_quoted(ifname, id, "private_key", fname) < 0) {
8444 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could "
8445 "not set private_key");
8446 return 0;
8447 }
8448 }
8449
8450 val = get_param(cmd, "ROOT_CA");
8451 if (val) {
8452 char fname[200];
8453 snprintf(fname, sizeof(fname), "%s/%s", sigma_cert_path, val);
8454#ifdef __linux__
8455 if (!file_exists(fname)) {
8456 char msg[300];
8457 snprintf(msg, sizeof(msg), "ErrorCode,ROOT_CA "
8458 "file (%s) not found", fname);
8459 send_resp(dut, conn, SIGMA_ERROR, msg);
8460 return 0;
8461 }
8462#endif /* __linux__ */
8463 if (set_cred_quoted(ifname, id, "ca_cert", fname) < 0) {
8464 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could "
8465 "not set root CA");
8466 return 0;
8467 }
8468 }
8469
8470 return 1;
8471}
8472
8473
8474static int cmd_sta_add_credential(struct sigma_dut *dut,
8475 struct sigma_conn *conn,
8476 struct sigma_cmd *cmd)
8477{
8478 const char *intf = get_param(cmd, "Interface");
8479 const char *type;
8480
8481 start_sta_mode(dut);
8482
8483 type = get_param(cmd, "Type");
8484 if (!type)
8485 return -1;
8486
8487 if (strcasecmp(type, "uname_pwd") == 0)
8488 return sta_add_credential_uname_pwd(dut, conn, intf, cmd);
8489
8490 if (strcasecmp(type, "sim") == 0)
8491 return sta_add_credential_sim(dut, conn, intf, cmd);
8492
8493 if (strcasecmp(type, "cert") == 0)
8494 return sta_add_credential_cert(dut, conn, intf, cmd);
8495
8496 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Unsupported credential "
8497 "type");
8498 return 0;
8499}
8500
8501
8502static int cmd_sta_scan(struct sigma_dut *dut, struct sigma_conn *conn,
8503 struct sigma_cmd *cmd)
8504{
8505 const char *intf = get_param(cmd, "Interface");
vamsi krishna89ad8c62017-09-19 12:51:18 +05308506 const char *val, *bssid, *ssid;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02008507 char buf[100];
vamsi krishna89ad8c62017-09-19 12:51:18 +05308508 char ssid_hex[65];
Jouni Malinencd4e3c32015-10-29 12:39:56 +02008509 int res;
8510
8511 val = get_param(cmd, "HESSID");
8512 if (val) {
8513 res = snprintf(buf, sizeof(buf), "SET hessid %s", val);
8514 if (res < 0 || res >= (int) sizeof(buf))
8515 return -1;
8516 wpa_command(intf, buf);
8517 }
8518
8519 val = get_param(cmd, "ACCS_NET_TYPE");
8520 if (val) {
8521 res = snprintf(buf, sizeof(buf), "SET access_network_type %s",
8522 val);
8523 if (res < 0 || res >= (int) sizeof(buf))
8524 return -1;
8525 wpa_command(intf, buf);
8526 }
8527
vamsi krishna89ad8c62017-09-19 12:51:18 +05308528 bssid = get_param(cmd, "Bssid");
8529 ssid = get_param(cmd, "Ssid");
8530
8531 if (ssid) {
8532 if (2 * strlen(ssid) >= sizeof(ssid_hex)) {
8533 send_resp(dut, conn, SIGMA_ERROR,
8534 "ErrorCode,Too long SSID");
8535 return 0;
8536 }
8537 ascii2hexstr(ssid, ssid_hex);
8538 }
8539
8540 res = snprintf(buf, sizeof(buf), "SCAN%s%s%s%s",
8541 bssid ? " bssid=": "",
8542 bssid ? bssid : "",
8543 ssid ? " ssid " : "",
8544 ssid ? ssid_hex : "");
8545 if (res < 0 || res >= (int) sizeof(buf))
8546 return -1;
8547
8548 if (wpa_command(intf, buf)) {
Jouni Malinencd4e3c32015-10-29 12:39:56 +02008549 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not start "
8550 "scan");
8551 return 0;
8552 }
8553
8554 return 1;
8555}
8556
8557
8558static int cmd_sta_set_systime(struct sigma_dut *dut, struct sigma_conn *conn,
8559 struct sigma_cmd *cmd)
8560{
8561#ifdef __linux__
8562 struct timeval tv;
8563 struct tm tm;
8564 time_t t;
8565 const char *val;
Pradeep Reddy POTTETI429c69e2016-10-13 17:22:03 +05308566 int v;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02008567
8568 wpa_command(get_station_ifname(), "PMKSA_FLUSH");
8569
8570 memset(&tm, 0, sizeof(tm));
8571 val = get_param(cmd, "seconds");
8572 if (val)
8573 tm.tm_sec = atoi(val);
8574 val = get_param(cmd, "minutes");
8575 if (val)
8576 tm.tm_min = atoi(val);
8577 val = get_param(cmd, "hours");
8578 if (val)
8579 tm.tm_hour = atoi(val);
8580 val = get_param(cmd, "date");
8581 if (val)
8582 tm.tm_mday = atoi(val);
8583 val = get_param(cmd, "month");
Pradeep Reddy POTTETI429c69e2016-10-13 17:22:03 +05308584 if (val) {
8585 v = atoi(val);
8586 if (v < 1 || v > 12) {
8587 send_resp(dut, conn, SIGMA_INVALID,
8588 "errorCode,Invalid month");
8589 return 0;
8590 }
8591 tm.tm_mon = v - 1;
8592 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02008593 val = get_param(cmd, "year");
8594 if (val) {
8595 int year = atoi(val);
8596#ifdef ANDROID
8597 if (year > 2035)
8598 year = 2035; /* years beyond 2035 not supported */
8599#endif /* ANDROID */
8600 tm.tm_year = year - 1900;
8601 }
8602 t = mktime(&tm);
8603 if (t == (time_t) -1) {
8604 send_resp(dut, conn, SIGMA_ERROR,
8605 "errorCode,Invalid date or time");
8606 return 0;
8607 }
8608
8609 memset(&tv, 0, sizeof(tv));
8610 tv.tv_sec = t;
8611
8612 if (settimeofday(&tv, NULL) < 0) {
8613 sigma_dut_print(dut, DUT_MSG_INFO, "settimeofday failed: %s",
8614 strerror(errno));
8615 send_resp(dut, conn, SIGMA_ERROR,
8616 "errorCode,Failed to set time");
8617 return 0;
8618 }
8619
8620 return 1;
8621#endif /* __linux__ */
8622
8623 return -1;
8624}
8625
8626
8627static int cmd_sta_osu(struct sigma_dut *dut, struct sigma_conn *conn,
8628 struct sigma_cmd *cmd)
8629{
8630 const char *intf = get_param(cmd, "Interface");
8631 const char *name, *val;
8632 int prod_ess_assoc = 1;
8633 char buf[200], bssid[100], ssid[100];
8634 int res;
8635 struct wpa_ctrl *ctrl;
8636
8637 name = get_param(cmd, "osuFriendlyName");
8638
8639 val = get_param(cmd, "ProdESSAssoc");
8640 if (val)
8641 prod_ess_assoc = atoi(val);
8642
8643 kill_dhcp_client(dut, intf);
8644 if (start_dhcp_client(dut, intf) < 0)
8645 return -2;
8646
8647 sigma_dut_print(dut, DUT_MSG_DEBUG, "Trigger OSU");
8648 mkdir("Logs", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
8649 res = snprintf(buf, sizeof(buf),
8650 "%s %s%s%s signup osu-ca.pem",
8651 prod_ess_assoc ? "" : "-N",
8652 name ? "-O'" : "", name ? name : "",
8653 name ? "'" : "");
8654
Kanchanapally, Vidyullatha12b66762015-12-31 16:46:42 +05308655 hs2_set_policy(dut);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02008656 if (run_hs20_osu(dut, buf) < 0) {
8657 FILE *f;
8658
8659 sigma_dut_print(dut, DUT_MSG_INFO, "Failed to complete OSU");
8660
8661 f = fopen("hs20-osu-client.res", "r");
8662 if (f) {
8663 char resp[400], res[300], *pos;
8664 if (!fgets(res, sizeof(res), f))
8665 res[0] = '\0';
8666 pos = strchr(res, '\n');
8667 if (pos)
8668 *pos = '\0';
8669 fclose(f);
8670 sigma_dut_summary(dut, "hs20-osu-client provisioning failed: %s",
8671 res);
8672 snprintf(resp, sizeof(resp), "notify-send '%s'", res);
8673 if (system(resp) != 0) {
8674 }
8675 snprintf(resp, sizeof(resp),
8676 "SSID,,BSSID,,failureReason,%s", res);
8677 send_resp(dut, conn, SIGMA_COMPLETE, resp);
8678 return 0;
8679 }
8680
8681 send_resp(dut, conn, SIGMA_COMPLETE, "SSID,,BSSID,");
8682 return 0;
8683 }
8684
8685 if (!prod_ess_assoc)
8686 goto report;
8687
8688 ctrl = open_wpa_mon(intf);
8689 if (ctrl == NULL) {
8690 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to open "
8691 "wpa_supplicant monitor connection");
8692 return -1;
8693 }
8694
8695 res = get_wpa_cli_event(dut, ctrl, "CTRL-EVENT-CONNECTED",
8696 buf, sizeof(buf));
8697
8698 wpa_ctrl_detach(ctrl);
8699 wpa_ctrl_close(ctrl);
8700
8701 if (res < 0) {
8702 sigma_dut_print(dut, DUT_MSG_INFO, "Failed to connect to "
8703 "network after OSU");
8704 send_resp(dut, conn, SIGMA_COMPLETE, "SSID,,BSSID,");
8705 return 0;
8706 }
8707
8708report:
8709 if (get_wpa_status(intf, "bssid", bssid, sizeof(bssid)) < 0 ||
8710 get_wpa_status(intf, "ssid", ssid, sizeof(ssid)) < 0) {
8711 sigma_dut_print(dut, DUT_MSG_INFO, "Failed to get BSSID/SSID");
8712 send_resp(dut, conn, SIGMA_COMPLETE, "SSID,,BSSID,");
8713 return 0;
8714 }
8715
8716 snprintf(buf, sizeof(buf), "SSID,%s,BSSID,%s", ssid, bssid);
8717 send_resp(dut, conn, SIGMA_COMPLETE, buf);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02008718 return 0;
8719}
8720
8721
8722static int cmd_sta_policy_update(struct sigma_dut *dut, struct sigma_conn *conn,
8723 struct sigma_cmd *cmd)
8724{
8725 const char *val;
8726 int timeout = 120;
8727
8728 val = get_param(cmd, "PolicyUpdate");
8729 if (val == NULL || atoi(val) == 0)
8730 return 1; /* No operation requested */
8731
8732 val = get_param(cmd, "Timeout");
8733 if (val)
8734 timeout = atoi(val);
8735
8736 if (timeout) {
8737 /* TODO: time out the command and return
8738 * PolicyUpdateStatus,TIMEOUT if needed. */
8739 }
8740
8741 sigma_dut_print(dut, DUT_MSG_DEBUG, "Trigger policy update");
8742 mkdir("Logs", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
8743 if (run_hs20_osu(dut, "pol_upd fqdn=wi-fi.org") < 0) {
8744 send_resp(dut, conn, SIGMA_COMPLETE, "PolicyUpdateStatus,FAIL");
8745 return 0;
8746 }
8747
8748 send_resp(dut, conn, SIGMA_COMPLETE, "PolicyUpdateStatus,SUCCESS");
8749 return 0;
8750}
8751
8752
8753static int cmd_sta_er_config(struct sigma_dut *dut, struct sigma_conn *conn,
8754 struct sigma_cmd *cmd)
8755{
8756 struct wpa_ctrl *ctrl;
8757 const char *intf = get_param(cmd, "Interface");
8758 const char *bssid = get_param(cmd, "Bssid");
8759 const char *ssid = get_param(cmd, "SSID");
8760 const char *security = get_param(cmd, "Security");
8761 const char *passphrase = get_param(cmd, "Passphrase");
8762 const char *pin = get_param(cmd, "PIN");
8763 char buf[1000];
8764 char ssid_hex[200], passphrase_hex[200];
8765 const char *keymgmt, *cipher;
8766
8767 if (intf == NULL)
8768 intf = get_main_ifname();
8769
8770 if (!bssid) {
8771 send_resp(dut, conn, SIGMA_ERROR,
8772 "ErrorCode,Missing Bssid argument");
8773 return 0;
8774 }
8775
8776 if (!ssid) {
8777 send_resp(dut, conn, SIGMA_ERROR,
8778 "ErrorCode,Missing SSID argument");
8779 return 0;
8780 }
8781
8782 if (!security) {
8783 send_resp(dut, conn, SIGMA_ERROR,
8784 "ErrorCode,Missing Security argument");
8785 return 0;
8786 }
8787
8788 if (!passphrase) {
8789 send_resp(dut, conn, SIGMA_ERROR,
8790 "ErrorCode,Missing Passphrase argument");
8791 return 0;
8792 }
8793
8794 if (!pin) {
8795 send_resp(dut, conn, SIGMA_ERROR,
8796 "ErrorCode,Missing PIN argument");
8797 return 0;
8798 }
8799
vamsi krishna8c9c1562017-05-12 15:51:46 +05308800 if (2 * strlen(ssid) >= sizeof(ssid_hex) ||
8801 2 * strlen(passphrase) >= sizeof(passphrase_hex)) {
Jouni Malinencd4e3c32015-10-29 12:39:56 +02008802 send_resp(dut, conn, SIGMA_ERROR,
8803 "ErrorCode,Too long SSID/passphrase");
8804 return 0;
8805 }
8806
8807 ctrl = open_wpa_mon(intf);
8808 if (ctrl == NULL) {
8809 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to open "
8810 "wpa_supplicant monitor connection");
8811 return -2;
8812 }
8813
8814 if (strcasecmp(security, "wpa2-psk") == 0) {
8815 keymgmt = "WPA2PSK";
8816 cipher = "CCMP";
8817 } else {
8818 wpa_ctrl_detach(ctrl);
8819 wpa_ctrl_close(ctrl);
8820 send_resp(dut, conn, SIGMA_ERROR,
8821 "ErrorCode,Unsupported Security value");
8822 return 0;
8823 }
8824
8825 ascii2hexstr(ssid, ssid_hex);
8826 ascii2hexstr(passphrase, passphrase_hex);
8827 snprintf(buf, sizeof(buf), "WPS_REG %s %s %s %s %s %s",
8828 bssid, pin, ssid_hex, keymgmt, cipher, passphrase_hex);
8829
8830 if (wpa_command(intf, buf) < 0) {
8831 wpa_ctrl_detach(ctrl);
8832 wpa_ctrl_close(ctrl);
8833 send_resp(dut, conn, SIGMA_ERROR,
8834 "ErrorCode,Failed to start registrar");
8835 return 0;
8836 }
8837
8838 snprintf(dut->er_oper_bssid, sizeof(dut->er_oper_bssid), "%s", bssid);
8839 dut->er_oper_performed = 1;
8840
8841 return wps_connection_event(dut, conn, ctrl, intf, 0);
8842}
8843
8844
8845static int cmd_sta_wps_connect_pw_token(struct sigma_dut *dut,
8846 struct sigma_conn *conn,
8847 struct sigma_cmd *cmd)
8848{
8849 struct wpa_ctrl *ctrl;
8850 const char *intf = get_param(cmd, "Interface");
8851 const char *bssid = get_param(cmd, "Bssid");
8852 char buf[100];
8853
8854 if (!bssid) {
8855 send_resp(dut, conn, SIGMA_ERROR,
8856 "ErrorCode,Missing Bssid argument");
8857 return 0;
8858 }
8859
8860 ctrl = open_wpa_mon(intf);
8861 if (ctrl == NULL) {
8862 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to open "
8863 "wpa_supplicant monitor connection");
8864 return -2;
8865 }
8866
8867 snprintf(buf, sizeof(buf), "WPS_NFC %s", bssid);
8868
8869 if (wpa_command(intf, buf) < 0) {
8870 wpa_ctrl_detach(ctrl);
8871 wpa_ctrl_close(ctrl);
8872 send_resp(dut, conn, SIGMA_ERROR,
8873 "ErrorCode,Failed to start registrar");
8874 return 0;
8875 }
8876
8877 return wps_connection_event(dut, conn, ctrl, intf, 0);
8878}
8879
8880
8881static int req_intf(struct sigma_cmd *cmd)
8882{
8883 return get_param(cmd, "interface") == NULL ? -1 : 0;
8884}
8885
8886
8887void sta_register_cmds(void)
8888{
8889 sigma_dut_reg_cmd("sta_get_ip_config", req_intf,
8890 cmd_sta_get_ip_config);
8891 sigma_dut_reg_cmd("sta_set_ip_config", req_intf,
8892 cmd_sta_set_ip_config);
8893 sigma_dut_reg_cmd("sta_get_info", req_intf, cmd_sta_get_info);
8894 sigma_dut_reg_cmd("sta_get_mac_address", req_intf,
8895 cmd_sta_get_mac_address);
8896 sigma_dut_reg_cmd("sta_is_connected", req_intf, cmd_sta_is_connected);
8897 sigma_dut_reg_cmd("sta_verify_ip_connection", req_intf,
8898 cmd_sta_verify_ip_connection);
8899 sigma_dut_reg_cmd("sta_get_bssid", req_intf, cmd_sta_get_bssid);
8900 sigma_dut_reg_cmd("sta_set_encryption", req_intf,
8901 cmd_sta_set_encryption);
8902 sigma_dut_reg_cmd("sta_set_psk", req_intf, cmd_sta_set_psk);
8903 sigma_dut_reg_cmd("sta_set_eaptls", req_intf, cmd_sta_set_eaptls);
8904 sigma_dut_reg_cmd("sta_set_eapttls", req_intf, cmd_sta_set_eapttls);
8905 sigma_dut_reg_cmd("sta_set_eapsim", req_intf, cmd_sta_set_eapsim);
8906 sigma_dut_reg_cmd("sta_set_peap", req_intf, cmd_sta_set_peap);
8907 sigma_dut_reg_cmd("sta_set_eapfast", req_intf, cmd_sta_set_eapfast);
8908 sigma_dut_reg_cmd("sta_set_eapaka", req_intf, cmd_sta_set_eapaka);
8909 sigma_dut_reg_cmd("sta_set_eapakaprime", req_intf,
8910 cmd_sta_set_eapakaprime);
8911 sigma_dut_reg_cmd("sta_set_security", req_intf, cmd_sta_set_security);
8912 sigma_dut_reg_cmd("sta_set_uapsd", req_intf, cmd_sta_set_uapsd);
8913 /* TODO: sta_set_ibss */
8914 /* TODO: sta_set_mode */
8915 sigma_dut_reg_cmd("sta_set_wmm", req_intf, cmd_sta_set_wmm);
8916 sigma_dut_reg_cmd("sta_associate", req_intf, cmd_sta_associate);
8917 /* TODO: sta_up_load */
8918 sigma_dut_reg_cmd("sta_preset_testparameters", req_intf,
8919 cmd_sta_preset_testparameters);
8920 /* TODO: sta_set_system */
8921 sigma_dut_reg_cmd("sta_set_11n", req_intf, cmd_sta_set_11n);
8922 /* TODO: sta_set_rifs_test */
8923 sigma_dut_reg_cmd("sta_set_wireless", req_intf, cmd_sta_set_wireless);
8924 sigma_dut_reg_cmd("sta_send_addba", req_intf, cmd_sta_send_addba);
8925 /* TODO: sta_send_coexist_mgmt */
8926 sigma_dut_reg_cmd("sta_disconnect", req_intf, cmd_sta_disconnect);
8927 sigma_dut_reg_cmd("sta_reassoc", req_intf, cmd_sta_reassoc);
8928 sigma_dut_reg_cmd("sta_reassociate", req_intf, cmd_sta_reassoc);
8929 sigma_dut_reg_cmd("sta_reset_default", req_intf,
8930 cmd_sta_reset_default);
8931 sigma_dut_reg_cmd("sta_send_frame", req_intf, cmd_sta_send_frame);
8932 sigma_dut_reg_cmd("sta_set_macaddr", req_intf, cmd_sta_set_macaddr);
8933 sigma_dut_reg_cmd("sta_set_rfeature", req_intf, cmd_sta_set_rfeature);
8934 sigma_dut_reg_cmd("sta_set_radio", req_intf, cmd_sta_set_radio);
8935 sigma_dut_reg_cmd("sta_set_pwrsave", req_intf, cmd_sta_set_pwrsave);
8936 sigma_dut_reg_cmd("sta_bssid_pool", req_intf, cmd_sta_bssid_pool);
8937 sigma_dut_reg_cmd("sta_reset_parm", req_intf, cmd_sta_reset_parm);
8938 sigma_dut_reg_cmd("sta_get_key", req_intf, cmd_sta_get_key);
8939 sigma_dut_reg_cmd("sta_hs2_associate", req_intf,
8940 cmd_sta_hs2_associate);
8941 sigma_dut_reg_cmd("sta_add_credential", req_intf,
8942 cmd_sta_add_credential);
8943 sigma_dut_reg_cmd("sta_scan", req_intf, cmd_sta_scan);
8944 sigma_dut_reg_cmd("sta_set_systime", NULL, cmd_sta_set_systime);
8945 sigma_dut_reg_cmd("sta_osu", req_intf, cmd_sta_osu);
8946 sigma_dut_reg_cmd("sta_policy_update", req_intf, cmd_sta_policy_update);
8947 sigma_dut_reg_cmd("sta_er_config", NULL, cmd_sta_er_config);
8948 sigma_dut_reg_cmd("sta_wps_connect_pw_token", req_intf,
8949 cmd_sta_wps_connect_pw_token);
8950 sigma_dut_reg_cmd("sta_exec_action", req_intf, cmd_sta_exec_action);
8951 sigma_dut_reg_cmd("sta_get_events", req_intf, cmd_sta_get_events);
8952 sigma_dut_reg_cmd("sta_get_parameter", req_intf, cmd_sta_get_parameter);
8953}