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