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