]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/ctrl_iface.c
Test functionality to override driver reported signal levels
[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 */
0382097e
JM
5348 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
5349 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
5350 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
5351 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
0382097e
JM
5352 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
5353 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
32d5295f
JM
5354
5355 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
5356 0);
5357 /* MLME-SETPROTECTION.request(None) */
5358 wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
5359 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
5360 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
5361 wpa_sm_drop_sa(wpa_s->wpa);
5362}
9ff4de6d 5363#endif /* CONFIG_TESTING_OPTIONS */
32d5295f
JM
5364
5365
86d4f806
JM
5366static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
5367 char *addr)
5368{
90b8fc8f
JM
5369#ifdef CONFIG_NO_SCAN_PROCESSING
5370 return -1;
5371#else /* CONFIG_NO_SCAN_PROCESSING */
86d4f806
JM
5372 u8 bssid[ETH_ALEN];
5373 struct wpa_bss *bss;
5374 struct wpa_ssid *ssid = wpa_s->current_ssid;
5375
5376 if (hwaddr_aton(addr, bssid)) {
5377 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
5378 "address '%s'", addr);
5379 return -1;
5380 }
5381
5382 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
5383
2f9b66d3
JM
5384 if (!ssid) {
5385 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
5386 "configuration known for the target AP");
5387 return -1;
5388 }
5389
5390 bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
86d4f806
JM
5391 if (!bss) {
5392 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
5393 "from BSS table");
5394 return -1;
5395 }
5396
5397 /*
5398 * TODO: Find best network configuration block from configuration to
5399 * allow roaming to other networks
5400 */
5401
86d4f806
JM
5402 wpa_s->reassociate = 1;
5403 wpa_supplicant_connect(wpa_s, bss, ssid);
5404
5405 return 0;
90b8fc8f 5406#endif /* CONFIG_NO_SCAN_PROCESSING */
86d4f806
JM
5407}
5408
5409
b563b388
JM
5410#ifdef CONFIG_P2P
5411static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
5412{
5413 unsigned int timeout = atoi(cmd);
5414 enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
6d92fa6e 5415 u8 dev_id[ETH_ALEN], *_dev_id = NULL;
2b384109 5416 u8 dev_type[WPS_DEV_TYPE_LEN], *_dev_type = NULL;
6d92fa6e 5417 char *pos;
05a77b3b 5418 unsigned int search_delay;
9542f21f 5419 const char *_seek[P2P_MAX_QUERY_HASH + 1], **seek = NULL;
51775096 5420 u8 seek_count = 0;
fa9f381f 5421 int freq = 0;
b563b388 5422
e9eb648e
JM
5423 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
5424 wpa_dbg(wpa_s, MSG_INFO,
5425 "Reject P2P_FIND since interface is disabled");
5426 return -1;
5427 }
b563b388
JM
5428 if (os_strstr(cmd, "type=social"))
5429 type = P2P_FIND_ONLY_SOCIAL;
5430 else if (os_strstr(cmd, "type=progressive"))
5431 type = P2P_FIND_PROGRESSIVE;
5432
6d92fa6e
JM
5433 pos = os_strstr(cmd, "dev_id=");
5434 if (pos) {
5435 pos += 7;
5436 if (hwaddr_aton(pos, dev_id))
5437 return -1;
5438 _dev_id = dev_id;
5439 }
5440
2b384109
JM
5441 pos = os_strstr(cmd, "dev_type=");
5442 if (pos) {
5443 pos += 9;
5444 if (wps_dev_type_str2bin(pos, dev_type) < 0)
5445 return -1;
5446 _dev_type = dev_type;
5447 }
5448
37448ede
JM
5449 pos = os_strstr(cmd, "delay=");
5450 if (pos) {
5451 pos += 6;
5452 search_delay = atoi(pos);
05a77b3b
JM
5453 } else
5454 search_delay = wpas_p2p_search_delay(wpa_s);
37448ede 5455
a9ea609c
SM
5456 pos = os_strstr(cmd, "freq=");
5457 if (pos) {
5458 pos += 5;
5459 freq = atoi(pos);
5460 if (freq <= 0)
5461 return -1;
5462 }
5463
51775096
BG
5464 /* Must be searched for last, because it adds nul termination */
5465 pos = os_strstr(cmd, " seek=");
129b6216
JM
5466 if (pos)
5467 pos += 6;
51775096
BG
5468 while (pos && seek_count < P2P_MAX_QUERY_HASH + 1) {
5469 char *term;
5470
129b6216 5471 _seek[seek_count++] = pos;
9542f21f 5472 seek = _seek;
129b6216
JM
5473 term = os_strchr(pos, ' ');
5474 if (!term)
5475 break;
5476 *term = '\0';
5477 pos = os_strstr(term + 1, "seek=");
5478 if (pos)
5479 pos += 5;
51775096 5480 }
9542f21f
JM
5481 if (seek_count > P2P_MAX_QUERY_HASH) {
5482 seek[0] = NULL;
5483 seek_count = 1;
5484 }
51775096 5485
2b384109 5486 return wpas_p2p_find(wpa_s, timeout, type, _dev_type != NULL, _dev_type,
fa9f381f 5487 _dev_id, search_delay, seek_count, seek, freq);
b563b388
JM
5488}
5489
5490
e2b7fbf2
MS
5491static int p2ps_ctrl_parse_cpt_priority(const char *pos, u8 *cpt)
5492{
5493 const char *last = NULL;
5494 const char *token;
5495 long int token_len;
5496 unsigned int i;
5497
5498 /* Expected predefined CPT names delimited by ':' */
5499 for (i = 0; (token = cstr_token(pos, ": \t", &last)); i++) {
5500 if (i >= P2PS_FEATURE_CAPAB_CPT_MAX) {
5501 wpa_printf(MSG_ERROR,
5502 "P2PS: CPT name list is too long, expected up to %d names",
5503 P2PS_FEATURE_CAPAB_CPT_MAX);
5504 cpt[0] = 0;
5505 return -1;
5506 }
5507
5508 token_len = last - token;
5509
5510 if (token_len == 3 &&
5511 os_memcmp(token, "UDP", token_len) == 0) {
5512 cpt[i] = P2PS_FEATURE_CAPAB_UDP_TRANSPORT;
5513 } else if (token_len == 3 &&
5514 os_memcmp(token, "MAC", token_len) == 0) {
5515 cpt[i] = P2PS_FEATURE_CAPAB_MAC_TRANSPORT;
5516 } else {
5517 wpa_printf(MSG_ERROR,
5518 "P2PS: Unsupported CPT name '%s'", token);
5519 cpt[0] = 0;
5520 return -1;
5521 }
5522
640b0b93 5523 if (isblank((unsigned char) *last)) {
e2b7fbf2
MS
5524 i++;
5525 break;
5526 }
5527 }
5528 cpt[i] = 0;
5529 return 0;
5530}
5531
5532
f309c18e
KV
5533static struct p2ps_provision * p2p_parse_asp_provision_cmd(const char *cmd)
5534{
5535 struct p2ps_provision *p2ps_prov;
5536 char *pos;
5537 size_t info_len = 0;
5538 char *info = NULL;
5539 u8 role = P2PS_SETUP_NONE;
5540 long long unsigned val;
0670de74 5541 int i;
f309c18e
KV
5542
5543 pos = os_strstr(cmd, "info=");
5544 if (pos) {
5545 pos += 5;
5546 info_len = os_strlen(pos);
5547
5548 if (info_len) {
5549 info = os_malloc(info_len + 1);
5550 if (info) {
5551 info_len = utf8_unescape(pos, info_len,
5552 info, info_len + 1);
5553 } else
5554 info_len = 0;
5555 }
5556 }
5557
5558 p2ps_prov = os_zalloc(sizeof(struct p2ps_provision) + info_len + 1);
5559 if (p2ps_prov == NULL) {
5560 os_free(info);
5561 return NULL;
5562 }
5563
5564 if (info) {
5565 os_memcpy(p2ps_prov->info, info, info_len);
5566 p2ps_prov->info[info_len] = '\0';
5567 os_free(info);
5568 }
5569
5570 pos = os_strstr(cmd, "status=");
5571 if (pos)
5572 p2ps_prov->status = atoi(pos + 7);
5573 else
5574 p2ps_prov->status = -1;
5575
5576 pos = os_strstr(cmd, "adv_id=");
5577 if (!pos || sscanf(pos + 7, "%llx", &val) != 1 || val > 0xffffffffULL)
5578 goto invalid_args;
5579 p2ps_prov->adv_id = val;
5580
5581 pos = os_strstr(cmd, "method=");
5582 if (pos)
5583 p2ps_prov->method = strtol(pos + 7, NULL, 16);
5584 else
5585 p2ps_prov->method = 0;
5586
5587 pos = os_strstr(cmd, "session=");
5588 if (!pos || sscanf(pos + 8, "%llx", &val) != 1 || val > 0xffffffffULL)
5589 goto invalid_args;
5590 p2ps_prov->session_id = val;
5591
5592 pos = os_strstr(cmd, "adv_mac=");
5593 if (!pos || hwaddr_aton(pos + 8, p2ps_prov->adv_mac))
5594 goto invalid_args;
5595
5596 pos = os_strstr(cmd, "session_mac=");
5597 if (!pos || hwaddr_aton(pos + 12, p2ps_prov->session_mac))
5598 goto invalid_args;
5599
0670de74
MS
5600 pos = os_strstr(cmd, "cpt=");
5601 if (pos) {
5602 if (p2ps_ctrl_parse_cpt_priority(pos + 4,
5603 p2ps_prov->cpt_priority))
5604 goto invalid_args;
5605 } else {
5606 p2ps_prov->cpt_priority[0] = P2PS_FEATURE_CAPAB_UDP_TRANSPORT;
5607 }
5608
5609 for (i = 0; p2ps_prov->cpt_priority[i]; i++)
5610 p2ps_prov->cpt_mask |= p2ps_prov->cpt_priority[i];
5611
f309c18e
KV
5612 /* force conncap with tstCap (no sanity checks) */
5613 pos = os_strstr(cmd, "tstCap=");
5614 if (pos) {
5615 role = strtol(pos + 7, NULL, 16);
5616 } else {
5617 pos = os_strstr(cmd, "role=");
5618 if (pos) {
5619 role = strtol(pos + 5, NULL, 16);
5620 if (role != P2PS_SETUP_CLIENT &&
5621 role != P2PS_SETUP_GROUP_OWNER)
5622 role = P2PS_SETUP_NONE;
5623 }
5624 }
5625 p2ps_prov->role = role;
5626
5627 return p2ps_prov;
5628
5629invalid_args:
5630 os_free(p2ps_prov);
5631 return NULL;
5632}
5633
5634
5635static int p2p_ctrl_asp_provision_resp(struct wpa_supplicant *wpa_s, char *cmd)
5636{
5637 u8 addr[ETH_ALEN];
5638 struct p2ps_provision *p2ps_prov;
5639 char *pos;
5640
5641 /* <addr> id=<adv_id> [role=<conncap>] [info=<infodata>] */
5642
5643 wpa_printf(MSG_DEBUG, "%s: %s", __func__, cmd);
5644
5645 if (hwaddr_aton(cmd, addr))
5646 return -1;
5647
5648 pos = cmd + 17;
5649 if (*pos != ' ')
5650 return -1;
5651
5652 p2ps_prov = p2p_parse_asp_provision_cmd(pos);
5653 if (!p2ps_prov)
5654 return -1;
5655
5656 if (p2ps_prov->status < 0) {
5657 os_free(p2ps_prov);
5658 return -1;
5659 }
5660
5661 return wpas_p2p_prov_disc(wpa_s, addr, NULL, WPAS_P2P_PD_FOR_ASP,
5662 p2ps_prov);
5663}
5664
5665
5666static int p2p_ctrl_asp_provision(struct wpa_supplicant *wpa_s, char *cmd)
5667{
5668 u8 addr[ETH_ALEN];
5669 struct p2ps_provision *p2ps_prov;
5670 char *pos;
5671
5672 /* <addr> id=<adv_id> adv_mac=<adv_mac> conncap=<conncap>
5673 * session=<ses_id> mac=<ses_mac> [info=<infodata>]
5674 */
5675
5676 wpa_printf(MSG_DEBUG, "%s: %s", __func__, cmd);
5677 if (hwaddr_aton(cmd, addr))
5678 return -1;
5679
5680 pos = cmd + 17;
5681 if (*pos != ' ')
5682 return -1;
5683
5684 p2ps_prov = p2p_parse_asp_provision_cmd(pos);
5685 if (!p2ps_prov)
5686 return -1;
5687
93f22b45
MS
5688 p2ps_prov->pd_seeker = 1;
5689
f309c18e
KV
5690 return wpas_p2p_prov_disc(wpa_s, addr, NULL, WPAS_P2P_PD_FOR_ASP,
5691 p2ps_prov);
5692}
5693
5694
c27f4c90
AK
5695static int parse_freq(int chwidth, int freq2)
5696{
5697 if (freq2 < 0)
5698 return -1;
5699 if (freq2)
464dcfd0 5700 return CHANWIDTH_80P80MHZ;
c27f4c90
AK
5701
5702 switch (chwidth) {
5703 case 0:
5704 case 20:
5705 case 40:
464dcfd0 5706 return CHANWIDTH_USE_HT;
c27f4c90 5707 case 80:
464dcfd0 5708 return CHANWIDTH_80MHZ;
c27f4c90 5709 case 160:
464dcfd0 5710 return CHANWIDTH_160MHZ;
c27f4c90
AK
5711 default:
5712 wpa_printf(MSG_DEBUG, "Unknown max oper bandwidth: %d",
5713 chwidth);
5714 return -1;
5715 }
5716}
5717
5718
b563b388
JM
5719static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
5720 char *buf, size_t buflen)
5721{
5722 u8 addr[ETH_ALEN];
5723 char *pos, *pos2;
5724 char *pin = NULL;
5725 enum p2p_wps_method wps_method;
5726 int new_pin;
5727 int ret;
23c84252 5728 int persistent_group, persistent_id = -1;
b563b388
JM
5729 int join;
5730 int auth;
b31be3a0 5731 int automatic;
b563b388
JM
5732 int go_intent = -1;
5733 int freq = 0;
3bc462cb 5734 int pd;
c27f4c90 5735 int ht40, vht, max_oper_chwidth, chwidth = 0, freq2 = 0;
99666225 5736 int edmg;
8edd9f10
JM
5737 u8 _group_ssid[SSID_MAX_LEN], *group_ssid = NULL;
5738 size_t group_ssid_len = 0;
5a3319ab 5739 int he;
b563b388 5740
bdf0518b
JM
5741 if (!wpa_s->global->p2p_init_wpa_s)
5742 return -1;
5743 if (wpa_s->global->p2p_init_wpa_s != wpa_s) {
5744 wpa_dbg(wpa_s, MSG_DEBUG, "Direct P2P_CONNECT command to %s",
5745 wpa_s->global->p2p_init_wpa_s->ifname);
5746 wpa_s = wpa_s->global->p2p_init_wpa_s;
5747 }
5748
4f88fc04 5749 /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad|p2ps]
23c84252 5750 * [persistent|persistent=<network id>]
e2308e4b 5751 * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
99666225 5752 * [ht40] [vht] [he] [edmg] [auto] [ssid=<hexdump>] */
b563b388
JM
5753
5754 if (hwaddr_aton(cmd, addr))
5755 return -1;
5756
5757 pos = cmd + 17;
5758 if (*pos != ' ')
5759 return -1;
5760 pos++;
5761
5762 persistent_group = os_strstr(pos, " persistent") != NULL;
23c84252
JM
5763 pos2 = os_strstr(pos, " persistent=");
5764 if (pos2) {
5765 struct wpa_ssid *ssid;
5766 persistent_id = atoi(pos2 + 12);
5767 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
5768 if (ssid == NULL || ssid->disabled != 2 ||
5769 ssid->mode != WPAS_MODE_P2P_GO) {
5770 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
5771 "SSID id=%d for persistent P2P group (GO)",
5772 persistent_id);
5773 return -1;
5774 }
5775 }
b563b388
JM
5776 join = os_strstr(pos, " join") != NULL;
5777 auth = os_strstr(pos, " auth") != NULL;
b31be3a0 5778 automatic = os_strstr(pos, " auto") != NULL;
3bc462cb 5779 pd = os_strstr(pos, " provdisc") != NULL;
20ea1ca4
EP
5780 vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
5781 ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
5782 vht;
5a3319ab 5783 he = (os_strstr(cmd, " he") != NULL) || wpa_s->conf->p2p_go_he;
99666225 5784 edmg = (os_strstr(cmd, " edmg") != NULL) || wpa_s->conf->p2p_go_edmg;
b563b388
JM
5785
5786 pos2 = os_strstr(pos, " go_intent=");
5787 if (pos2) {
5788 pos2 += 11;
5789 go_intent = atoi(pos2);
5790 if (go_intent < 0 || go_intent > 15)
5791 return -1;
5792 }
5793
5794 pos2 = os_strstr(pos, " freq=");
5795 if (pos2) {
5796 pos2 += 6;
5797 freq = atoi(pos2);
5798 if (freq <= 0)
5799 return -1;
5800 }
5801
c27f4c90
AK
5802 pos2 = os_strstr(pos, " freq2=");
5803 if (pos2)
5804 freq2 = atoi(pos2 + 7);
5805
5806 pos2 = os_strstr(pos, " max_oper_chwidth=");
5807 if (pos2)
5808 chwidth = atoi(pos2 + 18);
5809
5810 max_oper_chwidth = parse_freq(chwidth, freq2);
5811 if (max_oper_chwidth < 0)
5812 return -1;
5813
8edd9f10
JM
5814 pos2 = os_strstr(pos, " ssid=");
5815 if (pos2) {
5816 char *end;
5817
5818 pos2 += 6;
5819 end = os_strchr(pos2, ' ');
5820 if (!end)
5821 group_ssid_len = os_strlen(pos2) / 2;
5822 else
5823 group_ssid_len = (end - pos2) / 2;
5824 if (group_ssid_len == 0 || group_ssid_len > SSID_MAX_LEN ||
5825 hexstr2bin(pos2, _group_ssid, group_ssid_len) < 0)
5826 return -1;
5827 group_ssid = _group_ssid;
5828 }
5829
b563b388
JM
5830 if (os_strncmp(pos, "pin", 3) == 0) {
5831 /* Request random PIN (to be displayed) and enable the PIN */
5832 wps_method = WPS_PIN_DISPLAY;
5833 } else if (os_strncmp(pos, "pbc", 3) == 0) {
5834 wps_method = WPS_PBC;
31d7fb14
PK
5835 } else if (os_strstr(pos, "p2ps") != NULL) {
5836 wps_method = WPS_P2PS;
b563b388
JM
5837 } else {
5838 pin = pos;
5839 pos = os_strchr(pin, ' ');
5840 wps_method = WPS_PIN_KEYPAD;
5841 if (pos) {
5842 *pos++ = '\0';
07fecd39 5843 if (os_strncmp(pos, "display", 7) == 0)
b563b388
JM
5844 wps_method = WPS_PIN_DISPLAY;
5845 }
dcc33057 5846 if (!wps_pin_str_valid(pin)) {
36ebf7a1
MH
5847 os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
5848 return 17;
5849 }
b563b388
JM
5850 }
5851
5852 new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
b31be3a0 5853 persistent_group, automatic, join,
c27f4c90 5854 auth, go_intent, freq, freq2, persistent_id,
99666225 5855 pd, ht40, vht, max_oper_chwidth, he, edmg,
8edd9f10 5856 group_ssid, group_ssid_len);
d054a462
JM
5857 if (new_pin == -2) {
5858 os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
5859 return 25;
5860 }
5861 if (new_pin == -3) {
5862 os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
5863 return 25;
5864 }
b563b388
JM
5865 if (new_pin < 0)
5866 return -1;
5867 if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
5868 ret = os_snprintf(buf, buflen, "%08d", new_pin);
d85e1fc8 5869 if (os_snprintf_error(buflen, ret))
b563b388
JM
5870 return -1;
5871 return ret;
5872 }
5873
5874 os_memcpy(buf, "OK\n", 3);
5875 return 3;
5876}
5877
5878
5879static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
5880{
5881 unsigned int timeout = atoi(cmd);
e9eb648e
JM
5882 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
5883 wpa_dbg(wpa_s, MSG_INFO,
5884 "Reject P2P_LISTEN since interface is disabled");
5885 return -1;
5886 }
b563b388
JM
5887 return wpas_p2p_listen(wpa_s, timeout);
5888}
5889
5890
5891static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
5892{
5893 u8 addr[ETH_ALEN];
5894 char *pos;
0918c4bf 5895 enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
b563b388 5896
0918c4bf 5897 /* <addr> <config method> [join|auto] */
b563b388
JM
5898
5899 if (hwaddr_aton(cmd, addr))
5900 return -1;
5901
5902 pos = cmd + 17;
5903 if (*pos != ' ')
5904 return -1;
5905 pos++;
5906
0918c4bf
JM
5907 if (os_strstr(pos, " join") != NULL)
5908 use = WPAS_P2P_PD_FOR_JOIN;
5909 else if (os_strstr(pos, " auto") != NULL)
5910 use = WPAS_P2P_PD_AUTO;
5911
6d908514 5912 return wpas_p2p_prov_disc(wpa_s, addr, pos, use, NULL);
b563b388
JM
5913}
5914
5915
5916static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
5917 size_t buflen)
5918{
5919 struct wpa_ssid *ssid = wpa_s->current_ssid;
5920
5921 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
5922 ssid->passphrase == NULL)
5923 return -1;
5924
5925 os_strlcpy(buf, ssid->passphrase, buflen);
5926 return os_strlen(buf);
5927}
5928
5929
5930static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
5931 char *buf, size_t buflen)
5932{
5933 u64 ref;
5934 int res;
5935 u8 dst_buf[ETH_ALEN], *dst;
5936 struct wpabuf *tlvs;
5937 char *pos;
5938 size_t len;
5939
5940 if (hwaddr_aton(cmd, dst_buf))
5941 return -1;
5942 dst = dst_buf;
5943 if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
5944 dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
5945 dst = NULL;
5946 pos = cmd + 17;
5947 if (*pos != ' ')
5948 return -1;
5949 pos++;
5950
5951 if (os_strncmp(pos, "upnp ", 5) == 0) {
5952 u8 version;
5953 pos += 5;
5954 if (hexstr2bin(pos, &version, 1) < 0)
5955 return -1;
5956 pos += 2;
5957 if (*pos != ' ')
5958 return -1;
5959 pos++;
7165c5dc 5960 ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
347d6a5b
JM
5961#ifdef CONFIG_WIFI_DISPLAY
5962 } else if (os_strncmp(pos, "wifi-display ", 13) == 0) {
5963 ref = wpas_p2p_sd_request_wifi_display(wpa_s, dst, pos + 13);
5964#endif /* CONFIG_WIFI_DISPLAY */
5a4102ce
KV
5965 } else if (os_strncmp(pos, "asp ", 4) == 0) {
5966 char *svc_str;
5967 char *svc_info = NULL;
5968 u32 id;
5969
5970 pos += 4;
5971 if (sscanf(pos, "%x", &id) != 1 || id > 0xff)
5972 return -1;
5973
5974 pos = os_strchr(pos, ' ');
5975 if (pos == NULL || pos[1] == '\0' || pos[1] == ' ')
5976 return -1;
5977
5978 svc_str = pos + 1;
5979
5980 pos = os_strchr(svc_str, ' ');
5981
5982 if (pos)
5983 *pos++ = '\0';
5984
5985 /* All remaining data is the svc_info string */
5986 if (pos && pos[0] && pos[0] != ' ') {
5987 len = os_strlen(pos);
5988
5989 /* Unescape in place */
5990 len = utf8_unescape(pos, len, pos, len);
5991 if (len > 0xff)
5992 return -1;
5993
5994 svc_info = pos;
5995 }
5996
5997 ref = wpas_p2p_sd_request_asp(wpa_s, dst, (u8) id,
5998 svc_str, svc_info);
b563b388
JM
5999 } else {
6000 len = os_strlen(pos);
6001 if (len & 1)
6002 return -1;
6003 len /= 2;
6004 tlvs = wpabuf_alloc(len);
6005 if (tlvs == NULL)
6006 return -1;
6007 if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
6008 wpabuf_free(tlvs);
6009 return -1;
6010 }
6011
7165c5dc 6012 ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
b563b388
JM
6013 wpabuf_free(tlvs);
6014 }
7165c5dc
JM
6015 if (ref == 0)
6016 return -1;
b563b388 6017 res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
d85e1fc8 6018 if (os_snprintf_error(buflen, res))
b563b388
JM
6019 return -1;
6020 return res;
6021}
6022
6023
6024static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
6025 char *cmd)
6026{
6027 long long unsigned val;
6028 u64 req;
6029 if (sscanf(cmd, "%llx", &val) != 1)
6030 return -1;
6031 req = val;
7165c5dc 6032 return wpas_p2p_sd_cancel_request(wpa_s, req);
b563b388
JM
6033}
6034
6035
6036static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
6037{
6038 int freq;
d25f7212 6039 u8 dst[ETH_ALEN];
b563b388
JM
6040 u8 dialog_token;
6041 struct wpabuf *resp_tlvs;
6042 char *pos, *pos2;
6043 size_t len;
6044
6045 pos = os_strchr(cmd, ' ');
6046 if (pos == NULL)
6047 return -1;
6048 *pos++ = '\0';
6049 freq = atoi(cmd);
6050 if (freq == 0)
6051 return -1;
6052
d25f7212 6053 if (hwaddr_aton(pos, dst))
b563b388 6054 return -1;
b563b388
JM
6055 pos += 17;
6056 if (*pos != ' ')
6057 return -1;
6058 pos++;
6059
6060 pos2 = os_strchr(pos, ' ');
6061 if (pos2 == NULL)
6062 return -1;
6063 *pos2++ = '\0';
6064 dialog_token = atoi(pos);
6065
6066 len = os_strlen(pos2);
6067 if (len & 1)
6068 return -1;
6069 len /= 2;
6070 resp_tlvs = wpabuf_alloc(len);
6071 if (resp_tlvs == NULL)
6072 return -1;
6073 if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
6074 wpabuf_free(resp_tlvs);
6075 return -1;
6076 }
6077
6078 wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
6079 wpabuf_free(resp_tlvs);
6080 return 0;
6081}
6082
6083
6084static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
6085 char *cmd)
6086{
28ef705d
GB
6087 if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
6088 return -1;
b563b388
JM
6089 wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
6090 return 0;
6091}
6092
6093
6094static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
6095 char *cmd)
6096{
6097 char *pos;
6098 size_t len;
6099 struct wpabuf *query, *resp;
6100
6101 pos = os_strchr(cmd, ' ');
6102 if (pos == NULL)
6103 return -1;
6104 *pos++ = '\0';
6105
6106 len = os_strlen(cmd);
6107 if (len & 1)
6108 return -1;
6109 len /= 2;
6110 query = wpabuf_alloc(len);
6111 if (query == NULL)
6112 return -1;
6113 if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
6114 wpabuf_free(query);
6115 return -1;
6116 }
6117
6118 len = os_strlen(pos);
6119 if (len & 1) {
6120 wpabuf_free(query);
6121 return -1;
6122 }
6123 len /= 2;
6124 resp = wpabuf_alloc(len);
6125 if (resp == NULL) {
6126 wpabuf_free(query);
6127 return -1;
6128 }
6129 if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
6130 wpabuf_free(query);
6131 wpabuf_free(resp);
6132 return -1;
6133 }
6134
6135 if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
6136 wpabuf_free(query);
6137 wpabuf_free(resp);
6138 return -1;
6139 }
6140 return 0;
6141}
6142
6143
6144static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
6145{
6146 char *pos;
6147 u8 version;
6148
6149 pos = os_strchr(cmd, ' ');
6150 if (pos == NULL)
6151 return -1;
6152 *pos++ = '\0';
6153
6154 if (hexstr2bin(cmd, &version, 1) < 0)
6155 return -1;
6156
6157 return wpas_p2p_service_add_upnp(wpa_s, version, pos);
6158}
6159
6160
ae9d45f3
KV
6161static int p2p_ctrl_service_add_asp(struct wpa_supplicant *wpa_s,
6162 u8 replace, char *cmd)
6163{
6164 char *pos;
6165 char *adv_str;
6166 u32 auto_accept, adv_id, svc_state, config_methods;
6167 char *svc_info = NULL;
e2b7fbf2
MS
6168 char *cpt_prio_str;
6169 u8 cpt_prio[P2PS_FEATURE_CAPAB_CPT_MAX + 1];
ae9d45f3
KV
6170
6171 pos = os_strchr(cmd, ' ');
6172 if (pos == NULL)
6173 return -1;
6174 *pos++ = '\0';
6175
6176 /* Auto-Accept value is mandatory, and must be one of the
6177 * single values (0, 1, 2, 4) */
6178 auto_accept = atoi(cmd);
6179 switch (auto_accept) {
6180 case P2PS_SETUP_NONE: /* No auto-accept */
6181 case P2PS_SETUP_NEW:
6182 case P2PS_SETUP_CLIENT:
6183 case P2PS_SETUP_GROUP_OWNER:
6184 break;
6185 default:
6186 return -1;
6187 }
6188
6189 /* Advertisement ID is mandatory */
6190 cmd = pos;
6191 pos = os_strchr(cmd, ' ');
6192 if (pos == NULL)
6193 return -1;
6194 *pos++ = '\0';
6195
6196 /* Handle Adv_ID == 0 (wildcard "org.wi-fi.wfds") internally. */
6197 if (sscanf(cmd, "%x", &adv_id) != 1 || adv_id == 0)
6198 return -1;
6199
6200 /* Only allow replacements if exist, and adds if not */
6201 if (wpas_p2p_service_p2ps_id_exists(wpa_s, adv_id)) {
6202 if (!replace)
6203 return -1;
6204 } else {
6205 if (replace)
6206 return -1;
6207 }
6208
6209 /* svc_state between 0 - 0xff is mandatory */
6210 if (sscanf(pos, "%x", &svc_state) != 1 || svc_state > 0xff)
6211 return -1;
6212
6213 pos = os_strchr(pos, ' ');
6214 if (pos == NULL)
6215 return -1;
6216
6217 /* config_methods is mandatory */
6218 pos++;
6219 if (sscanf(pos, "%x", &config_methods) != 1)
6220 return -1;
6221
6222 if (!(config_methods &
6223 (WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD | WPS_CONFIG_P2PS)))
6224 return -1;
6225
6226 pos = os_strchr(pos, ' ');
6227 if (pos == NULL)
6228 return -1;
6229
6230 pos++;
6231 adv_str = pos;
6232
6233 /* Advertisement string is mandatory */
6234 if (!pos[0] || pos[0] == ' ')
6235 return -1;
6236
6237 /* Terminate svc string */
6238 pos = os_strchr(pos, ' ');
6239 if (pos != NULL)
6240 *pos++ = '\0';
6241
e2b7fbf2
MS
6242 cpt_prio_str = (pos && pos[0]) ? os_strstr(pos, "cpt=") : NULL;
6243 if (cpt_prio_str) {
6244 pos = os_strchr(pos, ' ');
6245 if (pos != NULL)
6246 *pos++ = '\0';
6247
6248 if (p2ps_ctrl_parse_cpt_priority(cpt_prio_str + 4, cpt_prio))
6249 return -1;
6250 } else {
6251 cpt_prio[0] = P2PS_FEATURE_CAPAB_UDP_TRANSPORT;
6252 cpt_prio[1] = 0;
6253 }
6254
ae9d45f3
KV
6255 /* Service and Response Information are optional */
6256 if (pos && pos[0]) {
6257 size_t len;
6258
6259 /* Note the bare ' included, which cannot exist legally
6260 * in unescaped string. */
6261 svc_info = os_strstr(pos, "svc_info='");
6262
6263 if (svc_info) {
6264 svc_info += 9;
6265 len = os_strlen(svc_info);
6266 utf8_unescape(svc_info, len, svc_info, len);
6267 }
6268 }
6269
6270 return wpas_p2p_service_add_asp(wpa_s, auto_accept, adv_id, adv_str,
6271 (u8) svc_state, (u16) config_methods,
e2b7fbf2 6272 svc_info, cpt_prio);
ae9d45f3
KV
6273}
6274
6275
b563b388
JM
6276static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
6277{
6278 char *pos;
6279
6280 pos = os_strchr(cmd, ' ');
6281 if (pos == NULL)
6282 return -1;
6283 *pos++ = '\0';
6284
6285 if (os_strcmp(cmd, "bonjour") == 0)
6286 return p2p_ctrl_service_add_bonjour(wpa_s, pos);
6287 if (os_strcmp(cmd, "upnp") == 0)
6288 return p2p_ctrl_service_add_upnp(wpa_s, pos);
ae9d45f3
KV
6289 if (os_strcmp(cmd, "asp") == 0)
6290 return p2p_ctrl_service_add_asp(wpa_s, 0, pos);
b563b388
JM
6291 wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
6292 return -1;
6293}
6294
6295
6296static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
6297 char *cmd)
6298{
6299 size_t len;
6300 struct wpabuf *query;
6301 int ret;
6302
6303 len = os_strlen(cmd);
6304 if (len & 1)
6305 return -1;
6306 len /= 2;
6307 query = wpabuf_alloc(len);
6308 if (query == NULL)
6309 return -1;
6310 if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
6311 wpabuf_free(query);
6312 return -1;
6313 }
6314
6315 ret = wpas_p2p_service_del_bonjour(wpa_s, query);
6316 wpabuf_free(query);
6317 return ret;
6318}
6319
6320
6321static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
6322{
6323 char *pos;
6324 u8 version;
6325
6326 pos = os_strchr(cmd, ' ');
6327 if (pos == NULL)
6328 return -1;
6329 *pos++ = '\0';
6330
6331 if (hexstr2bin(cmd, &version, 1) < 0)
6332 return -1;
6333
6334 return wpas_p2p_service_del_upnp(wpa_s, version, pos);
6335}
6336
6337
ae9d45f3
KV
6338static int p2p_ctrl_service_del_asp(struct wpa_supplicant *wpa_s, char *cmd)
6339{
6340 u32 adv_id;
6341
e9d28050
MS
6342 if (os_strcmp(cmd, "all") == 0) {
6343 wpas_p2p_service_flush_asp(wpa_s);
6344 return 0;
6345 }
6346
ae9d45f3
KV
6347 if (sscanf(cmd, "%x", &adv_id) != 1)
6348 return -1;
6349
6350 return wpas_p2p_service_del_asp(wpa_s, adv_id);
6351}
6352
6353
b563b388
JM
6354static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
6355{
6356 char *pos;
6357
6358 pos = os_strchr(cmd, ' ');
6359 if (pos == NULL)
6360 return -1;
6361 *pos++ = '\0';
6362
6363 if (os_strcmp(cmd, "bonjour") == 0)
6364 return p2p_ctrl_service_del_bonjour(wpa_s, pos);
6365 if (os_strcmp(cmd, "upnp") == 0)
6366 return p2p_ctrl_service_del_upnp(wpa_s, pos);
ae9d45f3
KV
6367 if (os_strcmp(cmd, "asp") == 0)
6368 return p2p_ctrl_service_del_asp(wpa_s, pos);
6369 wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
6370 return -1;
6371}
6372
6373
6374static int p2p_ctrl_service_replace(struct wpa_supplicant *wpa_s, char *cmd)
6375{
6376 char *pos;
6377
6378 pos = os_strchr(cmd, ' ');
6379 if (pos == NULL)
6380 return -1;
6381 *pos++ = '\0';
6382
6383 if (os_strcmp(cmd, "asp") == 0)
6384 return p2p_ctrl_service_add_asp(wpa_s, 1, pos);
6385
b563b388
JM
6386 wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
6387 return -1;
6388}
6389
6390
6391static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
6392{
6393 u8 addr[ETH_ALEN];
6394
6395 /* <addr> */
6396
6397 if (hwaddr_aton(cmd, addr))
6398 return -1;
6399
6400 return wpas_p2p_reject(wpa_s, addr);
6401}
6402
6403
6404static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
6405{
6406 char *pos;
6407 int id;
6408 struct wpa_ssid *ssid;
54c61e6e 6409 u8 *_peer = NULL, peer[ETH_ALEN];
f5877af0 6410 int freq = 0, pref_freq = 0;
5a3319ab 6411 int ht40, vht, he, max_oper_chwidth, chwidth = 0, freq2 = 0;
99666225 6412 int edmg;
b563b388
JM
6413
6414 id = atoi(cmd);
6415 pos = os_strstr(cmd, " peer=");
6416 if (pos) {
6417 pos += 6;
6418 if (hwaddr_aton(pos, peer))
6419 return -1;
54c61e6e 6420 _peer = peer;
b563b388
JM
6421 }
6422 ssid = wpa_config_get_network(wpa_s->conf, id);
6423 if (ssid == NULL || ssid->disabled != 2) {
6424 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
6425 "for persistent P2P group",
6426 id);
6427 return -1;
6428 }
6429
4d32c0c4
JM
6430 pos = os_strstr(cmd, " freq=");
6431 if (pos) {
6432 pos += 6;
6433 freq = atoi(pos);
6434 if (freq <= 0)
6435 return -1;
6436 }
6437
f5877af0
JM
6438 pos = os_strstr(cmd, " pref=");
6439 if (pos) {
6440 pos += 6;
6441 pref_freq = atoi(pos);
6442 if (pref_freq <= 0)
6443 return -1;
6444 }
6445
20ea1ca4
EP
6446 vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
6447 ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
6448 vht;
5a3319ab 6449 he = (os_strstr(cmd, " he") != NULL) || wpa_s->conf->p2p_go_he;
99666225 6450 edmg = (os_strstr(cmd, " edmg") != NULL) || wpa_s->conf->p2p_go_edmg;
4d32c0c4 6451
c27f4c90
AK
6452 pos = os_strstr(cmd, "freq2=");
6453 if (pos)
6454 freq2 = atoi(pos + 6);
6455
6456 pos = os_strstr(cmd, " max_oper_chwidth=");
6457 if (pos)
6458 chwidth = atoi(pos + 18);
6459
6460 max_oper_chwidth = parse_freq(chwidth, freq2);
6461 if (max_oper_chwidth < 0)
6462 return -1;
6463
6464 return wpas_p2p_invite(wpa_s, _peer, ssid, NULL, freq, freq2, ht40, vht,
99666225 6465 max_oper_chwidth, pref_freq, he, edmg);
b563b388
JM
6466}
6467
6468
6469static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
6470{
6471 char *pos;
6472 u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
6473
6474 pos = os_strstr(cmd, " peer=");
6475 if (!pos)
6476 return -1;
6477
6478 *pos = '\0';
6479 pos += 6;
6480 if (hwaddr_aton(pos, peer)) {
6481 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
6482 return -1;
6483 }
6484
6485 pos = os_strstr(pos, " go_dev_addr=");
6486 if (pos) {
6487 pos += 13;
6488 if (hwaddr_aton(pos, go_dev_addr)) {
6489 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
6490 pos);
6491 return -1;
6492 }
6493 go_dev = go_dev_addr;
6494 }
6495
6496 return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
6497}
6498
6499
6500static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
6501{
6502 if (os_strncmp(cmd, "persistent=", 11) == 0)
6503 return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
6504 if (os_strncmp(cmd, "group=", 6) == 0)
6505 return p2p_ctrl_invite_group(wpa_s, cmd + 6);
6506
6507 return -1;
6508}
6509
6510
6511static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
c27f4c90 6512 int id, int freq, int vht_center_freq2,
5a3319ab 6513 int ht40, int vht, int vht_chwidth,
99666225 6514 int he, int edmg)
b563b388 6515{
b563b388
JM
6516 struct wpa_ssid *ssid;
6517
b563b388
JM
6518 ssid = wpa_config_get_network(wpa_s->conf, id);
6519 if (ssid == NULL || ssid->disabled != 2) {
6520 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
6521 "for persistent P2P group",
6522 id);
6523 return -1;
6524 }
6525
c27f4c90
AK
6526 return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq,
6527 vht_center_freq2, 0, ht40, vht,
99666225
AM
6528 vht_chwidth, he, edmg,
6529 NULL, 0, 0);
b563b388
JM
6530}
6531
6532
6533static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
6534{
29292d53
EP
6535 int freq = 0, persistent = 0, group_id = -1;
6536 int vht = wpa_s->conf->p2p_go_vht;
6537 int ht40 = wpa_s->conf->p2p_go_ht40 || vht;
5a3319ab 6538 int he = wpa_s->conf->p2p_go_he;
99666225 6539 int edmg = wpa_s->conf->p2p_go_edmg;
c27f4c90 6540 int max_oper_chwidth, chwidth = 0, freq2 = 0;
29292d53 6541 char *token, *context = NULL;
37ed3254
SD
6542#ifdef CONFIG_ACS
6543 int acs = 0;
6544#endif /* CONFIG_ACS */
e11776a5 6545
29292d53 6546 while ((token = str_token(cmd, " ", &context))) {
37ed3254 6547 if (sscanf(token, "freq2=%d", &freq2) == 1 ||
c27f4c90
AK
6548 sscanf(token, "persistent=%d", &group_id) == 1 ||
6549 sscanf(token, "max_oper_chwidth=%d", &chwidth) == 1) {
29292d53 6550 continue;
37ed3254
SD
6551#ifdef CONFIG_ACS
6552 } else if (os_strcmp(token, "freq=acs") == 0) {
6553 acs = 1;
6554#endif /* CONFIG_ACS */
6555 } else if (sscanf(token, "freq=%d", &freq) == 1) {
6556 continue;
29292d53
EP
6557 } else if (os_strcmp(token, "ht40") == 0) {
6558 ht40 = 1;
6559 } else if (os_strcmp(token, "vht") == 0) {
6560 vht = 1;
6561 ht40 = 1;
5a3319ab
PX
6562 } else if (os_strcmp(token, "he") == 0) {
6563 he = 1;
99666225
AM
6564 } else if (os_strcmp(token, "edmg") == 0) {
6565 edmg = 1;
29292d53
EP
6566 } else if (os_strcmp(token, "persistent") == 0) {
6567 persistent = 1;
6568 } else {
6569 wpa_printf(MSG_DEBUG,
6570 "CTRL: Invalid P2P_GROUP_ADD parameter: '%s'",
6571 token);
6572 return -1;
6573 }
e11776a5
PK
6574 }
6575
37ed3254
SD
6576#ifdef CONFIG_ACS
6577 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_ACS_OFFLOAD) &&
6578 (acs || freq == 2 || freq == 5)) {
6579 if (freq == 2 && wpa_s->best_24_freq <= 0) {
6580 wpa_s->p2p_go_acs_band = HOSTAPD_MODE_IEEE80211G;
6581 wpa_s->p2p_go_do_acs = 1;
6582 freq = 0;
6583 } else if (freq == 5 && wpa_s->best_5_freq <= 0) {
6584 wpa_s->p2p_go_acs_band = HOSTAPD_MODE_IEEE80211A;
6585 wpa_s->p2p_go_do_acs = 1;
6586 freq = 0;
6587 } else {
6588 wpa_s->p2p_go_acs_band = HOSTAPD_MODE_IEEE80211ANY;
6589 wpa_s->p2p_go_do_acs = 1;
6590 }
2b7fa035
JS
6591 } else {
6592 wpa_s->p2p_go_do_acs = 0;
37ed3254
SD
6593 }
6594#endif /* CONFIG_ACS */
6595
c27f4c90
AK
6596 max_oper_chwidth = parse_freq(chwidth, freq2);
6597 if (max_oper_chwidth < 0)
6598 return -1;
6599
29292d53
EP
6600 if (group_id >= 0)
6601 return p2p_ctrl_group_add_persistent(wpa_s, group_id,
c27f4c90 6602 freq, freq2, ht40, vht,
99666225
AM
6603 max_oper_chwidth, he,
6604 edmg);
29292d53 6605
c27f4c90 6606 return wpas_p2p_group_add(wpa_s, persistent, freq, freq2, ht40, vht,
99666225 6607 max_oper_chwidth, he, edmg);
b563b388
JM
6608}
6609
6610
57b38882
PK
6611static int p2p_ctrl_group_member(struct wpa_supplicant *wpa_s, const char *cmd,
6612 char *buf, size_t buflen)
6613{
6614 u8 dev_addr[ETH_ALEN];
6615 struct wpa_ssid *ssid;
6616 int res;
6617 const u8 *iaddr;
6618
6619 ssid = wpa_s->current_ssid;
6620 if (!wpa_s->global->p2p || !ssid || ssid->mode != WPAS_MODE_P2P_GO ||
6621 hwaddr_aton(cmd, dev_addr))
6622 return -1;
6623
6624 iaddr = p2p_group_get_client_interface_addr(wpa_s->p2p_group, dev_addr);
6625 if (!iaddr)
6626 return -1;
6627 res = os_snprintf(buf, buflen, MACSTR, MAC2STR(iaddr));
6628 if (os_snprintf_error(buflen, res))
6629 return -1;
6630 return res;
6631}
6632
6633
f47f9361
SD
6634static int wpas_find_p2p_dev_addr_bss(struct wpa_global *global,
6635 const u8 *p2p_dev_addr)
6636{
6637 struct wpa_supplicant *wpa_s;
6638
6639 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6640 if (wpa_bss_get_p2p_dev_addr(wpa_s, p2p_dev_addr))
6641 return 1;
6642 }
6643
6644 return 0;
6645}
6646
6647
b563b388
JM
6648static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
6649 char *buf, size_t buflen)
6650{
f47f9361 6651 u8 addr[ETH_ALEN], *addr_ptr, group_capab;
b3ffc80b
JM
6652 int next, res;
6653 const struct p2p_peer_info *info;
6654 char *pos, *end;
6655 char devtype[WPS_DEV_TYPE_BUFSIZE];
87f841a1 6656 struct wpa_ssid *ssid;
f3989ced 6657 size_t i;
b563b388
JM
6658
6659 if (!wpa_s->global->p2p)
6660 return -1;
6661
6662 if (os_strcmp(cmd, "FIRST") == 0) {
6663 addr_ptr = NULL;
6664 next = 0;
6665 } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
6666 if (hwaddr_aton(cmd + 5, addr) < 0)
6667 return -1;
6668 addr_ptr = addr;
6669 next = 1;
6670 } else {
6671 if (hwaddr_aton(cmd, addr) < 0)
6672 return -1;
6673 addr_ptr = addr;
6674 next = 0;
6675 }
6676
b3ffc80b
JM
6677 info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
6678 if (info == NULL)
6679 return -1;
f47f9361
SD
6680 group_capab = info->group_capab;
6681
6682 if (group_capab &&
6683 !wpas_find_p2p_dev_addr_bss(wpa_s->global, info->p2p_device_addr)) {
6684 wpa_printf(MSG_DEBUG,
6685 "P2P: Could not find any BSS with p2p_dev_addr "
6686 MACSTR ", hence override group_capab from 0x%x to 0",
6687 MAC2STR(info->p2p_device_addr), group_capab);
6688 group_capab = 0;
6689 }
b3ffc80b
JM
6690
6691 pos = buf;
6692 end = buf + buflen;
6693
6694 res = os_snprintf(pos, end - pos, MACSTR "\n"
6695 "pri_dev_type=%s\n"
6696 "device_name=%s\n"
6697 "manufacturer=%s\n"
6698 "model_name=%s\n"
6699 "model_number=%s\n"
6700 "serial_number=%s\n"
6701 "config_methods=0x%x\n"
6702 "dev_capab=0x%x\n"
6703 "group_capab=0x%x\n"
6704 "level=%d\n",
6705 MAC2STR(info->p2p_device_addr),
6706 wps_dev_type_bin2str(info->pri_dev_type,
6707 devtype, sizeof(devtype)),
6708 info->device_name,
6709 info->manufacturer,
6710 info->model_name,
6711 info->model_number,
6712 info->serial_number,
6713 info->config_methods,
6714 info->dev_capab,
f47f9361 6715 group_capab,
b3ffc80b 6716 info->level);
d85e1fc8 6717 if (os_snprintf_error(end - pos, res))
b3ffc80b
JM
6718 return pos - buf;
6719 pos += res;
6720
f3989ced
JM
6721 for (i = 0; i < info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN; i++)
6722 {
6723 const u8 *t;
6724 t = &info->wps_sec_dev_type_list[i * WPS_DEV_TYPE_LEN];
6725 res = os_snprintf(pos, end - pos, "sec_dev_type=%s\n",
6726 wps_dev_type_bin2str(t, devtype,
6727 sizeof(devtype)));
d85e1fc8 6728 if (os_snprintf_error(end - pos, res))
f3989ced
JM
6729 return pos - buf;
6730 pos += res;
6731 }
6732
c427ac92 6733 ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
87f841a1
JM
6734 if (ssid) {
6735 res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
d85e1fc8 6736 if (os_snprintf_error(end - pos, res))
87f841a1
JM
6737 return pos - buf;
6738 pos += res;
6739 }
6740
b3ffc80b
JM
6741 res = p2p_get_peer_info_txt(info, pos, end - pos);
6742 if (res < 0)
87f841a1 6743 return pos - buf;
b3ffc80b
JM
6744 pos += res;
6745
71a0e395
JM
6746 if (info->vendor_elems) {
6747 res = os_snprintf(pos, end - pos, "vendor_elems=");
d85e1fc8 6748 if (os_snprintf_error(end - pos, res))
71a0e395
JM
6749 return pos - buf;
6750 pos += res;
6751
6752 pos += wpa_snprintf_hex(pos, end - pos,
6753 wpabuf_head(info->vendor_elems),
6754 wpabuf_len(info->vendor_elems));
6755
6756 res = os_snprintf(pos, end - pos, "\n");
d85e1fc8 6757 if (os_snprintf_error(end - pos, res))
71a0e395
JM
6758 return pos - buf;
6759 pos += res;
6760 }
6761
b3ffc80b 6762 return pos - buf;
b563b388
JM
6763}
6764
6765
6f3bc72b
JM
6766static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
6767 const char *param)
6768{
af8a827b 6769 unsigned int i;
6f3bc72b
JM
6770
6771 if (wpa_s->global->p2p == NULL)
6772 return -1;
6773
af8a827b
JM
6774 if (freq_range_list_parse(&wpa_s->global->p2p_disallow_freq, param) < 0)
6775 return -1;
6f3bc72b 6776
af8a827b
JM
6777 for (i = 0; i < wpa_s->global->p2p_disallow_freq.num; i++) {
6778 struct wpa_freq_range *freq;
6779 freq = &wpa_s->global->p2p_disallow_freq.range[i];
6f3bc72b 6780 wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
af8a827b 6781 freq->min, freq->max);
6f3bc72b
JM
6782 }
6783
3a8f008a 6784 wpas_p2p_update_channel_list(wpa_s, WPAS_P2P_CHANNEL_UPDATE_DISALLOW);
6f3bc72b
JM
6785 return 0;
6786}
6787
6788
b563b388
JM
6789static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
6790{
6791 char *param;
6792
6793 if (wpa_s->global->p2p == NULL)
6794 return -1;
6795
6796 param = os_strchr(cmd, ' ');
6797 if (param == NULL)
6798 return -1;
6799 *param++ = '\0';
6800
6801 if (os_strcmp(cmd, "discoverability") == 0) {
6802 p2p_set_client_discoverability(wpa_s->global->p2p,
6803 atoi(param));
6804 return 0;
6805 }
6806
6807 if (os_strcmp(cmd, "managed") == 0) {
6808 p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
6809 return 0;
6810 }
6811
6812 if (os_strcmp(cmd, "listen_channel") == 0) {
dfe0745c
LD
6813 char *pos;
6814 u8 channel, op_class;
6815
6816 channel = atoi(param);
6817 pos = os_strchr(param, ' ');
6818 op_class = pos ? atoi(pos) : 81;
6819
6820 return p2p_set_listen_channel(wpa_s->global->p2p, op_class,
6821 channel, 1);
b563b388
JM
6822 }
6823
6824 if (os_strcmp(cmd, "ssid_postfix") == 0) {
6825 return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
6826 os_strlen(param));
6827 }
6828
6829 if (os_strcmp(cmd, "noa") == 0) {
6830 char *pos;
6831 int count, start, duration;
6832 /* GO NoA parameters: count,start_offset(ms),duration(ms) */
6833 count = atoi(param);
6834 pos = os_strchr(param, ',');
6835 if (pos == NULL)
6836 return -1;
6837 pos++;
6838 start = atoi(pos);
6839 pos = os_strchr(pos, ',');
6840 if (pos == NULL)
6841 return -1;
6842 pos++;
6843 duration = atoi(pos);
6844 if (count < 0 || count > 255 || start < 0 || duration < 0)
6845 return -1;
6846 if (count == 0 && duration > 0)
6847 return -1;
6848 wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
6849 "start=%d duration=%d", count, start, duration);
aefb53bd 6850 return wpas_p2p_set_noa(wpa_s, count, start, duration);
b563b388
JM
6851 }
6852
c381508d
JM
6853 if (os_strcmp(cmd, "ps") == 0)
6854 return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
6855
6856 if (os_strcmp(cmd, "oppps") == 0)
6857 return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
6858
6859 if (os_strcmp(cmd, "ctwindow") == 0)
6860 return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
6861
b563b388
JM
6862 if (os_strcmp(cmd, "disabled") == 0) {
6863 wpa_s->global->p2p_disabled = atoi(param);
6864 wpa_printf(MSG_DEBUG, "P2P functionality %s",
6865 wpa_s->global->p2p_disabled ?
6866 "disabled" : "enabled");
6867 if (wpa_s->global->p2p_disabled) {
6868 wpas_p2p_stop_find(wpa_s);
108def93 6869 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
b563b388
JM
6870 p2p_flush(wpa_s->global->p2p);
6871 }
6872 return 0;
6873 }
6874
b9cfc09a
JJ
6875 if (os_strcmp(cmd, "conc_pref") == 0) {
6876 if (os_strcmp(param, "sta") == 0)
6877 wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
6878 else if (os_strcmp(param, "p2p") == 0)
6879 wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
6880 else {
6881 wpa_printf(MSG_INFO, "Invalid conc_pref value");
6882 return -1;
6883 }
6884 wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
6885 "%s", param);
6886 return 0;
6887 }
6888
6e6963ea
JM
6889 if (os_strcmp(cmd, "force_long_sd") == 0) {
6890 wpa_s->force_long_sd = atoi(param);
6891 return 0;
6892 }
6893
80c9582a
JM
6894 if (os_strcmp(cmd, "peer_filter") == 0) {
6895 u8 addr[ETH_ALEN];
6896 if (hwaddr_aton(param, addr))
6897 return -1;
6898 p2p_set_peer_filter(wpa_s->global->p2p, addr);
6899 return 0;
6900 }
6901
72044390
JM
6902 if (os_strcmp(cmd, "cross_connect") == 0)
6903 return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
6904
eea2fd9e
JM
6905 if (os_strcmp(cmd, "go_apsd") == 0) {
6906 if (os_strcmp(param, "disable") == 0)
6907 wpa_s->set_ap_uapsd = 0;
6908 else {
6909 wpa_s->set_ap_uapsd = 1;
6910 wpa_s->ap_uapsd = atoi(param);
6911 }
6912 return 0;
6913 }
6914
6915 if (os_strcmp(cmd, "client_apsd") == 0) {
6916 if (os_strcmp(param, "disable") == 0)
6917 wpa_s->set_sta_uapsd = 0;
6918 else {
6919 int be, bk, vi, vo;
6920 char *pos;
6921 /* format: BE,BK,VI,VO;max SP Length */
6922 be = atoi(param);
6923 pos = os_strchr(param, ',');
6924 if (pos == NULL)
6925 return -1;
6926 pos++;
6927 bk = atoi(pos);
6928 pos = os_strchr(pos, ',');
6929 if (pos == NULL)
6930 return -1;
6931 pos++;
6932 vi = atoi(pos);
6933 pos = os_strchr(pos, ',');
6934 if (pos == NULL)
6935 return -1;
6936 pos++;
6937 vo = atoi(pos);
6938 /* ignore max SP Length for now */
6939
6940 wpa_s->set_sta_uapsd = 1;
6941 wpa_s->sta_uapsd = 0;
6942 if (be)
6943 wpa_s->sta_uapsd |= BIT(0);
6944 if (bk)
6945 wpa_s->sta_uapsd |= BIT(1);
6946 if (vi)
6947 wpa_s->sta_uapsd |= BIT(2);
6948 if (vo)
6949 wpa_s->sta_uapsd |= BIT(3);
6950 }
6951 return 0;
6952 }
6953
6f3bc72b
JM
6954 if (os_strcmp(cmd, "disallow_freq") == 0)
6955 return p2p_ctrl_disallow_freq(wpa_s, param);
6956
96beff11
JM
6957 if (os_strcmp(cmd, "disc_int") == 0) {
6958 int min_disc_int, max_disc_int, max_disc_tu;
6959 char *pos;
6960
6961 pos = param;
6962
6963 min_disc_int = atoi(pos);
6964 pos = os_strchr(pos, ' ');
6965 if (pos == NULL)
6966 return -1;
6967 *pos++ = '\0';
6968
6969 max_disc_int = atoi(pos);
6970 pos = os_strchr(pos, ' ');
6971 if (pos == NULL)
6972 return -1;
6973 *pos++ = '\0';
6974
6975 max_disc_tu = atoi(pos);
6976
6977 return p2p_set_disc_int(wpa_s->global->p2p, min_disc_int,
6978 max_disc_int, max_disc_tu);
6979 }
6980
05766ed8
JM
6981 if (os_strcmp(cmd, "per_sta_psk") == 0) {
6982 wpa_s->global->p2p_per_sta_psk = !!atoi(param);
6983 return 0;
6984 }
6985
c4f87a70
JM
6986#ifdef CONFIG_WPS_NFC
6987 if (os_strcmp(cmd, "nfc_tag") == 0)
6988 return wpas_p2p_nfc_tag_enabled(wpa_s, !!atoi(param));
6989#endif /* CONFIG_WPS_NFC */
6990
201b0f5f
JM
6991 if (os_strcmp(cmd, "disable_ip_addr_req") == 0) {
6992 wpa_s->p2p_disable_ip_addr_req = !!atoi(param);
6993 return 0;
6994 }
6995
3a7819f0
JM
6996 if (os_strcmp(cmd, "override_pref_op_chan") == 0) {
6997 int op_class, chan;
6998
6999 op_class = atoi(param);
7000 param = os_strchr(param, ':');
7001 if (!param)
7002 return -1;
7003 param++;
7004 chan = atoi(param);
7005 p2p_set_override_pref_op_chan(wpa_s->global->p2p, op_class,
7006 chan);
7007 return 0;
7008 }
7009
b563b388
JM
7010 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
7011 cmd);
7012
7013 return -1;
7014}
7015
7016
acb54643
JM
7017static void p2p_ctrl_flush(struct wpa_supplicant *wpa_s)
7018{
7019 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
7020 wpa_s->force_long_sd = 0;
d02e4c8a
AO
7021
7022#ifdef CONFIG_TESTING_OPTIONS
7023 os_free(wpa_s->get_pref_freq_list_override);
7024 wpa_s->get_pref_freq_list_override = NULL;
7025#endif /* CONFIG_TESTING_OPTIONS */
7026
477b082c 7027 wpas_p2p_stop_find(wpa_s);
b9da88d6 7028 wpa_s->parent->p2ps_method_config_any = 0;
acb54643
JM
7029 if (wpa_s->global->p2p)
7030 p2p_flush(wpa_s->global->p2p);
7031}
7032
7033
b563b388
JM
7034static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
7035{
7036 char *pos, *pos2;
7037 unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
7038
7039 if (cmd[0]) {
7040 pos = os_strchr(cmd, ' ');
7041 if (pos == NULL)
7042 return -1;
7043 *pos++ = '\0';
7044 dur1 = atoi(cmd);
7045
7046 pos2 = os_strchr(pos, ' ');
7047 if (pos2)
7048 *pos2++ = '\0';
7049 int1 = atoi(pos);
7050 } else
7051 pos2 = NULL;
7052
7053 if (pos2) {
7054 pos = os_strchr(pos2, ' ');
7055 if (pos == NULL)
7056 return -1;
7057 *pos++ = '\0';
7058 dur2 = atoi(pos2);
7059 int2 = atoi(pos);
7060 }
7061
7062 return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
7063}
7064
7065
7066static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
7067{
7068 char *pos;
7069 unsigned int period = 0, interval = 0;
7070
7071 if (cmd[0]) {
7072 pos = os_strchr(cmd, ' ');
7073 if (pos == NULL)
7074 return -1;
7075 *pos++ = '\0';
7076 period = atoi(cmd);
7077 interval = atoi(pos);
7078 }
7079
7080 return wpas_p2p_ext_listen(wpa_s, period, interval);
7081}
7082
f2c56602
JM
7083
7084static int p2p_ctrl_remove_client(struct wpa_supplicant *wpa_s, const char *cmd)
7085{
7086 const char *pos;
7087 u8 peer[ETH_ALEN];
7088 int iface_addr = 0;
7089
7090 pos = cmd;
7091 if (os_strncmp(pos, "iface=", 6) == 0) {
7092 iface_addr = 1;
7093 pos += 6;
7094 }
7095 if (hwaddr_aton(pos, peer))
7096 return -1;
7097
7098 wpas_p2p_remove_client(wpa_s, peer, iface_addr);
7099 return 0;
7100}
7101
a6f5b193
PX
7102
7103static int p2p_ctrl_iface_p2p_lo_start(struct wpa_supplicant *wpa_s, char *cmd)
7104{
7105 int freq = 0, period = 0, interval = 0, count = 0;
7106
7107 if (sscanf(cmd, "%d %d %d %d", &freq, &period, &interval, &count) != 4)
7108 {
7109 wpa_printf(MSG_DEBUG,
7110 "CTRL: Invalid P2P LO Start parameter: '%s'", cmd);
7111 return -1;
7112 }
7113
7114 return wpas_p2p_lo_start(wpa_s, freq, period, interval, count);
7115}
7116
b563b388
JM
7117#endif /* CONFIG_P2P */
7118
7119
356d1488
JM
7120static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s, char *val)
7121{
7122 struct wpa_freq_range_list ranges;
7123 int *freqs = NULL;
7124 struct hostapd_hw_modes *mode;
7125 u16 i;
7126
7127 if (wpa_s->hw.modes == NULL)
7128 return NULL;
7129
7130 os_memset(&ranges, 0, sizeof(ranges));
7131 if (freq_range_list_parse(&ranges, val) < 0)
7132 return NULL;
7133
7134 for (i = 0; i < wpa_s->hw.num_modes; i++) {
7135 int j;
7136
7137 mode = &wpa_s->hw.modes[i];
7138 for (j = 0; j < mode->num_channels; j++) {
7139 unsigned int freq;
7140
7141 if (mode->channels[j].flag & HOSTAPD_CHAN_DISABLED)
7142 continue;
7143
7144 freq = mode->channels[j].freq;
7145 if (!freq_range_list_includes(&ranges, freq))
7146 continue;
7147
7148 int_array_add_unique(&freqs, freq);
7149 }
7150 }
7151
7152 os_free(ranges.range);
7153 return freqs;
7154}
7155
7156
afc064fe 7157#ifdef CONFIG_INTERWORKING
356d1488
JM
7158
7159static int ctrl_interworking_select(struct wpa_supplicant *wpa_s, char *param)
7160{
7161 int auto_sel = 0;
7162 int *freqs = NULL;
7163
7164 if (param) {
7165 char *pos;
7166
7167 auto_sel = os_strstr(param, "auto") != NULL;
7168
7169 pos = os_strstr(param, "freq=");
7170 if (pos) {
7171 freqs = freq_range_to_channel_list(wpa_s, pos + 5);
7172 if (freqs == NULL)
7173 return -1;
7174 }
7175
7176 }
7177
7178 return interworking_select(wpa_s, auto_sel, freqs);
7179}
7180
7181
f91a512f
JM
7182static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst,
7183 int only_add)
b02fe7ff
JM
7184{
7185 u8 bssid[ETH_ALEN];
7186 struct wpa_bss *bss;
7187
7188 if (hwaddr_aton(dst, bssid)) {
7189 wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
7190 return -1;
7191 }
7192
7193 bss = wpa_bss_get_bssid(wpa_s, bssid);
7194 if (bss == NULL) {
7195 wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
7196 MAC2STR(bssid));
7197 return -1;
7198 }
7199
783b2a97
JM
7200 if (bss->ssid_len == 0) {
7201 int found = 0;
7202
7203 wpa_printf(MSG_DEBUG, "Selected BSS entry for " MACSTR
7204 " does not have SSID information", MAC2STR(bssid));
7205
7206 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss,
7207 list) {
7208 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0 &&
7209 bss->ssid_len > 0) {
7210 found = 1;
7211 break;
7212 }
7213 }
7214
7215 if (!found)
7216 return -1;
7217 wpa_printf(MSG_DEBUG,
7218 "Found another matching BSS entry with SSID");
7219 }
7220
f91a512f 7221 return interworking_connect(wpa_s, bss, only_add);
b02fe7ff
JM
7222}
7223
7224
afc064fe
JM
7225static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
7226{
7227 u8 dst_addr[ETH_ALEN];
7228 int used;
7229 char *pos;
7230#define MAX_ANQP_INFO_ID 100
7231 u16 id[MAX_ANQP_INFO_ID];
7232 size_t num_id = 0;
cf28c66b 7233 u32 subtypes = 0;
2316cb35 7234 u32 mbo_subtypes = 0;
afc064fe
JM
7235
7236 used = hwaddr_aton2(dst, dst_addr);
7237 if (used < 0)
7238 return -1;
7239 pos = dst + used;
b68d602d
JM
7240 if (*pos == ' ')
7241 pos++;
afc064fe 7242 while (num_id < MAX_ANQP_INFO_ID) {
cf28c66b
DS
7243 if (os_strncmp(pos, "hs20:", 5) == 0) {
7244#ifdef CONFIG_HS20
7245 int num = atoi(pos + 5);
7246 if (num <= 0 || num > 31)
7247 return -1;
7248 subtypes |= BIT(num);
7249#else /* CONFIG_HS20 */
7250 return -1;
7251#endif /* CONFIG_HS20 */
8f479174 7252 } else if (os_strncmp(pos, "mbo:", 4) == 0) {
7253#ifdef CONFIG_MBO
7254 int num = atoi(pos + 4);
2316cb35
AS
7255
7256 if (num <= 0 || num > MAX_MBO_ANQP_SUBTYPE)
8f479174 7257 return -1;
2316cb35 7258 mbo_subtypes |= BIT(num);
8f479174 7259#else /* CONFIG_MBO */
7260 return -1;
7261#endif /* CONFIG_MBO */
cf28c66b
DS
7262 } else {
7263 id[num_id] = atoi(pos);
7264 if (id[num_id])
7265 num_id++;
7266 }
afc064fe
JM
7267 pos = os_strchr(pos + 1, ',');
7268 if (pos == NULL)
7269 break;
7270 pos++;
7271 }
7272
8ecf2231 7273 if (num_id == 0 && !subtypes && !mbo_subtypes)
afc064fe
JM
7274 return -1;
7275
8f479174 7276 return anqp_send_req(wpa_s, dst_addr, id, num_id, subtypes,
2316cb35 7277 mbo_subtypes);
afc064fe 7278}
b1f12296
JM
7279
7280
7281static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
7282{
7283 u8 dst_addr[ETH_ALEN];
7284 struct wpabuf *advproto, *query = NULL;
7285 int used, ret = -1;
7286 char *pos, *end;
7287 size_t len;
7288
7289 used = hwaddr_aton2(cmd, dst_addr);
7290 if (used < 0)
7291 return -1;
7292
7293 pos = cmd + used;
7294 while (*pos == ' ')
7295 pos++;
7296
7297 /* Advertisement Protocol ID */
7298 end = os_strchr(pos, ' ');
7299 if (end)
7300 len = end - pos;
7301 else
7302 len = os_strlen(pos);
7303 if (len & 0x01)
7304 return -1;
7305 len /= 2;
7306 if (len == 0)
7307 return -1;
7308 advproto = wpabuf_alloc(len);
7309 if (advproto == NULL)
7310 return -1;
7311 if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
7312 goto fail;
7313
7314 if (end) {
7315 /* Optional Query Request */
7316 pos = end + 1;
7317 while (*pos == ' ')
7318 pos++;
7319
7320 len = os_strlen(pos);
7321 if (len) {
7322 if (len & 0x01)
7323 goto fail;
7324 len /= 2;
7325 if (len == 0)
7326 goto fail;
7327 query = wpabuf_alloc(len);
7328 if (query == NULL)
7329 goto fail;
7330 if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
7331 goto fail;
7332 }
7333 }
7334
7335 ret = gas_send_request(wpa_s, dst_addr, advproto, query);
7336
7337fail:
7338 wpabuf_free(advproto);
7339 wpabuf_free(query);
7340
7341 return ret;
7342}
7343
7344
7345static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
7346 size_t buflen)
7347{
7348 u8 addr[ETH_ALEN];
7349 int dialog_token;
7350 int used;
7351 char *pos;
7352 size_t resp_len, start, requested_len;
b6a9590b
JM
7353 struct wpabuf *resp;
7354 int ret;
b1f12296
JM
7355
7356 used = hwaddr_aton2(cmd, addr);
7357 if (used < 0)
7358 return -1;
7359
7360 pos = cmd + used;
7361 while (*pos == ' ')
7362 pos++;
7363 dialog_token = atoi(pos);
7364
b6a9590b
JM
7365 if (wpa_s->last_gas_resp &&
7366 os_memcmp(addr, wpa_s->last_gas_addr, ETH_ALEN) == 0 &&
7367 dialog_token == wpa_s->last_gas_dialog_token)
7368 resp = wpa_s->last_gas_resp;
7369 else if (wpa_s->prev_gas_resp &&
7370 os_memcmp(addr, wpa_s->prev_gas_addr, ETH_ALEN) == 0 &&
7371 dialog_token == wpa_s->prev_gas_dialog_token)
7372 resp = wpa_s->prev_gas_resp;
7373 else
b1f12296
JM
7374 return -1;
7375
b6a9590b 7376 resp_len = wpabuf_len(resp);
b1f12296
JM
7377 start = 0;
7378 requested_len = resp_len;
7379
7380 pos = os_strchr(pos, ' ');
7381 if (pos) {
7382 start = atoi(pos);
7383 if (start > resp_len)
7384 return os_snprintf(buf, buflen, "FAIL-Invalid range");
7385 pos = os_strchr(pos, ',');
7386 if (pos == NULL)
7387 return -1;
7388 pos++;
7389 requested_len = atoi(pos);
7390 if (start + requested_len > resp_len)
7391 return os_snprintf(buf, buflen, "FAIL-Invalid range");
7392 }
7393
7394 if (requested_len * 2 + 1 > buflen)
7395 return os_snprintf(buf, buflen, "FAIL-Too long response");
7396
b6a9590b
JM
7397 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(resp) + start,
7398 requested_len);
7399
7400 if (start + requested_len == resp_len) {
7401 /*
7402 * Free memory by dropping the response after it has been
7403 * fetched.
7404 */
7405 if (resp == wpa_s->prev_gas_resp) {
7406 wpabuf_free(wpa_s->prev_gas_resp);
7407 wpa_s->prev_gas_resp = NULL;
7408 } else {
7409 wpabuf_free(wpa_s->last_gas_resp);
7410 wpa_s->last_gas_resp = NULL;
7411 }
7412 }
7413
7414 return ret;
b1f12296 7415}
afc064fe
JM
7416#endif /* CONFIG_INTERWORKING */
7417
7418
a8918e86
JK
7419#ifdef CONFIG_HS20
7420
7421static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
7422{
7423 u8 dst_addr[ETH_ALEN];
7424 int used;
7425 char *pos;
7426 u32 subtypes = 0;
7427
7428 used = hwaddr_aton2(dst, dst_addr);
7429 if (used < 0)
7430 return -1;
7431 pos = dst + used;
b68d602d
JM
7432 if (*pos == ' ')
7433 pos++;
a8918e86
JK
7434 for (;;) {
7435 int num = atoi(pos);
7436 if (num <= 0 || num > 31)
7437 return -1;
7438 subtypes |= BIT(num);
7439 pos = os_strchr(pos + 1, ',');
7440 if (pos == NULL)
7441 break;
7442 pos++;
7443 }
7444
7445 if (subtypes == 0)
7446 return -1;
7447
8dd5c1b4 7448 return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0, 0);
a8918e86
JK
7449}
7450
7451
7452static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
7453 const u8 *addr, const char *realm)
7454{
7455 u8 *buf;
7456 size_t rlen, len;
7457 int ret;
7458
7459 rlen = os_strlen(realm);
7460 len = 3 + rlen;
7461 buf = os_malloc(len);
7462 if (buf == NULL)
7463 return -1;
7464 buf[0] = 1; /* NAI Home Realm Count */
7465 buf[1] = 0; /* Formatted in accordance with RFC 4282 */
7466 buf[2] = rlen;
7467 os_memcpy(buf + 3, realm, rlen);
7468
7469 ret = hs20_anqp_send_req(wpa_s, addr,
7470 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
8dd5c1b4 7471 buf, len, 0);
a8918e86
JK
7472
7473 os_free(buf);
7474
7475 return ret;
7476}
7477
7478
7479static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
7480 char *dst)
7481{
7482 struct wpa_cred *cred = wpa_s->conf->cred;
7483 u8 dst_addr[ETH_ALEN];
7484 int used;
7485 u8 *buf;
7486 size_t len;
7487 int ret;
7488
7489 used = hwaddr_aton2(dst, dst_addr);
7490 if (used < 0)
7491 return -1;
7492
7493 while (dst[used] == ' ')
7494 used++;
7495 if (os_strncmp(dst + used, "realm=", 6) == 0)
7496 return hs20_nai_home_realm_list(wpa_s, dst_addr,
7497 dst + used + 6);
7498
7499 len = os_strlen(dst + used);
7500
7501 if (len == 0 && cred && cred->realm)
7502 return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
7503
0e87e798 7504 if (len & 1)
a8918e86
JK
7505 return -1;
7506 len /= 2;
7507 buf = os_malloc(len);
7508 if (buf == NULL)
7509 return -1;
7510 if (hexstr2bin(dst + used, buf, len) < 0) {
7511 os_free(buf);
7512 return -1;
7513 }
7514
7515 ret = hs20_anqp_send_req(wpa_s, dst_addr,
7516 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
8dd5c1b4 7517 buf, len, 0);
a8918e86
JK
7518 os_free(buf);
7519
7520 return ret;
7521}
7522
184e110c 7523
8dd5c1b4
JN
7524static int get_hs20_icon(struct wpa_supplicant *wpa_s, char *cmd, char *reply,
7525 int buflen)
7526{
7527 u8 dst_addr[ETH_ALEN];
7528 int used;
7529 char *ctx = NULL, *icon, *poffset, *psize;
7530
7531 used = hwaddr_aton2(cmd, dst_addr);
7532 if (used < 0)
7533 return -1;
7534 cmd += used;
7535
7536 icon = str_token(cmd, " ", &ctx);
7537 poffset = str_token(cmd, " ", &ctx);
7538 psize = str_token(cmd, " ", &ctx);
7539 if (!icon || !poffset || !psize)
7540 return -1;
7541
7542 wpa_s->fetch_osu_icon_in_progress = 0;
7543 return hs20_get_icon(wpa_s, dst_addr, icon, atoi(poffset), atoi(psize),
7544 reply, buflen);
7545}
7546
7547
7548static int del_hs20_icon(struct wpa_supplicant *wpa_s, char *cmd)
7549{
7550 u8 dst_addr[ETH_ALEN];
7551 int used;
7552 char *icon;
7553
7554 if (!cmd[0])
7555 return hs20_del_icon(wpa_s, NULL, NULL);
7556
7557 used = hwaddr_aton2(cmd, dst_addr);
7558 if (used < 0)
7559 return -1;
7560
7561 while (cmd[used] == ' ')
7562 used++;
7563 icon = cmd[used] ? &cmd[used] : NULL;
7564
7565 return hs20_del_icon(wpa_s, dst_addr, icon);
7566}
7567
7568
7569static int hs20_icon_request(struct wpa_supplicant *wpa_s, char *cmd, int inmem)
184e110c
JM
7570{
7571 u8 dst_addr[ETH_ALEN];
7572 int used;
7573 char *icon;
7574
7575 used = hwaddr_aton2(cmd, dst_addr);
7576 if (used < 0)
7577 return -1;
7578
7579 while (cmd[used] == ' ')
7580 used++;
7581 icon = &cmd[used];
7582
b572df86 7583 wpa_s->fetch_osu_icon_in_progress = 0;
184e110c 7584 return hs20_anqp_send_req(wpa_s, dst_addr, BIT(HS20_STYPE_ICON_REQUEST),
8dd5c1b4 7585 (u8 *) icon, os_strlen(icon), inmem);
184e110c
JM
7586}
7587
a8918e86
JK
7588#endif /* CONFIG_HS20 */
7589
7590
bc5d330a
TB
7591#ifdef CONFIG_AUTOSCAN
7592
7593static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
7594 char *cmd)
7595{
7596 enum wpa_states state = wpa_s->wpa_state;
7597 char *new_params = NULL;
7598
7599 if (os_strlen(cmd) > 0) {
7600 new_params = os_strdup(cmd);
7601 if (new_params == NULL)
7602 return -1;
7603 }
7604
7605 os_free(wpa_s->conf->autoscan);
7606 wpa_s->conf->autoscan = new_params;
7607
7608 if (wpa_s->conf->autoscan == NULL)
7609 autoscan_deinit(wpa_s);
7610 else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
99218999 7611 autoscan_init(wpa_s, 1);
99f00324
JM
7612 else if (state == WPA_SCANNING)
7613 wpa_supplicant_reinit_autoscan(wpa_s);
9d3f4a74
JM
7614 else
7615 wpa_printf(MSG_DEBUG, "No autoscan update in state %s",
7616 wpa_supplicant_state_txt(state));
bc5d330a
TB
7617
7618 return 0;
7619}
7620
7621#endif /* CONFIG_AUTOSCAN */
7622
7623
e9199e31
JM
7624#ifdef CONFIG_WNM
7625
7626static int wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant *wpa_s, char *cmd)
7627{
7628 int enter;
7629 int intval = 0;
7630 char *pos;
cd0ef657
JM
7631 int ret;
7632 struct wpabuf *tfs_req = NULL;
e9199e31
JM
7633
7634 if (os_strncmp(cmd, "enter", 5) == 0)
7635 enter = 1;
7636 else if (os_strncmp(cmd, "exit", 4) == 0)
7637 enter = 0;
7638 else
7639 return -1;
7640
7641 pos = os_strstr(cmd, " interval=");
7642 if (pos)
7643 intval = atoi(pos + 10);
7644
cd0ef657
JM
7645 pos = os_strstr(cmd, " tfs_req=");
7646 if (pos) {
7647 char *end;
7648 size_t len;
7649 pos += 9;
7650 end = os_strchr(pos, ' ');
7651 if (end)
7652 len = end - pos;
7653 else
7654 len = os_strlen(pos);
7655 if (len & 1)
7656 return -1;
7657 len /= 2;
7658 tfs_req = wpabuf_alloc(len);
7659 if (tfs_req == NULL)
7660 return -1;
7661 if (hexstr2bin(pos, wpabuf_put(tfs_req, len), len) < 0) {
7662 wpabuf_free(tfs_req);
7663 return -1;
7664 }
7665 }
7666
df80a0cc
JM
7667 ret = ieee802_11_send_wnmsleep_req(wpa_s, enter ? WNM_SLEEP_MODE_ENTER :
7668 WNM_SLEEP_MODE_EXIT, intval,
cd0ef657
JM
7669 tfs_req);
7670 wpabuf_free(tfs_req);
7671
7672 return ret;
e9199e31
JM
7673}
7674
65bcd0a9
VK
7675
7676static int wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant *wpa_s, char *cmd)
7677{
9a493fab 7678 int query_reason, list = 0;
15ab61ed 7679 char *btm_candidates = NULL;
65bcd0a9
VK
7680
7681 query_reason = atoi(cmd);
7682
9a493fab
AS
7683 cmd = os_strchr(cmd, ' ');
7684 if (cmd) {
15ab61ed 7685 if (os_strncmp(cmd, " list", 5) == 0)
9a493fab 7686 list = 1;
15ab61ed
AS
7687 else
7688 btm_candidates = cmd;
9a493fab
AS
7689 }
7690
7691 wpa_printf(MSG_DEBUG,
7692 "CTRL_IFACE: WNM_BSS_QUERY query_reason=%d%s",
7693 query_reason, list ? " candidate list" : "");
65bcd0a9 7694
15ab61ed
AS
7695 return wnm_send_bss_transition_mgmt_query(wpa_s, query_reason,
7696 btm_candidates,
7697 list);
65bcd0a9
VK
7698}
7699
d514b502
JM
7700
7701static int wpas_ctrl_iface_coloc_intf_report(struct wpa_supplicant *wpa_s,
7702 char *cmd)
7703{
7704 struct wpabuf *elems;
7705 int ret;
7706
7707 elems = wpabuf_parse_bin(cmd);
7708 if (!elems)
7709 return -1;
7710
7711 ret = wnm_send_coloc_intf_report(wpa_s, 0, elems);
7712 wpabuf_free(elems);
7713 return ret;
7714}
7715
e9199e31
JM
7716#endif /* CONFIG_WNM */
7717
7718
60b24b0d
DS
7719static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
7720 size_t buflen)
7721{
7722 struct wpa_signal_info si;
7723 int ret;
2cc8d8f4 7724 char *pos, *end;
60b24b0d
DS
7725
7726 ret = wpa_drv_signal_poll(wpa_s, &si);
7727 if (ret)
7728 return -1;
7729
2cc8d8f4
AO
7730 pos = buf;
7731 end = buf + buflen;
7732
7733 ret = os_snprintf(pos, end - pos, "RSSI=%d\nLINKSPEED=%d\n"
60b24b0d
DS
7734 "NOISE=%d\nFREQUENCY=%u\n",
7735 si.current_signal, si.current_txrate / 1000,
7736 si.current_noise, si.frequency);
7bdd8981 7737 if (os_snprintf_error(end - pos, ret))
60b24b0d 7738 return -1;
2cc8d8f4
AO
7739 pos += ret;
7740
7741 if (si.chanwidth != CHAN_WIDTH_UNKNOWN) {
7742 ret = os_snprintf(pos, end - pos, "WIDTH=%s\n",
7a4a93b9 7743 channel_width_to_string(si.chanwidth));
7bdd8981 7744 if (os_snprintf_error(end - pos, ret))
2cc8d8f4
AO
7745 return -1;
7746 pos += ret;
7747 }
7748
b55c623e
BP
7749 if (si.center_frq1 > 0) {
7750 ret = os_snprintf(pos, end - pos, "CENTER_FRQ1=%d\n",
7751 si.center_frq1);
7752 if (os_snprintf_error(end - pos, ret))
7753 return -1;
7754 pos += ret;
7755 }
7756
7757 if (si.center_frq2 > 0) {
7758 ret = os_snprintf(pos, end - pos, "CENTER_FRQ2=%d\n",
7759 si.center_frq2);
7bdd8981 7760 if (os_snprintf_error(end - pos, ret))
2cc8d8f4
AO
7761 return -1;
7762 pos += ret;
7763 }
7764
95783298
AO
7765 if (si.avg_signal) {
7766 ret = os_snprintf(pos, end - pos,
7767 "AVG_RSSI=%d\n", si.avg_signal);
d85e1fc8 7768 if (os_snprintf_error(end - pos, ret))
95783298
AO
7769 return -1;
7770 pos += ret;
7771 }
7772
74fa78b2
JM
7773 if (si.avg_beacon_signal) {
7774 ret = os_snprintf(pos, end - pos,
7775 "AVG_BEACON_RSSI=%d\n", si.avg_beacon_signal);
7776 if (os_snprintf_error(end - pos, ret))
7777 return -1;
7778 pos += ret;
7779 }
7780
2cc8d8f4 7781 return pos - buf;
60b24b0d
DS
7782}
7783
7784
96e8d831
DS
7785static int wpas_ctrl_iface_signal_monitor(struct wpa_supplicant *wpa_s,
7786 const char *cmd)
7787{
7788 const char *pos;
7789 int threshold = 0;
7790 int hysteresis = 0;
7791
7792 if (wpa_s->bgscan && wpa_s->bgscan_priv) {
7793 wpa_printf(MSG_DEBUG,
7794 "Reject SIGNAL_MONITOR command - bgscan is active");
7795 return -1;
7796 }
7797 pos = os_strstr(cmd, "THRESHOLD=");
7798 if (pos)
7799 threshold = atoi(pos + 10);
7800 pos = os_strstr(cmd, "HYSTERESIS=");
7801 if (pos)
7802 hysteresis = atoi(pos + 11);
7803 return wpa_drv_signal_monitor(wpa_s, threshold, hysteresis);
7804}
7805
7806
c06fca04
JM
7807#ifdef CONFIG_TESTING_OPTIONS
7808int wpas_ctrl_iface_get_pref_freq_list_override(struct wpa_supplicant *wpa_s,
7809 enum wpa_driver_if_type if_type,
7810 unsigned int *num,
7811 unsigned int *freq_list)
7812{
7813 char *pos = wpa_s->get_pref_freq_list_override;
7814 char *end;
7815 unsigned int count = 0;
7816
7817 /* Override string format:
7818 * <if_type1>:<freq1>,<freq2>,... <if_type2>:... */
7819
7820 while (pos) {
7821 if (atoi(pos) == (int) if_type)
7822 break;
7823 pos = os_strchr(pos, ' ');
7824 if (pos)
7825 pos++;
7826 }
7827 if (!pos)
7828 return -1;
7829 pos = os_strchr(pos, ':');
7830 if (!pos)
7831 return -1;
7832 pos++;
7833 end = os_strchr(pos, ' ');
7834 while (pos && (!end || pos < end) && count < *num) {
7835 freq_list[count++] = atoi(pos);
7836 pos = os_strchr(pos, ',');
7837 if (pos)
7838 pos++;
7839 }
7840
7841 *num = count;
7842 return 0;
7843}
7844#endif /* CONFIG_TESTING_OPTIONS */
7845
7846
98342208
AK
7847static int wpas_ctrl_iface_get_pref_freq_list(
7848 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
7849{
7850 unsigned int freq_list[100], num = 100, i;
7851 int ret;
7852 enum wpa_driver_if_type iface_type;
7853 char *pos, *end;
7854
7855 pos = buf;
7856 end = buf + buflen;
7857
7858 /* buf: "<interface_type>" */
7859 if (os_strcmp(cmd, "STATION") == 0)
7860 iface_type = WPA_IF_STATION;
7861 else if (os_strcmp(cmd, "AP") == 0)
7862 iface_type = WPA_IF_AP_BSS;
7863 else if (os_strcmp(cmd, "P2P_GO") == 0)
7864 iface_type = WPA_IF_P2P_GO;
7865 else if (os_strcmp(cmd, "P2P_CLIENT") == 0)
7866 iface_type = WPA_IF_P2P_CLIENT;
7867 else if (os_strcmp(cmd, "IBSS") == 0)
7868 iface_type = WPA_IF_IBSS;
7869 else if (os_strcmp(cmd, "TDLS") == 0)
7870 iface_type = WPA_IF_TDLS;
7871 else
7872 return -1;
7873
7874 wpa_printf(MSG_DEBUG,
7875 "CTRL_IFACE: GET_PREF_FREQ_LIST iface_type=%d (%s)",
e6ac47b4 7876 iface_type, cmd);
98342208
AK
7877
7878 ret = wpa_drv_get_pref_freq_list(wpa_s, iface_type, &num, freq_list);
7879 if (ret)
7880 return -1;
7881
7882 for (i = 0; i < num; i++) {
7883 ret = os_snprintf(pos, end - pos, "%s%u",
7884 i > 0 ? "," : "", freq_list[i]);
7885 if (os_snprintf_error(end - pos, ret))
7886 return -1;
7887 pos += ret;
7888 }
7889
7890 return pos - buf;
7891}
7892
7893
4d7aab78
EL
7894static int wpas_ctrl_iface_driver_flags(struct wpa_supplicant *wpa_s,
7895 char *buf, size_t buflen)
7896{
7897 int ret, i;
7898 char *pos, *end;
7899
7900 ret = os_snprintf(buf, buflen, "%016llX:\n",
7901 (long long unsigned) wpa_s->drv_flags);
7902 if (os_snprintf_error(buflen, ret))
7903 return -1;
7904
7905 pos = buf + ret;
7906 end = buf + buflen;
7907
7908 for (i = 0; i < 64; i++) {
7909 if (wpa_s->drv_flags & (1LLU << i)) {
7910 ret = os_snprintf(pos, end - pos, "%s\n",
7911 driver_flag_to_string(1LLU << i));
7912 if (os_snprintf_error(end - pos, ret))
7913 return -1;
7914 pos += ret;
7915 }
7916 }
7917
7918 return pos - buf;
7919}
7920
7921
dc7785f8
YZ
7922static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
7923 size_t buflen)
7924{
7925 struct hostap_sta_driver_data sta;
7926 int ret;
7927
7928 ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
7929 if (ret)
7930 return -1;
7931
7932 ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
7933 sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
7bdd8981 7934 if (os_snprintf_error(buflen, ret))
dc7785f8
YZ
7935 return -1;
7936 return ret;
7937}
7938
7939
5e2c3490
JM
7940#ifdef ANDROID
7941static int wpa_supplicant_driver_cmd(struct wpa_supplicant *wpa_s, char *cmd,
7942 char *buf, size_t buflen)
7943{
7944 int ret;
7945
7946 ret = wpa_drv_driver_cmd(wpa_s, cmd, buf, buflen);
a94737ea
DS
7947 if (ret == 0) {
7948 if (os_strncasecmp(cmd, "COUNTRY", 7) == 0) {
7949 struct p2p_data *p2p = wpa_s->global->p2p;
7950 if (p2p) {
7951 char country[3];
7952 country[0] = cmd[8];
7953 country[1] = cmd[9];
7954 country[2] = 0x04;
7955 p2p_set_country(p2p, country);
7956 }
7957 }
5e2c3490 7958 ret = os_snprintf(buf, buflen, "%s\n", "OK");
aaadd727
JM
7959 if (os_snprintf_error(buflen, ret))
7960 ret = -1;
a94737ea 7961 }
5e2c3490
JM
7962 return ret;
7963}
7964#endif /* ANDROID */
7965
7966
adef8948
BL
7967static int wpa_supplicant_vendor_cmd(struct wpa_supplicant *wpa_s, char *cmd,
7968 char *buf, size_t buflen)
7969{
7970 int ret;
7971 char *pos;
7972 u8 *data = NULL;
7973 unsigned int vendor_id, subcmd;
7974 struct wpabuf *reply;
7975 size_t data_len = 0;
7976
7977 /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
7978 vendor_id = strtoul(cmd, &pos, 16);
640b0b93 7979 if (!isblank((unsigned char) *pos))
adef8948
BL
7980 return -EINVAL;
7981
7982 subcmd = strtoul(pos, &pos, 10);
7983
7984 if (*pos != '\0') {
640b0b93 7985 if (!isblank((unsigned char) *pos++))
adef8948
BL
7986 return -EINVAL;
7987 data_len = os_strlen(pos);
7988 }
7989
7990 if (data_len) {
7991 data_len /= 2;
7992 data = os_malloc(data_len);
7993 if (!data)
2cebdee6 7994 return -1;
adef8948
BL
7995
7996 if (hexstr2bin(pos, data, data_len)) {
7997 wpa_printf(MSG_DEBUG,
7998 "Vendor command: wrong parameter format");
7999 os_free(data);
8000 return -EINVAL;
8001 }
8002 }
8003
8004 reply = wpabuf_alloc((buflen - 1) / 2);
8005 if (!reply) {
8006 os_free(data);
2cebdee6 8007 return -1;
adef8948
BL
8008 }
8009
8010 ret = wpa_drv_vendor_cmd(wpa_s, vendor_id, subcmd, data, data_len,
8011 reply);
8012
8013 if (ret == 0)
8014 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
8015 wpabuf_len(reply));
8016
8017 wpabuf_free(reply);
8018 os_free(data);
8019
8020 return ret;
8021}
8022
8023
acb54643
JM
8024static void wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant *wpa_s)
8025{
7e608d1d
IP
8026#ifdef CONFIG_P2P
8027 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s ?
8028 wpa_s->global->p2p_init_wpa_s : wpa_s;
8029#endif /* CONFIG_P2P */
8030
acb54643
JM
8031 wpa_dbg(wpa_s, MSG_DEBUG, "Flush all wpa_supplicant state");
8032
6e374bd4
JM
8033 if (wpas_abort_ongoing_scan(wpa_s) == 0)
8034 wpa_s->ignore_post_flush_scan_res = 1;
53401e91 8035
e9ccfc38
JM
8036 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
8037 /*
8038 * Avoid possible auto connect re-connection on getting
8039 * disconnected due to state flush.
8040 */
8041 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
8042 }
8043
acb54643 8044#ifdef CONFIG_P2P
1d20c66e 8045 wpas_p2p_group_remove(p2p_wpa_s, "*");
7e608d1d
IP
8046 wpas_p2p_cancel(p2p_wpa_s);
8047 p2p_ctrl_flush(p2p_wpa_s);
7e608d1d
IP
8048 wpas_p2p_service_flush(p2p_wpa_s);
8049 p2p_wpa_s->global->p2p_disabled = 0;
8050 p2p_wpa_s->global->p2p_per_sta_psk = 0;
8051 p2p_wpa_s->conf->num_sec_device_types = 0;
8052 p2p_wpa_s->p2p_disable_ip_addr_req = 0;
8053 os_free(p2p_wpa_s->global->p2p_go_avoid_freq.range);
8054 p2p_wpa_s->global->p2p_go_avoid_freq.range = NULL;
85e152b6 8055 p2p_wpa_s->global->p2p_go_avoid_freq.num = 0;
9a58e521 8056 p2p_wpa_s->global->pending_p2ps_group = 0;
8bb8e6ed 8057 p2p_wpa_s->global->pending_p2ps_group_freq = 0;
acb54643
JM
8058#endif /* CONFIG_P2P */
8059
8060#ifdef CONFIG_WPS_TESTING
8061 wps_version_number = 0x20;
8062 wps_testing_dummy_cred = 0;
91226e0d 8063 wps_corrupt_pkhash = 0;
6e379c6c
JM
8064 wps_force_auth_types_in_use = 0;
8065 wps_force_encr_types_in_use = 0;
acb54643
JM
8066#endif /* CONFIG_WPS_TESTING */
8067#ifdef CONFIG_WPS
7b02375a 8068 wpa_s->wps_fragment_size = 0;
acb54643 8069 wpas_wps_cancel(wpa_s);
422ba11e 8070 wps_registrar_flush(wpa_s->wps->registrar);
acb54643 8071#endif /* CONFIG_WPS */
7255983b 8072 wpa_s->after_wps = 0;
4d9fb08d 8073 wpa_s->known_wps_freq = 0;
acb54643 8074
30d27b04
JM
8075#ifdef CONFIG_DPP
8076 wpas_dpp_deinit(wpa_s);
f97ace34
JM
8077 wpa_s->dpp_init_max_tries = 0;
8078 wpa_s->dpp_init_retry_time = 0;
8079 wpa_s->dpp_resp_wait_time = 0;
95b0104a
JM
8080 wpa_s->dpp_resp_max_tries = 0;
8081 wpa_s->dpp_resp_retry_time = 0;
dc2f24f1 8082#ifdef CONFIG_TESTING_OPTIONS
2bdc47a9
JM
8083 os_memset(dpp_pkex_own_mac_override, 0, ETH_ALEN);
8084 os_memset(dpp_pkex_peer_mac_override, 0, ETH_ALEN);
acc555f9 8085 dpp_pkex_ephemeral_key_override_len = 0;
f5526975 8086 dpp_protocol_key_override_len = 0;
055cd397 8087 dpp_nonce_override_len = 0;
dc2f24f1 8088#endif /* CONFIG_TESTING_OPTIONS */
30d27b04
JM
8089#endif /* CONFIG_DPP */
8090
9d2cb3ec 8091#ifdef CONFIG_TDLS
acb54643 8092#ifdef CONFIG_TDLS_TESTING
acb54643
JM
8093 tdls_testing = 0;
8094#endif /* CONFIG_TDLS_TESTING */
acb54643
JM
8095 wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL);
8096 wpa_tdls_enable(wpa_s->wpa, 1);
8097#endif /* CONFIG_TDLS */
8098
e78aaca0
JM
8099 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
8100 wpa_supplicant_stop_countermeasures(wpa_s, NULL);
0d1d1f0d 8101 wpa_s->last_michael_mic_error.sec = 0;
e78aaca0 8102
acb54643 8103 wpa_s->no_keep_alive = 0;
c2805909 8104 wpa_s->own_disconnect_req = 0;
acb54643
JM
8105
8106 os_free(wpa_s->disallow_aps_bssid);
8107 wpa_s->disallow_aps_bssid = NULL;
8108 wpa_s->disallow_aps_bssid_count = 0;
8109 os_free(wpa_s->disallow_aps_ssid);
8110 wpa_s->disallow_aps_ssid = NULL;
8111 wpa_s->disallow_aps_ssid_count = 0;
8112
8113 wpa_s->set_sta_uapsd = 0;
8114 wpa_s->sta_uapsd = 0;
8115
8116 wpa_drv_radio_disable(wpa_s, 0);
acb54643 8117 wpa_blacklist_clear(wpa_s);
a8a7890d 8118 wpa_s->extra_blacklist_count = 0;
acb54643
JM
8119 wpa_supplicant_ctrl_iface_remove_network(wpa_s, "all");
8120 wpa_supplicant_ctrl_iface_remove_cred(wpa_s, "all");
d9bb2821 8121 wpa_config_flush_blobs(wpa_s->conf);
ea6e040c
JM
8122 wpa_s->conf->auto_interworking = 0;
8123 wpa_s->conf->okc = 0;
04f7ecc6 8124
b925506a
JM
8125 wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
8126 rsn_preauth_deinit(wpa_s->wpa);
8127
04f7ecc6
JM
8128 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME, 43200);
8129 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD, 70);
8130 wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, 60);
0d79b50a 8131 eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
b1ae396f 8132
b3253ebb 8133 radio_remove_works(wpa_s, NULL, 1);
e3745228 8134 wpa_s->ext_work_in_progress = 0;
3d910ef4
JM
8135
8136 wpa_s->next_ssid = NULL;
b572df86
JM
8137
8138#ifdef CONFIG_INTERWORKING
876e74aa 8139#ifdef CONFIG_HS20
b572df86 8140 hs20_cancel_fetch_osu(wpa_s);
8dd5c1b4 8141 hs20_del_icon(wpa_s, NULL, NULL);
876e74aa 8142#endif /* CONFIG_HS20 */
b572df86 8143#endif /* CONFIG_INTERWORKING */
60b893df
JM
8144
8145 wpa_s->ext_mgmt_frame_handling = 0;
9d4ff04a 8146 wpa_s->ext_eapol_frame_io = 0;
1f94e4ee
JM
8147#ifdef CONFIG_TESTING_OPTIONS
8148 wpa_s->extra_roc_dur = 0;
911942ee 8149 wpa_s->test_failure = WPAS_TEST_FAILURE_NONE;
ed7820b4 8150 wpa_s->p2p_go_csa_on_inv = 0;
02adead5 8151 wpa_s->ignore_auth_resp = 0;
6ad37d73 8152 wpa_s->ignore_assoc_disallow = 0;
daa40960 8153 wpa_s->testing_resend_assoc = 0;
405946d7 8154 wpa_s->ignore_sae_h2e_only = 0;
a483c6f1 8155 wpa_s->reject_btm_req_reason = 0;
651c6a84 8156 wpa_sm_set_test_assoc_ie(wpa_s->wpa, NULL);
c06fca04
JM
8157 os_free(wpa_s->get_pref_freq_list_override);
8158 wpa_s->get_pref_freq_list_override = NULL;
a0f19e9c
JM
8159 wpabuf_free(wpa_s->sae_commit_override);
8160 wpa_s->sae_commit_override = NULL;
b834e970
JM
8161 os_free(wpa_s->extra_sae_rejected_groups);
8162 wpa_s->extra_sae_rejected_groups = NULL;
13256553
JM
8163 wpabuf_free(wpa_s->rsnxe_override_assoc);
8164 wpa_s->rsnxe_override_assoc = NULL;
8165 wpabuf_free(wpa_s->rsnxe_override_eapol);
8166 wpa_s->rsnxe_override_eapol = NULL;
6c570193 8167 wpas_clear_driver_signal_override(wpa_s);
60239f60 8168#ifdef CONFIG_DPP
b7dddab7
JM
8169 os_free(wpa_s->dpp_config_obj_override);
8170 wpa_s->dpp_config_obj_override = NULL;
8171 os_free(wpa_s->dpp_discovery_override);
8172 wpa_s->dpp_discovery_override = NULL;
8173 os_free(wpa_s->dpp_groups_override);
8174 wpa_s->dpp_groups_override = NULL;
60239f60
JM
8175 dpp_test = DPP_TEST_DISABLED;
8176#endif /* CONFIG_DPP */
1f94e4ee 8177#endif /* CONFIG_TESTING_OPTIONS */
97cfe110
JM
8178
8179 wpa_s->disconnected = 0;
b8db1dfc
JM
8180 os_free(wpa_s->next_scan_freqs);
8181 wpa_s->next_scan_freqs = NULL;
88a44755
JM
8182 os_free(wpa_s->select_network_scan_freqs);
8183 wpa_s->select_network_scan_freqs = NULL;
6c699913
JM
8184
8185 wpa_bss_flush(wpa_s);
8186 if (!dl_list_empty(&wpa_s->bss)) {
8187 wpa_printf(MSG_DEBUG,
8188 "BSS table not empty after flush: %u entries, current_bss=%p bssid="
8189 MACSTR " pending_bssid=" MACSTR,
8190 dl_list_len(&wpa_s->bss), wpa_s->current_bss,
8191 MAC2STR(wpa_s->bssid),
8192 MAC2STR(wpa_s->pending_bssid));
8193 }
9bd566a3
AS
8194
8195 eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
03ed0a52 8196 wpa_s->wnmsleep_used = 0;
ab6ab07a
JM
8197
8198#ifdef CONFIG_SME
8199 wpa_s->sme.last_unprot_disconnect.sec = 0;
f97baef2 8200 wpa_s->sme.auth_alg = 0;
ab6ab07a 8201#endif /* CONFIG_SME */
c6c41f6e
JM
8202
8203 wpabuf_free(wpa_s->ric_ies);
8204 wpa_s->ric_ies = NULL;
32bb47fa
JM
8205
8206 wpa_supplicant_update_channel_list(wpa_s, NULL);
19677b77
BL
8207
8208 free_bss_tmp_disallowed(wpa_s);
acb54643
JM
8209}
8210
8211
1f965e62
JM
8212static int wpas_ctrl_radio_work_show(struct wpa_supplicant *wpa_s,
8213 char *buf, size_t buflen)
8214{
8215 struct wpa_radio_work *work;
8216 char *pos, *end;
8217 struct os_reltime now, diff;
8218
8219 pos = buf;
8220 end = buf + buflen;
8221
8222 os_get_reltime(&now);
8223
8224 dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
8225 {
8226 int ret;
8227
8228 os_reltime_sub(&now, &work->time, &diff);
8229 ret = os_snprintf(pos, end - pos, "%s@%s:%u:%u:%ld.%06ld\n",
8230 work->type, work->wpa_s->ifname, work->freq,
8231 work->started, diff.sec, diff.usec);
d85e1fc8 8232 if (os_snprintf_error(end - pos, ret))
1f965e62
JM
8233 break;
8234 pos += ret;
8235 }
8236
8237 return pos - buf;
8238}
8239
8240
8241static void wpas_ctrl_radio_work_timeout(void *eloop_ctx, void *timeout_ctx)
8242{
8243 struct wpa_radio_work *work = eloop_ctx;
8244 struct wpa_external_work *ework = work->ctx;
8245
8246 wpa_dbg(work->wpa_s, MSG_DEBUG,
8247 "Timing out external radio work %u (%s)",
8248 ework->id, work->type);
8249 wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_TIMEOUT "%u", ework->id);
e3745228 8250 work->wpa_s->ext_work_in_progress = 0;
1f965e62 8251 radio_work_done(work);
df48efc5 8252 os_free(ework);
1f965e62
JM
8253}
8254
8255
8256static void wpas_ctrl_radio_work_cb(struct wpa_radio_work *work, int deinit)
8257{
8258 struct wpa_external_work *ework = work->ctx;
8259
8260 if (deinit) {
b3253ebb
AO
8261 if (work->started)
8262 eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
8263 work, NULL);
8264
7d1007a6
JM
8265 /*
8266 * work->type points to a buffer in ework, so need to replace
8267 * that here with a fixed string to avoid use of freed memory
8268 * in debug prints.
8269 */
8270 work->type = "freed-ext-work";
8271 work->ctx = NULL;
1f965e62
JM
8272 os_free(ework);
8273 return;
8274 }
8275
8276 wpa_dbg(work->wpa_s, MSG_DEBUG, "Starting external radio work %u (%s)",
8277 ework->id, ework->type);
8278 wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_START "%u", ework->id);
e3745228 8279 work->wpa_s->ext_work_in_progress = 1;
1f965e62
JM
8280 if (!ework->timeout)
8281 ework->timeout = 10;
8282 eloop_register_timeout(ework->timeout, 0, wpas_ctrl_radio_work_timeout,
8283 work, NULL);
8284}
8285
8286
8287static int wpas_ctrl_radio_work_add(struct wpa_supplicant *wpa_s, char *cmd,
8288 char *buf, size_t buflen)
8289{
8290 struct wpa_external_work *ework;
8291 char *pos, *pos2;
8292 size_t type_len;
8293 int ret;
8294 unsigned int freq = 0;
8295
8296 /* format: <name> [freq=<MHz>] [timeout=<seconds>] */
8297
8298 ework = os_zalloc(sizeof(*ework));
8299 if (ework == NULL)
8300 return -1;
8301
8302 pos = os_strchr(cmd, ' ');
8303 if (pos) {
8304 type_len = pos - cmd;
8305 pos++;
8306
8307 pos2 = os_strstr(pos, "freq=");
8308 if (pos2)
8309 freq = atoi(pos2 + 5);
8310
8311 pos2 = os_strstr(pos, "timeout=");
8312 if (pos2)
8313 ework->timeout = atoi(pos2 + 8);
8314 } else {
8315 type_len = os_strlen(cmd);
8316 }
8317 if (4 + type_len >= sizeof(ework->type))
8318 type_len = sizeof(ework->type) - 4 - 1;
8319 os_strlcpy(ework->type, "ext:", sizeof(ework->type));
8320 os_memcpy(ework->type + 4, cmd, type_len);
8321 ework->type[4 + type_len] = '\0';
8322
8323 wpa_s->ext_work_id++;
8324 if (wpa_s->ext_work_id == 0)
8325 wpa_s->ext_work_id++;
8326 ework->id = wpa_s->ext_work_id;
8327
8328 if (radio_add_work(wpa_s, freq, ework->type, 0, wpas_ctrl_radio_work_cb,
8329 ework) < 0) {
8330 os_free(ework);
8331 return -1;
8332 }
8333
8334 ret = os_snprintf(buf, buflen, "%u", ework->id);
d85e1fc8 8335 if (os_snprintf_error(buflen, ret))
1f965e62
JM
8336 return -1;
8337 return ret;
8338}
8339
8340
8341static int wpas_ctrl_radio_work_done(struct wpa_supplicant *wpa_s, char *cmd)
8342{
8343 struct wpa_radio_work *work;
8344 unsigned int id = atoi(cmd);
8345
8346 dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
8347 {
8348 struct wpa_external_work *ework;
8349
8350 if (os_strncmp(work->type, "ext:", 4) != 0)
8351 continue;
8352 ework = work->ctx;
8353 if (id && ework->id != id)
8354 continue;
8355 wpa_dbg(wpa_s, MSG_DEBUG,
8356 "Completed external radio work %u (%s)",
8357 ework->id, ework->type);
8358 eloop_cancel_timeout(wpas_ctrl_radio_work_timeout, work, NULL);
e3745228 8359 wpa_s->ext_work_in_progress = 0;
1f965e62 8360 radio_work_done(work);
6829da39 8361 os_free(ework);
1f965e62
JM
8362 return 3; /* "OK\n" */
8363 }
8364
8365 return -1;
8366}
8367
8368
8369static int wpas_ctrl_radio_work(struct wpa_supplicant *wpa_s, char *cmd,
8370 char *buf, size_t buflen)
8371{
8372 if (os_strcmp(cmd, "show") == 0)
8373 return wpas_ctrl_radio_work_show(wpa_s, buf, buflen);
8374 if (os_strncmp(cmd, "add ", 4) == 0)
8375 return wpas_ctrl_radio_work_add(wpa_s, cmd + 4, buf, buflen);
8376 if (os_strncmp(cmd, "done ", 5) == 0)
8377 return wpas_ctrl_radio_work_done(wpa_s, cmd + 4);
8378 return -1;
8379}
8380
8381
8382void wpas_ctrl_radio_work_flush(struct wpa_supplicant *wpa_s)
8383{
8384 struct wpa_radio_work *work, *tmp;
8385
a6cff8bf
MS
8386 if (!wpa_s || !wpa_s->radio)
8387 return;
8388
1f965e62
JM
8389 dl_list_for_each_safe(work, tmp, &wpa_s->radio->work,
8390 struct wpa_radio_work, list) {
8391 struct wpa_external_work *ework;
8392
8393 if (os_strncmp(work->type, "ext:", 4) != 0)
8394 continue;
8395 ework = work->ctx;
8396 wpa_dbg(wpa_s, MSG_DEBUG,
023b466d 8397 "Flushing%s external radio work %u (%s)",
1f965e62
JM
8398 work->started ? " started" : "", ework->id,
8399 ework->type);
8400 if (work->started)
8401 eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
8402 work, NULL);
1f965e62 8403 radio_work_done(work);
df48efc5 8404 os_free(ework);
1f965e62
JM
8405 }
8406}
8407
8408
bceb8431
JM
8409static void wpas_ctrl_eapol_response(void *eloop_ctx, void *timeout_ctx)
8410{
8411 struct wpa_supplicant *wpa_s = eloop_ctx;
8412 eapol_sm_notify_ctrl_response(wpa_s->eapol);
8413}
8414
8415
43a66ecb
JM
8416static int scan_id_list_parse(struct wpa_supplicant *wpa_s, const char *value,
8417 unsigned int *scan_id_count, int scan_id[])
6891f0e6
LJ
8418{
8419 const char *pos = value;
8420
8421 while (pos) {
8422 if (*pos == ' ' || *pos == '\0')
8423 break;
43a66ecb 8424 if (*scan_id_count == MAX_SCAN_ID)
6891f0e6 8425 return -1;
43a66ecb 8426 scan_id[(*scan_id_count)++] = atoi(pos);
6891f0e6
LJ
8427 pos = os_strchr(pos, ',');
8428 if (pos)
8429 pos++;
8430 }
8431
8432 return 0;
8433}
8434
8435
fee52342
JM
8436static void wpas_ctrl_scan(struct wpa_supplicant *wpa_s, char *params,
8437 char *reply, int reply_size, int *reply_len)
8438{
8439 char *pos;
43a66ecb
JM
8440 unsigned int manual_scan_passive = 0;
8441 unsigned int manual_scan_use_id = 0;
8442 unsigned int manual_scan_only_new = 0;
8443 unsigned int scan_only = 0;
8444 unsigned int scan_id_count = 0;
8445 int scan_id[MAX_SCAN_ID];
8446 void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
8447 struct wpa_scan_results *scan_res);
8448 int *manual_scan_freqs = NULL;
a80651d0
KV
8449 struct wpa_ssid_value *ssid = NULL, *ns;
8450 unsigned int ssid_count = 0;
fee52342
JM
8451
8452 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
8453 *reply_len = -1;
8454 return;
8455 }
8456
e69ae5ff
JM
8457 if (radio_work_pending(wpa_s, "scan")) {
8458 wpa_printf(MSG_DEBUG,
8459 "Pending scan scheduled - reject new request");
8460 *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
8461 return;
8462 }
8463
9772af66
NM
8464#ifdef CONFIG_INTERWORKING
8465 if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select) {
8466 wpa_printf(MSG_DEBUG,
8467 "Interworking select in progress - reject new scan");
8468 *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
8469 return;
8470 }
8471#endif /* CONFIG_INTERWORKING */
8472
fee52342
JM
8473 if (params) {
8474 if (os_strncasecmp(params, "TYPE=ONLY", 9) == 0)
43a66ecb 8475 scan_only = 1;
fee52342
JM
8476
8477 pos = os_strstr(params, "freq=");
43a66ecb
JM
8478 if (pos) {
8479 manual_scan_freqs = freq_range_to_channel_list(wpa_s,
8480 pos + 5);
8481 if (manual_scan_freqs == NULL) {
8482 *reply_len = -1;
8483 goto done;
8484 }
fee52342 8485 }
88c2d488
JM
8486
8487 pos = os_strstr(params, "passive=");
8488 if (pos)
43a66ecb 8489 manual_scan_passive = !!atoi(pos + 8);
d81c73be
JM
8490
8491 pos = os_strstr(params, "use_id=");
8492 if (pos)
43a66ecb 8493 manual_scan_use_id = atoi(pos + 7);
949938aa
JM
8494
8495 pos = os_strstr(params, "only_new=1");
8496 if (pos)
43a66ecb 8497 manual_scan_only_new = 1;
6891f0e6
LJ
8498
8499 pos = os_strstr(params, "scan_id=");
43a66ecb
JM
8500 if (pos && scan_id_list_parse(wpa_s, pos + 8, &scan_id_count,
8501 scan_id) < 0) {
6891f0e6 8502 *reply_len = -1;
43a66ecb 8503 goto done;
6891f0e6 8504 }
a80651d0 8505
267fc0dd
AP
8506 pos = os_strstr(params, "bssid=");
8507 if (pos) {
8508 u8 bssid[ETH_ALEN];
8509
8510 pos += 6;
8511 if (hwaddr_aton(pos, bssid)) {
8512 wpa_printf(MSG_ERROR, "Invalid BSSID %s", pos);
8513 *reply_len = -1;
8514 goto done;
8515 }
8516 os_memcpy(wpa_s->next_scan_bssid, bssid, ETH_ALEN);
8517 }
8518
a80651d0
KV
8519 pos = params;
8520 while (pos && *pos != '\0') {
8521 if (os_strncmp(pos, "ssid ", 5) == 0) {
8522 char *end;
8523
8524 pos += 5;
8525 end = pos;
8526 while (*end) {
8527 if (*end == '\0' || *end == ' ')
8528 break;
8529 end++;
8530 }
8531
8532 ns = os_realloc_array(
8533 ssid, ssid_count + 1,
8534 sizeof(struct wpa_ssid_value));
8535 if (ns == NULL) {
8536 *reply_len = -1;
8537 goto done;
8538 }
8539 ssid = ns;
8540
8541 if ((end - pos) & 0x01 ||
8542 end - pos > 2 * SSID_MAX_LEN ||
8543 hexstr2bin(pos, ssid[ssid_count].ssid,
8544 (end - pos) / 2) < 0) {
8545 wpa_printf(MSG_DEBUG,
8546 "Invalid SSID value '%s'",
8547 pos);
8548 *reply_len = -1;
8549 goto done;
8550 }
8551 ssid[ssid_count].ssid_len = (end - pos) / 2;
8552 wpa_hexdump_ascii(MSG_DEBUG, "scan SSID",
8553 ssid[ssid_count].ssid,
8554 ssid[ssid_count].ssid_len);
8555 ssid_count++;
8556 pos = end;
8557 }
8558
8559 pos = os_strchr(pos, ' ');
8560 if (pos)
8561 pos++;
8562 }
8563 }
8564
8565 wpa_s->num_ssids_from_scan_req = ssid_count;
8566 os_free(wpa_s->ssids_from_scan_req);
8567 if (ssid_count) {
8568 wpa_s->ssids_from_scan_req = ssid;
8569 ssid = NULL;
8570 } else {
8571 wpa_s->ssids_from_scan_req = NULL;
fee52342
JM
8572 }
8573
43a66ecb
JM
8574 if (scan_only)
8575 scan_res_handler = scan_only_handler;
8576 else if (wpa_s->scan_res_handler == scan_only_handler)
8577 scan_res_handler = NULL;
8578 else
8579 scan_res_handler = wpa_s->scan_res_handler;
8580
fee52342
JM
8581 if (!wpa_s->sched_scanning && !wpa_s->scanning &&
8582 ((wpa_s->wpa_state <= WPA_SCANNING) ||
8583 (wpa_s->wpa_state == WPA_COMPLETED))) {
43a66ecb
JM
8584 wpa_s->manual_scan_passive = manual_scan_passive;
8585 wpa_s->manual_scan_use_id = manual_scan_use_id;
8586 wpa_s->manual_scan_only_new = manual_scan_only_new;
8587 wpa_s->scan_id_count = scan_id_count;
8588 os_memcpy(wpa_s->scan_id, scan_id, scan_id_count * sizeof(int));
8589 wpa_s->scan_res_handler = scan_res_handler;
8590 os_free(wpa_s->manual_scan_freqs);
8591 wpa_s->manual_scan_freqs = manual_scan_freqs;
8592 manual_scan_freqs = NULL;
8593
fee52342
JM
8594 wpa_s->normal_scans = 0;
8595 wpa_s->scan_req = MANUAL_SCAN_REQ;
8596 wpa_s->after_wps = 0;
8597 wpa_s->known_wps_freq = 0;
8598 wpa_supplicant_req_scan(wpa_s, 0, 0);
d81c73be
JM
8599 if (wpa_s->manual_scan_use_id) {
8600 wpa_s->manual_scan_id++;
8601 wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
8602 wpa_s->manual_scan_id);
8603 *reply_len = os_snprintf(reply, reply_size, "%u\n",
8604 wpa_s->manual_scan_id);
8605 }
fee52342 8606 } else if (wpa_s->sched_scanning) {
43a66ecb
JM
8607 wpa_s->manual_scan_passive = manual_scan_passive;
8608 wpa_s->manual_scan_use_id = manual_scan_use_id;
8609 wpa_s->manual_scan_only_new = manual_scan_only_new;
8610 wpa_s->scan_id_count = scan_id_count;
8611 os_memcpy(wpa_s->scan_id, scan_id, scan_id_count * sizeof(int));
8612 wpa_s->scan_res_handler = scan_res_handler;
8613 os_free(wpa_s->manual_scan_freqs);
8614 wpa_s->manual_scan_freqs = manual_scan_freqs;
8615 manual_scan_freqs = NULL;
8616
fee52342
JM
8617 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to allow requested full scan to proceed");
8618 wpa_supplicant_cancel_sched_scan(wpa_s);
8619 wpa_s->scan_req = MANUAL_SCAN_REQ;
8620 wpa_supplicant_req_scan(wpa_s, 0, 0);
d81c73be
JM
8621 if (wpa_s->manual_scan_use_id) {
8622 wpa_s->manual_scan_id++;
8623 *reply_len = os_snprintf(reply, reply_size, "%u\n",
8624 wpa_s->manual_scan_id);
8625 wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
8626 wpa_s->manual_scan_id);
8627 }
fee52342
JM
8628 } else {
8629 wpa_printf(MSG_DEBUG, "Ongoing scan action - reject new request");
8630 *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
8631 }
43a66ecb
JM
8632
8633done:
8634 os_free(manual_scan_freqs);
a80651d0 8635 os_free(ssid);
fee52342
JM
8636}
8637
8638
60b893df
JM
8639#ifdef CONFIG_TESTING_OPTIONS
8640
8641static void wpas_ctrl_iface_mgmt_tx_cb(struct wpa_supplicant *wpa_s,
8642 unsigned int freq, const u8 *dst,
8643 const u8 *src, const u8 *bssid,
8644 const u8 *data, size_t data_len,
8645 enum offchannel_send_action_result
8646 result)
8647{
8648 wpa_msg(wpa_s, MSG_INFO, "MGMT-TX-STATUS freq=%u dst=" MACSTR
8649 " src=" MACSTR " bssid=" MACSTR " result=%s",
8650 freq, MAC2STR(dst), MAC2STR(src), MAC2STR(bssid),
8651 result == OFFCHANNEL_SEND_ACTION_SUCCESS ?
8652 "SUCCESS" : (result == OFFCHANNEL_SEND_ACTION_NO_ACK ?
8653 "NO_ACK" : "FAILED"));
8654}
8655
8656
8657static int wpas_ctrl_iface_mgmt_tx(struct wpa_supplicant *wpa_s, char *cmd)
8658{
8659 char *pos, *param;
8660 size_t len;
8661 u8 *buf, da[ETH_ALEN], bssid[ETH_ALEN];
8662 int res, used;
8663 int freq = 0, no_cck = 0, wait_time = 0;
8664
8665 /* <DA> <BSSID> [freq=<MHz>] [wait_time=<ms>] [no_cck=1]
8666 * <action=Action frame payload> */
8667
8668 wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
8669
8670 pos = cmd;
8671 used = hwaddr_aton2(pos, da);
8672 if (used < 0)
8673 return -1;
8674 pos += used;
8675 while (*pos == ' ')
8676 pos++;
8677 used = hwaddr_aton2(pos, bssid);
8678 if (used < 0)
8679 return -1;
8680 pos += used;
8681
8682 param = os_strstr(pos, " freq=");
8683 if (param) {
8684 param += 6;
8685 freq = atoi(param);
8686 }
8687
8688 param = os_strstr(pos, " no_cck=");
8689 if (param) {
8690 param += 8;
8691 no_cck = atoi(param);
8692 }
8693
8694 param = os_strstr(pos, " wait_time=");
8695 if (param) {
8696 param += 11;
8697 wait_time = atoi(param);
8698 }
8699
8700 param = os_strstr(pos, " action=");
8701 if (param == NULL)
8702 return -1;
8703 param += 8;
8704
8705 len = os_strlen(param);
8706 if (len & 1)
8707 return -1;
8708 len /= 2;
8709
8710 buf = os_malloc(len);
8711 if (buf == NULL)
8712 return -1;
8713
8714 if (hexstr2bin(param, buf, len) < 0) {
8715 os_free(buf);
8716 return -1;
8717 }
8718
8719 res = offchannel_send_action(wpa_s, freq, da, wpa_s->own_addr, bssid,
8720 buf, len, wait_time,
8721 wpas_ctrl_iface_mgmt_tx_cb, no_cck);
8722 os_free(buf);
8723 return res;
8724}
8725
8726
8727static void wpas_ctrl_iface_mgmt_tx_done(struct wpa_supplicant *wpa_s)
8728{
8729 wpa_printf(MSG_DEBUG, "External MGMT TX - done waiting");
8730 offchannel_send_action_done(wpa_s);
8731}
8732
ad12f2f4 8733
4de70e23
JM
8734static int wpas_ctrl_iface_mgmt_rx_process(struct wpa_supplicant *wpa_s,
8735 char *cmd)
8736{
8737 char *pos, *param;
8738 size_t len;
8739 u8 *buf;
8740 int freq = 0, datarate = 0, ssi_signal = 0;
8741 union wpa_event_data event;
8742
8743 if (!wpa_s->ext_mgmt_frame_handling)
8744 return -1;
8745
8746 /* freq=<MHz> datarate=<val> ssi_signal=<val> frame=<frame hexdump> */
8747
8748 wpa_printf(MSG_DEBUG, "External MGMT RX process: %s", cmd);
8749
8750 pos = cmd;
8751 param = os_strstr(pos, "freq=");
8752 if (param) {
8753 param += 5;
8754 freq = atoi(param);
8755 }
8756
8757 param = os_strstr(pos, " datarate=");
8758 if (param) {
8759 param += 10;
8760 datarate = atoi(param);
8761 }
8762
8763 param = os_strstr(pos, " ssi_signal=");
8764 if (param) {
8765 param += 12;
8766 ssi_signal = atoi(param);
8767 }
8768
8769 param = os_strstr(pos, " frame=");
8770 if (param == NULL)
8771 return -1;
8772 param += 7;
8773
8774 len = os_strlen(param);
8775 if (len & 1)
8776 return -1;
8777 len /= 2;
8778
8779 buf = os_malloc(len);
8780 if (buf == NULL)
8781 return -1;
8782
8783 if (hexstr2bin(param, buf, len) < 0) {
8784 os_free(buf);
8785 return -1;
8786 }
8787
8788 os_memset(&event, 0, sizeof(event));
8789 event.rx_mgmt.freq = freq;
8790 event.rx_mgmt.frame = buf;
8791 event.rx_mgmt.frame_len = len;
8792 event.rx_mgmt.ssi_signal = ssi_signal;
8793 event.rx_mgmt.datarate = datarate;
8794 wpa_s->ext_mgmt_frame_handling = 0;
8795 wpa_supplicant_event(wpa_s, EVENT_RX_MGMT, &event);
8796 wpa_s->ext_mgmt_frame_handling = 1;
8797
8798 os_free(buf);
8799
8800 return 0;
8801}
8802
8803
e4a3e1d0
JM
8804static int wpas_ctrl_iface_driver_scan_res(struct wpa_supplicant *wpa_s,
8805 char *param)
8806{
8807 struct wpa_scan_res *res;
8808 struct os_reltime now;
8809 char *pos, *end;
1fb4437c 8810 int ret = -1;
e4a3e1d0
JM
8811
8812 if (!param)
8813 return -1;
8814
8815 if (os_strcmp(param, "START") == 0) {
8816 wpa_bss_update_start(wpa_s);
8817 return 0;
8818 }
8819
8820 if (os_strcmp(param, "END") == 0) {
8821 wpa_bss_update_end(wpa_s, NULL, 1);
8822 return 0;
8823 }
8824
8825 if (os_strncmp(param, "BSS ", 4) != 0)
8826 return -1;
8827 param += 3;
8828
8829 res = os_zalloc(sizeof(*res) + os_strlen(param) / 2);
8830 if (!res)
8831 return -1;
8832
8833 pos = os_strstr(param, " flags=");
8834 if (pos)
8835 res->flags = strtol(pos + 7, NULL, 16);
8836
8837 pos = os_strstr(param, " bssid=");
1fb4437c
JM
8838 if (pos && hwaddr_aton(pos + 7, res->bssid))
8839 goto fail;
e4a3e1d0
JM
8840
8841 pos = os_strstr(param, " freq=");
8842 if (pos)
8843 res->freq = atoi(pos + 6);
8844
8845 pos = os_strstr(param, " beacon_int=");
8846 if (pos)
8847 res->beacon_int = atoi(pos + 12);
8848
8849 pos = os_strstr(param, " caps=");
8850 if (pos)
8851 res->caps = strtol(pos + 6, NULL, 16);
8852
8853 pos = os_strstr(param, " qual=");
8854 if (pos)
8855 res->qual = atoi(pos + 6);
8856
8857 pos = os_strstr(param, " noise=");
8858 if (pos)
8859 res->noise = atoi(pos + 7);
8860
8861 pos = os_strstr(param, " level=");
8862 if (pos)
8863 res->level = atoi(pos + 7);
8864
8865 pos = os_strstr(param, " tsf=");
8866 if (pos)
8867 res->tsf = strtoll(pos + 5, NULL, 16);
8868
8869 pos = os_strstr(param, " age=");
8870 if (pos)
8871 res->age = atoi(pos + 5);
8872
8873 pos = os_strstr(param, " est_throughput=");
8874 if (pos)
8875 res->est_throughput = atoi(pos + 16);
8876
8877 pos = os_strstr(param, " snr=");
8878 if (pos)
8879 res->snr = atoi(pos + 5);
8880
8881 pos = os_strstr(param, " parent_tsf=");
8882 if (pos)
8883 res->parent_tsf = strtoll(pos + 7, NULL, 16);
8884
8885 pos = os_strstr(param, " tsf_bssid=");
1fb4437c
JM
8886 if (pos && hwaddr_aton(pos + 11, res->tsf_bssid))
8887 goto fail;
e4a3e1d0
JM
8888
8889 pos = os_strstr(param, " ie=");
8890 if (pos) {
8891 pos += 4;
8892 end = os_strchr(pos, ' ');
8893 if (!end)
8894 end = pos + os_strlen(pos);
8895 res->ie_len = (end - pos) / 2;
1fb4437c
JM
8896 if (hexstr2bin(pos, (u8 *) (res + 1), res->ie_len))
8897 goto fail;
e4a3e1d0
JM
8898 }
8899
8900 pos = os_strstr(param, " beacon_ie=");
8901 if (pos) {
8902 pos += 11;
8903 end = os_strchr(pos, ' ');
8904 if (!end)
8905 end = pos + os_strlen(pos);
8906 res->beacon_ie_len = (end - pos) / 2;
1fb4437c
JM
8907 if (hexstr2bin(pos, ((u8 *) (res + 1)) + res->ie_len,
8908 res->beacon_ie_len))
8909 goto fail;
e4a3e1d0
JM
8910 }
8911
8912 os_get_reltime(&now);
8913 wpa_bss_update_scan_res(wpa_s, res, &now);
1fb4437c
JM
8914 ret = 0;
8915fail:
e4a3e1d0
JM
8916 os_free(res);
8917
1fb4437c 8918 return ret;
e4a3e1d0
JM
8919}
8920
8921
ad12f2f4
JM
8922static int wpas_ctrl_iface_driver_event(struct wpa_supplicant *wpa_s, char *cmd)
8923{
8924 char *pos, *param;
8925 union wpa_event_data event;
8926 enum wpa_event_type ev;
8927
8928 /* <event name> [parameters..] */
8929
8930 wpa_dbg(wpa_s, MSG_DEBUG, "Testing - external driver event: %s", cmd);
8931
8932 pos = cmd;
8933 param = os_strchr(pos, ' ');
8934 if (param)
8935 *param++ = '\0';
8936
8937 os_memset(&event, 0, sizeof(event));
8938
8939 if (os_strcmp(cmd, "INTERFACE_ENABLED") == 0) {
8940 ev = EVENT_INTERFACE_ENABLED;
8941 } else if (os_strcmp(cmd, "INTERFACE_DISABLED") == 0) {
8942 ev = EVENT_INTERFACE_DISABLED;
7bb70909
JM
8943 } else if (os_strcmp(cmd, "AVOID_FREQUENCIES") == 0) {
8944 ev = EVENT_AVOID_FREQUENCIES;
8945 if (param == NULL)
8946 param = "";
8947 if (freq_range_list_parse(&event.freq_range, param) < 0)
8948 return -1;
8949 wpa_supplicant_event(wpa_s, ev, &event);
8950 os_free(event.freq_range.range);
8951 return 0;
e4a3e1d0
JM
8952 } else if (os_strcmp(cmd, "SCAN_RES") == 0) {
8953 return wpas_ctrl_iface_driver_scan_res(wpa_s, param);
ad12f2f4
JM
8954 } else {
8955 wpa_dbg(wpa_s, MSG_DEBUG, "Testing - unknown driver event: %s",
8956 cmd);
8957 return -1;
8958 }
8959
8960 wpa_supplicant_event(wpa_s, ev, &event);
8961
8962 return 0;
8963}
8964
9d4ff04a
JM
8965
8966static int wpas_ctrl_iface_eapol_rx(struct wpa_supplicant *wpa_s, char *cmd)
8967{
8968 char *pos;
8969 u8 src[ETH_ALEN], *buf;
8970 int used;
8971 size_t len;
8972
8973 wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
8974
8975 pos = cmd;
8976 used = hwaddr_aton2(pos, src);
8977 if (used < 0)
8978 return -1;
8979 pos += used;
8980 while (*pos == ' ')
8981 pos++;
8982
8983 len = os_strlen(pos);
8984 if (len & 1)
8985 return -1;
8986 len /= 2;
8987
8988 buf = os_malloc(len);
8989 if (buf == NULL)
8990 return -1;
8991
8992 if (hexstr2bin(pos, buf, len) < 0) {
8993 os_free(buf);
8994 return -1;
8995 }
8996
8997 wpa_supplicant_rx_eapol(wpa_s, src, buf, len);
8998 os_free(buf);
8999
9000 return 0;
9001}
9002
4a6cc862
JM
9003
9004static u16 ipv4_hdr_checksum(const void *buf, size_t len)
9005{
9006 size_t i;
9007 u32 sum = 0;
9008 const u16 *pos = buf;
9009
9010 for (i = 0; i < len / 2; i++)
9011 sum += *pos++;
9012
9013 while (sum >> 16)
9014 sum = (sum & 0xffff) + (sum >> 16);
9015
9016 return sum ^ 0xffff;
9017}
9018
9019
9020#define HWSIM_PACKETLEN 1500
9021#define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
9022
0dbe22be
JM
9023static void wpas_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf,
9024 size_t len)
4a6cc862
JM
9025{
9026 struct wpa_supplicant *wpa_s = ctx;
9027 const struct ether_header *eth;
a8b00423 9028 struct ip ip;
4a6cc862
JM
9029 const u8 *pos;
9030 unsigned int i;
93701b4a 9031 char extra[30];
4a6cc862 9032
93701b4a
JM
9033 if (len < sizeof(*eth) + sizeof(ip) || len > HWSIM_PACKETLEN) {
9034 wpa_printf(MSG_DEBUG,
9035 "test data: RX - ignore unexpected length %d",
9036 (int) len);
4a6cc862 9037 return;
93701b4a 9038 }
4a6cc862
JM
9039
9040 eth = (const struct ether_header *) buf;
75352270
JM
9041 os_memcpy(&ip, eth + 1, sizeof(ip));
9042 pos = &buf[sizeof(*eth) + sizeof(ip)];
4a6cc862 9043
a8b00423 9044 if (ip.ip_hl != 5 || ip.ip_v != 4 || ntohs(ip.ip_len) > HWSIM_IP_LEN) {
93701b4a
JM
9045 wpa_printf(MSG_DEBUG,
9046 "test data: RX - ignore unexpect IP header");
4a6cc862 9047 return;
93701b4a 9048 }
4a6cc862 9049
a8b00423 9050 for (i = 0; i < ntohs(ip.ip_len) - sizeof(ip); i++) {
93701b4a
JM
9051 if (*pos != (u8) i) {
9052 wpa_printf(MSG_DEBUG,
9053 "test data: RX - ignore mismatching payload");
4a6cc862 9054 return;
93701b4a 9055 }
4a6cc862
JM
9056 pos++;
9057 }
93701b4a 9058 extra[0] = '\0';
a8b00423
RM
9059 if (ntohs(ip.ip_len) != HWSIM_IP_LEN)
9060 os_snprintf(extra, sizeof(extra), " len=%d", ntohs(ip.ip_len));
93701b4a
JM
9061 wpa_msg(wpa_s, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR "%s",
9062 MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost), extra);
4a6cc862
JM
9063}
9064
9065
9066static int wpas_ctrl_iface_data_test_config(struct wpa_supplicant *wpa_s,
9067 char *cmd)
9068{
9069 int enabled = atoi(cmd);
ba91e920
MB
9070 char *pos;
9071 const char *ifname;
4a6cc862
JM
9072
9073 if (!enabled) {
9074 if (wpa_s->l2_test) {
9075 l2_packet_deinit(wpa_s->l2_test);
9076 wpa_s->l2_test = NULL;
9077 wpa_dbg(wpa_s, MSG_DEBUG, "test data: Disabled");
9078 }
9079 return 0;
9080 }
9081
9082 if (wpa_s->l2_test)
9083 return 0;
9084
ba91e920
MB
9085 pos = os_strstr(cmd, " ifname=");
9086 if (pos)
9087 ifname = pos + 8;
9088 else
9089 ifname = wpa_s->ifname;
9090
9091 wpa_s->l2_test = l2_packet_init(ifname, wpa_s->own_addr,
4a6cc862
JM
9092 ETHERTYPE_IP, wpas_data_test_rx,
9093 wpa_s, 1);
9094 if (wpa_s->l2_test == NULL)
9095 return -1;
9096
9097 wpa_dbg(wpa_s, MSG_DEBUG, "test data: Enabled");
9098
9099 return 0;
9100}
9101
9102
9103static int wpas_ctrl_iface_data_test_tx(struct wpa_supplicant *wpa_s, char *cmd)
9104{
9105 u8 dst[ETH_ALEN], src[ETH_ALEN];
93701b4a 9106 char *pos, *pos2;
4a6cc862
JM
9107 int used;
9108 long int val;
9109 u8 tos;
75352270 9110 u8 buf[2 + HWSIM_PACKETLEN];
4a6cc862 9111 struct ether_header *eth;
a8b00423 9112 struct ip *ip;
4a6cc862
JM
9113 u8 *dpos;
9114 unsigned int i;
93701b4a 9115 size_t send_len = HWSIM_IP_LEN;
4a6cc862
JM
9116
9117 if (wpa_s->l2_test == NULL)
9118 return -1;
9119
93701b4a 9120 /* format: <dst> <src> <tos> [len=<length>] */
4a6cc862
JM
9121
9122 pos = cmd;
9123 used = hwaddr_aton2(pos, dst);
9124 if (used < 0)
9125 return -1;
9126 pos += used;
9127 while (*pos == ' ')
9128 pos++;
9129 used = hwaddr_aton2(pos, src);
9130 if (used < 0)
9131 return -1;
9132 pos += used;
9133
93701b4a 9134 val = strtol(pos, &pos2, 0);
4a6cc862
JM
9135 if (val < 0 || val > 0xff)
9136 return -1;
9137 tos = val;
9138
93701b4a
JM
9139 pos = os_strstr(pos2, " len=");
9140 if (pos) {
9141 i = atoi(pos + 5);
9142 if (i < sizeof(*ip) || i > HWSIM_IP_LEN)
9143 return -1;
9144 send_len = i;
9145 }
9146
75352270 9147 eth = (struct ether_header *) &buf[2];
4a6cc862
JM
9148 os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
9149 os_memcpy(eth->ether_shost, src, ETH_ALEN);
9150 eth->ether_type = htons(ETHERTYPE_IP);
a8b00423 9151 ip = (struct ip *) (eth + 1);
4a6cc862 9152 os_memset(ip, 0, sizeof(*ip));
a8b00423
RM
9153 ip->ip_hl = 5;
9154 ip->ip_v = 4;
9155 ip->ip_ttl = 64;
9156 ip->ip_tos = tos;
9157 ip->ip_len = htons(send_len);
9158 ip->ip_p = 1;
9159 ip->ip_src.s_addr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
9160 ip->ip_dst.s_addr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
9161 ip->ip_sum = ipv4_hdr_checksum(ip, sizeof(*ip));
4a6cc862 9162 dpos = (u8 *) (ip + 1);
93701b4a 9163 for (i = 0; i < send_len - sizeof(*ip); i++)
4a6cc862
JM
9164 *dpos++ = i;
9165
75352270 9166 if (l2_packet_send(wpa_s->l2_test, dst, ETHERTYPE_IP, &buf[2],
93701b4a 9167 sizeof(struct ether_header) + send_len) < 0)
4a6cc862
JM
9168 return -1;
9169
9170 wpa_dbg(wpa_s, MSG_DEBUG, "test data: TX dst=" MACSTR " src=" MACSTR
9171 " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
9172
9173 return 0;
9174}
9175
fc0ef7c0
JM
9176
9177static int wpas_ctrl_iface_data_test_frame(struct wpa_supplicant *wpa_s,
9178 char *cmd)
9179{
9180 u8 *buf;
9181 struct ether_header *eth;
9182 struct l2_packet_data *l2 = NULL;
9183 size_t len;
9184 u16 ethertype;
9185 int res = -1;
9186
9187 len = os_strlen(cmd);
9188 if (len & 1 || len < ETH_HLEN * 2)
9189 return -1;
9190 len /= 2;
9191
9192 buf = os_malloc(len);
9193 if (buf == NULL)
9194 return -1;
9195
9196 if (hexstr2bin(cmd, buf, len) < 0)
9197 goto done;
9198
9199 eth = (struct ether_header *) buf;
9200 ethertype = ntohs(eth->ether_type);
9201
9202 l2 = l2_packet_init(wpa_s->ifname, wpa_s->own_addr, ethertype,
9203 wpas_data_test_rx, wpa_s, 1);
9204 if (l2 == NULL)
9205 goto done;
9206
9207 res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
9208 wpa_dbg(wpa_s, MSG_DEBUG, "test data: TX frame res=%d", res);
9209done:
9210 if (l2)
9211 l2_packet_deinit(l2);
9212 os_free(buf);
9213
9214 return res < 0 ? -1 : 0;
9215}
9216
a156ffda
JM
9217
9218static int wpas_ctrl_test_alloc_fail(struct wpa_supplicant *wpa_s, char *cmd)
9219{
9220#ifdef WPA_TRACE_BFD
a156ffda
JM
9221 char *pos;
9222
9223 wpa_trace_fail_after = atoi(cmd);
9224 pos = os_strchr(cmd, ':');
9225 if (pos) {
9226 pos++;
9227 os_strlcpy(wpa_trace_fail_func, pos,
9228 sizeof(wpa_trace_fail_func));
9229 } else {
9230 wpa_trace_fail_after = 0;
9231 }
9232 return 0;
9233#else /* WPA_TRACE_BFD */
9234 return -1;
9235#endif /* WPA_TRACE_BFD */
9236}
9237
9238
9239static int wpas_ctrl_get_alloc_fail(struct wpa_supplicant *wpa_s,
9240 char *buf, size_t buflen)
9241{
9242#ifdef WPA_TRACE_BFD
a156ffda
JM
9243 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after,
9244 wpa_trace_fail_func);
9245#else /* WPA_TRACE_BFD */
9246 return -1;
9247#endif /* WPA_TRACE_BFD */
9248}
9249
2da52565
JM
9250
9251static int wpas_ctrl_test_fail(struct wpa_supplicant *wpa_s, char *cmd)
9252{
9253#ifdef WPA_TRACE_BFD
2da52565
JM
9254 char *pos;
9255
9256 wpa_trace_test_fail_after = atoi(cmd);
9257 pos = os_strchr(cmd, ':');
9258 if (pos) {
9259 pos++;
9260 os_strlcpy(wpa_trace_test_fail_func, pos,
9261 sizeof(wpa_trace_test_fail_func));
9262 } else {
9263 wpa_trace_test_fail_after = 0;
9264 }
9265 return 0;
9266#else /* WPA_TRACE_BFD */
9267 return -1;
9268#endif /* WPA_TRACE_BFD */
9269}
9270
9271
9272static int wpas_ctrl_get_fail(struct wpa_supplicant *wpa_s,
9273 char *buf, size_t buflen)
9274{
9275#ifdef WPA_TRACE_BFD
2da52565
JM
9276 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_test_fail_after,
9277 wpa_trace_test_fail_func);
9278#else /* WPA_TRACE_BFD */
9279 return -1;
9280#endif /* WPA_TRACE_BFD */
9281}
9282
a530fe77
JM
9283
9284static void wpas_ctrl_event_test_cb(void *eloop_ctx, void *timeout_ctx)
9285{
9286 struct wpa_supplicant *wpa_s = eloop_ctx;
9287 int i, count = (intptr_t) timeout_ctx;
9288
9289 wpa_printf(MSG_DEBUG, "TEST: Send %d control interface event messages",
9290 count);
9291 for (i = 0; i < count; i++) {
9292 wpa_msg_ctrl(wpa_s, MSG_INFO, "TEST-EVENT-MESSAGE %d/%d",
9293 i + 1, count);
9294 }
9295}
9296
9297
9298static int wpas_ctrl_event_test(struct wpa_supplicant *wpa_s, const char *cmd)
9299{
9300 int count;
9301
9302 count = atoi(cmd);
9303 if (count <= 0)
9304 return -1;
9305
9306 return eloop_register_timeout(0, 0, wpas_ctrl_event_test_cb, wpa_s,
9307 (void *) (intptr_t) count);
9308}
9309
651c6a84
JM
9310
9311static int wpas_ctrl_test_assoc_ie(struct wpa_supplicant *wpa_s,
9312 const char *cmd)
9313{
9314 struct wpabuf *buf;
9315 size_t len;
9316
9317 len = os_strlen(cmd);
9318 if (len & 1)
9319 return -1;
9320 len /= 2;
9321
9322 if (len == 0) {
9323 buf = NULL;
9324 } else {
9325 buf = wpabuf_alloc(len);
9326 if (buf == NULL)
9327 return -1;
9328
9329 if (hexstr2bin(cmd, wpabuf_put(buf, len), len) < 0) {
9330 wpabuf_free(buf);
9331 return -1;
9332 }
9333 }
9334
9335 wpa_sm_set_test_assoc_ie(wpa_s->wpa, buf);
9336 return 0;
9337}
9338
16579769
JM
9339
9340static int wpas_ctrl_reset_pn(struct wpa_supplicant *wpa_s)
9341{
9342 u8 zero[WPA_TK_MAX_LEN];
9343
9344 if (wpa_s->last_tk_alg == WPA_ALG_NONE)
9345 return -1;
9346
9347 wpa_printf(MSG_INFO, "TESTING: Reset PN");
9348 os_memset(zero, 0, sizeof(zero));
9349
9350 /* First, use a zero key to avoid any possible duplicate key avoidance
9351 * in the driver. */
9352 if (wpa_drv_set_key(wpa_s, wpa_s->last_tk_alg, wpa_s->last_tk_addr,
9353 wpa_s->last_tk_key_idx, 1, zero, 6,
9354 zero, wpa_s->last_tk_len) < 0)
9355 return -1;
9356
9357 /* Set the previously configured key to reset its TSC/RSC */
9358 return wpa_drv_set_key(wpa_s, wpa_s->last_tk_alg, wpa_s->last_tk_addr,
9359 wpa_s->last_tk_key_idx, 1, zero, 6,
9360 wpa_s->last_tk, wpa_s->last_tk_len);
9361}
9362
751f5b29
JM
9363
9364static int wpas_ctrl_key_request(struct wpa_supplicant *wpa_s, const char *cmd)
9365{
9366 const char *pos = cmd;
9367 int error, pairwise;
9368
9369 error = atoi(pos);
9370 pos = os_strchr(pos, ' ');
9371 if (!pos)
9372 return -1;
9373 pairwise = atoi(pos);
9374 wpa_sm_key_request(wpa_s->wpa, error, pairwise);
9375 return 0;
9376}
9377
daa40960
JM
9378
9379static int wpas_ctrl_resend_assoc(struct wpa_supplicant *wpa_s)
9380{
9381#ifdef CONFIG_SME
9382 struct wpa_driver_associate_params params;
9383 int ret;
9384
9385 os_memset(&params, 0, sizeof(params));
9386 params.bssid = wpa_s->bssid;
9387 params.ssid = wpa_s->sme.ssid;
9388 params.ssid_len = wpa_s->sme.ssid_len;
9389 params.freq.freq = wpa_s->sme.freq;
9390 if (wpa_s->last_assoc_req_wpa_ie) {
9391 params.wpa_ie = wpabuf_head(wpa_s->last_assoc_req_wpa_ie);
9392 params.wpa_ie_len = wpabuf_len(wpa_s->last_assoc_req_wpa_ie);
9393 }
9394 params.pairwise_suite = wpa_s->pairwise_cipher;
9395 params.group_suite = wpa_s->group_cipher;
9396 params.mgmt_group_suite = wpa_s->mgmt_group_cipher;
9397 params.key_mgmt_suite = wpa_s->key_mgmt;
9398 params.wpa_proto = wpa_s->wpa_proto;
9399 params.mgmt_frame_protection = wpa_s->sme.mfp;
9400 params.rrm_used = wpa_s->rrm.rrm_used;
9401 if (wpa_s->sme.prev_bssid_set)
9402 params.prev_bssid = wpa_s->sme.prev_bssid;
9403 wpa_printf(MSG_INFO, "TESTING: Resend association request");
9404 ret = wpa_drv_associate(wpa_s, &params);
9405 wpa_s->testing_resend_assoc = 1;
9406 return ret;
9407#else /* CONFIG_SME */
9408 return -1;
9409#endif /* CONFIG_SME */
9410}
9411
60b893df
JM
9412#endif /* CONFIG_TESTING_OPTIONS */
9413
9414
86bd36f0
JM
9415static int wpas_ctrl_vendor_elem_add(struct wpa_supplicant *wpa_s, char *cmd)
9416{
9417 char *pos = cmd;
9418 int frame;
9419 size_t len;
9420 struct wpabuf *buf;
9421 struct ieee802_11_elems elems;
9422
9423 frame = atoi(pos);
9424 if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
9425 return -1;
af041f99 9426 wpa_s = wpas_vendor_elem(wpa_s, frame);
86bd36f0
JM
9427
9428 pos = os_strchr(pos, ' ');
9429 if (pos == NULL)
9430 return -1;
9431 pos++;
9432
9433 len = os_strlen(pos);
9434 if (len == 0)
9435 return 0;
9436 if (len & 1)
9437 return -1;
9438 len /= 2;
9439
9440 buf = wpabuf_alloc(len);
9441 if (buf == NULL)
9442 return -1;
9443
9444 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
9445 wpabuf_free(buf);
9446 return -1;
9447 }
9448
9449 if (ieee802_11_parse_elems(wpabuf_head_u8(buf), len, &elems, 0) ==
9450 ParseFailed) {
9451 wpabuf_free(buf);
9452 return -1;
9453 }
9454
9455 if (wpa_s->vendor_elem[frame] == NULL) {
9456 wpa_s->vendor_elem[frame] = buf;
af041f99 9457 wpas_vendor_elem_update(wpa_s);
86bd36f0
JM
9458 return 0;
9459 }
9460
9461 if (wpabuf_resize(&wpa_s->vendor_elem[frame], len) < 0) {
9462 wpabuf_free(buf);
9463 return -1;
9464 }
9465
9466 wpabuf_put_buf(wpa_s->vendor_elem[frame], buf);
9467 wpabuf_free(buf);
af041f99 9468 wpas_vendor_elem_update(wpa_s);
86bd36f0
JM
9469
9470 return 0;
9471}
9472
9473
9474static int wpas_ctrl_vendor_elem_get(struct wpa_supplicant *wpa_s, char *cmd,
9475 char *buf, size_t buflen)
9476{
9477 int frame = atoi(cmd);
9478
9479 if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
9480 return -1;
af041f99 9481 wpa_s = wpas_vendor_elem(wpa_s, frame);
86bd36f0
JM
9482
9483 if (wpa_s->vendor_elem[frame] == NULL)
9484 return 0;
9485
9486 return wpa_snprintf_hex(buf, buflen,
9487 wpabuf_head_u8(wpa_s->vendor_elem[frame]),
9488 wpabuf_len(wpa_s->vendor_elem[frame]));
9489}
9490
9491
9492static int wpas_ctrl_vendor_elem_remove(struct wpa_supplicant *wpa_s, char *cmd)
9493{
9494 char *pos = cmd;
9495 int frame;
9496 size_t len;
9497 u8 *buf;
9498 struct ieee802_11_elems elems;
af041f99 9499 int res;
86bd36f0
JM
9500
9501 frame = atoi(pos);
9502 if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
9503 return -1;
af041f99 9504 wpa_s = wpas_vendor_elem(wpa_s, frame);
86bd36f0
JM
9505
9506 pos = os_strchr(pos, ' ');
9507 if (pos == NULL)
9508 return -1;
9509 pos++;
9510
9511 if (*pos == '*') {
9512 wpabuf_free(wpa_s->vendor_elem[frame]);
9513 wpa_s->vendor_elem[frame] = NULL;
af041f99 9514 wpas_vendor_elem_update(wpa_s);
86bd36f0
JM
9515 return 0;
9516 }
9517
9518 if (wpa_s->vendor_elem[frame] == NULL)
9519 return -1;
9520
9521 len = os_strlen(pos);
9522 if (len == 0)
9523 return 0;
9524 if (len & 1)
9525 return -1;
9526 len /= 2;
9527
9528 buf = os_malloc(len);
9529 if (buf == NULL)
9530 return -1;
9531
9532 if (hexstr2bin(pos, buf, len) < 0) {
9533 os_free(buf);
9534 return -1;
9535 }
9536
9537 if (ieee802_11_parse_elems(buf, len, &elems, 0) == ParseFailed) {
9538 os_free(buf);
9539 return -1;
9540 }
9541
af041f99 9542 res = wpas_vendor_elem_remove(wpa_s, frame, buf, len);
86bd36f0 9543 os_free(buf);
af041f99 9544 return res;
86bd36f0
JM
9545}
9546
9547
f4b8bfae
AK
9548static void wpas_ctrl_neighbor_rep_cb(void *ctx, struct wpabuf *neighbor_rep)
9549{
9550 struct wpa_supplicant *wpa_s = ctx;
cf667c66
IP
9551 size_t len;
9552 const u8 *data;
f4b8bfae 9553
cf667c66
IP
9554 /*
9555 * Neighbor Report element (IEEE P802.11-REVmc/D5.0)
9556 * BSSID[6]
9557 * BSSID Information[4]
9558 * Operating Class[1]
9559 * Channel Number[1]
9560 * PHY Type[1]
9561 * Optional Subelements[variable]
9562 */
9563#define NR_IE_MIN_LEN (ETH_ALEN + 4 + 1 + 1 + 1)
9564
9565 if (!neighbor_rep || wpabuf_len(neighbor_rep) == 0) {
f4b8bfae 9566 wpa_msg_ctrl(wpa_s, MSG_INFO, RRM_EVENT_NEIGHBOR_REP_FAILED);
cf667c66
IP
9567 goto out;
9568 }
9569
9570 data = wpabuf_head_u8(neighbor_rep);
9571 len = wpabuf_len(neighbor_rep);
9572
9573 while (len >= 2 + NR_IE_MIN_LEN) {
9574 const u8 *nr;
9575 char lci[256 * 2 + 1];
9576 char civic[256 * 2 + 1];
9577 u8 nr_len = data[1];
9578 const u8 *pos = data, *end;
9579
9580 if (pos[0] != WLAN_EID_NEIGHBOR_REPORT ||
9581 nr_len < NR_IE_MIN_LEN) {
bf3ab50f
BG
9582 wpa_dbg(wpa_s, MSG_DEBUG,
9583 "CTRL: Invalid Neighbor Report element: id=%u len=%u",
9584 data[0], nr_len);
cf667c66
IP
9585 goto out;
9586 }
9587
9588 if (2U + nr_len > len) {
bf3ab50f
BG
9589 wpa_dbg(wpa_s, MSG_DEBUG,
9590 "CTRL: Invalid Neighbor Report element: id=%u len=%zu nr_len=%u",
9591 data[0], len, nr_len);
cf667c66
IP
9592 goto out;
9593 }
9594 pos += 2;
9595 end = pos + nr_len;
9596
9597 nr = pos;
9598 pos += NR_IE_MIN_LEN;
9599
9600 lci[0] = '\0';
9601 civic[0] = '\0';
9602 while (end - pos > 2) {
9603 u8 s_id, s_len;
9604
9605 s_id = *pos++;
9606 s_len = *pos++;
9607 if (s_len > end - pos)
9608 goto out;
9609 if (s_id == WLAN_EID_MEASURE_REPORT && s_len > 3) {
9610 /* Measurement Token[1] */
9611 /* Measurement Report Mode[1] */
9612 /* Measurement Type[1] */
9613 /* Measurement Report[variable] */
9614 switch (pos[2]) {
9615 case MEASURE_TYPE_LCI:
9616 if (lci[0])
9617 break;
9618 wpa_snprintf_hex(lci, sizeof(lci),
9619 pos, s_len);
9620 break;
9621 case MEASURE_TYPE_LOCATION_CIVIC:
9622 if (civic[0])
9623 break;
9624 wpa_snprintf_hex(civic, sizeof(civic),
9625 pos, s_len);
9626 break;
9627 }
9628 }
9629
9630 pos += s_len;
9631 }
9632
9633 wpa_msg(wpa_s, MSG_INFO, RRM_EVENT_NEIGHBOR_REP_RXED
9634 "bssid=" MACSTR
9635 " info=0x%x op_class=%u chan=%u phy_type=%u%s%s%s%s",
9636 MAC2STR(nr), WPA_GET_LE32(nr + ETH_ALEN),
9637 nr[ETH_ALEN + 4], nr[ETH_ALEN + 5],
9638 nr[ETH_ALEN + 6],
9639 lci[0] ? " lci=" : "", lci,
9640 civic[0] ? " civic=" : "", civic);
9641
9642 data = end;
9643 len -= 2 + nr_len;
f4b8bfae 9644 }
cf667c66
IP
9645
9646out:
9647 wpabuf_free(neighbor_rep);
f4b8bfae
AK
9648}
9649
9650
6a4f0ed7
JM
9651static int wpas_ctrl_iface_send_neighbor_rep(struct wpa_supplicant *wpa_s,
9652 char *cmd)
f4b8bfae 9653{
d41a5352
DS
9654 struct wpa_ssid_value ssid, *ssid_p = NULL;
9655 int ret, lci = 0, civic = 0;
9656 char *ssid_s;
4c4b2305 9657
d41a5352
DS
9658 ssid_s = os_strstr(cmd, "ssid=");
9659 if (ssid_s) {
9660 if (ssid_parse(ssid_s + 5, &ssid)) {
bf3ab50f
BG
9661 wpa_msg(wpa_s, MSG_INFO,
9662 "CTRL: Send Neighbor Report: bad SSID");
4c4b2305 9663 return -1;
d41a5352
DS
9664 }
9665
4c4b2305 9666 ssid_p = &ssid;
d41a5352
DS
9667
9668 /*
9669 * Move cmd after the SSID text that may include "lci" or
9670 * "civic".
9671 */
9672 cmd = os_strchr(ssid_s + 6, ssid_s[5] == '"' ? '"' : ' ');
9673 if (cmd)
9674 cmd++;
9675
4c4b2305
AK
9676 }
9677
d41a5352
DS
9678 if (cmd && os_strstr(cmd, "lci"))
9679 lci = 1;
9680
9681 if (cmd && os_strstr(cmd, "civic"))
9682 civic = 1;
9683
9684 ret = wpas_rrm_send_neighbor_rep_request(wpa_s, ssid_p, lci, civic,
4c4b2305
AK
9685 wpas_ctrl_neighbor_rep_cb,
9686 wpa_s);
9687
9688 return ret;
f4b8bfae
AK
9689}
9690
9691
65d9a5e2
JM
9692static int wpas_ctrl_iface_erp_flush(struct wpa_supplicant *wpa_s)
9693{
9694 eapol_sm_erp_flush(wpa_s->eapol);
9695 return 0;
9696}
9697
9698
fb375883
IP
9699static int wpas_ctrl_iface_mac_rand_scan(struct wpa_supplicant *wpa_s,
9700 char *cmd)
9701{
9702 char *token, *context = NULL;
9703 unsigned int enable = ~0, type = 0;
9704 u8 _addr[ETH_ALEN], _mask[ETH_ALEN];
9705 u8 *addr = NULL, *mask = NULL;
9706
9707 while ((token = str_token(cmd, " ", &context))) {
9708 if (os_strcasecmp(token, "scan") == 0) {
9709 type |= MAC_ADDR_RAND_SCAN;
9710 } else if (os_strcasecmp(token, "sched") == 0) {
9711 type |= MAC_ADDR_RAND_SCHED_SCAN;
9712 } else if (os_strcasecmp(token, "pno") == 0) {
9713 type |= MAC_ADDR_RAND_PNO;
9714 } else if (os_strcasecmp(token, "all") == 0) {
9715 type = wpa_s->mac_addr_rand_supported;
9716 } else if (os_strncasecmp(token, "enable=", 7) == 0) {
9717 enable = atoi(token + 7);
9718 } else if (os_strncasecmp(token, "addr=", 5) == 0) {
9719 addr = _addr;
9720 if (hwaddr_aton(token + 5, addr)) {
9721 wpa_printf(MSG_INFO,
9722 "CTRL: Invalid MAC address: %s",
9723 token);
9724 return -1;
9725 }
9726 } else if (os_strncasecmp(token, "mask=", 5) == 0) {
9727 mask = _mask;
9728 if (hwaddr_aton(token + 5, mask)) {
9729 wpa_printf(MSG_INFO,
9730 "CTRL: Invalid MAC address mask: %s",
9731 token);
9732 return -1;
9733 }
9734 } else {
9735 wpa_printf(MSG_INFO,
9736 "CTRL: Invalid MAC_RAND_SCAN parameter: %s",
9737 token);
9738 return -1;
9739 }
9740 }
9741
9742 if (!type) {
9743 wpa_printf(MSG_INFO, "CTRL: MAC_RAND_SCAN no type specified");
9744 return -1;
9745 }
9746
fb375883
IP
9747 if (enable > 1) {
9748 wpa_printf(MSG_INFO,
9749 "CTRL: MAC_RAND_SCAN enable=<0/1> not specified");
9750 return -1;
9751 }
9752
91b6eba7
EC
9753 if (!enable)
9754 return wpas_disable_mac_addr_randomization(wpa_s, type);
fb375883 9755
91b6eba7 9756 return wpas_enable_mac_addr_randomization(wpa_s, type, addr, mask);
fb375883
IP
9757}
9758
9759
b8daac18
MH
9760static int wpas_ctrl_iface_pmksa(struct wpa_supplicant *wpa_s,
9761 char *buf, size_t buflen)
9762{
9763 size_t reply_len;
9764
9765 reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, buf, buflen);
9766#ifdef CONFIG_AP
9767 reply_len += wpas_ap_pmksa_cache_list(wpa_s, &buf[reply_len],
9768 buflen - reply_len);
9769#endif /* CONFIG_AP */
9770 return reply_len;
9771}
9772
9773
4c522c77
MH
9774static void wpas_ctrl_iface_pmksa_flush(struct wpa_supplicant *wpa_s)
9775{
9776 wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
9777#ifdef CONFIG_AP
9778 wpas_ap_pmksa_cache_flush(wpa_s);
9779#endif /* CONFIG_AP */
9780}
9781
9782
3459381d
JM
9783#ifdef CONFIG_PMKSA_CACHE_EXTERNAL
9784
9785static int wpas_ctrl_iface_pmksa_get(struct wpa_supplicant *wpa_s,
9786 const char *cmd, char *buf, size_t buflen)
9787{
9788 struct rsn_pmksa_cache_entry *entry;
9789 struct wpa_ssid *ssid;
9790 char *pos, *pos2, *end;
9791 int ret;
9792 struct os_reltime now;
9793
9794 ssid = wpa_config_get_network(wpa_s->conf, atoi(cmd));
9795 if (!ssid)
9796 return -1;
9797
9798 pos = buf;
9799 end = buf + buflen;
9800
9801 os_get_reltime(&now);
9802
9803 /*
9804 * Entry format:
9805 * <BSSID> <PMKID> <PMK> <reauth_time in seconds>
9806 * <expiration in seconds> <akmp> <opportunistic>
b7286c1b 9807 * [FILS Cache Identifier]
3459381d
JM
9808 */
9809
9810 for (entry = wpa_sm_pmksa_cache_head(wpa_s->wpa); entry;
9811 entry = entry->next) {
9812 if (entry->network_ctx != ssid)
9813 continue;
9814
9815 pos2 = pos;
9816 ret = os_snprintf(pos2, end - pos2, MACSTR " ",
9817 MAC2STR(entry->aa));
9818 if (os_snprintf_error(end - pos2, ret))
9819 break;
9820 pos2 += ret;
9821
9822 pos2 += wpa_snprintf_hex(pos2, end - pos2, entry->pmkid,
9823 PMKID_LEN);
9824
9825 ret = os_snprintf(pos2, end - pos2, " ");
9826 if (os_snprintf_error(end - pos2, ret))
9827 break;
9828 pos2 += ret;
9829
9830 pos2 += wpa_snprintf_hex(pos2, end - pos2, entry->pmk,
9831 entry->pmk_len);
9832
9833 ret = os_snprintf(pos2, end - pos2, " %d %d %d %d",
9834 (int) (entry->reauth_time - now.sec),
9835 (int) (entry->expiration - now.sec),
9836 entry->akmp,
9837 entry->opportunistic);
9838 if (os_snprintf_error(end - pos2, ret))
9839 break;
9840 pos2 += ret;
9841
b7286c1b
JM
9842 if (entry->fils_cache_id_set) {
9843 ret = os_snprintf(pos2, end - pos2, " %02x%02x",
9844 entry->fils_cache_id[0],
9845 entry->fils_cache_id[1]);
9846 if (os_snprintf_error(end - pos2, ret))
9847 break;
9848 pos2 += ret;
9849 }
9850
3459381d
JM
9851 ret = os_snprintf(pos2, end - pos2, "\n");
9852 if (os_snprintf_error(end - pos2, ret))
9853 break;
9854 pos2 += ret;
9855
9856 pos = pos2;
9857 }
9858
9859 return pos - buf;
9860}
9861
9862
9863static int wpas_ctrl_iface_pmksa_add(struct wpa_supplicant *wpa_s,
9864 char *cmd)
9865{
9866 struct rsn_pmksa_cache_entry *entry;
9867 struct wpa_ssid *ssid;
9868 char *pos, *pos2;
9869 int ret = -1;
9870 struct os_reltime now;
b7286c1b 9871 int reauth_time = 0, expiration = 0, i;
3459381d
JM
9872
9873 /*
9874 * Entry format:
9875 * <network_id> <BSSID> <PMKID> <PMK> <reauth_time in seconds>
9876 * <expiration in seconds> <akmp> <opportunistic>
b7286c1b 9877 * [FILS Cache Identifier]
3459381d
JM
9878 */
9879
9880 ssid = wpa_config_get_network(wpa_s->conf, atoi(cmd));
9881 if (!ssid)
9882 return -1;
9883
9884 pos = os_strchr(cmd, ' ');
9885 if (!pos)
9886 return -1;
9887 pos++;
9888
9889 entry = os_zalloc(sizeof(*entry));
9890 if (!entry)
9891 return -1;
9892
9893 if (hwaddr_aton(pos, entry->aa))
9894 goto fail;
9895
9896 pos = os_strchr(pos, ' ');
9897 if (!pos)
9898 goto fail;
9899 pos++;
9900
9901 if (hexstr2bin(pos, entry->pmkid, PMKID_LEN) < 0)
9902 goto fail;
9903
9904 pos = os_strchr(pos, ' ');
9905 if (!pos)
9906 goto fail;
9907 pos++;
9908
9909 pos2 = os_strchr(pos, ' ');
9910 if (!pos2)
9911 goto fail;
9912 entry->pmk_len = (pos2 - pos) / 2;
9913 if (entry->pmk_len < PMK_LEN || entry->pmk_len > PMK_LEN_MAX ||
9914 hexstr2bin(pos, entry->pmk, entry->pmk_len) < 0)
9915 goto fail;
9916
9917 pos = os_strchr(pos, ' ');
9918 if (!pos)
9919 goto fail;
9920 pos++;
9921
9922 if (sscanf(pos, "%d %d %d %d", &reauth_time, &expiration,
9923 &entry->akmp, &entry->opportunistic) != 4)
9924 goto fail;
b7286c1b
JM
9925 for (i = 0; i < 4; i++) {
9926 pos = os_strchr(pos, ' ');
9927 if (!pos) {
9928 if (i < 3)
9929 goto fail;
9930 break;
9931 }
9932 pos++;
9933 }
9934 if (pos) {
9935 if (hexstr2bin(pos, entry->fils_cache_id,
9936 FILS_CACHE_ID_LEN) < 0)
9937 goto fail;
9938 entry->fils_cache_id_set = 1;
9939 }
3459381d
JM
9940 os_get_reltime(&now);
9941 entry->expiration = now.sec + expiration;
9942 entry->reauth_time = now.sec + reauth_time;
9943
9944 entry->network_ctx = ssid;
9945
9946 wpa_sm_pmksa_cache_add_entry(wpa_s->wpa, entry);
9947 entry = NULL;
9948 ret = 0;
9949fail:
9950 os_free(entry);
9951 return ret;
9952}
9953
4d77d80e
MH
9954
9955#ifdef CONFIG_MESH
9956
9957static int wpas_ctrl_iface_mesh_pmksa_get(struct wpa_supplicant *wpa_s,
9958 const char *cmd, char *buf,
9959 size_t buflen)
9960{
9961 u8 spa[ETH_ALEN];
9962
9963 if (!wpa_s->ifmsh)
9964 return -1;
9965
9966 if (os_strcasecmp(cmd, "any") == 0)
9967 return wpas_ap_pmksa_cache_list_mesh(wpa_s, NULL, buf, buflen);
9968
9969 if (hwaddr_aton(cmd, spa))
9970 return -1;
9971
9972 return wpas_ap_pmksa_cache_list_mesh(wpa_s, spa, buf, buflen);
9973}
9974
9975
9976static int wpas_ctrl_iface_mesh_pmksa_add(struct wpa_supplicant *wpa_s,
9977 char *cmd)
9978{
9979 /*
9980 * We do not check mesh interface existance because PMKSA should be
9981 * stored before wpa_s->ifmsh creation to suppress commit message
9982 * creation.
9983 */
9984 return wpas_ap_pmksa_cache_add_external(wpa_s, cmd);
9985}
9986
9987#endif /* CONFIG_MESH */
3459381d
JM
9988#endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
9989
9990
5732b770
JM
9991#ifdef CONFIG_FILS
9992static int wpas_ctrl_iface_fils_hlp_req_add(struct wpa_supplicant *wpa_s,
9993 const char *cmd)
9994{
9995 struct fils_hlp_req *req;
9996 const char *pos;
9997
9998 /* format: <dst> <packet starting from ethertype> */
9999
10000 req = os_zalloc(sizeof(*req));
10001 if (!req)
10002 return -1;
10003
10004 if (hwaddr_aton(cmd, req->dst))
10005 goto fail;
10006
10007 pos = os_strchr(cmd, ' ');
10008 if (!pos)
10009 goto fail;
10010 pos++;
10011 req->pkt = wpabuf_parse_bin(pos);
10012 if (!req->pkt)
10013 goto fail;
10014
10015 dl_list_add_tail(&wpa_s->fils_hlp_req, &req->list);
10016 return 0;
10017fail:
10018 wpabuf_free(req->pkt);
10019 os_free(req);
10020 return -1;
10021}
10022#endif /* CONFIG_FILS */
10023
10024
8db9a79d
JM
10025static int wpas_ctrl_cmd_debug_level(const char *cmd)
10026{
10027 if (os_strcmp(cmd, "PING") == 0 ||
10028 os_strncmp(cmd, "BSS ", 4) == 0 ||
10029 os_strncmp(cmd, "GET_NETWORK ", 12) == 0 ||
10030 os_strncmp(cmd, "STATUS", 6) == 0 ||
10031 os_strncmp(cmd, "STA ", 4) == 0 ||
10032 os_strncmp(cmd, "STA-", 4) == 0)
10033 return MSG_EXCESSIVE;
10034 return MSG_DEBUG;
10035}
10036
10037
6fc6879b
JM
10038char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
10039 char *buf, size_t *resp_len)
10040{
10041 char *reply;
b563b388 10042 const int reply_size = 4096;
6fc6879b
JM
10043 int reply_len;
10044
10045 if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
c10e0ccc
JM
10046 os_strncmp(buf, "SET_NETWORK ", 12) == 0 ||
10047 os_strncmp(buf, "PMKSA_ADD ", 10) == 0 ||
10048 os_strncmp(buf, "MESH_PMKSA_ADD ", 15) == 0) {
d31b5ac7
JM
10049 if (wpa_debug_show_keys)
10050 wpa_dbg(wpa_s, MSG_DEBUG,
10051 "Control interface command '%s'", buf);
10052 else
10053 wpa_dbg(wpa_s, MSG_DEBUG,
10054 "Control interface command '%s [REMOVED]'",
10055 os_strncmp(buf, WPA_CTRL_RSP,
10056 os_strlen(WPA_CTRL_RSP)) == 0 ?
c10e0ccc
JM
10057 WPA_CTRL_RSP :
10058 (os_strncmp(buf, "SET_NETWORK ", 12) == 0 ?
10059 "SET_NETWORK" : "key-add"));
d31b5ac7 10060 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ", 16) == 0 ||
679f2e7c 10061 os_strncmp(buf, "NFC_REPORT_HANDOVER", 19) == 0) {
6fc6879b
JM
10062 wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
10063 (const u8 *) buf, os_strlen(buf));
10064 } else {
8db9a79d 10065 int level = wpas_ctrl_cmd_debug_level(buf);
b470b2bf 10066 wpa_dbg(wpa_s, level, "Control interface command '%s'", buf);
6fc6879b
JM
10067 }
10068
10069 reply = os_malloc(reply_size);
10070 if (reply == NULL) {
10071 *resp_len = 1;
10072 return NULL;
10073 }
10074
10075 os_memcpy(reply, "OK\n", 3);
10076 reply_len = 3;
10077
10078 if (os_strcmp(buf, "PING") == 0) {
10079 os_memcpy(reply, "PONG\n", 5);
10080 reply_len = 5;
0eed2a8d
JD
10081 } else if (os_strcmp(buf, "IFNAME") == 0) {
10082 reply_len = os_strlen(wpa_s->ifname);
10083 os_memcpy(reply, wpa_s->ifname, reply_len);
ac6912b5
BG
10084 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
10085 if (wpa_debug_reopen_file() < 0)
10086 reply_len = -1;
77895cd9
JM
10087 } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
10088 wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
6fc6879b
JM
10089 } else if (os_strcmp(buf, "MIB") == 0) {
10090 reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
10091 if (reply_len >= 0) {
5ac73acf
JM
10092 reply_len += eapol_sm_get_mib(wpa_s->eapol,
10093 reply + reply_len,
10094 reply_size - reply_len);
948ba8c2
JM
10095#ifdef CONFIG_MACSEC
10096 reply_len += ieee802_1x_kay_get_mib(
10097 wpa_s->kay, reply + reply_len,
10098 reply_size - reply_len);
10099#endif /* CONFIG_MACSEC */
6fc6879b
JM
10100 }
10101 } else if (os_strncmp(buf, "STATUS", 6) == 0) {
10102 reply_len = wpa_supplicant_ctrl_iface_status(
10103 wpa_s, buf + 6, reply, reply_size);
10104 } else if (os_strcmp(buf, "PMKSA") == 0) {
b8daac18 10105 reply_len = wpas_ctrl_iface_pmksa(wpa_s, reply, reply_size);
79e2b1cc 10106 } else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
4c522c77 10107 wpas_ctrl_iface_pmksa_flush(wpa_s);
3459381d
JM
10108#ifdef CONFIG_PMKSA_CACHE_EXTERNAL
10109 } else if (os_strncmp(buf, "PMKSA_GET ", 10) == 0) {
10110 reply_len = wpas_ctrl_iface_pmksa_get(wpa_s, buf + 10,
10111 reply, reply_size);
10112 } else if (os_strncmp(buf, "PMKSA_ADD ", 10) == 0) {
10113 if (wpas_ctrl_iface_pmksa_add(wpa_s, buf + 10) < 0)
10114 reply_len = -1;
4d77d80e
MH
10115#ifdef CONFIG_MESH
10116 } else if (os_strncmp(buf, "MESH_PMKSA_GET ", 15) == 0) {
10117 reply_len = wpas_ctrl_iface_mesh_pmksa_get(wpa_s, buf + 15,
10118 reply, reply_size);
10119 } else if (os_strncmp(buf, "MESH_PMKSA_ADD ", 15) == 0) {
10120 if (wpas_ctrl_iface_mesh_pmksa_add(wpa_s, buf + 15) < 0)
10121 reply_len = -1;
10122#endif /* CONFIG_MESH */
3459381d 10123#endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
6fc6879b
JM
10124 } else if (os_strncmp(buf, "SET ", 4) == 0) {
10125 if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
10126 reply_len = -1;
10263dc2
OO
10127 } else if (os_strncmp(buf, "DUMP", 4) == 0) {
10128 reply_len = wpa_config_dump_values(wpa_s->conf,
10129 reply, reply_size);
acec8d32
JM
10130 } else if (os_strncmp(buf, "GET ", 4) == 0) {
10131 reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
10132 reply, reply_size);
6fc6879b
JM
10133 } else if (os_strcmp(buf, "LOGON") == 0) {
10134 eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
10135 } else if (os_strcmp(buf, "LOGOFF") == 0) {
10136 eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
10137 } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
8401a6b0
JM
10138 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
10139 reply_len = -1;
9796a86c
JM
10140 else
10141 wpas_request_connection(wpa_s);
0f44ec8e
PQ
10142 } else if (os_strcmp(buf, "REATTACH") == 0) {
10143 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED ||
10144 !wpa_s->current_ssid)
10145 reply_len = -1;
10146 else {
10147 wpa_s->reattach = 1;
10148 wpas_request_connection(wpa_s);
10149 }
6fc6879b 10150 } else if (os_strcmp(buf, "RECONNECT") == 0) {
8401a6b0
JM
10151 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
10152 reply_len = -1;
9796a86c
JM
10153 else if (wpa_s->disconnected)
10154 wpas_request_connection(wpa_s);
ec717917 10155#ifdef IEEE8021X_EAPOL
6fc6879b
JM
10156 } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
10157 if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
10158 reply_len = -1;
ec717917 10159#endif /* IEEE8021X_EAPOL */
6fc6879b
JM
10160#ifdef CONFIG_IEEE80211R
10161 } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
10162 if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
10163 reply_len = -1;
10164#endif /* CONFIG_IEEE80211R */
fcc60db4
JM
10165#ifdef CONFIG_WPS
10166 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
3152ff42
CWY
10167 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
10168 if (res == -2) {
10169 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
10170 reply_len = 17;
10171 } else if (res)
fcc60db4
JM
10172 reply_len = -1;
10173 } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
3152ff42
CWY
10174 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
10175 if (res == -2) {
10176 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
10177 reply_len = 17;
10178 } else if (res)
fcc60db4
JM
10179 reply_len = -1;
10180 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
10181 reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
10182 reply,
10183 reply_size);
3981cb3c
JM
10184 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
10185 reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
10186 wpa_s, buf + 14, reply, reply_size);
2f9929ff
AC
10187 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
10188 if (wpas_wps_cancel(wpa_s))
10189 reply_len = -1;
71892384 10190#ifdef CONFIG_WPS_NFC
3f2c8ba6
JM
10191 } else if (os_strcmp(buf, "WPS_NFC") == 0) {
10192 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
10193 reply_len = -1;
10194 } else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
10195 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
10196 reply_len = -1;
bbf41865
JM
10197 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
10198 reply_len = wpa_supplicant_ctrl_iface_wps_nfc_config_token(
10199 wpa_s, buf + 21, reply, reply_size);
3f2c8ba6
JM
10200 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
10201 reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
10202 wpa_s, buf + 14, reply, reply_size);
d7645d23
JM
10203 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
10204 if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
10205 buf + 17))
10206 reply_len = -1;
e65552dd
JM
10207 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_REQ ", 21) == 0) {
10208 reply_len = wpas_ctrl_nfc_get_handover_req(
10209 wpa_s, buf + 21, reply, reply_size);
10210 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
10211 reply_len = wpas_ctrl_nfc_get_handover_sel(
10212 wpa_s, buf + 21, reply, reply_size);
e4758827
JM
10213 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
10214 if (wpas_ctrl_nfc_report_handover(wpa_s, buf + 20))
10215 reply_len = -1;
71892384 10216#endif /* CONFIG_WPS_NFC */
fcc60db4
JM
10217 } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
10218 if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
10219 reply_len = -1;
70d84f11
JM
10220#ifdef CONFIG_AP
10221 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
10222 reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
10223 wpa_s, buf + 11, reply, reply_size);
10224#endif /* CONFIG_AP */
72df2f5f 10225#ifdef CONFIG_WPS_ER
e9bcfebf 10226 } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
08486685
JM
10227 if (wpas_wps_er_start(wpa_s, NULL))
10228 reply_len = -1;
10229 } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
10230 if (wpas_wps_er_start(wpa_s, buf + 13))
e9bcfebf
JM
10231 reply_len = -1;
10232 } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
f77cedc1 10233 wpas_wps_er_stop(wpa_s);
72df2f5f
JM
10234 } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
10235 if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
10236 reply_len = -1;
564cd7fa 10237 } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
ed159ad4
JM
10238 int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
10239 if (ret == -2) {
10240 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
10241 reply_len = 17;
10242 } else if (ret == -3) {
10243 os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
10244 reply_len = 18;
10245 } else if (ret == -4) {
10246 os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
10247 reply_len = 20;
10248 } else if (ret)
564cd7fa 10249 reply_len = -1;
e64dcfd5
JM
10250 } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
10251 if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
10252 reply_len = -1;
ef10f473
JM
10253 } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
10254 if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
10255 buf + 18))
10256 reply_len = -1;
7d6640a6
JM
10257 } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
10258 if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
10259 reply_len = -1;
1cea09a9
JM
10260#ifdef CONFIG_WPS_NFC
10261 } else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
10262 reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
10263 wpa_s, buf + 24, reply, reply_size);
10264#endif /* CONFIG_WPS_NFC */
72df2f5f 10265#endif /* CONFIG_WPS_ER */
fcc60db4 10266#endif /* CONFIG_WPS */
11ef8d35
JM
10267#ifdef CONFIG_IBSS_RSN
10268 } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
10269 if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
10270 reply_len = -1;
10271#endif /* CONFIG_IBSS_RSN */
603a3f34 10272#ifdef CONFIG_MESH
5b78493f
MH
10273 } else if (os_strncmp(buf, "MESH_INTERFACE_ADD ", 19) == 0) {
10274 reply_len = wpa_supplicant_ctrl_iface_mesh_interface_add(
10275 wpa_s, buf + 19, reply, reply_size);
10276 } else if (os_strcmp(buf, "MESH_INTERFACE_ADD") == 0) {
10277 reply_len = wpa_supplicant_ctrl_iface_mesh_interface_add(
10278 wpa_s, "", reply, reply_size);
603a3f34
JL
10279 } else if (os_strncmp(buf, "MESH_GROUP_ADD ", 15) == 0) {
10280 if (wpa_supplicant_ctrl_iface_mesh_group_add(wpa_s, buf + 15))
10281 reply_len = -1;
10282 } else if (os_strncmp(buf, "MESH_GROUP_REMOVE ", 18) == 0) {
10283 if (wpa_supplicant_ctrl_iface_mesh_group_remove(wpa_s,
10284 buf + 18))
10285 reply_len = -1;
e174ef34
MH
10286 } else if (os_strncmp(buf, "MESH_PEER_REMOVE ", 17) == 0) {
10287 if (wpa_supplicant_ctrl_iface_mesh_peer_remove(wpa_s, buf + 17))
10288 reply_len = -1;
2604edbf
MH
10289 } else if (os_strncmp(buf, "MESH_PEER_ADD ", 14) == 0) {
10290 if (wpa_supplicant_ctrl_iface_mesh_peer_add(wpa_s, buf + 14))
10291 reply_len = -1;
7ace4328
PKC
10292 } else if (os_strncmp(buf, "MESH_LINK_PROBE ", 16) == 0) {
10293 if (wpa_supplicant_ctrl_iface_mesh_link_probe(wpa_s, buf + 16))
10294 reply_len = -1;
603a3f34 10295#endif /* CONFIG_MESH */
b563b388
JM
10296#ifdef CONFIG_P2P
10297 } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
51775096 10298 if (p2p_ctrl_find(wpa_s, buf + 8))
b563b388
JM
10299 reply_len = -1;
10300 } else if (os_strcmp(buf, "P2P_FIND") == 0) {
10301 if (p2p_ctrl_find(wpa_s, ""))
10302 reply_len = -1;
10303 } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
10304 wpas_p2p_stop_find(wpa_s);
f309c18e
KV
10305 } else if (os_strncmp(buf, "P2P_ASP_PROVISION ", 18) == 0) {
10306 if (p2p_ctrl_asp_provision(wpa_s, buf + 18))
10307 reply_len = -1;
10308 } else if (os_strncmp(buf, "P2P_ASP_PROVISION_RESP ", 23) == 0) {
10309 if (p2p_ctrl_asp_provision_resp(wpa_s, buf + 23))
10310 reply_len = -1;
b563b388
JM
10311 } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
10312 reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
10313 reply_size);
10314 } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
10315 if (p2p_ctrl_listen(wpa_s, buf + 11))
10316 reply_len = -1;
10317 } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
10318 if (p2p_ctrl_listen(wpa_s, ""))
10319 reply_len = -1;
10320 } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
10321 if (wpas_p2p_group_remove(wpa_s, buf + 17))
10322 reply_len = -1;
10323 } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
38dcc86c 10324 if (p2p_ctrl_group_add(wpa_s, ""))
b563b388
JM
10325 reply_len = -1;
10326 } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
10327 if (p2p_ctrl_group_add(wpa_s, buf + 14))
10328 reply_len = -1;
57b38882
PK
10329 } else if (os_strncmp(buf, "P2P_GROUP_MEMBER ", 17) == 0) {
10330 reply_len = p2p_ctrl_group_member(wpa_s, buf + 17, reply,
10331 reply_size);
b563b388
JM
10332 } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
10333 if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
10334 reply_len = -1;
10335 } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
10336 reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
10337 } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
10338 reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
10339 reply_size);
10340 } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
10341 if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
10342 reply_len = -1;
10343 } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
10344 if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
10345 reply_len = -1;
10346 } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
10347 wpas_p2p_sd_service_update(wpa_s);
10348 } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
10349 if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
10350 reply_len = -1;
10351 } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
10352 wpas_p2p_service_flush(wpa_s);
10353 } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
10354 if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
10355 reply_len = -1;
10356 } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
10357 if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
10358 reply_len = -1;
ae9d45f3
KV
10359 } else if (os_strncmp(buf, "P2P_SERVICE_REP ", 16) == 0) {
10360 if (p2p_ctrl_service_replace(wpa_s, buf + 16) < 0)
10361 reply_len = -1;
b563b388
JM
10362 } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
10363 if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
10364 reply_len = -1;
10365 } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
10366 if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
10367 reply_len = -1;
10368 } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
10369 reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
10370 reply_size);
10371 } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
10372 if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
10373 reply_len = -1;
10374 } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
acb54643 10375 p2p_ctrl_flush(wpa_s);
9d562b79
SS
10376 } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
10377 if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
10378 reply_len = -1;
59eba7a2
JM
10379 } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
10380 if (wpas_p2p_cancel(wpa_s))
10381 reply_len = -1;
b563b388
JM
10382 } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
10383 if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
10384 reply_len = -1;
10385 } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
10386 if (p2p_ctrl_presence_req(wpa_s, "") < 0)
10387 reply_len = -1;
10388 } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
10389 if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
10390 reply_len = -1;
10391 } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
10392 if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
10393 reply_len = -1;
f2c56602
JM
10394 } else if (os_strncmp(buf, "P2P_REMOVE_CLIENT ", 18) == 0) {
10395 if (p2p_ctrl_remove_client(wpa_s, buf + 18) < 0)
10396 reply_len = -1;
a6f5b193
PX
10397 } else if (os_strncmp(buf, "P2P_LO_START ", 13) == 0) {
10398 if (p2p_ctrl_iface_p2p_lo_start(wpa_s, buf + 13))
10399 reply_len = -1;
10400 } else if (os_strcmp(buf, "P2P_LO_STOP") == 0) {
10401 if (wpas_p2p_lo_stop(wpa_s))
10402 reply_len = -1;
b563b388 10403#endif /* CONFIG_P2P */
9675ce35
JM
10404#ifdef CONFIG_WIFI_DISPLAY
10405 } else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
10406 if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
10407 reply_len = -1;
10408 } else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
10409 reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
10410 reply, reply_size);
10411#endif /* CONFIG_WIFI_DISPLAY */
afc064fe
JM
10412#ifdef CONFIG_INTERWORKING
10413 } else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
10414 if (interworking_fetch_anqp(wpa_s) < 0)
10415 reply_len = -1;
10416 } else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
10417 interworking_stop_fetch_anqp(wpa_s);
356d1488
JM
10418 } else if (os_strcmp(buf, "INTERWORKING_SELECT") == 0) {
10419 if (ctrl_interworking_select(wpa_s, NULL) < 0)
10420 reply_len = -1;
10421 } else if (os_strncmp(buf, "INTERWORKING_SELECT ", 20) == 0) {
10422 if (ctrl_interworking_select(wpa_s, buf + 20) < 0)
b02fe7ff
JM
10423 reply_len = -1;
10424 } else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
f91a512f 10425 if (ctrl_interworking_connect(wpa_s, buf + 21, 0) < 0)
b02fe7ff 10426 reply_len = -1;
f91a512f
JM
10427 } else if (os_strncmp(buf, "INTERWORKING_ADD_NETWORK ", 25) == 0) {
10428 int id;
10429
10430 id = ctrl_interworking_connect(wpa_s, buf + 25, 1);
10431 if (id < 0)
10432 reply_len = -1;
10433 else {
10434 reply_len = os_snprintf(reply, reply_size, "%d\n", id);
10435 if (os_snprintf_error(reply_size, reply_len))
10436 reply_len = -1;
10437 }
afc064fe
JM
10438 } else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
10439 if (get_anqp(wpa_s, buf + 9) < 0)
10440 reply_len = -1;
b1f12296
JM
10441 } else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
10442 if (gas_request(wpa_s, buf + 12) < 0)
10443 reply_len = -1;
10444 } else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
10445 reply_len = gas_response_get(wpa_s, buf + 17, reply,
10446 reply_size);
afc064fe 10447#endif /* CONFIG_INTERWORKING */
a8918e86
JK
10448#ifdef CONFIG_HS20
10449 } else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
10450 if (get_hs20_anqp(wpa_s, buf + 14) < 0)
10451 reply_len = -1;
10452 } else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
10453 if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
10454 reply_len = -1;
184e110c 10455 } else if (os_strncmp(buf, "HS20_ICON_REQUEST ", 18) == 0) {
8dd5c1b4
JN
10456 if (hs20_icon_request(wpa_s, buf + 18, 0) < 0)
10457 reply_len = -1;
10458 } else if (os_strncmp(buf, "REQ_HS20_ICON ", 14) == 0) {
10459 if (hs20_icon_request(wpa_s, buf + 14, 1) < 0)
10460 reply_len = -1;
10461 } else if (os_strncmp(buf, "GET_HS20_ICON ", 14) == 0) {
10462 reply_len = get_hs20_icon(wpa_s, buf + 14, reply, reply_size);
10463 } else if (os_strncmp(buf, "DEL_HS20_ICON ", 14) == 0) {
10464 if (del_hs20_icon(wpa_s, buf + 14) < 0)
184e110c 10465 reply_len = -1;
b572df86 10466 } else if (os_strcmp(buf, "FETCH_OSU") == 0) {
dd20eabd
JM
10467 if (hs20_fetch_osu(wpa_s, 0) < 0)
10468 reply_len = -1;
10469 } else if (os_strcmp(buf, "FETCH_OSU no-scan") == 0) {
10470 if (hs20_fetch_osu(wpa_s, 1) < 0)
b572df86
JM
10471 reply_len = -1;
10472 } else if (os_strcmp(buf, "CANCEL_FETCH_OSU") == 0) {
10473 hs20_cancel_fetch_osu(wpa_s);
a8918e86 10474#endif /* CONFIG_HS20 */
6fc6879b
JM
10475 } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
10476 {
10477 if (wpa_supplicant_ctrl_iface_ctrl_rsp(
10478 wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
10479 reply_len = -1;
bceb8431
JM
10480 else {
10481 /*
10482 * Notify response from timeout to allow the control
10483 * interface response to be sent first.
10484 */
10485 eloop_register_timeout(0, 0, wpas_ctrl_eapol_response,
10486 wpa_s, NULL);
10487 }
6fc6879b
JM
10488 } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
10489 if (wpa_supplicant_reload_configuration(wpa_s))
10490 reply_len = -1;
10491 } else if (os_strcmp(buf, "TERMINATE") == 0) {
1a1bf008 10492 wpa_supplicant_terminate_proc(wpa_s->global);
6fc6879b
JM
10493 } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
10494 if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
10495 reply_len = -1;
9aa10e2b
DS
10496 } else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
10497 reply_len = wpa_supplicant_ctrl_iface_blacklist(
10498 wpa_s, buf + 9, reply, reply_size);
0597a5b5
DS
10499 } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
10500 reply_len = wpa_supplicant_ctrl_iface_log_level(
10501 wpa_s, buf + 9, reply, reply_size);
90903a77
VD
10502 } else if (os_strncmp(buf, "LIST_NETWORKS ", 14) == 0) {
10503 reply_len = wpa_supplicant_ctrl_iface_list_networks(
10504 wpa_s, buf + 14, reply, reply_size);
6fc6879b
JM
10505 } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
10506 reply_len = wpa_supplicant_ctrl_iface_list_networks(
90903a77 10507 wpa_s, NULL, reply, reply_size);
6fc6879b 10508 } else if (os_strcmp(buf, "DISCONNECT") == 0) {
5f040be4 10509 wpas_request_disconnection(wpa_s);
fee52342
JM
10510 } else if (os_strcmp(buf, "SCAN") == 0) {
10511 wpas_ctrl_scan(wpa_s, NULL, reply, reply_size, &reply_len);
10512 } else if (os_strncmp(buf, "SCAN ", 5) == 0) {
10513 wpas_ctrl_scan(wpa_s, buf + 5, reply, reply_size, &reply_len);
6fc6879b
JM
10514 } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
10515 reply_len = wpa_supplicant_ctrl_iface_scan_results(
10516 wpa_s, reply, reply_size);
2ea2166d
JM
10517 } else if (os_strcmp(buf, "ABORT_SCAN") == 0) {
10518 if (wpas_abort_ongoing_scan(wpa_s) < 0)
10519 reply_len = -1;
6fc6879b
JM
10520 } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
10521 if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
10522 reply_len = -1;
10523 } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
10524 if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
10525 reply_len = -1;
10526 } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
10527 if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
10528 reply_len = -1;
10529 } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
10530 reply_len = wpa_supplicant_ctrl_iface_add_network(
10531 wpa_s, reply, reply_size);
10532 } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
10533 if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
10534 reply_len = -1;
10535 } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
10536 if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
10537 reply_len = -1;
10538 } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
10539 reply_len = wpa_supplicant_ctrl_iface_get_network(
10540 wpa_s, buf + 12, reply, reply_size);
1c330a2f 10541 } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
daae4995
AN
10542 if (wpa_supplicant_ctrl_iface_dup_network(wpa_s, buf + 12,
10543 wpa_s))
1c330a2f 10544 reply_len = -1;
d94c9ee6
JM
10545 } else if (os_strcmp(buf, "LIST_CREDS") == 0) {
10546 reply_len = wpa_supplicant_ctrl_iface_list_creds(
10547 wpa_s, reply, reply_size);
10548 } else if (os_strcmp(buf, "ADD_CRED") == 0) {
10549 reply_len = wpa_supplicant_ctrl_iface_add_cred(
10550 wpa_s, reply, reply_size);
10551 } else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
10552 if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
10553 reply_len = -1;
10554 } else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
10555 if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
10556 reply_len = -1;
c880ab87
JM
10557 } else if (os_strncmp(buf, "GET_CRED ", 9) == 0) {
10558 reply_len = wpa_supplicant_ctrl_iface_get_cred(wpa_s, buf + 9,
10559 reply,
10560 reply_size);
6fc6879b
JM
10561#ifndef CONFIG_NO_CONFIG_WRITE
10562 } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
10563 if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
10564 reply_len = -1;
10565#endif /* CONFIG_NO_CONFIG_WRITE */
10566 } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
10567 reply_len = wpa_supplicant_ctrl_iface_get_capability(
10568 wpa_s, buf + 15, reply, reply_size);
10569 } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
10570 if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
10571 reply_len = -1;
67b9bd08
DS
10572 } else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
10573 if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
10574 reply_len = -1;
4b4a8ae5
JM
10575 } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
10576 reply_len = wpa_supplicant_global_iface_list(
10577 wpa_s->global, reply, reply_size);
56e2fc2c 10578 } else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
6fc6879b 10579 reply_len = wpa_supplicant_global_iface_interfaces(
56e2fc2c 10580 wpa_s->global, buf + 10, reply, reply_size);
6fc6879b
JM
10581 } else if (os_strncmp(buf, "BSS ", 4) == 0) {
10582 reply_len = wpa_supplicant_ctrl_iface_bss(
10583 wpa_s, buf + 4, reply, reply_size);
e653b622
JM
10584#ifdef CONFIG_AP
10585 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
10586 reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
10587 } else if (os_strncmp(buf, "STA ", 4) == 0) {
10588 reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
10589 reply_size);
10590 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
10591 reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
10592 reply_size);
e60b2951
JJ
10593 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
10594 if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
10595 reply_len = -1;
10596 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
10597 if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
10598 reply_len = -1;
334bf36a
AO
10599 } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
10600 if (ap_ctrl_iface_chanswitch(wpa_s, buf + 12))
10601 reply_len = -1;
99650cad
JM
10602 } else if (os_strcmp(buf, "STOP_AP") == 0) {
10603 if (wpas_ap_stop_ap(wpa_s))
10604 reply_len = -1;
e653b622 10605#endif /* CONFIG_AP */
207ef3fb
JM
10606 } else if (os_strcmp(buf, "SUSPEND") == 0) {
10607 wpas_notify_suspend(wpa_s->global);
10608 } else if (os_strcmp(buf, "RESUME") == 0) {
10609 wpas_notify_resume(wpa_s->global);
9ff4de6d 10610#ifdef CONFIG_TESTING_OPTIONS
32d5295f
JM
10611 } else if (os_strcmp(buf, "DROP_SA") == 0) {
10612 wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
9ff4de6d 10613#endif /* CONFIG_TESTING_OPTIONS */
86d4f806
JM
10614 } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
10615 if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
10616 reply_len = -1;
0d0a8ca1 10617 } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
5407c69d 10618 wpa_s->auto_reconnect_disabled = atoi(buf + 16) == 0;
78633c37
SL
10619 } else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
10620 if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
10621 reply_len = -1;
10622 } else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
10623 if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
10624 buf + 17))
10625 reply_len = -1;
39ee845f 10626 } else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
a1144000 10627 wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10);
281ff0aa
GP
10628#ifdef CONFIG_TDLS
10629 } else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
10630 if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
10631 reply_len = -1;
10632 } else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
10633 if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
10634 reply_len = -1;
10635 } else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
10636 if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
10637 reply_len = -1;
6b90deae
AN
10638 } else if (os_strncmp(buf, "TDLS_CHAN_SWITCH ", 17) == 0) {
10639 if (wpa_supplicant_ctrl_iface_tdls_chan_switch(wpa_s,
10640 buf + 17))
10641 reply_len = -1;
10642 } else if (os_strncmp(buf, "TDLS_CANCEL_CHAN_SWITCH ", 24) == 0) {
10643 if (wpa_supplicant_ctrl_iface_tdls_cancel_chan_switch(wpa_s,
10644 buf + 24))
10645 reply_len = -1;
4504621f
OG
10646 } else if (os_strncmp(buf, "TDLS_LINK_STATUS ", 17) == 0) {
10647 reply_len = wpa_supplicant_ctrl_iface_tdls_link_status(
10648 wpa_s, buf + 17, reply, reply_size);
281ff0aa 10649#endif /* CONFIG_TDLS */
8506ea6f
MB
10650 } else if (os_strcmp(buf, "WMM_AC_STATUS") == 0) {
10651 reply_len = wpas_wmm_ac_status(wpa_s, reply, reply_size);
eb2f2088
MB
10652 } else if (os_strncmp(buf, "WMM_AC_ADDTS ", 13) == 0) {
10653 if (wmm_ac_ctrl_addts(wpa_s, buf + 13))
10654 reply_len = -1;
10655 } else if (os_strncmp(buf, "WMM_AC_DELTS ", 13) == 0) {
10656 if (wmm_ac_ctrl_delts(wpa_s, buf + 13))
10657 reply_len = -1;
60b24b0d
DS
10658 } else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
10659 reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
10660 reply_size);
96e8d831
DS
10661 } else if (os_strncmp(buf, "SIGNAL_MONITOR", 14) == 0) {
10662 if (wpas_ctrl_iface_signal_monitor(wpa_s, buf + 14))
10663 reply_len = -1;
dc7785f8
YZ
10664 } else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
10665 reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
10666 reply_size);
bc5d330a
TB
10667#ifdef CONFIG_AUTOSCAN
10668 } else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
10669 if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
10670 reply_len = -1;
10671#endif /* CONFIG_AUTOSCAN */
4d7aab78
EL
10672 } else if (os_strcmp(buf, "DRIVER_FLAGS") == 0) {
10673 reply_len = wpas_ctrl_iface_driver_flags(wpa_s, reply,
10674 reply_size);
5e2c3490
JM
10675#ifdef ANDROID
10676 } else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
10677 reply_len = wpa_supplicant_driver_cmd(wpa_s, buf + 7, reply,
10678 reply_size);
10679#endif /* ANDROID */
adef8948
BL
10680 } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
10681 reply_len = wpa_supplicant_vendor_cmd(wpa_s, buf + 7, reply,
10682 reply_size);
9482426e 10683 } else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
f5f37d3a 10684 pmksa_cache_clear_current(wpa_s->wpa);
9482426e 10685 eapol_sm_request_reauth(wpa_s->eapol);
e9199e31
JM
10686#ifdef CONFIG_WNM
10687 } else if (os_strncmp(buf, "WNM_SLEEP ", 10) == 0) {
10688 if (wpas_ctrl_iface_wnm_sleep(wpa_s, buf + 10))
10689 reply_len = -1;
07565ab0
MG
10690 } else if (os_strncmp(buf, "WNM_BSS_QUERY ", 14) == 0) {
10691 if (wpas_ctrl_iface_wnm_bss_query(wpa_s, buf + 14))
65bcd0a9 10692 reply_len = -1;
d514b502
JM
10693 } else if (os_strncmp(buf, "COLOC_INTF_REPORT ", 18) == 0) {
10694 if (wpas_ctrl_iface_coloc_intf_report(wpa_s, buf + 18))
10695 reply_len = -1;
e9199e31 10696#endif /* CONFIG_WNM */
acb54643
JM
10697 } else if (os_strcmp(buf, "FLUSH") == 0) {
10698 wpa_supplicant_ctrl_iface_flush(wpa_s);
1f965e62
JM
10699 } else if (os_strncmp(buf, "RADIO_WORK ", 11) == 0) {
10700 reply_len = wpas_ctrl_radio_work(wpa_s, buf + 11, reply,
10701 reply_size);
60b893df
JM
10702#ifdef CONFIG_TESTING_OPTIONS
10703 } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
10704 if (wpas_ctrl_iface_mgmt_tx(wpa_s, buf + 8) < 0)
10705 reply_len = -1;
10706 } else if (os_strcmp(buf, "MGMT_TX_DONE") == 0) {
10707 wpas_ctrl_iface_mgmt_tx_done(wpa_s);
4de70e23
JM
10708 } else if (os_strncmp(buf, "MGMT_RX_PROCESS ", 16) == 0) {
10709 if (wpas_ctrl_iface_mgmt_rx_process(wpa_s, buf + 16) < 0)
10710 reply_len = -1;
ad12f2f4
JM
10711 } else if (os_strncmp(buf, "DRIVER_EVENT ", 13) == 0) {
10712 if (wpas_ctrl_iface_driver_event(wpa_s, buf + 13) < 0)
10713 reply_len = -1;
9d4ff04a
JM
10714 } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
10715 if (wpas_ctrl_iface_eapol_rx(wpa_s, buf + 9) < 0)
10716 reply_len = -1;
4a6cc862
JM
10717 } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
10718 if (wpas_ctrl_iface_data_test_config(wpa_s, buf + 17) < 0)
10719 reply_len = -1;
10720 } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
10721 if (wpas_ctrl_iface_data_test_tx(wpa_s, buf + 13) < 0)
10722 reply_len = -1;
fc0ef7c0
JM
10723 } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
10724 if (wpas_ctrl_iface_data_test_frame(wpa_s, buf + 16) < 0)
10725 reply_len = -1;
a156ffda
JM
10726 } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
10727 if (wpas_ctrl_test_alloc_fail(wpa_s, buf + 16) < 0)
10728 reply_len = -1;
10729 } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
10730 reply_len = wpas_ctrl_get_alloc_fail(wpa_s, reply, reply_size);
2da52565
JM
10731 } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
10732 if (wpas_ctrl_test_fail(wpa_s, buf + 10) < 0)
10733 reply_len = -1;
10734 } else if (os_strcmp(buf, "GET_FAIL") == 0) {
10735 reply_len = wpas_ctrl_get_fail(wpa_s, reply, reply_size);
a530fe77
JM
10736 } else if (os_strncmp(buf, "EVENT_TEST ", 11) == 0) {
10737 if (wpas_ctrl_event_test(wpa_s, buf + 11) < 0)
10738 reply_len = -1;
651c6a84
JM
10739 } else if (os_strncmp(buf, "TEST_ASSOC_IE ", 14) == 0) {
10740 if (wpas_ctrl_test_assoc_ie(wpa_s, buf + 14) < 0)
10741 reply_len = -1;
16579769
JM
10742 } else if (os_strcmp(buf, "RESET_PN") == 0) {
10743 if (wpas_ctrl_reset_pn(wpa_s) < 0)
10744 reply_len = -1;
751f5b29
JM
10745 } else if (os_strncmp(buf, "KEY_REQUEST ", 12) == 0) {
10746 if (wpas_ctrl_key_request(wpa_s, buf + 12) < 0)
10747 reply_len = -1;
daa40960
JM
10748 } else if (os_strcmp(buf, "RESEND_ASSOC") == 0) {
10749 if (wpas_ctrl_resend_assoc(wpa_s) < 0)
10750 reply_len = -1;
5c7d35ba
MV
10751 } else if (os_strcmp(buf, "UNPROT_DEAUTH") == 0) {
10752 sme_event_unprot_disconnect(
10753 wpa_s, wpa_s->bssid, NULL,
10754 WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA);
60b893df 10755#endif /* CONFIG_TESTING_OPTIONS */
86bd36f0
JM
10756 } else if (os_strncmp(buf, "VENDOR_ELEM_ADD ", 16) == 0) {
10757 if (wpas_ctrl_vendor_elem_add(wpa_s, buf + 16) < 0)
10758 reply_len = -1;
10759 } else if (os_strncmp(buf, "VENDOR_ELEM_GET ", 16) == 0) {
10760 reply_len = wpas_ctrl_vendor_elem_get(wpa_s, buf + 16, reply,
10761 reply_size);
10762 } else if (os_strncmp(buf, "VENDOR_ELEM_REMOVE ", 19) == 0) {
10763 if (wpas_ctrl_vendor_elem_remove(wpa_s, buf + 19) < 0)
10764 reply_len = -1;
f4b8bfae 10765 } else if (os_strncmp(buf, "NEIGHBOR_REP_REQUEST", 20) == 0) {
6a4f0ed7 10766 if (wpas_ctrl_iface_send_neighbor_rep(wpa_s, buf + 20))
f4b8bfae 10767 reply_len = -1;
65d9a5e2
JM
10768 } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
10769 wpas_ctrl_iface_erp_flush(wpa_s);
fb375883
IP
10770 } else if (os_strncmp(buf, "MAC_RAND_SCAN ", 14) == 0) {
10771 if (wpas_ctrl_iface_mac_rand_scan(wpa_s, buf + 14))
10772 reply_len = -1;
98342208
AK
10773 } else if (os_strncmp(buf, "GET_PREF_FREQ_LIST ", 19) == 0) {
10774 reply_len = wpas_ctrl_iface_get_pref_freq_list(
10775 wpa_s, buf + 19, reply, reply_size);
5732b770
JM
10776#ifdef CONFIG_FILS
10777 } else if (os_strncmp(buf, "FILS_HLP_REQ_ADD ", 17) == 0) {
10778 if (wpas_ctrl_iface_fils_hlp_req_add(wpa_s, buf + 17))
10779 reply_len = -1;
10780 } else if (os_strcmp(buf, "FILS_HLP_REQ_FLUSH") == 0) {
10781 wpas_flush_fils_hlp_req(wpa_s);
10782#endif /* CONFIG_FILS */
be27e185
JM
10783#ifdef CONFIG_DPP
10784 } else if (os_strncmp(buf, "DPP_QR_CODE ", 12) == 0) {
10785 int res;
10786
10787 res = wpas_dpp_qr_code(wpa_s, buf + 12);
10788 if (res < 0) {
10789 reply_len = -1;
10790 } else {
10791 reply_len = os_snprintf(reply, reply_size, "%d", res);
10792 if (os_snprintf_error(reply_size, reply_len))
10793 reply_len = -1;
10794 }
e780b4bf
JM
10795 } else if (os_strncmp(buf, "DPP_NFC_URI ", 12) == 0) {
10796 int res;
10797
10798 res = wpas_dpp_nfc_uri(wpa_s, buf + 12);
10799 if (res < 0) {
10800 reply_len = -1;
10801 } else {
10802 reply_len = os_snprintf(reply, reply_size, "%d", res);
10803 if (os_snprintf_error(reply_size, reply_len))
10804 reply_len = -1;
10805 }
be27e185
JM
10806 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GEN ", 18) == 0) {
10807 int res;
10808
87d8435c 10809 res = dpp_bootstrap_gen(wpa_s->dpp, buf + 18);
be27e185
JM
10810 if (res < 0) {
10811 reply_len = -1;
10812 } else {
10813 reply_len = os_snprintf(reply, reply_size, "%d", res);
10814 if (os_snprintf_error(reply_size, reply_len))
10815 reply_len = -1;
10816 }
10817 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_REMOVE ", 21) == 0) {
87d8435c 10818 if (dpp_bootstrap_remove(wpa_s->dpp, buf + 21) < 0)
be27e185
JM
10819 reply_len = -1;
10820 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GET_URI ", 22) == 0) {
10821 const char *uri;
10822
87d8435c 10823 uri = dpp_bootstrap_get_uri(wpa_s->dpp, atoi(buf + 22));
be27e185
JM
10824 if (!uri) {
10825 reply_len = -1;
10826 } else {
10827 reply_len = os_snprintf(reply, reply_size, "%s", uri);
10828 if (os_snprintf_error(reply_size, reply_len))
10829 reply_len = -1;
10830 }
6a7182a9 10831 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_INFO ", 19) == 0) {
87d8435c
JM
10832 reply_len = dpp_bootstrap_info(wpa_s->dpp, atoi(buf + 19),
10833 reply, reply_size);
30d27b04
JM
10834 } else if (os_strncmp(buf, "DPP_AUTH_INIT ", 14) == 0) {
10835 if (wpas_dpp_auth_init(wpa_s, buf + 13) < 0)
10836 reply_len = -1;
10837 } else if (os_strncmp(buf, "DPP_LISTEN ", 11) == 0) {
10838 if (wpas_dpp_listen(wpa_s, buf + 11) < 0)
10839 reply_len = -1;
10840 } else if (os_strcmp(buf, "DPP_STOP_LISTEN") == 0) {
c1d37739 10841 wpas_dpp_stop(wpa_s);
30d27b04 10842 wpas_dpp_listen_stop(wpa_s);
461d39af
JM
10843 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_ADD", 20) == 0) {
10844 int res;
10845
87d8435c 10846 res = dpp_configurator_add(wpa_s->dpp, buf + 20);
461d39af
JM
10847 if (res < 0) {
10848 reply_len = -1;
10849 } else {
10850 reply_len = os_snprintf(reply, reply_size, "%d", res);
10851 if (os_snprintf_error(reply_size, reply_len))
10852 reply_len = -1;
10853 }
10854 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_REMOVE ", 24) == 0) {
87d8435c 10855 if (dpp_configurator_remove(wpa_s->dpp, buf + 24) < 0)
461d39af 10856 reply_len = -1;
f522bb23 10857 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_SIGN ", 22) == 0) {
53d5de6f 10858 if (wpas_dpp_configurator_sign(wpa_s, buf + 21) < 0)
f522bb23 10859 reply_len = -1;
8179ae3a 10860 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_GET_KEY ", 25) == 0) {
87d8435c
JM
10861 reply_len = dpp_configurator_get_key_id(wpa_s->dpp,
10862 atoi(buf + 25),
10863 reply, reply_size);
500ed7f0
JM
10864 } else if (os_strncmp(buf, "DPP_PKEX_ADD ", 13) == 0) {
10865 int res;
10866
10867 res = wpas_dpp_pkex_add(wpa_s, buf + 12);
10868 if (res < 0) {
10869 reply_len = -1;
10870 } else {
10871 reply_len = os_snprintf(reply, reply_size, "%d", res);
10872 if (os_snprintf_error(reply_size, reply_len))
10873 reply_len = -1;
10874 }
10875 } else if (os_strncmp(buf, "DPP_PKEX_REMOVE ", 16) == 0) {
10876 if (wpas_dpp_pkex_remove(wpa_s, buf + 16) < 0)
10877 reply_len = -1;
c02dd10d
JM
10878#ifdef CONFIG_DPP2
10879 } else if (os_strncmp(buf, "DPP_CONTROLLER_START ", 21) == 0) {
10880 if (wpas_dpp_controller_start(wpa_s, buf + 20) < 0)
10881 reply_len = -1;
10882 } else if (os_strcmp(buf, "DPP_CONTROLLER_START") == 0) {
10883 if (wpas_dpp_controller_start(wpa_s, NULL) < 0)
10884 reply_len = -1;
10885 } else if (os_strcmp(buf, "DPP_CONTROLLER_STOP") == 0) {
10886 dpp_controller_stop(wpa_s->dpp);
10887#endif /* CONFIG_DPP2 */
be27e185 10888#endif /* CONFIG_DPP */
6fc6879b
JM
10889 } else {
10890 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
10891 reply_len = 16;
10892 }
10893
10894 if (reply_len < 0) {
10895 os_memcpy(reply, "FAIL\n", 5);
10896 reply_len = 5;
10897 }
10898
6fc6879b
JM
10899 *resp_len = reply_len;
10900 return reply;
10901}
10902
10903
10904static int wpa_supplicant_global_iface_add(struct wpa_global *global,
10905 char *cmd)
10906{
10907 struct wpa_interface iface;
efa232f9
JJ
10908 char *pos, *extra;
10909 struct wpa_supplicant *wpa_s;
10910 unsigned int create_iface = 0;
10911 u8 mac_addr[ETH_ALEN];
0f039e34 10912 enum wpa_driver_if_type type = WPA_IF_STATION;
6fc6879b
JM
10913
10914 /*
10915 * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
0f039e34 10916 * TAB<bridge_ifname>[TAB<create>[TAB<interface_type>]]
6fc6879b
JM
10917 */
10918 wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
10919
10920 os_memset(&iface, 0, sizeof(iface));
10921
10922 do {
10923 iface.ifname = pos = cmd;
10924 pos = os_strchr(pos, '\t');
10925 if (pos)
10926 *pos++ = '\0';
10927 if (iface.ifname[0] == '\0')
10928 return -1;
10929 if (pos == NULL)
10930 break;
10931
10932 iface.confname = pos;
10933 pos = os_strchr(pos, '\t');
10934 if (pos)
10935 *pos++ = '\0';
10936 if (iface.confname[0] == '\0')
10937 iface.confname = NULL;
10938 if (pos == NULL)
10939 break;
10940
10941 iface.driver = pos;
10942 pos = os_strchr(pos, '\t');
10943 if (pos)
10944 *pos++ = '\0';
10945 if (iface.driver[0] == '\0')
10946 iface.driver = NULL;
10947 if (pos == NULL)
10948 break;
10949
10950 iface.ctrl_interface = pos;
10951 pos = os_strchr(pos, '\t');
10952 if (pos)
10953 *pos++ = '\0';
10954 if (iface.ctrl_interface[0] == '\0')
10955 iface.ctrl_interface = NULL;
10956 if (pos == NULL)
10957 break;
10958
10959 iface.driver_param = pos;
10960 pos = os_strchr(pos, '\t');
10961 if (pos)
10962 *pos++ = '\0';
10963 if (iface.driver_param[0] == '\0')
10964 iface.driver_param = NULL;
10965 if (pos == NULL)
10966 break;
10967
10968 iface.bridge_ifname = pos;
10969 pos = os_strchr(pos, '\t');
10970 if (pos)
10971 *pos++ = '\0';
10972 if (iface.bridge_ifname[0] == '\0')
10973 iface.bridge_ifname = NULL;
10974 if (pos == NULL)
10975 break;
efa232f9
JJ
10976
10977 extra = pos;
10978 pos = os_strchr(pos, '\t');
10979 if (pos)
10980 *pos++ = '\0';
da3db681
BG
10981 if (!extra[0])
10982 break;
10983
0f039e34 10984 if (os_strcmp(extra, "create") == 0) {
efa232f9 10985 create_iface = 1;
0f039e34
AS
10986 if (!pos)
10987 break;
10988
10989 if (os_strcmp(pos, "sta") == 0) {
10990 type = WPA_IF_STATION;
10991 } else if (os_strcmp(pos, "ap") == 0) {
10992 type = WPA_IF_AP_BSS;
10993 } else {
10994 wpa_printf(MSG_DEBUG,
10995 "INTERFACE_ADD unsupported interface type: '%s'",
10996 pos);
10997 return -1;
10998 }
10999 } else {
da3db681
BG
11000 wpa_printf(MSG_DEBUG,
11001 "INTERFACE_ADD unsupported extra parameter: '%s'",
11002 extra);
efa232f9 11003 return -1;
da3db681 11004 }
6fc6879b
JM
11005 } while (0);
11006
efa232f9
JJ
11007 if (create_iface) {
11008 wpa_printf(MSG_DEBUG, "CTRL_IFACE creating interface '%s'",
11009 iface.ifname);
11010 if (!global->ifaces)
11011 return -1;
0f039e34 11012 if (wpa_drv_if_add(global->ifaces, type, iface.ifname,
efa232f9
JJ
11013 NULL, NULL, NULL, mac_addr, NULL) < 0) {
11014 wpa_printf(MSG_ERROR,
11015 "CTRL_IFACE interface creation failed");
11016 return -1;
11017 }
11018
11019 wpa_printf(MSG_DEBUG,
11020 "CTRL_IFACE interface '%s' created with MAC addr: "
11021 MACSTR, iface.ifname, MAC2STR(mac_addr));
11022 }
11023
6fc6879b 11024 if (wpa_supplicant_get_iface(global, iface.ifname))
efa232f9
JJ
11025 goto fail;
11026
11027 wpa_s = wpa_supplicant_add_iface(global, &iface, NULL);
11028 if (!wpa_s)
11029 goto fail;
11030 wpa_s->added_vif = create_iface;
11031 return 0;
6fc6879b 11032
efa232f9
JJ
11033fail:
11034 if (create_iface)
11035 wpa_drv_if_remove(global->ifaces, WPA_IF_STATION, iface.ifname);
11036 return -1;
6fc6879b
JM
11037}
11038
11039
11040static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
11041 char *cmd)
11042{
11043 struct wpa_supplicant *wpa_s;
efa232f9
JJ
11044 int ret;
11045 unsigned int delete_iface;
6fc6879b
JM
11046
11047 wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
11048
11049 wpa_s = wpa_supplicant_get_iface(global, cmd);
11050 if (wpa_s == NULL)
11051 return -1;
efa232f9
JJ
11052 delete_iface = wpa_s->added_vif;
11053 ret = wpa_supplicant_remove_iface(global, wpa_s, 0);
11054 if (!ret && delete_iface) {
11055 wpa_printf(MSG_DEBUG, "CTRL_IFACE deleting the interface '%s'",
11056 cmd);
11057 ret = wpa_drv_if_remove(global->ifaces, WPA_IF_STATION, cmd);
11058 }
11059 return ret;
6fc6879b
JM
11060}
11061
11062
4b4a8ae5
JM
11063static void wpa_free_iface_info(struct wpa_interface_info *iface)
11064{
11065 struct wpa_interface_info *prev;
11066
11067 while (iface) {
11068 prev = iface;
11069 iface = iface->next;
11070
11071 os_free(prev->ifname);
11072 os_free(prev->desc);
11073 os_free(prev);
11074 }
11075}
11076
11077
11078static int wpa_supplicant_global_iface_list(struct wpa_global *global,
11079 char *buf, int len)
11080{
11081 int i, res;
11082 struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
11083 char *pos, *end;
11084
c5121837 11085 for (i = 0; wpa_drivers[i]; i++) {
8b423edb 11086 const struct wpa_driver_ops *drv = wpa_drivers[i];
4b4a8ae5
JM
11087 if (drv->get_interfaces == NULL)
11088 continue;
5fbc1f27 11089 tmp = drv->get_interfaces(global->drv_priv[i]);
4b4a8ae5
JM
11090 if (tmp == NULL)
11091 continue;
11092
11093 if (last == NULL)
11094 iface = last = tmp;
11095 else
11096 last->next = tmp;
11097 while (last->next)
11098 last = last->next;
11099 }
11100
11101 pos = buf;
11102 end = buf + len;
11103 for (tmp = iface; tmp; tmp = tmp->next) {
11104 res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
11105 tmp->drv_name, tmp->ifname,
11106 tmp->desc ? tmp->desc : "");
d85e1fc8 11107 if (os_snprintf_error(end - pos, res)) {
4b4a8ae5
JM
11108 *pos = '\0';
11109 break;
11110 }
11111 pos += res;
11112 }
11113
11114 wpa_free_iface_info(iface);
11115
11116 return pos - buf;
11117}
11118
11119
6fc6879b 11120static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
56e2fc2c 11121 const char *input,
6fc6879b
JM
11122 char *buf, int len)
11123{
11124 int res;
11125 char *pos, *end;
11126 struct wpa_supplicant *wpa_s;
56e2fc2c
JD
11127 int show_ctrl = 0;
11128
11129 if (input)
11130 show_ctrl = !!os_strstr(input, "ctrl");
6fc6879b
JM
11131
11132 wpa_s = global->ifaces;
11133 pos = buf;
11134 end = buf + len;
11135
11136 while (wpa_s) {
56e2fc2c
JD
11137 if (show_ctrl)
11138 res = os_snprintf(pos, end - pos, "%s ctrl_iface=%s\n",
11139 wpa_s->ifname,
11140 wpa_s->conf->ctrl_interface ?
11141 wpa_s->conf->ctrl_interface : "N/A");
11142 else
11143 res = os_snprintf(pos, end - pos, "%s\n",
11144 wpa_s->ifname);
11145
d85e1fc8 11146 if (os_snprintf_error(end - pos, res)) {
6fc6879b
JM
11147 *pos = '\0';
11148 break;
11149 }
11150 pos += res;
11151 wpa_s = wpa_s->next;
11152 }
11153 return pos - buf;
11154}
11155
11156
cf3bebf2
JM
11157static char * wpas_global_ctrl_iface_ifname(struct wpa_global *global,
11158 const char *ifname,
11159 char *cmd, size_t *resp_len)
11160{
11161 struct wpa_supplicant *wpa_s;
11162
11163 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
11164 if (os_strcmp(ifname, wpa_s->ifname) == 0)
11165 break;
11166 }
11167
11168 if (wpa_s == NULL) {
11169 char *resp = os_strdup("FAIL-NO-IFNAME-MATCH\n");
11170 if (resp)
11171 *resp_len = os_strlen(resp);
11172 else
11173 *resp_len = 1;
11174 return resp;
11175 }
11176
11177 return wpa_supplicant_ctrl_iface_process(wpa_s, cmd, resp_len);
11178}
11179
11180
576bce9c
JM
11181static char * wpas_global_ctrl_iface_redir_p2p(struct wpa_global *global,
11182 char *buf, size_t *resp_len)
11183{
11184#ifdef CONFIG_P2P
11185 static const char * cmd[] = {
443427e4 11186 "LIST_NETWORKS",
576bce9c
JM
11187 "P2P_FIND",
11188 "P2P_STOP_FIND",
11189 "P2P_LISTEN",
11190 "P2P_GROUP_ADD",
11191 "P2P_GET_PASSPHRASE",
11192 "P2P_SERVICE_UPDATE",
11193 "P2P_SERVICE_FLUSH",
11194 "P2P_FLUSH",
11195 "P2P_CANCEL",
11196 "P2P_PRESENCE_REQ",
11197 "P2P_EXT_LISTEN",
f2bc3448
DS
11198#ifdef CONFIG_AP
11199 "STA-FIRST",
11200#endif /* CONFIG_AP */
576bce9c
JM
11201 NULL
11202 };
11203 static const char * prefix[] = {
443427e4
DS
11204#ifdef ANDROID
11205 "DRIVER ",
11206#endif /* ANDROID */
be1ece46 11207 "GET_CAPABILITY ",
443427e4
DS
11208 "GET_NETWORK ",
11209 "REMOVE_NETWORK ",
576bce9c
JM
11210 "P2P_FIND ",
11211 "P2P_CONNECT ",
11212 "P2P_LISTEN ",
11213 "P2P_GROUP_REMOVE ",
11214 "P2P_GROUP_ADD ",
57b38882 11215 "P2P_GROUP_MEMBER ",
576bce9c
JM
11216 "P2P_PROV_DISC ",
11217 "P2P_SERV_DISC_REQ ",
11218 "P2P_SERV_DISC_CANCEL_REQ ",
11219 "P2P_SERV_DISC_RESP ",
11220 "P2P_SERV_DISC_EXTERNAL ",
11221 "P2P_SERVICE_ADD ",
11222 "P2P_SERVICE_DEL ",
87d5ef5a 11223 "P2P_SERVICE_REP ",
576bce9c
JM
11224 "P2P_REJECT ",
11225 "P2P_INVITE ",
11226 "P2P_PEER ",
11227 "P2P_SET ",
11228 "P2P_UNAUTHORIZE ",
11229 "P2P_PRESENCE_REQ ",
11230 "P2P_EXT_LISTEN ",
f2c56602 11231 "P2P_REMOVE_CLIENT ",
8ad8bc5c
DS
11232 "WPS_NFC_TOKEN ",
11233 "WPS_NFC_TAG_READ ",
f3ff9487
AM
11234 "NFC_GET_HANDOVER_SEL ",
11235 "NFC_GET_HANDOVER_REQ ",
11236 "NFC_REPORT_HANDOVER ",
87d5ef5a
KV
11237 "P2P_ASP_PROVISION ",
11238 "P2P_ASP_PROVISION_RESP ",
f2bc3448
DS
11239#ifdef CONFIG_AP
11240 "STA ",
11241 "STA-NEXT ",
11242#endif /* CONFIG_AP */
576bce9c
JM
11243 NULL
11244 };
11245 int found = 0;
11246 int i;
11247
11248 if (global->p2p_init_wpa_s == NULL)
11249 return NULL;
11250
11251 for (i = 0; !found && cmd[i]; i++) {
11252 if (os_strcmp(buf, cmd[i]) == 0)
11253 found = 1;
11254 }
11255
11256 for (i = 0; !found && prefix[i]; i++) {
11257 if (os_strncmp(buf, prefix[i], os_strlen(prefix[i])) == 0)
11258 found = 1;
11259 }
11260
11261 if (found)
11262 return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
11263 buf, resp_len);
11264#endif /* CONFIG_P2P */
11265 return NULL;
11266}
11267
11268
11269static char * wpas_global_ctrl_iface_redir_wfd(struct wpa_global *global,
11270 char *buf, size_t *resp_len)
11271{
11272#ifdef CONFIG_WIFI_DISPLAY
11273 if (global->p2p_init_wpa_s == NULL)
11274 return NULL;
11275 if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0 ||
11276 os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0)
11277 return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
11278 buf, resp_len);
11279#endif /* CONFIG_WIFI_DISPLAY */
11280 return NULL;
11281}
11282
11283
11284static char * wpas_global_ctrl_iface_redir(struct wpa_global *global,
11285 char *buf, size_t *resp_len)
11286{
11287 char *ret;
11288
11289 ret = wpas_global_ctrl_iface_redir_p2p(global, buf, resp_len);
11290 if (ret)
11291 return ret;
11292
11293 ret = wpas_global_ctrl_iface_redir_wfd(global, buf, resp_len);
11294 if (ret)
11295 return ret;
11296
11297 return NULL;
11298}
11299
11300
1b9b31c1
JM
11301static int wpas_global_ctrl_iface_set(struct wpa_global *global, char *cmd)
11302{
11303 char *value;
11304
11305 value = os_strchr(cmd, ' ');
11306 if (value == NULL)
11307 return -1;
11308 *value++ = '\0';
11309
11310 wpa_printf(MSG_DEBUG, "GLOBAL_CTRL_IFACE SET '%s'='%s'", cmd, value);
11311
11312#ifdef CONFIG_WIFI_DISPLAY
11313 if (os_strcasecmp(cmd, "wifi_display") == 0) {
11314 wifi_display_enable(global, !!atoi(value));
11315 return 0;
11316 }
11317#endif /* CONFIG_WIFI_DISPLAY */
11318
a7ca6dac
JM
11319 /* Restore cmd to its original value to allow redirection */
11320 value[-1] = ' ';
11321
1b9b31c1
JM
11322 return -1;
11323}
11324
11325
daae4995
AN
11326static int wpas_global_ctrl_iface_dup_network(struct wpa_global *global,
11327 char *cmd)
11328{
11329 struct wpa_supplicant *wpa_s[2]; /* src, dst */
11330 char *p;
11331 unsigned int i;
11332
11333 /* cmd: "<src ifname> <dst ifname> <src network id> <dst network id>
11334 * <variable name> */
11335
11336 for (i = 0; i < ARRAY_SIZE(wpa_s) ; i++) {
11337 p = os_strchr(cmd, ' ');
11338 if (p == NULL)
11339 return -1;
11340 *p = '\0';
11341
11342 wpa_s[i] = global->ifaces;
11343 for (; wpa_s[i]; wpa_s[i] = wpa_s[i]->next) {
11344 if (os_strcmp(cmd, wpa_s[i]->ifname) == 0)
11345 break;
11346 }
11347
11348 if (!wpa_s[i]) {
11349 wpa_printf(MSG_DEBUG,
11350 "CTRL_IFACE: Could not find iface=%s", cmd);
11351 return -1;
11352 }
11353
11354 cmd = p + 1;
11355 }
11356
11357 return wpa_supplicant_ctrl_iface_dup_network(wpa_s[0], cmd, wpa_s[1]);
11358}
11359
11360
42868f14
JM
11361#ifndef CONFIG_NO_CONFIG_WRITE
11362static int wpas_global_ctrl_iface_save_config(struct wpa_global *global)
11363{
d6b818ef 11364 int ret = 0, saved = 0;
42868f14
JM
11365 struct wpa_supplicant *wpa_s;
11366
11367 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
11368 if (!wpa_s->conf->update_config) {
11369 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed to update configuration (update_config=0)");
11370 continue;
11371 }
11372
11373 if (wpa_config_write(wpa_s->confname, wpa_s->conf)) {
11374 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to update configuration");
11375 ret = 1;
11376 } else {
11377 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration updated");
d6b818ef 11378 saved++;
42868f14
JM
11379 }
11380 }
11381
d6b818ef
JM
11382 if (!saved && !ret) {
11383 wpa_dbg(wpa_s, MSG_DEBUG,
11384 "CTRL_IFACE: SAVE_CONFIG - No configuration files could be updated");
11385 ret = 1;
11386 }
11387
42868f14
JM
11388 return ret;
11389}
11390#endif /* CONFIG_NO_CONFIG_WRITE */
11391
11392
ae8c27f7
JM
11393static int wpas_global_ctrl_iface_status(struct wpa_global *global,
11394 char *buf, size_t buflen)
11395{
11396 char *pos, *end;
11397 int ret;
11398 struct wpa_supplicant *wpa_s;
11399
11400 pos = buf;
11401 end = buf + buflen;
11402
11403#ifdef CONFIG_P2P
4c559019 11404 if (global->p2p && !global->p2p_disabled) {
ae8c27f7 11405 ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
4c559019
JM
11406 "\n"
11407 "p2p_state=%s\n",
11408 MAC2STR(global->p2p_dev_addr),
11409 p2p_get_state_txt(global->p2p));
d85e1fc8 11410 if (os_snprintf_error(end - pos, ret))
4c559019
JM
11411 return pos - buf;
11412 pos += ret;
11413 } else if (global->p2p) {
11414 ret = os_snprintf(pos, end - pos, "p2p_state=DISABLED\n");
d85e1fc8 11415 if (os_snprintf_error(end - pos, ret))
ae8c27f7
JM
11416 return pos - buf;
11417 pos += ret;
11418 }
11419#endif /* CONFIG_P2P */
11420
11421#ifdef CONFIG_WIFI_DISPLAY
11422 ret = os_snprintf(pos, end - pos, "wifi_display=%d\n",
11423 !!global->wifi_display);
d85e1fc8 11424 if (os_snprintf_error(end - pos, ret))
ae8c27f7
JM
11425 return pos - buf;
11426 pos += ret;
11427#endif /* CONFIG_WIFI_DISPLAY */
11428
11429 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
11430 ret = os_snprintf(pos, end - pos, "ifname=%s\n"
11431 "address=" MACSTR "\n",
11432 wpa_s->ifname, MAC2STR(wpa_s->own_addr));
d85e1fc8 11433 if (os_snprintf_error(end - pos, ret))
ae8c27f7
JM
11434 return pos - buf;
11435 pos += ret;
11436 }
11437
11438 return pos - buf;
11439}
11440
11441
3794af2d
AN
11442#ifdef CONFIG_FST
11443
11444static int wpas_global_ctrl_iface_fst_attach(struct wpa_global *global,
11445 char *cmd, char *buf,
11446 size_t reply_size)
11447{
11448 char ifname[IFNAMSIZ + 1];
11449 struct fst_iface_cfg cfg;
11450 struct wpa_supplicant *wpa_s;
11451 struct fst_wpa_obj iface_obj;
11452
11453 if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
11454 wpa_s = wpa_supplicant_get_iface(global, ifname);
11455 if (wpa_s) {
5dbd3bf9
JM
11456 if (wpa_s->fst) {
11457 wpa_printf(MSG_INFO, "FST: Already attached");
11458 return -1;
11459 }
3794af2d
AN
11460 fst_wpa_supplicant_fill_iface_obj(wpa_s, &iface_obj);
11461 wpa_s->fst = fst_attach(ifname, wpa_s->own_addr,
11462 &iface_obj, &cfg);
11463 if (wpa_s->fst)
11464 return os_snprintf(buf, reply_size, "OK\n");
11465 }
11466 }
11467
11468 return -1;
11469}
11470
11471
11472static int wpas_global_ctrl_iface_fst_detach(struct wpa_global *global,
11473 char *cmd, char *buf,
11474 size_t reply_size)
11475{
11476 char ifname[IFNAMSIZ + 1];
11477 struct wpa_supplicant *wpa_s;
11478
11479 if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
11480 wpa_s = wpa_supplicant_get_iface(global, ifname);
11481 if (wpa_s) {
11482 if (!fst_iface_detach(ifname)) {
11483 wpa_s->fst = NULL;
11484 return os_snprintf(buf, reply_size, "OK\n");
11485 }
11486 }
11487 }
11488
11489 return -1;
11490}
11491
11492#endif /* CONFIG_FST */
11493
11494
6fc6879b
JM
11495char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
11496 char *buf, size_t *resp_len)
11497{
11498 char *reply;
11499 const int reply_size = 2048;
11500 int reply_len;
f4a0a82c 11501 int level = MSG_DEBUG;
6fc6879b 11502
cf3bebf2
JM
11503 if (os_strncmp(buf, "IFNAME=", 7) == 0) {
11504 char *pos = os_strchr(buf + 7, ' ');
11505 if (pos) {
11506 *pos++ = '\0';
11507 return wpas_global_ctrl_iface_ifname(global,
11508 buf + 7, pos,
11509 resp_len);
11510 }
11511 }
11512
576bce9c
JM
11513 reply = wpas_global_ctrl_iface_redir(global, buf, resp_len);
11514 if (reply)
11515 return reply;
11516
f4a0a82c
JM
11517 if (os_strcmp(buf, "PING") == 0)
11518 level = MSG_EXCESSIVE;
11519 wpa_hexdump_ascii(level, "RX global ctrl_iface",
6fc6879b
JM
11520 (const u8 *) buf, os_strlen(buf));
11521
11522 reply = os_malloc(reply_size);
11523 if (reply == NULL) {
11524 *resp_len = 1;
11525 return NULL;
11526 }
11527
11528 os_memcpy(reply, "OK\n", 3);
11529 reply_len = 3;
11530
11531 if (os_strcmp(buf, "PING") == 0) {
11532 os_memcpy(reply, "PONG\n", 5);
11533 reply_len = 5;
11534 } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
11535 if (wpa_supplicant_global_iface_add(global, buf + 14))
11536 reply_len = -1;
11537 } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
11538 if (wpa_supplicant_global_iface_remove(global, buf + 17))
11539 reply_len = -1;
4b4a8ae5
JM
11540 } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
11541 reply_len = wpa_supplicant_global_iface_list(
11542 global, reply, reply_size);
56e2fc2c 11543 } else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
6fc6879b 11544 reply_len = wpa_supplicant_global_iface_interfaces(
56e2fc2c 11545 global, buf + 10, reply, reply_size);
3794af2d
AN
11546#ifdef CONFIG_FST
11547 } else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
11548 reply_len = wpas_global_ctrl_iface_fst_attach(global, buf + 11,
11549 reply,
11550 reply_size);
11551 } else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
11552 reply_len = wpas_global_ctrl_iface_fst_detach(global, buf + 11,
11553 reply,
11554 reply_size);
11555 } else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
11556 reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
11557#endif /* CONFIG_FST */
6fc6879b 11558 } else if (os_strcmp(buf, "TERMINATE") == 0) {
1a1bf008 11559 wpa_supplicant_terminate_proc(global);
207ef3fb
JM
11560 } else if (os_strcmp(buf, "SUSPEND") == 0) {
11561 wpas_notify_suspend(global);
11562 } else if (os_strcmp(buf, "RESUME") == 0) {
11563 wpas_notify_resume(global);
1b9b31c1 11564 } else if (os_strncmp(buf, "SET ", 4) == 0) {
a7ca6dac
JM
11565 if (wpas_global_ctrl_iface_set(global, buf + 4)) {
11566#ifdef CONFIG_P2P
11567 if (global->p2p_init_wpa_s) {
11568 os_free(reply);
11569 /* Check if P2P redirection would work for this
11570 * command. */
11571 return wpa_supplicant_ctrl_iface_process(
11572 global->p2p_init_wpa_s,
11573 buf, resp_len);
11574 }
11575#endif /* CONFIG_P2P */
1b9b31c1 11576 reply_len = -1;
a7ca6dac 11577 }
daae4995
AN
11578 } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
11579 if (wpas_global_ctrl_iface_dup_network(global, buf + 12))
11580 reply_len = -1;
42868f14
JM
11581#ifndef CONFIG_NO_CONFIG_WRITE
11582 } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
11583 if (wpas_global_ctrl_iface_save_config(global))
11584 reply_len = -1;
11585#endif /* CONFIG_NO_CONFIG_WRITE */
ae8c27f7
JM
11586 } else if (os_strcmp(buf, "STATUS") == 0) {
11587 reply_len = wpas_global_ctrl_iface_status(global, reply,
11588 reply_size);
ea449b5b
JM
11589#ifdef CONFIG_MODULE_TESTS
11590 } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
ea449b5b
JM
11591 if (wpas_module_tests() < 0)
11592 reply_len = -1;
11593#endif /* CONFIG_MODULE_TESTS */
5f797376
JM
11594 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
11595 if (wpa_debug_reopen_file() < 0)
11596 reply_len = -1;
6fc6879b
JM
11597 } else {
11598 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
11599 reply_len = 16;
11600 }
11601
11602 if (reply_len < 0) {
11603 os_memcpy(reply, "FAIL\n", 5);
11604 reply_len = 5;
11605 }
11606
11607 *resp_len = reply_len;
11608 return reply;
11609}