]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/ctrl_iface.c
Mark RSN msg 1/2 key data debug dump as key material
[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
3a7819f0
JM
6460 if (os_strcmp(cmd, "override_pref_op_chan") == 0) {
6461 int op_class, chan;
6462
6463 op_class = atoi(param);
6464 param = os_strchr(param, ':');
6465 if (!param)
6466 return -1;
6467 param++;
6468 chan = atoi(param);
6469 p2p_set_override_pref_op_chan(wpa_s->global->p2p, op_class,
6470 chan);
6471 return 0;
6472 }
6473
b563b388
JM
6474 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
6475 cmd);
6476
6477 return -1;
6478}
6479
6480
acb54643
JM
6481static void p2p_ctrl_flush(struct wpa_supplicant *wpa_s)
6482{
6483 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
6484 wpa_s->force_long_sd = 0;
477b082c 6485 wpas_p2p_stop_find(wpa_s);
b9da88d6 6486 wpa_s->parent->p2ps_method_config_any = 0;
acb54643
JM
6487 if (wpa_s->global->p2p)
6488 p2p_flush(wpa_s->global->p2p);
6489}
6490
6491
b563b388
JM
6492static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
6493{
6494 char *pos, *pos2;
6495 unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
6496
6497 if (cmd[0]) {
6498 pos = os_strchr(cmd, ' ');
6499 if (pos == NULL)
6500 return -1;
6501 *pos++ = '\0';
6502 dur1 = atoi(cmd);
6503
6504 pos2 = os_strchr(pos, ' ');
6505 if (pos2)
6506 *pos2++ = '\0';
6507 int1 = atoi(pos);
6508 } else
6509 pos2 = NULL;
6510
6511 if (pos2) {
6512 pos = os_strchr(pos2, ' ');
6513 if (pos == NULL)
6514 return -1;
6515 *pos++ = '\0';
6516 dur2 = atoi(pos2);
6517 int2 = atoi(pos);
6518 }
6519
6520 return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
6521}
6522
6523
6524static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
6525{
6526 char *pos;
6527 unsigned int period = 0, interval = 0;
6528
6529 if (cmd[0]) {
6530 pos = os_strchr(cmd, ' ');
6531 if (pos == NULL)
6532 return -1;
6533 *pos++ = '\0';
6534 period = atoi(cmd);
6535 interval = atoi(pos);
6536 }
6537
6538 return wpas_p2p_ext_listen(wpa_s, period, interval);
6539}
6540
f2c56602
JM
6541
6542static int p2p_ctrl_remove_client(struct wpa_supplicant *wpa_s, const char *cmd)
6543{
6544 const char *pos;
6545 u8 peer[ETH_ALEN];
6546 int iface_addr = 0;
6547
6548 pos = cmd;
6549 if (os_strncmp(pos, "iface=", 6) == 0) {
6550 iface_addr = 1;
6551 pos += 6;
6552 }
6553 if (hwaddr_aton(pos, peer))
6554 return -1;
6555
6556 wpas_p2p_remove_client(wpa_s, peer, iface_addr);
6557 return 0;
6558}
6559
a6f5b193
PX
6560
6561static int p2p_ctrl_iface_p2p_lo_start(struct wpa_supplicant *wpa_s, char *cmd)
6562{
6563 int freq = 0, period = 0, interval = 0, count = 0;
6564
6565 if (sscanf(cmd, "%d %d %d %d", &freq, &period, &interval, &count) != 4)
6566 {
6567 wpa_printf(MSG_DEBUG,
6568 "CTRL: Invalid P2P LO Start parameter: '%s'", cmd);
6569 return -1;
6570 }
6571
6572 return wpas_p2p_lo_start(wpa_s, freq, period, interval, count);
6573}
6574
b563b388
JM
6575#endif /* CONFIG_P2P */
6576
6577
356d1488
JM
6578static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s, char *val)
6579{
6580 struct wpa_freq_range_list ranges;
6581 int *freqs = NULL;
6582 struct hostapd_hw_modes *mode;
6583 u16 i;
6584
6585 if (wpa_s->hw.modes == NULL)
6586 return NULL;
6587
6588 os_memset(&ranges, 0, sizeof(ranges));
6589 if (freq_range_list_parse(&ranges, val) < 0)
6590 return NULL;
6591
6592 for (i = 0; i < wpa_s->hw.num_modes; i++) {
6593 int j;
6594
6595 mode = &wpa_s->hw.modes[i];
6596 for (j = 0; j < mode->num_channels; j++) {
6597 unsigned int freq;
6598
6599 if (mode->channels[j].flag & HOSTAPD_CHAN_DISABLED)
6600 continue;
6601
6602 freq = mode->channels[j].freq;
6603 if (!freq_range_list_includes(&ranges, freq))
6604 continue;
6605
6606 int_array_add_unique(&freqs, freq);
6607 }
6608 }
6609
6610 os_free(ranges.range);
6611 return freqs;
6612}
6613
6614
afc064fe 6615#ifdef CONFIG_INTERWORKING
356d1488
JM
6616
6617static int ctrl_interworking_select(struct wpa_supplicant *wpa_s, char *param)
6618{
6619 int auto_sel = 0;
6620 int *freqs = NULL;
6621
6622 if (param) {
6623 char *pos;
6624
6625 auto_sel = os_strstr(param, "auto") != NULL;
6626
6627 pos = os_strstr(param, "freq=");
6628 if (pos) {
6629 freqs = freq_range_to_channel_list(wpa_s, pos + 5);
6630 if (freqs == NULL)
6631 return -1;
6632 }
6633
6634 }
6635
6636 return interworking_select(wpa_s, auto_sel, freqs);
6637}
6638
6639
f91a512f
JM
6640static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst,
6641 int only_add)
b02fe7ff
JM
6642{
6643 u8 bssid[ETH_ALEN];
6644 struct wpa_bss *bss;
6645
6646 if (hwaddr_aton(dst, bssid)) {
6647 wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
6648 return -1;
6649 }
6650
6651 bss = wpa_bss_get_bssid(wpa_s, bssid);
6652 if (bss == NULL) {
6653 wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
6654 MAC2STR(bssid));
6655 return -1;
6656 }
6657
783b2a97
JM
6658 if (bss->ssid_len == 0) {
6659 int found = 0;
6660
6661 wpa_printf(MSG_DEBUG, "Selected BSS entry for " MACSTR
6662 " does not have SSID information", MAC2STR(bssid));
6663
6664 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss,
6665 list) {
6666 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0 &&
6667 bss->ssid_len > 0) {
6668 found = 1;
6669 break;
6670 }
6671 }
6672
6673 if (!found)
6674 return -1;
6675 wpa_printf(MSG_DEBUG,
6676 "Found another matching BSS entry with SSID");
6677 }
6678
f91a512f 6679 return interworking_connect(wpa_s, bss, only_add);
b02fe7ff
JM
6680}
6681
6682
afc064fe
JM
6683static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
6684{
6685 u8 dst_addr[ETH_ALEN];
6686 int used;
6687 char *pos;
6688#define MAX_ANQP_INFO_ID 100
6689 u16 id[MAX_ANQP_INFO_ID];
6690 size_t num_id = 0;
cf28c66b 6691 u32 subtypes = 0;
8f479174 6692 int get_cell_pref = 0;
afc064fe
JM
6693
6694 used = hwaddr_aton2(dst, dst_addr);
6695 if (used < 0)
6696 return -1;
6697 pos = dst + used;
b68d602d
JM
6698 if (*pos == ' ')
6699 pos++;
afc064fe 6700 while (num_id < MAX_ANQP_INFO_ID) {
cf28c66b
DS
6701 if (os_strncmp(pos, "hs20:", 5) == 0) {
6702#ifdef CONFIG_HS20
6703 int num = atoi(pos + 5);
6704 if (num <= 0 || num > 31)
6705 return -1;
6706 subtypes |= BIT(num);
6707#else /* CONFIG_HS20 */
6708 return -1;
6709#endif /* CONFIG_HS20 */
8f479174 6710 } else if (os_strncmp(pos, "mbo:", 4) == 0) {
6711#ifdef CONFIG_MBO
6712 int num = atoi(pos + 4);
6713 if (num != MBO_ANQP_SUBTYPE_CELL_CONN_PREF)
6714 return -1;
6715 get_cell_pref = 1;
6716#else /* CONFIG_MBO */
6717 return -1;
6718#endif /* CONFIG_MBO */
cf28c66b
DS
6719 } else {
6720 id[num_id] = atoi(pos);
6721 if (id[num_id])
6722 num_id++;
6723 }
afc064fe
JM
6724 pos = os_strchr(pos + 1, ',');
6725 if (pos == NULL)
6726 break;
6727 pos++;
6728 }
6729
6730 if (num_id == 0)
6731 return -1;
6732
8f479174 6733 return anqp_send_req(wpa_s, dst_addr, id, num_id, subtypes,
6734 get_cell_pref);
afc064fe 6735}
b1f12296
JM
6736
6737
6738static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
6739{
6740 u8 dst_addr[ETH_ALEN];
6741 struct wpabuf *advproto, *query = NULL;
6742 int used, ret = -1;
6743 char *pos, *end;
6744 size_t len;
6745
6746 used = hwaddr_aton2(cmd, dst_addr);
6747 if (used < 0)
6748 return -1;
6749
6750 pos = cmd + used;
6751 while (*pos == ' ')
6752 pos++;
6753
6754 /* Advertisement Protocol ID */
6755 end = os_strchr(pos, ' ');
6756 if (end)
6757 len = end - pos;
6758 else
6759 len = os_strlen(pos);
6760 if (len & 0x01)
6761 return -1;
6762 len /= 2;
6763 if (len == 0)
6764 return -1;
6765 advproto = wpabuf_alloc(len);
6766 if (advproto == NULL)
6767 return -1;
6768 if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
6769 goto fail;
6770
6771 if (end) {
6772 /* Optional Query Request */
6773 pos = end + 1;
6774 while (*pos == ' ')
6775 pos++;
6776
6777 len = os_strlen(pos);
6778 if (len) {
6779 if (len & 0x01)
6780 goto fail;
6781 len /= 2;
6782 if (len == 0)
6783 goto fail;
6784 query = wpabuf_alloc(len);
6785 if (query == NULL)
6786 goto fail;
6787 if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
6788 goto fail;
6789 }
6790 }
6791
6792 ret = gas_send_request(wpa_s, dst_addr, advproto, query);
6793
6794fail:
6795 wpabuf_free(advproto);
6796 wpabuf_free(query);
6797
6798 return ret;
6799}
6800
6801
6802static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
6803 size_t buflen)
6804{
6805 u8 addr[ETH_ALEN];
6806 int dialog_token;
6807 int used;
6808 char *pos;
6809 size_t resp_len, start, requested_len;
b6a9590b
JM
6810 struct wpabuf *resp;
6811 int ret;
b1f12296
JM
6812
6813 used = hwaddr_aton2(cmd, addr);
6814 if (used < 0)
6815 return -1;
6816
6817 pos = cmd + used;
6818 while (*pos == ' ')
6819 pos++;
6820 dialog_token = atoi(pos);
6821
b6a9590b
JM
6822 if (wpa_s->last_gas_resp &&
6823 os_memcmp(addr, wpa_s->last_gas_addr, ETH_ALEN) == 0 &&
6824 dialog_token == wpa_s->last_gas_dialog_token)
6825 resp = wpa_s->last_gas_resp;
6826 else if (wpa_s->prev_gas_resp &&
6827 os_memcmp(addr, wpa_s->prev_gas_addr, ETH_ALEN) == 0 &&
6828 dialog_token == wpa_s->prev_gas_dialog_token)
6829 resp = wpa_s->prev_gas_resp;
6830 else
b1f12296
JM
6831 return -1;
6832
b6a9590b 6833 resp_len = wpabuf_len(resp);
b1f12296
JM
6834 start = 0;
6835 requested_len = resp_len;
6836
6837 pos = os_strchr(pos, ' ');
6838 if (pos) {
6839 start = atoi(pos);
6840 if (start > resp_len)
6841 return os_snprintf(buf, buflen, "FAIL-Invalid range");
6842 pos = os_strchr(pos, ',');
6843 if (pos == NULL)
6844 return -1;
6845 pos++;
6846 requested_len = atoi(pos);
6847 if (start + requested_len > resp_len)
6848 return os_snprintf(buf, buflen, "FAIL-Invalid range");
6849 }
6850
6851 if (requested_len * 2 + 1 > buflen)
6852 return os_snprintf(buf, buflen, "FAIL-Too long response");
6853
b6a9590b
JM
6854 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(resp) + start,
6855 requested_len);
6856
6857 if (start + requested_len == resp_len) {
6858 /*
6859 * Free memory by dropping the response after it has been
6860 * fetched.
6861 */
6862 if (resp == wpa_s->prev_gas_resp) {
6863 wpabuf_free(wpa_s->prev_gas_resp);
6864 wpa_s->prev_gas_resp = NULL;
6865 } else {
6866 wpabuf_free(wpa_s->last_gas_resp);
6867 wpa_s->last_gas_resp = NULL;
6868 }
6869 }
6870
6871 return ret;
b1f12296 6872}
afc064fe
JM
6873#endif /* CONFIG_INTERWORKING */
6874
6875
a8918e86
JK
6876#ifdef CONFIG_HS20
6877
6878static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
6879{
6880 u8 dst_addr[ETH_ALEN];
6881 int used;
6882 char *pos;
6883 u32 subtypes = 0;
6884
6885 used = hwaddr_aton2(dst, dst_addr);
6886 if (used < 0)
6887 return -1;
6888 pos = dst + used;
b68d602d
JM
6889 if (*pos == ' ')
6890 pos++;
a8918e86
JK
6891 for (;;) {
6892 int num = atoi(pos);
6893 if (num <= 0 || num > 31)
6894 return -1;
6895 subtypes |= BIT(num);
6896 pos = os_strchr(pos + 1, ',');
6897 if (pos == NULL)
6898 break;
6899 pos++;
6900 }
6901
6902 if (subtypes == 0)
6903 return -1;
6904
8dd5c1b4 6905 return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0, 0);
a8918e86
JK
6906}
6907
6908
6909static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
6910 const u8 *addr, const char *realm)
6911{
6912 u8 *buf;
6913 size_t rlen, len;
6914 int ret;
6915
6916 rlen = os_strlen(realm);
6917 len = 3 + rlen;
6918 buf = os_malloc(len);
6919 if (buf == NULL)
6920 return -1;
6921 buf[0] = 1; /* NAI Home Realm Count */
6922 buf[1] = 0; /* Formatted in accordance with RFC 4282 */
6923 buf[2] = rlen;
6924 os_memcpy(buf + 3, realm, rlen);
6925
6926 ret = hs20_anqp_send_req(wpa_s, addr,
6927 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
8dd5c1b4 6928 buf, len, 0);
a8918e86
JK
6929
6930 os_free(buf);
6931
6932 return ret;
6933}
6934
6935
6936static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
6937 char *dst)
6938{
6939 struct wpa_cred *cred = wpa_s->conf->cred;
6940 u8 dst_addr[ETH_ALEN];
6941 int used;
6942 u8 *buf;
6943 size_t len;
6944 int ret;
6945
6946 used = hwaddr_aton2(dst, dst_addr);
6947 if (used < 0)
6948 return -1;
6949
6950 while (dst[used] == ' ')
6951 used++;
6952 if (os_strncmp(dst + used, "realm=", 6) == 0)
6953 return hs20_nai_home_realm_list(wpa_s, dst_addr,
6954 dst + used + 6);
6955
6956 len = os_strlen(dst + used);
6957
6958 if (len == 0 && cred && cred->realm)
6959 return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
6960
0e87e798 6961 if (len & 1)
a8918e86
JK
6962 return -1;
6963 len /= 2;
6964 buf = os_malloc(len);
6965 if (buf == NULL)
6966 return -1;
6967 if (hexstr2bin(dst + used, buf, len) < 0) {
6968 os_free(buf);
6969 return -1;
6970 }
6971
6972 ret = hs20_anqp_send_req(wpa_s, dst_addr,
6973 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
8dd5c1b4 6974 buf, len, 0);
a8918e86
JK
6975 os_free(buf);
6976
6977 return ret;
6978}
6979
184e110c 6980
8dd5c1b4
JN
6981static int get_hs20_icon(struct wpa_supplicant *wpa_s, char *cmd, char *reply,
6982 int buflen)
6983{
6984 u8 dst_addr[ETH_ALEN];
6985 int used;
6986 char *ctx = NULL, *icon, *poffset, *psize;
6987
6988 used = hwaddr_aton2(cmd, dst_addr);
6989 if (used < 0)
6990 return -1;
6991 cmd += used;
6992
6993 icon = str_token(cmd, " ", &ctx);
6994 poffset = str_token(cmd, " ", &ctx);
6995 psize = str_token(cmd, " ", &ctx);
6996 if (!icon || !poffset || !psize)
6997 return -1;
6998
6999 wpa_s->fetch_osu_icon_in_progress = 0;
7000 return hs20_get_icon(wpa_s, dst_addr, icon, atoi(poffset), atoi(psize),
7001 reply, buflen);
7002}
7003
7004
7005static int del_hs20_icon(struct wpa_supplicant *wpa_s, char *cmd)
7006{
7007 u8 dst_addr[ETH_ALEN];
7008 int used;
7009 char *icon;
7010
7011 if (!cmd[0])
7012 return hs20_del_icon(wpa_s, NULL, NULL);
7013
7014 used = hwaddr_aton2(cmd, dst_addr);
7015 if (used < 0)
7016 return -1;
7017
7018 while (cmd[used] == ' ')
7019 used++;
7020 icon = cmd[used] ? &cmd[used] : NULL;
7021
7022 return hs20_del_icon(wpa_s, dst_addr, icon);
7023}
7024
7025
7026static int hs20_icon_request(struct wpa_supplicant *wpa_s, char *cmd, int inmem)
184e110c
JM
7027{
7028 u8 dst_addr[ETH_ALEN];
7029 int used;
7030 char *icon;
7031
7032 used = hwaddr_aton2(cmd, dst_addr);
7033 if (used < 0)
7034 return -1;
7035
7036 while (cmd[used] == ' ')
7037 used++;
7038 icon = &cmd[used];
7039
b572df86 7040 wpa_s->fetch_osu_icon_in_progress = 0;
184e110c 7041 return hs20_anqp_send_req(wpa_s, dst_addr, BIT(HS20_STYPE_ICON_REQUEST),
8dd5c1b4 7042 (u8 *) icon, os_strlen(icon), inmem);
184e110c
JM
7043}
7044
a8918e86
JK
7045#endif /* CONFIG_HS20 */
7046
7047
bc5d330a
TB
7048#ifdef CONFIG_AUTOSCAN
7049
7050static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
7051 char *cmd)
7052{
7053 enum wpa_states state = wpa_s->wpa_state;
7054 char *new_params = NULL;
7055
7056 if (os_strlen(cmd) > 0) {
7057 new_params = os_strdup(cmd);
7058 if (new_params == NULL)
7059 return -1;
7060 }
7061
7062 os_free(wpa_s->conf->autoscan);
7063 wpa_s->conf->autoscan = new_params;
7064
7065 if (wpa_s->conf->autoscan == NULL)
7066 autoscan_deinit(wpa_s);
7067 else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
99218999 7068 autoscan_init(wpa_s, 1);
99f00324
JM
7069 else if (state == WPA_SCANNING)
7070 wpa_supplicant_reinit_autoscan(wpa_s);
9d3f4a74
JM
7071 else
7072 wpa_printf(MSG_DEBUG, "No autoscan update in state %s",
7073 wpa_supplicant_state_txt(state));
bc5d330a
TB
7074
7075 return 0;
7076}
7077
7078#endif /* CONFIG_AUTOSCAN */
7079
7080
e9199e31
JM
7081#ifdef CONFIG_WNM
7082
7083static int wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant *wpa_s, char *cmd)
7084{
7085 int enter;
7086 int intval = 0;
7087 char *pos;
cd0ef657
JM
7088 int ret;
7089 struct wpabuf *tfs_req = NULL;
e9199e31
JM
7090
7091 if (os_strncmp(cmd, "enter", 5) == 0)
7092 enter = 1;
7093 else if (os_strncmp(cmd, "exit", 4) == 0)
7094 enter = 0;
7095 else
7096 return -1;
7097
7098 pos = os_strstr(cmd, " interval=");
7099 if (pos)
7100 intval = atoi(pos + 10);
7101
cd0ef657
JM
7102 pos = os_strstr(cmd, " tfs_req=");
7103 if (pos) {
7104 char *end;
7105 size_t len;
7106 pos += 9;
7107 end = os_strchr(pos, ' ');
7108 if (end)
7109 len = end - pos;
7110 else
7111 len = os_strlen(pos);
7112 if (len & 1)
7113 return -1;
7114 len /= 2;
7115 tfs_req = wpabuf_alloc(len);
7116 if (tfs_req == NULL)
7117 return -1;
7118 if (hexstr2bin(pos, wpabuf_put(tfs_req, len), len) < 0) {
7119 wpabuf_free(tfs_req);
7120 return -1;
7121 }
7122 }
7123
df80a0cc
JM
7124 ret = ieee802_11_send_wnmsleep_req(wpa_s, enter ? WNM_SLEEP_MODE_ENTER :
7125 WNM_SLEEP_MODE_EXIT, intval,
cd0ef657
JM
7126 tfs_req);
7127 wpabuf_free(tfs_req);
7128
7129 return ret;
e9199e31
JM
7130}
7131
65bcd0a9
VK
7132
7133static int wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant *wpa_s, char *cmd)
7134{
9a493fab 7135 int query_reason, list = 0;
65bcd0a9
VK
7136
7137 query_reason = atoi(cmd);
7138
9a493fab
AS
7139 cmd = os_strchr(cmd, ' ');
7140 if (cmd) {
7141 cmd++;
7142 if (os_strncmp(cmd, "list", 4) == 0) {
7143 list = 1;
7144 } else {
7145 wpa_printf(MSG_DEBUG, "WNM Query: Invalid option %s",
7146 cmd);
7147 return -1;
7148 }
7149 }
7150
7151 wpa_printf(MSG_DEBUG,
7152 "CTRL_IFACE: WNM_BSS_QUERY query_reason=%d%s",
7153 query_reason, list ? " candidate list" : "");
65bcd0a9 7154
9a493fab 7155 return wnm_send_bss_transition_mgmt_query(wpa_s, query_reason, list);
65bcd0a9
VK
7156}
7157
e9199e31
JM
7158#endif /* CONFIG_WNM */
7159
7160
60b24b0d
DS
7161static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
7162 size_t buflen)
7163{
7164 struct wpa_signal_info si;
7165 int ret;
2cc8d8f4 7166 char *pos, *end;
60b24b0d
DS
7167
7168 ret = wpa_drv_signal_poll(wpa_s, &si);
7169 if (ret)
7170 return -1;
7171
2cc8d8f4
AO
7172 pos = buf;
7173 end = buf + buflen;
7174
7175 ret = os_snprintf(pos, end - pos, "RSSI=%d\nLINKSPEED=%d\n"
60b24b0d
DS
7176 "NOISE=%d\nFREQUENCY=%u\n",
7177 si.current_signal, si.current_txrate / 1000,
7178 si.current_noise, si.frequency);
7bdd8981 7179 if (os_snprintf_error(end - pos, ret))
60b24b0d 7180 return -1;
2cc8d8f4
AO
7181 pos += ret;
7182
7183 if (si.chanwidth != CHAN_WIDTH_UNKNOWN) {
7184 ret = os_snprintf(pos, end - pos, "WIDTH=%s\n",
7a4a93b9 7185 channel_width_to_string(si.chanwidth));
7bdd8981 7186 if (os_snprintf_error(end - pos, ret))
2cc8d8f4
AO
7187 return -1;
7188 pos += ret;
7189 }
7190
7191 if (si.center_frq1 > 0 && si.center_frq2 > 0) {
7192 ret = os_snprintf(pos, end - pos,
7193 "CENTER_FRQ1=%d\nCENTER_FRQ2=%d\n",
7194 si.center_frq1, si.center_frq2);
7bdd8981 7195 if (os_snprintf_error(end - pos, ret))
2cc8d8f4
AO
7196 return -1;
7197 pos += ret;
7198 }
7199
95783298
AO
7200 if (si.avg_signal) {
7201 ret = os_snprintf(pos, end - pos,
7202 "AVG_RSSI=%d\n", si.avg_signal);
d85e1fc8 7203 if (os_snprintf_error(end - pos, ret))
95783298
AO
7204 return -1;
7205 pos += ret;
7206 }
7207
74fa78b2
JM
7208 if (si.avg_beacon_signal) {
7209 ret = os_snprintf(pos, end - pos,
7210 "AVG_BEACON_RSSI=%d\n", si.avg_beacon_signal);
7211 if (os_snprintf_error(end - pos, ret))
7212 return -1;
7213 pos += ret;
7214 }
7215
2cc8d8f4 7216 return pos - buf;
60b24b0d
DS
7217}
7218
7219
96e8d831
DS
7220static int wpas_ctrl_iface_signal_monitor(struct wpa_supplicant *wpa_s,
7221 const char *cmd)
7222{
7223 const char *pos;
7224 int threshold = 0;
7225 int hysteresis = 0;
7226
7227 if (wpa_s->bgscan && wpa_s->bgscan_priv) {
7228 wpa_printf(MSG_DEBUG,
7229 "Reject SIGNAL_MONITOR command - bgscan is active");
7230 return -1;
7231 }
7232 pos = os_strstr(cmd, "THRESHOLD=");
7233 if (pos)
7234 threshold = atoi(pos + 10);
7235 pos = os_strstr(cmd, "HYSTERESIS=");
7236 if (pos)
7237 hysteresis = atoi(pos + 11);
7238 return wpa_drv_signal_monitor(wpa_s, threshold, hysteresis);
7239}
7240
7241
c06fca04
JM
7242#ifdef CONFIG_TESTING_OPTIONS
7243int wpas_ctrl_iface_get_pref_freq_list_override(struct wpa_supplicant *wpa_s,
7244 enum wpa_driver_if_type if_type,
7245 unsigned int *num,
7246 unsigned int *freq_list)
7247{
7248 char *pos = wpa_s->get_pref_freq_list_override;
7249 char *end;
7250 unsigned int count = 0;
7251
7252 /* Override string format:
7253 * <if_type1>:<freq1>,<freq2>,... <if_type2>:... */
7254
7255 while (pos) {
7256 if (atoi(pos) == (int) if_type)
7257 break;
7258 pos = os_strchr(pos, ' ');
7259 if (pos)
7260 pos++;
7261 }
7262 if (!pos)
7263 return -1;
7264 pos = os_strchr(pos, ':');
7265 if (!pos)
7266 return -1;
7267 pos++;
7268 end = os_strchr(pos, ' ');
7269 while (pos && (!end || pos < end) && count < *num) {
7270 freq_list[count++] = atoi(pos);
7271 pos = os_strchr(pos, ',');
7272 if (pos)
7273 pos++;
7274 }
7275
7276 *num = count;
7277 return 0;
7278}
7279#endif /* CONFIG_TESTING_OPTIONS */
7280
7281
98342208
AK
7282static int wpas_ctrl_iface_get_pref_freq_list(
7283 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
7284{
7285 unsigned int freq_list[100], num = 100, i;
7286 int ret;
7287 enum wpa_driver_if_type iface_type;
7288 char *pos, *end;
7289
7290 pos = buf;
7291 end = buf + buflen;
7292
7293 /* buf: "<interface_type>" */
7294 if (os_strcmp(cmd, "STATION") == 0)
7295 iface_type = WPA_IF_STATION;
7296 else if (os_strcmp(cmd, "AP") == 0)
7297 iface_type = WPA_IF_AP_BSS;
7298 else if (os_strcmp(cmd, "P2P_GO") == 0)
7299 iface_type = WPA_IF_P2P_GO;
7300 else if (os_strcmp(cmd, "P2P_CLIENT") == 0)
7301 iface_type = WPA_IF_P2P_CLIENT;
7302 else if (os_strcmp(cmd, "IBSS") == 0)
7303 iface_type = WPA_IF_IBSS;
7304 else if (os_strcmp(cmd, "TDLS") == 0)
7305 iface_type = WPA_IF_TDLS;
7306 else
7307 return -1;
7308
7309 wpa_printf(MSG_DEBUG,
7310 "CTRL_IFACE: GET_PREF_FREQ_LIST iface_type=%d (%s)",
7311 iface_type, buf);
7312
7313 ret = wpa_drv_get_pref_freq_list(wpa_s, iface_type, &num, freq_list);
7314 if (ret)
7315 return -1;
7316
7317 for (i = 0; i < num; i++) {
7318 ret = os_snprintf(pos, end - pos, "%s%u",
7319 i > 0 ? "," : "", freq_list[i]);
7320 if (os_snprintf_error(end - pos, ret))
7321 return -1;
7322 pos += ret;
7323 }
7324
7325 return pos - buf;
7326}
7327
7328
4d7aab78
EL
7329static int wpas_ctrl_iface_driver_flags(struct wpa_supplicant *wpa_s,
7330 char *buf, size_t buflen)
7331{
7332 int ret, i;
7333 char *pos, *end;
7334
7335 ret = os_snprintf(buf, buflen, "%016llX:\n",
7336 (long long unsigned) wpa_s->drv_flags);
7337 if (os_snprintf_error(buflen, ret))
7338 return -1;
7339
7340 pos = buf + ret;
7341 end = buf + buflen;
7342
7343 for (i = 0; i < 64; i++) {
7344 if (wpa_s->drv_flags & (1LLU << i)) {
7345 ret = os_snprintf(pos, end - pos, "%s\n",
7346 driver_flag_to_string(1LLU << i));
7347 if (os_snprintf_error(end - pos, ret))
7348 return -1;
7349 pos += ret;
7350 }
7351 }
7352
7353 return pos - buf;
7354}
7355
7356
dc7785f8
YZ
7357static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
7358 size_t buflen)
7359{
7360 struct hostap_sta_driver_data sta;
7361 int ret;
7362
7363 ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
7364 if (ret)
7365 return -1;
7366
7367 ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
7368 sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
7bdd8981 7369 if (os_snprintf_error(buflen, ret))
dc7785f8
YZ
7370 return -1;
7371 return ret;
7372}
7373
7374
5e2c3490
JM
7375#ifdef ANDROID
7376static int wpa_supplicant_driver_cmd(struct wpa_supplicant *wpa_s, char *cmd,
7377 char *buf, size_t buflen)
7378{
7379 int ret;
7380
7381 ret = wpa_drv_driver_cmd(wpa_s, cmd, buf, buflen);
a94737ea
DS
7382 if (ret == 0) {
7383 if (os_strncasecmp(cmd, "COUNTRY", 7) == 0) {
7384 struct p2p_data *p2p = wpa_s->global->p2p;
7385 if (p2p) {
7386 char country[3];
7387 country[0] = cmd[8];
7388 country[1] = cmd[9];
7389 country[2] = 0x04;
7390 p2p_set_country(p2p, country);
7391 }
7392 }
5e2c3490 7393 ret = os_snprintf(buf, buflen, "%s\n", "OK");
aaadd727
JM
7394 if (os_snprintf_error(buflen, ret))
7395 ret = -1;
a94737ea 7396 }
5e2c3490
JM
7397 return ret;
7398}
7399#endif /* ANDROID */
7400
7401
adef8948
BL
7402static int wpa_supplicant_vendor_cmd(struct wpa_supplicant *wpa_s, char *cmd,
7403 char *buf, size_t buflen)
7404{
7405 int ret;
7406 char *pos;
7407 u8 *data = NULL;
7408 unsigned int vendor_id, subcmd;
7409 struct wpabuf *reply;
7410 size_t data_len = 0;
7411
7412 /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
7413 vendor_id = strtoul(cmd, &pos, 16);
640b0b93 7414 if (!isblank((unsigned char) *pos))
adef8948
BL
7415 return -EINVAL;
7416
7417 subcmd = strtoul(pos, &pos, 10);
7418
7419 if (*pos != '\0') {
640b0b93 7420 if (!isblank((unsigned char) *pos++))
adef8948
BL
7421 return -EINVAL;
7422 data_len = os_strlen(pos);
7423 }
7424
7425 if (data_len) {
7426 data_len /= 2;
7427 data = os_malloc(data_len);
7428 if (!data)
2cebdee6 7429 return -1;
adef8948
BL
7430
7431 if (hexstr2bin(pos, data, data_len)) {
7432 wpa_printf(MSG_DEBUG,
7433 "Vendor command: wrong parameter format");
7434 os_free(data);
7435 return -EINVAL;
7436 }
7437 }
7438
7439 reply = wpabuf_alloc((buflen - 1) / 2);
7440 if (!reply) {
7441 os_free(data);
2cebdee6 7442 return -1;
adef8948
BL
7443 }
7444
7445 ret = wpa_drv_vendor_cmd(wpa_s, vendor_id, subcmd, data, data_len,
7446 reply);
7447
7448 if (ret == 0)
7449 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
7450 wpabuf_len(reply));
7451
7452 wpabuf_free(reply);
7453 os_free(data);
7454
7455 return ret;
7456}
7457
7458
acb54643
JM
7459static void wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant *wpa_s)
7460{
7e608d1d
IP
7461#ifdef CONFIG_P2P
7462 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s ?
7463 wpa_s->global->p2p_init_wpa_s : wpa_s;
7464#endif /* CONFIG_P2P */
7465
acb54643
JM
7466 wpa_dbg(wpa_s, MSG_DEBUG, "Flush all wpa_supplicant state");
7467
6e374bd4
JM
7468 if (wpas_abort_ongoing_scan(wpa_s) == 0)
7469 wpa_s->ignore_post_flush_scan_res = 1;
53401e91 7470
e9ccfc38
JM
7471 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
7472 /*
7473 * Avoid possible auto connect re-connection on getting
7474 * disconnected due to state flush.
7475 */
7476 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
7477 }
7478
acb54643 7479#ifdef CONFIG_P2P
1d20c66e 7480 wpas_p2p_group_remove(p2p_wpa_s, "*");
7e608d1d
IP
7481 wpas_p2p_cancel(p2p_wpa_s);
7482 p2p_ctrl_flush(p2p_wpa_s);
7e608d1d
IP
7483 wpas_p2p_service_flush(p2p_wpa_s);
7484 p2p_wpa_s->global->p2p_disabled = 0;
7485 p2p_wpa_s->global->p2p_per_sta_psk = 0;
7486 p2p_wpa_s->conf->num_sec_device_types = 0;
7487 p2p_wpa_s->p2p_disable_ip_addr_req = 0;
7488 os_free(p2p_wpa_s->global->p2p_go_avoid_freq.range);
7489 p2p_wpa_s->global->p2p_go_avoid_freq.range = NULL;
85e152b6 7490 p2p_wpa_s->global->p2p_go_avoid_freq.num = 0;
9a58e521 7491 p2p_wpa_s->global->pending_p2ps_group = 0;
8bb8e6ed 7492 p2p_wpa_s->global->pending_p2ps_group_freq = 0;
acb54643
JM
7493#endif /* CONFIG_P2P */
7494
7495#ifdef CONFIG_WPS_TESTING
7496 wps_version_number = 0x20;
7497 wps_testing_dummy_cred = 0;
91226e0d 7498 wps_corrupt_pkhash = 0;
6e379c6c
JM
7499 wps_force_auth_types_in_use = 0;
7500 wps_force_encr_types_in_use = 0;
acb54643
JM
7501#endif /* CONFIG_WPS_TESTING */
7502#ifdef CONFIG_WPS
7b02375a 7503 wpa_s->wps_fragment_size = 0;
acb54643 7504 wpas_wps_cancel(wpa_s);
422ba11e 7505 wps_registrar_flush(wpa_s->wps->registrar);
acb54643 7506#endif /* CONFIG_WPS */
7255983b 7507 wpa_s->after_wps = 0;
4d9fb08d 7508 wpa_s->known_wps_freq = 0;
acb54643 7509
9d2cb3ec 7510#ifdef CONFIG_TDLS
acb54643 7511#ifdef CONFIG_TDLS_TESTING
acb54643
JM
7512 tdls_testing = 0;
7513#endif /* CONFIG_TDLS_TESTING */
acb54643
JM
7514 wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL);
7515 wpa_tdls_enable(wpa_s->wpa, 1);
7516#endif /* CONFIG_TDLS */
7517
e78aaca0
JM
7518 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
7519 wpa_supplicant_stop_countermeasures(wpa_s, NULL);
7520
acb54643 7521 wpa_s->no_keep_alive = 0;
c2805909 7522 wpa_s->own_disconnect_req = 0;
acb54643
JM
7523
7524 os_free(wpa_s->disallow_aps_bssid);
7525 wpa_s->disallow_aps_bssid = NULL;
7526 wpa_s->disallow_aps_bssid_count = 0;
7527 os_free(wpa_s->disallow_aps_ssid);
7528 wpa_s->disallow_aps_ssid = NULL;
7529 wpa_s->disallow_aps_ssid_count = 0;
7530
7531 wpa_s->set_sta_uapsd = 0;
7532 wpa_s->sta_uapsd = 0;
7533
7534 wpa_drv_radio_disable(wpa_s, 0);
acb54643 7535 wpa_blacklist_clear(wpa_s);
a8a7890d 7536 wpa_s->extra_blacklist_count = 0;
acb54643
JM
7537 wpa_supplicant_ctrl_iface_remove_network(wpa_s, "all");
7538 wpa_supplicant_ctrl_iface_remove_cred(wpa_s, "all");
d9bb2821 7539 wpa_config_flush_blobs(wpa_s->conf);
ea6e040c
JM
7540 wpa_s->conf->auto_interworking = 0;
7541 wpa_s->conf->okc = 0;
04f7ecc6 7542
b925506a
JM
7543 wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
7544 rsn_preauth_deinit(wpa_s->wpa);
7545
04f7ecc6
JM
7546 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME, 43200);
7547 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD, 70);
7548 wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, 60);
0d79b50a 7549 eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
b1ae396f 7550
b3253ebb 7551 radio_remove_works(wpa_s, NULL, 1);
e3745228 7552 wpa_s->ext_work_in_progress = 0;
3d910ef4
JM
7553
7554 wpa_s->next_ssid = NULL;
b572df86
JM
7555
7556#ifdef CONFIG_INTERWORKING
876e74aa 7557#ifdef CONFIG_HS20
b572df86 7558 hs20_cancel_fetch_osu(wpa_s);
8dd5c1b4 7559 hs20_del_icon(wpa_s, NULL, NULL);
876e74aa 7560#endif /* CONFIG_HS20 */
b572df86 7561#endif /* CONFIG_INTERWORKING */
60b893df
JM
7562
7563 wpa_s->ext_mgmt_frame_handling = 0;
9d4ff04a 7564 wpa_s->ext_eapol_frame_io = 0;
1f94e4ee
JM
7565#ifdef CONFIG_TESTING_OPTIONS
7566 wpa_s->extra_roc_dur = 0;
911942ee 7567 wpa_s->test_failure = WPAS_TEST_FAILURE_NONE;
ed7820b4 7568 wpa_s->p2p_go_csa_on_inv = 0;
02adead5 7569 wpa_s->ignore_auth_resp = 0;
6ad37d73 7570 wpa_s->ignore_assoc_disallow = 0;
a483c6f1 7571 wpa_s->reject_btm_req_reason = 0;
651c6a84 7572 wpa_sm_set_test_assoc_ie(wpa_s->wpa, NULL);
c06fca04
JM
7573 os_free(wpa_s->get_pref_freq_list_override);
7574 wpa_s->get_pref_freq_list_override = NULL;
1f94e4ee 7575#endif /* CONFIG_TESTING_OPTIONS */
97cfe110
JM
7576
7577 wpa_s->disconnected = 0;
b8db1dfc
JM
7578 os_free(wpa_s->next_scan_freqs);
7579 wpa_s->next_scan_freqs = NULL;
6c699913
JM
7580
7581 wpa_bss_flush(wpa_s);
7582 if (!dl_list_empty(&wpa_s->bss)) {
7583 wpa_printf(MSG_DEBUG,
7584 "BSS table not empty after flush: %u entries, current_bss=%p bssid="
7585 MACSTR " pending_bssid=" MACSTR,
7586 dl_list_len(&wpa_s->bss), wpa_s->current_bss,
7587 MAC2STR(wpa_s->bssid),
7588 MAC2STR(wpa_s->pending_bssid));
7589 }
9bd566a3
AS
7590
7591 eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
03ed0a52 7592 wpa_s->wnmsleep_used = 0;
ab6ab07a
JM
7593
7594#ifdef CONFIG_SME
7595 wpa_s->sme.last_unprot_disconnect.sec = 0;
7596#endif /* CONFIG_SME */
acb54643
JM
7597}
7598
7599
1f965e62
JM
7600static int wpas_ctrl_radio_work_show(struct wpa_supplicant *wpa_s,
7601 char *buf, size_t buflen)
7602{
7603 struct wpa_radio_work *work;
7604 char *pos, *end;
7605 struct os_reltime now, diff;
7606
7607 pos = buf;
7608 end = buf + buflen;
7609
7610 os_get_reltime(&now);
7611
7612 dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
7613 {
7614 int ret;
7615
7616 os_reltime_sub(&now, &work->time, &diff);
7617 ret = os_snprintf(pos, end - pos, "%s@%s:%u:%u:%ld.%06ld\n",
7618 work->type, work->wpa_s->ifname, work->freq,
7619 work->started, diff.sec, diff.usec);
d85e1fc8 7620 if (os_snprintf_error(end - pos, ret))
1f965e62
JM
7621 break;
7622 pos += ret;
7623 }
7624
7625 return pos - buf;
7626}
7627
7628
7629static void wpas_ctrl_radio_work_timeout(void *eloop_ctx, void *timeout_ctx)
7630{
7631 struct wpa_radio_work *work = eloop_ctx;
7632 struct wpa_external_work *ework = work->ctx;
7633
7634 wpa_dbg(work->wpa_s, MSG_DEBUG,
7635 "Timing out external radio work %u (%s)",
7636 ework->id, work->type);
7637 wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_TIMEOUT "%u", ework->id);
e3745228 7638 work->wpa_s->ext_work_in_progress = 0;
1f965e62 7639 radio_work_done(work);
df48efc5 7640 os_free(ework);
1f965e62
JM
7641}
7642
7643
7644static void wpas_ctrl_radio_work_cb(struct wpa_radio_work *work, int deinit)
7645{
7646 struct wpa_external_work *ework = work->ctx;
7647
7648 if (deinit) {
b3253ebb
AO
7649 if (work->started)
7650 eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
7651 work, NULL);
7652
7d1007a6
JM
7653 /*
7654 * work->type points to a buffer in ework, so need to replace
7655 * that here with a fixed string to avoid use of freed memory
7656 * in debug prints.
7657 */
7658 work->type = "freed-ext-work";
7659 work->ctx = NULL;
1f965e62
JM
7660 os_free(ework);
7661 return;
7662 }
7663
7664 wpa_dbg(work->wpa_s, MSG_DEBUG, "Starting external radio work %u (%s)",
7665 ework->id, ework->type);
7666 wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_START "%u", ework->id);
e3745228 7667 work->wpa_s->ext_work_in_progress = 1;
1f965e62
JM
7668 if (!ework->timeout)
7669 ework->timeout = 10;
7670 eloop_register_timeout(ework->timeout, 0, wpas_ctrl_radio_work_timeout,
7671 work, NULL);
7672}
7673
7674
7675static int wpas_ctrl_radio_work_add(struct wpa_supplicant *wpa_s, char *cmd,
7676 char *buf, size_t buflen)
7677{
7678 struct wpa_external_work *ework;
7679 char *pos, *pos2;
7680 size_t type_len;
7681 int ret;
7682 unsigned int freq = 0;
7683
7684 /* format: <name> [freq=<MHz>] [timeout=<seconds>] */
7685
7686 ework = os_zalloc(sizeof(*ework));
7687 if (ework == NULL)
7688 return -1;
7689
7690 pos = os_strchr(cmd, ' ');
7691 if (pos) {
7692 type_len = pos - cmd;
7693 pos++;
7694
7695 pos2 = os_strstr(pos, "freq=");
7696 if (pos2)
7697 freq = atoi(pos2 + 5);
7698
7699 pos2 = os_strstr(pos, "timeout=");
7700 if (pos2)
7701 ework->timeout = atoi(pos2 + 8);
7702 } else {
7703 type_len = os_strlen(cmd);
7704 }
7705 if (4 + type_len >= sizeof(ework->type))
7706 type_len = sizeof(ework->type) - 4 - 1;
7707 os_strlcpy(ework->type, "ext:", sizeof(ework->type));
7708 os_memcpy(ework->type + 4, cmd, type_len);
7709 ework->type[4 + type_len] = '\0';
7710
7711 wpa_s->ext_work_id++;
7712 if (wpa_s->ext_work_id == 0)
7713 wpa_s->ext_work_id++;
7714 ework->id = wpa_s->ext_work_id;
7715
7716 if (radio_add_work(wpa_s, freq, ework->type, 0, wpas_ctrl_radio_work_cb,
7717 ework) < 0) {
7718 os_free(ework);
7719 return -1;
7720 }
7721
7722 ret = os_snprintf(buf, buflen, "%u", ework->id);
d85e1fc8 7723 if (os_snprintf_error(buflen, ret))
1f965e62
JM
7724 return -1;
7725 return ret;
7726}
7727
7728
7729static int wpas_ctrl_radio_work_done(struct wpa_supplicant *wpa_s, char *cmd)
7730{
7731 struct wpa_radio_work *work;
7732 unsigned int id = atoi(cmd);
7733
7734 dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
7735 {
7736 struct wpa_external_work *ework;
7737
7738 if (os_strncmp(work->type, "ext:", 4) != 0)
7739 continue;
7740 ework = work->ctx;
7741 if (id && ework->id != id)
7742 continue;
7743 wpa_dbg(wpa_s, MSG_DEBUG,
7744 "Completed external radio work %u (%s)",
7745 ework->id, ework->type);
7746 eloop_cancel_timeout(wpas_ctrl_radio_work_timeout, work, NULL);
e3745228 7747 wpa_s->ext_work_in_progress = 0;
1f965e62 7748 radio_work_done(work);
6829da39 7749 os_free(ework);
1f965e62
JM
7750 return 3; /* "OK\n" */
7751 }
7752
7753 return -1;
7754}
7755
7756
7757static int wpas_ctrl_radio_work(struct wpa_supplicant *wpa_s, char *cmd,
7758 char *buf, size_t buflen)
7759{
7760 if (os_strcmp(cmd, "show") == 0)
7761 return wpas_ctrl_radio_work_show(wpa_s, buf, buflen);
7762 if (os_strncmp(cmd, "add ", 4) == 0)
7763 return wpas_ctrl_radio_work_add(wpa_s, cmd + 4, buf, buflen);
7764 if (os_strncmp(cmd, "done ", 5) == 0)
7765 return wpas_ctrl_radio_work_done(wpa_s, cmd + 4);
7766 return -1;
7767}
7768
7769
7770void wpas_ctrl_radio_work_flush(struct wpa_supplicant *wpa_s)
7771{
7772 struct wpa_radio_work *work, *tmp;
7773
a6cff8bf
MS
7774 if (!wpa_s || !wpa_s->radio)
7775 return;
7776
1f965e62
JM
7777 dl_list_for_each_safe(work, tmp, &wpa_s->radio->work,
7778 struct wpa_radio_work, list) {
7779 struct wpa_external_work *ework;
7780
7781 if (os_strncmp(work->type, "ext:", 4) != 0)
7782 continue;
7783 ework = work->ctx;
7784 wpa_dbg(wpa_s, MSG_DEBUG,
023b466d 7785 "Flushing%s external radio work %u (%s)",
1f965e62
JM
7786 work->started ? " started" : "", ework->id,
7787 ework->type);
7788 if (work->started)
7789 eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
7790 work, NULL);
1f965e62 7791 radio_work_done(work);
df48efc5 7792 os_free(ework);
1f965e62
JM
7793 }
7794}
7795
7796
bceb8431
JM
7797static void wpas_ctrl_eapol_response(void *eloop_ctx, void *timeout_ctx)
7798{
7799 struct wpa_supplicant *wpa_s = eloop_ctx;
7800 eapol_sm_notify_ctrl_response(wpa_s->eapol);
7801}
7802
7803
43a66ecb
JM
7804static int scan_id_list_parse(struct wpa_supplicant *wpa_s, const char *value,
7805 unsigned int *scan_id_count, int scan_id[])
6891f0e6
LJ
7806{
7807 const char *pos = value;
7808
7809 while (pos) {
7810 if (*pos == ' ' || *pos == '\0')
7811 break;
43a66ecb 7812 if (*scan_id_count == MAX_SCAN_ID)
6891f0e6 7813 return -1;
43a66ecb 7814 scan_id[(*scan_id_count)++] = atoi(pos);
6891f0e6
LJ
7815 pos = os_strchr(pos, ',');
7816 if (pos)
7817 pos++;
7818 }
7819
7820 return 0;
7821}
7822
7823
fee52342
JM
7824static void wpas_ctrl_scan(struct wpa_supplicant *wpa_s, char *params,
7825 char *reply, int reply_size, int *reply_len)
7826{
7827 char *pos;
43a66ecb
JM
7828 unsigned int manual_scan_passive = 0;
7829 unsigned int manual_scan_use_id = 0;
7830 unsigned int manual_scan_only_new = 0;
7831 unsigned int scan_only = 0;
7832 unsigned int scan_id_count = 0;
7833 int scan_id[MAX_SCAN_ID];
7834 void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
7835 struct wpa_scan_results *scan_res);
7836 int *manual_scan_freqs = NULL;
a80651d0
KV
7837 struct wpa_ssid_value *ssid = NULL, *ns;
7838 unsigned int ssid_count = 0;
fee52342
JM
7839
7840 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
7841 *reply_len = -1;
7842 return;
7843 }
7844
e69ae5ff
JM
7845 if (radio_work_pending(wpa_s, "scan")) {
7846 wpa_printf(MSG_DEBUG,
7847 "Pending scan scheduled - reject new request");
7848 *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
7849 return;
7850 }
7851
9772af66
NM
7852#ifdef CONFIG_INTERWORKING
7853 if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select) {
7854 wpa_printf(MSG_DEBUG,
7855 "Interworking select in progress - reject new scan");
7856 *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
7857 return;
7858 }
7859#endif /* CONFIG_INTERWORKING */
7860
fee52342
JM
7861 if (params) {
7862 if (os_strncasecmp(params, "TYPE=ONLY", 9) == 0)
43a66ecb 7863 scan_only = 1;
fee52342
JM
7864
7865 pos = os_strstr(params, "freq=");
43a66ecb
JM
7866 if (pos) {
7867 manual_scan_freqs = freq_range_to_channel_list(wpa_s,
7868 pos + 5);
7869 if (manual_scan_freqs == NULL) {
7870 *reply_len = -1;
7871 goto done;
7872 }
fee52342 7873 }
88c2d488
JM
7874
7875 pos = os_strstr(params, "passive=");
7876 if (pos)
43a66ecb 7877 manual_scan_passive = !!atoi(pos + 8);
d81c73be
JM
7878
7879 pos = os_strstr(params, "use_id=");
7880 if (pos)
43a66ecb 7881 manual_scan_use_id = atoi(pos + 7);
949938aa
JM
7882
7883 pos = os_strstr(params, "only_new=1");
7884 if (pos)
43a66ecb 7885 manual_scan_only_new = 1;
6891f0e6
LJ
7886
7887 pos = os_strstr(params, "scan_id=");
43a66ecb
JM
7888 if (pos && scan_id_list_parse(wpa_s, pos + 8, &scan_id_count,
7889 scan_id) < 0) {
6891f0e6 7890 *reply_len = -1;
43a66ecb 7891 goto done;
6891f0e6 7892 }
a80651d0
KV
7893
7894 pos = params;
7895 while (pos && *pos != '\0') {
7896 if (os_strncmp(pos, "ssid ", 5) == 0) {
7897 char *end;
7898
7899 pos += 5;
7900 end = pos;
7901 while (*end) {
7902 if (*end == '\0' || *end == ' ')
7903 break;
7904 end++;
7905 }
7906
7907 ns = os_realloc_array(
7908 ssid, ssid_count + 1,
7909 sizeof(struct wpa_ssid_value));
7910 if (ns == NULL) {
7911 *reply_len = -1;
7912 goto done;
7913 }
7914 ssid = ns;
7915
7916 if ((end - pos) & 0x01 ||
7917 end - pos > 2 * SSID_MAX_LEN ||
7918 hexstr2bin(pos, ssid[ssid_count].ssid,
7919 (end - pos) / 2) < 0) {
7920 wpa_printf(MSG_DEBUG,
7921 "Invalid SSID value '%s'",
7922 pos);
7923 *reply_len = -1;
7924 goto done;
7925 }
7926 ssid[ssid_count].ssid_len = (end - pos) / 2;
7927 wpa_hexdump_ascii(MSG_DEBUG, "scan SSID",
7928 ssid[ssid_count].ssid,
7929 ssid[ssid_count].ssid_len);
7930 ssid_count++;
7931 pos = end;
7932 }
7933
7934 pos = os_strchr(pos, ' ');
7935 if (pos)
7936 pos++;
7937 }
7938 }
7939
7940 wpa_s->num_ssids_from_scan_req = ssid_count;
7941 os_free(wpa_s->ssids_from_scan_req);
7942 if (ssid_count) {
7943 wpa_s->ssids_from_scan_req = ssid;
7944 ssid = NULL;
7945 } else {
7946 wpa_s->ssids_from_scan_req = NULL;
fee52342
JM
7947 }
7948
43a66ecb
JM
7949 if (scan_only)
7950 scan_res_handler = scan_only_handler;
7951 else if (wpa_s->scan_res_handler == scan_only_handler)
7952 scan_res_handler = NULL;
7953 else
7954 scan_res_handler = wpa_s->scan_res_handler;
7955
fee52342
JM
7956 if (!wpa_s->sched_scanning && !wpa_s->scanning &&
7957 ((wpa_s->wpa_state <= WPA_SCANNING) ||
7958 (wpa_s->wpa_state == WPA_COMPLETED))) {
43a66ecb
JM
7959 wpa_s->manual_scan_passive = manual_scan_passive;
7960 wpa_s->manual_scan_use_id = manual_scan_use_id;
7961 wpa_s->manual_scan_only_new = manual_scan_only_new;
7962 wpa_s->scan_id_count = scan_id_count;
7963 os_memcpy(wpa_s->scan_id, scan_id, scan_id_count * sizeof(int));
7964 wpa_s->scan_res_handler = scan_res_handler;
7965 os_free(wpa_s->manual_scan_freqs);
7966 wpa_s->manual_scan_freqs = manual_scan_freqs;
7967 manual_scan_freqs = NULL;
7968
fee52342
JM
7969 wpa_s->normal_scans = 0;
7970 wpa_s->scan_req = MANUAL_SCAN_REQ;
7971 wpa_s->after_wps = 0;
7972 wpa_s->known_wps_freq = 0;
7973 wpa_supplicant_req_scan(wpa_s, 0, 0);
d81c73be
JM
7974 if (wpa_s->manual_scan_use_id) {
7975 wpa_s->manual_scan_id++;
7976 wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
7977 wpa_s->manual_scan_id);
7978 *reply_len = os_snprintf(reply, reply_size, "%u\n",
7979 wpa_s->manual_scan_id);
7980 }
fee52342 7981 } else if (wpa_s->sched_scanning) {
43a66ecb
JM
7982 wpa_s->manual_scan_passive = manual_scan_passive;
7983 wpa_s->manual_scan_use_id = manual_scan_use_id;
7984 wpa_s->manual_scan_only_new = manual_scan_only_new;
7985 wpa_s->scan_id_count = scan_id_count;
7986 os_memcpy(wpa_s->scan_id, scan_id, scan_id_count * sizeof(int));
7987 wpa_s->scan_res_handler = scan_res_handler;
7988 os_free(wpa_s->manual_scan_freqs);
7989 wpa_s->manual_scan_freqs = manual_scan_freqs;
7990 manual_scan_freqs = NULL;
7991
fee52342
JM
7992 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to allow requested full scan to proceed");
7993 wpa_supplicant_cancel_sched_scan(wpa_s);
7994 wpa_s->scan_req = MANUAL_SCAN_REQ;
7995 wpa_supplicant_req_scan(wpa_s, 0, 0);
d81c73be
JM
7996 if (wpa_s->manual_scan_use_id) {
7997 wpa_s->manual_scan_id++;
7998 *reply_len = os_snprintf(reply, reply_size, "%u\n",
7999 wpa_s->manual_scan_id);
8000 wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
8001 wpa_s->manual_scan_id);
8002 }
fee52342
JM
8003 } else {
8004 wpa_printf(MSG_DEBUG, "Ongoing scan action - reject new request");
8005 *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
8006 }
43a66ecb
JM
8007
8008done:
8009 os_free(manual_scan_freqs);
a80651d0 8010 os_free(ssid);
fee52342
JM
8011}
8012
8013
60b893df
JM
8014#ifdef CONFIG_TESTING_OPTIONS
8015
8016static void wpas_ctrl_iface_mgmt_tx_cb(struct wpa_supplicant *wpa_s,
8017 unsigned int freq, const u8 *dst,
8018 const u8 *src, const u8 *bssid,
8019 const u8 *data, size_t data_len,
8020 enum offchannel_send_action_result
8021 result)
8022{
8023 wpa_msg(wpa_s, MSG_INFO, "MGMT-TX-STATUS freq=%u dst=" MACSTR
8024 " src=" MACSTR " bssid=" MACSTR " result=%s",
8025 freq, MAC2STR(dst), MAC2STR(src), MAC2STR(bssid),
8026 result == OFFCHANNEL_SEND_ACTION_SUCCESS ?
8027 "SUCCESS" : (result == OFFCHANNEL_SEND_ACTION_NO_ACK ?
8028 "NO_ACK" : "FAILED"));
8029}
8030
8031
8032static int wpas_ctrl_iface_mgmt_tx(struct wpa_supplicant *wpa_s, char *cmd)
8033{
8034 char *pos, *param;
8035 size_t len;
8036 u8 *buf, da[ETH_ALEN], bssid[ETH_ALEN];
8037 int res, used;
8038 int freq = 0, no_cck = 0, wait_time = 0;
8039
8040 /* <DA> <BSSID> [freq=<MHz>] [wait_time=<ms>] [no_cck=1]
8041 * <action=Action frame payload> */
8042
8043 wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
8044
8045 pos = cmd;
8046 used = hwaddr_aton2(pos, da);
8047 if (used < 0)
8048 return -1;
8049 pos += used;
8050 while (*pos == ' ')
8051 pos++;
8052 used = hwaddr_aton2(pos, bssid);
8053 if (used < 0)
8054 return -1;
8055 pos += used;
8056
8057 param = os_strstr(pos, " freq=");
8058 if (param) {
8059 param += 6;
8060 freq = atoi(param);
8061 }
8062
8063 param = os_strstr(pos, " no_cck=");
8064 if (param) {
8065 param += 8;
8066 no_cck = atoi(param);
8067 }
8068
8069 param = os_strstr(pos, " wait_time=");
8070 if (param) {
8071 param += 11;
8072 wait_time = atoi(param);
8073 }
8074
8075 param = os_strstr(pos, " action=");
8076 if (param == NULL)
8077 return -1;
8078 param += 8;
8079
8080 len = os_strlen(param);
8081 if (len & 1)
8082 return -1;
8083 len /= 2;
8084
8085 buf = os_malloc(len);
8086 if (buf == NULL)
8087 return -1;
8088
8089 if (hexstr2bin(param, buf, len) < 0) {
8090 os_free(buf);
8091 return -1;
8092 }
8093
8094 res = offchannel_send_action(wpa_s, freq, da, wpa_s->own_addr, bssid,
8095 buf, len, wait_time,
8096 wpas_ctrl_iface_mgmt_tx_cb, no_cck);
8097 os_free(buf);
8098 return res;
8099}
8100
8101
8102static void wpas_ctrl_iface_mgmt_tx_done(struct wpa_supplicant *wpa_s)
8103{
8104 wpa_printf(MSG_DEBUG, "External MGMT TX - done waiting");
8105 offchannel_send_action_done(wpa_s);
8106}
8107
ad12f2f4 8108
4de70e23
JM
8109static int wpas_ctrl_iface_mgmt_rx_process(struct wpa_supplicant *wpa_s,
8110 char *cmd)
8111{
8112 char *pos, *param;
8113 size_t len;
8114 u8 *buf;
8115 int freq = 0, datarate = 0, ssi_signal = 0;
8116 union wpa_event_data event;
8117
8118 if (!wpa_s->ext_mgmt_frame_handling)
8119 return -1;
8120
8121 /* freq=<MHz> datarate=<val> ssi_signal=<val> frame=<frame hexdump> */
8122
8123 wpa_printf(MSG_DEBUG, "External MGMT RX process: %s", cmd);
8124
8125 pos = cmd;
8126 param = os_strstr(pos, "freq=");
8127 if (param) {
8128 param += 5;
8129 freq = atoi(param);
8130 }
8131
8132 param = os_strstr(pos, " datarate=");
8133 if (param) {
8134 param += 10;
8135 datarate = atoi(param);
8136 }
8137
8138 param = os_strstr(pos, " ssi_signal=");
8139 if (param) {
8140 param += 12;
8141 ssi_signal = atoi(param);
8142 }
8143
8144 param = os_strstr(pos, " frame=");
8145 if (param == NULL)
8146 return -1;
8147 param += 7;
8148
8149 len = os_strlen(param);
8150 if (len & 1)
8151 return -1;
8152 len /= 2;
8153
8154 buf = os_malloc(len);
8155 if (buf == NULL)
8156 return -1;
8157
8158 if (hexstr2bin(param, buf, len) < 0) {
8159 os_free(buf);
8160 return -1;
8161 }
8162
8163 os_memset(&event, 0, sizeof(event));
8164 event.rx_mgmt.freq = freq;
8165 event.rx_mgmt.frame = buf;
8166 event.rx_mgmt.frame_len = len;
8167 event.rx_mgmt.ssi_signal = ssi_signal;
8168 event.rx_mgmt.datarate = datarate;
8169 wpa_s->ext_mgmt_frame_handling = 0;
8170 wpa_supplicant_event(wpa_s, EVENT_RX_MGMT, &event);
8171 wpa_s->ext_mgmt_frame_handling = 1;
8172
8173 os_free(buf);
8174
8175 return 0;
8176}
8177
8178
e4a3e1d0
JM
8179static int wpas_ctrl_iface_driver_scan_res(struct wpa_supplicant *wpa_s,
8180 char *param)
8181{
8182 struct wpa_scan_res *res;
8183 struct os_reltime now;
8184 char *pos, *end;
1fb4437c 8185 int ret = -1;
e4a3e1d0
JM
8186
8187 if (!param)
8188 return -1;
8189
8190 if (os_strcmp(param, "START") == 0) {
8191 wpa_bss_update_start(wpa_s);
8192 return 0;
8193 }
8194
8195 if (os_strcmp(param, "END") == 0) {
8196 wpa_bss_update_end(wpa_s, NULL, 1);
8197 return 0;
8198 }
8199
8200 if (os_strncmp(param, "BSS ", 4) != 0)
8201 return -1;
8202 param += 3;
8203
8204 res = os_zalloc(sizeof(*res) + os_strlen(param) / 2);
8205 if (!res)
8206 return -1;
8207
8208 pos = os_strstr(param, " flags=");
8209 if (pos)
8210 res->flags = strtol(pos + 7, NULL, 16);
8211
8212 pos = os_strstr(param, " bssid=");
1fb4437c
JM
8213 if (pos && hwaddr_aton(pos + 7, res->bssid))
8214 goto fail;
e4a3e1d0
JM
8215
8216 pos = os_strstr(param, " freq=");
8217 if (pos)
8218 res->freq = atoi(pos + 6);
8219
8220 pos = os_strstr(param, " beacon_int=");
8221 if (pos)
8222 res->beacon_int = atoi(pos + 12);
8223
8224 pos = os_strstr(param, " caps=");
8225 if (pos)
8226 res->caps = strtol(pos + 6, NULL, 16);
8227
8228 pos = os_strstr(param, " qual=");
8229 if (pos)
8230 res->qual = atoi(pos + 6);
8231
8232 pos = os_strstr(param, " noise=");
8233 if (pos)
8234 res->noise = atoi(pos + 7);
8235
8236 pos = os_strstr(param, " level=");
8237 if (pos)
8238 res->level = atoi(pos + 7);
8239
8240 pos = os_strstr(param, " tsf=");
8241 if (pos)
8242 res->tsf = strtoll(pos + 5, NULL, 16);
8243
8244 pos = os_strstr(param, " age=");
8245 if (pos)
8246 res->age = atoi(pos + 5);
8247
8248 pos = os_strstr(param, " est_throughput=");
8249 if (pos)
8250 res->est_throughput = atoi(pos + 16);
8251
8252 pos = os_strstr(param, " snr=");
8253 if (pos)
8254 res->snr = atoi(pos + 5);
8255
8256 pos = os_strstr(param, " parent_tsf=");
8257 if (pos)
8258 res->parent_tsf = strtoll(pos + 7, NULL, 16);
8259
8260 pos = os_strstr(param, " tsf_bssid=");
1fb4437c
JM
8261 if (pos && hwaddr_aton(pos + 11, res->tsf_bssid))
8262 goto fail;
e4a3e1d0
JM
8263
8264 pos = os_strstr(param, " ie=");
8265 if (pos) {
8266 pos += 4;
8267 end = os_strchr(pos, ' ');
8268 if (!end)
8269 end = pos + os_strlen(pos);
8270 res->ie_len = (end - pos) / 2;
1fb4437c
JM
8271 if (hexstr2bin(pos, (u8 *) (res + 1), res->ie_len))
8272 goto fail;
e4a3e1d0
JM
8273 }
8274
8275 pos = os_strstr(param, " beacon_ie=");
8276 if (pos) {
8277 pos += 11;
8278 end = os_strchr(pos, ' ');
8279 if (!end)
8280 end = pos + os_strlen(pos);
8281 res->beacon_ie_len = (end - pos) / 2;
1fb4437c
JM
8282 if (hexstr2bin(pos, ((u8 *) (res + 1)) + res->ie_len,
8283 res->beacon_ie_len))
8284 goto fail;
e4a3e1d0
JM
8285 }
8286
8287 os_get_reltime(&now);
8288 wpa_bss_update_scan_res(wpa_s, res, &now);
1fb4437c
JM
8289 ret = 0;
8290fail:
e4a3e1d0
JM
8291 os_free(res);
8292
1fb4437c 8293 return ret;
e4a3e1d0
JM
8294}
8295
8296
ad12f2f4
JM
8297static int wpas_ctrl_iface_driver_event(struct wpa_supplicant *wpa_s, char *cmd)
8298{
8299 char *pos, *param;
8300 union wpa_event_data event;
8301 enum wpa_event_type ev;
8302
8303 /* <event name> [parameters..] */
8304
8305 wpa_dbg(wpa_s, MSG_DEBUG, "Testing - external driver event: %s", cmd);
8306
8307 pos = cmd;
8308 param = os_strchr(pos, ' ');
8309 if (param)
8310 *param++ = '\0';
8311
8312 os_memset(&event, 0, sizeof(event));
8313
8314 if (os_strcmp(cmd, "INTERFACE_ENABLED") == 0) {
8315 ev = EVENT_INTERFACE_ENABLED;
8316 } else if (os_strcmp(cmd, "INTERFACE_DISABLED") == 0) {
8317 ev = EVENT_INTERFACE_DISABLED;
7bb70909
JM
8318 } else if (os_strcmp(cmd, "AVOID_FREQUENCIES") == 0) {
8319 ev = EVENT_AVOID_FREQUENCIES;
8320 if (param == NULL)
8321 param = "";
8322 if (freq_range_list_parse(&event.freq_range, param) < 0)
8323 return -1;
8324 wpa_supplicant_event(wpa_s, ev, &event);
8325 os_free(event.freq_range.range);
8326 return 0;
e4a3e1d0
JM
8327 } else if (os_strcmp(cmd, "SCAN_RES") == 0) {
8328 return wpas_ctrl_iface_driver_scan_res(wpa_s, param);
ad12f2f4
JM
8329 } else {
8330 wpa_dbg(wpa_s, MSG_DEBUG, "Testing - unknown driver event: %s",
8331 cmd);
8332 return -1;
8333 }
8334
8335 wpa_supplicant_event(wpa_s, ev, &event);
8336
8337 return 0;
8338}
8339
9d4ff04a
JM
8340
8341static int wpas_ctrl_iface_eapol_rx(struct wpa_supplicant *wpa_s, char *cmd)
8342{
8343 char *pos;
8344 u8 src[ETH_ALEN], *buf;
8345 int used;
8346 size_t len;
8347
8348 wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
8349
8350 pos = cmd;
8351 used = hwaddr_aton2(pos, src);
8352 if (used < 0)
8353 return -1;
8354 pos += used;
8355 while (*pos == ' ')
8356 pos++;
8357
8358 len = os_strlen(pos);
8359 if (len & 1)
8360 return -1;
8361 len /= 2;
8362
8363 buf = os_malloc(len);
8364 if (buf == NULL)
8365 return -1;
8366
8367 if (hexstr2bin(pos, buf, len) < 0) {
8368 os_free(buf);
8369 return -1;
8370 }
8371
8372 wpa_supplicant_rx_eapol(wpa_s, src, buf, len);
8373 os_free(buf);
8374
8375 return 0;
8376}
8377
4a6cc862
JM
8378
8379static u16 ipv4_hdr_checksum(const void *buf, size_t len)
8380{
8381 size_t i;
8382 u32 sum = 0;
8383 const u16 *pos = buf;
8384
8385 for (i = 0; i < len / 2; i++)
8386 sum += *pos++;
8387
8388 while (sum >> 16)
8389 sum = (sum & 0xffff) + (sum >> 16);
8390
8391 return sum ^ 0xffff;
8392}
8393
8394
8395#define HWSIM_PACKETLEN 1500
8396#define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
8397
0dbe22be
JM
8398static void wpas_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf,
8399 size_t len)
4a6cc862
JM
8400{
8401 struct wpa_supplicant *wpa_s = ctx;
8402 const struct ether_header *eth;
75352270 8403 struct iphdr ip;
4a6cc862
JM
8404 const u8 *pos;
8405 unsigned int i;
8406
8407 if (len != HWSIM_PACKETLEN)
8408 return;
8409
8410 eth = (const struct ether_header *) buf;
75352270
JM
8411 os_memcpy(&ip, eth + 1, sizeof(ip));
8412 pos = &buf[sizeof(*eth) + sizeof(ip)];
4a6cc862 8413
75352270
JM
8414 if (ip.ihl != 5 || ip.version != 4 ||
8415 ntohs(ip.tot_len) != HWSIM_IP_LEN)
4a6cc862
JM
8416 return;
8417
75352270 8418 for (i = 0; i < HWSIM_IP_LEN - sizeof(ip); i++) {
4a6cc862
JM
8419 if (*pos != (u8) i)
8420 return;
8421 pos++;
8422 }
8423
8424 wpa_msg(wpa_s, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR,
8425 MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost));
8426}
8427
8428
8429static int wpas_ctrl_iface_data_test_config(struct wpa_supplicant *wpa_s,
8430 char *cmd)
8431{
8432 int enabled = atoi(cmd);
ba91e920
MB
8433 char *pos;
8434 const char *ifname;
4a6cc862
JM
8435
8436 if (!enabled) {
8437 if (wpa_s->l2_test) {
8438 l2_packet_deinit(wpa_s->l2_test);
8439 wpa_s->l2_test = NULL;
8440 wpa_dbg(wpa_s, MSG_DEBUG, "test data: Disabled");
8441 }
8442 return 0;
8443 }
8444
8445 if (wpa_s->l2_test)
8446 return 0;
8447
ba91e920
MB
8448 pos = os_strstr(cmd, " ifname=");
8449 if (pos)
8450 ifname = pos + 8;
8451 else
8452 ifname = wpa_s->ifname;
8453
8454 wpa_s->l2_test = l2_packet_init(ifname, wpa_s->own_addr,
4a6cc862
JM
8455 ETHERTYPE_IP, wpas_data_test_rx,
8456 wpa_s, 1);
8457 if (wpa_s->l2_test == NULL)
8458 return -1;
8459
8460 wpa_dbg(wpa_s, MSG_DEBUG, "test data: Enabled");
8461
8462 return 0;
8463}
8464
8465
8466static int wpas_ctrl_iface_data_test_tx(struct wpa_supplicant *wpa_s, char *cmd)
8467{
8468 u8 dst[ETH_ALEN], src[ETH_ALEN];
8469 char *pos;
8470 int used;
8471 long int val;
8472 u8 tos;
75352270 8473 u8 buf[2 + HWSIM_PACKETLEN];
4a6cc862
JM
8474 struct ether_header *eth;
8475 struct iphdr *ip;
8476 u8 *dpos;
8477 unsigned int i;
8478
8479 if (wpa_s->l2_test == NULL)
8480 return -1;
8481
8482 /* format: <dst> <src> <tos> */
8483
8484 pos = cmd;
8485 used = hwaddr_aton2(pos, dst);
8486 if (used < 0)
8487 return -1;
8488 pos += used;
8489 while (*pos == ' ')
8490 pos++;
8491 used = hwaddr_aton2(pos, src);
8492 if (used < 0)
8493 return -1;
8494 pos += used;
8495
8496 val = strtol(pos, NULL, 0);
8497 if (val < 0 || val > 0xff)
8498 return -1;
8499 tos = val;
8500
75352270 8501 eth = (struct ether_header *) &buf[2];
4a6cc862
JM
8502 os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
8503 os_memcpy(eth->ether_shost, src, ETH_ALEN);
8504 eth->ether_type = htons(ETHERTYPE_IP);
8505 ip = (struct iphdr *) (eth + 1);
8506 os_memset(ip, 0, sizeof(*ip));
8507 ip->ihl = 5;
8508 ip->version = 4;
8509 ip->ttl = 64;
8510 ip->tos = tos;
8511 ip->tot_len = htons(HWSIM_IP_LEN);
8512 ip->protocol = 1;
66f1e078
JM
8513 ip->saddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
8514 ip->daddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
4a6cc862
JM
8515 ip->check = ipv4_hdr_checksum(ip, sizeof(*ip));
8516 dpos = (u8 *) (ip + 1);
8517 for (i = 0; i < HWSIM_IP_LEN - sizeof(*ip); i++)
8518 *dpos++ = i;
8519
75352270 8520 if (l2_packet_send(wpa_s->l2_test, dst, ETHERTYPE_IP, &buf[2],
4a6cc862
JM
8521 HWSIM_PACKETLEN) < 0)
8522 return -1;
8523
8524 wpa_dbg(wpa_s, MSG_DEBUG, "test data: TX dst=" MACSTR " src=" MACSTR
8525 " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
8526
8527 return 0;
8528}
8529
fc0ef7c0
JM
8530
8531static int wpas_ctrl_iface_data_test_frame(struct wpa_supplicant *wpa_s,
8532 char *cmd)
8533{
8534 u8 *buf;
8535 struct ether_header *eth;
8536 struct l2_packet_data *l2 = NULL;
8537 size_t len;
8538 u16 ethertype;
8539 int res = -1;
8540
8541 len = os_strlen(cmd);
8542 if (len & 1 || len < ETH_HLEN * 2)
8543 return -1;
8544 len /= 2;
8545
8546 buf = os_malloc(len);
8547 if (buf == NULL)
8548 return -1;
8549
8550 if (hexstr2bin(cmd, buf, len) < 0)
8551 goto done;
8552
8553 eth = (struct ether_header *) buf;
8554 ethertype = ntohs(eth->ether_type);
8555
8556 l2 = l2_packet_init(wpa_s->ifname, wpa_s->own_addr, ethertype,
8557 wpas_data_test_rx, wpa_s, 1);
8558 if (l2 == NULL)
8559 goto done;
8560
8561 res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
8562 wpa_dbg(wpa_s, MSG_DEBUG, "test data: TX frame res=%d", res);
8563done:
8564 if (l2)
8565 l2_packet_deinit(l2);
8566 os_free(buf);
8567
8568 return res < 0 ? -1 : 0;
8569}
8570
a156ffda
JM
8571
8572static int wpas_ctrl_test_alloc_fail(struct wpa_supplicant *wpa_s, char *cmd)
8573{
8574#ifdef WPA_TRACE_BFD
a156ffda
JM
8575 char *pos;
8576
8577 wpa_trace_fail_after = atoi(cmd);
8578 pos = os_strchr(cmd, ':');
8579 if (pos) {
8580 pos++;
8581 os_strlcpy(wpa_trace_fail_func, pos,
8582 sizeof(wpa_trace_fail_func));
8583 } else {
8584 wpa_trace_fail_after = 0;
8585 }
8586 return 0;
8587#else /* WPA_TRACE_BFD */
8588 return -1;
8589#endif /* WPA_TRACE_BFD */
8590}
8591
8592
8593static int wpas_ctrl_get_alloc_fail(struct wpa_supplicant *wpa_s,
8594 char *buf, size_t buflen)
8595{
8596#ifdef WPA_TRACE_BFD
a156ffda
JM
8597 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after,
8598 wpa_trace_fail_func);
8599#else /* WPA_TRACE_BFD */
8600 return -1;
8601#endif /* WPA_TRACE_BFD */
8602}
8603
2da52565
JM
8604
8605static int wpas_ctrl_test_fail(struct wpa_supplicant *wpa_s, char *cmd)
8606{
8607#ifdef WPA_TRACE_BFD
2da52565
JM
8608 char *pos;
8609
8610 wpa_trace_test_fail_after = atoi(cmd);
8611 pos = os_strchr(cmd, ':');
8612 if (pos) {
8613 pos++;
8614 os_strlcpy(wpa_trace_test_fail_func, pos,
8615 sizeof(wpa_trace_test_fail_func));
8616 } else {
8617 wpa_trace_test_fail_after = 0;
8618 }
8619 return 0;
8620#else /* WPA_TRACE_BFD */
8621 return -1;
8622#endif /* WPA_TRACE_BFD */
8623}
8624
8625
8626static int wpas_ctrl_get_fail(struct wpa_supplicant *wpa_s,
8627 char *buf, size_t buflen)
8628{
8629#ifdef WPA_TRACE_BFD
2da52565
JM
8630 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_test_fail_after,
8631 wpa_trace_test_fail_func);
8632#else /* WPA_TRACE_BFD */
8633 return -1;
8634#endif /* WPA_TRACE_BFD */
8635}
8636
a530fe77
JM
8637
8638static void wpas_ctrl_event_test_cb(void *eloop_ctx, void *timeout_ctx)
8639{
8640 struct wpa_supplicant *wpa_s = eloop_ctx;
8641 int i, count = (intptr_t) timeout_ctx;
8642
8643 wpa_printf(MSG_DEBUG, "TEST: Send %d control interface event messages",
8644 count);
8645 for (i = 0; i < count; i++) {
8646 wpa_msg_ctrl(wpa_s, MSG_INFO, "TEST-EVENT-MESSAGE %d/%d",
8647 i + 1, count);
8648 }
8649}
8650
8651
8652static int wpas_ctrl_event_test(struct wpa_supplicant *wpa_s, const char *cmd)
8653{
8654 int count;
8655
8656 count = atoi(cmd);
8657 if (count <= 0)
8658 return -1;
8659
8660 return eloop_register_timeout(0, 0, wpas_ctrl_event_test_cb, wpa_s,
8661 (void *) (intptr_t) count);
8662}
8663
651c6a84
JM
8664
8665static int wpas_ctrl_test_assoc_ie(struct wpa_supplicant *wpa_s,
8666 const char *cmd)
8667{
8668 struct wpabuf *buf;
8669 size_t len;
8670
8671 len = os_strlen(cmd);
8672 if (len & 1)
8673 return -1;
8674 len /= 2;
8675
8676 if (len == 0) {
8677 buf = NULL;
8678 } else {
8679 buf = wpabuf_alloc(len);
8680 if (buf == NULL)
8681 return -1;
8682
8683 if (hexstr2bin(cmd, wpabuf_put(buf, len), len) < 0) {
8684 wpabuf_free(buf);
8685 return -1;
8686 }
8687 }
8688
8689 wpa_sm_set_test_assoc_ie(wpa_s->wpa, buf);
8690 return 0;
8691}
8692
60b893df
JM
8693#endif /* CONFIG_TESTING_OPTIONS */
8694
8695
86bd36f0
JM
8696static int wpas_ctrl_vendor_elem_add(struct wpa_supplicant *wpa_s, char *cmd)
8697{
8698 char *pos = cmd;
8699 int frame;
8700 size_t len;
8701 struct wpabuf *buf;
8702 struct ieee802_11_elems elems;
8703
8704 frame = atoi(pos);
8705 if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
8706 return -1;
af041f99 8707 wpa_s = wpas_vendor_elem(wpa_s, frame);
86bd36f0
JM
8708
8709 pos = os_strchr(pos, ' ');
8710 if (pos == NULL)
8711 return -1;
8712 pos++;
8713
8714 len = os_strlen(pos);
8715 if (len == 0)
8716 return 0;
8717 if (len & 1)
8718 return -1;
8719 len /= 2;
8720
8721 buf = wpabuf_alloc(len);
8722 if (buf == NULL)
8723 return -1;
8724
8725 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
8726 wpabuf_free(buf);
8727 return -1;
8728 }
8729
8730 if (ieee802_11_parse_elems(wpabuf_head_u8(buf), len, &elems, 0) ==
8731 ParseFailed) {
8732 wpabuf_free(buf);
8733 return -1;
8734 }
8735
8736 if (wpa_s->vendor_elem[frame] == NULL) {
8737 wpa_s->vendor_elem[frame] = buf;
af041f99 8738 wpas_vendor_elem_update(wpa_s);
86bd36f0
JM
8739 return 0;
8740 }
8741
8742 if (wpabuf_resize(&wpa_s->vendor_elem[frame], len) < 0) {
8743 wpabuf_free(buf);
8744 return -1;
8745 }
8746
8747 wpabuf_put_buf(wpa_s->vendor_elem[frame], buf);
8748 wpabuf_free(buf);
af041f99 8749 wpas_vendor_elem_update(wpa_s);
86bd36f0
JM
8750
8751 return 0;
8752}
8753
8754
8755static int wpas_ctrl_vendor_elem_get(struct wpa_supplicant *wpa_s, char *cmd,
8756 char *buf, size_t buflen)
8757{
8758 int frame = atoi(cmd);
8759
8760 if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
8761 return -1;
af041f99 8762 wpa_s = wpas_vendor_elem(wpa_s, frame);
86bd36f0
JM
8763
8764 if (wpa_s->vendor_elem[frame] == NULL)
8765 return 0;
8766
8767 return wpa_snprintf_hex(buf, buflen,
8768 wpabuf_head_u8(wpa_s->vendor_elem[frame]),
8769 wpabuf_len(wpa_s->vendor_elem[frame]));
8770}
8771
8772
8773static int wpas_ctrl_vendor_elem_remove(struct wpa_supplicant *wpa_s, char *cmd)
8774{
8775 char *pos = cmd;
8776 int frame;
8777 size_t len;
8778 u8 *buf;
8779 struct ieee802_11_elems elems;
af041f99 8780 int res;
86bd36f0
JM
8781
8782 frame = atoi(pos);
8783 if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
8784 return -1;
af041f99 8785 wpa_s = wpas_vendor_elem(wpa_s, frame);
86bd36f0
JM
8786
8787 pos = os_strchr(pos, ' ');
8788 if (pos == NULL)
8789 return -1;
8790 pos++;
8791
8792 if (*pos == '*') {
8793 wpabuf_free(wpa_s->vendor_elem[frame]);
8794 wpa_s->vendor_elem[frame] = NULL;
af041f99 8795 wpas_vendor_elem_update(wpa_s);
86bd36f0
JM
8796 return 0;
8797 }
8798
8799 if (wpa_s->vendor_elem[frame] == NULL)
8800 return -1;
8801
8802 len = os_strlen(pos);
8803 if (len == 0)
8804 return 0;
8805 if (len & 1)
8806 return -1;
8807 len /= 2;
8808
8809 buf = os_malloc(len);
8810 if (buf == NULL)
8811 return -1;
8812
8813 if (hexstr2bin(pos, buf, len) < 0) {
8814 os_free(buf);
8815 return -1;
8816 }
8817
8818 if (ieee802_11_parse_elems(buf, len, &elems, 0) == ParseFailed) {
8819 os_free(buf);
8820 return -1;
8821 }
8822
af041f99 8823 res = wpas_vendor_elem_remove(wpa_s, frame, buf, len);
86bd36f0 8824 os_free(buf);
af041f99 8825 return res;
86bd36f0
JM
8826}
8827
8828
f4b8bfae
AK
8829static void wpas_ctrl_neighbor_rep_cb(void *ctx, struct wpabuf *neighbor_rep)
8830{
8831 struct wpa_supplicant *wpa_s = ctx;
cf667c66
IP
8832 size_t len;
8833 const u8 *data;
f4b8bfae 8834
cf667c66
IP
8835 /*
8836 * Neighbor Report element (IEEE P802.11-REVmc/D5.0)
8837 * BSSID[6]
8838 * BSSID Information[4]
8839 * Operating Class[1]
8840 * Channel Number[1]
8841 * PHY Type[1]
8842 * Optional Subelements[variable]
8843 */
8844#define NR_IE_MIN_LEN (ETH_ALEN + 4 + 1 + 1 + 1)
8845
8846 if (!neighbor_rep || wpabuf_len(neighbor_rep) == 0) {
f4b8bfae 8847 wpa_msg_ctrl(wpa_s, MSG_INFO, RRM_EVENT_NEIGHBOR_REP_FAILED);
cf667c66
IP
8848 goto out;
8849 }
8850
8851 data = wpabuf_head_u8(neighbor_rep);
8852 len = wpabuf_len(neighbor_rep);
8853
8854 while (len >= 2 + NR_IE_MIN_LEN) {
8855 const u8 *nr;
8856 char lci[256 * 2 + 1];
8857 char civic[256 * 2 + 1];
8858 u8 nr_len = data[1];
8859 const u8 *pos = data, *end;
8860
8861 if (pos[0] != WLAN_EID_NEIGHBOR_REPORT ||
8862 nr_len < NR_IE_MIN_LEN) {
8863 wpa_printf(MSG_DEBUG,
8864 "CTRL: Invalid Neighbor Report element: id=%u len=%u",
8865 data[0], nr_len);
8866 goto out;
8867 }
8868
8869 if (2U + nr_len > len) {
8870 wpa_printf(MSG_DEBUG,
8871 "CTRL: Invalid Neighbor Report element: id=%u len=%zu nr_len=%u",
8872 data[0], len, nr_len);
8873 goto out;
8874 }
8875 pos += 2;
8876 end = pos + nr_len;
8877
8878 nr = pos;
8879 pos += NR_IE_MIN_LEN;
8880
8881 lci[0] = '\0';
8882 civic[0] = '\0';
8883 while (end - pos > 2) {
8884 u8 s_id, s_len;
8885
8886 s_id = *pos++;
8887 s_len = *pos++;
8888 if (s_len > end - pos)
8889 goto out;
8890 if (s_id == WLAN_EID_MEASURE_REPORT && s_len > 3) {
8891 /* Measurement Token[1] */
8892 /* Measurement Report Mode[1] */
8893 /* Measurement Type[1] */
8894 /* Measurement Report[variable] */
8895 switch (pos[2]) {
8896 case MEASURE_TYPE_LCI:
8897 if (lci[0])
8898 break;
8899 wpa_snprintf_hex(lci, sizeof(lci),
8900 pos, s_len);
8901 break;
8902 case MEASURE_TYPE_LOCATION_CIVIC:
8903 if (civic[0])
8904 break;
8905 wpa_snprintf_hex(civic, sizeof(civic),
8906 pos, s_len);
8907 break;
8908 }
8909 }
8910
8911 pos += s_len;
8912 }
8913
8914 wpa_msg(wpa_s, MSG_INFO, RRM_EVENT_NEIGHBOR_REP_RXED
8915 "bssid=" MACSTR
8916 " info=0x%x op_class=%u chan=%u phy_type=%u%s%s%s%s",
8917 MAC2STR(nr), WPA_GET_LE32(nr + ETH_ALEN),
8918 nr[ETH_ALEN + 4], nr[ETH_ALEN + 5],
8919 nr[ETH_ALEN + 6],
8920 lci[0] ? " lci=" : "", lci,
8921 civic[0] ? " civic=" : "", civic);
8922
8923 data = end;
8924 len -= 2 + nr_len;
f4b8bfae 8925 }
cf667c66
IP
8926
8927out:
8928 wpabuf_free(neighbor_rep);
f4b8bfae
AK
8929}
8930
8931
6a4f0ed7
JM
8932static int wpas_ctrl_iface_send_neighbor_rep(struct wpa_supplicant *wpa_s,
8933 char *cmd)
f4b8bfae 8934{
d41a5352
DS
8935 struct wpa_ssid_value ssid, *ssid_p = NULL;
8936 int ret, lci = 0, civic = 0;
8937 char *ssid_s;
4c4b2305 8938
d41a5352
DS
8939 ssid_s = os_strstr(cmd, "ssid=");
8940 if (ssid_s) {
8941 if (ssid_parse(ssid_s + 5, &ssid)) {
8942 wpa_printf(MSG_ERROR,
8943 "CTRL: Send Neighbor Report: bad SSID");
4c4b2305 8944 return -1;
d41a5352
DS
8945 }
8946
4c4b2305 8947 ssid_p = &ssid;
d41a5352
DS
8948
8949 /*
8950 * Move cmd after the SSID text that may include "lci" or
8951 * "civic".
8952 */
8953 cmd = os_strchr(ssid_s + 6, ssid_s[5] == '"' ? '"' : ' ');
8954 if (cmd)
8955 cmd++;
8956
4c4b2305
AK
8957 }
8958
d41a5352
DS
8959 if (cmd && os_strstr(cmd, "lci"))
8960 lci = 1;
8961
8962 if (cmd && os_strstr(cmd, "civic"))
8963 civic = 1;
8964
8965 ret = wpas_rrm_send_neighbor_rep_request(wpa_s, ssid_p, lci, civic,
4c4b2305
AK
8966 wpas_ctrl_neighbor_rep_cb,
8967 wpa_s);
8968
8969 return ret;
f4b8bfae
AK
8970}
8971
8972
65d9a5e2
JM
8973static int wpas_ctrl_iface_erp_flush(struct wpa_supplicant *wpa_s)
8974{
8975 eapol_sm_erp_flush(wpa_s->eapol);
8976 return 0;
8977}
8978
8979
fb375883
IP
8980static int wpas_ctrl_iface_mac_rand_scan(struct wpa_supplicant *wpa_s,
8981 char *cmd)
8982{
8983 char *token, *context = NULL;
8984 unsigned int enable = ~0, type = 0;
8985 u8 _addr[ETH_ALEN], _mask[ETH_ALEN];
8986 u8 *addr = NULL, *mask = NULL;
8987
8988 while ((token = str_token(cmd, " ", &context))) {
8989 if (os_strcasecmp(token, "scan") == 0) {
8990 type |= MAC_ADDR_RAND_SCAN;
8991 } else if (os_strcasecmp(token, "sched") == 0) {
8992 type |= MAC_ADDR_RAND_SCHED_SCAN;
8993 } else if (os_strcasecmp(token, "pno") == 0) {
8994 type |= MAC_ADDR_RAND_PNO;
8995 } else if (os_strcasecmp(token, "all") == 0) {
8996 type = wpa_s->mac_addr_rand_supported;
8997 } else if (os_strncasecmp(token, "enable=", 7) == 0) {
8998 enable = atoi(token + 7);
8999 } else if (os_strncasecmp(token, "addr=", 5) == 0) {
9000 addr = _addr;
9001 if (hwaddr_aton(token + 5, addr)) {
9002 wpa_printf(MSG_INFO,
9003 "CTRL: Invalid MAC address: %s",
9004 token);
9005 return -1;
9006 }
9007 } else if (os_strncasecmp(token, "mask=", 5) == 0) {
9008 mask = _mask;
9009 if (hwaddr_aton(token + 5, mask)) {
9010 wpa_printf(MSG_INFO,
9011 "CTRL: Invalid MAC address mask: %s",
9012 token);
9013 return -1;
9014 }
9015 } else {
9016 wpa_printf(MSG_INFO,
9017 "CTRL: Invalid MAC_RAND_SCAN parameter: %s",
9018 token);
9019 return -1;
9020 }
9021 }
9022
9023 if (!type) {
9024 wpa_printf(MSG_INFO, "CTRL: MAC_RAND_SCAN no type specified");
9025 return -1;
9026 }
9027
9028 if ((wpa_s->mac_addr_rand_supported & type) != type) {
9029 wpa_printf(MSG_INFO,
9030 "CTRL: MAC_RAND_SCAN types=%u != supported=%u",
9031 type, wpa_s->mac_addr_rand_supported);
9032 return -1;
9033 }
9034
9035 if (enable > 1) {
9036 wpa_printf(MSG_INFO,
9037 "CTRL: MAC_RAND_SCAN enable=<0/1> not specified");
9038 return -1;
9039 }
9040
9041 if (!enable) {
9042 wpas_mac_addr_rand_scan_clear(wpa_s, type);
9043 if (wpa_s->pno) {
9044 if (type & MAC_ADDR_RAND_PNO) {
9045 wpas_stop_pno(wpa_s);
9046 wpas_start_pno(wpa_s);
9047 }
9048 } else if (wpa_s->sched_scanning &&
9049 (type & MAC_ADDR_RAND_SCHED_SCAN)) {
5bb7327a 9050 wpas_scan_restart_sched_scan(wpa_s);
fb375883
IP
9051 }
9052 return 0;
9053 }
9054
9055 if ((addr && !mask) || (!addr && mask)) {
9056 wpa_printf(MSG_INFO,
9057 "CTRL: MAC_RAND_SCAN invalid addr/mask combination");
9058 return -1;
9059 }
9060
9061 if (addr && mask && (!(mask[0] & 0x01) || (addr[0] & 0x01))) {
9062 wpa_printf(MSG_INFO,
9063 "CTRL: MAC_RAND_SCAN cannot allow multicast address");
9064 return -1;
9065 }
9066
9067 if (type & MAC_ADDR_RAND_SCAN) {
9068 wpas_mac_addr_rand_scan_set(wpa_s, MAC_ADDR_RAND_SCAN,
9069 addr, mask);
9070 }
9071
9072 if (type & MAC_ADDR_RAND_SCHED_SCAN) {
9073 wpas_mac_addr_rand_scan_set(wpa_s, MAC_ADDR_RAND_SCHED_SCAN,
9074 addr, mask);
9075
5bb7327a
JM
9076 if (wpa_s->sched_scanning && !wpa_s->pno)
9077 wpas_scan_restart_sched_scan(wpa_s);
fb375883
IP
9078 }
9079
9080 if (type & MAC_ADDR_RAND_PNO) {
9081 wpas_mac_addr_rand_scan_set(wpa_s, MAC_ADDR_RAND_PNO,
9082 addr, mask);
9083 if (wpa_s->pno) {
9084 wpas_stop_pno(wpa_s);
9085 wpas_start_pno(wpa_s);
9086 }
9087 }
9088
9089 return 0;
9090}
9091
9092
b8daac18
MH
9093static int wpas_ctrl_iface_pmksa(struct wpa_supplicant *wpa_s,
9094 char *buf, size_t buflen)
9095{
9096 size_t reply_len;
9097
9098 reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, buf, buflen);
9099#ifdef CONFIG_AP
9100 reply_len += wpas_ap_pmksa_cache_list(wpa_s, &buf[reply_len],
9101 buflen - reply_len);
9102#endif /* CONFIG_AP */
9103 return reply_len;
9104}
9105
9106
4c522c77
MH
9107static void wpas_ctrl_iface_pmksa_flush(struct wpa_supplicant *wpa_s)
9108{
9109 wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
9110#ifdef CONFIG_AP
9111 wpas_ap_pmksa_cache_flush(wpa_s);
9112#endif /* CONFIG_AP */
9113}
9114
9115
3459381d
JM
9116#ifdef CONFIG_PMKSA_CACHE_EXTERNAL
9117
9118static int wpas_ctrl_iface_pmksa_get(struct wpa_supplicant *wpa_s,
9119 const char *cmd, char *buf, size_t buflen)
9120{
9121 struct rsn_pmksa_cache_entry *entry;
9122 struct wpa_ssid *ssid;
9123 char *pos, *pos2, *end;
9124 int ret;
9125 struct os_reltime now;
9126
9127 ssid = wpa_config_get_network(wpa_s->conf, atoi(cmd));
9128 if (!ssid)
9129 return -1;
9130
9131 pos = buf;
9132 end = buf + buflen;
9133
9134 os_get_reltime(&now);
9135
9136 /*
9137 * Entry format:
9138 * <BSSID> <PMKID> <PMK> <reauth_time in seconds>
9139 * <expiration in seconds> <akmp> <opportunistic>
9140 */
9141
9142 for (entry = wpa_sm_pmksa_cache_head(wpa_s->wpa); entry;
9143 entry = entry->next) {
9144 if (entry->network_ctx != ssid)
9145 continue;
9146
9147 pos2 = pos;
9148 ret = os_snprintf(pos2, end - pos2, MACSTR " ",
9149 MAC2STR(entry->aa));
9150 if (os_snprintf_error(end - pos2, ret))
9151 break;
9152 pos2 += ret;
9153
9154 pos2 += wpa_snprintf_hex(pos2, end - pos2, entry->pmkid,
9155 PMKID_LEN);
9156
9157 ret = os_snprintf(pos2, end - pos2, " ");
9158 if (os_snprintf_error(end - pos2, ret))
9159 break;
9160 pos2 += ret;
9161
9162 pos2 += wpa_snprintf_hex(pos2, end - pos2, entry->pmk,
9163 entry->pmk_len);
9164
9165 ret = os_snprintf(pos2, end - pos2, " %d %d %d %d",
9166 (int) (entry->reauth_time - now.sec),
9167 (int) (entry->expiration - now.sec),
9168 entry->akmp,
9169 entry->opportunistic);
9170 if (os_snprintf_error(end - pos2, ret))
9171 break;
9172 pos2 += ret;
9173
9174 ret = os_snprintf(pos2, end - pos2, "\n");
9175 if (os_snprintf_error(end - pos2, ret))
9176 break;
9177 pos2 += ret;
9178
9179 pos = pos2;
9180 }
9181
9182 return pos - buf;
9183}
9184
9185
9186static int wpas_ctrl_iface_pmksa_add(struct wpa_supplicant *wpa_s,
9187 char *cmd)
9188{
9189 struct rsn_pmksa_cache_entry *entry;
9190 struct wpa_ssid *ssid;
9191 char *pos, *pos2;
9192 int ret = -1;
9193 struct os_reltime now;
9194 int reauth_time = 0, expiration = 0;
9195
9196 /*
9197 * Entry format:
9198 * <network_id> <BSSID> <PMKID> <PMK> <reauth_time in seconds>
9199 * <expiration in seconds> <akmp> <opportunistic>
9200 */
9201
9202 ssid = wpa_config_get_network(wpa_s->conf, atoi(cmd));
9203 if (!ssid)
9204 return -1;
9205
9206 pos = os_strchr(cmd, ' ');
9207 if (!pos)
9208 return -1;
9209 pos++;
9210
9211 entry = os_zalloc(sizeof(*entry));
9212 if (!entry)
9213 return -1;
9214
9215 if (hwaddr_aton(pos, entry->aa))
9216 goto fail;
9217
9218 pos = os_strchr(pos, ' ');
9219 if (!pos)
9220 goto fail;
9221 pos++;
9222
9223 if (hexstr2bin(pos, entry->pmkid, PMKID_LEN) < 0)
9224 goto fail;
9225
9226 pos = os_strchr(pos, ' ');
9227 if (!pos)
9228 goto fail;
9229 pos++;
9230
9231 pos2 = os_strchr(pos, ' ');
9232 if (!pos2)
9233 goto fail;
9234 entry->pmk_len = (pos2 - pos) / 2;
9235 if (entry->pmk_len < PMK_LEN || entry->pmk_len > PMK_LEN_MAX ||
9236 hexstr2bin(pos, entry->pmk, entry->pmk_len) < 0)
9237 goto fail;
9238
9239 pos = os_strchr(pos, ' ');
9240 if (!pos)
9241 goto fail;
9242 pos++;
9243
9244 if (sscanf(pos, "%d %d %d %d", &reauth_time, &expiration,
9245 &entry->akmp, &entry->opportunistic) != 4)
9246 goto fail;
9247 os_get_reltime(&now);
9248 entry->expiration = now.sec + expiration;
9249 entry->reauth_time = now.sec + reauth_time;
9250
9251 entry->network_ctx = ssid;
9252
9253 wpa_sm_pmksa_cache_add_entry(wpa_s->wpa, entry);
9254 entry = NULL;
9255 ret = 0;
9256fail:
9257 os_free(entry);
9258 return ret;
9259}
9260
4d77d80e
MH
9261
9262#ifdef CONFIG_MESH
9263
9264static int wpas_ctrl_iface_mesh_pmksa_get(struct wpa_supplicant *wpa_s,
9265 const char *cmd, char *buf,
9266 size_t buflen)
9267{
9268 u8 spa[ETH_ALEN];
9269
9270 if (!wpa_s->ifmsh)
9271 return -1;
9272
9273 if (os_strcasecmp(cmd, "any") == 0)
9274 return wpas_ap_pmksa_cache_list_mesh(wpa_s, NULL, buf, buflen);
9275
9276 if (hwaddr_aton(cmd, spa))
9277 return -1;
9278
9279 return wpas_ap_pmksa_cache_list_mesh(wpa_s, spa, buf, buflen);
9280}
9281
9282
9283static int wpas_ctrl_iface_mesh_pmksa_add(struct wpa_supplicant *wpa_s,
9284 char *cmd)
9285{
9286 /*
9287 * We do not check mesh interface existance because PMKSA should be
9288 * stored before wpa_s->ifmsh creation to suppress commit message
9289 * creation.
9290 */
9291 return wpas_ap_pmksa_cache_add_external(wpa_s, cmd);
9292}
9293
9294#endif /* CONFIG_MESH */
3459381d
JM
9295#endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
9296
9297
5732b770
JM
9298#ifdef CONFIG_FILS
9299static int wpas_ctrl_iface_fils_hlp_req_add(struct wpa_supplicant *wpa_s,
9300 const char *cmd)
9301{
9302 struct fils_hlp_req *req;
9303 const char *pos;
9304
9305 /* format: <dst> <packet starting from ethertype> */
9306
9307 req = os_zalloc(sizeof(*req));
9308 if (!req)
9309 return -1;
9310
9311 if (hwaddr_aton(cmd, req->dst))
9312 goto fail;
9313
9314 pos = os_strchr(cmd, ' ');
9315 if (!pos)
9316 goto fail;
9317 pos++;
9318 req->pkt = wpabuf_parse_bin(pos);
9319 if (!req->pkt)
9320 goto fail;
9321
9322 dl_list_add_tail(&wpa_s->fils_hlp_req, &req->list);
9323 return 0;
9324fail:
9325 wpabuf_free(req->pkt);
9326 os_free(req);
9327 return -1;
9328}
9329#endif /* CONFIG_FILS */
9330
9331
8db9a79d
JM
9332static int wpas_ctrl_cmd_debug_level(const char *cmd)
9333{
9334 if (os_strcmp(cmd, "PING") == 0 ||
9335 os_strncmp(cmd, "BSS ", 4) == 0 ||
9336 os_strncmp(cmd, "GET_NETWORK ", 12) == 0 ||
9337 os_strncmp(cmd, "STATUS", 6) == 0 ||
9338 os_strncmp(cmd, "STA ", 4) == 0 ||
9339 os_strncmp(cmd, "STA-", 4) == 0)
9340 return MSG_EXCESSIVE;
9341 return MSG_DEBUG;
9342}
9343
9344
6fc6879b
JM
9345char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
9346 char *buf, size_t *resp_len)
9347{
9348 char *reply;
b563b388 9349 const int reply_size = 4096;
6fc6879b
JM
9350 int reply_len;
9351
9352 if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
d31b5ac7
JM
9353 os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
9354 if (wpa_debug_show_keys)
9355 wpa_dbg(wpa_s, MSG_DEBUG,
9356 "Control interface command '%s'", buf);
9357 else
9358 wpa_dbg(wpa_s, MSG_DEBUG,
9359 "Control interface command '%s [REMOVED]'",
9360 os_strncmp(buf, WPA_CTRL_RSP,
9361 os_strlen(WPA_CTRL_RSP)) == 0 ?
9362 WPA_CTRL_RSP : "SET_NETWORK");
9363 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ", 16) == 0 ||
679f2e7c 9364 os_strncmp(buf, "NFC_REPORT_HANDOVER", 19) == 0) {
6fc6879b
JM
9365 wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
9366 (const u8 *) buf, os_strlen(buf));
9367 } else {
8db9a79d 9368 int level = wpas_ctrl_cmd_debug_level(buf);
b470b2bf 9369 wpa_dbg(wpa_s, level, "Control interface command '%s'", buf);
6fc6879b
JM
9370 }
9371
9372 reply = os_malloc(reply_size);
9373 if (reply == NULL) {
9374 *resp_len = 1;
9375 return NULL;
9376 }
9377
9378 os_memcpy(reply, "OK\n", 3);
9379 reply_len = 3;
9380
9381 if (os_strcmp(buf, "PING") == 0) {
9382 os_memcpy(reply, "PONG\n", 5);
9383 reply_len = 5;
0eed2a8d
JD
9384 } else if (os_strcmp(buf, "IFNAME") == 0) {
9385 reply_len = os_strlen(wpa_s->ifname);
9386 os_memcpy(reply, wpa_s->ifname, reply_len);
ac6912b5
BG
9387 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
9388 if (wpa_debug_reopen_file() < 0)
9389 reply_len = -1;
77895cd9
JM
9390 } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
9391 wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
6fc6879b
JM
9392 } else if (os_strcmp(buf, "MIB") == 0) {
9393 reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
9394 if (reply_len >= 0) {
5ac73acf
JM
9395 reply_len += eapol_sm_get_mib(wpa_s->eapol,
9396 reply + reply_len,
9397 reply_size - reply_len);
6fc6879b
JM
9398 }
9399 } else if (os_strncmp(buf, "STATUS", 6) == 0) {
9400 reply_len = wpa_supplicant_ctrl_iface_status(
9401 wpa_s, buf + 6, reply, reply_size);
9402 } else if (os_strcmp(buf, "PMKSA") == 0) {
b8daac18 9403 reply_len = wpas_ctrl_iface_pmksa(wpa_s, reply, reply_size);
79e2b1cc 9404 } else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
4c522c77 9405 wpas_ctrl_iface_pmksa_flush(wpa_s);
3459381d
JM
9406#ifdef CONFIG_PMKSA_CACHE_EXTERNAL
9407 } else if (os_strncmp(buf, "PMKSA_GET ", 10) == 0) {
9408 reply_len = wpas_ctrl_iface_pmksa_get(wpa_s, buf + 10,
9409 reply, reply_size);
9410 } else if (os_strncmp(buf, "PMKSA_ADD ", 10) == 0) {
9411 if (wpas_ctrl_iface_pmksa_add(wpa_s, buf + 10) < 0)
9412 reply_len = -1;
4d77d80e
MH
9413#ifdef CONFIG_MESH
9414 } else if (os_strncmp(buf, "MESH_PMKSA_GET ", 15) == 0) {
9415 reply_len = wpas_ctrl_iface_mesh_pmksa_get(wpa_s, buf + 15,
9416 reply, reply_size);
9417 } else if (os_strncmp(buf, "MESH_PMKSA_ADD ", 15) == 0) {
9418 if (wpas_ctrl_iface_mesh_pmksa_add(wpa_s, buf + 15) < 0)
9419 reply_len = -1;
9420#endif /* CONFIG_MESH */
3459381d 9421#endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
6fc6879b
JM
9422 } else if (os_strncmp(buf, "SET ", 4) == 0) {
9423 if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
9424 reply_len = -1;
10263dc2
OO
9425 } else if (os_strncmp(buf, "DUMP", 4) == 0) {
9426 reply_len = wpa_config_dump_values(wpa_s->conf,
9427 reply, reply_size);
acec8d32
JM
9428 } else if (os_strncmp(buf, "GET ", 4) == 0) {
9429 reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
9430 reply, reply_size);
6fc6879b
JM
9431 } else if (os_strcmp(buf, "LOGON") == 0) {
9432 eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
9433 } else if (os_strcmp(buf, "LOGOFF") == 0) {
9434 eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
9435 } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
8401a6b0
JM
9436 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
9437 reply_len = -1;
9796a86c
JM
9438 else
9439 wpas_request_connection(wpa_s);
0f44ec8e
PQ
9440 } else if (os_strcmp(buf, "REATTACH") == 0) {
9441 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED ||
9442 !wpa_s->current_ssid)
9443 reply_len = -1;
9444 else {
9445 wpa_s->reattach = 1;
9446 wpas_request_connection(wpa_s);
9447 }
6fc6879b 9448 } else if (os_strcmp(buf, "RECONNECT") == 0) {
8401a6b0
JM
9449 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
9450 reply_len = -1;
9796a86c
JM
9451 else if (wpa_s->disconnected)
9452 wpas_request_connection(wpa_s);
ec717917 9453#ifdef IEEE8021X_EAPOL
6fc6879b
JM
9454 } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
9455 if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
9456 reply_len = -1;
ec717917 9457#endif /* IEEE8021X_EAPOL */
6fc6879b
JM
9458#ifdef CONFIG_PEERKEY
9459 } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
9460 if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
9461 reply_len = -1;
9462#endif /* CONFIG_PEERKEY */
9463#ifdef CONFIG_IEEE80211R
9464 } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
9465 if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
9466 reply_len = -1;
9467#endif /* CONFIG_IEEE80211R */
fcc60db4
JM
9468#ifdef CONFIG_WPS
9469 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
3152ff42
CWY
9470 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
9471 if (res == -2) {
9472 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
9473 reply_len = 17;
9474 } else if (res)
fcc60db4
JM
9475 reply_len = -1;
9476 } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
3152ff42
CWY
9477 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
9478 if (res == -2) {
9479 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
9480 reply_len = 17;
9481 } else if (res)
fcc60db4
JM
9482 reply_len = -1;
9483 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
9484 reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
9485 reply,
9486 reply_size);
3981cb3c
JM
9487 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
9488 reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
9489 wpa_s, buf + 14, reply, reply_size);
2f9929ff
AC
9490 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
9491 if (wpas_wps_cancel(wpa_s))
9492 reply_len = -1;
71892384 9493#ifdef CONFIG_WPS_NFC
3f2c8ba6
JM
9494 } else if (os_strcmp(buf, "WPS_NFC") == 0) {
9495 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
9496 reply_len = -1;
9497 } else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
9498 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
9499 reply_len = -1;
bbf41865
JM
9500 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
9501 reply_len = wpa_supplicant_ctrl_iface_wps_nfc_config_token(
9502 wpa_s, buf + 21, reply, reply_size);
3f2c8ba6
JM
9503 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
9504 reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
9505 wpa_s, buf + 14, reply, reply_size);
d7645d23
JM
9506 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
9507 if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
9508 buf + 17))
9509 reply_len = -1;
e65552dd
JM
9510 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_REQ ", 21) == 0) {
9511 reply_len = wpas_ctrl_nfc_get_handover_req(
9512 wpa_s, buf + 21, reply, reply_size);
9513 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
9514 reply_len = wpas_ctrl_nfc_get_handover_sel(
9515 wpa_s, buf + 21, reply, reply_size);
e4758827
JM
9516 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
9517 if (wpas_ctrl_nfc_report_handover(wpa_s, buf + 20))
9518 reply_len = -1;
71892384 9519#endif /* CONFIG_WPS_NFC */
fcc60db4
JM
9520 } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
9521 if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
9522 reply_len = -1;
70d84f11
JM
9523#ifdef CONFIG_AP
9524 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
9525 reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
9526 wpa_s, buf + 11, reply, reply_size);
9527#endif /* CONFIG_AP */
72df2f5f 9528#ifdef CONFIG_WPS_ER
e9bcfebf 9529 } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
08486685
JM
9530 if (wpas_wps_er_start(wpa_s, NULL))
9531 reply_len = -1;
9532 } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
9533 if (wpas_wps_er_start(wpa_s, buf + 13))
e9bcfebf
JM
9534 reply_len = -1;
9535 } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
f77cedc1 9536 wpas_wps_er_stop(wpa_s);
72df2f5f
JM
9537 } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
9538 if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
9539 reply_len = -1;
564cd7fa 9540 } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
ed159ad4
JM
9541 int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
9542 if (ret == -2) {
9543 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
9544 reply_len = 17;
9545 } else if (ret == -3) {
9546 os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
9547 reply_len = 18;
9548 } else if (ret == -4) {
9549 os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
9550 reply_len = 20;
9551 } else if (ret)
564cd7fa 9552 reply_len = -1;
e64dcfd5
JM
9553 } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
9554 if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
9555 reply_len = -1;
ef10f473
JM
9556 } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
9557 if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
9558 buf + 18))
9559 reply_len = -1;
7d6640a6
JM
9560 } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
9561 if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
9562 reply_len = -1;
1cea09a9
JM
9563#ifdef CONFIG_WPS_NFC
9564 } else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
9565 reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
9566 wpa_s, buf + 24, reply, reply_size);
9567#endif /* CONFIG_WPS_NFC */
72df2f5f 9568#endif /* CONFIG_WPS_ER */
fcc60db4 9569#endif /* CONFIG_WPS */
11ef8d35
JM
9570#ifdef CONFIG_IBSS_RSN
9571 } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
9572 if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
9573 reply_len = -1;
9574#endif /* CONFIG_IBSS_RSN */
603a3f34 9575#ifdef CONFIG_MESH
5b78493f
MH
9576 } else if (os_strncmp(buf, "MESH_INTERFACE_ADD ", 19) == 0) {
9577 reply_len = wpa_supplicant_ctrl_iface_mesh_interface_add(
9578 wpa_s, buf + 19, reply, reply_size);
9579 } else if (os_strcmp(buf, "MESH_INTERFACE_ADD") == 0) {
9580 reply_len = wpa_supplicant_ctrl_iface_mesh_interface_add(
9581 wpa_s, "", reply, reply_size);
603a3f34
JL
9582 } else if (os_strncmp(buf, "MESH_GROUP_ADD ", 15) == 0) {
9583 if (wpa_supplicant_ctrl_iface_mesh_group_add(wpa_s, buf + 15))
9584 reply_len = -1;
9585 } else if (os_strncmp(buf, "MESH_GROUP_REMOVE ", 18) == 0) {
9586 if (wpa_supplicant_ctrl_iface_mesh_group_remove(wpa_s,
9587 buf + 18))
9588 reply_len = -1;
e174ef34
MH
9589 } else if (os_strncmp(buf, "MESH_PEER_REMOVE ", 17) == 0) {
9590 if (wpa_supplicant_ctrl_iface_mesh_peer_remove(wpa_s, buf + 17))
9591 reply_len = -1;
2604edbf
MH
9592 } else if (os_strncmp(buf, "MESH_PEER_ADD ", 14) == 0) {
9593 if (wpa_supplicant_ctrl_iface_mesh_peer_add(wpa_s, buf + 14))
9594 reply_len = -1;
603a3f34 9595#endif /* CONFIG_MESH */
b563b388
JM
9596#ifdef CONFIG_P2P
9597 } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
51775096 9598 if (p2p_ctrl_find(wpa_s, buf + 8))
b563b388
JM
9599 reply_len = -1;
9600 } else if (os_strcmp(buf, "P2P_FIND") == 0) {
9601 if (p2p_ctrl_find(wpa_s, ""))
9602 reply_len = -1;
9603 } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
9604 wpas_p2p_stop_find(wpa_s);
f309c18e
KV
9605 } else if (os_strncmp(buf, "P2P_ASP_PROVISION ", 18) == 0) {
9606 if (p2p_ctrl_asp_provision(wpa_s, buf + 18))
9607 reply_len = -1;
9608 } else if (os_strncmp(buf, "P2P_ASP_PROVISION_RESP ", 23) == 0) {
9609 if (p2p_ctrl_asp_provision_resp(wpa_s, buf + 23))
9610 reply_len = -1;
b563b388
JM
9611 } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
9612 reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
9613 reply_size);
9614 } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
9615 if (p2p_ctrl_listen(wpa_s, buf + 11))
9616 reply_len = -1;
9617 } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
9618 if (p2p_ctrl_listen(wpa_s, ""))
9619 reply_len = -1;
9620 } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
9621 if (wpas_p2p_group_remove(wpa_s, buf + 17))
9622 reply_len = -1;
9623 } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
38dcc86c 9624 if (p2p_ctrl_group_add(wpa_s, ""))
b563b388
JM
9625 reply_len = -1;
9626 } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
9627 if (p2p_ctrl_group_add(wpa_s, buf + 14))
9628 reply_len = -1;
57b38882
PK
9629 } else if (os_strncmp(buf, "P2P_GROUP_MEMBER ", 17) == 0) {
9630 reply_len = p2p_ctrl_group_member(wpa_s, buf + 17, reply,
9631 reply_size);
b563b388
JM
9632 } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
9633 if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
9634 reply_len = -1;
9635 } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
9636 reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
9637 } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
9638 reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
9639 reply_size);
9640 } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
9641 if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
9642 reply_len = -1;
9643 } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
9644 if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
9645 reply_len = -1;
9646 } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
9647 wpas_p2p_sd_service_update(wpa_s);
9648 } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
9649 if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
9650 reply_len = -1;
9651 } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
9652 wpas_p2p_service_flush(wpa_s);
9653 } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
9654 if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
9655 reply_len = -1;
9656 } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
9657 if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
9658 reply_len = -1;
ae9d45f3
KV
9659 } else if (os_strncmp(buf, "P2P_SERVICE_REP ", 16) == 0) {
9660 if (p2p_ctrl_service_replace(wpa_s, buf + 16) < 0)
9661 reply_len = -1;
b563b388
JM
9662 } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
9663 if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
9664 reply_len = -1;
9665 } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
9666 if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
9667 reply_len = -1;
9668 } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
9669 reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
9670 reply_size);
9671 } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
9672 if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
9673 reply_len = -1;
9674 } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
acb54643 9675 p2p_ctrl_flush(wpa_s);
9d562b79
SS
9676 } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
9677 if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
9678 reply_len = -1;
59eba7a2
JM
9679 } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
9680 if (wpas_p2p_cancel(wpa_s))
9681 reply_len = -1;
b563b388
JM
9682 } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
9683 if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
9684 reply_len = -1;
9685 } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
9686 if (p2p_ctrl_presence_req(wpa_s, "") < 0)
9687 reply_len = -1;
9688 } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
9689 if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
9690 reply_len = -1;
9691 } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
9692 if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
9693 reply_len = -1;
f2c56602
JM
9694 } else if (os_strncmp(buf, "P2P_REMOVE_CLIENT ", 18) == 0) {
9695 if (p2p_ctrl_remove_client(wpa_s, buf + 18) < 0)
9696 reply_len = -1;
a6f5b193
PX
9697 } else if (os_strncmp(buf, "P2P_LO_START ", 13) == 0) {
9698 if (p2p_ctrl_iface_p2p_lo_start(wpa_s, buf + 13))
9699 reply_len = -1;
9700 } else if (os_strcmp(buf, "P2P_LO_STOP") == 0) {
9701 if (wpas_p2p_lo_stop(wpa_s))
9702 reply_len = -1;
b563b388 9703#endif /* CONFIG_P2P */
9675ce35
JM
9704#ifdef CONFIG_WIFI_DISPLAY
9705 } else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
9706 if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
9707 reply_len = -1;
9708 } else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
9709 reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
9710 reply, reply_size);
9711#endif /* CONFIG_WIFI_DISPLAY */
afc064fe
JM
9712#ifdef CONFIG_INTERWORKING
9713 } else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
9714 if (interworking_fetch_anqp(wpa_s) < 0)
9715 reply_len = -1;
9716 } else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
9717 interworking_stop_fetch_anqp(wpa_s);
356d1488
JM
9718 } else if (os_strcmp(buf, "INTERWORKING_SELECT") == 0) {
9719 if (ctrl_interworking_select(wpa_s, NULL) < 0)
9720 reply_len = -1;
9721 } else if (os_strncmp(buf, "INTERWORKING_SELECT ", 20) == 0) {
9722 if (ctrl_interworking_select(wpa_s, buf + 20) < 0)
b02fe7ff
JM
9723 reply_len = -1;
9724 } else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
f91a512f 9725 if (ctrl_interworking_connect(wpa_s, buf + 21, 0) < 0)
b02fe7ff 9726 reply_len = -1;
f91a512f
JM
9727 } else if (os_strncmp(buf, "INTERWORKING_ADD_NETWORK ", 25) == 0) {
9728 int id;
9729
9730 id = ctrl_interworking_connect(wpa_s, buf + 25, 1);
9731 if (id < 0)
9732 reply_len = -1;
9733 else {
9734 reply_len = os_snprintf(reply, reply_size, "%d\n", id);
9735 if (os_snprintf_error(reply_size, reply_len))
9736 reply_len = -1;
9737 }
afc064fe
JM
9738 } else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
9739 if (get_anqp(wpa_s, buf + 9) < 0)
9740 reply_len = -1;
b1f12296
JM
9741 } else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
9742 if (gas_request(wpa_s, buf + 12) < 0)
9743 reply_len = -1;
9744 } else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
9745 reply_len = gas_response_get(wpa_s, buf + 17, reply,
9746 reply_size);
afc064fe 9747#endif /* CONFIG_INTERWORKING */
a8918e86
JK
9748#ifdef CONFIG_HS20
9749 } else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
9750 if (get_hs20_anqp(wpa_s, buf + 14) < 0)
9751 reply_len = -1;
9752 } else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
9753 if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
9754 reply_len = -1;
184e110c 9755 } else if (os_strncmp(buf, "HS20_ICON_REQUEST ", 18) == 0) {
8dd5c1b4
JN
9756 if (hs20_icon_request(wpa_s, buf + 18, 0) < 0)
9757 reply_len = -1;
9758 } else if (os_strncmp(buf, "REQ_HS20_ICON ", 14) == 0) {
9759 if (hs20_icon_request(wpa_s, buf + 14, 1) < 0)
9760 reply_len = -1;
9761 } else if (os_strncmp(buf, "GET_HS20_ICON ", 14) == 0) {
9762 reply_len = get_hs20_icon(wpa_s, buf + 14, reply, reply_size);
9763 } else if (os_strncmp(buf, "DEL_HS20_ICON ", 14) == 0) {
9764 if (del_hs20_icon(wpa_s, buf + 14) < 0)
184e110c 9765 reply_len = -1;
b572df86 9766 } else if (os_strcmp(buf, "FETCH_OSU") == 0) {
dd20eabd
JM
9767 if (hs20_fetch_osu(wpa_s, 0) < 0)
9768 reply_len = -1;
9769 } else if (os_strcmp(buf, "FETCH_OSU no-scan") == 0) {
9770 if (hs20_fetch_osu(wpa_s, 1) < 0)
b572df86
JM
9771 reply_len = -1;
9772 } else if (os_strcmp(buf, "CANCEL_FETCH_OSU") == 0) {
9773 hs20_cancel_fetch_osu(wpa_s);
a8918e86 9774#endif /* CONFIG_HS20 */
6fc6879b
JM
9775 } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
9776 {
9777 if (wpa_supplicant_ctrl_iface_ctrl_rsp(
9778 wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
9779 reply_len = -1;
bceb8431
JM
9780 else {
9781 /*
9782 * Notify response from timeout to allow the control
9783 * interface response to be sent first.
9784 */
9785 eloop_register_timeout(0, 0, wpas_ctrl_eapol_response,
9786 wpa_s, NULL);
9787 }
6fc6879b
JM
9788 } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
9789 if (wpa_supplicant_reload_configuration(wpa_s))
9790 reply_len = -1;
9791 } else if (os_strcmp(buf, "TERMINATE") == 0) {
1a1bf008 9792 wpa_supplicant_terminate_proc(wpa_s->global);
6fc6879b
JM
9793 } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
9794 if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
9795 reply_len = -1;
9aa10e2b
DS
9796 } else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
9797 reply_len = wpa_supplicant_ctrl_iface_blacklist(
9798 wpa_s, buf + 9, reply, reply_size);
0597a5b5
DS
9799 } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
9800 reply_len = wpa_supplicant_ctrl_iface_log_level(
9801 wpa_s, buf + 9, reply, reply_size);
90903a77
VD
9802 } else if (os_strncmp(buf, "LIST_NETWORKS ", 14) == 0) {
9803 reply_len = wpa_supplicant_ctrl_iface_list_networks(
9804 wpa_s, buf + 14, reply, reply_size);
6fc6879b
JM
9805 } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
9806 reply_len = wpa_supplicant_ctrl_iface_list_networks(
90903a77 9807 wpa_s, NULL, reply, reply_size);
6fc6879b 9808 } else if (os_strcmp(buf, "DISCONNECT") == 0) {
5f040be4 9809 wpas_request_disconnection(wpa_s);
fee52342
JM
9810 } else if (os_strcmp(buf, "SCAN") == 0) {
9811 wpas_ctrl_scan(wpa_s, NULL, reply, reply_size, &reply_len);
9812 } else if (os_strncmp(buf, "SCAN ", 5) == 0) {
9813 wpas_ctrl_scan(wpa_s, buf + 5, reply, reply_size, &reply_len);
6fc6879b
JM
9814 } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
9815 reply_len = wpa_supplicant_ctrl_iface_scan_results(
9816 wpa_s, reply, reply_size);
2ea2166d
JM
9817 } else if (os_strcmp(buf, "ABORT_SCAN") == 0) {
9818 if (wpas_abort_ongoing_scan(wpa_s) < 0)
9819 reply_len = -1;
6fc6879b
JM
9820 } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
9821 if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
9822 reply_len = -1;
9823 } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
9824 if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
9825 reply_len = -1;
9826 } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
9827 if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
9828 reply_len = -1;
9829 } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
9830 reply_len = wpa_supplicant_ctrl_iface_add_network(
9831 wpa_s, reply, reply_size);
9832 } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
9833 if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
9834 reply_len = -1;
9835 } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
9836 if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
9837 reply_len = -1;
9838 } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
9839 reply_len = wpa_supplicant_ctrl_iface_get_network(
9840 wpa_s, buf + 12, reply, reply_size);
1c330a2f 9841 } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
daae4995
AN
9842 if (wpa_supplicant_ctrl_iface_dup_network(wpa_s, buf + 12,
9843 wpa_s))
1c330a2f 9844 reply_len = -1;
d94c9ee6
JM
9845 } else if (os_strcmp(buf, "LIST_CREDS") == 0) {
9846 reply_len = wpa_supplicant_ctrl_iface_list_creds(
9847 wpa_s, reply, reply_size);
9848 } else if (os_strcmp(buf, "ADD_CRED") == 0) {
9849 reply_len = wpa_supplicant_ctrl_iface_add_cred(
9850 wpa_s, reply, reply_size);
9851 } else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
9852 if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
9853 reply_len = -1;
9854 } else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
9855 if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
9856 reply_len = -1;
c880ab87
JM
9857 } else if (os_strncmp(buf, "GET_CRED ", 9) == 0) {
9858 reply_len = wpa_supplicant_ctrl_iface_get_cred(wpa_s, buf + 9,
9859 reply,
9860 reply_size);
6fc6879b
JM
9861#ifndef CONFIG_NO_CONFIG_WRITE
9862 } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
9863 if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
9864 reply_len = -1;
9865#endif /* CONFIG_NO_CONFIG_WRITE */
9866 } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
9867 reply_len = wpa_supplicant_ctrl_iface_get_capability(
9868 wpa_s, buf + 15, reply, reply_size);
9869 } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
9870 if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
9871 reply_len = -1;
67b9bd08
DS
9872 } else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
9873 if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
9874 reply_len = -1;
4b4a8ae5
JM
9875 } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
9876 reply_len = wpa_supplicant_global_iface_list(
9877 wpa_s->global, reply, reply_size);
56e2fc2c 9878 } else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
6fc6879b 9879 reply_len = wpa_supplicant_global_iface_interfaces(
56e2fc2c 9880 wpa_s->global, buf + 10, reply, reply_size);
6fc6879b
JM
9881 } else if (os_strncmp(buf, "BSS ", 4) == 0) {
9882 reply_len = wpa_supplicant_ctrl_iface_bss(
9883 wpa_s, buf + 4, reply, reply_size);
e653b622
JM
9884#ifdef CONFIG_AP
9885 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
9886 reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
9887 } else if (os_strncmp(buf, "STA ", 4) == 0) {
9888 reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
9889 reply_size);
9890 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
9891 reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
9892 reply_size);
e60b2951
JJ
9893 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
9894 if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
9895 reply_len = -1;
9896 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
9897 if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
9898 reply_len = -1;
334bf36a
AO
9899 } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
9900 if (ap_ctrl_iface_chanswitch(wpa_s, buf + 12))
9901 reply_len = -1;
99650cad
JM
9902 } else if (os_strcmp(buf, "STOP_AP") == 0) {
9903 if (wpas_ap_stop_ap(wpa_s))
9904 reply_len = -1;
e653b622 9905#endif /* CONFIG_AP */
207ef3fb
JM
9906 } else if (os_strcmp(buf, "SUSPEND") == 0) {
9907 wpas_notify_suspend(wpa_s->global);
9908 } else if (os_strcmp(buf, "RESUME") == 0) {
9909 wpas_notify_resume(wpa_s->global);
9ff4de6d 9910#ifdef CONFIG_TESTING_OPTIONS
32d5295f
JM
9911 } else if (os_strcmp(buf, "DROP_SA") == 0) {
9912 wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
9ff4de6d 9913#endif /* CONFIG_TESTING_OPTIONS */
86d4f806
JM
9914 } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
9915 if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
9916 reply_len = -1;
0d0a8ca1 9917 } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
5407c69d 9918 wpa_s->auto_reconnect_disabled = atoi(buf + 16) == 0;
78633c37
SL
9919 } else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
9920 if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
9921 reply_len = -1;
9922 } else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
9923 if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
9924 buf + 17))
9925 reply_len = -1;
39ee845f 9926 } else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
a1144000 9927 wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10);
281ff0aa
GP
9928#ifdef CONFIG_TDLS
9929 } else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
9930 if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
9931 reply_len = -1;
9932 } else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
9933 if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
9934 reply_len = -1;
9935 } else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
9936 if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
9937 reply_len = -1;
6b90deae
AN
9938 } else if (os_strncmp(buf, "TDLS_CHAN_SWITCH ", 17) == 0) {
9939 if (wpa_supplicant_ctrl_iface_tdls_chan_switch(wpa_s,
9940 buf + 17))
9941 reply_len = -1;
9942 } else if (os_strncmp(buf, "TDLS_CANCEL_CHAN_SWITCH ", 24) == 0) {
9943 if (wpa_supplicant_ctrl_iface_tdls_cancel_chan_switch(wpa_s,
9944 buf + 24))
9945 reply_len = -1;
4504621f
OG
9946 } else if (os_strncmp(buf, "TDLS_LINK_STATUS ", 17) == 0) {
9947 reply_len = wpa_supplicant_ctrl_iface_tdls_link_status(
9948 wpa_s, buf + 17, reply, reply_size);
281ff0aa 9949#endif /* CONFIG_TDLS */
8506ea6f
MB
9950 } else if (os_strcmp(buf, "WMM_AC_STATUS") == 0) {
9951 reply_len = wpas_wmm_ac_status(wpa_s, reply, reply_size);
eb2f2088
MB
9952 } else if (os_strncmp(buf, "WMM_AC_ADDTS ", 13) == 0) {
9953 if (wmm_ac_ctrl_addts(wpa_s, buf + 13))
9954 reply_len = -1;
9955 } else if (os_strncmp(buf, "WMM_AC_DELTS ", 13) == 0) {
9956 if (wmm_ac_ctrl_delts(wpa_s, buf + 13))
9957 reply_len = -1;
60b24b0d
DS
9958 } else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
9959 reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
9960 reply_size);
96e8d831
DS
9961 } else if (os_strncmp(buf, "SIGNAL_MONITOR", 14) == 0) {
9962 if (wpas_ctrl_iface_signal_monitor(wpa_s, buf + 14))
9963 reply_len = -1;
dc7785f8
YZ
9964 } else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
9965 reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
9966 reply_size);
bc5d330a
TB
9967#ifdef CONFIG_AUTOSCAN
9968 } else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
9969 if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
9970 reply_len = -1;
9971#endif /* CONFIG_AUTOSCAN */
4d7aab78
EL
9972 } else if (os_strcmp(buf, "DRIVER_FLAGS") == 0) {
9973 reply_len = wpas_ctrl_iface_driver_flags(wpa_s, reply,
9974 reply_size);
5e2c3490
JM
9975#ifdef ANDROID
9976 } else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
9977 reply_len = wpa_supplicant_driver_cmd(wpa_s, buf + 7, reply,
9978 reply_size);
9979#endif /* ANDROID */
adef8948
BL
9980 } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
9981 reply_len = wpa_supplicant_vendor_cmd(wpa_s, buf + 7, reply,
9982 reply_size);
9482426e 9983 } else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
f5f37d3a 9984 pmksa_cache_clear_current(wpa_s->wpa);
9482426e 9985 eapol_sm_request_reauth(wpa_s->eapol);
e9199e31
JM
9986#ifdef CONFIG_WNM
9987 } else if (os_strncmp(buf, "WNM_SLEEP ", 10) == 0) {
9988 if (wpas_ctrl_iface_wnm_sleep(wpa_s, buf + 10))
9989 reply_len = -1;
07565ab0
MG
9990 } else if (os_strncmp(buf, "WNM_BSS_QUERY ", 14) == 0) {
9991 if (wpas_ctrl_iface_wnm_bss_query(wpa_s, buf + 14))
65bcd0a9 9992 reply_len = -1;
e9199e31 9993#endif /* CONFIG_WNM */
acb54643
JM
9994 } else if (os_strcmp(buf, "FLUSH") == 0) {
9995 wpa_supplicant_ctrl_iface_flush(wpa_s);
1f965e62
JM
9996 } else if (os_strncmp(buf, "RADIO_WORK ", 11) == 0) {
9997 reply_len = wpas_ctrl_radio_work(wpa_s, buf + 11, reply,
9998 reply_size);
60b893df
JM
9999#ifdef CONFIG_TESTING_OPTIONS
10000 } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
10001 if (wpas_ctrl_iface_mgmt_tx(wpa_s, buf + 8) < 0)
10002 reply_len = -1;
10003 } else if (os_strcmp(buf, "MGMT_TX_DONE") == 0) {
10004 wpas_ctrl_iface_mgmt_tx_done(wpa_s);
4de70e23
JM
10005 } else if (os_strncmp(buf, "MGMT_RX_PROCESS ", 16) == 0) {
10006 if (wpas_ctrl_iface_mgmt_rx_process(wpa_s, buf + 16) < 0)
10007 reply_len = -1;
ad12f2f4
JM
10008 } else if (os_strncmp(buf, "DRIVER_EVENT ", 13) == 0) {
10009 if (wpas_ctrl_iface_driver_event(wpa_s, buf + 13) < 0)
10010 reply_len = -1;
9d4ff04a
JM
10011 } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
10012 if (wpas_ctrl_iface_eapol_rx(wpa_s, buf + 9) < 0)
10013 reply_len = -1;
4a6cc862
JM
10014 } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
10015 if (wpas_ctrl_iface_data_test_config(wpa_s, buf + 17) < 0)
10016 reply_len = -1;
10017 } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
10018 if (wpas_ctrl_iface_data_test_tx(wpa_s, buf + 13) < 0)
10019 reply_len = -1;
fc0ef7c0
JM
10020 } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
10021 if (wpas_ctrl_iface_data_test_frame(wpa_s, buf + 16) < 0)
10022 reply_len = -1;
a156ffda
JM
10023 } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
10024 if (wpas_ctrl_test_alloc_fail(wpa_s, buf + 16) < 0)
10025 reply_len = -1;
10026 } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
10027 reply_len = wpas_ctrl_get_alloc_fail(wpa_s, reply, reply_size);
2da52565
JM
10028 } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
10029 if (wpas_ctrl_test_fail(wpa_s, buf + 10) < 0)
10030 reply_len = -1;
10031 } else if (os_strcmp(buf, "GET_FAIL") == 0) {
10032 reply_len = wpas_ctrl_get_fail(wpa_s, reply, reply_size);
a530fe77
JM
10033 } else if (os_strncmp(buf, "EVENT_TEST ", 11) == 0) {
10034 if (wpas_ctrl_event_test(wpa_s, buf + 11) < 0)
10035 reply_len = -1;
651c6a84
JM
10036 } else if (os_strncmp(buf, "TEST_ASSOC_IE ", 14) == 0) {
10037 if (wpas_ctrl_test_assoc_ie(wpa_s, buf + 14) < 0)
10038 reply_len = -1;
60b893df 10039#endif /* CONFIG_TESTING_OPTIONS */
86bd36f0
JM
10040 } else if (os_strncmp(buf, "VENDOR_ELEM_ADD ", 16) == 0) {
10041 if (wpas_ctrl_vendor_elem_add(wpa_s, buf + 16) < 0)
10042 reply_len = -1;
10043 } else if (os_strncmp(buf, "VENDOR_ELEM_GET ", 16) == 0) {
10044 reply_len = wpas_ctrl_vendor_elem_get(wpa_s, buf + 16, reply,
10045 reply_size);
10046 } else if (os_strncmp(buf, "VENDOR_ELEM_REMOVE ", 19) == 0) {
10047 if (wpas_ctrl_vendor_elem_remove(wpa_s, buf + 19) < 0)
10048 reply_len = -1;
f4b8bfae 10049 } else if (os_strncmp(buf, "NEIGHBOR_REP_REQUEST", 20) == 0) {
6a4f0ed7 10050 if (wpas_ctrl_iface_send_neighbor_rep(wpa_s, buf + 20))
f4b8bfae 10051 reply_len = -1;
65d9a5e2
JM
10052 } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
10053 wpas_ctrl_iface_erp_flush(wpa_s);
fb375883
IP
10054 } else if (os_strncmp(buf, "MAC_RAND_SCAN ", 14) == 0) {
10055 if (wpas_ctrl_iface_mac_rand_scan(wpa_s, buf + 14))
10056 reply_len = -1;
98342208
AK
10057 } else if (os_strncmp(buf, "GET_PREF_FREQ_LIST ", 19) == 0) {
10058 reply_len = wpas_ctrl_iface_get_pref_freq_list(
10059 wpa_s, buf + 19, reply, reply_size);
5732b770
JM
10060#ifdef CONFIG_FILS
10061 } else if (os_strncmp(buf, "FILS_HLP_REQ_ADD ", 17) == 0) {
10062 if (wpas_ctrl_iface_fils_hlp_req_add(wpa_s, buf + 17))
10063 reply_len = -1;
10064 } else if (os_strcmp(buf, "FILS_HLP_REQ_FLUSH") == 0) {
10065 wpas_flush_fils_hlp_req(wpa_s);
10066#endif /* CONFIG_FILS */
6fc6879b
JM
10067 } else {
10068 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
10069 reply_len = 16;
10070 }
10071
10072 if (reply_len < 0) {
10073 os_memcpy(reply, "FAIL\n", 5);
10074 reply_len = 5;
10075 }
10076
6fc6879b
JM
10077 *resp_len = reply_len;
10078 return reply;
10079}
10080
10081
10082static int wpa_supplicant_global_iface_add(struct wpa_global *global,
10083 char *cmd)
10084{
10085 struct wpa_interface iface;
efa232f9
JJ
10086 char *pos, *extra;
10087 struct wpa_supplicant *wpa_s;
10088 unsigned int create_iface = 0;
10089 u8 mac_addr[ETH_ALEN];
0f039e34 10090 enum wpa_driver_if_type type = WPA_IF_STATION;
6fc6879b
JM
10091
10092 /*
10093 * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
0f039e34 10094 * TAB<bridge_ifname>[TAB<create>[TAB<interface_type>]]
6fc6879b
JM
10095 */
10096 wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
10097
10098 os_memset(&iface, 0, sizeof(iface));
10099
10100 do {
10101 iface.ifname = pos = cmd;
10102 pos = os_strchr(pos, '\t');
10103 if (pos)
10104 *pos++ = '\0';
10105 if (iface.ifname[0] == '\0')
10106 return -1;
10107 if (pos == NULL)
10108 break;
10109
10110 iface.confname = pos;
10111 pos = os_strchr(pos, '\t');
10112 if (pos)
10113 *pos++ = '\0';
10114 if (iface.confname[0] == '\0')
10115 iface.confname = NULL;
10116 if (pos == NULL)
10117 break;
10118
10119 iface.driver = pos;
10120 pos = os_strchr(pos, '\t');
10121 if (pos)
10122 *pos++ = '\0';
10123 if (iface.driver[0] == '\0')
10124 iface.driver = NULL;
10125 if (pos == NULL)
10126 break;
10127
10128 iface.ctrl_interface = pos;
10129 pos = os_strchr(pos, '\t');
10130 if (pos)
10131 *pos++ = '\0';
10132 if (iface.ctrl_interface[0] == '\0')
10133 iface.ctrl_interface = NULL;
10134 if (pos == NULL)
10135 break;
10136
10137 iface.driver_param = pos;
10138 pos = os_strchr(pos, '\t');
10139 if (pos)
10140 *pos++ = '\0';
10141 if (iface.driver_param[0] == '\0')
10142 iface.driver_param = NULL;
10143 if (pos == NULL)
10144 break;
10145
10146 iface.bridge_ifname = pos;
10147 pos = os_strchr(pos, '\t');
10148 if (pos)
10149 *pos++ = '\0';
10150 if (iface.bridge_ifname[0] == '\0')
10151 iface.bridge_ifname = NULL;
10152 if (pos == NULL)
10153 break;
efa232f9
JJ
10154
10155 extra = pos;
10156 pos = os_strchr(pos, '\t');
10157 if (pos)
10158 *pos++ = '\0';
da3db681
BG
10159 if (!extra[0])
10160 break;
10161
0f039e34 10162 if (os_strcmp(extra, "create") == 0) {
efa232f9 10163 create_iface = 1;
0f039e34
AS
10164 if (!pos)
10165 break;
10166
10167 if (os_strcmp(pos, "sta") == 0) {
10168 type = WPA_IF_STATION;
10169 } else if (os_strcmp(pos, "ap") == 0) {
10170 type = WPA_IF_AP_BSS;
10171 } else {
10172 wpa_printf(MSG_DEBUG,
10173 "INTERFACE_ADD unsupported interface type: '%s'",
10174 pos);
10175 return -1;
10176 }
10177 } else {
da3db681
BG
10178 wpa_printf(MSG_DEBUG,
10179 "INTERFACE_ADD unsupported extra parameter: '%s'",
10180 extra);
efa232f9 10181 return -1;
da3db681 10182 }
6fc6879b
JM
10183 } while (0);
10184
efa232f9
JJ
10185 if (create_iface) {
10186 wpa_printf(MSG_DEBUG, "CTRL_IFACE creating interface '%s'",
10187 iface.ifname);
10188 if (!global->ifaces)
10189 return -1;
0f039e34 10190 if (wpa_drv_if_add(global->ifaces, type, iface.ifname,
efa232f9
JJ
10191 NULL, NULL, NULL, mac_addr, NULL) < 0) {
10192 wpa_printf(MSG_ERROR,
10193 "CTRL_IFACE interface creation failed");
10194 return -1;
10195 }
10196
10197 wpa_printf(MSG_DEBUG,
10198 "CTRL_IFACE interface '%s' created with MAC addr: "
10199 MACSTR, iface.ifname, MAC2STR(mac_addr));
10200 }
10201
6fc6879b 10202 if (wpa_supplicant_get_iface(global, iface.ifname))
efa232f9
JJ
10203 goto fail;
10204
10205 wpa_s = wpa_supplicant_add_iface(global, &iface, NULL);
10206 if (!wpa_s)
10207 goto fail;
10208 wpa_s->added_vif = create_iface;
10209 return 0;
6fc6879b 10210
efa232f9
JJ
10211fail:
10212 if (create_iface)
10213 wpa_drv_if_remove(global->ifaces, WPA_IF_STATION, iface.ifname);
10214 return -1;
6fc6879b
JM
10215}
10216
10217
10218static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
10219 char *cmd)
10220{
10221 struct wpa_supplicant *wpa_s;
efa232f9
JJ
10222 int ret;
10223 unsigned int delete_iface;
6fc6879b
JM
10224
10225 wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
10226
10227 wpa_s = wpa_supplicant_get_iface(global, cmd);
10228 if (wpa_s == NULL)
10229 return -1;
efa232f9
JJ
10230 delete_iface = wpa_s->added_vif;
10231 ret = wpa_supplicant_remove_iface(global, wpa_s, 0);
10232 if (!ret && delete_iface) {
10233 wpa_printf(MSG_DEBUG, "CTRL_IFACE deleting the interface '%s'",
10234 cmd);
10235 ret = wpa_drv_if_remove(global->ifaces, WPA_IF_STATION, cmd);
10236 }
10237 return ret;
6fc6879b
JM
10238}
10239
10240
4b4a8ae5
JM
10241static void wpa_free_iface_info(struct wpa_interface_info *iface)
10242{
10243 struct wpa_interface_info *prev;
10244
10245 while (iface) {
10246 prev = iface;
10247 iface = iface->next;
10248
10249 os_free(prev->ifname);
10250 os_free(prev->desc);
10251 os_free(prev);
10252 }
10253}
10254
10255
10256static int wpa_supplicant_global_iface_list(struct wpa_global *global,
10257 char *buf, int len)
10258{
10259 int i, res;
10260 struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
10261 char *pos, *end;
10262
c5121837 10263 for (i = 0; wpa_drivers[i]; i++) {
8b423edb 10264 const struct wpa_driver_ops *drv = wpa_drivers[i];
4b4a8ae5
JM
10265 if (drv->get_interfaces == NULL)
10266 continue;
5fbc1f27 10267 tmp = drv->get_interfaces(global->drv_priv[i]);
4b4a8ae5
JM
10268 if (tmp == NULL)
10269 continue;
10270
10271 if (last == NULL)
10272 iface = last = tmp;
10273 else
10274 last->next = tmp;
10275 while (last->next)
10276 last = last->next;
10277 }
10278
10279 pos = buf;
10280 end = buf + len;
10281 for (tmp = iface; tmp; tmp = tmp->next) {
10282 res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
10283 tmp->drv_name, tmp->ifname,
10284 tmp->desc ? tmp->desc : "");
d85e1fc8 10285 if (os_snprintf_error(end - pos, res)) {
4b4a8ae5
JM
10286 *pos = '\0';
10287 break;
10288 }
10289 pos += res;
10290 }
10291
10292 wpa_free_iface_info(iface);
10293
10294 return pos - buf;
10295}
10296
10297
6fc6879b 10298static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
56e2fc2c 10299 const char *input,
6fc6879b
JM
10300 char *buf, int len)
10301{
10302 int res;
10303 char *pos, *end;
10304 struct wpa_supplicant *wpa_s;
56e2fc2c
JD
10305 int show_ctrl = 0;
10306
10307 if (input)
10308 show_ctrl = !!os_strstr(input, "ctrl");
6fc6879b
JM
10309
10310 wpa_s = global->ifaces;
10311 pos = buf;
10312 end = buf + len;
10313
10314 while (wpa_s) {
56e2fc2c
JD
10315 if (show_ctrl)
10316 res = os_snprintf(pos, end - pos, "%s ctrl_iface=%s\n",
10317 wpa_s->ifname,
10318 wpa_s->conf->ctrl_interface ?
10319 wpa_s->conf->ctrl_interface : "N/A");
10320 else
10321 res = os_snprintf(pos, end - pos, "%s\n",
10322 wpa_s->ifname);
10323
d85e1fc8 10324 if (os_snprintf_error(end - pos, res)) {
6fc6879b
JM
10325 *pos = '\0';
10326 break;
10327 }
10328 pos += res;
10329 wpa_s = wpa_s->next;
10330 }
10331 return pos - buf;
10332}
10333
10334
cf3bebf2
JM
10335static char * wpas_global_ctrl_iface_ifname(struct wpa_global *global,
10336 const char *ifname,
10337 char *cmd, size_t *resp_len)
10338{
10339 struct wpa_supplicant *wpa_s;
10340
10341 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
10342 if (os_strcmp(ifname, wpa_s->ifname) == 0)
10343 break;
10344 }
10345
10346 if (wpa_s == NULL) {
10347 char *resp = os_strdup("FAIL-NO-IFNAME-MATCH\n");
10348 if (resp)
10349 *resp_len = os_strlen(resp);
10350 else
10351 *resp_len = 1;
10352 return resp;
10353 }
10354
10355 return wpa_supplicant_ctrl_iface_process(wpa_s, cmd, resp_len);
10356}
10357
10358
576bce9c
JM
10359static char * wpas_global_ctrl_iface_redir_p2p(struct wpa_global *global,
10360 char *buf, size_t *resp_len)
10361{
10362#ifdef CONFIG_P2P
10363 static const char * cmd[] = {
443427e4 10364 "LIST_NETWORKS",
576bce9c
JM
10365 "P2P_FIND",
10366 "P2P_STOP_FIND",
10367 "P2P_LISTEN",
10368 "P2P_GROUP_ADD",
10369 "P2P_GET_PASSPHRASE",
10370 "P2P_SERVICE_UPDATE",
10371 "P2P_SERVICE_FLUSH",
10372 "P2P_FLUSH",
10373 "P2P_CANCEL",
10374 "P2P_PRESENCE_REQ",
10375 "P2P_EXT_LISTEN",
f2bc3448
DS
10376#ifdef CONFIG_AP
10377 "STA-FIRST",
10378#endif /* CONFIG_AP */
576bce9c
JM
10379 NULL
10380 };
10381 static const char * prefix[] = {
443427e4
DS
10382#ifdef ANDROID
10383 "DRIVER ",
10384#endif /* ANDROID */
10385 "GET_NETWORK ",
10386 "REMOVE_NETWORK ",
576bce9c
JM
10387 "P2P_FIND ",
10388 "P2P_CONNECT ",
10389 "P2P_LISTEN ",
10390 "P2P_GROUP_REMOVE ",
10391 "P2P_GROUP_ADD ",
57b38882 10392 "P2P_GROUP_MEMBER ",
576bce9c
JM
10393 "P2P_PROV_DISC ",
10394 "P2P_SERV_DISC_REQ ",
10395 "P2P_SERV_DISC_CANCEL_REQ ",
10396 "P2P_SERV_DISC_RESP ",
10397 "P2P_SERV_DISC_EXTERNAL ",
10398 "P2P_SERVICE_ADD ",
10399 "P2P_SERVICE_DEL ",
87d5ef5a 10400 "P2P_SERVICE_REP ",
576bce9c
JM
10401 "P2P_REJECT ",
10402 "P2P_INVITE ",
10403 "P2P_PEER ",
10404 "P2P_SET ",
10405 "P2P_UNAUTHORIZE ",
10406 "P2P_PRESENCE_REQ ",
10407 "P2P_EXT_LISTEN ",
f2c56602 10408 "P2P_REMOVE_CLIENT ",
8ad8bc5c
DS
10409 "WPS_NFC_TOKEN ",
10410 "WPS_NFC_TAG_READ ",
f3ff9487
AM
10411 "NFC_GET_HANDOVER_SEL ",
10412 "NFC_GET_HANDOVER_REQ ",
10413 "NFC_REPORT_HANDOVER ",
87d5ef5a
KV
10414 "P2P_ASP_PROVISION ",
10415 "P2P_ASP_PROVISION_RESP ",
f2bc3448
DS
10416#ifdef CONFIG_AP
10417 "STA ",
10418 "STA-NEXT ",
10419#endif /* CONFIG_AP */
576bce9c
JM
10420 NULL
10421 };
10422 int found = 0;
10423 int i;
10424
10425 if (global->p2p_init_wpa_s == NULL)
10426 return NULL;
10427
10428 for (i = 0; !found && cmd[i]; i++) {
10429 if (os_strcmp(buf, cmd[i]) == 0)
10430 found = 1;
10431 }
10432
10433 for (i = 0; !found && prefix[i]; i++) {
10434 if (os_strncmp(buf, prefix[i], os_strlen(prefix[i])) == 0)
10435 found = 1;
10436 }
10437
10438 if (found)
10439 return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
10440 buf, resp_len);
10441#endif /* CONFIG_P2P */
10442 return NULL;
10443}
10444
10445
10446static char * wpas_global_ctrl_iface_redir_wfd(struct wpa_global *global,
10447 char *buf, size_t *resp_len)
10448{
10449#ifdef CONFIG_WIFI_DISPLAY
10450 if (global->p2p_init_wpa_s == NULL)
10451 return NULL;
10452 if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0 ||
10453 os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0)
10454 return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
10455 buf, resp_len);
10456#endif /* CONFIG_WIFI_DISPLAY */
10457 return NULL;
10458}
10459
10460
10461static char * wpas_global_ctrl_iface_redir(struct wpa_global *global,
10462 char *buf, size_t *resp_len)
10463{
10464 char *ret;
10465
10466 ret = wpas_global_ctrl_iface_redir_p2p(global, buf, resp_len);
10467 if (ret)
10468 return ret;
10469
10470 ret = wpas_global_ctrl_iface_redir_wfd(global, buf, resp_len);
10471 if (ret)
10472 return ret;
10473
10474 return NULL;
10475}
10476
10477
1b9b31c1
JM
10478static int wpas_global_ctrl_iface_set(struct wpa_global *global, char *cmd)
10479{
10480 char *value;
10481
10482 value = os_strchr(cmd, ' ');
10483 if (value == NULL)
10484 return -1;
10485 *value++ = '\0';
10486
10487 wpa_printf(MSG_DEBUG, "GLOBAL_CTRL_IFACE SET '%s'='%s'", cmd, value);
10488
10489#ifdef CONFIG_WIFI_DISPLAY
10490 if (os_strcasecmp(cmd, "wifi_display") == 0) {
10491 wifi_display_enable(global, !!atoi(value));
10492 return 0;
10493 }
10494#endif /* CONFIG_WIFI_DISPLAY */
10495
a7ca6dac
JM
10496 /* Restore cmd to its original value to allow redirection */
10497 value[-1] = ' ';
10498
1b9b31c1
JM
10499 return -1;
10500}
10501
10502
daae4995
AN
10503static int wpas_global_ctrl_iface_dup_network(struct wpa_global *global,
10504 char *cmd)
10505{
10506 struct wpa_supplicant *wpa_s[2]; /* src, dst */
10507 char *p;
10508 unsigned int i;
10509
10510 /* cmd: "<src ifname> <dst ifname> <src network id> <dst network id>
10511 * <variable name> */
10512
10513 for (i = 0; i < ARRAY_SIZE(wpa_s) ; i++) {
10514 p = os_strchr(cmd, ' ');
10515 if (p == NULL)
10516 return -1;
10517 *p = '\0';
10518
10519 wpa_s[i] = global->ifaces;
10520 for (; wpa_s[i]; wpa_s[i] = wpa_s[i]->next) {
10521 if (os_strcmp(cmd, wpa_s[i]->ifname) == 0)
10522 break;
10523 }
10524
10525 if (!wpa_s[i]) {
10526 wpa_printf(MSG_DEBUG,
10527 "CTRL_IFACE: Could not find iface=%s", cmd);
10528 return -1;
10529 }
10530
10531 cmd = p + 1;
10532 }
10533
10534 return wpa_supplicant_ctrl_iface_dup_network(wpa_s[0], cmd, wpa_s[1]);
10535}
10536
10537
42868f14
JM
10538#ifndef CONFIG_NO_CONFIG_WRITE
10539static int wpas_global_ctrl_iface_save_config(struct wpa_global *global)
10540{
d6b818ef 10541 int ret = 0, saved = 0;
42868f14
JM
10542 struct wpa_supplicant *wpa_s;
10543
10544 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
10545 if (!wpa_s->conf->update_config) {
10546 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed to update configuration (update_config=0)");
10547 continue;
10548 }
10549
10550 if (wpa_config_write(wpa_s->confname, wpa_s->conf)) {
10551 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to update configuration");
10552 ret = 1;
10553 } else {
10554 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration updated");
d6b818ef 10555 saved++;
42868f14
JM
10556 }
10557 }
10558
d6b818ef
JM
10559 if (!saved && !ret) {
10560 wpa_dbg(wpa_s, MSG_DEBUG,
10561 "CTRL_IFACE: SAVE_CONFIG - No configuration files could be updated");
10562 ret = 1;
10563 }
10564
42868f14
JM
10565 return ret;
10566}
10567#endif /* CONFIG_NO_CONFIG_WRITE */
10568
10569
ae8c27f7
JM
10570static int wpas_global_ctrl_iface_status(struct wpa_global *global,
10571 char *buf, size_t buflen)
10572{
10573 char *pos, *end;
10574 int ret;
10575 struct wpa_supplicant *wpa_s;
10576
10577 pos = buf;
10578 end = buf + buflen;
10579
10580#ifdef CONFIG_P2P
4c559019 10581 if (global->p2p && !global->p2p_disabled) {
ae8c27f7 10582 ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
4c559019
JM
10583 "\n"
10584 "p2p_state=%s\n",
10585 MAC2STR(global->p2p_dev_addr),
10586 p2p_get_state_txt(global->p2p));
d85e1fc8 10587 if (os_snprintf_error(end - pos, ret))
4c559019
JM
10588 return pos - buf;
10589 pos += ret;
10590 } else if (global->p2p) {
10591 ret = os_snprintf(pos, end - pos, "p2p_state=DISABLED\n");
d85e1fc8 10592 if (os_snprintf_error(end - pos, ret))
ae8c27f7
JM
10593 return pos - buf;
10594 pos += ret;
10595 }
10596#endif /* CONFIG_P2P */
10597
10598#ifdef CONFIG_WIFI_DISPLAY
10599 ret = os_snprintf(pos, end - pos, "wifi_display=%d\n",
10600 !!global->wifi_display);
d85e1fc8 10601 if (os_snprintf_error(end - pos, ret))
ae8c27f7
JM
10602 return pos - buf;
10603 pos += ret;
10604#endif /* CONFIG_WIFI_DISPLAY */
10605
10606 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
10607 ret = os_snprintf(pos, end - pos, "ifname=%s\n"
10608 "address=" MACSTR "\n",
10609 wpa_s->ifname, MAC2STR(wpa_s->own_addr));
d85e1fc8 10610 if (os_snprintf_error(end - pos, ret))
ae8c27f7
JM
10611 return pos - buf;
10612 pos += ret;
10613 }
10614
10615 return pos - buf;
10616}
10617
10618
3794af2d
AN
10619#ifdef CONFIG_FST
10620
10621static int wpas_global_ctrl_iface_fst_attach(struct wpa_global *global,
10622 char *cmd, char *buf,
10623 size_t reply_size)
10624{
10625 char ifname[IFNAMSIZ + 1];
10626 struct fst_iface_cfg cfg;
10627 struct wpa_supplicant *wpa_s;
10628 struct fst_wpa_obj iface_obj;
10629
10630 if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
10631 wpa_s = wpa_supplicant_get_iface(global, ifname);
10632 if (wpa_s) {
5dbd3bf9
JM
10633 if (wpa_s->fst) {
10634 wpa_printf(MSG_INFO, "FST: Already attached");
10635 return -1;
10636 }
3794af2d
AN
10637 fst_wpa_supplicant_fill_iface_obj(wpa_s, &iface_obj);
10638 wpa_s->fst = fst_attach(ifname, wpa_s->own_addr,
10639 &iface_obj, &cfg);
10640 if (wpa_s->fst)
10641 return os_snprintf(buf, reply_size, "OK\n");
10642 }
10643 }
10644
10645 return -1;
10646}
10647
10648
10649static int wpas_global_ctrl_iface_fst_detach(struct wpa_global *global,
10650 char *cmd, char *buf,
10651 size_t reply_size)
10652{
10653 char ifname[IFNAMSIZ + 1];
10654 struct wpa_supplicant *wpa_s;
10655
10656 if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
10657 wpa_s = wpa_supplicant_get_iface(global, ifname);
10658 if (wpa_s) {
10659 if (!fst_iface_detach(ifname)) {
10660 wpa_s->fst = NULL;
10661 return os_snprintf(buf, reply_size, "OK\n");
10662 }
10663 }
10664 }
10665
10666 return -1;
10667}
10668
10669#endif /* CONFIG_FST */
10670
10671
6fc6879b
JM
10672char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
10673 char *buf, size_t *resp_len)
10674{
10675 char *reply;
10676 const int reply_size = 2048;
10677 int reply_len;
f4a0a82c 10678 int level = MSG_DEBUG;
6fc6879b 10679
cf3bebf2
JM
10680 if (os_strncmp(buf, "IFNAME=", 7) == 0) {
10681 char *pos = os_strchr(buf + 7, ' ');
10682 if (pos) {
10683 *pos++ = '\0';
10684 return wpas_global_ctrl_iface_ifname(global,
10685 buf + 7, pos,
10686 resp_len);
10687 }
10688 }
10689
576bce9c
JM
10690 reply = wpas_global_ctrl_iface_redir(global, buf, resp_len);
10691 if (reply)
10692 return reply;
10693
f4a0a82c
JM
10694 if (os_strcmp(buf, "PING") == 0)
10695 level = MSG_EXCESSIVE;
10696 wpa_hexdump_ascii(level, "RX global ctrl_iface",
6fc6879b
JM
10697 (const u8 *) buf, os_strlen(buf));
10698
10699 reply = os_malloc(reply_size);
10700 if (reply == NULL) {
10701 *resp_len = 1;
10702 return NULL;
10703 }
10704
10705 os_memcpy(reply, "OK\n", 3);
10706 reply_len = 3;
10707
10708 if (os_strcmp(buf, "PING") == 0) {
10709 os_memcpy(reply, "PONG\n", 5);
10710 reply_len = 5;
10711 } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
10712 if (wpa_supplicant_global_iface_add(global, buf + 14))
10713 reply_len = -1;
10714 } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
10715 if (wpa_supplicant_global_iface_remove(global, buf + 17))
10716 reply_len = -1;
4b4a8ae5
JM
10717 } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
10718 reply_len = wpa_supplicant_global_iface_list(
10719 global, reply, reply_size);
56e2fc2c 10720 } else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
6fc6879b 10721 reply_len = wpa_supplicant_global_iface_interfaces(
56e2fc2c 10722 global, buf + 10, reply, reply_size);
3794af2d
AN
10723#ifdef CONFIG_FST
10724 } else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
10725 reply_len = wpas_global_ctrl_iface_fst_attach(global, buf + 11,
10726 reply,
10727 reply_size);
10728 } else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
10729 reply_len = wpas_global_ctrl_iface_fst_detach(global, buf + 11,
10730 reply,
10731 reply_size);
10732 } else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
10733 reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
10734#endif /* CONFIG_FST */
6fc6879b 10735 } else if (os_strcmp(buf, "TERMINATE") == 0) {
1a1bf008 10736 wpa_supplicant_terminate_proc(global);
207ef3fb
JM
10737 } else if (os_strcmp(buf, "SUSPEND") == 0) {
10738 wpas_notify_suspend(global);
10739 } else if (os_strcmp(buf, "RESUME") == 0) {
10740 wpas_notify_resume(global);
1b9b31c1 10741 } else if (os_strncmp(buf, "SET ", 4) == 0) {
a7ca6dac
JM
10742 if (wpas_global_ctrl_iface_set(global, buf + 4)) {
10743#ifdef CONFIG_P2P
10744 if (global->p2p_init_wpa_s) {
10745 os_free(reply);
10746 /* Check if P2P redirection would work for this
10747 * command. */
10748 return wpa_supplicant_ctrl_iface_process(
10749 global->p2p_init_wpa_s,
10750 buf, resp_len);
10751 }
10752#endif /* CONFIG_P2P */
1b9b31c1 10753 reply_len = -1;
a7ca6dac 10754 }
daae4995
AN
10755 } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
10756 if (wpas_global_ctrl_iface_dup_network(global, buf + 12))
10757 reply_len = -1;
42868f14
JM
10758#ifndef CONFIG_NO_CONFIG_WRITE
10759 } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
10760 if (wpas_global_ctrl_iface_save_config(global))
10761 reply_len = -1;
10762#endif /* CONFIG_NO_CONFIG_WRITE */
ae8c27f7
JM
10763 } else if (os_strcmp(buf, "STATUS") == 0) {
10764 reply_len = wpas_global_ctrl_iface_status(global, reply,
10765 reply_size);
ea449b5b
JM
10766#ifdef CONFIG_MODULE_TESTS
10767 } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
ea449b5b
JM
10768 if (wpas_module_tests() < 0)
10769 reply_len = -1;
10770#endif /* CONFIG_MODULE_TESTS */
5f797376
JM
10771 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
10772 if (wpa_debug_reopen_file() < 0)
10773 reply_len = -1;
6fc6879b
JM
10774 } else {
10775 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
10776 reply_len = 16;
10777 }
10778
10779 if (reply_len < 0) {
10780 os_memcpy(reply, "FAIL\n", 5);
10781 reply_len = 5;
10782 }
10783
10784 *resp_len = reply_len;
10785 return reply;
10786}