]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/ctrl_iface.c
Sync with mac80211-next.git include/uapi/linux/nl80211.h
[thirdparty/hostap.git] / wpa_supplicant / ctrl_iface.c
CommitLineData
6fc6879b
JM
1/*
2 * WPA Supplicant / Control interface (shared code for all backends)
ed87f6a8 3 * Copyright (c) 2004-2019, Jouni Malinen <j@w1.fi>
6fc6879b 4 *
0f3d578e
JM
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
6fc6879b
JM
7 */
8
3a068632 9#include "utils/includes.h"
4a6cc862 10#ifdef CONFIG_TESTING_OPTIONS
4a6cc862
JM
11#include <netinet/ip.h>
12#endif /* CONFIG_TESTING_OPTIONS */
6fc6879b 13
3a068632
JM
14#include "utils/common.h"
15#include "utils/eloop.h"
8aaafcee 16#include "utils/uuid.h"
50a17a76 17#include "utils/module_tests.h"
acec8d32 18#include "common/version.h"
3a068632 19#include "common/ieee802_11_defs.h"
337c781f 20#include "common/ieee802_11_common.h"
3a068632 21#include "common/wpa_ctrl.h"
4b07484c 22#ifdef CONFIG_DPP
60239f60 23#include "common/dpp.h"
4b07484c 24#endif /* CONFIG_DPP */
a1651451 25#include "crypto/tls.h"
9d4ff04a 26#include "ap/hostapd.h"
3a068632
JM
27#include "eap_peer/eap.h"
28#include "eapol_supp/eapol_supp_sm.h"
3acb5005 29#include "rsn_supp/wpa.h"
3a068632
JM
30#include "rsn_supp/preauth.h"
31#include "rsn_supp/pmksa_cache.h"
32#include "l2_packet/l2_packet.h"
33#include "wps/wps.h"
df4cea89 34#include "fst/fst.h"
3794af2d 35#include "fst/fst_ctrl_iface.h"
6fc6879b 36#include "config.h"
6fc6879b 37#include "wpa_supplicant_i.h"
2d5b792d 38#include "driver_i.h"
fcc60db4 39#include "wps_supplicant.h"
11ef8d35 40#include "ibss_rsn.h"
3ec97afe 41#include "ap.h"
b563b388
JM
42#include "p2p_supplicant.h"
43#include "p2p/p2p.h"
a8918e86 44#include "hs20_supplicant.h"
9675ce35 45#include "wifi_display.h"
8bac466b 46#include "notify.h"
3a068632 47#include "bss.h"
9ba9fa07 48#include "scan.h"
3a068632 49#include "ctrl_iface.h"
afc064fe 50#include "interworking.h"
9aa10e2b 51#include "blacklist.h"
bc5d330a 52#include "autoscan.h"
e9199e31 53#include "wnm_sta.h"
60b893df 54#include "offchannel.h"
7a4a93b9 55#include "drivers/driver.h"
79070906 56#include "mesh.h"
be27e185 57#include "dpp_supplicant.h"
5c7d35ba 58#include "sme.h"
6fc6879b 59
7846e8d6
RM
60#ifdef __NetBSD__
61#include <net/if_ether.h>
62#elif !defined(__CYGWIN__) && !defined(CONFIG_NATIVE_WINDOWS)
63#include <net/ethernet.h>
64#endif
65
4b4a8ae5
JM
66static int wpa_supplicant_global_iface_list(struct wpa_global *global,
67 char *buf, int len);
6fc6879b 68static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
56e2fc2c 69 const char *input,
6fc6879b 70 char *buf, int len);
d3c9c35f
DS
71static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s,
72 char *val);
6fc6879b 73
fe3e0bac 74
d445a5cd
JM
75static int set_bssid_filter(struct wpa_supplicant *wpa_s, char *val)
76{
77 char *pos;
78 u8 addr[ETH_ALEN], *filter = NULL, *n;
79 size_t count = 0;
80
81 pos = val;
82 while (pos) {
83 if (*pos == '\0')
84 break;
1485ec07
JM
85 if (hwaddr_aton(pos, addr)) {
86 os_free(filter);
d445a5cd 87 return -1;
1485ec07 88 }
067ffa26 89 n = os_realloc_array(filter, count + 1, ETH_ALEN);
d445a5cd
JM
90 if (n == NULL) {
91 os_free(filter);
92 return -1;
93 }
94 filter = n;
95 os_memcpy(filter + count * ETH_ALEN, addr, ETH_ALEN);
96 count++;
97
98 pos = os_strchr(pos, ' ');
99 if (pos)
100 pos++;
101 }
102
103 wpa_hexdump(MSG_DEBUG, "bssid_filter", filter, count * ETH_ALEN);
104 os_free(wpa_s->bssid_filter);
105 wpa_s->bssid_filter = filter;
106 wpa_s->bssid_filter_count = count;
107
108 return 0;
109}
110
111
6407f413
JM
112static int set_disallow_aps(struct wpa_supplicant *wpa_s, char *val)
113{
114 char *pos;
115 u8 addr[ETH_ALEN], *bssid = NULL, *n;
116 struct wpa_ssid_value *ssid = NULL, *ns;
117 size_t count = 0, ssid_count = 0;
118 struct wpa_ssid *c;
119
120 /*
65015b2d 121 * disallow_list ::= <ssid_spec> | <bssid_spec> | <disallow_list> | ""
6407f413
JM
122 * SSID_SPEC ::= ssid <SSID_HEX>
123 * BSSID_SPEC ::= bssid <BSSID_HEX>
124 */
125
126 pos = val;
127 while (pos) {
128 if (*pos == '\0')
129 break;
130 if (os_strncmp(pos, "bssid ", 6) == 0) {
131 int res;
132 pos += 6;
133 res = hwaddr_aton2(pos, addr);
134 if (res < 0) {
135 os_free(ssid);
136 os_free(bssid);
137 wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
138 "BSSID value '%s'", pos);
139 return -1;
140 }
141 pos += res;
142 n = os_realloc_array(bssid, count + 1, ETH_ALEN);
143 if (n == NULL) {
144 os_free(ssid);
145 os_free(bssid);
146 return -1;
147 }
148 bssid = n;
149 os_memcpy(bssid + count * ETH_ALEN, addr, ETH_ALEN);
150 count++;
151 } else if (os_strncmp(pos, "ssid ", 5) == 0) {
152 char *end;
153 pos += 5;
154
155 end = pos;
156 while (*end) {
157 if (*end == '\0' || *end == ' ')
158 break;
159 end++;
160 }
161
162 ns = os_realloc_array(ssid, ssid_count + 1,
163 sizeof(struct wpa_ssid_value));
164 if (ns == NULL) {
165 os_free(ssid);
166 os_free(bssid);
167 return -1;
168 }
169 ssid = ns;
170
d9d1b952
JM
171 if ((end - pos) & 0x01 ||
172 end - pos > 2 * SSID_MAX_LEN ||
6407f413
JM
173 hexstr2bin(pos, ssid[ssid_count].ssid,
174 (end - pos) / 2) < 0) {
175 os_free(ssid);
176 os_free(bssid);
177 wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
178 "SSID value '%s'", pos);
179 return -1;
180 }
181 ssid[ssid_count].ssid_len = (end - pos) / 2;
182 wpa_hexdump_ascii(MSG_DEBUG, "disallow_aps SSID",
183 ssid[ssid_count].ssid,
184 ssid[ssid_count].ssid_len);
185 ssid_count++;
186 pos = end;
187 } else {
188 wpa_printf(MSG_DEBUG, "Unexpected disallow_aps value "
189 "'%s'", pos);
190 os_free(ssid);
191 os_free(bssid);
192 return -1;
193 }
194
195 pos = os_strchr(pos, ' ');
196 if (pos)
197 pos++;
198 }
199
200 wpa_hexdump(MSG_DEBUG, "disallow_aps_bssid", bssid, count * ETH_ALEN);
201 os_free(wpa_s->disallow_aps_bssid);
202 wpa_s->disallow_aps_bssid = bssid;
203 wpa_s->disallow_aps_bssid_count = count;
204
205 wpa_printf(MSG_DEBUG, "disallow_aps_ssid_count %d", (int) ssid_count);
206 os_free(wpa_s->disallow_aps_ssid);
207 wpa_s->disallow_aps_ssid = ssid;
208 wpa_s->disallow_aps_ssid_count = ssid_count;
209
210 if (!wpa_s->current_ssid || wpa_s->wpa_state < WPA_AUTHENTICATING)
211 return 0;
212
213 c = wpa_s->current_ssid;
214 if (c->mode != WPAS_MODE_INFRA && c->mode != WPAS_MODE_IBSS)
215 return 0;
216
217 if (!disallowed_bssid(wpa_s, wpa_s->bssid) &&
218 !disallowed_ssid(wpa_s, c->ssid, c->ssid_len))
219 return 0;
220
221 wpa_printf(MSG_DEBUG, "Disconnect and try to find another network "
222 "because current AP was marked disallowed");
223
224#ifdef CONFIG_SME
225 wpa_s->sme.prev_bssid_set = 0;
226#endif /* CONFIG_SME */
227 wpa_s->reassociate = 1;
c2805909 228 wpa_s->own_disconnect_req = 1;
6407f413
JM
229 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
230 wpa_supplicant_req_scan(wpa_s, 0, 0);
231
232 return 0;
233}
234
235
1120e452
JM
236#ifndef CONFIG_NO_CONFIG_BLOBS
237static int wpas_ctrl_set_blob(struct wpa_supplicant *wpa_s, char *pos)
238{
239 char *name = pos;
240 struct wpa_config_blob *blob;
241 size_t len;
242
243 pos = os_strchr(pos, ' ');
244 if (pos == NULL)
245 return -1;
246 *pos++ = '\0';
247 len = os_strlen(pos);
248 if (len & 1)
249 return -1;
250
251 wpa_printf(MSG_DEBUG, "CTRL: Set blob '%s'", name);
252 blob = os_zalloc(sizeof(*blob));
253 if (blob == NULL)
254 return -1;
255 blob->name = os_strdup(name);
256 blob->data = os_malloc(len / 2);
257 if (blob->name == NULL || blob->data == NULL) {
258 wpa_config_free_blob(blob);
259 return -1;
260 }
261
262 if (hexstr2bin(pos, blob->data, len / 2) < 0) {
263 wpa_printf(MSG_DEBUG, "CTRL: Invalid blob hex data");
264 wpa_config_free_blob(blob);
265 return -1;
266 }
267 blob->len = len / 2;
268
269 wpa_config_set_blob(wpa_s->conf, blob);
270
271 return 0;
272}
273#endif /* CONFIG_NO_CONFIG_BLOBS */
274
d3c9c35f
DS
275
276static int wpas_ctrl_pno(struct wpa_supplicant *wpa_s, char *cmd)
277{
278 char *params;
279 char *pos;
280 int *freqs = NULL;
281 int ret;
282
283 if (atoi(cmd)) {
284 params = os_strchr(cmd, ' ');
285 os_free(wpa_s->manual_sched_scan_freqs);
286 if (params) {
287 params++;
288 pos = os_strstr(params, "freq=");
289 if (pos)
290 freqs = freq_range_to_channel_list(wpa_s,
291 pos + 5);
292 }
293 wpa_s->manual_sched_scan_freqs = freqs;
294 ret = wpas_start_pno(wpa_s);
295 } else {
296 ret = wpas_stop_pno(wpa_s);
297 }
298 return ret;
299}
300
301
844dfeb8
SD
302static int wpas_ctrl_set_band(struct wpa_supplicant *wpa_s, char *band)
303{
304 union wpa_event_data event;
305
306 if (os_strcmp(band, "AUTO") == 0)
307 wpa_s->setband = WPA_SETBAND_AUTO;
308 else if (os_strcmp(band, "5G") == 0)
309 wpa_s->setband = WPA_SETBAND_5G;
310 else if (os_strcmp(band, "2G") == 0)
311 wpa_s->setband = WPA_SETBAND_2G;
312 else
313 return -1;
314
315 if (wpa_drv_setband(wpa_s, wpa_s->setband) == 0) {
316 os_memset(&event, 0, sizeof(event));
317 event.channel_list_changed.initiator = REGDOM_SET_BY_USER;
318 event.channel_list_changed.type = REGDOM_TYPE_UNKNOWN;
319 wpa_supplicant_event(wpa_s, EVENT_CHANNEL_LIST_CHANGED, &event);
320 }
321
322 return 0;
323}
324
325
4a742011
DS
326static int wpas_ctrl_iface_set_lci(struct wpa_supplicant *wpa_s,
327 const char *cmd)
328{
329 struct wpabuf *lci;
330
331 if (*cmd == '\0' || os_strcmp(cmd, "\"\"") == 0) {
332 wpabuf_free(wpa_s->lci);
333 wpa_s->lci = NULL;
334 return 0;
335 }
336
337 lci = wpabuf_parse_bin(cmd);
338 if (!lci)
339 return -1;
340
341 if (os_get_reltime(&wpa_s->lci_time)) {
342 wpabuf_free(lci);
343 return -1;
344 }
345
346 wpabuf_free(wpa_s->lci);
347 wpa_s->lci = lci;
348
349 return 0;
350}
351
352
57c3a605 353static int
354wpas_ctrl_set_relative_rssi(struct wpa_supplicant *wpa_s, const char *cmd)
355{
356 int relative_rssi;
357
358 if (os_strcmp(cmd, "disable") == 0) {
359 wpa_s->srp.relative_rssi_set = 0;
360 return 0;
361 }
362
363 relative_rssi = atoi(cmd);
364 if (relative_rssi < 0 || relative_rssi > 100)
365 return -1;
366 wpa_s->srp.relative_rssi = relative_rssi;
367 wpa_s->srp.relative_rssi_set = 1;
368 return 0;
369}
370
371
372static int wpas_ctrl_set_relative_band_adjust(struct wpa_supplicant *wpa_s,
373 const char *cmd)
374{
375 char *pos;
376 int adjust_rssi;
377
378 /* <band>:adjust_value */
379 pos = os_strchr(cmd, ':');
380 if (!pos)
381 return -1;
382 pos++;
383 adjust_rssi = atoi(pos);
384 if (adjust_rssi < -100 || adjust_rssi > 100)
385 return -1;
386
387 if (os_strncmp(cmd, "2G", 2) == 0)
388 wpa_s->srp.relative_adjust_band = WPA_SETBAND_2G;
389 else if (os_strncmp(cmd, "5G", 2) == 0)
390 wpa_s->srp.relative_adjust_band = WPA_SETBAND_5G;
391 else
392 return -1;
393
394 wpa_s->srp.relative_adjust_rssi = adjust_rssi;
395
396 return 0;
397}
398
399
c6c41f6e
JM
400static int wpas_ctrl_iface_set_ric_ies(struct wpa_supplicant *wpa_s,
401 const char *cmd)
402{
403 struct wpabuf *ric_ies;
404
405 if (*cmd == '\0' || os_strcmp(cmd, "\"\"") == 0) {
406 wpabuf_free(wpa_s->ric_ies);
407 wpa_s->ric_ies = NULL;
408 return 0;
409 }
410
411 ric_ies = wpabuf_parse_bin(cmd);
412 if (!ric_ies)
413 return -1;
414
415 wpabuf_free(wpa_s->ric_ies);
416 wpa_s->ric_ies = ric_ies;
417
418 return 0;
419}
420
421
6fc6879b
JM
422static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
423 char *cmd)
424{
425 char *value;
426 int ret = 0;
427
428 value = os_strchr(cmd, ' ');
429 if (value == NULL)
430 return -1;
431 *value++ = '\0';
432
433 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
434 if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
435 eapol_sm_configure(wpa_s->eapol,
436 atoi(value), -1, -1, -1);
437 } else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
438 eapol_sm_configure(wpa_s->eapol,
439 -1, atoi(value), -1, -1);
440 } else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
441 eapol_sm_configure(wpa_s->eapol,
442 -1, -1, atoi(value), -1);
443 } else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
444 eapol_sm_configure(wpa_s->eapol,
445 -1, -1, -1, atoi(value));
446 } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
447 if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
b4bdeadf 448 atoi(value))) {
6fc6879b 449 ret = -1;
b4bdeadf
JM
450 } else {
451 value[-1] = '=';
452 wpa_config_process_global(wpa_s->conf, cmd, -1);
453 }
6fc6879b
JM
454 } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
455 0) {
456 if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
b4bdeadf 457 atoi(value))) {
6fc6879b 458 ret = -1;
b4bdeadf
JM
459 } else {
460 value[-1] = '=';
461 wpa_config_process_global(wpa_s->conf, cmd, -1);
462 }
6fc6879b 463 } else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
b4bdeadf
JM
464 if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
465 atoi(value))) {
6fc6879b 466 ret = -1;
b4bdeadf
JM
467 } else {
468 value[-1] = '=';
469 wpa_config_process_global(wpa_s->conf, cmd, -1);
470 }
42f50264
JM
471 } else if (os_strcasecmp(cmd, "wps_fragment_size") == 0) {
472 wpa_s->wps_fragment_size = atoi(value);
b4e34f2f
JM
473#ifdef CONFIG_WPS_TESTING
474 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
475 long int val;
476 val = strtol(value, NULL, 0);
477 if (val < 0 || val > 0xff) {
478 ret = -1;
479 wpa_printf(MSG_DEBUG, "WPS: Invalid "
480 "wps_version_number %ld", val);
481 } else {
482 wps_version_number = val;
483 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
484 "version %u.%u",
485 (wps_version_number & 0xf0) >> 4,
486 wps_version_number & 0x0f);
487 }
488 } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
489 wps_testing_dummy_cred = atoi(value);
490 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
491 wps_testing_dummy_cred);
91226e0d
JM
492 } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
493 wps_corrupt_pkhash = atoi(value);
494 wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
495 wps_corrupt_pkhash);
6e379c6c
JM
496 } else if (os_strcasecmp(cmd, "wps_force_auth_types") == 0) {
497 if (value[0] == '\0') {
498 wps_force_auth_types_in_use = 0;
499 } else {
500 wps_force_auth_types = strtol(value, NULL, 0);
501 wps_force_auth_types_in_use = 1;
502 }
503 } else if (os_strcasecmp(cmd, "wps_force_encr_types") == 0) {
504 if (value[0] == '\0') {
505 wps_force_encr_types_in_use = 0;
506 } else {
507 wps_force_encr_types = strtol(value, NULL, 0);
508 wps_force_encr_types_in_use = 1;
509 }
b4e34f2f 510#endif /* CONFIG_WPS_TESTING */
b6c79a99
JM
511 } else if (os_strcasecmp(cmd, "ampdu") == 0) {
512 if (wpa_drv_ampdu(wpa_s, atoi(value)) < 0)
513 ret = -1;
9d2cb3ec 514#ifdef CONFIG_TDLS
5b0e6ece
JM
515#ifdef CONFIG_TDLS_TESTING
516 } else if (os_strcasecmp(cmd, "tdls_testing") == 0) {
5b0e6ece
JM
517 tdls_testing = strtol(value, NULL, 0);
518 wpa_printf(MSG_DEBUG, "TDLS: tdls_testing=0x%x", tdls_testing);
519#endif /* CONFIG_TDLS_TESTING */
b8f64582
JM
520 } else if (os_strcasecmp(cmd, "tdls_disabled") == 0) {
521 int disabled = atoi(value);
522 wpa_printf(MSG_DEBUG, "TDLS: tdls_disabled=%d", disabled);
523 if (disabled) {
524 if (wpa_drv_tdls_oper(wpa_s, TDLS_DISABLE, NULL) < 0)
525 ret = -1;
526 } else if (wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL) < 0)
527 ret = -1;
528 wpa_tdls_enable(wpa_s->wpa, !disabled);
529#endif /* CONFIG_TDLS */
b5c68312 530 } else if (os_strcasecmp(cmd, "pno") == 0) {
d3c9c35f 531 ret = wpas_ctrl_pno(wpa_s, value);
8b9d0bfa
JM
532 } else if (os_strcasecmp(cmd, "radio_disabled") == 0) {
533 int disabled = atoi(value);
534 if (wpa_drv_radio_disable(wpa_s, disabled) < 0)
535 ret = -1;
536 else if (disabled)
537 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
aa074a64
JM
538 } else if (os_strcasecmp(cmd, "uapsd") == 0) {
539 if (os_strcmp(value, "disable") == 0)
540 wpa_s->set_sta_uapsd = 0;
541 else {
542 int be, bk, vi, vo;
543 char *pos;
544 /* format: BE,BK,VI,VO;max SP Length */
545 be = atoi(value);
546 pos = os_strchr(value, ',');
547 if (pos == NULL)
548 return -1;
549 pos++;
550 bk = atoi(pos);
551 pos = os_strchr(pos, ',');
552 if (pos == NULL)
553 return -1;
554 pos++;
555 vi = atoi(pos);
556 pos = os_strchr(pos, ',');
557 if (pos == NULL)
558 return -1;
559 pos++;
560 vo = atoi(pos);
561 /* ignore max SP Length for now */
562
563 wpa_s->set_sta_uapsd = 1;
564 wpa_s->sta_uapsd = 0;
565 if (be)
566 wpa_s->sta_uapsd |= BIT(0);
567 if (bk)
568 wpa_s->sta_uapsd |= BIT(1);
569 if (vi)
570 wpa_s->sta_uapsd |= BIT(2);
571 if (vo)
572 wpa_s->sta_uapsd |= BIT(3);
573 }
b2ff1681
JM
574 } else if (os_strcasecmp(cmd, "ps") == 0) {
575 ret = wpa_drv_set_p2p_powersave(wpa_s, atoi(value), -1, -1);
9675ce35
JM
576#ifdef CONFIG_WIFI_DISPLAY
577 } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
bab6677a
JM
578 int enabled = !!atoi(value);
579 if (enabled && !wpa_s->global->p2p)
580 ret = -1;
581 else
582 wifi_display_enable(wpa_s->global, enabled);
9675ce35 583#endif /* CONFIG_WIFI_DISPLAY */
d445a5cd
JM
584 } else if (os_strcasecmp(cmd, "bssid_filter") == 0) {
585 ret = set_bssid_filter(wpa_s, value);
6407f413
JM
586 } else if (os_strcasecmp(cmd, "disallow_aps") == 0) {
587 ret = set_disallow_aps(wpa_s, value);
2ec535fd
JM
588 } else if (os_strcasecmp(cmd, "no_keep_alive") == 0) {
589 wpa_s->no_keep_alive = !!atoi(value);
b65b22d6
JM
590#ifdef CONFIG_DPP
591 } else if (os_strcasecmp(cmd, "dpp_configurator_params") == 0) {
592 os_free(wpa_s->dpp_configurator_params);
593 wpa_s->dpp_configurator_params = os_strdup(value);
f97ace34
JM
594 } else if (os_strcasecmp(cmd, "dpp_init_max_tries") == 0) {
595 wpa_s->dpp_init_max_tries = atoi(value);
596 } else if (os_strcasecmp(cmd, "dpp_init_retry_time") == 0) {
597 wpa_s->dpp_init_retry_time = atoi(value);
598 } else if (os_strcasecmp(cmd, "dpp_resp_wait_time") == 0) {
599 wpa_s->dpp_resp_wait_time = atoi(value);
95b0104a
JM
600 } else if (os_strcasecmp(cmd, "dpp_resp_max_tries") == 0) {
601 wpa_s->dpp_resp_max_tries = atoi(value);
602 } else if (os_strcasecmp(cmd, "dpp_resp_retry_time") == 0) {
603 wpa_s->dpp_resp_retry_time = atoi(value);
dc2f24f1 604#ifdef CONFIG_TESTING_OPTIONS
2bdc47a9
JM
605 } else if (os_strcasecmp(cmd, "dpp_pkex_own_mac_override") == 0) {
606 if (hwaddr_aton(value, dpp_pkex_own_mac_override))
607 ret = -1;
608 } else if (os_strcasecmp(cmd, "dpp_pkex_peer_mac_override") == 0) {
609 if (hwaddr_aton(value, dpp_pkex_peer_mac_override))
610 ret = -1;
acc555f9
JM
611 } else if (os_strcasecmp(cmd, "dpp_pkex_ephemeral_key_override") == 0) {
612 size_t hex_len = os_strlen(value);
613
614 if (hex_len >
615 2 * sizeof(dpp_pkex_ephemeral_key_override))
616 ret = -1;
617 else if (hexstr2bin(value, dpp_pkex_ephemeral_key_override,
618 hex_len / 2))
619 ret = -1;
620 else
621 dpp_pkex_ephemeral_key_override_len = hex_len / 2;
f5526975
JM
622 } else if (os_strcasecmp(cmd, "dpp_protocol_key_override") == 0) {
623 size_t hex_len = os_strlen(value);
624
625 if (hex_len > 2 * sizeof(dpp_protocol_key_override))
626 ret = -1;
627 else if (hexstr2bin(value, dpp_protocol_key_override,
628 hex_len / 2))
629 ret = -1;
630 else
631 dpp_protocol_key_override_len = hex_len / 2;
055cd397
JM
632 } else if (os_strcasecmp(cmd, "dpp_nonce_override") == 0) {
633 size_t hex_len = os_strlen(value);
634
635 if (hex_len > 2 * sizeof(dpp_nonce_override))
636 ret = -1;
637 else if (hexstr2bin(value, dpp_nonce_override, hex_len / 2))
638 ret = -1;
639 else
640 dpp_nonce_override_len = hex_len / 2;
dc2f24f1 641#endif /* CONFIG_TESTING_OPTIONS */
b65b22d6 642#endif /* CONFIG_DPP */
60b893df
JM
643#ifdef CONFIG_TESTING_OPTIONS
644 } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
645 wpa_s->ext_mgmt_frame_handling = !!atoi(value);
9d4ff04a
JM
646 } else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
647 wpa_s->ext_eapol_frame_io = !!atoi(value);
648#ifdef CONFIG_AP
649 if (wpa_s->ap_iface) {
650 wpa_s->ap_iface->bss[0]->ext_eapol_frame_io =
651 wpa_s->ext_eapol_frame_io;
652 }
653#endif /* CONFIG_AP */
1f94e4ee
JM
654 } else if (os_strcasecmp(cmd, "extra_roc_dur") == 0) {
655 wpa_s->extra_roc_dur = atoi(value);
911942ee
JM
656 } else if (os_strcasecmp(cmd, "test_failure") == 0) {
657 wpa_s->test_failure = atoi(value);
ed7820b4
IP
658 } else if (os_strcasecmp(cmd, "p2p_go_csa_on_inv") == 0) {
659 wpa_s->p2p_go_csa_on_inv = !!atoi(value);
02adead5
MK
660 } else if (os_strcasecmp(cmd, "ignore_auth_resp") == 0) {
661 wpa_s->ignore_auth_resp = !!atoi(value);
6ad37d73 662 } else if (os_strcasecmp(cmd, "ignore_assoc_disallow") == 0) {
663 wpa_s->ignore_assoc_disallow = !!atoi(value);
178553b7
VK
664 wpa_drv_ignore_assoc_disallow(wpa_s,
665 wpa_s->ignore_assoc_disallow);
a483c6f1 666 } else if (os_strcasecmp(cmd, "reject_btm_req_reason") == 0) {
667 wpa_s->reject_btm_req_reason = atoi(value);
c06fca04
JM
668 } else if (os_strcasecmp(cmd, "get_pref_freq_list_override") == 0) {
669 os_free(wpa_s->get_pref_freq_list_override);
670 if (!value[0])
671 wpa_s->get_pref_freq_list_override = NULL;
672 else
673 wpa_s->get_pref_freq_list_override = os_strdup(value);
a0f19e9c
JM
674 } else if (os_strcasecmp(cmd, "sae_commit_override") == 0) {
675 wpabuf_free(wpa_s->sae_commit_override);
676 if (value[0] == '\0')
677 wpa_s->sae_commit_override = NULL;
678 else
679 wpa_s->sae_commit_override = wpabuf_parse_bin(value);
461d39af
JM
680#ifdef CONFIG_DPP
681 } else if (os_strcasecmp(cmd, "dpp_config_obj_override") == 0) {
682 os_free(wpa_s->dpp_config_obj_override);
b7dddab7
JM
683 if (value[0] == '\0')
684 wpa_s->dpp_config_obj_override = NULL;
685 else
686 wpa_s->dpp_config_obj_override = os_strdup(value);
461d39af
JM
687 } else if (os_strcasecmp(cmd, "dpp_discovery_override") == 0) {
688 os_free(wpa_s->dpp_discovery_override);
b7dddab7
JM
689 if (value[0] == '\0')
690 wpa_s->dpp_discovery_override = NULL;
691 else
692 wpa_s->dpp_discovery_override = os_strdup(value);
461d39af
JM
693 } else if (os_strcasecmp(cmd, "dpp_groups_override") == 0) {
694 os_free(wpa_s->dpp_groups_override);
b7dddab7
JM
695 if (value[0] == '\0')
696 wpa_s->dpp_groups_override = NULL;
697 else
698 wpa_s->dpp_groups_override = os_strdup(value);
461d39af
JM
699 } else if (os_strcasecmp(cmd,
700 "dpp_ignore_netaccesskey_mismatch") == 0) {
701 wpa_s->dpp_ignore_netaccesskey_mismatch = atoi(value);
60239f60
JM
702 } else if (os_strcasecmp(cmd, "dpp_test") == 0) {
703 dpp_test = atoi(value);
461d39af 704#endif /* CONFIG_DPP */
60b893df 705#endif /* CONFIG_TESTING_OPTIONS */
d98038bb 706#ifdef CONFIG_FILS
707 } else if (os_strcasecmp(cmd, "disable_fils") == 0) {
708 wpa_s->disable_fils = !!atoi(value);
709 wpa_drv_disable_fils(wpa_s, wpa_s->disable_fils);
710 wpa_supplicant_set_default_scan_ies(wpa_s);
711#endif /* CONFIG_FILS */
1120e452
JM
712#ifndef CONFIG_NO_CONFIG_BLOBS
713 } else if (os_strcmp(cmd, "blob") == 0) {
714 ret = wpas_ctrl_set_blob(wpa_s, value);
715#endif /* CONFIG_NO_CONFIG_BLOBS */
209702d4 716 } else if (os_strcasecmp(cmd, "setband") == 0) {
844dfeb8 717 ret = wpas_ctrl_set_band(wpa_s, value);
92c6e2e3
DS
718#ifdef CONFIG_MBO
719 } else if (os_strcasecmp(cmd, "non_pref_chan") == 0) {
720 ret = wpas_mbo_update_non_pref_chan(wpa_s, value);
e3394c0e
JM
721 if (ret == 0) {
722 value[-1] = '=';
723 wpa_config_process_global(wpa_s->conf, cmd, -1);
724 }
016082e9
AS
725 } else if (os_strcasecmp(cmd, "mbo_cell_capa") == 0) {
726 wpas_mbo_update_cell_capa(wpa_s, atoi(value));
332aadb8
AP
727 } else if (os_strcasecmp(cmd, "oce") == 0) {
728 wpa_s->conf->oce = atoi(value);
729 if (wpa_s->conf->oce) {
730 if ((wpa_s->conf->oce & OCE_STA) &&
731 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_OCE_STA))
732 wpa_s->enable_oce = OCE_STA;
733
734 if ((wpa_s->conf->oce & OCE_STA_CFON) &&
735 (wpa_s->drv_flags &
736 WPA_DRIVER_FLAGS_OCE_STA_CFON)) {
737 /* TODO: Need to add STA-CFON support */
738 wpa_printf(MSG_ERROR,
739 "OCE STA-CFON feature is not yet supported");
740 return -1;
741 }
742 } else {
743 wpa_s->enable_oce = 0;
744 }
e37cea30 745 wpa_supplicant_set_default_scan_ies(wpa_s);
92c6e2e3 746#endif /* CONFIG_MBO */
4a742011
DS
747 } else if (os_strcasecmp(cmd, "lci") == 0) {
748 ret = wpas_ctrl_iface_set_lci(wpa_s, value);
2e4e4fb7
SD
749 } else if (os_strcasecmp(cmd, "tdls_trigger_control") == 0) {
750 ret = wpa_drv_set_tdls_mode(wpa_s, atoi(value));
57c3a605 751 } else if (os_strcasecmp(cmd, "relative_rssi") == 0) {
752 ret = wpas_ctrl_set_relative_rssi(wpa_s, value);
753 } else if (os_strcasecmp(cmd, "relative_band_adjust") == 0) {
754 ret = wpas_ctrl_set_relative_band_adjust(wpa_s, value);
c6c41f6e
JM
755 } else if (os_strcasecmp(cmd, "ric_ies") == 0) {
756 ret = wpas_ctrl_iface_set_ric_ies(wpa_s, value);
9a72bfe9
AP
757 } else if (os_strcasecmp(cmd, "roaming") == 0) {
758 ret = wpa_drv_roaming(wpa_s, atoi(value), NULL);
d514b502
JM
759#ifdef CONFIG_WNM
760 } else if (os_strcasecmp(cmd, "coloc_intf_elems") == 0) {
761 struct wpabuf *elems;
762
763 elems = wpabuf_parse_bin(value);
764 if (!elems)
765 return -1;
766 wnm_set_coloc_intf_elems(wpa_s, elems);
767#endif /* CONFIG_WNM */
611aea7d
JM
768 } else {
769 value[-1] = '=';
770 ret = wpa_config_process_global(wpa_s->conf, cmd, -1);
771 if (ret == 0)
772 wpa_supplicant_update_config(wpa_s);
773 }
6fc6879b
JM
774
775 return ret;
776}
777
778
acec8d32
JM
779static int wpa_supplicant_ctrl_iface_get(struct wpa_supplicant *wpa_s,
780 char *cmd, char *buf, size_t buflen)
781{
6ce937b8 782 int res = -1;
acec8d32
JM
783
784 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
785
786 if (os_strcmp(cmd, "version") == 0) {
787 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
6ce937b8
DS
788 } else if (os_strcasecmp(cmd, "country") == 0) {
789 if (wpa_s->conf->country[0] && wpa_s->conf->country[1])
790 res = os_snprintf(buf, buflen, "%c%c",
791 wpa_s->conf->country[0],
792 wpa_s->conf->country[1]);
9675ce35
JM
793#ifdef CONFIG_WIFI_DISPLAY
794 } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
bab6677a
JM
795 int enabled;
796 if (wpa_s->global->p2p == NULL ||
797 wpa_s->global->p2p_disabled)
798 enabled = 0;
799 else
800 enabled = wpa_s->global->wifi_display;
801 res = os_snprintf(buf, buflen, "%d", enabled);
9675ce35 802#endif /* CONFIG_WIFI_DISPLAY */
fa7ae950
JM
803#ifdef CONFIG_TESTING_GET_GTK
804 } else if (os_strcmp(cmd, "gtk") == 0) {
805 if (wpa_s->last_gtk_len == 0)
806 return -1;
807 res = wpa_snprintf_hex(buf, buflen, wpa_s->last_gtk,
808 wpa_s->last_gtk_len);
809 return res;
810#endif /* CONFIG_TESTING_GET_GTK */
a1651451
JM
811 } else if (os_strcmp(cmd, "tls_library") == 0) {
812 res = tls_get_library_version(buf, buflen);
bb06748f
JM
813#ifdef CONFIG_TESTING_OPTIONS
814 } else if (os_strcmp(cmd, "anonce") == 0) {
815 return wpa_snprintf_hex(buf, buflen,
816 wpa_sm_get_anonce(wpa_s->wpa),
817 WPA_NONCE_LEN);
818#endif /* CONFIG_TESTING_OPTIONS */
10263dc2
OO
819 } else {
820 res = wpa_config_get_value(cmd, wpa_s->conf, buf, buflen);
acec8d32
JM
821 }
822
1f102d3b 823 if (os_snprintf_error(buflen, res))
6ce937b8
DS
824 return -1;
825 return res;
acec8d32
JM
826}
827
828
ec717917 829#ifdef IEEE8021X_EAPOL
6fc6879b
JM
830static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
831 char *addr)
832{
833 u8 bssid[ETH_ALEN];
834 struct wpa_ssid *ssid = wpa_s->current_ssid;
835
836 if (hwaddr_aton(addr, bssid)) {
837 wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
838 "'%s'", addr);
839 return -1;
840 }
841
842 wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
843 rsn_preauth_deinit(wpa_s->wpa);
844 if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
845 return -1;
846
847 return 0;
848}
ec717917 849#endif /* IEEE8021X_EAPOL */
6fc6879b
JM
850
851
281ff0aa
GP
852#ifdef CONFIG_TDLS
853
854static int wpa_supplicant_ctrl_iface_tdls_discover(
855 struct wpa_supplicant *wpa_s, char *addr)
856{
857 u8 peer[ETH_ALEN];
2d565a61 858 int ret;
281ff0aa
GP
859
860 if (hwaddr_aton(addr, peer)) {
861 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER: invalid "
862 "address '%s'", addr);
863 return -1;
864 }
865
866 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER " MACSTR,
867 MAC2STR(peer));
868
2d565a61
AN
869 if (wpa_tdls_is_external_setup(wpa_s->wpa))
870 ret = wpa_tdls_send_discovery_request(wpa_s->wpa, peer);
871 else
872 ret = wpa_drv_tdls_oper(wpa_s, TDLS_DISCOVERY_REQ, peer);
873
874 return ret;
281ff0aa
GP
875}
876
877
878static int wpa_supplicant_ctrl_iface_tdls_setup(
879 struct wpa_supplicant *wpa_s, char *addr)
880{
881 u8 peer[ETH_ALEN];
94377fbc 882 int ret;
281ff0aa
GP
883
884 if (hwaddr_aton(addr, peer)) {
885 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP: invalid "
886 "address '%s'", addr);
887 return -1;
888 }
889
890 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP " MACSTR,
891 MAC2STR(peer));
892
800d5872
SD
893 if ((wpa_s->conf->tdls_external_control) &&
894 wpa_tdls_is_external_setup(wpa_s->wpa))
895 return wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
896
3887878e
SD
897 wpa_tdls_remove(wpa_s->wpa, peer);
898
899 if (wpa_tdls_is_external_setup(wpa_s->wpa))
900 ret = wpa_tdls_start(wpa_s->wpa, peer);
901 else
902 ret = wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
2d565a61 903
94377fbc 904 return ret;
281ff0aa
GP
905}
906
907
908static int wpa_supplicant_ctrl_iface_tdls_teardown(
909 struct wpa_supplicant *wpa_s, char *addr)
910{
911 u8 peer[ETH_ALEN];
4ed8d954 912 int ret;
281ff0aa 913
38ddccae
AN
914 if (os_strcmp(addr, "*") == 0) {
915 /* remove everyone */
916 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN *");
917 wpa_tdls_teardown_peers(wpa_s->wpa);
918 return 0;
919 }
920
281ff0aa
GP
921 if (hwaddr_aton(addr, peer)) {
922 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN: invalid "
923 "address '%s'", addr);
924 return -1;
925 }
926
927 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN " MACSTR,
928 MAC2STR(peer));
929
800d5872
SD
930 if ((wpa_s->conf->tdls_external_control) &&
931 wpa_tdls_is_external_setup(wpa_s->wpa))
932 return wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
933
4ed8d954
AS
934 if (wpa_tdls_is_external_setup(wpa_s->wpa))
935 ret = wpa_tdls_teardown_link(
936 wpa_s->wpa, peer,
937 WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
938 else
939 ret = wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
940
941 return ret;
281ff0aa
GP
942}
943
6e9375e4
DS
944
945static int ctrl_iface_get_capability_tdls(
946 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
947{
948 int ret;
949
950 ret = os_snprintf(buf, buflen, "%s\n",
951 wpa_s->drv_flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT ?
952 (wpa_s->drv_flags &
953 WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP ?
954 "EXTERNAL" : "INTERNAL") : "UNSUPPORTED");
7bdd8981 955 if (os_snprintf_error(buflen, ret))
6e9375e4
DS
956 return -1;
957 return ret;
958}
959
6b90deae
AN
960
961static int wpa_supplicant_ctrl_iface_tdls_chan_switch(
962 struct wpa_supplicant *wpa_s, char *cmd)
963{
964 u8 peer[ETH_ALEN];
965 struct hostapd_freq_params freq_params;
966 u8 oper_class;
967 char *pos, *end;
968
969 if (!wpa_tdls_is_external_setup(wpa_s->wpa)) {
970 wpa_printf(MSG_INFO,
971 "tdls_chanswitch: Only supported with external setup");
972 return -1;
973 }
974
975 os_memset(&freq_params, 0, sizeof(freq_params));
976
977 pos = os_strchr(cmd, ' ');
978 if (pos == NULL)
979 return -1;
980 *pos++ = '\0';
981
982 oper_class = strtol(pos, &end, 10);
983 if (pos == end) {
984 wpa_printf(MSG_INFO,
985 "tdls_chanswitch: Invalid op class provided");
986 return -1;
987 }
988
989 pos = end;
990 freq_params.freq = atoi(pos);
991 if (freq_params.freq == 0) {
992 wpa_printf(MSG_INFO, "tdls_chanswitch: Invalid freq provided");
993 return -1;
994 }
995
996#define SET_FREQ_SETTING(str) \
997 do { \
998 const char *pos2 = os_strstr(pos, " " #str "="); \
999 if (pos2) { \
1000 pos2 += sizeof(" " #str "=") - 1; \
1001 freq_params.str = atoi(pos2); \
1002 } \
1003 } while (0)
1004
1005 SET_FREQ_SETTING(center_freq1);
1006 SET_FREQ_SETTING(center_freq2);
1007 SET_FREQ_SETTING(bandwidth);
1008 SET_FREQ_SETTING(sec_channel_offset);
1009#undef SET_FREQ_SETTING
1010
1011 freq_params.ht_enabled = !!os_strstr(pos, " ht");
1012 freq_params.vht_enabled = !!os_strstr(pos, " vht");
1013
1014 if (hwaddr_aton(cmd, peer)) {
1015 wpa_printf(MSG_DEBUG,
1016 "CTRL_IFACE TDLS_CHAN_SWITCH: Invalid address '%s'",
1017 cmd);
1018 return -1;
1019 }
1020
1021 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_CHAN_SWITCH " MACSTR
1022 " OP CLASS %d FREQ %d CENTER1 %d CENTER2 %d BW %d SEC_OFFSET %d%s%s",
1023 MAC2STR(peer), oper_class, freq_params.freq,
1024 freq_params.center_freq1, freq_params.center_freq2,
1025 freq_params.bandwidth, freq_params.sec_channel_offset,
1026 freq_params.ht_enabled ? " HT" : "",
1027 freq_params.vht_enabled ? " VHT" : "");
1028
1029 return wpa_tdls_enable_chan_switch(wpa_s->wpa, peer, oper_class,
1030 &freq_params);
1031}
1032
1033
1034static int wpa_supplicant_ctrl_iface_tdls_cancel_chan_switch(
1035 struct wpa_supplicant *wpa_s, char *cmd)
1036{
1037 u8 peer[ETH_ALEN];
1038
1039 if (!wpa_tdls_is_external_setup(wpa_s->wpa)) {
1040 wpa_printf(MSG_INFO,
1041 "tdls_chanswitch: Only supported with external setup");
1042 return -1;
1043 }
1044
1045 if (hwaddr_aton(cmd, peer)) {
1046 wpa_printf(MSG_DEBUG,
1047 "CTRL_IFACE TDLS_CANCEL_CHAN_SWITCH: Invalid address '%s'",
1048 cmd);
1049 return -1;
1050 }
1051
1052 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_CANCEL_CHAN_SWITCH " MACSTR,
1053 MAC2STR(peer));
1054
1055 return wpa_tdls_disable_chan_switch(wpa_s->wpa, peer);
1056}
1057
4504621f
OG
1058
1059static int wpa_supplicant_ctrl_iface_tdls_link_status(
1060 struct wpa_supplicant *wpa_s, const char *addr,
1061 char *buf, size_t buflen)
1062{
1063 u8 peer[ETH_ALEN];
1064 const char *tdls_status;
1065 int ret;
1066
1067 if (hwaddr_aton(addr, peer)) {
1068 wpa_printf(MSG_DEBUG,
1069 "CTRL_IFACE TDLS_LINK_STATUS: Invalid address '%s'",
1070 addr);
1071 return -1;
1072 }
1073 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_LINK_STATUS " MACSTR,
1074 MAC2STR(peer));
1075
1076 tdls_status = wpa_tdls_get_link_status(wpa_s->wpa, peer);
1077 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_LINK_STATUS: %s", tdls_status);
1078 ret = os_snprintf(buf, buflen, "TDLS link status: %s\n", tdls_status);
1079 if (os_snprintf_error(buflen, ret))
1080 return -1;
1081
1082 return ret;
1083}
1084
281ff0aa
GP
1085#endif /* CONFIG_TDLS */
1086
1087
eb2f2088
MB
1088static int wmm_ac_ctrl_addts(struct wpa_supplicant *wpa_s, char *cmd)
1089{
1090 char *token, *context = NULL;
1091 struct wmm_ac_ts_setup_params params = {
1092 .tsid = 0xff,
1093 .direction = 0xff,
1094 };
1095
1096 while ((token = str_token(cmd, " ", &context))) {
1097 if (sscanf(token, "tsid=%i", &params.tsid) == 1 ||
1098 sscanf(token, "up=%i", &params.user_priority) == 1 ||
1099 sscanf(token, "nominal_msdu_size=%i",
1100 &params.nominal_msdu_size) == 1 ||
1101 sscanf(token, "mean_data_rate=%i",
1102 &params.mean_data_rate) == 1 ||
1103 sscanf(token, "min_phy_rate=%i",
1104 &params.minimum_phy_rate) == 1 ||
1105 sscanf(token, "sba=%i",
1106 &params.surplus_bandwidth_allowance) == 1)
1107 continue;
1108
1109 if (os_strcasecmp(token, "downlink") == 0) {
1110 params.direction = WMM_TSPEC_DIRECTION_DOWNLINK;
1111 } else if (os_strcasecmp(token, "uplink") == 0) {
1112 params.direction = WMM_TSPEC_DIRECTION_UPLINK;
1113 } else if (os_strcasecmp(token, "bidi") == 0) {
1114 params.direction = WMM_TSPEC_DIRECTION_BI_DIRECTIONAL;
1115 } else if (os_strcasecmp(token, "fixed_nominal_msdu") == 0) {
1116 params.fixed_nominal_msdu = 1;
1117 } else {
1118 wpa_printf(MSG_DEBUG,
1119 "CTRL: Invalid WMM_AC_ADDTS parameter: '%s'",
1120 token);
1121 return -1;
1122 }
1123
1124 }
1125
1126 return wpas_wmm_ac_addts(wpa_s, &params);
1127}
1128
1129
1130static int wmm_ac_ctrl_delts(struct wpa_supplicant *wpa_s, char *cmd)
1131{
1132 u8 tsid = atoi(cmd);
1133
1134 return wpas_wmm_ac_delts(wpa_s, tsid);
1135}
1136
1137
6fc6879b
JM
1138#ifdef CONFIG_IEEE80211R
1139static int wpa_supplicant_ctrl_iface_ft_ds(
1140 struct wpa_supplicant *wpa_s, char *addr)
1141{
1142 u8 target_ap[ETH_ALEN];
76b7981d
JM
1143 struct wpa_bss *bss;
1144 const u8 *mdie;
6fc6879b
JM
1145
1146 if (hwaddr_aton(addr, target_ap)) {
1147 wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
a7b6c422 1148 "address '%s'", addr);
6fc6879b
JM
1149 return -1;
1150 }
1151
1152 wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
1153
76b7981d
JM
1154 bss = wpa_bss_get_bssid(wpa_s, target_ap);
1155 if (bss)
1156 mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
1157 else
1158 mdie = NULL;
1159
1160 return wpa_ft_start_over_ds(wpa_s->wpa, target_ap, mdie);
6fc6879b
JM
1161}
1162#endif /* CONFIG_IEEE80211R */
1163
1164
fcc60db4
JM
1165#ifdef CONFIG_WPS
1166static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
1167 char *cmd)
1168{
3ec97afe 1169 u8 bssid[ETH_ALEN], *_bssid = bssid;
ceb34f25 1170#ifdef CONFIG_P2P
634ce802 1171 u8 p2p_dev_addr[ETH_ALEN];
ceb34f25 1172#endif /* CONFIG_P2P */
634ce802
JM
1173#ifdef CONFIG_AP
1174 u8 *_p2p_dev_addr = NULL;
1175#endif /* CONFIG_AP */
83ebf558
DL
1176 char *pos;
1177 int multi_ap = 0;
fcc60db4 1178
83ebf558
DL
1179 if (!cmd || os_strcmp(cmd, "any") == 0 ||
1180 os_strncmp(cmd, "any ", 4) == 0) {
3ec97afe 1181 _bssid = NULL;
d601247c
JM
1182#ifdef CONFIG_P2P
1183 } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
1184 if (hwaddr_aton(cmd + 13, p2p_dev_addr)) {
1185 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid "
1186 "P2P Device Address '%s'",
1187 cmd + 13);
1188 return -1;
1189 }
1190 _p2p_dev_addr = p2p_dev_addr;
1191#endif /* CONFIG_P2P */
83ebf558
DL
1192 } else if (os_strncmp(cmd, "multi_ap=", 9) == 0) {
1193 _bssid = NULL;
1194 multi_ap = atoi(cmd + 9);
d601247c 1195 } else if (hwaddr_aton(cmd, bssid)) {
fcc60db4
JM
1196 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid BSSID '%s'",
1197 cmd);
1198 return -1;
1199 }
1200
83ebf558
DL
1201 if (cmd) {
1202 pos = os_strstr(cmd, " multi_ap=");
1203 if (pos) {
1204 pos += 10;
1205 multi_ap = atoi(pos);
1206 }
1207 }
1208
3ec97afe
JM
1209#ifdef CONFIG_AP
1210 if (wpa_s->ap_iface)
d601247c 1211 return wpa_supplicant_ap_wps_pbc(wpa_s, _bssid, _p2p_dev_addr);
3ec97afe
JM
1212#endif /* CONFIG_AP */
1213
83ebf558 1214 return wpas_wps_start_pbc(wpa_s, _bssid, 0, multi_ap);
fcc60db4
JM
1215}
1216
1217
1218static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
1219 char *cmd, char *buf,
1220 size_t buflen)
1221{
1222 u8 bssid[ETH_ALEN], *_bssid = bssid;
1223 char *pin;
1224 int ret;
1225
1226 pin = os_strchr(cmd, ' ');
1227 if (pin)
1228 *pin++ = '\0';
1229
1230 if (os_strcmp(cmd, "any") == 0)
1231 _bssid = NULL;
98aa7ca5 1232 else if (os_strcmp(cmd, "get") == 0) {
98a516ea
NL
1233 if (wps_generate_pin((unsigned int *) &ret) < 0)
1234 return -1;
98aa7ca5
JM
1235 goto done;
1236 } else if (hwaddr_aton(cmd, bssid)) {
3c1e2765 1237 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PIN: invalid BSSID '%s'",
fcc60db4
JM
1238 cmd);
1239 return -1;
1240 }
1241
3ec97afe 1242#ifdef CONFIG_AP
c423708f
JM
1243 if (wpa_s->ap_iface) {
1244 int timeout = 0;
1245 char *pos;
1246
1247 if (pin) {
1248 pos = os_strchr(pin, ' ');
1249 if (pos) {
1250 *pos++ = '\0';
1251 timeout = atoi(pos);
1252 }
1253 }
1254
3ec97afe 1255 return wpa_supplicant_ap_wps_pin(wpa_s, _bssid, pin,
c423708f
JM
1256 buf, buflen, timeout);
1257 }
3ec97afe
JM
1258#endif /* CONFIG_AP */
1259
fcc60db4 1260 if (pin) {
3c5126a4
JM
1261 ret = wpas_wps_start_pin(wpa_s, _bssid, pin, 0,
1262 DEV_PW_DEFAULT);
fcc60db4
JM
1263 if (ret < 0)
1264 return -1;
1265 ret = os_snprintf(buf, buflen, "%s", pin);
d85e1fc8 1266 if (os_snprintf_error(buflen, ret))
fcc60db4
JM
1267 return -1;
1268 return ret;
1269 }
1270
3c5126a4 1271 ret = wpas_wps_start_pin(wpa_s, _bssid, NULL, 0, DEV_PW_DEFAULT);
fcc60db4
JM
1272 if (ret < 0)
1273 return -1;
1274
98aa7ca5 1275done:
fcc60db4
JM
1276 /* Return the generated PIN */
1277 ret = os_snprintf(buf, buflen, "%08d", ret);
d85e1fc8 1278 if (os_snprintf_error(buflen, ret))
fcc60db4
JM
1279 return -1;
1280 return ret;
1281}
1282
1283
3981cb3c
JM
1284static int wpa_supplicant_ctrl_iface_wps_check_pin(
1285 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
1286{
1287 char pin[9];
1288 size_t len;
1289 char *pos;
1290 int ret;
1291
1292 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
1293 (u8 *) cmd, os_strlen(cmd));
1294 for (pos = cmd, len = 0; *pos != '\0'; pos++) {
1295 if (*pos < '0' || *pos > '9')
1296 continue;
1297 pin[len++] = *pos;
1298 if (len == 9) {
1299 wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
1300 return -1;
1301 }
1302 }
1303 if (len != 4 && len != 8) {
1304 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
1305 return -1;
1306 }
1307 pin[len] = '\0';
1308
1309 if (len == 8) {
1310 unsigned int pin_val;
1311 pin_val = atoi(pin);
1312 if (!wps_pin_valid(pin_val)) {
1313 wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
1314 ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
d85e1fc8 1315 if (os_snprintf_error(buflen, ret))
3981cb3c
JM
1316 return -1;
1317 return ret;
1318 }
1319 }
1320
1321 ret = os_snprintf(buf, buflen, "%s", pin);
d85e1fc8 1322 if (os_snprintf_error(buflen, ret))
3981cb3c
JM
1323 return -1;
1324
1325 return ret;
1326}
1327
1328
71892384 1329#ifdef CONFIG_WPS_NFC
3f2c8ba6
JM
1330
1331static int wpa_supplicant_ctrl_iface_wps_nfc(struct wpa_supplicant *wpa_s,
1332 char *cmd)
1333{
1334 u8 bssid[ETH_ALEN], *_bssid = bssid;
1335
1336 if (cmd == NULL || cmd[0] == '\0')
1337 _bssid = NULL;
1338 else if (hwaddr_aton(cmd, bssid))
1339 return -1;
1340
23318bea 1341 return wpas_wps_start_nfc(wpa_s, NULL, _bssid, NULL, 0, 0, NULL, NULL,
91a65018 1342 0, 0);
3f2c8ba6
JM
1343}
1344
1345
bbf41865
JM
1346static int wpa_supplicant_ctrl_iface_wps_nfc_config_token(
1347 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
1348{
1349 int ndef;
1350 struct wpabuf *buf;
1351 int res;
88c8bf31 1352 char *pos;
bbf41865 1353
88c8bf31
JM
1354 pos = os_strchr(cmd, ' ');
1355 if (pos)
1356 *pos++ = '\0';
bbf41865
JM
1357 if (os_strcmp(cmd, "WPS") == 0)
1358 ndef = 0;
1359 else if (os_strcmp(cmd, "NDEF") == 0)
1360 ndef = 1;
1361 else
1362 return -1;
1363
88c8bf31 1364 buf = wpas_wps_nfc_config_token(wpa_s, ndef, pos);
bbf41865
JM
1365 if (buf == NULL)
1366 return -1;
1367
1368 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1369 wpabuf_len(buf));
1370 reply[res++] = '\n';
1371 reply[res] = '\0';
1372
1373 wpabuf_free(buf);
1374
1375 return res;
1376}
1377
1378
3f2c8ba6
JM
1379static int wpa_supplicant_ctrl_iface_wps_nfc_token(
1380 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
1381{
1382 int ndef;
1383 struct wpabuf *buf;
1384 int res;
1385
1386 if (os_strcmp(cmd, "WPS") == 0)
1387 ndef = 0;
1388 else if (os_strcmp(cmd, "NDEF") == 0)
1389 ndef = 1;
1390 else
1391 return -1;
1392
1393 buf = wpas_wps_nfc_token(wpa_s, ndef);
1394 if (buf == NULL)
1395 return -1;
1396
1397 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1398 wpabuf_len(buf));
1399 reply[res++] = '\n';
1400 reply[res] = '\0';
1401
1402 wpabuf_free(buf);
1403
1404 return res;
1405}
d7645d23
JM
1406
1407
1408static int wpa_supplicant_ctrl_iface_wps_nfc_tag_read(
1409 struct wpa_supplicant *wpa_s, char *pos)
1410{
1411 size_t len;
1412 struct wpabuf *buf;
1413 int ret;
b56f6c88
JM
1414 char *freq;
1415 int forced_freq = 0;
1416
1417 freq = strstr(pos, " freq=");
1418 if (freq) {
1419 *freq = '\0';
1420 freq += 6;
1421 forced_freq = atoi(freq);
1422 }
d7645d23
JM
1423
1424 len = os_strlen(pos);
1425 if (len & 0x01)
1426 return -1;
1427 len /= 2;
1428
1429 buf = wpabuf_alloc(len);
1430 if (buf == NULL)
1431 return -1;
1432 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
1433 wpabuf_free(buf);
1434 return -1;
1435 }
1436
b56f6c88 1437 ret = wpas_wps_nfc_tag_read(wpa_s, buf, forced_freq);
d7645d23
JM
1438 wpabuf_free(buf);
1439
1440 return ret;
1441}
71892384 1442
e65552dd
JM
1443
1444static int wpas_ctrl_nfc_get_handover_req_wps(struct wpa_supplicant *wpa_s,
bbaaaee1 1445 char *reply, size_t max_len,
41f9ffb6 1446 int ndef)
e65552dd
JM
1447{
1448 struct wpabuf *buf;
1449 int res;
1450
41f9ffb6 1451 buf = wpas_wps_nfc_handover_req(wpa_s, ndef);
e65552dd
JM
1452 if (buf == NULL)
1453 return -1;
1454
1455 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1456 wpabuf_len(buf));
1457 reply[res++] = '\n';
1458 reply[res] = '\0';
1459
1460 wpabuf_free(buf);
1461
1462 return res;
1463}
1464
1465
88853aed 1466#ifdef CONFIG_P2P
93588780
JM
1467static int wpas_ctrl_nfc_get_handover_req_p2p(struct wpa_supplicant *wpa_s,
1468 char *reply, size_t max_len,
1469 int ndef)
1470{
1471 struct wpabuf *buf;
1472 int res;
1473
1474 buf = wpas_p2p_nfc_handover_req(wpa_s, ndef);
1475 if (buf == NULL) {
1476 wpa_printf(MSG_DEBUG, "P2P: Could not generate NFC handover request");
1477 return -1;
1478 }
1479
1480 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1481 wpabuf_len(buf));
1482 reply[res++] = '\n';
1483 reply[res] = '\0';
1484
1485 wpabuf_free(buf);
1486
1487 return res;
1488}
88853aed 1489#endif /* CONFIG_P2P */
93588780
JM
1490
1491
e65552dd
JM
1492static int wpas_ctrl_nfc_get_handover_req(struct wpa_supplicant *wpa_s,
1493 char *cmd, char *reply,
1494 size_t max_len)
1495{
1496 char *pos;
41f9ffb6 1497 int ndef;
e65552dd
JM
1498
1499 pos = os_strchr(cmd, ' ');
1500 if (pos == NULL)
1501 return -1;
1502 *pos++ = '\0';
1503
41f9ffb6
JM
1504 if (os_strcmp(cmd, "WPS") == 0)
1505 ndef = 0;
1506 else if (os_strcmp(cmd, "NDEF") == 0)
1507 ndef = 1;
1508 else
e65552dd
JM
1509 return -1;
1510
bbaaaee1 1511 if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
41f9ffb6
JM
1512 if (!ndef)
1513 return -1;
bbaaaee1 1514 return wpas_ctrl_nfc_get_handover_req_wps(
41f9ffb6 1515 wpa_s, reply, max_len, ndef);
e65552dd
JM
1516 }
1517
88853aed 1518#ifdef CONFIG_P2P
93588780
JM
1519 if (os_strcmp(pos, "P2P-CR") == 0) {
1520 return wpas_ctrl_nfc_get_handover_req_p2p(
1521 wpa_s, reply, max_len, ndef);
1522 }
88853aed 1523#endif /* CONFIG_P2P */
93588780 1524
e65552dd
JM
1525 return -1;
1526}
1527
1528
1529static int wpas_ctrl_nfc_get_handover_sel_wps(struct wpa_supplicant *wpa_s,
5ab9a6a5 1530 char *reply, size_t max_len,
f3f2ba2e 1531 int ndef, int cr, char *uuid)
e65552dd
JM
1532{
1533 struct wpabuf *buf;
1534 int res;
1535
f3f2ba2e 1536 buf = wpas_wps_nfc_handover_sel(wpa_s, ndef, cr, uuid);
e65552dd
JM
1537 if (buf == NULL)
1538 return -1;
1539
1540 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1541 wpabuf_len(buf));
1542 reply[res++] = '\n';
1543 reply[res] = '\0';
1544
1545 wpabuf_free(buf);
1546
1547 return res;
1548}
1549
1550
88853aed 1551#ifdef CONFIG_P2P
93588780
JM
1552static int wpas_ctrl_nfc_get_handover_sel_p2p(struct wpa_supplicant *wpa_s,
1553 char *reply, size_t max_len,
1554 int ndef, int tag)
1555{
1556 struct wpabuf *buf;
1557 int res;
1558
1559 buf = wpas_p2p_nfc_handover_sel(wpa_s, ndef, tag);
1560 if (buf == NULL)
1561 return -1;
1562
1563 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1564 wpabuf_len(buf));
1565 reply[res++] = '\n';
1566 reply[res] = '\0';
1567
1568 wpabuf_free(buf);
1569
1570 return res;
1571}
88853aed 1572#endif /* CONFIG_P2P */
93588780
JM
1573
1574
e65552dd
JM
1575static int wpas_ctrl_nfc_get_handover_sel(struct wpa_supplicant *wpa_s,
1576 char *cmd, char *reply,
1577 size_t max_len)
1578{
f3f2ba2e 1579 char *pos, *pos2;
5ab9a6a5 1580 int ndef;
e65552dd
JM
1581
1582 pos = os_strchr(cmd, ' ');
1583 if (pos == NULL)
1584 return -1;
1585 *pos++ = '\0';
1586
5ab9a6a5
JM
1587 if (os_strcmp(cmd, "WPS") == 0)
1588 ndef = 0;
1589 else if (os_strcmp(cmd, "NDEF") == 0)
1590 ndef = 1;
1591 else
e65552dd
JM
1592 return -1;
1593
f3f2ba2e
JM
1594 pos2 = os_strchr(pos, ' ');
1595 if (pos2)
1596 *pos2++ = '\0';
5ab9a6a5 1597 if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
93588780
JM
1598 if (!ndef)
1599 return -1;
5ab9a6a5
JM
1600 return wpas_ctrl_nfc_get_handover_sel_wps(
1601 wpa_s, reply, max_len, ndef,
f3f2ba2e 1602 os_strcmp(pos, "WPS-CR") == 0, pos2);
e65552dd
JM
1603 }
1604
88853aed 1605#ifdef CONFIG_P2P
93588780
JM
1606 if (os_strcmp(pos, "P2P-CR") == 0) {
1607 return wpas_ctrl_nfc_get_handover_sel_p2p(
1608 wpa_s, reply, max_len, ndef, 0);
1609 }
1610
1611 if (os_strcmp(pos, "P2P-CR-TAG") == 0) {
1612 return wpas_ctrl_nfc_get_handover_sel_p2p(
1613 wpa_s, reply, max_len, ndef, 1);
1614 }
88853aed 1615#endif /* CONFIG_P2P */
93588780 1616
e65552dd
JM
1617 return -1;
1618}
1619
1620
e4758827
JM
1621static int wpas_ctrl_nfc_report_handover(struct wpa_supplicant *wpa_s,
1622 char *cmd)
1623{
1624 size_t len;
1625 struct wpabuf *req, *sel;
1626 int ret;
1627 char *pos, *role, *type, *pos2;
88853aed 1628#ifdef CONFIG_P2P
b56f6c88
JM
1629 char *freq;
1630 int forced_freq = 0;
1631
1632 freq = strstr(cmd, " freq=");
1633 if (freq) {
1634 *freq = '\0';
1635 freq += 6;
1636 forced_freq = atoi(freq);
1637 }
88853aed 1638#endif /* CONFIG_P2P */
e4758827
JM
1639
1640 role = cmd;
1641 pos = os_strchr(role, ' ');
73127764
JM
1642 if (pos == NULL) {
1643 wpa_printf(MSG_DEBUG, "NFC: Missing type in handover report");
e4758827 1644 return -1;
73127764 1645 }
e4758827
JM
1646 *pos++ = '\0';
1647
1648 type = pos;
1649 pos = os_strchr(type, ' ');
73127764
JM
1650 if (pos == NULL) {
1651 wpa_printf(MSG_DEBUG, "NFC: Missing request message in handover report");
e4758827 1652 return -1;
73127764 1653 }
e4758827
JM
1654 *pos++ = '\0';
1655
1656 pos2 = os_strchr(pos, ' ');
73127764
JM
1657 if (pos2 == NULL) {
1658 wpa_printf(MSG_DEBUG, "NFC: Missing select message in handover report");
e4758827 1659 return -1;
73127764 1660 }
e4758827
JM
1661 *pos2++ = '\0';
1662
1663 len = os_strlen(pos);
73127764
JM
1664 if (len & 0x01) {
1665 wpa_printf(MSG_DEBUG, "NFC: Invalid request message length in handover report");
e4758827 1666 return -1;
73127764 1667 }
e4758827
JM
1668 len /= 2;
1669
1670 req = wpabuf_alloc(len);
73127764
JM
1671 if (req == NULL) {
1672 wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for request message");
e4758827 1673 return -1;
73127764 1674 }
e4758827 1675 if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
73127764 1676 wpa_printf(MSG_DEBUG, "NFC: Invalid request message hexdump in handover report");
e4758827
JM
1677 wpabuf_free(req);
1678 return -1;
1679 }
1680
1681 len = os_strlen(pos2);
1682 if (len & 0x01) {
73127764 1683 wpa_printf(MSG_DEBUG, "NFC: Invalid select message length in handover report");
e4758827
JM
1684 wpabuf_free(req);
1685 return -1;
1686 }
1687 len /= 2;
1688
1689 sel = wpabuf_alloc(len);
1690 if (sel == NULL) {
73127764 1691 wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for select message");
e4758827
JM
1692 wpabuf_free(req);
1693 return -1;
1694 }
1695 if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
73127764 1696 wpa_printf(MSG_DEBUG, "NFC: Invalid select message hexdump in handover report");
e4758827
JM
1697 wpabuf_free(req);
1698 wpabuf_free(sel);
1699 return -1;
1700 }
1701
73127764
JM
1702 wpa_printf(MSG_DEBUG, "NFC: Connection handover reported - role=%s type=%s req_len=%d sel_len=%d",
1703 role, type, (int) wpabuf_len(req), (int) wpabuf_len(sel));
1704
e4758827
JM
1705 if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "WPS") == 0) {
1706 ret = wpas_wps_nfc_report_handover(wpa_s, req, sel);
88853aed 1707#ifdef CONFIG_AP
d9507936
JM
1708 } else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0)
1709 {
1710 ret = wpas_ap_wps_nfc_report_handover(wpa_s, req, sel);
50d1f890
JM
1711 if (ret < 0)
1712 ret = wpas_er_wps_nfc_report_handover(wpa_s, req, sel);
88853aed
JM
1713#endif /* CONFIG_AP */
1714#ifdef CONFIG_P2P
db6ae69e
JM
1715 } else if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "P2P") == 0)
1716 {
b56f6c88 1717 ret = wpas_p2p_nfc_report_handover(wpa_s, 1, req, sel, 0);
db6ae69e
JM
1718 } else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "P2P") == 0)
1719 {
b56f6c88
JM
1720 ret = wpas_p2p_nfc_report_handover(wpa_s, 0, req, sel,
1721 forced_freq);
88853aed 1722#endif /* CONFIG_P2P */
e4758827
JM
1723 } else {
1724 wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
1725 "reported: role=%s type=%s", role, type);
1726 ret = -1;
1727 }
1728 wpabuf_free(req);
1729 wpabuf_free(sel);
1730
73127764
JM
1731 if (ret)
1732 wpa_printf(MSG_DEBUG, "NFC: Failed to process reported handover messages");
1733
e4758827
JM
1734 return ret;
1735}
1736
71892384 1737#endif /* CONFIG_WPS_NFC */
46bdb83a
MH
1738
1739
fcc60db4
JM
1740static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
1741 char *cmd)
1742{
129eb428 1743 u8 bssid[ETH_ALEN];
fcc60db4 1744 char *pin;
52eb293d
JM
1745 char *new_ssid;
1746 char *new_auth;
1747 char *new_encr;
1748 char *new_key;
1749 struct wps_new_ap_settings ap;
fcc60db4
JM
1750
1751 pin = os_strchr(cmd, ' ');
1752 if (pin == NULL)
1753 return -1;
1754 *pin++ = '\0';
1755
129eb428 1756 if (hwaddr_aton(cmd, bssid)) {
fcc60db4
JM
1757 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
1758 cmd);
1759 return -1;
1760 }
1761
52eb293d
JM
1762 new_ssid = os_strchr(pin, ' ');
1763 if (new_ssid == NULL)
129eb428 1764 return wpas_wps_start_reg(wpa_s, bssid, pin, NULL);
52eb293d
JM
1765 *new_ssid++ = '\0';
1766
1767 new_auth = os_strchr(new_ssid, ' ');
1768 if (new_auth == NULL)
1769 return -1;
1770 *new_auth++ = '\0';
1771
1772 new_encr = os_strchr(new_auth, ' ');
1773 if (new_encr == NULL)
1774 return -1;
1775 *new_encr++ = '\0';
1776
1777 new_key = os_strchr(new_encr, ' ');
1778 if (new_key == NULL)
1779 return -1;
1780 *new_key++ = '\0';
1781
1782 os_memset(&ap, 0, sizeof(ap));
1783 ap.ssid_hex = new_ssid;
1784 ap.auth = new_auth;
1785 ap.encr = new_encr;
1786 ap.key_hex = new_key;
129eb428 1787 return wpas_wps_start_reg(wpa_s, bssid, pin, &ap);
fcc60db4 1788}
72df2f5f
JM
1789
1790
70d84f11
JM
1791#ifdef CONFIG_AP
1792static int wpa_supplicant_ctrl_iface_wps_ap_pin(struct wpa_supplicant *wpa_s,
1793 char *cmd, char *buf,
1794 size_t buflen)
1795{
1796 int timeout = 300;
1797 char *pos;
1798 const char *pin_txt;
1799
1800 if (!wpa_s->ap_iface)
1801 return -1;
1802
1803 pos = os_strchr(cmd, ' ');
1804 if (pos)
1805 *pos++ = '\0';
1806
1807 if (os_strcmp(cmd, "disable") == 0) {
1808 wpas_wps_ap_pin_disable(wpa_s);
1809 return os_snprintf(buf, buflen, "OK\n");
1810 }
1811
1812 if (os_strcmp(cmd, "random") == 0) {
1813 if (pos)
1814 timeout = atoi(pos);
1815 pin_txt = wpas_wps_ap_pin_random(wpa_s, timeout);
1816 if (pin_txt == NULL)
1817 return -1;
1818 return os_snprintf(buf, buflen, "%s", pin_txt);
1819 }
1820
1821 if (os_strcmp(cmd, "get") == 0) {
1822 pin_txt = wpas_wps_ap_pin_get(wpa_s);
1823 if (pin_txt == NULL)
1824 return -1;
1825 return os_snprintf(buf, buflen, "%s", pin_txt);
1826 }
1827
1828 if (os_strcmp(cmd, "set") == 0) {
1829 char *pin;
1830 if (pos == NULL)
1831 return -1;
1832 pin = pos;
1833 pos = os_strchr(pos, ' ');
1834 if (pos) {
1835 *pos++ = '\0';
1836 timeout = atoi(pos);
1837 }
1838 if (os_strlen(pin) > buflen)
1839 return -1;
1840 if (wpas_wps_ap_pin_set(wpa_s, pin, timeout) < 0)
1841 return -1;
1842 return os_snprintf(buf, buflen, "%s", pin);
1843 }
1844
1845 return -1;
1846}
1847#endif /* CONFIG_AP */
1848
1849
72df2f5f
JM
1850#ifdef CONFIG_WPS_ER
1851static int wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant *wpa_s,
1852 char *cmd)
1853{
31fcea93
JM
1854 char *uuid = cmd, *pin, *pos;
1855 u8 addr_buf[ETH_ALEN], *addr = NULL;
72df2f5f
JM
1856 pin = os_strchr(uuid, ' ');
1857 if (pin == NULL)
1858 return -1;
1859 *pin++ = '\0';
31fcea93
JM
1860 pos = os_strchr(pin, ' ');
1861 if (pos) {
1862 *pos++ = '\0';
1863 if (hwaddr_aton(pos, addr_buf) == 0)
1864 addr = addr_buf;
1865 }
1866 return wpas_wps_er_add_pin(wpa_s, addr, uuid, pin);
72df2f5f 1867}
e64dcfd5
JM
1868
1869
1870static int wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant *wpa_s,
1871 char *cmd)
1872{
1873 char *uuid = cmd, *pin;
1874 pin = os_strchr(uuid, ' ');
1875 if (pin == NULL)
1876 return -1;
1877 *pin++ = '\0';
1878 return wpas_wps_er_learn(wpa_s, uuid, pin);
1879}
7d6640a6
JM
1880
1881
ef10f473
JM
1882static int wpa_supplicant_ctrl_iface_wps_er_set_config(
1883 struct wpa_supplicant *wpa_s, char *cmd)
1884{
1885 char *uuid = cmd, *id;
1886 id = os_strchr(uuid, ' ');
1887 if (id == NULL)
1888 return -1;
1889 *id++ = '\0';
1890 return wpas_wps_er_set_config(wpa_s, uuid, atoi(id));
1891}
1892
1893
7d6640a6
JM
1894static int wpa_supplicant_ctrl_iface_wps_er_config(
1895 struct wpa_supplicant *wpa_s, char *cmd)
1896{
1897 char *pin;
1898 char *new_ssid;
1899 char *new_auth;
1900 char *new_encr;
1901 char *new_key;
1902 struct wps_new_ap_settings ap;
1903
1904 pin = os_strchr(cmd, ' ');
1905 if (pin == NULL)
1906 return -1;
1907 *pin++ = '\0';
1908
1909 new_ssid = os_strchr(pin, ' ');
1910 if (new_ssid == NULL)
1911 return -1;
1912 *new_ssid++ = '\0';
1913
1914 new_auth = os_strchr(new_ssid, ' ');
1915 if (new_auth == NULL)
1916 return -1;
1917 *new_auth++ = '\0';
1918
1919 new_encr = os_strchr(new_auth, ' ');
1920 if (new_encr == NULL)
1921 return -1;
1922 *new_encr++ = '\0';
1923
1924 new_key = os_strchr(new_encr, ' ');
1925 if (new_key == NULL)
1926 return -1;
1927 *new_key++ = '\0';
1928
1929 os_memset(&ap, 0, sizeof(ap));
1930 ap.ssid_hex = new_ssid;
1931 ap.auth = new_auth;
1932 ap.encr = new_encr;
1933 ap.key_hex = new_key;
1934 return wpas_wps_er_config(wpa_s, cmd, pin, &ap);
1935}
1cea09a9
JM
1936
1937
1938#ifdef CONFIG_WPS_NFC
1939static int wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
1940 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
1941{
1942 int ndef;
1943 struct wpabuf *buf;
1944 int res;
1945 char *uuid;
1946
1947 uuid = os_strchr(cmd, ' ');
1948 if (uuid == NULL)
1949 return -1;
1950 *uuid++ = '\0';
1951
1952 if (os_strcmp(cmd, "WPS") == 0)
1953 ndef = 0;
1954 else if (os_strcmp(cmd, "NDEF") == 0)
1955 ndef = 1;
1956 else
1957 return -1;
1958
1959 buf = wpas_wps_er_nfc_config_token(wpa_s, ndef, uuid);
1960 if (buf == NULL)
1961 return -1;
1962
1963 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1964 wpabuf_len(buf));
1965 reply[res++] = '\n';
1966 reply[res] = '\0';
1967
1968 wpabuf_free(buf);
1969
1970 return res;
1971}
1972#endif /* CONFIG_WPS_NFC */
72df2f5f
JM
1973#endif /* CONFIG_WPS_ER */
1974
fcc60db4
JM
1975#endif /* CONFIG_WPS */
1976
1977
11ef8d35
JM
1978#ifdef CONFIG_IBSS_RSN
1979static int wpa_supplicant_ctrl_iface_ibss_rsn(
1980 struct wpa_supplicant *wpa_s, char *addr)
1981{
1982 u8 peer[ETH_ALEN];
1983
1984 if (hwaddr_aton(addr, peer)) {
1985 wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
a7b6c422 1986 "address '%s'", addr);
11ef8d35
JM
1987 return -1;
1988 }
1989
1990 wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
1991 MAC2STR(peer));
1992
1993 return ibss_rsn_start(wpa_s->ibss_rsn, peer);
1994}
1995#endif /* CONFIG_IBSS_RSN */
1996
1997
7de5688d
DW
1998static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
1999 char *rsp)
2000{
2001#ifdef IEEE8021X_EAPOL
2002 char *pos, *id_pos;
2003 int id;
2004 struct wpa_ssid *ssid;
2005
2006 pos = os_strchr(rsp, '-');
2007 if (pos == NULL)
2008 return -1;
2009 *pos++ = '\0';
2010 id_pos = pos;
2011 pos = os_strchr(pos, ':');
2012 if (pos == NULL)
2013 return -1;
2014 *pos++ = '\0';
2015 id = atoi(id_pos);
2016 wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
2017 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
2018 (u8 *) pos, os_strlen(pos));
2019
2020 ssid = wpa_config_get_network(wpa_s->conf, id);
2021 if (ssid == NULL) {
2022 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
2023 "to update", id);
2024 return -1;
2025 }
2026
2027 return wpa_supplicant_ctrl_iface_ctrl_rsp_handle(wpa_s, ssid, rsp,
2028 pos);
6fc6879b
JM
2029#else /* IEEE8021X_EAPOL */
2030 wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
2031 return -1;
2032#endif /* IEEE8021X_EAPOL */
2033}
2034
2035
2036static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
2037 const char *params,
2038 char *buf, size_t buflen)
2039{
2040 char *pos, *end, tmp[30];
0bc13468 2041 int res, verbose, wps, ret;
f9cd147d
JM
2042#ifdef CONFIG_HS20
2043 const u8 *hs20;
2044#endif /* CONFIG_HS20 */
993a8654
JM
2045 const u8 *sess_id;
2046 size_t sess_id_len;
6fc6879b 2047
a771c07d
JM
2048 if (os_strcmp(params, "-DRIVER") == 0)
2049 return wpa_drv_status(wpa_s, buf, buflen);
6fc6879b 2050 verbose = os_strcmp(params, "-VERBOSE") == 0;
0bc13468 2051 wps = os_strcmp(params, "-WPS") == 0;
6fc6879b
JM
2052 pos = buf;
2053 end = buf + buflen;
2054 if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
2055 struct wpa_ssid *ssid = wpa_s->current_ssid;
2056 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
2057 MAC2STR(wpa_s->bssid));
d85e1fc8 2058 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
2059 return pos - buf;
2060 pos += ret;
b6ebdfbe
BP
2061 ret = os_snprintf(pos, end - pos, "freq=%u\n",
2062 wpa_s->assoc_freq);
d85e1fc8 2063 if (os_snprintf_error(end - pos, ret))
b6ebdfbe
BP
2064 return pos - buf;
2065 pos += ret;
6fc6879b
JM
2066 if (ssid) {
2067 u8 *_ssid = ssid->ssid;
2068 size_t ssid_len = ssid->ssid_len;
eaa8eefe 2069 u8 ssid_buf[SSID_MAX_LEN];
6fc6879b
JM
2070 if (ssid_len == 0) {
2071 int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
2072 if (_res < 0)
2073 ssid_len = 0;
2074 else
2075 ssid_len = _res;
2076 _ssid = ssid_buf;
2077 }
2078 ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
2079 wpa_ssid_txt(_ssid, ssid_len),
2080 ssid->id);
d85e1fc8 2081 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
2082 return pos - buf;
2083 pos += ret;
2084
0bc13468
JM
2085 if (wps && ssid->passphrase &&
2086 wpa_key_mgmt_wpa_psk(ssid->key_mgmt) &&
2087 (ssid->mode == WPAS_MODE_AP ||
2088 ssid->mode == WPAS_MODE_P2P_GO)) {
2089 ret = os_snprintf(pos, end - pos,
2090 "passphrase=%s\n",
2091 ssid->passphrase);
d85e1fc8 2092 if (os_snprintf_error(end - pos, ret))
0bc13468
JM
2093 return pos - buf;
2094 pos += ret;
2095 }
6fc6879b
JM
2096 if (ssid->id_str) {
2097 ret = os_snprintf(pos, end - pos,
2098 "id_str=%s\n",
2099 ssid->id_str);
d85e1fc8 2100 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
2101 return pos - buf;
2102 pos += ret;
2103 }
0e15e529
JM
2104
2105 switch (ssid->mode) {
d7dcba70 2106 case WPAS_MODE_INFRA:
0e15e529
JM
2107 ret = os_snprintf(pos, end - pos,
2108 "mode=station\n");
2109 break;
d7dcba70 2110 case WPAS_MODE_IBSS:
0e15e529
JM
2111 ret = os_snprintf(pos, end - pos,
2112 "mode=IBSS\n");
2113 break;
d7dcba70 2114 case WPAS_MODE_AP:
0e15e529
JM
2115 ret = os_snprintf(pos, end - pos,
2116 "mode=AP\n");
2117 break;
2c5d725c
JM
2118 case WPAS_MODE_P2P_GO:
2119 ret = os_snprintf(pos, end - pos,
2120 "mode=P2P GO\n");
2121 break;
2122 case WPAS_MODE_P2P_GROUP_FORMATION:
2123 ret = os_snprintf(pos, end - pos,
2124 "mode=P2P GO - group "
2125 "formation\n");
2126 break;
cee0be73
SB
2127 case WPAS_MODE_MESH:
2128 ret = os_snprintf(pos, end - pos,
2129 "mode=mesh\n");
2130 break;
0e15e529
JM
2131 default:
2132 ret = 0;
2133 break;
2134 }
1f102d3b 2135 if (os_snprintf_error(end - pos, ret))
0e15e529
JM
2136 return pos - buf;
2137 pos += ret;
6fc6879b
JM
2138 }
2139
3f8ceff5
JM
2140 if (wpa_s->connection_set &&
2141 (wpa_s->connection_ht || wpa_s->connection_vht ||
2142 wpa_s->connection_he)) {
2143 ret = os_snprintf(pos, end - pos,
2144 "wifi_generation=%u\n",
2145 wpa_s->connection_he ? 6 :
2146 (wpa_s->connection_vht ? 5 : 4));
2147 if (os_snprintf_error(end - pos, ret))
2148 return pos - buf;
2149 pos += ret;
2150 }
2151
43fb5297
JM
2152#ifdef CONFIG_AP
2153 if (wpa_s->ap_iface) {
2154 pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
2155 end - pos,
2156 verbose);
2157 } else
2158#endif /* CONFIG_AP */
6fc6879b
JM
2159 pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
2160 }
afe73100 2161#ifdef CONFIG_SME
4954c859
JM
2162#ifdef CONFIG_SAE
2163 if (wpa_s->wpa_state >= WPA_ASSOCIATED &&
e1ae5d74
JM
2164#ifdef CONFIG_AP
2165 !wpa_s->ap_iface &&
2166#endif /* CONFIG_AP */
2167 wpa_s->sme.sae.state == SAE_ACCEPTED) {
4954c859
JM
2168 ret = os_snprintf(pos, end - pos, "sae_group=%d\n",
2169 wpa_s->sme.sae.group);
d85e1fc8 2170 if (os_snprintf_error(end - pos, ret))
4954c859
JM
2171 return pos - buf;
2172 pos += ret;
2173 }
2174#endif /* CONFIG_SAE */
afe73100 2175#endif /* CONFIG_SME */
6fc6879b
JM
2176 ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
2177 wpa_supplicant_state_txt(wpa_s->wpa_state));
d85e1fc8 2178 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
2179 return pos - buf;
2180 pos += ret;
2181
2182 if (wpa_s->l2 &&
2183 l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
2184 ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
d85e1fc8 2185 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
2186 return pos - buf;
2187 pos += ret;
2188 }
2189
d23bd894
JM
2190#ifdef CONFIG_P2P
2191 if (wpa_s->global->p2p) {
2192 ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
2193 "\n", MAC2STR(wpa_s->global->p2p_dev_addr));
d85e1fc8 2194 if (os_snprintf_error(end - pos, ret))
d23bd894
JM
2195 return pos - buf;
2196 pos += ret;
2197 }
b21e2c84 2198#endif /* CONFIG_P2P */
6d4747a9
JM
2199
2200 ret = os_snprintf(pos, end - pos, "address=" MACSTR "\n",
2201 MAC2STR(wpa_s->own_addr));
d85e1fc8 2202 if (os_snprintf_error(end - pos, ret))
6d4747a9
JM
2203 return pos - buf;
2204 pos += ret;
d23bd894 2205
64855b96
JM
2206#ifdef CONFIG_HS20
2207 if (wpa_s->current_bss &&
f9cd147d
JM
2208 (hs20 = wpa_bss_get_vendor_ie(wpa_s->current_bss,
2209 HS20_IE_VENDOR_TYPE)) &&
4ed34f5a
JM
2210 wpa_s->wpa_proto == WPA_PROTO_RSN &&
2211 wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
f9cd147d
JM
2212 int release = 1;
2213 if (hs20[1] >= 5) {
2214 u8 rel_num = (hs20[6] & 0xf0) >> 4;
2215 release = rel_num + 1;
2216 }
2217 ret = os_snprintf(pos, end - pos, "hs20=%d\n", release);
d85e1fc8 2218 if (os_snprintf_error(end - pos, ret))
64855b96
JM
2219 return pos - buf;
2220 pos += ret;
2221 }
e99b4f3a
JM
2222
2223 if (wpa_s->current_ssid) {
2224 struct wpa_cred *cred;
2225 char *type;
2226
2227 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
463c8ffb
JM
2228 size_t i;
2229
e99b4f3a
JM
2230 if (wpa_s->current_ssid->parent_cred != cred)
2231 continue;
e99b4f3a 2232
aa26ba68 2233 if (cred->provisioning_sp) {
463c8ffb 2234 ret = os_snprintf(pos, end - pos,
aa26ba68
JM
2235 "provisioning_sp=%s\n",
2236 cred->provisioning_sp);
d85e1fc8 2237 if (os_snprintf_error(end - pos, ret))
463c8ffb
JM
2238 return pos - buf;
2239 pos += ret;
2240 }
e99b4f3a 2241
aa26ba68
JM
2242 if (!cred->domain)
2243 goto no_domain;
2244
2245 i = 0;
2246 if (wpa_s->current_bss && wpa_s->current_bss->anqp) {
2247 struct wpabuf *names =
2248 wpa_s->current_bss->anqp->domain_name;
2249 for (i = 0; names && i < cred->num_domain; i++)
2250 {
2251 if (domain_name_list_contains(
2252 names, cred->domain[i], 1))
2253 break;
2254 }
2255 if (i == cred->num_domain)
2256 i = 0; /* show first entry by default */
2257 }
2258 ret = os_snprintf(pos, end - pos, "home_sp=%s\n",
2259 cred->domain[i]);
d85e1fc8 2260 if (os_snprintf_error(end - pos, ret))
aa26ba68
JM
2261 return pos - buf;
2262 pos += ret;
2263
2264 no_domain:
e99b4f3a
JM
2265 if (wpa_s->current_bss == NULL ||
2266 wpa_s->current_bss->anqp == NULL)
2267 res = -1;
2268 else
2269 res = interworking_home_sp_cred(
2270 wpa_s, cred,
2271 wpa_s->current_bss->anqp->domain_name);
2272 if (res > 0)
2273 type = "home";
2274 else if (res == 0)
2275 type = "roaming";
2276 else
2277 type = "unknown";
2278
2279 ret = os_snprintf(pos, end - pos, "sp_type=%s\n", type);
d85e1fc8 2280 if (os_snprintf_error(end - pos, ret))
e99b4f3a
JM
2281 return pos - buf;
2282 pos += ret;
2283
2284 break;
2285 }
2286 }
64855b96
JM
2287#endif /* CONFIG_HS20 */
2288
56586197
JM
2289 if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
2290 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
6fc6879b
JM
2291 res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
2292 verbose);
2293 if (res >= 0)
2294 pos += res;
2295 }
2296
7508c2ad
BA
2297#ifdef CONFIG_MACSEC
2298 res = ieee802_1x_kay_get_status(wpa_s->kay, pos, end - pos);
2299 if (res > 0)
2300 pos += res;
2301#endif /* CONFIG_MACSEC */
2302
993a8654
JM
2303 sess_id = eapol_sm_get_session_id(wpa_s->eapol, &sess_id_len);
2304 if (sess_id) {
2305 char *start = pos;
2306
2307 ret = os_snprintf(pos, end - pos, "eap_session_id=");
2308 if (os_snprintf_error(end - pos, ret))
2309 return start - buf;
2310 pos += ret;
2311 ret = wpa_snprintf_hex(pos, end - pos, sess_id, sess_id_len);
2312 if (ret <= 0)
2313 return start - buf;
2314 pos += ret;
2315 ret = os_snprintf(pos, end - pos, "\n");
2316 if (os_snprintf_error(end - pos, ret))
2317 return start - buf;
2318 pos += ret;
2319 }
2320
6fc6879b
JM
2321 res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
2322 if (res >= 0)
2323 pos += res;
2324
8aaafcee
JM
2325#ifdef CONFIG_WPS
2326 {
2327 char uuid_str[100];
2328 uuid_bin2str(wpa_s->wps->uuid, uuid_str, sizeof(uuid_str));
2329 ret = os_snprintf(pos, end - pos, "uuid=%s\n", uuid_str);
d85e1fc8 2330 if (os_snprintf_error(end - pos, ret))
8aaafcee
JM
2331 return pos - buf;
2332 pos += ret;
2333 }
2334#endif /* CONFIG_WPS */
2335
d7f038de
BP
2336 if (wpa_s->ieee80211ac) {
2337 ret = os_snprintf(pos, end - pos, "ieee80211ac=1\n");
2338 if (os_snprintf_error(end - pos, ret))
2339 return pos - buf;
2340 pos += ret;
2341 }
2342
f6c2b8c3 2343#ifdef ANDROID
a6ab82d7 2344 /*
2345 * Allow using the STATUS command with default behavior, say for debug,
2346 * i.e., don't generate a "fake" CONNECTION and SUPPLICANT_STATE_CHANGE
2347 * events with STATUS-NO_EVENTS.
2348 */
2349 if (os_strcmp(params, "-NO_EVENTS")) {
2350 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_STATE_CHANGE
2351 "id=%d state=%d BSSID=" MACSTR " SSID=%s",
2352 wpa_s->current_ssid ? wpa_s->current_ssid->id : -1,
2353 wpa_s->wpa_state,
2354 MAC2STR(wpa_s->bssid),
2355 wpa_s->current_ssid && wpa_s->current_ssid->ssid ?
2356 wpa_ssid_txt(wpa_s->current_ssid->ssid,
2357 wpa_s->current_ssid->ssid_len) : "");
2358 if (wpa_s->wpa_state == WPA_COMPLETED) {
2359 struct wpa_ssid *ssid = wpa_s->current_ssid;
2360 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED
2361 "- connection to " MACSTR
2362 " completed %s [id=%d id_str=%s]",
2363 MAC2STR(wpa_s->bssid), "(auth)",
2364 ssid ? ssid->id : -1,
2365 ssid && ssid->id_str ? ssid->id_str : "");
2366 }
f6c2b8c3
DS
2367 }
2368#endif /* ANDROID */
2369
6fc6879b
JM
2370 return pos - buf;
2371}
2372
2373
2374static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
2375 char *cmd)
2376{
2377 char *pos;
2378 int id;
2379 struct wpa_ssid *ssid;
2380 u8 bssid[ETH_ALEN];
2381
2382 /* cmd: "<network id> <BSSID>" */
2383 pos = os_strchr(cmd, ' ');
2384 if (pos == NULL)
2385 return -1;
2386 *pos++ = '\0';
2387 id = atoi(cmd);
2388 wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
2389 if (hwaddr_aton(pos, bssid)) {
2390 wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
2391 return -1;
2392 }
2393
2394 ssid = wpa_config_get_network(wpa_s->conf, id);
2395 if (ssid == NULL) {
2396 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
2397 "to update", id);
2398 return -1;
2399 }
2400
2401 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
a8e16edc 2402 ssid->bssid_set = !is_zero_ether_addr(bssid);
6fc6879b
JM
2403
2404 return 0;
2405}
2406
2407
9aa10e2b
DS
2408static int wpa_supplicant_ctrl_iface_blacklist(struct wpa_supplicant *wpa_s,
2409 char *cmd, char *buf,
2410 size_t buflen)
2411{
2412 u8 bssid[ETH_ALEN];
2413 struct wpa_blacklist *e;
2414 char *pos, *end;
2415 int ret;
2416
2417 /* cmd: "BLACKLIST [<BSSID>]" */
2418 if (*cmd == '\0') {
2419 pos = buf;
2420 end = buf + buflen;
2421 e = wpa_s->blacklist;
2422 while (e) {
2423 ret = os_snprintf(pos, end - pos, MACSTR "\n",
2424 MAC2STR(e->bssid));
d85e1fc8 2425 if (os_snprintf_error(end - pos, ret))
9aa10e2b
DS
2426 return pos - buf;
2427 pos += ret;
2428 e = e->next;
2429 }
2430 return pos - buf;
2431 }
2432
2433 cmd++;
2434 if (os_strncmp(cmd, "clear", 5) == 0) {
2435 wpa_blacklist_clear(wpa_s);
2436 os_memcpy(buf, "OK\n", 3);
2437 return 3;
2438 }
2439
2440 wpa_printf(MSG_DEBUG, "CTRL_IFACE: BLACKLIST bssid='%s'", cmd);
2441 if (hwaddr_aton(cmd, bssid)) {
2442 wpa_printf(MSG_DEBUG, "CTRL_IFACE: invalid BSSID '%s'", cmd);
2443 return -1;
2444 }
2445
2446 /*
2447 * Add the BSSID twice, so its count will be 2, causing it to be
2448 * skipped when processing scan results.
2449 */
2450 ret = wpa_blacklist_add(wpa_s, bssid);
bd8838a3 2451 if (ret < 0)
9aa10e2b
DS
2452 return -1;
2453 ret = wpa_blacklist_add(wpa_s, bssid);
bd8838a3 2454 if (ret < 0)
9aa10e2b
DS
2455 return -1;
2456 os_memcpy(buf, "OK\n", 3);
2457 return 3;
2458}
2459
2460
0597a5b5
DS
2461static int wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant *wpa_s,
2462 char *cmd, char *buf,
2463 size_t buflen)
2464{
2465 char *pos, *end, *stamp;
2466 int ret;
2467
0597a5b5
DS
2468 /* cmd: "LOG_LEVEL [<level>]" */
2469 if (*cmd == '\0') {
2470 pos = buf;
2471 end = buf + buflen;
2472 ret = os_snprintf(pos, end - pos, "Current level: %s\n"
2473 "Timestamp: %d\n",
2474 debug_level_str(wpa_debug_level),
2475 wpa_debug_timestamp);
d85e1fc8 2476 if (os_snprintf_error(end - pos, ret))
0597a5b5
DS
2477 ret = 0;
2478
2479 return ret;
2480 }
2481
2482 while (*cmd == ' ')
2483 cmd++;
2484
2485 stamp = os_strchr(cmd, ' ');
2486 if (stamp) {
2487 *stamp++ = '\0';
2488 while (*stamp == ' ') {
2489 stamp++;
2490 }
2491 }
2492
137b2939 2493 if (os_strlen(cmd)) {
0597a5b5
DS
2494 int level = str_to_debug_level(cmd);
2495 if (level < 0)
2496 return -1;
2497 wpa_debug_level = level;
2498 }
2499
2500 if (stamp && os_strlen(stamp))
2501 wpa_debug_timestamp = atoi(stamp);
2502
2503 os_memcpy(buf, "OK\n", 3);
2504 return 3;
2505}
2506
2507
6fc6879b 2508static int wpa_supplicant_ctrl_iface_list_networks(
90903a77 2509 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
6fc6879b 2510{
f34891a3 2511 char *pos, *end, *prev;
6fc6879b
JM
2512 struct wpa_ssid *ssid;
2513 int ret;
2514
2515 pos = buf;
2516 end = buf + buflen;
2517 ret = os_snprintf(pos, end - pos,
2518 "network id / ssid / bssid / flags\n");
d85e1fc8 2519 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
2520 return pos - buf;
2521 pos += ret;
2522
2523 ssid = wpa_s->conf->ssid;
90903a77
VD
2524
2525 /* skip over ssids until we find next one */
2526 if (cmd != NULL && os_strncmp(cmd, "LAST_ID=", 8) == 0) {
2527 int last_id = atoi(cmd + 8);
2528 if (last_id != -1) {
2529 while (ssid != NULL && ssid->id <= last_id) {
2530 ssid = ssid->next;
2531 }
2532 }
2533 }
2534
6fc6879b 2535 while (ssid) {
f34891a3 2536 prev = pos;
6fc6879b
JM
2537 ret = os_snprintf(pos, end - pos, "%d\t%s",
2538 ssid->id,
2539 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
d85e1fc8 2540 if (os_snprintf_error(end - pos, ret))
f34891a3 2541 return prev - buf;
6fc6879b
JM
2542 pos += ret;
2543 if (ssid->bssid_set) {
2544 ret = os_snprintf(pos, end - pos, "\t" MACSTR,
2545 MAC2STR(ssid->bssid));
2546 } else {
2547 ret = os_snprintf(pos, end - pos, "\tany");
2548 }
d85e1fc8 2549 if (os_snprintf_error(end - pos, ret))
f34891a3 2550 return prev - buf;
6fc6879b 2551 pos += ret;
00e5e3d5 2552 ret = os_snprintf(pos, end - pos, "\t%s%s%s%s",
6fc6879b
JM
2553 ssid == wpa_s->current_ssid ?
2554 "[CURRENT]" : "",
4dac0245 2555 ssid->disabled ? "[DISABLED]" : "",
00e5e3d5
JM
2556 ssid->disabled_until.sec ?
2557 "[TEMP-DISABLED]" : "",
4dac0245
JM
2558 ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
2559 "");
d85e1fc8 2560 if (os_snprintf_error(end - pos, ret))
f34891a3 2561 return prev - buf;
6fc6879b
JM
2562 pos += ret;
2563 ret = os_snprintf(pos, end - pos, "\n");
d85e1fc8 2564 if (os_snprintf_error(end - pos, ret))
f34891a3 2565 return prev - buf;
6fc6879b
JM
2566 pos += ret;
2567
2568 ssid = ssid->next;
2569 }
2570
2571 return pos - buf;
2572}
2573
2574
2575static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
2576{
0282a8c4 2577 int ret;
6fc6879b 2578 ret = os_snprintf(pos, end - pos, "-");
d85e1fc8 2579 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
2580 return pos;
2581 pos += ret;
0282a8c4
JM
2582 ret = wpa_write_ciphers(pos, end, cipher, "+");
2583 if (ret < 0)
2584 return pos;
2585 pos += ret;
6fc6879b
JM
2586 return pos;
2587}
2588
2589
2590static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
2591 const u8 *ie, size_t ie_len)
2592{
2593 struct wpa_ie_data data;
ea3b8c1d
JM
2594 char *start;
2595 int ret;
6fc6879b
JM
2596
2597 ret = os_snprintf(pos, end - pos, "[%s-", proto);
d85e1fc8 2598 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
2599 return pos;
2600 pos += ret;
2601
2602 if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
2603 ret = os_snprintf(pos, end - pos, "?]");
d85e1fc8 2604 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
2605 return pos;
2606 pos += ret;
2607 return pos;
2608 }
2609
ea3b8c1d 2610 start = pos;
6fc6879b 2611 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
ea3b8c1d
JM
2612 ret = os_snprintf(pos, end - pos, "%sEAP",
2613 pos == start ? "" : "+");
d85e1fc8 2614 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
2615 return pos;
2616 pos += ret;
6fc6879b
JM
2617 }
2618 if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
ea3b8c1d
JM
2619 ret = os_snprintf(pos, end - pos, "%sPSK",
2620 pos == start ? "" : "+");
d85e1fc8 2621 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
2622 return pos;
2623 pos += ret;
6fc6879b
JM
2624 }
2625 if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
ea3b8c1d
JM
2626 ret = os_snprintf(pos, end - pos, "%sNone",
2627 pos == start ? "" : "+");
d85e1fc8 2628 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
2629 return pos;
2630 pos += ret;
6fc6879b 2631 }
be6b29f6
JA
2632 if (data.key_mgmt & WPA_KEY_MGMT_SAE) {
2633 ret = os_snprintf(pos, end - pos, "%sSAE",
2634 pos == start ? "" : "+");
d85e1fc8 2635 if (os_snprintf_error(end - pos, ret))
be6b29f6
JA
2636 return pos;
2637 pos += ret;
2638 }
6fc6879b
JM
2639#ifdef CONFIG_IEEE80211R
2640 if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
2641 ret = os_snprintf(pos, end - pos, "%sFT/EAP",
ea3b8c1d 2642 pos == start ? "" : "+");
d85e1fc8 2643 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
2644 return pos;
2645 pos += ret;
6fc6879b
JM
2646 }
2647 if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
2648 ret = os_snprintf(pos, end - pos, "%sFT/PSK",
ea3b8c1d 2649 pos == start ? "" : "+");
d85e1fc8 2650 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
2651 return pos;
2652 pos += ret;
6fc6879b 2653 }
be6b29f6
JA
2654 if (data.key_mgmt & WPA_KEY_MGMT_FT_SAE) {
2655 ret = os_snprintf(pos, end - pos, "%sFT/SAE",
2656 pos == start ? "" : "+");
d85e1fc8 2657 if (os_snprintf_error(end - pos, ret))
be6b29f6
JA
2658 return pos;
2659 pos += ret;
2660 }
6fc6879b 2661#endif /* CONFIG_IEEE80211R */
56586197
JM
2662 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
2663 ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
ea3b8c1d 2664 pos == start ? "" : "+");
d85e1fc8 2665 if (os_snprintf_error(end - pos, ret))
56586197
JM
2666 return pos;
2667 pos += ret;
56586197
JM
2668 }
2669 if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
2670 ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
ea3b8c1d 2671 pos == start ? "" : "+");
d85e1fc8 2672 if (os_snprintf_error(end - pos, ret))
56586197
JM
2673 return pos;
2674 pos += ret;
56586197 2675 }
6fc6879b 2676
5e3b5197 2677#ifdef CONFIG_SUITEB
666497c8
JM
2678 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
2679 ret = os_snprintf(pos, end - pos, "%sEAP-SUITE-B",
2680 pos == start ? "" : "+");
d85e1fc8 2681 if (os_snprintf_error(end - pos, ret))
666497c8
JM
2682 return pos;
2683 pos += ret;
2684 }
5e3b5197
JM
2685#endif /* CONFIG_SUITEB */
2686
2687#ifdef CONFIG_SUITEB192
2688 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
2689 ret = os_snprintf(pos, end - pos, "%sEAP-SUITE-B-192",
2690 pos == start ? "" : "+");
2691 if (os_snprintf_error(end - pos, ret))
2692 return pos;
2693 pos += ret;
2694 }
2695#endif /* CONFIG_SUITEB192 */
666497c8 2696
7147a834
JM
2697#ifdef CONFIG_FILS
2698 if (data.key_mgmt & WPA_KEY_MGMT_FILS_SHA256) {
2699 ret = os_snprintf(pos, end - pos, "%sFILS-SHA256",
2700 pos == start ? "" : "+");
2701 if (os_snprintf_error(end - pos, ret))
2702 return pos;
2703 pos += ret;
2704 }
2705 if (data.key_mgmt & WPA_KEY_MGMT_FILS_SHA384) {
2706 ret = os_snprintf(pos, end - pos, "%sFILS-SHA384",
2707 pos == start ? "" : "+");
2708 if (os_snprintf_error(end - pos, ret))
2709 return pos;
2710 pos += ret;
2711 }
2712#ifdef CONFIG_IEEE80211R
2713 if (data.key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256) {
2714 ret = os_snprintf(pos, end - pos, "%sFT-FILS-SHA256",
2715 pos == start ? "" : "+");
2716 if (os_snprintf_error(end - pos, ret))
2717 return pos;
2718 pos += ret;
2719 }
2720 if (data.key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384) {
2721 ret = os_snprintf(pos, end - pos, "%sFT-FILS-SHA384",
2722 pos == start ? "" : "+");
2723 if (os_snprintf_error(end - pos, ret))
2724 return pos;
2725 pos += ret;
2726 }
2727#endif /* CONFIG_IEEE80211R */
2728#endif /* CONFIG_FILS */
2729
a1ea1b45
JM
2730#ifdef CONFIG_OWE
2731 if (data.key_mgmt & WPA_KEY_MGMT_OWE) {
2732 ret = os_snprintf(pos, end - pos, "%sOWE",
2733 pos == start ? "" : "+");
2734 if (os_snprintf_error(end - pos, ret))
2735 return pos;
2736 pos += ret;
2737 }
2738#endif /* CONFIG_OWE */
2739
567da5bb
JM
2740#ifdef CONFIG_DPP
2741 if (data.key_mgmt & WPA_KEY_MGMT_DPP) {
2742 ret = os_snprintf(pos, end - pos, "%sDPP",
2743 pos == start ? "" : "+");
2744 if (os_snprintf_error(end - pos, ret))
2745 return pos;
2746 pos += ret;
2747 }
2748#endif /* CONFIG_DPP */
2749
0f8385e6
BG
2750 if (data.key_mgmt & WPA_KEY_MGMT_OSEN) {
2751 ret = os_snprintf(pos, end - pos, "%sOSEN",
2752 pos == start ? "" : "+");
2753 if (os_snprintf_error(end - pos, ret))
2754 return pos;
2755 pos += ret;
2756 }
2757
6fc6879b
JM
2758 pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
2759
2760 if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
2761 ret = os_snprintf(pos, end - pos, "-preauth");
d85e1fc8 2762 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
2763 return pos;
2764 pos += ret;
2765 }
2766
2767 ret = os_snprintf(pos, end - pos, "]");
d85e1fc8 2768 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
2769 return pos;
2770 pos += ret;
2771
2772 return pos;
2773}
2774
3a068632 2775
eef7d7a1 2776#ifdef CONFIG_WPS
31fcea93
JM
2777static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
2778 char *pos, char *end,
3a068632
JM
2779 struct wpabuf *wps_ie)
2780{
eef7d7a1
JM
2781 int ret;
2782 const char *txt;
2783
eef7d7a1
JM
2784 if (wps_ie == NULL)
2785 return pos;
eef7d7a1
JM
2786 if (wps_is_selected_pbc_registrar(wps_ie))
2787 txt = "[WPS-PBC]";
31fcea93
JM
2788 else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
2789 txt = "[WPS-AUTH]";
eef7d7a1
JM
2790 else if (wps_is_selected_pin_registrar(wps_ie))
2791 txt = "[WPS-PIN]";
2792 else
2793 txt = "[WPS]";
2794
2795 ret = os_snprintf(pos, end - pos, "%s", txt);
a80ba67a 2796 if (!os_snprintf_error(end - pos, ret))
eef7d7a1
JM
2797 pos += ret;
2798 wpabuf_free(wps_ie);
3a068632
JM
2799 return pos;
2800}
2801#endif /* CONFIG_WPS */
2802
2803
31fcea93
JM
2804static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
2805 char *pos, char *end,
16b71ac2 2806 const struct wpa_bss *bss)
3a068632
JM
2807{
2808#ifdef CONFIG_WPS
2809 struct wpabuf *wps_ie;
2810 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
31fcea93 2811 return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
3a068632 2812#else /* CONFIG_WPS */
eef7d7a1 2813 return pos;
3a068632 2814#endif /* CONFIG_WPS */
eef7d7a1
JM
2815}
2816
6fc6879b
JM
2817
2818/* Format one result on one text line into a buffer. */
2819static int wpa_supplicant_ctrl_iface_scan_result(
31fcea93 2820 struct wpa_supplicant *wpa_s,
16b71ac2 2821 const struct wpa_bss *bss, char *buf, size_t buflen)
6fc6879b
JM
2822{
2823 char *pos, *end;
2824 int ret;
e8b96490 2825 const u8 *ie, *ie2, *osen_ie, *p2p, *mesh, *owe;
0c6b310e 2826
638d9456 2827 mesh = wpa_bss_get_ie(bss, WLAN_EID_MESH_ID);
0c6b310e 2828 p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
bb50ae43
JM
2829 if (!p2p)
2830 p2p = wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE);
0c6b310e
JM
2831 if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
2832 os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
2833 0)
2834 return 0; /* Do not show P2P listen discovery results here */
6fc6879b
JM
2835
2836 pos = buf;
2837 end = buf + buflen;
2838
2839 ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
16b71ac2 2840 MAC2STR(bss->bssid), bss->freq, bss->level);
d85e1fc8 2841 if (os_snprintf_error(end - pos, ret))
fb0e5bd7 2842 return -1;
6fc6879b 2843 pos += ret;
16b71ac2 2844 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
6fc6879b
JM
2845 if (ie)
2846 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
16b71ac2 2847 ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
638d9456
JA
2848 if (ie2) {
2849 pos = wpa_supplicant_ie_txt(pos, end, mesh ? "RSN" : "WPA2",
2850 ie2, 2 + ie2[1]);
2851 }
0f8385e6
BG
2852 osen_ie = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
2853 if (osen_ie)
2854 pos = wpa_supplicant_ie_txt(pos, end, "OSEN",
2855 osen_ie, 2 + osen_ie[1]);
e8b96490
JM
2856 owe = wpa_bss_get_vendor_ie(bss, OWE_IE_VENDOR_TYPE);
2857 if (owe) {
2858 ret = os_snprintf(pos, end - pos,
2859 ie2 ? "[OWE-TRANS]" : "[OWE-TRANS-OPEN]");
2860 if (os_snprintf_error(end - pos, ret))
2861 return -1;
2862 pos += ret;
2863 }
31fcea93 2864 pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
0f8385e6 2865 if (!ie && !ie2 && !osen_ie && (bss->caps & IEEE80211_CAP_PRIVACY)) {
6fc6879b 2866 ret = os_snprintf(pos, end - pos, "[WEP]");
d85e1fc8 2867 if (os_snprintf_error(end - pos, ret))
fb0e5bd7 2868 return -1;
6fc6879b
JM
2869 pos += ret;
2870 }
638d9456
JA
2871 if (mesh) {
2872 ret = os_snprintf(pos, end - pos, "[MESH]");
d85e1fc8 2873 if (os_snprintf_error(end - pos, ret))
638d9456
JA
2874 return -1;
2875 pos += ret;
2876 }
e403ba85
BS
2877 if (bss_is_dmg(bss)) {
2878 const char *s;
35aed771
AAL
2879
2880 if (get_ie_ext((const u8 *) (bss + 1), bss->ie_len,
2881 WLAN_EID_EXT_EDMG_OPERATION)) {
2882 ret = os_snprintf(pos, end - pos, "[EDMG]");
2883 if (os_snprintf_error(end - pos, ret))
2884 return -1;
2885 pos += ret;
2886 }
2887
e403ba85 2888 ret = os_snprintf(pos, end - pos, "[DMG]");
d85e1fc8 2889 if (os_snprintf_error(end - pos, ret))
fb0e5bd7 2890 return -1;
6fc6879b 2891 pos += ret;
e403ba85
BS
2892 switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
2893 case IEEE80211_CAP_DMG_IBSS:
2894 s = "[IBSS]";
2895 break;
2896 case IEEE80211_CAP_DMG_AP:
2897 s = "[ESS]";
2898 break;
2899 case IEEE80211_CAP_DMG_PBSS:
2900 s = "[PBSS]";
2901 break;
2902 default:
2903 s = "";
2904 break;
2905 }
2906 ret = os_snprintf(pos, end - pos, "%s", s);
d85e1fc8 2907 if (os_snprintf_error(end - pos, ret))
fb0e5bd7 2908 return -1;
bd1af96a 2909 pos += ret;
e403ba85
BS
2910 } else {
2911 if (bss->caps & IEEE80211_CAP_IBSS) {
2912 ret = os_snprintf(pos, end - pos, "[IBSS]");
d85e1fc8 2913 if (os_snprintf_error(end - pos, ret))
e403ba85
BS
2914 return -1;
2915 pos += ret;
2916 }
2917 if (bss->caps & IEEE80211_CAP_ESS) {
2918 ret = os_snprintf(pos, end - pos, "[ESS]");
d85e1fc8 2919 if (os_snprintf_error(end - pos, ret))
e403ba85
BS
2920 return -1;
2921 pos += ret;
2922 }
bd1af96a 2923 }
0c6b310e
JM
2924 if (p2p) {
2925 ret = os_snprintf(pos, end - pos, "[P2P]");
d85e1fc8 2926 if (os_snprintf_error(end - pos, ret))
fb0e5bd7 2927 return -1;
0c6b310e
JM
2928 pos += ret;
2929 }
64855b96 2930#ifdef CONFIG_HS20
4ed34f5a 2931 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE) && ie2) {
64855b96 2932 ret = os_snprintf(pos, end - pos, "[HS20]");
d85e1fc8 2933 if (os_snprintf_error(end - pos, ret))
64855b96
JM
2934 return -1;
2935 pos += ret;
2936 }
2937#endif /* CONFIG_HS20 */
7147a834
JM
2938#ifdef CONFIG_FILS
2939 if (wpa_bss_get_ie(bss, WLAN_EID_FILS_INDICATION)) {
2940 ret = os_snprintf(pos, end - pos, "[FILS]");
2941 if (os_snprintf_error(end - pos, ret))
2942 return -1;
2943 pos += ret;
2944 }
2945#endif /* CONFIG_FILS */
55de4d4b
AN
2946#ifdef CONFIG_FST
2947 if (wpa_bss_get_ie(bss, WLAN_EID_MULTI_BAND)) {
2948 ret = os_snprintf(pos, end - pos, "[FST]");
2949 if (os_snprintf_error(end - pos, ret))
2950 return -1;
2951 pos += ret;
2952 }
2953#endif /* CONFIG_FST */
ed87f6a8 2954 if (wpa_bss_ext_capab(bss, WLAN_EXT_CAPAB_UTF_8_SSID)) {
1f2c42cc
JW
2955 ret = os_snprintf(pos, end - pos, "[UTF-8]");
2956 if (os_snprintf_error(end - pos, ret))
2957 return -1;
2958 pos += ret;
2959 }
6fc6879b 2960
6fc6879b 2961 ret = os_snprintf(pos, end - pos, "\t%s",
16b71ac2 2962 wpa_ssid_txt(bss->ssid, bss->ssid_len));
d85e1fc8 2963 if (os_snprintf_error(end - pos, ret))
fb0e5bd7 2964 return -1;
6fc6879b
JM
2965 pos += ret;
2966
2967 ret = os_snprintf(pos, end - pos, "\n");
d85e1fc8 2968 if (os_snprintf_error(end - pos, ret))
fb0e5bd7 2969 return -1;
6fc6879b
JM
2970 pos += ret;
2971
2972 return pos - buf;
2973}
2974
2975
2976static int wpa_supplicant_ctrl_iface_scan_results(
2977 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
2978{
2979 char *pos, *end;
16b71ac2 2980 struct wpa_bss *bss;
6fc6879b 2981 int ret;
6fc6879b
JM
2982
2983 pos = buf;
2984 end = buf + buflen;
2985 ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
2986 "flags / ssid\n");
d85e1fc8 2987 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
2988 return pos - buf;
2989 pos += ret;
2990
16b71ac2 2991 dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
31fcea93 2992 ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
6fc6879b
JM
2993 end - pos);
2994 if (ret < 0 || ret >= end - pos)
2995 return pos - buf;
2996 pos += ret;
2997 }
2998
2999 return pos - buf;
3000}
3001
3002
603a3f34
JL
3003#ifdef CONFIG_MESH
3004
5b78493f
MH
3005static int wpa_supplicant_ctrl_iface_mesh_interface_add(
3006 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
3007{
3008 char *pos, ifname[IFNAMSIZ + 1];
3009
3010 ifname[0] = '\0';
3011
3012 pos = os_strstr(cmd, "ifname=");
3013 if (pos) {
3014 pos += 7;
3015 os_strlcpy(ifname, pos, sizeof(ifname));
3016 }
3017
3018 if (wpas_mesh_add_interface(wpa_s, ifname, sizeof(ifname)) < 0)
3019 return -1;
3020
3021 os_strlcpy(reply, ifname, max_len);
3022 return os_strlen(ifname);
3023}
3024
3025
603a3f34
JL
3026static int wpa_supplicant_ctrl_iface_mesh_group_add(
3027 struct wpa_supplicant *wpa_s, char *cmd)
3028{
3029 int id;
3030 struct wpa_ssid *ssid;
3031
3032 id = atoi(cmd);
3033 wpa_printf(MSG_DEBUG, "CTRL_IFACE: MESH_GROUP_ADD id=%d", id);
3034
3035 ssid = wpa_config_get_network(wpa_s->conf, id);
3036 if (ssid == NULL) {
3037 wpa_printf(MSG_DEBUG,
3038 "CTRL_IFACE: Could not find network id=%d", id);
3039 return -1;
3040 }
3041 if (ssid->mode != WPAS_MODE_MESH) {
3042 wpa_printf(MSG_DEBUG,
3043 "CTRL_IFACE: Cannot use MESH_GROUP_ADD on a non mesh network");
3044 return -1;
3045 }
0c6099f3
MH
3046 if (ssid->key_mgmt != WPA_KEY_MGMT_NONE &&
3047 ssid->key_mgmt != WPA_KEY_MGMT_SAE) {
3048 wpa_printf(MSG_ERROR,
3049 "CTRL_IFACE: key_mgmt for mesh network should be open or SAE");
3050 return -1;
3051 }
603a3f34
JL
3052
3053 /*
3054 * TODO: If necessary write our own group_add function,
3055 * for now we can reuse select_network
3056 */
3057 wpa_supplicant_select_network(wpa_s, ssid);
3058
3059 return 0;
3060}
3061
3062
3063static int wpa_supplicant_ctrl_iface_mesh_group_remove(
3064 struct wpa_supplicant *wpa_s, char *cmd)
3065{
5b78493f
MH
3066 struct wpa_supplicant *orig;
3067 struct wpa_global *global;
3068 int found = 0;
3069
3070 wpa_printf(MSG_DEBUG, "CTRL_IFACE: MESH_GROUP_REMOVE ifname=%s", cmd);
3071
3072 global = wpa_s->global;
3073 orig = wpa_s;
3074
3075 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3076 if (os_strcmp(wpa_s->ifname, cmd) == 0) {
3077 found = 1;
3078 break;
3079 }
3080 }
3081 if (!found) {
3082 wpa_printf(MSG_ERROR,
3083 "CTRL_IFACE: MESH_GROUP_REMOVE ifname=%s not found",
603a3f34
JL
3084 cmd);
3085 return -1;
3086 }
5b78493f
MH
3087 if (wpa_s->mesh_if_created && wpa_s == orig) {
3088 wpa_printf(MSG_ERROR,
3089 "CTRL_IFACE: MESH_GROUP_REMOVE can't remove itself");
3090 return -1;
3091 }
603a3f34
JL
3092
3093 wpa_s->reassociate = 0;
3094 wpa_s->disconnected = 1;
3095 wpa_supplicant_cancel_sched_scan(wpa_s);
3096 wpa_supplicant_cancel_scan(wpa_s);
3097
3098 /*
3099 * TODO: If necessary write our own group_remove function,
3100 * for now we can reuse deauthenticate
3101 */
3102 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
3103
5b78493f
MH
3104 if (wpa_s->mesh_if_created)
3105 wpa_supplicant_remove_iface(global, wpa_s, 0);
3106
603a3f34
JL
3107 return 0;
3108}
3109
e174ef34
MH
3110
3111static int wpa_supplicant_ctrl_iface_mesh_peer_remove(
3112 struct wpa_supplicant *wpa_s, char *cmd)
3113{
3114 u8 addr[ETH_ALEN];
3115
3116 if (hwaddr_aton(cmd, addr) < 0)
3117 return -1;
3118
3119 return wpas_mesh_peer_remove(wpa_s, addr);
3120}
3121
2604edbf
MH
3122
3123static int wpa_supplicant_ctrl_iface_mesh_peer_add(
3124 struct wpa_supplicant *wpa_s, char *cmd)
3125{
3126 u8 addr[ETH_ALEN];
9f2cf23e
MH
3127 int duration;
3128 char *pos;
3129
3130 pos = os_strstr(cmd, " duration=");
3131 if (pos) {
3132 *pos = '\0';
3133 duration = atoi(pos + 10);
3134 } else {
3135 duration = -1;
3136 }
2604edbf
MH
3137
3138 if (hwaddr_aton(cmd, addr))
3139 return -1;
3140
9f2cf23e 3141 return wpas_mesh_peer_add(wpa_s, addr, duration);
2604edbf
MH
3142}
3143
7ace4328
PKC
3144
3145static int wpa_supplicant_ctrl_iface_mesh_link_probe(
3146 struct wpa_supplicant *wpa_s, char *cmd)
3147{
3148 struct ether_header *eth;
3149 u8 addr[ETH_ALEN];
3150 u8 *buf;
3151 char *pos;
3152 size_t payload_len = 0, len;
3153 int ret = -1;
3154
3155 if (hwaddr_aton(cmd, addr))
3156 return -1;
3157
3158 pos = os_strstr(cmd, " payload=");
3159 if (pos) {
3160 pos = pos + 9;
3161 payload_len = os_strlen(pos);
3162 if (payload_len & 1)
3163 return -1;
3164
3165 payload_len /= 2;
3166 }
3167
3168 len = ETH_HLEN + payload_len;
3169 buf = os_malloc(len);
3170 if (!buf)
3171 return -1;
3172
3173 eth = (struct ether_header *) buf;
3174 os_memcpy(eth->ether_dhost, addr, ETH_ALEN);
3175 os_memcpy(eth->ether_shost, wpa_s->own_addr, ETH_ALEN);
3176 eth->ether_type = htons(ETH_P_802_3);
3177
3178 if (payload_len && hexstr2bin(pos, buf + ETH_HLEN, payload_len) < 0)
3179 goto fail;
3180
3181 ret = wpa_drv_mesh_link_probe(wpa_s, addr, buf, len);
3182fail:
3183 os_free(buf);
3184 return -ret;
3185}
3186
603a3f34
JL
3187#endif /* CONFIG_MESH */
3188
3189
6fc6879b
JM
3190static int wpa_supplicant_ctrl_iface_select_network(
3191 struct wpa_supplicant *wpa_s, char *cmd)
3192{
3193 int id;
3194 struct wpa_ssid *ssid;
204c9ac4 3195 char *pos;
6fc6879b
JM
3196
3197 /* cmd: "<network id>" or "any" */
204c9ac4 3198 if (os_strncmp(cmd, "any", 3) == 0) {
6fc6879b 3199 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
86b89452
WS
3200 ssid = NULL;
3201 } else {
3202 id = atoi(cmd);
3203 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
6fc6879b 3204
86b89452
WS
3205 ssid = wpa_config_get_network(wpa_s->conf, id);
3206 if (ssid == NULL) {
3207 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3208 "network id=%d", id);
3209 return -1;
3210 }
4dac0245
JM
3211 if (ssid->disabled == 2) {
3212 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
3213 "SELECT_NETWORK with persistent P2P group");
3214 return -1;
3215 }
6fc6879b
JM
3216 }
3217
204c9ac4
DS
3218 pos = os_strstr(cmd, " freq=");
3219 if (pos) {
3220 int *freqs = freq_range_to_channel_list(wpa_s, pos + 6);
3221 if (freqs) {
88a44755
JM
3222 os_free(wpa_s->select_network_scan_freqs);
3223 wpa_s->select_network_scan_freqs = freqs;
204c9ac4
DS
3224 }
3225 }
3226
dfaf11d6
JM
3227 wpa_s->scan_min_time.sec = 0;
3228 wpa_s->scan_min_time.usec = 0;
86b89452 3229 wpa_supplicant_select_network(wpa_s, ssid);
6fc6879b
JM
3230
3231 return 0;
3232}
3233
3234
3235static int wpa_supplicant_ctrl_iface_enable_network(
3236 struct wpa_supplicant *wpa_s, char *cmd)
3237{
3238 int id;
3239 struct wpa_ssid *ssid;
3240
3241 /* cmd: "<network id>" or "all" */
3242 if (os_strcmp(cmd, "all") == 0) {
3243 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
86b89452
WS
3244 ssid = NULL;
3245 } else {
3246 id = atoi(cmd);
3247 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
6fc6879b 3248
86b89452
WS
3249 ssid = wpa_config_get_network(wpa_s->conf, id);
3250 if (ssid == NULL) {
3251 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3252 "network id=%d", id);
3253 return -1;
3254 }
4dac0245
JM
3255 if (ssid->disabled == 2) {
3256 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
3257 "ENABLE_NETWORK with persistent P2P group");
3258 return -1;
3259 }
84c78f95
JM
3260
3261 if (os_strstr(cmd, " no-connect")) {
3262 ssid->disabled = 0;
3263 return 0;
3264 }
6fc6879b 3265 }
dfaf11d6
JM
3266 wpa_s->scan_min_time.sec = 0;
3267 wpa_s->scan_min_time.usec = 0;
86b89452 3268 wpa_supplicant_enable_network(wpa_s, ssid);
6fc6879b
JM
3269
3270 return 0;
3271}
3272
3273
3274static int wpa_supplicant_ctrl_iface_disable_network(
3275 struct wpa_supplicant *wpa_s, char *cmd)
3276{
3277 int id;
3278 struct wpa_ssid *ssid;
3279
3280 /* cmd: "<network id>" or "all" */
3281 if (os_strcmp(cmd, "all") == 0) {
3282 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
86b89452
WS
3283 ssid = NULL;
3284 } else {
3285 id = atoi(cmd);
3286 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
6fc6879b 3287
86b89452
WS
3288 ssid = wpa_config_get_network(wpa_s->conf, id);
3289 if (ssid == NULL) {
3290 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3291 "network id=%d", id);
3292 return -1;
3293 }
4dac0245
JM
3294 if (ssid->disabled == 2) {
3295 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
3296 "DISABLE_NETWORK with persistent P2P "
3297 "group");
3298 return -1;
3299 }
6fc6879b 3300 }
86b89452 3301 wpa_supplicant_disable_network(wpa_s, ssid);
6fc6879b
JM
3302
3303 return 0;
3304}
3305
3306
3307static int wpa_supplicant_ctrl_iface_add_network(
3308 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
3309{
3310 struct wpa_ssid *ssid;
3311 int ret;
3312
3313 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
3314
d015bb05 3315 ssid = wpa_supplicant_add_network(wpa_s);
6fc6879b
JM
3316 if (ssid == NULL)
3317 return -1;
8bac466b 3318
6fc6879b 3319 ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
d85e1fc8 3320 if (os_snprintf_error(buflen, ret))
6fc6879b
JM
3321 return -1;
3322 return ret;
3323}
3324
3325
3326static int wpa_supplicant_ctrl_iface_remove_network(
3327 struct wpa_supplicant *wpa_s, char *cmd)
3328{
3329 int id;
3330 struct wpa_ssid *ssid;
d015bb05 3331 int result;
6fc6879b
JM
3332
3333 /* cmd: "<network id>" or "all" */
3334 if (os_strcmp(cmd, "all") == 0) {
3335 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
725fc39e
DS
3336 if (wpa_s->sched_scanning)
3337 wpa_supplicant_cancel_sched_scan(wpa_s);
3338
d8a790b9 3339 eapol_sm_invalidate_cached_session(wpa_s->eapol);
6fc6879b 3340 if (wpa_s->current_ssid) {
83df8149
JM
3341#ifdef CONFIG_SME
3342 wpa_s->sme.prev_bssid_set = 0;
3343#endif /* CONFIG_SME */
20a0b03d
JM
3344 wpa_sm_set_config(wpa_s->wpa, NULL);
3345 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
e66bcedd
JM
3346 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3347 wpa_s->own_disconnect_req = 1;
07783eaa
JM
3348 wpa_supplicant_deauthenticate(
3349 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
6fc6879b 3350 }
391f4925
JK
3351 ssid = wpa_s->conf->ssid;
3352 while (ssid) {
3353 struct wpa_ssid *remove_ssid = ssid;
3354 id = ssid->id;
3355 ssid = ssid->next;
c267753b
JM
3356 if (wpa_s->last_ssid == remove_ssid)
3357 wpa_s->last_ssid = NULL;
391f4925
JK
3358 wpas_notify_network_removed(wpa_s, remove_ssid);
3359 wpa_config_remove_network(wpa_s->conf, id);
3360 }
6fc6879b
JM
3361 return 0;
3362 }
3363
3364 id = atoi(cmd);
3365 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
3366
d015bb05
RP
3367 result = wpa_supplicant_remove_network(wpa_s, id);
3368 if (result == -1) {
6fc6879b
JM
3369 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
3370 "id=%d", id);
3371 return -1;
3372 }
d015bb05 3373 if (result == -2) {
59ff6653
DG
3374 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Not able to remove the "
3375 "network id=%d", id);
3376 return -1;
3377 }
6fc6879b
JM
3378 return 0;
3379}
3380
3381
1c330a2f
DS
3382static int wpa_supplicant_ctrl_iface_update_network(
3383 struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
3384 char *name, char *value)
3385{
5cd317d3
BKB
3386 int ret;
3387
3388 ret = wpa_config_set(ssid, name, value, 0);
3389 if (ret < 0) {
1c330a2f
DS
3390 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
3391 "variable '%s'", name);
3392 return -1;
3393 }
5cd317d3
BKB
3394 if (ret == 1)
3395 return 0; /* No change to the previously configured value */
1c330a2f
DS
3396
3397 if (os_strcmp(name, "bssid") != 0 &&
34ee12c5 3398 os_strcmp(name, "bssid_hint") != 0 &&
c3dc68e8 3399 os_strcmp(name, "priority") != 0) {
1c330a2f
DS
3400 wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
3401
c3dc68e8
JM
3402 if (wpa_s->current_ssid == ssid ||
3403 wpa_s->current_ssid == NULL) {
3404 /*
3405 * Invalidate the EAP session cache if anything in the
3406 * current or previously used configuration changes.
3407 */
3408 eapol_sm_invalidate_cached_session(wpa_s->eapol);
3409 }
1c330a2f
DS
3410 }
3411
3412 if ((os_strcmp(name, "psk") == 0 &&
3413 value[0] == '"' && ssid->ssid_len) ||
3414 (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
3415 wpa_config_update_psk(ssid);
3416 else if (os_strcmp(name, "priority") == 0)
3417 wpa_config_update_prio_list(wpa_s->conf);
3418
3419 return 0;
3420}
3421
3422
6fc6879b
JM
3423static int wpa_supplicant_ctrl_iface_set_network(
3424 struct wpa_supplicant *wpa_s, char *cmd)
3425{
1e529832 3426 int id, ret, prev_bssid_set, prev_disabled;
6fc6879b
JM
3427 struct wpa_ssid *ssid;
3428 char *name, *value;
0ef023e4 3429 u8 prev_bssid[ETH_ALEN];
6fc6879b
JM
3430
3431 /* cmd: "<network id> <variable name> <value>" */
3432 name = os_strchr(cmd, ' ');
3433 if (name == NULL)
3434 return -1;
3435 *name++ = '\0';
3436
3437 value = os_strchr(name, ' ');
3438 if (value == NULL)
3439 return -1;
3440 *value++ = '\0';
3441
3442 id = atoi(cmd);
3443 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
3444 id, name);
3445 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
3446 (u8 *) value, os_strlen(value));
3447
3448 ssid = wpa_config_get_network(wpa_s->conf, id);
3449 if (ssid == NULL) {
3450 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
3451 "id=%d", id);
3452 return -1;
3453 }
3454
0ef023e4 3455 prev_bssid_set = ssid->bssid_set;
1e529832 3456 prev_disabled = ssid->disabled;
0ef023e4
JM
3457 os_memcpy(prev_bssid, ssid->bssid, ETH_ALEN);
3458 ret = wpa_supplicant_ctrl_iface_update_network(wpa_s, ssid, name,
3459 value);
3460 if (ret == 0 &&
3461 (ssid->bssid_set != prev_bssid_set ||
3462 os_memcmp(ssid->bssid, prev_bssid, ETH_ALEN) != 0))
3463 wpas_notify_network_bssid_set_changed(wpa_s, ssid);
1e529832
JM
3464
3465 if (prev_disabled != ssid->disabled &&
3466 (prev_disabled == 2 || ssid->disabled == 2))
3467 wpas_notify_network_type_changed(wpa_s, ssid);
3468
0ef023e4 3469 return ret;
6fc6879b
JM
3470}
3471
3472
3473static int wpa_supplicant_ctrl_iface_get_network(
3474 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
3475{
3476 int id;
3477 size_t res;
3478 struct wpa_ssid *ssid;
3479 char *name, *value;
3480
3481 /* cmd: "<network id> <variable name>" */
3482 name = os_strchr(cmd, ' ');
3483 if (name == NULL || buflen == 0)
3484 return -1;
3485 *name++ = '\0';
3486
3487 id = atoi(cmd);
8db9a79d 3488 wpa_printf(MSG_EXCESSIVE, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
6fc6879b
JM
3489 id, name);
3490
3491 ssid = wpa_config_get_network(wpa_s->conf, id);
3492 if (ssid == NULL) {
8db9a79d 3493 wpa_printf(MSG_EXCESSIVE, "CTRL_IFACE: Could not find network "
6fc6879b
JM
3494 "id=%d", id);
3495 return -1;
3496 }
3497
3498 value = wpa_config_get_no_key(ssid, name);
3499 if (value == NULL) {
8db9a79d 3500 wpa_printf(MSG_EXCESSIVE, "CTRL_IFACE: Failed to get network "
6fc6879b
JM
3501 "variable '%s'", name);
3502 return -1;
3503 }
3504
3505 res = os_strlcpy(buf, value, buflen);
3506 if (res >= buflen) {
3507 os_free(value);
3508 return -1;
3509 }
3510
3511 os_free(value);
3512
3513 return res;
3514}
3515
3516
1c330a2f 3517static int wpa_supplicant_ctrl_iface_dup_network(
daae4995
AN
3518 struct wpa_supplicant *wpa_s, char *cmd,
3519 struct wpa_supplicant *dst_wpa_s)
1c330a2f
DS
3520{
3521 struct wpa_ssid *ssid_s, *ssid_d;
3522 char *name, *id, *value;
3523 int id_s, id_d, ret;
3524
3525 /* cmd: "<src network id> <dst network id> <variable name>" */
3526 id = os_strchr(cmd, ' ');
3527 if (id == NULL)
3528 return -1;
3529 *id++ = '\0';
3530
3531 name = os_strchr(id, ' ');
3532 if (name == NULL)
3533 return -1;
3534 *name++ = '\0';
3535
3536 id_s = atoi(cmd);
3537 id_d = atoi(id);
daae4995
AN
3538
3539 wpa_printf(MSG_DEBUG,
3540 "CTRL_IFACE: DUP_NETWORK ifname=%s->%s id=%d->%d name='%s'",
3541 wpa_s->ifname, dst_wpa_s->ifname, id_s, id_d, name);
1c330a2f
DS
3542
3543 ssid_s = wpa_config_get_network(wpa_s->conf, id_s);
3544 if (ssid_s == NULL) {
3545 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3546 "network id=%d", id_s);
3547 return -1;
3548 }
3549
daae4995 3550 ssid_d = wpa_config_get_network(dst_wpa_s->conf, id_d);
1c330a2f
DS
3551 if (ssid_d == NULL) {
3552 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
c0541906 3553 "network id=%d", id_d);
1c330a2f
DS
3554 return -1;
3555 }
3556
3557 value = wpa_config_get(ssid_s, name);
3558 if (value == NULL) {
3559 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
3560 "variable '%s'", name);
3561 return -1;
3562 }
3563
daae4995 3564 ret = wpa_supplicant_ctrl_iface_update_network(dst_wpa_s, ssid_d, name,
1c330a2f
DS
3565 value);
3566
3567 os_free(value);
3568
3569 return ret;
3570}
3571
3572
d94c9ee6
JM
3573static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
3574 char *buf, size_t buflen)
3575{
3576 char *pos, *end;
3577 struct wpa_cred *cred;
3578 int ret;
3579
3580 pos = buf;
3581 end = buf + buflen;
3582 ret = os_snprintf(pos, end - pos,
3583 "cred id / realm / username / domain / imsi\n");
d85e1fc8 3584 if (os_snprintf_error(end - pos, ret))
d94c9ee6
JM
3585 return pos - buf;
3586 pos += ret;
3587
3588 cred = wpa_s->conf->cred;
3589 while (cred) {
3590 ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
3591 cred->id, cred->realm ? cred->realm : "",
3592 cred->username ? cred->username : "",
463c8ffb 3593 cred->domain ? cred->domain[0] : "",
d94c9ee6 3594 cred->imsi ? cred->imsi : "");
d85e1fc8 3595 if (os_snprintf_error(end - pos, ret))
d94c9ee6
JM
3596 return pos - buf;
3597 pos += ret;
3598
3599 cred = cred->next;
3600 }
3601
3602 return pos - buf;
3603}
3604
3605
3606static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
3607 char *buf, size_t buflen)
3608{
3609 struct wpa_cred *cred;
3610 int ret;
3611
3612 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
3613
3614 cred = wpa_config_add_cred(wpa_s->conf);
3615 if (cred == NULL)
3616 return -1;
3617
1619e9d5
JM
3618 wpa_msg(wpa_s, MSG_INFO, CRED_ADDED "%d", cred->id);
3619
d94c9ee6 3620 ret = os_snprintf(buf, buflen, "%d\n", cred->id);
d85e1fc8 3621 if (os_snprintf_error(buflen, ret))
d94c9ee6
JM
3622 return -1;
3623 return ret;
3624}
3625
3626
736d4f2d
JM
3627static int wpas_ctrl_remove_cred(struct wpa_supplicant *wpa_s,
3628 struct wpa_cred *cred)
3629{
3630 struct wpa_ssid *ssid;
3631 char str[20];
1619e9d5 3632 int id;
736d4f2d 3633
1619e9d5 3634 if (cred == NULL) {
736d4f2d
JM
3635 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
3636 return -1;
3637 }
3638
1619e9d5
JM
3639 id = cred->id;
3640 if (wpa_config_remove_cred(wpa_s->conf, id) < 0) {
3641 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
3642 return -1;
3643 }
3644
3645 wpa_msg(wpa_s, MSG_INFO, CRED_REMOVED "%d", id);
3646
736d4f2d
JM
3647 /* Remove any network entry created based on the removed credential */
3648 ssid = wpa_s->conf->ssid;
3649 while (ssid) {
3650 if (ssid->parent_cred == cred) {
1d399771
JM
3651 int res;
3652
736d4f2d
JM
3653 wpa_printf(MSG_DEBUG, "Remove network id %d since it "
3654 "used the removed credential", ssid->id);
1d399771
JM
3655 res = os_snprintf(str, sizeof(str), "%d", ssid->id);
3656 if (os_snprintf_error(sizeof(str), res))
3657 str[sizeof(str) - 1] = '\0';
736d4f2d
JM
3658 ssid = ssid->next;
3659 wpa_supplicant_ctrl_iface_remove_network(wpa_s, str);
3660 } else
3661 ssid = ssid->next;
3662 }
3663
3664 return 0;
3665}
3666
3667
d94c9ee6
JM
3668static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
3669 char *cmd)
3670{
3671 int id;
736d4f2d 3672 struct wpa_cred *cred, *prev;
d94c9ee6 3673
aa26ba68
JM
3674 /* cmd: "<cred id>", "all", "sp_fqdn=<FQDN>", or
3675 * "provisioning_sp=<FQDN> */
d94c9ee6
JM
3676 if (os_strcmp(cmd, "all") == 0) {
3677 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
3678 cred = wpa_s->conf->cred;
3679 while (cred) {
736d4f2d 3680 prev = cred;
d94c9ee6 3681 cred = cred->next;
736d4f2d 3682 wpas_ctrl_remove_cred(wpa_s, prev);
d94c9ee6
JM
3683 }
3684 return 0;
3685 }
3686
9afe52eb
JM
3687 if (os_strncmp(cmd, "sp_fqdn=", 8) == 0) {
3688 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED SP FQDN '%s'",
3689 cmd + 8);
3690 cred = wpa_s->conf->cred;
3691 while (cred) {
3692 prev = cred;
3693 cred = cred->next;
463c8ffb
JM
3694 if (prev->domain) {
3695 size_t i;
3696 for (i = 0; i < prev->num_domain; i++) {
3697 if (os_strcmp(prev->domain[i], cmd + 8)
3698 != 0)
3699 continue;
3700 wpas_ctrl_remove_cred(wpa_s, prev);
3701 break;
3702 }
3703 }
9afe52eb
JM
3704 }
3705 return 0;
3706 }
3707
aa26ba68
JM
3708 if (os_strncmp(cmd, "provisioning_sp=", 16) == 0) {
3709 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED provisioning SP FQDN '%s'",
3710 cmd + 16);
3711 cred = wpa_s->conf->cred;
3712 while (cred) {
3713 prev = cred;
3714 cred = cred->next;
3715 if (prev->provisioning_sp &&
3716 os_strcmp(prev->provisioning_sp, cmd + 16) == 0)
3717 wpas_ctrl_remove_cred(wpa_s, prev);
3718 }
3719 return 0;
3720 }
3721
d94c9ee6
JM
3722 id = atoi(cmd);
3723 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
3724
3725 cred = wpa_config_get_cred(wpa_s->conf, id);
736d4f2d 3726 return wpas_ctrl_remove_cred(wpa_s, cred);
d94c9ee6
JM
3727}
3728
3729
3730static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
3731 char *cmd)
3732{
3733 int id;
3734 struct wpa_cred *cred;
3735 char *name, *value;
3736
3737 /* cmd: "<cred id> <variable name> <value>" */
3738 name = os_strchr(cmd, ' ');
3739 if (name == NULL)
3740 return -1;
3741 *name++ = '\0';
3742
3743 value = os_strchr(name, ' ');
3744 if (value == NULL)
3745 return -1;
3746 *value++ = '\0';
3747
3748 id = atoi(cmd);
3749 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
3750 id, name);
3751 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
3752 (u8 *) value, os_strlen(value));
3753
3754 cred = wpa_config_get_cred(wpa_s->conf, id);
3755 if (cred == NULL) {
3756 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
3757 id);
3758 return -1;
3759 }
3760
3761 if (wpa_config_set_cred(cred, name, value, 0) < 0) {
3762 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
3763 "variable '%s'", name);
3764 return -1;
3765 }
3766
1619e9d5
JM
3767 wpa_msg(wpa_s, MSG_INFO, CRED_MODIFIED "%d %s", cred->id, name);
3768
d94c9ee6
JM
3769 return 0;
3770}
3771
3772
c880ab87
JM
3773static int wpa_supplicant_ctrl_iface_get_cred(struct wpa_supplicant *wpa_s,
3774 char *cmd, char *buf,
3775 size_t buflen)
3776{
3777 int id;
3778 size_t res;
3779 struct wpa_cred *cred;
3780 char *name, *value;
3781
3782 /* cmd: "<cred id> <variable name>" */
3783 name = os_strchr(cmd, ' ');
3784 if (name == NULL)
3785 return -1;
3786 *name++ = '\0';
3787
3788 id = atoi(cmd);
3789 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CRED id=%d name='%s'",
3790 id, name);
3791
3792 cred = wpa_config_get_cred(wpa_s->conf, id);
3793 if (cred == NULL) {
3794 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
3795 id);
3796 return -1;
3797 }
3798
3799 value = wpa_config_get_cred_no_key(cred, name);
3800 if (value == NULL) {
3801 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get cred variable '%s'",
3802 name);
3803 return -1;
3804 }
3805
3806 res = os_strlcpy(buf, value, buflen);
3807 if (res >= buflen) {
3808 os_free(value);
3809 return -1;
3810 }
3811
3812 os_free(value);
3813
3814 return res;
3815}
3816
3817
6fc6879b
JM
3818#ifndef CONFIG_NO_CONFIG_WRITE
3819static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
3820{
3821 int ret;
3822
3823 if (!wpa_s->conf->update_config) {
3824 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
3825 "to update configuration (update_config=0)");
3826 return -1;
3827 }
3828
3829 ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
3830 if (ret) {
3831 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
3832 "update configuration");
3833 } else {
3834 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
3835 " updated");
3836 }
3837
3838 return ret;
3839}
3840#endif /* CONFIG_NO_CONFIG_WRITE */
3841
3842
4daa011b
JM
3843struct cipher_info {
3844 unsigned int capa;
3845 const char *name;
3846 int group_only;
3847};
3848
3849static const struct cipher_info ciphers[] = {
3850 { WPA_DRIVER_CAPA_ENC_CCMP_256, "CCMP-256", 0 },
3851 { WPA_DRIVER_CAPA_ENC_GCMP_256, "GCMP-256", 0 },
3852 { WPA_DRIVER_CAPA_ENC_CCMP, "CCMP", 0 },
3853 { WPA_DRIVER_CAPA_ENC_GCMP, "GCMP", 0 },
3854 { WPA_DRIVER_CAPA_ENC_TKIP, "TKIP", 0 },
3855 { WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE, "NONE", 0 },
3856 { WPA_DRIVER_CAPA_ENC_WEP104, "WEP104", 1 },
3857 { WPA_DRIVER_CAPA_ENC_WEP40, "WEP40", 1 }
3858};
3859
b5f045de
JM
3860static const struct cipher_info ciphers_group_mgmt[] = {
3861 { WPA_DRIVER_CAPA_ENC_BIP, "AES-128-CMAC", 1 },
3862 { WPA_DRIVER_CAPA_ENC_BIP_GMAC_128, "BIP-GMAC-128", 1 },
3863 { WPA_DRIVER_CAPA_ENC_BIP_GMAC_256, "BIP-GMAC-256", 1 },
3864 { WPA_DRIVER_CAPA_ENC_BIP_CMAC_256, "BIP-CMAC-256", 1 },
3865};
3866
4daa011b 3867
6fc6879b
JM
3868static int ctrl_iface_get_capability_pairwise(int res, char *strict,
3869 struct wpa_driver_capa *capa,
3870 char *buf, size_t buflen)
3871{
ea3b8c1d 3872 int ret;
6fc6879b
JM
3873 char *pos, *end;
3874 size_t len;
4daa011b 3875 unsigned int i;
6fc6879b
JM
3876
3877 pos = buf;
3878 end = pos + buflen;
3879
3880 if (res < 0) {
3881 if (strict)
3882 return 0;
3883 len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
3884 if (len >= buflen)
3885 return -1;
3886 return len;
3887 }
3888
4daa011b
JM
3889 for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
3890 if (!ciphers[i].group_only && capa->enc & ciphers[i].capa) {
3891 ret = os_snprintf(pos, end - pos, "%s%s",
ea3b8c1d
JM
3892 pos == buf ? "" : " ",
3893 ciphers[i].name);
d85e1fc8 3894 if (os_snprintf_error(end - pos, ret))
4daa011b
JM
3895 return pos - buf;
3896 pos += ret;
4daa011b 3897 }
6fc6879b
JM
3898 }
3899
3900 return pos - buf;
3901}
3902
3903
3904static int ctrl_iface_get_capability_group(int res, char *strict,
3905 struct wpa_driver_capa *capa,
3906 char *buf, size_t buflen)
3907{
ea3b8c1d 3908 int ret;
6fc6879b
JM
3909 char *pos, *end;
3910 size_t len;
4daa011b 3911 unsigned int i;
6fc6879b
JM
3912
3913 pos = buf;
3914 end = pos + buflen;
3915
3916 if (res < 0) {
3917 if (strict)
3918 return 0;
3919 len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
3920 if (len >= buflen)
3921 return -1;
3922 return len;
3923 }
3924
4daa011b
JM
3925 for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
3926 if (capa->enc & ciphers[i].capa) {
3927 ret = os_snprintf(pos, end - pos, "%s%s",
ea3b8c1d
JM
3928 pos == buf ? "" : " ",
3929 ciphers[i].name);
d85e1fc8 3930 if (os_snprintf_error(end - pos, ret))
4daa011b
JM
3931 return pos - buf;
3932 pos += ret;
4daa011b 3933 }
6fc6879b
JM
3934 }
3935
3936 return pos - buf;
3937}
3938
3939
b5f045de
JM
3940static int ctrl_iface_get_capability_group_mgmt(int res, char *strict,
3941 struct wpa_driver_capa *capa,
3942 char *buf, size_t buflen)
3943{
3944 int ret;
3945 char *pos, *end;
3946 unsigned int i;
3947
3948 pos = buf;
3949 end = pos + buflen;
3950
3951 if (res < 0)
3952 return 0;
3953
3954 for (i = 0; i < ARRAY_SIZE(ciphers_group_mgmt); i++) {
3955 if (capa->enc & ciphers_group_mgmt[i].capa) {
3956 ret = os_snprintf(pos, end - pos, "%s%s",
3957 pos == buf ? "" : " ",
3958 ciphers_group_mgmt[i].name);
3959 if (os_snprintf_error(end - pos, ret))
3960 return pos - buf;
3961 pos += ret;
3962 }
3963 }
3964
3965 return pos - buf;
3966}
3967
3968
6fc6879b
JM
3969static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
3970 struct wpa_driver_capa *capa,
3971 char *buf, size_t buflen)
3972{
3973 int ret;
3974 char *pos, *end;
3975 size_t len;
3976
3977 pos = buf;
3978 end = pos + buflen;
3979
3980 if (res < 0) {
3981 if (strict)
3982 return 0;
3983 len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
3984 "NONE", buflen);
3985 if (len >= buflen)
3986 return -1;
3987 return len;
3988 }
3989
3990 ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
d85e1fc8 3991 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
3992 return pos - buf;
3993 pos += ret;
3994
3995 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
3996 WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
3997 ret = os_snprintf(pos, end - pos, " WPA-EAP");
d85e1fc8 3998 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
3999 return pos - buf;
4000 pos += ret;
4001 }
4002
4003 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
4004 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
4005 ret = os_snprintf(pos, end - pos, " WPA-PSK");
d85e1fc8 4006 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
4007 return pos - buf;
4008 pos += ret;
4009 }
4010
4011 if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
4012 ret = os_snprintf(pos, end - pos, " WPA-NONE");
d85e1fc8 4013 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
4014 return pos - buf;
4015 pos += ret;
4016 }
4017
399e6135
JM
4018#ifdef CONFIG_SUITEB
4019 if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SUITE_B) {
4020 ret = os_snprintf(pos, end - pos, " WPA-EAP-SUITE-B");
4021 if (os_snprintf_error(end - pos, ret))
4022 return pos - buf;
4023 pos += ret;
4024 }
4025#endif /* CONFIG_SUITEB */
4026#ifdef CONFIG_SUITEB192
4027 if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SUITE_B_192) {
4028 ret = os_snprintf(pos, end - pos, " WPA-EAP-SUITE-B-192");
4029 if (os_snprintf_error(end - pos, ret))
4030 return pos - buf;
4031 pos += ret;
4032 }
4033#endif /* CONFIG_SUITEB192 */
f9561868
JM
4034#ifdef CONFIG_OWE
4035 if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_OWE) {
4036 ret = os_snprintf(pos, end - pos, " OWE");
4037 if (os_snprintf_error(end - pos, ret))
4038 return pos - buf;
4039 pos += ret;
4040 }
4041#endif /* CONFIG_OWE */
567da5bb
JM
4042#ifdef CONFIG_DPP
4043 if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_DPP) {
4044 ret = os_snprintf(pos, end - pos, " DPP");
4045 if (os_snprintf_error(end - pos, ret))
4046 return pos - buf;
4047 pos += ret;
4048 }
4049#endif /* CONFIG_DPP */
fe3e0bac
VK
4050#ifdef CONFIG_FILS
4051 if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FILS_SHA256) {
4052 ret = os_snprintf(pos, end - pos, " FILS-SHA256");
4053 if (os_snprintf_error(end - pos, ret))
4054 return pos - buf;
4055 pos += ret;
4056 }
4057 if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FILS_SHA384) {
4058 ret = os_snprintf(pos, end - pos, " FILS-SHA384");
4059 if (os_snprintf_error(end - pos, ret))
4060 return pos - buf;
4061 pos += ret;
4062 }
4063#ifdef CONFIG_IEEE80211R
4064 if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_FILS_SHA256) {
4065 ret = os_snprintf(pos, end - pos, " FT-FILS-SHA256");
4066 if (os_snprintf_error(end - pos, ret))
4067 return pos - buf;
4068 pos += ret;
4069 }
4070 if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_FILS_SHA384) {
4071 ret = os_snprintf(pos, end - pos, " FT-FILS-SHA384");
4072 if (os_snprintf_error(end - pos, ret))
4073 return pos - buf;
4074 pos += ret;
4075 }
4076#endif /* CONFIG_IEEE80211R */
4077#endif /* CONFIG_FILS */
73f285da
MH
4078#ifdef CONFIG_IEEE80211R
4079 if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK) {
4080 ret = os_snprintf(pos, end - pos, " FT-PSK");
4081 if (os_snprintf_error(end - pos, ret))
4082 return pos - buf;
4083 pos += ret;
4084 }
4085#endif /* CONFIG_IEEE80211R */
0fb292c0
JM
4086#ifdef CONFIG_SAE
4087 if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SAE) {
4088 ret = os_snprintf(pos, end - pos, " SAE");
4089 if (os_snprintf_error(end - pos, ret))
4090 return pos - buf;
4091 pos += ret;
4092 }
4093#endif /* CONFIG_SAE */
399e6135 4094
6fc6879b
JM
4095 return pos - buf;
4096}
4097
4098
4099static int ctrl_iface_get_capability_proto(int res, char *strict,
4100 struct wpa_driver_capa *capa,
4101 char *buf, size_t buflen)
4102{
ea3b8c1d 4103 int ret;
6fc6879b
JM
4104 char *pos, *end;
4105 size_t len;
4106
4107 pos = buf;
4108 end = pos + buflen;
4109
4110 if (res < 0) {
4111 if (strict)
4112 return 0;
4113 len = os_strlcpy(buf, "RSN WPA", buflen);
4114 if (len >= buflen)
4115 return -1;
4116 return len;
4117 }
4118
4119 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
4120 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
ea3b8c1d
JM
4121 ret = os_snprintf(pos, end - pos, "%sRSN",
4122 pos == buf ? "" : " ");
d85e1fc8 4123 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
4124 return pos - buf;
4125 pos += ret;
6fc6879b
JM
4126 }
4127
4128 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
4129 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
ea3b8c1d
JM
4130 ret = os_snprintf(pos, end - pos, "%sWPA",
4131 pos == buf ? "" : " ");
d85e1fc8 4132 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
4133 return pos - buf;
4134 pos += ret;
6fc6879b
JM
4135 }
4136
4137 return pos - buf;
4138}
4139
4140
db5adfe7
JM
4141static int ctrl_iface_get_capability_auth_alg(struct wpa_supplicant *wpa_s,
4142 int res, char *strict,
6fc6879b
JM
4143 struct wpa_driver_capa *capa,
4144 char *buf, size_t buflen)
4145{
ea3b8c1d 4146 int ret;
6fc6879b
JM
4147 char *pos, *end;
4148 size_t len;
4149
4150 pos = buf;
4151 end = pos + buflen;
4152
4153 if (res < 0) {
4154 if (strict)
4155 return 0;
4156 len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
4157 if (len >= buflen)
4158 return -1;
4159 return len;
4160 }
4161
4162 if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
ea3b8c1d
JM
4163 ret = os_snprintf(pos, end - pos, "%sOPEN",
4164 pos == buf ? "" : " ");
d85e1fc8 4165 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
4166 return pos - buf;
4167 pos += ret;
6fc6879b
JM
4168 }
4169
4170 if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
4171 ret = os_snprintf(pos, end - pos, "%sSHARED",
ea3b8c1d 4172 pos == buf ? "" : " ");
d85e1fc8 4173 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
4174 return pos - buf;
4175 pos += ret;
6fc6879b
JM
4176 }
4177
4178 if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
ea3b8c1d
JM
4179 ret = os_snprintf(pos, end - pos, "%sLEAP",
4180 pos == buf ? "" : " ");
d85e1fc8 4181 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
4182 return pos - buf;
4183 pos += ret;
6fc6879b
JM
4184 }
4185
db5adfe7
JM
4186#ifdef CONFIG_SAE
4187 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAE) {
4188 ret = os_snprintf(pos, end - pos, "%sSAE",
4189 pos == buf ? "" : " ");
4190 if (os_snprintf_error(end - pos, ret))
4191 return pos - buf;
4192 pos += ret;
4193 }
4194#endif /* CONFIG_SAE */
4195
fe3e0bac
VK
4196#ifdef CONFIG_FILS
4197 if (wpa_is_fils_supported(wpa_s)) {
4198 ret = os_snprintf(pos, end - pos, "%sFILS_SK_WITHOUT_PFS",
4199 pos == buf ? "" : " ");
4200 if (os_snprintf_error(end - pos, ret))
4201 return pos - buf;
4202 pos += ret;
4203 }
4204
4205#ifdef CONFIG_FILS_SK_PFS
4206 if (wpa_is_fils_sk_pfs_supported(wpa_s)) {
4207 ret = os_snprintf(pos, end - pos, "%sFILS_SK_WITH_PFS",
4208 pos == buf ? "" : " ");
4209 if (os_snprintf_error(end - pos, ret))
4210 return pos - buf;
4211 pos += ret;
4212 }
4213#endif /* CONFIG_FILS_SK_PFS */
4214#endif /* CONFIG_FILS */
4215
6fc6879b
JM
4216 return pos - buf;
4217}
4218
4219
65d52fc1
BR
4220static int ctrl_iface_get_capability_modes(int res, char *strict,
4221 struct wpa_driver_capa *capa,
4222 char *buf, size_t buflen)
4223{
ea3b8c1d 4224 int ret;
65d52fc1
BR
4225 char *pos, *end;
4226 size_t len;
4227
4228 pos = buf;
4229 end = pos + buflen;
4230
4231 if (res < 0) {
4232 if (strict)
4233 return 0;
4234 len = os_strlcpy(buf, "IBSS AP", buflen);
4235 if (len >= buflen)
4236 return -1;
4237 return len;
4238 }
4239
4240 if (capa->flags & WPA_DRIVER_FLAGS_IBSS) {
ea3b8c1d
JM
4241 ret = os_snprintf(pos, end - pos, "%sIBSS",
4242 pos == buf ? "" : " ");
d85e1fc8 4243 if (os_snprintf_error(end - pos, ret))
65d52fc1
BR
4244 return pos - buf;
4245 pos += ret;
65d52fc1
BR
4246 }
4247
4248 if (capa->flags & WPA_DRIVER_FLAGS_AP) {
ea3b8c1d
JM
4249 ret = os_snprintf(pos, end - pos, "%sAP",
4250 pos == buf ? "" : " ");
d85e1fc8 4251 if (os_snprintf_error(end - pos, ret))
65d52fc1
BR
4252 return pos - buf;
4253 pos += ret;
65d52fc1
BR
4254 }
4255
cf08e9b1
JM
4256#ifdef CONFIG_MESH
4257 if (capa->flags & WPA_DRIVER_FLAGS_MESH) {
4258 ret = os_snprintf(pos, end - pos, "%sMESH",
4259 pos == buf ? "" : " ");
4260 if (os_snprintf_error(end - pos, ret))
4261 return pos - buf;
4262 pos += ret;
4263 }
4264#endif /* CONFIG_MESH */
4265
65d52fc1
BR
4266 return pos - buf;
4267}
4268
4269
35aa088a
DS
4270static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
4271 char *buf, size_t buflen)
4272{
4273 struct hostapd_channel_data *chnl;
4274 int ret, i, j;
4275 char *pos, *end, *hmode;
4276
4277 pos = buf;
4278 end = pos + buflen;
4279
4280 for (j = 0; j < wpa_s->hw.num_modes; j++) {
4281 switch (wpa_s->hw.modes[j].mode) {
4282 case HOSTAPD_MODE_IEEE80211B:
4283 hmode = "B";
4284 break;
4285 case HOSTAPD_MODE_IEEE80211G:
4286 hmode = "G";
4287 break;
4288 case HOSTAPD_MODE_IEEE80211A:
4289 hmode = "A";
4290 break;
7829894c
VK
4291 case HOSTAPD_MODE_IEEE80211AD:
4292 hmode = "AD";
4293 break;
35aa088a
DS
4294 default:
4295 continue;
4296 }
4297 ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
d85e1fc8 4298 if (os_snprintf_error(end - pos, ret))
35aa088a
DS
4299 return pos - buf;
4300 pos += ret;
4301 chnl = wpa_s->hw.modes[j].channels;
4302 for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
4303 if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
4304 continue;
4305 ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
d85e1fc8 4306 if (os_snprintf_error(end - pos, ret))
35aa088a
DS
4307 return pos - buf;
4308 pos += ret;
4309 }
4310 ret = os_snprintf(pos, end - pos, "\n");
d85e1fc8 4311 if (os_snprintf_error(end - pos, ret))
35aa088a
DS
4312 return pos - buf;
4313 pos += ret;
4314 }
4315
4316 return pos - buf;
4317}
4318
4319
06060522
BR
4320static int ctrl_iface_get_capability_freq(struct wpa_supplicant *wpa_s,
4321 char *buf, size_t buflen)
4322{
4323 struct hostapd_channel_data *chnl;
4324 int ret, i, j;
4325 char *pos, *end, *hmode;
4326
4327 pos = buf;
4328 end = pos + buflen;
4329
4330 for (j = 0; j < wpa_s->hw.num_modes; j++) {
4331 switch (wpa_s->hw.modes[j].mode) {
4332 case HOSTAPD_MODE_IEEE80211B:
4333 hmode = "B";
4334 break;
4335 case HOSTAPD_MODE_IEEE80211G:
4336 hmode = "G";
4337 break;
4338 case HOSTAPD_MODE_IEEE80211A:
4339 hmode = "A";
4340 break;
4341 case HOSTAPD_MODE_IEEE80211AD:
4342 hmode = "AD";
4343 break;
4344 default:
4345 continue;
4346 }
4347 ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:\n",
4348 hmode);
d85e1fc8 4349 if (os_snprintf_error(end - pos, ret))
06060522
BR
4350 return pos - buf;
4351 pos += ret;
4352 chnl = wpa_s->hw.modes[j].channels;
4353 for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
4354 if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
4355 continue;
0547124d 4356 ret = os_snprintf(pos, end - pos, " %d = %d MHz%s%s\n",
06060522 4357 chnl[i].chan, chnl[i].freq,
0a443580
IP
4358 chnl[i].flag & HOSTAPD_CHAN_NO_IR ?
4359 " (NO_IR)" : "",
0547124d
DS
4360 chnl[i].flag & HOSTAPD_CHAN_RADAR ?
4361 " (DFS)" : "");
4362
d85e1fc8 4363 if (os_snprintf_error(end - pos, ret))
06060522
BR
4364 return pos - buf;
4365 pos += ret;
4366 }
4367 ret = os_snprintf(pos, end - pos, "\n");
d85e1fc8 4368 if (os_snprintf_error(end - pos, ret))
06060522
BR
4369 return pos - buf;
4370 pos += ret;
4371 }
4372
4373 return pos - buf;
4374}
4375
4376
6fc6879b
JM
4377static int wpa_supplicant_ctrl_iface_get_capability(
4378 struct wpa_supplicant *wpa_s, const char *_field, char *buf,
4379 size_t buflen)
4380{
4381 struct wpa_driver_capa capa;
4382 int res;
4383 char *strict;
4384 char field[30];
4385 size_t len;
4386
4387 /* Determine whether or not strict checking was requested */
4388 len = os_strlcpy(field, _field, sizeof(field));
4389 if (len >= sizeof(field))
4390 return -1;
4391 strict = os_strchr(field, ' ');
4392 if (strict != NULL) {
4393 *strict++ = '\0';
4394 if (os_strcmp(strict, "strict") != 0)
4395 return -1;
4396 }
4397
4398 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
4399 field, strict ? strict : "");
4400
4401 if (os_strcmp(field, "eap") == 0) {
4402 return eap_get_names(buf, buflen);
4403 }
4404
4405 res = wpa_drv_get_capa(wpa_s, &capa);
4406
4407 if (os_strcmp(field, "pairwise") == 0)
4408 return ctrl_iface_get_capability_pairwise(res, strict, &capa,
4409 buf, buflen);
4410
4411 if (os_strcmp(field, "group") == 0)
4412 return ctrl_iface_get_capability_group(res, strict, &capa,
4413 buf, buflen);
4414
b5f045de
JM
4415 if (os_strcmp(field, "group_mgmt") == 0)
4416 return ctrl_iface_get_capability_group_mgmt(res, strict, &capa,
4417 buf, buflen);
4418
6fc6879b
JM
4419 if (os_strcmp(field, "key_mgmt") == 0)
4420 return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
4421 buf, buflen);
4422
4423 if (os_strcmp(field, "proto") == 0)
4424 return ctrl_iface_get_capability_proto(res, strict, &capa,
4425 buf, buflen);
4426
4427 if (os_strcmp(field, "auth_alg") == 0)
db5adfe7
JM
4428 return ctrl_iface_get_capability_auth_alg(wpa_s, res, strict,
4429 &capa, buf, buflen);
6fc6879b 4430
65d52fc1
BR
4431 if (os_strcmp(field, "modes") == 0)
4432 return ctrl_iface_get_capability_modes(res, strict, &capa,
4433 buf, buflen);
4434
35aa088a
DS
4435 if (os_strcmp(field, "channels") == 0)
4436 return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
4437
06060522
BR
4438 if (os_strcmp(field, "freq") == 0)
4439 return ctrl_iface_get_capability_freq(wpa_s, buf, buflen);
4440
6e9375e4
DS
4441#ifdef CONFIG_TDLS
4442 if (os_strcmp(field, "tdls") == 0)
4443 return ctrl_iface_get_capability_tdls(wpa_s, buf, buflen);
4444#endif /* CONFIG_TDLS */
4445
02a8d45a
JM
4446#ifdef CONFIG_ERP
4447 if (os_strcmp(field, "erp") == 0) {
4448 res = os_snprintf(buf, buflen, "ERP");
d85e1fc8 4449 if (os_snprintf_error(buflen, res))
02a8d45a
JM
4450 return -1;
4451 return res;
4452 }
4453#endif /* CONFIG_EPR */
4454
1e4f7bf5
JM
4455#ifdef CONFIG_FIPS
4456 if (os_strcmp(field, "fips") == 0) {
4457 res = os_snprintf(buf, buflen, "FIPS");
4458 if (os_snprintf_error(buflen, res))
4459 return -1;
4460 return res;
4461 }
4462#endif /* CONFIG_FIPS */
4463
7d2f6743
JM
4464#ifdef CONFIG_ACS
4465 if (os_strcmp(field, "acs") == 0) {
4466 res = os_snprintf(buf, buflen, "ACS");
4467 if (os_snprintf_error(buflen, res))
4468 return -1;
4469 return res;
4470 }
4471#endif /* CONFIG_ACS */
4472
379e2b4d 4473#ifdef CONFIG_FILS
fe3e0bac 4474 if (os_strcmp(field, "fils") == 0) {
31e130f8 4475#ifdef CONFIG_FILS_SK_PFS
fe3e0bac
VK
4476 if (wpa_is_fils_supported(wpa_s) &&
4477 wpa_is_fils_sk_pfs_supported(wpa_s)) {
4478 res = os_snprintf(buf, buflen, "FILS FILS-SK-PFS");
4479 if (os_snprintf_error(buflen, res))
4480 return -1;
4481 return res;
4482 }
31e130f8 4483#endif /* CONFIG_FILS_SK_PFS */
fe3e0bac
VK
4484
4485 if (wpa_is_fils_supported(wpa_s)) {
4486 res = os_snprintf(buf, buflen, "FILS");
4487 if (os_snprintf_error(buflen, res))
4488 return -1;
4489 return res;
4490 }
379e2b4d
JM
4491 }
4492#endif /* CONFIG_FILS */
4493
2a93ecc8
JM
4494 if (os_strcmp(field, "multibss") == 0 && wpa_s->multi_bss_support) {
4495 res = os_snprintf(buf, buflen, "MULTIBSS-STA");
4496 if (os_snprintf_error(buflen, res))
4497 return -1;
4498 return res;
4499 }
4500
c98617b4
JM
4501#ifdef CONFIG_DPP
4502 if (os_strcmp(field, "dpp") == 0) {
4503#ifdef CONFIG_DPP2
4504 res = os_snprintf(buf, buflen, "DPP=2");
4505#else /* CONFIG_DPP2 */
4506 res = os_snprintf(buf, buflen, "DPP=1");
4507#endif /* CONFIG_DPP2 */
4508 if (os_snprintf_error(buflen, res))
4509 return -1;
4510 return res;
4511 }
4512#endif /* CONFIG_DPP */
4513
6fc6879b
JM
4514 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
4515 field);
4516
4517 return -1;
4518}
4519
4520
afc064fe
JM
4521#ifdef CONFIG_INTERWORKING
4522static char * anqp_add_hex(char *pos, char *end, const char *title,
4523 struct wpabuf *data)
4524{
4525 char *start = pos;
4526 size_t i;
4527 int ret;
4528 const u8 *d;
4529
4530 if (data == NULL)
4531 return start;
4532
4533 ret = os_snprintf(pos, end - pos, "%s=", title);
d85e1fc8 4534 if (os_snprintf_error(end - pos, ret))
afc064fe
JM
4535 return start;
4536 pos += ret;
4537
4538 d = wpabuf_head_u8(data);
4539 for (i = 0; i < wpabuf_len(data); i++) {
4540 ret = os_snprintf(pos, end - pos, "%02x", *d++);
d85e1fc8 4541 if (os_snprintf_error(end - pos, ret))
afc064fe
JM
4542 return start;
4543 pos += ret;
4544 }
4545
4546 ret = os_snprintf(pos, end - pos, "\n");
d85e1fc8 4547 if (os_snprintf_error(end - pos, ret))
afc064fe
JM
4548 return start;
4549 pos += ret;
4550
4551 return pos;
4552}
4553#endif /* CONFIG_INTERWORKING */
4554
4555
b54f4339
JM
4556#ifdef CONFIG_FILS
4557static int print_fils_indication(struct wpa_bss *bss, char *pos, char *end)
4558{
4559 char *start = pos;
4560 const u8 *ie, *ie_end;
4561 u16 info, realms;
4562 int ret;
4563
4564 ie = wpa_bss_get_ie(bss, WLAN_EID_FILS_INDICATION);
4565 if (!ie)
4566 return 0;
4567 ie_end = ie + 2 + ie[1];
4568 ie += 2;
4569 if (ie_end - ie < 2)
4570 return -1;
4571
4572 info = WPA_GET_LE16(ie);
4573 ie += 2;
4574 ret = os_snprintf(pos, end - pos, "fils_info=%04x\n", info);
4575 if (os_snprintf_error(end - pos, ret))
4576 return 0;
4577 pos += ret;
4578
4579 if (info & BIT(7)) {
4580 /* Cache Identifier Included */
4581 if (ie_end - ie < 2)
4582 return -1;
4583 ret = os_snprintf(pos, end - pos, "fils_cache_id=%02x%02x\n",
4584 ie[0], ie[1]);
4585 if (os_snprintf_error(end - pos, ret))
4586 return 0;
4587 pos += ret;
4588 ie += 2;
4589 }
4590
4591 if (info & BIT(8)) {
4592 /* HESSID Included */
4593 if (ie_end - ie < ETH_ALEN)
4594 return -1;
4595 ret = os_snprintf(pos, end - pos, "fils_hessid=" MACSTR "\n",
4596 MAC2STR(ie));
4597 if (os_snprintf_error(end - pos, ret))
4598 return 0;
4599 pos += ret;
4600 ie += ETH_ALEN;
4601 }
4602
4603 realms = (info & (BIT(3) | BIT(4) | BIT(5))) >> 3;
4604 if (realms) {
4605 if (ie_end - ie < realms * 2)
4606 return -1;
4607 ret = os_snprintf(pos, end - pos, "fils_realms=");
4608 if (os_snprintf_error(end - pos, ret))
4609 return 0;
4610 pos += ret;
4611
4612 ret = wpa_snprintf_hex(pos, end - pos, ie, realms * 2);
4613 if (ret <= 0)
4614 return 0;
4615 pos += ret;
4616 ie += realms * 2;
4617 ret = os_snprintf(pos, end - pos, "\n");
4618 if (os_snprintf_error(end - pos, ret))
4619 return 0;
4620 pos += ret;
4621 }
4622
4623 return pos - start;
4624}
4625#endif /* CONFIG_FILS */
4626
4627
61ce9085 4628static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
5f97dd1c 4629 unsigned long mask, char *buf, size_t buflen)
6fc6879b 4630{
6fc6879b 4631 size_t i;
6fc6879b
JM
4632 int ret;
4633 char *pos, *end;
e8b96490 4634 const u8 *ie, *ie2, *osen_ie, *mesh, *owe;
6fc6879b 4635
6fc6879b
JM
4636 pos = buf;
4637 end = buf + buflen;
6fc6879b 4638
5f97dd1c
DS
4639 if (mask & WPA_BSS_MASK_ID) {
4640 ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
d85e1fc8 4641 if (os_snprintf_error(end - pos, ret))
5f97dd1c 4642 return 0;
6fc6879b
JM
4643 pos += ret;
4644 }
4645
5f97dd1c
DS
4646 if (mask & WPA_BSS_MASK_BSSID) {
4647 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
4648 MAC2STR(bss->bssid));
d85e1fc8 4649 if (os_snprintf_error(end - pos, ret))
5f97dd1c
DS
4650 return 0;
4651 pos += ret;
4652 }
6fc6879b 4653
5f97dd1c
DS
4654 if (mask & WPA_BSS_MASK_FREQ) {
4655 ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
d85e1fc8 4656 if (os_snprintf_error(end - pos, ret))
5f97dd1c
DS
4657 return 0;
4658 pos += ret;
4659 }
6fc6879b 4660
5f97dd1c
DS
4661 if (mask & WPA_BSS_MASK_BEACON_INT) {
4662 ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
4663 bss->beacon_int);
d85e1fc8 4664 if (os_snprintf_error(end - pos, ret))
5f97dd1c 4665 return 0;
6fc6879b
JM
4666 pos += ret;
4667 }
5f97dd1c
DS
4668
4669 if (mask & WPA_BSS_MASK_CAPABILITIES) {
4670 ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
4671 bss->caps);
d85e1fc8 4672 if (os_snprintf_error(end - pos, ret))
5f97dd1c 4673 return 0;
6fc6879b
JM
4674 pos += ret;
4675 }
5f97dd1c
DS
4676
4677 if (mask & WPA_BSS_MASK_QUAL) {
4678 ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
d85e1fc8 4679 if (os_snprintf_error(end - pos, ret))
5f97dd1c 4680 return 0;
bd1af96a
JM
4681 pos += ret;
4682 }
5f97dd1c
DS
4683
4684 if (mask & WPA_BSS_MASK_NOISE) {
4685 ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
d85e1fc8 4686 if (os_snprintf_error(end - pos, ret))
5f97dd1c 4687 return 0;
cc81110d
JM
4688 pos += ret;
4689 }
6fc6879b 4690
5f97dd1c
DS
4691 if (mask & WPA_BSS_MASK_LEVEL) {
4692 ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
d85e1fc8 4693 if (os_snprintf_error(end - pos, ret))
5f97dd1c
DS
4694 return 0;
4695 pos += ret;
4696 }
6fc6879b 4697
5f97dd1c
DS
4698 if (mask & WPA_BSS_MASK_TSF) {
4699 ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
4700 (unsigned long long) bss->tsf);
d85e1fc8 4701 if (os_snprintf_error(end - pos, ret))
5f97dd1c
DS
4702 return 0;
4703 pos += ret;
4704 }
4705
4706 if (mask & WPA_BSS_MASK_AGE) {
acb69cec 4707 struct os_reltime now;
5f97dd1c 4708
acb69cec 4709 os_get_reltime(&now);
5f97dd1c
DS
4710 ret = os_snprintf(pos, end - pos, "age=%d\n",
4711 (int) (now.sec - bss->last_update.sec));
d85e1fc8 4712 if (os_snprintf_error(end - pos, ret))
5f97dd1c
DS
4713 return 0;
4714 pos += ret;
4715 }
4716
4717 if (mask & WPA_BSS_MASK_IE) {
4718 ret = os_snprintf(pos, end - pos, "ie=");
d85e1fc8 4719 if (os_snprintf_error(end - pos, ret))
5f97dd1c
DS
4720 return 0;
4721 pos += ret;
4722
4723 ie = (const u8 *) (bss + 1);
4724 for (i = 0; i < bss->ie_len; i++) {
4725 ret = os_snprintf(pos, end - pos, "%02x", *ie++);
d85e1fc8 4726 if (os_snprintf_error(end - pos, ret))
5f97dd1c
DS
4727 return 0;
4728 pos += ret;
4729 }
4730
4731 ret = os_snprintf(pos, end - pos, "\n");
d85e1fc8 4732 if (os_snprintf_error(end - pos, ret))
5f97dd1c
DS
4733 return 0;
4734 pos += ret;
4735 }
4736
4737 if (mask & WPA_BSS_MASK_FLAGS) {
4738 ret = os_snprintf(pos, end - pos, "flags=");
d85e1fc8 4739 if (os_snprintf_error(end - pos, ret))
5f97dd1c
DS
4740 return 0;
4741 pos += ret;
4742
4a45dc19
SD
4743 mesh = wpa_bss_get_ie(bss, WLAN_EID_MESH_ID);
4744
5f97dd1c
DS
4745 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
4746 if (ie)
4747 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
4748 2 + ie[1]);
4749 ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
4750 if (ie2)
4a45dc19
SD
4751 pos = wpa_supplicant_ie_txt(pos, end,
4752 mesh ? "RSN" : "WPA2", ie2,
5f97dd1c 4753 2 + ie2[1]);
0f8385e6
BG
4754 osen_ie = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
4755 if (osen_ie)
4756 pos = wpa_supplicant_ie_txt(pos, end, "OSEN",
4757 osen_ie, 2 + osen_ie[1]);
e8b96490
JM
4758 owe = wpa_bss_get_vendor_ie(bss, OWE_IE_VENDOR_TYPE);
4759 if (owe) {
4760 ret = os_snprintf(
4761 pos, end - pos,
4762 ie2 ? "[OWE-TRANS]" : "[OWE-TRANS-OPEN]");
4763 if (os_snprintf_error(end - pos, ret))
4764 return 0;
4765 pos += ret;
4766 }
5f97dd1c 4767 pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
0f8385e6
BG
4768 if (!ie && !ie2 && !osen_ie &&
4769 (bss->caps & IEEE80211_CAP_PRIVACY)) {
5f97dd1c 4770 ret = os_snprintf(pos, end - pos, "[WEP]");
d85e1fc8 4771 if (os_snprintf_error(end - pos, ret))
5f97dd1c
DS
4772 return 0;
4773 pos += ret;
4774 }
4a45dc19
SD
4775
4776 if (mesh) {
4777 ret = os_snprintf(pos, end - pos, "[MESH]");
4778 if (os_snprintf_error(end - pos, ret))
4779 return 0;
4780 pos += ret;
4781 }
4782
e403ba85
BS
4783 if (bss_is_dmg(bss)) {
4784 const char *s;
4785 ret = os_snprintf(pos, end - pos, "[DMG]");
d85e1fc8 4786 if (os_snprintf_error(end - pos, ret))
5f97dd1c
DS
4787 return 0;
4788 pos += ret;
e403ba85
BS
4789 switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
4790 case IEEE80211_CAP_DMG_IBSS:
4791 s = "[IBSS]";
4792 break;
4793 case IEEE80211_CAP_DMG_AP:
4794 s = "[ESS]";
4795 break;
4796 case IEEE80211_CAP_DMG_PBSS:
4797 s = "[PBSS]";
4798 break;
4799 default:
4800 s = "";
4801 break;
4802 }
4803 ret = os_snprintf(pos, end - pos, "%s", s);
d85e1fc8 4804 if (os_snprintf_error(end - pos, ret))
5f97dd1c
DS
4805 return 0;
4806 pos += ret;
e403ba85
BS
4807 } else {
4808 if (bss->caps & IEEE80211_CAP_IBSS) {
4809 ret = os_snprintf(pos, end - pos, "[IBSS]");
d85e1fc8 4810 if (os_snprintf_error(end - pos, ret))
e403ba85
BS
4811 return 0;
4812 pos += ret;
4813 }
4814 if (bss->caps & IEEE80211_CAP_ESS) {
4815 ret = os_snprintf(pos, end - pos, "[ESS]");
d85e1fc8 4816 if (os_snprintf_error(end - pos, ret))
e403ba85
BS
4817 return 0;
4818 pos += ret;
4819 }
5f97dd1c 4820 }
bb50ae43
JM
4821 if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
4822 wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
5f97dd1c 4823 ret = os_snprintf(pos, end - pos, "[P2P]");
d85e1fc8 4824 if (os_snprintf_error(end - pos, ret))
5f97dd1c
DS
4825 return 0;
4826 pos += ret;
4827 }
64855b96
JM
4828#ifdef CONFIG_HS20
4829 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
4830 ret = os_snprintf(pos, end - pos, "[HS20]");
d85e1fc8 4831 if (os_snprintf_error(end - pos, ret))
ff486913 4832 return 0;
64855b96
JM
4833 pos += ret;
4834 }
4835#endif /* CONFIG_HS20 */
7147a834
JM
4836#ifdef CONFIG_FILS
4837 if (wpa_bss_get_ie(bss, WLAN_EID_FILS_INDICATION)) {
4838 ret = os_snprintf(pos, end - pos, "[FILS]");
4839 if (os_snprintf_error(end - pos, ret))
4840 return 0;
4841 pos += ret;
4842 }
4843#endif /* CONFIG_FILS */
e6b0898d
JM
4844#ifdef CONFIG_FST
4845 if (wpa_bss_get_ie(bss, WLAN_EID_MULTI_BAND)) {
4846 ret = os_snprintf(pos, end - pos, "[FST]");
4847 if (os_snprintf_error(end - pos, ret))
4848 return 0;
4849 pos += ret;
4850 }
4851#endif /* CONFIG_FST */
ed87f6a8 4852 if (wpa_bss_ext_capab(bss, WLAN_EXT_CAPAB_UTF_8_SSID)) {
e6b0898d
JM
4853 ret = os_snprintf(pos, end - pos, "[UTF-8]");
4854 if (os_snprintf_error(end - pos, ret))
4855 return 0;
4856 pos += ret;
4857 }
5f97dd1c
DS
4858
4859 ret = os_snprintf(pos, end - pos, "\n");
d85e1fc8 4860 if (os_snprintf_error(end - pos, ret))
5f97dd1c
DS
4861 return 0;
4862 pos += ret;
4863 }
4864
4865 if (mask & WPA_BSS_MASK_SSID) {
4866 ret = os_snprintf(pos, end - pos, "ssid=%s\n",
4867 wpa_ssid_txt(bss->ssid, bss->ssid_len));
d85e1fc8 4868 if (os_snprintf_error(end - pos, ret))
5f97dd1c
DS
4869 return 0;
4870 pos += ret;
4871 }
6fc6879b 4872
611ed491 4873#ifdef CONFIG_WPS
5f97dd1c
DS
4874 if (mask & WPA_BSS_MASK_WPS_SCAN) {
4875 ie = (const u8 *) (bss + 1);
4876 ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
fc078be2 4877 if (ret >= end - pos)
5f97dd1c 4878 return 0;
fc078be2
JM
4879 if (ret > 0)
4880 pos += ret;
5f97dd1c 4881 }
611ed491
JM
4882#endif /* CONFIG_WPS */
4883
0c6b310e 4884#ifdef CONFIG_P2P
5f97dd1c
DS
4885 if (mask & WPA_BSS_MASK_P2P_SCAN) {
4886 ie = (const u8 *) (bss + 1);
4887 ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
3b208346 4888 if (ret >= end - pos)
5f97dd1c 4889 return 0;
3b208346
JA
4890 if (ret > 0)
4891 pos += ret;
5f97dd1c 4892 }
0c6b310e
JM
4893#endif /* CONFIG_P2P */
4894
337c781f
JM
4895#ifdef CONFIG_WIFI_DISPLAY
4896 if (mask & WPA_BSS_MASK_WIFI_DISPLAY) {
4897 struct wpabuf *wfd;
4898 ie = (const u8 *) (bss + 1);
4899 wfd = ieee802_11_vendor_ie_concat(ie, bss->ie_len,
4900 WFD_IE_VENDOR_TYPE);
4901 if (wfd) {
4902 ret = os_snprintf(pos, end - pos, "wfd_subelems=");
d85e1fc8 4903 if (os_snprintf_error(end - pos, ret)) {
5e6aa04b 4904 wpabuf_free(wfd);
ff486913 4905 return 0;
5e6aa04b 4906 }
337c781f
JM
4907 pos += ret;
4908
4909 pos += wpa_snprintf_hex(pos, end - pos,
4910 wpabuf_head(wfd),
4911 wpabuf_len(wfd));
4912 wpabuf_free(wfd);
4913
4914 ret = os_snprintf(pos, end - pos, "\n");
d85e1fc8 4915 if (os_snprintf_error(end - pos, ret))
ff486913 4916 return 0;
337c781f
JM
4917 pos += ret;
4918 }
4919 }
4920#endif /* CONFIG_WIFI_DISPLAY */
4921
afc064fe 4922#ifdef CONFIG_INTERWORKING
476aed35
JM
4923 if ((mask & WPA_BSS_MASK_INTERNETW) && bss->anqp) {
4924 struct wpa_bss_anqp *anqp = bss->anqp;
8c4a1026
JM
4925 struct wpa_bss_anqp_elem *elem;
4926
5ce6ac11
AN
4927 pos = anqp_add_hex(pos, end, "anqp_capability_list",
4928 anqp->capability_list);
5f97dd1c 4929 pos = anqp_add_hex(pos, end, "anqp_venue_name",
476aed35 4930 anqp->venue_name);
5f97dd1c 4931 pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
476aed35 4932 anqp->network_auth_type);
5f97dd1c 4933 pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
476aed35 4934 anqp->roaming_consortium);
5f97dd1c 4935 pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
476aed35 4936 anqp->ip_addr_type_availability);
5f97dd1c 4937 pos = anqp_add_hex(pos, end, "anqp_nai_realm",
476aed35
JM
4938 anqp->nai_realm);
4939 pos = anqp_add_hex(pos, end, "anqp_3gpp", anqp->anqp_3gpp);
5f97dd1c 4940 pos = anqp_add_hex(pos, end, "anqp_domain_name",
476aed35 4941 anqp->domain_name);
9cad6186
JM
4942 pos = anqp_add_hex(pos, end, "anqp_fils_realm_info",
4943 anqp->fils_realm_info);
25471fe3 4944#ifdef CONFIG_HS20
185ada47
AN
4945 pos = anqp_add_hex(pos, end, "hs20_capability_list",
4946 anqp->hs20_capability_list);
25471fe3 4947 pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
476aed35 4948 anqp->hs20_operator_friendly_name);
25471fe3 4949 pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
476aed35 4950 anqp->hs20_wan_metrics);
25471fe3 4951 pos = anqp_add_hex(pos, end, "hs20_connection_capability",
476aed35 4952 anqp->hs20_connection_capability);
1d2215fc
JM
4953 pos = anqp_add_hex(pos, end, "hs20_operating_class",
4954 anqp->hs20_operating_class);
4955 pos = anqp_add_hex(pos, end, "hs20_osu_providers_list",
4956 anqp->hs20_osu_providers_list);
6a8a04d7
JM
4957 pos = anqp_add_hex(pos, end, "hs20_operator_icon_metadata",
4958 anqp->hs20_operator_icon_metadata);
baf4c863
JM
4959 pos = anqp_add_hex(pos, end, "hs20_osu_providers_nai_list",
4960 anqp->hs20_osu_providers_nai_list);
25471fe3 4961#endif /* CONFIG_HS20 */
8c4a1026
JM
4962
4963 dl_list_for_each(elem, &anqp->anqp_elems,
4964 struct wpa_bss_anqp_elem, list) {
4965 char title[20];
4966
4967 os_snprintf(title, sizeof(title), "anqp[%u]",
4968 elem->infoid);
4969 pos = anqp_add_hex(pos, end, title, elem->payload);
4970 }
5f97dd1c 4971 }
afc064fe
JM
4972#endif /* CONFIG_INTERWORKING */
4973
79070906
MH
4974#ifdef CONFIG_MESH
4975 if (mask & WPA_BSS_MASK_MESH_SCAN) {
4976 ie = (const u8 *) (bss + 1);
4977 ret = wpas_mesh_scan_result_text(ie, bss->ie_len, pos, end);
3b208346 4978 if (ret >= end - pos)
79070906 4979 return 0;
3b208346
JA
4980 if (ret > 0)
4981 pos += ret;
79070906
MH
4982 }
4983#endif /* CONFIG_MESH */
4984
1d747e2a
JM
4985 if (mask & WPA_BSS_MASK_SNR) {
4986 ret = os_snprintf(pos, end - pos, "snr=%d\n", bss->snr);
4987 if (os_snprintf_error(end - pos, ret))
4988 return 0;
4989 pos += ret;
4990 }
4991
4992 if (mask & WPA_BSS_MASK_EST_THROUGHPUT) {
4993 ret = os_snprintf(pos, end - pos, "est_throughput=%d\n",
4994 bss->est_throughput);
4995 if (os_snprintf_error(end - pos, ret))
4996 return 0;
4997 pos += ret;
4998 }
4999
3794af2d
AN
5000#ifdef CONFIG_FST
5001 if (mask & WPA_BSS_MASK_FST) {
5002 ret = fst_ctrl_iface_mb_info(bss->bssid, pos, end - pos);
5003 if (ret < 0 || ret >= end - pos)
5004 return 0;
5005 pos += ret;
5006 }
5007#endif /* CONFIG_FST */
5008
71ac9345
JM
5009 if (mask & WPA_BSS_MASK_UPDATE_IDX) {
5010 ret = os_snprintf(pos, end - pos, "update_idx=%u\n",
5011 bss->last_update_idx);
5012 if (os_snprintf_error(end - pos, ret))
5013 return 0;
5014 pos += ret;
5015 }
5016
19810d29
JM
5017 if ((mask & WPA_BSS_MASK_BEACON_IE) && bss->beacon_ie_len) {
5018 ret = os_snprintf(pos, end - pos, "beacon_ie=");
5019 if (os_snprintf_error(end - pos, ret))
5020 return 0;
5021 pos += ret;
5022
5023 ie = (const u8 *) (bss + 1);
5024 ie += bss->ie_len;
5025 for (i = 0; i < bss->beacon_ie_len; i++) {
5026 ret = os_snprintf(pos, end - pos, "%02x", *ie++);
5027 if (os_snprintf_error(end - pos, ret))
5028 return 0;
5029 pos += ret;
5030 }
5031
5032 ret = os_snprintf(pos, end - pos, "\n");
5033 if (os_snprintf_error(end - pos, ret))
5034 return 0;
5035 pos += ret;
5036 }
5037
b54f4339
JM
5038#ifdef CONFIG_FILS
5039 if (mask & WPA_BSS_MASK_FILS_INDICATION) {
5040 ret = print_fils_indication(bss, pos, end);
5041 if (ret < 0)
5042 return 0;
5043 pos += ret;
5044 }
5045#endif /* CONFIG_FILS */
5046
c6673429
DS
5047 if (mask & WPA_BSS_MASK_DELIM) {
5048 ret = os_snprintf(pos, end - pos, "====\n");
d85e1fc8 5049 if (os_snprintf_error(end - pos, ret))
c6673429
DS
5050 return 0;
5051 pos += ret;
5052 }
5053
6fc6879b
JM
5054 return pos - buf;
5055}
5056
5057
61ce9085
DS
5058static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
5059 const char *cmd, char *buf,
5060 size_t buflen)
5061{
5062 u8 bssid[ETH_ALEN];
5063 size_t i;
5064 struct wpa_bss *bss;
eff1a95b
DS
5065 struct wpa_bss *bsslast = NULL;
5066 struct dl_list *next;
5067 int ret = 0;
5068 int len;
1d399771 5069 char *ctmp, *end = buf + buflen;
5f97dd1c 5070 unsigned long mask = WPA_BSS_MASK_ALL;
61ce9085 5071
eff1a95b
DS
5072 if (os_strncmp(cmd, "RANGE=", 6) == 0) {
5073 if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
5074 bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
5075 list_id);
5076 bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
5077 list_id);
5078 } else { /* N1-N2 */
5079 unsigned int id1, id2;
5080
5081 if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
5082 wpa_printf(MSG_INFO, "Wrong BSS range "
5083 "format");
5084 return 0;
5085 }
5086
9f42d49c
AS
5087 if (*(cmd + 6) == '-')
5088 id1 = 0;
5089 else
5090 id1 = atoi(cmd + 6);
5091 ctmp++;
5092 if (*ctmp >= '0' && *ctmp <= '9')
5093 id2 = atoi(ctmp);
5094 else
5095 id2 = (unsigned int) -1;
5096 bss = wpa_bss_get_id_range(wpa_s, id1, id2);
5097 if (id2 == (unsigned int) -1)
eff1a95b
DS
5098 bsslast = dl_list_last(&wpa_s->bss_id,
5099 struct wpa_bss,
5100 list_id);
5101 else {
5102 bsslast = wpa_bss_get_id(wpa_s, id2);
5103 if (bsslast == NULL && bss && id2 > id1) {
5104 struct wpa_bss *tmp = bss;
5105 for (;;) {
5106 next = tmp->list_id.next;
5107 if (next == &wpa_s->bss_id)
5108 break;
5109 tmp = dl_list_entry(
5110 next, struct wpa_bss,
5111 list_id);
5112 if (tmp->id > id2)
5113 break;
5114 bsslast = tmp;
5115 }
5116 }
5117 }
5118 }
f330b4b4 5119 } else if (os_strncmp(cmd, "FIRST", 5) == 0)
51a0c3d4 5120 bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
cc03d0fe
AS
5121 else if (os_strncmp(cmd, "LAST", 4) == 0)
5122 bss = dl_list_last(&wpa_s->bss_id, struct wpa_bss, list_id);
61ce9085
DS
5123 else if (os_strncmp(cmd, "ID-", 3) == 0) {
5124 i = atoi(cmd + 3);
5125 bss = wpa_bss_get_id(wpa_s, i);
5126 } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
5127 i = atoi(cmd + 5);
5128 bss = wpa_bss_get_id(wpa_s, i);
5129 if (bss) {
eff1a95b 5130 next = bss->list_id.next;
61ce9085
DS
5131 if (next == &wpa_s->bss_id)
5132 bss = NULL;
5133 else
5134 bss = dl_list_entry(next, struct wpa_bss,
5135 list_id);
5136 }
9187b13a
JC
5137 } else if (os_strncmp(cmd, "CURRENT", 7) == 0) {
5138 bss = wpa_s->current_bss;
61ce9085
DS
5139#ifdef CONFIG_P2P
5140 } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
5141 if (hwaddr_aton(cmd + 13, bssid) == 0)
5142 bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
5143 else
5144 bss = NULL;
5145#endif /* CONFIG_P2P */
5146 } else if (hwaddr_aton(cmd, bssid) == 0)
5147 bss = wpa_bss_get_bssid(wpa_s, bssid);
5148 else {
5149 struct wpa_bss *tmp;
5150 i = atoi(cmd);
5151 bss = NULL;
5152 dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
5153 {
01d01a31 5154 if (i == 0) {
61ce9085
DS
5155 bss = tmp;
5156 break;
5157 }
01d01a31 5158 i--;
61ce9085
DS
5159 }
5160 }
5161
5f97dd1c
DS
5162 if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
5163 mask = strtoul(ctmp + 5, NULL, 0x10);
5164 if (mask == 0)
5165 mask = WPA_BSS_MASK_ALL;
5166 }
5167
61ce9085
DS
5168 if (bss == NULL)
5169 return 0;
5170
eff1a95b
DS
5171 if (bsslast == NULL)
5172 bsslast = bss;
5173 do {
5174 len = print_bss_info(wpa_s, bss, mask, buf, buflen);
5175 ret += len;
5176 buf += len;
5177 buflen -= len;
cfd42c94
DS
5178 if (bss == bsslast) {
5179 if ((mask & WPA_BSS_MASK_DELIM) && len &&
5180 (bss == dl_list_last(&wpa_s->bss_id,
1d399771
JM
5181 struct wpa_bss, list_id))) {
5182 int res;
5183
5184 res = os_snprintf(buf - 5, end - buf + 5,
5185 "####\n");
5186 if (os_snprintf_error(end - buf + 5, res)) {
5187 wpa_printf(MSG_DEBUG,
5188 "Could not add end delim");
5189 }
5190 }
eff1a95b 5191 break;
cfd42c94 5192 }
eff1a95b
DS
5193 next = bss->list_id.next;
5194 if (next == &wpa_s->bss_id)
5195 break;
5196 bss = dl_list_entry(next, struct wpa_bss, list_id);
5197 } while (bss && len);
5198
5199 return ret;
61ce9085
DS
5200}
5201
5202
6fc6879b
JM
5203static int wpa_supplicant_ctrl_iface_ap_scan(
5204 struct wpa_supplicant *wpa_s, char *cmd)
5205{
5206 int ap_scan = atoi(cmd);
86b89452 5207 return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
6fc6879b
JM
5208}
5209
5210
67b9bd08
DS
5211static int wpa_supplicant_ctrl_iface_scan_interval(
5212 struct wpa_supplicant *wpa_s, char *cmd)
5213{
5214 int scan_int = atoi(cmd);
c6e86b63 5215 return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
67b9bd08
DS
5216}
5217
5218
78633c37
SL
5219static int wpa_supplicant_ctrl_iface_bss_expire_age(
5220 struct wpa_supplicant *wpa_s, char *cmd)
5221{
5222 int expire_age = atoi(cmd);
5223 return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
5224}
5225
5226
5227static int wpa_supplicant_ctrl_iface_bss_expire_count(
5228 struct wpa_supplicant *wpa_s, char *cmd)
5229{
5230 int expire_count = atoi(cmd);
5231 return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
5232}
5233
5234
a1144000 5235static void wpa_supplicant_ctrl_iface_bss_flush(
39ee845f
DS
5236 struct wpa_supplicant *wpa_s, char *cmd)
5237{
5238 int flush_age = atoi(cmd);
5239
5240 if (flush_age == 0)
5241 wpa_bss_flush(wpa_s);
5242 else
5243 wpa_bss_flush_by_age(wpa_s, flush_age);
39ee845f
DS
5244}
5245
5246
9ff4de6d 5247#ifdef CONFIG_TESTING_OPTIONS
32d5295f
JM
5248static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
5249{
32d5295f
JM
5250 wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
5251 /* MLME-DELETEKEYS.request */
0382097e
JM
5252 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
5253 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
5254 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
5255 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
0382097e
JM
5256 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
5257 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
32d5295f
JM
5258
5259 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
5260 0);
5261 /* MLME-SETPROTECTION.request(None) */
5262 wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
5263 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
5264 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
5265 wpa_sm_drop_sa(wpa_s->wpa);
5266}
9ff4de6d 5267#endif /* CONFIG_TESTING_OPTIONS */
32d5295f
JM
5268
5269
86d4f806
JM
5270static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
5271 char *addr)
5272{
90b8fc8f
JM
5273#ifdef CONFIG_NO_SCAN_PROCESSING
5274 return -1;
5275#else /* CONFIG_NO_SCAN_PROCESSING */
86d4f806
JM
5276 u8 bssid[ETH_ALEN];
5277 struct wpa_bss *bss;
5278 struct wpa_ssid *ssid = wpa_s->current_ssid;
5279
5280 if (hwaddr_aton(addr, bssid)) {
5281 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
5282 "address '%s'", addr);
5283 return -1;
5284 }
5285
5286 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
5287
2f9b66d3
JM
5288 if (!ssid) {
5289 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
5290 "configuration known for the target AP");
5291 return -1;
5292 }
5293
5294 bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
86d4f806
JM
5295 if (!bss) {
5296 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
5297 "from BSS table");
5298 return -1;
5299 }
5300
5301 /*
5302 * TODO: Find best network configuration block from configuration to
5303 * allow roaming to other networks
5304 */
5305
86d4f806
JM
5306 wpa_s->reassociate = 1;
5307 wpa_supplicant_connect(wpa_s, bss, ssid);
5308
5309 return 0;
90b8fc8f 5310#endif /* CONFIG_NO_SCAN_PROCESSING */
86d4f806
JM
5311}
5312
5313
b563b388
JM
5314#ifdef CONFIG_P2P
5315static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
5316{
5317 unsigned int timeout = atoi(cmd);
5318 enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
6d92fa6e 5319 u8 dev_id[ETH_ALEN], *_dev_id = NULL;
2b384109 5320 u8 dev_type[WPS_DEV_TYPE_LEN], *_dev_type = NULL;
6d92fa6e 5321 char *pos;
05a77b3b 5322 unsigned int search_delay;
9542f21f 5323 const char *_seek[P2P_MAX_QUERY_HASH + 1], **seek = NULL;
51775096 5324 u8 seek_count = 0;
fa9f381f 5325 int freq = 0;
b563b388 5326
e9eb648e
JM
5327 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
5328 wpa_dbg(wpa_s, MSG_INFO,
5329 "Reject P2P_FIND since interface is disabled");
5330 return -1;
5331 }
b563b388
JM
5332 if (os_strstr(cmd, "type=social"))
5333 type = P2P_FIND_ONLY_SOCIAL;
5334 else if (os_strstr(cmd, "type=progressive"))
5335 type = P2P_FIND_PROGRESSIVE;
5336
6d92fa6e
JM
5337 pos = os_strstr(cmd, "dev_id=");
5338 if (pos) {
5339 pos += 7;
5340 if (hwaddr_aton(pos, dev_id))
5341 return -1;
5342 _dev_id = dev_id;
5343 }
5344
2b384109
JM
5345 pos = os_strstr(cmd, "dev_type=");
5346 if (pos) {
5347 pos += 9;
5348 if (wps_dev_type_str2bin(pos, dev_type) < 0)
5349 return -1;
5350 _dev_type = dev_type;
5351 }
5352
37448ede
JM
5353 pos = os_strstr(cmd, "delay=");
5354 if (pos) {
5355 pos += 6;
5356 search_delay = atoi(pos);
05a77b3b
JM
5357 } else
5358 search_delay = wpas_p2p_search_delay(wpa_s);
37448ede 5359
a9ea609c
SM
5360 pos = os_strstr(cmd, "freq=");
5361 if (pos) {
5362 pos += 5;
5363 freq = atoi(pos);
5364 if (freq <= 0)
5365 return -1;
5366 }
5367
51775096
BG
5368 /* Must be searched for last, because it adds nul termination */
5369 pos = os_strstr(cmd, " seek=");
129b6216
JM
5370 if (pos)
5371 pos += 6;
51775096
BG
5372 while (pos && seek_count < P2P_MAX_QUERY_HASH + 1) {
5373 char *term;
5374
129b6216 5375 _seek[seek_count++] = pos;
9542f21f 5376 seek = _seek;
129b6216
JM
5377 term = os_strchr(pos, ' ');
5378 if (!term)
5379 break;
5380 *term = '\0';
5381 pos = os_strstr(term + 1, "seek=");
5382 if (pos)
5383 pos += 5;
51775096 5384 }
9542f21f
JM
5385 if (seek_count > P2P_MAX_QUERY_HASH) {
5386 seek[0] = NULL;
5387 seek_count = 1;
5388 }
51775096 5389
2b384109 5390 return wpas_p2p_find(wpa_s, timeout, type, _dev_type != NULL, _dev_type,
fa9f381f 5391 _dev_id, search_delay, seek_count, seek, freq);
b563b388
JM
5392}
5393
5394
e2b7fbf2
MS
5395static int p2ps_ctrl_parse_cpt_priority(const char *pos, u8 *cpt)
5396{
5397 const char *last = NULL;
5398 const char *token;
5399 long int token_len;
5400 unsigned int i;
5401
5402 /* Expected predefined CPT names delimited by ':' */
5403 for (i = 0; (token = cstr_token(pos, ": \t", &last)); i++) {
5404 if (i >= P2PS_FEATURE_CAPAB_CPT_MAX) {
5405 wpa_printf(MSG_ERROR,
5406 "P2PS: CPT name list is too long, expected up to %d names",
5407 P2PS_FEATURE_CAPAB_CPT_MAX);
5408 cpt[0] = 0;
5409 return -1;
5410 }
5411
5412 token_len = last - token;
5413
5414 if (token_len == 3 &&
5415 os_memcmp(token, "UDP", token_len) == 0) {
5416 cpt[i] = P2PS_FEATURE_CAPAB_UDP_TRANSPORT;
5417 } else if (token_len == 3 &&
5418 os_memcmp(token, "MAC", token_len) == 0) {
5419 cpt[i] = P2PS_FEATURE_CAPAB_MAC_TRANSPORT;
5420 } else {
5421 wpa_printf(MSG_ERROR,
5422 "P2PS: Unsupported CPT name '%s'", token);
5423 cpt[0] = 0;
5424 return -1;
5425 }
5426
640b0b93 5427 if (isblank((unsigned char) *last)) {
e2b7fbf2
MS
5428 i++;
5429 break;
5430 }
5431 }
5432 cpt[i] = 0;
5433 return 0;
5434}
5435
5436
f309c18e
KV
5437static struct p2ps_provision * p2p_parse_asp_provision_cmd(const char *cmd)
5438{
5439 struct p2ps_provision *p2ps_prov;
5440 char *pos;
5441 size_t info_len = 0;
5442 char *info = NULL;
5443 u8 role = P2PS_SETUP_NONE;
5444 long long unsigned val;
0670de74 5445 int i;
f309c18e
KV
5446
5447 pos = os_strstr(cmd, "info=");
5448 if (pos) {
5449 pos += 5;
5450 info_len = os_strlen(pos);
5451
5452 if (info_len) {
5453 info = os_malloc(info_len + 1);
5454 if (info) {
5455 info_len = utf8_unescape(pos, info_len,
5456 info, info_len + 1);
5457 } else
5458 info_len = 0;
5459 }
5460 }
5461
5462 p2ps_prov = os_zalloc(sizeof(struct p2ps_provision) + info_len + 1);
5463 if (p2ps_prov == NULL) {
5464 os_free(info);
5465 return NULL;
5466 }
5467
5468 if (info) {
5469 os_memcpy(p2ps_prov->info, info, info_len);
5470 p2ps_prov->info[info_len] = '\0';
5471 os_free(info);
5472 }
5473
5474 pos = os_strstr(cmd, "status=");
5475 if (pos)
5476 p2ps_prov->status = atoi(pos + 7);
5477 else
5478 p2ps_prov->status = -1;
5479
5480 pos = os_strstr(cmd, "adv_id=");
5481 if (!pos || sscanf(pos + 7, "%llx", &val) != 1 || val > 0xffffffffULL)
5482 goto invalid_args;
5483 p2ps_prov->adv_id = val;
5484
5485 pos = os_strstr(cmd, "method=");
5486 if (pos)
5487 p2ps_prov->method = strtol(pos + 7, NULL, 16);
5488 else
5489 p2ps_prov->method = 0;
5490
5491 pos = os_strstr(cmd, "session=");
5492 if (!pos || sscanf(pos + 8, "%llx", &val) != 1 || val > 0xffffffffULL)
5493 goto invalid_args;
5494 p2ps_prov->session_id = val;
5495
5496 pos = os_strstr(cmd, "adv_mac=");
5497 if (!pos || hwaddr_aton(pos + 8, p2ps_prov->adv_mac))
5498 goto invalid_args;
5499
5500 pos = os_strstr(cmd, "session_mac=");
5501 if (!pos || hwaddr_aton(pos + 12, p2ps_prov->session_mac))
5502 goto invalid_args;
5503
0670de74
MS
5504 pos = os_strstr(cmd, "cpt=");
5505 if (pos) {
5506 if (p2ps_ctrl_parse_cpt_priority(pos + 4,
5507 p2ps_prov->cpt_priority))
5508 goto invalid_args;
5509 } else {
5510 p2ps_prov->cpt_priority[0] = P2PS_FEATURE_CAPAB_UDP_TRANSPORT;
5511 }
5512
5513 for (i = 0; p2ps_prov->cpt_priority[i]; i++)
5514 p2ps_prov->cpt_mask |= p2ps_prov->cpt_priority[i];
5515
f309c18e
KV
5516 /* force conncap with tstCap (no sanity checks) */
5517 pos = os_strstr(cmd, "tstCap=");
5518 if (pos) {
5519 role = strtol(pos + 7, NULL, 16);
5520 } else {
5521 pos = os_strstr(cmd, "role=");
5522 if (pos) {
5523 role = strtol(pos + 5, NULL, 16);
5524 if (role != P2PS_SETUP_CLIENT &&
5525 role != P2PS_SETUP_GROUP_OWNER)
5526 role = P2PS_SETUP_NONE;
5527 }
5528 }
5529 p2ps_prov->role = role;
5530
5531 return p2ps_prov;
5532
5533invalid_args:
5534 os_free(p2ps_prov);
5535 return NULL;
5536}
5537
5538
5539static int p2p_ctrl_asp_provision_resp(struct wpa_supplicant *wpa_s, char *cmd)
5540{
5541 u8 addr[ETH_ALEN];
5542 struct p2ps_provision *p2ps_prov;
5543 char *pos;
5544
5545 /* <addr> id=<adv_id> [role=<conncap>] [info=<infodata>] */
5546
5547 wpa_printf(MSG_DEBUG, "%s: %s", __func__, cmd);
5548
5549 if (hwaddr_aton(cmd, addr))
5550 return -1;
5551
5552 pos = cmd + 17;
5553 if (*pos != ' ')
5554 return -1;
5555
5556 p2ps_prov = p2p_parse_asp_provision_cmd(pos);
5557 if (!p2ps_prov)
5558 return -1;
5559
5560 if (p2ps_prov->status < 0) {
5561 os_free(p2ps_prov);
5562 return -1;
5563 }
5564
5565 return wpas_p2p_prov_disc(wpa_s, addr, NULL, WPAS_P2P_PD_FOR_ASP,
5566 p2ps_prov);
5567}
5568
5569
5570static int p2p_ctrl_asp_provision(struct wpa_supplicant *wpa_s, char *cmd)
5571{
5572 u8 addr[ETH_ALEN];
5573 struct p2ps_provision *p2ps_prov;
5574 char *pos;
5575
5576 /* <addr> id=<adv_id> adv_mac=<adv_mac> conncap=<conncap>
5577 * session=<ses_id> mac=<ses_mac> [info=<infodata>]
5578 */
5579
5580 wpa_printf(MSG_DEBUG, "%s: %s", __func__, cmd);
5581 if (hwaddr_aton(cmd, addr))
5582 return -1;
5583
5584 pos = cmd + 17;
5585 if (*pos != ' ')
5586 return -1;
5587
5588 p2ps_prov = p2p_parse_asp_provision_cmd(pos);
5589 if (!p2ps_prov)
5590 return -1;
5591
93f22b45
MS
5592 p2ps_prov->pd_seeker = 1;
5593
f309c18e
KV
5594 return wpas_p2p_prov_disc(wpa_s, addr, NULL, WPAS_P2P_PD_FOR_ASP,
5595 p2ps_prov);
5596}
5597
5598
c27f4c90
AK
5599static int parse_freq(int chwidth, int freq2)
5600{
5601 if (freq2 < 0)
5602 return -1;
5603 if (freq2)
464dcfd0 5604 return CHANWIDTH_80P80MHZ;
c27f4c90
AK
5605
5606 switch (chwidth) {
5607 case 0:
5608 case 20:
5609 case 40:
464dcfd0 5610 return CHANWIDTH_USE_HT;
c27f4c90 5611 case 80:
464dcfd0 5612 return CHANWIDTH_80MHZ;
c27f4c90 5613 case 160:
464dcfd0 5614 return CHANWIDTH_160MHZ;
c27f4c90
AK
5615 default:
5616 wpa_printf(MSG_DEBUG, "Unknown max oper bandwidth: %d",
5617 chwidth);
5618 return -1;
5619 }
5620}
5621
5622
b563b388
JM
5623static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
5624 char *buf, size_t buflen)
5625{
5626 u8 addr[ETH_ALEN];
5627 char *pos, *pos2;
5628 char *pin = NULL;
5629 enum p2p_wps_method wps_method;
5630 int new_pin;
5631 int ret;
23c84252 5632 int persistent_group, persistent_id = -1;
b563b388
JM
5633 int join;
5634 int auth;
b31be3a0 5635 int automatic;
b563b388
JM
5636 int go_intent = -1;
5637 int freq = 0;
3bc462cb 5638 int pd;
c27f4c90 5639 int ht40, vht, max_oper_chwidth, chwidth = 0, freq2 = 0;
8edd9f10
JM
5640 u8 _group_ssid[SSID_MAX_LEN], *group_ssid = NULL;
5641 size_t group_ssid_len = 0;
5a3319ab 5642 int he;
b563b388 5643
bdf0518b
JM
5644 if (!wpa_s->global->p2p_init_wpa_s)
5645 return -1;
5646 if (wpa_s->global->p2p_init_wpa_s != wpa_s) {
5647 wpa_dbg(wpa_s, MSG_DEBUG, "Direct P2P_CONNECT command to %s",
5648 wpa_s->global->p2p_init_wpa_s->ifname);
5649 wpa_s = wpa_s->global->p2p_init_wpa_s;
5650 }
5651
4f88fc04 5652 /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad|p2ps]
23c84252 5653 * [persistent|persistent=<network id>]
e2308e4b 5654 * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
5a3319ab 5655 * [ht40] [vht] [he] [auto] [ssid=<hexdump>] */
b563b388
JM
5656
5657 if (hwaddr_aton(cmd, addr))
5658 return -1;
5659
5660 pos = cmd + 17;
5661 if (*pos != ' ')
5662 return -1;
5663 pos++;
5664
5665 persistent_group = os_strstr(pos, " persistent") != NULL;
23c84252
JM
5666 pos2 = os_strstr(pos, " persistent=");
5667 if (pos2) {
5668 struct wpa_ssid *ssid;
5669 persistent_id = atoi(pos2 + 12);
5670 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
5671 if (ssid == NULL || ssid->disabled != 2 ||
5672 ssid->mode != WPAS_MODE_P2P_GO) {
5673 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
5674 "SSID id=%d for persistent P2P group (GO)",
5675 persistent_id);
5676 return -1;
5677 }
5678 }
b563b388
JM
5679 join = os_strstr(pos, " join") != NULL;
5680 auth = os_strstr(pos, " auth") != NULL;
b31be3a0 5681 automatic = os_strstr(pos, " auto") != NULL;
3bc462cb 5682 pd = os_strstr(pos, " provdisc") != NULL;
20ea1ca4
EP
5683 vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
5684 ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
5685 vht;
5a3319ab 5686 he = (os_strstr(cmd, " he") != NULL) || wpa_s->conf->p2p_go_he;
b563b388
JM
5687
5688 pos2 = os_strstr(pos, " go_intent=");
5689 if (pos2) {
5690 pos2 += 11;
5691 go_intent = atoi(pos2);
5692 if (go_intent < 0 || go_intent > 15)
5693 return -1;
5694 }
5695
5696 pos2 = os_strstr(pos, " freq=");
5697 if (pos2) {
5698 pos2 += 6;
5699 freq = atoi(pos2);
5700 if (freq <= 0)
5701 return -1;
5702 }
5703
c27f4c90
AK
5704 pos2 = os_strstr(pos, " freq2=");
5705 if (pos2)
5706 freq2 = atoi(pos2 + 7);
5707
5708 pos2 = os_strstr(pos, " max_oper_chwidth=");
5709 if (pos2)
5710 chwidth = atoi(pos2 + 18);
5711
5712 max_oper_chwidth = parse_freq(chwidth, freq2);
5713 if (max_oper_chwidth < 0)
5714 return -1;
5715
8edd9f10
JM
5716 pos2 = os_strstr(pos, " ssid=");
5717 if (pos2) {
5718 char *end;
5719
5720 pos2 += 6;
5721 end = os_strchr(pos2, ' ');
5722 if (!end)
5723 group_ssid_len = os_strlen(pos2) / 2;
5724 else
5725 group_ssid_len = (end - pos2) / 2;
5726 if (group_ssid_len == 0 || group_ssid_len > SSID_MAX_LEN ||
5727 hexstr2bin(pos2, _group_ssid, group_ssid_len) < 0)
5728 return -1;
5729 group_ssid = _group_ssid;
5730 }
5731
b563b388
JM
5732 if (os_strncmp(pos, "pin", 3) == 0) {
5733 /* Request random PIN (to be displayed) and enable the PIN */
5734 wps_method = WPS_PIN_DISPLAY;
5735 } else if (os_strncmp(pos, "pbc", 3) == 0) {
5736 wps_method = WPS_PBC;
31d7fb14
PK
5737 } else if (os_strstr(pos, "p2ps") != NULL) {
5738 wps_method = WPS_P2PS;
b563b388
JM
5739 } else {
5740 pin = pos;
5741 pos = os_strchr(pin, ' ');
5742 wps_method = WPS_PIN_KEYPAD;
5743 if (pos) {
5744 *pos++ = '\0';
07fecd39 5745 if (os_strncmp(pos, "display", 7) == 0)
b563b388
JM
5746 wps_method = WPS_PIN_DISPLAY;
5747 }
dcc33057 5748 if (!wps_pin_str_valid(pin)) {
36ebf7a1
MH
5749 os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
5750 return 17;
5751 }
b563b388
JM
5752 }
5753
5754 new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
b31be3a0 5755 persistent_group, automatic, join,
c27f4c90 5756 auth, go_intent, freq, freq2, persistent_id,
5a3319ab 5757 pd, ht40, vht, max_oper_chwidth, he,
8edd9f10 5758 group_ssid, group_ssid_len);
d054a462
JM
5759 if (new_pin == -2) {
5760 os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
5761 return 25;
5762 }
5763 if (new_pin == -3) {
5764 os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
5765 return 25;
5766 }
b563b388
JM
5767 if (new_pin < 0)
5768 return -1;
5769 if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
5770 ret = os_snprintf(buf, buflen, "%08d", new_pin);
d85e1fc8 5771 if (os_snprintf_error(buflen, ret))
b563b388
JM
5772 return -1;
5773 return ret;
5774 }
5775
5776 os_memcpy(buf, "OK\n", 3);
5777 return 3;
5778}
5779
5780
5781static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
5782{
5783 unsigned int timeout = atoi(cmd);
e9eb648e
JM
5784 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
5785 wpa_dbg(wpa_s, MSG_INFO,
5786 "Reject P2P_LISTEN since interface is disabled");
5787 return -1;
5788 }
b563b388
JM
5789 return wpas_p2p_listen(wpa_s, timeout);
5790}
5791
5792
5793static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
5794{
5795 u8 addr[ETH_ALEN];
5796 char *pos;
0918c4bf 5797 enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
b563b388 5798
0918c4bf 5799 /* <addr> <config method> [join|auto] */
b563b388
JM
5800
5801 if (hwaddr_aton(cmd, addr))
5802 return -1;
5803
5804 pos = cmd + 17;
5805 if (*pos != ' ')
5806 return -1;
5807 pos++;
5808
0918c4bf
JM
5809 if (os_strstr(pos, " join") != NULL)
5810 use = WPAS_P2P_PD_FOR_JOIN;
5811 else if (os_strstr(pos, " auto") != NULL)
5812 use = WPAS_P2P_PD_AUTO;
5813
6d908514 5814 return wpas_p2p_prov_disc(wpa_s, addr, pos, use, NULL);
b563b388
JM
5815}
5816
5817
5818static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
5819 size_t buflen)
5820{
5821 struct wpa_ssid *ssid = wpa_s->current_ssid;
5822
5823 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
5824 ssid->passphrase == NULL)
5825 return -1;
5826
5827 os_strlcpy(buf, ssid->passphrase, buflen);
5828 return os_strlen(buf);
5829}
5830
5831
5832static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
5833 char *buf, size_t buflen)
5834{
5835 u64 ref;
5836 int res;
5837 u8 dst_buf[ETH_ALEN], *dst;
5838 struct wpabuf *tlvs;
5839 char *pos;
5840 size_t len;
5841
5842 if (hwaddr_aton(cmd, dst_buf))
5843 return -1;
5844 dst = dst_buf;
5845 if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
5846 dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
5847 dst = NULL;
5848 pos = cmd + 17;
5849 if (*pos != ' ')
5850 return -1;
5851 pos++;
5852
5853 if (os_strncmp(pos, "upnp ", 5) == 0) {
5854 u8 version;
5855 pos += 5;
5856 if (hexstr2bin(pos, &version, 1) < 0)
5857 return -1;
5858 pos += 2;
5859 if (*pos != ' ')
5860 return -1;
5861 pos++;
7165c5dc 5862 ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
347d6a5b
JM
5863#ifdef CONFIG_WIFI_DISPLAY
5864 } else if (os_strncmp(pos, "wifi-display ", 13) == 0) {
5865 ref = wpas_p2p_sd_request_wifi_display(wpa_s, dst, pos + 13);
5866#endif /* CONFIG_WIFI_DISPLAY */
5a4102ce
KV
5867 } else if (os_strncmp(pos, "asp ", 4) == 0) {
5868 char *svc_str;
5869 char *svc_info = NULL;
5870 u32 id;
5871
5872 pos += 4;
5873 if (sscanf(pos, "%x", &id) != 1 || id > 0xff)
5874 return -1;
5875
5876 pos = os_strchr(pos, ' ');
5877 if (pos == NULL || pos[1] == '\0' || pos[1] == ' ')
5878 return -1;
5879
5880 svc_str = pos + 1;
5881
5882 pos = os_strchr(svc_str, ' ');
5883
5884 if (pos)
5885 *pos++ = '\0';
5886
5887 /* All remaining data is the svc_info string */
5888 if (pos && pos[0] && pos[0] != ' ') {
5889 len = os_strlen(pos);
5890
5891 /* Unescape in place */
5892 len = utf8_unescape(pos, len, pos, len);
5893 if (len > 0xff)
5894 return -1;
5895
5896 svc_info = pos;
5897 }
5898
5899 ref = wpas_p2p_sd_request_asp(wpa_s, dst, (u8) id,
5900 svc_str, svc_info);
b563b388
JM
5901 } else {
5902 len = os_strlen(pos);
5903 if (len & 1)
5904 return -1;
5905 len /= 2;
5906 tlvs = wpabuf_alloc(len);
5907 if (tlvs == NULL)
5908 return -1;
5909 if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
5910 wpabuf_free(tlvs);
5911 return -1;
5912 }
5913
7165c5dc 5914 ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
b563b388
JM
5915 wpabuf_free(tlvs);
5916 }
7165c5dc
JM
5917 if (ref == 0)
5918 return -1;
b563b388 5919 res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
d85e1fc8 5920 if (os_snprintf_error(buflen, res))
b563b388
JM
5921 return -1;
5922 return res;
5923}
5924
5925
5926static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
5927 char *cmd)
5928{
5929 long long unsigned val;
5930 u64 req;
5931 if (sscanf(cmd, "%llx", &val) != 1)
5932 return -1;
5933 req = val;
7165c5dc 5934 return wpas_p2p_sd_cancel_request(wpa_s, req);
b563b388
JM
5935}
5936
5937
5938static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
5939{
5940 int freq;
d25f7212 5941 u8 dst[ETH_ALEN];
b563b388
JM
5942 u8 dialog_token;
5943 struct wpabuf *resp_tlvs;
5944 char *pos, *pos2;
5945 size_t len;
5946
5947 pos = os_strchr(cmd, ' ');
5948 if (pos == NULL)
5949 return -1;
5950 *pos++ = '\0';
5951 freq = atoi(cmd);
5952 if (freq == 0)
5953 return -1;
5954
d25f7212 5955 if (hwaddr_aton(pos, dst))
b563b388 5956 return -1;
b563b388
JM
5957 pos += 17;
5958 if (*pos != ' ')
5959 return -1;
5960 pos++;
5961
5962 pos2 = os_strchr(pos, ' ');
5963 if (pos2 == NULL)
5964 return -1;
5965 *pos2++ = '\0';
5966 dialog_token = atoi(pos);
5967
5968 len = os_strlen(pos2);
5969 if (len & 1)
5970 return -1;
5971 len /= 2;
5972 resp_tlvs = wpabuf_alloc(len);
5973 if (resp_tlvs == NULL)
5974 return -1;
5975 if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
5976 wpabuf_free(resp_tlvs);
5977 return -1;
5978 }
5979
5980 wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
5981 wpabuf_free(resp_tlvs);
5982 return 0;
5983}
5984
5985
5986static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
5987 char *cmd)
5988{
28ef705d
GB
5989 if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
5990 return -1;
b563b388
JM
5991 wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
5992 return 0;
5993}
5994
5995
5996static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
5997 char *cmd)
5998{
5999 char *pos;
6000 size_t len;
6001 struct wpabuf *query, *resp;
6002
6003 pos = os_strchr(cmd, ' ');
6004 if (pos == NULL)
6005 return -1;
6006 *pos++ = '\0';
6007
6008 len = os_strlen(cmd);
6009 if (len & 1)
6010 return -1;
6011 len /= 2;
6012 query = wpabuf_alloc(len);
6013 if (query == NULL)
6014 return -1;
6015 if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
6016 wpabuf_free(query);
6017 return -1;
6018 }
6019
6020 len = os_strlen(pos);
6021 if (len & 1) {
6022 wpabuf_free(query);
6023 return -1;
6024 }
6025 len /= 2;
6026 resp = wpabuf_alloc(len);
6027 if (resp == NULL) {
6028 wpabuf_free(query);
6029 return -1;
6030 }
6031 if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
6032 wpabuf_free(query);
6033 wpabuf_free(resp);
6034 return -1;
6035 }
6036
6037 if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
6038 wpabuf_free(query);
6039 wpabuf_free(resp);
6040 return -1;
6041 }
6042 return 0;
6043}
6044
6045
6046static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
6047{
6048 char *pos;
6049 u8 version;
6050
6051 pos = os_strchr(cmd, ' ');
6052 if (pos == NULL)
6053 return -1;
6054 *pos++ = '\0';
6055
6056 if (hexstr2bin(cmd, &version, 1) < 0)
6057 return -1;
6058
6059 return wpas_p2p_service_add_upnp(wpa_s, version, pos);
6060}
6061
6062
ae9d45f3
KV
6063static int p2p_ctrl_service_add_asp(struct wpa_supplicant *wpa_s,
6064 u8 replace, char *cmd)
6065{
6066 char *pos;
6067 char *adv_str;
6068 u32 auto_accept, adv_id, svc_state, config_methods;
6069 char *svc_info = NULL;
e2b7fbf2
MS
6070 char *cpt_prio_str;
6071 u8 cpt_prio[P2PS_FEATURE_CAPAB_CPT_MAX + 1];
ae9d45f3
KV
6072
6073 pos = os_strchr(cmd, ' ');
6074 if (pos == NULL)
6075 return -1;
6076 *pos++ = '\0';
6077
6078 /* Auto-Accept value is mandatory, and must be one of the
6079 * single values (0, 1, 2, 4) */
6080 auto_accept = atoi(cmd);
6081 switch (auto_accept) {
6082 case P2PS_SETUP_NONE: /* No auto-accept */
6083 case P2PS_SETUP_NEW:
6084 case P2PS_SETUP_CLIENT:
6085 case P2PS_SETUP_GROUP_OWNER:
6086 break;
6087 default:
6088 return -1;
6089 }
6090
6091 /* Advertisement ID is mandatory */
6092 cmd = pos;
6093 pos = os_strchr(cmd, ' ');
6094 if (pos == NULL)
6095 return -1;
6096 *pos++ = '\0';
6097
6098 /* Handle Adv_ID == 0 (wildcard "org.wi-fi.wfds") internally. */
6099 if (sscanf(cmd, "%x", &adv_id) != 1 || adv_id == 0)
6100 return -1;
6101
6102 /* Only allow replacements if exist, and adds if not */
6103 if (wpas_p2p_service_p2ps_id_exists(wpa_s, adv_id)) {
6104 if (!replace)
6105 return -1;
6106 } else {
6107 if (replace)
6108 return -1;
6109 }
6110
6111 /* svc_state between 0 - 0xff is mandatory */
6112 if (sscanf(pos, "%x", &svc_state) != 1 || svc_state > 0xff)
6113 return -1;
6114
6115 pos = os_strchr(pos, ' ');
6116 if (pos == NULL)
6117 return -1;
6118
6119 /* config_methods is mandatory */
6120 pos++;
6121 if (sscanf(pos, "%x", &config_methods) != 1)
6122 return -1;
6123
6124 if (!(config_methods &
6125 (WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD | WPS_CONFIG_P2PS)))
6126 return -1;
6127
6128 pos = os_strchr(pos, ' ');
6129 if (pos == NULL)
6130 return -1;
6131
6132 pos++;
6133 adv_str = pos;
6134
6135 /* Advertisement string is mandatory */
6136 if (!pos[0] || pos[0] == ' ')
6137 return -1;
6138
6139 /* Terminate svc string */
6140 pos = os_strchr(pos, ' ');
6141 if (pos != NULL)
6142 *pos++ = '\0';
6143
e2b7fbf2
MS
6144 cpt_prio_str = (pos && pos[0]) ? os_strstr(pos, "cpt=") : NULL;
6145 if (cpt_prio_str) {
6146 pos = os_strchr(pos, ' ');
6147 if (pos != NULL)
6148 *pos++ = '\0';
6149
6150 if (p2ps_ctrl_parse_cpt_priority(cpt_prio_str + 4, cpt_prio))
6151 return -1;
6152 } else {
6153 cpt_prio[0] = P2PS_FEATURE_CAPAB_UDP_TRANSPORT;
6154 cpt_prio[1] = 0;
6155 }
6156
ae9d45f3
KV
6157 /* Service and Response Information are optional */
6158 if (pos && pos[0]) {
6159 size_t len;
6160
6161 /* Note the bare ' included, which cannot exist legally
6162 * in unescaped string. */
6163 svc_info = os_strstr(pos, "svc_info='");
6164
6165 if (svc_info) {
6166 svc_info += 9;
6167 len = os_strlen(svc_info);
6168 utf8_unescape(svc_info, len, svc_info, len);
6169 }
6170 }
6171
6172 return wpas_p2p_service_add_asp(wpa_s, auto_accept, adv_id, adv_str,
6173 (u8) svc_state, (u16) config_methods,
e2b7fbf2 6174 svc_info, cpt_prio);
ae9d45f3
KV
6175}
6176
6177
b563b388
JM
6178static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
6179{
6180 char *pos;
6181
6182 pos = os_strchr(cmd, ' ');
6183 if (pos == NULL)
6184 return -1;
6185 *pos++ = '\0';
6186
6187 if (os_strcmp(cmd, "bonjour") == 0)
6188 return p2p_ctrl_service_add_bonjour(wpa_s, pos);
6189 if (os_strcmp(cmd, "upnp") == 0)
6190 return p2p_ctrl_service_add_upnp(wpa_s, pos);
ae9d45f3
KV
6191 if (os_strcmp(cmd, "asp") == 0)
6192 return p2p_ctrl_service_add_asp(wpa_s, 0, pos);
b563b388
JM
6193 wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
6194 return -1;
6195}
6196
6197
6198static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
6199 char *cmd)
6200{
6201 size_t len;
6202 struct wpabuf *query;
6203 int ret;
6204
6205 len = os_strlen(cmd);
6206 if (len & 1)
6207 return -1;
6208 len /= 2;
6209 query = wpabuf_alloc(len);
6210 if (query == NULL)
6211 return -1;
6212 if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
6213 wpabuf_free(query);
6214 return -1;
6215 }
6216
6217 ret = wpas_p2p_service_del_bonjour(wpa_s, query);
6218 wpabuf_free(query);
6219 return ret;
6220}
6221
6222
6223static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
6224{
6225 char *pos;
6226 u8 version;
6227
6228 pos = os_strchr(cmd, ' ');
6229 if (pos == NULL)
6230 return -1;
6231 *pos++ = '\0';
6232
6233 if (hexstr2bin(cmd, &version, 1) < 0)
6234 return -1;
6235
6236 return wpas_p2p_service_del_upnp(wpa_s, version, pos);
6237}
6238
6239
ae9d45f3
KV
6240static int p2p_ctrl_service_del_asp(struct wpa_supplicant *wpa_s, char *cmd)
6241{
6242 u32 adv_id;
6243
e9d28050
MS
6244 if (os_strcmp(cmd, "all") == 0) {
6245 wpas_p2p_service_flush_asp(wpa_s);
6246 return 0;
6247 }
6248
ae9d45f3
KV
6249 if (sscanf(cmd, "%x", &adv_id) != 1)
6250 return -1;
6251
6252 return wpas_p2p_service_del_asp(wpa_s, adv_id);
6253}
6254
6255
b563b388
JM
6256static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
6257{
6258 char *pos;
6259
6260 pos = os_strchr(cmd, ' ');
6261 if (pos == NULL)
6262 return -1;
6263 *pos++ = '\0';
6264
6265 if (os_strcmp(cmd, "bonjour") == 0)
6266 return p2p_ctrl_service_del_bonjour(wpa_s, pos);
6267 if (os_strcmp(cmd, "upnp") == 0)
6268 return p2p_ctrl_service_del_upnp(wpa_s, pos);
ae9d45f3
KV
6269 if (os_strcmp(cmd, "asp") == 0)
6270 return p2p_ctrl_service_del_asp(wpa_s, pos);
6271 wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
6272 return -1;
6273}
6274
6275
6276static int p2p_ctrl_service_replace(struct wpa_supplicant *wpa_s, char *cmd)
6277{
6278 char *pos;
6279
6280 pos = os_strchr(cmd, ' ');
6281 if (pos == NULL)
6282 return -1;
6283 *pos++ = '\0';
6284
6285 if (os_strcmp(cmd, "asp") == 0)
6286 return p2p_ctrl_service_add_asp(wpa_s, 1, pos);
6287
b563b388
JM
6288 wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
6289 return -1;
6290}
6291
6292
6293static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
6294{
6295 u8 addr[ETH_ALEN];
6296
6297 /* <addr> */
6298
6299 if (hwaddr_aton(cmd, addr))
6300 return -1;
6301
6302 return wpas_p2p_reject(wpa_s, addr);
6303}
6304
6305
6306static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
6307{
6308 char *pos;
6309 int id;
6310 struct wpa_ssid *ssid;
54c61e6e 6311 u8 *_peer = NULL, peer[ETH_ALEN];
f5877af0 6312 int freq = 0, pref_freq = 0;
5a3319ab 6313 int ht40, vht, he, max_oper_chwidth, chwidth = 0, freq2 = 0;
b563b388
JM
6314
6315 id = atoi(cmd);
6316 pos = os_strstr(cmd, " peer=");
6317 if (pos) {
6318 pos += 6;
6319 if (hwaddr_aton(pos, peer))
6320 return -1;
54c61e6e 6321 _peer = peer;
b563b388
JM
6322 }
6323 ssid = wpa_config_get_network(wpa_s->conf, id);
6324 if (ssid == NULL || ssid->disabled != 2) {
6325 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
6326 "for persistent P2P group",
6327 id);
6328 return -1;
6329 }
6330
4d32c0c4
JM
6331 pos = os_strstr(cmd, " freq=");
6332 if (pos) {
6333 pos += 6;
6334 freq = atoi(pos);
6335 if (freq <= 0)
6336 return -1;
6337 }
6338
f5877af0
JM
6339 pos = os_strstr(cmd, " pref=");
6340 if (pos) {
6341 pos += 6;
6342 pref_freq = atoi(pos);
6343 if (pref_freq <= 0)
6344 return -1;
6345 }
6346
20ea1ca4
EP
6347 vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
6348 ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
6349 vht;
5a3319ab 6350 he = (os_strstr(cmd, " he") != NULL) || wpa_s->conf->p2p_go_he;
4d32c0c4 6351
c27f4c90
AK
6352 pos = os_strstr(cmd, "freq2=");
6353 if (pos)
6354 freq2 = atoi(pos + 6);
6355
6356 pos = os_strstr(cmd, " max_oper_chwidth=");
6357 if (pos)
6358 chwidth = atoi(pos + 18);
6359
6360 max_oper_chwidth = parse_freq(chwidth, freq2);
6361 if (max_oper_chwidth < 0)
6362 return -1;
6363
6364 return wpas_p2p_invite(wpa_s, _peer, ssid, NULL, freq, freq2, ht40, vht,
5a3319ab 6365 max_oper_chwidth, pref_freq, he);
b563b388
JM
6366}
6367
6368
6369static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
6370{
6371 char *pos;
6372 u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
6373
6374 pos = os_strstr(cmd, " peer=");
6375 if (!pos)
6376 return -1;
6377
6378 *pos = '\0';
6379 pos += 6;
6380 if (hwaddr_aton(pos, peer)) {
6381 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
6382 return -1;
6383 }
6384
6385 pos = os_strstr(pos, " go_dev_addr=");
6386 if (pos) {
6387 pos += 13;
6388 if (hwaddr_aton(pos, go_dev_addr)) {
6389 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
6390 pos);
6391 return -1;
6392 }
6393 go_dev = go_dev_addr;
6394 }
6395
6396 return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
6397}
6398
6399
6400static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
6401{
6402 if (os_strncmp(cmd, "persistent=", 11) == 0)
6403 return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
6404 if (os_strncmp(cmd, "group=", 6) == 0)
6405 return p2p_ctrl_invite_group(wpa_s, cmd + 6);
6406
6407 return -1;
6408}
6409
6410
6411static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
c27f4c90 6412 int id, int freq, int vht_center_freq2,
5a3319ab
PX
6413 int ht40, int vht, int vht_chwidth,
6414 int he)
b563b388 6415{
b563b388
JM
6416 struct wpa_ssid *ssid;
6417
b563b388
JM
6418 ssid = wpa_config_get_network(wpa_s->conf, id);
6419 if (ssid == NULL || ssid->disabled != 2) {
6420 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
6421 "for persistent P2P group",
6422 id);
6423 return -1;
6424 }
6425
c27f4c90
AK
6426 return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq,
6427 vht_center_freq2, 0, ht40, vht,
5a3319ab 6428 vht_chwidth, he, NULL, 0, 0);
b563b388
JM
6429}
6430
6431
6432static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
6433{
29292d53
EP
6434 int freq = 0, persistent = 0, group_id = -1;
6435 int vht = wpa_s->conf->p2p_go_vht;
6436 int ht40 = wpa_s->conf->p2p_go_ht40 || vht;
5a3319ab 6437 int he = wpa_s->conf->p2p_go_he;
c27f4c90 6438 int max_oper_chwidth, chwidth = 0, freq2 = 0;
29292d53 6439 char *token, *context = NULL;
37ed3254
SD
6440#ifdef CONFIG_ACS
6441 int acs = 0;
6442#endif /* CONFIG_ACS */
e11776a5 6443
29292d53 6444 while ((token = str_token(cmd, " ", &context))) {
37ed3254 6445 if (sscanf(token, "freq2=%d", &freq2) == 1 ||
c27f4c90
AK
6446 sscanf(token, "persistent=%d", &group_id) == 1 ||
6447 sscanf(token, "max_oper_chwidth=%d", &chwidth) == 1) {
29292d53 6448 continue;
37ed3254
SD
6449#ifdef CONFIG_ACS
6450 } else if (os_strcmp(token, "freq=acs") == 0) {
6451 acs = 1;
6452#endif /* CONFIG_ACS */
6453 } else if (sscanf(token, "freq=%d", &freq) == 1) {
6454 continue;
29292d53
EP
6455 } else if (os_strcmp(token, "ht40") == 0) {
6456 ht40 = 1;
6457 } else if (os_strcmp(token, "vht") == 0) {
6458 vht = 1;
6459 ht40 = 1;
5a3319ab
PX
6460 } else if (os_strcmp(token, "he") == 0) {
6461 he = 1;
29292d53
EP
6462 } else if (os_strcmp(token, "persistent") == 0) {
6463 persistent = 1;
6464 } else {
6465 wpa_printf(MSG_DEBUG,
6466 "CTRL: Invalid P2P_GROUP_ADD parameter: '%s'",
6467 token);
6468 return -1;
6469 }
e11776a5
PK
6470 }
6471
37ed3254
SD
6472#ifdef CONFIG_ACS
6473 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_ACS_OFFLOAD) &&
6474 (acs || freq == 2 || freq == 5)) {
6475 if (freq == 2 && wpa_s->best_24_freq <= 0) {
6476 wpa_s->p2p_go_acs_band = HOSTAPD_MODE_IEEE80211G;
6477 wpa_s->p2p_go_do_acs = 1;
6478 freq = 0;
6479 } else if (freq == 5 && wpa_s->best_5_freq <= 0) {
6480 wpa_s->p2p_go_acs_band = HOSTAPD_MODE_IEEE80211A;
6481 wpa_s->p2p_go_do_acs = 1;
6482 freq = 0;
6483 } else {
6484 wpa_s->p2p_go_acs_band = HOSTAPD_MODE_IEEE80211ANY;
6485 wpa_s->p2p_go_do_acs = 1;
6486 }
2b7fa035
JS
6487 } else {
6488 wpa_s->p2p_go_do_acs = 0;
37ed3254
SD
6489 }
6490#endif /* CONFIG_ACS */
6491
c27f4c90
AK
6492 max_oper_chwidth = parse_freq(chwidth, freq2);
6493 if (max_oper_chwidth < 0)
6494 return -1;
6495
29292d53
EP
6496 if (group_id >= 0)
6497 return p2p_ctrl_group_add_persistent(wpa_s, group_id,
c27f4c90 6498 freq, freq2, ht40, vht,
5a3319ab 6499 max_oper_chwidth, he);
29292d53 6500
c27f4c90 6501 return wpas_p2p_group_add(wpa_s, persistent, freq, freq2, ht40, vht,
5a3319ab 6502 max_oper_chwidth, he);
b563b388
JM
6503}
6504
6505
57b38882
PK
6506static int p2p_ctrl_group_member(struct wpa_supplicant *wpa_s, const char *cmd,
6507 char *buf, size_t buflen)
6508{
6509 u8 dev_addr[ETH_ALEN];
6510 struct wpa_ssid *ssid;
6511 int res;
6512 const u8 *iaddr;
6513
6514 ssid = wpa_s->current_ssid;
6515 if (!wpa_s->global->p2p || !ssid || ssid->mode != WPAS_MODE_P2P_GO ||
6516 hwaddr_aton(cmd, dev_addr))
6517 return -1;
6518
6519 iaddr = p2p_group_get_client_interface_addr(wpa_s->p2p_group, dev_addr);
6520 if (!iaddr)
6521 return -1;
6522 res = os_snprintf(buf, buflen, MACSTR, MAC2STR(iaddr));
6523 if (os_snprintf_error(buflen, res))
6524 return -1;
6525 return res;
6526}
6527
6528
f47f9361
SD
6529static int wpas_find_p2p_dev_addr_bss(struct wpa_global *global,
6530 const u8 *p2p_dev_addr)
6531{
6532 struct wpa_supplicant *wpa_s;
6533
6534 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6535 if (wpa_bss_get_p2p_dev_addr(wpa_s, p2p_dev_addr))
6536 return 1;
6537 }
6538
6539 return 0;
6540}
6541
6542
b563b388
JM
6543static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
6544 char *buf, size_t buflen)
6545{
f47f9361 6546 u8 addr[ETH_ALEN], *addr_ptr, group_capab;
b3ffc80b
JM
6547 int next, res;
6548 const struct p2p_peer_info *info;
6549 char *pos, *end;
6550 char devtype[WPS_DEV_TYPE_BUFSIZE];
87f841a1 6551 struct wpa_ssid *ssid;
f3989ced 6552 size_t i;
b563b388
JM
6553
6554 if (!wpa_s->global->p2p)
6555 return -1;
6556
6557 if (os_strcmp(cmd, "FIRST") == 0) {
6558 addr_ptr = NULL;
6559 next = 0;
6560 } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
6561 if (hwaddr_aton(cmd + 5, addr) < 0)
6562 return -1;
6563 addr_ptr = addr;
6564 next = 1;
6565 } else {
6566 if (hwaddr_aton(cmd, addr) < 0)
6567 return -1;
6568 addr_ptr = addr;
6569 next = 0;
6570 }
6571
b3ffc80b
JM
6572 info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
6573 if (info == NULL)
6574 return -1;
f47f9361
SD
6575 group_capab = info->group_capab;
6576
6577 if (group_capab &&
6578 !wpas_find_p2p_dev_addr_bss(wpa_s->global, info->p2p_device_addr)) {
6579 wpa_printf(MSG_DEBUG,
6580 "P2P: Could not find any BSS with p2p_dev_addr "
6581 MACSTR ", hence override group_capab from 0x%x to 0",
6582 MAC2STR(info->p2p_device_addr), group_capab);
6583 group_capab = 0;
6584 }
b3ffc80b
JM
6585
6586 pos = buf;
6587 end = buf + buflen;
6588
6589 res = os_snprintf(pos, end - pos, MACSTR "\n"
6590 "pri_dev_type=%s\n"
6591 "device_name=%s\n"
6592 "manufacturer=%s\n"
6593 "model_name=%s\n"
6594 "model_number=%s\n"
6595 "serial_number=%s\n"
6596 "config_methods=0x%x\n"
6597 "dev_capab=0x%x\n"
6598 "group_capab=0x%x\n"
6599 "level=%d\n",
6600 MAC2STR(info->p2p_device_addr),
6601 wps_dev_type_bin2str(info->pri_dev_type,
6602 devtype, sizeof(devtype)),
6603 info->device_name,
6604 info->manufacturer,
6605 info->model_name,
6606 info->model_number,
6607 info->serial_number,
6608 info->config_methods,
6609 info->dev_capab,
f47f9361 6610 group_capab,
b3ffc80b 6611 info->level);
d85e1fc8 6612 if (os_snprintf_error(end - pos, res))
b3ffc80b
JM
6613 return pos - buf;
6614 pos += res;
6615
f3989ced
JM
6616 for (i = 0; i < info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN; i++)
6617 {
6618 const u8 *t;
6619 t = &info->wps_sec_dev_type_list[i * WPS_DEV_TYPE_LEN];
6620 res = os_snprintf(pos, end - pos, "sec_dev_type=%s\n",
6621 wps_dev_type_bin2str(t, devtype,
6622 sizeof(devtype)));
d85e1fc8 6623 if (os_snprintf_error(end - pos, res))
f3989ced
JM
6624 return pos - buf;
6625 pos += res;
6626 }
6627
c427ac92 6628 ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
87f841a1
JM
6629 if (ssid) {
6630 res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
d85e1fc8 6631 if (os_snprintf_error(end - pos, res))
87f841a1
JM
6632 return pos - buf;
6633 pos += res;
6634 }
6635
b3ffc80b
JM
6636 res = p2p_get_peer_info_txt(info, pos, end - pos);
6637 if (res < 0)
87f841a1 6638 return pos - buf;
b3ffc80b
JM
6639 pos += res;
6640
71a0e395
JM
6641 if (info->vendor_elems) {
6642 res = os_snprintf(pos, end - pos, "vendor_elems=");
d85e1fc8 6643 if (os_snprintf_error(end - pos, res))
71a0e395
JM
6644 return pos - buf;
6645 pos += res;
6646
6647 pos += wpa_snprintf_hex(pos, end - pos,
6648 wpabuf_head(info->vendor_elems),
6649 wpabuf_len(info->vendor_elems));
6650
6651 res = os_snprintf(pos, end - pos, "\n");
d85e1fc8 6652 if (os_snprintf_error(end - pos, res))
71a0e395
JM
6653 return pos - buf;
6654 pos += res;
6655 }
6656
b3ffc80b 6657 return pos - buf;
b563b388
JM
6658}
6659
6660
6f3bc72b
JM
6661static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
6662 const char *param)
6663{
af8a827b 6664 unsigned int i;
6f3bc72b
JM
6665
6666 if (wpa_s->global->p2p == NULL)
6667 return -1;
6668
af8a827b
JM
6669 if (freq_range_list_parse(&wpa_s->global->p2p_disallow_freq, param) < 0)
6670 return -1;
6f3bc72b 6671
af8a827b
JM
6672 for (i = 0; i < wpa_s->global->p2p_disallow_freq.num; i++) {
6673 struct wpa_freq_range *freq;
6674 freq = &wpa_s->global->p2p_disallow_freq.range[i];
6f3bc72b 6675 wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
af8a827b 6676 freq->min, freq->max);
6f3bc72b
JM
6677 }
6678
3a8f008a 6679 wpas_p2p_update_channel_list(wpa_s, WPAS_P2P_CHANNEL_UPDATE_DISALLOW);
6f3bc72b
JM
6680 return 0;
6681}
6682
6683
b563b388
JM
6684static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
6685{
6686 char *param;
6687
6688 if (wpa_s->global->p2p == NULL)
6689 return -1;
6690
6691 param = os_strchr(cmd, ' ');
6692 if (param == NULL)
6693 return -1;
6694 *param++ = '\0';
6695
6696 if (os_strcmp(cmd, "discoverability") == 0) {
6697 p2p_set_client_discoverability(wpa_s->global->p2p,
6698 atoi(param));
6699 return 0;
6700 }
6701
6702 if (os_strcmp(cmd, "managed") == 0) {
6703 p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
6704 return 0;
6705 }
6706
6707 if (os_strcmp(cmd, "listen_channel") == 0) {
dfe0745c
LD
6708 char *pos;
6709 u8 channel, op_class;
6710
6711 channel = atoi(param);
6712 pos = os_strchr(param, ' ');
6713 op_class = pos ? atoi(pos) : 81;
6714
6715 return p2p_set_listen_channel(wpa_s->global->p2p, op_class,
6716 channel, 1);
b563b388
JM
6717 }
6718
6719 if (os_strcmp(cmd, "ssid_postfix") == 0) {
6720 return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
6721 os_strlen(param));
6722 }
6723
6724 if (os_strcmp(cmd, "noa") == 0) {
6725 char *pos;
6726 int count, start, duration;
6727 /* GO NoA parameters: count,start_offset(ms),duration(ms) */
6728 count = atoi(param);
6729 pos = os_strchr(param, ',');
6730 if (pos == NULL)
6731 return -1;
6732 pos++;
6733 start = atoi(pos);
6734 pos = os_strchr(pos, ',');
6735 if (pos == NULL)
6736 return -1;
6737 pos++;
6738 duration = atoi(pos);
6739 if (count < 0 || count > 255 || start < 0 || duration < 0)
6740 return -1;
6741 if (count == 0 && duration > 0)
6742 return -1;
6743 wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
6744 "start=%d duration=%d", count, start, duration);
aefb53bd 6745 return wpas_p2p_set_noa(wpa_s, count, start, duration);
b563b388
JM
6746 }
6747
c381508d
JM
6748 if (os_strcmp(cmd, "ps") == 0)
6749 return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
6750
6751 if (os_strcmp(cmd, "oppps") == 0)
6752 return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
6753
6754 if (os_strcmp(cmd, "ctwindow") == 0)
6755 return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
6756
b563b388
JM
6757 if (os_strcmp(cmd, "disabled") == 0) {
6758 wpa_s->global->p2p_disabled = atoi(param);
6759 wpa_printf(MSG_DEBUG, "P2P functionality %s",
6760 wpa_s->global->p2p_disabled ?
6761 "disabled" : "enabled");
6762 if (wpa_s->global->p2p_disabled) {
6763 wpas_p2p_stop_find(wpa_s);
108def93 6764 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
b563b388
JM
6765 p2p_flush(wpa_s->global->p2p);
6766 }
6767 return 0;
6768 }
6769
b9cfc09a
JJ
6770 if (os_strcmp(cmd, "conc_pref") == 0) {
6771 if (os_strcmp(param, "sta") == 0)
6772 wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
6773 else if (os_strcmp(param, "p2p") == 0)
6774 wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
6775 else {
6776 wpa_printf(MSG_INFO, "Invalid conc_pref value");
6777 return -1;
6778 }
6779 wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
6780 "%s", param);
6781 return 0;
6782 }
6783
6e6963ea
JM
6784 if (os_strcmp(cmd, "force_long_sd") == 0) {
6785 wpa_s->force_long_sd = atoi(param);
6786 return 0;
6787 }
6788
80c9582a
JM
6789 if (os_strcmp(cmd, "peer_filter") == 0) {
6790 u8 addr[ETH_ALEN];
6791 if (hwaddr_aton(param, addr))
6792 return -1;
6793 p2p_set_peer_filter(wpa_s->global->p2p, addr);
6794 return 0;
6795 }
6796
72044390
JM
6797 if (os_strcmp(cmd, "cross_connect") == 0)
6798 return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
6799
eea2fd9e
JM
6800 if (os_strcmp(cmd, "go_apsd") == 0) {
6801 if (os_strcmp(param, "disable") == 0)
6802 wpa_s->set_ap_uapsd = 0;
6803 else {
6804 wpa_s->set_ap_uapsd = 1;
6805 wpa_s->ap_uapsd = atoi(param);
6806 }
6807 return 0;
6808 }
6809
6810 if (os_strcmp(cmd, "client_apsd") == 0) {
6811 if (os_strcmp(param, "disable") == 0)
6812 wpa_s->set_sta_uapsd = 0;
6813 else {
6814 int be, bk, vi, vo;
6815 char *pos;
6816 /* format: BE,BK,VI,VO;max SP Length */
6817 be = atoi(param);
6818 pos = os_strchr(param, ',');
6819 if (pos == NULL)
6820 return -1;
6821 pos++;
6822 bk = atoi(pos);
6823 pos = os_strchr(pos, ',');
6824 if (pos == NULL)
6825 return -1;
6826 pos++;
6827 vi = atoi(pos);
6828 pos = os_strchr(pos, ',');
6829 if (pos == NULL)
6830 return -1;
6831 pos++;
6832 vo = atoi(pos);
6833 /* ignore max SP Length for now */
6834
6835 wpa_s->set_sta_uapsd = 1;
6836 wpa_s->sta_uapsd = 0;
6837 if (be)
6838 wpa_s->sta_uapsd |= BIT(0);
6839 if (bk)
6840 wpa_s->sta_uapsd |= BIT(1);
6841 if (vi)
6842 wpa_s->sta_uapsd |= BIT(2);
6843 if (vo)
6844 wpa_s->sta_uapsd |= BIT(3);
6845 }
6846 return 0;
6847 }
6848
6f3bc72b
JM
6849 if (os_strcmp(cmd, "disallow_freq") == 0)
6850 return p2p_ctrl_disallow_freq(wpa_s, param);
6851
96beff11
JM
6852 if (os_strcmp(cmd, "disc_int") == 0) {
6853 int min_disc_int, max_disc_int, max_disc_tu;
6854 char *pos;
6855
6856 pos = param;
6857
6858 min_disc_int = atoi(pos);
6859 pos = os_strchr(pos, ' ');
6860 if (pos == NULL)
6861 return -1;
6862 *pos++ = '\0';
6863
6864 max_disc_int = atoi(pos);
6865 pos = os_strchr(pos, ' ');
6866 if (pos == NULL)
6867 return -1;
6868 *pos++ = '\0';
6869
6870 max_disc_tu = atoi(pos);
6871
6872 return p2p_set_disc_int(wpa_s->global->p2p, min_disc_int,
6873 max_disc_int, max_disc_tu);
6874 }
6875
05766ed8
JM
6876 if (os_strcmp(cmd, "per_sta_psk") == 0) {
6877 wpa_s->global->p2p_per_sta_psk = !!atoi(param);
6878 return 0;
6879 }
6880
c4f87a70
JM
6881#ifdef CONFIG_WPS_NFC
6882 if (os_strcmp(cmd, "nfc_tag") == 0)
6883 return wpas_p2p_nfc_tag_enabled(wpa_s, !!atoi(param));
6884#endif /* CONFIG_WPS_NFC */
6885
201b0f5f
JM
6886 if (os_strcmp(cmd, "disable_ip_addr_req") == 0) {
6887 wpa_s->p2p_disable_ip_addr_req = !!atoi(param);
6888 return 0;
6889 }
6890
3a7819f0
JM
6891 if (os_strcmp(cmd, "override_pref_op_chan") == 0) {
6892 int op_class, chan;
6893
6894 op_class = atoi(param);
6895 param = os_strchr(param, ':');
6896 if (!param)
6897 return -1;
6898 param++;
6899 chan = atoi(param);
6900 p2p_set_override_pref_op_chan(wpa_s->global->p2p, op_class,
6901 chan);
6902 return 0;
6903 }
6904
b563b388
JM
6905 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
6906 cmd);
6907
6908 return -1;
6909}
6910
6911
acb54643
JM
6912static void p2p_ctrl_flush(struct wpa_supplicant *wpa_s)
6913{
6914 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
6915 wpa_s->force_long_sd = 0;
d02e4c8a
AO
6916
6917#ifdef CONFIG_TESTING_OPTIONS
6918 os_free(wpa_s->get_pref_freq_list_override);
6919 wpa_s->get_pref_freq_list_override = NULL;
6920#endif /* CONFIG_TESTING_OPTIONS */
6921
477b082c 6922 wpas_p2p_stop_find(wpa_s);
b9da88d6 6923 wpa_s->parent->p2ps_method_config_any = 0;
acb54643
JM
6924 if (wpa_s->global->p2p)
6925 p2p_flush(wpa_s->global->p2p);
6926}
6927
6928
b563b388
JM
6929static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
6930{
6931 char *pos, *pos2;
6932 unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
6933
6934 if (cmd[0]) {
6935 pos = os_strchr(cmd, ' ');
6936 if (pos == NULL)
6937 return -1;
6938 *pos++ = '\0';
6939 dur1 = atoi(cmd);
6940
6941 pos2 = os_strchr(pos, ' ');
6942 if (pos2)
6943 *pos2++ = '\0';
6944 int1 = atoi(pos);
6945 } else
6946 pos2 = NULL;
6947
6948 if (pos2) {
6949 pos = os_strchr(pos2, ' ');
6950 if (pos == NULL)
6951 return -1;
6952 *pos++ = '\0';
6953 dur2 = atoi(pos2);
6954 int2 = atoi(pos);
6955 }
6956
6957 return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
6958}
6959
6960
6961static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
6962{
6963 char *pos;
6964 unsigned int period = 0, interval = 0;
6965
6966 if (cmd[0]) {
6967 pos = os_strchr(cmd, ' ');
6968 if (pos == NULL)
6969 return -1;
6970 *pos++ = '\0';
6971 period = atoi(cmd);
6972 interval = atoi(pos);
6973 }
6974
6975 return wpas_p2p_ext_listen(wpa_s, period, interval);
6976}
6977
f2c56602
JM
6978
6979static int p2p_ctrl_remove_client(struct wpa_supplicant *wpa_s, const char *cmd)
6980{
6981 const char *pos;
6982 u8 peer[ETH_ALEN];
6983 int iface_addr = 0;
6984
6985 pos = cmd;
6986 if (os_strncmp(pos, "iface=", 6) == 0) {
6987 iface_addr = 1;
6988 pos += 6;
6989 }
6990 if (hwaddr_aton(pos, peer))
6991 return -1;
6992
6993 wpas_p2p_remove_client(wpa_s, peer, iface_addr);
6994 return 0;
6995}
6996
a6f5b193
PX
6997
6998static int p2p_ctrl_iface_p2p_lo_start(struct wpa_supplicant *wpa_s, char *cmd)
6999{
7000 int freq = 0, period = 0, interval = 0, count = 0;
7001
7002 if (sscanf(cmd, "%d %d %d %d", &freq, &period, &interval, &count) != 4)
7003 {
7004 wpa_printf(MSG_DEBUG,
7005 "CTRL: Invalid P2P LO Start parameter: '%s'", cmd);
7006 return -1;
7007 }
7008
7009 return wpas_p2p_lo_start(wpa_s, freq, period, interval, count);
7010}
7011
b563b388
JM
7012#endif /* CONFIG_P2P */
7013
7014
356d1488
JM
7015static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s, char *val)
7016{
7017 struct wpa_freq_range_list ranges;
7018 int *freqs = NULL;
7019 struct hostapd_hw_modes *mode;
7020 u16 i;
7021
7022 if (wpa_s->hw.modes == NULL)
7023 return NULL;
7024
7025 os_memset(&ranges, 0, sizeof(ranges));
7026 if (freq_range_list_parse(&ranges, val) < 0)
7027 return NULL;
7028
7029 for (i = 0; i < wpa_s->hw.num_modes; i++) {
7030 int j;
7031
7032 mode = &wpa_s->hw.modes[i];
7033 for (j = 0; j < mode->num_channels; j++) {
7034 unsigned int freq;
7035
7036 if (mode->channels[j].flag & HOSTAPD_CHAN_DISABLED)
7037 continue;
7038
7039 freq = mode->channels[j].freq;
7040 if (!freq_range_list_includes(&ranges, freq))
7041 continue;
7042
7043 int_array_add_unique(&freqs, freq);
7044 }
7045 }
7046
7047 os_free(ranges.range);
7048 return freqs;
7049}
7050
7051
afc064fe 7052#ifdef CONFIG_INTERWORKING
356d1488
JM
7053
7054static int ctrl_interworking_select(struct wpa_supplicant *wpa_s, char *param)
7055{
7056 int auto_sel = 0;
7057 int *freqs = NULL;
7058
7059 if (param) {
7060 char *pos;
7061
7062 auto_sel = os_strstr(param, "auto") != NULL;
7063
7064 pos = os_strstr(param, "freq=");
7065 if (pos) {
7066 freqs = freq_range_to_channel_list(wpa_s, pos + 5);
7067 if (freqs == NULL)
7068 return -1;
7069 }
7070
7071 }
7072
7073 return interworking_select(wpa_s, auto_sel, freqs);
7074}
7075
7076
f91a512f
JM
7077static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst,
7078 int only_add)
b02fe7ff
JM
7079{
7080 u8 bssid[ETH_ALEN];
7081 struct wpa_bss *bss;
7082
7083 if (hwaddr_aton(dst, bssid)) {
7084 wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
7085 return -1;
7086 }
7087
7088 bss = wpa_bss_get_bssid(wpa_s, bssid);
7089 if (bss == NULL) {
7090 wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
7091 MAC2STR(bssid));
7092 return -1;
7093 }
7094
783b2a97
JM
7095 if (bss->ssid_len == 0) {
7096 int found = 0;
7097
7098 wpa_printf(MSG_DEBUG, "Selected BSS entry for " MACSTR
7099 " does not have SSID information", MAC2STR(bssid));
7100
7101 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss,
7102 list) {
7103 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0 &&
7104 bss->ssid_len > 0) {
7105 found = 1;
7106 break;
7107 }
7108 }
7109
7110 if (!found)
7111 return -1;
7112 wpa_printf(MSG_DEBUG,
7113 "Found another matching BSS entry with SSID");
7114 }
7115
f91a512f 7116 return interworking_connect(wpa_s, bss, only_add);
b02fe7ff
JM
7117}
7118
7119
afc064fe
JM
7120static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
7121{
7122 u8 dst_addr[ETH_ALEN];
7123 int used;
7124 char *pos;
7125#define MAX_ANQP_INFO_ID 100
7126 u16 id[MAX_ANQP_INFO_ID];
7127 size_t num_id = 0;
cf28c66b 7128 u32 subtypes = 0;
2316cb35 7129 u32 mbo_subtypes = 0;
afc064fe
JM
7130
7131 used = hwaddr_aton2(dst, dst_addr);
7132 if (used < 0)
7133 return -1;
7134 pos = dst + used;
b68d602d
JM
7135 if (*pos == ' ')
7136 pos++;
afc064fe 7137 while (num_id < MAX_ANQP_INFO_ID) {
cf28c66b
DS
7138 if (os_strncmp(pos, "hs20:", 5) == 0) {
7139#ifdef CONFIG_HS20
7140 int num = atoi(pos + 5);
7141 if (num <= 0 || num > 31)
7142 return -1;
7143 subtypes |= BIT(num);
7144#else /* CONFIG_HS20 */
7145 return -1;
7146#endif /* CONFIG_HS20 */
8f479174 7147 } else if (os_strncmp(pos, "mbo:", 4) == 0) {
7148#ifdef CONFIG_MBO
7149 int num = atoi(pos + 4);
2316cb35
AS
7150
7151 if (num <= 0 || num > MAX_MBO_ANQP_SUBTYPE)
8f479174 7152 return -1;
2316cb35 7153 mbo_subtypes |= BIT(num);
8f479174 7154#else /* CONFIG_MBO */
7155 return -1;
7156#endif /* CONFIG_MBO */
cf28c66b
DS
7157 } else {
7158 id[num_id] = atoi(pos);
7159 if (id[num_id])
7160 num_id++;
7161 }
afc064fe
JM
7162 pos = os_strchr(pos + 1, ',');
7163 if (pos == NULL)
7164 break;
7165 pos++;
7166 }
7167
8ecf2231 7168 if (num_id == 0 && !subtypes && !mbo_subtypes)
afc064fe
JM
7169 return -1;
7170
8f479174 7171 return anqp_send_req(wpa_s, dst_addr, id, num_id, subtypes,
2316cb35 7172 mbo_subtypes);
afc064fe 7173}
b1f12296
JM
7174
7175
7176static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
7177{
7178 u8 dst_addr[ETH_ALEN];
7179 struct wpabuf *advproto, *query = NULL;
7180 int used, ret = -1;
7181 char *pos, *end;
7182 size_t len;
7183
7184 used = hwaddr_aton2(cmd, dst_addr);
7185 if (used < 0)
7186 return -1;
7187
7188 pos = cmd + used;
7189 while (*pos == ' ')
7190 pos++;
7191
7192 /* Advertisement Protocol ID */
7193 end = os_strchr(pos, ' ');
7194 if (end)
7195 len = end - pos;
7196 else
7197 len = os_strlen(pos);
7198 if (len & 0x01)
7199 return -1;
7200 len /= 2;
7201 if (len == 0)
7202 return -1;
7203 advproto = wpabuf_alloc(len);
7204 if (advproto == NULL)
7205 return -1;
7206 if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
7207 goto fail;
7208
7209 if (end) {
7210 /* Optional Query Request */
7211 pos = end + 1;
7212 while (*pos == ' ')
7213 pos++;
7214
7215 len = os_strlen(pos);
7216 if (len) {
7217 if (len & 0x01)
7218 goto fail;
7219 len /= 2;
7220 if (len == 0)
7221 goto fail;
7222 query = wpabuf_alloc(len);
7223 if (query == NULL)
7224 goto fail;
7225 if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
7226 goto fail;
7227 }
7228 }
7229
7230 ret = gas_send_request(wpa_s, dst_addr, advproto, query);
7231
7232fail:
7233 wpabuf_free(advproto);
7234 wpabuf_free(query);
7235
7236 return ret;
7237}
7238
7239
7240static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
7241 size_t buflen)
7242{
7243 u8 addr[ETH_ALEN];
7244 int dialog_token;
7245 int used;
7246 char *pos;
7247 size_t resp_len, start, requested_len;
b6a9590b
JM
7248 struct wpabuf *resp;
7249 int ret;
b1f12296
JM
7250
7251 used = hwaddr_aton2(cmd, addr);
7252 if (used < 0)
7253 return -1;
7254
7255 pos = cmd + used;
7256 while (*pos == ' ')
7257 pos++;
7258 dialog_token = atoi(pos);
7259
b6a9590b
JM
7260 if (wpa_s->last_gas_resp &&
7261 os_memcmp(addr, wpa_s->last_gas_addr, ETH_ALEN) == 0 &&
7262 dialog_token == wpa_s->last_gas_dialog_token)
7263 resp = wpa_s->last_gas_resp;
7264 else if (wpa_s->prev_gas_resp &&
7265 os_memcmp(addr, wpa_s->prev_gas_addr, ETH_ALEN) == 0 &&
7266 dialog_token == wpa_s->prev_gas_dialog_token)
7267 resp = wpa_s->prev_gas_resp;
7268 else
b1f12296
JM
7269 return -1;
7270
b6a9590b 7271 resp_len = wpabuf_len(resp);
b1f12296
JM
7272 start = 0;
7273 requested_len = resp_len;
7274
7275 pos = os_strchr(pos, ' ');
7276 if (pos) {
7277 start = atoi(pos);
7278 if (start > resp_len)
7279 return os_snprintf(buf, buflen, "FAIL-Invalid range");
7280 pos = os_strchr(pos, ',');
7281 if (pos == NULL)
7282 return -1;
7283 pos++;
7284 requested_len = atoi(pos);
7285 if (start + requested_len > resp_len)
7286 return os_snprintf(buf, buflen, "FAIL-Invalid range");
7287 }
7288
7289 if (requested_len * 2 + 1 > buflen)
7290 return os_snprintf(buf, buflen, "FAIL-Too long response");
7291
b6a9590b
JM
7292 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(resp) + start,
7293 requested_len);
7294
7295 if (start + requested_len == resp_len) {
7296 /*
7297 * Free memory by dropping the response after it has been
7298 * fetched.
7299 */
7300 if (resp == wpa_s->prev_gas_resp) {
7301 wpabuf_free(wpa_s->prev_gas_resp);
7302 wpa_s->prev_gas_resp = NULL;
7303 } else {
7304 wpabuf_free(wpa_s->last_gas_resp);
7305 wpa_s->last_gas_resp = NULL;
7306 }
7307 }
7308
7309 return ret;
b1f12296 7310}
afc064fe
JM
7311#endif /* CONFIG_INTERWORKING */
7312
7313
a8918e86
JK
7314#ifdef CONFIG_HS20
7315
7316static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
7317{
7318 u8 dst_addr[ETH_ALEN];
7319 int used;
7320 char *pos;
7321 u32 subtypes = 0;
7322
7323 used = hwaddr_aton2(dst, dst_addr);
7324 if (used < 0)
7325 return -1;
7326 pos = dst + used;
b68d602d
JM
7327 if (*pos == ' ')
7328 pos++;
a8918e86
JK
7329 for (;;) {
7330 int num = atoi(pos);
7331 if (num <= 0 || num > 31)
7332 return -1;
7333 subtypes |= BIT(num);
7334 pos = os_strchr(pos + 1, ',');
7335 if (pos == NULL)
7336 break;
7337 pos++;
7338 }
7339
7340 if (subtypes == 0)
7341 return -1;
7342
8dd5c1b4 7343 return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0, 0);
a8918e86
JK
7344}
7345
7346
7347static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
7348 const u8 *addr, const char *realm)
7349{
7350 u8 *buf;
7351 size_t rlen, len;
7352 int ret;
7353
7354 rlen = os_strlen(realm);
7355 len = 3 + rlen;
7356 buf = os_malloc(len);
7357 if (buf == NULL)
7358 return -1;
7359 buf[0] = 1; /* NAI Home Realm Count */
7360 buf[1] = 0; /* Formatted in accordance with RFC 4282 */
7361 buf[2] = rlen;
7362 os_memcpy(buf + 3, realm, rlen);
7363
7364 ret = hs20_anqp_send_req(wpa_s, addr,
7365 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
8dd5c1b4 7366 buf, len, 0);
a8918e86
JK
7367
7368 os_free(buf);
7369
7370 return ret;
7371}
7372
7373
7374static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
7375 char *dst)
7376{
7377 struct wpa_cred *cred = wpa_s->conf->cred;
7378 u8 dst_addr[ETH_ALEN];
7379 int used;
7380 u8 *buf;
7381 size_t len;
7382 int ret;
7383
7384 used = hwaddr_aton2(dst, dst_addr);
7385 if (used < 0)
7386 return -1;
7387
7388 while (dst[used] == ' ')
7389 used++;
7390 if (os_strncmp(dst + used, "realm=", 6) == 0)
7391 return hs20_nai_home_realm_list(wpa_s, dst_addr,
7392 dst + used + 6);
7393
7394 len = os_strlen(dst + used);
7395
7396 if (len == 0 && cred && cred->realm)
7397 return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
7398
0e87e798 7399 if (len & 1)
a8918e86
JK
7400 return -1;
7401 len /= 2;
7402 buf = os_malloc(len);
7403 if (buf == NULL)
7404 return -1;
7405 if (hexstr2bin(dst + used, buf, len) < 0) {
7406 os_free(buf);
7407 return -1;
7408 }
7409
7410 ret = hs20_anqp_send_req(wpa_s, dst_addr,
7411 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
8dd5c1b4 7412 buf, len, 0);
a8918e86
JK
7413 os_free(buf);
7414
7415 return ret;
7416}
7417
184e110c 7418
8dd5c1b4
JN
7419static int get_hs20_icon(struct wpa_supplicant *wpa_s, char *cmd, char *reply,
7420 int buflen)
7421{
7422 u8 dst_addr[ETH_ALEN];
7423 int used;
7424 char *ctx = NULL, *icon, *poffset, *psize;
7425
7426 used = hwaddr_aton2(cmd, dst_addr);
7427 if (used < 0)
7428 return -1;
7429 cmd += used;
7430
7431 icon = str_token(cmd, " ", &ctx);
7432 poffset = str_token(cmd, " ", &ctx);
7433 psize = str_token(cmd, " ", &ctx);
7434 if (!icon || !poffset || !psize)
7435 return -1;
7436
7437 wpa_s->fetch_osu_icon_in_progress = 0;
7438 return hs20_get_icon(wpa_s, dst_addr, icon, atoi(poffset), atoi(psize),
7439 reply, buflen);
7440}
7441
7442
7443static int del_hs20_icon(struct wpa_supplicant *wpa_s, char *cmd)
7444{
7445 u8 dst_addr[ETH_ALEN];
7446 int used;
7447 char *icon;
7448
7449 if (!cmd[0])
7450 return hs20_del_icon(wpa_s, NULL, NULL);
7451
7452 used = hwaddr_aton2(cmd, dst_addr);
7453 if (used < 0)
7454 return -1;
7455
7456 while (cmd[used] == ' ')
7457 used++;
7458 icon = cmd[used] ? &cmd[used] : NULL;
7459
7460 return hs20_del_icon(wpa_s, dst_addr, icon);
7461}
7462
7463
7464static int hs20_icon_request(struct wpa_supplicant *wpa_s, char *cmd, int inmem)
184e110c
JM
7465{
7466 u8 dst_addr[ETH_ALEN];
7467 int used;
7468 char *icon;
7469
7470 used = hwaddr_aton2(cmd, dst_addr);
7471 if (used < 0)
7472 return -1;
7473
7474 while (cmd[used] == ' ')
7475 used++;
7476 icon = &cmd[used];
7477
b572df86 7478 wpa_s->fetch_osu_icon_in_progress = 0;
184e110c 7479 return hs20_anqp_send_req(wpa_s, dst_addr, BIT(HS20_STYPE_ICON_REQUEST),
8dd5c1b4 7480 (u8 *) icon, os_strlen(icon), inmem);
184e110c
JM
7481}
7482
a8918e86
JK
7483#endif /* CONFIG_HS20 */
7484
7485
bc5d330a
TB
7486#ifdef CONFIG_AUTOSCAN
7487
7488static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
7489 char *cmd)
7490{
7491 enum wpa_states state = wpa_s->wpa_state;
7492 char *new_params = NULL;
7493
7494 if (os_strlen(cmd) > 0) {
7495 new_params = os_strdup(cmd);
7496 if (new_params == NULL)
7497 return -1;
7498 }
7499
7500 os_free(wpa_s->conf->autoscan);
7501 wpa_s->conf->autoscan = new_params;
7502
7503 if (wpa_s->conf->autoscan == NULL)
7504 autoscan_deinit(wpa_s);
7505 else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
99218999 7506 autoscan_init(wpa_s, 1);
99f00324
JM
7507 else if (state == WPA_SCANNING)
7508 wpa_supplicant_reinit_autoscan(wpa_s);
9d3f4a74
JM
7509 else
7510 wpa_printf(MSG_DEBUG, "No autoscan update in state %s",
7511 wpa_supplicant_state_txt(state));
bc5d330a
TB
7512
7513 return 0;
7514}
7515
7516#endif /* CONFIG_AUTOSCAN */
7517
7518
e9199e31
JM
7519#ifdef CONFIG_WNM
7520
7521static int wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant *wpa_s, char *cmd)
7522{
7523 int enter;
7524 int intval = 0;
7525 char *pos;
cd0ef657
JM
7526 int ret;
7527 struct wpabuf *tfs_req = NULL;
e9199e31
JM
7528
7529 if (os_strncmp(cmd, "enter", 5) == 0)
7530 enter = 1;
7531 else if (os_strncmp(cmd, "exit", 4) == 0)
7532 enter = 0;
7533 else
7534 return -1;
7535
7536 pos = os_strstr(cmd, " interval=");
7537 if (pos)
7538 intval = atoi(pos + 10);
7539
cd0ef657
JM
7540 pos = os_strstr(cmd, " tfs_req=");
7541 if (pos) {
7542 char *end;
7543 size_t len;
7544 pos += 9;
7545 end = os_strchr(pos, ' ');
7546 if (end)
7547 len = end - pos;
7548 else
7549 len = os_strlen(pos);
7550 if (len & 1)
7551 return -1;
7552 len /= 2;
7553 tfs_req = wpabuf_alloc(len);
7554 if (tfs_req == NULL)
7555 return -1;
7556 if (hexstr2bin(pos, wpabuf_put(tfs_req, len), len) < 0) {
7557 wpabuf_free(tfs_req);
7558 return -1;
7559 }
7560 }
7561
df80a0cc
JM
7562 ret = ieee802_11_send_wnmsleep_req(wpa_s, enter ? WNM_SLEEP_MODE_ENTER :
7563 WNM_SLEEP_MODE_EXIT, intval,
cd0ef657
JM
7564 tfs_req);
7565 wpabuf_free(tfs_req);
7566
7567 return ret;
e9199e31
JM
7568}
7569
65bcd0a9
VK
7570
7571static int wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant *wpa_s, char *cmd)
7572{
9a493fab 7573 int query_reason, list = 0;
15ab61ed 7574 char *btm_candidates = NULL;
65bcd0a9
VK
7575
7576 query_reason = atoi(cmd);
7577
9a493fab
AS
7578 cmd = os_strchr(cmd, ' ');
7579 if (cmd) {
15ab61ed 7580 if (os_strncmp(cmd, " list", 5) == 0)
9a493fab 7581 list = 1;
15ab61ed
AS
7582 else
7583 btm_candidates = cmd;
9a493fab
AS
7584 }
7585
7586 wpa_printf(MSG_DEBUG,
7587 "CTRL_IFACE: WNM_BSS_QUERY query_reason=%d%s",
7588 query_reason, list ? " candidate list" : "");
65bcd0a9 7589
15ab61ed
AS
7590 return wnm_send_bss_transition_mgmt_query(wpa_s, query_reason,
7591 btm_candidates,
7592 list);
65bcd0a9
VK
7593}
7594
d514b502
JM
7595
7596static int wpas_ctrl_iface_coloc_intf_report(struct wpa_supplicant *wpa_s,
7597 char *cmd)
7598{
7599 struct wpabuf *elems;
7600 int ret;
7601
7602 elems = wpabuf_parse_bin(cmd);
7603 if (!elems)
7604 return -1;
7605
7606 ret = wnm_send_coloc_intf_report(wpa_s, 0, elems);
7607 wpabuf_free(elems);
7608 return ret;
7609}
7610
e9199e31
JM
7611#endif /* CONFIG_WNM */
7612
7613
60b24b0d
DS
7614static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
7615 size_t buflen)
7616{
7617 struct wpa_signal_info si;
7618 int ret;
2cc8d8f4 7619 char *pos, *end;
60b24b0d
DS
7620
7621 ret = wpa_drv_signal_poll(wpa_s, &si);
7622 if (ret)
7623 return -1;
7624
2cc8d8f4
AO
7625 pos = buf;
7626 end = buf + buflen;
7627
7628 ret = os_snprintf(pos, end - pos, "RSSI=%d\nLINKSPEED=%d\n"
60b24b0d
DS
7629 "NOISE=%d\nFREQUENCY=%u\n",
7630 si.current_signal, si.current_txrate / 1000,
7631 si.current_noise, si.frequency);
7bdd8981 7632 if (os_snprintf_error(end - pos, ret))
60b24b0d 7633 return -1;
2cc8d8f4
AO
7634 pos += ret;
7635
7636 if (si.chanwidth != CHAN_WIDTH_UNKNOWN) {
7637 ret = os_snprintf(pos, end - pos, "WIDTH=%s\n",
7a4a93b9 7638 channel_width_to_string(si.chanwidth));
7bdd8981 7639 if (os_snprintf_error(end - pos, ret))
2cc8d8f4
AO
7640 return -1;
7641 pos += ret;
7642 }
7643
b55c623e
BP
7644 if (si.center_frq1 > 0) {
7645 ret = os_snprintf(pos, end - pos, "CENTER_FRQ1=%d\n",
7646 si.center_frq1);
7647 if (os_snprintf_error(end - pos, ret))
7648 return -1;
7649 pos += ret;
7650 }
7651
7652 if (si.center_frq2 > 0) {
7653 ret = os_snprintf(pos, end - pos, "CENTER_FRQ2=%d\n",
7654 si.center_frq2);
7bdd8981 7655 if (os_snprintf_error(end - pos, ret))
2cc8d8f4
AO
7656 return -1;
7657 pos += ret;
7658 }
7659
95783298
AO
7660 if (si.avg_signal) {
7661 ret = os_snprintf(pos, end - pos,
7662 "AVG_RSSI=%d\n", si.avg_signal);
d85e1fc8 7663 if (os_snprintf_error(end - pos, ret))
95783298
AO
7664 return -1;
7665 pos += ret;
7666 }
7667
74fa78b2
JM
7668 if (si.avg_beacon_signal) {
7669 ret = os_snprintf(pos, end - pos,
7670 "AVG_BEACON_RSSI=%d\n", si.avg_beacon_signal);
7671 if (os_snprintf_error(end - pos, ret))
7672 return -1;
7673 pos += ret;
7674 }
7675
2cc8d8f4 7676 return pos - buf;
60b24b0d
DS
7677}
7678
7679
96e8d831
DS
7680static int wpas_ctrl_iface_signal_monitor(struct wpa_supplicant *wpa_s,
7681 const char *cmd)
7682{
7683 const char *pos;
7684 int threshold = 0;
7685 int hysteresis = 0;
7686
7687 if (wpa_s->bgscan && wpa_s->bgscan_priv) {
7688 wpa_printf(MSG_DEBUG,
7689 "Reject SIGNAL_MONITOR command - bgscan is active");
7690 return -1;
7691 }
7692 pos = os_strstr(cmd, "THRESHOLD=");
7693 if (pos)
7694 threshold = atoi(pos + 10);
7695 pos = os_strstr(cmd, "HYSTERESIS=");
7696 if (pos)
7697 hysteresis = atoi(pos + 11);
7698 return wpa_drv_signal_monitor(wpa_s, threshold, hysteresis);
7699}
7700
7701
c06fca04
JM
7702#ifdef CONFIG_TESTING_OPTIONS
7703int wpas_ctrl_iface_get_pref_freq_list_override(struct wpa_supplicant *wpa_s,
7704 enum wpa_driver_if_type if_type,
7705 unsigned int *num,
7706 unsigned int *freq_list)
7707{
7708 char *pos = wpa_s->get_pref_freq_list_override;
7709 char *end;
7710 unsigned int count = 0;
7711
7712 /* Override string format:
7713 * <if_type1>:<freq1>,<freq2>,... <if_type2>:... */
7714
7715 while (pos) {
7716 if (atoi(pos) == (int) if_type)
7717 break;
7718 pos = os_strchr(pos, ' ');
7719 if (pos)
7720 pos++;
7721 }
7722 if (!pos)
7723 return -1;
7724 pos = os_strchr(pos, ':');
7725 if (!pos)
7726 return -1;
7727 pos++;
7728 end = os_strchr(pos, ' ');
7729 while (pos && (!end || pos < end) && count < *num) {
7730 freq_list[count++] = atoi(pos);
7731 pos = os_strchr(pos, ',');
7732 if (pos)
7733 pos++;
7734 }
7735
7736 *num = count;
7737 return 0;
7738}
7739#endif /* CONFIG_TESTING_OPTIONS */
7740
7741
98342208
AK
7742static int wpas_ctrl_iface_get_pref_freq_list(
7743 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
7744{
7745 unsigned int freq_list[100], num = 100, i;
7746 int ret;
7747 enum wpa_driver_if_type iface_type;
7748 char *pos, *end;
7749
7750 pos = buf;
7751 end = buf + buflen;
7752
7753 /* buf: "<interface_type>" */
7754 if (os_strcmp(cmd, "STATION") == 0)
7755 iface_type = WPA_IF_STATION;
7756 else if (os_strcmp(cmd, "AP") == 0)
7757 iface_type = WPA_IF_AP_BSS;
7758 else if (os_strcmp(cmd, "P2P_GO") == 0)
7759 iface_type = WPA_IF_P2P_GO;
7760 else if (os_strcmp(cmd, "P2P_CLIENT") == 0)
7761 iface_type = WPA_IF_P2P_CLIENT;
7762 else if (os_strcmp(cmd, "IBSS") == 0)
7763 iface_type = WPA_IF_IBSS;
7764 else if (os_strcmp(cmd, "TDLS") == 0)
7765 iface_type = WPA_IF_TDLS;
7766 else
7767 return -1;
7768
7769 wpa_printf(MSG_DEBUG,
7770 "CTRL_IFACE: GET_PREF_FREQ_LIST iface_type=%d (%s)",
e6ac47b4 7771 iface_type, cmd);
98342208
AK
7772
7773 ret = wpa_drv_get_pref_freq_list(wpa_s, iface_type, &num, freq_list);
7774 if (ret)
7775 return -1;
7776
7777 for (i = 0; i < num; i++) {
7778 ret = os_snprintf(pos, end - pos, "%s%u",
7779 i > 0 ? "," : "", freq_list[i]);
7780 if (os_snprintf_error(end - pos, ret))
7781 return -1;
7782 pos += ret;
7783 }
7784
7785 return pos - buf;
7786}
7787
7788
4d7aab78
EL
7789static int wpas_ctrl_iface_driver_flags(struct wpa_supplicant *wpa_s,
7790 char *buf, size_t buflen)
7791{
7792 int ret, i;
7793 char *pos, *end;
7794
7795 ret = os_snprintf(buf, buflen, "%016llX:\n",
7796 (long long unsigned) wpa_s->drv_flags);
7797 if (os_snprintf_error(buflen, ret))
7798 return -1;
7799
7800 pos = buf + ret;
7801 end = buf + buflen;
7802
7803 for (i = 0; i < 64; i++) {
7804 if (wpa_s->drv_flags & (1LLU << i)) {
7805 ret = os_snprintf(pos, end - pos, "%s\n",
7806 driver_flag_to_string(1LLU << i));
7807 if (os_snprintf_error(end - pos, ret))
7808 return -1;
7809 pos += ret;
7810 }
7811 }
7812
7813 return pos - buf;
7814}
7815
7816
dc7785f8
YZ
7817static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
7818 size_t buflen)
7819{
7820 struct hostap_sta_driver_data sta;
7821 int ret;
7822
7823 ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
7824 if (ret)
7825 return -1;
7826
7827 ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
7828 sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
7bdd8981 7829 if (os_snprintf_error(buflen, ret))
dc7785f8
YZ
7830 return -1;
7831 return ret;
7832}
7833
7834
5e2c3490
JM
7835#ifdef ANDROID
7836static int wpa_supplicant_driver_cmd(struct wpa_supplicant *wpa_s, char *cmd,
7837 char *buf, size_t buflen)
7838{
7839 int ret;
7840
7841 ret = wpa_drv_driver_cmd(wpa_s, cmd, buf, buflen);
a94737ea
DS
7842 if (ret == 0) {
7843 if (os_strncasecmp(cmd, "COUNTRY", 7) == 0) {
7844 struct p2p_data *p2p = wpa_s->global->p2p;
7845 if (p2p) {
7846 char country[3];
7847 country[0] = cmd[8];
7848 country[1] = cmd[9];
7849 country[2] = 0x04;
7850 p2p_set_country(p2p, country);
7851 }
7852 }
5e2c3490 7853 ret = os_snprintf(buf, buflen, "%s\n", "OK");
aaadd727
JM
7854 if (os_snprintf_error(buflen, ret))
7855 ret = -1;
a94737ea 7856 }
5e2c3490
JM
7857 return ret;
7858}
7859#endif /* ANDROID */
7860
7861
adef8948
BL
7862static int wpa_supplicant_vendor_cmd(struct wpa_supplicant *wpa_s, char *cmd,
7863 char *buf, size_t buflen)
7864{
7865 int ret;
7866 char *pos;
7867 u8 *data = NULL;
7868 unsigned int vendor_id, subcmd;
7869 struct wpabuf *reply;
7870 size_t data_len = 0;
7871
7872 /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
7873 vendor_id = strtoul(cmd, &pos, 16);
640b0b93 7874 if (!isblank((unsigned char) *pos))
adef8948
BL
7875 return -EINVAL;
7876
7877 subcmd = strtoul(pos, &pos, 10);
7878
7879 if (*pos != '\0') {
640b0b93 7880 if (!isblank((unsigned char) *pos++))
adef8948
BL
7881 return -EINVAL;
7882 data_len = os_strlen(pos);
7883 }
7884
7885 if (data_len) {
7886 data_len /= 2;
7887 data = os_malloc(data_len);
7888 if (!data)
2cebdee6 7889 return -1;
adef8948
BL
7890
7891 if (hexstr2bin(pos, data, data_len)) {
7892 wpa_printf(MSG_DEBUG,
7893 "Vendor command: wrong parameter format");
7894 os_free(data);
7895 return -EINVAL;
7896 }
7897 }
7898
7899 reply = wpabuf_alloc((buflen - 1) / 2);
7900 if (!reply) {
7901 os_free(data);
2cebdee6 7902 return -1;
adef8948
BL
7903 }
7904
7905 ret = wpa_drv_vendor_cmd(wpa_s, vendor_id, subcmd, data, data_len,
7906 reply);
7907
7908 if (ret == 0)
7909 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
7910 wpabuf_len(reply));
7911
7912 wpabuf_free(reply);
7913 os_free(data);
7914
7915 return ret;
7916}
7917
7918
acb54643
JM
7919static void wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant *wpa_s)
7920{
7e608d1d
IP
7921#ifdef CONFIG_P2P
7922 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s ?
7923 wpa_s->global->p2p_init_wpa_s : wpa_s;
7924#endif /* CONFIG_P2P */
7925
acb54643
JM
7926 wpa_dbg(wpa_s, MSG_DEBUG, "Flush all wpa_supplicant state");
7927
6e374bd4
JM
7928 if (wpas_abort_ongoing_scan(wpa_s) == 0)
7929 wpa_s->ignore_post_flush_scan_res = 1;
53401e91 7930
e9ccfc38
JM
7931 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
7932 /*
7933 * Avoid possible auto connect re-connection on getting
7934 * disconnected due to state flush.
7935 */
7936 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
7937 }
7938
acb54643 7939#ifdef CONFIG_P2P
1d20c66e 7940 wpas_p2p_group_remove(p2p_wpa_s, "*");
7e608d1d
IP
7941 wpas_p2p_cancel(p2p_wpa_s);
7942 p2p_ctrl_flush(p2p_wpa_s);
7e608d1d
IP
7943 wpas_p2p_service_flush(p2p_wpa_s);
7944 p2p_wpa_s->global->p2p_disabled = 0;
7945 p2p_wpa_s->global->p2p_per_sta_psk = 0;
7946 p2p_wpa_s->conf->num_sec_device_types = 0;
7947 p2p_wpa_s->p2p_disable_ip_addr_req = 0;
7948 os_free(p2p_wpa_s->global->p2p_go_avoid_freq.range);
7949 p2p_wpa_s->global->p2p_go_avoid_freq.range = NULL;
85e152b6 7950 p2p_wpa_s->global->p2p_go_avoid_freq.num = 0;
9a58e521 7951 p2p_wpa_s->global->pending_p2ps_group = 0;
8bb8e6ed 7952 p2p_wpa_s->global->pending_p2ps_group_freq = 0;
acb54643
JM
7953#endif /* CONFIG_P2P */
7954
7955#ifdef CONFIG_WPS_TESTING
7956 wps_version_number = 0x20;
7957 wps_testing_dummy_cred = 0;
91226e0d 7958 wps_corrupt_pkhash = 0;
6e379c6c
JM
7959 wps_force_auth_types_in_use = 0;
7960 wps_force_encr_types_in_use = 0;
acb54643
JM
7961#endif /* CONFIG_WPS_TESTING */
7962#ifdef CONFIG_WPS
7b02375a 7963 wpa_s->wps_fragment_size = 0;
acb54643 7964 wpas_wps_cancel(wpa_s);
422ba11e 7965 wps_registrar_flush(wpa_s->wps->registrar);
acb54643 7966#endif /* CONFIG_WPS */
7255983b 7967 wpa_s->after_wps = 0;
4d9fb08d 7968 wpa_s->known_wps_freq = 0;
acb54643 7969
30d27b04
JM
7970#ifdef CONFIG_DPP
7971 wpas_dpp_deinit(wpa_s);
f97ace34
JM
7972 wpa_s->dpp_init_max_tries = 0;
7973 wpa_s->dpp_init_retry_time = 0;
7974 wpa_s->dpp_resp_wait_time = 0;
95b0104a
JM
7975 wpa_s->dpp_resp_max_tries = 0;
7976 wpa_s->dpp_resp_retry_time = 0;
dc2f24f1 7977#ifdef CONFIG_TESTING_OPTIONS
2bdc47a9
JM
7978 os_memset(dpp_pkex_own_mac_override, 0, ETH_ALEN);
7979 os_memset(dpp_pkex_peer_mac_override, 0, ETH_ALEN);
acc555f9 7980 dpp_pkex_ephemeral_key_override_len = 0;
f5526975 7981 dpp_protocol_key_override_len = 0;
055cd397 7982 dpp_nonce_override_len = 0;
dc2f24f1 7983#endif /* CONFIG_TESTING_OPTIONS */
30d27b04
JM
7984#endif /* CONFIG_DPP */
7985
9d2cb3ec 7986#ifdef CONFIG_TDLS
acb54643 7987#ifdef CONFIG_TDLS_TESTING
acb54643
JM
7988 tdls_testing = 0;
7989#endif /* CONFIG_TDLS_TESTING */
acb54643
JM
7990 wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL);
7991 wpa_tdls_enable(wpa_s->wpa, 1);
7992#endif /* CONFIG_TDLS */
7993
e78aaca0
JM
7994 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
7995 wpa_supplicant_stop_countermeasures(wpa_s, NULL);
7996
acb54643 7997 wpa_s->no_keep_alive = 0;
c2805909 7998 wpa_s->own_disconnect_req = 0;
acb54643
JM
7999
8000 os_free(wpa_s->disallow_aps_bssid);
8001 wpa_s->disallow_aps_bssid = NULL;
8002 wpa_s->disallow_aps_bssid_count = 0;
8003 os_free(wpa_s->disallow_aps_ssid);
8004 wpa_s->disallow_aps_ssid = NULL;
8005 wpa_s->disallow_aps_ssid_count = 0;
8006
8007 wpa_s->set_sta_uapsd = 0;
8008 wpa_s->sta_uapsd = 0;
8009
8010 wpa_drv_radio_disable(wpa_s, 0);
acb54643 8011 wpa_blacklist_clear(wpa_s);
a8a7890d 8012 wpa_s->extra_blacklist_count = 0;
acb54643
JM
8013 wpa_supplicant_ctrl_iface_remove_network(wpa_s, "all");
8014 wpa_supplicant_ctrl_iface_remove_cred(wpa_s, "all");
d9bb2821 8015 wpa_config_flush_blobs(wpa_s->conf);
ea6e040c
JM
8016 wpa_s->conf->auto_interworking = 0;
8017 wpa_s->conf->okc = 0;
04f7ecc6 8018
b925506a
JM
8019 wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
8020 rsn_preauth_deinit(wpa_s->wpa);
8021
04f7ecc6
JM
8022 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME, 43200);
8023 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD, 70);
8024 wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, 60);
0d79b50a 8025 eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
b1ae396f 8026
b3253ebb 8027 radio_remove_works(wpa_s, NULL, 1);
e3745228 8028 wpa_s->ext_work_in_progress = 0;
3d910ef4
JM
8029
8030 wpa_s->next_ssid = NULL;
b572df86
JM
8031
8032#ifdef CONFIG_INTERWORKING
876e74aa 8033#ifdef CONFIG_HS20
b572df86 8034 hs20_cancel_fetch_osu(wpa_s);
8dd5c1b4 8035 hs20_del_icon(wpa_s, NULL, NULL);
876e74aa 8036#endif /* CONFIG_HS20 */
b572df86 8037#endif /* CONFIG_INTERWORKING */
60b893df
JM
8038
8039 wpa_s->ext_mgmt_frame_handling = 0;
9d4ff04a 8040 wpa_s->ext_eapol_frame_io = 0;
1f94e4ee
JM
8041#ifdef CONFIG_TESTING_OPTIONS
8042 wpa_s->extra_roc_dur = 0;
911942ee 8043 wpa_s->test_failure = WPAS_TEST_FAILURE_NONE;
ed7820b4 8044 wpa_s->p2p_go_csa_on_inv = 0;
02adead5 8045 wpa_s->ignore_auth_resp = 0;
6ad37d73 8046 wpa_s->ignore_assoc_disallow = 0;
daa40960 8047 wpa_s->testing_resend_assoc = 0;
a483c6f1 8048 wpa_s->reject_btm_req_reason = 0;
651c6a84 8049 wpa_sm_set_test_assoc_ie(wpa_s->wpa, NULL);
c06fca04
JM
8050 os_free(wpa_s->get_pref_freq_list_override);
8051 wpa_s->get_pref_freq_list_override = NULL;
a0f19e9c
JM
8052 wpabuf_free(wpa_s->sae_commit_override);
8053 wpa_s->sae_commit_override = NULL;
60239f60 8054#ifdef CONFIG_DPP
b7dddab7
JM
8055 os_free(wpa_s->dpp_config_obj_override);
8056 wpa_s->dpp_config_obj_override = NULL;
8057 os_free(wpa_s->dpp_discovery_override);
8058 wpa_s->dpp_discovery_override = NULL;
8059 os_free(wpa_s->dpp_groups_override);
8060 wpa_s->dpp_groups_override = NULL;
60239f60
JM
8061 dpp_test = DPP_TEST_DISABLED;
8062#endif /* CONFIG_DPP */
1f94e4ee 8063#endif /* CONFIG_TESTING_OPTIONS */
97cfe110
JM
8064
8065 wpa_s->disconnected = 0;
b8db1dfc
JM
8066 os_free(wpa_s->next_scan_freqs);
8067 wpa_s->next_scan_freqs = NULL;
88a44755
JM
8068 os_free(wpa_s->select_network_scan_freqs);
8069 wpa_s->select_network_scan_freqs = NULL;
6c699913
JM
8070
8071 wpa_bss_flush(wpa_s);
8072 if (!dl_list_empty(&wpa_s->bss)) {
8073 wpa_printf(MSG_DEBUG,
8074 "BSS table not empty after flush: %u entries, current_bss=%p bssid="
8075 MACSTR " pending_bssid=" MACSTR,
8076 dl_list_len(&wpa_s->bss), wpa_s->current_bss,
8077 MAC2STR(wpa_s->bssid),
8078 MAC2STR(wpa_s->pending_bssid));
8079 }
9bd566a3
AS
8080
8081 eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
03ed0a52 8082 wpa_s->wnmsleep_used = 0;
ab6ab07a
JM
8083
8084#ifdef CONFIG_SME
8085 wpa_s->sme.last_unprot_disconnect.sec = 0;
8086#endif /* CONFIG_SME */
c6c41f6e
JM
8087
8088 wpabuf_free(wpa_s->ric_ies);
8089 wpa_s->ric_ies = NULL;
32bb47fa
JM
8090
8091 wpa_supplicant_update_channel_list(wpa_s, NULL);
19677b77
BL
8092
8093 free_bss_tmp_disallowed(wpa_s);
acb54643
JM
8094}
8095
8096
1f965e62
JM
8097static int wpas_ctrl_radio_work_show(struct wpa_supplicant *wpa_s,
8098 char *buf, size_t buflen)
8099{
8100 struct wpa_radio_work *work;
8101 char *pos, *end;
8102 struct os_reltime now, diff;
8103
8104 pos = buf;
8105 end = buf + buflen;
8106
8107 os_get_reltime(&now);
8108
8109 dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
8110 {
8111 int ret;
8112
8113 os_reltime_sub(&now, &work->time, &diff);
8114 ret = os_snprintf(pos, end - pos, "%s@%s:%u:%u:%ld.%06ld\n",
8115 work->type, work->wpa_s->ifname, work->freq,
8116 work->started, diff.sec, diff.usec);
d85e1fc8 8117 if (os_snprintf_error(end - pos, ret))
1f965e62
JM
8118 break;
8119 pos += ret;
8120 }
8121
8122 return pos - buf;
8123}
8124
8125
8126static void wpas_ctrl_radio_work_timeout(void *eloop_ctx, void *timeout_ctx)
8127{
8128 struct wpa_radio_work *work = eloop_ctx;
8129 struct wpa_external_work *ework = work->ctx;
8130
8131 wpa_dbg(work->wpa_s, MSG_DEBUG,
8132 "Timing out external radio work %u (%s)",
8133 ework->id, work->type);
8134 wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_TIMEOUT "%u", ework->id);
e3745228 8135 work->wpa_s->ext_work_in_progress = 0;
1f965e62 8136 radio_work_done(work);
df48efc5 8137 os_free(ework);
1f965e62
JM
8138}
8139
8140
8141static void wpas_ctrl_radio_work_cb(struct wpa_radio_work *work, int deinit)
8142{
8143 struct wpa_external_work *ework = work->ctx;
8144
8145 if (deinit) {
b3253ebb
AO
8146 if (work->started)
8147 eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
8148 work, NULL);
8149
7d1007a6
JM
8150 /*
8151 * work->type points to a buffer in ework, so need to replace
8152 * that here with a fixed string to avoid use of freed memory
8153 * in debug prints.
8154 */
8155 work->type = "freed-ext-work";
8156 work->ctx = NULL;
1f965e62
JM
8157 os_free(ework);
8158 return;
8159 }
8160
8161 wpa_dbg(work->wpa_s, MSG_DEBUG, "Starting external radio work %u (%s)",
8162 ework->id, ework->type);
8163 wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_START "%u", ework->id);
e3745228 8164 work->wpa_s->ext_work_in_progress = 1;
1f965e62
JM
8165 if (!ework->timeout)
8166 ework->timeout = 10;
8167 eloop_register_timeout(ework->timeout, 0, wpas_ctrl_radio_work_timeout,
8168 work, NULL);
8169}
8170
8171
8172static int wpas_ctrl_radio_work_add(struct wpa_supplicant *wpa_s, char *cmd,
8173 char *buf, size_t buflen)
8174{
8175 struct wpa_external_work *ework;
8176 char *pos, *pos2;
8177 size_t type_len;
8178 int ret;
8179 unsigned int freq = 0;
8180
8181 /* format: <name> [freq=<MHz>] [timeout=<seconds>] */
8182
8183 ework = os_zalloc(sizeof(*ework));
8184 if (ework == NULL)
8185 return -1;
8186
8187 pos = os_strchr(cmd, ' ');
8188 if (pos) {
8189 type_len = pos - cmd;
8190 pos++;
8191
8192 pos2 = os_strstr(pos, "freq=");
8193 if (pos2)
8194 freq = atoi(pos2 + 5);
8195
8196 pos2 = os_strstr(pos, "timeout=");
8197 if (pos2)
8198 ework->timeout = atoi(pos2 + 8);
8199 } else {
8200 type_len = os_strlen(cmd);
8201 }
8202 if (4 + type_len >= sizeof(ework->type))
8203 type_len = sizeof(ework->type) - 4 - 1;
8204 os_strlcpy(ework->type, "ext:", sizeof(ework->type));
8205 os_memcpy(ework->type + 4, cmd, type_len);
8206 ework->type[4 + type_len] = '\0';
8207
8208 wpa_s->ext_work_id++;
8209 if (wpa_s->ext_work_id == 0)
8210 wpa_s->ext_work_id++;
8211 ework->id = wpa_s->ext_work_id;
8212
8213 if (radio_add_work(wpa_s, freq, ework->type, 0, wpas_ctrl_radio_work_cb,
8214 ework) < 0) {
8215 os_free(ework);
8216 return -1;
8217 }
8218
8219 ret = os_snprintf(buf, buflen, "%u", ework->id);
d85e1fc8 8220 if (os_snprintf_error(buflen, ret))
1f965e62
JM
8221 return -1;
8222 return ret;
8223}
8224
8225
8226static int wpas_ctrl_radio_work_done(struct wpa_supplicant *wpa_s, char *cmd)
8227{
8228 struct wpa_radio_work *work;
8229 unsigned int id = atoi(cmd);
8230
8231 dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
8232 {
8233 struct wpa_external_work *ework;
8234
8235 if (os_strncmp(work->type, "ext:", 4) != 0)
8236 continue;
8237 ework = work->ctx;
8238 if (id && ework->id != id)
8239 continue;
8240 wpa_dbg(wpa_s, MSG_DEBUG,
8241 "Completed external radio work %u (%s)",
8242 ework->id, ework->type);
8243 eloop_cancel_timeout(wpas_ctrl_radio_work_timeout, work, NULL);
e3745228 8244 wpa_s->ext_work_in_progress = 0;
1f965e62 8245 radio_work_done(work);
6829da39 8246 os_free(ework);
1f965e62
JM
8247 return 3; /* "OK\n" */
8248 }
8249
8250 return -1;
8251}
8252
8253
8254static int wpas_ctrl_radio_work(struct wpa_supplicant *wpa_s, char *cmd,
8255 char *buf, size_t buflen)
8256{
8257 if (os_strcmp(cmd, "show") == 0)
8258 return wpas_ctrl_radio_work_show(wpa_s, buf, buflen);
8259 if (os_strncmp(cmd, "add ", 4) == 0)
8260 return wpas_ctrl_radio_work_add(wpa_s, cmd + 4, buf, buflen);
8261 if (os_strncmp(cmd, "done ", 5) == 0)
8262 return wpas_ctrl_radio_work_done(wpa_s, cmd + 4);
8263 return -1;
8264}
8265
8266
8267void wpas_ctrl_radio_work_flush(struct wpa_supplicant *wpa_s)
8268{
8269 struct wpa_radio_work *work, *tmp;
8270
a6cff8bf
MS
8271 if (!wpa_s || !wpa_s->radio)
8272 return;
8273
1f965e62
JM
8274 dl_list_for_each_safe(work, tmp, &wpa_s->radio->work,
8275 struct wpa_radio_work, list) {
8276 struct wpa_external_work *ework;
8277
8278 if (os_strncmp(work->type, "ext:", 4) != 0)
8279 continue;
8280 ework = work->ctx;
8281 wpa_dbg(wpa_s, MSG_DEBUG,
023b466d 8282 "Flushing%s external radio work %u (%s)",
1f965e62
JM
8283 work->started ? " started" : "", ework->id,
8284 ework->type);
8285 if (work->started)
8286 eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
8287 work, NULL);
1f965e62 8288 radio_work_done(work);
df48efc5 8289 os_free(ework);
1f965e62
JM
8290 }
8291}
8292
8293
bceb8431
JM
8294static void wpas_ctrl_eapol_response(void *eloop_ctx, void *timeout_ctx)
8295{
8296 struct wpa_supplicant *wpa_s = eloop_ctx;
8297 eapol_sm_notify_ctrl_response(wpa_s->eapol);
8298}
8299
8300
43a66ecb
JM
8301static int scan_id_list_parse(struct wpa_supplicant *wpa_s, const char *value,
8302 unsigned int *scan_id_count, int scan_id[])
6891f0e6
LJ
8303{
8304 const char *pos = value;
8305
8306 while (pos) {
8307 if (*pos == ' ' || *pos == '\0')
8308 break;
43a66ecb 8309 if (*scan_id_count == MAX_SCAN_ID)
6891f0e6 8310 return -1;
43a66ecb 8311 scan_id[(*scan_id_count)++] = atoi(pos);
6891f0e6
LJ
8312 pos = os_strchr(pos, ',');
8313 if (pos)
8314 pos++;
8315 }
8316
8317 return 0;
8318}
8319
8320
fee52342
JM
8321static void wpas_ctrl_scan(struct wpa_supplicant *wpa_s, char *params,
8322 char *reply, int reply_size, int *reply_len)
8323{
8324 char *pos;
43a66ecb
JM
8325 unsigned int manual_scan_passive = 0;
8326 unsigned int manual_scan_use_id = 0;
8327 unsigned int manual_scan_only_new = 0;
8328 unsigned int scan_only = 0;
8329 unsigned int scan_id_count = 0;
8330 int scan_id[MAX_SCAN_ID];
8331 void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
8332 struct wpa_scan_results *scan_res);
8333 int *manual_scan_freqs = NULL;
a80651d0
KV
8334 struct wpa_ssid_value *ssid = NULL, *ns;
8335 unsigned int ssid_count = 0;
fee52342
JM
8336
8337 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
8338 *reply_len = -1;
8339 return;
8340 }
8341
e69ae5ff
JM
8342 if (radio_work_pending(wpa_s, "scan")) {
8343 wpa_printf(MSG_DEBUG,
8344 "Pending scan scheduled - reject new request");
8345 *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
8346 return;
8347 }
8348
9772af66
NM
8349#ifdef CONFIG_INTERWORKING
8350 if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select) {
8351 wpa_printf(MSG_DEBUG,
8352 "Interworking select in progress - reject new scan");
8353 *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
8354 return;
8355 }
8356#endif /* CONFIG_INTERWORKING */
8357
fee52342
JM
8358 if (params) {
8359 if (os_strncasecmp(params, "TYPE=ONLY", 9) == 0)
43a66ecb 8360 scan_only = 1;
fee52342
JM
8361
8362 pos = os_strstr(params, "freq=");
43a66ecb
JM
8363 if (pos) {
8364 manual_scan_freqs = freq_range_to_channel_list(wpa_s,
8365 pos + 5);
8366 if (manual_scan_freqs == NULL) {
8367 *reply_len = -1;
8368 goto done;
8369 }
fee52342 8370 }
88c2d488
JM
8371
8372 pos = os_strstr(params, "passive=");
8373 if (pos)
43a66ecb 8374 manual_scan_passive = !!atoi(pos + 8);
d81c73be
JM
8375
8376 pos = os_strstr(params, "use_id=");
8377 if (pos)
43a66ecb 8378 manual_scan_use_id = atoi(pos + 7);
949938aa
JM
8379
8380 pos = os_strstr(params, "only_new=1");
8381 if (pos)
43a66ecb 8382 manual_scan_only_new = 1;
6891f0e6
LJ
8383
8384 pos = os_strstr(params, "scan_id=");
43a66ecb
JM
8385 if (pos && scan_id_list_parse(wpa_s, pos + 8, &scan_id_count,
8386 scan_id) < 0) {
6891f0e6 8387 *reply_len = -1;
43a66ecb 8388 goto done;
6891f0e6 8389 }
a80651d0 8390
267fc0dd
AP
8391 pos = os_strstr(params, "bssid=");
8392 if (pos) {
8393 u8 bssid[ETH_ALEN];
8394
8395 pos += 6;
8396 if (hwaddr_aton(pos, bssid)) {
8397 wpa_printf(MSG_ERROR, "Invalid BSSID %s", pos);
8398 *reply_len = -1;
8399 goto done;
8400 }
8401 os_memcpy(wpa_s->next_scan_bssid, bssid, ETH_ALEN);
8402 }
8403
a80651d0
KV
8404 pos = params;
8405 while (pos && *pos != '\0') {
8406 if (os_strncmp(pos, "ssid ", 5) == 0) {
8407 char *end;
8408
8409 pos += 5;
8410 end = pos;
8411 while (*end) {
8412 if (*end == '\0' || *end == ' ')
8413 break;
8414 end++;
8415 }
8416
8417 ns = os_realloc_array(
8418 ssid, ssid_count + 1,
8419 sizeof(struct wpa_ssid_value));
8420 if (ns == NULL) {
8421 *reply_len = -1;
8422 goto done;
8423 }
8424 ssid = ns;
8425
8426 if ((end - pos) & 0x01 ||
8427 end - pos > 2 * SSID_MAX_LEN ||
8428 hexstr2bin(pos, ssid[ssid_count].ssid,
8429 (end - pos) / 2) < 0) {
8430 wpa_printf(MSG_DEBUG,
8431 "Invalid SSID value '%s'",
8432 pos);
8433 *reply_len = -1;
8434 goto done;
8435 }
8436 ssid[ssid_count].ssid_len = (end - pos) / 2;
8437 wpa_hexdump_ascii(MSG_DEBUG, "scan SSID",
8438 ssid[ssid_count].ssid,
8439 ssid[ssid_count].ssid_len);
8440 ssid_count++;
8441 pos = end;
8442 }
8443
8444 pos = os_strchr(pos, ' ');
8445 if (pos)
8446 pos++;
8447 }
8448 }
8449
8450 wpa_s->num_ssids_from_scan_req = ssid_count;
8451 os_free(wpa_s->ssids_from_scan_req);
8452 if (ssid_count) {
8453 wpa_s->ssids_from_scan_req = ssid;
8454 ssid = NULL;
8455 } else {
8456 wpa_s->ssids_from_scan_req = NULL;
fee52342
JM
8457 }
8458
43a66ecb
JM
8459 if (scan_only)
8460 scan_res_handler = scan_only_handler;
8461 else if (wpa_s->scan_res_handler == scan_only_handler)
8462 scan_res_handler = NULL;
8463 else
8464 scan_res_handler = wpa_s->scan_res_handler;
8465
fee52342
JM
8466 if (!wpa_s->sched_scanning && !wpa_s->scanning &&
8467 ((wpa_s->wpa_state <= WPA_SCANNING) ||
8468 (wpa_s->wpa_state == WPA_COMPLETED))) {
43a66ecb
JM
8469 wpa_s->manual_scan_passive = manual_scan_passive;
8470 wpa_s->manual_scan_use_id = manual_scan_use_id;
8471 wpa_s->manual_scan_only_new = manual_scan_only_new;
8472 wpa_s->scan_id_count = scan_id_count;
8473 os_memcpy(wpa_s->scan_id, scan_id, scan_id_count * sizeof(int));
8474 wpa_s->scan_res_handler = scan_res_handler;
8475 os_free(wpa_s->manual_scan_freqs);
8476 wpa_s->manual_scan_freqs = manual_scan_freqs;
8477 manual_scan_freqs = NULL;
8478
fee52342
JM
8479 wpa_s->normal_scans = 0;
8480 wpa_s->scan_req = MANUAL_SCAN_REQ;
8481 wpa_s->after_wps = 0;
8482 wpa_s->known_wps_freq = 0;
8483 wpa_supplicant_req_scan(wpa_s, 0, 0);
d81c73be
JM
8484 if (wpa_s->manual_scan_use_id) {
8485 wpa_s->manual_scan_id++;
8486 wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
8487 wpa_s->manual_scan_id);
8488 *reply_len = os_snprintf(reply, reply_size, "%u\n",
8489 wpa_s->manual_scan_id);
8490 }
fee52342 8491 } else if (wpa_s->sched_scanning) {
43a66ecb
JM
8492 wpa_s->manual_scan_passive = manual_scan_passive;
8493 wpa_s->manual_scan_use_id = manual_scan_use_id;
8494 wpa_s->manual_scan_only_new = manual_scan_only_new;
8495 wpa_s->scan_id_count = scan_id_count;
8496 os_memcpy(wpa_s->scan_id, scan_id, scan_id_count * sizeof(int));
8497 wpa_s->scan_res_handler = scan_res_handler;
8498 os_free(wpa_s->manual_scan_freqs);
8499 wpa_s->manual_scan_freqs = manual_scan_freqs;
8500 manual_scan_freqs = NULL;
8501
fee52342
JM
8502 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to allow requested full scan to proceed");
8503 wpa_supplicant_cancel_sched_scan(wpa_s);
8504 wpa_s->scan_req = MANUAL_SCAN_REQ;
8505 wpa_supplicant_req_scan(wpa_s, 0, 0);
d81c73be
JM
8506 if (wpa_s->manual_scan_use_id) {
8507 wpa_s->manual_scan_id++;
8508 *reply_len = os_snprintf(reply, reply_size, "%u\n",
8509 wpa_s->manual_scan_id);
8510 wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
8511 wpa_s->manual_scan_id);
8512 }
fee52342
JM
8513 } else {
8514 wpa_printf(MSG_DEBUG, "Ongoing scan action - reject new request");
8515 *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
8516 }
43a66ecb
JM
8517
8518done:
8519 os_free(manual_scan_freqs);
a80651d0 8520 os_free(ssid);
fee52342
JM
8521}
8522
8523
60b893df
JM
8524#ifdef CONFIG_TESTING_OPTIONS
8525
8526static void wpas_ctrl_iface_mgmt_tx_cb(struct wpa_supplicant *wpa_s,
8527 unsigned int freq, const u8 *dst,
8528 const u8 *src, const u8 *bssid,
8529 const u8 *data, size_t data_len,
8530 enum offchannel_send_action_result
8531 result)
8532{
8533 wpa_msg(wpa_s, MSG_INFO, "MGMT-TX-STATUS freq=%u dst=" MACSTR
8534 " src=" MACSTR " bssid=" MACSTR " result=%s",
8535 freq, MAC2STR(dst), MAC2STR(src), MAC2STR(bssid),
8536 result == OFFCHANNEL_SEND_ACTION_SUCCESS ?
8537 "SUCCESS" : (result == OFFCHANNEL_SEND_ACTION_NO_ACK ?
8538 "NO_ACK" : "FAILED"));
8539}
8540
8541
8542static int wpas_ctrl_iface_mgmt_tx(struct wpa_supplicant *wpa_s, char *cmd)
8543{
8544 char *pos, *param;
8545 size_t len;
8546 u8 *buf, da[ETH_ALEN], bssid[ETH_ALEN];
8547 int res, used;
8548 int freq = 0, no_cck = 0, wait_time = 0;
8549
8550 /* <DA> <BSSID> [freq=<MHz>] [wait_time=<ms>] [no_cck=1]
8551 * <action=Action frame payload> */
8552
8553 wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
8554
8555 pos = cmd;
8556 used = hwaddr_aton2(pos, da);
8557 if (used < 0)
8558 return -1;
8559 pos += used;
8560 while (*pos == ' ')
8561 pos++;
8562 used = hwaddr_aton2(pos, bssid);
8563 if (used < 0)
8564 return -1;
8565 pos += used;
8566
8567 param = os_strstr(pos, " freq=");
8568 if (param) {
8569 param += 6;
8570 freq = atoi(param);
8571 }
8572
8573 param = os_strstr(pos, " no_cck=");
8574 if (param) {
8575 param += 8;
8576 no_cck = atoi(param);
8577 }
8578
8579 param = os_strstr(pos, " wait_time=");
8580 if (param) {
8581 param += 11;
8582 wait_time = atoi(param);
8583 }
8584
8585 param = os_strstr(pos, " action=");
8586 if (param == NULL)
8587 return -1;
8588 param += 8;
8589
8590 len = os_strlen(param);
8591 if (len & 1)
8592 return -1;
8593 len /= 2;
8594
8595 buf = os_malloc(len);
8596 if (buf == NULL)
8597 return -1;
8598
8599 if (hexstr2bin(param, buf, len) < 0) {
8600 os_free(buf);
8601 return -1;
8602 }
8603
8604 res = offchannel_send_action(wpa_s, freq, da, wpa_s->own_addr, bssid,
8605 buf, len, wait_time,
8606 wpas_ctrl_iface_mgmt_tx_cb, no_cck);
8607 os_free(buf);
8608 return res;
8609}
8610
8611
8612static void wpas_ctrl_iface_mgmt_tx_done(struct wpa_supplicant *wpa_s)
8613{
8614 wpa_printf(MSG_DEBUG, "External MGMT TX - done waiting");
8615 offchannel_send_action_done(wpa_s);
8616}
8617
ad12f2f4 8618
4de70e23
JM
8619static int wpas_ctrl_iface_mgmt_rx_process(struct wpa_supplicant *wpa_s,
8620 char *cmd)
8621{
8622 char *pos, *param;
8623 size_t len;
8624 u8 *buf;
8625 int freq = 0, datarate = 0, ssi_signal = 0;
8626 union wpa_event_data event;
8627
8628 if (!wpa_s->ext_mgmt_frame_handling)
8629 return -1;
8630
8631 /* freq=<MHz> datarate=<val> ssi_signal=<val> frame=<frame hexdump> */
8632
8633 wpa_printf(MSG_DEBUG, "External MGMT RX process: %s", cmd);
8634
8635 pos = cmd;
8636 param = os_strstr(pos, "freq=");
8637 if (param) {
8638 param += 5;
8639 freq = atoi(param);
8640 }
8641
8642 param = os_strstr(pos, " datarate=");
8643 if (param) {
8644 param += 10;
8645 datarate = atoi(param);
8646 }
8647
8648 param = os_strstr(pos, " ssi_signal=");
8649 if (param) {
8650 param += 12;
8651 ssi_signal = atoi(param);
8652 }
8653
8654 param = os_strstr(pos, " frame=");
8655 if (param == NULL)
8656 return -1;
8657 param += 7;
8658
8659 len = os_strlen(param);
8660 if (len & 1)
8661 return -1;
8662 len /= 2;
8663
8664 buf = os_malloc(len);
8665 if (buf == NULL)
8666 return -1;
8667
8668 if (hexstr2bin(param, buf, len) < 0) {
8669 os_free(buf);
8670 return -1;
8671 }
8672
8673 os_memset(&event, 0, sizeof(event));
8674 event.rx_mgmt.freq = freq;
8675 event.rx_mgmt.frame = buf;
8676 event.rx_mgmt.frame_len = len;
8677 event.rx_mgmt.ssi_signal = ssi_signal;
8678 event.rx_mgmt.datarate = datarate;
8679 wpa_s->ext_mgmt_frame_handling = 0;
8680 wpa_supplicant_event(wpa_s, EVENT_RX_MGMT, &event);
8681 wpa_s->ext_mgmt_frame_handling = 1;
8682
8683 os_free(buf);
8684
8685 return 0;
8686}
8687
8688
e4a3e1d0
JM
8689static int wpas_ctrl_iface_driver_scan_res(struct wpa_supplicant *wpa_s,
8690 char *param)
8691{
8692 struct wpa_scan_res *res;
8693 struct os_reltime now;
8694 char *pos, *end;
1fb4437c 8695 int ret = -1;
e4a3e1d0
JM
8696
8697 if (!param)
8698 return -1;
8699
8700 if (os_strcmp(param, "START") == 0) {
8701 wpa_bss_update_start(wpa_s);
8702 return 0;
8703 }
8704
8705 if (os_strcmp(param, "END") == 0) {
8706 wpa_bss_update_end(wpa_s, NULL, 1);
8707 return 0;
8708 }
8709
8710 if (os_strncmp(param, "BSS ", 4) != 0)
8711 return -1;
8712 param += 3;
8713
8714 res = os_zalloc(sizeof(*res) + os_strlen(param) / 2);
8715 if (!res)
8716 return -1;
8717
8718 pos = os_strstr(param, " flags=");
8719 if (pos)
8720 res->flags = strtol(pos + 7, NULL, 16);
8721
8722 pos = os_strstr(param, " bssid=");
1fb4437c
JM
8723 if (pos && hwaddr_aton(pos + 7, res->bssid))
8724 goto fail;
e4a3e1d0
JM
8725
8726 pos = os_strstr(param, " freq=");
8727 if (pos)
8728 res->freq = atoi(pos + 6);
8729
8730 pos = os_strstr(param, " beacon_int=");
8731 if (pos)
8732 res->beacon_int = atoi(pos + 12);
8733
8734 pos = os_strstr(param, " caps=");
8735 if (pos)
8736 res->caps = strtol(pos + 6, NULL, 16);
8737
8738 pos = os_strstr(param, " qual=");
8739 if (pos)
8740 res->qual = atoi(pos + 6);
8741
8742 pos = os_strstr(param, " noise=");
8743 if (pos)
8744 res->noise = atoi(pos + 7);
8745
8746 pos = os_strstr(param, " level=");
8747 if (pos)
8748 res->level = atoi(pos + 7);
8749
8750 pos = os_strstr(param, " tsf=");
8751 if (pos)
8752 res->tsf = strtoll(pos + 5, NULL, 16);
8753
8754 pos = os_strstr(param, " age=");
8755 if (pos)
8756 res->age = atoi(pos + 5);
8757
8758 pos = os_strstr(param, " est_throughput=");
8759 if (pos)
8760 res->est_throughput = atoi(pos + 16);
8761
8762 pos = os_strstr(param, " snr=");
8763 if (pos)
8764 res->snr = atoi(pos + 5);
8765
8766 pos = os_strstr(param, " parent_tsf=");
8767 if (pos)
8768 res->parent_tsf = strtoll(pos + 7, NULL, 16);
8769
8770 pos = os_strstr(param, " tsf_bssid=");
1fb4437c
JM
8771 if (pos && hwaddr_aton(pos + 11, res->tsf_bssid))
8772 goto fail;
e4a3e1d0
JM
8773
8774 pos = os_strstr(param, " ie=");
8775 if (pos) {
8776 pos += 4;
8777 end = os_strchr(pos, ' ');
8778 if (!end)
8779 end = pos + os_strlen(pos);
8780 res->ie_len = (end - pos) / 2;
1fb4437c
JM
8781 if (hexstr2bin(pos, (u8 *) (res + 1), res->ie_len))
8782 goto fail;
e4a3e1d0
JM
8783 }
8784
8785 pos = os_strstr(param, " beacon_ie=");
8786 if (pos) {
8787 pos += 11;
8788 end = os_strchr(pos, ' ');
8789 if (!end)
8790 end = pos + os_strlen(pos);
8791 res->beacon_ie_len = (end - pos) / 2;
1fb4437c
JM
8792 if (hexstr2bin(pos, ((u8 *) (res + 1)) + res->ie_len,
8793 res->beacon_ie_len))
8794 goto fail;
e4a3e1d0
JM
8795 }
8796
8797 os_get_reltime(&now);
8798 wpa_bss_update_scan_res(wpa_s, res, &now);
1fb4437c
JM
8799 ret = 0;
8800fail:
e4a3e1d0
JM
8801 os_free(res);
8802
1fb4437c 8803 return ret;
e4a3e1d0
JM
8804}
8805
8806
ad12f2f4
JM
8807static int wpas_ctrl_iface_driver_event(struct wpa_supplicant *wpa_s, char *cmd)
8808{
8809 char *pos, *param;
8810 union wpa_event_data event;
8811 enum wpa_event_type ev;
8812
8813 /* <event name> [parameters..] */
8814
8815 wpa_dbg(wpa_s, MSG_DEBUG, "Testing - external driver event: %s", cmd);
8816
8817 pos = cmd;
8818 param = os_strchr(pos, ' ');
8819 if (param)
8820 *param++ = '\0';
8821
8822 os_memset(&event, 0, sizeof(event));
8823
8824 if (os_strcmp(cmd, "INTERFACE_ENABLED") == 0) {
8825 ev = EVENT_INTERFACE_ENABLED;
8826 } else if (os_strcmp(cmd, "INTERFACE_DISABLED") == 0) {
8827 ev = EVENT_INTERFACE_DISABLED;
7bb70909
JM
8828 } else if (os_strcmp(cmd, "AVOID_FREQUENCIES") == 0) {
8829 ev = EVENT_AVOID_FREQUENCIES;
8830 if (param == NULL)
8831 param = "";
8832 if (freq_range_list_parse(&event.freq_range, param) < 0)
8833 return -1;
8834 wpa_supplicant_event(wpa_s, ev, &event);
8835 os_free(event.freq_range.range);
8836 return 0;
e4a3e1d0
JM
8837 } else if (os_strcmp(cmd, "SCAN_RES") == 0) {
8838 return wpas_ctrl_iface_driver_scan_res(wpa_s, param);
ad12f2f4
JM
8839 } else {
8840 wpa_dbg(wpa_s, MSG_DEBUG, "Testing - unknown driver event: %s",
8841 cmd);
8842 return -1;
8843 }
8844
8845 wpa_supplicant_event(wpa_s, ev, &event);
8846
8847 return 0;
8848}
8849
9d4ff04a
JM
8850
8851static int wpas_ctrl_iface_eapol_rx(struct wpa_supplicant *wpa_s, char *cmd)
8852{
8853 char *pos;
8854 u8 src[ETH_ALEN], *buf;
8855 int used;
8856 size_t len;
8857
8858 wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
8859
8860 pos = cmd;
8861 used = hwaddr_aton2(pos, src);
8862 if (used < 0)
8863 return -1;
8864 pos += used;
8865 while (*pos == ' ')
8866 pos++;
8867
8868 len = os_strlen(pos);
8869 if (len & 1)
8870 return -1;
8871 len /= 2;
8872
8873 buf = os_malloc(len);
8874 if (buf == NULL)
8875 return -1;
8876
8877 if (hexstr2bin(pos, buf, len) < 0) {
8878 os_free(buf);
8879 return -1;
8880 }
8881
8882 wpa_supplicant_rx_eapol(wpa_s, src, buf, len);
8883 os_free(buf);
8884
8885 return 0;
8886}
8887
4a6cc862
JM
8888
8889static u16 ipv4_hdr_checksum(const void *buf, size_t len)
8890{
8891 size_t i;
8892 u32 sum = 0;
8893 const u16 *pos = buf;
8894
8895 for (i = 0; i < len / 2; i++)
8896 sum += *pos++;
8897
8898 while (sum >> 16)
8899 sum = (sum & 0xffff) + (sum >> 16);
8900
8901 return sum ^ 0xffff;
8902}
8903
8904
8905#define HWSIM_PACKETLEN 1500
8906#define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
8907
0dbe22be
JM
8908static void wpas_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf,
8909 size_t len)
4a6cc862
JM
8910{
8911 struct wpa_supplicant *wpa_s = ctx;
8912 const struct ether_header *eth;
75352270 8913 struct iphdr ip;
4a6cc862
JM
8914 const u8 *pos;
8915 unsigned int i;
93701b4a 8916 char extra[30];
4a6cc862 8917
93701b4a
JM
8918 if (len < sizeof(*eth) + sizeof(ip) || len > HWSIM_PACKETLEN) {
8919 wpa_printf(MSG_DEBUG,
8920 "test data: RX - ignore unexpected length %d",
8921 (int) len);
4a6cc862 8922 return;
93701b4a 8923 }
4a6cc862
JM
8924
8925 eth = (const struct ether_header *) buf;
75352270
JM
8926 os_memcpy(&ip, eth + 1, sizeof(ip));
8927 pos = &buf[sizeof(*eth) + sizeof(ip)];
4a6cc862 8928
75352270 8929 if (ip.ihl != 5 || ip.version != 4 ||
93701b4a
JM
8930 ntohs(ip.tot_len) > HWSIM_IP_LEN) {
8931 wpa_printf(MSG_DEBUG,
8932 "test data: RX - ignore unexpect IP header");
4a6cc862 8933 return;
93701b4a 8934 }
4a6cc862 8935
93701b4a
JM
8936 for (i = 0; i < ntohs(ip.tot_len) - sizeof(ip); i++) {
8937 if (*pos != (u8) i) {
8938 wpa_printf(MSG_DEBUG,
8939 "test data: RX - ignore mismatching payload");
4a6cc862 8940 return;
93701b4a 8941 }
4a6cc862
JM
8942 pos++;
8943 }
93701b4a
JM
8944 extra[0] = '\0';
8945 if (ntohs(ip.tot_len) != HWSIM_IP_LEN)
8946 os_snprintf(extra, sizeof(extra), " len=%d", ntohs(ip.tot_len));
8947 wpa_msg(wpa_s, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR "%s",
8948 MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost), extra);
4a6cc862
JM
8949}
8950
8951
8952static int wpas_ctrl_iface_data_test_config(struct wpa_supplicant *wpa_s,
8953 char *cmd)
8954{
8955 int enabled = atoi(cmd);
ba91e920
MB
8956 char *pos;
8957 const char *ifname;
4a6cc862
JM
8958
8959 if (!enabled) {
8960 if (wpa_s->l2_test) {
8961 l2_packet_deinit(wpa_s->l2_test);
8962 wpa_s->l2_test = NULL;
8963 wpa_dbg(wpa_s, MSG_DEBUG, "test data: Disabled");
8964 }
8965 return 0;
8966 }
8967
8968 if (wpa_s->l2_test)
8969 return 0;
8970
ba91e920
MB
8971 pos = os_strstr(cmd, " ifname=");
8972 if (pos)
8973 ifname = pos + 8;
8974 else
8975 ifname = wpa_s->ifname;
8976
8977 wpa_s->l2_test = l2_packet_init(ifname, wpa_s->own_addr,
4a6cc862
JM
8978 ETHERTYPE_IP, wpas_data_test_rx,
8979 wpa_s, 1);
8980 if (wpa_s->l2_test == NULL)
8981 return -1;
8982
8983 wpa_dbg(wpa_s, MSG_DEBUG, "test data: Enabled");
8984
8985 return 0;
8986}
8987
8988
8989static int wpas_ctrl_iface_data_test_tx(struct wpa_supplicant *wpa_s, char *cmd)
8990{
8991 u8 dst[ETH_ALEN], src[ETH_ALEN];
93701b4a 8992 char *pos, *pos2;
4a6cc862
JM
8993 int used;
8994 long int val;
8995 u8 tos;
75352270 8996 u8 buf[2 + HWSIM_PACKETLEN];
4a6cc862
JM
8997 struct ether_header *eth;
8998 struct iphdr *ip;
8999 u8 *dpos;
9000 unsigned int i;
93701b4a 9001 size_t send_len = HWSIM_IP_LEN;
4a6cc862
JM
9002
9003 if (wpa_s->l2_test == NULL)
9004 return -1;
9005
93701b4a 9006 /* format: <dst> <src> <tos> [len=<length>] */
4a6cc862
JM
9007
9008 pos = cmd;
9009 used = hwaddr_aton2(pos, dst);
9010 if (used < 0)
9011 return -1;
9012 pos += used;
9013 while (*pos == ' ')
9014 pos++;
9015 used = hwaddr_aton2(pos, src);
9016 if (used < 0)
9017 return -1;
9018 pos += used;
9019
93701b4a 9020 val = strtol(pos, &pos2, 0);
4a6cc862
JM
9021 if (val < 0 || val > 0xff)
9022 return -1;
9023 tos = val;
9024
93701b4a
JM
9025 pos = os_strstr(pos2, " len=");
9026 if (pos) {
9027 i = atoi(pos + 5);
9028 if (i < sizeof(*ip) || i > HWSIM_IP_LEN)
9029 return -1;
9030 send_len = i;
9031 }
9032
75352270 9033 eth = (struct ether_header *) &buf[2];
4a6cc862
JM
9034 os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
9035 os_memcpy(eth->ether_shost, src, ETH_ALEN);
9036 eth->ether_type = htons(ETHERTYPE_IP);
9037 ip = (struct iphdr *) (eth + 1);
9038 os_memset(ip, 0, sizeof(*ip));
9039 ip->ihl = 5;
9040 ip->version = 4;
9041 ip->ttl = 64;
9042 ip->tos = tos;
93701b4a 9043 ip->tot_len = htons(send_len);
4a6cc862 9044 ip->protocol = 1;
66f1e078
JM
9045 ip->saddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
9046 ip->daddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
4a6cc862
JM
9047 ip->check = ipv4_hdr_checksum(ip, sizeof(*ip));
9048 dpos = (u8 *) (ip + 1);
93701b4a 9049 for (i = 0; i < send_len - sizeof(*ip); i++)
4a6cc862
JM
9050 *dpos++ = i;
9051
75352270 9052 if (l2_packet_send(wpa_s->l2_test, dst, ETHERTYPE_IP, &buf[2],
93701b4a 9053 sizeof(struct ether_header) + send_len) < 0)
4a6cc862
JM
9054 return -1;
9055
9056 wpa_dbg(wpa_s, MSG_DEBUG, "test data: TX dst=" MACSTR " src=" MACSTR
9057 " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
9058
9059 return 0;
9060}
9061
fc0ef7c0
JM
9062
9063static int wpas_ctrl_iface_data_test_frame(struct wpa_supplicant *wpa_s,
9064 char *cmd)
9065{
9066 u8 *buf;
9067 struct ether_header *eth;
9068 struct l2_packet_data *l2 = NULL;
9069 size_t len;
9070 u16 ethertype;
9071 int res = -1;
9072
9073 len = os_strlen(cmd);
9074 if (len & 1 || len < ETH_HLEN * 2)
9075 return -1;
9076 len /= 2;
9077
9078 buf = os_malloc(len);
9079 if (buf == NULL)
9080 return -1;
9081
9082 if (hexstr2bin(cmd, buf, len) < 0)
9083 goto done;
9084
9085 eth = (struct ether_header *) buf;
9086 ethertype = ntohs(eth->ether_type);
9087
9088 l2 = l2_packet_init(wpa_s->ifname, wpa_s->own_addr, ethertype,
9089 wpas_data_test_rx, wpa_s, 1);
9090 if (l2 == NULL)
9091 goto done;
9092
9093 res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
9094 wpa_dbg(wpa_s, MSG_DEBUG, "test data: TX frame res=%d", res);
9095done:
9096 if (l2)
9097 l2_packet_deinit(l2);
9098 os_free(buf);
9099
9100 return res < 0 ? -1 : 0;
9101}
9102
a156ffda
JM
9103
9104static int wpas_ctrl_test_alloc_fail(struct wpa_supplicant *wpa_s, char *cmd)
9105{
9106#ifdef WPA_TRACE_BFD
a156ffda
JM
9107 char *pos;
9108
9109 wpa_trace_fail_after = atoi(cmd);
9110 pos = os_strchr(cmd, ':');
9111 if (pos) {
9112 pos++;
9113 os_strlcpy(wpa_trace_fail_func, pos,
9114 sizeof(wpa_trace_fail_func));
9115 } else {
9116 wpa_trace_fail_after = 0;
9117 }
9118 return 0;
9119#else /* WPA_TRACE_BFD */
9120 return -1;
9121#endif /* WPA_TRACE_BFD */
9122}
9123
9124
9125static int wpas_ctrl_get_alloc_fail(struct wpa_supplicant *wpa_s,
9126 char *buf, size_t buflen)
9127{
9128#ifdef WPA_TRACE_BFD
a156ffda
JM
9129 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after,
9130 wpa_trace_fail_func);
9131#else /* WPA_TRACE_BFD */
9132 return -1;
9133#endif /* WPA_TRACE_BFD */
9134}
9135
2da52565
JM
9136
9137static int wpas_ctrl_test_fail(struct wpa_supplicant *wpa_s, char *cmd)
9138{
9139#ifdef WPA_TRACE_BFD
2da52565
JM
9140 char *pos;
9141
9142 wpa_trace_test_fail_after = atoi(cmd);
9143 pos = os_strchr(cmd, ':');
9144 if (pos) {
9145 pos++;
9146 os_strlcpy(wpa_trace_test_fail_func, pos,
9147 sizeof(wpa_trace_test_fail_func));
9148 } else {
9149 wpa_trace_test_fail_after = 0;
9150 }
9151 return 0;
9152#else /* WPA_TRACE_BFD */
9153 return -1;
9154#endif /* WPA_TRACE_BFD */
9155}
9156
9157
9158static int wpas_ctrl_get_fail(struct wpa_supplicant *wpa_s,
9159 char *buf, size_t buflen)
9160{
9161#ifdef WPA_TRACE_BFD
2da52565
JM
9162 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_test_fail_after,
9163 wpa_trace_test_fail_func);
9164#else /* WPA_TRACE_BFD */
9165 return -1;
9166#endif /* WPA_TRACE_BFD */
9167}
9168
a530fe77
JM
9169
9170static void wpas_ctrl_event_test_cb(void *eloop_ctx, void *timeout_ctx)
9171{
9172 struct wpa_supplicant *wpa_s = eloop_ctx;
9173 int i, count = (intptr_t) timeout_ctx;
9174
9175 wpa_printf(MSG_DEBUG, "TEST: Send %d control interface event messages",
9176 count);
9177 for (i = 0; i < count; i++) {
9178 wpa_msg_ctrl(wpa_s, MSG_INFO, "TEST-EVENT-MESSAGE %d/%d",
9179 i + 1, count);
9180 }
9181}
9182
9183
9184static int wpas_ctrl_event_test(struct wpa_supplicant *wpa_s, const char *cmd)
9185{
9186 int count;
9187
9188 count = atoi(cmd);
9189 if (count <= 0)
9190 return -1;
9191
9192 return eloop_register_timeout(0, 0, wpas_ctrl_event_test_cb, wpa_s,
9193 (void *) (intptr_t) count);
9194}
9195
651c6a84
JM
9196
9197static int wpas_ctrl_test_assoc_ie(struct wpa_supplicant *wpa_s,
9198 const char *cmd)
9199{
9200 struct wpabuf *buf;
9201 size_t len;
9202
9203 len = os_strlen(cmd);
9204 if (len & 1)
9205 return -1;
9206 len /= 2;
9207
9208 if (len == 0) {
9209 buf = NULL;
9210 } else {
9211 buf = wpabuf_alloc(len);
9212 if (buf == NULL)
9213 return -1;
9214
9215 if (hexstr2bin(cmd, wpabuf_put(buf, len), len) < 0) {
9216 wpabuf_free(buf);
9217 return -1;
9218 }
9219 }
9220
9221 wpa_sm_set_test_assoc_ie(wpa_s->wpa, buf);
9222 return 0;
9223}
9224
16579769
JM
9225
9226static int wpas_ctrl_reset_pn(struct wpa_supplicant *wpa_s)
9227{
9228 u8 zero[WPA_TK_MAX_LEN];
9229
9230 if (wpa_s->last_tk_alg == WPA_ALG_NONE)
9231 return -1;
9232
9233 wpa_printf(MSG_INFO, "TESTING: Reset PN");
9234 os_memset(zero, 0, sizeof(zero));
9235
9236 /* First, use a zero key to avoid any possible duplicate key avoidance
9237 * in the driver. */
9238 if (wpa_drv_set_key(wpa_s, wpa_s->last_tk_alg, wpa_s->last_tk_addr,
9239 wpa_s->last_tk_key_idx, 1, zero, 6,
9240 zero, wpa_s->last_tk_len) < 0)
9241 return -1;
9242
9243 /* Set the previously configured key to reset its TSC/RSC */
9244 return wpa_drv_set_key(wpa_s, wpa_s->last_tk_alg, wpa_s->last_tk_addr,
9245 wpa_s->last_tk_key_idx, 1, zero, 6,
9246 wpa_s->last_tk, wpa_s->last_tk_len);
9247}
9248
751f5b29
JM
9249
9250static int wpas_ctrl_key_request(struct wpa_supplicant *wpa_s, const char *cmd)
9251{
9252 const char *pos = cmd;
9253 int error, pairwise;
9254
9255 error = atoi(pos);
9256 pos = os_strchr(pos, ' ');
9257 if (!pos)
9258 return -1;
9259 pairwise = atoi(pos);
9260 wpa_sm_key_request(wpa_s->wpa, error, pairwise);
9261 return 0;
9262}
9263
daa40960
JM
9264
9265static int wpas_ctrl_resend_assoc(struct wpa_supplicant *wpa_s)
9266{
9267#ifdef CONFIG_SME
9268 struct wpa_driver_associate_params params;
9269 int ret;
9270
9271 os_memset(&params, 0, sizeof(params));
9272 params.bssid = wpa_s->bssid;
9273 params.ssid = wpa_s->sme.ssid;
9274 params.ssid_len = wpa_s->sme.ssid_len;
9275 params.freq.freq = wpa_s->sme.freq;
9276 if (wpa_s->last_assoc_req_wpa_ie) {
9277 params.wpa_ie = wpabuf_head(wpa_s->last_assoc_req_wpa_ie);
9278 params.wpa_ie_len = wpabuf_len(wpa_s->last_assoc_req_wpa_ie);
9279 }
9280 params.pairwise_suite = wpa_s->pairwise_cipher;
9281 params.group_suite = wpa_s->group_cipher;
9282 params.mgmt_group_suite = wpa_s->mgmt_group_cipher;
9283 params.key_mgmt_suite = wpa_s->key_mgmt;
9284 params.wpa_proto = wpa_s->wpa_proto;
9285 params.mgmt_frame_protection = wpa_s->sme.mfp;
9286 params.rrm_used = wpa_s->rrm.rrm_used;
9287 if (wpa_s->sme.prev_bssid_set)
9288 params.prev_bssid = wpa_s->sme.prev_bssid;
9289 wpa_printf(MSG_INFO, "TESTING: Resend association request");
9290 ret = wpa_drv_associate(wpa_s, &params);
9291 wpa_s->testing_resend_assoc = 1;
9292 return ret;
9293#else /* CONFIG_SME */
9294 return -1;
9295#endif /* CONFIG_SME */
9296}
9297
60b893df
JM
9298#endif /* CONFIG_TESTING_OPTIONS */
9299
9300
86bd36f0
JM
9301static int wpas_ctrl_vendor_elem_add(struct wpa_supplicant *wpa_s, char *cmd)
9302{
9303 char *pos = cmd;
9304 int frame;
9305 size_t len;
9306 struct wpabuf *buf;
9307 struct ieee802_11_elems elems;
9308
9309 frame = atoi(pos);
9310 if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
9311 return -1;
af041f99 9312 wpa_s = wpas_vendor_elem(wpa_s, frame);
86bd36f0
JM
9313
9314 pos = os_strchr(pos, ' ');
9315 if (pos == NULL)
9316 return -1;
9317 pos++;
9318
9319 len = os_strlen(pos);
9320 if (len == 0)
9321 return 0;
9322 if (len & 1)
9323 return -1;
9324 len /= 2;
9325
9326 buf = wpabuf_alloc(len);
9327 if (buf == NULL)
9328 return -1;
9329
9330 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
9331 wpabuf_free(buf);
9332 return -1;
9333 }
9334
9335 if (ieee802_11_parse_elems(wpabuf_head_u8(buf), len, &elems, 0) ==
9336 ParseFailed) {
9337 wpabuf_free(buf);
9338 return -1;
9339 }
9340
9341 if (wpa_s->vendor_elem[frame] == NULL) {
9342 wpa_s->vendor_elem[frame] = buf;
af041f99 9343 wpas_vendor_elem_update(wpa_s);
86bd36f0
JM
9344 return 0;
9345 }
9346
9347 if (wpabuf_resize(&wpa_s->vendor_elem[frame], len) < 0) {
9348 wpabuf_free(buf);
9349 return -1;
9350 }
9351
9352 wpabuf_put_buf(wpa_s->vendor_elem[frame], buf);
9353 wpabuf_free(buf);
af041f99 9354 wpas_vendor_elem_update(wpa_s);
86bd36f0
JM
9355
9356 return 0;
9357}
9358
9359
9360static int wpas_ctrl_vendor_elem_get(struct wpa_supplicant *wpa_s, char *cmd,
9361 char *buf, size_t buflen)
9362{
9363 int frame = atoi(cmd);
9364
9365 if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
9366 return -1;
af041f99 9367 wpa_s = wpas_vendor_elem(wpa_s, frame);
86bd36f0
JM
9368
9369 if (wpa_s->vendor_elem[frame] == NULL)
9370 return 0;
9371
9372 return wpa_snprintf_hex(buf, buflen,
9373 wpabuf_head_u8(wpa_s->vendor_elem[frame]),
9374 wpabuf_len(wpa_s->vendor_elem[frame]));
9375}
9376
9377
9378static int wpas_ctrl_vendor_elem_remove(struct wpa_supplicant *wpa_s, char *cmd)
9379{
9380 char *pos = cmd;
9381 int frame;
9382 size_t len;
9383 u8 *buf;
9384 struct ieee802_11_elems elems;
af041f99 9385 int res;
86bd36f0
JM
9386
9387 frame = atoi(pos);
9388 if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
9389 return -1;
af041f99 9390 wpa_s = wpas_vendor_elem(wpa_s, frame);
86bd36f0
JM
9391
9392 pos = os_strchr(pos, ' ');
9393 if (pos == NULL)
9394 return -1;
9395 pos++;
9396
9397 if (*pos == '*') {
9398 wpabuf_free(wpa_s->vendor_elem[frame]);
9399 wpa_s->vendor_elem[frame] = NULL;
af041f99 9400 wpas_vendor_elem_update(wpa_s);
86bd36f0
JM
9401 return 0;
9402 }
9403
9404 if (wpa_s->vendor_elem[frame] == NULL)
9405 return -1;
9406
9407 len = os_strlen(pos);
9408 if (len == 0)
9409 return 0;
9410 if (len & 1)
9411 return -1;
9412 len /= 2;
9413
9414 buf = os_malloc(len);
9415 if (buf == NULL)
9416 return -1;
9417
9418 if (hexstr2bin(pos, buf, len) < 0) {
9419 os_free(buf);
9420 return -1;
9421 }
9422
9423 if (ieee802_11_parse_elems(buf, len, &elems, 0) == ParseFailed) {
9424 os_free(buf);
9425 return -1;
9426 }
9427
af041f99 9428 res = wpas_vendor_elem_remove(wpa_s, frame, buf, len);
86bd36f0 9429 os_free(buf);
af041f99 9430 return res;
86bd36f0
JM
9431}
9432
9433
f4b8bfae
AK
9434static void wpas_ctrl_neighbor_rep_cb(void *ctx, struct wpabuf *neighbor_rep)
9435{
9436 struct wpa_supplicant *wpa_s = ctx;
cf667c66
IP
9437 size_t len;
9438 const u8 *data;
f4b8bfae 9439
cf667c66
IP
9440 /*
9441 * Neighbor Report element (IEEE P802.11-REVmc/D5.0)
9442 * BSSID[6]
9443 * BSSID Information[4]
9444 * Operating Class[1]
9445 * Channel Number[1]
9446 * PHY Type[1]
9447 * Optional Subelements[variable]
9448 */
9449#define NR_IE_MIN_LEN (ETH_ALEN + 4 + 1 + 1 + 1)
9450
9451 if (!neighbor_rep || wpabuf_len(neighbor_rep) == 0) {
f4b8bfae 9452 wpa_msg_ctrl(wpa_s, MSG_INFO, RRM_EVENT_NEIGHBOR_REP_FAILED);
cf667c66
IP
9453 goto out;
9454 }
9455
9456 data = wpabuf_head_u8(neighbor_rep);
9457 len = wpabuf_len(neighbor_rep);
9458
9459 while (len >= 2 + NR_IE_MIN_LEN) {
9460 const u8 *nr;
9461 char lci[256 * 2 + 1];
9462 char civic[256 * 2 + 1];
9463 u8 nr_len = data[1];
9464 const u8 *pos = data, *end;
9465
9466 if (pos[0] != WLAN_EID_NEIGHBOR_REPORT ||
9467 nr_len < NR_IE_MIN_LEN) {
9468 wpa_printf(MSG_DEBUG,
9469 "CTRL: Invalid Neighbor Report element: id=%u len=%u",
9470 data[0], nr_len);
9471 goto out;
9472 }
9473
9474 if (2U + nr_len > len) {
9475 wpa_printf(MSG_DEBUG,
9476 "CTRL: Invalid Neighbor Report element: id=%u len=%zu nr_len=%u",
9477 data[0], len, nr_len);
9478 goto out;
9479 }
9480 pos += 2;
9481 end = pos + nr_len;
9482
9483 nr = pos;
9484 pos += NR_IE_MIN_LEN;
9485
9486 lci[0] = '\0';
9487 civic[0] = '\0';
9488 while (end - pos > 2) {
9489 u8 s_id, s_len;
9490
9491 s_id = *pos++;
9492 s_len = *pos++;
9493 if (s_len > end - pos)
9494 goto out;
9495 if (s_id == WLAN_EID_MEASURE_REPORT && s_len > 3) {
9496 /* Measurement Token[1] */
9497 /* Measurement Report Mode[1] */
9498 /* Measurement Type[1] */
9499 /* Measurement Report[variable] */
9500 switch (pos[2]) {
9501 case MEASURE_TYPE_LCI:
9502 if (lci[0])
9503 break;
9504 wpa_snprintf_hex(lci, sizeof(lci),
9505 pos, s_len);
9506 break;
9507 case MEASURE_TYPE_LOCATION_CIVIC:
9508 if (civic[0])
9509 break;
9510 wpa_snprintf_hex(civic, sizeof(civic),
9511 pos, s_len);
9512 break;
9513 }
9514 }
9515
9516 pos += s_len;
9517 }
9518
9519 wpa_msg(wpa_s, MSG_INFO, RRM_EVENT_NEIGHBOR_REP_RXED
9520 "bssid=" MACSTR
9521 " info=0x%x op_class=%u chan=%u phy_type=%u%s%s%s%s",
9522 MAC2STR(nr), WPA_GET_LE32(nr + ETH_ALEN),
9523 nr[ETH_ALEN + 4], nr[ETH_ALEN + 5],
9524 nr[ETH_ALEN + 6],
9525 lci[0] ? " lci=" : "", lci,
9526 civic[0] ? " civic=" : "", civic);
9527
9528 data = end;
9529 len -= 2 + nr_len;
f4b8bfae 9530 }
cf667c66
IP
9531
9532out:
9533 wpabuf_free(neighbor_rep);
f4b8bfae
AK
9534}
9535
9536
6a4f0ed7
JM
9537static int wpas_ctrl_iface_send_neighbor_rep(struct wpa_supplicant *wpa_s,
9538 char *cmd)
f4b8bfae 9539{
d41a5352
DS
9540 struct wpa_ssid_value ssid, *ssid_p = NULL;
9541 int ret, lci = 0, civic = 0;
9542 char *ssid_s;
4c4b2305 9543
d41a5352
DS
9544 ssid_s = os_strstr(cmd, "ssid=");
9545 if (ssid_s) {
9546 if (ssid_parse(ssid_s + 5, &ssid)) {
9547 wpa_printf(MSG_ERROR,
9548 "CTRL: Send Neighbor Report: bad SSID");
4c4b2305 9549 return -1;
d41a5352
DS
9550 }
9551
4c4b2305 9552 ssid_p = &ssid;
d41a5352
DS
9553
9554 /*
9555 * Move cmd after the SSID text that may include "lci" or
9556 * "civic".
9557 */
9558 cmd = os_strchr(ssid_s + 6, ssid_s[5] == '"' ? '"' : ' ');
9559 if (cmd)
9560 cmd++;
9561
4c4b2305
AK
9562 }
9563
d41a5352
DS
9564 if (cmd && os_strstr(cmd, "lci"))
9565 lci = 1;
9566
9567 if (cmd && os_strstr(cmd, "civic"))
9568 civic = 1;
9569
9570 ret = wpas_rrm_send_neighbor_rep_request(wpa_s, ssid_p, lci, civic,
4c4b2305
AK
9571 wpas_ctrl_neighbor_rep_cb,
9572 wpa_s);
9573
9574 return ret;
f4b8bfae
AK
9575}
9576
9577
65d9a5e2
JM
9578static int wpas_ctrl_iface_erp_flush(struct wpa_supplicant *wpa_s)
9579{
9580 eapol_sm_erp_flush(wpa_s->eapol);
9581 return 0;
9582}
9583
9584
fb375883
IP
9585static int wpas_ctrl_iface_mac_rand_scan(struct wpa_supplicant *wpa_s,
9586 char *cmd)
9587{
9588 char *token, *context = NULL;
9589 unsigned int enable = ~0, type = 0;
9590 u8 _addr[ETH_ALEN], _mask[ETH_ALEN];
9591 u8 *addr = NULL, *mask = NULL;
9592
9593 while ((token = str_token(cmd, " ", &context))) {
9594 if (os_strcasecmp(token, "scan") == 0) {
9595 type |= MAC_ADDR_RAND_SCAN;
9596 } else if (os_strcasecmp(token, "sched") == 0) {
9597 type |= MAC_ADDR_RAND_SCHED_SCAN;
9598 } else if (os_strcasecmp(token, "pno") == 0) {
9599 type |= MAC_ADDR_RAND_PNO;
9600 } else if (os_strcasecmp(token, "all") == 0) {
9601 type = wpa_s->mac_addr_rand_supported;
9602 } else if (os_strncasecmp(token, "enable=", 7) == 0) {
9603 enable = atoi(token + 7);
9604 } else if (os_strncasecmp(token, "addr=", 5) == 0) {
9605 addr = _addr;
9606 if (hwaddr_aton(token + 5, addr)) {
9607 wpa_printf(MSG_INFO,
9608 "CTRL: Invalid MAC address: %s",
9609 token);
9610 return -1;
9611 }
9612 } else if (os_strncasecmp(token, "mask=", 5) == 0) {
9613 mask = _mask;
9614 if (hwaddr_aton(token + 5, mask)) {
9615 wpa_printf(MSG_INFO,
9616 "CTRL: Invalid MAC address mask: %s",
9617 token);
9618 return -1;
9619 }
9620 } else {
9621 wpa_printf(MSG_INFO,
9622 "CTRL: Invalid MAC_RAND_SCAN parameter: %s",
9623 token);
9624 return -1;
9625 }
9626 }
9627
9628 if (!type) {
9629 wpa_printf(MSG_INFO, "CTRL: MAC_RAND_SCAN no type specified");
9630 return -1;
9631 }
9632
fb375883
IP
9633 if (enable > 1) {
9634 wpa_printf(MSG_INFO,
9635 "CTRL: MAC_RAND_SCAN enable=<0/1> not specified");
9636 return -1;
9637 }
9638
91b6eba7
EC
9639 if (!enable)
9640 return wpas_disable_mac_addr_randomization(wpa_s, type);
fb375883 9641
91b6eba7 9642 return wpas_enable_mac_addr_randomization(wpa_s, type, addr, mask);
fb375883
IP
9643}
9644
9645
b8daac18
MH
9646static int wpas_ctrl_iface_pmksa(struct wpa_supplicant *wpa_s,
9647 char *buf, size_t buflen)
9648{
9649 size_t reply_len;
9650
9651 reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, buf, buflen);
9652#ifdef CONFIG_AP
9653 reply_len += wpas_ap_pmksa_cache_list(wpa_s, &buf[reply_len],
9654 buflen - reply_len);
9655#endif /* CONFIG_AP */
9656 return reply_len;
9657}
9658
9659
4c522c77
MH
9660static void wpas_ctrl_iface_pmksa_flush(struct wpa_supplicant *wpa_s)
9661{
9662 wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
9663#ifdef CONFIG_AP
9664 wpas_ap_pmksa_cache_flush(wpa_s);
9665#endif /* CONFIG_AP */
9666}
9667
9668
3459381d
JM
9669#ifdef CONFIG_PMKSA_CACHE_EXTERNAL
9670
9671static int wpas_ctrl_iface_pmksa_get(struct wpa_supplicant *wpa_s,
9672 const char *cmd, char *buf, size_t buflen)
9673{
9674 struct rsn_pmksa_cache_entry *entry;
9675 struct wpa_ssid *ssid;
9676 char *pos, *pos2, *end;
9677 int ret;
9678 struct os_reltime now;
9679
9680 ssid = wpa_config_get_network(wpa_s->conf, atoi(cmd));
9681 if (!ssid)
9682 return -1;
9683
9684 pos = buf;
9685 end = buf + buflen;
9686
9687 os_get_reltime(&now);
9688
9689 /*
9690 * Entry format:
9691 * <BSSID> <PMKID> <PMK> <reauth_time in seconds>
9692 * <expiration in seconds> <akmp> <opportunistic>
b7286c1b 9693 * [FILS Cache Identifier]
3459381d
JM
9694 */
9695
9696 for (entry = wpa_sm_pmksa_cache_head(wpa_s->wpa); entry;
9697 entry = entry->next) {
9698 if (entry->network_ctx != ssid)
9699 continue;
9700
9701 pos2 = pos;
9702 ret = os_snprintf(pos2, end - pos2, MACSTR " ",
9703 MAC2STR(entry->aa));
9704 if (os_snprintf_error(end - pos2, ret))
9705 break;
9706 pos2 += ret;
9707
9708 pos2 += wpa_snprintf_hex(pos2, end - pos2, entry->pmkid,
9709 PMKID_LEN);
9710
9711 ret = os_snprintf(pos2, end - pos2, " ");
9712 if (os_snprintf_error(end - pos2, ret))
9713 break;
9714 pos2 += ret;
9715
9716 pos2 += wpa_snprintf_hex(pos2, end - pos2, entry->pmk,
9717 entry->pmk_len);
9718
9719 ret = os_snprintf(pos2, end - pos2, " %d %d %d %d",
9720 (int) (entry->reauth_time - now.sec),
9721 (int) (entry->expiration - now.sec),
9722 entry->akmp,
9723 entry->opportunistic);
9724 if (os_snprintf_error(end - pos2, ret))
9725 break;
9726 pos2 += ret;
9727
b7286c1b
JM
9728 if (entry->fils_cache_id_set) {
9729 ret = os_snprintf(pos2, end - pos2, " %02x%02x",
9730 entry->fils_cache_id[0],
9731 entry->fils_cache_id[1]);
9732 if (os_snprintf_error(end - pos2, ret))
9733 break;
9734 pos2 += ret;
9735 }
9736
3459381d
JM
9737 ret = os_snprintf(pos2, end - pos2, "\n");
9738 if (os_snprintf_error(end - pos2, ret))
9739 break;
9740 pos2 += ret;
9741
9742 pos = pos2;
9743 }
9744
9745 return pos - buf;
9746}
9747
9748
9749static int wpas_ctrl_iface_pmksa_add(struct wpa_supplicant *wpa_s,
9750 char *cmd)
9751{
9752 struct rsn_pmksa_cache_entry *entry;
9753 struct wpa_ssid *ssid;
9754 char *pos, *pos2;
9755 int ret = -1;
9756 struct os_reltime now;
b7286c1b 9757 int reauth_time = 0, expiration = 0, i;
3459381d
JM
9758
9759 /*
9760 * Entry format:
9761 * <network_id> <BSSID> <PMKID> <PMK> <reauth_time in seconds>
9762 * <expiration in seconds> <akmp> <opportunistic>
b7286c1b 9763 * [FILS Cache Identifier]
3459381d
JM
9764 */
9765
9766 ssid = wpa_config_get_network(wpa_s->conf, atoi(cmd));
9767 if (!ssid)
9768 return -1;
9769
9770 pos = os_strchr(cmd, ' ');
9771 if (!pos)
9772 return -1;
9773 pos++;
9774
9775 entry = os_zalloc(sizeof(*entry));
9776 if (!entry)
9777 return -1;
9778
9779 if (hwaddr_aton(pos, entry->aa))
9780 goto fail;
9781
9782 pos = os_strchr(pos, ' ');
9783 if (!pos)
9784 goto fail;
9785 pos++;
9786
9787 if (hexstr2bin(pos, entry->pmkid, PMKID_LEN) < 0)
9788 goto fail;
9789
9790 pos = os_strchr(pos, ' ');
9791 if (!pos)
9792 goto fail;
9793 pos++;
9794
9795 pos2 = os_strchr(pos, ' ');
9796 if (!pos2)
9797 goto fail;
9798 entry->pmk_len = (pos2 - pos) / 2;
9799 if (entry->pmk_len < PMK_LEN || entry->pmk_len > PMK_LEN_MAX ||
9800 hexstr2bin(pos, entry->pmk, entry->pmk_len) < 0)
9801 goto fail;
9802
9803 pos = os_strchr(pos, ' ');
9804 if (!pos)
9805 goto fail;
9806 pos++;
9807
9808 if (sscanf(pos, "%d %d %d %d", &reauth_time, &expiration,
9809 &entry->akmp, &entry->opportunistic) != 4)
9810 goto fail;
b7286c1b
JM
9811 for (i = 0; i < 4; i++) {
9812 pos = os_strchr(pos, ' ');
9813 if (!pos) {
9814 if (i < 3)
9815 goto fail;
9816 break;
9817 }
9818 pos++;
9819 }
9820 if (pos) {
9821 if (hexstr2bin(pos, entry->fils_cache_id,
9822 FILS_CACHE_ID_LEN) < 0)
9823 goto fail;
9824 entry->fils_cache_id_set = 1;
9825 }
3459381d
JM
9826 os_get_reltime(&now);
9827 entry->expiration = now.sec + expiration;
9828 entry->reauth_time = now.sec + reauth_time;
9829
9830 entry->network_ctx = ssid;
9831
9832 wpa_sm_pmksa_cache_add_entry(wpa_s->wpa, entry);
9833 entry = NULL;
9834 ret = 0;
9835fail:
9836 os_free(entry);
9837 return ret;
9838}
9839
4d77d80e
MH
9840
9841#ifdef CONFIG_MESH
9842
9843static int wpas_ctrl_iface_mesh_pmksa_get(struct wpa_supplicant *wpa_s,
9844 const char *cmd, char *buf,
9845 size_t buflen)
9846{
9847 u8 spa[ETH_ALEN];
9848
9849 if (!wpa_s->ifmsh)
9850 return -1;
9851
9852 if (os_strcasecmp(cmd, "any") == 0)
9853 return wpas_ap_pmksa_cache_list_mesh(wpa_s, NULL, buf, buflen);
9854
9855 if (hwaddr_aton(cmd, spa))
9856 return -1;
9857
9858 return wpas_ap_pmksa_cache_list_mesh(wpa_s, spa, buf, buflen);
9859}
9860
9861
9862static int wpas_ctrl_iface_mesh_pmksa_add(struct wpa_supplicant *wpa_s,
9863 char *cmd)
9864{
9865 /*
9866 * We do not check mesh interface existance because PMKSA should be
9867 * stored before wpa_s->ifmsh creation to suppress commit message
9868 * creation.
9869 */
9870 return wpas_ap_pmksa_cache_add_external(wpa_s, cmd);
9871}
9872
9873#endif /* CONFIG_MESH */
3459381d
JM
9874#endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
9875
9876
5732b770
JM
9877#ifdef CONFIG_FILS
9878static int wpas_ctrl_iface_fils_hlp_req_add(struct wpa_supplicant *wpa_s,
9879 const char *cmd)
9880{
9881 struct fils_hlp_req *req;
9882 const char *pos;
9883
9884 /* format: <dst> <packet starting from ethertype> */
9885
9886 req = os_zalloc(sizeof(*req));
9887 if (!req)
9888 return -1;
9889
9890 if (hwaddr_aton(cmd, req->dst))
9891 goto fail;
9892
9893 pos = os_strchr(cmd, ' ');
9894 if (!pos)
9895 goto fail;
9896 pos++;
9897 req->pkt = wpabuf_parse_bin(pos);
9898 if (!req->pkt)
9899 goto fail;
9900
9901 dl_list_add_tail(&wpa_s->fils_hlp_req, &req->list);
9902 return 0;
9903fail:
9904 wpabuf_free(req->pkt);
9905 os_free(req);
9906 return -1;
9907}
9908#endif /* CONFIG_FILS */
9909
9910
8db9a79d
JM
9911static int wpas_ctrl_cmd_debug_level(const char *cmd)
9912{
9913 if (os_strcmp(cmd, "PING") == 0 ||
9914 os_strncmp(cmd, "BSS ", 4) == 0 ||
9915 os_strncmp(cmd, "GET_NETWORK ", 12) == 0 ||
9916 os_strncmp(cmd, "STATUS", 6) == 0 ||
9917 os_strncmp(cmd, "STA ", 4) == 0 ||
9918 os_strncmp(cmd, "STA-", 4) == 0)
9919 return MSG_EXCESSIVE;
9920 return MSG_DEBUG;
9921}
9922
9923
6fc6879b
JM
9924char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
9925 char *buf, size_t *resp_len)
9926{
9927 char *reply;
b563b388 9928 const int reply_size = 4096;
6fc6879b
JM
9929 int reply_len;
9930
9931 if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
c10e0ccc
JM
9932 os_strncmp(buf, "SET_NETWORK ", 12) == 0 ||
9933 os_strncmp(buf, "PMKSA_ADD ", 10) == 0 ||
9934 os_strncmp(buf, "MESH_PMKSA_ADD ", 15) == 0) {
d31b5ac7
JM
9935 if (wpa_debug_show_keys)
9936 wpa_dbg(wpa_s, MSG_DEBUG,
9937 "Control interface command '%s'", buf);
9938 else
9939 wpa_dbg(wpa_s, MSG_DEBUG,
9940 "Control interface command '%s [REMOVED]'",
9941 os_strncmp(buf, WPA_CTRL_RSP,
9942 os_strlen(WPA_CTRL_RSP)) == 0 ?
c10e0ccc
JM
9943 WPA_CTRL_RSP :
9944 (os_strncmp(buf, "SET_NETWORK ", 12) == 0 ?
9945 "SET_NETWORK" : "key-add"));
d31b5ac7 9946 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ", 16) == 0 ||
679f2e7c 9947 os_strncmp(buf, "NFC_REPORT_HANDOVER", 19) == 0) {
6fc6879b
JM
9948 wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
9949 (const u8 *) buf, os_strlen(buf));
9950 } else {
8db9a79d 9951 int level = wpas_ctrl_cmd_debug_level(buf);
b470b2bf 9952 wpa_dbg(wpa_s, level, "Control interface command '%s'", buf);
6fc6879b
JM
9953 }
9954
9955 reply = os_malloc(reply_size);
9956 if (reply == NULL) {
9957 *resp_len = 1;
9958 return NULL;
9959 }
9960
9961 os_memcpy(reply, "OK\n", 3);
9962 reply_len = 3;
9963
9964 if (os_strcmp(buf, "PING") == 0) {
9965 os_memcpy(reply, "PONG\n", 5);
9966 reply_len = 5;
0eed2a8d
JD
9967 } else if (os_strcmp(buf, "IFNAME") == 0) {
9968 reply_len = os_strlen(wpa_s->ifname);
9969 os_memcpy(reply, wpa_s->ifname, reply_len);
ac6912b5
BG
9970 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
9971 if (wpa_debug_reopen_file() < 0)
9972 reply_len = -1;
77895cd9
JM
9973 } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
9974 wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
6fc6879b
JM
9975 } else if (os_strcmp(buf, "MIB") == 0) {
9976 reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
9977 if (reply_len >= 0) {
5ac73acf
JM
9978 reply_len += eapol_sm_get_mib(wpa_s->eapol,
9979 reply + reply_len,
9980 reply_size - reply_len);
948ba8c2
JM
9981#ifdef CONFIG_MACSEC
9982 reply_len += ieee802_1x_kay_get_mib(
9983 wpa_s->kay, reply + reply_len,
9984 reply_size - reply_len);
9985#endif /* CONFIG_MACSEC */
6fc6879b
JM
9986 }
9987 } else if (os_strncmp(buf, "STATUS", 6) == 0) {
9988 reply_len = wpa_supplicant_ctrl_iface_status(
9989 wpa_s, buf + 6, reply, reply_size);
9990 } else if (os_strcmp(buf, "PMKSA") == 0) {
b8daac18 9991 reply_len = wpas_ctrl_iface_pmksa(wpa_s, reply, reply_size);
79e2b1cc 9992 } else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
4c522c77 9993 wpas_ctrl_iface_pmksa_flush(wpa_s);
3459381d
JM
9994#ifdef CONFIG_PMKSA_CACHE_EXTERNAL
9995 } else if (os_strncmp(buf, "PMKSA_GET ", 10) == 0) {
9996 reply_len = wpas_ctrl_iface_pmksa_get(wpa_s, buf + 10,
9997 reply, reply_size);
9998 } else if (os_strncmp(buf, "PMKSA_ADD ", 10) == 0) {
9999 if (wpas_ctrl_iface_pmksa_add(wpa_s, buf + 10) < 0)
10000 reply_len = -1;
4d77d80e
MH
10001#ifdef CONFIG_MESH
10002 } else if (os_strncmp(buf, "MESH_PMKSA_GET ", 15) == 0) {
10003 reply_len = wpas_ctrl_iface_mesh_pmksa_get(wpa_s, buf + 15,
10004 reply, reply_size);
10005 } else if (os_strncmp(buf, "MESH_PMKSA_ADD ", 15) == 0) {
10006 if (wpas_ctrl_iface_mesh_pmksa_add(wpa_s, buf + 15) < 0)
10007 reply_len = -1;
10008#endif /* CONFIG_MESH */
3459381d 10009#endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
6fc6879b
JM
10010 } else if (os_strncmp(buf, "SET ", 4) == 0) {
10011 if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
10012 reply_len = -1;
10263dc2
OO
10013 } else if (os_strncmp(buf, "DUMP", 4) == 0) {
10014 reply_len = wpa_config_dump_values(wpa_s->conf,
10015 reply, reply_size);
acec8d32
JM
10016 } else if (os_strncmp(buf, "GET ", 4) == 0) {
10017 reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
10018 reply, reply_size);
6fc6879b
JM
10019 } else if (os_strcmp(buf, "LOGON") == 0) {
10020 eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
10021 } else if (os_strcmp(buf, "LOGOFF") == 0) {
10022 eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
10023 } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
8401a6b0
JM
10024 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
10025 reply_len = -1;
9796a86c
JM
10026 else
10027 wpas_request_connection(wpa_s);
0f44ec8e
PQ
10028 } else if (os_strcmp(buf, "REATTACH") == 0) {
10029 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED ||
10030 !wpa_s->current_ssid)
10031 reply_len = -1;
10032 else {
10033 wpa_s->reattach = 1;
10034 wpas_request_connection(wpa_s);
10035 }
6fc6879b 10036 } else if (os_strcmp(buf, "RECONNECT") == 0) {
8401a6b0
JM
10037 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
10038 reply_len = -1;
9796a86c
JM
10039 else if (wpa_s->disconnected)
10040 wpas_request_connection(wpa_s);
ec717917 10041#ifdef IEEE8021X_EAPOL
6fc6879b
JM
10042 } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
10043 if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
10044 reply_len = -1;
ec717917 10045#endif /* IEEE8021X_EAPOL */
6fc6879b
JM
10046#ifdef CONFIG_IEEE80211R
10047 } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
10048 if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
10049 reply_len = -1;
10050#endif /* CONFIG_IEEE80211R */
fcc60db4
JM
10051#ifdef CONFIG_WPS
10052 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
3152ff42
CWY
10053 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
10054 if (res == -2) {
10055 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
10056 reply_len = 17;
10057 } else if (res)
fcc60db4
JM
10058 reply_len = -1;
10059 } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
3152ff42
CWY
10060 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
10061 if (res == -2) {
10062 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
10063 reply_len = 17;
10064 } else if (res)
fcc60db4
JM
10065 reply_len = -1;
10066 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
10067 reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
10068 reply,
10069 reply_size);
3981cb3c
JM
10070 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
10071 reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
10072 wpa_s, buf + 14, reply, reply_size);
2f9929ff
AC
10073 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
10074 if (wpas_wps_cancel(wpa_s))
10075 reply_len = -1;
71892384 10076#ifdef CONFIG_WPS_NFC
3f2c8ba6
JM
10077 } else if (os_strcmp(buf, "WPS_NFC") == 0) {
10078 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
10079 reply_len = -1;
10080 } else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
10081 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
10082 reply_len = -1;
bbf41865
JM
10083 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
10084 reply_len = wpa_supplicant_ctrl_iface_wps_nfc_config_token(
10085 wpa_s, buf + 21, reply, reply_size);
3f2c8ba6
JM
10086 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
10087 reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
10088 wpa_s, buf + 14, reply, reply_size);
d7645d23
JM
10089 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
10090 if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
10091 buf + 17))
10092 reply_len = -1;
e65552dd
JM
10093 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_REQ ", 21) == 0) {
10094 reply_len = wpas_ctrl_nfc_get_handover_req(
10095 wpa_s, buf + 21, reply, reply_size);
10096 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
10097 reply_len = wpas_ctrl_nfc_get_handover_sel(
10098 wpa_s, buf + 21, reply, reply_size);
e4758827
JM
10099 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
10100 if (wpas_ctrl_nfc_report_handover(wpa_s, buf + 20))
10101 reply_len = -1;
71892384 10102#endif /* CONFIG_WPS_NFC */
fcc60db4
JM
10103 } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
10104 if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
10105 reply_len = -1;
70d84f11
JM
10106#ifdef CONFIG_AP
10107 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
10108 reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
10109 wpa_s, buf + 11, reply, reply_size);
10110#endif /* CONFIG_AP */
72df2f5f 10111#ifdef CONFIG_WPS_ER
e9bcfebf 10112 } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
08486685
JM
10113 if (wpas_wps_er_start(wpa_s, NULL))
10114 reply_len = -1;
10115 } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
10116 if (wpas_wps_er_start(wpa_s, buf + 13))
e9bcfebf
JM
10117 reply_len = -1;
10118 } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
f77cedc1 10119 wpas_wps_er_stop(wpa_s);
72df2f5f
JM
10120 } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
10121 if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
10122 reply_len = -1;
564cd7fa 10123 } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
ed159ad4
JM
10124 int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
10125 if (ret == -2) {
10126 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
10127 reply_len = 17;
10128 } else if (ret == -3) {
10129 os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
10130 reply_len = 18;
10131 } else if (ret == -4) {
10132 os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
10133 reply_len = 20;
10134 } else if (ret)
564cd7fa 10135 reply_len = -1;
e64dcfd5
JM
10136 } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
10137 if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
10138 reply_len = -1;
ef10f473
JM
10139 } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
10140 if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
10141 buf + 18))
10142 reply_len = -1;
7d6640a6
JM
10143 } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
10144 if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
10145 reply_len = -1;
1cea09a9
JM
10146#ifdef CONFIG_WPS_NFC
10147 } else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
10148 reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
10149 wpa_s, buf + 24, reply, reply_size);
10150#endif /* CONFIG_WPS_NFC */
72df2f5f 10151#endif /* CONFIG_WPS_ER */
fcc60db4 10152#endif /* CONFIG_WPS */
11ef8d35
JM
10153#ifdef CONFIG_IBSS_RSN
10154 } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
10155 if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
10156 reply_len = -1;
10157#endif /* CONFIG_IBSS_RSN */
603a3f34 10158#ifdef CONFIG_MESH
5b78493f
MH
10159 } else if (os_strncmp(buf, "MESH_INTERFACE_ADD ", 19) == 0) {
10160 reply_len = wpa_supplicant_ctrl_iface_mesh_interface_add(
10161 wpa_s, buf + 19, reply, reply_size);
10162 } else if (os_strcmp(buf, "MESH_INTERFACE_ADD") == 0) {
10163 reply_len = wpa_supplicant_ctrl_iface_mesh_interface_add(
10164 wpa_s, "", reply, reply_size);
603a3f34
JL
10165 } else if (os_strncmp(buf, "MESH_GROUP_ADD ", 15) == 0) {
10166 if (wpa_supplicant_ctrl_iface_mesh_group_add(wpa_s, buf + 15))
10167 reply_len = -1;
10168 } else if (os_strncmp(buf, "MESH_GROUP_REMOVE ", 18) == 0) {
10169 if (wpa_supplicant_ctrl_iface_mesh_group_remove(wpa_s,
10170 buf + 18))
10171 reply_len = -1;
e174ef34
MH
10172 } else if (os_strncmp(buf, "MESH_PEER_REMOVE ", 17) == 0) {
10173 if (wpa_supplicant_ctrl_iface_mesh_peer_remove(wpa_s, buf + 17))
10174 reply_len = -1;
2604edbf
MH
10175 } else if (os_strncmp(buf, "MESH_PEER_ADD ", 14) == 0) {
10176 if (wpa_supplicant_ctrl_iface_mesh_peer_add(wpa_s, buf + 14))
10177 reply_len = -1;
7ace4328
PKC
10178 } else if (os_strncmp(buf, "MESH_LINK_PROBE ", 16) == 0) {
10179 if (wpa_supplicant_ctrl_iface_mesh_link_probe(wpa_s, buf + 16))
10180 reply_len = -1;
603a3f34 10181#endif /* CONFIG_MESH */
b563b388
JM
10182#ifdef CONFIG_P2P
10183 } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
51775096 10184 if (p2p_ctrl_find(wpa_s, buf + 8))
b563b388
JM
10185 reply_len = -1;
10186 } else if (os_strcmp(buf, "P2P_FIND") == 0) {
10187 if (p2p_ctrl_find(wpa_s, ""))
10188 reply_len = -1;
10189 } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
10190 wpas_p2p_stop_find(wpa_s);
f309c18e
KV
10191 } else if (os_strncmp(buf, "P2P_ASP_PROVISION ", 18) == 0) {
10192 if (p2p_ctrl_asp_provision(wpa_s, buf + 18))
10193 reply_len = -1;
10194 } else if (os_strncmp(buf, "P2P_ASP_PROVISION_RESP ", 23) == 0) {
10195 if (p2p_ctrl_asp_provision_resp(wpa_s, buf + 23))
10196 reply_len = -1;
b563b388
JM
10197 } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
10198 reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
10199 reply_size);
10200 } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
10201 if (p2p_ctrl_listen(wpa_s, buf + 11))
10202 reply_len = -1;
10203 } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
10204 if (p2p_ctrl_listen(wpa_s, ""))
10205 reply_len = -1;
10206 } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
10207 if (wpas_p2p_group_remove(wpa_s, buf + 17))
10208 reply_len = -1;
10209 } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
38dcc86c 10210 if (p2p_ctrl_group_add(wpa_s, ""))
b563b388
JM
10211 reply_len = -1;
10212 } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
10213 if (p2p_ctrl_group_add(wpa_s, buf + 14))
10214 reply_len = -1;
57b38882
PK
10215 } else if (os_strncmp(buf, "P2P_GROUP_MEMBER ", 17) == 0) {
10216 reply_len = p2p_ctrl_group_member(wpa_s, buf + 17, reply,
10217 reply_size);
b563b388
JM
10218 } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
10219 if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
10220 reply_len = -1;
10221 } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
10222 reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
10223 } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
10224 reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
10225 reply_size);
10226 } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
10227 if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
10228 reply_len = -1;
10229 } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
10230 if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
10231 reply_len = -1;
10232 } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
10233 wpas_p2p_sd_service_update(wpa_s);
10234 } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
10235 if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
10236 reply_len = -1;
10237 } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
10238 wpas_p2p_service_flush(wpa_s);
10239 } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
10240 if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
10241 reply_len = -1;
10242 } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
10243 if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
10244 reply_len = -1;
ae9d45f3
KV
10245 } else if (os_strncmp(buf, "P2P_SERVICE_REP ", 16) == 0) {
10246 if (p2p_ctrl_service_replace(wpa_s, buf + 16) < 0)
10247 reply_len = -1;
b563b388
JM
10248 } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
10249 if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
10250 reply_len = -1;
10251 } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
10252 if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
10253 reply_len = -1;
10254 } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
10255 reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
10256 reply_size);
10257 } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
10258 if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
10259 reply_len = -1;
10260 } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
acb54643 10261 p2p_ctrl_flush(wpa_s);
9d562b79
SS
10262 } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
10263 if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
10264 reply_len = -1;
59eba7a2
JM
10265 } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
10266 if (wpas_p2p_cancel(wpa_s))
10267 reply_len = -1;
b563b388
JM
10268 } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
10269 if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
10270 reply_len = -1;
10271 } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
10272 if (p2p_ctrl_presence_req(wpa_s, "") < 0)
10273 reply_len = -1;
10274 } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
10275 if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
10276 reply_len = -1;
10277 } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
10278 if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
10279 reply_len = -1;
f2c56602
JM
10280 } else if (os_strncmp(buf, "P2P_REMOVE_CLIENT ", 18) == 0) {
10281 if (p2p_ctrl_remove_client(wpa_s, buf + 18) < 0)
10282 reply_len = -1;
a6f5b193
PX
10283 } else if (os_strncmp(buf, "P2P_LO_START ", 13) == 0) {
10284 if (p2p_ctrl_iface_p2p_lo_start(wpa_s, buf + 13))
10285 reply_len = -1;
10286 } else if (os_strcmp(buf, "P2P_LO_STOP") == 0) {
10287 if (wpas_p2p_lo_stop(wpa_s))
10288 reply_len = -1;
b563b388 10289#endif /* CONFIG_P2P */
9675ce35
JM
10290#ifdef CONFIG_WIFI_DISPLAY
10291 } else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
10292 if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
10293 reply_len = -1;
10294 } else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
10295 reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
10296 reply, reply_size);
10297#endif /* CONFIG_WIFI_DISPLAY */
afc064fe
JM
10298#ifdef CONFIG_INTERWORKING
10299 } else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
10300 if (interworking_fetch_anqp(wpa_s) < 0)
10301 reply_len = -1;
10302 } else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
10303 interworking_stop_fetch_anqp(wpa_s);
356d1488
JM
10304 } else if (os_strcmp(buf, "INTERWORKING_SELECT") == 0) {
10305 if (ctrl_interworking_select(wpa_s, NULL) < 0)
10306 reply_len = -1;
10307 } else if (os_strncmp(buf, "INTERWORKING_SELECT ", 20) == 0) {
10308 if (ctrl_interworking_select(wpa_s, buf + 20) < 0)
b02fe7ff
JM
10309 reply_len = -1;
10310 } else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
f91a512f 10311 if (ctrl_interworking_connect(wpa_s, buf + 21, 0) < 0)
b02fe7ff 10312 reply_len = -1;
f91a512f
JM
10313 } else if (os_strncmp(buf, "INTERWORKING_ADD_NETWORK ", 25) == 0) {
10314 int id;
10315
10316 id = ctrl_interworking_connect(wpa_s, buf + 25, 1);
10317 if (id < 0)
10318 reply_len = -1;
10319 else {
10320 reply_len = os_snprintf(reply, reply_size, "%d\n", id);
10321 if (os_snprintf_error(reply_size, reply_len))
10322 reply_len = -1;
10323 }
afc064fe
JM
10324 } else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
10325 if (get_anqp(wpa_s, buf + 9) < 0)
10326 reply_len = -1;
b1f12296
JM
10327 } else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
10328 if (gas_request(wpa_s, buf + 12) < 0)
10329 reply_len = -1;
10330 } else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
10331 reply_len = gas_response_get(wpa_s, buf + 17, reply,
10332 reply_size);
afc064fe 10333#endif /* CONFIG_INTERWORKING */
a8918e86
JK
10334#ifdef CONFIG_HS20
10335 } else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
10336 if (get_hs20_anqp(wpa_s, buf + 14) < 0)
10337 reply_len = -1;
10338 } else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
10339 if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
10340 reply_len = -1;
184e110c 10341 } else if (os_strncmp(buf, "HS20_ICON_REQUEST ", 18) == 0) {
8dd5c1b4
JN
10342 if (hs20_icon_request(wpa_s, buf + 18, 0) < 0)
10343 reply_len = -1;
10344 } else if (os_strncmp(buf, "REQ_HS20_ICON ", 14) == 0) {
10345 if (hs20_icon_request(wpa_s, buf + 14, 1) < 0)
10346 reply_len = -1;
10347 } else if (os_strncmp(buf, "GET_HS20_ICON ", 14) == 0) {
10348 reply_len = get_hs20_icon(wpa_s, buf + 14, reply, reply_size);
10349 } else if (os_strncmp(buf, "DEL_HS20_ICON ", 14) == 0) {
10350 if (del_hs20_icon(wpa_s, buf + 14) < 0)
184e110c 10351 reply_len = -1;
b572df86 10352 } else if (os_strcmp(buf, "FETCH_OSU") == 0) {
dd20eabd
JM
10353 if (hs20_fetch_osu(wpa_s, 0) < 0)
10354 reply_len = -1;
10355 } else if (os_strcmp(buf, "FETCH_OSU no-scan") == 0) {
10356 if (hs20_fetch_osu(wpa_s, 1) < 0)
b572df86
JM
10357 reply_len = -1;
10358 } else if (os_strcmp(buf, "CANCEL_FETCH_OSU") == 0) {
10359 hs20_cancel_fetch_osu(wpa_s);
a8918e86 10360#endif /* CONFIG_HS20 */
6fc6879b
JM
10361 } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
10362 {
10363 if (wpa_supplicant_ctrl_iface_ctrl_rsp(
10364 wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
10365 reply_len = -1;
bceb8431
JM
10366 else {
10367 /*
10368 * Notify response from timeout to allow the control
10369 * interface response to be sent first.
10370 */
10371 eloop_register_timeout(0, 0, wpas_ctrl_eapol_response,
10372 wpa_s, NULL);
10373 }
6fc6879b
JM
10374 } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
10375 if (wpa_supplicant_reload_configuration(wpa_s))
10376 reply_len = -1;
10377 } else if (os_strcmp(buf, "TERMINATE") == 0) {
1a1bf008 10378 wpa_supplicant_terminate_proc(wpa_s->global);
6fc6879b
JM
10379 } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
10380 if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
10381 reply_len = -1;
9aa10e2b
DS
10382 } else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
10383 reply_len = wpa_supplicant_ctrl_iface_blacklist(
10384 wpa_s, buf + 9, reply, reply_size);
0597a5b5
DS
10385 } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
10386 reply_len = wpa_supplicant_ctrl_iface_log_level(
10387 wpa_s, buf + 9, reply, reply_size);
90903a77
VD
10388 } else if (os_strncmp(buf, "LIST_NETWORKS ", 14) == 0) {
10389 reply_len = wpa_supplicant_ctrl_iface_list_networks(
10390 wpa_s, buf + 14, reply, reply_size);
6fc6879b
JM
10391 } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
10392 reply_len = wpa_supplicant_ctrl_iface_list_networks(
90903a77 10393 wpa_s, NULL, reply, reply_size);
6fc6879b 10394 } else if (os_strcmp(buf, "DISCONNECT") == 0) {
5f040be4 10395 wpas_request_disconnection(wpa_s);
fee52342
JM
10396 } else if (os_strcmp(buf, "SCAN") == 0) {
10397 wpas_ctrl_scan(wpa_s, NULL, reply, reply_size, &reply_len);
10398 } else if (os_strncmp(buf, "SCAN ", 5) == 0) {
10399 wpas_ctrl_scan(wpa_s, buf + 5, reply, reply_size, &reply_len);
6fc6879b
JM
10400 } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
10401 reply_len = wpa_supplicant_ctrl_iface_scan_results(
10402 wpa_s, reply, reply_size);
2ea2166d
JM
10403 } else if (os_strcmp(buf, "ABORT_SCAN") == 0) {
10404 if (wpas_abort_ongoing_scan(wpa_s) < 0)
10405 reply_len = -1;
6fc6879b
JM
10406 } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
10407 if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
10408 reply_len = -1;
10409 } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
10410 if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
10411 reply_len = -1;
10412 } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
10413 if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
10414 reply_len = -1;
10415 } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
10416 reply_len = wpa_supplicant_ctrl_iface_add_network(
10417 wpa_s, reply, reply_size);
10418 } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
10419 if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
10420 reply_len = -1;
10421 } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
10422 if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
10423 reply_len = -1;
10424 } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
10425 reply_len = wpa_supplicant_ctrl_iface_get_network(
10426 wpa_s, buf + 12, reply, reply_size);
1c330a2f 10427 } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
daae4995
AN
10428 if (wpa_supplicant_ctrl_iface_dup_network(wpa_s, buf + 12,
10429 wpa_s))
1c330a2f 10430 reply_len = -1;
d94c9ee6
JM
10431 } else if (os_strcmp(buf, "LIST_CREDS") == 0) {
10432 reply_len = wpa_supplicant_ctrl_iface_list_creds(
10433 wpa_s, reply, reply_size);
10434 } else if (os_strcmp(buf, "ADD_CRED") == 0) {
10435 reply_len = wpa_supplicant_ctrl_iface_add_cred(
10436 wpa_s, reply, reply_size);
10437 } else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
10438 if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
10439 reply_len = -1;
10440 } else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
10441 if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
10442 reply_len = -1;
c880ab87
JM
10443 } else if (os_strncmp(buf, "GET_CRED ", 9) == 0) {
10444 reply_len = wpa_supplicant_ctrl_iface_get_cred(wpa_s, buf + 9,
10445 reply,
10446 reply_size);
6fc6879b
JM
10447#ifndef CONFIG_NO_CONFIG_WRITE
10448 } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
10449 if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
10450 reply_len = -1;
10451#endif /* CONFIG_NO_CONFIG_WRITE */
10452 } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
10453 reply_len = wpa_supplicant_ctrl_iface_get_capability(
10454 wpa_s, buf + 15, reply, reply_size);
10455 } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
10456 if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
10457 reply_len = -1;
67b9bd08
DS
10458 } else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
10459 if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
10460 reply_len = -1;
4b4a8ae5
JM
10461 } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
10462 reply_len = wpa_supplicant_global_iface_list(
10463 wpa_s->global, reply, reply_size);
56e2fc2c 10464 } else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
6fc6879b 10465 reply_len = wpa_supplicant_global_iface_interfaces(
56e2fc2c 10466 wpa_s->global, buf + 10, reply, reply_size);
6fc6879b
JM
10467 } else if (os_strncmp(buf, "BSS ", 4) == 0) {
10468 reply_len = wpa_supplicant_ctrl_iface_bss(
10469 wpa_s, buf + 4, reply, reply_size);
e653b622
JM
10470#ifdef CONFIG_AP
10471 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
10472 reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
10473 } else if (os_strncmp(buf, "STA ", 4) == 0) {
10474 reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
10475 reply_size);
10476 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
10477 reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
10478 reply_size);
e60b2951
JJ
10479 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
10480 if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
10481 reply_len = -1;
10482 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
10483 if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
10484 reply_len = -1;
334bf36a
AO
10485 } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
10486 if (ap_ctrl_iface_chanswitch(wpa_s, buf + 12))
10487 reply_len = -1;
99650cad
JM
10488 } else if (os_strcmp(buf, "STOP_AP") == 0) {
10489 if (wpas_ap_stop_ap(wpa_s))
10490 reply_len = -1;
e653b622 10491#endif /* CONFIG_AP */
207ef3fb
JM
10492 } else if (os_strcmp(buf, "SUSPEND") == 0) {
10493 wpas_notify_suspend(wpa_s->global);
10494 } else if (os_strcmp(buf, "RESUME") == 0) {
10495 wpas_notify_resume(wpa_s->global);
9ff4de6d 10496#ifdef CONFIG_TESTING_OPTIONS
32d5295f
JM
10497 } else if (os_strcmp(buf, "DROP_SA") == 0) {
10498 wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
9ff4de6d 10499#endif /* CONFIG_TESTING_OPTIONS */
86d4f806
JM
10500 } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
10501 if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
10502 reply_len = -1;
0d0a8ca1 10503 } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
5407c69d 10504 wpa_s->auto_reconnect_disabled = atoi(buf + 16) == 0;
78633c37
SL
10505 } else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
10506 if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
10507 reply_len = -1;
10508 } else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
10509 if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
10510 buf + 17))
10511 reply_len = -1;
39ee845f 10512 } else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
a1144000 10513 wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10);
281ff0aa
GP
10514#ifdef CONFIG_TDLS
10515 } else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
10516 if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
10517 reply_len = -1;
10518 } else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
10519 if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
10520 reply_len = -1;
10521 } else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
10522 if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
10523 reply_len = -1;
6b90deae
AN
10524 } else if (os_strncmp(buf, "TDLS_CHAN_SWITCH ", 17) == 0) {
10525 if (wpa_supplicant_ctrl_iface_tdls_chan_switch(wpa_s,
10526 buf + 17))
10527 reply_len = -1;
10528 } else if (os_strncmp(buf, "TDLS_CANCEL_CHAN_SWITCH ", 24) == 0) {
10529 if (wpa_supplicant_ctrl_iface_tdls_cancel_chan_switch(wpa_s,
10530 buf + 24))
10531 reply_len = -1;
4504621f
OG
10532 } else if (os_strncmp(buf, "TDLS_LINK_STATUS ", 17) == 0) {
10533 reply_len = wpa_supplicant_ctrl_iface_tdls_link_status(
10534 wpa_s, buf + 17, reply, reply_size);
281ff0aa 10535#endif /* CONFIG_TDLS */
8506ea6f
MB
10536 } else if (os_strcmp(buf, "WMM_AC_STATUS") == 0) {
10537 reply_len = wpas_wmm_ac_status(wpa_s, reply, reply_size);
eb2f2088
MB
10538 } else if (os_strncmp(buf, "WMM_AC_ADDTS ", 13) == 0) {
10539 if (wmm_ac_ctrl_addts(wpa_s, buf + 13))
10540 reply_len = -1;
10541 } else if (os_strncmp(buf, "WMM_AC_DELTS ", 13) == 0) {
10542 if (wmm_ac_ctrl_delts(wpa_s, buf + 13))
10543 reply_len = -1;
60b24b0d
DS
10544 } else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
10545 reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
10546 reply_size);
96e8d831
DS
10547 } else if (os_strncmp(buf, "SIGNAL_MONITOR", 14) == 0) {
10548 if (wpas_ctrl_iface_signal_monitor(wpa_s, buf + 14))
10549 reply_len = -1;
dc7785f8
YZ
10550 } else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
10551 reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
10552 reply_size);
bc5d330a
TB
10553#ifdef CONFIG_AUTOSCAN
10554 } else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
10555 if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
10556 reply_len = -1;
10557#endif /* CONFIG_AUTOSCAN */
4d7aab78
EL
10558 } else if (os_strcmp(buf, "DRIVER_FLAGS") == 0) {
10559 reply_len = wpas_ctrl_iface_driver_flags(wpa_s, reply,
10560 reply_size);
5e2c3490
JM
10561#ifdef ANDROID
10562 } else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
10563 reply_len = wpa_supplicant_driver_cmd(wpa_s, buf + 7, reply,
10564 reply_size);
10565#endif /* ANDROID */
adef8948
BL
10566 } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
10567 reply_len = wpa_supplicant_vendor_cmd(wpa_s, buf + 7, reply,
10568 reply_size);
9482426e 10569 } else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
f5f37d3a 10570 pmksa_cache_clear_current(wpa_s->wpa);
9482426e 10571 eapol_sm_request_reauth(wpa_s->eapol);
e9199e31
JM
10572#ifdef CONFIG_WNM
10573 } else if (os_strncmp(buf, "WNM_SLEEP ", 10) == 0) {
10574 if (wpas_ctrl_iface_wnm_sleep(wpa_s, buf + 10))
10575 reply_len = -1;
07565ab0
MG
10576 } else if (os_strncmp(buf, "WNM_BSS_QUERY ", 14) == 0) {
10577 if (wpas_ctrl_iface_wnm_bss_query(wpa_s, buf + 14))
65bcd0a9 10578 reply_len = -1;
d514b502
JM
10579 } else if (os_strncmp(buf, "COLOC_INTF_REPORT ", 18) == 0) {
10580 if (wpas_ctrl_iface_coloc_intf_report(wpa_s, buf + 18))
10581 reply_len = -1;
e9199e31 10582#endif /* CONFIG_WNM */
acb54643
JM
10583 } else if (os_strcmp(buf, "FLUSH") == 0) {
10584 wpa_supplicant_ctrl_iface_flush(wpa_s);
1f965e62
JM
10585 } else if (os_strncmp(buf, "RADIO_WORK ", 11) == 0) {
10586 reply_len = wpas_ctrl_radio_work(wpa_s, buf + 11, reply,
10587 reply_size);
60b893df
JM
10588#ifdef CONFIG_TESTING_OPTIONS
10589 } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
10590 if (wpas_ctrl_iface_mgmt_tx(wpa_s, buf + 8) < 0)
10591 reply_len = -1;
10592 } else if (os_strcmp(buf, "MGMT_TX_DONE") == 0) {
10593 wpas_ctrl_iface_mgmt_tx_done(wpa_s);
4de70e23
JM
10594 } else if (os_strncmp(buf, "MGMT_RX_PROCESS ", 16) == 0) {
10595 if (wpas_ctrl_iface_mgmt_rx_process(wpa_s, buf + 16) < 0)
10596 reply_len = -1;
ad12f2f4
JM
10597 } else if (os_strncmp(buf, "DRIVER_EVENT ", 13) == 0) {
10598 if (wpas_ctrl_iface_driver_event(wpa_s, buf + 13) < 0)
10599 reply_len = -1;
9d4ff04a
JM
10600 } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
10601 if (wpas_ctrl_iface_eapol_rx(wpa_s, buf + 9) < 0)
10602 reply_len = -1;
4a6cc862
JM
10603 } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
10604 if (wpas_ctrl_iface_data_test_config(wpa_s, buf + 17) < 0)
10605 reply_len = -1;
10606 } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
10607 if (wpas_ctrl_iface_data_test_tx(wpa_s, buf + 13) < 0)
10608 reply_len = -1;
fc0ef7c0
JM
10609 } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
10610 if (wpas_ctrl_iface_data_test_frame(wpa_s, buf + 16) < 0)
10611 reply_len = -1;
a156ffda
JM
10612 } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
10613 if (wpas_ctrl_test_alloc_fail(wpa_s, buf + 16) < 0)
10614 reply_len = -1;
10615 } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
10616 reply_len = wpas_ctrl_get_alloc_fail(wpa_s, reply, reply_size);
2da52565
JM
10617 } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
10618 if (wpas_ctrl_test_fail(wpa_s, buf + 10) < 0)
10619 reply_len = -1;
10620 } else if (os_strcmp(buf, "GET_FAIL") == 0) {
10621 reply_len = wpas_ctrl_get_fail(wpa_s, reply, reply_size);
a530fe77
JM
10622 } else if (os_strncmp(buf, "EVENT_TEST ", 11) == 0) {
10623 if (wpas_ctrl_event_test(wpa_s, buf + 11) < 0)
10624 reply_len = -1;
651c6a84
JM
10625 } else if (os_strncmp(buf, "TEST_ASSOC_IE ", 14) == 0) {
10626 if (wpas_ctrl_test_assoc_ie(wpa_s, buf + 14) < 0)
10627 reply_len = -1;
16579769
JM
10628 } else if (os_strcmp(buf, "RESET_PN") == 0) {
10629 if (wpas_ctrl_reset_pn(wpa_s) < 0)
10630 reply_len = -1;
751f5b29
JM
10631 } else if (os_strncmp(buf, "KEY_REQUEST ", 12) == 0) {
10632 if (wpas_ctrl_key_request(wpa_s, buf + 12) < 0)
10633 reply_len = -1;
daa40960
JM
10634 } else if (os_strcmp(buf, "RESEND_ASSOC") == 0) {
10635 if (wpas_ctrl_resend_assoc(wpa_s) < 0)
10636 reply_len = -1;
5c7d35ba
MV
10637 } else if (os_strcmp(buf, "UNPROT_DEAUTH") == 0) {
10638 sme_event_unprot_disconnect(
10639 wpa_s, wpa_s->bssid, NULL,
10640 WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA);
60b893df 10641#endif /* CONFIG_TESTING_OPTIONS */
86bd36f0
JM
10642 } else if (os_strncmp(buf, "VENDOR_ELEM_ADD ", 16) == 0) {
10643 if (wpas_ctrl_vendor_elem_add(wpa_s, buf + 16) < 0)
10644 reply_len = -1;
10645 } else if (os_strncmp(buf, "VENDOR_ELEM_GET ", 16) == 0) {
10646 reply_len = wpas_ctrl_vendor_elem_get(wpa_s, buf + 16, reply,
10647 reply_size);
10648 } else if (os_strncmp(buf, "VENDOR_ELEM_REMOVE ", 19) == 0) {
10649 if (wpas_ctrl_vendor_elem_remove(wpa_s, buf + 19) < 0)
10650 reply_len = -1;
f4b8bfae 10651 } else if (os_strncmp(buf, "NEIGHBOR_REP_REQUEST", 20) == 0) {
6a4f0ed7 10652 if (wpas_ctrl_iface_send_neighbor_rep(wpa_s, buf + 20))
f4b8bfae 10653 reply_len = -1;
65d9a5e2
JM
10654 } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
10655 wpas_ctrl_iface_erp_flush(wpa_s);
fb375883
IP
10656 } else if (os_strncmp(buf, "MAC_RAND_SCAN ", 14) == 0) {
10657 if (wpas_ctrl_iface_mac_rand_scan(wpa_s, buf + 14))
10658 reply_len = -1;
98342208
AK
10659 } else if (os_strncmp(buf, "GET_PREF_FREQ_LIST ", 19) == 0) {
10660 reply_len = wpas_ctrl_iface_get_pref_freq_list(
10661 wpa_s, buf + 19, reply, reply_size);
5732b770
JM
10662#ifdef CONFIG_FILS
10663 } else if (os_strncmp(buf, "FILS_HLP_REQ_ADD ", 17) == 0) {
10664 if (wpas_ctrl_iface_fils_hlp_req_add(wpa_s, buf + 17))
10665 reply_len = -1;
10666 } else if (os_strcmp(buf, "FILS_HLP_REQ_FLUSH") == 0) {
10667 wpas_flush_fils_hlp_req(wpa_s);
10668#endif /* CONFIG_FILS */
be27e185
JM
10669#ifdef CONFIG_DPP
10670 } else if (os_strncmp(buf, "DPP_QR_CODE ", 12) == 0) {
10671 int res;
10672
10673 res = wpas_dpp_qr_code(wpa_s, buf + 12);
10674 if (res < 0) {
10675 reply_len = -1;
10676 } else {
10677 reply_len = os_snprintf(reply, reply_size, "%d", res);
10678 if (os_snprintf_error(reply_size, reply_len))
10679 reply_len = -1;
10680 }
10681 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GEN ", 18) == 0) {
10682 int res;
10683
87d8435c 10684 res = dpp_bootstrap_gen(wpa_s->dpp, buf + 18);
be27e185
JM
10685 if (res < 0) {
10686 reply_len = -1;
10687 } else {
10688 reply_len = os_snprintf(reply, reply_size, "%d", res);
10689 if (os_snprintf_error(reply_size, reply_len))
10690 reply_len = -1;
10691 }
10692 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_REMOVE ", 21) == 0) {
87d8435c 10693 if (dpp_bootstrap_remove(wpa_s->dpp, buf + 21) < 0)
be27e185
JM
10694 reply_len = -1;
10695 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GET_URI ", 22) == 0) {
10696 const char *uri;
10697
87d8435c 10698 uri = dpp_bootstrap_get_uri(wpa_s->dpp, atoi(buf + 22));
be27e185
JM
10699 if (!uri) {
10700 reply_len = -1;
10701 } else {
10702 reply_len = os_snprintf(reply, reply_size, "%s", uri);
10703 if (os_snprintf_error(reply_size, reply_len))
10704 reply_len = -1;
10705 }
6a7182a9 10706 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_INFO ", 19) == 0) {
87d8435c
JM
10707 reply_len = dpp_bootstrap_info(wpa_s->dpp, atoi(buf + 19),
10708 reply, reply_size);
30d27b04
JM
10709 } else if (os_strncmp(buf, "DPP_AUTH_INIT ", 14) == 0) {
10710 if (wpas_dpp_auth_init(wpa_s, buf + 13) < 0)
10711 reply_len = -1;
10712 } else if (os_strncmp(buf, "DPP_LISTEN ", 11) == 0) {
10713 if (wpas_dpp_listen(wpa_s, buf + 11) < 0)
10714 reply_len = -1;
10715 } else if (os_strcmp(buf, "DPP_STOP_LISTEN") == 0) {
c1d37739 10716 wpas_dpp_stop(wpa_s);
30d27b04 10717 wpas_dpp_listen_stop(wpa_s);
461d39af
JM
10718 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_ADD", 20) == 0) {
10719 int res;
10720
87d8435c 10721 res = dpp_configurator_add(wpa_s->dpp, buf + 20);
461d39af
JM
10722 if (res < 0) {
10723 reply_len = -1;
10724 } else {
10725 reply_len = os_snprintf(reply, reply_size, "%d", res);
10726 if (os_snprintf_error(reply_size, reply_len))
10727 reply_len = -1;
10728 }
10729 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_REMOVE ", 24) == 0) {
87d8435c 10730 if (dpp_configurator_remove(wpa_s->dpp, buf + 24) < 0)
461d39af 10731 reply_len = -1;
f522bb23 10732 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_SIGN ", 22) == 0) {
53d5de6f 10733 if (wpas_dpp_configurator_sign(wpa_s, buf + 21) < 0)
f522bb23 10734 reply_len = -1;
8179ae3a 10735 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_GET_KEY ", 25) == 0) {
87d8435c
JM
10736 reply_len = dpp_configurator_get_key_id(wpa_s->dpp,
10737 atoi(buf + 25),
10738 reply, reply_size);
500ed7f0
JM
10739 } else if (os_strncmp(buf, "DPP_PKEX_ADD ", 13) == 0) {
10740 int res;
10741
10742 res = wpas_dpp_pkex_add(wpa_s, buf + 12);
10743 if (res < 0) {
10744 reply_len = -1;
10745 } else {
10746 reply_len = os_snprintf(reply, reply_size, "%d", res);
10747 if (os_snprintf_error(reply_size, reply_len))
10748 reply_len = -1;
10749 }
10750 } else if (os_strncmp(buf, "DPP_PKEX_REMOVE ", 16) == 0) {
10751 if (wpas_dpp_pkex_remove(wpa_s, buf + 16) < 0)
10752 reply_len = -1;
c02dd10d
JM
10753#ifdef CONFIG_DPP2
10754 } else if (os_strncmp(buf, "DPP_CONTROLLER_START ", 21) == 0) {
10755 if (wpas_dpp_controller_start(wpa_s, buf + 20) < 0)
10756 reply_len = -1;
10757 } else if (os_strcmp(buf, "DPP_CONTROLLER_START") == 0) {
10758 if (wpas_dpp_controller_start(wpa_s, NULL) < 0)
10759 reply_len = -1;
10760 } else if (os_strcmp(buf, "DPP_CONTROLLER_STOP") == 0) {
10761 dpp_controller_stop(wpa_s->dpp);
10762#endif /* CONFIG_DPP2 */
be27e185 10763#endif /* CONFIG_DPP */
6fc6879b
JM
10764 } else {
10765 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
10766 reply_len = 16;
10767 }
10768
10769 if (reply_len < 0) {
10770 os_memcpy(reply, "FAIL\n", 5);
10771 reply_len = 5;
10772 }
10773
6fc6879b
JM
10774 *resp_len = reply_len;
10775 return reply;
10776}
10777
10778
10779static int wpa_supplicant_global_iface_add(struct wpa_global *global,
10780 char *cmd)
10781{
10782 struct wpa_interface iface;
efa232f9
JJ
10783 char *pos, *extra;
10784 struct wpa_supplicant *wpa_s;
10785 unsigned int create_iface = 0;
10786 u8 mac_addr[ETH_ALEN];
0f039e34 10787 enum wpa_driver_if_type type = WPA_IF_STATION;
6fc6879b
JM
10788
10789 /*
10790 * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
0f039e34 10791 * TAB<bridge_ifname>[TAB<create>[TAB<interface_type>]]
6fc6879b
JM
10792 */
10793 wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
10794
10795 os_memset(&iface, 0, sizeof(iface));
10796
10797 do {
10798 iface.ifname = pos = cmd;
10799 pos = os_strchr(pos, '\t');
10800 if (pos)
10801 *pos++ = '\0';
10802 if (iface.ifname[0] == '\0')
10803 return -1;
10804 if (pos == NULL)
10805 break;
10806
10807 iface.confname = pos;
10808 pos = os_strchr(pos, '\t');
10809 if (pos)
10810 *pos++ = '\0';
10811 if (iface.confname[0] == '\0')
10812 iface.confname = NULL;
10813 if (pos == NULL)
10814 break;
10815
10816 iface.driver = pos;
10817 pos = os_strchr(pos, '\t');
10818 if (pos)
10819 *pos++ = '\0';
10820 if (iface.driver[0] == '\0')
10821 iface.driver = NULL;
10822 if (pos == NULL)
10823 break;
10824
10825 iface.ctrl_interface = pos;
10826 pos = os_strchr(pos, '\t');
10827 if (pos)
10828 *pos++ = '\0';
10829 if (iface.ctrl_interface[0] == '\0')
10830 iface.ctrl_interface = NULL;
10831 if (pos == NULL)
10832 break;
10833
10834 iface.driver_param = pos;
10835 pos = os_strchr(pos, '\t');
10836 if (pos)
10837 *pos++ = '\0';
10838 if (iface.driver_param[0] == '\0')
10839 iface.driver_param = NULL;
10840 if (pos == NULL)
10841 break;
10842
10843 iface.bridge_ifname = pos;
10844 pos = os_strchr(pos, '\t');
10845 if (pos)
10846 *pos++ = '\0';
10847 if (iface.bridge_ifname[0] == '\0')
10848 iface.bridge_ifname = NULL;
10849 if (pos == NULL)
10850 break;
efa232f9
JJ
10851
10852 extra = pos;
10853 pos = os_strchr(pos, '\t');
10854 if (pos)
10855 *pos++ = '\0';
da3db681
BG
10856 if (!extra[0])
10857 break;
10858
0f039e34 10859 if (os_strcmp(extra, "create") == 0) {
efa232f9 10860 create_iface = 1;
0f039e34
AS
10861 if (!pos)
10862 break;
10863
10864 if (os_strcmp(pos, "sta") == 0) {
10865 type = WPA_IF_STATION;
10866 } else if (os_strcmp(pos, "ap") == 0) {
10867 type = WPA_IF_AP_BSS;
10868 } else {
10869 wpa_printf(MSG_DEBUG,
10870 "INTERFACE_ADD unsupported interface type: '%s'",
10871 pos);
10872 return -1;
10873 }
10874 } else {
da3db681
BG
10875 wpa_printf(MSG_DEBUG,
10876 "INTERFACE_ADD unsupported extra parameter: '%s'",
10877 extra);
efa232f9 10878 return -1;
da3db681 10879 }
6fc6879b
JM
10880 } while (0);
10881
efa232f9
JJ
10882 if (create_iface) {
10883 wpa_printf(MSG_DEBUG, "CTRL_IFACE creating interface '%s'",
10884 iface.ifname);
10885 if (!global->ifaces)
10886 return -1;
0f039e34 10887 if (wpa_drv_if_add(global->ifaces, type, iface.ifname,
efa232f9
JJ
10888 NULL, NULL, NULL, mac_addr, NULL) < 0) {
10889 wpa_printf(MSG_ERROR,
10890 "CTRL_IFACE interface creation failed");
10891 return -1;
10892 }
10893
10894 wpa_printf(MSG_DEBUG,
10895 "CTRL_IFACE interface '%s' created with MAC addr: "
10896 MACSTR, iface.ifname, MAC2STR(mac_addr));
10897 }
10898
6fc6879b 10899 if (wpa_supplicant_get_iface(global, iface.ifname))
efa232f9
JJ
10900 goto fail;
10901
10902 wpa_s = wpa_supplicant_add_iface(global, &iface, NULL);
10903 if (!wpa_s)
10904 goto fail;
10905 wpa_s->added_vif = create_iface;
10906 return 0;
6fc6879b 10907
efa232f9
JJ
10908fail:
10909 if (create_iface)
10910 wpa_drv_if_remove(global->ifaces, WPA_IF_STATION, iface.ifname);
10911 return -1;
6fc6879b
JM
10912}
10913
10914
10915static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
10916 char *cmd)
10917{
10918 struct wpa_supplicant *wpa_s;
efa232f9
JJ
10919 int ret;
10920 unsigned int delete_iface;
6fc6879b
JM
10921
10922 wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
10923
10924 wpa_s = wpa_supplicant_get_iface(global, cmd);
10925 if (wpa_s == NULL)
10926 return -1;
efa232f9
JJ
10927 delete_iface = wpa_s->added_vif;
10928 ret = wpa_supplicant_remove_iface(global, wpa_s, 0);
10929 if (!ret && delete_iface) {
10930 wpa_printf(MSG_DEBUG, "CTRL_IFACE deleting the interface '%s'",
10931 cmd);
10932 ret = wpa_drv_if_remove(global->ifaces, WPA_IF_STATION, cmd);
10933 }
10934 return ret;
6fc6879b
JM
10935}
10936
10937
4b4a8ae5
JM
10938static void wpa_free_iface_info(struct wpa_interface_info *iface)
10939{
10940 struct wpa_interface_info *prev;
10941
10942 while (iface) {
10943 prev = iface;
10944 iface = iface->next;
10945
10946 os_free(prev->ifname);
10947 os_free(prev->desc);
10948 os_free(prev);
10949 }
10950}
10951
10952
10953static int wpa_supplicant_global_iface_list(struct wpa_global *global,
10954 char *buf, int len)
10955{
10956 int i, res;
10957 struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
10958 char *pos, *end;
10959
c5121837 10960 for (i = 0; wpa_drivers[i]; i++) {
8b423edb 10961 const struct wpa_driver_ops *drv = wpa_drivers[i];
4b4a8ae5
JM
10962 if (drv->get_interfaces == NULL)
10963 continue;
5fbc1f27 10964 tmp = drv->get_interfaces(global->drv_priv[i]);
4b4a8ae5
JM
10965 if (tmp == NULL)
10966 continue;
10967
10968 if (last == NULL)
10969 iface = last = tmp;
10970 else
10971 last->next = tmp;
10972 while (last->next)
10973 last = last->next;
10974 }
10975
10976 pos = buf;
10977 end = buf + len;
10978 for (tmp = iface; tmp; tmp = tmp->next) {
10979 res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
10980 tmp->drv_name, tmp->ifname,
10981 tmp->desc ? tmp->desc : "");
d85e1fc8 10982 if (os_snprintf_error(end - pos, res)) {
4b4a8ae5
JM
10983 *pos = '\0';
10984 break;
10985 }
10986 pos += res;
10987 }
10988
10989 wpa_free_iface_info(iface);
10990
10991 return pos - buf;
10992}
10993
10994
6fc6879b 10995static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
56e2fc2c 10996 const char *input,
6fc6879b
JM
10997 char *buf, int len)
10998{
10999 int res;
11000 char *pos, *end;
11001 struct wpa_supplicant *wpa_s;
56e2fc2c
JD
11002 int show_ctrl = 0;
11003
11004 if (input)
11005 show_ctrl = !!os_strstr(input, "ctrl");
6fc6879b
JM
11006
11007 wpa_s = global->ifaces;
11008 pos = buf;
11009 end = buf + len;
11010
11011 while (wpa_s) {
56e2fc2c
JD
11012 if (show_ctrl)
11013 res = os_snprintf(pos, end - pos, "%s ctrl_iface=%s\n",
11014 wpa_s->ifname,
11015 wpa_s->conf->ctrl_interface ?
11016 wpa_s->conf->ctrl_interface : "N/A");
11017 else
11018 res = os_snprintf(pos, end - pos, "%s\n",
11019 wpa_s->ifname);
11020
d85e1fc8 11021 if (os_snprintf_error(end - pos, res)) {
6fc6879b
JM
11022 *pos = '\0';
11023 break;
11024 }
11025 pos += res;
11026 wpa_s = wpa_s->next;
11027 }
11028 return pos - buf;
11029}
11030
11031
cf3bebf2
JM
11032static char * wpas_global_ctrl_iface_ifname(struct wpa_global *global,
11033 const char *ifname,
11034 char *cmd, size_t *resp_len)
11035{
11036 struct wpa_supplicant *wpa_s;
11037
11038 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
11039 if (os_strcmp(ifname, wpa_s->ifname) == 0)
11040 break;
11041 }
11042
11043 if (wpa_s == NULL) {
11044 char *resp = os_strdup("FAIL-NO-IFNAME-MATCH\n");
11045 if (resp)
11046 *resp_len = os_strlen(resp);
11047 else
11048 *resp_len = 1;
11049 return resp;
11050 }
11051
11052 return wpa_supplicant_ctrl_iface_process(wpa_s, cmd, resp_len);
11053}
11054
11055
576bce9c
JM
11056static char * wpas_global_ctrl_iface_redir_p2p(struct wpa_global *global,
11057 char *buf, size_t *resp_len)
11058{
11059#ifdef CONFIG_P2P
11060 static const char * cmd[] = {
443427e4 11061 "LIST_NETWORKS",
576bce9c
JM
11062 "P2P_FIND",
11063 "P2P_STOP_FIND",
11064 "P2P_LISTEN",
11065 "P2P_GROUP_ADD",
11066 "P2P_GET_PASSPHRASE",
11067 "P2P_SERVICE_UPDATE",
11068 "P2P_SERVICE_FLUSH",
11069 "P2P_FLUSH",
11070 "P2P_CANCEL",
11071 "P2P_PRESENCE_REQ",
11072 "P2P_EXT_LISTEN",
f2bc3448
DS
11073#ifdef CONFIG_AP
11074 "STA-FIRST",
11075#endif /* CONFIG_AP */
576bce9c
JM
11076 NULL
11077 };
11078 static const char * prefix[] = {
443427e4
DS
11079#ifdef ANDROID
11080 "DRIVER ",
11081#endif /* ANDROID */
be1ece46 11082 "GET_CAPABILITY ",
443427e4
DS
11083 "GET_NETWORK ",
11084 "REMOVE_NETWORK ",
576bce9c
JM
11085 "P2P_FIND ",
11086 "P2P_CONNECT ",
11087 "P2P_LISTEN ",
11088 "P2P_GROUP_REMOVE ",
11089 "P2P_GROUP_ADD ",
57b38882 11090 "P2P_GROUP_MEMBER ",
576bce9c
JM
11091 "P2P_PROV_DISC ",
11092 "P2P_SERV_DISC_REQ ",
11093 "P2P_SERV_DISC_CANCEL_REQ ",
11094 "P2P_SERV_DISC_RESP ",
11095 "P2P_SERV_DISC_EXTERNAL ",
11096 "P2P_SERVICE_ADD ",
11097 "P2P_SERVICE_DEL ",
87d5ef5a 11098 "P2P_SERVICE_REP ",
576bce9c
JM
11099 "P2P_REJECT ",
11100 "P2P_INVITE ",
11101 "P2P_PEER ",
11102 "P2P_SET ",
11103 "P2P_UNAUTHORIZE ",
11104 "P2P_PRESENCE_REQ ",
11105 "P2P_EXT_LISTEN ",
f2c56602 11106 "P2P_REMOVE_CLIENT ",
8ad8bc5c
DS
11107 "WPS_NFC_TOKEN ",
11108 "WPS_NFC_TAG_READ ",
f3ff9487
AM
11109 "NFC_GET_HANDOVER_SEL ",
11110 "NFC_GET_HANDOVER_REQ ",
11111 "NFC_REPORT_HANDOVER ",
87d5ef5a
KV
11112 "P2P_ASP_PROVISION ",
11113 "P2P_ASP_PROVISION_RESP ",
f2bc3448
DS
11114#ifdef CONFIG_AP
11115 "STA ",
11116 "STA-NEXT ",
11117#endif /* CONFIG_AP */
576bce9c
JM
11118 NULL
11119 };
11120 int found = 0;
11121 int i;
11122
11123 if (global->p2p_init_wpa_s == NULL)
11124 return NULL;
11125
11126 for (i = 0; !found && cmd[i]; i++) {
11127 if (os_strcmp(buf, cmd[i]) == 0)
11128 found = 1;
11129 }
11130
11131 for (i = 0; !found && prefix[i]; i++) {
11132 if (os_strncmp(buf, prefix[i], os_strlen(prefix[i])) == 0)
11133 found = 1;
11134 }
11135
11136 if (found)
11137 return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
11138 buf, resp_len);
11139#endif /* CONFIG_P2P */
11140 return NULL;
11141}
11142
11143
11144static char * wpas_global_ctrl_iface_redir_wfd(struct wpa_global *global,
11145 char *buf, size_t *resp_len)
11146{
11147#ifdef CONFIG_WIFI_DISPLAY
11148 if (global->p2p_init_wpa_s == NULL)
11149 return NULL;
11150 if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0 ||
11151 os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0)
11152 return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
11153 buf, resp_len);
11154#endif /* CONFIG_WIFI_DISPLAY */
11155 return NULL;
11156}
11157
11158
11159static char * wpas_global_ctrl_iface_redir(struct wpa_global *global,
11160 char *buf, size_t *resp_len)
11161{
11162 char *ret;
11163
11164 ret = wpas_global_ctrl_iface_redir_p2p(global, buf, resp_len);
11165 if (ret)
11166 return ret;
11167
11168 ret = wpas_global_ctrl_iface_redir_wfd(global, buf, resp_len);
11169 if (ret)
11170 return ret;
11171
11172 return NULL;
11173}
11174
11175
1b9b31c1
JM
11176static int wpas_global_ctrl_iface_set(struct wpa_global *global, char *cmd)
11177{
11178 char *value;
11179
11180 value = os_strchr(cmd, ' ');
11181 if (value == NULL)
11182 return -1;
11183 *value++ = '\0';
11184
11185 wpa_printf(MSG_DEBUG, "GLOBAL_CTRL_IFACE SET '%s'='%s'", cmd, value);
11186
11187#ifdef CONFIG_WIFI_DISPLAY
11188 if (os_strcasecmp(cmd, "wifi_display") == 0) {
11189 wifi_display_enable(global, !!atoi(value));
11190 return 0;
11191 }
11192#endif /* CONFIG_WIFI_DISPLAY */
11193
a7ca6dac
JM
11194 /* Restore cmd to its original value to allow redirection */
11195 value[-1] = ' ';
11196
1b9b31c1
JM
11197 return -1;
11198}
11199
11200
daae4995
AN
11201static int wpas_global_ctrl_iface_dup_network(struct wpa_global *global,
11202 char *cmd)
11203{
11204 struct wpa_supplicant *wpa_s[2]; /* src, dst */
11205 char *p;
11206 unsigned int i;
11207
11208 /* cmd: "<src ifname> <dst ifname> <src network id> <dst network id>
11209 * <variable name> */
11210
11211 for (i = 0; i < ARRAY_SIZE(wpa_s) ; i++) {
11212 p = os_strchr(cmd, ' ');
11213 if (p == NULL)
11214 return -1;
11215 *p = '\0';
11216
11217 wpa_s[i] = global->ifaces;
11218 for (; wpa_s[i]; wpa_s[i] = wpa_s[i]->next) {
11219 if (os_strcmp(cmd, wpa_s[i]->ifname) == 0)
11220 break;
11221 }
11222
11223 if (!wpa_s[i]) {
11224 wpa_printf(MSG_DEBUG,
11225 "CTRL_IFACE: Could not find iface=%s", cmd);
11226 return -1;
11227 }
11228
11229 cmd = p + 1;
11230 }
11231
11232 return wpa_supplicant_ctrl_iface_dup_network(wpa_s[0], cmd, wpa_s[1]);
11233}
11234
11235
42868f14
JM
11236#ifndef CONFIG_NO_CONFIG_WRITE
11237static int wpas_global_ctrl_iface_save_config(struct wpa_global *global)
11238{
d6b818ef 11239 int ret = 0, saved = 0;
42868f14
JM
11240 struct wpa_supplicant *wpa_s;
11241
11242 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
11243 if (!wpa_s->conf->update_config) {
11244 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed to update configuration (update_config=0)");
11245 continue;
11246 }
11247
11248 if (wpa_config_write(wpa_s->confname, wpa_s->conf)) {
11249 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to update configuration");
11250 ret = 1;
11251 } else {
11252 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration updated");
d6b818ef 11253 saved++;
42868f14
JM
11254 }
11255 }
11256
d6b818ef
JM
11257 if (!saved && !ret) {
11258 wpa_dbg(wpa_s, MSG_DEBUG,
11259 "CTRL_IFACE: SAVE_CONFIG - No configuration files could be updated");
11260 ret = 1;
11261 }
11262
42868f14
JM
11263 return ret;
11264}
11265#endif /* CONFIG_NO_CONFIG_WRITE */
11266
11267
ae8c27f7
JM
11268static int wpas_global_ctrl_iface_status(struct wpa_global *global,
11269 char *buf, size_t buflen)
11270{
11271 char *pos, *end;
11272 int ret;
11273 struct wpa_supplicant *wpa_s;
11274
11275 pos = buf;
11276 end = buf + buflen;
11277
11278#ifdef CONFIG_P2P
4c559019 11279 if (global->p2p && !global->p2p_disabled) {
ae8c27f7 11280 ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
4c559019
JM
11281 "\n"
11282 "p2p_state=%s\n",
11283 MAC2STR(global->p2p_dev_addr),
11284 p2p_get_state_txt(global->p2p));
d85e1fc8 11285 if (os_snprintf_error(end - pos, ret))
4c559019
JM
11286 return pos - buf;
11287 pos += ret;
11288 } else if (global->p2p) {
11289 ret = os_snprintf(pos, end - pos, "p2p_state=DISABLED\n");
d85e1fc8 11290 if (os_snprintf_error(end - pos, ret))
ae8c27f7
JM
11291 return pos - buf;
11292 pos += ret;
11293 }
11294#endif /* CONFIG_P2P */
11295
11296#ifdef CONFIG_WIFI_DISPLAY
11297 ret = os_snprintf(pos, end - pos, "wifi_display=%d\n",
11298 !!global->wifi_display);
d85e1fc8 11299 if (os_snprintf_error(end - pos, ret))
ae8c27f7
JM
11300 return pos - buf;
11301 pos += ret;
11302#endif /* CONFIG_WIFI_DISPLAY */
11303
11304 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
11305 ret = os_snprintf(pos, end - pos, "ifname=%s\n"
11306 "address=" MACSTR "\n",
11307 wpa_s->ifname, MAC2STR(wpa_s->own_addr));
d85e1fc8 11308 if (os_snprintf_error(end - pos, ret))
ae8c27f7
JM
11309 return pos - buf;
11310 pos += ret;
11311 }
11312
11313 return pos - buf;
11314}
11315
11316
3794af2d
AN
11317#ifdef CONFIG_FST
11318
11319static int wpas_global_ctrl_iface_fst_attach(struct wpa_global *global,
11320 char *cmd, char *buf,
11321 size_t reply_size)
11322{
11323 char ifname[IFNAMSIZ + 1];
11324 struct fst_iface_cfg cfg;
11325 struct wpa_supplicant *wpa_s;
11326 struct fst_wpa_obj iface_obj;
11327
11328 if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
11329 wpa_s = wpa_supplicant_get_iface(global, ifname);
11330 if (wpa_s) {
5dbd3bf9
JM
11331 if (wpa_s->fst) {
11332 wpa_printf(MSG_INFO, "FST: Already attached");
11333 return -1;
11334 }
3794af2d
AN
11335 fst_wpa_supplicant_fill_iface_obj(wpa_s, &iface_obj);
11336 wpa_s->fst = fst_attach(ifname, wpa_s->own_addr,
11337 &iface_obj, &cfg);
11338 if (wpa_s->fst)
11339 return os_snprintf(buf, reply_size, "OK\n");
11340 }
11341 }
11342
11343 return -1;
11344}
11345
11346
11347static int wpas_global_ctrl_iface_fst_detach(struct wpa_global *global,
11348 char *cmd, char *buf,
11349 size_t reply_size)
11350{
11351 char ifname[IFNAMSIZ + 1];
11352 struct wpa_supplicant *wpa_s;
11353
11354 if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
11355 wpa_s = wpa_supplicant_get_iface(global, ifname);
11356 if (wpa_s) {
11357 if (!fst_iface_detach(ifname)) {
11358 wpa_s->fst = NULL;
11359 return os_snprintf(buf, reply_size, "OK\n");
11360 }
11361 }
11362 }
11363
11364 return -1;
11365}
11366
11367#endif /* CONFIG_FST */
11368
11369
6fc6879b
JM
11370char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
11371 char *buf, size_t *resp_len)
11372{
11373 char *reply;
11374 const int reply_size = 2048;
11375 int reply_len;
f4a0a82c 11376 int level = MSG_DEBUG;
6fc6879b 11377
cf3bebf2
JM
11378 if (os_strncmp(buf, "IFNAME=", 7) == 0) {
11379 char *pos = os_strchr(buf + 7, ' ');
11380 if (pos) {
11381 *pos++ = '\0';
11382 return wpas_global_ctrl_iface_ifname(global,
11383 buf + 7, pos,
11384 resp_len);
11385 }
11386 }
11387
576bce9c
JM
11388 reply = wpas_global_ctrl_iface_redir(global, buf, resp_len);
11389 if (reply)
11390 return reply;
11391
f4a0a82c
JM
11392 if (os_strcmp(buf, "PING") == 0)
11393 level = MSG_EXCESSIVE;
11394 wpa_hexdump_ascii(level, "RX global ctrl_iface",
6fc6879b
JM
11395 (const u8 *) buf, os_strlen(buf));
11396
11397 reply = os_malloc(reply_size);
11398 if (reply == NULL) {
11399 *resp_len = 1;
11400 return NULL;
11401 }
11402
11403 os_memcpy(reply, "OK\n", 3);
11404 reply_len = 3;
11405
11406 if (os_strcmp(buf, "PING") == 0) {
11407 os_memcpy(reply, "PONG\n", 5);
11408 reply_len = 5;
11409 } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
11410 if (wpa_supplicant_global_iface_add(global, buf + 14))
11411 reply_len = -1;
11412 } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
11413 if (wpa_supplicant_global_iface_remove(global, buf + 17))
11414 reply_len = -1;
4b4a8ae5
JM
11415 } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
11416 reply_len = wpa_supplicant_global_iface_list(
11417 global, reply, reply_size);
56e2fc2c 11418 } else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
6fc6879b 11419 reply_len = wpa_supplicant_global_iface_interfaces(
56e2fc2c 11420 global, buf + 10, reply, reply_size);
3794af2d
AN
11421#ifdef CONFIG_FST
11422 } else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
11423 reply_len = wpas_global_ctrl_iface_fst_attach(global, buf + 11,
11424 reply,
11425 reply_size);
11426 } else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
11427 reply_len = wpas_global_ctrl_iface_fst_detach(global, buf + 11,
11428 reply,
11429 reply_size);
11430 } else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
11431 reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
11432#endif /* CONFIG_FST */
6fc6879b 11433 } else if (os_strcmp(buf, "TERMINATE") == 0) {
1a1bf008 11434 wpa_supplicant_terminate_proc(global);
207ef3fb
JM
11435 } else if (os_strcmp(buf, "SUSPEND") == 0) {
11436 wpas_notify_suspend(global);
11437 } else if (os_strcmp(buf, "RESUME") == 0) {
11438 wpas_notify_resume(global);
1b9b31c1 11439 } else if (os_strncmp(buf, "SET ", 4) == 0) {
a7ca6dac
JM
11440 if (wpas_global_ctrl_iface_set(global, buf + 4)) {
11441#ifdef CONFIG_P2P
11442 if (global->p2p_init_wpa_s) {
11443 os_free(reply);
11444 /* Check if P2P redirection would work for this
11445 * command. */
11446 return wpa_supplicant_ctrl_iface_process(
11447 global->p2p_init_wpa_s,
11448 buf, resp_len);
11449 }
11450#endif /* CONFIG_P2P */
1b9b31c1 11451 reply_len = -1;
a7ca6dac 11452 }
daae4995
AN
11453 } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
11454 if (wpas_global_ctrl_iface_dup_network(global, buf + 12))
11455 reply_len = -1;
42868f14
JM
11456#ifndef CONFIG_NO_CONFIG_WRITE
11457 } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
11458 if (wpas_global_ctrl_iface_save_config(global))
11459 reply_len = -1;
11460#endif /* CONFIG_NO_CONFIG_WRITE */
ae8c27f7
JM
11461 } else if (os_strcmp(buf, "STATUS") == 0) {
11462 reply_len = wpas_global_ctrl_iface_status(global, reply,
11463 reply_size);
ea449b5b
JM
11464#ifdef CONFIG_MODULE_TESTS
11465 } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
ea449b5b
JM
11466 if (wpas_module_tests() < 0)
11467 reply_len = -1;
11468#endif /* CONFIG_MODULE_TESTS */
5f797376
JM
11469 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
11470 if (wpa_debug_reopen_file() < 0)
11471 reply_len = -1;
6fc6879b
JM
11472 } else {
11473 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
11474 reply_len = 16;
11475 }
11476
11477 if (reply_len < 0) {
11478 os_memcpy(reply, "FAIL\n", 5);
11479 reply_len = 5;
11480 }
11481
11482 *resp_len = reply_len;
11483 return reply;
11484}