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