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