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