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