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