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