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