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