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