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