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