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