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