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