blob: 7f8d16b3c516ab6668b6dfed181c515d61ca1ffd [file] [log] [blame]
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001/*
2 * Sigma Control API DUT (station/AP)
3 * Copyright (c) 2010-2011, Atheros Communications, Inc.
4 * Copyright (c) 2011-2015, Qualcomm Atheros, Inc.
5 * All Rights Reserved.
6 * Licensed under the Clear BSD license. See README for more details.
7 */
8
9#include "sigma_dut.h"
10#include <sys/ioctl.h>
11#include <sys/stat.h>
12#ifdef __linux__
13#include <sys/time.h>
14#include <netpacket/packet.h>
15#include <linux/if_ether.h>
16#ifdef ANDROID
17#include <cutils/properties.h>
18#include <android/log.h>
19#include "keystore_get.h"
20#else /* ANDROID */
21#include <ifaddrs.h>
22#endif /* ANDROID */
23#include <netdb.h>
24#endif /* __linux__ */
25#ifdef __QNXNTO__
26#include <net/if_dl.h>
27#endif /* __QNXNTO__ */
28#include "wpa_ctrl.h"
29#include "wpa_helpers.h"
30
31/* Temporary files for sta_send_addba */
32#define VI_QOS_TMP_FILE "/tmp/vi-qos.tmp"
33#define VI_QOS_FILE "/tmp/vi-qos.txt"
34#define VI_QOS_REFFILE "/etc/vi-qos.txt"
35
36/*
37 * MTU for Ethernet need to take into account 8-byte SNAP header
38 * to be added when encapsulating Ethernet frame into 802.11
39 */
40#ifndef IEEE80211_MAX_DATA_LEN_DMG
41#define IEEE80211_MAX_DATA_LEN_DMG 7920
42#endif
43#ifndef IEEE80211_SNAP_LEN_DMG
44#define IEEE80211_SNAP_LEN_DMG 8
45#endif
46
47extern char *sigma_wpas_ctrl;
48extern char *sigma_cert_path;
49extern enum driver_type wifi_chip_type;
50extern char *sigma_radio_ifname[];
51
52
53#ifdef ANDROID
54
55static int add_ipv6_rule(struct sigma_dut *dut, const char *ifname);
56
57#define ANDROID_KEYSTORE_GET 'g'
58#define ANDROID_KEYSTORE_GET_PUBKEY 'b'
59
60static int android_keystore_get(char cmd, const char *key, unsigned char *val)
61{
62#ifdef ANDROID43
63 /* Android 4.3 changed keystore design, so need to use keystore_get() */
64#ifndef KEYSTORE_MESSAGE_SIZE
65#define KEYSTORE_MESSAGE_SIZE 65535
66#endif /* KEYSTORE_MESSAGE_SIZE */
67
68 ssize_t len;
69 uint8_t *value = NULL;
70
71 __android_log_print(ANDROID_LOG_DEBUG, "sigma_dut",
72 "keystore command '%c' key '%s' --> keystore_get",
73 cmd, key);
74
75 len = keystore_get(key, strlen(key), &value);
76 if (len < 0) {
77 __android_log_print(ANDROID_LOG_DEBUG, "sigma_dut",
78 "keystore_get() failed");
79 return -1;
80 }
81
82 if (len > KEYSTORE_MESSAGE_SIZE)
83 len = KEYSTORE_MESSAGE_SIZE;
84 memcpy(val, value, len);
85 free(value);
86 return len;
87#else /* ANDROID43 */
88 int s, res, reslen = -1, received;
89 size_t keylen;
90 unsigned char hdr[3];
91
92 __android_log_print(ANDROID_LOG_DEBUG, "sigma_dut",
93 "keystore command '%c' key '%s'", cmd, key);
94 keylen = strlen(key);
95 if (keylen > KEYSTORE_MESSAGE_SIZE)
96 return -1;
97
98 s = socket_local_client("keystore", ANDROID_SOCKET_NAMESPACE_RESERVED,
99 SOCK_STREAM);
100 if (s < 0) {
101 __android_log_print(ANDROID_LOG_DEBUG, "sigma_dut",
102 "could not connect to keystore");
103 return -1;
104 }
105
106 hdr[0] = cmd;
107 hdr[1] = keylen >> 8;
108 hdr[2] = keylen & 0xff;
109
110 if (send(s, hdr, sizeof(hdr), MSG_NOSIGNAL) != sizeof(hdr) ||
111 send(s, key, keylen, MSG_NOSIGNAL) != (int) keylen ||
112 shutdown(s, SHUT_WR) != 0) {
113 __android_log_print(ANDROID_LOG_DEBUG, "sigma_dut",
114 "could not send keystore command");
115 goto fail;
116 }
117
118 if (recv(s, hdr, 1, 0) != 1)
119 goto fail;
120 if (hdr[0] != 1) {
121 __android_log_print(ANDROID_LOG_DEBUG, "sigma_dut",
122 "unexpected keystore response %u", hdr[0]);
123 goto fail;
124 }
125 if (recv(s, hdr + 1, 2, 0) != 2)
126 goto fail;
127 reslen = hdr[1] * 256 + hdr[2];
128 __android_log_print(ANDROID_LOG_DEBUG, "sigma_dut",
129 "keystore response length %d", reslen);
130 if (reslen > KEYSTORE_MESSAGE_SIZE) {
131 reslen = -1;
132 goto fail;
133 }
134
135 received = 0;
136 while (received < reslen) {
137 res = recv(s, val + received, reslen - received, 0);
138 __android_log_print(ANDROID_LOG_DEBUG, "sigma_dut",
139 "keystore recv -> %d", res);
140 if (res <= 0) {
141 reslen = -1;
142 break;
143 }
144 received += res;
145 }
146
147fail:
148 close(s);
149 __android_log_print(ANDROID_LOG_DEBUG, "sigma_dut",
150 "keystore get -> %d", reslen);
151 return reslen;
152#endif /* ANDROID43 */
153}
154#endif /* ANDROID */
155
156
157int set_ps(const char *intf, struct sigma_dut *dut, int enabled)
158{
159#ifdef __linux__
160 char buf[100];
161
162 if (wifi_chip_type == DRIVER_WCN) {
163 if (enabled) {
164 snprintf(buf, sizeof(buf), "iwpriv wlan0 dump 906");
165 if (system(buf) != 0) {
166 sigma_dut_print(dut, DUT_MSG_ERROR,
167 "Failed to enable power save");
168 return -1;
169 }
170 } else {
171 snprintf(buf, sizeof(buf), "iwpriv wlan0 dump 905");
172 if (system(buf) != 0) {
173 sigma_dut_print(dut, DUT_MSG_ERROR,
174 "Failed to stop power save timer");
175 return -1;
176 }
177 snprintf(buf, sizeof(buf), "iwpriv wlan0 dump 912");
178 if (system(buf) != 0) {
179 sigma_dut_print(dut, DUT_MSG_ERROR,
180 "Failed to disable power save");
181 return -1;
182 }
183 }
184
185 return 0;
186 }
187
188 snprintf(buf, sizeof(buf), "./iw dev %s set power_save %s",
189 intf, enabled ? "on" : "off");
190 if (system(buf) != 0) {
191 snprintf(buf, sizeof(buf), "iw dev %s set power_save %s",
192 intf, enabled ? "on" : "off");
193 if (system(buf) != 0)
194 return -1;
195 }
196
197 return 0;
198#else /* __linux__ */
199 return -1;
200#endif /* __linux__ */
201}
202
203
204static void static_ip_file(int proto, const char *addr, const char *mask,
205 const char *gw)
206{
207 if (proto) {
208 FILE *f = fopen("static-ip", "w");
209 if (f) {
210 fprintf(f, "%d %s %s %s\n", proto, addr,
211 mask ? mask : "N/A",
212 gw ? gw : "N/A");
213 fclose(f);
214 }
215 } else {
216 unlink("static-ip");
217 }
218}
219
220
221static int send_neighbor_request(struct sigma_dut *dut, const char *intf,
222 const char *ssid)
223{
224#ifdef __linux__
225 char buf[100];
226
227 snprintf(buf, sizeof(buf), "iwpriv %s neighbor %s",
228 intf, ssid);
229 sigma_dut_print(dut, DUT_MSG_INFO, "Request: %s", buf);
230
231 if (system(buf) != 0) {
232 sigma_dut_print(dut, DUT_MSG_ERROR,
233 "iwpriv neighbor request failed");
234 return -1;
235 }
236
237 sigma_dut_print(dut, DUT_MSG_INFO, "iwpriv neighbor request send");
238
239 return 0;
240#else /* __linux__ */
241 return -1;
242#endif /* __linux__ */
243}
244
245
246static int send_trans_mgmt_query(struct sigma_dut *dut, const char *intf,
247 const char *ssid)
248{
249 /*
250 * In the earlier builds we used WNM_QUERY and in later
251 * builds used WNM_BSS_QUERY.
252 */
253
254 if (wpa_command(intf, "WNM_BSS_QUERY 0") != 0) {
255 sigma_dut_print(dut, DUT_MSG_ERROR,
256 "transition management query failed");
257 return -1;
258 }
259
260 sigma_dut_print(dut, DUT_MSG_DEBUG,
261 "transition management query sent");
262
263 return 0;
264}
265
266
267int is_ip_addr(const char *str)
268{
269 const char *pos = str;
270 struct in_addr addr;
271
272 while (*pos) {
273 if (*pos != '.' && (*pos < '0' || *pos > '9'))
274 return 0;
275 pos++;
276 }
277
278 return inet_aton(str, &addr);
279}
280
281
282int is_ipv6_addr(const char *str)
283{
284 struct sockaddr_in6 addr;
285
286 return inet_pton(AF_INET6, str, &(addr.sin6_addr));
287}
288
289
290int get_ip_config(struct sigma_dut *dut, const char *ifname, char *buf,
291 size_t buf_len)
292{
293 char tmp[256], *pos, *pos2;
294 FILE *f;
295 char ip[16], mask[15], dns[16], sec_dns[16];
296 int is_dhcp = 0;
297 int s;
298#ifdef ANDROID
299 char prop[PROPERTY_VALUE_MAX];
300#endif /* ANDROID */
301
302 ip[0] = '\0';
303 mask[0] = '\0';
304 dns[0] = '\0';
305 sec_dns[0] = '\0';
306
307 s = socket(PF_INET, SOCK_DGRAM, 0);
308 if (s >= 0) {
309 struct ifreq ifr;
310 struct sockaddr_in saddr;
311
312 memset(&ifr, 0, sizeof(ifr));
313 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
314 if (ioctl(s, SIOCGIFADDR, &ifr) < 0) {
315 sigma_dut_print(dut, DUT_MSG_INFO, "Failed to get "
316 "%s IP address: %s",
317 ifname, strerror(errno));
318 } else {
319 memcpy(&saddr, &ifr.ifr_addr,
320 sizeof(struct sockaddr_in));
321 strncpy(ip, inet_ntoa(saddr.sin_addr), sizeof(ip));
322 }
323
324 if (ioctl(s, SIOCGIFNETMASK, &ifr) == 0) {
325 memcpy(&saddr, &ifr.ifr_addr,
326 sizeof(struct sockaddr_in));
327 strncpy(mask, inet_ntoa(saddr.sin_addr), sizeof(mask));
328 }
329 close(s);
330 }
331
332#ifdef ANDROID
333 snprintf(tmp, sizeof(tmp), "dhcp.%s.pid", ifname);
334 if (property_get(tmp, prop, NULL) != 0 && atoi(prop) > 0) {
335 snprintf(tmp, sizeof(tmp), "dhcp.%s.result", ifname);
336 if (property_get(tmp, prop, NULL) != 0 &&
337 strcmp(prop, "ok") == 0) {
338 snprintf(tmp, sizeof(tmp), "dhcp.%s.ipaddress",
339 ifname);
340 if (property_get(tmp, prop, NULL) != 0 &&
341 strcmp(ip, prop) == 0)
342 is_dhcp = 1;
343 }
344 }
345
346 snprintf(tmp, sizeof(tmp), "dhcp.%s.dns1", ifname);
347 if (property_get(tmp, prop, NULL) != 0) {
348 strncpy(dns, prop, sizeof(dns));
349 dns[sizeof(dns) - 1] = '\0';
350 } else {
351 if (property_get("net.dns1", prop, NULL) != 0) {
352 strncpy(dns, prop, sizeof(dns));
353 dns[sizeof(dns) - 1] = '\0';
354 }
355 }
356
357 snprintf(tmp, sizeof(tmp), "dhcp.%s.dns2", ifname);
358 if (property_get(tmp, prop, NULL) != 0) {
359 strncpy(sec_dns, prop, sizeof(sec_dns));
360 sec_dns[sizeof(sec_dns) - 1] = '\0';
361 }
362#else /* ANDROID */
363#ifdef __linux__
364 snprintf(tmp, sizeof(tmp), "ps ax | grep dhclient | grep -v grep | "
365 "grep -q %s", ifname);
366 if (system(tmp) == 0)
367 is_dhcp = 1;
368 else {
369 snprintf(tmp, sizeof(tmp), "ps ax | grep udhcpc | "
370 "grep -v grep | grep -q %s", ifname);
371 if (system(tmp) == 0)
372 is_dhcp = 1;
373 else {
374 snprintf(tmp, sizeof(tmp), "ps ax | grep dhcpcd | "
375 "grep -v grep | grep -q %s", ifname);
376 if (system(tmp) == 0)
377 is_dhcp = 1;
378 }
379 }
380#endif /* __linux__ */
381
382 f = fopen("/etc/resolv.conf", "r");
383 if (f) {
384 while (fgets(tmp, sizeof(tmp), f)) {
385 if (strncmp(tmp, "nameserver", 10) != 0)
386 continue;
387 pos = tmp + 10;
388 while (*pos == ' ' || *pos == '\t')
389 pos++;
390 pos2 = pos;
391 while (*pos2) {
392 if (*pos2 == '\n' || *pos2 == '\r') {
393 *pos2 = '\0';
394 break;
395 }
396 pos2++;
397 }
398 if (!dns[0]) {
399 strncpy(dns, pos, sizeof(dns));
400 dns[sizeof(dns) - 1] = '\0';
401 } else if (!sec_dns[0]) {
402 strncpy(sec_dns, pos, sizeof(sec_dns));
403 sec_dns[sizeof(sec_dns) - 1] = '\0';
404 }
405 }
406 fclose(f);
407 }
408#endif /* ANDROID */
409
410 snprintf(buf, buf_len, "dhcp,%d,ip,%s,mask,%s,primary-dns,%s",
411 is_dhcp, ip, mask, dns);
412 buf[buf_len - 1] = '\0';
413
414 return 0;
415}
416
417
418
419
420int get_ipv6_config(struct sigma_dut *dut, const char *ifname, char *buf,
421 size_t buf_len)
422{
423#ifdef __linux__
424#ifdef ANDROID
425 char cmd[200], result[1000], *pos, *end;
426 FILE *f;
427 size_t len;
428
429 snprintf(cmd, sizeof(cmd), "ip addr show dev %s scope global", ifname);
430 f = popen(cmd, "r");
431 if (f == NULL)
432 return -1;
433 len = fread(result, 1, sizeof(result) - 1, f);
434 pclose(f);
435 if (len == 0)
436 return -1;
437 result[len] = '\0';
438 sigma_dut_print(dut, DUT_MSG_DEBUG, "%s result: %s\n", cmd, result);
439
440 pos = strstr(result, "inet6 ");
441 if (pos == NULL)
442 return -1;
443 pos += 6;
444 end = strchr(pos, ' ');
445 if (end)
446 *end = '\0';
447 end = strchr(pos, '/');
448 if (end)
449 *end = '\0';
450 snprintf(buf, buf_len, "ip,%s", pos);
451 buf[buf_len - 1] = '\0';
452 return 0;
453#else /* ANDROID */
454 struct ifaddrs *ifaddr, *ifa;
455 int res, found = 0;
456 char host[NI_MAXHOST];
457
458 if (getifaddrs(&ifaddr) < 0) {
459 perror("getifaddrs");
460 return -1;
461 }
462
463 for (ifa = ifaddr; ifa; ifa = ifa->ifa_next) {
464 if (strcasecmp(ifname, ifa->ifa_name) != 0)
465 continue;
466 if (ifa->ifa_addr == NULL ||
467 ifa->ifa_addr->sa_family != AF_INET6)
468 continue;
469
470 res = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in6),
471 host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
472 if (res != 0) {
473 sigma_dut_print(dut, DUT_MSG_DEBUG, "getnameinfo: %s",
474 gai_strerror(res));
475 continue;
476 }
477 if (strncmp(host, "fe80::", 6) == 0)
478 continue; /* skip link-local */
479
480 sigma_dut_print(dut, DUT_MSG_DEBUG, "ifaddr: %s", host);
481 found = 1;
482 break;
483 }
484
485 freeifaddrs(ifaddr);
486
487 if (found) {
488 char *pos;
489 pos = strchr(host, '%');
490 if (pos)
491 *pos = '\0';
492 snprintf(buf, buf_len, "ip,%s", host);
493 buf[buf_len - 1] = '\0';
494 return 0;
495 }
496
497#endif /* ANDROID */
498#endif /* __linux__ */
499 return -1;
500}
501
502
503static int cmd_sta_get_ip_config(struct sigma_dut *dut,
504 struct sigma_conn *conn,
505 struct sigma_cmd *cmd)
506{
507 const char *intf = get_param(cmd, "Interface");
508 const char *ifname;
509 char buf[200];
510 const char *val;
511 int type = 1;
512
513 if (intf == NULL)
514 return -1;
515
516 if (strcmp(intf, get_main_ifname()) == 0)
517 ifname = get_station_ifname();
518 else
519 ifname = intf;
520
521 /*
522 * UCC may assume the IP address to be available immediately after
523 * association without trying to run sta_get_ip_config multiple times.
524 * Sigma CAPI does not specify this command as a block command that
525 * would wait for the address to become available, but to pass tests
526 * more reliably, it looks like such a wait may be needed here.
527 */
528 if (wait_ip_addr(dut, ifname, 15) < 0) {
529 sigma_dut_print(dut, DUT_MSG_INFO, "Could not get IP address "
530 "for sta_get_ip_config");
531 /*
532 * Try to continue anyway since many UCC tests do not really
533 * care about the return value from here..
534 */
535 }
536
537 val = get_param(cmd, "Type");
538 if (val)
539 type = atoi(val);
540 if (type == 2 || dut->last_set_ip_config_ipv6) {
541 int i;
542
543 /*
544 * Since we do not have proper wait for IPv6 addresses, use a
545 * fixed two second delay here as a workaround for UCC script
546 * assuming IPv6 address is available when this command returns.
547 * Some scripts did not use Type,2 properly for IPv6, so include
548 * also the cases where the previous sta_set_ip_config indicated
549 * use of IPv6.
550 */
551 sigma_dut_print(dut, DUT_MSG_INFO, "Wait up to extra ten seconds in sta_get_ip_config for IPv6 address");
552 for (i = 0; i < 10; i++) {
553 sleep(1);
554 if (get_ipv6_config(dut, ifname, buf, sizeof(buf)) == 0)
555 {
556 sigma_dut_print(dut, DUT_MSG_INFO, "Found IPv6 address");
557 send_resp(dut, conn, SIGMA_COMPLETE, buf);
558#ifdef ANDROID
559 sigma_dut_print(dut, DUT_MSG_INFO,
560 "Adding IPv6 rule on Android");
561 add_ipv6_rule(dut, intf);
562#endif /* ANDROID */
563
564 return 0;
565 }
566 }
567 }
568 if (type == 1) {
569 if (get_ip_config(dut, ifname, buf, sizeof(buf)) < 0)
570 return -2;
571 } else if (type == 2) {
572 if (get_ipv6_config(dut, ifname, buf, sizeof(buf)) < 0)
573 return -2;
574 } else {
575 send_resp(dut, conn, SIGMA_ERROR,
576 "errorCode,Unsupported address type");
577 return 0;
578 }
579
580 send_resp(dut, conn, SIGMA_COMPLETE, buf);
581 return 0;
582}
583
584
585static void kill_dhcp_client(struct sigma_dut *dut, const char *ifname)
586{
587#ifdef __linux__
588 char buf[200];
589 char path[128];
590 struct stat s;
591
592#ifdef ANDROID
593 snprintf(path, sizeof(path), "/data/misc/dhcp/dhcpcd-%s.pid", ifname);
594#else /* ANDROID */
595 snprintf(path, sizeof(path), "/var/run/dhclient-%s.pid", ifname);
596#endif /* ANDROID */
597 if (stat(path, &s) == 0) {
598 snprintf(buf, sizeof(buf), "kill `cat %s`", path);
599 sigma_dut_print(dut, DUT_MSG_INFO,
600 "Kill previous DHCP client: %s", buf);
601 if (system(buf) != 0)
602 sigma_dut_print(dut, DUT_MSG_INFO,
603 "Failed to kill DHCP client");
604 unlink(path);
605 sleep(1);
606 } else {
607 snprintf(path, sizeof(path), "/var/run/dhcpcd-%s.pid", ifname);
608
609 if (stat(path, &s) == 0) {
610 snprintf(buf, sizeof(buf), "kill `cat %s`", path);
611 sigma_dut_print(dut, DUT_MSG_INFO,
612 "Kill previous DHCP client: %s", buf);
613 if (system(buf) != 0)
614 sigma_dut_print(dut, DUT_MSG_INFO,
615 "Failed to kill DHCP client");
616 unlink(path);
617 sleep(1);
618 }
619 }
620#endif /* __linux__ */
621}
622
623
624static int start_dhcp_client(struct sigma_dut *dut, const char *ifname)
625{
626#ifdef __linux__
627 char buf[200];
628
629#ifdef ANDROID
630 snprintf(buf, sizeof(buf),
631 "/system/bin/dhcpcd -b %s", ifname);
632#else /* ANDROID */
633 snprintf(buf, sizeof(buf),
634 "dhclient -nw -pf /var/run/dhclient-%s.pid %s",
635 ifname, ifname);
636#endif /* ANDROID */
637 sigma_dut_print(dut, DUT_MSG_INFO, "Start DHCP client: %s", buf);
638 if (system(buf) != 0) {
639 snprintf(buf, sizeof(buf), "dhcpcd -t 0 %s &", ifname);
640 if (system(buf) != 0) {
641 sigma_dut_print(dut, DUT_MSG_INFO,
642 "Failed to start DHCP client");
643#ifndef ANDROID
644 return -1;
645#endif /* ANDROID */
646 }
647 }
648#endif /* __linux__ */
649
650 return 0;
651}
652
653
654static int clear_ip_addr(struct sigma_dut *dut, const char *ifname)
655{
656#ifdef __linux__
657 char buf[200];
658
659 snprintf(buf, sizeof(buf), "ip addr flush dev %s", ifname);
660 if (system(buf) != 0) {
661 sigma_dut_print(dut, DUT_MSG_INFO,
662 "Failed to clear IP addresses");
663 return -1;
664 }
665#endif /* __linux__ */
666
667 return 0;
668}
669
670
671#ifdef ANDROID
672static int add_ipv6_rule(struct sigma_dut *dut, const char *ifname)
673{
674 char cmd[200], *result, *pos;
675 FILE *fp;
676 int len, tableid, result_len = 1000;
677
678 snprintf(cmd, sizeof(cmd), "ip -6 route list table all | grep %s",
679 ifname);
680 fp = popen(cmd, "r");
681 if (fp == NULL)
682 return -1;
683
684 result = malloc(result_len);
685 if (result == NULL)
686 return -1;
687
688 len = fread(result, 1, result_len, fp);
689 fclose(fp);
690
691 if (len == 0) {
692 free(result);
693 return -1;
694 }
695
696 pos = strstr(result, "table ");
697 if (pos == NULL) {
698 free(result);
699 return -1;
700 }
701
702 pos += strlen("table ");
703 tableid = atoi(pos);
704 if (tableid != 0) {
705 if (system("ip -6 rule del prio 22000") != 0) {
706 /* ignore any error */
707 }
708 snprintf(cmd, sizeof(cmd),
709 "ip -6 rule add from all lookup %d prio 22000",
710 tableid);
711 if (system(cmd) != 0) {
712 sigma_dut_print(dut, DUT_MSG_INFO,
713 "Failed to run %s", cmd);
714 free(result);
715 return -1;
716 }
717 } else {
718 sigma_dut_print(dut, DUT_MSG_INFO,
719 "No Valid Table Id found %s", pos);
720 free(result);
721 return -1;
722 }
723 free(result);
724
725 return 0;
726}
727#endif /* ANDROID */
728
729
730static int cmd_sta_set_ip_config(struct sigma_dut *dut,
731 struct sigma_conn *conn,
732 struct sigma_cmd *cmd)
733{
734 const char *intf = get_param(cmd, "Interface");
735 const char *ifname;
736 char buf[200];
737 const char *val, *ip, *mask, *gw;
738 int type = 1;
739
740 if (intf == NULL)
741 return -1;
742
743 if (strcmp(intf, get_main_ifname()) == 0)
744 ifname = get_station_ifname();
745 else
746 ifname = intf;
747
748 if (if_nametoindex(ifname) == 0) {
749 send_resp(dut, conn, SIGMA_ERROR,
750 "ErrorCode,Unknown interface");
751 return 0;
752 }
753
754 val = get_param(cmd, "Type");
755 if (val) {
756 type = atoi(val);
757 if (type != 1 && type != 2) {
758 send_resp(dut, conn, SIGMA_ERROR,
759 "ErrorCode,Unsupported address type");
760 return 0;
761 }
762 }
763
764 dut->last_set_ip_config_ipv6 = 0;
765
766 val = get_param(cmd, "dhcp");
767 if (val && (strcmp(val, "1") == 0 || strcasecmp(val, "true") == 0)) {
768 static_ip_file(0, NULL, NULL, NULL);
769#ifdef __linux__
770 if (type == 2) {
771 dut->last_set_ip_config_ipv6 = 1;
772 sigma_dut_print(dut, DUT_MSG_INFO, "Using IPv6 "
773 "stateless address autoconfiguration");
774#ifdef ANDROID
775 /*
776 * This sleep is required as the assignment in case of
777 * Android is taking time and is done by the kernel.
778 * The subsequent ping for IPv6 is impacting HS20 test
779 * case.
780 */
781 sleep(2);
782 add_ipv6_rule(dut, intf);
783#endif /* ANDROID */
784 /* Assume this happens by default */
785 return 1;
786 }
787
788 kill_dhcp_client(dut, ifname);
789 if (start_dhcp_client(dut, ifname) < 0)
790 return -2;
791 return 1;
792#endif /* __linux__ */
793 return -2;
794 }
795
796 ip = get_param(cmd, "ip");
797 mask = get_param(cmd, "mask");
798
799 if (type == 2) {
800 int net = atoi(mask);
801
802 if ((net < 0 && net > 64) || !is_ipv6_addr(ip))
803 return -1;
804
805 if (dut->no_ip_addr_set) {
806 snprintf(buf, sizeof(buf),
807 "sysctl net.ipv6.conf.%s.disable_ipv6=1",
808 ifname);
809 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
810 if (system(buf) != 0) {
811 sigma_dut_print(dut, DUT_MSG_DEBUG,
812 "Failed to disable IPv6 address before association");
813 }
814 } else {
815 snprintf(buf, sizeof(buf),
816 "ip -6 addr del %s/%s dev %s",
817 ip, mask, ifname);
818 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
819 if (system(buf) != 0) {
820 /*
821 * This command may fail if the address being
822 * deleted does not exist. Inaction here is
823 * intentional.
824 */
825 }
826
827 snprintf(buf, sizeof(buf),
828 "ip -6 addr add %s/%s dev %s",
829 ip, mask, ifname);
830 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
831 if (system(buf) != 0) {
832 send_resp(dut, conn, SIGMA_ERROR,
833 "ErrorCode,Failed to set IPv6 address");
834 return 0;
835 }
836 }
837
838 dut->last_set_ip_config_ipv6 = 1;
839 static_ip_file(6, ip, mask, NULL);
840 return 1;
841 } else if (type == 1) {
842 if (ip == NULL || !is_ip_addr(ip) ||
843 mask == NULL || !is_ip_addr(mask))
844 return -1;
845 }
846
847 kill_dhcp_client(dut, ifname);
848
849 if (!dut->no_ip_addr_set) {
850 snprintf(buf, sizeof(buf), "ifconfig %s %s netmask %s",
851 ifname, ip, mask);
852 if (system(buf) != 0) {
853 send_resp(dut, conn, SIGMA_ERROR,
854 "ErrorCode,Failed to set IP address");
855 return 0;
856 }
857 }
858
859 gw = get_param(cmd, "defaultGateway");
860 if (gw) {
861 if (!is_ip_addr(gw))
862 return -1;
863 snprintf(buf, sizeof(buf), "route add default gw %s", gw);
864 if (!dut->no_ip_addr_set && system(buf) != 0) {
865 snprintf(buf, sizeof(buf), "ip ro re default via %s",
866 gw);
867 if (system(buf) != 0) {
868 send_resp(dut, conn, SIGMA_ERROR,
869 "ErrorCode,Failed "
870 "to set default gateway");
871 return 0;
872 }
873 }
874 }
875
876 val = get_param(cmd, "primary-dns");
877 if (val) {
878 /* TODO */
879 sigma_dut_print(dut, DUT_MSG_INFO, "Ignored primary-dns %s "
880 "setting", val);
881 }
882
883 val = get_param(cmd, "secondary-dns");
884 if (val) {
885 /* TODO */
886 sigma_dut_print(dut, DUT_MSG_INFO, "Ignored secondary-dns %s "
887 "setting", val);
888 }
889
890 static_ip_file(4, ip, mask, gw);
891
892 return 1;
893}
894
895
896static int cmd_sta_get_info(struct sigma_dut *dut, struct sigma_conn *conn,
897 struct sigma_cmd *cmd)
898{
899 /* const char *intf = get_param(cmd, "Interface"); */
900 /* TODO: could report more details here */
901 send_resp(dut, conn, SIGMA_COMPLETE, "vendor,Atheros");
902 return 0;
903}
904
905
906static int cmd_sta_get_mac_address(struct sigma_dut *dut,
907 struct sigma_conn *conn,
908 struct sigma_cmd *cmd)
909{
910 /* const char *intf = get_param(cmd, "Interface"); */
911 char addr[20], resp[50];
912
913 if (get_wpa_status(get_station_ifname(), "address", addr, sizeof(addr))
914 < 0)
915 return -2;
916
917 snprintf(resp, sizeof(resp), "mac,%s", addr);
918 send_resp(dut, conn, SIGMA_COMPLETE, resp);
919 return 0;
920}
921
922
923static int cmd_sta_is_connected(struct sigma_dut *dut, struct sigma_conn *conn,
924 struct sigma_cmd *cmd)
925{
926 /* const char *intf = get_param(cmd, "Interface"); */
927 int connected = 0;
928 char result[32];
929 if (get_wpa_status(get_station_ifname(), "wpa_state", result,
930 sizeof(result)) < 0) {
931 sigma_dut_print(dut, DUT_MSG_INFO, "Could not get interface "
932 "%s status", get_station_ifname());
933 return -2;
934 }
935
936 sigma_dut_print(dut, DUT_MSG_DEBUG, "wpa_state=%s", result);
937 if (strncmp(result, "COMPLETED", 9) == 0)
938 connected = 1;
939
940 if (connected)
941 send_resp(dut, conn, SIGMA_COMPLETE, "connected,1");
942 else
943 send_resp(dut, conn, SIGMA_COMPLETE, "connected,0");
944
945 return 0;
946}
947
948
949static int cmd_sta_verify_ip_connection(struct sigma_dut *dut,
950 struct sigma_conn *conn,
951 struct sigma_cmd *cmd)
952{
953 /* const char *intf = get_param(cmd, "Interface"); */
954 const char *dst, *timeout;
955 int wait_time = 90;
956 char buf[100];
957 int res;
958
959 dst = get_param(cmd, "destination");
960 if (dst == NULL || !is_ip_addr(dst))
961 return -1;
962
963 timeout = get_param(cmd, "timeout");
964 if (timeout) {
965 wait_time = atoi(timeout);
966 if (wait_time < 1)
967 wait_time = 1;
968 }
969
970 /* TODO: force renewal of IP lease if DHCP is enabled */
971
972 snprintf(buf, sizeof(buf), "ping %s -c 3 -W %d", dst, wait_time);
973 res = system(buf);
974 sigma_dut_print(dut, DUT_MSG_DEBUG, "ping returned: %d", res);
975 if (res == 0)
976 send_resp(dut, conn, SIGMA_COMPLETE, "connected,1");
977 else if (res == 256)
978 send_resp(dut, conn, SIGMA_COMPLETE, "connected,0");
979 else
980 return -2;
981
982 return 0;
983}
984
985
986static int cmd_sta_get_bssid(struct sigma_dut *dut, struct sigma_conn *conn,
987 struct sigma_cmd *cmd)
988{
989 /* const char *intf = get_param(cmd, "Interface"); */
990 char bssid[20], resp[50];
991
992 if (get_wpa_status(get_station_ifname(), "bssid", bssid, sizeof(bssid))
993 < 0)
994 strncpy(bssid, "00:00:00:00:00:00", sizeof(bssid));
995
996 snprintf(resp, sizeof(resp), "bssid,%s", bssid);
997 send_resp(dut, conn, SIGMA_COMPLETE, resp);
998 return 0;
999}
1000
1001
1002#ifdef __SAMSUNG__
1003static int add_use_network(const char *ifname)
1004{
1005 char buf[100];
1006
1007 snprintf(buf, sizeof(buf), "USE_NETWORK ON");
1008 wpa_command(ifname, buf);
1009 return 0;
1010}
1011#endif /* __SAMSUNG__ */
1012
1013
1014static int add_network_common(struct sigma_dut *dut, struct sigma_conn *conn,
1015 const char *ifname, struct sigma_cmd *cmd)
1016{
1017 const char *ssid = get_param(cmd, "ssid");
1018 int id;
1019 const char *val;
1020
1021 if (ssid == NULL)
1022 return -1;
1023
1024 start_sta_mode(dut);
1025
1026#ifdef __SAMSUNG__
1027 add_use_network(ifname);
1028#endif /* __SAMSUNG__ */
1029
1030 id = add_network(ifname);
1031 if (id < 0)
1032 return -2;
1033 sigma_dut_print(dut, DUT_MSG_DEBUG, "Adding network %d", id);
1034
1035 if (set_network_quoted(ifname, id, "ssid", ssid) < 0)
1036 return -2;
1037
1038 dut->infra_network_id = id;
1039 snprintf(dut->infra_ssid, sizeof(dut->infra_ssid), "%s", ssid);
1040
1041 val = get_param(cmd, "program");
1042 if (!val)
1043 val = get_param(cmd, "prog");
1044 if (val && strcasecmp(val, "hs2") == 0) {
1045 char buf[100];
1046 snprintf(buf, sizeof(buf), "ENABLE_NETWORK %d no-connect", id);
1047 wpa_command(ifname, buf);
1048
1049 val = get_param(cmd, "prefer");
1050 if (val && atoi(val) > 0)
1051 set_network(ifname, id, "priority", "1");
1052 }
1053
1054 return id;
1055}
1056
1057
1058static int cmd_sta_set_encryption(struct sigma_dut *dut,
1059 struct sigma_conn *conn,
1060 struct sigma_cmd *cmd)
1061{
1062 const char *intf = get_param(cmd, "Interface");
1063 const char *ssid = get_param(cmd, "ssid");
1064 const char *type = get_param(cmd, "encpType");
1065 const char *ifname;
1066 char buf[200];
1067 int id;
1068
1069 if (intf == NULL || ssid == NULL)
1070 return -1;
1071
1072 if (strcmp(intf, get_main_ifname()) == 0)
1073 ifname = get_station_ifname();
1074 else
1075 ifname = intf;
1076
1077 id = add_network_common(dut, conn, ifname, cmd);
1078 if (id < 0)
1079 return id;
1080
1081 if (set_network(ifname, id, "key_mgmt", "NONE") < 0)
1082 return -2;
1083
1084 if (type && strcasecmp(type, "wep") == 0) {
1085 const char *val;
1086 int i;
1087
1088 val = get_param(cmd, "activeKey");
1089 if (val) {
1090 int keyid;
1091 keyid = atoi(val);
1092 if (keyid < 1 || keyid > 4)
1093 return -1;
1094 snprintf(buf, sizeof(buf), "%d", keyid - 1);
1095 if (set_network(ifname, id, "wep_tx_keyidx", buf) < 0)
1096 return -2;
1097 }
1098
1099 for (i = 0; i < 4; i++) {
1100 snprintf(buf, sizeof(buf), "key%d", i + 1);
1101 val = get_param(cmd, buf);
1102 if (val == NULL)
1103 continue;
1104 snprintf(buf, sizeof(buf), "wep_key%d", i);
1105 if (set_network(ifname, id, buf, val) < 0)
1106 return -2;
1107 }
1108 }
1109
1110 return 1;
1111}
1112
1113
1114static int set_wpa_common(struct sigma_dut *dut, struct sigma_conn *conn,
1115 const char *ifname, struct sigma_cmd *cmd)
1116{
1117 const char *val;
1118 int id;
1119
1120 id = add_network_common(dut, conn, ifname, cmd);
1121 if (id < 0)
1122 return id;
1123
1124 val = get_param(cmd, "keyMgmtType");
1125 if (val == NULL) {
1126 send_resp(dut, conn, SIGMA_INVALID, "errorCode,Missing keyMgmtType");
1127 return 0;
1128 }
1129 if (strcasecmp(val, "wpa") == 0 ||
1130 strcasecmp(val, "wpa-psk") == 0) {
1131 if (set_network(ifname, id, "proto", "WPA") < 0)
1132 return -2;
1133 } else if (strcasecmp(val, "wpa2") == 0 ||
1134 strcasecmp(val, "wpa2-psk") == 0 ||
1135 strcasecmp(val, "wpa2-ft") == 0 ||
1136 strcasecmp(val, "wpa2-sha256") == 0) {
1137 if (set_network(ifname, id, "proto", "WPA2") < 0)
1138 return -2;
1139 } else if (strcasecmp(val, "wpa2-wpa-psk") == 0) {
1140 if (set_network(ifname, id, "proto", "WPA WPA2") < 0)
1141 return -2;
1142 } else {
1143 send_resp(dut, conn, SIGMA_INVALID, "errorCode,Unrecognized keyMgmtType value");
1144 return 0;
1145 }
1146
1147 val = get_param(cmd, "encpType");
1148 if (val == NULL) {
1149 send_resp(dut, conn, SIGMA_INVALID, "errorCode,Missing encpType");
1150 return 0;
1151 }
1152 if (strcasecmp(val, "tkip") == 0) {
1153 if (set_network(ifname, id, "pairwise", "TKIP") < 0)
1154 return -2;
1155 } else if (strcasecmp(val, "aes-ccmp") == 0) {
1156 if (set_network(ifname, id, "pairwise", "CCMP") < 0)
1157 return -2;
1158 } else if (strcasecmp(val, "aes-ccmp-tkip") == 0) {
1159 if (set_network(ifname, id, "pairwise", "CCMP TKIP") < 0)
1160 return -2;
1161 } else if (strcasecmp(val, "aes-gcmp") == 0) {
1162 if (set_network(ifname, id, "pairwise", "GCMP") < 0)
1163 return -2;
1164 if (set_network(ifname, id, "group", "GCMP") < 0)
1165 return -2;
1166 } else {
1167 send_resp(dut, conn, SIGMA_INVALID, "errorCode,Unrecognized encpType value");
1168 return 0;
1169 }
1170
1171 dut->sta_pmf = STA_PMF_DISABLED;
1172 val = get_param(cmd, "PMF");
1173 if (val) {
1174 if (strcasecmp(val, "Required") == 0 ||
1175 strcasecmp(val, "Forced_Required") == 0) {
1176 dut->sta_pmf = STA_PMF_REQUIRED;
1177 if (set_network(ifname, id, "ieee80211w", "2") < 0)
1178 return -2;
1179 } else if (strcasecmp(val, "Optional") == 0) {
1180 dut->sta_pmf = STA_PMF_OPTIONAL;
1181 if (set_network(ifname, id, "ieee80211w", "1") < 0)
1182 return -2;
1183 } else if (strcasecmp(val, "Disabled") == 0 ||
1184 strcasecmp(val, "Forced_Disabled") == 0) {
1185 dut->sta_pmf = STA_PMF_DISABLED;
1186 } else {
1187 send_resp(dut, conn, SIGMA_INVALID, "errorCode,Unrecognized PMF value");
1188 return 0;
1189 }
1190 }
1191
1192 return id;
1193}
1194
1195
1196static int cmd_sta_set_psk(struct sigma_dut *dut, struct sigma_conn *conn,
1197 struct sigma_cmd *cmd)
1198{
1199 const char *intf = get_param(cmd, "Interface");
1200 const char *ifname, *val, *alg;
1201 int id;
1202
1203 if (intf == NULL)
1204 return -1;
1205
1206 if (strcmp(intf, get_main_ifname()) == 0)
1207 ifname = get_station_ifname();
1208 else
1209 ifname = intf;
1210
1211 id = set_wpa_common(dut, conn, ifname, cmd);
1212 if (id < 0)
1213 return id;
1214
1215 val = get_param(cmd, "keyMgmtType");
1216 alg = get_param(cmd, "micAlg");
1217
1218 if (alg && strcasecmp(alg, "SHA-256") == 0) {
1219 if (set_network(ifname, id, "key_mgmt", "WPA-PSK-SHA256") < 0)
1220 return -2;
1221 } else if (alg && strcasecmp(alg, "SHA-1") == 0) {
1222 if (set_network(ifname, id, "key_mgmt", "WPA-PSK") < 0)
1223 return -2;
1224 } else if ((val && strcasecmp(val, "wpa2-sha256") == 0) ||
1225 dut->sta_pmf == STA_PMF_REQUIRED) {
1226 if (set_network(ifname, id, "key_mgmt",
1227 "WPA-PSK WPA-PSK-SHA256") < 0)
1228 return -2;
1229 } else if (dut->sta_pmf == STA_PMF_OPTIONAL) {
1230 if (set_network(ifname, id, "key_mgmt",
1231 "WPA-PSK WPA-PSK-SHA256") < 0)
1232 return -2;
1233 } else {
1234 if (set_network(ifname, id, "key_mgmt", "WPA-PSK") < 0)
1235 return -2;
1236 }
1237
1238 val = get_param(cmd, "passPhrase");
1239 if (val == NULL)
1240 return -1;
1241 if (set_network_quoted(ifname, id, "psk", val) < 0)
1242 return -2;
1243
1244 return 1;
1245}
1246
1247
1248static int set_eap_common(struct sigma_dut *dut, struct sigma_conn *conn,
1249 const char *ifname, struct sigma_cmd *cmd)
1250{
1251 const char *val, *alg;
1252 int id;
1253 char buf[200];
1254#ifdef ANDROID
1255 unsigned char kvalue[KEYSTORE_MESSAGE_SIZE];
1256 int length;
1257#endif /* ANDROID */
1258
1259 id = set_wpa_common(dut, conn, ifname, cmd);
1260 if (id < 0)
1261 return id;
1262
1263 val = get_param(cmd, "keyMgmtType");
1264 alg = get_param(cmd, "micAlg");
1265
1266 if (alg && strcasecmp(alg, "SHA-256") == 0) {
1267 if (set_network(ifname, id, "key_mgmt", "WPA-EAP-SHA256") < 0)
1268 return -2;
1269 } else if (alg && strcasecmp(alg, "SHA-1") == 0) {
1270 if (set_network(ifname, id, "key_mgmt", "WPA-EAP") < 0)
1271 return -2;
1272 } else if (val && strcasecmp(val, "wpa2-ft") == 0) {
1273 if (set_network(ifname, id, "key_mgmt", "FT-EAP") < 0)
1274 return -2;
1275 } else if ((val && strcasecmp(val, "wpa2-sha256") == 0) ||
1276 dut->sta_pmf == STA_PMF_REQUIRED) {
1277 if (set_network(ifname, id, "key_mgmt",
1278 "WPA-EAP WPA-EAP-SHA256") < 0)
1279 return -2;
1280 } else if (dut->sta_pmf == STA_PMF_OPTIONAL) {
1281 if (set_network(ifname, id, "key_mgmt",
1282 "WPA-EAP WPA-EAP-SHA256") < 0)
1283 return -2;
1284 } else {
1285 if (set_network(ifname, id, "key_mgmt", "WPA-EAP") < 0)
1286 return -2;
1287 }
1288
1289 val = get_param(cmd, "trustedRootCA");
1290 if (val) {
1291#ifdef ANDROID
1292 snprintf(buf, sizeof(buf), "CACERT_%s", val);
1293 length = android_keystore_get(ANDROID_KEYSTORE_GET, buf,
1294 kvalue);
1295 if (length > 0) {
1296 sigma_dut_print(dut, DUT_MSG_INFO,
1297 "Use Android keystore [%s]", buf);
1298 snprintf(buf, sizeof(buf), "keystore://CACERT_%s",
1299 val);
1300 goto ca_cert_selected;
1301 }
1302#endif /* ANDROID */
1303
1304 snprintf(buf, sizeof(buf), "%s/%s", sigma_cert_path, val);
1305#ifdef __linux__
1306 if (!file_exists(buf)) {
1307 char msg[300];
1308 snprintf(msg, sizeof(msg), "ErrorCode,trustedRootCA "
1309 "file (%s) not found", buf);
1310 send_resp(dut, conn, SIGMA_ERROR, msg);
1311 return -3;
1312 }
1313#endif /* __linux__ */
1314#ifdef ANDROID
1315ca_cert_selected:
1316#endif /* ANDROID */
1317 if (set_network_quoted(ifname, id, "ca_cert", buf) < 0)
1318 return -2;
1319 }
1320
1321 val = get_param(cmd, "username");
1322 if (val) {
1323 if (set_network_quoted(ifname, id, "identity", val) < 0)
1324 return -2;
1325 }
1326
1327 val = get_param(cmd, "password");
1328 if (val) {
1329 if (set_network_quoted(ifname, id, "password", val) < 0)
1330 return -2;
1331 }
1332
1333 return id;
1334}
1335
1336
1337static int cmd_sta_set_eaptls(struct sigma_dut *dut, struct sigma_conn *conn,
1338 struct sigma_cmd *cmd)
1339{
1340 const char *intf = get_param(cmd, "Interface");
1341 const char *ifname, *val;
1342 int id;
1343 char buf[200];
1344#ifdef ANDROID
1345 unsigned char kvalue[KEYSTORE_MESSAGE_SIZE];
1346 int length;
1347 int jb_or_newer = 0;
1348 char prop[PROPERTY_VALUE_MAX];
1349#endif /* ANDROID */
1350
1351 if (intf == NULL)
1352 return -1;
1353
1354 if (strcmp(intf, get_main_ifname()) == 0)
1355 ifname = get_station_ifname();
1356 else
1357 ifname = intf;
1358
1359 id = set_eap_common(dut, conn, ifname, cmd);
1360 if (id < 0)
1361 return id;
1362
1363 if (set_network(ifname, id, "eap", "TLS") < 0)
1364 return -2;
1365
1366 if (set_network_quoted(ifname, id, "identity",
1367 "wifi-user@wifilabs.local") < 0)
1368 return -2;
1369
1370 val = get_param(cmd, "clientCertificate");
1371 if (val == NULL)
1372 return -1;
1373#ifdef ANDROID
1374 snprintf(buf, sizeof(buf), "USRPKEY_%s", val);
1375 length = android_keystore_get(ANDROID_KEYSTORE_GET, buf, kvalue);
1376 if (length < 0) {
1377 /*
1378 * JB started reporting keystore type mismatches, so retry with
1379 * the GET_PUBKEY command if the generic GET fails.
1380 */
1381 length = android_keystore_get(ANDROID_KEYSTORE_GET_PUBKEY,
1382 buf, kvalue);
1383 }
1384
1385 if (property_get("ro.build.version.release", prop, NULL) != 0) {
1386 sigma_dut_print(dut, DUT_MSG_DEBUG, "Android release %s", prop);
1387 if (strncmp(prop, "4.0", 3) != 0)
1388 jb_or_newer = 1;
1389 } else
1390 jb_or_newer = 1; /* assume newer */
1391
1392 if (jb_or_newer && length > 0) {
1393 sigma_dut_print(dut, DUT_MSG_INFO,
1394 "Use Android keystore [%s]", buf);
1395 if (set_network(ifname, id, "engine", "1") < 0)
1396 return -2;
1397 if (set_network_quoted(ifname, id, "engine_id", "keystore") < 0)
1398 return -2;
1399 snprintf(buf, sizeof(buf), "USRPKEY_%s", val);
1400 if (set_network_quoted(ifname, id, "key_id", buf) < 0)
1401 return -2;
1402 snprintf(buf, sizeof(buf), "keystore://USRCERT_%s", val);
1403 if (set_network_quoted(ifname, id, "client_cert", buf) < 0)
1404 return -2;
1405 return 1;
1406 } else if (length > 0) {
1407 sigma_dut_print(dut, DUT_MSG_INFO,
1408 "Use Android keystore [%s]", buf);
1409 snprintf(buf, sizeof(buf), "keystore://USRPKEY_%s", val);
1410 if (set_network_quoted(ifname, id, "private_key", buf) < 0)
1411 return -2;
1412 snprintf(buf, sizeof(buf), "keystore://USRCERT_%s", val);
1413 if (set_network_quoted(ifname, id, "client_cert", buf) < 0)
1414 return -2;
1415 return 1;
1416 }
1417#endif /* ANDROID */
1418
1419 snprintf(buf, sizeof(buf), "%s/%s", sigma_cert_path, val);
1420#ifdef __linux__
1421 if (!file_exists(buf)) {
1422 char msg[300];
1423 snprintf(msg, sizeof(msg), "ErrorCode,clientCertificate file "
1424 "(%s) not found", buf);
1425 send_resp(dut, conn, SIGMA_ERROR, msg);
1426 return -3;
1427 }
1428#endif /* __linux__ */
1429 if (set_network_quoted(ifname, id, "private_key", buf) < 0)
1430 return -2;
1431 if (set_network_quoted(ifname, id, "client_cert", buf) < 0)
1432 return -2;
1433
1434 if (set_network_quoted(ifname, id, "private_key_passwd", "wifi") < 0)
1435 return -2;
1436
1437 return 1;
1438}
1439
1440
1441static int cmd_sta_set_eapttls(struct sigma_dut *dut, struct sigma_conn *conn,
1442 struct sigma_cmd *cmd)
1443{
1444 const char *intf = get_param(cmd, "Interface");
1445 const char *ifname;
1446 int id;
1447
1448 if (intf == NULL)
1449 return -1;
1450
1451 if (strcmp(intf, get_main_ifname()) == 0)
1452 ifname = get_station_ifname();
1453 else
1454 ifname = intf;
1455
1456 id = set_eap_common(dut, conn, ifname, cmd);
1457 if (id < 0)
1458 return id;
1459
1460 if (set_network(ifname, id, "eap", "TTLS") < 0) {
1461 send_resp(dut, conn, SIGMA_ERROR,
1462 "errorCode,Failed to set TTLS method");
1463 return 0;
1464 }
1465
1466 if (set_network_quoted(ifname, id, "phase2", "auth=MSCHAPV2") < 0) {
1467 send_resp(dut, conn, SIGMA_ERROR,
1468 "errorCode,Failed to set MSCHAPv2 for TTLS Phase 2");
1469 return 0;
1470 }
1471
1472 return 1;
1473}
1474
1475
1476static int cmd_sta_set_eapsim(struct sigma_dut *dut, struct sigma_conn *conn,
1477 struct sigma_cmd *cmd)
1478{
1479 const char *intf = get_param(cmd, "Interface");
1480 const char *ifname;
1481 int id;
1482
1483 if (intf == NULL)
1484 return -1;
1485
1486 if (strcmp(intf, get_main_ifname()) == 0)
1487 ifname = get_station_ifname();
1488 else
1489 ifname = intf;
1490
1491 id = set_eap_common(dut, conn, ifname, cmd);
1492 if (id < 0)
1493 return id;
1494
1495 if (set_network(ifname, id, "eap", "SIM") < 0)
1496 return -2;
1497
1498 return 1;
1499}
1500
1501
1502static int cmd_sta_set_peap(struct sigma_dut *dut, struct sigma_conn *conn,
1503 struct sigma_cmd *cmd)
1504{
1505 const char *intf = get_param(cmd, "Interface");
1506 const char *ifname, *val;
1507 int id;
1508 char buf[100];
1509
1510 if (intf == NULL)
1511 return -1;
1512
1513 if (strcmp(intf, get_main_ifname()) == 0)
1514 ifname = get_station_ifname();
1515 else
1516 ifname = intf;
1517
1518 id = set_eap_common(dut, conn, ifname, cmd);
1519 if (id < 0)
1520 return id;
1521
1522 if (set_network(ifname, id, "eap", "PEAP") < 0)
1523 return -2;
1524
1525 val = get_param(cmd, "innerEAP");
1526 if (val) {
1527 if (strcasecmp(val, "MSCHAPv2") == 0) {
1528 if (set_network_quoted(ifname, id, "phase2",
1529 "auth=MSCHAPV2") < 0)
1530 return -2;
1531 } else if (strcasecmp(val, "GTC") == 0) {
1532 if (set_network_quoted(ifname, id, "phase2",
1533 "auth=GTC") < 0)
1534 return -2;
1535 } else
1536 return -1;
1537 }
1538
1539 val = get_param(cmd, "peapVersion");
1540 if (val) {
1541 int ver = atoi(val);
1542 if (ver < 0 || ver > 1)
1543 return -1;
1544 snprintf(buf, sizeof(buf), "peapver=%d", ver);
1545 if (set_network_quoted(ifname, id, "phase1", buf) < 0)
1546 return -2;
1547 }
1548
1549 return 1;
1550}
1551
1552
1553static int cmd_sta_set_eapfast(struct sigma_dut *dut, struct sigma_conn *conn,
1554 struct sigma_cmd *cmd)
1555{
1556 const char *intf = get_param(cmd, "Interface");
1557 const char *ifname, *val;
1558 int id;
1559 char buf[100];
1560
1561 if (intf == NULL)
1562 return -1;
1563
1564 if (strcmp(intf, get_main_ifname()) == 0)
1565 ifname = get_station_ifname();
1566 else
1567 ifname = intf;
1568
1569 id = set_eap_common(dut, conn, ifname, cmd);
1570 if (id < 0)
1571 return id;
1572
1573 if (set_network(ifname, id, "eap", "FAST") < 0)
1574 return -2;
1575
1576 val = get_param(cmd, "innerEAP");
1577 if (val) {
1578 if (strcasecmp(val, "MSCHAPV2") == 0) {
1579 if (set_network_quoted(ifname, id, "phase2",
1580 "auth=MSCHAPV2") < 0)
1581 return -2;
1582 } else if (strcasecmp(val, "GTC") == 0) {
1583 if (set_network_quoted(ifname, id, "phase2",
1584 "auth=GTC") < 0)
1585 return -2;
1586 } else
1587 return -1;
1588 }
1589
1590 val = get_param(cmd, "validateServer");
1591 if (val) {
1592 /* TODO */
1593 sigma_dut_print(dut, DUT_MSG_INFO, "Ignored EAP-FAST "
1594 "validateServer=%s", val);
1595 }
1596
1597 val = get_param(cmd, "pacFile");
1598 if (val) {
1599 snprintf(buf, sizeof(buf), "blob://%s", val);
1600 if (set_network_quoted(ifname, id, "pac_file", buf) < 0)
1601 return -2;
1602 }
1603
1604 if (set_network_quoted(ifname, id, "phase1", "fast_provisioning=2") <
1605 0)
1606 return -2;
1607
1608 return 1;
1609}
1610
1611
1612static int cmd_sta_set_eapaka(struct sigma_dut *dut, struct sigma_conn *conn,
1613 struct sigma_cmd *cmd)
1614{
1615 const char *intf = get_param(cmd, "Interface");
1616 const char *ifname;
1617 int id;
1618
1619 if (intf == NULL)
1620 return -1;
1621
1622 if (strcmp(intf, get_main_ifname()) == 0)
1623 ifname = get_station_ifname();
1624 else
1625 ifname = intf;
1626
1627 id = set_eap_common(dut, conn, ifname, cmd);
1628 if (id < 0)
1629 return id;
1630
1631 if (set_network(ifname, id, "eap", "AKA") < 0)
1632 return -2;
1633
1634 return 1;
1635}
1636
1637
1638static int cmd_sta_set_eapakaprime(struct sigma_dut *dut,
1639 struct sigma_conn *conn,
1640 struct sigma_cmd *cmd)
1641{
1642 const char *intf = get_param(cmd, "Interface");
1643 const char *ifname;
1644 int id;
1645
1646 if (intf == NULL)
1647 return -1;
1648
1649 if (strcmp(intf, get_main_ifname()) == 0)
1650 ifname = get_station_ifname();
1651 else
1652 ifname = intf;
1653
1654 id = set_eap_common(dut, conn, ifname, cmd);
1655 if (id < 0)
1656 return id;
1657
1658 if (set_network(ifname, id, "eap", "AKA'") < 0)
1659 return -2;
1660
1661 return 1;
1662}
1663
1664
1665static int sta_set_open(struct sigma_dut *dut, struct sigma_conn *conn,
1666 struct sigma_cmd *cmd)
1667{
1668 const char *intf = get_param(cmd, "Interface");
1669 const char *ifname;
1670 int id;
1671
1672 if (strcmp(intf, get_main_ifname()) == 0)
1673 ifname = get_station_ifname();
1674 else
1675 ifname = intf;
1676
1677 id = add_network_common(dut, conn, ifname, cmd);
1678 if (id < 0)
1679 return id;
1680
1681 if (set_network(ifname, id, "key_mgmt", "NONE") < 0)
1682 return -2;
1683
1684 return 1;
1685}
1686
1687
1688static int cmd_sta_set_security(struct sigma_dut *dut, struct sigma_conn *conn,
1689 struct sigma_cmd *cmd)
1690{
1691 const char *type = get_param(cmd, "Type");
1692
1693 if (type == NULL) {
1694 send_resp(dut, conn, SIGMA_ERROR,
1695 "ErrorCode,Missing Type argument");
1696 return 0;
1697 }
1698
1699 if (strcasecmp(type, "OPEN") == 0)
1700 return sta_set_open(dut, conn, cmd);
1701 if (strcasecmp(type, "PSK") == 0)
1702 return cmd_sta_set_psk(dut, conn, cmd);
1703 if (strcasecmp(type, "EAPTLS") == 0)
1704 return cmd_sta_set_eaptls(dut, conn, cmd);
1705 if (strcasecmp(type, "EAPTTLS") == 0)
1706 return cmd_sta_set_eapttls(dut, conn, cmd);
1707 if (strcasecmp(type, "EAPPEAP") == 0)
1708 return cmd_sta_set_peap(dut, conn, cmd);
1709 if (strcasecmp(type, "EAPSIM") == 0)
1710 return cmd_sta_set_eapsim(dut, conn, cmd);
1711 if (strcasecmp(type, "EAPFAST") == 0)
1712 return cmd_sta_set_eapfast(dut, conn, cmd);
1713 if (strcasecmp(type, "EAPAKA") == 0)
1714 return cmd_sta_set_eapaka(dut, conn, cmd);
1715 if (strcasecmp(type, "EAPAKAPRIME") == 0)
1716 return cmd_sta_set_eapakaprime(dut, conn, cmd);
1717
1718 send_resp(dut, conn, SIGMA_ERROR,
1719 "ErrorCode,Unsupported Type value");
1720 return 0;
1721}
1722
1723
1724int ath6kl_client_uapsd(struct sigma_dut *dut, const char *intf, int uapsd)
1725{
1726#ifdef __linux__
1727 /* special handling for ath6kl */
1728 char path[128], fname[128], *pos;
1729 ssize_t res;
1730 FILE *f;
1731
1732 snprintf(path, sizeof(path), "/sys/class/net/%s/phy80211", intf);
1733 res = readlink(path, path, sizeof(path));
1734 if (res < 0)
1735 return 0; /* not ath6kl */
1736
1737 if (res >= (int) sizeof(path))
1738 res = sizeof(path) - 1;
1739 path[res] = '\0';
1740 pos = strrchr(path, '/');
1741 if (pos == NULL)
1742 pos = path;
1743 else
1744 pos++;
1745 snprintf(fname, sizeof(fname),
1746 "/sys/kernel/debug/ieee80211/%s/ath6kl/"
1747 "create_qos", pos);
1748 if (!file_exists(fname))
1749 return 0; /* not ath6kl */
1750
1751 if (uapsd) {
1752 f = fopen(fname, "w");
1753 if (f == NULL)
1754 return -1;
1755
1756 sigma_dut_print(dut, DUT_MSG_DEBUG, "Use ath6kl create_qos");
1757 fprintf(f, "4 2 2 1 2 9999999 9999999 9999999 7777777 0 4 "
1758 "45000 200 56789000 56789000 5678900 0 0 9999999 "
1759 "20000 0\n");
1760 fclose(f);
1761 } else {
1762 snprintf(fname, sizeof(fname),
1763 "/sys/kernel/debug/ieee80211/%s/ath6kl/"
1764 "delete_qos", pos);
1765
1766 f = fopen(fname, "w");
1767 if (f == NULL)
1768 return -1;
1769
1770 sigma_dut_print(dut, DUT_MSG_DEBUG, "Use ath6kl delete_qos");
1771 fprintf(f, "2 4\n");
1772 fclose(f);
1773 }
1774#endif /* __linux__ */
1775
1776 return 0;
1777}
1778
1779
1780static int cmd_sta_set_uapsd(struct sigma_dut *dut, struct sigma_conn *conn,
1781 struct sigma_cmd *cmd)
1782{
1783 const char *intf = get_param(cmd, "Interface");
1784 /* const char *ssid = get_param(cmd, "ssid"); */
1785 const char *val;
1786 int max_sp_len = 4;
1787 int ac_be = 1, ac_bk = 1, ac_vi = 1, ac_vo = 1;
1788 char buf[100];
1789 int ret1, ret2;
1790
1791 val = get_param(cmd, "maxSPLength");
1792 if (val) {
1793 max_sp_len = atoi(val);
1794 if (max_sp_len != 0 && max_sp_len != 1 && max_sp_len != 2 &&
1795 max_sp_len != 4)
1796 return -1;
1797 }
1798
1799 val = get_param(cmd, "acBE");
1800 if (val)
1801 ac_be = atoi(val);
1802
1803 val = get_param(cmd, "acBK");
1804 if (val)
1805 ac_bk = atoi(val);
1806
1807 val = get_param(cmd, "acVI");
1808 if (val)
1809 ac_vi = atoi(val);
1810
1811 val = get_param(cmd, "acVO");
1812 if (val)
1813 ac_vo = atoi(val);
1814
1815 dut->client_uapsd = ac_be || ac_bk || ac_vi || ac_vo;
1816
1817 snprintf(buf, sizeof(buf), "P2P_SET client_apsd %d,%d,%d,%d;%d",
1818 ac_be, ac_bk, ac_vi, ac_vo, max_sp_len);
1819 ret1 = wpa_command(intf, buf);
1820
1821 snprintf(buf, sizeof(buf), "SET uapsd %d,%d,%d,%d;%d",
1822 ac_be, ac_bk, ac_vi, ac_vo, max_sp_len);
1823 ret2 = wpa_command(intf, buf);
1824
1825 if (ret1 && ret2) {
1826 sigma_dut_print(dut, DUT_MSG_INFO, "Failed to set client mode "
1827 "UAPSD parameters.");
1828 return -2;
1829 }
1830
1831 if (ath6kl_client_uapsd(dut, intf, dut->client_uapsd) < 0) {
1832 send_resp(dut, conn, SIGMA_ERROR,
1833 "ErrorCode,Failed to set ath6kl QoS parameters");
1834 return 0;
1835 }
1836
1837 return 1;
1838}
1839
1840
1841static int cmd_sta_set_wmm(struct sigma_dut *dut, struct sigma_conn *conn,
1842 struct sigma_cmd *cmd)
1843{
1844 char buf[1000];
1845 const char *intf = get_param(cmd, "Interface");
1846 const char *grp = get_param(cmd, "Group");
1847 const char *act = get_param(cmd, "Action");
1848 const char *tid = get_param(cmd, "Tid");
1849 const char *dir = get_param(cmd, "Direction");
1850 const char *psb = get_param(cmd, "Psb");
1851 const char *up = get_param(cmd, "Up");
1852 const char *fixed = get_param(cmd, "Fixed");
1853 const char *size = get_param(cmd, "Size");
1854 const char *msize = get_param(cmd, "Maxsize");
1855 const char *minsi = get_param(cmd, "Min_srvc_intrvl");
1856 const char *maxsi = get_param(cmd, "Max_srvc_intrvl");
1857 const char *inact = get_param(cmd, "Inactivity");
1858 const char *sus = get_param(cmd, "Suspension");
1859 const char *mindr = get_param(cmd, "Mindatarate");
1860 const char *meandr = get_param(cmd, "Meandatarate");
1861 const char *peakdr = get_param(cmd, "Peakdatarate");
1862 const char *phyrate = get_param(cmd, "Phyrate");
1863 const char *burstsize = get_param(cmd, "Burstsize");
1864 const char *sba = get_param(cmd, "Sba");
1865 int direction;
1866 int handle;
1867 float sba_fv;
1868 int fixed_int;
1869 int psb_ts;
1870
1871 if (intf == NULL || grp == NULL || act == NULL )
1872 return -1;
1873
1874 if (strcasecmp(act, "addts") == 0) {
1875 if (tid == NULL || dir == NULL || psb == NULL ||
1876 up == NULL || fixed == NULL || size == NULL)
1877 return -1;
1878
1879 /*
1880 * Note: Sigma CAPI spec lists uplink, downlink, and bidi as the
1881 * possible values, but WMM-AC and V-E test scripts use "UP,
1882 * "DOWN", and "BIDI".
1883 */
1884 if (strcasecmp(dir, "uplink") == 0 ||
1885 strcasecmp(dir, "up") == 0) {
1886 direction = 0;
1887 } else if (strcasecmp(dir, "downlink") == 0 ||
1888 strcasecmp(dir, "down") == 0) {
1889 direction = 1;
1890 } else if (strcasecmp(dir, "bidi") == 0) {
1891 direction = 2;
1892 } else {
1893 sigma_dut_print(dut, DUT_MSG_ERROR,
1894 "Direction %s not supported", dir);
1895 return -1;
1896 }
1897
1898 if (strcasecmp(psb, "legacy") == 0) {
1899 psb_ts = 0;
1900 } else if (strcasecmp(psb, "uapsd") == 0) {
1901 psb_ts = 1;
1902 } else {
1903 sigma_dut_print(dut, DUT_MSG_ERROR,
1904 "PSB %s not supported", psb);
1905 return -1;
1906 }
1907
1908 if (atoi(tid) < 0 || atoi(tid) > 7) {
1909 sigma_dut_print(dut, DUT_MSG_ERROR,
1910 "TID %s not supported", tid);
1911 return -1;
1912 }
1913
1914 if (strcasecmp(fixed, "true") == 0) {
1915 fixed_int = 1;
1916 } else {
1917 fixed_int = 0;
1918 }
1919
1920 sba_fv = atof(sba);
1921
1922 dut->dialog_token++;
1923 handle = 7000 + dut->dialog_token;
1924
1925 /*
1926 * size: convert to hex
1927 * maxsi: convert to hex
1928 * mindr: convert to hex
1929 * meandr: convert to hex
1930 * peakdr: convert to hex
1931 * burstsize: convert to hex
1932 * phyrate: convert to hex
1933 * sba: convert to hex with modification
1934 * minsi: convert to integer
1935 * sus: convert to integer
1936 * inact: convert to integer
1937 * maxsi: convert to integer
1938 */
1939
1940 /*
1941 * The Nominal MSDU Size field is 2 octets long and contains an
1942 * unsigned integer that specifies the nominal size, in octets,
1943 * of MSDUs belonging to the traffic under this traffic
1944 * specification and is defined in Figure 16. If the Fixed
1945 * subfield is set to 1, then the size of the MSDU is fixed and
1946 * is indicated by the Size Subfield. If the Fixed subfield is
1947 * set to 0, then the size of the MSDU might not be fixed and
1948 * the Size indicates the nominal MSDU size.
1949 *
1950 * The Surplus Bandwidth Allowance Factor field is 2 octets long
1951 * and specifies the excess allocation of time (and bandwidth)
1952 * over and above the stated rates required to transport an MSDU
1953 * belonging to the traffic in this TSPEC. This field is
1954 * represented as an unsigned binary number with an implicit
1955 * binary point after the leftmost 3 bits. For example, an SBA
1956 * of 1.75 is represented as 0x3800. This field is included to
1957 * account for retransmissions. As such, the value of this field
1958 * must be greater than unity.
1959 */
1960
1961 snprintf(buf, sizeof(buf),
1962 "iwpriv %s addTspec %d %s %d %d %s 0x%X"
1963 " 0x%X 0x%X 0x%X"
1964 " 0x%X 0x%X 0x%X"
1965 " 0x%X %d %d %d %d"
1966 " %d %d",
1967 intf, handle, tid, direction, psb_ts, up,
1968 (unsigned int) ((fixed_int << 15) | atoi(size)),
1969 msize ? atoi(msize) : 0,
1970 mindr ? atoi(mindr) : 0,
1971 meandr ? atoi(meandr) : 0,
1972 peakdr ? atoi(peakdr) : 0,
1973 burstsize ? atoi(burstsize) : 0,
1974 phyrate ? atoi(phyrate) : 0,
1975 sba ? ((unsigned int) (((int) sba_fv << 13) |
1976 (int)((sba_fv - (int) sba_fv) *
1977 8192))) : 0,
1978 minsi ? atoi(minsi) : 0,
1979 sus ? atoi(sus) : 0,
1980 0, 0,
1981 inact ? atoi(inact) : 0,
1982 maxsi ? atoi(maxsi) : 0);
1983
1984 if (system(buf) != 0) {
1985 sigma_dut_print(dut, DUT_MSG_ERROR,
1986 "iwpriv addtspec request failed");
1987 send_resp(dut, conn, SIGMA_ERROR,
1988 "errorCode,Failed to execute addTspec command");
1989 return 0;
1990 }
1991
1992 sigma_dut_print(dut, DUT_MSG_INFO,
1993 "iwpriv addtspec request send");
1994
1995 /* Mapping handle to a TID */
1996 dut->tid_to_handle[atoi(tid)] = handle;
1997 } else if (strcasecmp(act, "delts") == 0) {
1998 if (tid == NULL)
1999 return -1;
2000
2001 if (atoi(tid) < 0 || atoi(tid) > 7) {
2002 sigma_dut_print(dut, DUT_MSG_ERROR,
2003 "TID %s not supported", tid);
2004 send_resp(dut, conn, SIGMA_ERROR,
2005 "errorCode,Unsupported TID");
2006 return 0;
2007 }
2008
2009 handle = dut->tid_to_handle[atoi(tid)];
2010
2011 if (handle < 7000 || handle > 7255) {
2012 /* Invalid handle ie no mapping for that TID */
2013 sigma_dut_print(dut, DUT_MSG_ERROR,
2014 "handle-> %d not found", handle);
2015 }
2016
2017 snprintf(buf, sizeof(buf), "iwpriv %s delTspec %d",
2018 intf, handle);
2019
2020 if (system(buf) != 0) {
2021 sigma_dut_print(dut, DUT_MSG_ERROR,
2022 "iwpriv deltspec request failed");
2023 send_resp(dut, conn, SIGMA_ERROR,
2024 "errorCode,Failed to execute delTspec command");
2025 return 0;
2026 }
2027
2028 sigma_dut_print(dut, DUT_MSG_INFO,
2029 "iwpriv deltspec request send");
2030
2031 dut->tid_to_handle[atoi(tid)] = 0;
2032 } else {
2033 sigma_dut_print(dut, DUT_MSG_ERROR,
2034 "Action type %s not supported", act);
2035 send_resp(dut, conn, SIGMA_ERROR,
2036 "errorCode,Unsupported Action");
2037 return 0;
2038 }
2039
2040 return 1;
2041}
2042
2043
2044static int cmd_sta_associate(struct sigma_dut *dut, struct sigma_conn *conn,
2045 struct sigma_cmd *cmd)
2046{
2047 /* const char *intf = get_param(cmd, "Interface"); */
2048 const char *ssid = get_param(cmd, "ssid");
2049 const char *wps_param = get_param(cmd, "WPS");
2050 const char *bssid = get_param(cmd, "bssid");
2051 int wps = 0;
2052 char buf[100];
2053
2054 if (ssid == NULL)
2055 return -1;
2056
2057 if (wps_param &&
2058 (strcmp(wps_param, "1") == 0 || strcasecmp(wps_param, "On") == 0))
2059 wps = 1;
2060
2061 if (wps) {
2062 if (dut->wps_method == WFA_CS_WPS_NOT_READY) {
2063 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,WPS "
2064 "parameters not yet set");
2065 return 0;
2066 }
2067 if (dut->wps_method == WFA_CS_WPS_PBC) {
2068 if (wpa_command(get_station_ifname(), "WPS_PBC") < 0)
2069 return -2;
2070 } else {
2071 snprintf(buf, sizeof(buf), "WPS_PIN any %s",
2072 dut->wps_pin);
2073 if (wpa_command(get_station_ifname(), buf) < 0)
2074 return -2;
2075 }
2076 } else {
2077 if (strcmp(ssid, dut->infra_ssid) != 0) {
2078 printf("No network parameters known for network "
2079 "(ssid='%s')", ssid);
2080 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,"
2081 "No network parameters known for network");
2082 return 0;
2083 }
2084
2085 if (bssid &&
2086 set_network(get_station_ifname(), dut->infra_network_id,
2087 "bssid", bssid) < 0) {
2088 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,"
2089 "Invalid bssid argument");
2090 return 0;
2091 }
2092
2093 snprintf(buf, sizeof(buf), "SELECT_NETWORK %d",
2094 dut->infra_network_id);
2095 if (wpa_command(get_station_ifname(), buf) < 0) {
2096 sigma_dut_print(dut, DUT_MSG_INFO, "Failed to select "
2097 "network id %d on %s",
2098 dut->infra_network_id,
2099 get_station_ifname());
2100 return -2;
2101 }
2102 }
2103
2104 return 1;
2105}
2106
2107
2108static int run_hs20_osu(struct sigma_dut *dut, const char *params)
2109{
2110 char buf[500], cmd[200];
2111 int res;
2112
2113 /* Use hs20-osu-client file at the current dir, if found; otherwise use
2114 * default path */
2115 res = snprintf(cmd, sizeof(cmd),
2116 "%s -w \"%s\" -r hs20-osu-client.res %s%s -dddKt -f Logs/hs20-osu-client.txt",
2117 file_exists("./hs20-osu-client") ?
2118 "./hs20-osu-client" : "hs20-osu-client",
2119 sigma_wpas_ctrl,
2120 dut->summary_log ? "-s " : "",
2121 dut->summary_log ? dut->summary_log : "");
2122 if (res < 0 || res >= (int) sizeof(cmd))
2123 return -1;
2124
2125 res = snprintf(buf, sizeof(buf), "%s %s", cmd, params);
2126 if (res < 0 || res >= (int) sizeof(buf))
2127 return -1;
2128 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
2129
2130 if (system(buf) != 0) {
2131 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to run: %s", buf);
2132 return -1;
2133 }
2134 sigma_dut_print(dut, DUT_MSG_DEBUG,
2135 "Completed hs20-osu-client operation");
2136
2137 return 0;
2138}
2139
2140
2141static int download_ppsmo(struct sigma_dut *dut,
2142 struct sigma_conn *conn,
2143 const char *intf,
2144 struct sigma_cmd *cmd)
2145{
2146 const char *name, *path, *val;
2147 char url[500], buf[600], fbuf[100];
2148 char *fqdn = NULL;
2149
2150 name = get_param(cmd, "FileName");
2151 path = get_param(cmd, "FilePath");
2152 if (name == NULL || path == NULL)
2153 return -1;
2154
2155 if (strcasecmp(path, "VendorSpecific") == 0) {
2156 snprintf(url, sizeof(url), "PPS/%s", name);
2157 sigma_dut_print(dut, DUT_MSG_INFO, "Use pre-configured PPS MO "
2158 "from the device (%s)", url);
2159 if (!file_exists(url)) {
2160 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Requested "
2161 "PPS MO file does not exist");
2162 return 0;
2163 }
2164 snprintf(buf, sizeof(buf), "cp %s pps-tnds.xml", url);
2165 if (system(buf) != 0) {
2166 send_resp(dut, conn, SIGMA_ERROR,
2167 "errorCode,Failed to copy PPS MO");
2168 return 0;
2169 }
2170 } else if (strncasecmp(path, "http:", 5) != 0 &&
2171 strncasecmp(path, "https:", 6) != 0) {
2172 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,"
2173 "Unsupported FilePath value");
2174 return 0;
2175 } else {
2176 snprintf(url, sizeof(url), "%s/%s", path, name);
2177 sigma_dut_print(dut, DUT_MSG_INFO, "Downloading PPS MO from %s",
2178 url);
2179 snprintf(buf, sizeof(buf), "wget -T 10 -t 3 -O pps-tnds.xml '%s'", url);
2180 remove("pps-tnds.xml");
2181 if (system(buf) != 0) {
2182 send_resp(dut, conn, SIGMA_ERROR,
2183 "errorCode,Failed to download PPS MO");
2184 return 0;
2185 }
2186 }
2187
2188 if (run_hs20_osu(dut, "from_tnds pps-tnds.xml pps.xml") < 0) {
2189 send_resp(dut, conn, SIGMA_ERROR,
2190 "errorCode,Failed to parse downloaded PPSMO");
2191 return 0;
2192 }
2193 unlink("pps-tnds.xml");
2194
2195 val = get_param(cmd, "managementTreeURI");
2196 if (val) {
2197 const char *pos, *end;
2198 sigma_dut_print(dut, DUT_MSG_DEBUG, "managementTreeURI: %s",
2199 val);
2200 if (strncmp(val, "./Wi-Fi/", 8) != 0) {
2201 send_resp(dut, conn, SIGMA_ERROR,
2202 "errorCode,Invalid managementTreeURI prefix");
2203 return 0;
2204 }
2205 pos = val + 8;
2206 end = strchr(pos, '/');
2207 if (end == NULL ||
2208 strcmp(end, "/PerProviderSubscription") != 0) {
2209 send_resp(dut, conn, SIGMA_ERROR,
2210 "errorCode,Invalid managementTreeURI postfix");
2211 return 0;
2212 }
2213 if (end - pos >= (int) sizeof(fbuf)) {
2214 send_resp(dut, conn, SIGMA_ERROR,
2215 "errorCode,Too long FQDN in managementTreeURI");
2216 return 0;
2217 }
2218 memcpy(fbuf, pos, end - pos);
2219 fbuf[end - pos] = '\0';
2220 fqdn = fbuf;
2221 sigma_dut_print(dut, DUT_MSG_INFO,
2222 "FQDN from managementTreeURI: %s", fqdn);
2223 } else if (run_hs20_osu(dut, "get_fqdn pps.xml") == 0) {
2224 FILE *f = fopen("pps-fqdn", "r");
2225 if (f) {
2226 if (fgets(fbuf, sizeof(fbuf), f)) {
2227 fbuf[sizeof(fbuf) - 1] = '\0';
2228 fqdn = fbuf;
2229 sigma_dut_print(dut, DUT_MSG_DEBUG,
2230 "Use FQDN %s", fqdn);
2231 }
2232 fclose(f);
2233 }
2234 }
2235
2236 if (fqdn == NULL) {
2237 send_resp(dut, conn, SIGMA_ERROR,
2238 "errorCode,No FQDN specified");
2239 return 0;
2240 }
2241
2242 mkdir("SP", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
2243 snprintf(buf, sizeof(buf), "SP/%s", fqdn);
2244 mkdir(buf, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
2245
2246 snprintf(buf, sizeof(buf), "SP/%s/pps.xml", fqdn);
2247 if (rename("pps.xml", buf) < 0) {
2248 send_resp(dut, conn, SIGMA_ERROR,
2249 "errorCode,Could not move PPS MO");
2250 return 0;
2251 }
2252
2253 if (strcasecmp(path, "VendorSpecific") == 0) {
2254 snprintf(buf, sizeof(buf), "cp Certs/ca.pem SP/%s/ca.pem",
2255 fqdn);
2256 if (system(buf)) {
2257 send_resp(dut, conn, SIGMA_ERROR,
2258 "errorCode,Failed to copy OSU CA cert");
2259 return 0;
2260 }
2261
2262 snprintf(buf, sizeof(buf),
2263 "cp Certs/aaa-ca.pem SP/%s/aaa-ca.pem",
2264 fqdn);
2265 if (system(buf)) {
2266 send_resp(dut, conn, SIGMA_ERROR,
2267 "errorCode,Failed to copy AAA CA cert");
2268 return 0;
2269 }
2270 } else {
2271 snprintf(buf, sizeof(buf),
2272 "dl_osu_ca SP/%s/pps.xml SP/%s/ca.pem",
2273 fqdn, fqdn);
2274 if (run_hs20_osu(dut, buf) < 0) {
2275 send_resp(dut, conn, SIGMA_ERROR,
2276 "errorCode,Failed to download OSU CA cert");
2277 return 0;
2278 }
2279
2280 snprintf(buf, sizeof(buf),
2281 "dl_aaa_ca SP/%s/pps.xml SP/%s/aaa-ca.pem",
2282 fqdn, fqdn);
2283 if (run_hs20_osu(dut, buf) < 0) {
2284 sigma_dut_print(dut, DUT_MSG_INFO,
2285 "Failed to download AAA CA cert");
2286 }
2287 }
2288
2289 if (file_exists("next-client-cert.pem")) {
2290 snprintf(buf, sizeof(buf), "SP/%s/client-cert.pem", fqdn);
2291 if (rename("next-client-cert.pem", buf) < 0) {
2292 send_resp(dut, conn, SIGMA_ERROR,
2293 "errorCode,Could not move client certificate");
2294 return 0;
2295 }
2296 }
2297
2298 if (file_exists("next-client-key.pem")) {
2299 snprintf(buf, sizeof(buf), "SP/%s/client-key.pem", fqdn);
2300 if (rename("next-client-key.pem", buf) < 0) {
2301 send_resp(dut, conn, SIGMA_ERROR,
2302 "errorCode,Could not move client key");
2303 return 0;
2304 }
2305 }
2306
2307 snprintf(buf, sizeof(buf), "set_pps SP/%s/pps.xml", fqdn);
2308 if (run_hs20_osu(dut, buf) < 0) {
2309 send_resp(dut, conn, SIGMA_ERROR,
2310 "errorCode,Failed to configure credential from "
2311 "PPSMO");
2312 return 0;
2313 }
2314
2315 return 1;
2316}
2317
2318
2319static int download_cert(struct sigma_dut *dut,
2320 struct sigma_conn *conn,
2321 const char *intf,
2322 struct sigma_cmd *cmd)
2323{
2324 const char *name, *path;
2325 char url[500], buf[600];
2326
2327 name = get_param(cmd, "FileName");
2328 path = get_param(cmd, "FilePath");
2329 if (name == NULL || path == NULL)
2330 return -1;
2331
2332 if (strcasecmp(path, "VendorSpecific") == 0) {
2333 snprintf(url, sizeof(url), "Certs/%s-cert.pem", name);
2334 sigma_dut_print(dut, DUT_MSG_INFO, "Use pre-configured client "
2335 "certificate from the device (%s)", url);
2336 if (!file_exists(url)) {
2337 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Requested "
2338 "certificate file does not exist");
2339 return 0;
2340 }
2341 snprintf(buf, sizeof(buf), "cp %s next-client-cert.pem", url);
2342 if (system(buf) != 0) {
2343 send_resp(dut, conn, SIGMA_ERROR,
2344 "errorCode,Failed to copy client "
2345 "certificate");
2346 return 0;
2347 }
2348
2349 snprintf(url, sizeof(url), "Certs/%s-key.pem", name);
2350 sigma_dut_print(dut, DUT_MSG_INFO, "Use pre-configured client "
2351 "private key from the device (%s)", url);
2352 if (!file_exists(url)) {
2353 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Requested "
2354 "private key file does not exist");
2355 return 0;
2356 }
2357 snprintf(buf, sizeof(buf), "cp %s next-client-key.pem", url);
2358 if (system(buf) != 0) {
2359 send_resp(dut, conn, SIGMA_ERROR,
2360 "errorCode,Failed to copy client key");
2361 return 0;
2362 }
2363 } else if (strncasecmp(path, "http:", 5) != 0 &&
2364 strncasecmp(path, "https:", 6) != 0) {
2365 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,"
2366 "Unsupported FilePath value");
2367 return 0;
2368 } else {
2369 snprintf(url, sizeof(url), "%s/%s.pem", path, name);
2370 sigma_dut_print(dut, DUT_MSG_INFO, "Downloading client "
2371 "certificate/key from %s", url);
2372 snprintf(buf, sizeof(buf),
2373 "wget -T 10 -t 3 -O next-client-cert.pem '%s'", url);
2374 if (system(buf) != 0) {
2375 send_resp(dut, conn, SIGMA_ERROR,
2376 "errorCode,Failed to download client "
2377 "certificate");
2378 return 0;
2379 }
2380
2381 if (system("cp next-client-cert.pem next-client-key.pem") != 0)
2382 {
2383 send_resp(dut, conn, SIGMA_ERROR,
2384 "errorCode,Failed to copy client key");
2385 return 0;
2386 }
2387 }
2388
2389 return 1;
2390}
2391
2392
2393static int cmd_sta_preset_testparameters_hs2_r2(struct sigma_dut *dut,
2394 struct sigma_conn *conn,
2395 const char *intf,
2396 struct sigma_cmd *cmd)
2397{
2398 const char *val;
2399
2400 val = get_param(cmd, "FileType");
2401 if (val && strcasecmp(val, "PPSMO") == 0)
2402 return download_ppsmo(dut, conn, intf, cmd);
2403 if (val && strcasecmp(val, "CERT") == 0)
2404 return download_cert(dut, conn, intf, cmd);
2405 if (val) {
2406 send_resp(dut, conn, SIGMA_ERROR,
2407 "ErrorCode,Unsupported FileType");
2408 return 0;
2409 }
2410
2411 return 1;
2412}
2413
2414
2415static void ath_sta_set_noack(struct sigma_dut *dut, const char *intf,
2416 const char *val)
2417{
2418 int counter = 0;
2419 char token[50];
2420 char *result;
2421 char buf[100];
2422
2423 strncpy(token, val, sizeof(token));
2424 token[sizeof(token) - 1] = '\0';
2425 result = strtok(token, ":");
2426 while (result) {
2427 if (strcmp(result, "disable") == 0) {
2428 snprintf(buf, sizeof(buf),
2429 "iwpriv %s noackpolicy %d 1 0",
2430 intf, counter);
2431 } else {
2432 snprintf(buf, sizeof(buf),
2433 "iwpriv %s noackpolicy %d 1 1",
2434 intf, counter);
2435 }
2436 if (system(buf) != 0) {
2437 sigma_dut_print(dut, DUT_MSG_ERROR,
2438 "iwpriv noackpolicy failed");
2439 }
2440 result = strtok(NULL, ":");
2441 counter++;
2442 }
2443}
2444
2445
2446static void ath_sta_set_rts(struct sigma_dut *dut, const char *intf,
2447 const char *val)
2448{
2449 char buf[100];
2450
2451 snprintf(buf, sizeof(buf), "iwconfig %s rts %s", intf, val);
2452 if (system(buf) != 0) {
2453 sigma_dut_print(dut, DUT_MSG_ERROR, "iwconfig RTS failed");
2454 }
2455}
2456
2457
2458static void ath_sta_set_wmm(struct sigma_dut *dut, const char *intf,
2459 const char *val)
2460{
2461 char buf[100];
2462
2463 if (strcasecmp(val, "off") == 0) {
2464 snprintf(buf, sizeof(buf), "iwpriv %s wmm 0", intf);
2465 if (system(buf) != 0) {
2466 sigma_dut_print(dut, DUT_MSG_ERROR,
2467 "Failed to turn off WMM");
2468 }
2469 }
2470}
2471
2472
2473static void ath_sta_set_sgi(struct sigma_dut *dut, const char *intf,
2474 const char *val)
2475{
2476 char buf[100];
2477 int sgi20;
2478
2479 sgi20 = strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0;
2480
2481 snprintf(buf, sizeof(buf), "iwpriv %s shortgi %d", intf, sgi20);
2482 if (system(buf) != 0)
2483 sigma_dut_print(dut, DUT_MSG_ERROR, "iwpriv shortgi failed");
2484}
2485
2486
2487static void ath_sta_set_11nrates(struct sigma_dut *dut, const char *intf,
2488 const char *val)
2489{
2490 char buf[100];
2491 int rate_code;
2492
2493 /* Disable Tx Beam forming when using a fixed rate */
2494 ath_disable_txbf(dut, intf);
2495
2496 rate_code = 0x80 + atoi(val);
2497
2498 snprintf(buf, sizeof(buf), "iwpriv %s set11NRates 0x%x",
2499 intf, rate_code);
2500 if (system(buf) != 0) {
2501 sigma_dut_print(dut, DUT_MSG_ERROR,
2502 "iwpriv set11NRates failed");
2503 }
2504
2505 /* Channel width gets messed up, fix this */
2506 snprintf(buf, sizeof(buf), "iwpriv %s chwidth %d", intf, dut->chwidth);
2507 if (system(buf) != 0)
2508 sigma_dut_print(dut, DUT_MSG_ERROR, "iwpriv chwidth failed");
2509}
2510
2511
2512static void ath_sta_set_amsdu(struct sigma_dut *dut, const char *intf,
2513 const char *val)
2514{
2515 char buf[60];
2516
2517 if (strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0)
2518 snprintf(buf, sizeof(buf), "iwpriv %s amsdu 2", intf);
2519 else
2520 snprintf(buf, sizeof(buf), "iwpriv %s amsdu 1", intf);
2521
2522 if (system(buf) != 0)
2523 sigma_dut_print(dut, DUT_MSG_ERROR, "iwpriv amsdu failed");
2524}
2525
2526
2527static void ath_sta_set_stbc(struct sigma_dut *dut, const char *intf,
2528 const char *val)
2529{
2530 char buf[60];
2531
2532 snprintf(buf, sizeof(buf), "iwpriv %s tx_stbc %s", intf, val);
2533 if (system(buf) != 0) {
2534 sigma_dut_print(dut, DUT_MSG_ERROR,
2535 "iwpriv tx_stbc failed");
2536 }
2537
2538 snprintf(buf, sizeof(buf), "iwpriv %s rx_stbc %s", intf, val);
2539 if (system(buf) != 0) {
2540 sigma_dut_print(dut, DUT_MSG_ERROR,
2541 "iwpriv rx_stbc failed");
2542 }
2543}
2544
2545
2546static int wcn_sta_set_cts_width(struct sigma_dut *dut, const char *intf,
2547 const char *val)
2548{
2549 char buf[60];
2550
2551 if (strcmp(val, "80") == 0) {
2552 snprintf(buf, sizeof(buf), "iwpriv %s cts_cbw 3", intf);
2553 } else if (strcmp(val, "40") == 0) {
2554 snprintf(buf, sizeof(buf), "iwpriv %s cts_cbw 2", intf);
2555 } else if (strcmp(val, "20") == 0) {
2556 snprintf(buf, sizeof(buf), "iwpriv %s cts_cbw 1", intf);
2557 } else if (strcasecmp(val, "Auto") == 0) {
2558 buf[0] = '\0';
2559 } else {
2560 sigma_dut_print(dut, DUT_MSG_ERROR,
2561 "WIDTH/CTS_WIDTH value not supported");
2562 return -1;
2563 }
2564
2565 if (buf[0] != '\0' && system(buf) != 0) {
2566 sigma_dut_print(dut, DUT_MSG_ERROR,
2567 "Failed to set WIDTH/CTS_WIDTH");
2568 return -1;
2569 }
2570
2571 return 0;
2572}
2573
2574
2575int ath_set_width(struct sigma_dut *dut, struct sigma_conn *conn,
2576 const char *intf, const char *val)
2577{
2578 char buf[60];
2579
2580 if (strcasecmp(val, "Auto") == 0) {
2581 snprintf(buf, sizeof(buf), "iwpriv %s chwidth 0", intf);
2582 dut->chwidth = 0;
2583 } else if (strcasecmp(val, "20") == 0) {
2584 snprintf(buf, sizeof(buf), "iwpriv %s chwidth 0", intf);
2585 dut->chwidth = 0;
2586 } else if (strcasecmp(val, "40") == 0) {
2587 snprintf(buf, sizeof(buf), "iwpriv %s chwidth 1", intf);
2588 dut->chwidth = 1;
2589 } else if (strcasecmp(val, "80") == 0) {
2590 snprintf(buf, sizeof(buf), "iwpriv %s chwidth 2", intf);
2591 dut->chwidth = 2;
2592 } else if (strcasecmp(val, "160") == 0) {
2593 snprintf(buf, sizeof(buf), "iwpriv %s chwidth 3", intf);
2594 dut->chwidth = 3;
2595 } else {
2596 send_resp(dut, conn, SIGMA_ERROR,
2597 "ErrorCode,WIDTH not supported");
2598 return -1;
2599 }
2600
2601 if (system(buf) != 0) {
2602 sigma_dut_print(dut, DUT_MSG_ERROR,
2603 "iwpriv chwidth failed");
2604 }
2605
2606 return 0;
2607}
2608
2609
2610static int wcn_sta_set_sp_stream(struct sigma_dut *dut, const char *intf,
2611 const char *val)
2612{
2613 char buf[60];
2614
2615 if (strcmp(val, "1SS") == 0) {
2616 snprintf(buf, sizeof(buf), "iwpriv %s nss 1", intf);
2617 } else if (strcmp(val, "2SS") == 0) {
2618 snprintf(buf, sizeof(buf), "iwpriv %s nss 2", intf);
2619 } else {
2620 sigma_dut_print(dut, DUT_MSG_ERROR,
2621 "SP_STREAM value not supported");
2622 return -1;
2623 }
2624
2625 if (system(buf) != 0) {
2626 sigma_dut_print(dut, DUT_MSG_ERROR,
2627 "Failed to set SP_STREAM");
2628 return -1;
2629 }
2630
2631 return 0;
2632}
2633
2634
2635static int cmd_sta_preset_testparameters(struct sigma_dut *dut,
2636 struct sigma_conn *conn,
2637 struct sigma_cmd *cmd)
2638{
2639 const char *intf = get_param(cmd, "Interface");
2640 const char *val;
2641
2642 val = get_param(cmd, "Program");
2643 if (val && strcasecmp(val, "HS2-R2") == 0) {
2644 if (intf == NULL)
2645 return -1;
2646 return cmd_sta_preset_testparameters_hs2_r2(dut, conn, intf,
2647 cmd);
2648 }
2649
2650#ifdef ANDROID_NAN
2651 if (val && strcasecmp(val, "NAN") == 0)
2652 return nan_cmd_sta_preset_testparameters(dut, conn, cmd);
2653#endif /* ANDROID_NAN */
2654
2655#if 0
2656 val = get_param(cmd, "Supplicant");
2657 if (val && strcasecmp(val, "Default") != 0) {
2658 send_resp(dut, conn, SIGMA_ERROR,
2659 "ErrorCode,Only default(Vendor) supplicant "
2660 "supported");
2661 return 0;
2662 }
2663#endif
2664
2665 val = get_param(cmd, "RTS");
2666 if (val) {
2667 switch (get_driver_type()) {
2668 case DRIVER_ATHEROS:
2669 ath_sta_set_rts(dut, intf, val);
2670 break;
2671 default:
2672#if 0
2673 send_resp(dut, conn, SIGMA_ERROR,
2674 "ErrorCode,Setting RTS not supported");
2675 return 0;
2676#else
2677 sigma_dut_print(dut, DUT_MSG_DEBUG,
2678 "Setting RTS not supported");
2679 break;
2680#endif
2681 }
2682 }
2683
2684#if 0
2685 val = get_param(cmd, "FRGMNT");
2686 if (val) {
2687 /* TODO */
2688 send_resp(dut, conn, SIGMA_ERROR,
2689 "ErrorCode,Setting FRGMNT not supported");
2690 return 0;
2691 }
2692#endif
2693
2694#if 0
2695 val = get_param(cmd, "Preamble");
2696 if (val) {
2697 /* TODO: Long/Short */
2698 send_resp(dut, conn, SIGMA_ERROR,
2699 "ErrorCode,Setting Preamble not supported");
2700 return 0;
2701 }
2702#endif
2703
2704 val = get_param(cmd, "Mode");
2705 if (val) {
2706 if (strcmp(val, "11b") == 0 ||
2707 strcmp(val, "11g") == 0 ||
2708 strcmp(val, "11a") == 0 ||
2709 strcmp(val, "11n") == 0 ||
2710 strcmp(val, "11ng") == 0 ||
2711 strcmp(val, "11nl") == 0 ||
2712 strcmp(val, "11nl(nabg)") == 0 ||
2713 strcmp(val, "AC") == 0 ||
2714 strcmp(val, "11AC") == 0 ||
2715 strcmp(val, "11ac") == 0 ||
2716 strcmp(val, "11na") == 0 ||
2717 strcmp(val, "11an") == 0) {
2718 /* STA supports all modes by default */
2719 } else {
2720 send_resp(dut, conn, SIGMA_ERROR,
2721 "ErrorCode,Setting Mode not supported");
2722 return 0;
2723 }
2724 }
2725
2726 val = get_param(cmd, "wmm");
2727 if (val) {
2728 switch (get_driver_type()) {
2729 case DRIVER_ATHEROS:
2730 ath_sta_set_wmm(dut, intf, val);
2731 break;
2732 default:
2733 sigma_dut_print(dut, DUT_MSG_DEBUG,
2734 "Setting wmm not supported");
2735 break;
2736 }
2737 }
2738
2739 val = get_param(cmd, "Powersave");
2740 if (val) {
2741 if (strcmp(val, "0") == 0 || strcasecmp(val, "off") == 0) {
2742 if (wpa_command(get_station_ifname(),
2743 "P2P_SET ps 0") < 0)
2744 return -2;
2745 /* Make sure test modes are disabled */
2746 wpa_command(get_station_ifname(), "P2P_SET ps 98");
2747 wpa_command(get_station_ifname(), "P2P_SET ps 96");
2748 } else if (strcmp(val, "1") == 0 ||
2749 strcasecmp(val, "PSPoll") == 0 ||
2750 strcasecmp(val, "on") == 0) {
2751 /* Disable default power save mode */
2752 wpa_command(get_station_ifname(), "P2P_SET ps 0");
2753 /* Enable PS-Poll test mode */
2754 if (wpa_command(get_station_ifname(),
2755 "P2P_SET ps 97") < 0 ||
2756 wpa_command(get_station_ifname(),
2757 "P2P_SET ps 99") < 0)
2758 return -2;
2759 } else if (strcmp(val, "2") == 0 ||
2760 strcasecmp(val, "Fast") == 0) {
2761 /* TODO */
2762 send_resp(dut, conn, SIGMA_ERROR,
2763 "ErrorCode,Powersave=Fast not supported");
2764 return 0;
2765 } else if (strcmp(val, "3") == 0 ||
2766 strcasecmp(val, "PSNonPoll") == 0) {
2767 /* Make sure test modes are disabled */
2768 wpa_command(get_station_ifname(), "P2P_SET ps 98");
2769 wpa_command(get_station_ifname(), "P2P_SET ps 96");
2770
2771 /* Enable default power save mode */
2772 if (wpa_command(get_station_ifname(),
2773 "P2P_SET ps 1") < 0)
2774 return -2;
2775 } else
2776 return -1;
2777 }
2778
2779 val = get_param(cmd, "NoAck");
2780 if (val) {
2781 switch (get_driver_type()) {
2782 case DRIVER_ATHEROS:
2783 ath_sta_set_noack(dut, intf, val);
2784 break;
2785 default:
2786 send_resp(dut, conn, SIGMA_ERROR,
2787 "ErrorCode,Setting NoAck not supported");
2788 return 0;
2789 }
2790 }
2791
2792 val = get_param(cmd, "IgnoreChswitchProhibit");
2793 if (val) {
2794 /* TODO: Enabled/disabled */
2795 if (strcasecmp(val, "Enabled") == 0) {
2796 send_resp(dut, conn, SIGMA_ERROR,
2797 "ErrorCode,Enabling IgnoreChswitchProhibit "
2798 "not supported");
2799 return 0;
2800 }
2801 }
2802
2803 val = get_param(cmd, "TDLS");
2804 if (val) {
2805 if (strcasecmp(val, "Disabled") == 0) {
2806 if (wpa_command(intf, "SET tdls_disabled 1")) {
2807 send_resp(dut, conn, SIGMA_ERROR,
2808 "ErrorCode,Failed to disable TDLS");
2809 return 0;
2810 }
2811 } else if (strcasecmp(val, "Enabled") == 0) {
2812 if (wpa_command(intf, "SET tdls_disabled 0")) {
2813 send_resp(dut, conn, SIGMA_ERROR,
2814 "ErrorCode,Failed to enable TDLS");
2815 return 0;
2816 }
2817 } else {
2818 send_resp(dut, conn, SIGMA_ERROR,
2819 "ErrorCode,Unsupported TDLS value");
2820 return 0;
2821 }
2822 }
2823
2824 val = get_param(cmd, "TDLSmode");
2825 if (val) {
2826 if (strcasecmp(val, "Default") == 0) {
2827 wpa_command(intf, "SET tdls_testing 0");
2828 } else if (strcasecmp(val, "APProhibit") == 0) {
2829 if (wpa_command(intf, "SET tdls_testing 0x400")) {
2830 send_resp(dut, conn, SIGMA_ERROR,
2831 "ErrorCode,Failed to enable ignore "
2832 "APProhibit TDLS mode");
2833 return 0;
2834 }
2835 } else if (strcasecmp(val, "HiLoMac") == 0) {
2836 /* STA should respond with TDLS setup req for a TDLS
2837 * setup req */
2838 if (wpa_command(intf, "SET tdls_testing 0x80")) {
2839 send_resp(dut, conn, SIGMA_ERROR,
2840 "ErrorCode,Failed to enable HiLoMac "
2841 "TDLS mode");
2842 return 0;
2843 }
2844 } else if (strcasecmp(val, "WeakSecurity") == 0) {
2845 /*
2846 * Since all security modes are enabled by default when
2847 * Sigma control is used, there is no need to do
2848 * anything here.
2849 */
2850 } else if (strcasecmp(val, "ExistLink") == 0) {
2851 /*
2852 * Since we allow new TDLS Setup Request even if there
2853 * is an existing link, nothing needs to be done for
2854 * this.
2855 */
2856 } else {
2857 /* TODO:
2858 * ExistLink: STA should send TDLS setup req even if
2859 * direct link already exists
2860 */
2861 send_resp(dut, conn, SIGMA_ERROR,
2862 "ErrorCode,Unsupported TDLSmode value");
2863 return 0;
2864 }
2865 }
2866
2867 val = get_param(cmd, "FakePubKey");
2868 if (val && atoi(val) && wpa_command(intf, "SET wps_corrupt_pkhash 1")) {
2869 send_resp(dut, conn, SIGMA_ERROR,
2870 "ErrorCode,Failed to enable FakePubKey");
2871 return 0;
2872 }
2873
2874 return 1;
2875}
2876
2877
2878static const char * ath_get_radio_name(const char *radio_name)
2879{
2880 if (radio_name == NULL)
2881 return "wifi0";
2882 if (strcmp(radio_name, "wifi1") == 0)
2883 return "wifi1";
2884 if (strcmp(radio_name, "wifi2") == 0)
2885 return "wifi2";
2886 return "wifi0";
2887}
2888
2889
2890static void ath_sta_set_txsp_stream(struct sigma_dut *dut, const char *intf,
2891 const char *val)
2892{
2893 char buf[60];
2894 unsigned int vht_mcsmap = 0;
2895 int txchainmask = 0;
2896 const char *basedev = ath_get_radio_name(sigma_radio_ifname[0]);
2897
2898 if (strcasecmp(val, "1") == 0 || strcasecmp(val, "1SS") == 0) {
2899 if (dut->testbed_flag_txsp == 1) {
2900 vht_mcsmap = 0xfffc;
2901 dut->testbed_flag_txsp = 0;
2902 } else {
2903 vht_mcsmap = 0xfffe;
2904 }
2905 txchainmask = 1;
2906 } else if (strcasecmp(val, "2") == 0 || strcasecmp(val, "2SS") == 0) {
2907 if (dut->testbed_flag_txsp == 1) {
2908 vht_mcsmap = 0xfff0;
2909 dut->testbed_flag_txsp = 0;
2910 } else {
2911 vht_mcsmap = 0xfffa;
2912 }
2913 txchainmask = 3;
2914 } else if (strcasecmp(val, "3") == 0 || strcasecmp(val, "3SS") == 0) {
2915 if (dut->testbed_flag_txsp == 1) {
2916 vht_mcsmap = 0xffc0;
2917 dut->testbed_flag_txsp = 0;
2918 } else {
2919 vht_mcsmap = 0xffea;
2920 }
2921 txchainmask = 7;
2922 } else if (strcasecmp(val, "4") == 0 || strcasecmp(val, "4SS") == 0) {
2923 if (dut->testbed_flag_txsp == 1) {
2924 vht_mcsmap = 0xff00;
2925 dut->testbed_flag_txsp = 0;
2926 } else {
2927 vht_mcsmap = 0xffaa;
2928 }
2929 txchainmask = 15;
2930 } else {
2931 if (dut->testbed_flag_txsp == 1) {
2932 vht_mcsmap = 0xffc0;
2933 dut->testbed_flag_txsp = 0;
2934 } else {
2935 vht_mcsmap = 0xffea;
2936 }
2937 }
2938
2939 if (txchainmask) {
2940 snprintf(buf, sizeof(buf), "iwpriv %s txchainmask %d",
2941 basedev, txchainmask);
2942 if (system(buf) != 0) {
2943 sigma_dut_print(dut, DUT_MSG_ERROR,
2944 "iwpriv txchainmask failed");
2945 }
2946 }
2947
2948 snprintf(buf, sizeof(buf), "iwpriv %s vht_mcsmap 0x%04x",
2949 intf, vht_mcsmap);
2950 if (system(buf) != 0) {
2951 sigma_dut_print(dut, DUT_MSG_ERROR,
2952 "iwpriv %s vht_mcsmap 0x%04x failed",
2953 intf, vht_mcsmap);
2954 }
2955}
2956
2957
2958static void ath_sta_set_rxsp_stream(struct sigma_dut *dut, const char *intf,
2959 const char *val)
2960{
2961 char buf[60];
2962 unsigned int vht_mcsmap = 0;
2963 int rxchainmask = 0;
2964 const char *basedev = ath_get_radio_name(sigma_radio_ifname[0]);
2965
2966 if (strcasecmp(val, "1") == 0 || strcasecmp(val, "1SS") == 0) {
2967 if (dut->testbed_flag_rxsp == 1) {
2968 vht_mcsmap = 0xfffc;
2969 dut->testbed_flag_rxsp = 0;
2970 } else {
2971 vht_mcsmap = 0xfffe;
2972 }
2973 rxchainmask = 1;
2974 } else if (strcasecmp(val, "2") == 0 || strcasecmp(val, "2SS") == 0) {
2975 if (dut->testbed_flag_rxsp == 1) {
2976 vht_mcsmap = 0xfff0;
2977 dut->testbed_flag_rxsp = 0;
2978 } else {
2979 vht_mcsmap = 0xfffa;
2980 }
2981 rxchainmask = 3;
2982 } else if (strcasecmp(val, "3") == 0 || strcasecmp(val, "3SS") == 0) {
2983 if (dut->testbed_flag_rxsp == 1) {
2984 vht_mcsmap = 0xffc0;
2985 dut->testbed_flag_rxsp = 0;
2986 } else {
2987 vht_mcsmap = 0xffea;
2988 }
2989 rxchainmask = 7;
2990 } else if (strcasecmp(val, "4") == 0 || strcasecmp(val, "4SS") == 0) {
2991 if (dut->testbed_flag_rxsp == 1) {
2992 vht_mcsmap = 0xff00;
2993 dut->testbed_flag_rxsp = 0;
2994 } else {
2995 vht_mcsmap = 0xffaa;
2996 }
2997 rxchainmask = 15;
2998 } else {
2999 if (dut->testbed_flag_rxsp == 1) {
3000 vht_mcsmap = 0xffc0;
3001 dut->testbed_flag_rxsp = 0;
3002 } else {
3003 vht_mcsmap = 0xffea;
3004 }
3005 }
3006
3007 if (rxchainmask) {
3008 snprintf(buf, sizeof(buf), "iwpriv %s rxchainmask %d",
3009 basedev, rxchainmask);
3010 if (system(buf) != 0) {
3011 sigma_dut_print(dut, DUT_MSG_ERROR,
3012 "iwpriv rxchainmask failed");
3013 }
3014 }
3015
3016 snprintf(buf, sizeof(buf), "iwpriv %s vht_mcsmap 0x%04x",
3017 intf, vht_mcsmap);
3018 if (system(buf) != 0) {
3019 sigma_dut_print(dut, DUT_MSG_ERROR,
3020 "iwpriv %s vht_mcsmap 0x%04x",
3021 intf, vht_mcsmap);
3022 }
3023}
3024
3025
3026void ath_set_zero_crc(struct sigma_dut *dut, const char *val)
3027{
3028 if (strcasecmp(val, "enable") == 0) {
3029 if (system("athdiag --set --address=0x2a204 --and=0xbfffffff")
3030 != 0) {
3031 sigma_dut_print(dut, DUT_MSG_ERROR,
3032 "Disable BB_VHTSIGB_CRC_CALC failed");
3033 }
3034
3035 if (system("athdiag --set --address=0x2a204 --or=0x80000000")
3036 != 0) {
3037 sigma_dut_print(dut, DUT_MSG_ERROR,
3038 "Enable FORCE_VHT_SIGB_CRC_VALUE_ZERO failed");
3039 }
3040 } else {
3041 if (system("athdiag --set --address=0x2a204 --and=0x7fffffff")
3042 != 0) {
3043 sigma_dut_print(dut, DUT_MSG_ERROR,
3044 "Disable FORCE_VHT_SIGB_CRC_VALUE_ZERO failed");
3045 }
3046
3047 if (system("athdiag --set --address=0x2a204 --or=0x40000000")
3048 != 0) {
3049 sigma_dut_print(dut, DUT_MSG_ERROR,
3050 "Enable BB_VHTSIGB_CRC_CALC failed");
3051 }
3052 }
3053}
3054
3055
3056static int cmd_sta_set_wireless_common(const char *intf, struct sigma_dut *dut,
3057 struct sigma_conn *conn,
3058 struct sigma_cmd *cmd)
3059{
3060 const char *val;
3061 int ampdu = -1;
3062 char buf[30];
3063
3064 val = get_param(cmd, "40_INTOLERANT");
3065 if (val) {
3066 if (strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0) {
3067 /* TODO: iwpriv ht40intol through wpa_supplicant */
3068 send_resp(dut, conn, SIGMA_ERROR,
3069 "ErrorCode,40_INTOLERANT not supported");
3070 return 0;
3071 }
3072 }
3073
3074 val = get_param(cmd, "ADDBA_REJECT");
3075 if (val) {
3076 if (strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0) {
3077 /* reject any ADDBA with status "decline" */
3078 ampdu = 0;
3079 } else {
3080 /* accept ADDBA */
3081 ampdu = 1;
3082 }
3083 }
3084
3085 val = get_param(cmd, "AMPDU");
3086 if (val) {
3087 if (strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0) {
3088 /* enable AMPDU Aggregation */
3089 if (ampdu == 0) {
3090 send_resp(dut, conn, SIGMA_ERROR,
3091 "ErrorCode,Mismatch in "
3092 "addba_reject/ampdu - "
3093 "not supported");
3094 return 0;
3095 }
3096 ampdu = 1;
3097 } else {
3098 /* disable AMPDU Aggregation */
3099 if (ampdu == 1) {
3100 send_resp(dut, conn, SIGMA_ERROR,
3101 "ErrorCode,Mismatch in "
3102 "addba_reject/ampdu - "
3103 "not supported");
3104 return 0;
3105 }
3106 ampdu = 0;
3107 }
3108 }
3109
3110 if (ampdu >= 0) {
3111 sigma_dut_print(dut, DUT_MSG_DEBUG, "%s A-MPDU aggregation",
3112 ampdu ? "Enabling" : "Disabling");
3113 snprintf(buf, sizeof(buf), "SET ampdu %d", ampdu);
3114 if (wpa_command(intf, buf) < 0) {
3115 send_resp(dut, conn, SIGMA_ERROR,
3116 "ErrorCode,set aggr failed");
3117 return 0;
3118 }
3119 }
3120
3121 val = get_param(cmd, "AMSDU");
3122 if (val) {
3123 switch (get_driver_type()) {
3124 case DRIVER_ATHEROS:
3125 ath_sta_set_amsdu(dut, intf, val);
3126 break;
3127 default:
3128 if (strcmp(val, "1") == 0 ||
3129 strcasecmp(val, "Enable") == 0) {
3130 /* Enable AMSDU Aggregation */
3131 send_resp(dut, conn, SIGMA_ERROR,
3132 "ErrorCode,AMSDU aggregation not supported");
3133 return 0;
3134 }
3135 break;
3136 }
3137 }
3138
3139 val = get_param(cmd, "STBC_RX");
3140 if (val) {
3141 switch (get_driver_type()) {
3142 case DRIVER_ATHEROS:
3143 ath_sta_set_stbc(dut, intf, val);
3144 break;
3145 default:
3146 send_resp(dut, conn, SIGMA_ERROR,
3147 "ErrorCode,STBC_RX not supported");
3148 return 0;
3149 }
3150 }
3151
3152 val = get_param(cmd, "WIDTH");
3153 if (val) {
3154 switch (get_driver_type()) {
3155 case DRIVER_WCN:
3156 if (wcn_sta_set_cts_width(dut, intf, val) < 0) {
3157 send_resp(dut, conn, SIGMA_ERROR,
3158 "ErrorCode,Failed to set WIDTH");
3159 return 0;
3160 }
3161 break;
3162 case DRIVER_ATHEROS:
3163 if (ath_set_width(dut, conn, intf, val) < 0)
3164 return 0;
3165 break;
3166 default:
3167 sigma_dut_print(dut, DUT_MSG_ERROR,
3168 "Setting WIDTH not supported");
3169 break;
3170 }
3171 }
3172
3173 val = get_param(cmd, "SMPS");
3174 if (val) {
3175 /* TODO: Dynamic/0, Static/1, No Limit/2 */
3176 send_resp(dut, conn, SIGMA_ERROR,
3177 "ErrorCode,SMPS not supported");
3178 return 0;
3179 }
3180
3181 val = get_param(cmd, "TXSP_STREAM");
3182 if (val) {
3183 switch (get_driver_type()) {
3184 case DRIVER_WCN:
3185 if (wcn_sta_set_sp_stream(dut, intf, val) < 0) {
3186 send_resp(dut, conn, SIGMA_ERROR,
3187 "ErrorCode,Failed to set TXSP_STREAM");
3188 return 0;
3189 }
3190 break;
3191 case DRIVER_ATHEROS:
3192 ath_sta_set_txsp_stream(dut, intf, val);
3193 break;
3194 default:
3195 sigma_dut_print(dut, DUT_MSG_ERROR,
3196 "Setting TXSP_STREAM not supported");
3197 break;
3198 }
3199 }
3200
3201 val = get_param(cmd, "RXSP_STREAM");
3202 if (val) {
3203 switch (get_driver_type()) {
3204 case DRIVER_WCN:
3205 if (wcn_sta_set_sp_stream(dut, intf, val) < 0) {
3206 send_resp(dut, conn, SIGMA_ERROR,
3207 "ErrorCode,Failed to set RXSP_STREAM");
3208 return 0;
3209 }
3210 break;
3211 case DRIVER_ATHEROS:
3212 ath_sta_set_rxsp_stream(dut, intf, val);
3213 break;
3214 default:
3215 sigma_dut_print(dut, DUT_MSG_ERROR,
3216 "Setting RXSP_STREAM not supported");
3217 break;
3218 }
3219 }
3220
3221 val = get_param(cmd, "DYN_BW_SGNL");
3222 if (val) {
3223 if (strcasecmp(val, "Enable") == 0) {
3224 snprintf(buf, sizeof(buf), "iwpriv %s cwmenable 1",
3225 intf);
3226 } else if (strcasecmp(val, "Disable") == 0) {
3227 snprintf(buf, sizeof(buf), "iwpriv %s cwmenable 0",
3228 intf);
3229 } else {
3230 send_resp(dut, conn, SIGMA_ERROR,
3231 "ErrorCode,DYN_BW_SGNL value not supported");
3232 return 0;
3233 }
3234
3235 if (system(buf) != 0) {
3236 sigma_dut_print(dut, DUT_MSG_ERROR,
3237 "Failed to set DYN_BW_SGNL");
3238 }
3239
3240 if (get_driver_type() == DRIVER_WCN) {
3241 snprintf(buf, sizeof(buf), "iwpriv %s cts_cbw 3", intf);
3242 if (system(buf) != 0) {
3243 sigma_dut_print(dut, DUT_MSG_ERROR,
3244 "Failed to set cts_cbw in DYN_BW_SGNL");
3245 return 0;
3246 }
3247 }
3248
3249 }
3250
3251 val = get_param(cmd, "RTS_FORCE");
3252 if (val) {
3253 if (strcasecmp(val, "Enable") == 0) {
3254 snprintf(buf, sizeof(buf), "iwconfig %s rts 64", intf);
3255 } else if (strcasecmp(val, "Disable") == 0) {
3256 snprintf(buf, sizeof(buf), "iwconfig %s rts 2347",
3257 intf);
3258 } else {
3259 send_resp(dut, conn, SIGMA_ERROR,
3260 "ErrorCode,RTS_FORCE value not supported");
3261 return 0;
3262 }
3263
3264 if (system(buf) != 0) {
3265 sigma_dut_print(dut, DUT_MSG_ERROR,
3266 "Failed to set RTS_FORCE");
3267 }
3268 }
3269
3270 val = get_param(cmd, "CTS_WIDTH");
3271 if (val) {
3272 switch (get_driver_type()) {
3273 case DRIVER_WCN:
3274 if (wcn_sta_set_cts_width(dut, intf, val) < 0) {
3275 send_resp(dut, conn, SIGMA_ERROR,
3276 "ErrorCode,Failed to set CTS_WIDTH");
3277 return 0;
3278 }
3279 break;
3280 case DRIVER_ATHEROS:
3281 ath_set_cts_width(dut, intf, val);
3282 break;
3283 default:
3284 sigma_dut_print(dut, DUT_MSG_ERROR,
3285 "Setting CTS_WIDTH not supported");
3286 break;
3287 }
3288 }
3289
3290 val = get_param(cmd, "BW_SGNL");
3291 if (val) {
3292 if (strcasecmp(val, "Enable") == 0) {
3293 snprintf(buf, sizeof(buf), "iwpriv %s cwmenable 1",
3294 intf);
3295 } else if (strcasecmp(val, "Disable") == 0) {
3296 /* TODO: Disable */
3297 buf[0] = '\0';
3298 } else {
3299 send_resp(dut, conn, SIGMA_ERROR,
3300 "ErrorCode,BW_SGNL value not supported");
3301 return 0;
3302 }
3303
3304 if (buf[0] != '\0' && system(buf) != 0) {
3305 sigma_dut_print(dut, DUT_MSG_ERROR,
3306 "Failed to set BW_SGNL");
3307 }
3308 }
3309
3310 val = get_param(cmd, "Band");
3311 if (val) {
3312 if (strcmp(val, "2.4") == 0 || strcmp(val, "5") == 0) {
3313 /* STA supports all bands by default */
3314 } else {
3315 send_resp(dut, conn, SIGMA_ERROR,
3316 "ErrorCode,Unsupported Band");
3317 return 0;
3318 }
3319 }
3320
3321 val = get_param(cmd, "zero_crc");
3322 if (val) {
3323 switch (get_driver_type()) {
3324 case DRIVER_ATHEROS:
3325 ath_set_zero_crc(dut, val);
3326 break;
3327 default:
3328 break;
3329 }
3330 }
3331
3332 return 1;
3333}
3334
3335
3336static int sta_set_60g_common(struct sigma_dut *dut, struct sigma_conn *conn,
3337 struct sigma_cmd *cmd)
3338{
3339 const char *val;
3340 char buf[100];
3341
3342 val = get_param(cmd, "MSDUSize");
3343 if (val) {
3344 int mtu;
3345
3346 dut->amsdu_size = atoi(val);
3347 if (dut->amsdu_size > IEEE80211_MAX_DATA_LEN_DMG ||
3348 dut->amsdu_size < IEEE80211_SNAP_LEN_DMG) {
3349 sigma_dut_print(dut, DUT_MSG_ERROR,
3350 "MSDUSize %d is above max %d or below min %d",
3351 dut->amsdu_size,
3352 IEEE80211_MAX_DATA_LEN_DMG,
3353 IEEE80211_SNAP_LEN_DMG);
3354 dut->amsdu_size = 0;
3355 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
3356 }
3357
3358 mtu = dut->amsdu_size - IEEE80211_SNAP_LEN_DMG;
3359 sigma_dut_print(dut, DUT_MSG_DEBUG,
3360 "Setting amsdu_size to %d", mtu);
3361 snprintf(buf, sizeof(buf), "ifconfig %s mtu %d",
3362 get_station_ifname(), mtu);
3363
3364 if (system(buf) != 0) {
3365 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to set %s",
3366 buf);
3367 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
3368 }
3369 }
3370
3371 val = get_param(cmd, "BAckRcvBuf");
3372 if (val) {
3373 dut->back_rcv_buf = atoi(val);
3374 if (dut->back_rcv_buf == 0) {
3375 sigma_dut_print(dut, DUT_MSG_ERROR,
3376 "Failed to convert %s or value is 0",
3377 val);
3378 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
3379 }
3380
3381 sigma_dut_print(dut, DUT_MSG_DEBUG,
3382 "Setting BAckRcvBuf to %s", val);
3383 }
3384
3385 return SIGMA_DUT_SUCCESS_CALLER_SEND_STATUS;
3386}
3387
3388
3389static int sta_pcp_start(struct sigma_dut *dut, struct sigma_conn *conn,
3390 struct sigma_cmd *cmd)
3391{
3392 int net_id;
3393 char *ifname;
3394 const char *val;
3395 char buf[100];
3396
3397 dut->mode = SIGMA_MODE_STATION;
3398 ifname = get_main_ifname();
3399 if (wpa_command(ifname, "PING") != 0) {
3400 sigma_dut_print(dut, DUT_MSG_ERROR, "Supplicant not running");
3401 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
3402 }
3403
3404 wpa_command(ifname, "FLUSH");
3405 net_id = add_network_common(dut, conn, ifname, cmd);
3406 if (net_id < 0) {
3407 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to add network");
3408 return net_id;
3409 }
3410
3411 /* TODO: mode=2 for the AP; in the future, replace for mode PCP */
3412 if (set_network(ifname, net_id, "mode", "2") < 0) {
3413 sigma_dut_print(dut, DUT_MSG_ERROR,
3414 "Failed to set supplicant network mode");
3415 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
3416 }
3417
3418 sigma_dut_print(dut, DUT_MSG_DEBUG,
3419 "Supplicant set network with mode 2");
3420
3421 val = get_param(cmd, "Security");
3422 if (val && strcasecmp(val, "OPEN") == 0) {
3423 dut->ap_key_mgmt = AP_OPEN;
3424 if (set_network(ifname, net_id, "key_mgmt", "NONE") < 0) {
3425 sigma_dut_print(dut, DUT_MSG_ERROR,
3426 "Failed to set supplicant to %s security",
3427 val);
3428 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
3429 }
3430 } else if (val && strcasecmp(val, "WPA2-PSK") == 0) {
3431 dut->ap_key_mgmt = AP_WPA2_PSK;
3432 if (set_network(ifname, net_id, "key_mgmt", "WPA-PSK") < 0) {
3433 sigma_dut_print(dut, DUT_MSG_ERROR,
3434 "Failed to set supplicant to %s security",
3435 val);
3436 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
3437 }
3438
3439 if (set_network(ifname, net_id, "proto", "RSN") < 0) {
3440 sigma_dut_print(dut, DUT_MSG_ERROR,
3441 "Failed to set supplicant to proto RSN");
3442 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
3443 }
3444 } else if (val) {
3445 sigma_dut_print(dut, DUT_MSG_ERROR,
3446 "Requested Security %s is not supported on 60GHz",
3447 val);
3448 return SIGMA_DUT_INVALID_CALLER_SEND_STATUS;
3449 }
3450
3451 val = get_param(cmd, "Encrypt");
3452 if (val && strcasecmp(val, "AES-GCMP") == 0) {
3453 if (set_network(ifname, net_id, "pairwise", "GCMP") < 0) {
3454 sigma_dut_print(dut, DUT_MSG_ERROR,
3455 "Failed to set supplicant to pairwise GCMP");
3456 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
3457 }
3458 if (set_network(ifname, net_id, "group", "GCMP") < 0) {
3459 sigma_dut_print(dut, DUT_MSG_ERROR,
3460 "Failed to set supplicant to group GCMP");
3461 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
3462 }
3463 } else if (val) {
3464 sigma_dut_print(dut, DUT_MSG_ERROR,
3465 "Requested Encrypt %s is not supported on 60 GHz",
3466 val);
3467 return SIGMA_DUT_INVALID_CALLER_SEND_STATUS;
3468 }
3469
3470 val = get_param(cmd, "PSK");
3471 if (val && set_network_quoted(ifname, net_id, "psk", val) < 0) {
3472 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to set psk %s",
3473 val);
3474 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
3475 }
3476
3477 /* Convert 60G channel to freq */
3478 switch (dut->ap_channel) {
3479 case 1:
3480 val = "58320";
3481 break;
3482 case 2:
3483 val = "60480";
3484 break;
3485 case 3:
3486 val = "62640";
3487 break;
3488 default:
3489 sigma_dut_print(dut, DUT_MSG_ERROR,
3490 "Failed to configure channel %d. Not supported",
3491 dut->ap_channel);
3492 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
3493 }
3494
3495 if (set_network(ifname, net_id, "frequency", val) < 0) {
3496 sigma_dut_print(dut, DUT_MSG_ERROR,
3497 "Failed to set supplicant network frequency");
3498 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
3499 }
3500
3501 sigma_dut_print(dut, DUT_MSG_DEBUG,
3502 "Supplicant set network with frequency");
3503
3504 snprintf(buf, sizeof(buf), "SELECT_NETWORK %d", net_id);
3505 if (wpa_command(ifname, buf) < 0) {
3506 sigma_dut_print(dut, DUT_MSG_INFO,
3507 "Failed to select network id %d on %s",
3508 net_id, ifname);
3509 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
3510 }
3511
3512 sigma_dut_print(dut, DUT_MSG_DEBUG, "Selected network");
3513
3514 return SIGMA_DUT_SUCCESS_CALLER_SEND_STATUS;
3515}
3516
3517
3518static int sta_set_60g_pcp(struct sigma_dut *dut, struct sigma_conn *conn,
3519 struct sigma_cmd *cmd)
3520{
3521 const char *val;
3522
3523 if (dut->dev_role != DEVROLE_PCP) {
3524 send_resp(dut, conn, SIGMA_INVALID,
3525 "ErrorCode,Invalid DevRole");
3526 return 0;
3527 }
3528
3529 val = get_param(cmd, "SSID");
3530 if (val) {
3531 if (strlen(val) > sizeof(dut->ap_ssid) - 1) {
3532 send_resp(dut, conn, SIGMA_INVALID,
3533 "ErrorCode,Invalid SSID");
3534 return -1;
3535 }
3536
3537 strncpy(dut->ap_ssid, val, sizeof(dut->ap_ssid));
3538 }
3539
3540 val = get_param(cmd, "CHANNEL");
3541 if (val) {
3542 const char *pos;
3543
3544 dut->ap_channel = atoi(val);
3545 pos = strchr(val, ';');
3546 if (pos) {
3547 pos++;
3548 dut->ap_channel_1 = atoi(pos);
3549 }
3550 }
3551
3552 switch (dut->ap_channel) {
3553 case 1:
3554 case 2:
3555 case 3:
3556 break;
3557 default:
3558 sigma_dut_print(dut, DUT_MSG_ERROR,
3559 "Channel %d is not supported", dut->ap_channel);
3560 send_resp(dut, conn, SIGMA_ERROR,
3561 "Requested channel is not supported");
3562 return -1;
3563 }
3564
3565 val = get_param(cmd, "BCNINT");
3566 if (val)
3567 dut->ap_bcnint = atoi(val);
3568
3569
3570 val = get_param(cmd, "ExtSchIE");
3571 if (val) {
3572 send_resp(dut, conn, SIGMA_ERROR,
3573 "ErrorCode,ExtSchIE is not supported yet");
3574 return -1;
3575 }
3576
3577 val = get_param(cmd, "AllocType");
3578 if (val) {
3579 send_resp(dut, conn, SIGMA_ERROR,
3580 "ErrorCode,AllocType is not supported yet");
3581 return -1;
3582 }
3583
3584 val = get_param(cmd, "PercentBI");
3585 if (val) {
3586 send_resp(dut, conn, SIGMA_ERROR,
3587 "ErrorCode,PercentBI is not supported yet");
3588 return -1;
3589 }
3590
3591 val = get_param(cmd, "CBAPOnly");
3592 if (val) {
3593 send_resp(dut, conn, SIGMA_ERROR,
3594 "ErrorCode,CBAPOnly is not supported yet");
3595 return -1;
3596 }
3597
3598 val = get_param(cmd, "AMPDU");
3599 if (val) {
3600 if (strcasecmp(val, "Enable") == 0)
3601 dut->ap_ampdu = 1;
3602 else if (strcasecmp(val, "Disable") == 0)
3603 dut->ap_ampdu = 2;
3604 else {
3605 send_resp(dut, conn, SIGMA_ERROR,
3606 "ErrorCode,AMPDU value is not Enable nor Disabled");
3607 return -1;
3608 }
3609 }
3610
3611 val = get_param(cmd, "AMSDU");
3612 if (val) {
3613 if (strcasecmp(val, "Enable") == 0)
3614 dut->ap_amsdu = 1;
3615 else if (strcasecmp(val, "Disable") == 0)
3616 dut->ap_amsdu = 2;
3617 }
3618
3619 val = get_param(cmd, "NumMSDU");
3620 if (val) {
3621 send_resp(dut, conn, SIGMA_ERROR,
3622 "ErrorCode, NumMSDU is not supported yet");
3623 return -1;
3624 }
3625
3626 val = get_param(cmd, "ABFTLRang");
3627 if (val) {
3628 sigma_dut_print(dut, DUT_MSG_DEBUG,
3629 "Ignoring ABFTLRang parameter since FW default is greater than 1");
3630 }
3631
3632 if (sta_pcp_start(dut, conn, cmd) < 0) {
3633 send_resp(dut, conn, SIGMA_ERROR,
3634 "ErrorCode, Can't start PCP role");
3635 return -1;
3636 }
3637
3638 return sta_set_60g_common(dut, conn, cmd);
3639}
3640
3641
3642static int sta_set_60g_sta(struct sigma_dut *dut, struct sigma_conn *conn,
3643 struct sigma_cmd *cmd)
3644{
3645 const char *val = get_param(cmd, "DiscoveryMode");
3646
3647 if (dut->dev_role != DEVROLE_STA) {
3648 send_resp(dut, conn, SIGMA_INVALID,
3649 "ErrorCode,Invalid DevRole");
3650 return 0;
3651 }
3652
3653 if (val) {
3654 sigma_dut_print(dut, DUT_MSG_DEBUG, "Discovery: %s", val);
3655 /* Ignore Discovery mode till Driver expose API. */
3656#if 0
3657 if (strcasecmp(val, "1") == 0) {
3658 send_resp(dut, conn, SIGMA_INVALID,
3659 "ErrorCode,DiscoveryMode 1 not supported");
3660 return 0;
3661 }
3662
3663 if (strcasecmp(val, "0") == 0) {
3664 /* OK */
3665 } else {
3666 send_resp(dut, conn, SIGMA_INVALID,
3667 "ErrorCode,DiscoveryMode not supported");
3668 return 0;
3669 }
3670#endif
3671 }
3672
3673 if (start_sta_mode(dut) != 0)
3674 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
3675 return sta_set_60g_common(dut, conn, cmd);
3676}
3677
3678
3679static int cmd_sta_disconnect(struct sigma_dut *dut, struct sigma_conn *conn,
3680 struct sigma_cmd *cmd)
3681{
3682 const char *intf = get_param(cmd, "Interface");
3683 disconnect_station(dut);
3684 /* Try to ignore old scan results to avoid HS 2.0R2 test case failures
3685 * due to cached results. */
3686 wpa_command(intf, "SET ignore_old_scan_res 1");
3687 wpa_command(intf, "BSS_FLUSH");
3688 return 1;
3689}
3690
3691
3692static int cmd_sta_reassoc(struct sigma_dut *dut, struct sigma_conn *conn,
3693 struct sigma_cmd *cmd)
3694{
3695 const char *intf = get_param(cmd, "Interface");
3696 const char *bssid = get_param(cmd, "bssid");
3697 const char *val = get_param(cmd, "CHANNEL");
3698 struct wpa_ctrl *ctrl;
3699 char buf[100];
3700 int res;
3701 int chan = 0;
3702
3703 if (bssid == NULL) {
3704 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Missing bssid "
3705 "argument");
3706 return 0;
3707 }
3708
3709 if (val)
3710 chan = atoi(val);
3711
3712 if (wifi_chip_type != DRIVER_WCN && wifi_chip_type != DRIVER_AR6003) {
3713 /* The current network may be from sta_associate or
3714 * sta_hs2_associate
3715 */
3716 if (set_network(intf, dut->infra_network_id, "bssid", bssid) <
3717 0 ||
3718 set_network(intf, 0, "bssid", bssid) < 0)
3719 return -2;
3720 }
3721
3722 ctrl = open_wpa_mon(intf);
3723 if (ctrl == NULL) {
3724 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to open "
3725 "wpa_supplicant monitor connection");
3726 return -1;
3727 }
3728
3729 if (wifi_chip_type == DRIVER_WCN) {
3730#ifdef ANDROID
3731 if (set_network(intf, dut->infra_network_id, "bssid", "any")
3732 < 0) {
3733 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to set "
3734 "bssid to any during FASTREASSOC");
3735 return -2;
3736 }
3737 res = snprintf(buf, sizeof(buf), "DRIVER FASTREASSOC %s %d",
3738 bssid, chan);
3739 if (res > 0 && res < (int) sizeof(buf))
3740 res = wpa_command(intf, buf);
3741
3742 if (res < 0 || res >= (int) sizeof(buf)) {
3743 send_resp(dut, conn, SIGMA_ERROR,
3744 "errorCode,Failed to run DRIVER FASTREASSOC");
3745 wpa_ctrl_detach(ctrl);
3746 wpa_ctrl_close(ctrl);
3747 return 0;
3748 }
3749#else /* ANDROID */
3750 sigma_dut_print(dut, DUT_MSG_DEBUG,
3751 "Reassoc using iwpriv - skip chan=%d info",
3752 chan);
3753 snprintf(buf, sizeof(buf), "iwpriv %s reassoc", intf);
3754 if (system(buf) != 0) {
3755 sigma_dut_print(dut, DUT_MSG_ERROR, "%s failed", buf);
3756 wpa_ctrl_detach(ctrl);
3757 wpa_ctrl_close(ctrl);
3758 return 0;
3759 }
3760#endif /* ANDROID */
3761 sigma_dut_print(dut, DUT_MSG_INFO,
3762 "sta_reassoc: Run %s successful", buf);
3763 } else if (wpa_command(intf, "REASSOCIATE")) {
3764 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to "
3765 "request reassociation");
3766 wpa_ctrl_detach(ctrl);
3767 wpa_ctrl_close(ctrl);
3768 return 0;
3769 }
3770
3771 res = get_wpa_cli_event(dut, ctrl, "CTRL-EVENT-CONNECTED",
3772 buf, sizeof(buf));
3773
3774 wpa_ctrl_detach(ctrl);
3775 wpa_ctrl_close(ctrl);
3776
3777 if (res < 0) {
3778 sigma_dut_print(dut, DUT_MSG_INFO, "Scan did not complete");
3779 return -1;
3780 }
3781
3782 return 1;
3783}
3784
3785
3786static void hs2_clear_credentials(const char *intf)
3787{
3788 wpa_command(intf, "REMOVE_CRED all");
3789}
3790
3791
3792static int sta_get_parameter_60g(struct sigma_dut *dut, struct sigma_conn *conn,
3793 struct sigma_cmd *cmd)
3794{
3795 char buf[MAX_CMD_LEN];
3796 char bss_list[MAX_CMD_LEN];
3797 const char *parameter = get_param(cmd, "Parameter");
3798
3799 if (parameter == NULL)
3800 return -1;
3801
3802 if (strcasecmp(parameter, "DiscoveredDevList") == 0) {
3803 char *bss_line;
3804 char *bss_id = NULL;
3805 const char *ifname = get_param(cmd, "Interface");
3806
3807 if (ifname == NULL) {
3808 sigma_dut_print(dut, DUT_MSG_INFO,
3809 "For get DiscoveredDevList need Interface name.");
3810 return -1;
3811 }
3812
3813 /*
3814 * Use "BSS RANGE=ALL MASK=0x2" which provides a list
3815 * of BSSIDs in "bssid=<BSSID>\n"
3816 */
3817 if (wpa_command_resp(ifname, "BSS RANGE=ALL MASK=0x2",
3818 bss_list,
3819 sizeof(bss_list)) < 0) {
3820 sigma_dut_print(dut, DUT_MSG_ERROR,
3821 "Failed to get bss list");
3822 return -1;
3823 }
3824
3825 sigma_dut_print(dut, DUT_MSG_DEBUG,
3826 "bss list for ifname:%s is:%s",
3827 ifname, bss_list);
3828
3829 snprintf(buf, sizeof(buf), "DeviceList");
3830 bss_line = strtok(bss_list, "\n");
3831 while (bss_line) {
3832 if (sscanf(bss_line, "bssid=%ms", &bss_id) > 0 &&
3833 bss_id) {
3834 int len;
3835
3836 len = snprintf(buf + strlen(buf),
3837 sizeof(buf) - strlen(buf),
3838 ",%s", bss_id);
3839 free(bss_id);
3840 bss_id = NULL;
3841 if (len < 0) {
3842 sigma_dut_print(dut,
3843 DUT_MSG_ERROR,
3844 "Failed to read BSSID");
3845 send_resp(dut, conn, SIGMA_ERROR,
3846 "ErrorCode,Failed to read BSS ID");
3847 return 0;
3848 }
3849
3850 if ((size_t) len >= sizeof(buf) - strlen(buf)) {
3851 sigma_dut_print(dut,
3852 DUT_MSG_ERROR,
3853 "Response buf too small for list");
3854 send_resp(dut, conn,
3855 SIGMA_ERROR,
3856 "ErrorCode,Response buf too small for list");
3857 return 0;
3858 }
3859 }
3860
3861 bss_line = strtok(NULL, "\n");
3862 }
3863
3864 sigma_dut_print(dut, DUT_MSG_INFO, "DiscoveredDevList is %s",
3865 buf);
3866 send_resp(dut, conn, SIGMA_COMPLETE, buf);
3867 return 0;
3868 }
3869
3870 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Unsupported parameter");
3871 return 0;
3872}
3873
3874
3875static int cmd_sta_get_parameter(struct sigma_dut *dut, struct sigma_conn *conn,
3876 struct sigma_cmd *cmd)
3877{
3878 const char *program = get_param(cmd, "Program");
3879
3880 if (program == NULL)
3881 return -1;
3882
3883 if (strcasecmp(program, "P2PNFC") == 0)
3884 return p2p_cmd_sta_get_parameter(dut, conn, cmd);
3885
3886 if (strcasecmp(program, "60ghz") == 0)
3887 return sta_get_parameter_60g(dut, conn, cmd);
3888
3889#ifdef ANDROID_NAN
3890 if (strcasecmp(program, "NAN") == 0)
3891 return nan_cmd_sta_exec_action(dut, conn, cmd);
3892#endif /* ANDROID_NAN */
3893
3894 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Unsupported parameter");
3895 return 0;
3896}
3897
3898
3899static void sta_reset_default_ath(struct sigma_dut *dut, const char *intf,
3900 const char *type)
3901{
3902 char buf[100];
3903
3904 if (dut->program == PROGRAM_VHT) {
3905 snprintf(buf, sizeof(buf), "iwpriv %s chwidth 2", intf);
3906 if (system(buf) != 0) {
3907 sigma_dut_print(dut, DUT_MSG_ERROR,
3908 "iwpriv %s chwidth failed", intf);
3909 }
3910
3911 snprintf(buf, sizeof(buf), "iwpriv %s mode 11ACVHT80", intf);
3912 if (system(buf) != 0) {
3913 sigma_dut_print(dut, DUT_MSG_ERROR,
3914 "iwpriv %s mode 11ACVHT80 failed",
3915 intf);
3916 }
3917
3918 snprintf(buf, sizeof(buf), "iwpriv %s vhtmcs -1", intf);
3919 if (system(buf) != 0) {
3920 sigma_dut_print(dut, DUT_MSG_ERROR,
3921 "iwpriv %s vhtmcs -1 failed", intf);
3922 }
3923 }
3924
3925 if (dut->program == PROGRAM_HT) {
3926 snprintf(buf, sizeof(buf), "iwpriv %s chwidth 0", intf);
3927 if (system(buf) != 0) {
3928 sigma_dut_print(dut, DUT_MSG_ERROR,
3929 "iwpriv %s chwidth failed", intf);
3930 }
3931
3932 snprintf(buf, sizeof(buf), "iwpriv %s mode 11naht40", intf);
3933 if (system(buf) != 0) {
3934 sigma_dut_print(dut, DUT_MSG_ERROR,
3935 "iwpriv %s mode 11naht40 failed",
3936 intf);
3937 }
3938
3939 snprintf(buf, sizeof(buf), "iwpriv %s set11NRates 0", intf);
3940 if (system(buf) != 0) {
3941 sigma_dut_print(dut, DUT_MSG_ERROR,
3942 "iwpriv set11NRates failed");
3943 }
3944 }
3945
3946 if (dut->program == PROGRAM_VHT || dut->program == PROGRAM_HT) {
3947 snprintf(buf, sizeof(buf), "iwpriv %s powersave 0", intf);
3948 if (system(buf) != 0) {
3949 sigma_dut_print(dut, DUT_MSG_ERROR,
3950 "disabling powersave failed");
3951 }
3952
3953 /* Reset CTS width */
3954 snprintf(buf, sizeof(buf), "wifitool %s beeliner_fw_test 54 0",
3955 intf);
3956 if (system(buf) != 0) {
3957 sigma_dut_print(dut, DUT_MSG_ERROR,
3958 "wifitool %s beeliner_fw_test 54 0 failed",
3959 intf);
3960 }
3961
3962 /* Enable Dynamic Bandwidth signalling by default */
3963 snprintf(buf, sizeof(buf), "iwpriv %s cwmenable 1", intf);
3964 if (system(buf) != 0) {
3965 sigma_dut_print(dut, DUT_MSG_ERROR,
3966 "iwpriv %s cwmenable 1 failed", intf);
3967 }
3968
3969 snprintf(buf, sizeof(buf), "iwconfig %s rts 2347", intf);
3970 if (system(buf) != 0) {
3971 sigma_dut_print(dut, DUT_MSG_ERROR,
3972 "iwpriv rts failed");
3973 }
3974 }
3975
3976 if (type && strcasecmp(type, "Testbed") == 0) {
3977 dut->testbed_flag_txsp = 1;
3978 dut->testbed_flag_rxsp = 1;
3979 /* STA has to set spatial stream to 2 per Appendix H */
3980 snprintf(buf, sizeof(buf), "iwpriv %s vht_mcsmap 0xfff0", intf);
3981 if (system(buf) != 0) {
3982 sigma_dut_print(dut, DUT_MSG_ERROR,
3983 "iwpriv vht_mcsmap failed");
3984 }
3985
3986 /* Disable LDPC per Appendix H */
3987 snprintf(buf, sizeof(buf), "iwpriv %s ldpc 0", intf);
3988 if (system(buf) != 0) {
3989 sigma_dut_print(dut, DUT_MSG_ERROR,
3990 "iwpriv %s ldpc 0 failed", intf);
3991 }
3992
3993 snprintf(buf, sizeof(buf), "iwpriv %s amsdu 1", intf);
3994 if (system(buf) != 0) {
3995 sigma_dut_print(dut, DUT_MSG_ERROR,
3996 "iwpriv amsdu failed");
3997 }
3998
3999 /* TODO: Disable STBC 2x1 transmit and receive */
4000 snprintf(buf, sizeof(buf), "iwpriv %s tx_stbc 0", intf);
4001 if (system(buf) != 0) {
4002 sigma_dut_print(dut, DUT_MSG_ERROR,
4003 "Disable tx_stbc 0 failed");
4004 }
4005
4006 snprintf(buf, sizeof(buf), "iwpriv %s rx_stbc 0", intf);
4007 if (system(buf) != 0) {
4008 sigma_dut_print(dut, DUT_MSG_ERROR,
4009 "Disable rx_stbc 0 failed");
4010 }
4011
4012 /* STA has to disable Short GI per Appendix H */
4013 snprintf(buf, sizeof(buf), "iwpriv %s shortgi 0", intf);
4014 if (system(buf) != 0) {
4015 sigma_dut_print(dut, DUT_MSG_ERROR,
4016 "iwpriv %s shortgi 0 failed", intf);
4017 }
4018 }
4019
4020 if (type && strcasecmp(type, "DUT") == 0) {
4021 snprintf(buf, sizeof(buf), "iwpriv %s nss 3", intf);
4022 if (system(buf) != 0) {
4023 sigma_dut_print(dut, DUT_MSG_ERROR,
4024 "iwpriv %s nss 3 failed", intf);
4025 }
4026
4027 snprintf(buf, sizeof(buf), "iwpriv %s shortgi 1", intf);
4028 if (system(buf) != 0) {
4029 sigma_dut_print(dut, DUT_MSG_ERROR,
4030 "iwpriv %s shortgi 1 failed", intf);
4031 }
4032 }
4033}
4034
4035
4036static int cmd_sta_reset_default(struct sigma_dut *dut,
4037 struct sigma_conn *conn,
4038 struct sigma_cmd *cmd)
4039{
4040 int cmd_sta_p2p_reset(struct sigma_dut *dut, struct sigma_conn *conn,
4041 struct sigma_cmd *cmd);
4042 const char *intf = get_param(cmd, "Interface");
4043 const char *type;
4044
4045 dut->program = sigma_program_to_enum(get_param(cmd, "prog"));
4046 dut->device_type = STA_unknown;
4047 type = get_param(cmd, "type");
4048 if (type && strcasecmp(type, "Testbed") == 0)
4049 dut->device_type = STA_testbed;
4050 if (type && strcasecmp(type, "DUT") == 0)
4051 dut->device_type = STA_dut;
4052
4053 if (dut->program == PROGRAM_TDLS) {
4054 /* Clear TDLS testing mode */
4055 wpa_command(intf, "SET tdls_disabled 0");
4056 wpa_command(intf, "SET tdls_testing 0");
4057 dut->no_tpk_expiration = 0;
4058 }
4059
4060 switch (get_driver_type()) {
4061 case DRIVER_ATHEROS:
4062 sta_reset_default_ath(dut, intf, type);
4063 break;
4064 default:
4065 break;
4066 }
4067
4068#ifdef ANDROID_NAN
4069 if (dut->program == PROGRAM_NAN)
4070 nan_cmd_sta_reset_default(dut, conn, cmd);
4071#endif /* ANDROID_NAN */
4072
4073 if (dut->program == PROGRAM_HS2_R2) {
4074 unlink("SP/wi-fi.org/pps.xml");
4075 if (system("rm -r SP/*") != 0) {
4076 }
4077 unlink("next-client-cert.pem");
4078 unlink("next-client-key.pem");
4079 }
4080
4081 if (dut->program == PROGRAM_60GHZ) {
4082 const char *dev_role = get_param(cmd, "DevRole");
4083
4084 if (!dev_role) {
4085 send_resp(dut, conn, SIGMA_ERROR,
4086 "errorCode,Missing DevRole argument");
4087 return 0;
4088 }
4089
4090 if (strcasecmp(dev_role, "STA") == 0)
4091 dut->dev_role = DEVROLE_STA;
4092 else if (strcasecmp(dev_role, "PCP") == 0)
4093 dut->dev_role = DEVROLE_PCP;
4094 else {
4095 send_resp(dut, conn, SIGMA_ERROR,
4096 "errorCode,Unknown DevRole");
4097 return 0;
4098 }
4099
4100 if (dut->device_type == STA_unknown) {
4101 sigma_dut_print(dut, DUT_MSG_ERROR,
4102 "Device type is not STA testbed or DUT");
4103 send_resp(dut, conn, SIGMA_ERROR,
4104 "errorCode,Unknown device type");
4105 return 0;
4106 }
4107 }
4108
4109 wpa_command(intf, "WPS_ER_STOP");
4110 wpa_command(intf, "FLUSH");
4111 wpa_command(intf, "SET radio_disabled 0");
4112
4113 if (dut->tmp_mac_addr && dut->set_macaddr) {
4114 dut->tmp_mac_addr = 0;
4115 if (system(dut->set_macaddr) != 0) {
4116 sigma_dut_print(dut, DUT_MSG_INFO, "Failed to clear "
4117 "temporary MAC address");
4118 }
4119 }
4120
4121 set_ps(intf, dut, 0);
4122
4123 if (dut->program == PROGRAM_HS2 || dut->program == PROGRAM_HS2_R2) {
4124 wpa_command(intf, "SET interworking 1");
4125 wpa_command(intf, "SET hs20 1");
4126 }
4127
4128 if (dut->program == PROGRAM_HS2_R2) {
4129 wpa_command(intf, "SET pmf 1");
4130 } else {
4131 wpa_command(intf, "SET pmf 0");
4132 }
4133
4134 hs2_clear_credentials(intf);
4135 wpa_command(intf, "SET hessid 00:00:00:00:00:00");
4136 wpa_command(intf, "SET access_network_type 15");
4137
4138 static_ip_file(0, NULL, NULL, NULL);
4139 kill_dhcp_client(dut, intf);
4140 clear_ip_addr(dut, intf);
4141
4142 dut->er_oper_performed = 0;
4143 dut->er_oper_bssid[0] = '\0';
4144
4145 return cmd_sta_p2p_reset(dut, conn, cmd);
4146}
4147
4148
4149static int cmd_sta_get_events(struct sigma_dut *dut, struct sigma_conn *conn,
4150 struct sigma_cmd *cmd)
4151{
4152 const char *program = get_param(cmd, "Program");
4153
4154 if (program == NULL)
4155 return -1;
4156#ifdef ANDROID_NAN
4157 if (strcasecmp(program, "NAN") == 0)
4158 return nan_cmd_sta_get_events(dut, conn, cmd);
4159#endif /* ANDROID_NAN */
4160 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Unsupported parameter");
4161 return 0;
4162}
4163
4164
4165static int cmd_sta_exec_action(struct sigma_dut *dut, struct sigma_conn *conn,
4166 struct sigma_cmd *cmd)
4167{
4168 const char *program = get_param(cmd, "Prog");
4169
4170 if (program == NULL)
4171 return -1;
4172#ifdef ANDROID_NAN
4173 if (strcasecmp(program, "NAN") == 0)
4174 return nan_cmd_sta_exec_action(dut, conn, cmd);
4175#endif /* ANDROID_NAN */
4176 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Unsupported parameter");
4177 return 0;
4178}
4179
4180
4181static int cmd_sta_set_11n(struct sigma_dut *dut, struct sigma_conn *conn,
4182 struct sigma_cmd *cmd)
4183{
4184 const char *intf = get_param(cmd, "Interface");
4185 const char *val, *mcs32, *rate;
4186
4187 val = get_param(cmd, "GREENFIELD");
4188 if (val) {
4189 if (strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0) {
4190 /* Enable GD */
4191 send_resp(dut, conn, SIGMA_ERROR,
4192 "ErrorCode,GF not supported");
4193 return 0;
4194 }
4195 }
4196
4197 val = get_param(cmd, "SGI20");
4198 if (val) {
4199 switch (get_driver_type()) {
4200 case DRIVER_ATHEROS:
4201 ath_sta_set_sgi(dut, intf, val);
4202 break;
4203 default:
4204 send_resp(dut, conn, SIGMA_ERROR,
4205 "ErrorCode,SGI20 not supported");
4206 return 0;
4207 }
4208 }
4209
4210 mcs32 = get_param(cmd, "MCS32"); /* HT Duplicate Mode Enable/Disable */
4211 rate = get_param(cmd, "MCS_FIXEDRATE"); /* Fixed MCS rate (0..31) */
4212 if (mcs32 && rate) {
4213 /* TODO */
4214 send_resp(dut, conn, SIGMA_ERROR,
4215 "ErrorCode,MCS32,MCS_FIXEDRATE not supported");
4216 return 0;
4217 } else if (mcs32 && !rate) {
4218 /* TODO */
4219 send_resp(dut, conn, SIGMA_ERROR,
4220 "ErrorCode,MCS32 not supported");
4221 return 0;
4222 } else if (!mcs32 && rate) {
4223 switch (get_driver_type()) {
4224 case DRIVER_ATHEROS:
4225 ath_sta_set_11nrates(dut, intf, rate);
4226 break;
4227 default:
4228 send_resp(dut, conn, SIGMA_ERROR,
4229 "ErrorCode,MCS32_FIXEDRATE not supported");
4230 return 0;
4231 }
4232 }
4233
4234 return cmd_sta_set_wireless_common(intf, dut, conn, cmd);
4235}
4236
4237
4238static int cmd_sta_set_wireless_vht(struct sigma_dut *dut,
4239 struct sigma_conn *conn,
4240 struct sigma_cmd *cmd)
4241{
4242 const char *intf = get_param(cmd, "Interface");
4243 const char *val;
4244 char buf[30];
4245 int tkip = -1;
4246 int wep = -1;
4247
4248 val = get_param(cmd, "SGI80");
4249 if (val) {
4250 int sgi80;
4251
4252 sgi80 = strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0;
4253 snprintf(buf, sizeof(buf), "iwpriv %s shortgi %d", intf, sgi80);
4254 if (system(buf) != 0) {
4255 sigma_dut_print(dut, DUT_MSG_ERROR,
4256 "iwpriv shortgi failed");
4257 }
4258 }
4259
4260 val = get_param(cmd, "TxBF");
4261 if (val && (strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0)) {
4262 snprintf(buf, sizeof(buf), "iwpriv %s vhtsubfee 1", intf);
4263 if (system(buf) != 0) {
4264 sigma_dut_print(dut, DUT_MSG_ERROR,
4265 "iwpriv vhtsubfee failed");
4266 }
4267 snprintf(buf, sizeof(buf), "iwpriv %s vhtsubfer 1", intf);
4268 if (system(buf) != 0) {
4269 sigma_dut_print(dut, DUT_MSG_ERROR,
4270 "iwpriv vhtsubfer failed");
4271 }
4272 }
4273
4274 val = get_param(cmd, "MU_TxBF");
4275 if (val && (strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0)) {
4276 switch (get_driver_type()) {
4277 case DRIVER_ATHEROS:
4278 ath_sta_set_txsp_stream(dut, intf, "1SS");
4279 ath_sta_set_rxsp_stream(dut, intf, "1SS");
4280 case DRIVER_WCN:
4281 if (wcn_sta_set_sp_stream(dut, intf, "1SS") < 0) {
4282 send_resp(dut, conn, SIGMA_ERROR,
4283 "ErrorCode,Failed to set RX/TXSP_STREAM");
4284 return 0;
4285 }
4286 default:
4287 sigma_dut_print(dut, DUT_MSG_ERROR,
4288 "Setting SP_STREAM not supported");
4289 break;
4290 }
4291 snprintf(buf, sizeof(buf), "iwpriv %s vhtmubfee 1", intf);
4292 if (system(buf) != 0) {
4293 sigma_dut_print(dut, DUT_MSG_ERROR,
4294 "iwpriv vhtmubfee failed");
4295 }
4296 snprintf(buf, sizeof(buf), "iwpriv %s vhtmubfer 1", intf);
4297 if (system(buf) != 0) {
4298 sigma_dut_print(dut, DUT_MSG_ERROR,
4299 "iwpriv vhtmubfer failed");
4300 }
4301 }
4302
4303 val = get_param(cmd, "LDPC");
4304 if (val) {
4305 int ldpc;
4306
4307 ldpc = strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0;
4308 snprintf(buf, sizeof(buf), "iwpriv %s ldpc %d", intf, ldpc);
4309 if (system(buf) != 0) {
4310 sigma_dut_print(dut, DUT_MSG_ERROR,
4311 "iwpriv ldpc failed");
4312 }
4313 }
4314
4315 val = get_param(cmd, "opt_md_notif_ie");
4316 if (val) {
4317 char *result = NULL;
4318 char delim[] = ";";
4319 char token[30];
4320 int value, config_val = 0;
4321
4322 strncpy(token, val, sizeof(token));
4323 token[sizeof(token) - 1] = '\0';
4324 result = strtok(token, delim);
4325
4326 /* Extract the NSS information */
4327 if (result) {
4328 value = atoi(result);
4329 switch (value) {
4330 case 1:
4331 config_val = 1;
4332 break;
4333 case 2:
4334 config_val = 3;
4335 break;
4336 case 3:
4337 config_val = 7;
4338 break;
4339 case 4:
4340 config_val = 15;
4341 break;
4342 default:
4343 config_val = 3;
4344 break;
4345 }
4346
4347 snprintf(buf, sizeof(buf), "iwpriv %s rxchainmask %d",
4348 intf, config_val);
4349 if (system(buf) != 0) {
4350 sigma_dut_print(dut, DUT_MSG_ERROR,
4351 "iwpriv rxchainmask failed");
4352 }
4353
4354 snprintf(buf, sizeof(buf), "iwpriv %s txchainmask %d",
4355 intf, config_val);
4356 if (system(buf) != 0) {
4357 sigma_dut_print(dut, DUT_MSG_ERROR,
4358 "iwpriv txchainmask failed");
4359 }
4360 }
4361
4362 /* Extract the channel width information */
4363 result = strtok(NULL, delim);
4364 if (result) {
4365 value = atoi(result);
4366 switch (value) {
4367 case 20:
4368 config_val = 0;
4369 break;
4370 case 40:
4371 config_val = 1;
4372 break;
4373 case 80:
4374 config_val = 2;
4375 break;
4376 case 160:
4377 config_val = 3;
4378 break;
4379 default:
4380 config_val = 2;
4381 break;
4382 }
4383
4384 dut->chwidth = config_val;
4385
4386 snprintf(buf, sizeof(buf), "iwpriv %s chwidth %d",
4387 intf, config_val);
4388 if (system(buf) != 0) {
4389 sigma_dut_print(dut, DUT_MSG_ERROR,
4390 "iwpriv chwidth failed");
4391 }
4392 }
4393
4394 snprintf(buf, sizeof(buf), "iwpriv %s opmode_notify 1", intf);
4395 if (system(buf) != 0) {
4396 sigma_dut_print(dut, DUT_MSG_ERROR,
4397 "iwpriv opmode_notify failed");
4398 }
4399 }
4400
4401 val = get_param(cmd, "nss_mcs_cap");
4402 if (val) {
4403 int nss, mcs;
4404 char token[20];
4405 char *result = NULL;
4406 unsigned int vht_mcsmap = 0;
4407
4408 strncpy(token, val, sizeof(token));
4409 token[sizeof(token) - 1] = '\0';
4410 result = strtok(token, ";");
4411 nss = atoi(result);
4412
4413 snprintf(buf, sizeof(buf), "iwpriv %s nss %d", intf, nss);
4414 if (system(buf) != 0) {
4415 sigma_dut_print(dut, DUT_MSG_ERROR,
4416 "iwpriv nss failed");
4417 }
4418
4419 result = strtok(NULL, ";");
4420 if (result == NULL) {
4421 sigma_dut_print(dut, DUT_MSG_ERROR,
4422 "VHTMCS NOT SPECIFIED!");
4423 return 0;
4424 }
4425 result = strtok(result, "-");
4426 result = strtok(NULL, "-");
4427 mcs = atoi(result);
4428
4429 snprintf(buf, sizeof(buf), "iwpriv %s vhtmcs %d", intf, mcs);
4430 if (system(buf) != 0) {
4431 sigma_dut_print(dut, DUT_MSG_ERROR,
4432 "iwpriv mcs failed");
4433 }
4434
4435 switch (nss) {
4436 case 1:
4437 switch (mcs) {
4438 case 7:
4439 vht_mcsmap = 0xfffc;
4440 break;
4441 case 8:
4442 vht_mcsmap = 0xfffd;
4443 break;
4444 case 9:
4445 vht_mcsmap = 0xfffe;
4446 break;
4447 default:
4448 vht_mcsmap = 0xfffe;
4449 break;
4450 }
4451 break;
4452 case 2:
4453 switch (mcs) {
4454 case 7:
4455 vht_mcsmap = 0xfff0;
4456 break;
4457 case 8:
4458 vht_mcsmap = 0xfff5;
4459 break;
4460 case 9:
4461 vht_mcsmap = 0xfffa;
4462 break;
4463 default:
4464 vht_mcsmap = 0xfffa;
4465 break;
4466 }
4467 break;
4468 case 3:
4469 switch (mcs) {
4470 case 7:
4471 vht_mcsmap = 0xffc0;
4472 break;
4473 case 8:
4474 vht_mcsmap = 0xffd5;
4475 break;
4476 case 9:
4477 vht_mcsmap = 0xffea;
4478 break;
4479 default:
4480 vht_mcsmap = 0xffea;
4481 break;
4482 }
4483 break;
4484 default:
4485 vht_mcsmap = 0xffea;
4486 break;
4487 }
4488 snprintf(buf, sizeof(buf), "iwpriv %s vht_mcsmap 0x%04x",
4489 intf, vht_mcsmap);
4490 if (system(buf) != 0) {
4491 sigma_dut_print(dut, DUT_MSG_ERROR,
4492 "iwpriv vht_mcsmap failed");
4493 }
4494 }
4495
4496 /* UNSUPPORTED: val = get_param(cmd, "Tx_lgi_rate"); */
4497
4498 val = get_param(cmd, "Vht_tkip");
4499 if (val)
4500 tkip = strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0;
4501
4502 val = get_param(cmd, "Vht_wep");
4503 if (val)
4504 wep = strcmp(val, "1") == 0 || strcasecmp(val, "Enable") == 0;
4505
4506 if (tkip != -1 || wep != -1) {
4507 if ((tkip == 1 && wep != 0) || (wep == 1 && tkip != 0)) {
4508 snprintf(buf, sizeof(buf), "iwpriv %s htweptkip 1",
4509 intf);
4510 } else if ((tkip == 0 && wep != 1) || (wep == 0 && tkip != 1)) {
4511 snprintf(buf, sizeof(buf), "iwpriv %s htweptkip 0",
4512 intf);
4513 } else {
4514 sigma_dut_print(dut, DUT_MSG_ERROR,
4515 "ErrorCode,mixed mode of VHT TKIP/WEP not supported");
4516 return 0;
4517 }
4518
4519 if (system(buf) != 0) {
4520 sigma_dut_print(dut, DUT_MSG_ERROR,
4521 "iwpriv htweptkip failed");
4522 }
4523 }
4524
4525 val = get_param(cmd, "txBandwidth");
4526 if (val) {
4527 switch (get_driver_type()) {
4528 case DRIVER_ATHEROS:
4529 if (ath_set_width(dut, conn, intf, val) < 0) {
4530 send_resp(dut, conn, SIGMA_ERROR,
4531 "ErrorCode,Failed to set txBandwidth");
4532 return 0;
4533 }
4534 break;
4535 default:
4536 sigma_dut_print(dut, DUT_MSG_ERROR,
4537 "Setting txBandwidth not supported");
4538 break;
4539 }
4540 }
4541
4542 return cmd_sta_set_wireless_common(intf, dut, conn, cmd);
4543}
4544
4545
4546static int sta_set_wireless_60g(struct sigma_dut *dut,
4547 struct sigma_conn *conn,
4548 struct sigma_cmd *cmd)
4549{
4550 const char *dev_role = get_param(cmd, "DevRole");
4551
4552 if (!dev_role) {
4553 send_resp(dut, conn, SIGMA_INVALID,
4554 "ErrorCode,DevRole not specified");
4555 return 0;
4556 }
4557
4558 if (strcasecmp(dev_role, "PCP") == 0)
4559 return sta_set_60g_pcp(dut, conn, cmd);
4560 if (strcasecmp(dev_role, "STA") == 0)
4561 return sta_set_60g_sta(dut, conn, cmd);
4562 send_resp(dut, conn, SIGMA_INVALID,
4563 "ErrorCode,DevRole not supported");
4564 return 0;
4565}
4566
4567
4568static int cmd_sta_set_wireless(struct sigma_dut *dut, struct sigma_conn *conn,
4569 struct sigma_cmd *cmd)
4570{
4571 const char *val;
4572
4573 val = get_param(cmd, "Program");
4574 if (val) {
4575 if (strcasecmp(val, "11n") == 0)
4576 return cmd_sta_set_11n(dut, conn, cmd);
4577 if (strcasecmp(val, "VHT") == 0)
4578 return cmd_sta_set_wireless_vht(dut, conn, cmd);
4579 if (strcasecmp(val, "60ghz") == 0)
4580 return sta_set_wireless_60g(dut, conn, cmd);
4581 send_resp(dut, conn, SIGMA_ERROR,
4582 "ErrorCode,Program value not supported");
4583 } else {
4584 send_resp(dut, conn, SIGMA_ERROR,
4585 "ErrorCode,Program argument not available");
4586 }
4587
4588 return 0;
4589}
4590
4591
4592static void ath_sta_inject_frame(struct sigma_dut *dut, const char *intf,
4593 int tid)
4594{
4595 char buf[100];
4596 int tid_to_dscp [] = { 0x00, 0x20, 0x40, 0x60, 0x80, 0xa0, 0xc0, 0xe0 };
4597
4598 /*
4599 * Two ways to ensure that addba request with a
4600 * non zero TID could be sent out. EV 117296
4601 */
4602 snprintf(buf, sizeof(buf),
4603 "ping -c 8 -Q %d `arp -a | grep wlan0 | awk '{print $2}' | tr -d '()'`",
4604 tid);
4605 if (system(buf) != 0) {
4606 sigma_dut_print(dut, DUT_MSG_ERROR,
4607 "Ping did not send out");
4608 }
4609
4610 snprintf(buf, sizeof(buf),
4611 "iwconfig %s | grep Access | awk '{print $6}' > %s",
4612 intf, VI_QOS_TMP_FILE);
4613 if (system(buf) != 0)
4614 return;
4615
4616 snprintf(buf, sizeof(buf),
4617 "ifconfig %s | grep HWaddr | cut -b 39-56 >> %s",
4618 intf, VI_QOS_TMP_FILE);
4619 if (system(buf) != 0)
4620 sigma_dut_print(dut, DUT_MSG_ERROR, "HWaddr matching failed");
4621
4622 snprintf(buf,sizeof(buf), "sed -n '3,$p' %s >> %s",
4623 VI_QOS_REFFILE, VI_QOS_TMP_FILE);
4624 if (system(buf) != 0) {
4625 sigma_dut_print(dut, DUT_MSG_ERROR,
4626 "VI_QOS_TEMP_FILE generation error failed");
4627 }
4628 snprintf(buf, sizeof(buf), "sed '5 c %x' %s > %s",
4629 tid_to_dscp[tid], VI_QOS_TMP_FILE, VI_QOS_FILE);
4630 if (system(buf) != 0) {
4631 sigma_dut_print(dut, DUT_MSG_ERROR,
4632 "VI_QOS_FILE generation failed");
4633 }
4634
4635 snprintf(buf, sizeof(buf), "sed '5 c %x' %s > %s",
4636 tid_to_dscp[tid], VI_QOS_TMP_FILE, VI_QOS_FILE);
4637 if (system(buf) != 0) {
4638 sigma_dut_print(dut, DUT_MSG_ERROR,
4639 "VI_QOS_FILE generation failed");
4640 }
4641
4642 snprintf(buf, sizeof(buf), "ethinject %s %s", intf, VI_QOS_FILE);
4643 if (system(buf) != 0) {
4644 }
4645}
4646
4647
4648static int ath_sta_send_addba(struct sigma_dut *dut, struct sigma_conn *conn,
4649 struct sigma_cmd *cmd)
4650{
4651 const char *intf = get_param(cmd, "Interface");
4652 const char *val;
4653 int tid = 0;
4654 char buf[100];
4655
4656 val = get_param(cmd, "TID");
4657 if (val) {
4658 tid = atoi(val);
4659 if (tid)
4660 ath_sta_inject_frame(dut, intf, tid);
4661 }
4662
4663 /* Command sequence for ADDBA request on Peregrine based devices */
4664 snprintf(buf, sizeof(buf), "iwpriv %s setaddbaoper 1", intf);
4665 if (system(buf) != 0) {
4666 sigma_dut_print(dut, DUT_MSG_ERROR,
4667 "iwpriv setaddbaoper failed");
4668 }
4669
4670 snprintf(buf, sizeof(buf), "wifitool %s senddelba 1 %d 1 4", intf, tid);
4671 if (system(buf) != 0) {
4672 sigma_dut_print(dut, DUT_MSG_ERROR,
4673 "wifitool senddelba failed");
4674 }
4675
4676 snprintf(buf, sizeof(buf), "wifitool %s sendaddba 1 %d 64", intf, tid);
4677 if (system(buf) != 0) {
4678 sigma_dut_print(dut, DUT_MSG_ERROR,
4679 "wifitool sendaddba failed");
4680 }
4681
4682 /* UNSUPPORTED: val = get_param(cmd, "Dest_mac"); */
4683
4684 return 1;
4685}
4686
4687
4688static int send_addba_60g(struct sigma_dut *dut, struct sigma_conn *conn,
4689 struct sigma_cmd *cmd)
4690{
4691 const char *val;
4692 int tid = 0;
4693 char buf[100];
4694
4695 val = get_param(cmd, "TID");
4696 if (val) {
4697 tid = atoi(val);
4698 if (tid != 0) {
4699 sigma_dut_print(dut, DUT_MSG_ERROR,
4700 "Ignore TID %d for send_addba use TID 0 for 60g since only 0 required on TX",
4701 tid);
4702 }
4703 }
4704
4705 val = get_param(cmd, "Dest_mac");
4706 if (!val) {
4707 sigma_dut_print(dut, DUT_MSG_ERROR,
4708 "Currently not supporting addba for 60G without Dest_mac");
4709 return SIGMA_DUT_ERROR_CALLER_SEND_STATUS;
4710 }
4711
4712 snprintf(buf, sizeof(buf), "wil6210_addba_req.py %s %d",
4713 val, dut->back_rcv_buf);
4714 sigma_dut_print(dut, DUT_MSG_INFO, "Run: %s", buf);
4715
4716 if (system(buf) != 0) {
4717 sigma_dut_print(dut, DUT_MSG_ERROR,
4718 "Failed to send addba");
4719 return -1;
4720 }
4721
4722 return 1;
4723}
4724
4725
4726static int cmd_sta_send_addba(struct sigma_dut *dut, struct sigma_conn *conn,
4727 struct sigma_cmd *cmd)
4728{
4729 switch (get_driver_type()) {
4730 case DRIVER_ATHEROS:
4731 return ath_sta_send_addba(dut, conn, cmd);
4732 case DRIVER_WIL6210:
4733 return send_addba_60g(dut, conn, cmd);
4734 default:
4735 /*
4736 * There is no driver specific implementation for other drivers.
4737 * Ignore the command and report COMPLETE since the following
4738 * throughput test operation will end up sending ADDBA anyway.
4739 */
4740 return 1;
4741 }
4742}
4743
4744
4745int inject_eth_frame(int s, const void *data, size_t len,
4746 unsigned short ethtype, char *dst, char *src)
4747{
4748 struct iovec iov[4] = {
4749 {
4750 .iov_base = dst,
4751 .iov_len = ETH_ALEN,
4752 },
4753 {
4754 .iov_base = src,
4755 .iov_len = ETH_ALEN,
4756 },
4757 {
4758 .iov_base = &ethtype,
4759 .iov_len = sizeof(unsigned short),
4760 },
4761 {
4762 .iov_base = (void *) data,
4763 .iov_len = len,
4764 }
4765 };
4766 struct msghdr msg = {
4767 .msg_name = NULL,
4768 .msg_namelen = 0,
4769 .msg_iov = iov,
4770 .msg_iovlen = 4,
4771 .msg_control = NULL,
4772 .msg_controllen = 0,
4773 .msg_flags = 0,
4774 };
4775
4776 return sendmsg(s, &msg, 0);
4777}
4778
4779#if defined(__linux__) || defined(__QNXNTO__)
4780
4781int inject_frame(int s, const void *data, size_t len, int encrypt)
4782{
4783#define IEEE80211_RADIOTAP_F_WEP 0x04
4784#define IEEE80211_RADIOTAP_F_FRAG 0x08
4785 unsigned char rtap_hdr[] = {
4786 0x00, 0x00, /* radiotap version */
4787 0x0e, 0x00, /* radiotap length */
4788 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
4789 IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
4790 0x00, /* padding */
4791 0x00, 0x00, /* RX and TX flags to indicate that */
4792 0x00, 0x00, /* this is the injected frame directly */
4793 };
4794 struct iovec iov[2] = {
4795 {
4796 .iov_base = &rtap_hdr,
4797 .iov_len = sizeof(rtap_hdr),
4798 },
4799 {
4800 .iov_base = (void *) data,
4801 .iov_len = len,
4802 }
4803 };
4804 struct msghdr msg = {
4805 .msg_name = NULL,
4806 .msg_namelen = 0,
4807 .msg_iov = iov,
4808 .msg_iovlen = 2,
4809 .msg_control = NULL,
4810 .msg_controllen = 0,
4811 .msg_flags = 0,
4812 };
4813
4814 if (encrypt)
4815 rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
4816
4817 return sendmsg(s, &msg, 0);
4818}
4819
4820
4821int open_monitor(const char *ifname)
4822{
4823#ifdef __QNXNTO__
4824 struct sockaddr_dl ll;
4825 int s;
4826
4827 memset(&ll, 0, sizeof(ll));
4828 ll.sdl_family = AF_LINK;
4829 ll.sdl_index = if_nametoindex(ifname);
4830 if (ll.sdl_index == 0) {
4831 perror("if_nametoindex");
4832 return -1;
4833 }
4834 s = socket(PF_INET, SOCK_RAW, 0);
4835#else /* __QNXNTO__ */
4836 struct sockaddr_ll ll;
4837 int s;
4838
4839 memset(&ll, 0, sizeof(ll));
4840 ll.sll_family = AF_PACKET;
4841 ll.sll_ifindex = if_nametoindex(ifname);
4842 if (ll.sll_ifindex == 0) {
4843 perror("if_nametoindex");
4844 return -1;
4845 }
4846 s = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
4847#endif /* __QNXNTO__ */
4848 if (s < 0) {
4849 perror("socket[PF_PACKET,SOCK_RAW]");
4850 return -1;
4851 }
4852
4853 if (bind(s, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
4854 perror("monitor socket bind");
4855 close(s);
4856 return -1;
4857 }
4858
4859 return s;
4860}
4861
4862
4863static int hex2num(char c)
4864{
4865 if (c >= '0' && c <= '9')
4866 return c - '0';
4867 if (c >= 'a' && c <= 'f')
4868 return c - 'a' + 10;
4869 if (c >= 'A' && c <= 'F')
4870 return c - 'A' + 10;
4871 return -1;
4872}
4873
4874
4875int hwaddr_aton(const char *txt, unsigned char *addr)
4876{
4877 int i;
4878
4879 for (i = 0; i < 6; i++) {
4880 int a, b;
4881
4882 a = hex2num(*txt++);
4883 if (a < 0)
4884 return -1;
4885 b = hex2num(*txt++);
4886 if (b < 0)
4887 return -1;
4888 *addr++ = (a << 4) | b;
4889 if (i < 5 && *txt++ != ':')
4890 return -1;
4891 }
4892
4893 return 0;
4894}
4895
4896#endif /* defined(__linux__) || defined(__QNXNTO__) */
4897
4898enum send_frame_type {
4899 DISASSOC, DEAUTH, SAQUERY, AUTH, ASSOCREQ, REASSOCREQ, DLS_REQ
4900};
4901enum send_frame_protection {
4902 CORRECT_KEY, INCORRECT_KEY, UNPROTECTED
4903};
4904
4905
4906static int sta_inject_frame(struct sigma_dut *dut, struct sigma_conn *conn,
4907 enum send_frame_type frame,
4908 enum send_frame_protection protected,
4909 const char *dest)
4910{
4911#ifdef __linux__
4912 unsigned char buf[1000], *pos;
4913 int s, res;
4914 char bssid[20], addr[20];
4915 char result[32], ssid[100];
4916 size_t ssid_len;
4917
4918 if (get_wpa_status(get_station_ifname(), "wpa_state", result,
4919 sizeof(result)) < 0 ||
4920 strncmp(result, "COMPLETED", 9) != 0) {
4921 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Not connected");
4922 return 0;
4923 }
4924
4925 if (get_wpa_status(get_station_ifname(), "bssid", bssid, sizeof(bssid))
4926 < 0) {
4927 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not get "
4928 "current BSSID");
4929 return 0;
4930 }
4931
4932 if (get_wpa_status(get_station_ifname(), "address", addr, sizeof(addr))
4933 < 0) {
4934 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not get "
4935 "own MAC address");
4936 return 0;
4937 }
4938
4939 if (get_wpa_status(get_station_ifname(), "ssid", ssid, sizeof(ssid))
4940 < 0) {
4941 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not get "
4942 "current SSID");
4943 return 0;
4944 }
4945 ssid_len = strlen(ssid);
4946
4947 pos = buf;
4948
4949 /* Frame Control */
4950 switch (frame) {
4951 case DISASSOC:
4952 *pos++ = 0xa0;
4953 break;
4954 case DEAUTH:
4955 *pos++ = 0xc0;
4956 break;
4957 case SAQUERY:
4958 *pos++ = 0xd0;
4959 break;
4960 case AUTH:
4961 *pos++ = 0xb0;
4962 break;
4963 case ASSOCREQ:
4964 *pos++ = 0x00;
4965 break;
4966 case REASSOCREQ:
4967 *pos++ = 0x20;
4968 break;
4969 case DLS_REQ:
4970 *pos++ = 0xd0;
4971 break;
4972 }
4973
4974 if (protected == INCORRECT_KEY)
4975 *pos++ = 0x40; /* Set Protected field to 1 */
4976 else
4977 *pos++ = 0x00;
4978
4979 /* Duration */
4980 *pos++ = 0x00;
4981 *pos++ = 0x00;
4982
4983 /* addr1 = DA (current AP) */
4984 hwaddr_aton(bssid, pos);
4985 pos += 6;
4986 /* addr2 = SA (own address) */
4987 hwaddr_aton(addr, pos);
4988 pos += 6;
4989 /* addr3 = BSSID (current AP) */
4990 hwaddr_aton(bssid, pos);
4991 pos += 6;
4992
4993 /* Seq# (to be filled by driver/mac80211) */
4994 *pos++ = 0x00;
4995 *pos++ = 0x00;
4996
4997 if (protected == INCORRECT_KEY) {
4998 /* CCMP parameters */
4999 memcpy(pos, "\x61\x01\x00\x20\x00\x10\x00\x00", 8);
5000 pos += 8;
5001 }
5002
5003 if (protected == INCORRECT_KEY) {
5004 switch (frame) {
5005 case DEAUTH:
5006 /* Reason code (encrypted) */
5007 memcpy(pos, "\xa7\x39", 2);
5008 pos += 2;
5009 break;
5010 case DISASSOC:
5011 /* Reason code (encrypted) */
5012 memcpy(pos, "\xa7\x39", 2);
5013 pos += 2;
5014 break;
5015 case SAQUERY:
5016 /* Category|Action|TransID (encrypted) */
5017 memcpy(pos, "\x6f\xbd\xe9\x4d", 4);
5018 pos += 4;
5019 break;
5020 default:
5021 return -1;
5022 }
5023
5024 /* CCMP MIC */
5025 memcpy(pos, "\xc8\xd8\x3b\x06\x5d\xb7\x25\x68", 8);
5026 pos += 8;
5027 } else {
5028 switch (frame) {
5029 case DEAUTH:
5030 /* reason code = 8 */
5031 *pos++ = 0x08;
5032 *pos++ = 0x00;
5033 break;
5034 case DISASSOC:
5035 /* reason code = 8 */
5036 *pos++ = 0x08;
5037 *pos++ = 0x00;
5038 break;
5039 case SAQUERY:
5040 /* Category - SA Query */
5041 *pos++ = 0x08;
5042 /* SA query Action - Request */
5043 *pos++ = 0x00;
5044 /* Transaction ID */
5045 *pos++ = 0x12;
5046 *pos++ = 0x34;
5047 break;
5048 case AUTH:
5049 /* Auth Alg (Open) */
5050 *pos++ = 0x00;
5051 *pos++ = 0x00;
5052 /* Seq# */
5053 *pos++ = 0x01;
5054 *pos++ = 0x00;
5055 /* Status code */
5056 *pos++ = 0x00;
5057 *pos++ = 0x00;
5058 break;
5059 case ASSOCREQ:
5060 /* Capability Information */
5061 *pos++ = 0x31;
5062 *pos++ = 0x04;
5063 /* Listen Interval */
5064 *pos++ = 0x0a;
5065 *pos++ = 0x00;
5066 /* SSID */
5067 *pos++ = 0x00;
5068 *pos++ = ssid_len;
5069 memcpy(pos, ssid, ssid_len);
5070 pos += ssid_len;
5071 /* Supported Rates */
5072 memcpy(pos, "\x01\x08\x02\x04\x0b\x16\x0c\x12\x18\x24",
5073 10);
5074 pos += 10;
5075 /* Extended Supported Rates */
5076 memcpy(pos, "\x32\x04\x30\x48\x60\x6c", 6);
5077 pos += 6;
5078 /* RSN */
5079 memcpy(pos, "\x30\x1a\x01\x00\x00\x0f\xac\x04\x01\x00"
5080 "\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x02\xc0"
5081 "\x00\x00\x00\x00\x0f\xac\x06", 28);
5082 pos += 28;
5083 break;
5084 case REASSOCREQ:
5085 /* Capability Information */
5086 *pos++ = 0x31;
5087 *pos++ = 0x04;
5088 /* Listen Interval */
5089 *pos++ = 0x0a;
5090 *pos++ = 0x00;
5091 /* Current AP */
5092 hwaddr_aton(bssid, pos);
5093 pos += 6;
5094 /* SSID */
5095 *pos++ = 0x00;
5096 *pos++ = ssid_len;
5097 memcpy(pos, ssid, ssid_len);
5098 pos += ssid_len;
5099 /* Supported Rates */
5100 memcpy(pos, "\x01\x08\x02\x04\x0b\x16\x0c\x12\x18\x24",
5101 10);
5102 pos += 10;
5103 /* Extended Supported Rates */
5104 memcpy(pos, "\x32\x04\x30\x48\x60\x6c", 6);
5105 pos += 6;
5106 /* RSN */
5107 memcpy(pos, "\x30\x1a\x01\x00\x00\x0f\xac\x04\x01\x00"
5108 "\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x02\xc0"
5109 "\x00\x00\x00\x00\x0f\xac\x06", 28);
5110 pos += 28;
5111 break;
5112 case DLS_REQ:
5113 /* Category - DLS */
5114 *pos++ = 0x02;
5115 /* DLS Action - Request */
5116 *pos++ = 0x00;
5117 /* Destination MACAddress */
5118 if (dest)
5119 hwaddr_aton(dest, pos);
5120 else
5121 memset(pos, 0, 6);
5122 pos += 6;
5123 /* Source MACAddress */
5124 hwaddr_aton(addr, pos);
5125 pos += 6;
5126 /* Capability Information */
5127 *pos++ = 0x10; /* Privacy */
5128 *pos++ = 0x06; /* QoS */
5129 /* DLS Timeout Value */
5130 *pos++ = 0x00;
5131 *pos++ = 0x01;
5132 /* Supported rates */
5133 *pos++ = 0x01;
5134 *pos++ = 0x08;
5135 *pos++ = 0x0c; /* 6 Mbps */
5136 *pos++ = 0x12; /* 9 Mbps */
5137 *pos++ = 0x18; /* 12 Mbps */
5138 *pos++ = 0x24; /* 18 Mbps */
5139 *pos++ = 0x30; /* 24 Mbps */
5140 *pos++ = 0x48; /* 36 Mbps */
5141 *pos++ = 0x60; /* 48 Mbps */
5142 *pos++ = 0x6c; /* 54 Mbps */
5143 /* TODO: Extended Supported Rates */
5144 /* TODO: HT Capabilities */
5145 break;
5146 }
5147 }
5148
5149 s = open_monitor("sigmadut");
5150 if (s < 0) {
5151 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to open "
5152 "monitor socket");
5153 return 0;
5154 }
5155
5156 res = inject_frame(s, buf, pos - buf, protected == CORRECT_KEY);
5157 if (res < 0) {
5158 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to "
5159 "inject frame");
5160 return 0;
5161 }
5162 if (res < pos - buf) {
5163 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Only partial "
5164 "frame sent");
5165 return 0;
5166 }
5167
5168 close(s);
5169
5170 return 1;
5171#else /* __linux__ */
5172 send_resp(dut, conn, SIGMA_ERROR, "errorCode,sta_send_frame not "
5173 "yet supported");
5174 return 0;
5175#endif /* __linux__ */
5176}
5177
5178
5179static int cmd_sta_send_frame_tdls(struct sigma_dut *dut,
5180 struct sigma_conn *conn,
5181 struct sigma_cmd *cmd)
5182{
5183 const char *intf = get_param(cmd, "Interface");
5184 const char *sta, *val;
5185 unsigned char addr[ETH_ALEN];
5186 char buf[100];
5187
5188 sta = get_param(cmd, "peer");
5189 if (sta == NULL)
5190 sta = get_param(cmd, "station");
5191 if (sta == NULL) {
5192 send_resp(dut, conn, SIGMA_ERROR,
5193 "ErrorCode,Missing peer address");
5194 return 0;
5195 }
5196 if (hwaddr_aton(sta, addr) < 0) {
5197 send_resp(dut, conn, SIGMA_ERROR,
5198 "ErrorCode,Invalid peer address");
5199 return 0;
5200 }
5201
5202 val = get_param(cmd, "type");
5203 if (val == NULL)
5204 return -1;
5205
5206 if (strcasecmp(val, "DISCOVERY") == 0) {
5207 snprintf(buf, sizeof(buf), "TDLS_DISCOVER %s", sta);
5208 if (wpa_command(intf, buf) < 0) {
5209 send_resp(dut, conn, SIGMA_ERROR,
5210 "ErrorCode,Failed to send TDLS discovery");
5211 return 0;
5212 }
5213 return 1;
5214 }
5215
5216 if (strcasecmp(val, "SETUP") == 0) {
5217 int status = 0, timeout = 0;
5218
5219 val = get_param(cmd, "Status");
5220 if (val)
5221 status = atoi(val);
5222
5223 val = get_param(cmd, "Timeout");
5224 if (val)
5225 timeout = atoi(val);
5226
5227 if (status != 0 && status != 37) {
5228 send_resp(dut, conn, SIGMA_ERROR,
5229 "ErrorCode,Unsupported status value");
5230 return 0;
5231 }
5232
5233 if (timeout != 0 && timeout != 301) {
5234 send_resp(dut, conn, SIGMA_ERROR,
5235 "ErrorCode,Unsupported timeout value");
5236 return 0;
5237 }
5238
5239 if (status && timeout) {
5240 send_resp(dut, conn, SIGMA_ERROR,
5241 "ErrorCode,Unsupported timeout+status "
5242 "combination");
5243 return 0;
5244 }
5245
5246 if (status == 37 &&
5247 wpa_command(intf, "SET tdls_testing 0x200")) {
5248 send_resp(dut, conn, SIGMA_ERROR,
5249 "ErrorCode,Failed to enable "
5250 "decline setup response test mode");
5251 return 0;
5252 }
5253
5254 if (timeout == 301) {
5255 int res;
5256 if (dut->no_tpk_expiration)
5257 res = wpa_command(intf,
5258 "SET tdls_testing 0x108");
5259 else
5260 res = wpa_command(intf,
5261 "SET tdls_testing 0x8");
5262 if (res) {
5263 send_resp(dut, conn, SIGMA_ERROR,
5264 "ErrorCode,Failed to set short TPK "
5265 "lifetime");
5266 return 0;
5267 }
5268 }
5269
5270 snprintf(buf, sizeof(buf), "TDLS_SETUP %s", sta);
5271 if (wpa_command(intf, buf) < 0) {
5272 send_resp(dut, conn, SIGMA_ERROR,
5273 "ErrorCode,Failed to send TDLS setup");
5274 return 0;
5275 }
5276 return 1;
5277 }
5278
5279 if (strcasecmp(val, "TEARDOWN") == 0) {
5280 snprintf(buf, sizeof(buf), "TDLS_TEARDOWN %s", sta);
5281 if (wpa_command(intf, buf) < 0) {
5282 send_resp(dut, conn, SIGMA_ERROR,
5283 "ErrorCode,Failed to send TDLS teardown");
5284 return 0;
5285 }
5286 return 1;
5287 }
5288
5289 send_resp(dut, conn, SIGMA_ERROR,
5290 "ErrorCode,Unsupported TDLS frame");
5291 return 0;
5292}
5293
5294
5295static int sta_ap_known(const char *ifname, const char *bssid)
5296{
5297 char buf[4096];
5298
5299 snprintf(buf, sizeof(buf), "BSS %s", bssid);
5300 if (wpa_command_resp(ifname, buf, buf, sizeof(buf)) < 0)
5301 return 0;
5302 if (strncmp(buf, "id=", 3) != 0)
5303 return 0;
5304 return 1;
5305}
5306
5307
5308static int sta_scan_ap(struct sigma_dut *dut, const char *ifname,
5309 const char *bssid)
5310{
5311 int res;
5312 struct wpa_ctrl *ctrl;
5313 char buf[256];
5314
5315 if (sta_ap_known(ifname, bssid))
5316 return 0;
5317 sigma_dut_print(dut, DUT_MSG_DEBUG,
5318 "AP not in BSS table - start scan");
5319
5320 ctrl = open_wpa_mon(ifname);
5321 if (ctrl == NULL) {
5322 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to open "
5323 "wpa_supplicant monitor connection");
5324 return -1;
5325 }
5326
5327 if (wpa_command(ifname, "SCAN") < 0) {
5328 sigma_dut_print(dut, DUT_MSG_INFO, "Failed to start scan");
5329 wpa_ctrl_detach(ctrl);
5330 wpa_ctrl_close(ctrl);
5331 return -1;
5332 }
5333
5334 res = get_wpa_cli_event(dut, ctrl, "CTRL-EVENT-SCAN-RESULTS",
5335 buf, sizeof(buf));
5336
5337 wpa_ctrl_detach(ctrl);
5338 wpa_ctrl_close(ctrl);
5339
5340 if (res < 0) {
5341 sigma_dut_print(dut, DUT_MSG_INFO, "Scan did not complete");
5342 return -1;
5343 }
5344
5345 if (sta_ap_known(ifname, bssid))
5346 return 0;
5347 sigma_dut_print(dut, DUT_MSG_INFO, "AP not in BSS table");
5348 return -1;
5349}
5350
5351
5352static int cmd_sta_send_frame_hs2_neighadv(struct sigma_dut *dut,
5353 struct sigma_conn *conn,
5354 struct sigma_cmd *cmd,
5355 const char *intf)
5356{
5357 char buf[200];
5358
5359 snprintf(buf, sizeof(buf), "ndsend 2001:DB8::1 %s", intf);
5360 if (system(buf) != 0) {
5361 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Failed to run "
5362 "ndsend");
5363 return 0;
5364 }
5365
5366 return 1;
5367}
5368
5369
5370static int cmd_sta_send_frame_hs2_neighsolreq(struct sigma_dut *dut,
5371 struct sigma_conn *conn,
5372 struct sigma_cmd *cmd,
5373 const char *intf)
5374{
5375 char buf[200];
5376 const char *ip = get_param(cmd, "SenderIP");
5377
5378 snprintf(buf, sizeof(buf), "ndisc6 -nm %s %s -r 4", ip, intf);
5379 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
5380 if (system(buf) == 0) {
5381 sigma_dut_print(dut, DUT_MSG_INFO,
5382 "Neighbor Solicitation got a response "
5383 "for %s@%s", ip, intf);
5384 }
5385
5386 return 1;
5387}
5388
5389
5390static int cmd_sta_send_frame_hs2_arpprobe(struct sigma_dut *dut,
5391 struct sigma_conn *conn,
5392 struct sigma_cmd *cmd,
5393 const char *ifname)
5394{
5395 char buf[200];
5396 const char *ip = get_param(cmd, "SenderIP");
5397
5398 if (ip == NULL) {
5399 send_resp(dut, conn, SIGMA_ERROR,
5400 "ErrorCode,Missing SenderIP parameter");
5401 return 0;
5402 }
5403 snprintf(buf, sizeof(buf), "arping -I %s -D %s -c 4", ifname, ip);
5404 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
5405 if (system(buf) != 0) {
5406 sigma_dut_print(dut, DUT_MSG_INFO, "arping DAD got a response "
5407 "for %s@%s", ip, ifname);
5408 }
5409
5410 return 1;
5411}
5412
5413
5414static int cmd_sta_send_frame_hs2_arpannounce(struct sigma_dut *dut,
5415 struct sigma_conn *conn,
5416 struct sigma_cmd *cmd,
5417 const char *ifname)
5418{
5419 char buf[200];
5420 char ip[16];
5421 int s;
5422
5423 s = socket(PF_INET, SOCK_DGRAM, 0);
5424 if (s >= 0) {
5425 struct ifreq ifr;
5426 struct sockaddr_in saddr;
5427
5428 memset(&ifr, 0, sizeof(ifr));
5429 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
5430 if (ioctl(s, SIOCGIFADDR, &ifr) < 0) {
5431 sigma_dut_print(dut, DUT_MSG_INFO, "Failed to get "
5432 "%s IP address: %s",
5433 ifname, strerror(errno));
5434 return -1;
5435 } else {
5436 memcpy(&saddr, &ifr.ifr_addr,
5437 sizeof(struct sockaddr_in));
5438 strncpy(ip, inet_ntoa(saddr.sin_addr), sizeof(ip));
5439 }
5440 close(s);
5441
5442 }
5443
5444 snprintf(buf, sizeof(buf), "arping -I %s -s %s %s -c 4", ifname, ip,
5445 ip);
5446 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
5447 if (system(buf) != 0) {
5448 }
5449
5450 return 1;
5451}
5452
5453
5454static int cmd_sta_send_frame_hs2_arpreply(struct sigma_dut *dut,
5455 struct sigma_conn *conn,
5456 struct sigma_cmd *cmd,
5457 const char *ifname)
5458{
5459 char buf[200], addr[20];
5460 char dst[ETH_ALEN], src[ETH_ALEN];
5461 short ethtype = htons(ETH_P_ARP);
5462 char *pos;
5463 int s, res;
5464 const char *val;
5465 struct sockaddr_in taddr;
5466
5467 val = get_param(cmd, "dest");
5468 if (val)
5469 hwaddr_aton(val, (unsigned char *) dst);
5470
5471 val = get_param(cmd, "DestIP");
5472 if (val)
5473 inet_aton(val, &taddr.sin_addr);
5474
5475 if (get_wpa_status(get_station_ifname(), "address", addr,
5476 sizeof(addr)) < 0)
5477 return -2;
5478 hwaddr_aton(addr, (unsigned char *) src);
5479
5480 pos = buf;
5481 *pos++ = 0x00;
5482 *pos++ = 0x01;
5483 *pos++ = 0x08;
5484 *pos++ = 0x00;
5485 *pos++ = 0x06;
5486 *pos++ = 0x04;
5487 *pos++ = 0x00;
5488 *pos++ = 0x02;
5489 memcpy(pos, src, ETH_ALEN);
5490 pos += ETH_ALEN;
5491 memcpy(pos, &taddr.sin_addr, 4);
5492 pos += 4;
5493 memcpy(pos, dst, ETH_ALEN);
5494 pos += ETH_ALEN;
5495 memcpy(pos, &taddr.sin_addr, 4);
5496 pos += 4;
5497
5498 s = open_monitor(get_station_ifname());
5499 if (s < 0) {
5500 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to open "
5501 "monitor socket");
5502 return 0;
5503 }
5504
5505 res = inject_eth_frame(s, buf, pos - buf, ethtype, dst, src);
5506 if (res < 0) {
5507 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to "
5508 "inject frame");
5509 return 0;
5510 }
5511
5512 close(s);
5513
5514 return 1;
5515}
5516
5517
5518static int cmd_sta_send_frame_hs2_dls_req(struct sigma_dut *dut,
5519 struct sigma_conn *conn,
5520 struct sigma_cmd *cmd,
5521 const char *intf, const char *dest)
5522{
5523 char buf[100];
5524
5525 if (if_nametoindex("sigmadut") == 0) {
5526 snprintf(buf, sizeof(buf),
5527 "iw dev %s interface add sigmadut type monitor",
5528 get_station_ifname());
5529 if (system(buf) != 0 ||
5530 if_nametoindex("sigmadut") == 0) {
5531 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to add "
5532 "monitor interface with '%s'", buf);
5533 return -2;
5534 }
5535 }
5536
5537 if (system("ifconfig sigmadut up") != 0) {
5538 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to set "
5539 "monitor interface up");
5540 return -2;
5541 }
5542
5543 return sta_inject_frame(dut, conn, DLS_REQ, UNPROTECTED, dest);
5544}
5545
5546
5547static int cmd_sta_send_frame_hs2(struct sigma_dut *dut,
5548 struct sigma_conn *conn,
5549 struct sigma_cmd *cmd)
5550{
5551 const char *intf = get_param(cmd, "Interface");
5552 const char *dest = get_param(cmd, "Dest");
5553 const char *type = get_param(cmd, "FrameName");
5554 const char *val;
5555 char buf[200], *pos, *end;
5556 int count, count2;
5557
5558 if (type == NULL)
5559 type = get_param(cmd, "Type");
5560
5561 if (intf == NULL || dest == NULL || type == NULL)
5562 return -1;
5563
5564 if (strcasecmp(type, "NeighAdv") == 0)
5565 return cmd_sta_send_frame_hs2_neighadv(dut, conn, cmd, intf);
5566
5567 if (strcasecmp(type, "NeighSolicitReq") == 0)
5568 return cmd_sta_send_frame_hs2_neighsolreq(dut, conn, cmd, intf);
5569
5570 if (strcasecmp(type, "ARPProbe") == 0)
5571 return cmd_sta_send_frame_hs2_arpprobe(dut, conn, cmd, intf);
5572
5573 if (strcasecmp(type, "ARPAnnounce") == 0)
5574 return cmd_sta_send_frame_hs2_arpannounce(dut, conn, cmd, intf);
5575
5576 if (strcasecmp(type, "ARPReply") == 0)
5577 return cmd_sta_send_frame_hs2_arpreply(dut, conn, cmd, intf);
5578
5579 if (strcasecmp(type, "DLS-request") == 0 ||
5580 strcasecmp(type, "DLSrequest") == 0)
5581 return cmd_sta_send_frame_hs2_dls_req(dut, conn, cmd, intf,
5582 dest);
5583
5584 if (strcasecmp(type, "ANQPQuery") != 0 &&
5585 strcasecmp(type, "Query") != 0) {
5586 send_resp(dut, conn, SIGMA_ERROR,
5587 "ErrorCode,Unsupported HS 2.0 send frame type");
5588 return 0;
5589 }
5590
5591 if (sta_scan_ap(dut, intf, dest) < 0) {
5592 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Could not find "
5593 "the requested AP");
5594 return 0;
5595 }
5596
5597 pos = buf;
5598 end = buf + sizeof(buf);
5599 count = 0;
5600 pos += snprintf(pos, end - pos, "ANQP_GET %s ", dest);
5601
5602 val = get_param(cmd, "ANQP_CAP_LIST");
5603 if (val && atoi(val)) {
5604 pos += snprintf(pos, end - pos, "%s257", count > 0 ? "," : "");
5605 count++;
5606 }
5607
5608 val = get_param(cmd, "VENUE_NAME");
5609 if (val && atoi(val)) {
5610 pos += snprintf(pos, end - pos, "%s258", count > 0 ? "," : "");
5611 count++;
5612 }
5613
5614 val = get_param(cmd, "NETWORK_AUTH_TYPE");
5615 if (val && atoi(val)) {
5616 pos += snprintf(pos, end - pos, "%s260", count > 0 ? "," : "");
5617 count++;
5618 }
5619
5620 val = get_param(cmd, "ROAMING_CONS");
5621 if (val && atoi(val)) {
5622 pos += snprintf(pos, end - pos, "%s261", count > 0 ? "," : "");
5623 count++;
5624 }
5625
5626 val = get_param(cmd, "IP_ADDR_TYPE_AVAILABILITY");
5627 if (val && atoi(val)) {
5628 pos += snprintf(pos, end - pos, "%s262", count > 0 ? "," : "");
5629 count++;
5630 }
5631
5632 val = get_param(cmd, "NAI_REALM_LIST");
5633 if (val && atoi(val)) {
5634 pos += snprintf(pos, end - pos, "%s263", count > 0 ? "," : "");
5635 count++;
5636 }
5637
5638 val = get_param(cmd, "3GPP_INFO");
5639 if (val && atoi(val)) {
5640 pos += snprintf(pos, end - pos, "%s264", count > 0 ? "," : "");
5641 count++;
5642 }
5643
5644 val = get_param(cmd, "DOMAIN_LIST");
5645 if (val && atoi(val)) {
5646 pos += snprintf(pos, end - pos, "%s268", count > 0 ? "," : "");
5647 count++;
5648 }
5649
5650 if (count && wpa_command(intf, buf)) {
5651 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,ANQP_GET failed");
5652 return 0;
5653 }
5654
5655 pos = buf;
5656 end = buf + sizeof(buf);
5657 count2 = 0;
5658 pos += snprintf(pos, end - pos, "HS20_ANQP_GET %s ", dest);
5659
5660 val = get_param(cmd, "HS_CAP_LIST");
5661 if (val && atoi(val)) {
5662 pos += snprintf(pos, end - pos, "%s2", count2 > 0 ? "," : "");
5663 count2++;
5664 }
5665
5666 val = get_param(cmd, "OPER_NAME");
5667 if (val && atoi(val)) {
5668 pos += snprintf(pos, end - pos, "%s3", count2 > 0 ? "," : "");
5669 count2++;
5670 }
5671
5672 val = get_param(cmd, "WAN_METRICS");
5673 if (!val)
5674 val = get_param(cmd, "WAN_MAT");
5675 if (!val)
5676 val = get_param(cmd, "WAN_MET");
5677 if (val && atoi(val)) {
5678 pos += snprintf(pos, end - pos, "%s4", count2 > 0 ? "," : "");
5679 count2++;
5680 }
5681
5682 val = get_param(cmd, "CONNECTION_CAPABILITY");
5683 if (val && atoi(val)) {
5684 pos += snprintf(pos, end - pos, "%s5", count2 > 0 ? "," : "");
5685 count2++;
5686 }
5687
5688 val = get_param(cmd, "OP_CLASS");
5689 if (val && atoi(val)) {
5690 pos += snprintf(pos, end - pos, "%s7", count2 > 0 ? "," : "");
5691 count2++;
5692 }
5693
5694 val = get_param(cmd, "OSU_PROVIDER_LIST");
5695 if (val && atoi(val)) {
5696 pos += snprintf(pos, end - pos, "%s8", count2 > 0 ? "," : "");
5697 count2++;
5698 }
5699
5700 if (count && count2) {
5701 sigma_dut_print(dut, DUT_MSG_DEBUG, "Wait before sending out "
5702 "second query");
5703 sleep(1);
5704 }
5705
5706 if (count2 && wpa_command(intf, buf)) {
5707 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,HS20_ANQP_GET "
5708 "failed");
5709 return 0;
5710 }
5711
5712 val = get_param(cmd, "NAI_HOME_REALM_LIST");
5713 if (val) {
5714 if (count || count2) {
5715 sigma_dut_print(dut, DUT_MSG_DEBUG, "Wait before "
5716 "sending out second query");
5717 sleep(1);
5718 }
5719
5720 if (strcmp(val, "1") == 0)
5721 val = "mail.example.com";
5722 snprintf(buf, end - pos,
5723 "HS20_GET_NAI_HOME_REALM_LIST %s realm=%s",
5724 dest, val);
5725 if (wpa_command(intf, buf)) {
5726 send_resp(dut, conn, SIGMA_ERROR,
5727 "ErrorCode,HS20_GET_NAI_HOME_REALM_LIST "
5728 "failed");
5729 return 0;
5730 }
5731 }
5732
5733 val = get_param(cmd, "ICON_REQUEST");
5734 if (val) {
5735 if (count || count2) {
5736 sigma_dut_print(dut, DUT_MSG_DEBUG, "Wait before "
5737 "sending out second query");
5738 sleep(1);
5739 }
5740
5741 snprintf(buf, end - pos,
5742 "HS20_ICON_REQUEST %s %s", dest, val);
5743 if (wpa_command(intf, buf)) {
5744 send_resp(dut, conn, SIGMA_ERROR,
5745 "ErrorCode,HS20_ICON_REQUEST failed");
5746 return 0;
5747 }
5748 }
5749
5750 return 1;
5751}
5752
5753
5754static int ath_sta_send_frame_vht(struct sigma_dut *dut,
5755 struct sigma_conn *conn,
5756 struct sigma_cmd *cmd)
5757{
5758 const char *val;
5759 char *ifname;
5760 char buf[100];
5761 int chwidth, nss;
5762
5763 val = get_param(cmd, "framename");
5764 if (!val)
5765 return -1;
5766 sigma_dut_print(dut, DUT_MSG_DEBUG, "framename is %s", val);
5767
5768 /* Command sequence to generate Op mode notification */
5769 if (val && strcasecmp(val, "Op_md_notif_frm") == 0) {
5770 ifname = get_station_ifname();
5771
5772 /* Disable STBC */
5773 snprintf(buf, sizeof(buf),
5774 "iwpriv %s tx_stbc 0", ifname);
5775 if (system(buf) != 0) {
5776 sigma_dut_print(dut, DUT_MSG_ERROR,
5777 "iwpriv tx_stbc 0 failed!");
5778 }
5779
5780 /* Extract Channel width */
5781 val = get_param(cmd, "Channel_width");
5782 if (val) {
5783 switch (atoi(val)) {
5784 case 20:
5785 chwidth = 0;
5786 break;
5787 case 40:
5788 chwidth = 1;
5789 break;
5790 case 80:
5791 chwidth = 2;
5792 break;
5793 case 160:
5794 chwidth = 3;
5795 break;
5796 default:
5797 chwidth = 2;
5798 break;
5799 }
5800
5801 snprintf(buf, sizeof(buf), "iwpriv %s chwidth %d",
5802 ifname, chwidth);
5803 if (system(buf) != 0) {
5804 sigma_dut_print(dut, DUT_MSG_ERROR,
5805 "iwpriv chwidth failed!");
5806 }
5807 }
5808
5809 /* Extract NSS */
5810 val = get_param(cmd, "NSS");
5811 if (val) {
5812 switch (atoi(val)) {
5813 case 1:
5814 nss = 1;
5815 break;
5816 case 2:
5817 nss = 3;
5818 break;
5819 case 3:
5820 nss = 7;
5821 break;
5822 default:
5823 /* We do not support NSS > 3 */
5824 nss = 3;
5825 break;
5826 }
5827 snprintf(buf, sizeof(buf),
5828 "iwpriv %s rxchainmask %d", ifname, nss);
5829 if (system(buf) != 0) {
5830 sigma_dut_print(dut, DUT_MSG_ERROR,
5831 "iwpriv rxchainmask failed!");
5832 }
5833 }
5834
5835 /* Opmode notify */
5836 snprintf(buf, sizeof(buf), "iwpriv %s opmode_notify 1", ifname);
5837 if (system(buf) != 0) {
5838 sigma_dut_print(dut, DUT_MSG_ERROR,
5839 "iwpriv opmode_notify failed!");
5840 } else {
5841 sigma_dut_print(dut, DUT_MSG_INFO,
5842 "Sent out the notify frame!");
5843 }
5844 }
5845
5846 return 1;
5847}
5848
5849
5850static int cmd_sta_send_frame_vht(struct sigma_dut *dut,
5851 struct sigma_conn *conn,
5852 struct sigma_cmd *cmd)
5853{
5854 switch (get_driver_type()) {
5855 case DRIVER_ATHEROS:
5856 return ath_sta_send_frame_vht(dut, conn, cmd);
5857 default:
5858 send_resp(dut, conn, SIGMA_ERROR,
5859 "errorCode,Unsupported sta_set_frame(VHT) with the current driver");
5860 return 0;
5861 }
5862}
5863
5864
5865int cmd_sta_send_frame(struct sigma_dut *dut, struct sigma_conn *conn,
5866 struct sigma_cmd *cmd)
5867{
5868 const char *intf = get_param(cmd, "Interface");
5869 const char *val;
5870 enum send_frame_type frame;
5871 enum send_frame_protection protected;
5872 char buf[100];
5873 unsigned char addr[ETH_ALEN];
5874 int res;
5875
5876 val = get_param(cmd, "program");
5877 if (val == NULL)
5878 val = get_param(cmd, "frame");
5879 if (val && strcasecmp(val, "TDLS") == 0)
5880 return cmd_sta_send_frame_tdls(dut, conn, cmd);
5881 if (val && (strcasecmp(val, "HS2") == 0 ||
5882 strcasecmp(val, "HS2-R2") == 0))
5883 return cmd_sta_send_frame_hs2(dut, conn, cmd);
5884 if (val && strcasecmp(val, "VHT") == 0)
5885 return cmd_sta_send_frame_vht(dut, conn, cmd);
5886
5887 val = get_param(cmd, "TD_DISC");
5888 if (val) {
5889 if (hwaddr_aton(val, addr) < 0)
5890 return -1;
5891 snprintf(buf, sizeof(buf), "TDLS_DISCOVER %s", val);
5892 if (wpa_command(intf, buf) < 0) {
5893 send_resp(dut, conn, SIGMA_ERROR,
5894 "ErrorCode,Failed to send TDLS discovery");
5895 return 0;
5896 }
5897 return 1;
5898 }
5899
5900 val = get_param(cmd, "TD_Setup");
5901 if (val) {
5902 if (hwaddr_aton(val, addr) < 0)
5903 return -1;
5904 snprintf(buf, sizeof(buf), "TDLS_SETUP %s", val);
5905 if (wpa_command(intf, buf) < 0) {
5906 send_resp(dut, conn, SIGMA_ERROR,
5907 "ErrorCode,Failed to start TDLS setup");
5908 return 0;
5909 }
5910 return 1;
5911 }
5912
5913 val = get_param(cmd, "TD_TearDown");
5914 if (val) {
5915 if (hwaddr_aton(val, addr) < 0)
5916 return -1;
5917 snprintf(buf, sizeof(buf), "TDLS_TEARDOWN %s", val);
5918 if (wpa_command(intf, buf) < 0) {
5919 send_resp(dut, conn, SIGMA_ERROR,
5920 "ErrorCode,Failed to tear down TDLS link");
5921 return 0;
5922 }
5923 return 1;
5924 }
5925
5926 val = get_param(cmd, "TD_ChannelSwitch");
5927 if (val) {
5928 /* TODO */
5929 send_resp(dut, conn, SIGMA_ERROR,
5930 "ErrorCode,TD_ChannelSwitch not yet supported");
5931 return 0;
5932 }
5933
5934 val = get_param(cmd, "TD_NF");
5935 if (val) {
5936 /* TODO */
5937 send_resp(dut, conn, SIGMA_ERROR,
5938 "ErrorCode,TD_NF not yet supported");
5939 return 0;
5940 }
5941
5942 val = get_param(cmd, "PMFFrameType");
5943 if (val == NULL)
5944 val = get_param(cmd, "FrameName");
5945 if (val == NULL)
5946 val = get_param(cmd, "Type");
5947 if (val == NULL)
5948 return -1;
5949 if (strcasecmp(val, "disassoc") == 0)
5950 frame = DISASSOC;
5951 else if (strcasecmp(val, "deauth") == 0)
5952 frame = DEAUTH;
5953 else if (strcasecmp(val, "saquery") == 0)
5954 frame = SAQUERY;
5955 else if (strcasecmp(val, "auth") == 0)
5956 frame = AUTH;
5957 else if (strcasecmp(val, "assocreq") == 0)
5958 frame = ASSOCREQ;
5959 else if (strcasecmp(val, "reassocreq") == 0)
5960 frame = REASSOCREQ;
5961 else if (strcasecmp(val, "neigreq") == 0) {
5962 sigma_dut_print(dut, DUT_MSG_INFO, "Got neighbor request");
5963
5964 val = get_param(cmd, "ssid");
5965 if (val == NULL)
5966 return -1;
5967
5968 res = send_neighbor_request(dut, intf, val);
5969 if (res) {
5970 send_resp(dut, conn, SIGMA_ERROR, "errorCode,"
5971 "Failed to send neighbor report request");
5972 return 0;
5973 }
5974
5975 return 1;
5976 } else if (strcasecmp(val, "transmgmtquery") == 0) {
5977 sigma_dut_print(dut, DUT_MSG_DEBUG,
5978 "Got Transition Management Query");
5979
5980 val = get_param(cmd, "ssid");
5981 res = send_trans_mgmt_query(dut, intf, val);
5982 if (res) {
5983 send_resp(dut, conn, SIGMA_ERROR, "errorCode,"
5984 "Failed to send Transition Management Query");
5985 return 0;
5986 }
5987
5988 return 1;
5989 } else {
5990 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Unsupported "
5991 "PMFFrameType");
5992 return 0;
5993 }
5994
5995 val = get_param(cmd, "PMFProtected");
5996 if (val == NULL)
5997 val = get_param(cmd, "Protected");
5998 if (val == NULL)
5999 return -1;
6000 if (strcasecmp(val, "Correct-key") == 0 ||
6001 strcasecmp(val, "CorrectKey") == 0)
6002 protected = CORRECT_KEY;
6003 else if (strcasecmp(val, "IncorrectKey") == 0)
6004 protected = INCORRECT_KEY;
6005 else if (strcasecmp(val, "Unprotected") == 0)
6006 protected = UNPROTECTED;
6007 else {
6008 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Unsupported "
6009 "PMFProtected");
6010 return 0;
6011 }
6012
6013 if (protected != UNPROTECTED &&
6014 (frame == AUTH || frame == ASSOCREQ || frame == REASSOCREQ)) {
6015 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Impossible "
6016 "PMFProtected for auth/assocreq/reassocreq");
6017 return 0;
6018 }
6019
6020 if (if_nametoindex("sigmadut") == 0) {
6021 snprintf(buf, sizeof(buf),
6022 "iw dev %s interface add sigmadut type monitor",
6023 get_station_ifname());
6024 if (system(buf) != 0 ||
6025 if_nametoindex("sigmadut") == 0) {
6026 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to add "
6027 "monitor interface with '%s'", buf);
6028 return -2;
6029 }
6030 }
6031
6032 if (system("ifconfig sigmadut up") != 0) {
6033 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to set "
6034 "monitor interface up");
6035 return -2;
6036 }
6037
6038 return sta_inject_frame(dut, conn, frame, protected, NULL);
6039}
6040
6041
6042static int cmd_sta_set_parameter_hs2(struct sigma_dut *dut,
6043 struct sigma_conn *conn,
6044 struct sigma_cmd *cmd,
6045 const char *ifname)
6046{
6047 char buf[200];
6048 const char *val;
6049
6050 val = get_param(cmd, "ClearARP");
6051 if (val && atoi(val) == 1) {
6052 snprintf(buf, sizeof(buf), "ip neigh flush dev %s", ifname);
6053 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
6054 if (system(buf) != 0) {
6055 send_resp(dut, conn, SIGMA_ERROR,
6056 "errorCode,Failed to clear ARP cache");
6057 return 0;
6058 }
6059 }
6060
6061 return 1;
6062}
6063
6064
6065int cmd_sta_set_parameter(struct sigma_dut *dut, struct sigma_conn *conn,
6066 struct sigma_cmd *cmd)
6067{
6068 const char *intf = get_param(cmd, "Interface");
6069 const char *val;
6070
6071 if (intf == NULL)
6072 return -1;
6073
6074 val = get_param(cmd, "program");
6075 if (val && (strcasecmp(val, "HS2") == 0 ||
6076 strcasecmp(val, "HS2-R2") == 0))
6077 return cmd_sta_set_parameter_hs2(dut, conn, cmd, intf);
6078
6079 return -1;
6080}
6081
6082
6083static int cmd_sta_set_macaddr(struct sigma_dut *dut, struct sigma_conn *conn,
6084 struct sigma_cmd *cmd)
6085{
6086 const char *intf = get_param(cmd, "Interface");
6087 const char *mac = get_param(cmd, "MAC");
6088
6089 if (intf == NULL || mac == NULL)
6090 return -1;
6091
6092 sigma_dut_print(dut, DUT_MSG_INFO, "Change local MAC address for "
6093 "interface %s to %s", intf, mac);
6094
6095 if (dut->set_macaddr) {
6096 char buf[128];
6097 int res;
6098 if (strcasecmp(mac, "default") == 0) {
6099 res = snprintf(buf, sizeof(buf), "%s",
6100 dut->set_macaddr);
6101 dut->tmp_mac_addr = 0;
6102 } else {
6103 res = snprintf(buf, sizeof(buf), "%s %s",
6104 dut->set_macaddr, mac);
6105 dut->tmp_mac_addr = 1;
6106 }
6107 if (res < 0 || res >= (int) sizeof(buf))
6108 return -1;
6109 if (system(buf) != 0) {
6110 send_resp(dut, conn, SIGMA_ERROR,
6111 "errorCode,Failed to set MAC "
6112 "address");
6113 return 0;
6114 }
6115 return 1;
6116 }
6117
6118 if (strcasecmp(mac, "default") == 0)
6119 return 1;
6120
6121 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Unsupported "
6122 "command");
6123 return 0;
6124}
6125
6126
6127static int iwpriv_tdlsoffchnmode(struct sigma_dut *dut,
6128 struct sigma_conn *conn, const char *intf,
6129 int val)
6130{
6131 char buf[200];
6132 int res;
6133
6134 res = snprintf(buf, sizeof(buf), "iwpriv %s tdlsoffchnmode %d",
6135 intf, val);
6136 if (res < 0 || res >= (int) sizeof(buf))
6137 return -1;
6138 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
6139 if (system(buf) != 0) {
6140 send_resp(dut, conn, SIGMA_ERROR,
6141 "errorCode,Failed to configure offchannel mode");
6142 return 0;
6143 }
6144
6145 return 1;
6146}
6147
6148
6149enum sec_ch_offset {
6150 SEC_CH_NO,
6151 SEC_CH_40ABOVE,
6152 SEC_CH_40BELOW
6153};
6154
6155
6156static int off_chan_val(enum sec_ch_offset off)
6157{
6158 switch (off) {
6159 case SEC_CH_NO:
6160 return 0;
6161 case SEC_CH_40ABOVE:
6162 return 40;
6163 case SEC_CH_40BELOW:
6164 return -40;
6165 }
6166
6167 return 0;
6168}
6169
6170
6171static int iwpriv_set_offchan(struct sigma_dut *dut, struct sigma_conn *conn,
6172 const char *intf, int off_ch_num,
6173 enum sec_ch_offset sec)
6174{
6175 char buf[200];
6176 int res;
6177
6178 res = snprintf(buf, sizeof(buf), "iwpriv %s tdlsoffchan %d",
6179 intf, off_ch_num);
6180 if (res < 0 || res >= (int) sizeof(buf))
6181 return -1;
6182 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
6183 if (system(buf) != 0) {
6184 send_resp(dut, conn, SIGMA_ERROR,
6185 "errorCode,Failed to set offchan");
6186 return 0;
6187 }
6188
6189 res = snprintf(buf, sizeof(buf), "iwpriv %s tdlsecchnoffst %d",
6190 intf, off_chan_val(sec));
6191 if (res < 0 || res >= (int) sizeof(buf))
6192 return -1;
6193 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
6194 if (system(buf) != 0) {
6195 send_resp(dut, conn, SIGMA_ERROR,
6196 "errorCode,Failed to set sec chan offset");
6197 return 0;
6198 }
6199
6200 return 1;
6201}
6202
6203
6204static int tdls_set_offchannel_offset(struct sigma_dut *dut,
6205 struct sigma_conn *conn,
6206 const char *intf, int off_ch_num,
6207 enum sec_ch_offset sec)
6208{
6209 char buf[200];
6210 int res;
6211
6212 res = snprintf(buf, sizeof(buf), "DRIVER TDLSOFFCHANNEL %d",
6213 off_ch_num);
6214 if (res < 0 || res >= (int) sizeof(buf))
6215 return -1;
6216 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
6217
6218 if (wpa_command(intf, buf) < 0) {
6219 send_resp(dut, conn, SIGMA_ERROR,
6220 "ErrorCode,Failed to set offchan");
6221 return 0;
6222 }
6223 res = snprintf(buf, sizeof(buf), "DRIVER TDLSSECONDARYCHANNELOFFSET %d",
6224 off_chan_val(sec));
6225 if (res < 0 || res >= (int) sizeof(buf))
6226 return -1;
6227
6228 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
6229
6230 if (wpa_command(intf, buf) < 0) {
6231 send_resp(dut, conn, SIGMA_ERROR,
6232 "ErrorCode,Failed to set sec chan offset");
6233 return 0;
6234 }
6235
6236 return 1;
6237}
6238
6239
6240static int tdls_set_offchannel_mode(struct sigma_dut *dut,
6241 struct sigma_conn *conn,
6242 const char *intf, int val)
6243{
6244 char buf[200];
6245 int res;
6246
6247 res = snprintf(buf, sizeof(buf), "DRIVER TDLSOFFCHANNELMODE %d",
6248 val);
6249 if (res < 0 || res >= (int) sizeof(buf))
6250 return -1;
6251 sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", buf);
6252
6253 if (wpa_command(intf, buf) < 0) {
6254 send_resp(dut, conn, SIGMA_ERROR,
6255 "ErrorCode,Failed to configure offchannel mode");
6256 return 0;
6257 }
6258
6259 return 1;
6260}
6261
6262
6263static int cmd_sta_set_rfeature_tdls(const char *intf, struct sigma_dut *dut,
6264 struct sigma_conn *conn,
6265 struct sigma_cmd *cmd)
6266{
6267 const char *val;
6268 enum {
6269 CHSM_NOT_SET,
6270 CHSM_ENABLE,
6271 CHSM_DISABLE,
6272 CHSM_REJREQ,
6273 CHSM_UNSOLRESP
6274 } chsm = CHSM_NOT_SET;
6275 int off_ch_num = -1;
6276 enum sec_ch_offset sec_ch = SEC_CH_NO;
6277 int res;
6278
6279 val = get_param(cmd, "Uapsd");
6280 if (val) {
6281 char buf[100];
6282 if (strcasecmp(val, "Enable") == 0)
6283 snprintf(buf, sizeof(buf), "SET ps 99");
6284 else if (strcasecmp(val, "Disable") == 0)
6285 snprintf(buf, sizeof(buf), "SET ps 98");
6286 else {
6287 send_resp(dut, conn, SIGMA_ERROR, "errorCode,"
6288 "Unsupported uapsd parameter value");
6289 return 0;
6290 }
6291 if (wpa_command(intf, buf)) {
6292 send_resp(dut, conn, SIGMA_ERROR,
6293 "ErrorCode,Failed to change U-APSD "
6294 "powersave mode");
6295 return 0;
6296 }
6297 }
6298
6299 val = get_param(cmd, "TPKTIMER");
6300 if (val && strcasecmp(val, "DISABLE") == 0) {
6301 if (wpa_command(intf, "SET tdls_testing 0x100")) {
6302 send_resp(dut, conn, SIGMA_ERROR,
6303 "ErrorCode,Failed to enable no TPK "
6304 "expiration test mode");
6305 return 0;
6306 }
6307 dut->no_tpk_expiration = 1;
6308 }
6309
6310 val = get_param(cmd, "ChSwitchMode");
6311 if (val) {
6312 if (strcasecmp(val, "Enable") == 0 ||
6313 strcasecmp(val, "Initiate") == 0)
6314 chsm = CHSM_ENABLE;
6315 else if (strcasecmp(val, "Disable") == 0 ||
6316 strcasecmp(val, "passive") == 0)
6317 chsm = CHSM_DISABLE;
6318 else if (strcasecmp(val, "RejReq") == 0)
6319 chsm = CHSM_REJREQ;
6320 else if (strcasecmp(val, "UnSolResp") == 0)
6321 chsm = CHSM_UNSOLRESP;
6322 else {
6323 send_resp(dut, conn, SIGMA_ERROR,
6324 "ErrorCode,Unknown ChSwitchMode value");
6325 return 0;
6326 }
6327 }
6328
6329 val = get_param(cmd, "OffChNum");
6330 if (val) {
6331 off_ch_num = atoi(val);
6332 if (off_ch_num == 0) {
6333 send_resp(dut, conn, SIGMA_ERROR,
6334 "ErrorCode,Invalid OffChNum");
6335 return 0;
6336 }
6337 }
6338
6339 val = get_param(cmd, "SecChOffset");
6340 if (val) {
6341 if (strcmp(val, "20") == 0)
6342 sec_ch = SEC_CH_NO;
6343 else if (strcasecmp(val, "40above") == 0)
6344 sec_ch = SEC_CH_40ABOVE;
6345 else if (strcasecmp(val, "40below") == 0)
6346 sec_ch = SEC_CH_40BELOW;
6347 else {
6348 send_resp(dut, conn, SIGMA_ERROR,
6349 "ErrorCode,Unknown SecChOffset value");
6350 return 0;
6351 }
6352 }
6353
6354 if (chsm == CHSM_NOT_SET) {
6355 /* no offchannel changes requested */
6356 return 1;
6357 }
6358
6359 if (strcmp(intf, get_main_ifname()) != 0 &&
6360 strcmp(intf, get_station_ifname()) != 0) {
6361 send_resp(dut, conn, SIGMA_ERROR,
6362 "ErrorCode,Unknown interface");
6363 return 0;
6364 }
6365
6366 switch (chsm) {
6367 case CHSM_NOT_SET:
6368 break;
6369 case CHSM_ENABLE:
6370 if (off_ch_num < 0) {
6371 send_resp(dut, conn, SIGMA_ERROR,
6372 "ErrorCode,Missing OffChNum argument");
6373 return 0;
6374 }
6375 if (wifi_chip_type == DRIVER_WCN) {
6376 res = tdls_set_offchannel_offset(dut, conn, intf,
6377 off_ch_num, sec_ch);
6378 } else {
6379 res = iwpriv_set_offchan(dut, conn, intf, off_ch_num,
6380 sec_ch);
6381 }
6382 if (res != 1)
6383 return res;
6384 if (wifi_chip_type == DRIVER_WCN)
6385 res = tdls_set_offchannel_mode(dut, conn, intf, 1);
6386 else
6387 res = iwpriv_tdlsoffchnmode(dut, conn, intf, 1);
6388 break;
6389 case CHSM_DISABLE:
6390 if (wifi_chip_type == DRIVER_WCN)
6391 res = tdls_set_offchannel_mode(dut, conn, intf, 2);
6392 else
6393 res = iwpriv_tdlsoffchnmode(dut, conn, intf, 2);
6394 break;
6395 case CHSM_REJREQ:
6396 if (wifi_chip_type == DRIVER_WCN)
6397 res = tdls_set_offchannel_mode(dut, conn, intf, 3);
6398 else
6399 res = iwpriv_tdlsoffchnmode(dut, conn, intf, 3);
6400 break;
6401 case CHSM_UNSOLRESP:
6402 if (off_ch_num < 0) {
6403 send_resp(dut, conn, SIGMA_ERROR,
6404 "ErrorCode,Missing OffChNum argument");
6405 return 0;
6406 }
6407 if (wifi_chip_type == DRIVER_WCN) {
6408 res = tdls_set_offchannel_offset(dut, conn, intf,
6409 off_ch_num, sec_ch);
6410 } else {
6411 res = iwpriv_set_offchan(dut, conn, intf, off_ch_num,
6412 sec_ch);
6413 }
6414 if (res != 1)
6415 return res;
6416 if (wifi_chip_type == DRIVER_WCN)
6417 res = tdls_set_offchannel_mode(dut, conn, intf, 4);
6418 else
6419 res = iwpriv_tdlsoffchnmode(dut, conn, intf, 4);
6420 break;
6421 }
6422
6423 return res;
6424}
6425
6426
6427static int ath_sta_set_rfeature_vht(const char *intf, struct sigma_dut *dut,
6428 struct sigma_conn *conn,
6429 struct sigma_cmd *cmd)
6430{
6431 const char *val;
6432 char *token, *result;
6433
6434 val = get_param(cmd, "nss_mcs_opt");
6435 if (val) {
6436 /* String (nss_operating_mode; mcs_operating_mode) */
6437 int nss, mcs;
6438 char buf[50];
6439
6440 token = strdup(val);
6441 if (!token)
6442 return 0;
6443 result = strtok(token, ";");
6444 if (strcasecmp(result, "def") != 0) {
6445 nss = atoi(result);
6446 if (nss == 4)
6447 ath_disable_txbf(dut, intf);
6448 snprintf(buf, sizeof(buf), "iwpriv %s nss %d",
6449 intf, nss);
6450 if (system(buf) != 0) {
6451 sigma_dut_print(dut, DUT_MSG_ERROR,
6452 "iwpriv nss failed");
6453 goto failed;
6454 }
6455 }
6456
6457 result = strtok(NULL, ";");
6458 if (strcasecmp(result, "def") == 0) {
6459 snprintf(buf, sizeof(buf), "iwpriv %s set11NRates 0",
6460 intf);
6461 if (system(buf) != 0) {
6462 sigma_dut_print(dut, DUT_MSG_ERROR,
6463 "iwpriv set11NRates failed");
6464 goto failed;
6465 }
6466
6467 } else {
6468 mcs = atoi(result);
6469 snprintf(buf, sizeof(buf), "iwpriv %s vhtmcs %d",
6470 intf, mcs);
6471 if (system(buf) != 0) {
6472 sigma_dut_print(dut, DUT_MSG_ERROR,
6473 "iwpriv vhtmcs failed");
6474 goto failed;
6475 }
6476 }
6477 /* Channel width gets messed up, fix this */
6478 snprintf(buf, sizeof(buf), "iwpriv %s chwidth %d",
6479 intf, dut->chwidth);
6480 if (system(buf) != 0) {
6481 sigma_dut_print(dut, DUT_MSG_ERROR,
6482 "iwpriv chwidth failed");
6483 }
6484 }
6485
6486 return 1;
6487failed:
6488 free(token);
6489 return 0;
6490}
6491
6492
6493static int cmd_sta_set_rfeature_vht(const char *intf, struct sigma_dut *dut,
6494 struct sigma_conn *conn,
6495 struct sigma_cmd *cmd)
6496{
6497 switch (get_driver_type()) {
6498 case DRIVER_ATHEROS:
6499 return ath_sta_set_rfeature_vht(intf, dut, conn, cmd);
6500 default:
6501 send_resp(dut, conn, SIGMA_ERROR,
6502 "errorCode,Unsupported sta_set_rfeature(VHT) with the current driver");
6503 return 0;
6504 }
6505}
6506
6507
6508static int cmd_sta_set_rfeature(struct sigma_dut *dut, struct sigma_conn *conn,
6509 struct sigma_cmd *cmd)
6510{
6511 const char *intf = get_param(cmd, "Interface");
6512 const char *prog = get_param(cmd, "Prog");
6513
6514 if (intf == NULL || prog == NULL)
6515 return -1;
6516
6517 if (strcasecmp(prog, "TDLS") == 0)
6518 return cmd_sta_set_rfeature_tdls(intf, dut, conn, cmd);
6519
6520 if (strcasecmp(prog, "VHT") == 0)
6521 return cmd_sta_set_rfeature_vht(intf, dut, conn, cmd);
6522
6523 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Unsupported Prog");
6524 return 0;
6525}
6526
6527
6528static int cmd_sta_set_radio(struct sigma_dut *dut, struct sigma_conn *conn,
6529 struct sigma_cmd *cmd)
6530{
6531 const char *intf = get_param(cmd, "Interface");
6532 const char *mode = get_param(cmd, "Mode");
6533 int res;
6534
6535 if (intf == NULL || mode == NULL)
6536 return -1;
6537
6538 if (strcasecmp(mode, "On") == 0)
6539 res = wpa_command(intf, "SET radio_disabled 0");
6540 else if (strcasecmp(mode, "Off") == 0)
6541 res = wpa_command(intf, "SET radio_disabled 1");
6542 else
6543 return -1;
6544
6545 if (res) {
6546 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to change "
6547 "radio mode");
6548 return 0;
6549 }
6550
6551 return 1;
6552}
6553
6554
6555static int cmd_sta_set_pwrsave(struct sigma_dut *dut, struct sigma_conn *conn,
6556 struct sigma_cmd *cmd)
6557{
6558 const char *intf = get_param(cmd, "Interface");
6559 const char *mode = get_param(cmd, "Mode");
6560 int res;
6561
6562 if (intf == NULL || mode == NULL)
6563 return -1;
6564
6565 if (strcasecmp(mode, "On") == 0)
6566 res = set_ps(intf, dut, 1);
6567 else if (strcasecmp(mode, "Off") == 0)
6568 res = set_ps(intf, dut, 0);
6569 else
6570 return -1;
6571
6572 if (res) {
6573 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to change "
6574 "power save mode");
6575 return 0;
6576 }
6577
6578 return 1;
6579}
6580
6581
6582static int cmd_sta_bssid_pool(struct sigma_dut *dut, struct sigma_conn *conn,
6583 struct sigma_cmd *cmd)
6584{
6585 const char *intf = get_param(cmd, "Interface");
6586 const char *val, *bssid;
6587 int res;
6588 char *buf;
6589 size_t buf_len;
6590
6591 val = get_param(cmd, "BSSID_FILTER");
6592 if (val == NULL)
6593 return -1;
6594
6595 bssid = get_param(cmd, "BSSID_List");
6596 if (atoi(val) == 0 || bssid == NULL) {
6597 /* Disable BSSID filter */
6598 if (wpa_command(intf, "SET bssid_filter ")) {
6599 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed "
6600 "to disable BSSID filter");
6601 return 0;
6602 }
6603
6604 return 1;
6605 }
6606
6607 buf_len = 100 + strlen(bssid);
6608 buf = malloc(buf_len);
6609 if (buf == NULL)
6610 return -1;
6611
6612 snprintf(buf, buf_len, "SET bssid_filter %s", bssid);
6613 res = wpa_command(intf, buf);
6614 free(buf);
6615 if (res) {
6616 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to enable "
6617 "BSSID filter");
6618 return 0;
6619 }
6620
6621 return 1;
6622}
6623
6624
6625static int cmd_sta_reset_parm(struct sigma_dut *dut, struct sigma_conn *conn,
6626 struct sigma_cmd *cmd)
6627{
6628 const char *intf = get_param(cmd, "Interface");
6629 const char *val;
6630
6631 /* TODO: ARP */
6632
6633 val = get_param(cmd, "HS2_CACHE_PROFILE");
6634 if (val && strcasecmp(val, "All") == 0)
6635 hs2_clear_credentials(intf);
6636
6637 return 1;
6638}
6639
6640
6641static int cmd_sta_get_key(struct sigma_dut *dut, struct sigma_conn *conn,
6642 struct sigma_cmd *cmd)
6643{
6644 const char *intf = get_param(cmd, "Interface");
6645 const char *key_type = get_param(cmd, "KeyType");
6646 char buf[100], resp[200];
6647
6648 if (key_type == NULL)
6649 return -1;
6650
6651 if (strcasecmp(key_type, "GTK") == 0) {
6652 if (wpa_command_resp(intf, "GET gtk", buf, sizeof(buf)) < 0 ||
6653 strncmp(buf, "FAIL", 4) == 0) {
6654 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could "
6655 "not fetch current GTK");
6656 return 0;
6657 }
6658 snprintf(resp, sizeof(resp), "KeyValue,%s", buf);
6659 send_resp(dut, conn, SIGMA_COMPLETE, resp);
6660 return 0;
6661 } else {
6662 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Unsupported "
6663 "KeyType");
6664 return 0;
6665 }
6666
6667 return 1;
6668}
6669
6670
6671static int hs2_set_policy(struct sigma_dut *dut)
6672{
6673#ifdef ANDROID
6674 system("ip rule del prio 23000");
6675 if (system("ip rule add from all lookup main prio 23000") != 0) {
6676 sigma_dut_print(dut, DUT_MSG_ERROR,
6677 "Failed to run:ip rule add from all lookup main prio");
6678 return -1;
6679 }
6680 if (system("ip route flush cache") != 0) {
6681 sigma_dut_print(dut, DUT_MSG_ERROR,
6682 "Failed to run ip route flush cache");
6683 return -1;
6684 }
6685 return 1;
6686#else /* ANDROID */
6687 return 0;
6688#endif /* ANDROID */
6689}
6690
6691
6692static int cmd_sta_hs2_associate(struct sigma_dut *dut,
6693 struct sigma_conn *conn,
6694 struct sigma_cmd *cmd)
6695{
6696 const char *intf = get_param(cmd, "Interface");
6697 const char *val = get_param(cmd, "Ignore_blacklist");
6698 struct wpa_ctrl *ctrl;
6699 int res;
6700 char bssid[20], ssid[40], resp[100], buf[100], blacklisted[100];
6701 int tries = 0;
6702 int ignore_blacklist = 0;
6703 const char *events[] = {
6704 "CTRL-EVENT-CONNECTED",
6705 "INTERWORKING-BLACKLISTED",
6706 "INTERWORKING-NO-MATCH",
6707 NULL
6708 };
6709
6710 start_sta_mode(dut);
6711
6712 blacklisted[0] = '\0';
6713 if (val && atoi(val))
6714 ignore_blacklist = 1;
6715
6716try_again:
6717 ctrl = open_wpa_mon(intf);
6718 if (ctrl == NULL) {
6719 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to open "
6720 "wpa_supplicant monitor connection");
6721 return -2;
6722 }
6723
6724 tries++;
6725 if (wpa_command(intf, "INTERWORKING_SELECT auto")) {
6726 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to start "
6727 "Interworking connection");
6728 wpa_ctrl_detach(ctrl);
6729 wpa_ctrl_close(ctrl);
6730 return 0;
6731 }
6732
6733 buf[0] = '\0';
6734 while (1) {
6735 char *pos;
6736 res = get_wpa_cli_events(dut, ctrl, events, buf, sizeof(buf));
6737 pos = strstr(buf, "INTERWORKING-BLACKLISTED");
6738 if (!pos)
6739 break;
6740 pos += 25;
6741 sigma_dut_print(dut, DUT_MSG_DEBUG, "Found blacklisted AP: %s",
6742 pos);
6743 if (!blacklisted[0])
6744 memcpy(blacklisted, pos, strlen(pos) + 1);
6745 }
6746
6747 if (ignore_blacklist && blacklisted[0]) {
6748 char *end;
6749 end = strchr(blacklisted, ' ');
6750 if (end)
6751 *end = '\0';
6752 sigma_dut_print(dut, DUT_MSG_DEBUG, "Try to connect to a blacklisted network: %s",
6753 blacklisted);
6754 snprintf(buf, sizeof(buf), "INTERWORKING_CONNECT %s",
6755 blacklisted);
6756 if (wpa_command(intf, buf)) {
6757 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to start Interworking connection to blacklisted network");
6758 wpa_ctrl_detach(ctrl);
6759 wpa_ctrl_close(ctrl);
6760 return 0;
6761 }
6762 res = get_wpa_cli_event(dut, ctrl, "CTRL-EVENT-CONNECTED",
6763 buf, sizeof(buf));
6764 }
6765
6766 wpa_ctrl_detach(ctrl);
6767 wpa_ctrl_close(ctrl);
6768
6769 if (res < 0) {
6770 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Could not "
6771 "connect");
6772 return 0;
6773 }
6774
6775 if (strstr(buf, "INTERWORKING-NO-MATCH") ||
6776 strstr(buf, "INTERWORKING-BLACKLISTED")) {
6777 if (tries < 2) {
6778 sigma_dut_print(dut, DUT_MSG_INFO, "No match found - try again to verify no APs were missed in the scan");
6779 goto try_again;
6780 }
6781 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,No network with "
6782 "matching credentials found");
6783 return 0;
6784 }
6785
6786 if (get_wpa_status(intf, "bssid", bssid, sizeof(bssid)) < 0 ||
6787 get_wpa_status(intf, "ssid", ssid, sizeof(ssid)) < 0) {
6788 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Could not "
6789 "get current BSSID/SSID");
6790 return 0;
6791 }
6792
6793 snprintf(resp, sizeof(resp), "SSID,%s,BSSID,%s", ssid, bssid);
6794 send_resp(dut, conn, SIGMA_COMPLETE, resp);
6795 hs2_set_policy(dut);
6796 return 0;
6797}
6798
6799
6800static int sta_add_credential_uname_pwd(struct sigma_dut *dut,
6801 struct sigma_conn *conn,
6802 const char *ifname,
6803 struct sigma_cmd *cmd)
6804{
6805 const char *val;
6806 int id;
6807
6808 id = add_cred(ifname);
6809 if (id < 0)
6810 return -2;
6811 sigma_dut_print(dut, DUT_MSG_DEBUG, "Adding credential %d", id);
6812
6813 val = get_param(cmd, "prefer");
6814 if (val && atoi(val) > 0)
6815 set_cred(ifname, id, "priority", "1");
6816
6817 val = get_param(cmd, "REALM");
6818 if (val && set_cred_quoted(ifname, id, "realm", val) < 0) {
6819 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not set "
6820 "realm");
6821 return 0;
6822 }
6823
6824 val = get_param(cmd, "HOME_FQDN");
6825 if (val && set_cred_quoted(ifname, id, "domain", val) < 0) {
6826 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not set "
6827 "home_fqdn");
6828 return 0;
6829 }
6830
6831 val = get_param(cmd, "Username");
6832 if (val && set_cred_quoted(ifname, id, "username", val) < 0) {
6833 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not set "
6834 "username");
6835 return 0;
6836 }
6837
6838 val = get_param(cmd, "Password");
6839 if (val && set_cred_quoted(ifname, id, "password", val) < 0) {
6840 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not set "
6841 "password");
6842 return 0;
6843 }
6844
6845 val = get_param(cmd, "ROOT_CA");
6846 if (val) {
6847 char fname[200];
6848 snprintf(fname, sizeof(fname), "%s/%s", sigma_cert_path, val);
6849#ifdef __linux__
6850 if (!file_exists(fname)) {
6851 char msg[300];
6852 snprintf(msg, sizeof(msg), "ErrorCode,ROOT_CA "
6853 "file (%s) not found", fname);
6854 send_resp(dut, conn, SIGMA_ERROR, msg);
6855 return 0;
6856 }
6857#endif /* __linux__ */
6858 if (set_cred_quoted(ifname, id, "ca_cert", fname) < 0) {
6859 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could "
6860 "not set root CA");
6861 return 0;
6862 }
6863 }
6864
6865 return 1;
6866}
6867
6868
6869static int update_devdetail_imsi(struct sigma_dut *dut, const char *imsi)
6870{
6871 FILE *in, *out;
6872 char buf[500];
6873 int found = 0;
6874
6875 in = fopen("devdetail.xml", "r");
6876 if (in == NULL)
6877 return -1;
6878 out = fopen("devdetail.xml.tmp", "w");
6879 if (out == NULL) {
6880 fclose(in);
6881 return -1;
6882 }
6883
6884 while (fgets(buf, sizeof(buf), in)) {
6885 char *pos = strstr(buf, "<IMSI>");
6886 if (pos) {
6887 sigma_dut_print(dut, DUT_MSG_INFO, "Updated DevDetail IMSI to %s",
6888 imsi);
6889 pos += 6;
6890 *pos = '\0';
6891 fprintf(out, "%s%s</IMSI>\n", buf, imsi);
6892 found++;
6893 } else {
6894 fprintf(out, "%s", buf);
6895 }
6896 }
6897
6898 fclose(out);
6899 fclose(in);
6900 if (found)
6901 rename("devdetail.xml.tmp", "devdetail.xml");
6902 else
6903 unlink("devdetail.xml.tmp");
6904
6905 return 0;
6906}
6907
6908
6909static int sta_add_credential_sim(struct sigma_dut *dut,
6910 struct sigma_conn *conn,
6911 const char *ifname, struct sigma_cmd *cmd)
6912{
6913 const char *val, *imsi = NULL;
6914 int id;
6915 char buf[200];
6916 int res;
6917 const char *pos;
6918 size_t mnc_len;
6919 char plmn_mcc[4];
6920 char plmn_mnc[4];
6921
6922 id = add_cred(ifname);
6923 if (id < 0)
6924 return -2;
6925 sigma_dut_print(dut, DUT_MSG_DEBUG, "Adding credential %d", id);
6926
6927 val = get_param(cmd, "prefer");
6928 if (val && atoi(val) > 0)
6929 set_cred(ifname, id, "priority", "1");
6930
6931 val = get_param(cmd, "PLMN_MCC");
6932 if (val == NULL) {
6933 send_resp(dut, conn, SIGMA_ERROR,
6934 "errorCode,Missing PLMN_MCC");
6935 return 0;
6936 }
6937 if (strlen(val) != 3) {
6938 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Invalid MCC");
6939 return 0;
6940 }
6941 snprintf(plmn_mcc, sizeof(plmn_mcc), "%s", val);
6942
6943 val = get_param(cmd, "PLMN_MNC");
6944 if (val == NULL) {
6945 send_resp(dut, conn, SIGMA_ERROR,
6946 "errorCode,Missing PLMN_MNC");
6947 return 0;
6948 }
6949 if (strlen(val) != 2 && strlen(val) != 3) {
6950 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Invalid MNC");
6951 return 0;
6952 }
6953 snprintf(plmn_mnc, sizeof(plmn_mnc), "%s", val);
6954
6955 val = get_param(cmd, "IMSI");
6956 if (val == NULL) {
6957 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Missing SIM "
6958 "IMSI");
6959 return 0;
6960 }
6961
6962 imsi = pos = val;
6963
6964 if (strncmp(plmn_mcc, pos, 3) != 0) {
6965 send_resp(dut, conn, SIGMA_ERROR, "errorCode,MCC mismatch");
6966 return 0;
6967 }
6968 pos += 3;
6969
6970 mnc_len = strlen(plmn_mnc);
6971 if (mnc_len < 2) {
6972 send_resp(dut, conn, SIGMA_ERROR, "errorCode,MNC not set");
6973 return 0;
6974 }
6975
6976 if (strncmp(plmn_mnc, pos, mnc_len) != 0) {
6977 send_resp(dut, conn, SIGMA_ERROR, "errorCode,MNC mismatch");
6978 return 0;
6979 }
6980 pos += mnc_len;
6981
6982 res = snprintf(buf, sizeof(buf), "%s%s-%s",plmn_mcc, plmn_mnc, pos);
6983 if (res < 0 || res >= (int) sizeof(buf))
6984 return -1;
6985 if (set_cred_quoted(ifname, id, "imsi", buf) < 0) {
6986 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could "
6987 "not set IMSI");
6988 return 0;
6989 }
6990
6991 val = get_param(cmd, "Password");
6992 if (val && set_cred_quoted(ifname, id, "milenage", val) < 0) {
6993 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could "
6994 "not set password");
6995 return 0;
6996 }
6997
6998 if (dut->program == PROGRAM_HS2_R2) {
6999 /*
7000 * Set provisioning_sp for the test cases where SIM/USIM
7001 * provisioning is used.
7002 */
7003 if (val && set_cred_quoted(ifname, id, "provisioning_sp",
7004 "wi-fi.org") < 0) {
7005 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could "
7006 "not set provisioning_sp");
7007 return 0;
7008 }
7009
7010 update_devdetail_imsi(dut, imsi);
7011 }
7012
7013 return 1;
7014}
7015
7016
7017static int sta_add_credential_cert(struct sigma_dut *dut,
7018 struct sigma_conn *conn,
7019 const char *ifname,
7020 struct sigma_cmd *cmd)
7021{
7022 const char *val;
7023 int id;
7024
7025 id = add_cred(ifname);
7026 if (id < 0)
7027 return -2;
7028 sigma_dut_print(dut, DUT_MSG_DEBUG, "Adding credential %d", id);
7029
7030 val = get_param(cmd, "prefer");
7031 if (val && atoi(val) > 0)
7032 set_cred(ifname, id, "priority", "1");
7033
7034 val = get_param(cmd, "REALM");
7035 if (val && set_cred_quoted(ifname, id, "realm", val) < 0) {
7036 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not set "
7037 "realm");
7038 return 0;
7039 }
7040
7041 val = get_param(cmd, "HOME_FQDN");
7042 if (val && set_cred_quoted(ifname, id, "domain", val) < 0) {
7043 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not set "
7044 "home_fqdn");
7045 return 0;
7046 }
7047
7048 val = get_param(cmd, "Username");
7049 if (val && set_cred_quoted(ifname, id, "username", val) < 0) {
7050 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not set "
7051 "username");
7052 return 0;
7053 }
7054
7055 val = get_param(cmd, "clientCertificate");
7056 if (val) {
7057 char fname[200];
7058 snprintf(fname, sizeof(fname), "%s/%s", sigma_cert_path, val);
7059#ifdef __linux__
7060 if (!file_exists(fname)) {
7061 char msg[300];
7062 snprintf(msg, sizeof(msg),
7063 "ErrorCode,clientCertificate "
7064 "file (%s) not found", fname);
7065 send_resp(dut, conn, SIGMA_ERROR, msg);
7066 return 0;
7067 }
7068#endif /* __linux__ */
7069 if (set_cred_quoted(ifname, id, "client_cert", fname) < 0) {
7070 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could "
7071 "not set client_cert");
7072 return 0;
7073 }
7074 if (set_cred_quoted(ifname, id, "private_key", fname) < 0) {
7075 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could "
7076 "not set private_key");
7077 return 0;
7078 }
7079 }
7080
7081 val = get_param(cmd, "ROOT_CA");
7082 if (val) {
7083 char fname[200];
7084 snprintf(fname, sizeof(fname), "%s/%s", sigma_cert_path, val);
7085#ifdef __linux__
7086 if (!file_exists(fname)) {
7087 char msg[300];
7088 snprintf(msg, sizeof(msg), "ErrorCode,ROOT_CA "
7089 "file (%s) not found", fname);
7090 send_resp(dut, conn, SIGMA_ERROR, msg);
7091 return 0;
7092 }
7093#endif /* __linux__ */
7094 if (set_cred_quoted(ifname, id, "ca_cert", fname) < 0) {
7095 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could "
7096 "not set root CA");
7097 return 0;
7098 }
7099 }
7100
7101 return 1;
7102}
7103
7104
7105static int cmd_sta_add_credential(struct sigma_dut *dut,
7106 struct sigma_conn *conn,
7107 struct sigma_cmd *cmd)
7108{
7109 const char *intf = get_param(cmd, "Interface");
7110 const char *type;
7111
7112 start_sta_mode(dut);
7113
7114 type = get_param(cmd, "Type");
7115 if (!type)
7116 return -1;
7117
7118 if (strcasecmp(type, "uname_pwd") == 0)
7119 return sta_add_credential_uname_pwd(dut, conn, intf, cmd);
7120
7121 if (strcasecmp(type, "sim") == 0)
7122 return sta_add_credential_sim(dut, conn, intf, cmd);
7123
7124 if (strcasecmp(type, "cert") == 0)
7125 return sta_add_credential_cert(dut, conn, intf, cmd);
7126
7127 send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Unsupported credential "
7128 "type");
7129 return 0;
7130}
7131
7132
7133static int cmd_sta_scan(struct sigma_dut *dut, struct sigma_conn *conn,
7134 struct sigma_cmd *cmd)
7135{
7136 const char *intf = get_param(cmd, "Interface");
7137 const char *val;
7138 char buf[100];
7139 int res;
7140
7141 val = get_param(cmd, "HESSID");
7142 if (val) {
7143 res = snprintf(buf, sizeof(buf), "SET hessid %s", val);
7144 if (res < 0 || res >= (int) sizeof(buf))
7145 return -1;
7146 wpa_command(intf, buf);
7147 }
7148
7149 val = get_param(cmd, "ACCS_NET_TYPE");
7150 if (val) {
7151 res = snprintf(buf, sizeof(buf), "SET access_network_type %s",
7152 val);
7153 if (res < 0 || res >= (int) sizeof(buf))
7154 return -1;
7155 wpa_command(intf, buf);
7156 }
7157
7158 if (wpa_command(intf, "SCAN")) {
7159 send_resp(dut, conn, SIGMA_ERROR, "errorCode,Could not start "
7160 "scan");
7161 return 0;
7162 }
7163
7164 return 1;
7165}
7166
7167
7168static int cmd_sta_set_systime(struct sigma_dut *dut, struct sigma_conn *conn,
7169 struct sigma_cmd *cmd)
7170{
7171#ifdef __linux__
7172 struct timeval tv;
7173 struct tm tm;
7174 time_t t;
7175 const char *val;
7176
7177 wpa_command(get_station_ifname(), "PMKSA_FLUSH");
7178
7179 memset(&tm, 0, sizeof(tm));
7180 val = get_param(cmd, "seconds");
7181 if (val)
7182 tm.tm_sec = atoi(val);
7183 val = get_param(cmd, "minutes");
7184 if (val)
7185 tm.tm_min = atoi(val);
7186 val = get_param(cmd, "hours");
7187 if (val)
7188 tm.tm_hour = atoi(val);
7189 val = get_param(cmd, "date");
7190 if (val)
7191 tm.tm_mday = atoi(val);
7192 val = get_param(cmd, "month");
7193 if (val)
7194 tm.tm_mon = atoi(val) - 1;
7195 val = get_param(cmd, "year");
7196 if (val) {
7197 int year = atoi(val);
7198#ifdef ANDROID
7199 if (year > 2035)
7200 year = 2035; /* years beyond 2035 not supported */
7201#endif /* ANDROID */
7202 tm.tm_year = year - 1900;
7203 }
7204 t = mktime(&tm);
7205 if (t == (time_t) -1) {
7206 send_resp(dut, conn, SIGMA_ERROR,
7207 "errorCode,Invalid date or time");
7208 return 0;
7209 }
7210
7211 memset(&tv, 0, sizeof(tv));
7212 tv.tv_sec = t;
7213
7214 if (settimeofday(&tv, NULL) < 0) {
7215 sigma_dut_print(dut, DUT_MSG_INFO, "settimeofday failed: %s",
7216 strerror(errno));
7217 send_resp(dut, conn, SIGMA_ERROR,
7218 "errorCode,Failed to set time");
7219 return 0;
7220 }
7221
7222 return 1;
7223#endif /* __linux__ */
7224
7225 return -1;
7226}
7227
7228
7229static int cmd_sta_osu(struct sigma_dut *dut, struct sigma_conn *conn,
7230 struct sigma_cmd *cmd)
7231{
7232 const char *intf = get_param(cmd, "Interface");
7233 const char *name, *val;
7234 int prod_ess_assoc = 1;
7235 char buf[200], bssid[100], ssid[100];
7236 int res;
7237 struct wpa_ctrl *ctrl;
7238
7239 name = get_param(cmd, "osuFriendlyName");
7240
7241 val = get_param(cmd, "ProdESSAssoc");
7242 if (val)
7243 prod_ess_assoc = atoi(val);
7244
7245 kill_dhcp_client(dut, intf);
7246 if (start_dhcp_client(dut, intf) < 0)
7247 return -2;
7248
7249 sigma_dut_print(dut, DUT_MSG_DEBUG, "Trigger OSU");
7250 mkdir("Logs", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
7251 res = snprintf(buf, sizeof(buf),
7252 "%s %s%s%s signup osu-ca.pem",
7253 prod_ess_assoc ? "" : "-N",
7254 name ? "-O'" : "", name ? name : "",
7255 name ? "'" : "");
7256
7257 if (run_hs20_osu(dut, buf) < 0) {
7258 FILE *f;
7259
7260 sigma_dut_print(dut, DUT_MSG_INFO, "Failed to complete OSU");
7261
7262 f = fopen("hs20-osu-client.res", "r");
7263 if (f) {
7264 char resp[400], res[300], *pos;
7265 if (!fgets(res, sizeof(res), f))
7266 res[0] = '\0';
7267 pos = strchr(res, '\n');
7268 if (pos)
7269 *pos = '\0';
7270 fclose(f);
7271 sigma_dut_summary(dut, "hs20-osu-client provisioning failed: %s",
7272 res);
7273 snprintf(resp, sizeof(resp), "notify-send '%s'", res);
7274 if (system(resp) != 0) {
7275 }
7276 snprintf(resp, sizeof(resp),
7277 "SSID,,BSSID,,failureReason,%s", res);
7278 send_resp(dut, conn, SIGMA_COMPLETE, resp);
7279 return 0;
7280 }
7281
7282 send_resp(dut, conn, SIGMA_COMPLETE, "SSID,,BSSID,");
7283 return 0;
7284 }
7285
7286 if (!prod_ess_assoc)
7287 goto report;
7288
7289 ctrl = open_wpa_mon(intf);
7290 if (ctrl == NULL) {
7291 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to open "
7292 "wpa_supplicant monitor connection");
7293 return -1;
7294 }
7295
7296 res = get_wpa_cli_event(dut, ctrl, "CTRL-EVENT-CONNECTED",
7297 buf, sizeof(buf));
7298
7299 wpa_ctrl_detach(ctrl);
7300 wpa_ctrl_close(ctrl);
7301
7302 if (res < 0) {
7303 sigma_dut_print(dut, DUT_MSG_INFO, "Failed to connect to "
7304 "network after OSU");
7305 send_resp(dut, conn, SIGMA_COMPLETE, "SSID,,BSSID,");
7306 return 0;
7307 }
7308
7309report:
7310 if (get_wpa_status(intf, "bssid", bssid, sizeof(bssid)) < 0 ||
7311 get_wpa_status(intf, "ssid", ssid, sizeof(ssid)) < 0) {
7312 sigma_dut_print(dut, DUT_MSG_INFO, "Failed to get BSSID/SSID");
7313 send_resp(dut, conn, SIGMA_COMPLETE, "SSID,,BSSID,");
7314 return 0;
7315 }
7316
7317 snprintf(buf, sizeof(buf), "SSID,%s,BSSID,%s", ssid, bssid);
7318 send_resp(dut, conn, SIGMA_COMPLETE, buf);
7319 hs2_set_policy(dut);
7320 return 0;
7321}
7322
7323
7324static int cmd_sta_policy_update(struct sigma_dut *dut, struct sigma_conn *conn,
7325 struct sigma_cmd *cmd)
7326{
7327 const char *val;
7328 int timeout = 120;
7329
7330 val = get_param(cmd, "PolicyUpdate");
7331 if (val == NULL || atoi(val) == 0)
7332 return 1; /* No operation requested */
7333
7334 val = get_param(cmd, "Timeout");
7335 if (val)
7336 timeout = atoi(val);
7337
7338 if (timeout) {
7339 /* TODO: time out the command and return
7340 * PolicyUpdateStatus,TIMEOUT if needed. */
7341 }
7342
7343 sigma_dut_print(dut, DUT_MSG_DEBUG, "Trigger policy update");
7344 mkdir("Logs", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
7345 if (run_hs20_osu(dut, "pol_upd fqdn=wi-fi.org") < 0) {
7346 send_resp(dut, conn, SIGMA_COMPLETE, "PolicyUpdateStatus,FAIL");
7347 return 0;
7348 }
7349
7350 send_resp(dut, conn, SIGMA_COMPLETE, "PolicyUpdateStatus,SUCCESS");
7351 return 0;
7352}
7353
7354
7355static int cmd_sta_er_config(struct sigma_dut *dut, struct sigma_conn *conn,
7356 struct sigma_cmd *cmd)
7357{
7358 struct wpa_ctrl *ctrl;
7359 const char *intf = get_param(cmd, "Interface");
7360 const char *bssid = get_param(cmd, "Bssid");
7361 const char *ssid = get_param(cmd, "SSID");
7362 const char *security = get_param(cmd, "Security");
7363 const char *passphrase = get_param(cmd, "Passphrase");
7364 const char *pin = get_param(cmd, "PIN");
7365 char buf[1000];
7366 char ssid_hex[200], passphrase_hex[200];
7367 const char *keymgmt, *cipher;
7368
7369 if (intf == NULL)
7370 intf = get_main_ifname();
7371
7372 if (!bssid) {
7373 send_resp(dut, conn, SIGMA_ERROR,
7374 "ErrorCode,Missing Bssid argument");
7375 return 0;
7376 }
7377
7378 if (!ssid) {
7379 send_resp(dut, conn, SIGMA_ERROR,
7380 "ErrorCode,Missing SSID argument");
7381 return 0;
7382 }
7383
7384 if (!security) {
7385 send_resp(dut, conn, SIGMA_ERROR,
7386 "ErrorCode,Missing Security argument");
7387 return 0;
7388 }
7389
7390 if (!passphrase) {
7391 send_resp(dut, conn, SIGMA_ERROR,
7392 "ErrorCode,Missing Passphrase argument");
7393 return 0;
7394 }
7395
7396 if (!pin) {
7397 send_resp(dut, conn, SIGMA_ERROR,
7398 "ErrorCode,Missing PIN argument");
7399 return 0;
7400 }
7401
7402 if (strlen(ssid) >= 2 * sizeof(ssid_hex) ||
7403 strlen(passphrase) >= 2 * sizeof(passphrase_hex)) {
7404 send_resp(dut, conn, SIGMA_ERROR,
7405 "ErrorCode,Too long SSID/passphrase");
7406 return 0;
7407 }
7408
7409 ctrl = open_wpa_mon(intf);
7410 if (ctrl == NULL) {
7411 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to open "
7412 "wpa_supplicant monitor connection");
7413 return -2;
7414 }
7415
7416 if (strcasecmp(security, "wpa2-psk") == 0) {
7417 keymgmt = "WPA2PSK";
7418 cipher = "CCMP";
7419 } else {
7420 wpa_ctrl_detach(ctrl);
7421 wpa_ctrl_close(ctrl);
7422 send_resp(dut, conn, SIGMA_ERROR,
7423 "ErrorCode,Unsupported Security value");
7424 return 0;
7425 }
7426
7427 ascii2hexstr(ssid, ssid_hex);
7428 ascii2hexstr(passphrase, passphrase_hex);
7429 snprintf(buf, sizeof(buf), "WPS_REG %s %s %s %s %s %s",
7430 bssid, pin, ssid_hex, keymgmt, cipher, passphrase_hex);
7431
7432 if (wpa_command(intf, buf) < 0) {
7433 wpa_ctrl_detach(ctrl);
7434 wpa_ctrl_close(ctrl);
7435 send_resp(dut, conn, SIGMA_ERROR,
7436 "ErrorCode,Failed to start registrar");
7437 return 0;
7438 }
7439
7440 snprintf(dut->er_oper_bssid, sizeof(dut->er_oper_bssid), "%s", bssid);
7441 dut->er_oper_performed = 1;
7442
7443 return wps_connection_event(dut, conn, ctrl, intf, 0);
7444}
7445
7446
7447static int cmd_sta_wps_connect_pw_token(struct sigma_dut *dut,
7448 struct sigma_conn *conn,
7449 struct sigma_cmd *cmd)
7450{
7451 struct wpa_ctrl *ctrl;
7452 const char *intf = get_param(cmd, "Interface");
7453 const char *bssid = get_param(cmd, "Bssid");
7454 char buf[100];
7455
7456 if (!bssid) {
7457 send_resp(dut, conn, SIGMA_ERROR,
7458 "ErrorCode,Missing Bssid argument");
7459 return 0;
7460 }
7461
7462 ctrl = open_wpa_mon(intf);
7463 if (ctrl == NULL) {
7464 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to open "
7465 "wpa_supplicant monitor connection");
7466 return -2;
7467 }
7468
7469 snprintf(buf, sizeof(buf), "WPS_NFC %s", bssid);
7470
7471 if (wpa_command(intf, buf) < 0) {
7472 wpa_ctrl_detach(ctrl);
7473 wpa_ctrl_close(ctrl);
7474 send_resp(dut, conn, SIGMA_ERROR,
7475 "ErrorCode,Failed to start registrar");
7476 return 0;
7477 }
7478
7479 return wps_connection_event(dut, conn, ctrl, intf, 0);
7480}
7481
7482
7483static int req_intf(struct sigma_cmd *cmd)
7484{
7485 return get_param(cmd, "interface") == NULL ? -1 : 0;
7486}
7487
7488
7489void sta_register_cmds(void)
7490{
7491 sigma_dut_reg_cmd("sta_get_ip_config", req_intf,
7492 cmd_sta_get_ip_config);
7493 sigma_dut_reg_cmd("sta_set_ip_config", req_intf,
7494 cmd_sta_set_ip_config);
7495 sigma_dut_reg_cmd("sta_get_info", req_intf, cmd_sta_get_info);
7496 sigma_dut_reg_cmd("sta_get_mac_address", req_intf,
7497 cmd_sta_get_mac_address);
7498 sigma_dut_reg_cmd("sta_is_connected", req_intf, cmd_sta_is_connected);
7499 sigma_dut_reg_cmd("sta_verify_ip_connection", req_intf,
7500 cmd_sta_verify_ip_connection);
7501 sigma_dut_reg_cmd("sta_get_bssid", req_intf, cmd_sta_get_bssid);
7502 sigma_dut_reg_cmd("sta_set_encryption", req_intf,
7503 cmd_sta_set_encryption);
7504 sigma_dut_reg_cmd("sta_set_psk", req_intf, cmd_sta_set_psk);
7505 sigma_dut_reg_cmd("sta_set_eaptls", req_intf, cmd_sta_set_eaptls);
7506 sigma_dut_reg_cmd("sta_set_eapttls", req_intf, cmd_sta_set_eapttls);
7507 sigma_dut_reg_cmd("sta_set_eapsim", req_intf, cmd_sta_set_eapsim);
7508 sigma_dut_reg_cmd("sta_set_peap", req_intf, cmd_sta_set_peap);
7509 sigma_dut_reg_cmd("sta_set_eapfast", req_intf, cmd_sta_set_eapfast);
7510 sigma_dut_reg_cmd("sta_set_eapaka", req_intf, cmd_sta_set_eapaka);
7511 sigma_dut_reg_cmd("sta_set_eapakaprime", req_intf,
7512 cmd_sta_set_eapakaprime);
7513 sigma_dut_reg_cmd("sta_set_security", req_intf, cmd_sta_set_security);
7514 sigma_dut_reg_cmd("sta_set_uapsd", req_intf, cmd_sta_set_uapsd);
7515 /* TODO: sta_set_ibss */
7516 /* TODO: sta_set_mode */
7517 sigma_dut_reg_cmd("sta_set_wmm", req_intf, cmd_sta_set_wmm);
7518 sigma_dut_reg_cmd("sta_associate", req_intf, cmd_sta_associate);
7519 /* TODO: sta_up_load */
7520 sigma_dut_reg_cmd("sta_preset_testparameters", req_intf,
7521 cmd_sta_preset_testparameters);
7522 /* TODO: sta_set_system */
7523 sigma_dut_reg_cmd("sta_set_11n", req_intf, cmd_sta_set_11n);
7524 /* TODO: sta_set_rifs_test */
7525 sigma_dut_reg_cmd("sta_set_wireless", req_intf, cmd_sta_set_wireless);
7526 sigma_dut_reg_cmd("sta_send_addba", req_intf, cmd_sta_send_addba);
7527 /* TODO: sta_send_coexist_mgmt */
7528 sigma_dut_reg_cmd("sta_disconnect", req_intf, cmd_sta_disconnect);
7529 sigma_dut_reg_cmd("sta_reassoc", req_intf, cmd_sta_reassoc);
7530 sigma_dut_reg_cmd("sta_reassociate", req_intf, cmd_sta_reassoc);
7531 sigma_dut_reg_cmd("sta_reset_default", req_intf,
7532 cmd_sta_reset_default);
7533 sigma_dut_reg_cmd("sta_send_frame", req_intf, cmd_sta_send_frame);
7534 sigma_dut_reg_cmd("sta_set_macaddr", req_intf, cmd_sta_set_macaddr);
7535 sigma_dut_reg_cmd("sta_set_rfeature", req_intf, cmd_sta_set_rfeature);
7536 sigma_dut_reg_cmd("sta_set_radio", req_intf, cmd_sta_set_radio);
7537 sigma_dut_reg_cmd("sta_set_pwrsave", req_intf, cmd_sta_set_pwrsave);
7538 sigma_dut_reg_cmd("sta_bssid_pool", req_intf, cmd_sta_bssid_pool);
7539 sigma_dut_reg_cmd("sta_reset_parm", req_intf, cmd_sta_reset_parm);
7540 sigma_dut_reg_cmd("sta_get_key", req_intf, cmd_sta_get_key);
7541 sigma_dut_reg_cmd("sta_hs2_associate", req_intf,
7542 cmd_sta_hs2_associate);
7543 sigma_dut_reg_cmd("sta_add_credential", req_intf,
7544 cmd_sta_add_credential);
7545 sigma_dut_reg_cmd("sta_scan", req_intf, cmd_sta_scan);
7546 sigma_dut_reg_cmd("sta_set_systime", NULL, cmd_sta_set_systime);
7547 sigma_dut_reg_cmd("sta_osu", req_intf, cmd_sta_osu);
7548 sigma_dut_reg_cmd("sta_policy_update", req_intf, cmd_sta_policy_update);
7549 sigma_dut_reg_cmd("sta_er_config", NULL, cmd_sta_er_config);
7550 sigma_dut_reg_cmd("sta_wps_connect_pw_token", req_intf,
7551 cmd_sta_wps_connect_pw_token);
7552 sigma_dut_reg_cmd("sta_exec_action", req_intf, cmd_sta_exec_action);
7553 sigma_dut_reg_cmd("sta_get_events", req_intf, cmd_sta_get_events);
7554 sigma_dut_reg_cmd("sta_get_parameter", req_intf, cmd_sta_get_parameter);
7555}