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