blob: 0ac7fb3e1ffa96342c5aad9c8caa1420c2f646cd [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 Malinend86e5822017-08-29 03:55:32 +03005109
Jouni Malinenfac9cad2017-10-10 18:35:55 +03005110 wpa_command(intf, "VENDOR_ELEM_REMOVE 13 *");
5111
Ankita Bajaja2cb5672017-10-25 16:08:28 +05305112 if (dut->program == PROGRAM_OCE)
5113 wpa_command(intf, "SET oce 1");
5114
Priyadharshini Gowthamana7dfd492015-11-09 14:34:08 -08005115 if (dut->program != PROGRAM_VHT)
5116 return cmd_sta_p2p_reset(dut, conn, cmd);
5117 return 1;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005118}
5119
5120
5121static int cmd_sta_get_events(struct sigma_dut *dut, struct sigma_conn *conn,
5122 struct sigma_cmd *cmd)
5123{
5124 const char *program = get_param(cmd, "Program");
5125
5126 if (program == NULL)
5127 return -1;
5128#ifdef ANDROID_NAN
5129 if (strcasecmp(program, "NAN") == 0)
5130 return nan_cmd_sta_get_events(dut, conn, cmd);
5131#endif /* ANDROID_NAN */
5132 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Unsupported parameter");
5133 return 0;
5134}
5135
5136
5137static int cmd_sta_exec_action(struct sigma_dut *dut, struct sigma_conn *conn,
5138 struct sigma_cmd *cmd)
5139{
5140 const char *program = get_param(cmd, "Prog");
5141
5142 if (program == NULL)
5143 return -1;
5144#ifdef ANDROID_NAN
5145 if (strcasecmp(program, "NAN") == 0)
5146 return nan_cmd_sta_exec_action(dut, conn, cmd);
5147#endif /* ANDROID_NAN */
priyadharshini gowthamand66913a2016-07-29 15:11:17 -07005148 if (strcasecmp(program, "Loc") == 0)
5149 return loc_cmd_sta_exec_action(dut, conn, cmd);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005150 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Unsupported parameter");
5151 return 0;
5152}
5153
5154
5155static int cmd_sta_set_11n(struct sigma_dut *dut, struct sigma_conn *conn,
5156 struct sigma_cmd *cmd)
5157{
5158 const char *intf = get_param(cmd, "Interface");
5159 const char *val, *mcs32, *rate;
5160
5161 val = get_param(cmd, "GREENFIELD");
5162 if (val) {
5163 if (strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0) {
5164 /* Enable GD */
5165 send_resp(dut, conn, SIGMA_ERROR,
5166 "ErrorCode,GF not supported");
5167 return 0;
5168 }
5169 }
5170
5171 val = get_param(cmd, "SGI20");
5172 if (val) {
5173 switch (get_driver_type()) {
5174 case DRIVER_ATHEROS:
5175 ath_sta_set_sgi(dut, intf, val);
5176 break;
5177 default:
5178 send_resp(dut, conn, SIGMA_ERROR,
5179 "ErrorCode,SGI20 not supported");
5180 return 0;
5181 }
5182 }
5183
5184 mcs32 = get_param(cmd, "MCS32"); /* HT Duplicate Mode Enable/Disable */
5185 rate = get_param(cmd, "MCS_FIXEDRATE"); /* Fixed MCS rate (0..31) */
5186 if (mcs32 && rate) {
5187 /* TODO */
5188 send_resp(dut, conn, SIGMA_ERROR,
5189 "ErrorCode,MCS32,MCS_FIXEDRATE not supported");
5190 return 0;
5191 } else if (mcs32 && !rate) {
5192 /* TODO */
5193 send_resp(dut, conn, SIGMA_ERROR,
5194 "ErrorCode,MCS32 not supported");
5195 return 0;
5196 } else if (!mcs32 && rate) {
5197 switch (get_driver_type()) {
5198 case DRIVER_ATHEROS:
priyadharshini gowthamane5e25172015-12-08 14:53:48 -08005199 novap_reset(dut, intf);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005200 ath_sta_set_11nrates(dut, intf, rate);
5201 break;
5202 default:
5203 send_resp(dut, conn, SIGMA_ERROR,
5204 "ErrorCode,MCS32_FIXEDRATE not supported");
5205 return 0;
5206 }
5207 }
5208
5209 return cmd_sta_set_wireless_common(intf, dut, conn, cmd);
5210}
5211
5212
5213static int cmd_sta_set_wireless_vht(struct sigma_dut *dut,
5214 struct sigma_conn *conn,
5215 struct sigma_cmd *cmd)
5216{
5217 const char *intf = get_param(cmd, "Interface");
5218 const char *val;
5219 char buf[30];
5220 int tkip = -1;
5221 int wep = -1;
5222
5223 val = get_param(cmd, "SGI80");
5224 if (val) {
5225 int sgi80;
5226
5227 sgi80 = strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0;
5228 snprintf(buf, sizeof(buf), "iwpriv %s shortgi %d", intf, sgi80);
5229 if (system(buf) != 0) {
5230 sigma_dut_print(dut, DUT_MSG_ERROR,
5231 "iwpriv shortgi failed");
5232 }
5233 }
5234
5235 val = get_param(cmd, "TxBF");
5236 if (val && (strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0)) {
5237 snprintf(buf, sizeof(buf), "iwpriv %s vhtsubfee 1", intf);
5238 if (system(buf) != 0) {
5239 sigma_dut_print(dut, DUT_MSG_ERROR,
5240 "iwpriv vhtsubfee failed");
5241 }
5242 snprintf(buf, sizeof(buf), "iwpriv %s vhtsubfer 1", intf);
5243 if (system(buf) != 0) {
5244 sigma_dut_print(dut, DUT_MSG_ERROR,
5245 "iwpriv vhtsubfer failed");
5246 }
5247 }
5248
5249 val = get_param(cmd, "MU_TxBF");
5250 if (val && (strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0)) {
5251 switch (get_driver_type()) {
5252 case DRIVER_ATHEROS:
5253 ath_sta_set_txsp_stream(dut, intf, "1SS");
5254 ath_sta_set_rxsp_stream(dut, intf, "1SS");
5255 case DRIVER_WCN:
5256 if (wcn_sta_set_sp_stream(dut, intf, "1SS") < 0) {
5257 send_resp(dut, conn, SIGMA_ERROR,
5258 "ErrorCode,Failed to set RX/TXSP_STREAM");
5259 return 0;
5260 }
5261 default:
5262 sigma_dut_print(dut, DUT_MSG_ERROR,
5263 "Setting SP_STREAM not supported");
5264 break;
5265 }
5266 snprintf(buf, sizeof(buf), "iwpriv %s vhtmubfee 1", intf);
5267 if (system(buf) != 0) {
5268 sigma_dut_print(dut, DUT_MSG_ERROR,
5269 "iwpriv vhtmubfee failed");
5270 }
5271 snprintf(buf, sizeof(buf), "iwpriv %s vhtmubfer 1", intf);
5272 if (system(buf) != 0) {
5273 sigma_dut_print(dut, DUT_MSG_ERROR,
5274 "iwpriv vhtmubfer failed");
5275 }
5276 }
5277
5278 val = get_param(cmd, "LDPC");
5279 if (val) {
5280 int ldpc;
5281
5282 ldpc = strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0;
5283 snprintf(buf, sizeof(buf), "iwpriv %s ldpc %d", intf, ldpc);
5284 if (system(buf) != 0) {
5285 sigma_dut_print(dut, DUT_MSG_ERROR,
5286 "iwpriv ldpc failed");
5287 }
5288 }
5289
5290 val = get_param(cmd, "opt_md_notif_ie");
5291 if (val) {
5292 char *result = NULL;
5293 char delim[] = ";";
5294 char token[30];
5295 int value, config_val = 0;
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05305296 char *saveptr;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005297
Peng Xub8fc5cc2017-05-10 17:27:28 -07005298 strlcpy(token, val, sizeof(token));
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05305299 result = strtok_r(token, delim, &saveptr);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005300
5301 /* Extract the NSS information */
5302 if (result) {
5303 value = atoi(result);
5304 switch (value) {
5305 case 1:
5306 config_val = 1;
5307 break;
5308 case 2:
5309 config_val = 3;
5310 break;
5311 case 3:
5312 config_val = 7;
5313 break;
5314 case 4:
5315 config_val = 15;
5316 break;
5317 default:
5318 config_val = 3;
5319 break;
5320 }
5321
5322 snprintf(buf, sizeof(buf), "iwpriv %s rxchainmask %d",
5323 intf, config_val);
5324 if (system(buf) != 0) {
5325 sigma_dut_print(dut, DUT_MSG_ERROR,
5326 "iwpriv rxchainmask failed");
5327 }
5328
5329 snprintf(buf, sizeof(buf), "iwpriv %s txchainmask %d",
5330 intf, config_val);
5331 if (system(buf) != 0) {
5332 sigma_dut_print(dut, DUT_MSG_ERROR,
5333 "iwpriv txchainmask failed");
5334 }
5335 }
5336
5337 /* Extract the channel width information */
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05305338 result = strtok_r(NULL, delim, &saveptr);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005339 if (result) {
5340 value = atoi(result);
5341 switch (value) {
5342 case 20:
5343 config_val = 0;
5344 break;
5345 case 40:
5346 config_val = 1;
5347 break;
5348 case 80:
5349 config_val = 2;
5350 break;
5351 case 160:
5352 config_val = 3;
5353 break;
5354 default:
5355 config_val = 2;
5356 break;
5357 }
5358
5359 dut->chwidth = config_val;
5360
5361 snprintf(buf, sizeof(buf), "iwpriv %s chwidth %d",
5362 intf, config_val);
5363 if (system(buf) != 0) {
5364 sigma_dut_print(dut, DUT_MSG_ERROR,
5365 "iwpriv chwidth failed");
5366 }
5367 }
5368
5369 snprintf(buf, sizeof(buf), "iwpriv %s opmode_notify 1", intf);
5370 if (system(buf) != 0) {
5371 sigma_dut_print(dut, DUT_MSG_ERROR,
5372 "iwpriv opmode_notify failed");
5373 }
5374 }
5375
5376 val = get_param(cmd, "nss_mcs_cap");
5377 if (val) {
5378 int nss, mcs;
5379 char token[20];
5380 char *result = NULL;
5381 unsigned int vht_mcsmap = 0;
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05305382 char *saveptr;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005383
Peng Xub8fc5cc2017-05-10 17:27:28 -07005384 strlcpy(token, val, sizeof(token));
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05305385 result = strtok_r(token, ";", &saveptr);
Pradeep Reddy POTTETIcd649a22016-01-29 12:55:59 +05305386 if (!result) {
5387 sigma_dut_print(dut, DUT_MSG_ERROR,
5388 "VHT NSS not specified");
5389 return 0;
5390 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005391 nss = atoi(result);
5392
5393 snprintf(buf, sizeof(buf), "iwpriv %s nss %d", intf, nss);
5394 if (system(buf) != 0) {
5395 sigma_dut_print(dut, DUT_MSG_ERROR,
5396 "iwpriv nss failed");
5397 }
5398
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05305399 result = strtok_r(NULL, ";", &saveptr);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005400 if (result == NULL) {
5401 sigma_dut_print(dut, DUT_MSG_ERROR,
5402 "VHTMCS NOT SPECIFIED!");
5403 return 0;
5404 }
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05305405 result = strtok_r(result, "-", &saveptr);
5406 result = strtok_r(NULL, "-", &saveptr);
Pradeep Reddy POTTETIcd649a22016-01-29 12:55:59 +05305407 if (!result) {
5408 sigma_dut_print(dut, DUT_MSG_ERROR,
5409 "VHT MCS not specified");
5410 return 0;
5411 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005412 mcs = atoi(result);
5413
5414 snprintf(buf, sizeof(buf), "iwpriv %s vhtmcs %d", intf, mcs);
5415 if (system(buf) != 0) {
5416 sigma_dut_print(dut, DUT_MSG_ERROR,
5417 "iwpriv mcs failed");
5418 }
5419
5420 switch (nss) {
5421 case 1:
5422 switch (mcs) {
5423 case 7:
5424 vht_mcsmap = 0xfffc;
5425 break;
5426 case 8:
5427 vht_mcsmap = 0xfffd;
5428 break;
5429 case 9:
5430 vht_mcsmap = 0xfffe;
5431 break;
5432 default:
5433 vht_mcsmap = 0xfffe;
5434 break;
5435 }
5436 break;
5437 case 2:
5438 switch (mcs) {
5439 case 7:
5440 vht_mcsmap = 0xfff0;
5441 break;
5442 case 8:
5443 vht_mcsmap = 0xfff5;
5444 break;
5445 case 9:
5446 vht_mcsmap = 0xfffa;
5447 break;
5448 default:
5449 vht_mcsmap = 0xfffa;
5450 break;
5451 }
5452 break;
5453 case 3:
5454 switch (mcs) {
5455 case 7:
5456 vht_mcsmap = 0xffc0;
5457 break;
5458 case 8:
5459 vht_mcsmap = 0xffd5;
5460 break;
5461 case 9:
5462 vht_mcsmap = 0xffea;
5463 break;
5464 default:
5465 vht_mcsmap = 0xffea;
5466 break;
5467 }
5468 break;
5469 default:
5470 vht_mcsmap = 0xffea;
5471 break;
5472 }
5473 snprintf(buf, sizeof(buf), "iwpriv %s vht_mcsmap 0x%04x",
5474 intf, vht_mcsmap);
5475 if (system(buf) != 0) {
5476 sigma_dut_print(dut, DUT_MSG_ERROR,
5477 "iwpriv vht_mcsmap failed");
5478 }
5479 }
5480
5481 /* UNSUPPORTED: val = get_param(cmd, "Tx_lgi_rate"); */
5482
5483 val = get_param(cmd, "Vht_tkip");
5484 if (val)
5485 tkip = strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0;
5486
5487 val = get_param(cmd, "Vht_wep");
5488 if (val)
5489 wep = strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0;
5490
5491 if (tkip != -1 || wep != -1) {
5492 if ((tkip == 1 && wep != 0) || (wep == 1 && tkip != 0)) {
5493 snprintf(buf, sizeof(buf), "iwpriv %s htweptkip 1",
5494 intf);
5495 } else if ((tkip == 0 && wep != 1) || (wep == 0 && tkip != 1)) {
5496 snprintf(buf, sizeof(buf), "iwpriv %s htweptkip 0",
5497 intf);
5498 } else {
5499 sigma_dut_print(dut, DUT_MSG_ERROR,
5500 "ErrorCode,mixed mode of VHT TKIP/WEP not supported");
5501 return 0;
5502 }
5503
5504 if (system(buf) != 0) {
5505 sigma_dut_print(dut, DUT_MSG_ERROR,
5506 "iwpriv htweptkip failed");
5507 }
5508 }
5509
5510 val = get_param(cmd, "txBandwidth");
5511 if (val) {
5512 switch (get_driver_type()) {
5513 case DRIVER_ATHEROS:
5514 if (ath_set_width(dut, conn, intf, val) < 0) {
5515 send_resp(dut, conn, SIGMA_ERROR,
5516 "ErrorCode,Failed to set txBandwidth");
5517 return 0;
5518 }
5519 break;
5520 default:
5521 sigma_dut_print(dut, DUT_MSG_ERROR,
5522 "Setting txBandwidth not supported");
5523 break;
5524 }
5525 }
5526
5527 return cmd_sta_set_wireless_common(intf, dut, conn, cmd);
5528}
5529
5530
5531static int sta_set_wireless_60g(struct sigma_dut *dut,
5532 struct sigma_conn *conn,
5533 struct sigma_cmd *cmd)
5534{
5535 const char *dev_role = get_param(cmd, "DevRole");
5536
5537 if (!dev_role) {
5538 send_resp(dut, conn, SIGMA_INVALID,
5539 "ErrorCode,DevRole not specified");
5540 return 0;
5541 }
5542
5543 if (strcasecmp(dev_role, "PCP") == 0)
5544 return sta_set_60g_pcp(dut, conn, cmd);
5545 if (strcasecmp(dev_role, "STA") == 0)
5546 return sta_set_60g_sta(dut, conn, cmd);
5547 send_resp(dut, conn, SIGMA_INVALID,
5548 "ErrorCode,DevRole not supported");
5549 return 0;
5550}
5551
5552
5553static int cmd_sta_set_wireless(struct sigma_dut *dut, struct sigma_conn *conn,
5554 struct sigma_cmd *cmd)
5555{
5556 const char *val;
5557
5558 val = get_param(cmd, "Program");
5559 if (val) {
5560 if (strcasecmp(val, "11n") == 0)
5561 return cmd_sta_set_11n(dut, conn, cmd);
5562 if (strcasecmp(val, "VHT") == 0)
5563 return cmd_sta_set_wireless_vht(dut, conn, cmd);
5564 if (strcasecmp(val, "60ghz") == 0)
5565 return sta_set_wireless_60g(dut, conn, cmd);
5566 send_resp(dut, conn, SIGMA_ERROR,
5567 "ErrorCode,Program value not supported");
5568 } else {
5569 send_resp(dut, conn, SIGMA_ERROR,
5570 "ErrorCode,Program argument not available");
5571 }
5572
5573 return 0;
5574}
5575
5576
5577static void ath_sta_inject_frame(struct sigma_dut *dut, const char *intf,
5578 int tid)
5579{
5580 char buf[100];
5581 int tid_to_dscp [] = { 0x00, 0x20, 0x40, 0x60, 0x80, 0xa0, 0xc0, 0xe0 };
5582
Pradeep Reddy POTTETId31d1322016-10-13 17:22:03 +05305583 if (tid < 0 ||
5584 tid >= (int) (sizeof(tid_to_dscp) / sizeof(tid_to_dscp[0]))) {
5585 sigma_dut_print(dut, DUT_MSG_ERROR, "Unsupported TID: %d", tid);
5586 return;
5587 }
5588
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005589 /*
5590 * Two ways to ensure that addba request with a
5591 * non zero TID could be sent out. EV 117296
5592 */
5593 snprintf(buf, sizeof(buf),
5594 "ping -c 8 -Q %d `arp -a | grep wlan0 | awk '{print $2}' | tr -d '()'`",
5595 tid);
5596 if (system(buf) != 0) {
5597 sigma_dut_print(dut, DUT_MSG_ERROR,
5598 "Ping did not send out");
5599 }
5600
5601 snprintf(buf, sizeof(buf),
5602 "iwconfig %s | grep Access | awk '{print $6}' > %s",
5603 intf, VI_QOS_TMP_FILE);
5604 if (system(buf) != 0)
5605 return;
5606
5607 snprintf(buf, sizeof(buf),
5608 "ifconfig %s | grep HWaddr | cut -b 39-56 >> %s",
5609 intf, VI_QOS_TMP_FILE);
5610 if (system(buf) != 0)
5611 sigma_dut_print(dut, DUT_MSG_ERROR, "HWaddr matching failed");
5612
5613 snprintf(buf,sizeof(buf), "sed -n '3,$p' %s >> %s",
5614 VI_QOS_REFFILE, VI_QOS_TMP_FILE);
5615 if (system(buf) != 0) {
5616 sigma_dut_print(dut, DUT_MSG_ERROR,
5617 "VI_QOS_TEMP_FILE generation error failed");
5618 }
5619 snprintf(buf, sizeof(buf), "sed '5 c %x' %s > %s",
5620 tid_to_dscp[tid], VI_QOS_TMP_FILE, VI_QOS_FILE);
5621 if (system(buf) != 0) {
5622 sigma_dut_print(dut, DUT_MSG_ERROR,
5623 "VI_QOS_FILE generation failed");
5624 }
5625
5626 snprintf(buf, sizeof(buf), "sed '5 c %x' %s > %s",
5627 tid_to_dscp[tid], VI_QOS_TMP_FILE, VI_QOS_FILE);
5628 if (system(buf) != 0) {
5629 sigma_dut_print(dut, DUT_MSG_ERROR,
5630 "VI_QOS_FILE generation failed");
5631 }
5632
5633 snprintf(buf, sizeof(buf), "ethinject %s %s", intf, VI_QOS_FILE);
5634 if (system(buf) != 0) {
5635 }
5636}
5637
5638
5639static int ath_sta_send_addba(struct sigma_dut *dut, struct sigma_conn *conn,
5640 struct sigma_cmd *cmd)
5641{
5642 const char *intf = get_param(cmd, "Interface");
5643 const char *val;
5644 int tid = 0;
5645 char buf[100];
5646
5647 val = get_param(cmd, "TID");
5648 if (val) {
5649 tid = atoi(val);
5650 if (tid)
5651 ath_sta_inject_frame(dut, intf, tid);
5652 }
5653
5654 /* Command sequence for ADDBA request on Peregrine based devices */
5655 snprintf(buf, sizeof(buf), "iwpriv %s setaddbaoper 1", intf);
5656 if (system(buf) != 0) {
5657 sigma_dut_print(dut, DUT_MSG_ERROR,
5658 "iwpriv setaddbaoper failed");
5659 }
5660
5661 snprintf(buf, sizeof(buf), "wifitool %s senddelba 1 %d 1 4", intf, tid);
5662 if (system(buf) != 0) {
5663 sigma_dut_print(dut, DUT_MSG_ERROR,
5664 "wifitool senddelba failed");
5665 }
5666
5667 snprintf(buf, sizeof(buf), "wifitool %s sendaddba 1 %d 64", intf, tid);
5668 if (system(buf) != 0) {
5669 sigma_dut_print(dut, DUT_MSG_ERROR,
5670 "wifitool sendaddba failed");
5671 }
5672
5673 /* UNSUPPORTED: val = get_param(cmd, "Dest_mac"); */
5674
5675 return 1;
5676}
5677
5678
Lior David9981b512017-01-20 13:16:40 +02005679#ifdef __linux__
5680
5681static int wil6210_send_addba(struct sigma_dut *dut, const char *dest_mac,
5682 int agg_size)
5683{
5684 char dir[128], buf[128];
5685 FILE *f;
5686 regex_t re;
5687 regmatch_t m[2];
5688 int rc, ret = -1, vring_id, found;
5689
5690 if (wil6210_get_debugfs_dir(dut, dir, sizeof(dir))) {
5691 sigma_dut_print(dut, DUT_MSG_ERROR,
5692 "failed to get wil6210 debugfs dir");
5693 return -1;
5694 }
5695
5696 snprintf(buf, sizeof(buf), "%s/vrings", dir);
5697 f = fopen(buf, "r");
5698 if (!f) {
5699 sigma_dut_print(dut, DUT_MSG_ERROR, "failed to open: %s", buf);
5700 return -1;
5701 }
5702
5703 if (regcomp(&re, "VRING tx_[ \t]*([0-9]+)", REG_EXTENDED)) {
5704 sigma_dut_print(dut, DUT_MSG_ERROR, "regcomp failed");
5705 goto out;
5706 }
5707
5708 /* find TX VRING for the mac address */
5709 found = 0;
5710 while (fgets(buf, sizeof(buf), f)) {
5711 if (strcasestr(buf, dest_mac)) {
5712 found = 1;
5713 break;
5714 }
5715 }
5716
5717 if (!found) {
5718 sigma_dut_print(dut, DUT_MSG_ERROR,
5719 "no TX VRING for %s", dest_mac);
5720 goto out;
5721 }
5722
5723 /* extract VRING ID, "VRING tx_<id> = {" */
5724 if (!fgets(buf, sizeof(buf), f)) {
5725 sigma_dut_print(dut, DUT_MSG_ERROR,
5726 "no VRING start line for %s", dest_mac);
5727 goto out;
5728 }
5729
5730 rc = regexec(&re, buf, 2, m, 0);
5731 regfree(&re);
5732 if (rc || m[1].rm_so < 0) {
5733 sigma_dut_print(dut, DUT_MSG_ERROR,
5734 "no VRING TX ID for %s", dest_mac);
5735 goto out;
5736 }
5737 buf[m[1].rm_eo] = 0;
5738 vring_id = atoi(&buf[m[1].rm_so]);
5739
5740 /* send the addba command */
5741 fclose(f);
5742 snprintf(buf, sizeof(buf), "%s/back", dir);
5743 f = fopen(buf, "w");
5744 if (!f) {
5745 sigma_dut_print(dut, DUT_MSG_ERROR,
5746 "failed to open: %s", buf);
5747 return -1;
5748 }
5749
5750 fprintf(f, "add %d %d\n", vring_id, agg_size);
5751
5752 ret = 0;
5753
5754out:
5755 fclose(f);
5756
5757 return ret;
5758}
5759
5760
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005761static int send_addba_60g(struct sigma_dut *dut, struct sigma_conn *conn,
5762 struct sigma_cmd *cmd)
5763{
5764 const char *val;
5765 int tid = 0;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005766
5767 val = get_param(cmd, "TID");
5768 if (val) {
5769 tid = atoi(val);
5770 if (tid != 0) {
5771 sigma_dut_print(dut, DUT_MSG_ERROR,
5772 "Ignore TID %d for send_addba use TID 0 for 60g since only 0 required on TX",
5773 tid);
5774 }
5775 }
5776
5777 val = get_param(cmd, "Dest_mac");
5778 if (!val) {
5779 sigma_dut_print(dut, DUT_MSG_ERROR,
5780 "Currently not supporting addba for 60G without Dest_mac");
5781 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
5782 }
5783
Lior David9981b512017-01-20 13:16:40 +02005784 if (wil6210_send_addba(dut, val, dut->back_rcv_buf))
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005785 return -1;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005786
5787 return 1;
5788}
5789
Lior David9981b512017-01-20 13:16:40 +02005790#endif /* __linux__ */
5791
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005792
5793static int cmd_sta_send_addba(struct sigma_dut *dut, struct sigma_conn *conn,
5794 struct sigma_cmd *cmd)
5795{
5796 switch (get_driver_type()) {
5797 case DRIVER_ATHEROS:
5798 return ath_sta_send_addba(dut, conn, cmd);
Lior David9981b512017-01-20 13:16:40 +02005799#ifdef __linux__
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005800 case DRIVER_WIL6210:
5801 return send_addba_60g(dut, conn, cmd);
Lior David9981b512017-01-20 13:16:40 +02005802#endif /* __linux__ */
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005803 default:
5804 /*
5805 * There is no driver specific implementation for other drivers.
5806 * Ignore the command and report COMPLETE since the following
5807 * throughput test operation will end up sending ADDBA anyway.
5808 */
5809 return 1;
5810 }
5811}
5812
5813
5814int inject_eth_frame(int s, const void *data, size_t len,
5815 unsigned short ethtype, char *dst, char *src)
5816{
5817 struct iovec iov[4] = {
5818 {
5819 .iov_base = dst,
5820 .iov_len = ETH_ALEN,
5821 },
5822 {
5823 .iov_base = src,
5824 .iov_len = ETH_ALEN,
5825 },
5826 {
5827 .iov_base = &ethtype,
5828 .iov_len = sizeof(unsigned short),
5829 },
5830 {
5831 .iov_base = (void *) data,
5832 .iov_len = len,
5833 }
5834 };
5835 struct msghdr msg = {
5836 .msg_name = NULL,
5837 .msg_namelen = 0,
5838 .msg_iov = iov,
5839 .msg_iovlen = 4,
5840 .msg_control = NULL,
5841 .msg_controllen = 0,
5842 .msg_flags = 0,
5843 };
5844
5845 return sendmsg(s, &msg, 0);
5846}
5847
5848#if defined(__linux__) || defined(__QNXNTO__)
5849
5850int inject_frame(int s, const void *data, size_t len, int encrypt)
5851{
5852#define IEEE80211_RADIOTAP_F_WEP 0x04
5853#define IEEE80211_RADIOTAP_F_FRAG 0x08
5854 unsigned char rtap_hdr[] = {
5855 0x00, 0x00, /* radiotap version */
5856 0x0e, 0x00, /* radiotap length */
5857 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
5858 IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
5859 0x00, /* padding */
5860 0x00, 0x00, /* RX and TX flags to indicate that */
5861 0x00, 0x00, /* this is the injected frame directly */
5862 };
5863 struct iovec iov[2] = {
5864 {
5865 .iov_base = &rtap_hdr,
5866 .iov_len = sizeof(rtap_hdr),
5867 },
5868 {
5869 .iov_base = (void *) data,
5870 .iov_len = len,
5871 }
5872 };
5873 struct msghdr msg = {
5874 .msg_name = NULL,
5875 .msg_namelen = 0,
5876 .msg_iov = iov,
5877 .msg_iovlen = 2,
5878 .msg_control = NULL,
5879 .msg_controllen = 0,
5880 .msg_flags = 0,
5881 };
5882
5883 if (encrypt)
5884 rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
5885
5886 return sendmsg(s, &msg, 0);
5887}
5888
5889
5890int open_monitor(const char *ifname)
5891{
5892#ifdef __QNXNTO__
5893 struct sockaddr_dl ll;
5894 int s;
5895
5896 memset(&ll, 0, sizeof(ll));
5897 ll.sdl_family = AF_LINK;
5898 ll.sdl_index = if_nametoindex(ifname);
5899 if (ll.sdl_index == 0) {
5900 perror("if_nametoindex");
5901 return -1;
5902 }
5903 s = socket(PF_INET, SOCK_RAW, 0);
5904#else /* __QNXNTO__ */
5905 struct sockaddr_ll ll;
5906 int s;
5907
5908 memset(&ll, 0, sizeof(ll));
5909 ll.sll_family = AF_PACKET;
5910 ll.sll_ifindex = if_nametoindex(ifname);
5911 if (ll.sll_ifindex == 0) {
5912 perror("if_nametoindex");
5913 return -1;
5914 }
5915 s = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
5916#endif /* __QNXNTO__ */
5917 if (s < 0) {
5918 perror("socket[PF_PACKET,SOCK_RAW]");
5919 return -1;
5920 }
5921
5922 if (bind(s, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
5923 perror("monitor socket bind");
5924 close(s);
5925 return -1;
5926 }
5927
5928 return s;
5929}
5930
5931
5932static int hex2num(char c)
5933{
5934 if (c >= '0' && c <= '9')
5935 return c - '0';
5936 if (c >= 'a' && c <= 'f')
5937 return c - 'a' + 10;
5938 if (c >= 'A' && c <= 'F')
5939 return c - 'A' + 10;
5940 return -1;
5941}
5942
5943
5944int hwaddr_aton(const char *txt, unsigned char *addr)
5945{
5946 int i;
5947
5948 for (i = 0; i < 6; i++) {
5949 int a, b;
5950
5951 a = hex2num(*txt++);
5952 if (a < 0)
5953 return -1;
5954 b = hex2num(*txt++);
5955 if (b < 0)
5956 return -1;
5957 *addr++ = (a << 4) | b;
5958 if (i < 5 && *txt++ != ':')
5959 return -1;
5960 }
5961
5962 return 0;
5963}
5964
5965#endif /* defined(__linux__) || defined(__QNXNTO__) */
5966
5967enum send_frame_type {
5968 DISASSOC, DEAUTH, SAQUERY, AUTH, ASSOCREQ, REASSOCREQ, DLS_REQ
5969};
5970enum send_frame_protection {
5971 CORRECT_KEY, INCORRECT_KEY, UNPROTECTED
5972};
5973
5974
5975static int sta_inject_frame(struct sigma_dut *dut, struct sigma_conn *conn,
5976 enum send_frame_type frame,
5977 enum send_frame_protection protected,
5978 const char *dest)
5979{
5980#ifdef __linux__
5981 unsigned char buf[1000], *pos;
5982 int s, res;
5983 char bssid[20], addr[20];
5984 char result[32], ssid[100];
5985 size_t ssid_len;
5986
5987 if (get_wpa_status(get_station_ifname(), "wpa_state", result,
5988 sizeof(result)) < 0 ||
5989 strncmp(result, "COMPLETED", 9) != 0) {
5990 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Not connected");
5991 return 0;
5992 }
5993
5994 if (get_wpa_status(get_station_ifname(), "bssid", bssid, sizeof(bssid))
5995 < 0) {
5996 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not get "
5997 "current BSSID");
5998 return 0;
5999 }
6000
6001 if (get_wpa_status(get_station_ifname(), "address", addr, sizeof(addr))
6002 < 0) {
6003 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not get "
6004 "own MAC address");
6005 return 0;
6006 }
6007
6008 if (get_wpa_status(get_station_ifname(), "ssid", ssid, sizeof(ssid))
6009 < 0) {
6010 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not get "
6011 "current SSID");
6012 return 0;
6013 }
6014 ssid_len = strlen(ssid);
6015
6016 pos = buf;
6017
6018 /* Frame Control */
6019 switch (frame) {
6020 case DISASSOC:
6021 *pos++ = 0xa0;
6022 break;
6023 case DEAUTH:
6024 *pos++ = 0xc0;
6025 break;
6026 case SAQUERY:
6027 *pos++ = 0xd0;
6028 break;
6029 case AUTH:
6030 *pos++ = 0xb0;
6031 break;
6032 case ASSOCREQ:
6033 *pos++ = 0x00;
6034 break;
6035 case REASSOCREQ:
6036 *pos++ = 0x20;
6037 break;
6038 case DLS_REQ:
6039 *pos++ = 0xd0;
6040 break;
6041 }
6042
6043 if (protected == INCORRECT_KEY)
6044 *pos++ = 0x40; /* Set Protected field to 1 */
6045 else
6046 *pos++ = 0x00;
6047
6048 /* Duration */
6049 *pos++ = 0x00;
6050 *pos++ = 0x00;
6051
6052 /* addr1 = DA (current AP) */
6053 hwaddr_aton(bssid, pos);
6054 pos += 6;
6055 /* addr2 = SA (own address) */
6056 hwaddr_aton(addr, pos);
6057 pos += 6;
6058 /* addr3 = BSSID (current AP) */
6059 hwaddr_aton(bssid, pos);
6060 pos += 6;
6061
6062 /* Seq# (to be filled by driver/mac80211) */
6063 *pos++ = 0x00;
6064 *pos++ = 0x00;
6065
6066 if (protected == INCORRECT_KEY) {
6067 /* CCMP parameters */
6068 memcpy(pos, "\x61\x01\x00\x20\x00\x10\x00\x00", 8);
6069 pos += 8;
6070 }
6071
6072 if (protected == INCORRECT_KEY) {
6073 switch (frame) {
6074 case DEAUTH:
6075 /* Reason code (encrypted) */
6076 memcpy(pos, "\xa7\x39", 2);
6077 pos += 2;
6078 break;
6079 case DISASSOC:
6080 /* Reason code (encrypted) */
6081 memcpy(pos, "\xa7\x39", 2);
6082 pos += 2;
6083 break;
6084 case SAQUERY:
6085 /* Category|Action|TransID (encrypted) */
6086 memcpy(pos, "\x6f\xbd\xe9\x4d", 4);
6087 pos += 4;
6088 break;
6089 default:
6090 return -1;
6091 }
6092
6093 /* CCMP MIC */
6094 memcpy(pos, "\xc8\xd8\x3b\x06\x5d\xb7\x25\x68", 8);
6095 pos += 8;
6096 } else {
6097 switch (frame) {
6098 case DEAUTH:
6099 /* reason code = 8 */
6100 *pos++ = 0x08;
6101 *pos++ = 0x00;
6102 break;
6103 case DISASSOC:
6104 /* reason code = 8 */
6105 *pos++ = 0x08;
6106 *pos++ = 0x00;
6107 break;
6108 case SAQUERY:
6109 /* Category - SA Query */
6110 *pos++ = 0x08;
6111 /* SA query Action - Request */
6112 *pos++ = 0x00;
6113 /* Transaction ID */
6114 *pos++ = 0x12;
6115 *pos++ = 0x34;
6116 break;
6117 case AUTH:
6118 /* Auth Alg (Open) */
6119 *pos++ = 0x00;
6120 *pos++ = 0x00;
6121 /* Seq# */
6122 *pos++ = 0x01;
6123 *pos++ = 0x00;
6124 /* Status code */
6125 *pos++ = 0x00;
6126 *pos++ = 0x00;
6127 break;
6128 case ASSOCREQ:
6129 /* Capability Information */
6130 *pos++ = 0x31;
6131 *pos++ = 0x04;
6132 /* Listen Interval */
6133 *pos++ = 0x0a;
6134 *pos++ = 0x00;
6135 /* SSID */
6136 *pos++ = 0x00;
6137 *pos++ = ssid_len;
6138 memcpy(pos, ssid, ssid_len);
6139 pos += ssid_len;
6140 /* Supported Rates */
6141 memcpy(pos, "\x01\x08\x02\x04\x0b\x16\x0c\x12\x18\x24",
6142 10);
6143 pos += 10;
6144 /* Extended Supported Rates */
6145 memcpy(pos, "\x32\x04\x30\x48\x60\x6c", 6);
6146 pos += 6;
6147 /* RSN */
6148 memcpy(pos, "\x30\x1a\x01\x00\x00\x0f\xac\x04\x01\x00"
6149 "\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x02\xc0"
6150 "\x00\x00\x00\x00\x0f\xac\x06", 28);
6151 pos += 28;
6152 break;
6153 case REASSOCREQ:
6154 /* Capability Information */
6155 *pos++ = 0x31;
6156 *pos++ = 0x04;
6157 /* Listen Interval */
6158 *pos++ = 0x0a;
6159 *pos++ = 0x00;
6160 /* Current AP */
6161 hwaddr_aton(bssid, pos);
6162 pos += 6;
6163 /* SSID */
6164 *pos++ = 0x00;
6165 *pos++ = ssid_len;
6166 memcpy(pos, ssid, ssid_len);
6167 pos += ssid_len;
6168 /* Supported Rates */
6169 memcpy(pos, "\x01\x08\x02\x04\x0b\x16\x0c\x12\x18\x24",
6170 10);
6171 pos += 10;
6172 /* Extended Supported Rates */
6173 memcpy(pos, "\x32\x04\x30\x48\x60\x6c", 6);
6174 pos += 6;
6175 /* RSN */
6176 memcpy(pos, "\x30\x1a\x01\x00\x00\x0f\xac\x04\x01\x00"
6177 "\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x02\xc0"
6178 "\x00\x00\x00\x00\x0f\xac\x06", 28);
6179 pos += 28;
6180 break;
6181 case DLS_REQ:
6182 /* Category - DLS */
6183 *pos++ = 0x02;
6184 /* DLS Action - Request */
6185 *pos++ = 0x00;
6186 /* Destination MACAddress */
6187 if (dest)
6188 hwaddr_aton(dest, pos);
6189 else
6190 memset(pos, 0, 6);
6191 pos += 6;
6192 /* Source MACAddress */
6193 hwaddr_aton(addr, pos);
6194 pos += 6;
6195 /* Capability Information */
6196 *pos++ = 0x10; /* Privacy */
6197 *pos++ = 0x06; /* QoS */
6198 /* DLS Timeout Value */
6199 *pos++ = 0x00;
6200 *pos++ = 0x01;
6201 /* Supported rates */
6202 *pos++ = 0x01;
6203 *pos++ = 0x08;
6204 *pos++ = 0x0c; /* 6 Mbps */
6205 *pos++ = 0x12; /* 9 Mbps */
6206 *pos++ = 0x18; /* 12 Mbps */
6207 *pos++ = 0x24; /* 18 Mbps */
6208 *pos++ = 0x30; /* 24 Mbps */
6209 *pos++ = 0x48; /* 36 Mbps */
6210 *pos++ = 0x60; /* 48 Mbps */
6211 *pos++ = 0x6c; /* 54 Mbps */
6212 /* TODO: Extended Supported Rates */
6213 /* TODO: HT Capabilities */
6214 break;
6215 }
6216 }
6217
6218 s = open_monitor("sigmadut");
6219 if (s < 0) {
6220 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to open "
6221 "monitor socket");
6222 return 0;
6223 }
6224
6225 res = inject_frame(s, buf, pos - buf, protected == CORRECT_KEY);
6226 if (res < 0) {
6227 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to "
6228 "inject frame");
Pradeep Reddy POTTETI673d85c2016-07-26 19:08:07 +05306229 close(s);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02006230 return 0;
6231 }
6232 if (res < pos - buf) {
6233 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Only partial "
6234 "frame sent");
Pradeep Reddy POTTETI673d85c2016-07-26 19:08:07 +05306235 close(s);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02006236 return 0;
6237 }
6238
6239 close(s);
6240
6241 return 1;
6242#else /* __linux__ */
6243 send_resp(dut, conn, SIGMA_ERROR, "errorCode,sta_send_frame not "
6244 "yet supported");
6245 return 0;
6246#endif /* __linux__ */
6247}
6248
6249
6250static int cmd_sta_send_frame_tdls(struct sigma_dut *dut,
6251 struct sigma_conn *conn,
6252 struct sigma_cmd *cmd)
6253{
6254 const char *intf = get_param(cmd, "Interface");
6255 const char *sta, *val;
6256 unsigned char addr[ETH_ALEN];
6257 char buf[100];
6258
6259 sta = get_param(cmd, "peer");
6260 if (sta == NULL)
6261 sta = get_param(cmd, "station");
6262 if (sta == NULL) {
6263 send_resp(dut, conn, SIGMA_ERROR,
6264 "ErrorCode,Missing peer address");
6265 return 0;
6266 }
6267 if (hwaddr_aton(sta, addr) < 0) {
6268 send_resp(dut, conn, SIGMA_ERROR,
6269 "ErrorCode,Invalid peer address");
6270 return 0;
6271 }
6272
6273 val = get_param(cmd, "type");
6274 if (val == NULL)
6275 return -1;
6276
6277 if (strcasecmp(val, "DISCOVERY") == 0) {
6278 snprintf(buf, sizeof(buf), "TDLS_DISCOVER %s", sta);
6279 if (wpa_command(intf, buf) < 0) {
6280 send_resp(dut, conn, SIGMA_ERROR,
6281 "ErrorCode,Failed to send TDLS discovery");
6282 return 0;
6283 }
6284 return 1;
6285 }
6286
6287 if (strcasecmp(val, "SETUP") == 0) {
6288 int status = 0, timeout = 0;
6289
6290 val = get_param(cmd, "Status");
6291 if (val)
6292 status = atoi(val);
6293
6294 val = get_param(cmd, "Timeout");
6295 if (val)
6296 timeout = atoi(val);
6297
6298 if (status != 0 && status != 37) {
6299 send_resp(dut, conn, SIGMA_ERROR,
6300 "ErrorCode,Unsupported status value");
6301 return 0;
6302 }
6303
6304 if (timeout != 0 && timeout != 301) {
6305 send_resp(dut, conn, SIGMA_ERROR,
6306 "ErrorCode,Unsupported timeout value");
6307 return 0;
6308 }
6309
6310 if (status && timeout) {
6311 send_resp(dut, conn, SIGMA_ERROR,
6312 "ErrorCode,Unsupported timeout+status "
6313 "combination");
6314 return 0;
6315 }
6316
6317 if (status == 37 &&
6318 wpa_command(intf, "SET tdls_testing 0x200")) {
6319 send_resp(dut, conn, SIGMA_ERROR,
6320 "ErrorCode,Failed to enable "
6321 "decline setup response test mode");
6322 return 0;
6323 }
6324
6325 if (timeout == 301) {
6326 int res;
6327 if (dut->no_tpk_expiration)
6328 res = wpa_command(intf,
6329 "SET tdls_testing 0x108");
6330 else
6331 res = wpa_command(intf,
6332 "SET tdls_testing 0x8");
6333 if (res) {
6334 send_resp(dut, conn, SIGMA_ERROR,
6335 "ErrorCode,Failed to set short TPK "
6336 "lifetime");
6337 return 0;
6338 }
6339 }
6340
6341 snprintf(buf, sizeof(buf), "TDLS_SETUP %s", sta);
6342 if (wpa_command(intf, buf) < 0) {
6343 send_resp(dut, conn, SIGMA_ERROR,
6344 "ErrorCode,Failed to send TDLS setup");
6345 return 0;
6346 }
6347 return 1;
6348 }
6349
6350 if (strcasecmp(val, "TEARDOWN") == 0) {
6351 snprintf(buf, sizeof(buf), "TDLS_TEARDOWN %s", sta);
6352 if (wpa_command(intf, buf) < 0) {
6353 send_resp(dut, conn, SIGMA_ERROR,
6354 "ErrorCode,Failed to send TDLS teardown");
6355 return 0;
6356 }
6357 return 1;
6358 }
6359
6360 send_resp(dut, conn, SIGMA_ERROR,
6361 "ErrorCode,Unsupported TDLS frame");
6362 return 0;
6363}
6364
6365
6366static int sta_ap_known(const char *ifname, const char *bssid)
6367{
6368 char buf[4096];
6369
6370 snprintf(buf, sizeof(buf), "BSS %s", bssid);
6371 if (wpa_command_resp(ifname, buf, buf, sizeof(buf)) < 0)
6372 return 0;
6373 if (strncmp(buf, "id=", 3) != 0)
6374 return 0;
6375 return 1;
6376}
6377
6378
6379static int sta_scan_ap(struct sigma_dut *dut, const char *ifname,
6380 const char *bssid)
6381{
6382 int res;
6383 struct wpa_ctrl *ctrl;
6384 char buf[256];
6385
6386 if (sta_ap_known(ifname, bssid))
6387 return 0;
6388 sigma_dut_print(dut, DUT_MSG_DEBUG,
6389 "AP not in BSS table - start scan");
6390
6391 ctrl = open_wpa_mon(ifname);
6392 if (ctrl == NULL) {
6393 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to open "
6394 "wpa_supplicant monitor connection");
6395 return -1;
6396 }
6397
6398 if (wpa_command(ifname, "SCAN") < 0) {
6399 sigma_dut_print(dut, DUT_MSG_INFO, "Failed to start scan");
6400 wpa_ctrl_detach(ctrl);
6401 wpa_ctrl_close(ctrl);
6402 return -1;
6403 }
6404
6405 res = get_wpa_cli_event(dut, ctrl, "CTRL-EVENT-SCAN-RESULTS",
6406 buf, sizeof(buf));
6407
6408 wpa_ctrl_detach(ctrl);
6409 wpa_ctrl_close(ctrl);
6410
6411 if (res < 0) {
6412 sigma_dut_print(dut, DUT_MSG_INFO, "Scan did not complete");
6413 return -1;
6414 }
6415
6416 if (sta_ap_known(ifname, bssid))
6417 return 0;
6418 sigma_dut_print(dut, DUT_MSG_INFO, "AP not in BSS table");
6419 return -1;
6420}
6421
6422
6423static int cmd_sta_send_frame_hs2_neighadv(struct sigma_dut *dut,
6424 struct sigma_conn *conn,
6425 struct sigma_cmd *cmd,
6426 const char *intf)
6427{
6428 char buf[200];
6429
6430 snprintf(buf, sizeof(buf), "ndsend 2001:DB8::1 %s", intf);
6431 if (system(buf) != 0) {
6432 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Failed to run "
6433 "ndsend");
6434 return 0;
6435 }
6436
6437 return 1;
6438}
6439
6440
6441static int cmd_sta_send_frame_hs2_neighsolreq(struct sigma_dut *dut,
6442 struct sigma_conn *conn,
6443 struct sigma_cmd *cmd,
6444 const char *intf)
6445{
6446 char buf[200];
6447 const char *ip = get_param(cmd, "SenderIP");
6448
Peng Xu26b356d2017-10-04 17:58:16 -07006449 if (!ip)
6450 return 0;
6451
Jouni Malinencd4e3c32015-10-29 12:39:56 +02006452 snprintf(buf, sizeof(buf), "ndisc6 -nm %s %s -r 4", ip, intf);
6453 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
6454 if (system(buf) == 0) {
6455 sigma_dut_print(dut, DUT_MSG_INFO,
6456 "Neighbor Solicitation got a response "
6457 "for %s@%s", ip, intf);
6458 }
6459
6460 return 1;
6461}
6462
6463
6464static int cmd_sta_send_frame_hs2_arpprobe(struct sigma_dut *dut,
6465 struct sigma_conn *conn,
6466 struct sigma_cmd *cmd,
6467 const char *ifname)
6468{
6469 char buf[200];
6470 const char *ip = get_param(cmd, "SenderIP");
6471
6472 if (ip == NULL) {
6473 send_resp(dut, conn, SIGMA_ERROR,
6474 "ErrorCode,Missing SenderIP parameter");
6475 return 0;
6476 }
6477 snprintf(buf, sizeof(buf), "arping -I %s -D %s -c 4", ifname, ip);
6478 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
6479 if (system(buf) != 0) {
6480 sigma_dut_print(dut, DUT_MSG_INFO, "arping DAD got a response "
6481 "for %s@%s", ip, ifname);
6482 }
6483
6484 return 1;
6485}
6486
6487
6488static int cmd_sta_send_frame_hs2_arpannounce(struct sigma_dut *dut,
6489 struct sigma_conn *conn,
6490 struct sigma_cmd *cmd,
6491 const char *ifname)
6492{
6493 char buf[200];
6494 char ip[16];
6495 int s;
Peng Xub3756882017-10-04 14:39:09 -07006496 struct ifreq ifr;
6497 struct sockaddr_in saddr;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02006498
6499 s = socket(PF_INET, SOCK_DGRAM, 0);
Peng Xub3756882017-10-04 14:39:09 -07006500 if (s < 0) {
6501 perror("socket");
6502 return -1;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02006503 }
6504
Peng Xub3756882017-10-04 14:39:09 -07006505 memset(&ifr, 0, sizeof(ifr));
6506 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
6507 if (ioctl(s, SIOCGIFADDR, &ifr) < 0) {
6508 sigma_dut_print(dut, DUT_MSG_INFO,
6509 "Failed to get %s IP address: %s",
6510 ifname, strerror(errno));
6511 close(s);
6512 return -1;
6513 }
6514 close(s);
6515
6516 memcpy(&saddr, &ifr.ifr_addr, sizeof(struct sockaddr_in));
6517 strlcpy(ip, inet_ntoa(saddr.sin_addr), sizeof(ip));
6518
Jouni Malinencd4e3c32015-10-29 12:39:56 +02006519 snprintf(buf, sizeof(buf), "arping -I %s -s %s %s -c 4", ifname, ip,
6520 ip);
6521 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
6522 if (system(buf) != 0) {
6523 }
6524
6525 return 1;
6526}
6527
6528
6529static int cmd_sta_send_frame_hs2_arpreply(struct sigma_dut *dut,
6530 struct sigma_conn *conn,
6531 struct sigma_cmd *cmd,
6532 const char *ifname)
6533{
6534 char buf[200], addr[20];
6535 char dst[ETH_ALEN], src[ETH_ALEN];
6536 short ethtype = htons(ETH_P_ARP);
6537 char *pos;
6538 int s, res;
6539 const char *val;
6540 struct sockaddr_in taddr;
6541
6542 val = get_param(cmd, "dest");
6543 if (val)
6544 hwaddr_aton(val, (unsigned char *) dst);
6545
6546 val = get_param(cmd, "DestIP");
6547 if (val)
6548 inet_aton(val, &taddr.sin_addr);
Peng Xu151c9e12017-10-04 14:39:09 -07006549 else
6550 return -2;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02006551
6552 if (get_wpa_status(get_station_ifname(), "address", addr,
6553 sizeof(addr)) < 0)
6554 return -2;
6555 hwaddr_aton(addr, (unsigned char *) src);
6556
6557 pos = buf;
6558 *pos++ = 0x00;
6559 *pos++ = 0x01;
6560 *pos++ = 0x08;
6561 *pos++ = 0x00;
6562 *pos++ = 0x06;
6563 *pos++ = 0x04;
6564 *pos++ = 0x00;
6565 *pos++ = 0x02;
6566 memcpy(pos, src, ETH_ALEN);
6567 pos += ETH_ALEN;
6568 memcpy(pos, &taddr.sin_addr, 4);
6569 pos += 4;
6570 memcpy(pos, dst, ETH_ALEN);
6571 pos += ETH_ALEN;
6572 memcpy(pos, &taddr.sin_addr, 4);
6573 pos += 4;
6574
6575 s = open_monitor(get_station_ifname());
6576 if (s < 0) {
6577 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to open "
6578 "monitor socket");
6579 return 0;
6580 }
6581
6582 res = inject_eth_frame(s, buf, pos - buf, ethtype, dst, src);
6583 if (res < 0) {
6584 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to "
6585 "inject frame");
Pradeep Reddy POTTETI673d85c2016-07-26 19:08:07 +05306586 close(s);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02006587 return 0;
6588 }
6589
6590 close(s);
6591
6592 return 1;
6593}
6594
6595
6596static int cmd_sta_send_frame_hs2_dls_req(struct sigma_dut *dut,
6597 struct sigma_conn *conn,
6598 struct sigma_cmd *cmd,
6599 const char *intf, const char *dest)
6600{
6601 char buf[100];
6602
6603 if (if_nametoindex("sigmadut") == 0) {
6604 snprintf(buf, sizeof(buf),
6605 "iw dev %s interface add sigmadut type monitor",
6606 get_station_ifname());
6607 if (system(buf) != 0 ||
6608 if_nametoindex("sigmadut") == 0) {
6609 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to add "
6610 "monitor interface with '%s'", buf);
6611 return -2;
6612 }
6613 }
6614
6615 if (system("ifconfig sigmadut up") != 0) {
6616 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to set "
6617 "monitor interface up");
6618 return -2;
6619 }
6620
6621 return sta_inject_frame(dut, conn, DLS_REQ, UNPROTECTED, dest);
6622}
6623
6624
6625static int cmd_sta_send_frame_hs2(struct sigma_dut *dut,
6626 struct sigma_conn *conn,
6627 struct sigma_cmd *cmd)
6628{
6629 const char *intf = get_param(cmd, "Interface");
6630 const char *dest = get_param(cmd, "Dest");
6631 const char *type = get_param(cmd, "FrameName");
6632 const char *val;
6633 char buf[200], *pos, *end;
6634 int count, count2;
6635
6636 if (type == NULL)
6637 type = get_param(cmd, "Type");
6638
6639 if (intf == NULL || dest == NULL || type == NULL)
6640 return -1;
6641
6642 if (strcasecmp(type, "NeighAdv") == 0)
6643 return cmd_sta_send_frame_hs2_neighadv(dut, conn, cmd, intf);
6644
6645 if (strcasecmp(type, "NeighSolicitReq") == 0)
6646 return cmd_sta_send_frame_hs2_neighsolreq(dut, conn, cmd, intf);
6647
6648 if (strcasecmp(type, "ARPProbe") == 0)
6649 return cmd_sta_send_frame_hs2_arpprobe(dut, conn, cmd, intf);
6650
6651 if (strcasecmp(type, "ARPAnnounce") == 0)
6652 return cmd_sta_send_frame_hs2_arpannounce(dut, conn, cmd, intf);
6653
6654 if (strcasecmp(type, "ARPReply") == 0)
6655 return cmd_sta_send_frame_hs2_arpreply(dut, conn, cmd, intf);
6656
6657 if (strcasecmp(type, "DLS-request") == 0 ||
6658 strcasecmp(type, "DLSrequest") == 0)
6659 return cmd_sta_send_frame_hs2_dls_req(dut, conn, cmd, intf,
6660 dest);
6661
6662 if (strcasecmp(type, "ANQPQuery") != 0 &&
6663 strcasecmp(type, "Query") != 0) {
6664 send_resp(dut, conn, SIGMA_ERROR,
6665 "ErrorCode,Unsupported HS 2.0 send frame type");
6666 return 0;
6667 }
6668
6669 if (sta_scan_ap(dut, intf, dest) < 0) {
6670 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Could not find "
6671 "the requested AP");
6672 return 0;
6673 }
6674
6675 pos = buf;
6676 end = buf + sizeof(buf);
6677 count = 0;
6678 pos += snprintf(pos, end - pos, "ANQP_GET %s ", dest);
6679
6680 val = get_param(cmd, "ANQP_CAP_LIST");
6681 if (val && atoi(val)) {
6682 pos += snprintf(pos, end - pos, "%s257", count > 0 ? "," : "");
6683 count++;
6684 }
6685
6686 val = get_param(cmd, "VENUE_NAME");
6687 if (val && atoi(val)) {
6688 pos += snprintf(pos, end - pos, "%s258", count > 0 ? "," : "");
6689 count++;
6690 }
6691
6692 val = get_param(cmd, "NETWORK_AUTH_TYPE");
6693 if (val && atoi(val)) {
6694 pos += snprintf(pos, end - pos, "%s260", count > 0 ? "," : "");
6695 count++;
6696 }
6697
6698 val = get_param(cmd, "ROAMING_CONS");
6699 if (val && atoi(val)) {
6700 pos += snprintf(pos, end - pos, "%s261", count > 0 ? "," : "");
6701 count++;
6702 }
6703
6704 val = get_param(cmd, "IP_ADDR_TYPE_AVAILABILITY");
6705 if (val && atoi(val)) {
6706 pos += snprintf(pos, end - pos, "%s262", count > 0 ? "," : "");
6707 count++;
6708 }
6709
6710 val = get_param(cmd, "NAI_REALM_LIST");
6711 if (val && atoi(val)) {
6712 pos += snprintf(pos, end - pos, "%s263", count > 0 ? "," : "");
6713 count++;
6714 }
6715
6716 val = get_param(cmd, "3GPP_INFO");
6717 if (val && atoi(val)) {
6718 pos += snprintf(pos, end - pos, "%s264", count > 0 ? "," : "");
6719 count++;
6720 }
6721
6722 val = get_param(cmd, "DOMAIN_LIST");
6723 if (val && atoi(val)) {
6724 pos += snprintf(pos, end - pos, "%s268", count > 0 ? "," : "");
6725 count++;
6726 }
6727
6728 if (count && wpa_command(intf, buf)) {
6729 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,ANQP_GET failed");
6730 return 0;
6731 }
6732
6733 pos = buf;
6734 end = buf + sizeof(buf);
6735 count2 = 0;
6736 pos += snprintf(pos, end - pos, "HS20_ANQP_GET %s ", dest);
6737
6738 val = get_param(cmd, "HS_CAP_LIST");
6739 if (val && atoi(val)) {
6740 pos += snprintf(pos, end - pos, "%s2", count2 > 0 ? "," : "");
6741 count2++;
6742 }
6743
6744 val = get_param(cmd, "OPER_NAME");
6745 if (val && atoi(val)) {
6746 pos += snprintf(pos, end - pos, "%s3", count2 > 0 ? "," : "");
6747 count2++;
6748 }
6749
6750 val = get_param(cmd, "WAN_METRICS");
6751 if (!val)
6752 val = get_param(cmd, "WAN_MAT");
6753 if (!val)
6754 val = get_param(cmd, "WAN_MET");
6755 if (val && atoi(val)) {
6756 pos += snprintf(pos, end - pos, "%s4", count2 > 0 ? "," : "");
6757 count2++;
6758 }
6759
6760 val = get_param(cmd, "CONNECTION_CAPABILITY");
6761 if (val && atoi(val)) {
6762 pos += snprintf(pos, end - pos, "%s5", count2 > 0 ? "," : "");
6763 count2++;
6764 }
6765
6766 val = get_param(cmd, "OP_CLASS");
6767 if (val && atoi(val)) {
6768 pos += snprintf(pos, end - pos, "%s7", count2 > 0 ? "," : "");
6769 count2++;
6770 }
6771
6772 val = get_param(cmd, "OSU_PROVIDER_LIST");
6773 if (val && atoi(val)) {
6774 pos += snprintf(pos, end - pos, "%s8", count2 > 0 ? "," : "");
6775 count2++;
6776 }
6777
6778 if (count && count2) {
6779 sigma_dut_print(dut, DUT_MSG_DEBUG, "Wait before sending out "
6780 "second query");
6781 sleep(1);
6782 }
6783
6784 if (count2 && wpa_command(intf, buf)) {
6785 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,HS20_ANQP_GET "
6786 "failed");
6787 return 0;
6788 }
6789
6790 val = get_param(cmd, "NAI_HOME_REALM_LIST");
6791 if (val) {
6792 if (count || count2) {
6793 sigma_dut_print(dut, DUT_MSG_DEBUG, "Wait before "
6794 "sending out second query");
6795 sleep(1);
6796 }
6797
6798 if (strcmp(val, "1") == 0)
6799 val = "mail.example.com";
6800 snprintf(buf, end - pos,
6801 "HS20_GET_NAI_HOME_REALM_LIST %s realm=%s",
6802 dest, val);
6803 if (wpa_command(intf, buf)) {
6804 send_resp(dut, conn, SIGMA_ERROR,
6805 "ErrorCode,HS20_GET_NAI_HOME_REALM_LIST "
6806 "failed");
6807 return 0;
6808 }
6809 }
6810
6811 val = get_param(cmd, "ICON_REQUEST");
6812 if (val) {
6813 if (count || count2) {
6814 sigma_dut_print(dut, DUT_MSG_DEBUG, "Wait before "
6815 "sending out second query");
6816 sleep(1);
6817 }
6818
6819 snprintf(buf, end - pos,
6820 "HS20_ICON_REQUEST %s %s", dest, val);
6821 if (wpa_command(intf, buf)) {
6822 send_resp(dut, conn, SIGMA_ERROR,
6823 "ErrorCode,HS20_ICON_REQUEST failed");
6824 return 0;
6825 }
6826 }
6827
6828 return 1;
6829}
6830
6831
6832static int ath_sta_send_frame_vht(struct sigma_dut *dut,
6833 struct sigma_conn *conn,
6834 struct sigma_cmd *cmd)
6835{
6836 const char *val;
6837 char *ifname;
6838 char buf[100];
6839 int chwidth, nss;
6840
6841 val = get_param(cmd, "framename");
6842 if (!val)
6843 return -1;
6844 sigma_dut_print(dut, DUT_MSG_DEBUG, "framename is %s", val);
6845
6846 /* Command sequence to generate Op mode notification */
6847 if (val && strcasecmp(val, "Op_md_notif_frm") == 0) {
6848 ifname = get_station_ifname();
6849
6850 /* Disable STBC */
6851 snprintf(buf, sizeof(buf),
6852 "iwpriv %s tx_stbc 0", ifname);
6853 if (system(buf) != 0) {
6854 sigma_dut_print(dut, DUT_MSG_ERROR,
6855 "iwpriv tx_stbc 0 failed!");
6856 }
6857
6858 /* Extract Channel width */
6859 val = get_param(cmd, "Channel_width");
6860 if (val) {
6861 switch (atoi(val)) {
6862 case 20:
6863 chwidth = 0;
6864 break;
6865 case 40:
6866 chwidth = 1;
6867 break;
6868 case 80:
6869 chwidth = 2;
6870 break;
6871 case 160:
6872 chwidth = 3;
6873 break;
6874 default:
6875 chwidth = 2;
6876 break;
6877 }
6878
6879 snprintf(buf, sizeof(buf), "iwpriv %s chwidth %d",
6880 ifname, chwidth);
6881 if (system(buf) != 0) {
6882 sigma_dut_print(dut, DUT_MSG_ERROR,
6883 "iwpriv chwidth failed!");
6884 }
6885 }
6886
6887 /* Extract NSS */
6888 val = get_param(cmd, "NSS");
6889 if (val) {
6890 switch (atoi(val)) {
6891 case 1:
6892 nss = 1;
6893 break;
6894 case 2:
6895 nss = 3;
6896 break;
6897 case 3:
6898 nss = 7;
6899 break;
6900 default:
6901 /* We do not support NSS > 3 */
6902 nss = 3;
6903 break;
6904 }
6905 snprintf(buf, sizeof(buf),
6906 "iwpriv %s rxchainmask %d", ifname, nss);
6907 if (system(buf) != 0) {
6908 sigma_dut_print(dut, DUT_MSG_ERROR,
6909 "iwpriv rxchainmask failed!");
6910 }
6911 }
6912
6913 /* Opmode notify */
6914 snprintf(buf, sizeof(buf), "iwpriv %s opmode_notify 1", ifname);
6915 if (system(buf) != 0) {
6916 sigma_dut_print(dut, DUT_MSG_ERROR,
6917 "iwpriv opmode_notify failed!");
6918 } else {
6919 sigma_dut_print(dut, DUT_MSG_INFO,
6920 "Sent out the notify frame!");
6921 }
6922 }
6923
6924 return 1;
6925}
6926
6927
6928static int cmd_sta_send_frame_vht(struct sigma_dut *dut,
6929 struct sigma_conn *conn,
6930 struct sigma_cmd *cmd)
6931{
6932 switch (get_driver_type()) {
6933 case DRIVER_ATHEROS:
6934 return ath_sta_send_frame_vht(dut, conn, cmd);
6935 default:
6936 send_resp(dut, conn, SIGMA_ERROR,
6937 "errorCode,Unsupported sta_set_frame(VHT) with the current driver");
6938 return 0;
6939 }
6940}
6941
6942
Lior David0fe101e2017-03-09 16:09:50 +02006943#ifdef __linux__
6944int wil6210_send_frame_60g(struct sigma_dut *dut, struct sigma_conn *conn,
6945 struct sigma_cmd *cmd)
6946{
6947 const char *frame_name = get_param(cmd, "framename");
6948 const char *mac = get_param(cmd, "dest_mac");
6949
6950 if (!frame_name || !mac) {
6951 sigma_dut_print(dut, DUT_MSG_ERROR,
6952 "framename and dest_mac must be provided");
6953 return -1;
6954 }
6955
6956 if (strcasecmp(frame_name, "brp") == 0) {
6957 const char *l_rx = get_param(cmd, "L-RX");
6958 int l_rx_i;
6959
6960 if (!l_rx) {
6961 sigma_dut_print(dut, DUT_MSG_ERROR,
6962 "L-RX must be provided");
6963 return -1;
6964 }
6965 l_rx_i = atoi(l_rx);
6966
6967 sigma_dut_print(dut, DUT_MSG_INFO,
6968 "dev_send_frame: BRP-RX, dest_mac %s, L-RX %s",
6969 mac, l_rx);
6970 if (l_rx_i != 16) {
6971 sigma_dut_print(dut, DUT_MSG_ERROR,
6972 "unsupported L-RX: %s", l_rx);
6973 return -1;
6974 }
6975
6976 if (wil6210_send_brp_rx(dut, mac, l_rx_i))
6977 return -1;
6978 } else if (strcasecmp(frame_name, "ssw") == 0) {
6979 sigma_dut_print(dut, DUT_MSG_INFO,
6980 "dev_send_frame: SLS, dest_mac %s", mac);
6981 if (wil6210_send_sls(dut, mac))
6982 return -1;
6983 } else {
6984 sigma_dut_print(dut, DUT_MSG_ERROR,
6985 "unsupported frame type: %s", frame_name);
6986 return -1;
6987 }
6988
6989 return 1;
6990}
6991#endif /* __linux__ */
6992
6993
6994static int cmd_sta_send_frame_60g(struct sigma_dut *dut,
6995 struct sigma_conn *conn,
6996 struct sigma_cmd *cmd)
6997{
6998 switch (get_driver_type()) {
6999#ifdef __linux__
7000 case DRIVER_WIL6210:
7001 return wil6210_send_frame_60g(dut, conn, cmd);
7002#endif /* __linux__ */
7003 default:
7004 send_resp(dut, conn, SIGMA_ERROR,
7005 "errorCode,Unsupported sta_set_frame(60G) with the current driver");
7006 return 0;
7007 }
7008}
7009
7010
Ashwini Patildb59b3c2017-04-13 15:19:23 +05307011static int mbo_send_anqp_query(struct sigma_dut *dut, struct sigma_conn *conn,
7012 const char *intf, struct sigma_cmd *cmd)
7013{
7014 const char *val, *addr;
7015 char buf[100];
7016
7017 addr = get_param(cmd, "DestMac");
7018 if (!addr) {
7019 send_resp(dut, conn, SIGMA_INVALID,
7020 "ErrorCode,AP MAC address is missing");
7021 return 0;
7022 }
7023
7024 val = get_param(cmd, "ANQPQuery_ID");
7025 if (!val) {
7026 send_resp(dut, conn, SIGMA_INVALID,
7027 "ErrorCode,Missing ANQPQuery_ID");
7028 return 0;
7029 }
7030
7031 if (strcasecmp(val, "NeighborReportReq") == 0) {
7032 snprintf(buf, sizeof(buf), "ANQP_GET %s 272", addr);
7033 } else if (strcasecmp(val, "QueryListWithCellPref") == 0) {
7034 snprintf(buf, sizeof(buf), "ANQP_GET %s 272,mbo:2", addr);
7035 } else {
7036 sigma_dut_print(dut, DUT_MSG_ERROR, "Invalid ANQPQuery_ID: %s",
7037 val);
7038 send_resp(dut, conn, SIGMA_INVALID,
7039 "ErrorCode,Invalid ANQPQuery_ID");
7040 return 0;
7041 }
7042
Ashwini Patild174f2c2017-04-13 16:49:46 +05307043 /* Set gas_address3 field to IEEE 802.11-2012 standard compliant form
7044 * (Address3 = Wildcard BSSID when sent to not-associated AP;
7045 * if associated, AP BSSID).
7046 */
7047 if (wpa_command(intf, "SET gas_address3 1") < 0) {
7048 send_resp(dut, conn, SIGMA_ERROR,
7049 "ErrorCode,Failed to set gas_address3");
7050 return 0;
7051 }
7052
Ashwini Patildb59b3c2017-04-13 15:19:23 +05307053 if (wpa_command(intf, buf) < 0) {
7054 send_resp(dut, conn, SIGMA_ERROR,
7055 "ErrorCode,Failed to send ANQP query");
7056 return 0;
7057 }
7058
7059 return 1;
7060}
7061
7062
7063static int mbo_cmd_sta_send_frame(struct sigma_dut *dut,
7064 struct sigma_conn *conn,
7065 const char *intf,
7066 struct sigma_cmd *cmd)
7067{
7068 const char *val = get_param(cmd, "FrameName");
7069
7070 if (val && strcasecmp(val, "ANQPQuery") == 0)
7071 return mbo_send_anqp_query(dut, conn, intf, cmd);
7072
7073 return 2;
7074}
7075
7076
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007077int cmd_sta_send_frame(struct sigma_dut *dut, struct sigma_conn *conn,
7078 struct sigma_cmd *cmd)
7079{
7080 const char *intf = get_param(cmd, "Interface");
7081 const char *val;
7082 enum send_frame_type frame;
7083 enum send_frame_protection protected;
7084 char buf[100];
7085 unsigned char addr[ETH_ALEN];
7086 int res;
7087
7088 val = get_param(cmd, "program");
7089 if (val == NULL)
7090 val = get_param(cmd, "frame");
7091 if (val && strcasecmp(val, "TDLS") == 0)
7092 return cmd_sta_send_frame_tdls(dut, conn, cmd);
7093 if (val && (strcasecmp(val, "HS2") == 0 ||
7094 strcasecmp(val, "HS2-R2") == 0))
7095 return cmd_sta_send_frame_hs2(dut, conn, cmd);
7096 if (val && strcasecmp(val, "VHT") == 0)
7097 return cmd_sta_send_frame_vht(dut, conn, cmd);
priyadharshini gowthamand66913a2016-07-29 15:11:17 -07007098 if (val && strcasecmp(val, "LOC") == 0)
7099 return loc_cmd_sta_send_frame(dut, conn, cmd);
Lior David0fe101e2017-03-09 16:09:50 +02007100 if (val && strcasecmp(val, "60GHz") == 0)
7101 return cmd_sta_send_frame_60g(dut, conn, cmd);
Ashwini Patildb59b3c2017-04-13 15:19:23 +05307102 if (val && strcasecmp(val, "MBO") == 0) {
7103 res = mbo_cmd_sta_send_frame(dut, conn, intf, cmd);
7104 if (res != 2)
7105 return res;
7106 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007107
7108 val = get_param(cmd, "TD_DISC");
7109 if (val) {
7110 if (hwaddr_aton(val, addr) < 0)
7111 return -1;
7112 snprintf(buf, sizeof(buf), "TDLS_DISCOVER %s", val);
7113 if (wpa_command(intf, buf) < 0) {
7114 send_resp(dut, conn, SIGMA_ERROR,
7115 "ErrorCode,Failed to send TDLS discovery");
7116 return 0;
7117 }
7118 return 1;
7119 }
7120
7121 val = get_param(cmd, "TD_Setup");
7122 if (val) {
7123 if (hwaddr_aton(val, addr) < 0)
7124 return -1;
7125 snprintf(buf, sizeof(buf), "TDLS_SETUP %s", val);
7126 if (wpa_command(intf, buf) < 0) {
7127 send_resp(dut, conn, SIGMA_ERROR,
7128 "ErrorCode,Failed to start TDLS setup");
7129 return 0;
7130 }
7131 return 1;
7132 }
7133
7134 val = get_param(cmd, "TD_TearDown");
7135 if (val) {
7136 if (hwaddr_aton(val, addr) < 0)
7137 return -1;
7138 snprintf(buf, sizeof(buf), "TDLS_TEARDOWN %s", val);
7139 if (wpa_command(intf, buf) < 0) {
7140 send_resp(dut, conn, SIGMA_ERROR,
7141 "ErrorCode,Failed to tear down TDLS link");
7142 return 0;
7143 }
7144 return 1;
7145 }
7146
7147 val = get_param(cmd, "TD_ChannelSwitch");
7148 if (val) {
7149 /* TODO */
7150 send_resp(dut, conn, SIGMA_ERROR,
7151 "ErrorCode,TD_ChannelSwitch not yet supported");
7152 return 0;
7153 }
7154
7155 val = get_param(cmd, "TD_NF");
7156 if (val) {
7157 /* TODO */
7158 send_resp(dut, conn, SIGMA_ERROR,
7159 "ErrorCode,TD_NF not yet supported");
7160 return 0;
7161 }
7162
7163 val = get_param(cmd, "PMFFrameType");
7164 if (val == NULL)
7165 val = get_param(cmd, "FrameName");
7166 if (val == NULL)
7167 val = get_param(cmd, "Type");
7168 if (val == NULL)
7169 return -1;
7170 if (strcasecmp(val, "disassoc") == 0)
7171 frame = DISASSOC;
7172 else if (strcasecmp(val, "deauth") == 0)
7173 frame = DEAUTH;
7174 else if (strcasecmp(val, "saquery") == 0)
7175 frame = SAQUERY;
7176 else if (strcasecmp(val, "auth") == 0)
7177 frame = AUTH;
7178 else if (strcasecmp(val, "assocreq") == 0)
7179 frame = ASSOCREQ;
7180 else if (strcasecmp(val, "reassocreq") == 0)
7181 frame = REASSOCREQ;
7182 else if (strcasecmp(val, "neigreq") == 0) {
7183 sigma_dut_print(dut, DUT_MSG_INFO, "Got neighbor request");
7184
7185 val = get_param(cmd, "ssid");
7186 if (val == NULL)
7187 return -1;
7188
7189 res = send_neighbor_request(dut, intf, val);
7190 if (res) {
7191 send_resp(dut, conn, SIGMA_ERROR, "errorCode,"
7192 "Failed to send neighbor report request");
7193 return 0;
7194 }
7195
7196 return 1;
Ashwini Patil5acd7382017-04-13 15:55:04 +05307197 } else if (strcasecmp(val, "transmgmtquery") == 0 ||
7198 strcasecmp(val, "BTMQuery") == 0) {
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007199 sigma_dut_print(dut, DUT_MSG_DEBUG,
7200 "Got Transition Management Query");
7201
Ashwini Patil5acd7382017-04-13 15:55:04 +05307202 res = send_trans_mgmt_query(dut, intf, cmd);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007203 if (res) {
7204 send_resp(dut, conn, SIGMA_ERROR, "errorCode,"
7205 "Failed to send Transition Management Query");
7206 return 0;
7207 }
7208
7209 return 1;
7210 } else {
7211 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Unsupported "
7212 "PMFFrameType");
7213 return 0;
7214 }
7215
7216 val = get_param(cmd, "PMFProtected");
7217 if (val == NULL)
7218 val = get_param(cmd, "Protected");
7219 if (val == NULL)
7220 return -1;
7221 if (strcasecmp(val, "Correct-key") == 0 ||
7222 strcasecmp(val, "CorrectKey") == 0)
7223 protected = CORRECT_KEY;
7224 else if (strcasecmp(val, "IncorrectKey") == 0)
7225 protected = INCORRECT_KEY;
7226 else if (strcasecmp(val, "Unprotected") == 0)
7227 protected = UNPROTECTED;
7228 else {
7229 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Unsupported "
7230 "PMFProtected");
7231 return 0;
7232 }
7233
7234 if (protected != UNPROTECTED &&
7235 (frame == AUTH || frame == ASSOCREQ || frame == REASSOCREQ)) {
7236 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Impossible "
7237 "PMFProtected for auth/assocreq/reassocreq");
7238 return 0;
7239 }
7240
7241 if (if_nametoindex("sigmadut") == 0) {
7242 snprintf(buf, sizeof(buf),
7243 "iw dev %s interface add sigmadut type monitor",
7244 get_station_ifname());
7245 if (system(buf) != 0 ||
7246 if_nametoindex("sigmadut") == 0) {
7247 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to add "
7248 "monitor interface with '%s'", buf);
7249 return -2;
7250 }
7251 }
7252
7253 if (system("ifconfig sigmadut up") != 0) {
7254 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to set "
7255 "monitor interface up");
7256 return -2;
7257 }
7258
7259 return sta_inject_frame(dut, conn, frame, protected, NULL);
7260}
7261
7262
7263static int cmd_sta_set_parameter_hs2(struct sigma_dut *dut,
7264 struct sigma_conn *conn,
7265 struct sigma_cmd *cmd,
7266 const char *ifname)
7267{
7268 char buf[200];
7269 const char *val;
7270
7271 val = get_param(cmd, "ClearARP");
7272 if (val && atoi(val) == 1) {
7273 snprintf(buf, sizeof(buf), "ip neigh flush dev %s", ifname);
7274 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
7275 if (system(buf) != 0) {
7276 send_resp(dut, conn, SIGMA_ERROR,
7277 "errorCode,Failed to clear ARP cache");
7278 return 0;
7279 }
7280 }
7281
7282 return 1;
7283}
7284
7285
7286int cmd_sta_set_parameter(struct sigma_dut *dut, struct sigma_conn *conn,
7287 struct sigma_cmd *cmd)
7288{
7289 const char *intf = get_param(cmd, "Interface");
7290 const char *val;
7291
7292 if (intf == NULL)
7293 return -1;
7294
7295 val = get_param(cmd, "program");
7296 if (val && (strcasecmp(val, "HS2") == 0 ||
7297 strcasecmp(val, "HS2-R2") == 0))
7298 return cmd_sta_set_parameter_hs2(dut, conn, cmd, intf);
7299
7300 return -1;
7301}
7302
7303
7304static int cmd_sta_set_macaddr(struct sigma_dut *dut, struct sigma_conn *conn,
7305 struct sigma_cmd *cmd)
7306{
7307 const char *intf = get_param(cmd, "Interface");
7308 const char *mac = get_param(cmd, "MAC");
7309
7310 if (intf == NULL || mac == NULL)
7311 return -1;
7312
7313 sigma_dut_print(dut, DUT_MSG_INFO, "Change local MAC address for "
7314 "interface %s to %s", intf, mac);
7315
7316 if (dut->set_macaddr) {
7317 char buf[128];
7318 int res;
7319 if (strcasecmp(mac, "default") == 0) {
7320 res = snprintf(buf, sizeof(buf), "%s",
7321 dut->set_macaddr);
7322 dut->tmp_mac_addr = 0;
7323 } else {
7324 res = snprintf(buf, sizeof(buf), "%s %s",
7325 dut->set_macaddr, mac);
7326 dut->tmp_mac_addr = 1;
7327 }
7328 if (res < 0 || res >= (int) sizeof(buf))
7329 return -1;
7330 if (system(buf) != 0) {
7331 send_resp(dut, conn, SIGMA_ERROR,
7332 "errorCode,Failed to set MAC "
7333 "address");
7334 return 0;
7335 }
7336 return 1;
7337 }
7338
7339 if (strcasecmp(mac, "default") == 0)
7340 return 1;
7341
7342 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Unsupported "
7343 "command");
7344 return 0;
7345}
7346
7347
7348static int iwpriv_tdlsoffchnmode(struct sigma_dut *dut,
7349 struct sigma_conn *conn, const char *intf,
7350 int val)
7351{
7352 char buf[200];
7353 int res;
7354
7355 res = snprintf(buf, sizeof(buf), "iwpriv %s tdlsoffchnmode %d",
7356 intf, val);
7357 if (res < 0 || res >= (int) sizeof(buf))
7358 return -1;
7359 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
7360 if (system(buf) != 0) {
7361 send_resp(dut, conn, SIGMA_ERROR,
7362 "errorCode,Failed to configure offchannel mode");
7363 return 0;
7364 }
7365
7366 return 1;
7367}
7368
7369
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007370static int off_chan_val(enum sec_ch_offset off)
7371{
7372 switch (off) {
7373 case SEC_CH_NO:
7374 return 0;
7375 case SEC_CH_40ABOVE:
7376 return 40;
7377 case SEC_CH_40BELOW:
7378 return -40;
7379 }
7380
7381 return 0;
7382}
7383
7384
7385static int iwpriv_set_offchan(struct sigma_dut *dut, struct sigma_conn *conn,
7386 const char *intf, int off_ch_num,
7387 enum sec_ch_offset sec)
7388{
7389 char buf[200];
7390 int res;
7391
7392 res = snprintf(buf, sizeof(buf), "iwpriv %s tdlsoffchan %d",
7393 intf, off_ch_num);
7394 if (res < 0 || res >= (int) sizeof(buf))
7395 return -1;
7396 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
7397 if (system(buf) != 0) {
7398 send_resp(dut, conn, SIGMA_ERROR,
7399 "errorCode,Failed to set offchan");
7400 return 0;
7401 }
7402
7403 res = snprintf(buf, sizeof(buf), "iwpriv %s tdlsecchnoffst %d",
7404 intf, off_chan_val(sec));
7405 if (res < 0 || res >= (int) sizeof(buf))
7406 return -1;
7407 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
7408 if (system(buf) != 0) {
7409 send_resp(dut, conn, SIGMA_ERROR,
7410 "errorCode,Failed to set sec chan offset");
7411 return 0;
7412 }
7413
7414 return 1;
7415}
7416
7417
7418static int tdls_set_offchannel_offset(struct sigma_dut *dut,
7419 struct sigma_conn *conn,
7420 const char *intf, int off_ch_num,
7421 enum sec_ch_offset sec)
7422{
7423 char buf[200];
7424 int res;
7425
7426 res = snprintf(buf, sizeof(buf), "DRIVER TDLSOFFCHANNEL %d",
7427 off_ch_num);
7428 if (res < 0 || res >= (int) sizeof(buf))
7429 return -1;
7430 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
7431
7432 if (wpa_command(intf, buf) < 0) {
7433 send_resp(dut, conn, SIGMA_ERROR,
7434 "ErrorCode,Failed to set offchan");
7435 return 0;
7436 }
7437 res = snprintf(buf, sizeof(buf), "DRIVER TDLSSECONDARYCHANNELOFFSET %d",
7438 off_chan_val(sec));
7439 if (res < 0 || res >= (int) sizeof(buf))
7440 return -1;
7441
7442 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
7443
7444 if (wpa_command(intf, buf) < 0) {
7445 send_resp(dut, conn, SIGMA_ERROR,
7446 "ErrorCode,Failed to set sec chan offset");
7447 return 0;
7448 }
7449
7450 return 1;
7451}
7452
7453
7454static int tdls_set_offchannel_mode(struct sigma_dut *dut,
7455 struct sigma_conn *conn,
7456 const char *intf, int val)
7457{
7458 char buf[200];
7459 int res;
7460
7461 res = snprintf(buf, sizeof(buf), "DRIVER TDLSOFFCHANNELMODE %d",
7462 val);
7463 if (res < 0 || res >= (int) sizeof(buf))
7464 return -1;
7465 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
7466
7467 if (wpa_command(intf, buf) < 0) {
7468 send_resp(dut, conn, SIGMA_ERROR,
7469 "ErrorCode,Failed to configure offchannel mode");
7470 return 0;
7471 }
7472
7473 return 1;
7474}
7475
7476
7477static int cmd_sta_set_rfeature_tdls(const char *intf, struct sigma_dut *dut,
7478 struct sigma_conn *conn,
7479 struct sigma_cmd *cmd)
7480{
7481 const char *val;
7482 enum {
7483 CHSM_NOT_SET,
7484 CHSM_ENABLE,
7485 CHSM_DISABLE,
7486 CHSM_REJREQ,
7487 CHSM_UNSOLRESP
7488 } chsm = CHSM_NOT_SET;
7489 int off_ch_num = -1;
7490 enum sec_ch_offset sec_ch = SEC_CH_NO;
7491 int res;
7492
7493 val = get_param(cmd, "Uapsd");
7494 if (val) {
7495 char buf[100];
7496 if (strcasecmp(val, "Enable") == 0)
7497 snprintf(buf, sizeof(buf), "SET ps 99");
7498 else if (strcasecmp(val, "Disable") == 0)
7499 snprintf(buf, sizeof(buf), "SET ps 98");
7500 else {
7501 send_resp(dut, conn, SIGMA_ERROR, "errorCode,"
7502 "Unsupported uapsd parameter value");
7503 return 0;
7504 }
7505 if (wpa_command(intf, buf)) {
7506 send_resp(dut, conn, SIGMA_ERROR,
7507 "ErrorCode,Failed to change U-APSD "
7508 "powersave mode");
7509 return 0;
7510 }
7511 }
7512
7513 val = get_param(cmd, "TPKTIMER");
7514 if (val && strcasecmp(val, "DISABLE") == 0) {
7515 if (wpa_command(intf, "SET tdls_testing 0x100")) {
7516 send_resp(dut, conn, SIGMA_ERROR,
7517 "ErrorCode,Failed to enable no TPK "
7518 "expiration test mode");
7519 return 0;
7520 }
7521 dut->no_tpk_expiration = 1;
7522 }
7523
7524 val = get_param(cmd, "ChSwitchMode");
7525 if (val) {
7526 if (strcasecmp(val, "Enable") == 0 ||
7527 strcasecmp(val, "Initiate") == 0)
7528 chsm = CHSM_ENABLE;
7529 else if (strcasecmp(val, "Disable") == 0 ||
7530 strcasecmp(val, "passive") == 0)
7531 chsm = CHSM_DISABLE;
7532 else if (strcasecmp(val, "RejReq") == 0)
7533 chsm = CHSM_REJREQ;
7534 else if (strcasecmp(val, "UnSolResp") == 0)
7535 chsm = CHSM_UNSOLRESP;
7536 else {
7537 send_resp(dut, conn, SIGMA_ERROR,
7538 "ErrorCode,Unknown ChSwitchMode value");
7539 return 0;
7540 }
7541 }
7542
7543 val = get_param(cmd, "OffChNum");
7544 if (val) {
7545 off_ch_num = atoi(val);
7546 if (off_ch_num == 0) {
7547 send_resp(dut, conn, SIGMA_ERROR,
7548 "ErrorCode,Invalid OffChNum");
7549 return 0;
7550 }
7551 }
7552
7553 val = get_param(cmd, "SecChOffset");
7554 if (val) {
7555 if (strcmp(val, "20") == 0)
7556 sec_ch = SEC_CH_NO;
7557 else if (strcasecmp(val, "40above") == 0)
7558 sec_ch = SEC_CH_40ABOVE;
7559 else if (strcasecmp(val, "40below") == 0)
7560 sec_ch = SEC_CH_40BELOW;
7561 else {
7562 send_resp(dut, conn, SIGMA_ERROR,
7563 "ErrorCode,Unknown SecChOffset value");
7564 return 0;
7565 }
7566 }
7567
7568 if (chsm == CHSM_NOT_SET) {
7569 /* no offchannel changes requested */
7570 return 1;
7571 }
7572
7573 if (strcmp(intf, get_main_ifname()) != 0 &&
7574 strcmp(intf, get_station_ifname()) != 0) {
7575 send_resp(dut, conn, SIGMA_ERROR,
7576 "ErrorCode,Unknown interface");
7577 return 0;
7578 }
7579
7580 switch (chsm) {
7581 case CHSM_NOT_SET:
Jouni Malinen280f5ba2016-08-29 21:33:10 +03007582 res = 1;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007583 break;
7584 case CHSM_ENABLE:
7585 if (off_ch_num < 0) {
7586 send_resp(dut, conn, SIGMA_ERROR,
7587 "ErrorCode,Missing OffChNum argument");
7588 return 0;
7589 }
7590 if (wifi_chip_type == DRIVER_WCN) {
7591 res = tdls_set_offchannel_offset(dut, conn, intf,
7592 off_ch_num, sec_ch);
7593 } else {
7594 res = iwpriv_set_offchan(dut, conn, intf, off_ch_num,
7595 sec_ch);
7596 }
7597 if (res != 1)
7598 return res;
7599 if (wifi_chip_type == DRIVER_WCN)
7600 res = tdls_set_offchannel_mode(dut, conn, intf, 1);
7601 else
7602 res = iwpriv_tdlsoffchnmode(dut, conn, intf, 1);
7603 break;
7604 case CHSM_DISABLE:
7605 if (wifi_chip_type == DRIVER_WCN)
7606 res = tdls_set_offchannel_mode(dut, conn, intf, 2);
7607 else
7608 res = iwpriv_tdlsoffchnmode(dut, conn, intf, 2);
7609 break;
7610 case CHSM_REJREQ:
7611 if (wifi_chip_type == DRIVER_WCN)
7612 res = tdls_set_offchannel_mode(dut, conn, intf, 3);
7613 else
7614 res = iwpriv_tdlsoffchnmode(dut, conn, intf, 3);
7615 break;
7616 case CHSM_UNSOLRESP:
7617 if (off_ch_num < 0) {
7618 send_resp(dut, conn, SIGMA_ERROR,
7619 "ErrorCode,Missing OffChNum argument");
7620 return 0;
7621 }
7622 if (wifi_chip_type == DRIVER_WCN) {
7623 res = tdls_set_offchannel_offset(dut, conn, intf,
7624 off_ch_num, sec_ch);
7625 } else {
7626 res = iwpriv_set_offchan(dut, conn, intf, off_ch_num,
7627 sec_ch);
7628 }
7629 if (res != 1)
7630 return res;
7631 if (wifi_chip_type == DRIVER_WCN)
7632 res = tdls_set_offchannel_mode(dut, conn, intf, 4);
7633 else
7634 res = iwpriv_tdlsoffchnmode(dut, conn, intf, 4);
7635 break;
7636 }
7637
7638 return res;
7639}
7640
7641
7642static int ath_sta_set_rfeature_vht(const char *intf, struct sigma_dut *dut,
7643 struct sigma_conn *conn,
7644 struct sigma_cmd *cmd)
7645{
7646 const char *val;
7647 char *token, *result;
7648
priyadharshini gowthamane5e25172015-12-08 14:53:48 -08007649 novap_reset(dut, intf);
7650
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007651 val = get_param(cmd, "nss_mcs_opt");
7652 if (val) {
7653 /* String (nss_operating_mode; mcs_operating_mode) */
7654 int nss, mcs;
7655 char buf[50];
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05307656 char *saveptr;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007657
7658 token = strdup(val);
7659 if (!token)
7660 return 0;
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05307661 result = strtok_r(token, ";", &saveptr);
Pradeep Reddy POTTETI41b8c542016-06-15 16:09:46 +05307662 if (!result) {
7663 sigma_dut_print(dut, DUT_MSG_ERROR,
7664 "VHT NSS not specified");
7665 goto failed;
7666 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007667 if (strcasecmp(result, "def") != 0) {
7668 nss = atoi(result);
7669 if (nss == 4)
7670 ath_disable_txbf(dut, intf);
7671 snprintf(buf, sizeof(buf), "iwpriv %s nss %d",
7672 intf, nss);
7673 if (system(buf) != 0) {
7674 sigma_dut_print(dut, DUT_MSG_ERROR,
7675 "iwpriv nss failed");
7676 goto failed;
7677 }
7678 }
7679
Pradeep Reddy POTTETIdbf7d712016-04-28 18:42:07 +05307680 result = strtok_r(NULL, ";", &saveptr);
Pradeep Reddy POTTETI41b8c542016-06-15 16:09:46 +05307681 if (!result) {
7682 sigma_dut_print(dut, DUT_MSG_ERROR,
7683 "VHT MCS not specified");
7684 goto failed;
7685 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007686 if (strcasecmp(result, "def") == 0) {
7687 snprintf(buf, sizeof(buf), "iwpriv %s set11NRates 0",
7688 intf);
7689 if (system(buf) != 0) {
7690 sigma_dut_print(dut, DUT_MSG_ERROR,
7691 "iwpriv set11NRates failed");
7692 goto failed;
7693 }
7694
7695 } else {
7696 mcs = atoi(result);
7697 snprintf(buf, sizeof(buf), "iwpriv %s vhtmcs %d",
7698 intf, mcs);
7699 if (system(buf) != 0) {
7700 sigma_dut_print(dut, DUT_MSG_ERROR,
7701 "iwpriv vhtmcs failed");
7702 goto failed;
7703 }
7704 }
7705 /* Channel width gets messed up, fix this */
7706 snprintf(buf, sizeof(buf), "iwpriv %s chwidth %d",
7707 intf, dut->chwidth);
7708 if (system(buf) != 0) {
7709 sigma_dut_print(dut, DUT_MSG_ERROR,
7710 "iwpriv chwidth failed");
7711 }
7712 }
7713
7714 return 1;
7715failed:
7716 free(token);
7717 return 0;
7718}
7719
7720
7721static int cmd_sta_set_rfeature_vht(const char *intf, struct sigma_dut *dut,
7722 struct sigma_conn *conn,
7723 struct sigma_cmd *cmd)
7724{
7725 switch (get_driver_type()) {
7726 case DRIVER_ATHEROS:
7727 return ath_sta_set_rfeature_vht(intf, dut, conn, cmd);
7728 default:
7729 send_resp(dut, conn, SIGMA_ERROR,
7730 "errorCode,Unsupported sta_set_rfeature(VHT) with the current driver");
7731 return 0;
7732 }
7733}
7734
7735
Ashwini Patil5acd7382017-04-13 15:55:04 +05307736static int btm_query_candidate_list(struct sigma_dut *dut,
7737 struct sigma_conn *conn,
7738 struct sigma_cmd *cmd)
7739{
7740 const char *bssid, *info, *op_class, *ch, *phy_type, *pref;
7741 int len, ret;
7742 char buf[10];
7743
7744 /*
7745 * Neighbor Report elements format:
7746 * neighbor=<BSSID>,<BSSID Information>,<Operating Class>,
7747 * <Channel Number>,<PHY Type>[,<hexdump of Optional Subelements>]
7748 * eg: neighbor=aa:bb:cc:dd:ee:ff,17,81,6,1,030101
7749 */
7750
7751 bssid = get_param(cmd, "Nebor_BSSID");
7752 if (!bssid) {
7753 send_resp(dut, conn, SIGMA_INVALID,
7754 "errorCode,Nebor_BSSID is missing");
7755 return 0;
7756 }
7757
7758 info = get_param(cmd, "Nebor_Bssid_Info");
7759 if (!info) {
7760 sigma_dut_print(dut, DUT_MSG_INFO,
7761 "Using default value for Nebor_Bssid_Info: %s",
7762 DEFAULT_NEIGHBOR_BSSID_INFO);
7763 info = DEFAULT_NEIGHBOR_BSSID_INFO;
7764 }
7765
7766 op_class = get_param(cmd, "Nebor_Op_Class");
7767 if (!op_class) {
7768 send_resp(dut, conn, SIGMA_INVALID,
7769 "errorCode,Nebor_Op_Class is missing");
7770 return 0;
7771 }
7772
7773 ch = get_param(cmd, "Nebor_Op_Ch");
7774 if (!ch) {
7775 send_resp(dut, conn, SIGMA_INVALID,
7776 "errorCode,Nebor_Op_Ch is missing");
7777 return 0;
7778 }
7779
7780 phy_type = get_param(cmd, "Nebor_Phy_Type");
7781 if (!phy_type) {
7782 sigma_dut_print(dut, DUT_MSG_INFO,
7783 "Using default value for Nebor_Phy_Type: %s",
7784 DEFAULT_NEIGHBOR_PHY_TYPE);
7785 phy_type = DEFAULT_NEIGHBOR_PHY_TYPE;
7786 }
7787
7788 /* Parse optional subelements */
7789 buf[0] = '\0';
7790 pref = get_param(cmd, "Nebor_Pref");
7791 if (pref) {
7792 /* hexdump for preferrence subelement */
7793 ret = snprintf(buf, sizeof(buf), ",0301%02x", atoi(pref));
7794 if (ret < 0 || ret >= (int) sizeof(buf)) {
7795 sigma_dut_print(dut, DUT_MSG_ERROR,
7796 "snprintf failed for optional subelement ret: %d",
7797 ret);
7798 send_resp(dut, conn, SIGMA_ERROR,
7799 "errorCode,snprintf failed for subelement");
7800 return 0;
7801 }
7802 }
7803
7804 if (!dut->btm_query_cand_list) {
7805 dut->btm_query_cand_list = calloc(1, NEIGHBOR_REPORT_SIZE);
7806 if (!dut->btm_query_cand_list) {
7807 send_resp(dut, conn, SIGMA_ERROR,
7808 "errorCode,Failed to allocate memory for btm_query_cand_list");
7809 return 0;
7810 }
7811 }
7812
7813 len = strlen(dut->btm_query_cand_list);
7814 ret = snprintf(dut->btm_query_cand_list + len,
7815 NEIGHBOR_REPORT_SIZE - len, " neighbor=%s,%s,%s,%s,%s%s",
7816 bssid, info, op_class, ch, phy_type, buf);
7817 if (ret < 0 || ret >= NEIGHBOR_REPORT_SIZE - len) {
7818 sigma_dut_print(dut, DUT_MSG_ERROR,
7819 "snprintf failed for neighbor report list ret: %d",
7820 ret);
7821 send_resp(dut, conn, SIGMA_ERROR,
7822 "errorCode,snprintf failed for neighbor report");
7823 free(dut->btm_query_cand_list);
7824 dut->btm_query_cand_list = NULL;
7825 return 0;
7826 }
7827
7828 return 1;
7829}
7830
7831
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007832static int cmd_sta_set_rfeature(struct sigma_dut *dut, struct sigma_conn *conn,
7833 struct sigma_cmd *cmd)
7834{
7835 const char *intf = get_param(cmd, "Interface");
7836 const char *prog = get_param(cmd, "Prog");
Ashwini Patil68d02cd2017-01-10 15:39:16 +05307837 const char *val;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007838
7839 if (intf == NULL || prog == NULL)
7840 return -1;
7841
Ashwini Patil5acd7382017-04-13 15:55:04 +05307842 /* BSS Transition candidate list for BTM query */
7843 val = get_param(cmd, "Nebor_BSSID");
7844 if (val && btm_query_candidate_list(dut, conn, cmd) == 0)
7845 return 0;
7846
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007847 if (strcasecmp(prog, "TDLS") == 0)
7848 return cmd_sta_set_rfeature_tdls(intf, dut, conn, cmd);
7849
7850 if (strcasecmp(prog, "VHT") == 0)
7851 return cmd_sta_set_rfeature_vht(intf, dut, conn, cmd);
7852
Ashwini Patil68d02cd2017-01-10 15:39:16 +05307853 if (strcasecmp(prog, "MBO") == 0) {
7854 val = get_param(cmd, "Cellular_Data_Cap");
7855 if (val &&
7856 mbo_set_cellular_data_capa(dut, conn, intf, atoi(val)) == 0)
7857 return 0;
Ashwini Patil00402582017-04-13 12:29:39 +05307858
7859 val = get_param(cmd, "Ch_Pref");
7860 if (val && mbo_set_non_pref_ch_list(dut, conn, intf, cmd) == 0)
7861 return 0;
7862
Ashwini Patil68d02cd2017-01-10 15:39:16 +05307863 return 1;
7864 }
7865
Jouni Malinencd4e3c32015-10-29 12:39:56 +02007866 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Unsupported Prog");
7867 return 0;
7868}
7869
7870
7871static int cmd_sta_set_radio(struct sigma_dut *dut, struct sigma_conn *conn,
7872 struct sigma_cmd *cmd)
7873{
7874 const char *intf = get_param(cmd, "Interface");
7875 const char *mode = get_param(cmd, "Mode");
7876 int res;
7877
7878 if (intf == NULL || mode == NULL)
7879 return -1;
7880
7881 if (strcasecmp(mode, "On") == 0)
7882 res = wpa_command(intf, "SET radio_disabled 0");
7883 else if (strcasecmp(mode, "Off") == 0)
7884 res = wpa_command(intf, "SET radio_disabled 1");
7885 else
7886 return -1;
7887
7888 if (res) {
7889 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to change "
7890 "radio mode");
7891 return 0;
7892 }
7893
7894 return 1;
7895}
7896
7897
7898static int cmd_sta_set_pwrsave(struct sigma_dut *dut, struct sigma_conn *conn,
7899 struct sigma_cmd *cmd)
7900{
7901 const char *intf = get_param(cmd, "Interface");
7902 const char *mode = get_param(cmd, "Mode");
7903 int res;
7904
7905 if (intf == NULL || mode == NULL)
7906 return -1;
7907
7908 if (strcasecmp(mode, "On") == 0)
7909 res = set_ps(intf, dut, 1);
7910 else if (strcasecmp(mode, "Off") == 0)
7911 res = set_ps(intf, dut, 0);
7912 else
7913 return -1;
7914
7915 if (res) {
7916 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to change "
7917 "power save mode");
7918 return 0;
7919 }
7920
7921 return 1;
7922}
7923
7924
7925static int cmd_sta_bssid_pool(struct sigma_dut *dut, struct sigma_conn *conn,
7926 struct sigma_cmd *cmd)
7927{
7928 const char *intf = get_param(cmd, "Interface");
7929 const char *val, *bssid;
7930 int res;
7931 char *buf;
7932 size_t buf_len;
7933
7934 val = get_param(cmd, "BSSID_FILTER");
7935 if (val == NULL)
7936 return -1;
7937
7938 bssid = get_param(cmd, "BSSID_List");
7939 if (atoi(val) == 0 || bssid == NULL) {
7940 /* Disable BSSID filter */
7941 if (wpa_command(intf, "SET bssid_filter ")) {
7942 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed "
7943 "to disable BSSID filter");
7944 return 0;
7945 }
7946
7947 return 1;
7948 }
7949
7950 buf_len = 100 + strlen(bssid);
7951 buf = malloc(buf_len);
7952 if (buf == NULL)
7953 return -1;
7954
7955 snprintf(buf, buf_len, "SET bssid_filter %s", bssid);
7956 res = wpa_command(intf, buf);
7957 free(buf);
7958 if (res) {
7959 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to enable "
7960 "BSSID filter");
7961 return 0;
7962 }
7963
7964 return 1;
7965}
7966
7967
7968static int cmd_sta_reset_parm(struct sigma_dut *dut, struct sigma_conn *conn,
7969 struct sigma_cmd *cmd)
7970{
7971 const char *intf = get_param(cmd, "Interface");
7972 const char *val;
7973
7974 /* TODO: ARP */
7975
7976 val = get_param(cmd, "HS2_CACHE_PROFILE");
7977 if (val && strcasecmp(val, "All") == 0)
7978 hs2_clear_credentials(intf);
7979
7980 return 1;
7981}
7982
7983
7984static int cmd_sta_get_key(struct sigma_dut *dut, struct sigma_conn *conn,
7985 struct sigma_cmd *cmd)
7986{
7987 const char *intf = get_param(cmd, "Interface");
7988 const char *key_type = get_param(cmd, "KeyType");
7989 char buf[100], resp[200];
7990
7991 if (key_type == NULL)
7992 return -1;
7993
7994 if (strcasecmp(key_type, "GTK") == 0) {
7995 if (wpa_command_resp(intf, "GET gtk", buf, sizeof(buf)) < 0 ||
7996 strncmp(buf, "FAIL", 4) == 0) {
7997 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could "
7998 "not fetch current GTK");
7999 return 0;
8000 }
8001 snprintf(resp, sizeof(resp), "KeyValue,%s", buf);
8002 send_resp(dut, conn, SIGMA_COMPLETE, resp);
8003 return 0;
8004 } else {
8005 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Unsupported "
8006 "KeyType");
8007 return 0;
8008 }
8009
8010 return 1;
8011}
8012
8013
8014static int hs2_set_policy(struct sigma_dut *dut)
8015{
8016#ifdef ANDROID
8017 system("ip rule del prio 23000");
8018 if (system("ip rule add from all lookup main prio 23000") != 0) {
8019 sigma_dut_print(dut, DUT_MSG_ERROR,
8020 "Failed to run:ip rule add from all lookup main prio");
8021 return -1;
8022 }
8023 if (system("ip route flush cache") != 0) {
8024 sigma_dut_print(dut, DUT_MSG_ERROR,
8025 "Failed to run ip route flush cache");
8026 return -1;
8027 }
8028 return 1;
8029#else /* ANDROID */
8030 return 0;
8031#endif /* ANDROID */
8032}
8033
8034
8035static int cmd_sta_hs2_associate(struct sigma_dut *dut,
8036 struct sigma_conn *conn,
8037 struct sigma_cmd *cmd)
8038{
8039 const char *intf = get_param(cmd, "Interface");
8040 const char *val = get_param(cmd, "Ignore_blacklist");
8041 struct wpa_ctrl *ctrl;
8042 int res;
8043 char bssid[20], ssid[40], resp[100], buf[100], blacklisted[100];
8044 int tries = 0;
8045 int ignore_blacklist = 0;
8046 const char *events[] = {
8047 "CTRL-EVENT-CONNECTED",
8048 "INTERWORKING-BLACKLISTED",
8049 "INTERWORKING-NO-MATCH",
8050 NULL
8051 };
8052
8053 start_sta_mode(dut);
8054
8055 blacklisted[0] = '\0';
8056 if (val && atoi(val))
8057 ignore_blacklist = 1;
8058
8059try_again:
8060 ctrl = open_wpa_mon(intf);
8061 if (ctrl == NULL) {
8062 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to open "
8063 "wpa_supplicant monitor connection");
8064 return -2;
8065 }
8066
8067 tries++;
8068 if (wpa_command(intf, "INTERWORKING_SELECT auto")) {
8069 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to start "
8070 "Interworking connection");
8071 wpa_ctrl_detach(ctrl);
8072 wpa_ctrl_close(ctrl);
8073 return 0;
8074 }
8075
8076 buf[0] = '\0';
8077 while (1) {
8078 char *pos;
8079 res = get_wpa_cli_events(dut, ctrl, events, buf, sizeof(buf));
8080 pos = strstr(buf, "INTERWORKING-BLACKLISTED");
8081 if (!pos)
8082 break;
8083 pos += 25;
8084 sigma_dut_print(dut, DUT_MSG_DEBUG, "Found blacklisted AP: %s",
8085 pos);
8086 if (!blacklisted[0])
8087 memcpy(blacklisted, pos, strlen(pos) + 1);
8088 }
8089
8090 if (ignore_blacklist && blacklisted[0]) {
8091 char *end;
8092 end = strchr(blacklisted, ' ');
8093 if (end)
8094 *end = '\0';
8095 sigma_dut_print(dut, DUT_MSG_DEBUG, "Try to connect to a blacklisted network: %s",
8096 blacklisted);
8097 snprintf(buf, sizeof(buf), "INTERWORKING_CONNECT %s",
8098 blacklisted);
8099 if (wpa_command(intf, buf)) {
8100 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to start Interworking connection to blacklisted network");
8101 wpa_ctrl_detach(ctrl);
8102 wpa_ctrl_close(ctrl);
8103 return 0;
8104 }
8105 res = get_wpa_cli_event(dut, ctrl, "CTRL-EVENT-CONNECTED",
8106 buf, sizeof(buf));
8107 }
8108
8109 wpa_ctrl_detach(ctrl);
8110 wpa_ctrl_close(ctrl);
8111
8112 if (res < 0) {
8113 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Could not "
8114 "connect");
8115 return 0;
8116 }
8117
8118 if (strstr(buf, "INTERWORKING-NO-MATCH") ||
8119 strstr(buf, "INTERWORKING-BLACKLISTED")) {
8120 if (tries < 2) {
8121 sigma_dut_print(dut, DUT_MSG_INFO, "No match found - try again to verify no APs were missed in the scan");
8122 goto try_again;
8123 }
8124 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,No network with "
8125 "matching credentials found");
8126 return 0;
8127 }
8128
8129 if (get_wpa_status(intf, "bssid", bssid, sizeof(bssid)) < 0 ||
8130 get_wpa_status(intf, "ssid", ssid, sizeof(ssid)) < 0) {
8131 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Could not "
8132 "get current BSSID/SSID");
8133 return 0;
8134 }
8135
8136 snprintf(resp, sizeof(resp), "SSID,%s,BSSID,%s", ssid, bssid);
8137 send_resp(dut, conn, SIGMA_COMPLETE, resp);
8138 hs2_set_policy(dut);
8139 return 0;
8140}
8141
8142
8143static int sta_add_credential_uname_pwd(struct sigma_dut *dut,
8144 struct sigma_conn *conn,
8145 const char *ifname,
8146 struct sigma_cmd *cmd)
8147{
8148 const char *val;
8149 int id;
8150
8151 id = add_cred(ifname);
8152 if (id < 0)
8153 return -2;
8154 sigma_dut_print(dut, DUT_MSG_DEBUG, "Adding credential %d", id);
8155
8156 val = get_param(cmd, "prefer");
8157 if (val && atoi(val) > 0)
8158 set_cred(ifname, id, "priority", "1");
8159
8160 val = get_param(cmd, "REALM");
8161 if (val && set_cred_quoted(ifname, id, "realm", val) < 0) {
8162 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not set "
8163 "realm");
8164 return 0;
8165 }
8166
8167 val = get_param(cmd, "HOME_FQDN");
8168 if (val && set_cred_quoted(ifname, id, "domain", val) < 0) {
8169 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not set "
8170 "home_fqdn");
8171 return 0;
8172 }
8173
8174 val = get_param(cmd, "Username");
8175 if (val && set_cred_quoted(ifname, id, "username", val) < 0) {
8176 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not set "
8177 "username");
8178 return 0;
8179 }
8180
8181 val = get_param(cmd, "Password");
8182 if (val && set_cred_quoted(ifname, id, "password", val) < 0) {
8183 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not set "
8184 "password");
8185 return 0;
8186 }
8187
8188 val = get_param(cmd, "ROOT_CA");
8189 if (val) {
8190 char fname[200];
8191 snprintf(fname, sizeof(fname), "%s/%s", sigma_cert_path, val);
8192#ifdef __linux__
8193 if (!file_exists(fname)) {
8194 char msg[300];
8195 snprintf(msg, sizeof(msg), "ErrorCode,ROOT_CA "
8196 "file (%s) not found", fname);
8197 send_resp(dut, conn, SIGMA_ERROR, msg);
8198 return 0;
8199 }
8200#endif /* __linux__ */
8201 if (set_cred_quoted(ifname, id, "ca_cert", fname) < 0) {
8202 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could "
8203 "not set root CA");
8204 return 0;
8205 }
8206 }
8207
8208 return 1;
8209}
8210
8211
8212static int update_devdetail_imsi(struct sigma_dut *dut, const char *imsi)
8213{
8214 FILE *in, *out;
8215 char buf[500];
8216 int found = 0;
8217
8218 in = fopen("devdetail.xml", "r");
8219 if (in == NULL)
8220 return -1;
8221 out = fopen("devdetail.xml.tmp", "w");
8222 if (out == NULL) {
8223 fclose(in);
8224 return -1;
8225 }
8226
8227 while (fgets(buf, sizeof(buf), in)) {
8228 char *pos = strstr(buf, "<IMSI>");
8229 if (pos) {
8230 sigma_dut_print(dut, DUT_MSG_INFO, "Updated DevDetail IMSI to %s",
8231 imsi);
8232 pos += 6;
8233 *pos = '\0';
8234 fprintf(out, "%s%s</IMSI>\n", buf, imsi);
8235 found++;
8236 } else {
8237 fprintf(out, "%s", buf);
8238 }
8239 }
8240
8241 fclose(out);
8242 fclose(in);
8243 if (found)
8244 rename("devdetail.xml.tmp", "devdetail.xml");
8245 else
8246 unlink("devdetail.xml.tmp");
8247
8248 return 0;
8249}
8250
8251
8252static int sta_add_credential_sim(struct sigma_dut *dut,
8253 struct sigma_conn *conn,
8254 const char *ifname, struct sigma_cmd *cmd)
8255{
8256 const char *val, *imsi = NULL;
8257 int id;
8258 char buf[200];
8259 int res;
8260 const char *pos;
8261 size_t mnc_len;
8262 char plmn_mcc[4];
8263 char plmn_mnc[4];
8264
8265 id = add_cred(ifname);
8266 if (id < 0)
8267 return -2;
8268 sigma_dut_print(dut, DUT_MSG_DEBUG, "Adding credential %d", id);
8269
8270 val = get_param(cmd, "prefer");
8271 if (val && atoi(val) > 0)
8272 set_cred(ifname, id, "priority", "1");
8273
8274 val = get_param(cmd, "PLMN_MCC");
8275 if (val == NULL) {
8276 send_resp(dut, conn, SIGMA_ERROR,
8277 "errorCode,Missing PLMN_MCC");
8278 return 0;
8279 }
8280 if (strlen(val) != 3) {
8281 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Invalid MCC");
8282 return 0;
8283 }
8284 snprintf(plmn_mcc, sizeof(plmn_mcc), "%s", val);
8285
8286 val = get_param(cmd, "PLMN_MNC");
8287 if (val == NULL) {
8288 send_resp(dut, conn, SIGMA_ERROR,
8289 "errorCode,Missing PLMN_MNC");
8290 return 0;
8291 }
8292 if (strlen(val) != 2 && strlen(val) != 3) {
8293 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Invalid MNC");
8294 return 0;
8295 }
8296 snprintf(plmn_mnc, sizeof(plmn_mnc), "%s", val);
8297
8298 val = get_param(cmd, "IMSI");
8299 if (val == NULL) {
8300 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Missing SIM "
8301 "IMSI");
8302 return 0;
8303 }
8304
8305 imsi = pos = val;
8306
8307 if (strncmp(plmn_mcc, pos, 3) != 0) {
8308 send_resp(dut, conn, SIGMA_ERROR, "errorCode,MCC mismatch");
8309 return 0;
8310 }
8311 pos += 3;
8312
8313 mnc_len = strlen(plmn_mnc);
8314 if (mnc_len < 2) {
8315 send_resp(dut, conn, SIGMA_ERROR, "errorCode,MNC not set");
8316 return 0;
8317 }
8318
8319 if (strncmp(plmn_mnc, pos, mnc_len) != 0) {
8320 send_resp(dut, conn, SIGMA_ERROR, "errorCode,MNC mismatch");
8321 return 0;
8322 }
8323 pos += mnc_len;
8324
8325 res = snprintf(buf, sizeof(buf), "%s%s-%s",plmn_mcc, plmn_mnc, pos);
8326 if (res < 0 || res >= (int) sizeof(buf))
8327 return -1;
8328 if (set_cred_quoted(ifname, id, "imsi", buf) < 0) {
8329 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could "
8330 "not set IMSI");
8331 return 0;
8332 }
8333
8334 val = get_param(cmd, "Password");
8335 if (val && set_cred_quoted(ifname, id, "milenage", val) < 0) {
8336 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could "
8337 "not set password");
8338 return 0;
8339 }
8340
8341 if (dut->program == PROGRAM_HS2_R2) {
8342 /*
8343 * Set provisioning_sp for the test cases where SIM/USIM
8344 * provisioning is used.
8345 */
8346 if (val && set_cred_quoted(ifname, id, "provisioning_sp",
8347 "wi-fi.org") < 0) {
8348 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could "
8349 "not set provisioning_sp");
8350 return 0;
8351 }
8352
8353 update_devdetail_imsi(dut, imsi);
8354 }
8355
8356 return 1;
8357}
8358
8359
8360static int sta_add_credential_cert(struct sigma_dut *dut,
8361 struct sigma_conn *conn,
8362 const char *ifname,
8363 struct sigma_cmd *cmd)
8364{
8365 const char *val;
8366 int id;
8367
8368 id = add_cred(ifname);
8369 if (id < 0)
8370 return -2;
8371 sigma_dut_print(dut, DUT_MSG_DEBUG, "Adding credential %d", id);
8372
8373 val = get_param(cmd, "prefer");
8374 if (val && atoi(val) > 0)
8375 set_cred(ifname, id, "priority", "1");
8376
8377 val = get_param(cmd, "REALM");
8378 if (val && set_cred_quoted(ifname, id, "realm", val) < 0) {
8379 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not set "
8380 "realm");
8381 return 0;
8382 }
8383
8384 val = get_param(cmd, "HOME_FQDN");
8385 if (val && set_cred_quoted(ifname, id, "domain", val) < 0) {
8386 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not set "
8387 "home_fqdn");
8388 return 0;
8389 }
8390
8391 val = get_param(cmd, "Username");
8392 if (val && set_cred_quoted(ifname, id, "username", val) < 0) {
8393 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not set "
8394 "username");
8395 return 0;
8396 }
8397
8398 val = get_param(cmd, "clientCertificate");
8399 if (val) {
8400 char fname[200];
8401 snprintf(fname, sizeof(fname), "%s/%s", sigma_cert_path, val);
8402#ifdef __linux__
8403 if (!file_exists(fname)) {
8404 char msg[300];
8405 snprintf(msg, sizeof(msg),
8406 "ErrorCode,clientCertificate "
8407 "file (%s) not found", fname);
8408 send_resp(dut, conn, SIGMA_ERROR, msg);
8409 return 0;
8410 }
8411#endif /* __linux__ */
8412 if (set_cred_quoted(ifname, id, "client_cert", fname) < 0) {
8413 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could "
8414 "not set client_cert");
8415 return 0;
8416 }
8417 if (set_cred_quoted(ifname, id, "private_key", fname) < 0) {
8418 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could "
8419 "not set private_key");
8420 return 0;
8421 }
8422 }
8423
8424 val = get_param(cmd, "ROOT_CA");
8425 if (val) {
8426 char fname[200];
8427 snprintf(fname, sizeof(fname), "%s/%s", sigma_cert_path, val);
8428#ifdef __linux__
8429 if (!file_exists(fname)) {
8430 char msg[300];
8431 snprintf(msg, sizeof(msg), "ErrorCode,ROOT_CA "
8432 "file (%s) not found", fname);
8433 send_resp(dut, conn, SIGMA_ERROR, msg);
8434 return 0;
8435 }
8436#endif /* __linux__ */
8437 if (set_cred_quoted(ifname, id, "ca_cert", fname) < 0) {
8438 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could "
8439 "not set root CA");
8440 return 0;
8441 }
8442 }
8443
8444 return 1;
8445}
8446
8447
8448static int cmd_sta_add_credential(struct sigma_dut *dut,
8449 struct sigma_conn *conn,
8450 struct sigma_cmd *cmd)
8451{
8452 const char *intf = get_param(cmd, "Interface");
8453 const char *type;
8454
8455 start_sta_mode(dut);
8456
8457 type = get_param(cmd, "Type");
8458 if (!type)
8459 return -1;
8460
8461 if (strcasecmp(type, "uname_pwd") == 0)
8462 return sta_add_credential_uname_pwd(dut, conn, intf, cmd);
8463
8464 if (strcasecmp(type, "sim") == 0)
8465 return sta_add_credential_sim(dut, conn, intf, cmd);
8466
8467 if (strcasecmp(type, "cert") == 0)
8468 return sta_add_credential_cert(dut, conn, intf, cmd);
8469
8470 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Unsupported credential "
8471 "type");
8472 return 0;
8473}
8474
8475
8476static int cmd_sta_scan(struct sigma_dut *dut, struct sigma_conn *conn,
8477 struct sigma_cmd *cmd)
8478{
8479 const char *intf = get_param(cmd, "Interface");
8480 const char *val;
8481 char buf[100];
8482 int res;
8483
8484 val = get_param(cmd, "HESSID");
8485 if (val) {
8486 res = snprintf(buf, sizeof(buf), "SET hessid %s", val);
8487 if (res < 0 || res >= (int) sizeof(buf))
8488 return -1;
8489 wpa_command(intf, buf);
8490 }
8491
8492 val = get_param(cmd, "ACCS_NET_TYPE");
8493 if (val) {
8494 res = snprintf(buf, sizeof(buf), "SET access_network_type %s",
8495 val);
8496 if (res < 0 || res >= (int) sizeof(buf))
8497 return -1;
8498 wpa_command(intf, buf);
8499 }
8500
8501 if (wpa_command(intf, "SCAN")) {
8502 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not start "
8503 "scan");
8504 return 0;
8505 }
8506
8507 return 1;
8508}
8509
8510
8511static int cmd_sta_set_systime(struct sigma_dut *dut, struct sigma_conn *conn,
8512 struct sigma_cmd *cmd)
8513{
8514#ifdef __linux__
8515 struct timeval tv;
8516 struct tm tm;
8517 time_t t;
8518 const char *val;
Pradeep Reddy POTTETI429c69e2016-10-13 17:22:03 +05308519 int v;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02008520
8521 wpa_command(get_station_ifname(), "PMKSA_FLUSH");
8522
8523 memset(&tm, 0, sizeof(tm));
8524 val = get_param(cmd, "seconds");
8525 if (val)
8526 tm.tm_sec = atoi(val);
8527 val = get_param(cmd, "minutes");
8528 if (val)
8529 tm.tm_min = atoi(val);
8530 val = get_param(cmd, "hours");
8531 if (val)
8532 tm.tm_hour = atoi(val);
8533 val = get_param(cmd, "date");
8534 if (val)
8535 tm.tm_mday = atoi(val);
8536 val = get_param(cmd, "month");
Pradeep Reddy POTTETI429c69e2016-10-13 17:22:03 +05308537 if (val) {
8538 v = atoi(val);
8539 if (v < 1 || v > 12) {
8540 send_resp(dut, conn, SIGMA_INVALID,
8541 "errorCode,Invalid month");
8542 return 0;
8543 }
8544 tm.tm_mon = v - 1;
8545 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02008546 val = get_param(cmd, "year");
8547 if (val) {
8548 int year = atoi(val);
8549#ifdef ANDROID
8550 if (year > 2035)
8551 year = 2035; /* years beyond 2035 not supported */
8552#endif /* ANDROID */
8553 tm.tm_year = year - 1900;
8554 }
8555 t = mktime(&tm);
8556 if (t == (time_t) -1) {
8557 send_resp(dut, conn, SIGMA_ERROR,
8558 "errorCode,Invalid date or time");
8559 return 0;
8560 }
8561
8562 memset(&tv, 0, sizeof(tv));
8563 tv.tv_sec = t;
8564
8565 if (settimeofday(&tv, NULL) < 0) {
8566 sigma_dut_print(dut, DUT_MSG_INFO, "settimeofday failed: %s",
8567 strerror(errno));
8568 send_resp(dut, conn, SIGMA_ERROR,
8569 "errorCode,Failed to set time");
8570 return 0;
8571 }
8572
8573 return 1;
8574#endif /* __linux__ */
8575
8576 return -1;
8577}
8578
8579
8580static int cmd_sta_osu(struct sigma_dut *dut, struct sigma_conn *conn,
8581 struct sigma_cmd *cmd)
8582{
8583 const char *intf = get_param(cmd, "Interface");
8584 const char *name, *val;
8585 int prod_ess_assoc = 1;
8586 char buf[200], bssid[100], ssid[100];
8587 int res;
8588 struct wpa_ctrl *ctrl;
8589
8590 name = get_param(cmd, "osuFriendlyName");
8591
8592 val = get_param(cmd, "ProdESSAssoc");
8593 if (val)
8594 prod_ess_assoc = atoi(val);
8595
8596 kill_dhcp_client(dut, intf);
8597 if (start_dhcp_client(dut, intf) < 0)
8598 return -2;
8599
8600 sigma_dut_print(dut, DUT_MSG_DEBUG, "Trigger OSU");
8601 mkdir("Logs", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
8602 res = snprintf(buf, sizeof(buf),
8603 "%s %s%s%s signup osu-ca.pem",
8604 prod_ess_assoc ? "" : "-N",
8605 name ? "-O'" : "", name ? name : "",
8606 name ? "'" : "");
8607
Kanchanapally, Vidyullatha12b66762015-12-31 16:46:42 +05308608 hs2_set_policy(dut);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02008609 if (run_hs20_osu(dut, buf) < 0) {
8610 FILE *f;
8611
8612 sigma_dut_print(dut, DUT_MSG_INFO, "Failed to complete OSU");
8613
8614 f = fopen("hs20-osu-client.res", "r");
8615 if (f) {
8616 char resp[400], res[300], *pos;
8617 if (!fgets(res, sizeof(res), f))
8618 res[0] = '\0';
8619 pos = strchr(res, '\n');
8620 if (pos)
8621 *pos = '\0';
8622 fclose(f);
8623 sigma_dut_summary(dut, "hs20-osu-client provisioning failed: %s",
8624 res);
8625 snprintf(resp, sizeof(resp), "notify-send '%s'", res);
8626 if (system(resp) != 0) {
8627 }
8628 snprintf(resp, sizeof(resp),
8629 "SSID,,BSSID,,failureReason,%s", res);
8630 send_resp(dut, conn, SIGMA_COMPLETE, resp);
8631 return 0;
8632 }
8633
8634 send_resp(dut, conn, SIGMA_COMPLETE, "SSID,,BSSID,");
8635 return 0;
8636 }
8637
8638 if (!prod_ess_assoc)
8639 goto report;
8640
8641 ctrl = open_wpa_mon(intf);
8642 if (ctrl == NULL) {
8643 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to open "
8644 "wpa_supplicant monitor connection");
8645 return -1;
8646 }
8647
8648 res = get_wpa_cli_event(dut, ctrl, "CTRL-EVENT-CONNECTED",
8649 buf, sizeof(buf));
8650
8651 wpa_ctrl_detach(ctrl);
8652 wpa_ctrl_close(ctrl);
8653
8654 if (res < 0) {
8655 sigma_dut_print(dut, DUT_MSG_INFO, "Failed to connect to "
8656 "network after OSU");
8657 send_resp(dut, conn, SIGMA_COMPLETE, "SSID,,BSSID,");
8658 return 0;
8659 }
8660
8661report:
8662 if (get_wpa_status(intf, "bssid", bssid, sizeof(bssid)) < 0 ||
8663 get_wpa_status(intf, "ssid", ssid, sizeof(ssid)) < 0) {
8664 sigma_dut_print(dut, DUT_MSG_INFO, "Failed to get BSSID/SSID");
8665 send_resp(dut, conn, SIGMA_COMPLETE, "SSID,,BSSID,");
8666 return 0;
8667 }
8668
8669 snprintf(buf, sizeof(buf), "SSID,%s,BSSID,%s", ssid, bssid);
8670 send_resp(dut, conn, SIGMA_COMPLETE, buf);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02008671 return 0;
8672}
8673
8674
8675static int cmd_sta_policy_update(struct sigma_dut *dut, struct sigma_conn *conn,
8676 struct sigma_cmd *cmd)
8677{
8678 const char *val;
8679 int timeout = 120;
8680
8681 val = get_param(cmd, "PolicyUpdate");
8682 if (val == NULL || atoi(val) == 0)
8683 return 1; /* No operation requested */
8684
8685 val = get_param(cmd, "Timeout");
8686 if (val)
8687 timeout = atoi(val);
8688
8689 if (timeout) {
8690 /* TODO: time out the command and return
8691 * PolicyUpdateStatus,TIMEOUT if needed. */
8692 }
8693
8694 sigma_dut_print(dut, DUT_MSG_DEBUG, "Trigger policy update");
8695 mkdir("Logs", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
8696 if (run_hs20_osu(dut, "pol_upd fqdn=wi-fi.org") < 0) {
8697 send_resp(dut, conn, SIGMA_COMPLETE, "PolicyUpdateStatus,FAIL");
8698 return 0;
8699 }
8700
8701 send_resp(dut, conn, SIGMA_COMPLETE, "PolicyUpdateStatus,SUCCESS");
8702 return 0;
8703}
8704
8705
8706static int cmd_sta_er_config(struct sigma_dut *dut, struct sigma_conn *conn,
8707 struct sigma_cmd *cmd)
8708{
8709 struct wpa_ctrl *ctrl;
8710 const char *intf = get_param(cmd, "Interface");
8711 const char *bssid = get_param(cmd, "Bssid");
8712 const char *ssid = get_param(cmd, "SSID");
8713 const char *security = get_param(cmd, "Security");
8714 const char *passphrase = get_param(cmd, "Passphrase");
8715 const char *pin = get_param(cmd, "PIN");
8716 char buf[1000];
8717 char ssid_hex[200], passphrase_hex[200];
8718 const char *keymgmt, *cipher;
8719
8720 if (intf == NULL)
8721 intf = get_main_ifname();
8722
8723 if (!bssid) {
8724 send_resp(dut, conn, SIGMA_ERROR,
8725 "ErrorCode,Missing Bssid argument");
8726 return 0;
8727 }
8728
8729 if (!ssid) {
8730 send_resp(dut, conn, SIGMA_ERROR,
8731 "ErrorCode,Missing SSID argument");
8732 return 0;
8733 }
8734
8735 if (!security) {
8736 send_resp(dut, conn, SIGMA_ERROR,
8737 "ErrorCode,Missing Security argument");
8738 return 0;
8739 }
8740
8741 if (!passphrase) {
8742 send_resp(dut, conn, SIGMA_ERROR,
8743 "ErrorCode,Missing Passphrase argument");
8744 return 0;
8745 }
8746
8747 if (!pin) {
8748 send_resp(dut, conn, SIGMA_ERROR,
8749 "ErrorCode,Missing PIN argument");
8750 return 0;
8751 }
8752
vamsi krishna8c9c1562017-05-12 15:51:46 +05308753 if (2 * strlen(ssid) >= sizeof(ssid_hex) ||
8754 2 * strlen(passphrase) >= sizeof(passphrase_hex)) {
Jouni Malinencd4e3c32015-10-29 12:39:56 +02008755 send_resp(dut, conn, SIGMA_ERROR,
8756 "ErrorCode,Too long SSID/passphrase");
8757 return 0;
8758 }
8759
8760 ctrl = open_wpa_mon(intf);
8761 if (ctrl == NULL) {
8762 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to open "
8763 "wpa_supplicant monitor connection");
8764 return -2;
8765 }
8766
8767 if (strcasecmp(security, "wpa2-psk") == 0) {
8768 keymgmt = "WPA2PSK";
8769 cipher = "CCMP";
8770 } else {
8771 wpa_ctrl_detach(ctrl);
8772 wpa_ctrl_close(ctrl);
8773 send_resp(dut, conn, SIGMA_ERROR,
8774 "ErrorCode,Unsupported Security value");
8775 return 0;
8776 }
8777
8778 ascii2hexstr(ssid, ssid_hex);
8779 ascii2hexstr(passphrase, passphrase_hex);
8780 snprintf(buf, sizeof(buf), "WPS_REG %s %s %s %s %s %s",
8781 bssid, pin, ssid_hex, keymgmt, cipher, passphrase_hex);
8782
8783 if (wpa_command(intf, buf) < 0) {
8784 wpa_ctrl_detach(ctrl);
8785 wpa_ctrl_close(ctrl);
8786 send_resp(dut, conn, SIGMA_ERROR,
8787 "ErrorCode,Failed to start registrar");
8788 return 0;
8789 }
8790
8791 snprintf(dut->er_oper_bssid, sizeof(dut->er_oper_bssid), "%s", bssid);
8792 dut->er_oper_performed = 1;
8793
8794 return wps_connection_event(dut, conn, ctrl, intf, 0);
8795}
8796
8797
8798static int cmd_sta_wps_connect_pw_token(struct sigma_dut *dut,
8799 struct sigma_conn *conn,
8800 struct sigma_cmd *cmd)
8801{
8802 struct wpa_ctrl *ctrl;
8803 const char *intf = get_param(cmd, "Interface");
8804 const char *bssid = get_param(cmd, "Bssid");
8805 char buf[100];
8806
8807 if (!bssid) {
8808 send_resp(dut, conn, SIGMA_ERROR,
8809 "ErrorCode,Missing Bssid argument");
8810 return 0;
8811 }
8812
8813 ctrl = open_wpa_mon(intf);
8814 if (ctrl == NULL) {
8815 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to open "
8816 "wpa_supplicant monitor connection");
8817 return -2;
8818 }
8819
8820 snprintf(buf, sizeof(buf), "WPS_NFC %s", bssid);
8821
8822 if (wpa_command(intf, buf) < 0) {
8823 wpa_ctrl_detach(ctrl);
8824 wpa_ctrl_close(ctrl);
8825 send_resp(dut, conn, SIGMA_ERROR,
8826 "ErrorCode,Failed to start registrar");
8827 return 0;
8828 }
8829
8830 return wps_connection_event(dut, conn, ctrl, intf, 0);
8831}
8832
8833
8834static int req_intf(struct sigma_cmd *cmd)
8835{
8836 return get_param(cmd, "interface") == NULL ? -1 : 0;
8837}
8838
8839
8840void sta_register_cmds(void)
8841{
8842 sigma_dut_reg_cmd("sta_get_ip_config", req_intf,
8843 cmd_sta_get_ip_config);
8844 sigma_dut_reg_cmd("sta_set_ip_config", req_intf,
8845 cmd_sta_set_ip_config);
8846 sigma_dut_reg_cmd("sta_get_info", req_intf, cmd_sta_get_info);
8847 sigma_dut_reg_cmd("sta_get_mac_address", req_intf,
8848 cmd_sta_get_mac_address);
8849 sigma_dut_reg_cmd("sta_is_connected", req_intf, cmd_sta_is_connected);
8850 sigma_dut_reg_cmd("sta_verify_ip_connection", req_intf,
8851 cmd_sta_verify_ip_connection);
8852 sigma_dut_reg_cmd("sta_get_bssid", req_intf, cmd_sta_get_bssid);
8853 sigma_dut_reg_cmd("sta_set_encryption", req_intf,
8854 cmd_sta_set_encryption);
8855 sigma_dut_reg_cmd("sta_set_psk", req_intf, cmd_sta_set_psk);
8856 sigma_dut_reg_cmd("sta_set_eaptls", req_intf, cmd_sta_set_eaptls);
8857 sigma_dut_reg_cmd("sta_set_eapttls", req_intf, cmd_sta_set_eapttls);
8858 sigma_dut_reg_cmd("sta_set_eapsim", req_intf, cmd_sta_set_eapsim);
8859 sigma_dut_reg_cmd("sta_set_peap", req_intf, cmd_sta_set_peap);
8860 sigma_dut_reg_cmd("sta_set_eapfast", req_intf, cmd_sta_set_eapfast);
8861 sigma_dut_reg_cmd("sta_set_eapaka", req_intf, cmd_sta_set_eapaka);
8862 sigma_dut_reg_cmd("sta_set_eapakaprime", req_intf,
8863 cmd_sta_set_eapakaprime);
8864 sigma_dut_reg_cmd("sta_set_security", req_intf, cmd_sta_set_security);
8865 sigma_dut_reg_cmd("sta_set_uapsd", req_intf, cmd_sta_set_uapsd);
8866 /* TODO: sta_set_ibss */
8867 /* TODO: sta_set_mode */
8868 sigma_dut_reg_cmd("sta_set_wmm", req_intf, cmd_sta_set_wmm);
8869 sigma_dut_reg_cmd("sta_associate", req_intf, cmd_sta_associate);
8870 /* TODO: sta_up_load */
8871 sigma_dut_reg_cmd("sta_preset_testparameters", req_intf,
8872 cmd_sta_preset_testparameters);
8873 /* TODO: sta_set_system */
8874 sigma_dut_reg_cmd("sta_set_11n", req_intf, cmd_sta_set_11n);
8875 /* TODO: sta_set_rifs_test */
8876 sigma_dut_reg_cmd("sta_set_wireless", req_intf, cmd_sta_set_wireless);
8877 sigma_dut_reg_cmd("sta_send_addba", req_intf, cmd_sta_send_addba);
8878 /* TODO: sta_send_coexist_mgmt */
8879 sigma_dut_reg_cmd("sta_disconnect", req_intf, cmd_sta_disconnect);
8880 sigma_dut_reg_cmd("sta_reassoc", req_intf, cmd_sta_reassoc);
8881 sigma_dut_reg_cmd("sta_reassociate", req_intf, cmd_sta_reassoc);
8882 sigma_dut_reg_cmd("sta_reset_default", req_intf,
8883 cmd_sta_reset_default);
8884 sigma_dut_reg_cmd("sta_send_frame", req_intf, cmd_sta_send_frame);
8885 sigma_dut_reg_cmd("sta_set_macaddr", req_intf, cmd_sta_set_macaddr);
8886 sigma_dut_reg_cmd("sta_set_rfeature", req_intf, cmd_sta_set_rfeature);
8887 sigma_dut_reg_cmd("sta_set_radio", req_intf, cmd_sta_set_radio);
8888 sigma_dut_reg_cmd("sta_set_pwrsave", req_intf, cmd_sta_set_pwrsave);
8889 sigma_dut_reg_cmd("sta_bssid_pool", req_intf, cmd_sta_bssid_pool);
8890 sigma_dut_reg_cmd("sta_reset_parm", req_intf, cmd_sta_reset_parm);
8891 sigma_dut_reg_cmd("sta_get_key", req_intf, cmd_sta_get_key);
8892 sigma_dut_reg_cmd("sta_hs2_associate", req_intf,
8893 cmd_sta_hs2_associate);
8894 sigma_dut_reg_cmd("sta_add_credential", req_intf,
8895 cmd_sta_add_credential);
8896 sigma_dut_reg_cmd("sta_scan", req_intf, cmd_sta_scan);
8897 sigma_dut_reg_cmd("sta_set_systime", NULL, cmd_sta_set_systime);
8898 sigma_dut_reg_cmd("sta_osu", req_intf, cmd_sta_osu);
8899 sigma_dut_reg_cmd("sta_policy_update", req_intf, cmd_sta_policy_update);
8900 sigma_dut_reg_cmd("sta_er_config", NULL, cmd_sta_er_config);
8901 sigma_dut_reg_cmd("sta_wps_connect_pw_token", req_intf,
8902 cmd_sta_wps_connect_pw_token);
8903 sigma_dut_reg_cmd("sta_exec_action", req_intf, cmd_sta_exec_action);
8904 sigma_dut_reg_cmd("sta_get_events", req_intf, cmd_sta_get_events);
8905 sigma_dut_reg_cmd("sta_get_parameter", req_intf, cmd_sta_get_parameter);
8906}