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