blob: ef616a5ca0bbf0403e2f5850b1e70660217c893e [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 Malinencd4e3c32015-10-29 12:39:56 +02001312
1313 id = add_network_common(dut, conn, ifname, cmd);
1314 if (id < 0)
1315 return id;
1316
1317 val = get_param(cmd, "keyMgmtType");
1318 if (val == NULL) {
1319 send_resp(dut, conn, SIGMA_INVALID, "errorCode,Missing keyMgmtType");
1320 return 0;
1321 }
1322 if (strcasecmp(val, "wpa") == 0 ||
1323 strcasecmp(val, "wpa-psk") == 0) {
1324 if (set_network(ifname, id, "proto", "WPA") < 0)
1325 return -2;
1326 } else if (strcasecmp(val, "wpa2") == 0 ||
1327 strcasecmp(val, "wpa2-psk") == 0 ||
1328 strcasecmp(val, "wpa2-ft") == 0 ||
1329 strcasecmp(val, "wpa2-sha256") == 0) {
1330 if (set_network(ifname, id, "proto", "WPA2") < 0)
1331 return -2;
Pradeep Reddy POTTETI6d04b3b2016-11-15 14:51:26 +05301332 } else if (strcasecmp(val, "wpa2-wpa-psk") == 0 ||
1333 strcasecmp(val, "wpa2-wpa-ent") == 0) {
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001334 if (set_network(ifname, id, "proto", "WPA WPA2") < 0)
1335 return -2;
Jouni Malinenad395a22017-09-01 21:13:46 +03001336 } else if (strcasecmp(val, "SuiteB") == 0) {
1337 if (set_network(ifname, id, "proto", "WPA2") < 0)
1338 return -2;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001339 } else {
1340 send_resp(dut, conn, SIGMA_INVALID, "errorCode,Unrecognized keyMgmtType value");
1341 return 0;
1342 }
1343
1344 val = get_param(cmd, "encpType");
Jouni Malinenad395a22017-09-01 21:13:46 +03001345 if (val) {
1346 cipher_set = 1;
1347 if (strcasecmp(val, "tkip") == 0) {
1348 if (set_network(ifname, id, "pairwise", "TKIP") < 0)
1349 return -2;
1350 } else if (strcasecmp(val, "aes-ccmp") == 0) {
1351 if (set_network(ifname, id, "pairwise", "CCMP") < 0)
1352 return -2;
1353 } else if (strcasecmp(val, "aes-ccmp-tkip") == 0) {
1354 if (set_network(ifname, id, "pairwise",
1355 "CCMP TKIP") < 0)
1356 return -2;
1357 } else if (strcasecmp(val, "aes-gcmp") == 0) {
1358 if (set_network(ifname, id, "pairwise", "GCMP") < 0)
1359 return -2;
1360 if (set_network(ifname, id, "group", "GCMP") < 0)
1361 return -2;
1362 } else {
1363 send_resp(dut, conn, SIGMA_ERROR,
1364 "errorCode,Unrecognized encpType value");
1365 return 0;
1366 }
1367 }
1368
1369 val = get_param(cmd, "PairwiseCipher");
1370 if (val) {
1371 cipher_set = 1;
1372 /* TODO: Support space separated list */
1373 if (strcasecmp(val, "AES-GCMP-256") == 0) {
1374 if (set_network(ifname, id, "pairwise", "GCMP-256") < 0)
1375 return -2;
1376 } else if (strcasecmp(val, "AES-CCMP-256") == 0) {
1377 if (set_network(ifname, id, "pairwise",
1378 "CCMP-256") < 0)
1379 return -2;
1380 } else if (strcasecmp(val, "AES-GCMP-128") == 0) {
1381 if (set_network(ifname, id, "pairwise", "GCMP") < 0)
1382 return -2;
1383 } else if (strcasecmp(val, "AES-CCMP-128") == 0) {
1384 if (set_network(ifname, id, "pairwise", "CCMP") < 0)
1385 return -2;
1386 } else {
1387 send_resp(dut, conn, SIGMA_ERROR,
1388 "errorCode,Unrecognized PairwiseCipher value");
1389 return 0;
1390 }
1391 }
1392
1393 if (!cipher_set) {
1394 send_resp(dut, conn, SIGMA_ERROR,
1395 "errorCode,Missing encpType and PairwiseCipher");
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001396 return 0;
1397 }
Jouni Malinenad395a22017-09-01 21:13:46 +03001398
1399 val = get_param(cmd, "GroupCipher");
1400 if (val) {
1401 if (strcasecmp(val, "AES-GCMP-256") == 0) {
1402 if (set_network(ifname, id, "group", "GCMP-256") < 0)
1403 return -2;
1404 } else if (strcasecmp(val, "AES-CCMP-256") == 0) {
1405 if (set_network(ifname, id, "group", "CCMP-256") < 0)
1406 return -2;
1407 } else if (strcasecmp(val, "AES-GCMP-128") == 0) {
1408 if (set_network(ifname, id, "group", "GCMP") < 0)
1409 return -2;
1410 } else if (strcasecmp(val, "AES-CCMP-128") == 0) {
1411 if (set_network(ifname, id, "group", "CCMP") < 0)
1412 return -2;
1413 } else {
1414 send_resp(dut, conn, SIGMA_ERROR,
1415 "errorCode,Unrecognized GroupCipher value");
1416 return 0;
1417 }
1418 }
1419
Jouni Malinen7b239522017-09-14 21:37:18 +03001420 val = get_param(cmd, "GroupMgntCipher");
Jouni Malinenad395a22017-09-01 21:13:46 +03001421 if (val) {
Jouni Malinene8898cb2017-09-26 17:55:26 +03001422 const char *cipher;
1423
1424 if (strcasecmp(val, "BIP-GMAC-256") == 0) {
1425 cipher = "BIP-GMAC-256";
1426 } else if (strcasecmp(val, "BIP-CMAC-256") == 0) {
1427 cipher = "BIP-CMAC-256";
1428 } else if (strcasecmp(val, "BIP-GMAC-128") == 0) {
1429 cipher = "BIP-GMAC-128";
1430 } else if (strcasecmp(val, "BIP-CMAC-128") == 0) {
1431 cipher = "AES-128-CMAC";
1432 } else {
1433 send_resp(dut, conn, SIGMA_INVALID,
1434 "errorCode,Unsupported GroupMgntCipher");
1435 return 0;
1436 }
1437 if (set_network(ifname, id, "group_mgmt", cipher) < 0) {
1438 send_resp(dut, conn, SIGMA_INVALID,
1439 "errorCode,Failed to set GroupMgntCipher");
1440 return 0;
1441 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001442 }
1443
1444 dut->sta_pmf = STA_PMF_DISABLED;
1445 val = get_param(cmd, "PMF");
1446 if (val) {
1447 if (strcasecmp(val, "Required") == 0 ||
1448 strcasecmp(val, "Forced_Required") == 0) {
1449 dut->sta_pmf = STA_PMF_REQUIRED;
1450 if (set_network(ifname, id, "ieee80211w", "2") < 0)
1451 return -2;
1452 } else if (strcasecmp(val, "Optional") == 0) {
1453 dut->sta_pmf = STA_PMF_OPTIONAL;
1454 if (set_network(ifname, id, "ieee80211w", "1") < 0)
1455 return -2;
1456 } else if (strcasecmp(val, "Disabled") == 0 ||
1457 strcasecmp(val, "Forced_Disabled") == 0) {
1458 dut->sta_pmf = STA_PMF_DISABLED;
1459 } else {
1460 send_resp(dut, conn, SIGMA_INVALID, "errorCode,Unrecognized PMF value");
1461 return 0;
1462 }
1463 }
1464
1465 return id;
1466}
1467
1468
1469static int cmd_sta_set_psk(struct sigma_dut *dut, struct sigma_conn *conn,
1470 struct sigma_cmd *cmd)
1471{
1472 const char *intf = get_param(cmd, "Interface");
Jouni Malinen992a81e2017-08-22 13:57:47 +03001473 const char *type = get_param(cmd, "Type");
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001474 const char *ifname, *val, *alg;
1475 int id;
1476
1477 if (intf == NULL)
1478 return -1;
1479
1480 if (strcmp(intf, get_main_ifname()) == 0)
1481 ifname = get_station_ifname();
1482 else
1483 ifname = intf;
1484
1485 id = set_wpa_common(dut, conn, ifname, cmd);
1486 if (id < 0)
1487 return id;
1488
1489 val = get_param(cmd, "keyMgmtType");
1490 alg = get_param(cmd, "micAlg");
1491
Jouni Malinen992a81e2017-08-22 13:57:47 +03001492 if (type && strcasecmp(type, "SAE") == 0) {
1493 if (val && strcasecmp(val, "wpa2-ft") == 0) {
1494 if (set_network(ifname, id, "key_mgmt", "FT-SAE") < 0)
1495 return -2;
1496 } else {
1497 if (set_network(ifname, id, "key_mgmt", "SAE") < 0)
1498 return -2;
1499 }
1500 if (wpa_command(ifname, "SET sae_groups ") != 0) {
1501 sigma_dut_print(dut, DUT_MSG_ERROR,
1502 "Failed to clear sae_groups to default");
1503 return -2;
1504 }
Jouni Malinen0ab50f42017-08-31 01:34:59 +03001505 } else if (type && strcasecmp(type, "PSK-SAE") == 0) {
1506 if (val && strcasecmp(val, "wpa2-ft") == 0) {
1507 if (set_network(ifname, id, "key_mgmt",
1508 "FT-SAE FT-PSK") < 0)
1509 return -2;
1510 } else {
1511 if (set_network(ifname, id, "key_mgmt",
1512 "SAE WPA-PSK") < 0)
1513 return -2;
1514 }
1515 if (wpa_command(ifname, "SET sae_groups ") != 0) {
1516 sigma_dut_print(dut, DUT_MSG_ERROR,
1517 "Failed to clear sae_groups to default");
1518 return -2;
1519 }
Jouni Malinen992a81e2017-08-22 13:57:47 +03001520 } else if (alg && strcasecmp(alg, "SHA-256") == 0) {
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001521 if (set_network(ifname, id, "key_mgmt", "WPA-PSK-SHA256") < 0)
1522 return -2;
1523 } else if (alg && strcasecmp(alg, "SHA-1") == 0) {
1524 if (set_network(ifname, id, "key_mgmt", "WPA-PSK") < 0)
1525 return -2;
Ashwini Patil6dbf7b02017-03-20 13:42:11 +05301526 } else if (val && strcasecmp(val, "wpa2-ft") == 0) {
1527 if (set_network(ifname, id, "key_mgmt", "FT-PSK") < 0)
1528 return -2;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001529 } else if ((val && strcasecmp(val, "wpa2-sha256") == 0) ||
1530 dut->sta_pmf == STA_PMF_REQUIRED) {
1531 if (set_network(ifname, id, "key_mgmt",
1532 "WPA-PSK WPA-PSK-SHA256") < 0)
1533 return -2;
1534 } else if (dut->sta_pmf == STA_PMF_OPTIONAL) {
1535 if (set_network(ifname, id, "key_mgmt",
1536 "WPA-PSK WPA-PSK-SHA256") < 0)
1537 return -2;
1538 } else {
1539 if (set_network(ifname, id, "key_mgmt", "WPA-PSK") < 0)
1540 return -2;
1541 }
1542
1543 val = get_param(cmd, "passPhrase");
1544 if (val == NULL)
1545 return -1;
1546 if (set_network_quoted(ifname, id, "psk", val) < 0)
1547 return -2;
1548
Jouni Malinen992a81e2017-08-22 13:57:47 +03001549 val = get_param(cmd, "ECGroupID");
1550 if (val) {
1551 char buf[50];
1552
1553 snprintf(buf, sizeof(buf), "SET sae_groups %u", atoi(val));
1554 if (wpa_command(ifname, buf) != 0) {
1555 sigma_dut_print(dut, DUT_MSG_ERROR,
1556 "Failed to clear sae_groups");
1557 return -2;
1558 }
1559 }
1560
Jouni Malinen68143132017-09-02 02:34:08 +03001561 val = get_param(cmd, "InvalidSAEElement");
1562 if (val) {
1563 free(dut->sae_commit_override);
1564 dut->sae_commit_override = strdup(val);
1565 }
1566
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001567 return 1;
1568}
1569
1570
1571static int set_eap_common(struct sigma_dut *dut, struct sigma_conn *conn,
Bala Krishna Bhamidipati73d7af02016-03-24 12:27:56 +05301572 const char *ifname, int username_identity,
1573 struct sigma_cmd *cmd)
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001574{
1575 const char *val, *alg;
1576 int id;
1577 char buf[200];
1578#ifdef ANDROID
1579 unsigned char kvalue[KEYSTORE_MESSAGE_SIZE];
1580 int length;
1581#endif /* ANDROID */
1582
1583 id = set_wpa_common(dut, conn, ifname, cmd);
1584 if (id < 0)
1585 return id;
1586
1587 val = get_param(cmd, "keyMgmtType");
1588 alg = get_param(cmd, "micAlg");
1589
Jouni Malinenad395a22017-09-01 21:13:46 +03001590 if (val && strcasecmp(val, "SuiteB") == 0) {
1591 if (set_network(ifname, id, "key_mgmt", "WPA-EAP-SUITE-B-192") <
1592 0)
1593 return -2;
1594 } else if (alg && strcasecmp(alg, "SHA-256") == 0) {
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001595 if (set_network(ifname, id, "key_mgmt", "WPA-EAP-SHA256") < 0)
1596 return -2;
1597 } else if (alg && strcasecmp(alg, "SHA-1") == 0) {
1598 if (set_network(ifname, id, "key_mgmt", "WPA-EAP") < 0)
1599 return -2;
1600 } else if (val && strcasecmp(val, "wpa2-ft") == 0) {
1601 if (set_network(ifname, id, "key_mgmt", "FT-EAP") < 0)
1602 return -2;
1603 } else if ((val && strcasecmp(val, "wpa2-sha256") == 0) ||
1604 dut->sta_pmf == STA_PMF_REQUIRED) {
1605 if (set_network(ifname, id, "key_mgmt",
1606 "WPA-EAP WPA-EAP-SHA256") < 0)
1607 return -2;
1608 } else if (dut->sta_pmf == STA_PMF_OPTIONAL) {
1609 if (set_network(ifname, id, "key_mgmt",
1610 "WPA-EAP WPA-EAP-SHA256") < 0)
1611 return -2;
1612 } else {
1613 if (set_network(ifname, id, "key_mgmt", "WPA-EAP") < 0)
1614 return -2;
1615 }
1616
1617 val = get_param(cmd, "trustedRootCA");
1618 if (val) {
1619#ifdef ANDROID
1620 snprintf(buf, sizeof(buf), "CACERT_%s", val);
1621 length = android_keystore_get(ANDROID_KEYSTORE_GET, buf,
1622 kvalue);
1623 if (length > 0) {
1624 sigma_dut_print(dut, DUT_MSG_INFO,
1625 "Use Android keystore [%s]", buf);
1626 snprintf(buf, sizeof(buf), "keystore://CACERT_%s",
1627 val);
1628 goto ca_cert_selected;
1629 }
1630#endif /* ANDROID */
1631
1632 snprintf(buf, sizeof(buf), "%s/%s", sigma_cert_path, val);
1633#ifdef __linux__
1634 if (!file_exists(buf)) {
1635 char msg[300];
1636 snprintf(msg, sizeof(msg), "ErrorCode,trustedRootCA "
1637 "file (%s) not found", buf);
1638 send_resp(dut, conn, SIGMA_ERROR, msg);
1639 return -3;
1640 }
1641#endif /* __linux__ */
1642#ifdef ANDROID
1643ca_cert_selected:
1644#endif /* ANDROID */
1645 if (set_network_quoted(ifname, id, "ca_cert", buf) < 0)
1646 return -2;
1647 }
1648
Bala Krishna Bhamidipati73d7af02016-03-24 12:27:56 +05301649 if (username_identity) {
1650 val = get_param(cmd, "username");
1651 if (val) {
1652 if (set_network_quoted(ifname, id, "identity", val) < 0)
1653 return -2;
1654 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001655
Bala Krishna Bhamidipati73d7af02016-03-24 12:27:56 +05301656 val = get_param(cmd, "password");
1657 if (val) {
1658 if (set_network_quoted(ifname, id, "password", val) < 0)
1659 return -2;
1660 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001661 }
1662
1663 return id;
1664}
1665
1666
Jouni Malinen5eabb2a2017-10-03 18:17:30 +03001667static int set_tls_cipher(const char *ifname, int id, const char *cipher)
1668{
1669 const char *val;
1670
1671 if (!cipher)
1672 return 0;
1673
1674 if (strcasecmp(cipher, "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384") == 0)
1675 val = "ECDHE-ECDSA-AES256-GCM-SHA384";
1676 else if (strcasecmp(cipher,
1677 "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384") == 0)
1678 val = "ECDHE-RSA-AES256-GCM-SHA384";
1679 else if (strcasecmp(cipher, "TLS_DHE_RSA_WITH_AES_256_GCM_SHA384") == 0)
1680 val = "DHE-RSA-AES256-GCM-SHA384";
1681 else if (strcasecmp(cipher,
1682 "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256") == 0)
1683 val = "ECDHE-ECDSA-AES128-GCM-SHA256";
1684 else
1685 return -1;
1686
1687 /* Need to clear phase1="tls_suiteb=1" to allow cipher enforcement */
1688 set_network_quoted(ifname, id, "phase1", "");
1689
1690 return set_network_quoted(ifname, id, "openssl_ciphers", val);
1691}
1692
1693
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001694static int cmd_sta_set_eaptls(struct sigma_dut *dut, struct sigma_conn *conn,
1695 struct sigma_cmd *cmd)
1696{
1697 const char *intf = get_param(cmd, "Interface");
1698 const char *ifname, *val;
1699 int id;
1700 char buf[200];
1701#ifdef ANDROID
1702 unsigned char kvalue[KEYSTORE_MESSAGE_SIZE];
1703 int length;
1704 int jb_or_newer = 0;
1705 char prop[PROPERTY_VALUE_MAX];
1706#endif /* ANDROID */
1707
1708 if (intf == NULL)
1709 return -1;
1710
1711 if (strcmp(intf, get_main_ifname()) == 0)
1712 ifname = get_station_ifname();
1713 else
1714 ifname = intf;
1715
Bala Krishna Bhamidipati73d7af02016-03-24 12:27:56 +05301716 id = set_eap_common(dut, conn, ifname, 1, cmd);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001717 if (id < 0)
1718 return id;
1719
1720 if (set_network(ifname, id, "eap", "TLS") < 0)
1721 return -2;
1722
Pradeep Reddy POTTETI9f6c2132016-05-05 16:28:19 +05301723 if (!get_param(cmd, "username") &&
1724 set_network_quoted(ifname, id, "identity",
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001725 "wifi-user@wifilabs.local") < 0)
1726 return -2;
1727
1728 val = get_param(cmd, "clientCertificate");
1729 if (val == NULL)
1730 return -1;
1731#ifdef ANDROID
1732 snprintf(buf, sizeof(buf), "USRPKEY_%s", val);
1733 length = android_keystore_get(ANDROID_KEYSTORE_GET, buf, kvalue);
1734 if (length < 0) {
1735 /*
1736 * JB started reporting keystore type mismatches, so retry with
1737 * the GET_PUBKEY command if the generic GET fails.
1738 */
1739 length = android_keystore_get(ANDROID_KEYSTORE_GET_PUBKEY,
1740 buf, kvalue);
1741 }
1742
1743 if (property_get("ro.build.version.release", prop, NULL) != 0) {
1744 sigma_dut_print(dut, DUT_MSG_DEBUG, "Android release %s", prop);
1745 if (strncmp(prop, "4.0", 3) != 0)
1746 jb_or_newer = 1;
1747 } else
1748 jb_or_newer = 1; /* assume newer */
1749
1750 if (jb_or_newer && length > 0) {
1751 sigma_dut_print(dut, DUT_MSG_INFO,
1752 "Use Android keystore [%s]", buf);
1753 if (set_network(ifname, id, "engine", "1") < 0)
1754 return -2;
1755 if (set_network_quoted(ifname, id, "engine_id", "keystore") < 0)
1756 return -2;
1757 snprintf(buf, sizeof(buf), "USRPKEY_%s", val);
1758 if (set_network_quoted(ifname, id, "key_id", buf) < 0)
1759 return -2;
1760 snprintf(buf, sizeof(buf), "keystore://USRCERT_%s", val);
1761 if (set_network_quoted(ifname, id, "client_cert", buf) < 0)
1762 return -2;
1763 return 1;
1764 } else if (length > 0) {
1765 sigma_dut_print(dut, DUT_MSG_INFO,
1766 "Use Android keystore [%s]", buf);
1767 snprintf(buf, sizeof(buf), "keystore://USRPKEY_%s", val);
1768 if (set_network_quoted(ifname, id, "private_key", buf) < 0)
1769 return -2;
1770 snprintf(buf, sizeof(buf), "keystore://USRCERT_%s", val);
1771 if (set_network_quoted(ifname, id, "client_cert", buf) < 0)
1772 return -2;
1773 return 1;
1774 }
1775#endif /* ANDROID */
1776
1777 snprintf(buf, sizeof(buf), "%s/%s", sigma_cert_path, val);
1778#ifdef __linux__
1779 if (!file_exists(buf)) {
1780 char msg[300];
1781 snprintf(msg, sizeof(msg), "ErrorCode,clientCertificate file "
1782 "(%s) not found", buf);
1783 send_resp(dut, conn, SIGMA_ERROR, msg);
1784 return -3;
1785 }
1786#endif /* __linux__ */
1787 if (set_network_quoted(ifname, id, "private_key", buf) < 0)
1788 return -2;
1789 if (set_network_quoted(ifname, id, "client_cert", buf) < 0)
1790 return -2;
1791
1792 if (set_network_quoted(ifname, id, "private_key_passwd", "wifi") < 0)
1793 return -2;
1794
Jouni Malinen5eabb2a2017-10-03 18:17:30 +03001795 val = get_param(cmd, "keyMgmtType");
1796 if (val && strcasecmp(val, "SuiteB") == 0) {
1797 val = get_param(cmd, "CertType");
1798 if (val && strcasecmp(val, "RSA") == 0) {
1799 if (set_network_quoted(ifname, id, "phase1",
1800 "tls_suiteb=1") < 0)
1801 return -2;
1802 } else {
1803 if (set_network_quoted(ifname, id, "openssl_ciphers",
1804 "SUITEB192") < 0)
1805 return -2;
1806 }
1807
1808 val = get_param(cmd, "TLSCipher");
1809 if (set_tls_cipher(ifname, id, val) < 0) {
1810 send_resp(dut, conn, SIGMA_ERROR,
1811 "ErrorCode,Unsupported TLSCipher value");
1812 return -3;
1813 }
1814 }
1815
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001816 return 1;
1817}
1818
1819
1820static int cmd_sta_set_eapttls(struct sigma_dut *dut, struct sigma_conn *conn,
1821 struct sigma_cmd *cmd)
1822{
1823 const char *intf = get_param(cmd, "Interface");
1824 const char *ifname;
1825 int id;
1826
1827 if (intf == NULL)
1828 return -1;
1829
1830 if (strcmp(intf, get_main_ifname()) == 0)
1831 ifname = get_station_ifname();
1832 else
1833 ifname = intf;
1834
Bala Krishna Bhamidipati73d7af02016-03-24 12:27:56 +05301835 id = set_eap_common(dut, conn, ifname, 1, cmd);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001836 if (id < 0)
1837 return id;
1838
1839 if (set_network(ifname, id, "eap", "TTLS") < 0) {
1840 send_resp(dut, conn, SIGMA_ERROR,
1841 "errorCode,Failed to set TTLS method");
1842 return 0;
1843 }
1844
1845 if (set_network_quoted(ifname, id, "phase2", "auth=MSCHAPV2") < 0) {
1846 send_resp(dut, conn, SIGMA_ERROR,
1847 "errorCode,Failed to set MSCHAPv2 for TTLS Phase 2");
1848 return 0;
1849 }
1850
1851 return 1;
1852}
1853
1854
1855static int cmd_sta_set_eapsim(struct sigma_dut *dut, struct sigma_conn *conn,
1856 struct sigma_cmd *cmd)
1857{
1858 const char *intf = get_param(cmd, "Interface");
1859 const char *ifname;
1860 int id;
1861
1862 if (intf == NULL)
1863 return -1;
1864
1865 if (strcmp(intf, get_main_ifname()) == 0)
1866 ifname = get_station_ifname();
1867 else
1868 ifname = intf;
1869
Bala Krishna Bhamidipati73d7af02016-03-24 12:27:56 +05301870 id = set_eap_common(dut, conn, ifname, !dut->sim_no_username, cmd);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001871 if (id < 0)
1872 return id;
1873
1874 if (set_network(ifname, id, "eap", "SIM") < 0)
1875 return -2;
1876
1877 return 1;
1878}
1879
1880
1881static int cmd_sta_set_peap(struct sigma_dut *dut, struct sigma_conn *conn,
1882 struct sigma_cmd *cmd)
1883{
1884 const char *intf = get_param(cmd, "Interface");
1885 const char *ifname, *val;
1886 int id;
1887 char buf[100];
1888
1889 if (intf == NULL)
1890 return -1;
1891
1892 if (strcmp(intf, get_main_ifname()) == 0)
1893 ifname = get_station_ifname();
1894 else
1895 ifname = intf;
1896
Bala Krishna Bhamidipati73d7af02016-03-24 12:27:56 +05301897 id = set_eap_common(dut, conn, ifname, 1, cmd);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001898 if (id < 0)
1899 return id;
1900
1901 if (set_network(ifname, id, "eap", "PEAP") < 0)
1902 return -2;
1903
1904 val = get_param(cmd, "innerEAP");
1905 if (val) {
1906 if (strcasecmp(val, "MSCHAPv2") == 0) {
1907 if (set_network_quoted(ifname, id, "phase2",
1908 "auth=MSCHAPV2") < 0)
1909 return -2;
1910 } else if (strcasecmp(val, "GTC") == 0) {
1911 if (set_network_quoted(ifname, id, "phase2",
1912 "auth=GTC") < 0)
1913 return -2;
1914 } else
1915 return -1;
1916 }
1917
1918 val = get_param(cmd, "peapVersion");
1919 if (val) {
1920 int ver = atoi(val);
1921 if (ver < 0 || ver > 1)
1922 return -1;
1923 snprintf(buf, sizeof(buf), "peapver=%d", ver);
1924 if (set_network_quoted(ifname, id, "phase1", buf) < 0)
1925 return -2;
1926 }
1927
1928 return 1;
1929}
1930
1931
1932static int cmd_sta_set_eapfast(struct sigma_dut *dut, struct sigma_conn *conn,
1933 struct sigma_cmd *cmd)
1934{
1935 const char *intf = get_param(cmd, "Interface");
1936 const char *ifname, *val;
1937 int id;
1938 char buf[100];
1939
1940 if (intf == NULL)
1941 return -1;
1942
1943 if (strcmp(intf, get_main_ifname()) == 0)
1944 ifname = get_station_ifname();
1945 else
1946 ifname = intf;
1947
Bala Krishna Bhamidipati73d7af02016-03-24 12:27:56 +05301948 id = set_eap_common(dut, conn, ifname, 1, cmd);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001949 if (id < 0)
1950 return id;
1951
1952 if (set_network(ifname, id, "eap", "FAST") < 0)
1953 return -2;
1954
1955 val = get_param(cmd, "innerEAP");
1956 if (val) {
1957 if (strcasecmp(val, "MSCHAPV2") == 0) {
1958 if (set_network_quoted(ifname, id, "phase2",
1959 "auth=MSCHAPV2") < 0)
1960 return -2;
1961 } else if (strcasecmp(val, "GTC") == 0) {
1962 if (set_network_quoted(ifname, id, "phase2",
1963 "auth=GTC") < 0)
1964 return -2;
1965 } else
1966 return -1;
1967 }
1968
1969 val = get_param(cmd, "validateServer");
1970 if (val) {
1971 /* TODO */
1972 sigma_dut_print(dut, DUT_MSG_INFO, "Ignored EAP-FAST "
1973 "validateServer=%s", val);
1974 }
1975
1976 val = get_param(cmd, "pacFile");
1977 if (val) {
1978 snprintf(buf, sizeof(buf), "blob://%s", val);
1979 if (set_network_quoted(ifname, id, "pac_file", buf) < 0)
1980 return -2;
1981 }
1982
1983 if (set_network_quoted(ifname, id, "phase1", "fast_provisioning=2") <
1984 0)
1985 return -2;
1986
1987 return 1;
1988}
1989
1990
1991static int cmd_sta_set_eapaka(struct sigma_dut *dut, struct sigma_conn *conn,
1992 struct sigma_cmd *cmd)
1993{
1994 const char *intf = get_param(cmd, "Interface");
1995 const char *ifname;
1996 int id;
1997
1998 if (intf == NULL)
1999 return -1;
2000
2001 if (strcmp(intf, get_main_ifname()) == 0)
2002 ifname = get_station_ifname();
2003 else
2004 ifname = intf;
2005
Bala Krishna Bhamidipati73d7af02016-03-24 12:27:56 +05302006 id = set_eap_common(dut, conn, ifname, !dut->sim_no_username, cmd);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02002007 if (id < 0)
2008 return id;
2009
2010 if (set_network(ifname, id, "eap", "AKA") < 0)
2011 return -2;
2012
2013 return 1;
2014}
2015
2016
2017static int cmd_sta_set_eapakaprime(struct sigma_dut *dut,
2018 struct sigma_conn *conn,
2019 struct sigma_cmd *cmd)
2020{
2021 const char *intf = get_param(cmd, "Interface");
2022 const char *ifname;
2023 int id;
2024
2025 if (intf == NULL)
2026 return -1;
2027
2028 if (strcmp(intf, get_main_ifname()) == 0)
2029 ifname = get_station_ifname();
2030 else
2031 ifname = intf;
2032
Bala Krishna Bhamidipati73d7af02016-03-24 12:27:56 +05302033 id = set_eap_common(dut, conn, ifname, !dut->sim_no_username, cmd);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02002034 if (id < 0)
2035 return id;
2036
2037 if (set_network(ifname, id, "eap", "AKA'") < 0)
2038 return -2;
2039
2040 return 1;
2041}
2042
2043
2044static int sta_set_open(struct sigma_dut *dut, struct sigma_conn *conn,
2045 struct sigma_cmd *cmd)
2046{
2047 const char *intf = get_param(cmd, "Interface");
2048 const char *ifname;
2049 int id;
2050
2051 if (strcmp(intf, get_main_ifname()) == 0)
2052 ifname = get_station_ifname();
2053 else
2054 ifname = intf;
2055
2056 id = add_network_common(dut, conn, ifname, cmd);
2057 if (id < 0)
2058 return id;
2059
2060 if (set_network(ifname, id, "key_mgmt", "NONE") < 0)
2061 return -2;
2062
2063 return 1;
2064}
2065
2066
2067static int cmd_sta_set_security(struct sigma_dut *dut, struct sigma_conn *conn,
2068 struct sigma_cmd *cmd)
2069{
2070 const char *type = get_param(cmd, "Type");
2071
2072 if (type == NULL) {
2073 send_resp(dut, conn, SIGMA_ERROR,
2074 "ErrorCode,Missing Type argument");
2075 return 0;
2076 }
2077
2078 if (strcasecmp(type, "OPEN") == 0)
2079 return sta_set_open(dut, conn, cmd);
Jouni Malinen992a81e2017-08-22 13:57:47 +03002080 if (strcasecmp(type, "PSK") == 0 ||
Jouni Malinen0ab50f42017-08-31 01:34:59 +03002081 strcasecmp(type, "PSK-SAE") == 0 ||
Jouni Malinen992a81e2017-08-22 13:57:47 +03002082 strcasecmp(type, "SAE") == 0)
Jouni Malinencd4e3c32015-10-29 12:39:56 +02002083 return cmd_sta_set_psk(dut, conn, cmd);
2084 if (strcasecmp(type, "EAPTLS") == 0)
2085 return cmd_sta_set_eaptls(dut, conn, cmd);
2086 if (strcasecmp(type, "EAPTTLS") == 0)
2087 return cmd_sta_set_eapttls(dut, conn, cmd);
2088 if (strcasecmp(type, "EAPPEAP") == 0)
2089 return cmd_sta_set_peap(dut, conn, cmd);
2090 if (strcasecmp(type, "EAPSIM") == 0)
2091 return cmd_sta_set_eapsim(dut, conn, cmd);
2092 if (strcasecmp(type, "EAPFAST") == 0)
2093 return cmd_sta_set_eapfast(dut, conn, cmd);
2094 if (strcasecmp(type, "EAPAKA") == 0)
2095 return cmd_sta_set_eapaka(dut, conn, cmd);
2096 if (strcasecmp(type, "EAPAKAPRIME") == 0)
2097 return cmd_sta_set_eapakaprime(dut, conn, cmd);
2098
2099 send_resp(dut, conn, SIGMA_ERROR,
2100 "ErrorCode,Unsupported Type value");
2101 return 0;
2102}
2103
2104
2105int ath6kl_client_uapsd(struct sigma_dut *dut, const char *intf, int uapsd)
2106{
2107#ifdef __linux__
2108 /* special handling for ath6kl */
2109 char path[128], fname[128], *pos;
2110 ssize_t res;
2111 FILE *f;
2112
2113 snprintf(path, sizeof(path), "/sys/class/net/%s/phy80211", intf);
2114 res = readlink(path, path, sizeof(path));
2115 if (res < 0)
2116 return 0; /* not ath6kl */
2117
2118 if (res >= (int) sizeof(path))
2119 res = sizeof(path) - 1;
2120 path[res] = '\0';
2121 pos = strrchr(path, '/');
2122 if (pos == NULL)
2123 pos = path;
2124 else
2125 pos++;
2126 snprintf(fname, sizeof(fname),
2127 "/sys/kernel/debug/ieee80211/%s/ath6kl/"
2128 "create_qos", pos);
2129 if (!file_exists(fname))
2130 return 0; /* not ath6kl */
2131
2132 if (uapsd) {
2133 f = fopen(fname, "w");
2134 if (f == NULL)
2135 return -1;
2136
2137 sigma_dut_print(dut, DUT_MSG_DEBUG, "Use ath6kl create_qos");
2138 fprintf(f, "4 2 2 1 2 9999999 9999999 9999999 7777777 0 4 "
2139 "45000 200 56789000 56789000 5678900 0 0 9999999 "
2140 "20000 0\n");
2141 fclose(f);
2142 } else {
2143 snprintf(fname, sizeof(fname),
2144 "/sys/kernel/debug/ieee80211/%s/ath6kl/"
2145 "delete_qos", pos);
2146
2147 f = fopen(fname, "w");
2148 if (f == NULL)
2149 return -1;
2150
2151 sigma_dut_print(dut, DUT_MSG_DEBUG, "Use ath6kl delete_qos");
2152 fprintf(f, "2 4\n");
2153 fclose(f);
2154 }
2155#endif /* __linux__ */
2156
2157 return 0;
2158}
2159
2160
2161static int cmd_sta_set_uapsd(struct sigma_dut *dut, struct sigma_conn *conn,
2162 struct sigma_cmd *cmd)
2163{
2164 const char *intf = get_param(cmd, "Interface");
2165 /* const char *ssid = get_param(cmd, "ssid"); */
2166 const char *val;
2167 int max_sp_len = 4;
2168 int ac_be = 1, ac_bk = 1, ac_vi = 1, ac_vo = 1;
2169 char buf[100];
2170 int ret1, ret2;
2171
2172 val = get_param(cmd, "maxSPLength");
2173 if (val) {
2174 max_sp_len = atoi(val);
2175 if (max_sp_len != 0 && max_sp_len != 1 && max_sp_len != 2 &&
2176 max_sp_len != 4)
2177 return -1;
2178 }
2179
2180 val = get_param(cmd, "acBE");
2181 if (val)
2182 ac_be = atoi(val);
2183
2184 val = get_param(cmd, "acBK");
2185 if (val)
2186 ac_bk = atoi(val);
2187
2188 val = get_param(cmd, "acVI");
2189 if (val)
2190 ac_vi = atoi(val);
2191
2192 val = get_param(cmd, "acVO");
2193 if (val)
2194 ac_vo = atoi(val);
2195
2196 dut->client_uapsd = ac_be || ac_bk || ac_vi || ac_vo;
2197
2198 snprintf(buf, sizeof(buf), "P2P_SET client_apsd %d,%d,%d,%d;%d",
2199 ac_be, ac_bk, ac_vi, ac_vo, max_sp_len);
2200 ret1 = wpa_command(intf, buf);
2201
2202 snprintf(buf, sizeof(buf), "SET uapsd %d,%d,%d,%d;%d",
2203 ac_be, ac_bk, ac_vi, ac_vo, max_sp_len);
2204 ret2 = wpa_command(intf, buf);
2205
2206 if (ret1 && ret2) {
2207 sigma_dut_print(dut, DUT_MSG_INFO, "Failed to set client mode "
2208 "UAPSD parameters.");
2209 return -2;
2210 }
2211
2212 if (ath6kl_client_uapsd(dut, intf, dut->client_uapsd) < 0) {
2213 send_resp(dut, conn, SIGMA_ERROR,
2214 "ErrorCode,Failed to set ath6kl QoS parameters");
2215 return 0;
2216 }
2217
2218 return 1;
2219}
2220
2221
2222static int cmd_sta_set_wmm(struct sigma_dut *dut, struct sigma_conn *conn,
2223 struct sigma_cmd *cmd)
2224{
2225 char buf[1000];
2226 const char *intf = get_param(cmd, "Interface");
2227 const char *grp = get_param(cmd, "Group");
2228 const char *act = get_param(cmd, "Action");
2229 const char *tid = get_param(cmd, "Tid");
2230 const char *dir = get_param(cmd, "Direction");
2231 const char *psb = get_param(cmd, "Psb");
2232 const char *up = get_param(cmd, "Up");
2233 const char *fixed = get_param(cmd, "Fixed");
2234 const char *size = get_param(cmd, "Size");
2235 const char *msize = get_param(cmd, "Maxsize");
2236 const char *minsi = get_param(cmd, "Min_srvc_intrvl");
2237 const char *maxsi = get_param(cmd, "Max_srvc_intrvl");
2238 const char *inact = get_param(cmd, "Inactivity");
2239 const char *sus = get_param(cmd, "Suspension");
2240 const char *mindr = get_param(cmd, "Mindatarate");
2241 const char *meandr = get_param(cmd, "Meandatarate");
2242 const char *peakdr = get_param(cmd, "Peakdatarate");
2243 const char *phyrate = get_param(cmd, "Phyrate");
2244 const char *burstsize = get_param(cmd, "Burstsize");
2245 const char *sba = get_param(cmd, "Sba");
2246 int direction;
2247 int handle;
2248 float sba_fv;
2249 int fixed_int;
2250 int psb_ts;
2251
2252 if (intf == NULL || grp == NULL || act == NULL )
2253 return -1;
2254
2255 if (strcasecmp(act, "addts") == 0) {
2256 if (tid == NULL || dir == NULL || psb == NULL ||
2257 up == NULL || fixed == NULL || size == NULL)
2258 return -1;
2259
2260 /*
2261 * Note: Sigma CAPI spec lists uplink, downlink, and bidi as the
2262 * possible values, but WMM-AC and V-E test scripts use "UP,
2263 * "DOWN", and "BIDI".
2264 */
2265 if (strcasecmp(dir, "uplink") == 0 ||
2266 strcasecmp(dir, "up") == 0) {
2267 direction = 0;
2268 } else if (strcasecmp(dir, "downlink") == 0 ||
2269 strcasecmp(dir, "down") == 0) {
2270 direction = 1;
2271 } else if (strcasecmp(dir, "bidi") == 0) {
2272 direction = 2;
2273 } else {
2274 sigma_dut_print(dut, DUT_MSG_ERROR,
2275 "Direction %s not supported", dir);
2276 return -1;
2277 }
2278
2279 if (strcasecmp(psb, "legacy") == 0) {
2280 psb_ts = 0;
2281 } else if (strcasecmp(psb, "uapsd") == 0) {
2282 psb_ts = 1;
2283 } else {
2284 sigma_dut_print(dut, DUT_MSG_ERROR,
2285 "PSB %s not supported", psb);
2286 return -1;
2287 }
2288
2289 if (atoi(tid) < 0 || atoi(tid) > 7) {
2290 sigma_dut_print(dut, DUT_MSG_ERROR,
2291 "TID %s not supported", tid);
2292 return -1;
2293 }
2294
2295 if (strcasecmp(fixed, "true") == 0) {
2296 fixed_int = 1;
2297 } else {
2298 fixed_int = 0;
2299 }
2300
2301 sba_fv = atof(sba);
2302
2303 dut->dialog_token++;
2304 handle = 7000 + dut->dialog_token;
2305
2306 /*
2307 * size: convert to hex
2308 * maxsi: convert to hex
2309 * mindr: convert to hex
2310 * meandr: convert to hex
2311 * peakdr: convert to hex
2312 * burstsize: convert to hex
2313 * phyrate: convert to hex
2314 * sba: convert to hex with modification
2315 * minsi: convert to integer
2316 * sus: convert to integer
2317 * inact: convert to integer
2318 * maxsi: convert to integer
2319 */
2320
2321 /*
2322 * The Nominal MSDU Size field is 2 octets long and contains an
2323 * unsigned integer that specifies the nominal size, in octets,
2324 * of MSDUs belonging to the traffic under this traffic
2325 * specification and is defined in Figure 16. If the Fixed
2326 * subfield is set to 1, then the size of the MSDU is fixed and
2327 * is indicated by the Size Subfield. If the Fixed subfield is
2328 * set to 0, then the size of the MSDU might not be fixed and
2329 * the Size indicates the nominal MSDU size.
2330 *
2331 * The Surplus Bandwidth Allowance Factor field is 2 octets long
2332 * and specifies the excess allocation of time (and bandwidth)
2333 * over and above the stated rates required to transport an MSDU
2334 * belonging to the traffic in this TSPEC. This field is
2335 * represented as an unsigned binary number with an implicit
2336 * binary point after the leftmost 3 bits. For example, an SBA
2337 * of 1.75 is represented as 0x3800. This field is included to
2338 * account for retransmissions. As such, the value of this field
2339 * must be greater than unity.
2340 */
2341
2342 snprintf(buf, sizeof(buf),
2343 "iwpriv %s addTspec %d %s %d %d %s 0x%X"
2344 " 0x%X 0x%X 0x%X"
2345 " 0x%X 0x%X 0x%X"
2346 " 0x%X %d %d %d %d"
2347 " %d %d",
2348 intf, handle, tid, direction, psb_ts, up,
2349 (unsigned int) ((fixed_int << 15) | atoi(size)),
2350 msize ? atoi(msize) : 0,
2351 mindr ? atoi(mindr) : 0,
2352 meandr ? atoi(meandr) : 0,
2353 peakdr ? atoi(peakdr) : 0,
2354 burstsize ? atoi(burstsize) : 0,
2355 phyrate ? atoi(phyrate) : 0,
2356 sba ? ((unsigned int) (((int) sba_fv << 13) |
2357 (int)((sba_fv - (int) sba_fv) *
2358 8192))) : 0,
2359 minsi ? atoi(minsi) : 0,
2360 sus ? atoi(sus) : 0,
2361 0, 0,
2362 inact ? atoi(inact) : 0,
2363 maxsi ? atoi(maxsi) : 0);
2364
2365 if (system(buf) != 0) {
2366 sigma_dut_print(dut, DUT_MSG_ERROR,
2367 "iwpriv addtspec request failed");
2368 send_resp(dut, conn, SIGMA_ERROR,
2369 "errorCode,Failed to execute addTspec command");
2370 return 0;
2371 }
2372
2373 sigma_dut_print(dut, DUT_MSG_INFO,
2374 "iwpriv addtspec request send");
2375
2376 /* Mapping handle to a TID */
2377 dut->tid_to_handle[atoi(tid)] = handle;
2378 } else if (strcasecmp(act, "delts") == 0) {
2379 if (tid == NULL)
2380 return -1;
2381
2382 if (atoi(tid) < 0 || atoi(tid) > 7) {
2383 sigma_dut_print(dut, DUT_MSG_ERROR,
2384 "TID %s not supported", tid);
2385 send_resp(dut, conn, SIGMA_ERROR,
2386 "errorCode,Unsupported TID");
2387 return 0;
2388 }
2389
2390 handle = dut->tid_to_handle[atoi(tid)];
2391
2392 if (handle < 7000 || handle > 7255) {
2393 /* Invalid handle ie no mapping for that TID */
2394 sigma_dut_print(dut, DUT_MSG_ERROR,
2395 "handle-> %d not found", handle);
2396 }
2397
2398 snprintf(buf, sizeof(buf), "iwpriv %s delTspec %d",
2399 intf, handle);
2400
2401 if (system(buf) != 0) {
2402 sigma_dut_print(dut, DUT_MSG_ERROR,
2403 "iwpriv deltspec request failed");
2404 send_resp(dut, conn, SIGMA_ERROR,
2405 "errorCode,Failed to execute delTspec command");
2406 return 0;
2407 }
2408
2409 sigma_dut_print(dut, DUT_MSG_INFO,
2410 "iwpriv deltspec request send");
2411
2412 dut->tid_to_handle[atoi(tid)] = 0;
2413 } else {
2414 sigma_dut_print(dut, DUT_MSG_ERROR,
2415 "Action type %s not supported", act);
2416 send_resp(dut, conn, SIGMA_ERROR,
2417 "errorCode,Unsupported Action");
2418 return 0;
2419 }
2420
2421 return 1;
2422}
2423
2424
2425static int cmd_sta_associate(struct sigma_dut *dut, struct sigma_conn *conn,
2426 struct sigma_cmd *cmd)
2427{
2428 /* const char *intf = get_param(cmd, "Interface"); */
2429 const char *ssid = get_param(cmd, "ssid");
2430 const char *wps_param = get_param(cmd, "WPS");
2431 const char *bssid = get_param(cmd, "bssid");
Jouni Malinen46a19b62017-06-23 14:31:27 +03002432 const char *chan = get_param(cmd, "channel");
Jouni Malinencd4e3c32015-10-29 12:39:56 +02002433 int wps = 0;
Jouni Malinen3c367e82017-06-23 17:01:47 +03002434 char buf[1000], extra[50];
Jouni Malinencd4e3c32015-10-29 12:39:56 +02002435
2436 if (ssid == NULL)
2437 return -1;
2438
Jouni Malinen3c367e82017-06-23 17:01:47 +03002439 if (dut->rsne_override) {
2440 snprintf(buf, sizeof(buf), "TEST_ASSOC_IE %s",
2441 dut->rsne_override);
2442 if (wpa_command(get_station_ifname(), buf) < 0) {
2443 send_resp(dut, conn, SIGMA_ERROR,
2444 "ErrorCode,Failed to set DEV_CONFIGURE_IE RSNE override");
2445 return 0;
2446 }
2447 }
2448
Jouni Malinen68143132017-09-02 02:34:08 +03002449 if (dut->sae_commit_override) {
2450 snprintf(buf, sizeof(buf), "SET sae_commit_override %s",
2451 dut->sae_commit_override);
2452 if (wpa_command(get_station_ifname(), buf) < 0) {
2453 send_resp(dut, conn, SIGMA_ERROR,
2454 "ErrorCode,Failed to set SAE commit override");
2455 return 0;
2456 }
2457 }
2458
Jouni Malinencd4e3c32015-10-29 12:39:56 +02002459 if (wps_param &&
2460 (strcmp(wps_param, "1") == 0 || strcasecmp(wps_param, "On") == 0))
2461 wps = 1;
2462
2463 if (wps) {
2464 if (dut->wps_method == WFA_CS_WPS_NOT_READY) {
2465 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,WPS "
2466 "parameters not yet set");
2467 return 0;
2468 }
2469 if (dut->wps_method == WFA_CS_WPS_PBC) {
2470 if (wpa_command(get_station_ifname(), "WPS_PBC") < 0)
2471 return -2;
2472 } else {
2473 snprintf(buf, sizeof(buf), "WPS_PIN any %s",
2474 dut->wps_pin);
2475 if (wpa_command(get_station_ifname(), buf) < 0)
2476 return -2;
2477 }
2478 } else {
2479 if (strcmp(ssid, dut->infra_ssid) != 0) {
2480 printf("No network parameters known for network "
2481 "(ssid='%s')", ssid);
2482 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,"
2483 "No network parameters known for network");
2484 return 0;
2485 }
2486
2487 if (bssid &&
2488 set_network(get_station_ifname(), dut->infra_network_id,
2489 "bssid", bssid) < 0) {
2490 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,"
2491 "Invalid bssid argument");
2492 return 0;
2493 }
2494
Jouni Malinen46a19b62017-06-23 14:31:27 +03002495 extra[0] = '\0';
2496 if (chan)
2497 snprintf(extra, sizeof(extra), " freq=%u",
2498 channel_to_freq(atoi(chan)));
2499 snprintf(buf, sizeof(buf), "SELECT_NETWORK %d%s",
2500 dut->infra_network_id, extra);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02002501 if (wpa_command(get_station_ifname(), buf) < 0) {
2502 sigma_dut_print(dut, DUT_MSG_INFO, "Failed to select "
2503 "network id %d on %s",
2504 dut->infra_network_id,
2505 get_station_ifname());
2506 return -2;
2507 }
2508 }
2509
2510 return 1;
2511}
2512
2513
2514static int run_hs20_osu(struct sigma_dut *dut, const char *params)
2515{
2516 char buf[500], cmd[200];
2517 int res;
2518
2519 /* Use hs20-osu-client file at the current dir, if found; otherwise use
2520 * default path */
2521 res = snprintf(cmd, sizeof(cmd),
2522 "%s -w \"%s\" -r hs20-osu-client.res %s%s -dddKt -f Logs/hs20-osu-client.txt",
2523 file_exists("./hs20-osu-client") ?
2524 "./hs20-osu-client" : "hs20-osu-client",
2525 sigma_wpas_ctrl,
2526 dut->summary_log ? "-s " : "",
2527 dut->summary_log ? dut->summary_log : "");
2528 if (res < 0 || res >= (int) sizeof(cmd))
2529 return -1;
2530
2531 res = snprintf(buf, sizeof(buf), "%s %s", cmd, params);
2532 if (res < 0 || res >= (int) sizeof(buf))
2533 return -1;
2534 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
2535
2536 if (system(buf) != 0) {
2537 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to run: %s", buf);
2538 return -1;
2539 }
2540 sigma_dut_print(dut, DUT_MSG_DEBUG,
2541 "Completed hs20-osu-client operation");
2542
2543 return 0;
2544}
2545
2546
2547static int download_ppsmo(struct sigma_dut *dut,
2548 struct sigma_conn *conn,
2549 const char *intf,
2550 struct sigma_cmd *cmd)
2551{
2552 const char *name, *path, *val;
2553 char url[500], buf[600], fbuf[100];
2554 char *fqdn = NULL;
2555
2556 name = get_param(cmd, "FileName");
2557 path = get_param(cmd, "FilePath");
2558 if (name == NULL || path == NULL)
2559 return -1;
2560
2561 if (strcasecmp(path, "VendorSpecific") == 0) {
2562 snprintf(url, sizeof(url), "PPS/%s", name);
2563 sigma_dut_print(dut, DUT_MSG_INFO, "Use pre-configured PPS MO "
2564 "from the device (%s)", url);
2565 if (!file_exists(url)) {
2566 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Requested "
2567 "PPS MO file does not exist");
2568 return 0;
2569 }
2570 snprintf(buf, sizeof(buf), "cp %s pps-tnds.xml", url);
2571 if (system(buf) != 0) {
2572 send_resp(dut, conn, SIGMA_ERROR,
2573 "errorCode,Failed to copy PPS MO");
2574 return 0;
2575 }
2576 } else if (strncasecmp(path, "http:", 5) != 0 &&
2577 strncasecmp(path, "https:", 6) != 0) {
2578 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,"
2579 "Unsupported FilePath value");
2580 return 0;
2581 } else {
2582 snprintf(url, sizeof(url), "%s/%s", path, name);
2583 sigma_dut_print(dut, DUT_MSG_INFO, "Downloading PPS MO from %s",
2584 url);
2585 snprintf(buf, sizeof(buf), "wget -T 10 -t 3 -O pps-tnds.xml '%s'", url);
2586 remove("pps-tnds.xml");
2587 if (system(buf) != 0) {
2588 send_resp(dut, conn, SIGMA_ERROR,
2589 "errorCode,Failed to download PPS MO");
2590 return 0;
2591 }
2592 }
2593
2594 if (run_hs20_osu(dut, "from_tnds pps-tnds.xml pps.xml") < 0) {
2595 send_resp(dut, conn, SIGMA_ERROR,
2596 "errorCode,Failed to parse downloaded PPSMO");
2597 return 0;
2598 }
2599 unlink("pps-tnds.xml");
2600
2601 val = get_param(cmd, "managementTreeURI");
2602 if (val) {
2603 const char *pos, *end;
2604 sigma_dut_print(dut, DUT_MSG_DEBUG, "managementTreeURI: %s",
2605 val);
2606 if (strncmp(val, "./Wi-Fi/", 8) != 0) {
2607 send_resp(dut, conn, SIGMA_ERROR,
2608 "errorCode,Invalid managementTreeURI prefix");
2609 return 0;
2610 }
2611 pos = val + 8;
2612 end = strchr(pos, '/');
2613 if (end == NULL ||
2614 strcmp(end, "/PerProviderSubscription") != 0) {
2615 send_resp(dut, conn, SIGMA_ERROR,
2616 "errorCode,Invalid managementTreeURI postfix");
2617 return 0;
2618 }
2619 if (end - pos >= (int) sizeof(fbuf)) {
2620 send_resp(dut, conn, SIGMA_ERROR,
2621 "errorCode,Too long FQDN in managementTreeURI");
2622 return 0;
2623 }
2624 memcpy(fbuf, pos, end - pos);
2625 fbuf[end - pos] = '\0';
2626 fqdn = fbuf;
2627 sigma_dut_print(dut, DUT_MSG_INFO,
2628 "FQDN from managementTreeURI: %s", fqdn);
2629 } else if (run_hs20_osu(dut, "get_fqdn pps.xml") == 0) {
2630 FILE *f = fopen("pps-fqdn", "r");
2631 if (f) {
2632 if (fgets(fbuf, sizeof(fbuf), f)) {
2633 fbuf[sizeof(fbuf) - 1] = '\0';
2634 fqdn = fbuf;
2635 sigma_dut_print(dut, DUT_MSG_DEBUG,
2636 "Use FQDN %s", fqdn);
2637 }
2638 fclose(f);
2639 }
2640 }
2641
2642 if (fqdn == NULL) {
2643 send_resp(dut, conn, SIGMA_ERROR,
2644 "errorCode,No FQDN specified");
2645 return 0;
2646 }
2647
2648 mkdir("SP", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
2649 snprintf(buf, sizeof(buf), "SP/%s", fqdn);
2650 mkdir(buf, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
2651
2652 snprintf(buf, sizeof(buf), "SP/%s/pps.xml", fqdn);
2653 if (rename("pps.xml", buf) < 0) {
2654 send_resp(dut, conn, SIGMA_ERROR,
2655 "errorCode,Could not move PPS MO");
2656 return 0;
2657 }
2658
2659 if (strcasecmp(path, "VendorSpecific") == 0) {
2660 snprintf(buf, sizeof(buf), "cp Certs/ca.pem SP/%s/ca.pem",
2661 fqdn);
2662 if (system(buf)) {
2663 send_resp(dut, conn, SIGMA_ERROR,
2664 "errorCode,Failed to copy OSU CA cert");
2665 return 0;
2666 }
2667
2668 snprintf(buf, sizeof(buf),
2669 "cp Certs/aaa-ca.pem SP/%s/aaa-ca.pem",
2670 fqdn);
2671 if (system(buf)) {
2672 send_resp(dut, conn, SIGMA_ERROR,
2673 "errorCode,Failed to copy AAA CA cert");
2674 return 0;
2675 }
2676 } else {
2677 snprintf(buf, sizeof(buf),
2678 "dl_osu_ca SP/%s/pps.xml SP/%s/ca.pem",
2679 fqdn, fqdn);
2680 if (run_hs20_osu(dut, buf) < 0) {
2681 send_resp(dut, conn, SIGMA_ERROR,
2682 "errorCode,Failed to download OSU CA cert");
2683 return 0;
2684 }
2685
2686 snprintf(buf, sizeof(buf),
2687 "dl_aaa_ca SP/%s/pps.xml SP/%s/aaa-ca.pem",
2688 fqdn, fqdn);
2689 if (run_hs20_osu(dut, buf) < 0) {
2690 sigma_dut_print(dut, DUT_MSG_INFO,
2691 "Failed to download AAA CA cert");
2692 }
2693 }
2694
2695 if (file_exists("next-client-cert.pem")) {
2696 snprintf(buf, sizeof(buf), "SP/%s/client-cert.pem", fqdn);
2697 if (rename("next-client-cert.pem", buf) < 0) {
2698 send_resp(dut, conn, SIGMA_ERROR,
2699 "errorCode,Could not move client certificate");
2700 return 0;
2701 }
2702 }
2703
2704 if (file_exists("next-client-key.pem")) {
2705 snprintf(buf, sizeof(buf), "SP/%s/client-key.pem", fqdn);
2706 if (rename("next-client-key.pem", buf) < 0) {
2707 send_resp(dut, conn, SIGMA_ERROR,
2708 "errorCode,Could not move client key");
2709 return 0;
2710 }
2711 }
2712
2713 snprintf(buf, sizeof(buf), "set_pps SP/%s/pps.xml", fqdn);
2714 if (run_hs20_osu(dut, buf) < 0) {
2715 send_resp(dut, conn, SIGMA_ERROR,
2716 "errorCode,Failed to configure credential from "
2717 "PPSMO");
2718 return 0;
2719 }
2720
2721 return 1;
2722}
2723
2724
2725static int download_cert(struct sigma_dut *dut,
2726 struct sigma_conn *conn,
2727 const char *intf,
2728 struct sigma_cmd *cmd)
2729{
2730 const char *name, *path;
2731 char url[500], buf[600];
2732
2733 name = get_param(cmd, "FileName");
2734 path = get_param(cmd, "FilePath");
2735 if (name == NULL || path == NULL)
2736 return -1;
2737
2738 if (strcasecmp(path, "VendorSpecific") == 0) {
2739 snprintf(url, sizeof(url), "Certs/%s-cert.pem", name);
2740 sigma_dut_print(dut, DUT_MSG_INFO, "Use pre-configured client "
2741 "certificate from the device (%s)", url);
2742 if (!file_exists(url)) {
2743 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Requested "
2744 "certificate file does not exist");
2745 return 0;
2746 }
2747 snprintf(buf, sizeof(buf), "cp %s next-client-cert.pem", url);
2748 if (system(buf) != 0) {
2749 send_resp(dut, conn, SIGMA_ERROR,
2750 "errorCode,Failed to copy client "
2751 "certificate");
2752 return 0;
2753 }
2754
2755 snprintf(url, sizeof(url), "Certs/%s-key.pem", name);
2756 sigma_dut_print(dut, DUT_MSG_INFO, "Use pre-configured client "
2757 "private key from the device (%s)", url);
2758 if (!file_exists(url)) {
2759 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Requested "
2760 "private key file does not exist");
2761 return 0;
2762 }
2763 snprintf(buf, sizeof(buf), "cp %s next-client-key.pem", url);
2764 if (system(buf) != 0) {
2765 send_resp(dut, conn, SIGMA_ERROR,
2766 "errorCode,Failed to copy client key");
2767 return 0;
2768 }
2769 } else if (strncasecmp(path, "http:", 5) != 0 &&
2770 strncasecmp(path, "https:", 6) != 0) {
2771 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,"
2772 "Unsupported FilePath value");
2773 return 0;
2774 } else {
2775 snprintf(url, sizeof(url), "%s/%s.pem", path, name);
2776 sigma_dut_print(dut, DUT_MSG_INFO, "Downloading client "
2777 "certificate/key from %s", url);
2778 snprintf(buf, sizeof(buf),
2779 "wget -T 10 -t 3 -O next-client-cert.pem '%s'", url);
2780 if (system(buf) != 0) {
2781 send_resp(dut, conn, SIGMA_ERROR,
2782 "errorCode,Failed to download client "
2783 "certificate");
2784 return 0;
2785 }
2786
2787 if (system("cp next-client-cert.pem next-client-key.pem") != 0)
2788 {
2789 send_resp(dut, conn, SIGMA_ERROR,
2790 "errorCode,Failed to copy client key");
2791 return 0;
2792 }
2793 }
2794
2795 return 1;
2796}
2797
2798
2799static int cmd_sta_preset_testparameters_hs2_r2(struct sigma_dut *dut,
2800 struct sigma_conn *conn,
2801 const char *intf,
2802 struct sigma_cmd *cmd)
2803{
2804 const char *val;
2805
2806 val = get_param(cmd, "FileType");
2807 if (val && strcasecmp(val, "PPSMO") == 0)
2808 return download_ppsmo(dut, conn, intf, cmd);
2809 if (val && strcasecmp(val, "CERT") == 0)
2810 return download_cert(dut, conn, intf, cmd);
2811 if (val) {
2812 send_resp(dut, conn, SIGMA_ERROR,
2813 "ErrorCode,Unsupported FileType");
2814 return 0;
2815 }
2816
2817 return 1;
2818}
2819
2820
2821static void ath_sta_set_noack(struct sigma_dut *dut, const char *intf,
2822 const char *val)
2823{
2824 int counter = 0;
2825 char token[50];
2826 char *result;
2827 char buf[100];
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05302828 char *saveptr;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02002829
Peng Xub8fc5cc2017-05-10 17:27:28 -07002830 strlcpy(token, val, sizeof(token));
Jouni Malinencd4e3c32015-10-29 12:39:56 +02002831 token[sizeof(token) - 1] = '\0';
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05302832 result = strtok_r(token, ":", &saveptr);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02002833 while (result) {
2834 if (strcmp(result, "disable") == 0) {
2835 snprintf(buf, sizeof(buf),
2836 "iwpriv %s noackpolicy %d 1 0",
2837 intf, counter);
2838 } else {
2839 snprintf(buf, sizeof(buf),
2840 "iwpriv %s noackpolicy %d 1 1",
2841 intf, counter);
2842 }
2843 if (system(buf) != 0) {
2844 sigma_dut_print(dut, DUT_MSG_ERROR,
2845 "iwpriv noackpolicy failed");
2846 }
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05302847 result = strtok_r(NULL, ":", &saveptr);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02002848 counter++;
2849 }
2850}
2851
2852
2853static void ath_sta_set_rts(struct sigma_dut *dut, const char *intf,
2854 const char *val)
2855{
2856 char buf[100];
2857
2858 snprintf(buf, sizeof(buf), "iwconfig %s rts %s", intf, val);
2859 if (system(buf) != 0) {
2860 sigma_dut_print(dut, DUT_MSG_ERROR, "iwconfig RTS failed");
2861 }
2862}
2863
2864
2865static void ath_sta_set_wmm(struct sigma_dut *dut, const char *intf,
2866 const char *val)
2867{
2868 char buf[100];
2869
2870 if (strcasecmp(val, "off") == 0) {
2871 snprintf(buf, sizeof(buf), "iwpriv %s wmm 0", intf);
2872 if (system(buf) != 0) {
2873 sigma_dut_print(dut, DUT_MSG_ERROR,
2874 "Failed to turn off WMM");
2875 }
2876 }
2877}
2878
2879
2880static void ath_sta_set_sgi(struct sigma_dut *dut, const char *intf,
2881 const char *val)
2882{
2883 char buf[100];
2884 int sgi20;
2885
2886 sgi20 = strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0;
2887
2888 snprintf(buf, sizeof(buf), "iwpriv %s shortgi %d", intf, sgi20);
2889 if (system(buf) != 0)
2890 sigma_dut_print(dut, DUT_MSG_ERROR, "iwpriv shortgi failed");
2891}
2892
2893
2894static void ath_sta_set_11nrates(struct sigma_dut *dut, const char *intf,
2895 const char *val)
2896{
2897 char buf[100];
Pradeep Reddy POTTETI67376b72016-10-25 20:08:17 +05302898 int rate_code, v;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02002899
2900 /* Disable Tx Beam forming when using a fixed rate */
2901 ath_disable_txbf(dut, intf);
2902
Pradeep Reddy POTTETI67376b72016-10-25 20:08:17 +05302903 v = atoi(val);
2904 if (v < 0 || v > 32) {
2905 sigma_dut_print(dut, DUT_MSG_ERROR,
2906 "Invalid Fixed MCS rate: %d", v);
2907 return;
2908 }
2909 rate_code = 0x80 + v;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02002910
2911 snprintf(buf, sizeof(buf), "iwpriv %s set11NRates 0x%x",
2912 intf, rate_code);
2913 if (system(buf) != 0) {
2914 sigma_dut_print(dut, DUT_MSG_ERROR,
2915 "iwpriv set11NRates failed");
2916 }
2917
2918 /* Channel width gets messed up, fix this */
2919 snprintf(buf, sizeof(buf), "iwpriv %s chwidth %d", intf, dut->chwidth);
2920 if (system(buf) != 0)
2921 sigma_dut_print(dut, DUT_MSG_ERROR, "iwpriv chwidth failed");
2922}
2923
2924
2925static void ath_sta_set_amsdu(struct sigma_dut *dut, const char *intf,
2926 const char *val)
2927{
2928 char buf[60];
2929
2930 if (strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0)
2931 snprintf(buf, sizeof(buf), "iwpriv %s amsdu 2", intf);
2932 else
2933 snprintf(buf, sizeof(buf), "iwpriv %s amsdu 1", intf);
2934
2935 if (system(buf) != 0)
2936 sigma_dut_print(dut, DUT_MSG_ERROR, "iwpriv amsdu failed");
2937}
2938
2939
Deepak Dhamdhere80356cb2016-03-28 16:48:32 -07002940static int iwpriv_sta_set_ampdu(struct sigma_dut *dut, const char *intf,
2941 int ampdu)
2942{
2943 char buf[60];
2944
2945 snprintf(buf, sizeof(buf), "iwpriv %s ampdu %d", intf, ampdu);
2946 if (system(buf) != 0) {
2947 sigma_dut_print(dut, DUT_MSG_ERROR, "iwpriv ampdu failed");
2948 return -1;
2949 }
2950
2951 return 0;
2952}
2953
2954
Jouni Malinencd4e3c32015-10-29 12:39:56 +02002955static void ath_sta_set_stbc(struct sigma_dut *dut, const char *intf,
2956 const char *val)
2957{
2958 char buf[60];
2959
2960 snprintf(buf, sizeof(buf), "iwpriv %s tx_stbc %s", intf, val);
2961 if (system(buf) != 0) {
2962 sigma_dut_print(dut, DUT_MSG_ERROR,
2963 "iwpriv tx_stbc failed");
2964 }
2965
2966 snprintf(buf, sizeof(buf), "iwpriv %s rx_stbc %s", intf, val);
2967 if (system(buf) != 0) {
2968 sigma_dut_print(dut, DUT_MSG_ERROR,
2969 "iwpriv rx_stbc failed");
2970 }
2971}
2972
2973
2974static int wcn_sta_set_cts_width(struct sigma_dut *dut, const char *intf,
2975 const char *val)
2976{
2977 char buf[60];
2978
Peng Xucc317ed2017-05-18 16:44:37 -07002979 if (strcmp(val, "160") == 0) {
2980 snprintf(buf, sizeof(buf), "iwpriv %s cts_cbw 5", intf);
2981 } else if (strcmp(val, "80") == 0) {
Jouni Malinencd4e3c32015-10-29 12:39:56 +02002982 snprintf(buf, sizeof(buf), "iwpriv %s cts_cbw 3", intf);
2983 } else if (strcmp(val, "40") == 0) {
2984 snprintf(buf, sizeof(buf), "iwpriv %s cts_cbw 2", intf);
2985 } else if (strcmp(val, "20") == 0) {
2986 snprintf(buf, sizeof(buf), "iwpriv %s cts_cbw 1", intf);
2987 } else if (strcasecmp(val, "Auto") == 0) {
2988 buf[0] = '\0';
2989 } else {
2990 sigma_dut_print(dut, DUT_MSG_ERROR,
2991 "WIDTH/CTS_WIDTH value not supported");
2992 return -1;
2993 }
2994
2995 if (buf[0] != '\0' && system(buf) != 0) {
2996 sigma_dut_print(dut, DUT_MSG_ERROR,
2997 "Failed to set WIDTH/CTS_WIDTH");
2998 return -1;
2999 }
3000
3001 return 0;
3002}
3003
3004
3005int ath_set_width(struct sigma_dut *dut, struct sigma_conn *conn,
3006 const char *intf, const char *val)
3007{
3008 char buf[60];
3009
3010 if (strcasecmp(val, "Auto") == 0) {
3011 snprintf(buf, sizeof(buf), "iwpriv %s chwidth 0", intf);
3012 dut->chwidth = 0;
3013 } else if (strcasecmp(val, "20") == 0) {
3014 snprintf(buf, sizeof(buf), "iwpriv %s chwidth 0", intf);
3015 dut->chwidth = 0;
3016 } else if (strcasecmp(val, "40") == 0) {
3017 snprintf(buf, sizeof(buf), "iwpriv %s chwidth 1", intf);
3018 dut->chwidth = 1;
3019 } else if (strcasecmp(val, "80") == 0) {
3020 snprintf(buf, sizeof(buf), "iwpriv %s chwidth 2", intf);
3021 dut->chwidth = 2;
3022 } else if (strcasecmp(val, "160") == 0) {
3023 snprintf(buf, sizeof(buf), "iwpriv %s chwidth 3", intf);
3024 dut->chwidth = 3;
3025 } else {
3026 send_resp(dut, conn, SIGMA_ERROR,
3027 "ErrorCode,WIDTH not supported");
3028 return -1;
3029 }
3030
3031 if (system(buf) != 0) {
3032 sigma_dut_print(dut, DUT_MSG_ERROR,
3033 "iwpriv chwidth failed");
3034 }
3035
3036 return 0;
3037}
3038
3039
3040static int wcn_sta_set_sp_stream(struct sigma_dut *dut, const char *intf,
3041 const char *val)
3042{
3043 char buf[60];
3044
3045 if (strcmp(val, "1SS") == 0) {
3046 snprintf(buf, sizeof(buf), "iwpriv %s nss 1", intf);
3047 } else if (strcmp(val, "2SS") == 0) {
3048 snprintf(buf, sizeof(buf), "iwpriv %s nss 2", intf);
3049 } else {
3050 sigma_dut_print(dut, DUT_MSG_ERROR,
3051 "SP_STREAM value not supported");
3052 return -1;
3053 }
3054
3055 if (system(buf) != 0) {
3056 sigma_dut_print(dut, DUT_MSG_ERROR,
3057 "Failed to set SP_STREAM");
3058 return -1;
3059 }
3060
3061 return 0;
3062}
3063
3064
Pradeep Reddy POTTETI4a1f6b32016-11-23 13:15:21 +05303065static void wcn_sta_set_stbc(struct sigma_dut *dut, const char *intf,
3066 const char *val)
3067{
3068 char buf[60];
3069
3070 snprintf(buf, sizeof(buf), "iwpriv %s tx_stbc %s", intf, val);
3071 if (system(buf) != 0)
3072 sigma_dut_print(dut, DUT_MSG_ERROR, "iwpriv tx_stbc failed");
3073
3074 snprintf(buf, sizeof(buf), "iwpriv %s rx_stbc %s", intf, val);
3075 if (system(buf) != 0)
3076 sigma_dut_print(dut, DUT_MSG_ERROR, "iwpriv rx_stbc failed");
3077}
3078
3079
Ashwini Patil68d02cd2017-01-10 15:39:16 +05303080static int mbo_set_cellular_data_capa(struct sigma_dut *dut,
3081 struct sigma_conn *conn,
3082 const char *intf, int capa)
3083{
3084 char buf[32];
3085
3086 if (capa > 0 && capa < 4) {
3087 snprintf(buf, sizeof(buf), "SET mbo_cell_capa %d", capa);
3088 if (wpa_command(intf, buf) < 0) {
3089 send_resp(dut, conn, SIGMA_ERROR,
3090 "ErrorCode, Failed to set cellular data capability");
3091 return 0;
3092 }
3093 return 1;
3094 }
3095
3096 sigma_dut_print(dut, DUT_MSG_ERROR,
3097 "Invalid Cellular data capability: %d", capa);
3098 send_resp(dut, conn, SIGMA_INVALID,
3099 "ErrorCode,Invalid cellular data capability");
3100 return 0;
3101}
3102
3103
Ashwini Patil9183fdb2017-04-13 16:58:25 +05303104static int mbo_set_roaming(struct sigma_dut *dut, struct sigma_conn *conn,
3105 const char *intf, const char *val)
3106{
3107 if (strcasecmp(val, "Disable") == 0) {
3108 if (wpa_command(intf, "SET roaming 0") < 0) {
3109 send_resp(dut, conn, SIGMA_ERROR,
3110 "ErrorCode,Failed to disable roaming");
3111 return 0;
3112 }
3113 return 1;
3114 }
3115
3116 if (strcasecmp(val, "Enable") == 0) {
3117 if (wpa_command(intf, "SET roaming 1") < 0) {
3118 send_resp(dut, conn, SIGMA_ERROR,
3119 "ErrorCode,Failed to enable roaming");
3120 return 0;
3121 }
3122 return 1;
3123 }
3124
3125 sigma_dut_print(dut, DUT_MSG_ERROR,
3126 "Invalid value provided for roaming: %s", val);
3127 send_resp(dut, conn, SIGMA_INVALID,
3128 "ErrorCode,Unknown value provided for Roaming");
3129 return 0;
3130}
3131
3132
Ashwini Patila75de5a2017-04-13 16:35:05 +05303133static int mbo_set_assoc_disallow(struct sigma_dut *dut,
3134 struct sigma_conn *conn,
3135 const char *intf, const char *val)
3136{
3137 if (strcasecmp(val, "Disable") == 0) {
3138 if (wpa_command(intf, "SET ignore_assoc_disallow 1") < 0) {
3139 send_resp(dut, conn, SIGMA_ERROR,
3140 "ErrorCode,Failed to disable Assoc_disallow");
3141 return 0;
3142 }
3143 return 1;
3144 }
3145
3146 if (strcasecmp(val, "Enable") == 0) {
3147 if (wpa_command(intf, "SET ignore_assoc_disallow 0") < 0) {
3148 send_resp(dut, conn, SIGMA_ERROR,
3149 "ErrorCode,Failed to enable Assoc_disallow");
3150 return 0;
3151 }
3152 return 1;
3153 }
3154
3155 sigma_dut_print(dut, DUT_MSG_ERROR,
3156 "Invalid value provided for Assoc_disallow: %s", val);
3157 send_resp(dut, conn, SIGMA_INVALID,
3158 "ErrorCode,Unknown value provided for Assoc_disallow");
3159 return 0;
3160}
3161
3162
Ashwini Patilc63161e2017-04-13 16:30:23 +05303163static int mbo_set_bss_trans_req(struct sigma_dut *dut, struct sigma_conn *conn,
3164 const char *intf, const char *val)
3165{
3166 if (strcasecmp(val, "Reject") == 0) {
3167 if (wpa_command(intf, "SET reject_btm_req_reason 1") < 0) {
3168 send_resp(dut, conn, SIGMA_ERROR,
3169 "ErrorCode,Failed to Reject BTM Request");
3170 return 0;
3171 }
3172 return 1;
3173 }
3174
3175 if (strcasecmp(val, "Accept") == 0) {
3176 if (wpa_command(intf, "SET reject_btm_req_reason 0") < 0) {
3177 send_resp(dut, conn, SIGMA_ERROR,
3178 "ErrorCode,Failed to Accept BTM Request");
3179 return 0;
3180 }
3181 return 1;
3182 }
3183
3184 sigma_dut_print(dut, DUT_MSG_ERROR,
3185 "Invalid value provided for BSS_Transition: %s", val);
3186 send_resp(dut, conn, SIGMA_INVALID,
3187 "ErrorCode,Unknown value provided for BSS_Transition");
3188 return 0;
3189}
3190
3191
Ashwini Patil00402582017-04-13 12:29:39 +05303192static int mbo_set_non_pref_ch_list(struct sigma_dut *dut,
3193 struct sigma_conn *conn,
3194 const char *intf,
3195 struct sigma_cmd *cmd)
3196{
3197 const char *ch, *pref, *op_class, *reason;
3198 char buf[120];
3199 int len, ret;
3200
3201 pref = get_param(cmd, "Ch_Pref");
3202 if (!pref)
3203 return 1;
3204
3205 if (strcasecmp(pref, "clear") == 0) {
3206 free(dut->non_pref_ch_list);
3207 dut->non_pref_ch_list = NULL;
3208 } else {
3209 op_class = get_param(cmd, "Ch_Op_Class");
3210 if (!op_class) {
3211 send_resp(dut, conn, SIGMA_INVALID,
3212 "ErrorCode,Ch_Op_Class not provided");
3213 return 0;
3214 }
3215
3216 ch = get_param(cmd, "Ch_Pref_Num");
3217 if (!ch) {
3218 send_resp(dut, conn, SIGMA_INVALID,
3219 "ErrorCode,Ch_Pref_Num not provided");
3220 return 0;
3221 }
3222
3223 reason = get_param(cmd, "Ch_Reason_Code");
3224 if (!reason) {
3225 send_resp(dut, conn, SIGMA_INVALID,
3226 "ErrorCode,Ch_Reason_Code not provided");
3227 return 0;
3228 }
3229
3230 if (!dut->non_pref_ch_list) {
3231 dut->non_pref_ch_list =
3232 calloc(1, NON_PREF_CH_LIST_SIZE);
3233 if (!dut->non_pref_ch_list) {
3234 send_resp(dut, conn, SIGMA_ERROR,
3235 "ErrorCode,Failed to allocate memory for non_pref_ch_list");
3236 return 0;
3237 }
3238 }
3239 len = strlen(dut->non_pref_ch_list);
3240 ret = snprintf(dut->non_pref_ch_list + len,
3241 NON_PREF_CH_LIST_SIZE - len,
3242 " %s:%s:%s:%s", op_class, ch, pref, reason);
3243 if (ret > 0 && ret < NON_PREF_CH_LIST_SIZE - len) {
3244 sigma_dut_print(dut, DUT_MSG_DEBUG, "non_pref_list: %s",
3245 dut->non_pref_ch_list);
3246 } else {
3247 sigma_dut_print(dut, DUT_MSG_ERROR,
3248 "snprintf failed for non_pref_list, ret = %d",
3249 ret);
3250 send_resp(dut, conn, SIGMA_ERROR,
3251 "ErrorCode,snprintf failed");
3252 free(dut->non_pref_ch_list);
3253 dut->non_pref_ch_list = NULL;
3254 return 0;
3255 }
3256 }
3257
3258 ret = snprintf(buf, sizeof(buf), "SET non_pref_chan%s",
3259 dut->non_pref_ch_list ? dut->non_pref_ch_list : " ");
3260 if (ret < 0 || ret >= (int) sizeof(buf)) {
3261 sigma_dut_print(dut, DUT_MSG_DEBUG,
3262 "snprintf failed for set non_pref_chan, ret: %d",
3263 ret);
3264 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,snprint failed");
3265 return 0;
3266 }
3267
3268 if (wpa_command(intf, buf) < 0) {
3269 send_resp(dut, conn, SIGMA_ERROR,
3270 "ErrorCode,Failed to set non-preferred channel list");
3271 return 0;
3272 }
3273
3274 return 1;
3275}
3276
3277
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003278static int cmd_sta_preset_testparameters(struct sigma_dut *dut,
3279 struct sigma_conn *conn,
3280 struct sigma_cmd *cmd)
3281{
3282 const char *intf = get_param(cmd, "Interface");
3283 const char *val;
3284
3285 val = get_param(cmd, "Program");
Peng Xue9fa7952017-05-09 15:59:49 -07003286 if (val && strcasecmp(val, "HS2-R2") == 0)
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003287 return cmd_sta_preset_testparameters_hs2_r2(dut, conn, intf,
3288 cmd);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003289
priyadharshini gowthamand66913a2016-07-29 15:11:17 -07003290 if (val && strcasecmp(val, "LOC") == 0)
3291 return loc_cmd_sta_preset_testparameters(dut, conn, cmd);
3292
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003293#ifdef ANDROID_NAN
3294 if (val && strcasecmp(val, "NAN") == 0)
3295 return nan_cmd_sta_preset_testparameters(dut, conn, cmd);
3296#endif /* ANDROID_NAN */
Amarnath Hullur Subramanyam9c381f52017-03-17 00:04:41 -07003297#ifdef MIRACAST
3298 if (val && (strcasecmp(val, "WFD") == 0 ||
3299 strcasecmp(val, "DisplayR2") == 0))
3300 return miracast_preset_testparameters(dut, conn, cmd);
3301#endif /* MIRACAST */
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003302
Ashwini Patil68d02cd2017-01-10 15:39:16 +05303303 if (val && strcasecmp(val, "MBO") == 0) {
3304 val = get_param(cmd, "Cellular_Data_Cap");
3305 if (val &&
3306 mbo_set_cellular_data_capa(dut, conn, intf, atoi(val)) == 0)
3307 return 0;
Ashwini Patil00402582017-04-13 12:29:39 +05303308
3309 val = get_param(cmd, "Ch_Pref");
3310 if (val && mbo_set_non_pref_ch_list(dut, conn, intf, cmd) == 0)
3311 return 0;
3312
Ashwini Patilc63161e2017-04-13 16:30:23 +05303313 val = get_param(cmd, "BSS_Transition");
3314 if (val && mbo_set_bss_trans_req(dut, conn, intf, val) == 0)
3315 return 0;
3316
Ashwini Patila75de5a2017-04-13 16:35:05 +05303317 val = get_param(cmd, "Assoc_Disallow");
3318 if (val && mbo_set_assoc_disallow(dut, conn, intf, val) == 0)
3319 return 0;
3320
Ashwini Patil9183fdb2017-04-13 16:58:25 +05303321 val = get_param(cmd, "Roaming");
3322 if (val && mbo_set_roaming(dut, conn, intf, val) == 0)
3323 return 0;
3324
Ashwini Patil68d02cd2017-01-10 15:39:16 +05303325 return 1;
3326 }
3327
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003328#if 0
3329 val = get_param(cmd, "Supplicant");
3330 if (val && strcasecmp(val, "Default") != 0) {
3331 send_resp(dut, conn, SIGMA_ERROR,
3332 "ErrorCode,Only default(Vendor) supplicant "
3333 "supported");
3334 return 0;
3335 }
3336#endif
3337
3338 val = get_param(cmd, "RTS");
3339 if (val) {
3340 switch (get_driver_type()) {
3341 case DRIVER_ATHEROS:
3342 ath_sta_set_rts(dut, intf, val);
3343 break;
3344 default:
3345#if 0
3346 send_resp(dut, conn, SIGMA_ERROR,
3347 "ErrorCode,Setting RTS not supported");
3348 return 0;
3349#else
3350 sigma_dut_print(dut, DUT_MSG_DEBUG,
3351 "Setting RTS not supported");
3352 break;
3353#endif
3354 }
3355 }
3356
3357#if 0
3358 val = get_param(cmd, "FRGMNT");
3359 if (val) {
3360 /* TODO */
3361 send_resp(dut, conn, SIGMA_ERROR,
3362 "ErrorCode,Setting FRGMNT not supported");
3363 return 0;
3364 }
3365#endif
3366
3367#if 0
3368 val = get_param(cmd, "Preamble");
3369 if (val) {
3370 /* TODO: Long/Short */
3371 send_resp(dut, conn, SIGMA_ERROR,
3372 "ErrorCode,Setting Preamble not supported");
3373 return 0;
3374 }
3375#endif
3376
3377 val = get_param(cmd, "Mode");
3378 if (val) {
3379 if (strcmp(val, "11b") == 0 ||
3380 strcmp(val, "11g") == 0 ||
3381 strcmp(val, "11a") == 0 ||
3382 strcmp(val, "11n") == 0 ||
3383 strcmp(val, "11ng") == 0 ||
3384 strcmp(val, "11nl") == 0 ||
3385 strcmp(val, "11nl(nabg)") == 0 ||
3386 strcmp(val, "AC") == 0 ||
3387 strcmp(val, "11AC") == 0 ||
3388 strcmp(val, "11ac") == 0 ||
3389 strcmp(val, "11na") == 0 ||
3390 strcmp(val, "11an") == 0) {
3391 /* STA supports all modes by default */
3392 } else {
3393 send_resp(dut, conn, SIGMA_ERROR,
3394 "ErrorCode,Setting Mode not supported");
3395 return 0;
3396 }
3397 }
3398
3399 val = get_param(cmd, "wmm");
3400 if (val) {
3401 switch (get_driver_type()) {
3402 case DRIVER_ATHEROS:
3403 ath_sta_set_wmm(dut, intf, val);
3404 break;
3405 default:
3406 sigma_dut_print(dut, DUT_MSG_DEBUG,
3407 "Setting wmm not supported");
3408 break;
3409 }
3410 }
3411
3412 val = get_param(cmd, "Powersave");
3413 if (val) {
3414 if (strcmp(val, "0") == 0 || strcasecmp(val, "off") == 0) {
3415 if (wpa_command(get_station_ifname(),
3416 "P2P_SET ps 0") < 0)
3417 return -2;
3418 /* Make sure test modes are disabled */
3419 wpa_command(get_station_ifname(), "P2P_SET ps 98");
3420 wpa_command(get_station_ifname(), "P2P_SET ps 96");
3421 } else if (strcmp(val, "1") == 0 ||
3422 strcasecmp(val, "PSPoll") == 0 ||
3423 strcasecmp(val, "on") == 0) {
3424 /* Disable default power save mode */
3425 wpa_command(get_station_ifname(), "P2P_SET ps 0");
3426 /* Enable PS-Poll test mode */
3427 if (wpa_command(get_station_ifname(),
3428 "P2P_SET ps 97") < 0 ||
3429 wpa_command(get_station_ifname(),
3430 "P2P_SET ps 99") < 0)
3431 return -2;
3432 } else if (strcmp(val, "2") == 0 ||
3433 strcasecmp(val, "Fast") == 0) {
3434 /* TODO */
3435 send_resp(dut, conn, SIGMA_ERROR,
3436 "ErrorCode,Powersave=Fast not supported");
3437 return 0;
3438 } else if (strcmp(val, "3") == 0 ||
3439 strcasecmp(val, "PSNonPoll") == 0) {
3440 /* Make sure test modes are disabled */
3441 wpa_command(get_station_ifname(), "P2P_SET ps 98");
3442 wpa_command(get_station_ifname(), "P2P_SET ps 96");
3443
3444 /* Enable default power save mode */
3445 if (wpa_command(get_station_ifname(),
3446 "P2P_SET ps 1") < 0)
3447 return -2;
3448 } else
3449 return -1;
3450 }
3451
3452 val = get_param(cmd, "NoAck");
3453 if (val) {
3454 switch (get_driver_type()) {
3455 case DRIVER_ATHEROS:
3456 ath_sta_set_noack(dut, intf, val);
3457 break;
3458 default:
3459 send_resp(dut, conn, SIGMA_ERROR,
3460 "ErrorCode,Setting NoAck not supported");
3461 return 0;
3462 }
3463 }
3464
3465 val = get_param(cmd, "IgnoreChswitchProhibit");
3466 if (val) {
3467 /* TODO: Enabled/disabled */
3468 if (strcasecmp(val, "Enabled") == 0) {
3469 send_resp(dut, conn, SIGMA_ERROR,
3470 "ErrorCode,Enabling IgnoreChswitchProhibit "
3471 "not supported");
3472 return 0;
3473 }
3474 }
3475
3476 val = get_param(cmd, "TDLS");
3477 if (val) {
3478 if (strcasecmp(val, "Disabled") == 0) {
3479 if (wpa_command(intf, "SET tdls_disabled 1")) {
3480 send_resp(dut, conn, SIGMA_ERROR,
3481 "ErrorCode,Failed to disable TDLS");
3482 return 0;
3483 }
3484 } else if (strcasecmp(val, "Enabled") == 0) {
3485 if (wpa_command(intf, "SET tdls_disabled 0")) {
3486 send_resp(dut, conn, SIGMA_ERROR,
3487 "ErrorCode,Failed to enable TDLS");
3488 return 0;
3489 }
3490 } else {
3491 send_resp(dut, conn, SIGMA_ERROR,
3492 "ErrorCode,Unsupported TDLS value");
3493 return 0;
3494 }
3495 }
3496
3497 val = get_param(cmd, "TDLSmode");
3498 if (val) {
3499 if (strcasecmp(val, "Default") == 0) {
3500 wpa_command(intf, "SET tdls_testing 0");
3501 } else if (strcasecmp(val, "APProhibit") == 0) {
3502 if (wpa_command(intf, "SET tdls_testing 0x400")) {
3503 send_resp(dut, conn, SIGMA_ERROR,
3504 "ErrorCode,Failed to enable ignore "
3505 "APProhibit TDLS mode");
3506 return 0;
3507 }
3508 } else if (strcasecmp(val, "HiLoMac") == 0) {
3509 /* STA should respond with TDLS setup req for a TDLS
3510 * setup req */
3511 if (wpa_command(intf, "SET tdls_testing 0x80")) {
3512 send_resp(dut, conn, SIGMA_ERROR,
3513 "ErrorCode,Failed to enable HiLoMac "
3514 "TDLS mode");
3515 return 0;
3516 }
3517 } else if (strcasecmp(val, "WeakSecurity") == 0) {
3518 /*
3519 * Since all security modes are enabled by default when
3520 * Sigma control is used, there is no need to do
3521 * anything here.
3522 */
3523 } else if (strcasecmp(val, "ExistLink") == 0) {
3524 /*
3525 * Since we allow new TDLS Setup Request even if there
3526 * is an existing link, nothing needs to be done for
3527 * this.
3528 */
3529 } else {
3530 /* TODO:
3531 * ExistLink: STA should send TDLS setup req even if
3532 * direct link already exists
3533 */
3534 send_resp(dut, conn, SIGMA_ERROR,
3535 "ErrorCode,Unsupported TDLSmode value");
3536 return 0;
3537 }
3538 }
3539
3540 val = get_param(cmd, "FakePubKey");
3541 if (val && atoi(val) && wpa_command(intf, "SET wps_corrupt_pkhash 1")) {
3542 send_resp(dut, conn, SIGMA_ERROR,
3543 "ErrorCode,Failed to enable FakePubKey");
3544 return 0;
3545 }
3546
3547 return 1;
3548}
3549
3550
3551static const char * ath_get_radio_name(const char *radio_name)
3552{
3553 if (radio_name == NULL)
3554 return "wifi0";
3555 if (strcmp(radio_name, "wifi1") == 0)
3556 return "wifi1";
3557 if (strcmp(radio_name, "wifi2") == 0)
3558 return "wifi2";
3559 return "wifi0";
3560}
3561
3562
3563static void ath_sta_set_txsp_stream(struct sigma_dut *dut, const char *intf,
3564 const char *val)
3565{
3566 char buf[60];
3567 unsigned int vht_mcsmap = 0;
3568 int txchainmask = 0;
3569 const char *basedev = ath_get_radio_name(sigma_radio_ifname[0]);
3570
3571 if (strcasecmp(val, "1") == 0 || strcasecmp(val, "1SS") == 0) {
3572 if (dut->testbed_flag_txsp == 1) {
3573 vht_mcsmap = 0xfffc;
3574 dut->testbed_flag_txsp = 0;
3575 } else {
3576 vht_mcsmap = 0xfffe;
3577 }
3578 txchainmask = 1;
3579 } else if (strcasecmp(val, "2") == 0 || strcasecmp(val, "2SS") == 0) {
3580 if (dut->testbed_flag_txsp == 1) {
3581 vht_mcsmap = 0xfff0;
3582 dut->testbed_flag_txsp = 0;
3583 } else {
3584 vht_mcsmap = 0xfffa;
3585 }
3586 txchainmask = 3;
3587 } else if (strcasecmp(val, "3") == 0 || strcasecmp(val, "3SS") == 0) {
3588 if (dut->testbed_flag_txsp == 1) {
3589 vht_mcsmap = 0xffc0;
3590 dut->testbed_flag_txsp = 0;
3591 } else {
3592 vht_mcsmap = 0xffea;
3593 }
3594 txchainmask = 7;
3595 } else if (strcasecmp(val, "4") == 0 || strcasecmp(val, "4SS") == 0) {
3596 if (dut->testbed_flag_txsp == 1) {
3597 vht_mcsmap = 0xff00;
3598 dut->testbed_flag_txsp = 0;
3599 } else {
3600 vht_mcsmap = 0xffaa;
3601 }
3602 txchainmask = 15;
3603 } else {
3604 if (dut->testbed_flag_txsp == 1) {
3605 vht_mcsmap = 0xffc0;
3606 dut->testbed_flag_txsp = 0;
3607 } else {
3608 vht_mcsmap = 0xffea;
3609 }
3610 }
3611
3612 if (txchainmask) {
3613 snprintf(buf, sizeof(buf), "iwpriv %s txchainmask %d",
3614 basedev, txchainmask);
3615 if (system(buf) != 0) {
3616 sigma_dut_print(dut, DUT_MSG_ERROR,
3617 "iwpriv txchainmask failed");
3618 }
3619 }
3620
3621 snprintf(buf, sizeof(buf), "iwpriv %s vht_mcsmap 0x%04x",
3622 intf, vht_mcsmap);
3623 if (system(buf) != 0) {
3624 sigma_dut_print(dut, DUT_MSG_ERROR,
3625 "iwpriv %s vht_mcsmap 0x%04x failed",
3626 intf, vht_mcsmap);
3627 }
3628}
3629
3630
3631static void ath_sta_set_rxsp_stream(struct sigma_dut *dut, const char *intf,
3632 const char *val)
3633{
3634 char buf[60];
3635 unsigned int vht_mcsmap = 0;
3636 int rxchainmask = 0;
3637 const char *basedev = ath_get_radio_name(sigma_radio_ifname[0]);
3638
3639 if (strcasecmp(val, "1") == 0 || strcasecmp(val, "1SS") == 0) {
3640 if (dut->testbed_flag_rxsp == 1) {
3641 vht_mcsmap = 0xfffc;
3642 dut->testbed_flag_rxsp = 0;
3643 } else {
3644 vht_mcsmap = 0xfffe;
3645 }
3646 rxchainmask = 1;
3647 } else if (strcasecmp(val, "2") == 0 || strcasecmp(val, "2SS") == 0) {
3648 if (dut->testbed_flag_rxsp == 1) {
3649 vht_mcsmap = 0xfff0;
3650 dut->testbed_flag_rxsp = 0;
3651 } else {
3652 vht_mcsmap = 0xfffa;
3653 }
3654 rxchainmask = 3;
3655 } else if (strcasecmp(val, "3") == 0 || strcasecmp(val, "3SS") == 0) {
3656 if (dut->testbed_flag_rxsp == 1) {
3657 vht_mcsmap = 0xffc0;
3658 dut->testbed_flag_rxsp = 0;
3659 } else {
3660 vht_mcsmap = 0xffea;
3661 }
3662 rxchainmask = 7;
3663 } else if (strcasecmp(val, "4") == 0 || strcasecmp(val, "4SS") == 0) {
3664 if (dut->testbed_flag_rxsp == 1) {
3665 vht_mcsmap = 0xff00;
3666 dut->testbed_flag_rxsp = 0;
3667 } else {
3668 vht_mcsmap = 0xffaa;
3669 }
3670 rxchainmask = 15;
3671 } else {
3672 if (dut->testbed_flag_rxsp == 1) {
3673 vht_mcsmap = 0xffc0;
3674 dut->testbed_flag_rxsp = 0;
3675 } else {
3676 vht_mcsmap = 0xffea;
3677 }
3678 }
3679
3680 if (rxchainmask) {
3681 snprintf(buf, sizeof(buf), "iwpriv %s rxchainmask %d",
3682 basedev, rxchainmask);
3683 if (system(buf) != 0) {
3684 sigma_dut_print(dut, DUT_MSG_ERROR,
3685 "iwpriv rxchainmask failed");
3686 }
3687 }
3688
3689 snprintf(buf, sizeof(buf), "iwpriv %s vht_mcsmap 0x%04x",
3690 intf, vht_mcsmap);
3691 if (system(buf) != 0) {
3692 sigma_dut_print(dut, DUT_MSG_ERROR,
3693 "iwpriv %s vht_mcsmap 0x%04x",
3694 intf, vht_mcsmap);
3695 }
3696}
3697
3698
3699void ath_set_zero_crc(struct sigma_dut *dut, const char *val)
3700{
3701 if (strcasecmp(val, "enable") == 0) {
3702 if (system("athdiag --set --address=0x2a204 --and=0xbfffffff")
3703 != 0) {
3704 sigma_dut_print(dut, DUT_MSG_ERROR,
3705 "Disable BB_VHTSIGB_CRC_CALC failed");
3706 }
3707
3708 if (system("athdiag --set --address=0x2a204 --or=0x80000000")
3709 != 0) {
3710 sigma_dut_print(dut, DUT_MSG_ERROR,
3711 "Enable FORCE_VHT_SIGB_CRC_VALUE_ZERO failed");
3712 }
3713 } else {
3714 if (system("athdiag --set --address=0x2a204 --and=0x7fffffff")
3715 != 0) {
3716 sigma_dut_print(dut, DUT_MSG_ERROR,
3717 "Disable FORCE_VHT_SIGB_CRC_VALUE_ZERO failed");
3718 }
3719
3720 if (system("athdiag --set --address=0x2a204 --or=0x40000000")
3721 != 0) {
3722 sigma_dut_print(dut, DUT_MSG_ERROR,
3723 "Enable BB_VHTSIGB_CRC_CALC failed");
3724 }
3725 }
3726}
3727
3728
3729static int cmd_sta_set_wireless_common(const char *intf, struct sigma_dut *dut,
3730 struct sigma_conn *conn,
3731 struct sigma_cmd *cmd)
3732{
3733 const char *val;
3734 int ampdu = -1;
3735 char buf[30];
3736
3737 val = get_param(cmd, "40_INTOLERANT");
3738 if (val) {
3739 if (strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0) {
3740 /* TODO: iwpriv ht40intol through wpa_supplicant */
3741 send_resp(dut, conn, SIGMA_ERROR,
3742 "ErrorCode,40_INTOLERANT not supported");
3743 return 0;
3744 }
3745 }
3746
3747 val = get_param(cmd, "ADDBA_REJECT");
3748 if (val) {
3749 if (strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0) {
3750 /* reject any ADDBA with status "decline" */
3751 ampdu = 0;
3752 } else {
3753 /* accept ADDBA */
3754 ampdu = 1;
3755 }
3756 }
3757
3758 val = get_param(cmd, "AMPDU");
3759 if (val) {
3760 if (strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0) {
3761 /* enable AMPDU Aggregation */
3762 if (ampdu == 0) {
3763 send_resp(dut, conn, SIGMA_ERROR,
3764 "ErrorCode,Mismatch in "
3765 "addba_reject/ampdu - "
3766 "not supported");
3767 return 0;
3768 }
3769 ampdu = 1;
3770 } else {
3771 /* disable AMPDU Aggregation */
3772 if (ampdu == 1) {
3773 send_resp(dut, conn, SIGMA_ERROR,
3774 "ErrorCode,Mismatch in "
3775 "addba_reject/ampdu - "
3776 "not supported");
3777 return 0;
3778 }
3779 ampdu = 0;
3780 }
3781 }
3782
3783 if (ampdu >= 0) {
3784 sigma_dut_print(dut, DUT_MSG_DEBUG, "%s A-MPDU aggregation",
3785 ampdu ? "Enabling" : "Disabling");
3786 snprintf(buf, sizeof(buf), "SET ampdu %d", ampdu);
Deepak Dhamdhere80356cb2016-03-28 16:48:32 -07003787 if (wpa_command(intf, buf) < 0 &&
3788 iwpriv_sta_set_ampdu(dut, intf, ampdu) < 0) {
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003789 send_resp(dut, conn, SIGMA_ERROR,
3790 "ErrorCode,set aggr failed");
3791 return 0;
3792 }
3793 }
3794
3795 val = get_param(cmd, "AMSDU");
3796 if (val) {
3797 switch (get_driver_type()) {
3798 case DRIVER_ATHEROS:
3799 ath_sta_set_amsdu(dut, intf, val);
3800 break;
3801 default:
3802 if (strcmp(val, "1") == 0 ||
3803 strcasecmp(val, "Enable") == 0) {
3804 /* Enable AMSDU Aggregation */
3805 send_resp(dut, conn, SIGMA_ERROR,
3806 "ErrorCode,AMSDU aggregation not supported");
3807 return 0;
3808 }
3809 break;
3810 }
3811 }
3812
3813 val = get_param(cmd, "STBC_RX");
3814 if (val) {
3815 switch (get_driver_type()) {
3816 case DRIVER_ATHEROS:
3817 ath_sta_set_stbc(dut, intf, val);
3818 break;
Pradeep Reddy POTTETI4a1f6b32016-11-23 13:15:21 +05303819 case DRIVER_WCN:
3820 wcn_sta_set_stbc(dut, intf, val);
3821 break;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003822 default:
3823 send_resp(dut, conn, SIGMA_ERROR,
3824 "ErrorCode,STBC_RX not supported");
3825 return 0;
3826 }
3827 }
3828
3829 val = get_param(cmd, "WIDTH");
3830 if (val) {
3831 switch (get_driver_type()) {
3832 case DRIVER_WCN:
3833 if (wcn_sta_set_cts_width(dut, intf, val) < 0) {
3834 send_resp(dut, conn, SIGMA_ERROR,
3835 "ErrorCode,Failed to set WIDTH");
3836 return 0;
3837 }
3838 break;
3839 case DRIVER_ATHEROS:
3840 if (ath_set_width(dut, conn, intf, val) < 0)
3841 return 0;
3842 break;
3843 default:
3844 sigma_dut_print(dut, DUT_MSG_ERROR,
3845 "Setting WIDTH not supported");
3846 break;
3847 }
3848 }
3849
3850 val = get_param(cmd, "SMPS");
3851 if (val) {
3852 /* TODO: Dynamic/0, Static/1, No Limit/2 */
3853 send_resp(dut, conn, SIGMA_ERROR,
3854 "ErrorCode,SMPS not supported");
3855 return 0;
3856 }
3857
3858 val = get_param(cmd, "TXSP_STREAM");
3859 if (val) {
3860 switch (get_driver_type()) {
3861 case DRIVER_WCN:
3862 if (wcn_sta_set_sp_stream(dut, intf, val) < 0) {
3863 send_resp(dut, conn, SIGMA_ERROR,
3864 "ErrorCode,Failed to set TXSP_STREAM");
3865 return 0;
3866 }
3867 break;
3868 case DRIVER_ATHEROS:
3869 ath_sta_set_txsp_stream(dut, intf, val);
3870 break;
3871 default:
3872 sigma_dut_print(dut, DUT_MSG_ERROR,
3873 "Setting TXSP_STREAM not supported");
3874 break;
3875 }
3876 }
3877
3878 val = get_param(cmd, "RXSP_STREAM");
3879 if (val) {
3880 switch (get_driver_type()) {
3881 case DRIVER_WCN:
3882 if (wcn_sta_set_sp_stream(dut, intf, val) < 0) {
3883 send_resp(dut, conn, SIGMA_ERROR,
3884 "ErrorCode,Failed to set RXSP_STREAM");
3885 return 0;
3886 }
3887 break;
3888 case DRIVER_ATHEROS:
3889 ath_sta_set_rxsp_stream(dut, intf, val);
3890 break;
3891 default:
3892 sigma_dut_print(dut, DUT_MSG_ERROR,
3893 "Setting RXSP_STREAM not supported");
3894 break;
3895 }
3896 }
3897
3898 val = get_param(cmd, "DYN_BW_SGNL");
3899 if (val) {
Priyadharshini Gowthaman818afef2015-11-09 13:28:15 -08003900 switch (get_driver_type()) {
3901 case DRIVER_WCN:
Peng Xuc59afd32016-11-21 15:01:11 -08003902 if (strcasecmp(val, "enable") == 0) {
3903 snprintf(buf, sizeof(buf),
3904 "iwpriv %s cwmenable 1", intf);
3905 if (system(buf) != 0) {
3906 sigma_dut_print(dut, DUT_MSG_ERROR,
3907 "iwpriv cwmenable 1 failed");
3908 return 0;
3909 }
3910 } else if (strcasecmp(val, "disable") == 0) {
3911 snprintf(buf, sizeof(buf),
3912 "iwpriv %s cwmenable 0", intf);
3913 if (system(buf) != 0) {
3914 sigma_dut_print(dut, DUT_MSG_ERROR,
3915 "iwpriv cwmenable 0 failed");
3916 return 0;
3917 }
3918 } else {
3919 sigma_dut_print(dut, DUT_MSG_ERROR,
3920 "Unsupported DYN_BW_SGL");
3921 }
3922
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003923 snprintf(buf, sizeof(buf), "iwpriv %s cts_cbw 3", intf);
3924 if (system(buf) != 0) {
3925 sigma_dut_print(dut, DUT_MSG_ERROR,
3926 "Failed to set cts_cbw in DYN_BW_SGNL");
3927 return 0;
3928 }
Priyadharshini Gowthaman818afef2015-11-09 13:28:15 -08003929 break;
3930 case DRIVER_ATHEROS:
priyadharshini gowthamane5e25172015-12-08 14:53:48 -08003931 novap_reset(dut, intf);
Priyadharshini Gowthaman818afef2015-11-09 13:28:15 -08003932 ath_config_dyn_bw_sig(dut, intf, val);
3933 break;
3934 default:
3935 sigma_dut_print(dut, DUT_MSG_ERROR,
3936 "Failed to set DYN_BW_SGNL");
3937 break;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003938 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003939 }
3940
3941 val = get_param(cmd, "RTS_FORCE");
3942 if (val) {
priyadharshini gowthamane5e25172015-12-08 14:53:48 -08003943 novap_reset(dut, intf);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003944 if (strcasecmp(val, "Enable") == 0) {
3945 snprintf(buf, sizeof(buf), "iwconfig %s rts 64", intf);
Priyadharshini Gowthamanabdb2122015-11-17 11:52:19 +02003946 if (system(buf) != 0) {
3947 sigma_dut_print(dut, DUT_MSG_ERROR,
3948 "Failed to set RTS_FORCE 64");
3949 }
priyadharshini gowthaman270870e2015-12-09 10:10:23 -08003950 snprintf(buf, sizeof(buf),
3951 "wifitool %s beeliner_fw_test 100 1", intf);
3952 if (system(buf) != 0) {
3953 sigma_dut_print(dut, DUT_MSG_ERROR,
3954 "wifitool beeliner_fw_test 100 1 failed");
3955 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003956 } else if (strcasecmp(val, "Disable") == 0) {
3957 snprintf(buf, sizeof(buf), "iwconfig %s rts 2347",
3958 intf);
Priyadharshini Gowthamanabdb2122015-11-17 11:52:19 +02003959 if (system(buf) != 0) {
3960 sigma_dut_print(dut, DUT_MSG_ERROR,
3961 "Failed to set RTS_FORCE 2347");
3962 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003963 } else {
3964 send_resp(dut, conn, SIGMA_ERROR,
3965 "ErrorCode,RTS_FORCE value not supported");
3966 return 0;
3967 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02003968 }
3969
3970 val = get_param(cmd, "CTS_WIDTH");
3971 if (val) {
3972 switch (get_driver_type()) {
3973 case DRIVER_WCN:
3974 if (wcn_sta_set_cts_width(dut, intf, val) < 0) {
3975 send_resp(dut, conn, SIGMA_ERROR,
3976 "ErrorCode,Failed to set CTS_WIDTH");
3977 return 0;
3978 }
3979 break;
3980 case DRIVER_ATHEROS:
3981 ath_set_cts_width(dut, intf, val);
3982 break;
3983 default:
3984 sigma_dut_print(dut, DUT_MSG_ERROR,
3985 "Setting CTS_WIDTH not supported");
3986 break;
3987 }
3988 }
3989
3990 val = get_param(cmd, "BW_SGNL");
3991 if (val) {
3992 if (strcasecmp(val, "Enable") == 0) {
3993 snprintf(buf, sizeof(buf), "iwpriv %s cwmenable 1",
3994 intf);
3995 } else if (strcasecmp(val, "Disable") == 0) {
3996 /* TODO: Disable */
3997 buf[0] = '\0';
3998 } else {
3999 send_resp(dut, conn, SIGMA_ERROR,
4000 "ErrorCode,BW_SGNL value not supported");
4001 return 0;
4002 }
4003
4004 if (buf[0] != '\0' && system(buf) != 0) {
4005 sigma_dut_print(dut, DUT_MSG_ERROR,
4006 "Failed to set BW_SGNL");
4007 }
4008 }
4009
4010 val = get_param(cmd, "Band");
4011 if (val) {
4012 if (strcmp(val, "2.4") == 0 || strcmp(val, "5") == 0) {
4013 /* STA supports all bands by default */
4014 } else {
4015 send_resp(dut, conn, SIGMA_ERROR,
4016 "ErrorCode,Unsupported Band");
4017 return 0;
4018 }
4019 }
4020
4021 val = get_param(cmd, "zero_crc");
4022 if (val) {
4023 switch (get_driver_type()) {
4024 case DRIVER_ATHEROS:
4025 ath_set_zero_crc(dut, val);
4026 break;
4027 default:
4028 break;
4029 }
4030 }
4031
4032 return 1;
4033}
4034
4035
4036static int sta_set_60g_common(struct sigma_dut *dut, struct sigma_conn *conn,
4037 struct sigma_cmd *cmd)
4038{
4039 const char *val;
4040 char buf[100];
4041
4042 val = get_param(cmd, "MSDUSize");
4043 if (val) {
4044 int mtu;
4045
4046 dut->amsdu_size = atoi(val);
4047 if (dut->amsdu_size > IEEE80211_MAX_DATA_LEN_DMG ||
4048 dut->amsdu_size < IEEE80211_SNAP_LEN_DMG) {
4049 sigma_dut_print(dut, DUT_MSG_ERROR,
4050 "MSDUSize %d is above max %d or below min %d",
4051 dut->amsdu_size,
4052 IEEE80211_MAX_DATA_LEN_DMG,
4053 IEEE80211_SNAP_LEN_DMG);
4054 dut->amsdu_size = 0;
4055 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
4056 }
4057
4058 mtu = dut->amsdu_size - IEEE80211_SNAP_LEN_DMG;
4059 sigma_dut_print(dut, DUT_MSG_DEBUG,
4060 "Setting amsdu_size to %d", mtu);
4061 snprintf(buf, sizeof(buf), "ifconfig %s mtu %d",
4062 get_station_ifname(), mtu);
4063
4064 if (system(buf) != 0) {
4065 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to set %s",
4066 buf);
4067 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
4068 }
4069 }
4070
4071 val = get_param(cmd, "BAckRcvBuf");
4072 if (val) {
4073 dut->back_rcv_buf = atoi(val);
4074 if (dut->back_rcv_buf == 0) {
4075 sigma_dut_print(dut, DUT_MSG_ERROR,
4076 "Failed to convert %s or value is 0",
4077 val);
4078 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
4079 }
4080
4081 sigma_dut_print(dut, DUT_MSG_DEBUG,
4082 "Setting BAckRcvBuf to %s", val);
4083 }
4084
4085 return SIGMA_DUT_SUCCESS_CALLER_SEND_STATUS;
4086}
4087
4088
4089static int sta_pcp_start(struct sigma_dut *dut, struct sigma_conn *conn,
4090 struct sigma_cmd *cmd)
4091{
4092 int net_id;
4093 char *ifname;
4094 const char *val;
4095 char buf[100];
4096
4097 dut->mode = SIGMA_MODE_STATION;
4098 ifname = get_main_ifname();
4099 if (wpa_command(ifname, "PING") != 0) {
4100 sigma_dut_print(dut, DUT_MSG_ERROR, "Supplicant not running");
4101 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
4102 }
4103
4104 wpa_command(ifname, "FLUSH");
4105 net_id = add_network_common(dut, conn, ifname, cmd);
4106 if (net_id < 0) {
4107 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to add network");
4108 return net_id;
4109 }
4110
4111 /* TODO: mode=2 for the AP; in the future, replace for mode PCP */
4112 if (set_network(ifname, net_id, "mode", "2") < 0) {
4113 sigma_dut_print(dut, DUT_MSG_ERROR,
4114 "Failed to set supplicant network mode");
4115 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
4116 }
4117
4118 sigma_dut_print(dut, DUT_MSG_DEBUG,
4119 "Supplicant set network with mode 2");
4120
4121 val = get_param(cmd, "Security");
4122 if (val && strcasecmp(val, "OPEN") == 0) {
4123 dut->ap_key_mgmt = AP_OPEN;
4124 if (set_network(ifname, net_id, "key_mgmt", "NONE") < 0) {
4125 sigma_dut_print(dut, DUT_MSG_ERROR,
4126 "Failed to set supplicant to %s security",
4127 val);
4128 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
4129 }
4130 } else if (val && strcasecmp(val, "WPA2-PSK") == 0) {
4131 dut->ap_key_mgmt = AP_WPA2_PSK;
4132 if (set_network(ifname, net_id, "key_mgmt", "WPA-PSK") < 0) {
4133 sigma_dut_print(dut, DUT_MSG_ERROR,
4134 "Failed to set supplicant to %s security",
4135 val);
4136 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
4137 }
4138
4139 if (set_network(ifname, net_id, "proto", "RSN") < 0) {
4140 sigma_dut_print(dut, DUT_MSG_ERROR,
4141 "Failed to set supplicant to proto RSN");
4142 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
4143 }
4144 } else if (val) {
4145 sigma_dut_print(dut, DUT_MSG_ERROR,
4146 "Requested Security %s is not supported on 60GHz",
4147 val);
4148 return SIGMA_DUT_INVALID_CALLER_SEND_STATUS;
4149 }
4150
4151 val = get_param(cmd, "Encrypt");
4152 if (val && strcasecmp(val, "AES-GCMP") == 0) {
4153 if (set_network(ifname, net_id, "pairwise", "GCMP") < 0) {
4154 sigma_dut_print(dut, DUT_MSG_ERROR,
4155 "Failed to set supplicant to pairwise GCMP");
4156 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
4157 }
4158 if (set_network(ifname, net_id, "group", "GCMP") < 0) {
4159 sigma_dut_print(dut, DUT_MSG_ERROR,
4160 "Failed to set supplicant to group GCMP");
4161 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
4162 }
4163 } else if (val) {
4164 sigma_dut_print(dut, DUT_MSG_ERROR,
4165 "Requested Encrypt %s is not supported on 60 GHz",
4166 val);
4167 return SIGMA_DUT_INVALID_CALLER_SEND_STATUS;
4168 }
4169
4170 val = get_param(cmd, "PSK");
4171 if (val && set_network_quoted(ifname, net_id, "psk", val) < 0) {
4172 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to set psk %s",
4173 val);
4174 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
4175 }
4176
4177 /* Convert 60G channel to freq */
4178 switch (dut->ap_channel) {
4179 case 1:
4180 val = "58320";
4181 break;
4182 case 2:
4183 val = "60480";
4184 break;
4185 case 3:
4186 val = "62640";
4187 break;
4188 default:
4189 sigma_dut_print(dut, DUT_MSG_ERROR,
4190 "Failed to configure channel %d. Not supported",
4191 dut->ap_channel);
4192 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
4193 }
4194
4195 if (set_network(ifname, net_id, "frequency", val) < 0) {
4196 sigma_dut_print(dut, DUT_MSG_ERROR,
4197 "Failed to set supplicant network frequency");
4198 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
4199 }
4200
4201 sigma_dut_print(dut, DUT_MSG_DEBUG,
4202 "Supplicant set network with frequency");
4203
4204 snprintf(buf, sizeof(buf), "SELECT_NETWORK %d", net_id);
4205 if (wpa_command(ifname, buf) < 0) {
4206 sigma_dut_print(dut, DUT_MSG_INFO,
4207 "Failed to select network id %d on %s",
4208 net_id, ifname);
4209 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
4210 }
4211
4212 sigma_dut_print(dut, DUT_MSG_DEBUG, "Selected network");
4213
4214 return SIGMA_DUT_SUCCESS_CALLER_SEND_STATUS;
4215}
4216
4217
Lior David67543f52017-01-03 19:04:22 +02004218static int wil6210_set_abft_len(struct sigma_dut *dut, int abft_len)
4219{
4220 char buf[128], fname[128];
4221 FILE *f;
4222
4223 if (wil6210_get_debugfs_dir(dut, buf, sizeof(buf))) {
4224 sigma_dut_print(dut, DUT_MSG_ERROR,
4225 "failed to get wil6210 debugfs dir");
4226 return -1;
4227 }
4228
4229 snprintf(fname, sizeof(fname), "%s/abft_len", buf);
4230 f = fopen(fname, "w");
4231 if (!f) {
4232 sigma_dut_print(dut, DUT_MSG_ERROR,
4233 "failed to open: %s", fname);
4234 return -1;
4235 }
4236
4237 fprintf(f, "%d\n", abft_len);
4238 fclose(f);
4239
4240 return 0;
4241}
4242
4243
4244static int sta_set_60g_abft_len(struct sigma_dut *dut, struct sigma_conn *conn,
4245 int abft_len)
4246{
4247 switch (get_driver_type()) {
4248 case DRIVER_WIL6210:
4249 return wil6210_set_abft_len(dut, abft_len);
4250 default:
4251 sigma_dut_print(dut, DUT_MSG_ERROR,
4252 "set abft_len not supported");
4253 return -1;
4254 }
4255}
4256
4257
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004258static int sta_set_60g_pcp(struct sigma_dut *dut, struct sigma_conn *conn,
4259 struct sigma_cmd *cmd)
4260{
4261 const char *val;
Lior David67543f52017-01-03 19:04:22 +02004262 unsigned int abft_len = 1; /* default is one slot */
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004263
4264 if (dut->dev_role != DEVROLE_PCP) {
4265 send_resp(dut, conn, SIGMA_INVALID,
4266 "ErrorCode,Invalid DevRole");
4267 return 0;
4268 }
4269
4270 val = get_param(cmd, "SSID");
4271 if (val) {
4272 if (strlen(val) > sizeof(dut->ap_ssid) - 1) {
4273 send_resp(dut, conn, SIGMA_INVALID,
4274 "ErrorCode,Invalid SSID");
4275 return -1;
4276 }
4277
Peng Xub8fc5cc2017-05-10 17:27:28 -07004278 strlcpy(dut->ap_ssid, val, sizeof(dut->ap_ssid));
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004279 }
4280
4281 val = get_param(cmd, "CHANNEL");
4282 if (val) {
4283 const char *pos;
4284
4285 dut->ap_channel = atoi(val);
4286 pos = strchr(val, ';');
4287 if (pos) {
4288 pos++;
4289 dut->ap_channel_1 = atoi(pos);
4290 }
4291 }
4292
4293 switch (dut->ap_channel) {
4294 case 1:
4295 case 2:
4296 case 3:
4297 break;
4298 default:
4299 sigma_dut_print(dut, DUT_MSG_ERROR,
4300 "Channel %d is not supported", dut->ap_channel);
4301 send_resp(dut, conn, SIGMA_ERROR,
4302 "Requested channel is not supported");
4303 return -1;
4304 }
4305
4306 val = get_param(cmd, "BCNINT");
4307 if (val)
4308 dut->ap_bcnint = atoi(val);
4309
4310
4311 val = get_param(cmd, "ExtSchIE");
4312 if (val) {
4313 send_resp(dut, conn, SIGMA_ERROR,
4314 "ErrorCode,ExtSchIE is not supported yet");
4315 return -1;
4316 }
4317
4318 val = get_param(cmd, "AllocType");
4319 if (val) {
4320 send_resp(dut, conn, SIGMA_ERROR,
4321 "ErrorCode,AllocType is not supported yet");
4322 return -1;
4323 }
4324
4325 val = get_param(cmd, "PercentBI");
4326 if (val) {
4327 send_resp(dut, conn, SIGMA_ERROR,
4328 "ErrorCode,PercentBI is not supported yet");
4329 return -1;
4330 }
4331
4332 val = get_param(cmd, "CBAPOnly");
4333 if (val) {
4334 send_resp(dut, conn, SIGMA_ERROR,
4335 "ErrorCode,CBAPOnly is not supported yet");
4336 return -1;
4337 }
4338
4339 val = get_param(cmd, "AMPDU");
4340 if (val) {
4341 if (strcasecmp(val, "Enable") == 0)
4342 dut->ap_ampdu = 1;
4343 else if (strcasecmp(val, "Disable") == 0)
4344 dut->ap_ampdu = 2;
4345 else {
4346 send_resp(dut, conn, SIGMA_ERROR,
4347 "ErrorCode,AMPDU value is not Enable nor Disabled");
4348 return -1;
4349 }
4350 }
4351
4352 val = get_param(cmd, "AMSDU");
4353 if (val) {
4354 if (strcasecmp(val, "Enable") == 0)
4355 dut->ap_amsdu = 1;
4356 else if (strcasecmp(val, "Disable") == 0)
4357 dut->ap_amsdu = 2;
4358 }
4359
4360 val = get_param(cmd, "NumMSDU");
4361 if (val) {
4362 send_resp(dut, conn, SIGMA_ERROR,
4363 "ErrorCode, NumMSDU is not supported yet");
4364 return -1;
4365 }
4366
4367 val = get_param(cmd, "ABFTLRang");
4368 if (val) {
4369 sigma_dut_print(dut, DUT_MSG_DEBUG,
Lior David67543f52017-01-03 19:04:22 +02004370 "ABFTLRang parameter %s", val);
4371 if (strcmp(val, "Gt1") == 0)
4372 abft_len = 2; /* 2 slots in this case */
4373 }
4374
4375 if (sta_set_60g_abft_len(dut, conn, abft_len)) {
4376 send_resp(dut, conn, SIGMA_ERROR,
4377 "ErrorCode, Can't set ABFT length");
4378 return -1;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004379 }
4380
4381 if (sta_pcp_start(dut, conn, cmd) < 0) {
4382 send_resp(dut, conn, SIGMA_ERROR,
4383 "ErrorCode, Can't start PCP role");
4384 return -1;
4385 }
4386
4387 return sta_set_60g_common(dut, conn, cmd);
4388}
4389
4390
4391static int sta_set_60g_sta(struct sigma_dut *dut, struct sigma_conn *conn,
4392 struct sigma_cmd *cmd)
4393{
4394 const char *val = get_param(cmd, "DiscoveryMode");
4395
4396 if (dut->dev_role != DEVROLE_STA) {
4397 send_resp(dut, conn, SIGMA_INVALID,
4398 "ErrorCode,Invalid DevRole");
4399 return 0;
4400 }
4401
4402 if (val) {
4403 sigma_dut_print(dut, DUT_MSG_DEBUG, "Discovery: %s", val);
4404 /* Ignore Discovery mode till Driver expose API. */
4405#if 0
4406 if (strcasecmp(val, "1") == 0) {
4407 send_resp(dut, conn, SIGMA_INVALID,
4408 "ErrorCode,DiscoveryMode 1 not supported");
4409 return 0;
4410 }
4411
4412 if (strcasecmp(val, "0") == 0) {
4413 /* OK */
4414 } else {
4415 send_resp(dut, conn, SIGMA_INVALID,
4416 "ErrorCode,DiscoveryMode not supported");
4417 return 0;
4418 }
4419#endif
4420 }
4421
4422 if (start_sta_mode(dut) != 0)
4423 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
4424 return sta_set_60g_common(dut, conn, cmd);
4425}
4426
4427
4428static int cmd_sta_disconnect(struct sigma_dut *dut, struct sigma_conn *conn,
4429 struct sigma_cmd *cmd)
4430{
4431 const char *intf = get_param(cmd, "Interface");
4432 disconnect_station(dut);
4433 /* Try to ignore old scan results to avoid HS 2.0R2 test case failures
4434 * due to cached results. */
4435 wpa_command(intf, "SET ignore_old_scan_res 1");
4436 wpa_command(intf, "BSS_FLUSH");
4437 return 1;
4438}
4439
4440
4441static int cmd_sta_reassoc(struct sigma_dut *dut, struct sigma_conn *conn,
4442 struct sigma_cmd *cmd)
4443{
4444 const char *intf = get_param(cmd, "Interface");
4445 const char *bssid = get_param(cmd, "bssid");
4446 const char *val = get_param(cmd, "CHANNEL");
4447 struct wpa_ctrl *ctrl;
4448 char buf[100];
4449 int res;
4450 int chan = 0;
Ashwini Patil467efef2017-05-25 12:18:27 +05304451 int status = 0;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004452
4453 if (bssid == NULL) {
4454 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Missing bssid "
4455 "argument");
4456 return 0;
4457 }
4458
4459 if (val)
4460 chan = atoi(val);
4461
4462 if (wifi_chip_type != DRIVER_WCN && wifi_chip_type != DRIVER_AR6003) {
4463 /* The current network may be from sta_associate or
4464 * sta_hs2_associate
4465 */
4466 if (set_network(intf, dut->infra_network_id, "bssid", bssid) <
4467 0 ||
4468 set_network(intf, 0, "bssid", bssid) < 0)
4469 return -2;
4470 }
4471
4472 ctrl = open_wpa_mon(intf);
4473 if (ctrl == NULL) {
4474 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to open "
4475 "wpa_supplicant monitor connection");
4476 return -1;
4477 }
4478
4479 if (wifi_chip_type == DRIVER_WCN) {
4480#ifdef ANDROID
Ashwini Patil4c8158f2017-05-25 12:49:21 +05304481 if (chan) {
4482 unsigned int freq;
4483
4484 freq = channel_to_freq(chan);
4485 if (!freq) {
4486 sigma_dut_print(dut, DUT_MSG_ERROR,
4487 "Invalid channel number provided: %d",
4488 chan);
4489 send_resp(dut, conn, SIGMA_INVALID,
4490 "ErrorCode,Invalid channel number");
4491 goto close_mon_conn;
4492 }
4493 res = snprintf(buf, sizeof(buf),
4494 "SCAN TYPE=ONLY freq=%d", freq);
4495 } else {
4496 res = snprintf(buf, sizeof(buf), "SCAN TYPE=ONLY");
4497 }
4498 if (res < 0 || res >= (int) sizeof(buf)) {
4499 send_resp(dut, conn, SIGMA_ERROR,
4500 "ErrorCode,snprintf failed");
4501 goto close_mon_conn;
4502 }
4503 if (wpa_command(intf, buf) < 0) {
4504 sigma_dut_print(dut, DUT_MSG_INFO,
4505 "Failed to start scan");
4506 send_resp(dut, conn, SIGMA_ERROR,
4507 "ErrorCode,scan failed");
4508 goto close_mon_conn;
4509 }
4510
4511 res = get_wpa_cli_event(dut, ctrl, "CTRL-EVENT-SCAN-RESULTS",
4512 buf, sizeof(buf));
4513 if (res < 0) {
4514 sigma_dut_print(dut, DUT_MSG_INFO,
4515 "Scan did not complete");
4516 send_resp(dut, conn, SIGMA_ERROR,
4517 "ErrorCode,scan did not complete");
4518 goto close_mon_conn;
4519 }
4520
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004521 if (set_network(intf, dut->infra_network_id, "bssid", "any")
4522 < 0) {
4523 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to set "
4524 "bssid to any during FASTREASSOC");
Ashwini Patil467efef2017-05-25 12:18:27 +05304525 status = -2;
4526 goto close_mon_conn;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004527 }
4528 res = snprintf(buf, sizeof(buf), "DRIVER FASTREASSOC %s %d",
4529 bssid, chan);
4530 if (res > 0 && res < (int) sizeof(buf))
4531 res = wpa_command(intf, buf);
4532
4533 if (res < 0 || res >= (int) sizeof(buf)) {
4534 send_resp(dut, conn, SIGMA_ERROR,
4535 "errorCode,Failed to run DRIVER FASTREASSOC");
Ashwini Patil467efef2017-05-25 12:18:27 +05304536 goto close_mon_conn;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004537 }
4538#else /* ANDROID */
4539 sigma_dut_print(dut, DUT_MSG_DEBUG,
4540 "Reassoc using iwpriv - skip chan=%d info",
4541 chan);
4542 snprintf(buf, sizeof(buf), "iwpriv %s reassoc", intf);
4543 if (system(buf) != 0) {
4544 sigma_dut_print(dut, DUT_MSG_ERROR, "%s failed", buf);
Ashwini Patil467efef2017-05-25 12:18:27 +05304545 status = -2;
4546 goto close_mon_conn;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004547 }
4548#endif /* ANDROID */
4549 sigma_dut_print(dut, DUT_MSG_INFO,
4550 "sta_reassoc: Run %s successful", buf);
4551 } else if (wpa_command(intf, "REASSOCIATE")) {
4552 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to "
4553 "request reassociation");
Ashwini Patil467efef2017-05-25 12:18:27 +05304554 goto close_mon_conn;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004555 }
4556
4557 res = get_wpa_cli_event(dut, ctrl, "CTRL-EVENT-CONNECTED",
4558 buf, sizeof(buf));
Ashwini Patil467efef2017-05-25 12:18:27 +05304559 if (res < 0) {
4560 sigma_dut_print(dut, DUT_MSG_INFO, "Connection did not complete");
4561 status = -1;
4562 goto close_mon_conn;
4563 }
4564 status = 1;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004565
Ashwini Patil467efef2017-05-25 12:18:27 +05304566close_mon_conn:
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004567 wpa_ctrl_detach(ctrl);
4568 wpa_ctrl_close(ctrl);
Ashwini Patil467efef2017-05-25 12:18:27 +05304569 return status;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004570}
4571
4572
4573static void hs2_clear_credentials(const char *intf)
4574{
4575 wpa_command(intf, "REMOVE_CRED all");
4576}
4577
4578
Lior Davidcc88b562017-01-03 18:52:09 +02004579#ifdef __linux__
4580static int wil6210_get_aid(struct sigma_dut *dut, const char *bssid,
4581 unsigned int *aid)
4582{
Lior David0fe101e2017-03-09 16:09:50 +02004583 const char *pattern = "AID[ \t]+([0-9]+)";
Lior Davidcc88b562017-01-03 18:52:09 +02004584
Lior David0fe101e2017-03-09 16:09:50 +02004585 return wil6210_get_sta_info_field(dut, bssid, pattern, aid);
Lior Davidcc88b562017-01-03 18:52:09 +02004586}
4587#endif /* __linux__ */
4588
4589
4590static int sta_get_aid_60g(struct sigma_dut *dut, const char *bssid,
4591 unsigned int *aid)
4592{
4593 switch (get_driver_type()) {
4594#ifdef __linux__
4595 case DRIVER_WIL6210:
4596 return wil6210_get_aid(dut, bssid, aid);
4597#endif /* __linux__ */
4598 default:
4599 sigma_dut_print(dut, DUT_MSG_ERROR, "get AID not supported");
4600 return -1;
4601 }
4602}
4603
4604
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004605static int sta_get_parameter_60g(struct sigma_dut *dut, struct sigma_conn *conn,
4606 struct sigma_cmd *cmd)
4607{
4608 char buf[MAX_CMD_LEN];
4609 char bss_list[MAX_CMD_LEN];
4610 const char *parameter = get_param(cmd, "Parameter");
4611
4612 if (parameter == NULL)
4613 return -1;
4614
Lior Davidcc88b562017-01-03 18:52:09 +02004615 if (strcasecmp(parameter, "AID") == 0) {
4616 unsigned int aid = 0;
4617 char bssid[20];
4618
4619 if (get_wpa_status(get_station_ifname(), "bssid",
4620 bssid, sizeof(bssid)) < 0) {
4621 sigma_dut_print(dut, DUT_MSG_ERROR,
4622 "could not get bssid");
4623 return -2;
4624 }
4625
4626 if (sta_get_aid_60g(dut, bssid, &aid))
4627 return -2;
4628
4629 snprintf(buf, sizeof(buf), "aid,%d", aid);
4630 sigma_dut_print(dut, DUT_MSG_INFO, "%s", buf);
4631 send_resp(dut, conn, SIGMA_COMPLETE, buf);
4632 return 0;
4633 }
4634
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004635 if (strcasecmp(parameter, "DiscoveredDevList") == 0) {
4636 char *bss_line;
4637 char *bss_id = NULL;
4638 const char *ifname = get_param(cmd, "Interface");
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05304639 char *saveptr;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004640
4641 if (ifname == NULL) {
4642 sigma_dut_print(dut, DUT_MSG_INFO,
4643 "For get DiscoveredDevList need Interface name.");
4644 return -1;
4645 }
4646
4647 /*
4648 * Use "BSS RANGE=ALL MASK=0x2" which provides a list
4649 * of BSSIDs in "bssid=<BSSID>\n"
4650 */
4651 if (wpa_command_resp(ifname, "BSS RANGE=ALL MASK=0x2",
4652 bss_list,
4653 sizeof(bss_list)) < 0) {
4654 sigma_dut_print(dut, DUT_MSG_ERROR,
4655 "Failed to get bss list");
4656 return -1;
4657 }
4658
4659 sigma_dut_print(dut, DUT_MSG_DEBUG,
4660 "bss list for ifname:%s is:%s",
4661 ifname, bss_list);
4662
4663 snprintf(buf, sizeof(buf), "DeviceList");
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05304664 bss_line = strtok_r(bss_list, "\n", &saveptr);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004665 while (bss_line) {
4666 if (sscanf(bss_line, "bssid=%ms", &bss_id) > 0 &&
4667 bss_id) {
4668 int len;
4669
4670 len = snprintf(buf + strlen(buf),
4671 sizeof(buf) - strlen(buf),
4672 ",%s", bss_id);
4673 free(bss_id);
4674 bss_id = NULL;
4675 if (len < 0) {
4676 sigma_dut_print(dut,
4677 DUT_MSG_ERROR,
4678 "Failed to read BSSID");
4679 send_resp(dut, conn, SIGMA_ERROR,
4680 "ErrorCode,Failed to read BSS ID");
4681 return 0;
4682 }
4683
4684 if ((size_t) len >= sizeof(buf) - strlen(buf)) {
4685 sigma_dut_print(dut,
4686 DUT_MSG_ERROR,
4687 "Response buf too small for list");
4688 send_resp(dut, conn,
4689 SIGMA_ERROR,
4690 "ErrorCode,Response buf too small for list");
4691 return 0;
4692 }
4693 }
4694
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05304695 bss_line = strtok_r(NULL, "\n", &saveptr);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004696 }
4697
4698 sigma_dut_print(dut, DUT_MSG_INFO, "DiscoveredDevList is %s",
4699 buf);
4700 send_resp(dut, conn, SIGMA_COMPLETE, buf);
4701 return 0;
4702 }
4703
4704 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Unsupported parameter");
4705 return 0;
4706}
4707
4708
4709static int cmd_sta_get_parameter(struct sigma_dut *dut, struct sigma_conn *conn,
4710 struct sigma_cmd *cmd)
4711{
4712 const char *program = get_param(cmd, "Program");
4713
4714 if (program == NULL)
4715 return -1;
4716
4717 if (strcasecmp(program, "P2PNFC") == 0)
4718 return p2p_cmd_sta_get_parameter(dut, conn, cmd);
4719
4720 if (strcasecmp(program, "60ghz") == 0)
4721 return sta_get_parameter_60g(dut, conn, cmd);
4722
4723#ifdef ANDROID_NAN
4724 if (strcasecmp(program, "NAN") == 0)
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -07004725 return nan_cmd_sta_get_parameter(dut, conn, cmd);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004726#endif /* ANDROID_NAN */
4727
Amarnath Hullur Subramanyam9c381f52017-03-17 00:04:41 -07004728#ifdef MIRACAST
4729 if (strcasecmp(program, "WFD") == 0 ||
4730 strcasecmp(program, "DisplayR2") == 0)
4731 return miracast_cmd_sta_get_parameter(dut, conn, cmd);
4732#endif /* MIRACAST */
4733
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004734 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Unsupported parameter");
4735 return 0;
4736}
4737
4738
4739static void sta_reset_default_ath(struct sigma_dut *dut, const char *intf,
4740 const char *type)
4741{
4742 char buf[100];
4743
4744 if (dut->program == PROGRAM_VHT) {
4745 snprintf(buf, sizeof(buf), "iwpriv %s chwidth 2", intf);
4746 if (system(buf) != 0) {
4747 sigma_dut_print(dut, DUT_MSG_ERROR,
4748 "iwpriv %s chwidth failed", intf);
4749 }
4750
4751 snprintf(buf, sizeof(buf), "iwpriv %s mode 11ACVHT80", intf);
4752 if (system(buf) != 0) {
4753 sigma_dut_print(dut, DUT_MSG_ERROR,
4754 "iwpriv %s mode 11ACVHT80 failed",
4755 intf);
4756 }
4757
4758 snprintf(buf, sizeof(buf), "iwpriv %s vhtmcs -1", intf);
4759 if (system(buf) != 0) {
4760 sigma_dut_print(dut, DUT_MSG_ERROR,
4761 "iwpriv %s vhtmcs -1 failed", intf);
4762 }
4763 }
4764
4765 if (dut->program == PROGRAM_HT) {
4766 snprintf(buf, sizeof(buf), "iwpriv %s chwidth 0", intf);
4767 if (system(buf) != 0) {
4768 sigma_dut_print(dut, DUT_MSG_ERROR,
4769 "iwpriv %s chwidth failed", intf);
4770 }
4771
4772 snprintf(buf, sizeof(buf), "iwpriv %s mode 11naht40", intf);
4773 if (system(buf) != 0) {
4774 sigma_dut_print(dut, DUT_MSG_ERROR,
4775 "iwpriv %s mode 11naht40 failed",
4776 intf);
4777 }
4778
4779 snprintf(buf, sizeof(buf), "iwpriv %s set11NRates 0", intf);
4780 if (system(buf) != 0) {
4781 sigma_dut_print(dut, DUT_MSG_ERROR,
4782 "iwpriv set11NRates failed");
4783 }
4784 }
4785
4786 if (dut->program == PROGRAM_VHT || dut->program == PROGRAM_HT) {
4787 snprintf(buf, sizeof(buf), "iwpriv %s powersave 0", intf);
4788 if (system(buf) != 0) {
4789 sigma_dut_print(dut, DUT_MSG_ERROR,
4790 "disabling powersave failed");
4791 }
4792
4793 /* Reset CTS width */
4794 snprintf(buf, sizeof(buf), "wifitool %s beeliner_fw_test 54 0",
4795 intf);
4796 if (system(buf) != 0) {
4797 sigma_dut_print(dut, DUT_MSG_ERROR,
4798 "wifitool %s beeliner_fw_test 54 0 failed",
4799 intf);
4800 }
4801
4802 /* Enable Dynamic Bandwidth signalling by default */
4803 snprintf(buf, sizeof(buf), "iwpriv %s cwmenable 1", intf);
4804 if (system(buf) != 0) {
4805 sigma_dut_print(dut, DUT_MSG_ERROR,
4806 "iwpriv %s cwmenable 1 failed", intf);
4807 }
4808
4809 snprintf(buf, sizeof(buf), "iwconfig %s rts 2347", intf);
4810 if (system(buf) != 0) {
4811 sigma_dut_print(dut, DUT_MSG_ERROR,
4812 "iwpriv rts failed");
4813 }
4814 }
4815
4816 if (type && strcasecmp(type, "Testbed") == 0) {
4817 dut->testbed_flag_txsp = 1;
4818 dut->testbed_flag_rxsp = 1;
4819 /* STA has to set spatial stream to 2 per Appendix H */
4820 snprintf(buf, sizeof(buf), "iwpriv %s vht_mcsmap 0xfff0", intf);
4821 if (system(buf) != 0) {
4822 sigma_dut_print(dut, DUT_MSG_ERROR,
4823 "iwpriv vht_mcsmap failed");
4824 }
4825
4826 /* Disable LDPC per Appendix H */
4827 snprintf(buf, sizeof(buf), "iwpriv %s ldpc 0", intf);
4828 if (system(buf) != 0) {
4829 sigma_dut_print(dut, DUT_MSG_ERROR,
4830 "iwpriv %s ldpc 0 failed", intf);
4831 }
4832
4833 snprintf(buf, sizeof(buf), "iwpriv %s amsdu 1", intf);
4834 if (system(buf) != 0) {
4835 sigma_dut_print(dut, DUT_MSG_ERROR,
4836 "iwpriv amsdu failed");
4837 }
4838
4839 /* TODO: Disable STBC 2x1 transmit and receive */
4840 snprintf(buf, sizeof(buf), "iwpriv %s tx_stbc 0", intf);
4841 if (system(buf) != 0) {
4842 sigma_dut_print(dut, DUT_MSG_ERROR,
4843 "Disable tx_stbc 0 failed");
4844 }
4845
4846 snprintf(buf, sizeof(buf), "iwpriv %s rx_stbc 0", intf);
4847 if (system(buf) != 0) {
4848 sigma_dut_print(dut, DUT_MSG_ERROR,
4849 "Disable rx_stbc 0 failed");
4850 }
4851
4852 /* STA has to disable Short GI per Appendix H */
4853 snprintf(buf, sizeof(buf), "iwpriv %s shortgi 0", intf);
4854 if (system(buf) != 0) {
4855 sigma_dut_print(dut, DUT_MSG_ERROR,
4856 "iwpriv %s shortgi 0 failed", intf);
4857 }
4858 }
4859
4860 if (type && strcasecmp(type, "DUT") == 0) {
4861 snprintf(buf, sizeof(buf), "iwpriv %s nss 3", intf);
4862 if (system(buf) != 0) {
4863 sigma_dut_print(dut, DUT_MSG_ERROR,
4864 "iwpriv %s nss 3 failed", intf);
4865 }
4866
4867 snprintf(buf, sizeof(buf), "iwpriv %s shortgi 1", intf);
4868 if (system(buf) != 0) {
4869 sigma_dut_print(dut, DUT_MSG_ERROR,
4870 "iwpriv %s shortgi 1 failed", intf);
4871 }
4872 }
4873}
4874
4875
4876static int cmd_sta_reset_default(struct sigma_dut *dut,
4877 struct sigma_conn *conn,
4878 struct sigma_cmd *cmd)
4879{
4880 int cmd_sta_p2p_reset(struct sigma_dut *dut, struct sigma_conn *conn,
4881 struct sigma_cmd *cmd);
4882 const char *intf = get_param(cmd, "Interface");
4883 const char *type;
Amarnath Hullur Subramanyam9c381f52017-03-17 00:04:41 -07004884 const char *program = get_param(cmd, "program");
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004885
Amarnath Hullur Subramanyam9c381f52017-03-17 00:04:41 -07004886 if (!program)
4887 program = get_param(cmd, "prog");
4888 dut->program = sigma_program_to_enum(program);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004889 dut->device_type = STA_unknown;
4890 type = get_param(cmd, "type");
4891 if (type && strcasecmp(type, "Testbed") == 0)
4892 dut->device_type = STA_testbed;
4893 if (type && strcasecmp(type, "DUT") == 0)
4894 dut->device_type = STA_dut;
4895
4896 if (dut->program == PROGRAM_TDLS) {
4897 /* Clear TDLS testing mode */
4898 wpa_command(intf, "SET tdls_disabled 0");
4899 wpa_command(intf, "SET tdls_testing 0");
4900 dut->no_tpk_expiration = 0;
Pradeep Reddy POTTETI8ce2a232016-10-28 12:17:32 +05304901 if (get_driver_type() == DRIVER_WCN) {
4902 /* Enable the WCN driver in TDLS Explicit trigger mode
4903 */
4904 wpa_command(intf, "SET tdls_external_control 0");
4905 wpa_command(intf, "SET tdls_trigger_control 0");
4906 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004907 }
4908
Amarnath Hullur Subramanyam9c381f52017-03-17 00:04:41 -07004909#ifdef MIRACAST
4910 if (dut->program == PROGRAM_WFD ||
4911 dut->program == PROGRAM_DISPLAYR2)
4912 miracast_sta_reset_default(dut, conn, cmd);
4913#endif /* MIRACAST */
4914
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004915 switch (get_driver_type()) {
4916 case DRIVER_ATHEROS:
4917 sta_reset_default_ath(dut, intf, type);
4918 break;
4919 default:
4920 break;
4921 }
4922
4923#ifdef ANDROID_NAN
4924 if (dut->program == PROGRAM_NAN)
4925 nan_cmd_sta_reset_default(dut, conn, cmd);
4926#endif /* ANDROID_NAN */
4927
4928 if (dut->program == PROGRAM_HS2_R2) {
4929 unlink("SP/wi-fi.org/pps.xml");
4930 if (system("rm -r SP/*") != 0) {
4931 }
4932 unlink("next-client-cert.pem");
4933 unlink("next-client-key.pem");
4934 }
4935
4936 if (dut->program == PROGRAM_60GHZ) {
4937 const char *dev_role = get_param(cmd, "DevRole");
4938
4939 if (!dev_role) {
4940 send_resp(dut, conn, SIGMA_ERROR,
4941 "errorCode,Missing DevRole argument");
4942 return 0;
4943 }
4944
4945 if (strcasecmp(dev_role, "STA") == 0)
4946 dut->dev_role = DEVROLE_STA;
4947 else if (strcasecmp(dev_role, "PCP") == 0)
4948 dut->dev_role = DEVROLE_PCP;
4949 else {
4950 send_resp(dut, conn, SIGMA_ERROR,
4951 "errorCode,Unknown DevRole");
4952 return 0;
4953 }
4954
4955 if (dut->device_type == STA_unknown) {
4956 sigma_dut_print(dut, DUT_MSG_ERROR,
4957 "Device type is not STA testbed or DUT");
4958 send_resp(dut, conn, SIGMA_ERROR,
4959 "errorCode,Unknown device type");
4960 return 0;
4961 }
4962 }
4963
4964 wpa_command(intf, "WPS_ER_STOP");
4965 wpa_command(intf, "FLUSH");
4966 wpa_command(intf, "SET radio_disabled 0");
4967
4968 if (dut->tmp_mac_addr && dut->set_macaddr) {
4969 dut->tmp_mac_addr = 0;
4970 if (system(dut->set_macaddr) != 0) {
4971 sigma_dut_print(dut, DUT_MSG_INFO, "Failed to clear "
4972 "temporary MAC address");
4973 }
4974 }
4975
4976 set_ps(intf, dut, 0);
4977
4978 if (dut->program == PROGRAM_HS2 || dut->program == PROGRAM_HS2_R2) {
4979 wpa_command(intf, "SET interworking 1");
4980 wpa_command(intf, "SET hs20 1");
4981 }
4982
4983 if (dut->program == PROGRAM_HS2_R2) {
4984 wpa_command(intf, "SET pmf 1");
4985 } else {
4986 wpa_command(intf, "SET pmf 0");
4987 }
4988
4989 hs2_clear_credentials(intf);
4990 wpa_command(intf, "SET hessid 00:00:00:00:00:00");
4991 wpa_command(intf, "SET access_network_type 15");
4992
4993 static_ip_file(0, NULL, NULL, NULL);
4994 kill_dhcp_client(dut, intf);
4995 clear_ip_addr(dut, intf);
4996
4997 dut->er_oper_performed = 0;
4998 dut->er_oper_bssid[0] = '\0';
4999
priyadharshini gowthamanad6cbba2016-10-04 10:39:58 -07005000 if (dut->program == PROGRAM_LOC) {
5001 /* Disable Interworking by default */
5002 wpa_command(get_station_ifname(), "SET interworking 0");
5003 }
5004
Ashwini Patil00402582017-04-13 12:29:39 +05305005 if (dut->program == PROGRAM_MBO) {
5006 free(dut->non_pref_ch_list);
5007 dut->non_pref_ch_list = NULL;
Ashwini Patil5acd7382017-04-13 15:55:04 +05305008 free(dut->btm_query_cand_list);
5009 dut->btm_query_cand_list = NULL;
Ashwini Patilc63161e2017-04-13 16:30:23 +05305010 wpa_command(intf, "SET reject_btm_req_reason 0");
Ashwini Patila75de5a2017-04-13 16:35:05 +05305011 wpa_command(intf, "SET ignore_assoc_disallow 0");
Ashwini Patild174f2c2017-04-13 16:49:46 +05305012 wpa_command(intf, "SET gas_address3 0");
Ashwini Patil9183fdb2017-04-13 16:58:25 +05305013 wpa_command(intf, "SET roaming 1");
Ashwini Patil00402582017-04-13 12:29:39 +05305014 }
5015
Jouni Malinen3c367e82017-06-23 17:01:47 +03005016 free(dut->rsne_override);
5017 dut->rsne_override = NULL;
5018
Jouni Malinen68143132017-09-02 02:34:08 +03005019 free(dut->sae_commit_override);
5020 dut->sae_commit_override = NULL;
5021
Jouni Malinend86e5822017-08-29 03:55:32 +03005022 dut->dpp_conf_id = -1;
5023
Priyadharshini Gowthamana7dfd492015-11-09 14:34:08 -08005024 if (dut->program != PROGRAM_VHT)
5025 return cmd_sta_p2p_reset(dut, conn, cmd);
5026 return 1;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005027}
5028
5029
5030static int cmd_sta_get_events(struct sigma_dut *dut, struct sigma_conn *conn,
5031 struct sigma_cmd *cmd)
5032{
5033 const char *program = get_param(cmd, "Program");
5034
5035 if (program == NULL)
5036 return -1;
5037#ifdef ANDROID_NAN
5038 if (strcasecmp(program, "NAN") == 0)
5039 return nan_cmd_sta_get_events(dut, conn, cmd);
5040#endif /* ANDROID_NAN */
5041 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Unsupported parameter");
5042 return 0;
5043}
5044
5045
5046static int cmd_sta_exec_action(struct sigma_dut *dut, struct sigma_conn *conn,
5047 struct sigma_cmd *cmd)
5048{
5049 const char *program = get_param(cmd, "Prog");
5050
5051 if (program == NULL)
5052 return -1;
5053#ifdef ANDROID_NAN
5054 if (strcasecmp(program, "NAN") == 0)
5055 return nan_cmd_sta_exec_action(dut, conn, cmd);
5056#endif /* ANDROID_NAN */
priyadharshini gowthamand66913a2016-07-29 15:11:17 -07005057 if (strcasecmp(program, "Loc") == 0)
5058 return loc_cmd_sta_exec_action(dut, conn, cmd);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005059 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Unsupported parameter");
5060 return 0;
5061}
5062
5063
5064static int cmd_sta_set_11n(struct sigma_dut *dut, struct sigma_conn *conn,
5065 struct sigma_cmd *cmd)
5066{
5067 const char *intf = get_param(cmd, "Interface");
5068 const char *val, *mcs32, *rate;
5069
5070 val = get_param(cmd, "GREENFIELD");
5071 if (val) {
5072 if (strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0) {
5073 /* Enable GD */
5074 send_resp(dut, conn, SIGMA_ERROR,
5075 "ErrorCode,GF not supported");
5076 return 0;
5077 }
5078 }
5079
5080 val = get_param(cmd, "SGI20");
5081 if (val) {
5082 switch (get_driver_type()) {
5083 case DRIVER_ATHEROS:
5084 ath_sta_set_sgi(dut, intf, val);
5085 break;
5086 default:
5087 send_resp(dut, conn, SIGMA_ERROR,
5088 "ErrorCode,SGI20 not supported");
5089 return 0;
5090 }
5091 }
5092
5093 mcs32 = get_param(cmd, "MCS32"); /* HT Duplicate Mode Enable/Disable */
5094 rate = get_param(cmd, "MCS_FIXEDRATE"); /* Fixed MCS rate (0..31) */
5095 if (mcs32 && rate) {
5096 /* TODO */
5097 send_resp(dut, conn, SIGMA_ERROR,
5098 "ErrorCode,MCS32,MCS_FIXEDRATE not supported");
5099 return 0;
5100 } else if (mcs32 && !rate) {
5101 /* TODO */
5102 send_resp(dut, conn, SIGMA_ERROR,
5103 "ErrorCode,MCS32 not supported");
5104 return 0;
5105 } else if (!mcs32 && rate) {
5106 switch (get_driver_type()) {
5107 case DRIVER_ATHEROS:
priyadharshini gowthamane5e25172015-12-08 14:53:48 -08005108 novap_reset(dut, intf);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005109 ath_sta_set_11nrates(dut, intf, rate);
5110 break;
5111 default:
5112 send_resp(dut, conn, SIGMA_ERROR,
5113 "ErrorCode,MCS32_FIXEDRATE not supported");
5114 return 0;
5115 }
5116 }
5117
5118 return cmd_sta_set_wireless_common(intf, dut, conn, cmd);
5119}
5120
5121
5122static int cmd_sta_set_wireless_vht(struct sigma_dut *dut,
5123 struct sigma_conn *conn,
5124 struct sigma_cmd *cmd)
5125{
5126 const char *intf = get_param(cmd, "Interface");
5127 const char *val;
5128 char buf[30];
5129 int tkip = -1;
5130 int wep = -1;
5131
5132 val = get_param(cmd, "SGI80");
5133 if (val) {
5134 int sgi80;
5135
5136 sgi80 = strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0;
5137 snprintf(buf, sizeof(buf), "iwpriv %s shortgi %d", intf, sgi80);
5138 if (system(buf) != 0) {
5139 sigma_dut_print(dut, DUT_MSG_ERROR,
5140 "iwpriv shortgi failed");
5141 }
5142 }
5143
5144 val = get_param(cmd, "TxBF");
5145 if (val && (strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0)) {
5146 snprintf(buf, sizeof(buf), "iwpriv %s vhtsubfee 1", intf);
5147 if (system(buf) != 0) {
5148 sigma_dut_print(dut, DUT_MSG_ERROR,
5149 "iwpriv vhtsubfee failed");
5150 }
5151 snprintf(buf, sizeof(buf), "iwpriv %s vhtsubfer 1", intf);
5152 if (system(buf) != 0) {
5153 sigma_dut_print(dut, DUT_MSG_ERROR,
5154 "iwpriv vhtsubfer failed");
5155 }
5156 }
5157
5158 val = get_param(cmd, "MU_TxBF");
5159 if (val && (strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0)) {
5160 switch (get_driver_type()) {
5161 case DRIVER_ATHEROS:
5162 ath_sta_set_txsp_stream(dut, intf, "1SS");
5163 ath_sta_set_rxsp_stream(dut, intf, "1SS");
5164 case DRIVER_WCN:
5165 if (wcn_sta_set_sp_stream(dut, intf, "1SS") < 0) {
5166 send_resp(dut, conn, SIGMA_ERROR,
5167 "ErrorCode,Failed to set RX/TXSP_STREAM");
5168 return 0;
5169 }
5170 default:
5171 sigma_dut_print(dut, DUT_MSG_ERROR,
5172 "Setting SP_STREAM not supported");
5173 break;
5174 }
5175 snprintf(buf, sizeof(buf), "iwpriv %s vhtmubfee 1", intf);
5176 if (system(buf) != 0) {
5177 sigma_dut_print(dut, DUT_MSG_ERROR,
5178 "iwpriv vhtmubfee failed");
5179 }
5180 snprintf(buf, sizeof(buf), "iwpriv %s vhtmubfer 1", intf);
5181 if (system(buf) != 0) {
5182 sigma_dut_print(dut, DUT_MSG_ERROR,
5183 "iwpriv vhtmubfer failed");
5184 }
5185 }
5186
5187 val = get_param(cmd, "LDPC");
5188 if (val) {
5189 int ldpc;
5190
5191 ldpc = strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0;
5192 snprintf(buf, sizeof(buf), "iwpriv %s ldpc %d", intf, ldpc);
5193 if (system(buf) != 0) {
5194 sigma_dut_print(dut, DUT_MSG_ERROR,
5195 "iwpriv ldpc failed");
5196 }
5197 }
5198
5199 val = get_param(cmd, "opt_md_notif_ie");
5200 if (val) {
5201 char *result = NULL;
5202 char delim[] = ";";
5203 char token[30];
5204 int value, config_val = 0;
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05305205 char *saveptr;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005206
Peng Xub8fc5cc2017-05-10 17:27:28 -07005207 strlcpy(token, val, sizeof(token));
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05305208 result = strtok_r(token, delim, &saveptr);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005209
5210 /* Extract the NSS information */
5211 if (result) {
5212 value = atoi(result);
5213 switch (value) {
5214 case 1:
5215 config_val = 1;
5216 break;
5217 case 2:
5218 config_val = 3;
5219 break;
5220 case 3:
5221 config_val = 7;
5222 break;
5223 case 4:
5224 config_val = 15;
5225 break;
5226 default:
5227 config_val = 3;
5228 break;
5229 }
5230
5231 snprintf(buf, sizeof(buf), "iwpriv %s rxchainmask %d",
5232 intf, config_val);
5233 if (system(buf) != 0) {
5234 sigma_dut_print(dut, DUT_MSG_ERROR,
5235 "iwpriv rxchainmask failed");
5236 }
5237
5238 snprintf(buf, sizeof(buf), "iwpriv %s txchainmask %d",
5239 intf, config_val);
5240 if (system(buf) != 0) {
5241 sigma_dut_print(dut, DUT_MSG_ERROR,
5242 "iwpriv txchainmask failed");
5243 }
5244 }
5245
5246 /* Extract the channel width information */
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05305247 result = strtok_r(NULL, delim, &saveptr);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005248 if (result) {
5249 value = atoi(result);
5250 switch (value) {
5251 case 20:
5252 config_val = 0;
5253 break;
5254 case 40:
5255 config_val = 1;
5256 break;
5257 case 80:
5258 config_val = 2;
5259 break;
5260 case 160:
5261 config_val = 3;
5262 break;
5263 default:
5264 config_val = 2;
5265 break;
5266 }
5267
5268 dut->chwidth = config_val;
5269
5270 snprintf(buf, sizeof(buf), "iwpriv %s chwidth %d",
5271 intf, config_val);
5272 if (system(buf) != 0) {
5273 sigma_dut_print(dut, DUT_MSG_ERROR,
5274 "iwpriv chwidth failed");
5275 }
5276 }
5277
5278 snprintf(buf, sizeof(buf), "iwpriv %s opmode_notify 1", intf);
5279 if (system(buf) != 0) {
5280 sigma_dut_print(dut, DUT_MSG_ERROR,
5281 "iwpriv opmode_notify failed");
5282 }
5283 }
5284
5285 val = get_param(cmd, "nss_mcs_cap");
5286 if (val) {
5287 int nss, mcs;
5288 char token[20];
5289 char *result = NULL;
5290 unsigned int vht_mcsmap = 0;
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05305291 char *saveptr;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005292
Peng Xub8fc5cc2017-05-10 17:27:28 -07005293 strlcpy(token, val, sizeof(token));
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05305294 result = strtok_r(token, ";", &saveptr);
Pradeep Reddy POTTETIcd649a22016-01-29 12:55:59 +05305295 if (!result) {
5296 sigma_dut_print(dut, DUT_MSG_ERROR,
5297 "VHT NSS not specified");
5298 return 0;
5299 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005300 nss = atoi(result);
5301
5302 snprintf(buf, sizeof(buf), "iwpriv %s nss %d", intf, nss);
5303 if (system(buf) != 0) {
5304 sigma_dut_print(dut, DUT_MSG_ERROR,
5305 "iwpriv nss failed");
5306 }
5307
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05305308 result = strtok_r(NULL, ";", &saveptr);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005309 if (result == NULL) {
5310 sigma_dut_print(dut, DUT_MSG_ERROR,
5311 "VHTMCS NOT SPECIFIED!");
5312 return 0;
5313 }
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05305314 result = strtok_r(result, "-", &saveptr);
5315 result = strtok_r(NULL, "-", &saveptr);
Pradeep Reddy POTTETIcd649a22016-01-29 12:55:59 +05305316 if (!result) {
5317 sigma_dut_print(dut, DUT_MSG_ERROR,
5318 "VHT MCS not specified");
5319 return 0;
5320 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005321 mcs = atoi(result);
5322
5323 snprintf(buf, sizeof(buf), "iwpriv %s vhtmcs %d", intf, mcs);
5324 if (system(buf) != 0) {
5325 sigma_dut_print(dut, DUT_MSG_ERROR,
5326 "iwpriv mcs failed");
5327 }
5328
5329 switch (nss) {
5330 case 1:
5331 switch (mcs) {
5332 case 7:
5333 vht_mcsmap = 0xfffc;
5334 break;
5335 case 8:
5336 vht_mcsmap = 0xfffd;
5337 break;
5338 case 9:
5339 vht_mcsmap = 0xfffe;
5340 break;
5341 default:
5342 vht_mcsmap = 0xfffe;
5343 break;
5344 }
5345 break;
5346 case 2:
5347 switch (mcs) {
5348 case 7:
5349 vht_mcsmap = 0xfff0;
5350 break;
5351 case 8:
5352 vht_mcsmap = 0xfff5;
5353 break;
5354 case 9:
5355 vht_mcsmap = 0xfffa;
5356 break;
5357 default:
5358 vht_mcsmap = 0xfffa;
5359 break;
5360 }
5361 break;
5362 case 3:
5363 switch (mcs) {
5364 case 7:
5365 vht_mcsmap = 0xffc0;
5366 break;
5367 case 8:
5368 vht_mcsmap = 0xffd5;
5369 break;
5370 case 9:
5371 vht_mcsmap = 0xffea;
5372 break;
5373 default:
5374 vht_mcsmap = 0xffea;
5375 break;
5376 }
5377 break;
5378 default:
5379 vht_mcsmap = 0xffea;
5380 break;
5381 }
5382 snprintf(buf, sizeof(buf), "iwpriv %s vht_mcsmap 0x%04x",
5383 intf, vht_mcsmap);
5384 if (system(buf) != 0) {
5385 sigma_dut_print(dut, DUT_MSG_ERROR,
5386 "iwpriv vht_mcsmap failed");
5387 }
5388 }
5389
5390 /* UNSUPPORTED: val = get_param(cmd, "Tx_lgi_rate"); */
5391
5392 val = get_param(cmd, "Vht_tkip");
5393 if (val)
5394 tkip = strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0;
5395
5396 val = get_param(cmd, "Vht_wep");
5397 if (val)
5398 wep = strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0;
5399
5400 if (tkip != -1 || wep != -1) {
5401 if ((tkip == 1 && wep != 0) || (wep == 1 && tkip != 0)) {
5402 snprintf(buf, sizeof(buf), "iwpriv %s htweptkip 1",
5403 intf);
5404 } else if ((tkip == 0 && wep != 1) || (wep == 0 && tkip != 1)) {
5405 snprintf(buf, sizeof(buf), "iwpriv %s htweptkip 0",
5406 intf);
5407 } else {
5408 sigma_dut_print(dut, DUT_MSG_ERROR,
5409 "ErrorCode,mixed mode of VHT TKIP/WEP not supported");
5410 return 0;
5411 }
5412
5413 if (system(buf) != 0) {
5414 sigma_dut_print(dut, DUT_MSG_ERROR,
5415 "iwpriv htweptkip failed");
5416 }
5417 }
5418
5419 val = get_param(cmd, "txBandwidth");
5420 if (val) {
5421 switch (get_driver_type()) {
5422 case DRIVER_ATHEROS:
5423 if (ath_set_width(dut, conn, intf, val) < 0) {
5424 send_resp(dut, conn, SIGMA_ERROR,
5425 "ErrorCode,Failed to set txBandwidth");
5426 return 0;
5427 }
5428 break;
5429 default:
5430 sigma_dut_print(dut, DUT_MSG_ERROR,
5431 "Setting txBandwidth not supported");
5432 break;
5433 }
5434 }
5435
5436 return cmd_sta_set_wireless_common(intf, dut, conn, cmd);
5437}
5438
5439
5440static int sta_set_wireless_60g(struct sigma_dut *dut,
5441 struct sigma_conn *conn,
5442 struct sigma_cmd *cmd)
5443{
5444 const char *dev_role = get_param(cmd, "DevRole");
5445
5446 if (!dev_role) {
5447 send_resp(dut, conn, SIGMA_INVALID,
5448 "ErrorCode,DevRole not specified");
5449 return 0;
5450 }
5451
5452 if (strcasecmp(dev_role, "PCP") == 0)
5453 return sta_set_60g_pcp(dut, conn, cmd);
5454 if (strcasecmp(dev_role, "STA") == 0)
5455 return sta_set_60g_sta(dut, conn, cmd);
5456 send_resp(dut, conn, SIGMA_INVALID,
5457 "ErrorCode,DevRole not supported");
5458 return 0;
5459}
5460
5461
5462static int cmd_sta_set_wireless(struct sigma_dut *dut, struct sigma_conn *conn,
5463 struct sigma_cmd *cmd)
5464{
5465 const char *val;
5466
5467 val = get_param(cmd, "Program");
5468 if (val) {
5469 if (strcasecmp(val, "11n") == 0)
5470 return cmd_sta_set_11n(dut, conn, cmd);
5471 if (strcasecmp(val, "VHT") == 0)
5472 return cmd_sta_set_wireless_vht(dut, conn, cmd);
5473 if (strcasecmp(val, "60ghz") == 0)
5474 return sta_set_wireless_60g(dut, conn, cmd);
5475 send_resp(dut, conn, SIGMA_ERROR,
5476 "ErrorCode,Program value not supported");
5477 } else {
5478 send_resp(dut, conn, SIGMA_ERROR,
5479 "ErrorCode,Program argument not available");
5480 }
5481
5482 return 0;
5483}
5484
5485
5486static void ath_sta_inject_frame(struct sigma_dut *dut, const char *intf,
5487 int tid)
5488{
5489 char buf[100];
5490 int tid_to_dscp [] = { 0x00, 0x20, 0x40, 0x60, 0x80, 0xa0, 0xc0, 0xe0 };
5491
Pradeep Reddy POTTETId31d1322016-10-13 17:22:03 +05305492 if (tid < 0 ||
5493 tid >= (int) (sizeof(tid_to_dscp) / sizeof(tid_to_dscp[0]))) {
5494 sigma_dut_print(dut, DUT_MSG_ERROR, "Unsupported TID: %d", tid);
5495 return;
5496 }
5497
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005498 /*
5499 * Two ways to ensure that addba request with a
5500 * non zero TID could be sent out. EV 117296
5501 */
5502 snprintf(buf, sizeof(buf),
5503 "ping -c 8 -Q %d `arp -a | grep wlan0 | awk '{print $2}' | tr -d '()'`",
5504 tid);
5505 if (system(buf) != 0) {
5506 sigma_dut_print(dut, DUT_MSG_ERROR,
5507 "Ping did not send out");
5508 }
5509
5510 snprintf(buf, sizeof(buf),
5511 "iwconfig %s | grep Access | awk '{print $6}' > %s",
5512 intf, VI_QOS_TMP_FILE);
5513 if (system(buf) != 0)
5514 return;
5515
5516 snprintf(buf, sizeof(buf),
5517 "ifconfig %s | grep HWaddr | cut -b 39-56 >> %s",
5518 intf, VI_QOS_TMP_FILE);
5519 if (system(buf) != 0)
5520 sigma_dut_print(dut, DUT_MSG_ERROR, "HWaddr matching failed");
5521
5522 snprintf(buf,sizeof(buf), "sed -n '3,$p' %s >> %s",
5523 VI_QOS_REFFILE, VI_QOS_TMP_FILE);
5524 if (system(buf) != 0) {
5525 sigma_dut_print(dut, DUT_MSG_ERROR,
5526 "VI_QOS_TEMP_FILE generation error failed");
5527 }
5528 snprintf(buf, sizeof(buf), "sed '5 c %x' %s > %s",
5529 tid_to_dscp[tid], VI_QOS_TMP_FILE, VI_QOS_FILE);
5530 if (system(buf) != 0) {
5531 sigma_dut_print(dut, DUT_MSG_ERROR,
5532 "VI_QOS_FILE generation failed");
5533 }
5534
5535 snprintf(buf, sizeof(buf), "sed '5 c %x' %s > %s",
5536 tid_to_dscp[tid], VI_QOS_TMP_FILE, VI_QOS_FILE);
5537 if (system(buf) != 0) {
5538 sigma_dut_print(dut, DUT_MSG_ERROR,
5539 "VI_QOS_FILE generation failed");
5540 }
5541
5542 snprintf(buf, sizeof(buf), "ethinject %s %s", intf, VI_QOS_FILE);
5543 if (system(buf) != 0) {
5544 }
5545}
5546
5547
5548static int ath_sta_send_addba(struct sigma_dut *dut, struct sigma_conn *conn,
5549 struct sigma_cmd *cmd)
5550{
5551 const char *intf = get_param(cmd, "Interface");
5552 const char *val;
5553 int tid = 0;
5554 char buf[100];
5555
5556 val = get_param(cmd, "TID");
5557 if (val) {
5558 tid = atoi(val);
5559 if (tid)
5560 ath_sta_inject_frame(dut, intf, tid);
5561 }
5562
5563 /* Command sequence for ADDBA request on Peregrine based devices */
5564 snprintf(buf, sizeof(buf), "iwpriv %s setaddbaoper 1", intf);
5565 if (system(buf) != 0) {
5566 sigma_dut_print(dut, DUT_MSG_ERROR,
5567 "iwpriv setaddbaoper failed");
5568 }
5569
5570 snprintf(buf, sizeof(buf), "wifitool %s senddelba 1 %d 1 4", intf, tid);
5571 if (system(buf) != 0) {
5572 sigma_dut_print(dut, DUT_MSG_ERROR,
5573 "wifitool senddelba failed");
5574 }
5575
5576 snprintf(buf, sizeof(buf), "wifitool %s sendaddba 1 %d 64", intf, tid);
5577 if (system(buf) != 0) {
5578 sigma_dut_print(dut, DUT_MSG_ERROR,
5579 "wifitool sendaddba failed");
5580 }
5581
5582 /* UNSUPPORTED: val = get_param(cmd, "Dest_mac"); */
5583
5584 return 1;
5585}
5586
5587
Lior David9981b512017-01-20 13:16:40 +02005588#ifdef __linux__
5589
5590static int wil6210_send_addba(struct sigma_dut *dut, const char *dest_mac,
5591 int agg_size)
5592{
5593 char dir[128], buf[128];
5594 FILE *f;
5595 regex_t re;
5596 regmatch_t m[2];
5597 int rc, ret = -1, vring_id, found;
5598
5599 if (wil6210_get_debugfs_dir(dut, dir, sizeof(dir))) {
5600 sigma_dut_print(dut, DUT_MSG_ERROR,
5601 "failed to get wil6210 debugfs dir");
5602 return -1;
5603 }
5604
5605 snprintf(buf, sizeof(buf), "%s/vrings", dir);
5606 f = fopen(buf, "r");
5607 if (!f) {
5608 sigma_dut_print(dut, DUT_MSG_ERROR, "failed to open: %s", buf);
5609 return -1;
5610 }
5611
5612 if (regcomp(&re, "VRING tx_[ \t]*([0-9]+)", REG_EXTENDED)) {
5613 sigma_dut_print(dut, DUT_MSG_ERROR, "regcomp failed");
5614 goto out;
5615 }
5616
5617 /* find TX VRING for the mac address */
5618 found = 0;
5619 while (fgets(buf, sizeof(buf), f)) {
5620 if (strcasestr(buf, dest_mac)) {
5621 found = 1;
5622 break;
5623 }
5624 }
5625
5626 if (!found) {
5627 sigma_dut_print(dut, DUT_MSG_ERROR,
5628 "no TX VRING for %s", dest_mac);
5629 goto out;
5630 }
5631
5632 /* extract VRING ID, "VRING tx_<id> = {" */
5633 if (!fgets(buf, sizeof(buf), f)) {
5634 sigma_dut_print(dut, DUT_MSG_ERROR,
5635 "no VRING start line for %s", dest_mac);
5636 goto out;
5637 }
5638
5639 rc = regexec(&re, buf, 2, m, 0);
5640 regfree(&re);
5641 if (rc || m[1].rm_so < 0) {
5642 sigma_dut_print(dut, DUT_MSG_ERROR,
5643 "no VRING TX ID for %s", dest_mac);
5644 goto out;
5645 }
5646 buf[m[1].rm_eo] = 0;
5647 vring_id = atoi(&buf[m[1].rm_so]);
5648
5649 /* send the addba command */
5650 fclose(f);
5651 snprintf(buf, sizeof(buf), "%s/back", dir);
5652 f = fopen(buf, "w");
5653 if (!f) {
5654 sigma_dut_print(dut, DUT_MSG_ERROR,
5655 "failed to open: %s", buf);
5656 return -1;
5657 }
5658
5659 fprintf(f, "add %d %d\n", vring_id, agg_size);
5660
5661 ret = 0;
5662
5663out:
5664 fclose(f);
5665
5666 return ret;
5667}
5668
5669
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005670static int send_addba_60g(struct sigma_dut *dut, struct sigma_conn *conn,
5671 struct sigma_cmd *cmd)
5672{
5673 const char *val;
5674 int tid = 0;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005675
5676 val = get_param(cmd, "TID");
5677 if (val) {
5678 tid = atoi(val);
5679 if (tid != 0) {
5680 sigma_dut_print(dut, DUT_MSG_ERROR,
5681 "Ignore TID %d for send_addba use TID 0 for 60g since only 0 required on TX",
5682 tid);
5683 }
5684 }
5685
5686 val = get_param(cmd, "Dest_mac");
5687 if (!val) {
5688 sigma_dut_print(dut, DUT_MSG_ERROR,
5689 "Currently not supporting addba for 60G without Dest_mac");
5690 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
5691 }
5692
Lior David9981b512017-01-20 13:16:40 +02005693 if (wil6210_send_addba(dut, val, dut->back_rcv_buf))
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005694 return -1;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005695
5696 return 1;
5697}
5698
Lior David9981b512017-01-20 13:16:40 +02005699#endif /* __linux__ */
5700
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005701
5702static int cmd_sta_send_addba(struct sigma_dut *dut, struct sigma_conn *conn,
5703 struct sigma_cmd *cmd)
5704{
5705 switch (get_driver_type()) {
5706 case DRIVER_ATHEROS:
5707 return ath_sta_send_addba(dut, conn, cmd);
Lior David9981b512017-01-20 13:16:40 +02005708#ifdef __linux__
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005709 case DRIVER_WIL6210:
5710 return send_addba_60g(dut, conn, cmd);
Lior David9981b512017-01-20 13:16:40 +02005711#endif /* __linux__ */
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005712 default:
5713 /*
5714 * There is no driver specific implementation for other drivers.
5715 * Ignore the command and report COMPLETE since the following
5716 * throughput test operation will end up sending ADDBA anyway.
5717 */
5718 return 1;
5719 }
5720}
5721
5722
5723int inject_eth_frame(int s, const void *data, size_t len,
5724 unsigned short ethtype, char *dst, char *src)
5725{
5726 struct iovec iov[4] = {
5727 {
5728 .iov_base = dst,
5729 .iov_len = ETH_ALEN,
5730 },
5731 {
5732 .iov_base = src,
5733 .iov_len = ETH_ALEN,
5734 },
5735 {
5736 .iov_base = &ethtype,
5737 .iov_len = sizeof(unsigned short),
5738 },
5739 {
5740 .iov_base = (void *) data,
5741 .iov_len = len,
5742 }
5743 };
5744 struct msghdr msg = {
5745 .msg_name = NULL,
5746 .msg_namelen = 0,
5747 .msg_iov = iov,
5748 .msg_iovlen = 4,
5749 .msg_control = NULL,
5750 .msg_controllen = 0,
5751 .msg_flags = 0,
5752 };
5753
5754 return sendmsg(s, &msg, 0);
5755}
5756
5757#if defined(__linux__) || defined(__QNXNTO__)
5758
5759int inject_frame(int s, const void *data, size_t len, int encrypt)
5760{
5761#define IEEE80211_RADIOTAP_F_WEP 0x04
5762#define IEEE80211_RADIOTAP_F_FRAG 0x08
5763 unsigned char rtap_hdr[] = {
5764 0x00, 0x00, /* radiotap version */
5765 0x0e, 0x00, /* radiotap length */
5766 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
5767 IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
5768 0x00, /* padding */
5769 0x00, 0x00, /* RX and TX flags to indicate that */
5770 0x00, 0x00, /* this is the injected frame directly */
5771 };
5772 struct iovec iov[2] = {
5773 {
5774 .iov_base = &rtap_hdr,
5775 .iov_len = sizeof(rtap_hdr),
5776 },
5777 {
5778 .iov_base = (void *) data,
5779 .iov_len = len,
5780 }
5781 };
5782 struct msghdr msg = {
5783 .msg_name = NULL,
5784 .msg_namelen = 0,
5785 .msg_iov = iov,
5786 .msg_iovlen = 2,
5787 .msg_control = NULL,
5788 .msg_controllen = 0,
5789 .msg_flags = 0,
5790 };
5791
5792 if (encrypt)
5793 rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
5794
5795 return sendmsg(s, &msg, 0);
5796}
5797
5798
5799int open_monitor(const char *ifname)
5800{
5801#ifdef __QNXNTO__
5802 struct sockaddr_dl ll;
5803 int s;
5804
5805 memset(&ll, 0, sizeof(ll));
5806 ll.sdl_family = AF_LINK;
5807 ll.sdl_index = if_nametoindex(ifname);
5808 if (ll.sdl_index == 0) {
5809 perror("if_nametoindex");
5810 return -1;
5811 }
5812 s = socket(PF_INET, SOCK_RAW, 0);
5813#else /* __QNXNTO__ */
5814 struct sockaddr_ll ll;
5815 int s;
5816
5817 memset(&ll, 0, sizeof(ll));
5818 ll.sll_family = AF_PACKET;
5819 ll.sll_ifindex = if_nametoindex(ifname);
5820 if (ll.sll_ifindex == 0) {
5821 perror("if_nametoindex");
5822 return -1;
5823 }
5824 s = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
5825#endif /* __QNXNTO__ */
5826 if (s < 0) {
5827 perror("socket[PF_PACKET,SOCK_RAW]");
5828 return -1;
5829 }
5830
5831 if (bind(s, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
5832 perror("monitor socket bind");
5833 close(s);
5834 return -1;
5835 }
5836
5837 return s;
5838}
5839
5840
5841static int hex2num(char c)
5842{
5843 if (c >= '0' && c <= '9')
5844 return c - '0';
5845 if (c >= 'a' && c <= 'f')
5846 return c - 'a' + 10;
5847 if (c >= 'A' && c <= 'F')
5848 return c - 'A' + 10;
5849 return -1;
5850}
5851
5852
5853int hwaddr_aton(const char *txt, unsigned char *addr)
5854{
5855 int i;
5856
5857 for (i = 0; i < 6; i++) {
5858 int a, b;
5859
5860 a = hex2num(*txt++);
5861 if (a < 0)
5862 return -1;
5863 b = hex2num(*txt++);
5864 if (b < 0)
5865 return -1;
5866 *addr++ = (a << 4) | b;
5867 if (i < 5 && *txt++ != ':')
5868 return -1;
5869 }
5870
5871 return 0;
5872}
5873
5874#endif /* defined(__linux__) || defined(__QNXNTO__) */
5875
5876enum send_frame_type {
5877 DISASSOC, DEAUTH, SAQUERY, AUTH, ASSOCREQ, REASSOCREQ, DLS_REQ
5878};
5879enum send_frame_protection {
5880 CORRECT_KEY, INCORRECT_KEY, UNPROTECTED
5881};
5882
5883
5884static int sta_inject_frame(struct sigma_dut *dut, struct sigma_conn *conn,
5885 enum send_frame_type frame,
5886 enum send_frame_protection protected,
5887 const char *dest)
5888{
5889#ifdef __linux__
5890 unsigned char buf[1000], *pos;
5891 int s, res;
5892 char bssid[20], addr[20];
5893 char result[32], ssid[100];
5894 size_t ssid_len;
5895
5896 if (get_wpa_status(get_station_ifname(), "wpa_state", result,
5897 sizeof(result)) < 0 ||
5898 strncmp(result, "COMPLETED", 9) != 0) {
5899 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Not connected");
5900 return 0;
5901 }
5902
5903 if (get_wpa_status(get_station_ifname(), "bssid", bssid, sizeof(bssid))
5904 < 0) {
5905 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not get "
5906 "current BSSID");
5907 return 0;
5908 }
5909
5910 if (get_wpa_status(get_station_ifname(), "address", addr, sizeof(addr))
5911 < 0) {
5912 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not get "
5913 "own MAC address");
5914 return 0;
5915 }
5916
5917 if (get_wpa_status(get_station_ifname(), "ssid", ssid, sizeof(ssid))
5918 < 0) {
5919 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not get "
5920 "current SSID");
5921 return 0;
5922 }
5923 ssid_len = strlen(ssid);
5924
5925 pos = buf;
5926
5927 /* Frame Control */
5928 switch (frame) {
5929 case DISASSOC:
5930 *pos++ = 0xa0;
5931 break;
5932 case DEAUTH:
5933 *pos++ = 0xc0;
5934 break;
5935 case SAQUERY:
5936 *pos++ = 0xd0;
5937 break;
5938 case AUTH:
5939 *pos++ = 0xb0;
5940 break;
5941 case ASSOCREQ:
5942 *pos++ = 0x00;
5943 break;
5944 case REASSOCREQ:
5945 *pos++ = 0x20;
5946 break;
5947 case DLS_REQ:
5948 *pos++ = 0xd0;
5949 break;
5950 }
5951
5952 if (protected == INCORRECT_KEY)
5953 *pos++ = 0x40; /* Set Protected field to 1 */
5954 else
5955 *pos++ = 0x00;
5956
5957 /* Duration */
5958 *pos++ = 0x00;
5959 *pos++ = 0x00;
5960
5961 /* addr1 = DA (current AP) */
5962 hwaddr_aton(bssid, pos);
5963 pos += 6;
5964 /* addr2 = SA (own address) */
5965 hwaddr_aton(addr, pos);
5966 pos += 6;
5967 /* addr3 = BSSID (current AP) */
5968 hwaddr_aton(bssid, pos);
5969 pos += 6;
5970
5971 /* Seq# (to be filled by driver/mac80211) */
5972 *pos++ = 0x00;
5973 *pos++ = 0x00;
5974
5975 if (protected == INCORRECT_KEY) {
5976 /* CCMP parameters */
5977 memcpy(pos, "\x61\x01\x00\x20\x00\x10\x00\x00", 8);
5978 pos += 8;
5979 }
5980
5981 if (protected == INCORRECT_KEY) {
5982 switch (frame) {
5983 case DEAUTH:
5984 /* Reason code (encrypted) */
5985 memcpy(pos, "\xa7\x39", 2);
5986 pos += 2;
5987 break;
5988 case DISASSOC:
5989 /* Reason code (encrypted) */
5990 memcpy(pos, "\xa7\x39", 2);
5991 pos += 2;
5992 break;
5993 case SAQUERY:
5994 /* Category|Action|TransID (encrypted) */
5995 memcpy(pos, "\x6f\xbd\xe9\x4d", 4);
5996 pos += 4;
5997 break;
5998 default:
5999 return -1;
6000 }
6001
6002 /* CCMP MIC */
6003 memcpy(pos, "\xc8\xd8\x3b\x06\x5d\xb7\x25\x68", 8);
6004 pos += 8;
6005 } else {
6006 switch (frame) {
6007 case DEAUTH:
6008 /* reason code = 8 */
6009 *pos++ = 0x08;
6010 *pos++ = 0x00;
6011 break;
6012 case DISASSOC:
6013 /* reason code = 8 */
6014 *pos++ = 0x08;
6015 *pos++ = 0x00;
6016 break;
6017 case SAQUERY:
6018 /* Category - SA Query */
6019 *pos++ = 0x08;
6020 /* SA query Action - Request */
6021 *pos++ = 0x00;
6022 /* Transaction ID */
6023 *pos++ = 0x12;
6024 *pos++ = 0x34;
6025 break;
6026 case AUTH:
6027 /* Auth Alg (Open) */
6028 *pos++ = 0x00;
6029 *pos++ = 0x00;
6030 /* Seq# */
6031 *pos++ = 0x01;
6032 *pos++ = 0x00;
6033 /* Status code */
6034 *pos++ = 0x00;
6035 *pos++ = 0x00;
6036 break;
6037 case ASSOCREQ:
6038 /* Capability Information */
6039 *pos++ = 0x31;
6040 *pos++ = 0x04;
6041 /* Listen Interval */
6042 *pos++ = 0x0a;
6043 *pos++ = 0x00;
6044 /* SSID */
6045 *pos++ = 0x00;
6046 *pos++ = ssid_len;
6047 memcpy(pos, ssid, ssid_len);
6048 pos += ssid_len;
6049 /* Supported Rates */
6050 memcpy(pos, "\x01\x08\x02\x04\x0b\x16\x0c\x12\x18\x24",
6051 10);
6052 pos += 10;
6053 /* Extended Supported Rates */
6054 memcpy(pos, "\x32\x04\x30\x48\x60\x6c", 6);
6055 pos += 6;
6056 /* RSN */
6057 memcpy(pos, "\x30\x1a\x01\x00\x00\x0f\xac\x04\x01\x00"
6058 "\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x02\xc0"
6059 "\x00\x00\x00\x00\x0f\xac\x06", 28);
6060 pos += 28;
6061 break;
6062 case REASSOCREQ:
6063 /* Capability Information */
6064 *pos++ = 0x31;
6065 *pos++ = 0x04;
6066 /* Listen Interval */
6067 *pos++ = 0x0a;
6068 *pos++ = 0x00;
6069 /* Current AP */
6070 hwaddr_aton(bssid, pos);
6071 pos += 6;
6072 /* SSID */
6073 *pos++ = 0x00;
6074 *pos++ = ssid_len;
6075 memcpy(pos, ssid, ssid_len);
6076 pos += ssid_len;
6077 /* Supported Rates */
6078 memcpy(pos, "\x01\x08\x02\x04\x0b\x16\x0c\x12\x18\x24",
6079 10);
6080 pos += 10;
6081 /* Extended Supported Rates */
6082 memcpy(pos, "\x32\x04\x30\x48\x60\x6c", 6);
6083 pos += 6;
6084 /* RSN */
6085 memcpy(pos, "\x30\x1a\x01\x00\x00\x0f\xac\x04\x01\x00"
6086 "\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x02\xc0"
6087 "\x00\x00\x00\x00\x0f\xac\x06", 28);
6088 pos += 28;
6089 break;
6090 case DLS_REQ:
6091 /* Category - DLS */
6092 *pos++ = 0x02;
6093 /* DLS Action - Request */
6094 *pos++ = 0x00;
6095 /* Destination MACAddress */
6096 if (dest)
6097 hwaddr_aton(dest, pos);
6098 else
6099 memset(pos, 0, 6);
6100 pos += 6;
6101 /* Source MACAddress */
6102 hwaddr_aton(addr, pos);
6103 pos += 6;
6104 /* Capability Information */
6105 *pos++ = 0x10; /* Privacy */
6106 *pos++ = 0x06; /* QoS */
6107 /* DLS Timeout Value */
6108 *pos++ = 0x00;
6109 *pos++ = 0x01;
6110 /* Supported rates */
6111 *pos++ = 0x01;
6112 *pos++ = 0x08;
6113 *pos++ = 0x0c; /* 6 Mbps */
6114 *pos++ = 0x12; /* 9 Mbps */
6115 *pos++ = 0x18; /* 12 Mbps */
6116 *pos++ = 0x24; /* 18 Mbps */
6117 *pos++ = 0x30; /* 24 Mbps */
6118 *pos++ = 0x48; /* 36 Mbps */
6119 *pos++ = 0x60; /* 48 Mbps */
6120 *pos++ = 0x6c; /* 54 Mbps */
6121 /* TODO: Extended Supported Rates */
6122 /* TODO: HT Capabilities */
6123 break;
6124 }
6125 }
6126
6127 s = open_monitor("sigmadut");
6128 if (s < 0) {
6129 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to open "
6130 "monitor socket");
6131 return 0;
6132 }
6133
6134 res = inject_frame(s, buf, pos - buf, protected == CORRECT_KEY);
6135 if (res < 0) {
6136 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to "
6137 "inject frame");
Pradeep Reddy POTTETI673d85c2016-07-26 19:08:07 +05306138 close(s);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02006139 return 0;
6140 }
6141 if (res < pos - buf) {
6142 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Only partial "
6143 "frame sent");
Pradeep Reddy POTTETI673d85c2016-07-26 19:08:07 +05306144 close(s);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02006145 return 0;
6146 }
6147
6148 close(s);
6149
6150 return 1;
6151#else /* __linux__ */
6152 send_resp(dut, conn, SIGMA_ERROR, "errorCode,sta_send_frame not "
6153 "yet supported");
6154 return 0;
6155#endif /* __linux__ */
6156}
6157
6158
6159static int cmd_sta_send_frame_tdls(struct sigma_dut *dut,
6160 struct sigma_conn *conn,
6161 struct sigma_cmd *cmd)
6162{
6163 const char *intf = get_param(cmd, "Interface");
6164 const char *sta, *val;
6165 unsigned char addr[ETH_ALEN];
6166 char buf[100];
6167
6168 sta = get_param(cmd, "peer");
6169 if (sta == NULL)
6170 sta = get_param(cmd, "station");
6171 if (sta == NULL) {
6172 send_resp(dut, conn, SIGMA_ERROR,
6173 "ErrorCode,Missing peer address");
6174 return 0;
6175 }
6176 if (hwaddr_aton(sta, addr) < 0) {
6177 send_resp(dut, conn, SIGMA_ERROR,
6178 "ErrorCode,Invalid peer address");
6179 return 0;
6180 }
6181
6182 val = get_param(cmd, "type");
6183 if (val == NULL)
6184 return -1;
6185
6186 if (strcasecmp(val, "DISCOVERY") == 0) {
6187 snprintf(buf, sizeof(buf), "TDLS_DISCOVER %s", sta);
6188 if (wpa_command(intf, buf) < 0) {
6189 send_resp(dut, conn, SIGMA_ERROR,
6190 "ErrorCode,Failed to send TDLS discovery");
6191 return 0;
6192 }
6193 return 1;
6194 }
6195
6196 if (strcasecmp(val, "SETUP") == 0) {
6197 int status = 0, timeout = 0;
6198
6199 val = get_param(cmd, "Status");
6200 if (val)
6201 status = atoi(val);
6202
6203 val = get_param(cmd, "Timeout");
6204 if (val)
6205 timeout = atoi(val);
6206
6207 if (status != 0 && status != 37) {
6208 send_resp(dut, conn, SIGMA_ERROR,
6209 "ErrorCode,Unsupported status value");
6210 return 0;
6211 }
6212
6213 if (timeout != 0 && timeout != 301) {
6214 send_resp(dut, conn, SIGMA_ERROR,
6215 "ErrorCode,Unsupported timeout value");
6216 return 0;
6217 }
6218
6219 if (status && timeout) {
6220 send_resp(dut, conn, SIGMA_ERROR,
6221 "ErrorCode,Unsupported timeout+status "
6222 "combination");
6223 return 0;
6224 }
6225
6226 if (status == 37 &&
6227 wpa_command(intf, "SET tdls_testing 0x200")) {
6228 send_resp(dut, conn, SIGMA_ERROR,
6229 "ErrorCode,Failed to enable "
6230 "decline setup response test mode");
6231 return 0;
6232 }
6233
6234 if (timeout == 301) {
6235 int res;
6236 if (dut->no_tpk_expiration)
6237 res = wpa_command(intf,
6238 "SET tdls_testing 0x108");
6239 else
6240 res = wpa_command(intf,
6241 "SET tdls_testing 0x8");
6242 if (res) {
6243 send_resp(dut, conn, SIGMA_ERROR,
6244 "ErrorCode,Failed to set short TPK "
6245 "lifetime");
6246 return 0;
6247 }
6248 }
6249
6250 snprintf(buf, sizeof(buf), "TDLS_SETUP %s", sta);
6251 if (wpa_command(intf, buf) < 0) {
6252 send_resp(dut, conn, SIGMA_ERROR,
6253 "ErrorCode,Failed to send TDLS setup");
6254 return 0;
6255 }
6256 return 1;
6257 }
6258
6259 if (strcasecmp(val, "TEARDOWN") == 0) {
6260 snprintf(buf, sizeof(buf), "TDLS_TEARDOWN %s", sta);
6261 if (wpa_command(intf, buf) < 0) {
6262 send_resp(dut, conn, SIGMA_ERROR,
6263 "ErrorCode,Failed to send TDLS teardown");
6264 return 0;
6265 }
6266 return 1;
6267 }
6268
6269 send_resp(dut, conn, SIGMA_ERROR,
6270 "ErrorCode,Unsupported TDLS frame");
6271 return 0;
6272}
6273
6274
6275static int sta_ap_known(const char *ifname, const char *bssid)
6276{
6277 char buf[4096];
6278
6279 snprintf(buf, sizeof(buf), "BSS %s", bssid);
6280 if (wpa_command_resp(ifname, buf, buf, sizeof(buf)) < 0)
6281 return 0;
6282 if (strncmp(buf, "id=", 3) != 0)
6283 return 0;
6284 return 1;
6285}
6286
6287
6288static int sta_scan_ap(struct sigma_dut *dut, const char *ifname,
6289 const char *bssid)
6290{
6291 int res;
6292 struct wpa_ctrl *ctrl;
6293 char buf[256];
6294
6295 if (sta_ap_known(ifname, bssid))
6296 return 0;
6297 sigma_dut_print(dut, DUT_MSG_DEBUG,
6298 "AP not in BSS table - start scan");
6299
6300 ctrl = open_wpa_mon(ifname);
6301 if (ctrl == NULL) {
6302 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to open "
6303 "wpa_supplicant monitor connection");
6304 return -1;
6305 }
6306
6307 if (wpa_command(ifname, "SCAN") < 0) {
6308 sigma_dut_print(dut, DUT_MSG_INFO, "Failed to start scan");
6309 wpa_ctrl_detach(ctrl);
6310 wpa_ctrl_close(ctrl);
6311 return -1;
6312 }
6313
6314 res = get_wpa_cli_event(dut, ctrl, "CTRL-EVENT-SCAN-RESULTS",
6315 buf, sizeof(buf));
6316
6317 wpa_ctrl_detach(ctrl);
6318 wpa_ctrl_close(ctrl);
6319
6320 if (res < 0) {
6321 sigma_dut_print(dut, DUT_MSG_INFO, "Scan did not complete");
6322 return -1;
6323 }
6324
6325 if (sta_ap_known(ifname, bssid))
6326 return 0;
6327 sigma_dut_print(dut, DUT_MSG_INFO, "AP not in BSS table");
6328 return -1;
6329}
6330
6331
6332static int cmd_sta_send_frame_hs2_neighadv(struct sigma_dut *dut,
6333 struct sigma_conn *conn,
6334 struct sigma_cmd *cmd,
6335 const char *intf)
6336{
6337 char buf[200];
6338
6339 snprintf(buf, sizeof(buf), "ndsend 2001:DB8::1 %s", intf);
6340 if (system(buf) != 0) {
6341 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Failed to run "
6342 "ndsend");
6343 return 0;
6344 }
6345
6346 return 1;
6347}
6348
6349
6350static int cmd_sta_send_frame_hs2_neighsolreq(struct sigma_dut *dut,
6351 struct sigma_conn *conn,
6352 struct sigma_cmd *cmd,
6353 const char *intf)
6354{
6355 char buf[200];
6356 const char *ip = get_param(cmd, "SenderIP");
6357
6358 snprintf(buf, sizeof(buf), "ndisc6 -nm %s %s -r 4", ip, intf);
6359 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
6360 if (system(buf) == 0) {
6361 sigma_dut_print(dut, DUT_MSG_INFO,
6362 "Neighbor Solicitation got a response "
6363 "for %s@%s", ip, intf);
6364 }
6365
6366 return 1;
6367}
6368
6369
6370static int cmd_sta_send_frame_hs2_arpprobe(struct sigma_dut *dut,
6371 struct sigma_conn *conn,
6372 struct sigma_cmd *cmd,
6373 const char *ifname)
6374{
6375 char buf[200];
6376 const char *ip = get_param(cmd, "SenderIP");
6377
6378 if (ip == NULL) {
6379 send_resp(dut, conn, SIGMA_ERROR,
6380 "ErrorCode,Missing SenderIP parameter");
6381 return 0;
6382 }
6383 snprintf(buf, sizeof(buf), "arping -I %s -D %s -c 4", ifname, ip);
6384 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
6385 if (system(buf) != 0) {
6386 sigma_dut_print(dut, DUT_MSG_INFO, "arping DAD got a response "
6387 "for %s@%s", ip, ifname);
6388 }
6389
6390 return 1;
6391}
6392
6393
6394static int cmd_sta_send_frame_hs2_arpannounce(struct sigma_dut *dut,
6395 struct sigma_conn *conn,
6396 struct sigma_cmd *cmd,
6397 const char *ifname)
6398{
6399 char buf[200];
6400 char ip[16];
6401 int s;
6402
6403 s = socket(PF_INET, SOCK_DGRAM, 0);
6404 if (s >= 0) {
6405 struct ifreq ifr;
6406 struct sockaddr_in saddr;
6407
6408 memset(&ifr, 0, sizeof(ifr));
Peng Xub8fc5cc2017-05-10 17:27:28 -07006409 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
Jouni Malinencd4e3c32015-10-29 12:39:56 +02006410 if (ioctl(s, SIOCGIFADDR, &ifr) < 0) {
6411 sigma_dut_print(dut, DUT_MSG_INFO, "Failed to get "
6412 "%s IP address: %s",
6413 ifname, strerror(errno));
Pradeep Reddy POTTETI673d85c2016-07-26 19:08:07 +05306414 close(s);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02006415 return -1;
6416 } else {
6417 memcpy(&saddr, &ifr.ifr_addr,
6418 sizeof(struct sockaddr_in));
Peng Xub8fc5cc2017-05-10 17:27:28 -07006419 strlcpy(ip, inet_ntoa(saddr.sin_addr), sizeof(ip));
Jouni Malinencd4e3c32015-10-29 12:39:56 +02006420 }
6421 close(s);
6422
6423 }
6424
6425 snprintf(buf, sizeof(buf), "arping -I %s -s %s %s -c 4", ifname, ip,
6426 ip);
6427 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
6428 if (system(buf) != 0) {
6429 }
6430
6431 return 1;
6432}
6433
6434
6435static int cmd_sta_send_frame_hs2_arpreply(struct sigma_dut *dut,
6436 struct sigma_conn *conn,
6437 struct sigma_cmd *cmd,
6438 const char *ifname)
6439{
6440 char buf[200], addr[20];
6441 char dst[ETH_ALEN], src[ETH_ALEN];
6442 short ethtype = htons(ETH_P_ARP);
6443 char *pos;
6444 int s, res;
6445 const char *val;
6446 struct sockaddr_in taddr;
6447
6448 val = get_param(cmd, "dest");
6449 if (val)
6450 hwaddr_aton(val, (unsigned char *) dst);
6451
6452 val = get_param(cmd, "DestIP");
6453 if (val)
6454 inet_aton(val, &taddr.sin_addr);
6455
6456 if (get_wpa_status(get_station_ifname(), "address", addr,
6457 sizeof(addr)) < 0)
6458 return -2;
6459 hwaddr_aton(addr, (unsigned char *) src);
6460
6461 pos = buf;
6462 *pos++ = 0x00;
6463 *pos++ = 0x01;
6464 *pos++ = 0x08;
6465 *pos++ = 0x00;
6466 *pos++ = 0x06;
6467 *pos++ = 0x04;
6468 *pos++ = 0x00;
6469 *pos++ = 0x02;
6470 memcpy(pos, src, ETH_ALEN);
6471 pos += ETH_ALEN;
6472 memcpy(pos, &taddr.sin_addr, 4);
6473 pos += 4;
6474 memcpy(pos, dst, ETH_ALEN);
6475 pos += ETH_ALEN;
6476 memcpy(pos, &taddr.sin_addr, 4);
6477 pos += 4;
6478
6479 s = open_monitor(get_station_ifname());
6480 if (s < 0) {
6481 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to open "
6482 "monitor socket");
6483 return 0;
6484 }
6485
6486 res = inject_eth_frame(s, buf, pos - buf, ethtype, dst, src);
6487 if (res < 0) {
6488 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to "
6489 "inject frame");
Pradeep Reddy POTTETI673d85c2016-07-26 19:08:07 +05306490 close(s);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02006491 return 0;
6492 }
6493
6494 close(s);
6495
6496 return 1;
6497}
6498
6499
6500static int cmd_sta_send_frame_hs2_dls_req(struct sigma_dut *dut,
6501 struct sigma_conn *conn,
6502 struct sigma_cmd *cmd,
6503 const char *intf, const char *dest)
6504{
6505 char buf[100];
6506
6507 if (if_nametoindex("sigmadut") == 0) {
6508 snprintf(buf, sizeof(buf),
6509 "iw dev %s interface add sigmadut type monitor",
6510 get_station_ifname());
6511 if (system(buf) != 0 ||
6512 if_nametoindex("sigmadut") == 0) {
6513 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to add "
6514 "monitor interface with '%s'", buf);
6515 return -2;
6516 }
6517 }
6518
6519 if (system("ifconfig sigmadut up") != 0) {
6520 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to set "
6521 "monitor interface up");
6522 return -2;
6523 }
6524
6525 return sta_inject_frame(dut, conn, DLS_REQ, UNPROTECTED, dest);
6526}
6527
6528
6529static int cmd_sta_send_frame_hs2(struct sigma_dut *dut,
6530 struct sigma_conn *conn,
6531 struct sigma_cmd *cmd)
6532{
6533 const char *intf = get_param(cmd, "Interface");
6534 const char *dest = get_param(cmd, "Dest");
6535 const char *type = get_param(cmd, "FrameName");
6536 const char *val;
6537 char buf[200], *pos, *end;
6538 int count, count2;
6539
6540 if (type == NULL)
6541 type = get_param(cmd, "Type");
6542
6543 if (intf == NULL || dest == NULL || type == NULL)
6544 return -1;
6545
6546 if (strcasecmp(type, "NeighAdv") == 0)
6547 return cmd_sta_send_frame_hs2_neighadv(dut, conn, cmd, intf);
6548
6549 if (strcasecmp(type, "NeighSolicitReq") == 0)
6550 return cmd_sta_send_frame_hs2_neighsolreq(dut, conn, cmd, intf);
6551
6552 if (strcasecmp(type, "ARPProbe") == 0)
6553 return cmd_sta_send_frame_hs2_arpprobe(dut, conn, cmd, intf);
6554
6555 if (strcasecmp(type, "ARPAnnounce") == 0)
6556 return cmd_sta_send_frame_hs2_arpannounce(dut, conn, cmd, intf);
6557
6558 if (strcasecmp(type, "ARPReply") == 0)
6559 return cmd_sta_send_frame_hs2_arpreply(dut, conn, cmd, intf);
6560
6561 if (strcasecmp(type, "DLS-request") == 0 ||
6562 strcasecmp(type, "DLSrequest") == 0)
6563 return cmd_sta_send_frame_hs2_dls_req(dut, conn, cmd, intf,
6564 dest);
6565
6566 if (strcasecmp(type, "ANQPQuery") != 0 &&
6567 strcasecmp(type, "Query") != 0) {
6568 send_resp(dut, conn, SIGMA_ERROR,
6569 "ErrorCode,Unsupported HS 2.0 send frame type");
6570 return 0;
6571 }
6572
6573 if (sta_scan_ap(dut, intf, dest) < 0) {
6574 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Could not find "
6575 "the requested AP");
6576 return 0;
6577 }
6578
6579 pos = buf;
6580 end = buf + sizeof(buf);
6581 count = 0;
6582 pos += snprintf(pos, end - pos, "ANQP_GET %s ", dest);
6583
6584 val = get_param(cmd, "ANQP_CAP_LIST");
6585 if (val && atoi(val)) {
6586 pos += snprintf(pos, end - pos, "%s257", count > 0 ? "," : "");
6587 count++;
6588 }
6589
6590 val = get_param(cmd, "VENUE_NAME");
6591 if (val && atoi(val)) {
6592 pos += snprintf(pos, end - pos, "%s258", count > 0 ? "," : "");
6593 count++;
6594 }
6595
6596 val = get_param(cmd, "NETWORK_AUTH_TYPE");
6597 if (val && atoi(val)) {
6598 pos += snprintf(pos, end - pos, "%s260", count > 0 ? "," : "");
6599 count++;
6600 }
6601
6602 val = get_param(cmd, "ROAMING_CONS");
6603 if (val && atoi(val)) {
6604 pos += snprintf(pos, end - pos, "%s261", count > 0 ? "," : "");
6605 count++;
6606 }
6607
6608 val = get_param(cmd, "IP_ADDR_TYPE_AVAILABILITY");
6609 if (val && atoi(val)) {
6610 pos += snprintf(pos, end - pos, "%s262", count > 0 ? "," : "");
6611 count++;
6612 }
6613
6614 val = get_param(cmd, "NAI_REALM_LIST");
6615 if (val && atoi(val)) {
6616 pos += snprintf(pos, end - pos, "%s263", count > 0 ? "," : "");
6617 count++;
6618 }
6619
6620 val = get_param(cmd, "3GPP_INFO");
6621 if (val && atoi(val)) {
6622 pos += snprintf(pos, end - pos, "%s264", count > 0 ? "," : "");
6623 count++;
6624 }
6625
6626 val = get_param(cmd, "DOMAIN_LIST");
6627 if (val && atoi(val)) {
6628 pos += snprintf(pos, end - pos, "%s268", count > 0 ? "," : "");
6629 count++;
6630 }
6631
6632 if (count && wpa_command(intf, buf)) {
6633 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,ANQP_GET failed");
6634 return 0;
6635 }
6636
6637 pos = buf;
6638 end = buf + sizeof(buf);
6639 count2 = 0;
6640 pos += snprintf(pos, end - pos, "HS20_ANQP_GET %s ", dest);
6641
6642 val = get_param(cmd, "HS_CAP_LIST");
6643 if (val && atoi(val)) {
6644 pos += snprintf(pos, end - pos, "%s2", count2 > 0 ? "," : "");
6645 count2++;
6646 }
6647
6648 val = get_param(cmd, "OPER_NAME");
6649 if (val && atoi(val)) {
6650 pos += snprintf(pos, end - pos, "%s3", count2 > 0 ? "," : "");
6651 count2++;
6652 }
6653
6654 val = get_param(cmd, "WAN_METRICS");
6655 if (!val)
6656 val = get_param(cmd, "WAN_MAT");
6657 if (!val)
6658 val = get_param(cmd, "WAN_MET");
6659 if (val && atoi(val)) {
6660 pos += snprintf(pos, end - pos, "%s4", count2 > 0 ? "," : "");
6661 count2++;
6662 }
6663
6664 val = get_param(cmd, "CONNECTION_CAPABILITY");
6665 if (val && atoi(val)) {
6666 pos += snprintf(pos, end - pos, "%s5", count2 > 0 ? "," : "");
6667 count2++;
6668 }
6669
6670 val = get_param(cmd, "OP_CLASS");
6671 if (val && atoi(val)) {
6672 pos += snprintf(pos, end - pos, "%s7", count2 > 0 ? "," : "");
6673 count2++;
6674 }
6675
6676 val = get_param(cmd, "OSU_PROVIDER_LIST");
6677 if (val && atoi(val)) {
6678 pos += snprintf(pos, end - pos, "%s8", count2 > 0 ? "," : "");
6679 count2++;
6680 }
6681
6682 if (count && count2) {
6683 sigma_dut_print(dut, DUT_MSG_DEBUG, "Wait before sending out "
6684 "second query");
6685 sleep(1);
6686 }
6687
6688 if (count2 && wpa_command(intf, buf)) {
6689 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,HS20_ANQP_GET "
6690 "failed");
6691 return 0;
6692 }
6693
6694 val = get_param(cmd, "NAI_HOME_REALM_LIST");
6695 if (val) {
6696 if (count || count2) {
6697 sigma_dut_print(dut, DUT_MSG_DEBUG, "Wait before "
6698 "sending out second query");
6699 sleep(1);
6700 }
6701
6702 if (strcmp(val, "1") == 0)
6703 val = "mail.example.com";
6704 snprintf(buf, end - pos,
6705 "HS20_GET_NAI_HOME_REALM_LIST %s realm=%s",
6706 dest, val);
6707 if (wpa_command(intf, buf)) {
6708 send_resp(dut, conn, SIGMA_ERROR,
6709 "ErrorCode,HS20_GET_NAI_HOME_REALM_LIST "
6710 "failed");
6711 return 0;
6712 }
6713 }
6714
6715 val = get_param(cmd, "ICON_REQUEST");
6716 if (val) {
6717 if (count || count2) {
6718 sigma_dut_print(dut, DUT_MSG_DEBUG, "Wait before "
6719 "sending out second query");
6720 sleep(1);
6721 }
6722
6723 snprintf(buf, end - pos,
6724 "HS20_ICON_REQUEST %s %s", dest, val);
6725 if (wpa_command(intf, buf)) {
6726 send_resp(dut, conn, SIGMA_ERROR,
6727 "ErrorCode,HS20_ICON_REQUEST failed");
6728 return 0;
6729 }
6730 }
6731
6732 return 1;
6733}
6734
6735
6736static int ath_sta_send_frame_vht(struct sigma_dut *dut,
6737 struct sigma_conn *conn,
6738 struct sigma_cmd *cmd)
6739{
6740 const char *val;
6741 char *ifname;
6742 char buf[100];
6743 int chwidth, nss;
6744
6745 val = get_param(cmd, "framename");
6746 if (!val)
6747 return -1;
6748 sigma_dut_print(dut, DUT_MSG_DEBUG, "framename is %s", val);
6749
6750 /* Command sequence to generate Op mode notification */
6751 if (val && strcasecmp(val, "Op_md_notif_frm") == 0) {
6752 ifname = get_station_ifname();
6753
6754 /* Disable STBC */
6755 snprintf(buf, sizeof(buf),
6756 "iwpriv %s tx_stbc 0", ifname);
6757 if (system(buf) != 0) {
6758 sigma_dut_print(dut, DUT_MSG_ERROR,
6759 "iwpriv tx_stbc 0 failed!");
6760 }
6761
6762 /* Extract Channel width */
6763 val = get_param(cmd, "Channel_width");
6764 if (val) {
6765 switch (atoi(val)) {
6766 case 20:
6767 chwidth = 0;
6768 break;
6769 case 40:
6770 chwidth = 1;
6771 break;
6772 case 80:
6773 chwidth = 2;
6774 break;
6775 case 160:
6776 chwidth = 3;
6777 break;
6778 default:
6779 chwidth = 2;
6780 break;
6781 }
6782
6783 snprintf(buf, sizeof(buf), "iwpriv %s chwidth %d",
6784 ifname, chwidth);
6785 if (system(buf) != 0) {
6786 sigma_dut_print(dut, DUT_MSG_ERROR,
6787 "iwpriv chwidth failed!");
6788 }
6789 }
6790
6791 /* Extract NSS */
6792 val = get_param(cmd, "NSS");
6793 if (val) {
6794 switch (atoi(val)) {
6795 case 1:
6796 nss = 1;
6797 break;
6798 case 2:
6799 nss = 3;
6800 break;
6801 case 3:
6802 nss = 7;
6803 break;
6804 default:
6805 /* We do not support NSS > 3 */
6806 nss = 3;
6807 break;
6808 }
6809 snprintf(buf, sizeof(buf),
6810 "iwpriv %s rxchainmask %d", ifname, nss);
6811 if (system(buf) != 0) {
6812 sigma_dut_print(dut, DUT_MSG_ERROR,
6813 "iwpriv rxchainmask failed!");
6814 }
6815 }
6816
6817 /* Opmode notify */
6818 snprintf(buf, sizeof(buf), "iwpriv %s opmode_notify 1", ifname);
6819 if (system(buf) != 0) {
6820 sigma_dut_print(dut, DUT_MSG_ERROR,
6821 "iwpriv opmode_notify failed!");
6822 } else {
6823 sigma_dut_print(dut, DUT_MSG_INFO,
6824 "Sent out the notify frame!");
6825 }
6826 }
6827
6828 return 1;
6829}
6830
6831
6832static int cmd_sta_send_frame_vht(struct sigma_dut *dut,
6833 struct sigma_conn *conn,
6834 struct sigma_cmd *cmd)
6835{
6836 switch (get_driver_type()) {
6837 case DRIVER_ATHEROS:
6838 return ath_sta_send_frame_vht(dut, conn, cmd);
6839 default:
6840 send_resp(dut, conn, SIGMA_ERROR,
6841 "errorCode,Unsupported sta_set_frame(VHT) with the current driver");
6842 return 0;
6843 }
6844}
6845
6846
Lior David0fe101e2017-03-09 16:09:50 +02006847#ifdef __linux__
6848int wil6210_send_frame_60g(struct sigma_dut *dut, struct sigma_conn *conn,
6849 struct sigma_cmd *cmd)
6850{
6851 const char *frame_name = get_param(cmd, "framename");
6852 const char *mac = get_param(cmd, "dest_mac");
6853
6854 if (!frame_name || !mac) {
6855 sigma_dut_print(dut, DUT_MSG_ERROR,
6856 "framename and dest_mac must be provided");
6857 return -1;
6858 }
6859
6860 if (strcasecmp(frame_name, "brp") == 0) {
6861 const char *l_rx = get_param(cmd, "L-RX");
6862 int l_rx_i;
6863
6864 if (!l_rx) {
6865 sigma_dut_print(dut, DUT_MSG_ERROR,
6866 "L-RX must be provided");
6867 return -1;
6868 }
6869 l_rx_i = atoi(l_rx);
6870
6871 sigma_dut_print(dut, DUT_MSG_INFO,
6872 "dev_send_frame: BRP-RX, dest_mac %s, L-RX %s",
6873 mac, l_rx);
6874 if (l_rx_i != 16) {
6875 sigma_dut_print(dut, DUT_MSG_ERROR,
6876 "unsupported L-RX: %s", l_rx);
6877 return -1;
6878 }
6879
6880 if (wil6210_send_brp_rx(dut, mac, l_rx_i))
6881 return -1;
6882 } else if (strcasecmp(frame_name, "ssw") == 0) {
6883 sigma_dut_print(dut, DUT_MSG_INFO,
6884 "dev_send_frame: SLS, dest_mac %s", mac);
6885 if (wil6210_send_sls(dut, mac))
6886 return -1;
6887 } else {
6888 sigma_dut_print(dut, DUT_MSG_ERROR,
6889 "unsupported frame type: %s", frame_name);
6890 return -1;
6891 }
6892
6893 return 1;
6894}
6895#endif /* __linux__ */
6896
6897
6898static int cmd_sta_send_frame_60g(struct sigma_dut *dut,
6899 struct sigma_conn *conn,
6900 struct sigma_cmd *cmd)
6901{
6902 switch (get_driver_type()) {
6903#ifdef __linux__
6904 case DRIVER_WIL6210:
6905 return wil6210_send_frame_60g(dut, conn, cmd);
6906#endif /* __linux__ */
6907 default:
6908 send_resp(dut, conn, SIGMA_ERROR,
6909 "errorCode,Unsupported sta_set_frame(60G) with the current driver");
6910 return 0;
6911 }
6912}
6913
6914
Ashwini Patildb59b3c2017-04-13 15:19:23 +05306915static int mbo_send_anqp_query(struct sigma_dut *dut, struct sigma_conn *conn,
6916 const char *intf, struct sigma_cmd *cmd)
6917{
6918 const char *val, *addr;
6919 char buf[100];
6920
6921 addr = get_param(cmd, "DestMac");
6922 if (!addr) {
6923 send_resp(dut, conn, SIGMA_INVALID,
6924 "ErrorCode,AP MAC address is missing");
6925 return 0;
6926 }
6927
6928 val = get_param(cmd, "ANQPQuery_ID");
6929 if (!val) {
6930 send_resp(dut, conn, SIGMA_INVALID,
6931 "ErrorCode,Missing ANQPQuery_ID");
6932 return 0;
6933 }
6934
6935 if (strcasecmp(val, "NeighborReportReq") == 0) {
6936 snprintf(buf, sizeof(buf), "ANQP_GET %s 272", addr);
6937 } else if (strcasecmp(val, "QueryListWithCellPref") == 0) {
6938 snprintf(buf, sizeof(buf), "ANQP_GET %s 272,mbo:2", addr);
6939 } else {
6940 sigma_dut_print(dut, DUT_MSG_ERROR, "Invalid ANQPQuery_ID: %s",
6941 val);
6942 send_resp(dut, conn, SIGMA_INVALID,
6943 "ErrorCode,Invalid ANQPQuery_ID");
6944 return 0;
6945 }
6946
Ashwini Patild174f2c2017-04-13 16:49:46 +05306947 /* Set gas_address3 field to IEEE 802.11-2012 standard compliant form
6948 * (Address3 = Wildcard BSSID when sent to not-associated AP;
6949 * if associated, AP BSSID).
6950 */
6951 if (wpa_command(intf, "SET gas_address3 1") < 0) {
6952 send_resp(dut, conn, SIGMA_ERROR,
6953 "ErrorCode,Failed to set gas_address3");
6954 return 0;
6955 }
6956
Ashwini Patildb59b3c2017-04-13 15:19:23 +05306957 if (wpa_command(intf, buf) < 0) {
6958 send_resp(dut, conn, SIGMA_ERROR,
6959 "ErrorCode,Failed to send ANQP query");
6960 return 0;
6961 }
6962
6963 return 1;
6964}
6965
6966
6967static int mbo_cmd_sta_send_frame(struct sigma_dut *dut,
6968 struct sigma_conn *conn,
6969 const char *intf,
6970 struct sigma_cmd *cmd)
6971{
6972 const char *val = get_param(cmd, "FrameName");
6973
6974 if (val && strcasecmp(val, "ANQPQuery") == 0)
6975 return mbo_send_anqp_query(dut, conn, intf, cmd);
6976
6977 return 2;
6978}
6979
6980
Jouni Malinencd4e3c32015-10-29 12:39:56 +02006981int cmd_sta_send_frame(struct sigma_dut *dut, struct sigma_conn *conn,
6982 struct sigma_cmd *cmd)
6983{
6984 const char *intf = get_param(cmd, "Interface");
6985 const char *val;
6986 enum send_frame_type frame;
6987 enum send_frame_protection protected;
6988 char buf[100];
6989 unsigned char addr[ETH_ALEN];
6990 int res;
6991
6992 val = get_param(cmd, "program");
6993 if (val == NULL)
6994 val = get_param(cmd, "frame");
6995 if (val && strcasecmp(val, "TDLS") == 0)
6996 return cmd_sta_send_frame_tdls(dut, conn, cmd);
6997 if (val && (strcasecmp(val, "HS2") == 0 ||
6998 strcasecmp(val, "HS2-R2") == 0))
6999 return cmd_sta_send_frame_hs2(dut, conn, cmd);
7000 if (val && strcasecmp(val, "VHT") == 0)
7001 return cmd_sta_send_frame_vht(dut, conn, cmd);
priyadharshini gowthamand66913a2016-07-29 15:11:17 -07007002 if (val && strcasecmp(val, "LOC") == 0)
7003 return loc_cmd_sta_send_frame(dut, conn, cmd);
Lior David0fe101e2017-03-09 16:09:50 +02007004 if (val && strcasecmp(val, "60GHz") == 0)
7005 return cmd_sta_send_frame_60g(dut, conn, cmd);
Ashwini Patildb59b3c2017-04-13 15:19:23 +05307006 if (val && strcasecmp(val, "MBO") == 0) {
7007 res = mbo_cmd_sta_send_frame(dut, conn, intf, cmd);
7008 if (res != 2)
7009 return res;
7010 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007011
7012 val = get_param(cmd, "TD_DISC");
7013 if (val) {
7014 if (hwaddr_aton(val, addr) < 0)
7015 return -1;
7016 snprintf(buf, sizeof(buf), "TDLS_DISCOVER %s", val);
7017 if (wpa_command(intf, buf) < 0) {
7018 send_resp(dut, conn, SIGMA_ERROR,
7019 "ErrorCode,Failed to send TDLS discovery");
7020 return 0;
7021 }
7022 return 1;
7023 }
7024
7025 val = get_param(cmd, "TD_Setup");
7026 if (val) {
7027 if (hwaddr_aton(val, addr) < 0)
7028 return -1;
7029 snprintf(buf, sizeof(buf), "TDLS_SETUP %s", val);
7030 if (wpa_command(intf, buf) < 0) {
7031 send_resp(dut, conn, SIGMA_ERROR,
7032 "ErrorCode,Failed to start TDLS setup");
7033 return 0;
7034 }
7035 return 1;
7036 }
7037
7038 val = get_param(cmd, "TD_TearDown");
7039 if (val) {
7040 if (hwaddr_aton(val, addr) < 0)
7041 return -1;
7042 snprintf(buf, sizeof(buf), "TDLS_TEARDOWN %s", val);
7043 if (wpa_command(intf, buf) < 0) {
7044 send_resp(dut, conn, SIGMA_ERROR,
7045 "ErrorCode,Failed to tear down TDLS link");
7046 return 0;
7047 }
7048 return 1;
7049 }
7050
7051 val = get_param(cmd, "TD_ChannelSwitch");
7052 if (val) {
7053 /* TODO */
7054 send_resp(dut, conn, SIGMA_ERROR,
7055 "ErrorCode,TD_ChannelSwitch not yet supported");
7056 return 0;
7057 }
7058
7059 val = get_param(cmd, "TD_NF");
7060 if (val) {
7061 /* TODO */
7062 send_resp(dut, conn, SIGMA_ERROR,
7063 "ErrorCode,TD_NF not yet supported");
7064 return 0;
7065 }
7066
7067 val = get_param(cmd, "PMFFrameType");
7068 if (val == NULL)
7069 val = get_param(cmd, "FrameName");
7070 if (val == NULL)
7071 val = get_param(cmd, "Type");
7072 if (val == NULL)
7073 return -1;
7074 if (strcasecmp(val, "disassoc") == 0)
7075 frame = DISASSOC;
7076 else if (strcasecmp(val, "deauth") == 0)
7077 frame = DEAUTH;
7078 else if (strcasecmp(val, "saquery") == 0)
7079 frame = SAQUERY;
7080 else if (strcasecmp(val, "auth") == 0)
7081 frame = AUTH;
7082 else if (strcasecmp(val, "assocreq") == 0)
7083 frame = ASSOCREQ;
7084 else if (strcasecmp(val, "reassocreq") == 0)
7085 frame = REASSOCREQ;
7086 else if (strcasecmp(val, "neigreq") == 0) {
7087 sigma_dut_print(dut, DUT_MSG_INFO, "Got neighbor request");
7088
7089 val = get_param(cmd, "ssid");
7090 if (val == NULL)
7091 return -1;
7092
7093 res = send_neighbor_request(dut, intf, val);
7094 if (res) {
7095 send_resp(dut, conn, SIGMA_ERROR, "errorCode,"
7096 "Failed to send neighbor report request");
7097 return 0;
7098 }
7099
7100 return 1;
Ashwini Patil5acd7382017-04-13 15:55:04 +05307101 } else if (strcasecmp(val, "transmgmtquery") == 0 ||
7102 strcasecmp(val, "BTMQuery") == 0) {
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007103 sigma_dut_print(dut, DUT_MSG_DEBUG,
7104 "Got Transition Management Query");
7105
Ashwini Patil5acd7382017-04-13 15:55:04 +05307106 res = send_trans_mgmt_query(dut, intf, cmd);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007107 if (res) {
7108 send_resp(dut, conn, SIGMA_ERROR, "errorCode,"
7109 "Failed to send Transition Management Query");
7110 return 0;
7111 }
7112
7113 return 1;
7114 } else {
7115 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Unsupported "
7116 "PMFFrameType");
7117 return 0;
7118 }
7119
7120 val = get_param(cmd, "PMFProtected");
7121 if (val == NULL)
7122 val = get_param(cmd, "Protected");
7123 if (val == NULL)
7124 return -1;
7125 if (strcasecmp(val, "Correct-key") == 0 ||
7126 strcasecmp(val, "CorrectKey") == 0)
7127 protected = CORRECT_KEY;
7128 else if (strcasecmp(val, "IncorrectKey") == 0)
7129 protected = INCORRECT_KEY;
7130 else if (strcasecmp(val, "Unprotected") == 0)
7131 protected = UNPROTECTED;
7132 else {
7133 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Unsupported "
7134 "PMFProtected");
7135 return 0;
7136 }
7137
7138 if (protected != UNPROTECTED &&
7139 (frame == AUTH || frame == ASSOCREQ || frame == REASSOCREQ)) {
7140 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Impossible "
7141 "PMFProtected for auth/assocreq/reassocreq");
7142 return 0;
7143 }
7144
7145 if (if_nametoindex("sigmadut") == 0) {
7146 snprintf(buf, sizeof(buf),
7147 "iw dev %s interface add sigmadut type monitor",
7148 get_station_ifname());
7149 if (system(buf) != 0 ||
7150 if_nametoindex("sigmadut") == 0) {
7151 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to add "
7152 "monitor interface with '%s'", buf);
7153 return -2;
7154 }
7155 }
7156
7157 if (system("ifconfig sigmadut up") != 0) {
7158 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to set "
7159 "monitor interface up");
7160 return -2;
7161 }
7162
7163 return sta_inject_frame(dut, conn, frame, protected, NULL);
7164}
7165
7166
7167static int cmd_sta_set_parameter_hs2(struct sigma_dut *dut,
7168 struct sigma_conn *conn,
7169 struct sigma_cmd *cmd,
7170 const char *ifname)
7171{
7172 char buf[200];
7173 const char *val;
7174
7175 val = get_param(cmd, "ClearARP");
7176 if (val && atoi(val) == 1) {
7177 snprintf(buf, sizeof(buf), "ip neigh flush dev %s", ifname);
7178 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
7179 if (system(buf) != 0) {
7180 send_resp(dut, conn, SIGMA_ERROR,
7181 "errorCode,Failed to clear ARP cache");
7182 return 0;
7183 }
7184 }
7185
7186 return 1;
7187}
7188
7189
7190int cmd_sta_set_parameter(struct sigma_dut *dut, struct sigma_conn *conn,
7191 struct sigma_cmd *cmd)
7192{
7193 const char *intf = get_param(cmd, "Interface");
7194 const char *val;
7195
7196 if (intf == NULL)
7197 return -1;
7198
7199 val = get_param(cmd, "program");
7200 if (val && (strcasecmp(val, "HS2") == 0 ||
7201 strcasecmp(val, "HS2-R2") == 0))
7202 return cmd_sta_set_parameter_hs2(dut, conn, cmd, intf);
7203
7204 return -1;
7205}
7206
7207
7208static int cmd_sta_set_macaddr(struct sigma_dut *dut, struct sigma_conn *conn,
7209 struct sigma_cmd *cmd)
7210{
7211 const char *intf = get_param(cmd, "Interface");
7212 const char *mac = get_param(cmd, "MAC");
7213
7214 if (intf == NULL || mac == NULL)
7215 return -1;
7216
7217 sigma_dut_print(dut, DUT_MSG_INFO, "Change local MAC address for "
7218 "interface %s to %s", intf, mac);
7219
7220 if (dut->set_macaddr) {
7221 char buf[128];
7222 int res;
7223 if (strcasecmp(mac, "default") == 0) {
7224 res = snprintf(buf, sizeof(buf), "%s",
7225 dut->set_macaddr);
7226 dut->tmp_mac_addr = 0;
7227 } else {
7228 res = snprintf(buf, sizeof(buf), "%s %s",
7229 dut->set_macaddr, mac);
7230 dut->tmp_mac_addr = 1;
7231 }
7232 if (res < 0 || res >= (int) sizeof(buf))
7233 return -1;
7234 if (system(buf) != 0) {
7235 send_resp(dut, conn, SIGMA_ERROR,
7236 "errorCode,Failed to set MAC "
7237 "address");
7238 return 0;
7239 }
7240 return 1;
7241 }
7242
7243 if (strcasecmp(mac, "default") == 0)
7244 return 1;
7245
7246 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Unsupported "
7247 "command");
7248 return 0;
7249}
7250
7251
7252static int iwpriv_tdlsoffchnmode(struct sigma_dut *dut,
7253 struct sigma_conn *conn, const char *intf,
7254 int val)
7255{
7256 char buf[200];
7257 int res;
7258
7259 res = snprintf(buf, sizeof(buf), "iwpriv %s tdlsoffchnmode %d",
7260 intf, val);
7261 if (res < 0 || res >= (int) sizeof(buf))
7262 return -1;
7263 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
7264 if (system(buf) != 0) {
7265 send_resp(dut, conn, SIGMA_ERROR,
7266 "errorCode,Failed to configure offchannel mode");
7267 return 0;
7268 }
7269
7270 return 1;
7271}
7272
7273
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007274static int off_chan_val(enum sec_ch_offset off)
7275{
7276 switch (off) {
7277 case SEC_CH_NO:
7278 return 0;
7279 case SEC_CH_40ABOVE:
7280 return 40;
7281 case SEC_CH_40BELOW:
7282 return -40;
7283 }
7284
7285 return 0;
7286}
7287
7288
7289static int iwpriv_set_offchan(struct sigma_dut *dut, struct sigma_conn *conn,
7290 const char *intf, int off_ch_num,
7291 enum sec_ch_offset sec)
7292{
7293 char buf[200];
7294 int res;
7295
7296 res = snprintf(buf, sizeof(buf), "iwpriv %s tdlsoffchan %d",
7297 intf, off_ch_num);
7298 if (res < 0 || res >= (int) sizeof(buf))
7299 return -1;
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 set offchan");
7304 return 0;
7305 }
7306
7307 res = snprintf(buf, sizeof(buf), "iwpriv %s tdlsecchnoffst %d",
7308 intf, off_chan_val(sec));
7309 if (res < 0 || res >= (int) sizeof(buf))
7310 return -1;
7311 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
7312 if (system(buf) != 0) {
7313 send_resp(dut, conn, SIGMA_ERROR,
7314 "errorCode,Failed to set sec chan offset");
7315 return 0;
7316 }
7317
7318 return 1;
7319}
7320
7321
7322static int tdls_set_offchannel_offset(struct sigma_dut *dut,
7323 struct sigma_conn *conn,
7324 const char *intf, int off_ch_num,
7325 enum sec_ch_offset sec)
7326{
7327 char buf[200];
7328 int res;
7329
7330 res = snprintf(buf, sizeof(buf), "DRIVER TDLSOFFCHANNEL %d",
7331 off_ch_num);
7332 if (res < 0 || res >= (int) sizeof(buf))
7333 return -1;
7334 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
7335
7336 if (wpa_command(intf, buf) < 0) {
7337 send_resp(dut, conn, SIGMA_ERROR,
7338 "ErrorCode,Failed to set offchan");
7339 return 0;
7340 }
7341 res = snprintf(buf, sizeof(buf), "DRIVER TDLSSECONDARYCHANNELOFFSET %d",
7342 off_chan_val(sec));
7343 if (res < 0 || res >= (int) sizeof(buf))
7344 return -1;
7345
7346 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
7347
7348 if (wpa_command(intf, buf) < 0) {
7349 send_resp(dut, conn, SIGMA_ERROR,
7350 "ErrorCode,Failed to set sec chan offset");
7351 return 0;
7352 }
7353
7354 return 1;
7355}
7356
7357
7358static int tdls_set_offchannel_mode(struct sigma_dut *dut,
7359 struct sigma_conn *conn,
7360 const char *intf, int val)
7361{
7362 char buf[200];
7363 int res;
7364
7365 res = snprintf(buf, sizeof(buf), "DRIVER TDLSOFFCHANNELMODE %d",
7366 val);
7367 if (res < 0 || res >= (int) sizeof(buf))
7368 return -1;
7369 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
7370
7371 if (wpa_command(intf, buf) < 0) {
7372 send_resp(dut, conn, SIGMA_ERROR,
7373 "ErrorCode,Failed to configure offchannel mode");
7374 return 0;
7375 }
7376
7377 return 1;
7378}
7379
7380
7381static int cmd_sta_set_rfeature_tdls(const char *intf, struct sigma_dut *dut,
7382 struct sigma_conn *conn,
7383 struct sigma_cmd *cmd)
7384{
7385 const char *val;
7386 enum {
7387 CHSM_NOT_SET,
7388 CHSM_ENABLE,
7389 CHSM_DISABLE,
7390 CHSM_REJREQ,
7391 CHSM_UNSOLRESP
7392 } chsm = CHSM_NOT_SET;
7393 int off_ch_num = -1;
7394 enum sec_ch_offset sec_ch = SEC_CH_NO;
7395 int res;
7396
7397 val = get_param(cmd, "Uapsd");
7398 if (val) {
7399 char buf[100];
7400 if (strcasecmp(val, "Enable") == 0)
7401 snprintf(buf, sizeof(buf), "SET ps 99");
7402 else if (strcasecmp(val, "Disable") == 0)
7403 snprintf(buf, sizeof(buf), "SET ps 98");
7404 else {
7405 send_resp(dut, conn, SIGMA_ERROR, "errorCode,"
7406 "Unsupported uapsd parameter value");
7407 return 0;
7408 }
7409 if (wpa_command(intf, buf)) {
7410 send_resp(dut, conn, SIGMA_ERROR,
7411 "ErrorCode,Failed to change U-APSD "
7412 "powersave mode");
7413 return 0;
7414 }
7415 }
7416
7417 val = get_param(cmd, "TPKTIMER");
7418 if (val && strcasecmp(val, "DISABLE") == 0) {
7419 if (wpa_command(intf, "SET tdls_testing 0x100")) {
7420 send_resp(dut, conn, SIGMA_ERROR,
7421 "ErrorCode,Failed to enable no TPK "
7422 "expiration test mode");
7423 return 0;
7424 }
7425 dut->no_tpk_expiration = 1;
7426 }
7427
7428 val = get_param(cmd, "ChSwitchMode");
7429 if (val) {
7430 if (strcasecmp(val, "Enable") == 0 ||
7431 strcasecmp(val, "Initiate") == 0)
7432 chsm = CHSM_ENABLE;
7433 else if (strcasecmp(val, "Disable") == 0 ||
7434 strcasecmp(val, "passive") == 0)
7435 chsm = CHSM_DISABLE;
7436 else if (strcasecmp(val, "RejReq") == 0)
7437 chsm = CHSM_REJREQ;
7438 else if (strcasecmp(val, "UnSolResp") == 0)
7439 chsm = CHSM_UNSOLRESP;
7440 else {
7441 send_resp(dut, conn, SIGMA_ERROR,
7442 "ErrorCode,Unknown ChSwitchMode value");
7443 return 0;
7444 }
7445 }
7446
7447 val = get_param(cmd, "OffChNum");
7448 if (val) {
7449 off_ch_num = atoi(val);
7450 if (off_ch_num == 0) {
7451 send_resp(dut, conn, SIGMA_ERROR,
7452 "ErrorCode,Invalid OffChNum");
7453 return 0;
7454 }
7455 }
7456
7457 val = get_param(cmd, "SecChOffset");
7458 if (val) {
7459 if (strcmp(val, "20") == 0)
7460 sec_ch = SEC_CH_NO;
7461 else if (strcasecmp(val, "40above") == 0)
7462 sec_ch = SEC_CH_40ABOVE;
7463 else if (strcasecmp(val, "40below") == 0)
7464 sec_ch = SEC_CH_40BELOW;
7465 else {
7466 send_resp(dut, conn, SIGMA_ERROR,
7467 "ErrorCode,Unknown SecChOffset value");
7468 return 0;
7469 }
7470 }
7471
7472 if (chsm == CHSM_NOT_SET) {
7473 /* no offchannel changes requested */
7474 return 1;
7475 }
7476
7477 if (strcmp(intf, get_main_ifname()) != 0 &&
7478 strcmp(intf, get_station_ifname()) != 0) {
7479 send_resp(dut, conn, SIGMA_ERROR,
7480 "ErrorCode,Unknown interface");
7481 return 0;
7482 }
7483
7484 switch (chsm) {
7485 case CHSM_NOT_SET:
Jouni Malinen280f5ba2016-08-29 21:33:10 +03007486 res = 1;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007487 break;
7488 case CHSM_ENABLE:
7489 if (off_ch_num < 0) {
7490 send_resp(dut, conn, SIGMA_ERROR,
7491 "ErrorCode,Missing OffChNum argument");
7492 return 0;
7493 }
7494 if (wifi_chip_type == DRIVER_WCN) {
7495 res = tdls_set_offchannel_offset(dut, conn, intf,
7496 off_ch_num, sec_ch);
7497 } else {
7498 res = iwpriv_set_offchan(dut, conn, intf, off_ch_num,
7499 sec_ch);
7500 }
7501 if (res != 1)
7502 return res;
7503 if (wifi_chip_type == DRIVER_WCN)
7504 res = tdls_set_offchannel_mode(dut, conn, intf, 1);
7505 else
7506 res = iwpriv_tdlsoffchnmode(dut, conn, intf, 1);
7507 break;
7508 case CHSM_DISABLE:
7509 if (wifi_chip_type == DRIVER_WCN)
7510 res = tdls_set_offchannel_mode(dut, conn, intf, 2);
7511 else
7512 res = iwpriv_tdlsoffchnmode(dut, conn, intf, 2);
7513 break;
7514 case CHSM_REJREQ:
7515 if (wifi_chip_type == DRIVER_WCN)
7516 res = tdls_set_offchannel_mode(dut, conn, intf, 3);
7517 else
7518 res = iwpriv_tdlsoffchnmode(dut, conn, intf, 3);
7519 break;
7520 case CHSM_UNSOLRESP:
7521 if (off_ch_num < 0) {
7522 send_resp(dut, conn, SIGMA_ERROR,
7523 "ErrorCode,Missing OffChNum argument");
7524 return 0;
7525 }
7526 if (wifi_chip_type == DRIVER_WCN) {
7527 res = tdls_set_offchannel_offset(dut, conn, intf,
7528 off_ch_num, sec_ch);
7529 } else {
7530 res = iwpriv_set_offchan(dut, conn, intf, off_ch_num,
7531 sec_ch);
7532 }
7533 if (res != 1)
7534 return res;
7535 if (wifi_chip_type == DRIVER_WCN)
7536 res = tdls_set_offchannel_mode(dut, conn, intf, 4);
7537 else
7538 res = iwpriv_tdlsoffchnmode(dut, conn, intf, 4);
7539 break;
7540 }
7541
7542 return res;
7543}
7544
7545
7546static int ath_sta_set_rfeature_vht(const char *intf, struct sigma_dut *dut,
7547 struct sigma_conn *conn,
7548 struct sigma_cmd *cmd)
7549{
7550 const char *val;
7551 char *token, *result;
7552
priyadharshini gowthamane5e25172015-12-08 14:53:48 -08007553 novap_reset(dut, intf);
7554
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007555 val = get_param(cmd, "nss_mcs_opt");
7556 if (val) {
7557 /* String (nss_operating_mode; mcs_operating_mode) */
7558 int nss, mcs;
7559 char buf[50];
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05307560 char *saveptr;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007561
7562 token = strdup(val);
7563 if (!token)
7564 return 0;
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05307565 result = strtok_r(token, ";", &saveptr);
Pradeep Reddy POTTETI41b8c542016-06-15 16:09:46 +05307566 if (!result) {
7567 sigma_dut_print(dut, DUT_MSG_ERROR,
7568 "VHT NSS not specified");
7569 goto failed;
7570 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007571 if (strcasecmp(result, "def") != 0) {
7572 nss = atoi(result);
7573 if (nss == 4)
7574 ath_disable_txbf(dut, intf);
7575 snprintf(buf, sizeof(buf), "iwpriv %s nss %d",
7576 intf, nss);
7577 if (system(buf) != 0) {
7578 sigma_dut_print(dut, DUT_MSG_ERROR,
7579 "iwpriv nss failed");
7580 goto failed;
7581 }
7582 }
7583
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05307584 result = strtok_r(NULL, ";", &saveptr);
Pradeep Reddy POTTETI41b8c542016-06-15 16:09:46 +05307585 if (!result) {
7586 sigma_dut_print(dut, DUT_MSG_ERROR,
7587 "VHT MCS not specified");
7588 goto failed;
7589 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007590 if (strcasecmp(result, "def") == 0) {
7591 snprintf(buf, sizeof(buf), "iwpriv %s set11NRates 0",
7592 intf);
7593 if (system(buf) != 0) {
7594 sigma_dut_print(dut, DUT_MSG_ERROR,
7595 "iwpriv set11NRates failed");
7596 goto failed;
7597 }
7598
7599 } else {
7600 mcs = atoi(result);
7601 snprintf(buf, sizeof(buf), "iwpriv %s vhtmcs %d",
7602 intf, mcs);
7603 if (system(buf) != 0) {
7604 sigma_dut_print(dut, DUT_MSG_ERROR,
7605 "iwpriv vhtmcs failed");
7606 goto failed;
7607 }
7608 }
7609 /* Channel width gets messed up, fix this */
7610 snprintf(buf, sizeof(buf), "iwpriv %s chwidth %d",
7611 intf, dut->chwidth);
7612 if (system(buf) != 0) {
7613 sigma_dut_print(dut, DUT_MSG_ERROR,
7614 "iwpriv chwidth failed");
7615 }
7616 }
7617
7618 return 1;
7619failed:
7620 free(token);
7621 return 0;
7622}
7623
7624
7625static int cmd_sta_set_rfeature_vht(const char *intf, struct sigma_dut *dut,
7626 struct sigma_conn *conn,
7627 struct sigma_cmd *cmd)
7628{
7629 switch (get_driver_type()) {
7630 case DRIVER_ATHEROS:
7631 return ath_sta_set_rfeature_vht(intf, dut, conn, cmd);
7632 default:
7633 send_resp(dut, conn, SIGMA_ERROR,
7634 "errorCode,Unsupported sta_set_rfeature(VHT) with the current driver");
7635 return 0;
7636 }
7637}
7638
7639
Ashwini Patil5acd7382017-04-13 15:55:04 +05307640static int btm_query_candidate_list(struct sigma_dut *dut,
7641 struct sigma_conn *conn,
7642 struct sigma_cmd *cmd)
7643{
7644 const char *bssid, *info, *op_class, *ch, *phy_type, *pref;
7645 int len, ret;
7646 char buf[10];
7647
7648 /*
7649 * Neighbor Report elements format:
7650 * neighbor=<BSSID>,<BSSID Information>,<Operating Class>,
7651 * <Channel Number>,<PHY Type>[,<hexdump of Optional Subelements>]
7652 * eg: neighbor=aa:bb:cc:dd:ee:ff,17,81,6,1,030101
7653 */
7654
7655 bssid = get_param(cmd, "Nebor_BSSID");
7656 if (!bssid) {
7657 send_resp(dut, conn, SIGMA_INVALID,
7658 "errorCode,Nebor_BSSID is missing");
7659 return 0;
7660 }
7661
7662 info = get_param(cmd, "Nebor_Bssid_Info");
7663 if (!info) {
7664 sigma_dut_print(dut, DUT_MSG_INFO,
7665 "Using default value for Nebor_Bssid_Info: %s",
7666 DEFAULT_NEIGHBOR_BSSID_INFO);
7667 info = DEFAULT_NEIGHBOR_BSSID_INFO;
7668 }
7669
7670 op_class = get_param(cmd, "Nebor_Op_Class");
7671 if (!op_class) {
7672 send_resp(dut, conn, SIGMA_INVALID,
7673 "errorCode,Nebor_Op_Class is missing");
7674 return 0;
7675 }
7676
7677 ch = get_param(cmd, "Nebor_Op_Ch");
7678 if (!ch) {
7679 send_resp(dut, conn, SIGMA_INVALID,
7680 "errorCode,Nebor_Op_Ch is missing");
7681 return 0;
7682 }
7683
7684 phy_type = get_param(cmd, "Nebor_Phy_Type");
7685 if (!phy_type) {
7686 sigma_dut_print(dut, DUT_MSG_INFO,
7687 "Using default value for Nebor_Phy_Type: %s",
7688 DEFAULT_NEIGHBOR_PHY_TYPE);
7689 phy_type = DEFAULT_NEIGHBOR_PHY_TYPE;
7690 }
7691
7692 /* Parse optional subelements */
7693 buf[0] = '\0';
7694 pref = get_param(cmd, "Nebor_Pref");
7695 if (pref) {
7696 /* hexdump for preferrence subelement */
7697 ret = snprintf(buf, sizeof(buf), ",0301%02x", atoi(pref));
7698 if (ret < 0 || ret >= (int) sizeof(buf)) {
7699 sigma_dut_print(dut, DUT_MSG_ERROR,
7700 "snprintf failed for optional subelement ret: %d",
7701 ret);
7702 send_resp(dut, conn, SIGMA_ERROR,
7703 "errorCode,snprintf failed for subelement");
7704 return 0;
7705 }
7706 }
7707
7708 if (!dut->btm_query_cand_list) {
7709 dut->btm_query_cand_list = calloc(1, NEIGHBOR_REPORT_SIZE);
7710 if (!dut->btm_query_cand_list) {
7711 send_resp(dut, conn, SIGMA_ERROR,
7712 "errorCode,Failed to allocate memory for btm_query_cand_list");
7713 return 0;
7714 }
7715 }
7716
7717 len = strlen(dut->btm_query_cand_list);
7718 ret = snprintf(dut->btm_query_cand_list + len,
7719 NEIGHBOR_REPORT_SIZE - len, " neighbor=%s,%s,%s,%s,%s%s",
7720 bssid, info, op_class, ch, phy_type, buf);
7721 if (ret < 0 || ret >= NEIGHBOR_REPORT_SIZE - len) {
7722 sigma_dut_print(dut, DUT_MSG_ERROR,
7723 "snprintf failed for neighbor report list ret: %d",
7724 ret);
7725 send_resp(dut, conn, SIGMA_ERROR,
7726 "errorCode,snprintf failed for neighbor report");
7727 free(dut->btm_query_cand_list);
7728 dut->btm_query_cand_list = NULL;
7729 return 0;
7730 }
7731
7732 return 1;
7733}
7734
7735
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007736static int cmd_sta_set_rfeature(struct sigma_dut *dut, struct sigma_conn *conn,
7737 struct sigma_cmd *cmd)
7738{
7739 const char *intf = get_param(cmd, "Interface");
7740 const char *prog = get_param(cmd, "Prog");
Ashwini Patil68d02cd2017-01-10 15:39:16 +05307741 const char *val;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007742
7743 if (intf == NULL || prog == NULL)
7744 return -1;
7745
Ashwini Patil5acd7382017-04-13 15:55:04 +05307746 /* BSS Transition candidate list for BTM query */
7747 val = get_param(cmd, "Nebor_BSSID");
7748 if (val && btm_query_candidate_list(dut, conn, cmd) == 0)
7749 return 0;
7750
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007751 if (strcasecmp(prog, "TDLS") == 0)
7752 return cmd_sta_set_rfeature_tdls(intf, dut, conn, cmd);
7753
7754 if (strcasecmp(prog, "VHT") == 0)
7755 return cmd_sta_set_rfeature_vht(intf, dut, conn, cmd);
7756
Ashwini Patil68d02cd2017-01-10 15:39:16 +05307757 if (strcasecmp(prog, "MBO") == 0) {
7758 val = get_param(cmd, "Cellular_Data_Cap");
7759 if (val &&
7760 mbo_set_cellular_data_capa(dut, conn, intf, atoi(val)) == 0)
7761 return 0;
Ashwini Patil00402582017-04-13 12:29:39 +05307762
7763 val = get_param(cmd, "Ch_Pref");
7764 if (val && mbo_set_non_pref_ch_list(dut, conn, intf, cmd) == 0)
7765 return 0;
7766
Ashwini Patil68d02cd2017-01-10 15:39:16 +05307767 return 1;
7768 }
7769
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007770 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Unsupported Prog");
7771 return 0;
7772}
7773
7774
7775static int cmd_sta_set_radio(struct sigma_dut *dut, struct sigma_conn *conn,
7776 struct sigma_cmd *cmd)
7777{
7778 const char *intf = get_param(cmd, "Interface");
7779 const char *mode = get_param(cmd, "Mode");
7780 int res;
7781
7782 if (intf == NULL || mode == NULL)
7783 return -1;
7784
7785 if (strcasecmp(mode, "On") == 0)
7786 res = wpa_command(intf, "SET radio_disabled 0");
7787 else if (strcasecmp(mode, "Off") == 0)
7788 res = wpa_command(intf, "SET radio_disabled 1");
7789 else
7790 return -1;
7791
7792 if (res) {
7793 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to change "
7794 "radio mode");
7795 return 0;
7796 }
7797
7798 return 1;
7799}
7800
7801
7802static int cmd_sta_set_pwrsave(struct sigma_dut *dut, struct sigma_conn *conn,
7803 struct sigma_cmd *cmd)
7804{
7805 const char *intf = get_param(cmd, "Interface");
7806 const char *mode = get_param(cmd, "Mode");
7807 int res;
7808
7809 if (intf == NULL || mode == NULL)
7810 return -1;
7811
7812 if (strcasecmp(mode, "On") == 0)
7813 res = set_ps(intf, dut, 1);
7814 else if (strcasecmp(mode, "Off") == 0)
7815 res = set_ps(intf, dut, 0);
7816 else
7817 return -1;
7818
7819 if (res) {
7820 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to change "
7821 "power save mode");
7822 return 0;
7823 }
7824
7825 return 1;
7826}
7827
7828
7829static int cmd_sta_bssid_pool(struct sigma_dut *dut, struct sigma_conn *conn,
7830 struct sigma_cmd *cmd)
7831{
7832 const char *intf = get_param(cmd, "Interface");
7833 const char *val, *bssid;
7834 int res;
7835 char *buf;
7836 size_t buf_len;
7837
7838 val = get_param(cmd, "BSSID_FILTER");
7839 if (val == NULL)
7840 return -1;
7841
7842 bssid = get_param(cmd, "BSSID_List");
7843 if (atoi(val) == 0 || bssid == NULL) {
7844 /* Disable BSSID filter */
7845 if (wpa_command(intf, "SET bssid_filter ")) {
7846 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed "
7847 "to disable BSSID filter");
7848 return 0;
7849 }
7850
7851 return 1;
7852 }
7853
7854 buf_len = 100 + strlen(bssid);
7855 buf = malloc(buf_len);
7856 if (buf == NULL)
7857 return -1;
7858
7859 snprintf(buf, buf_len, "SET bssid_filter %s", bssid);
7860 res = wpa_command(intf, buf);
7861 free(buf);
7862 if (res) {
7863 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to enable "
7864 "BSSID filter");
7865 return 0;
7866 }
7867
7868 return 1;
7869}
7870
7871
7872static int cmd_sta_reset_parm(struct sigma_dut *dut, struct sigma_conn *conn,
7873 struct sigma_cmd *cmd)
7874{
7875 const char *intf = get_param(cmd, "Interface");
7876 const char *val;
7877
7878 /* TODO: ARP */
7879
7880 val = get_param(cmd, "HS2_CACHE_PROFILE");
7881 if (val && strcasecmp(val, "All") == 0)
7882 hs2_clear_credentials(intf);
7883
7884 return 1;
7885}
7886
7887
7888static int cmd_sta_get_key(struct sigma_dut *dut, struct sigma_conn *conn,
7889 struct sigma_cmd *cmd)
7890{
7891 const char *intf = get_param(cmd, "Interface");
7892 const char *key_type = get_param(cmd, "KeyType");
7893 char buf[100], resp[200];
7894
7895 if (key_type == NULL)
7896 return -1;
7897
7898 if (strcasecmp(key_type, "GTK") == 0) {
7899 if (wpa_command_resp(intf, "GET gtk", buf, sizeof(buf)) < 0 ||
7900 strncmp(buf, "FAIL", 4) == 0) {
7901 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could "
7902 "not fetch current GTK");
7903 return 0;
7904 }
7905 snprintf(resp, sizeof(resp), "KeyValue,%s", buf);
7906 send_resp(dut, conn, SIGMA_COMPLETE, resp);
7907 return 0;
7908 } else {
7909 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Unsupported "
7910 "KeyType");
7911 return 0;
7912 }
7913
7914 return 1;
7915}
7916
7917
7918static int hs2_set_policy(struct sigma_dut *dut)
7919{
7920#ifdef ANDROID
7921 system("ip rule del prio 23000");
7922 if (system("ip rule add from all lookup main prio 23000") != 0) {
7923 sigma_dut_print(dut, DUT_MSG_ERROR,
7924 "Failed to run:ip rule add from all lookup main prio");
7925 return -1;
7926 }
7927 if (system("ip route flush cache") != 0) {
7928 sigma_dut_print(dut, DUT_MSG_ERROR,
7929 "Failed to run ip route flush cache");
7930 return -1;
7931 }
7932 return 1;
7933#else /* ANDROID */
7934 return 0;
7935#endif /* ANDROID */
7936}
7937
7938
7939static int cmd_sta_hs2_associate(struct sigma_dut *dut,
7940 struct sigma_conn *conn,
7941 struct sigma_cmd *cmd)
7942{
7943 const char *intf = get_param(cmd, "Interface");
7944 const char *val = get_param(cmd, "Ignore_blacklist");
7945 struct wpa_ctrl *ctrl;
7946 int res;
7947 char bssid[20], ssid[40], resp[100], buf[100], blacklisted[100];
7948 int tries = 0;
7949 int ignore_blacklist = 0;
7950 const char *events[] = {
7951 "CTRL-EVENT-CONNECTED",
7952 "INTERWORKING-BLACKLISTED",
7953 "INTERWORKING-NO-MATCH",
7954 NULL
7955 };
7956
7957 start_sta_mode(dut);
7958
7959 blacklisted[0] = '\0';
7960 if (val && atoi(val))
7961 ignore_blacklist = 1;
7962
7963try_again:
7964 ctrl = open_wpa_mon(intf);
7965 if (ctrl == NULL) {
7966 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to open "
7967 "wpa_supplicant monitor connection");
7968 return -2;
7969 }
7970
7971 tries++;
7972 if (wpa_command(intf, "INTERWORKING_SELECT auto")) {
7973 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to start "
7974 "Interworking connection");
7975 wpa_ctrl_detach(ctrl);
7976 wpa_ctrl_close(ctrl);
7977 return 0;
7978 }
7979
7980 buf[0] = '\0';
7981 while (1) {
7982 char *pos;
7983 res = get_wpa_cli_events(dut, ctrl, events, buf, sizeof(buf));
7984 pos = strstr(buf, "INTERWORKING-BLACKLISTED");
7985 if (!pos)
7986 break;
7987 pos += 25;
7988 sigma_dut_print(dut, DUT_MSG_DEBUG, "Found blacklisted AP: %s",
7989 pos);
7990 if (!blacklisted[0])
7991 memcpy(blacklisted, pos, strlen(pos) + 1);
7992 }
7993
7994 if (ignore_blacklist && blacklisted[0]) {
7995 char *end;
7996 end = strchr(blacklisted, ' ');
7997 if (end)
7998 *end = '\0';
7999 sigma_dut_print(dut, DUT_MSG_DEBUG, "Try to connect to a blacklisted network: %s",
8000 blacklisted);
8001 snprintf(buf, sizeof(buf), "INTERWORKING_CONNECT %s",
8002 blacklisted);
8003 if (wpa_command(intf, buf)) {
8004 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to start Interworking connection to blacklisted network");
8005 wpa_ctrl_detach(ctrl);
8006 wpa_ctrl_close(ctrl);
8007 return 0;
8008 }
8009 res = get_wpa_cli_event(dut, ctrl, "CTRL-EVENT-CONNECTED",
8010 buf, sizeof(buf));
8011 }
8012
8013 wpa_ctrl_detach(ctrl);
8014 wpa_ctrl_close(ctrl);
8015
8016 if (res < 0) {
8017 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Could not "
8018 "connect");
8019 return 0;
8020 }
8021
8022 if (strstr(buf, "INTERWORKING-NO-MATCH") ||
8023 strstr(buf, "INTERWORKING-BLACKLISTED")) {
8024 if (tries < 2) {
8025 sigma_dut_print(dut, DUT_MSG_INFO, "No match found - try again to verify no APs were missed in the scan");
8026 goto try_again;
8027 }
8028 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,No network with "
8029 "matching credentials found");
8030 return 0;
8031 }
8032
8033 if (get_wpa_status(intf, "bssid", bssid, sizeof(bssid)) < 0 ||
8034 get_wpa_status(intf, "ssid", ssid, sizeof(ssid)) < 0) {
8035 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Could not "
8036 "get current BSSID/SSID");
8037 return 0;
8038 }
8039
8040 snprintf(resp, sizeof(resp), "SSID,%s,BSSID,%s", ssid, bssid);
8041 send_resp(dut, conn, SIGMA_COMPLETE, resp);
8042 hs2_set_policy(dut);
8043 return 0;
8044}
8045
8046
8047static int sta_add_credential_uname_pwd(struct sigma_dut *dut,
8048 struct sigma_conn *conn,
8049 const char *ifname,
8050 struct sigma_cmd *cmd)
8051{
8052 const char *val;
8053 int id;
8054
8055 id = add_cred(ifname);
8056 if (id < 0)
8057 return -2;
8058 sigma_dut_print(dut, DUT_MSG_DEBUG, "Adding credential %d", id);
8059
8060 val = get_param(cmd, "prefer");
8061 if (val && atoi(val) > 0)
8062 set_cred(ifname, id, "priority", "1");
8063
8064 val = get_param(cmd, "REALM");
8065 if (val && set_cred_quoted(ifname, id, "realm", val) < 0) {
8066 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not set "
8067 "realm");
8068 return 0;
8069 }
8070
8071 val = get_param(cmd, "HOME_FQDN");
8072 if (val && set_cred_quoted(ifname, id, "domain", val) < 0) {
8073 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not set "
8074 "home_fqdn");
8075 return 0;
8076 }
8077
8078 val = get_param(cmd, "Username");
8079 if (val && set_cred_quoted(ifname, id, "username", val) < 0) {
8080 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not set "
8081 "username");
8082 return 0;
8083 }
8084
8085 val = get_param(cmd, "Password");
8086 if (val && set_cred_quoted(ifname, id, "password", val) < 0) {
8087 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not set "
8088 "password");
8089 return 0;
8090 }
8091
8092 val = get_param(cmd, "ROOT_CA");
8093 if (val) {
8094 char fname[200];
8095 snprintf(fname, sizeof(fname), "%s/%s", sigma_cert_path, val);
8096#ifdef __linux__
8097 if (!file_exists(fname)) {
8098 char msg[300];
8099 snprintf(msg, sizeof(msg), "ErrorCode,ROOT_CA "
8100 "file (%s) not found", fname);
8101 send_resp(dut, conn, SIGMA_ERROR, msg);
8102 return 0;
8103 }
8104#endif /* __linux__ */
8105 if (set_cred_quoted(ifname, id, "ca_cert", fname) < 0) {
8106 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could "
8107 "not set root CA");
8108 return 0;
8109 }
8110 }
8111
8112 return 1;
8113}
8114
8115
8116static int update_devdetail_imsi(struct sigma_dut *dut, const char *imsi)
8117{
8118 FILE *in, *out;
8119 char buf[500];
8120 int found = 0;
8121
8122 in = fopen("devdetail.xml", "r");
8123 if (in == NULL)
8124 return -1;
8125 out = fopen("devdetail.xml.tmp", "w");
8126 if (out == NULL) {
8127 fclose(in);
8128 return -1;
8129 }
8130
8131 while (fgets(buf, sizeof(buf), in)) {
8132 char *pos = strstr(buf, "<IMSI>");
8133 if (pos) {
8134 sigma_dut_print(dut, DUT_MSG_INFO, "Updated DevDetail IMSI to %s",
8135 imsi);
8136 pos += 6;
8137 *pos = '\0';
8138 fprintf(out, "%s%s</IMSI>\n", buf, imsi);
8139 found++;
8140 } else {
8141 fprintf(out, "%s", buf);
8142 }
8143 }
8144
8145 fclose(out);
8146 fclose(in);
8147 if (found)
8148 rename("devdetail.xml.tmp", "devdetail.xml");
8149 else
8150 unlink("devdetail.xml.tmp");
8151
8152 return 0;
8153}
8154
8155
8156static int sta_add_credential_sim(struct sigma_dut *dut,
8157 struct sigma_conn *conn,
8158 const char *ifname, struct sigma_cmd *cmd)
8159{
8160 const char *val, *imsi = NULL;
8161 int id;
8162 char buf[200];
8163 int res;
8164 const char *pos;
8165 size_t mnc_len;
8166 char plmn_mcc[4];
8167 char plmn_mnc[4];
8168
8169 id = add_cred(ifname);
8170 if (id < 0)
8171 return -2;
8172 sigma_dut_print(dut, DUT_MSG_DEBUG, "Adding credential %d", id);
8173
8174 val = get_param(cmd, "prefer");
8175 if (val && atoi(val) > 0)
8176 set_cred(ifname, id, "priority", "1");
8177
8178 val = get_param(cmd, "PLMN_MCC");
8179 if (val == NULL) {
8180 send_resp(dut, conn, SIGMA_ERROR,
8181 "errorCode,Missing PLMN_MCC");
8182 return 0;
8183 }
8184 if (strlen(val) != 3) {
8185 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Invalid MCC");
8186 return 0;
8187 }
8188 snprintf(plmn_mcc, sizeof(plmn_mcc), "%s", val);
8189
8190 val = get_param(cmd, "PLMN_MNC");
8191 if (val == NULL) {
8192 send_resp(dut, conn, SIGMA_ERROR,
8193 "errorCode,Missing PLMN_MNC");
8194 return 0;
8195 }
8196 if (strlen(val) != 2 && strlen(val) != 3) {
8197 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Invalid MNC");
8198 return 0;
8199 }
8200 snprintf(plmn_mnc, sizeof(plmn_mnc), "%s", val);
8201
8202 val = get_param(cmd, "IMSI");
8203 if (val == NULL) {
8204 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Missing SIM "
8205 "IMSI");
8206 return 0;
8207 }
8208
8209 imsi = pos = val;
8210
8211 if (strncmp(plmn_mcc, pos, 3) != 0) {
8212 send_resp(dut, conn, SIGMA_ERROR, "errorCode,MCC mismatch");
8213 return 0;
8214 }
8215 pos += 3;
8216
8217 mnc_len = strlen(plmn_mnc);
8218 if (mnc_len < 2) {
8219 send_resp(dut, conn, SIGMA_ERROR, "errorCode,MNC not set");
8220 return 0;
8221 }
8222
8223 if (strncmp(plmn_mnc, pos, mnc_len) != 0) {
8224 send_resp(dut, conn, SIGMA_ERROR, "errorCode,MNC mismatch");
8225 return 0;
8226 }
8227 pos += mnc_len;
8228
8229 res = snprintf(buf, sizeof(buf), "%s%s-%s",plmn_mcc, plmn_mnc, pos);
8230 if (res < 0 || res >= (int) sizeof(buf))
8231 return -1;
8232 if (set_cred_quoted(ifname, id, "imsi", buf) < 0) {
8233 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could "
8234 "not set IMSI");
8235 return 0;
8236 }
8237
8238 val = get_param(cmd, "Password");
8239 if (val && set_cred_quoted(ifname, id, "milenage", val) < 0) {
8240 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could "
8241 "not set password");
8242 return 0;
8243 }
8244
8245 if (dut->program == PROGRAM_HS2_R2) {
8246 /*
8247 * Set provisioning_sp for the test cases where SIM/USIM
8248 * provisioning is used.
8249 */
8250 if (val && set_cred_quoted(ifname, id, "provisioning_sp",
8251 "wi-fi.org") < 0) {
8252 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could "
8253 "not set provisioning_sp");
8254 return 0;
8255 }
8256
8257 update_devdetail_imsi(dut, imsi);
8258 }
8259
8260 return 1;
8261}
8262
8263
8264static int sta_add_credential_cert(struct sigma_dut *dut,
8265 struct sigma_conn *conn,
8266 const char *ifname,
8267 struct sigma_cmd *cmd)
8268{
8269 const char *val;
8270 int id;
8271
8272 id = add_cred(ifname);
8273 if (id < 0)
8274 return -2;
8275 sigma_dut_print(dut, DUT_MSG_DEBUG, "Adding credential %d", id);
8276
8277 val = get_param(cmd, "prefer");
8278 if (val && atoi(val) > 0)
8279 set_cred(ifname, id, "priority", "1");
8280
8281 val = get_param(cmd, "REALM");
8282 if (val && set_cred_quoted(ifname, id, "realm", val) < 0) {
8283 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not set "
8284 "realm");
8285 return 0;
8286 }
8287
8288 val = get_param(cmd, "HOME_FQDN");
8289 if (val && set_cred_quoted(ifname, id, "domain", val) < 0) {
8290 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not set "
8291 "home_fqdn");
8292 return 0;
8293 }
8294
8295 val = get_param(cmd, "Username");
8296 if (val && set_cred_quoted(ifname, id, "username", val) < 0) {
8297 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not set "
8298 "username");
8299 return 0;
8300 }
8301
8302 val = get_param(cmd, "clientCertificate");
8303 if (val) {
8304 char fname[200];
8305 snprintf(fname, sizeof(fname), "%s/%s", sigma_cert_path, val);
8306#ifdef __linux__
8307 if (!file_exists(fname)) {
8308 char msg[300];
8309 snprintf(msg, sizeof(msg),
8310 "ErrorCode,clientCertificate "
8311 "file (%s) not found", fname);
8312 send_resp(dut, conn, SIGMA_ERROR, msg);
8313 return 0;
8314 }
8315#endif /* __linux__ */
8316 if (set_cred_quoted(ifname, id, "client_cert", fname) < 0) {
8317 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could "
8318 "not set client_cert");
8319 return 0;
8320 }
8321 if (set_cred_quoted(ifname, id, "private_key", fname) < 0) {
8322 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could "
8323 "not set private_key");
8324 return 0;
8325 }
8326 }
8327
8328 val = get_param(cmd, "ROOT_CA");
8329 if (val) {
8330 char fname[200];
8331 snprintf(fname, sizeof(fname), "%s/%s", sigma_cert_path, val);
8332#ifdef __linux__
8333 if (!file_exists(fname)) {
8334 char msg[300];
8335 snprintf(msg, sizeof(msg), "ErrorCode,ROOT_CA "
8336 "file (%s) not found", fname);
8337 send_resp(dut, conn, SIGMA_ERROR, msg);
8338 return 0;
8339 }
8340#endif /* __linux__ */
8341 if (set_cred_quoted(ifname, id, "ca_cert", fname) < 0) {
8342 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could "
8343 "not set root CA");
8344 return 0;
8345 }
8346 }
8347
8348 return 1;
8349}
8350
8351
8352static int cmd_sta_add_credential(struct sigma_dut *dut,
8353 struct sigma_conn *conn,
8354 struct sigma_cmd *cmd)
8355{
8356 const char *intf = get_param(cmd, "Interface");
8357 const char *type;
8358
8359 start_sta_mode(dut);
8360
8361 type = get_param(cmd, "Type");
8362 if (!type)
8363 return -1;
8364
8365 if (strcasecmp(type, "uname_pwd") == 0)
8366 return sta_add_credential_uname_pwd(dut, conn, intf, cmd);
8367
8368 if (strcasecmp(type, "sim") == 0)
8369 return sta_add_credential_sim(dut, conn, intf, cmd);
8370
8371 if (strcasecmp(type, "cert") == 0)
8372 return sta_add_credential_cert(dut, conn, intf, cmd);
8373
8374 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Unsupported credential "
8375 "type");
8376 return 0;
8377}
8378
8379
8380static int cmd_sta_scan(struct sigma_dut *dut, struct sigma_conn *conn,
8381 struct sigma_cmd *cmd)
8382{
8383 const char *intf = get_param(cmd, "Interface");
8384 const char *val;
8385 char buf[100];
8386 int res;
8387
8388 val = get_param(cmd, "HESSID");
8389 if (val) {
8390 res = snprintf(buf, sizeof(buf), "SET hessid %s", val);
8391 if (res < 0 || res >= (int) sizeof(buf))
8392 return -1;
8393 wpa_command(intf, buf);
8394 }
8395
8396 val = get_param(cmd, "ACCS_NET_TYPE");
8397 if (val) {
8398 res = snprintf(buf, sizeof(buf), "SET access_network_type %s",
8399 val);
8400 if (res < 0 || res >= (int) sizeof(buf))
8401 return -1;
8402 wpa_command(intf, buf);
8403 }
8404
8405 if (wpa_command(intf, "SCAN")) {
8406 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not start "
8407 "scan");
8408 return 0;
8409 }
8410
8411 return 1;
8412}
8413
8414
8415static int cmd_sta_set_systime(struct sigma_dut *dut, struct sigma_conn *conn,
8416 struct sigma_cmd *cmd)
8417{
8418#ifdef __linux__
8419 struct timeval tv;
8420 struct tm tm;
8421 time_t t;
8422 const char *val;
Pradeep Reddy POTTETI429c69e2016-10-13 17:22:03 +05308423 int v;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02008424
8425 wpa_command(get_station_ifname(), "PMKSA_FLUSH");
8426
8427 memset(&tm, 0, sizeof(tm));
8428 val = get_param(cmd, "seconds");
8429 if (val)
8430 tm.tm_sec = atoi(val);
8431 val = get_param(cmd, "minutes");
8432 if (val)
8433 tm.tm_min = atoi(val);
8434 val = get_param(cmd, "hours");
8435 if (val)
8436 tm.tm_hour = atoi(val);
8437 val = get_param(cmd, "date");
8438 if (val)
8439 tm.tm_mday = atoi(val);
8440 val = get_param(cmd, "month");
Pradeep Reddy POTTETI429c69e2016-10-13 17:22:03 +05308441 if (val) {
8442 v = atoi(val);
8443 if (v < 1 || v > 12) {
8444 send_resp(dut, conn, SIGMA_INVALID,
8445 "errorCode,Invalid month");
8446 return 0;
8447 }
8448 tm.tm_mon = v - 1;
8449 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02008450 val = get_param(cmd, "year");
8451 if (val) {
8452 int year = atoi(val);
8453#ifdef ANDROID
8454 if (year > 2035)
8455 year = 2035; /* years beyond 2035 not supported */
8456#endif /* ANDROID */
8457 tm.tm_year = year - 1900;
8458 }
8459 t = mktime(&tm);
8460 if (t == (time_t) -1) {
8461 send_resp(dut, conn, SIGMA_ERROR,
8462 "errorCode,Invalid date or time");
8463 return 0;
8464 }
8465
8466 memset(&tv, 0, sizeof(tv));
8467 tv.tv_sec = t;
8468
8469 if (settimeofday(&tv, NULL) < 0) {
8470 sigma_dut_print(dut, DUT_MSG_INFO, "settimeofday failed: %s",
8471 strerror(errno));
8472 send_resp(dut, conn, SIGMA_ERROR,
8473 "errorCode,Failed to set time");
8474 return 0;
8475 }
8476
8477 return 1;
8478#endif /* __linux__ */
8479
8480 return -1;
8481}
8482
8483
8484static int cmd_sta_osu(struct sigma_dut *dut, struct sigma_conn *conn,
8485 struct sigma_cmd *cmd)
8486{
8487 const char *intf = get_param(cmd, "Interface");
8488 const char *name, *val;
8489 int prod_ess_assoc = 1;
8490 char buf[200], bssid[100], ssid[100];
8491 int res;
8492 struct wpa_ctrl *ctrl;
8493
8494 name = get_param(cmd, "osuFriendlyName");
8495
8496 val = get_param(cmd, "ProdESSAssoc");
8497 if (val)
8498 prod_ess_assoc = atoi(val);
8499
8500 kill_dhcp_client(dut, intf);
8501 if (start_dhcp_client(dut, intf) < 0)
8502 return -2;
8503
8504 sigma_dut_print(dut, DUT_MSG_DEBUG, "Trigger OSU");
8505 mkdir("Logs", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
8506 res = snprintf(buf, sizeof(buf),
8507 "%s %s%s%s signup osu-ca.pem",
8508 prod_ess_assoc ? "" : "-N",
8509 name ? "-O'" : "", name ? name : "",
8510 name ? "'" : "");
8511
Kanchanapally, Vidyullatha12b66762015-12-31 16:46:42 +05308512 hs2_set_policy(dut);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02008513 if (run_hs20_osu(dut, buf) < 0) {
8514 FILE *f;
8515
8516 sigma_dut_print(dut, DUT_MSG_INFO, "Failed to complete OSU");
8517
8518 f = fopen("hs20-osu-client.res", "r");
8519 if (f) {
8520 char resp[400], res[300], *pos;
8521 if (!fgets(res, sizeof(res), f))
8522 res[0] = '\0';
8523 pos = strchr(res, '\n');
8524 if (pos)
8525 *pos = '\0';
8526 fclose(f);
8527 sigma_dut_summary(dut, "hs20-osu-client provisioning failed: %s",
8528 res);
8529 snprintf(resp, sizeof(resp), "notify-send '%s'", res);
8530 if (system(resp) != 0) {
8531 }
8532 snprintf(resp, sizeof(resp),
8533 "SSID,,BSSID,,failureReason,%s", res);
8534 send_resp(dut, conn, SIGMA_COMPLETE, resp);
8535 return 0;
8536 }
8537
8538 send_resp(dut, conn, SIGMA_COMPLETE, "SSID,,BSSID,");
8539 return 0;
8540 }
8541
8542 if (!prod_ess_assoc)
8543 goto report;
8544
8545 ctrl = open_wpa_mon(intf);
8546 if (ctrl == NULL) {
8547 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to open "
8548 "wpa_supplicant monitor connection");
8549 return -1;
8550 }
8551
8552 res = get_wpa_cli_event(dut, ctrl, "CTRL-EVENT-CONNECTED",
8553 buf, sizeof(buf));
8554
8555 wpa_ctrl_detach(ctrl);
8556 wpa_ctrl_close(ctrl);
8557
8558 if (res < 0) {
8559 sigma_dut_print(dut, DUT_MSG_INFO, "Failed to connect to "
8560 "network after OSU");
8561 send_resp(dut, conn, SIGMA_COMPLETE, "SSID,,BSSID,");
8562 return 0;
8563 }
8564
8565report:
8566 if (get_wpa_status(intf, "bssid", bssid, sizeof(bssid)) < 0 ||
8567 get_wpa_status(intf, "ssid", ssid, sizeof(ssid)) < 0) {
8568 sigma_dut_print(dut, DUT_MSG_INFO, "Failed to get BSSID/SSID");
8569 send_resp(dut, conn, SIGMA_COMPLETE, "SSID,,BSSID,");
8570 return 0;
8571 }
8572
8573 snprintf(buf, sizeof(buf), "SSID,%s,BSSID,%s", ssid, bssid);
8574 send_resp(dut, conn, SIGMA_COMPLETE, buf);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02008575 return 0;
8576}
8577
8578
8579static int cmd_sta_policy_update(struct sigma_dut *dut, struct sigma_conn *conn,
8580 struct sigma_cmd *cmd)
8581{
8582 const char *val;
8583 int timeout = 120;
8584
8585 val = get_param(cmd, "PolicyUpdate");
8586 if (val == NULL || atoi(val) == 0)
8587 return 1; /* No operation requested */
8588
8589 val = get_param(cmd, "Timeout");
8590 if (val)
8591 timeout = atoi(val);
8592
8593 if (timeout) {
8594 /* TODO: time out the command and return
8595 * PolicyUpdateStatus,TIMEOUT if needed. */
8596 }
8597
8598 sigma_dut_print(dut, DUT_MSG_DEBUG, "Trigger policy update");
8599 mkdir("Logs", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
8600 if (run_hs20_osu(dut, "pol_upd fqdn=wi-fi.org") < 0) {
8601 send_resp(dut, conn, SIGMA_COMPLETE, "PolicyUpdateStatus,FAIL");
8602 return 0;
8603 }
8604
8605 send_resp(dut, conn, SIGMA_COMPLETE, "PolicyUpdateStatus,SUCCESS");
8606 return 0;
8607}
8608
8609
8610static int cmd_sta_er_config(struct sigma_dut *dut, struct sigma_conn *conn,
8611 struct sigma_cmd *cmd)
8612{
8613 struct wpa_ctrl *ctrl;
8614 const char *intf = get_param(cmd, "Interface");
8615 const char *bssid = get_param(cmd, "Bssid");
8616 const char *ssid = get_param(cmd, "SSID");
8617 const char *security = get_param(cmd, "Security");
8618 const char *passphrase = get_param(cmd, "Passphrase");
8619 const char *pin = get_param(cmd, "PIN");
8620 char buf[1000];
8621 char ssid_hex[200], passphrase_hex[200];
8622 const char *keymgmt, *cipher;
8623
8624 if (intf == NULL)
8625 intf = get_main_ifname();
8626
8627 if (!bssid) {
8628 send_resp(dut, conn, SIGMA_ERROR,
8629 "ErrorCode,Missing Bssid argument");
8630 return 0;
8631 }
8632
8633 if (!ssid) {
8634 send_resp(dut, conn, SIGMA_ERROR,
8635 "ErrorCode,Missing SSID argument");
8636 return 0;
8637 }
8638
8639 if (!security) {
8640 send_resp(dut, conn, SIGMA_ERROR,
8641 "ErrorCode,Missing Security argument");
8642 return 0;
8643 }
8644
8645 if (!passphrase) {
8646 send_resp(dut, conn, SIGMA_ERROR,
8647 "ErrorCode,Missing Passphrase argument");
8648 return 0;
8649 }
8650
8651 if (!pin) {
8652 send_resp(dut, conn, SIGMA_ERROR,
8653 "ErrorCode,Missing PIN argument");
8654 return 0;
8655 }
8656
vamsi krishna8c9c1562017-05-12 15:51:46 +05308657 if (2 * strlen(ssid) >= sizeof(ssid_hex) ||
8658 2 * strlen(passphrase) >= sizeof(passphrase_hex)) {
Jouni Malinencd4e3c32015-10-29 12:39:56 +02008659 send_resp(dut, conn, SIGMA_ERROR,
8660 "ErrorCode,Too long SSID/passphrase");
8661 return 0;
8662 }
8663
8664 ctrl = open_wpa_mon(intf);
8665 if (ctrl == NULL) {
8666 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to open "
8667 "wpa_supplicant monitor connection");
8668 return -2;
8669 }
8670
8671 if (strcasecmp(security, "wpa2-psk") == 0) {
8672 keymgmt = "WPA2PSK";
8673 cipher = "CCMP";
8674 } else {
8675 wpa_ctrl_detach(ctrl);
8676 wpa_ctrl_close(ctrl);
8677 send_resp(dut, conn, SIGMA_ERROR,
8678 "ErrorCode,Unsupported Security value");
8679 return 0;
8680 }
8681
8682 ascii2hexstr(ssid, ssid_hex);
8683 ascii2hexstr(passphrase, passphrase_hex);
8684 snprintf(buf, sizeof(buf), "WPS_REG %s %s %s %s %s %s",
8685 bssid, pin, ssid_hex, keymgmt, cipher, passphrase_hex);
8686
8687 if (wpa_command(intf, buf) < 0) {
8688 wpa_ctrl_detach(ctrl);
8689 wpa_ctrl_close(ctrl);
8690 send_resp(dut, conn, SIGMA_ERROR,
8691 "ErrorCode,Failed to start registrar");
8692 return 0;
8693 }
8694
8695 snprintf(dut->er_oper_bssid, sizeof(dut->er_oper_bssid), "%s", bssid);
8696 dut->er_oper_performed = 1;
8697
8698 return wps_connection_event(dut, conn, ctrl, intf, 0);
8699}
8700
8701
8702static int cmd_sta_wps_connect_pw_token(struct sigma_dut *dut,
8703 struct sigma_conn *conn,
8704 struct sigma_cmd *cmd)
8705{
8706 struct wpa_ctrl *ctrl;
8707 const char *intf = get_param(cmd, "Interface");
8708 const char *bssid = get_param(cmd, "Bssid");
8709 char buf[100];
8710
8711 if (!bssid) {
8712 send_resp(dut, conn, SIGMA_ERROR,
8713 "ErrorCode,Missing Bssid argument");
8714 return 0;
8715 }
8716
8717 ctrl = open_wpa_mon(intf);
8718 if (ctrl == NULL) {
8719 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to open "
8720 "wpa_supplicant monitor connection");
8721 return -2;
8722 }
8723
8724 snprintf(buf, sizeof(buf), "WPS_NFC %s", bssid);
8725
8726 if (wpa_command(intf, buf) < 0) {
8727 wpa_ctrl_detach(ctrl);
8728 wpa_ctrl_close(ctrl);
8729 send_resp(dut, conn, SIGMA_ERROR,
8730 "ErrorCode,Failed to start registrar");
8731 return 0;
8732 }
8733
8734 return wps_connection_event(dut, conn, ctrl, intf, 0);
8735}
8736
8737
8738static int req_intf(struct sigma_cmd *cmd)
8739{
8740 return get_param(cmd, "interface") == NULL ? -1 : 0;
8741}
8742
8743
8744void sta_register_cmds(void)
8745{
8746 sigma_dut_reg_cmd("sta_get_ip_config", req_intf,
8747 cmd_sta_get_ip_config);
8748 sigma_dut_reg_cmd("sta_set_ip_config", req_intf,
8749 cmd_sta_set_ip_config);
8750 sigma_dut_reg_cmd("sta_get_info", req_intf, cmd_sta_get_info);
8751 sigma_dut_reg_cmd("sta_get_mac_address", req_intf,
8752 cmd_sta_get_mac_address);
8753 sigma_dut_reg_cmd("sta_is_connected", req_intf, cmd_sta_is_connected);
8754 sigma_dut_reg_cmd("sta_verify_ip_connection", req_intf,
8755 cmd_sta_verify_ip_connection);
8756 sigma_dut_reg_cmd("sta_get_bssid", req_intf, cmd_sta_get_bssid);
8757 sigma_dut_reg_cmd("sta_set_encryption", req_intf,
8758 cmd_sta_set_encryption);
8759 sigma_dut_reg_cmd("sta_set_psk", req_intf, cmd_sta_set_psk);
8760 sigma_dut_reg_cmd("sta_set_eaptls", req_intf, cmd_sta_set_eaptls);
8761 sigma_dut_reg_cmd("sta_set_eapttls", req_intf, cmd_sta_set_eapttls);
8762 sigma_dut_reg_cmd("sta_set_eapsim", req_intf, cmd_sta_set_eapsim);
8763 sigma_dut_reg_cmd("sta_set_peap", req_intf, cmd_sta_set_peap);
8764 sigma_dut_reg_cmd("sta_set_eapfast", req_intf, cmd_sta_set_eapfast);
8765 sigma_dut_reg_cmd("sta_set_eapaka", req_intf, cmd_sta_set_eapaka);
8766 sigma_dut_reg_cmd("sta_set_eapakaprime", req_intf,
8767 cmd_sta_set_eapakaprime);
8768 sigma_dut_reg_cmd("sta_set_security", req_intf, cmd_sta_set_security);
8769 sigma_dut_reg_cmd("sta_set_uapsd", req_intf, cmd_sta_set_uapsd);
8770 /* TODO: sta_set_ibss */
8771 /* TODO: sta_set_mode */
8772 sigma_dut_reg_cmd("sta_set_wmm", req_intf, cmd_sta_set_wmm);
8773 sigma_dut_reg_cmd("sta_associate", req_intf, cmd_sta_associate);
8774 /* TODO: sta_up_load */
8775 sigma_dut_reg_cmd("sta_preset_testparameters", req_intf,
8776 cmd_sta_preset_testparameters);
8777 /* TODO: sta_set_system */
8778 sigma_dut_reg_cmd("sta_set_11n", req_intf, cmd_sta_set_11n);
8779 /* TODO: sta_set_rifs_test */
8780 sigma_dut_reg_cmd("sta_set_wireless", req_intf, cmd_sta_set_wireless);
8781 sigma_dut_reg_cmd("sta_send_addba", req_intf, cmd_sta_send_addba);
8782 /* TODO: sta_send_coexist_mgmt */
8783 sigma_dut_reg_cmd("sta_disconnect", req_intf, cmd_sta_disconnect);
8784 sigma_dut_reg_cmd("sta_reassoc", req_intf, cmd_sta_reassoc);
8785 sigma_dut_reg_cmd("sta_reassociate", req_intf, cmd_sta_reassoc);
8786 sigma_dut_reg_cmd("sta_reset_default", req_intf,
8787 cmd_sta_reset_default);
8788 sigma_dut_reg_cmd("sta_send_frame", req_intf, cmd_sta_send_frame);
8789 sigma_dut_reg_cmd("sta_set_macaddr", req_intf, cmd_sta_set_macaddr);
8790 sigma_dut_reg_cmd("sta_set_rfeature", req_intf, cmd_sta_set_rfeature);
8791 sigma_dut_reg_cmd("sta_set_radio", req_intf, cmd_sta_set_radio);
8792 sigma_dut_reg_cmd("sta_set_pwrsave", req_intf, cmd_sta_set_pwrsave);
8793 sigma_dut_reg_cmd("sta_bssid_pool", req_intf, cmd_sta_bssid_pool);
8794 sigma_dut_reg_cmd("sta_reset_parm", req_intf, cmd_sta_reset_parm);
8795 sigma_dut_reg_cmd("sta_get_key", req_intf, cmd_sta_get_key);
8796 sigma_dut_reg_cmd("sta_hs2_associate", req_intf,
8797 cmd_sta_hs2_associate);
8798 sigma_dut_reg_cmd("sta_add_credential", req_intf,
8799 cmd_sta_add_credential);
8800 sigma_dut_reg_cmd("sta_scan", req_intf, cmd_sta_scan);
8801 sigma_dut_reg_cmd("sta_set_systime", NULL, cmd_sta_set_systime);
8802 sigma_dut_reg_cmd("sta_osu", req_intf, cmd_sta_osu);
8803 sigma_dut_reg_cmd("sta_policy_update", req_intf, cmd_sta_policy_update);
8804 sigma_dut_reg_cmd("sta_er_config", NULL, cmd_sta_er_config);
8805 sigma_dut_reg_cmd("sta_wps_connect_pw_token", req_intf,
8806 cmd_sta_wps_connect_pw_token);
8807 sigma_dut_reg_cmd("sta_exec_action", req_intf, cmd_sta_exec_action);
8808 sigma_dut_reg_cmd("sta_get_events", req_intf, cmd_sta_get_events);
8809 sigma_dut_reg_cmd("sta_get_parameter", req_intf, cmd_sta_get_parameter);
8810}