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