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