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