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