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