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