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