]> git.ipfire.org Git - thirdparty/hostap.git/blob - wpa_supplicant/ctrl_iface.c
Add SAE and FT/SAE into scan results info
[thirdparty/hostap.git] / wpa_supplicant / ctrl_iface.c
1 /*
2 * WPA Supplicant / Control interface (shared code for all backends)
3 * Copyright (c) 2004-2014, 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 "common/version.h"
19 #include "common/ieee802_11_defs.h"
20 #include "common/ieee802_11_common.h"
21 #include "common/wpa_ctrl.h"
22 #include "ap/hostapd.h"
23 #include "eap_peer/eap.h"
24 #include "eapol_supp/eapol_supp_sm.h"
25 #include "rsn_supp/wpa.h"
26 #include "rsn_supp/preauth.h"
27 #include "rsn_supp/pmksa_cache.h"
28 #include "l2_packet/l2_packet.h"
29 #include "wps/wps.h"
30 #include "config.h"
31 #include "wpa_supplicant_i.h"
32 #include "driver_i.h"
33 #include "wps_supplicant.h"
34 #include "ibss_rsn.h"
35 #include "ap.h"
36 #include "p2p_supplicant.h"
37 #include "p2p/p2p.h"
38 #include "hs20_supplicant.h"
39 #include "wifi_display.h"
40 #include "notify.h"
41 #include "bss.h"
42 #include "scan.h"
43 #include "ctrl_iface.h"
44 #include "interworking.h"
45 #include "blacklist.h"
46 #include "autoscan.h"
47 #include "wnm_sta.h"
48 #include "offchannel.h"
49 #include "drivers/driver.h"
50
51 static int wpa_supplicant_global_iface_list(struct wpa_global *global,
52 char *buf, int len);
53 static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
54 char *buf, int len);
55 static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s,
56 char *val);
57
58 static int set_bssid_filter(struct wpa_supplicant *wpa_s, char *val)
59 {
60 char *pos;
61 u8 addr[ETH_ALEN], *filter = NULL, *n;
62 size_t count = 0;
63
64 pos = val;
65 while (pos) {
66 if (*pos == '\0')
67 break;
68 if (hwaddr_aton(pos, addr)) {
69 os_free(filter);
70 return -1;
71 }
72 n = os_realloc_array(filter, count + 1, ETH_ALEN);
73 if (n == NULL) {
74 os_free(filter);
75 return -1;
76 }
77 filter = n;
78 os_memcpy(filter + count * ETH_ALEN, addr, ETH_ALEN);
79 count++;
80
81 pos = os_strchr(pos, ' ');
82 if (pos)
83 pos++;
84 }
85
86 wpa_hexdump(MSG_DEBUG, "bssid_filter", filter, count * ETH_ALEN);
87 os_free(wpa_s->bssid_filter);
88 wpa_s->bssid_filter = filter;
89 wpa_s->bssid_filter_count = count;
90
91 return 0;
92 }
93
94
95 static int set_disallow_aps(struct wpa_supplicant *wpa_s, char *val)
96 {
97 char *pos;
98 u8 addr[ETH_ALEN], *bssid = NULL, *n;
99 struct wpa_ssid_value *ssid = NULL, *ns;
100 size_t count = 0, ssid_count = 0;
101 struct wpa_ssid *c;
102
103 /*
104 * disallow_list ::= <ssid_spec> | <bssid_spec> | <disallow_list> | ""
105 * SSID_SPEC ::= ssid <SSID_HEX>
106 * BSSID_SPEC ::= bssid <BSSID_HEX>
107 */
108
109 pos = val;
110 while (pos) {
111 if (*pos == '\0')
112 break;
113 if (os_strncmp(pos, "bssid ", 6) == 0) {
114 int res;
115 pos += 6;
116 res = hwaddr_aton2(pos, addr);
117 if (res < 0) {
118 os_free(ssid);
119 os_free(bssid);
120 wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
121 "BSSID value '%s'", pos);
122 return -1;
123 }
124 pos += res;
125 n = os_realloc_array(bssid, count + 1, ETH_ALEN);
126 if (n == NULL) {
127 os_free(ssid);
128 os_free(bssid);
129 return -1;
130 }
131 bssid = n;
132 os_memcpy(bssid + count * ETH_ALEN, addr, ETH_ALEN);
133 count++;
134 } else if (os_strncmp(pos, "ssid ", 5) == 0) {
135 char *end;
136 pos += 5;
137
138 end = pos;
139 while (*end) {
140 if (*end == '\0' || *end == ' ')
141 break;
142 end++;
143 }
144
145 ns = os_realloc_array(ssid, ssid_count + 1,
146 sizeof(struct wpa_ssid_value));
147 if (ns == NULL) {
148 os_free(ssid);
149 os_free(bssid);
150 return -1;
151 }
152 ssid = ns;
153
154 if ((end - pos) & 0x01 || end - pos > 2 * 32 ||
155 hexstr2bin(pos, ssid[ssid_count].ssid,
156 (end - pos) / 2) < 0) {
157 os_free(ssid);
158 os_free(bssid);
159 wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
160 "SSID value '%s'", pos);
161 return -1;
162 }
163 ssid[ssid_count].ssid_len = (end - pos) / 2;
164 wpa_hexdump_ascii(MSG_DEBUG, "disallow_aps SSID",
165 ssid[ssid_count].ssid,
166 ssid[ssid_count].ssid_len);
167 ssid_count++;
168 pos = end;
169 } else {
170 wpa_printf(MSG_DEBUG, "Unexpected disallow_aps value "
171 "'%s'", pos);
172 os_free(ssid);
173 os_free(bssid);
174 return -1;
175 }
176
177 pos = os_strchr(pos, ' ');
178 if (pos)
179 pos++;
180 }
181
182 wpa_hexdump(MSG_DEBUG, "disallow_aps_bssid", bssid, count * ETH_ALEN);
183 os_free(wpa_s->disallow_aps_bssid);
184 wpa_s->disallow_aps_bssid = bssid;
185 wpa_s->disallow_aps_bssid_count = count;
186
187 wpa_printf(MSG_DEBUG, "disallow_aps_ssid_count %d", (int) ssid_count);
188 os_free(wpa_s->disallow_aps_ssid);
189 wpa_s->disallow_aps_ssid = ssid;
190 wpa_s->disallow_aps_ssid_count = ssid_count;
191
192 if (!wpa_s->current_ssid || wpa_s->wpa_state < WPA_AUTHENTICATING)
193 return 0;
194
195 c = wpa_s->current_ssid;
196 if (c->mode != WPAS_MODE_INFRA && c->mode != WPAS_MODE_IBSS)
197 return 0;
198
199 if (!disallowed_bssid(wpa_s, wpa_s->bssid) &&
200 !disallowed_ssid(wpa_s, c->ssid, c->ssid_len))
201 return 0;
202
203 wpa_printf(MSG_DEBUG, "Disconnect and try to find another network "
204 "because current AP was marked disallowed");
205
206 #ifdef CONFIG_SME
207 wpa_s->sme.prev_bssid_set = 0;
208 #endif /* CONFIG_SME */
209 wpa_s->reassociate = 1;
210 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
211 wpa_supplicant_req_scan(wpa_s, 0, 0);
212
213 return 0;
214 }
215
216
217 #ifndef CONFIG_NO_CONFIG_BLOBS
218 static int wpas_ctrl_set_blob(struct wpa_supplicant *wpa_s, char *pos)
219 {
220 char *name = pos;
221 struct wpa_config_blob *blob;
222 size_t len;
223
224 pos = os_strchr(pos, ' ');
225 if (pos == NULL)
226 return -1;
227 *pos++ = '\0';
228 len = os_strlen(pos);
229 if (len & 1)
230 return -1;
231
232 wpa_printf(MSG_DEBUG, "CTRL: Set blob '%s'", name);
233 blob = os_zalloc(sizeof(*blob));
234 if (blob == NULL)
235 return -1;
236 blob->name = os_strdup(name);
237 blob->data = os_malloc(len / 2);
238 if (blob->name == NULL || blob->data == NULL) {
239 wpa_config_free_blob(blob);
240 return -1;
241 }
242
243 if (hexstr2bin(pos, blob->data, len / 2) < 0) {
244 wpa_printf(MSG_DEBUG, "CTRL: Invalid blob hex data");
245 wpa_config_free_blob(blob);
246 return -1;
247 }
248 blob->len = len / 2;
249
250 wpa_config_set_blob(wpa_s->conf, blob);
251
252 return 0;
253 }
254 #endif /* CONFIG_NO_CONFIG_BLOBS */
255
256
257 static int wpas_ctrl_pno(struct wpa_supplicant *wpa_s, char *cmd)
258 {
259 char *params;
260 char *pos;
261 int *freqs = NULL;
262 int ret;
263
264 if (atoi(cmd)) {
265 params = os_strchr(cmd, ' ');
266 os_free(wpa_s->manual_sched_scan_freqs);
267 if (params) {
268 params++;
269 pos = os_strstr(params, "freq=");
270 if (pos)
271 freqs = freq_range_to_channel_list(wpa_s,
272 pos + 5);
273 }
274 wpa_s->manual_sched_scan_freqs = freqs;
275 ret = wpas_start_pno(wpa_s);
276 } else {
277 ret = wpas_stop_pno(wpa_s);
278 }
279 return ret;
280 }
281
282
283 static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
284 char *cmd)
285 {
286 char *value;
287 int ret = 0;
288
289 value = os_strchr(cmd, ' ');
290 if (value == NULL)
291 return -1;
292 *value++ = '\0';
293
294 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
295 if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
296 eapol_sm_configure(wpa_s->eapol,
297 atoi(value), -1, -1, -1);
298 } else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
299 eapol_sm_configure(wpa_s->eapol,
300 -1, atoi(value), -1, -1);
301 } else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
302 eapol_sm_configure(wpa_s->eapol,
303 -1, -1, atoi(value), -1);
304 } else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
305 eapol_sm_configure(wpa_s->eapol,
306 -1, -1, -1, atoi(value));
307 } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
308 if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
309 atoi(value)))
310 ret = -1;
311 } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
312 0) {
313 if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
314 atoi(value)))
315 ret = -1;
316 } else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
317 if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, atoi(value)))
318 ret = -1;
319 } else if (os_strcasecmp(cmd, "wps_fragment_size") == 0) {
320 wpa_s->wps_fragment_size = atoi(value);
321 #ifdef CONFIG_WPS_TESTING
322 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
323 long int val;
324 val = strtol(value, NULL, 0);
325 if (val < 0 || val > 0xff) {
326 ret = -1;
327 wpa_printf(MSG_DEBUG, "WPS: Invalid "
328 "wps_version_number %ld", val);
329 } else {
330 wps_version_number = val;
331 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
332 "version %u.%u",
333 (wps_version_number & 0xf0) >> 4,
334 wps_version_number & 0x0f);
335 }
336 } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
337 wps_testing_dummy_cred = atoi(value);
338 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
339 wps_testing_dummy_cred);
340 } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
341 wps_corrupt_pkhash = atoi(value);
342 wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
343 wps_corrupt_pkhash);
344 #endif /* CONFIG_WPS_TESTING */
345 } else if (os_strcasecmp(cmd, "ampdu") == 0) {
346 if (wpa_drv_ampdu(wpa_s, atoi(value)) < 0)
347 ret = -1;
348 #ifdef CONFIG_TDLS
349 #ifdef CONFIG_TDLS_TESTING
350 } else if (os_strcasecmp(cmd, "tdls_testing") == 0) {
351 extern unsigned int tdls_testing;
352 tdls_testing = strtol(value, NULL, 0);
353 wpa_printf(MSG_DEBUG, "TDLS: tdls_testing=0x%x", tdls_testing);
354 #endif /* CONFIG_TDLS_TESTING */
355 } else if (os_strcasecmp(cmd, "tdls_disabled") == 0) {
356 int disabled = atoi(value);
357 wpa_printf(MSG_DEBUG, "TDLS: tdls_disabled=%d", disabled);
358 if (disabled) {
359 if (wpa_drv_tdls_oper(wpa_s, TDLS_DISABLE, NULL) < 0)
360 ret = -1;
361 } else if (wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL) < 0)
362 ret = -1;
363 wpa_tdls_enable(wpa_s->wpa, !disabled);
364 #endif /* CONFIG_TDLS */
365 } else if (os_strcasecmp(cmd, "pno") == 0) {
366 ret = wpas_ctrl_pno(wpa_s, value);
367 } else if (os_strcasecmp(cmd, "radio_disabled") == 0) {
368 int disabled = atoi(value);
369 if (wpa_drv_radio_disable(wpa_s, disabled) < 0)
370 ret = -1;
371 else if (disabled)
372 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
373 } else if (os_strcasecmp(cmd, "uapsd") == 0) {
374 if (os_strcmp(value, "disable") == 0)
375 wpa_s->set_sta_uapsd = 0;
376 else {
377 int be, bk, vi, vo;
378 char *pos;
379 /* format: BE,BK,VI,VO;max SP Length */
380 be = atoi(value);
381 pos = os_strchr(value, ',');
382 if (pos == NULL)
383 return -1;
384 pos++;
385 bk = atoi(pos);
386 pos = os_strchr(pos, ',');
387 if (pos == NULL)
388 return -1;
389 pos++;
390 vi = atoi(pos);
391 pos = os_strchr(pos, ',');
392 if (pos == NULL)
393 return -1;
394 pos++;
395 vo = atoi(pos);
396 /* ignore max SP Length for now */
397
398 wpa_s->set_sta_uapsd = 1;
399 wpa_s->sta_uapsd = 0;
400 if (be)
401 wpa_s->sta_uapsd |= BIT(0);
402 if (bk)
403 wpa_s->sta_uapsd |= BIT(1);
404 if (vi)
405 wpa_s->sta_uapsd |= BIT(2);
406 if (vo)
407 wpa_s->sta_uapsd |= BIT(3);
408 }
409 } else if (os_strcasecmp(cmd, "ps") == 0) {
410 ret = wpa_drv_set_p2p_powersave(wpa_s, atoi(value), -1, -1);
411 #ifdef CONFIG_WIFI_DISPLAY
412 } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
413 int enabled = !!atoi(value);
414 if (enabled && !wpa_s->global->p2p)
415 ret = -1;
416 else
417 wifi_display_enable(wpa_s->global, enabled);
418 #endif /* CONFIG_WIFI_DISPLAY */
419 } else if (os_strcasecmp(cmd, "bssid_filter") == 0) {
420 ret = set_bssid_filter(wpa_s, value);
421 } else if (os_strcasecmp(cmd, "disallow_aps") == 0) {
422 ret = set_disallow_aps(wpa_s, value);
423 } else if (os_strcasecmp(cmd, "no_keep_alive") == 0) {
424 wpa_s->no_keep_alive = !!atoi(value);
425 #ifdef CONFIG_TESTING_OPTIONS
426 } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
427 wpa_s->ext_mgmt_frame_handling = !!atoi(value);
428 } else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
429 wpa_s->ext_eapol_frame_io = !!atoi(value);
430 #ifdef CONFIG_AP
431 if (wpa_s->ap_iface) {
432 wpa_s->ap_iface->bss[0]->ext_eapol_frame_io =
433 wpa_s->ext_eapol_frame_io;
434 }
435 #endif /* CONFIG_AP */
436 #endif /* CONFIG_TESTING_OPTIONS */
437 #ifndef CONFIG_NO_CONFIG_BLOBS
438 } else if (os_strcmp(cmd, "blob") == 0) {
439 ret = wpas_ctrl_set_blob(wpa_s, value);
440 #endif /* CONFIG_NO_CONFIG_BLOBS */
441 } else {
442 value[-1] = '=';
443 ret = wpa_config_process_global(wpa_s->conf, cmd, -1);
444 if (ret == 0)
445 wpa_supplicant_update_config(wpa_s);
446 }
447
448 return ret;
449 }
450
451
452 static int wpa_supplicant_ctrl_iface_get(struct wpa_supplicant *wpa_s,
453 char *cmd, char *buf, size_t buflen)
454 {
455 int res = -1;
456
457 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
458
459 if (os_strcmp(cmd, "version") == 0) {
460 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
461 } else if (os_strcasecmp(cmd, "country") == 0) {
462 if (wpa_s->conf->country[0] && wpa_s->conf->country[1])
463 res = os_snprintf(buf, buflen, "%c%c",
464 wpa_s->conf->country[0],
465 wpa_s->conf->country[1]);
466 #ifdef CONFIG_WIFI_DISPLAY
467 } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
468 int enabled;
469 if (wpa_s->global->p2p == NULL ||
470 wpa_s->global->p2p_disabled)
471 enabled = 0;
472 else
473 enabled = wpa_s->global->wifi_display;
474 res = os_snprintf(buf, buflen, "%d", enabled);
475 if (res < 0 || (unsigned int) res >= buflen)
476 return -1;
477 return res;
478 #endif /* CONFIG_WIFI_DISPLAY */
479 #ifdef CONFIG_TESTING_GET_GTK
480 } else if (os_strcmp(cmd, "gtk") == 0) {
481 if (wpa_s->last_gtk_len == 0)
482 return -1;
483 res = wpa_snprintf_hex(buf, buflen, wpa_s->last_gtk,
484 wpa_s->last_gtk_len);
485 return res;
486 #endif /* CONFIG_TESTING_GET_GTK */
487 }
488
489 if (res < 0 || (unsigned int) res >= buflen)
490 return -1;
491 return res;
492 }
493
494
495 #ifdef IEEE8021X_EAPOL
496 static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
497 char *addr)
498 {
499 u8 bssid[ETH_ALEN];
500 struct wpa_ssid *ssid = wpa_s->current_ssid;
501
502 if (hwaddr_aton(addr, bssid)) {
503 wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
504 "'%s'", addr);
505 return -1;
506 }
507
508 wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
509 rsn_preauth_deinit(wpa_s->wpa);
510 if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
511 return -1;
512
513 return 0;
514 }
515 #endif /* IEEE8021X_EAPOL */
516
517
518 #ifdef CONFIG_PEERKEY
519 /* MLME-STKSTART.request(peer) */
520 static int wpa_supplicant_ctrl_iface_stkstart(
521 struct wpa_supplicant *wpa_s, char *addr)
522 {
523 u8 peer[ETH_ALEN];
524
525 if (hwaddr_aton(addr, peer)) {
526 wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART: invalid "
527 "address '%s'", addr);
528 return -1;
529 }
530
531 wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART " MACSTR,
532 MAC2STR(peer));
533
534 return wpa_sm_stkstart(wpa_s->wpa, peer);
535 }
536 #endif /* CONFIG_PEERKEY */
537
538
539 #ifdef CONFIG_TDLS
540
541 static int wpa_supplicant_ctrl_iface_tdls_discover(
542 struct wpa_supplicant *wpa_s, char *addr)
543 {
544 u8 peer[ETH_ALEN];
545 int ret;
546
547 if (hwaddr_aton(addr, peer)) {
548 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER: invalid "
549 "address '%s'", addr);
550 return -1;
551 }
552
553 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER " MACSTR,
554 MAC2STR(peer));
555
556 if (wpa_tdls_is_external_setup(wpa_s->wpa))
557 ret = wpa_tdls_send_discovery_request(wpa_s->wpa, peer);
558 else
559 ret = wpa_drv_tdls_oper(wpa_s, TDLS_DISCOVERY_REQ, peer);
560
561 return ret;
562 }
563
564
565 static int wpa_supplicant_ctrl_iface_tdls_setup(
566 struct wpa_supplicant *wpa_s, char *addr)
567 {
568 u8 peer[ETH_ALEN];
569 int ret;
570
571 if (hwaddr_aton(addr, peer)) {
572 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP: invalid "
573 "address '%s'", addr);
574 return -1;
575 }
576
577 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP " MACSTR,
578 MAC2STR(peer));
579
580 if ((wpa_s->conf->tdls_external_control) &&
581 wpa_tdls_is_external_setup(wpa_s->wpa))
582 return wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
583
584 wpa_tdls_remove(wpa_s->wpa, peer);
585
586 if (wpa_tdls_is_external_setup(wpa_s->wpa))
587 ret = wpa_tdls_start(wpa_s->wpa, peer);
588 else
589 ret = wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
590
591 return ret;
592 }
593
594
595 static int wpa_supplicant_ctrl_iface_tdls_teardown(
596 struct wpa_supplicant *wpa_s, char *addr)
597 {
598 u8 peer[ETH_ALEN];
599 int ret;
600
601 if (os_strcmp(addr, "*") == 0) {
602 /* remove everyone */
603 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN *");
604 wpa_tdls_teardown_peers(wpa_s->wpa);
605 return 0;
606 }
607
608 if (hwaddr_aton(addr, peer)) {
609 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN: invalid "
610 "address '%s'", addr);
611 return -1;
612 }
613
614 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN " MACSTR,
615 MAC2STR(peer));
616
617 if ((wpa_s->conf->tdls_external_control) &&
618 wpa_tdls_is_external_setup(wpa_s->wpa))
619 return wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
620
621 if (wpa_tdls_is_external_setup(wpa_s->wpa))
622 ret = wpa_tdls_teardown_link(
623 wpa_s->wpa, peer,
624 WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
625 else
626 ret = wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
627
628 return ret;
629 }
630
631
632 static int ctrl_iface_get_capability_tdls(
633 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
634 {
635 int ret;
636
637 ret = os_snprintf(buf, buflen, "%s\n",
638 wpa_s->drv_flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT ?
639 (wpa_s->drv_flags &
640 WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP ?
641 "EXTERNAL" : "INTERNAL") : "UNSUPPORTED");
642 if (ret < 0 || (size_t) ret > buflen)
643 return -1;
644 return ret;
645 }
646
647 #endif /* CONFIG_TDLS */
648
649
650 #ifdef CONFIG_IEEE80211R
651 static int wpa_supplicant_ctrl_iface_ft_ds(
652 struct wpa_supplicant *wpa_s, char *addr)
653 {
654 u8 target_ap[ETH_ALEN];
655 struct wpa_bss *bss;
656 const u8 *mdie;
657
658 if (hwaddr_aton(addr, target_ap)) {
659 wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
660 "address '%s'", addr);
661 return -1;
662 }
663
664 wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
665
666 bss = wpa_bss_get_bssid(wpa_s, target_ap);
667 if (bss)
668 mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
669 else
670 mdie = NULL;
671
672 return wpa_ft_start_over_ds(wpa_s->wpa, target_ap, mdie);
673 }
674 #endif /* CONFIG_IEEE80211R */
675
676
677 #ifdef CONFIG_WPS
678 static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
679 char *cmd)
680 {
681 u8 bssid[ETH_ALEN], *_bssid = bssid;
682 #ifdef CONFIG_P2P
683 u8 p2p_dev_addr[ETH_ALEN];
684 #endif /* CONFIG_P2P */
685 #ifdef CONFIG_AP
686 u8 *_p2p_dev_addr = NULL;
687 #endif /* CONFIG_AP */
688
689 if (cmd == NULL || os_strcmp(cmd, "any") == 0) {
690 _bssid = NULL;
691 #ifdef CONFIG_P2P
692 } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
693 if (hwaddr_aton(cmd + 13, p2p_dev_addr)) {
694 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid "
695 "P2P Device Address '%s'",
696 cmd + 13);
697 return -1;
698 }
699 _p2p_dev_addr = p2p_dev_addr;
700 #endif /* CONFIG_P2P */
701 } else if (hwaddr_aton(cmd, bssid)) {
702 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid BSSID '%s'",
703 cmd);
704 return -1;
705 }
706
707 #ifdef CONFIG_AP
708 if (wpa_s->ap_iface)
709 return wpa_supplicant_ap_wps_pbc(wpa_s, _bssid, _p2p_dev_addr);
710 #endif /* CONFIG_AP */
711
712 return wpas_wps_start_pbc(wpa_s, _bssid, 0);
713 }
714
715
716 static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
717 char *cmd, char *buf,
718 size_t buflen)
719 {
720 u8 bssid[ETH_ALEN], *_bssid = bssid;
721 char *pin;
722 int ret;
723
724 pin = os_strchr(cmd, ' ');
725 if (pin)
726 *pin++ = '\0';
727
728 if (os_strcmp(cmd, "any") == 0)
729 _bssid = NULL;
730 else if (os_strcmp(cmd, "get") == 0) {
731 ret = wps_generate_pin();
732 goto done;
733 } else if (hwaddr_aton(cmd, bssid)) {
734 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PIN: invalid BSSID '%s'",
735 cmd);
736 return -1;
737 }
738
739 #ifdef CONFIG_AP
740 if (wpa_s->ap_iface) {
741 int timeout = 0;
742 char *pos;
743
744 if (pin) {
745 pos = os_strchr(pin, ' ');
746 if (pos) {
747 *pos++ = '\0';
748 timeout = atoi(pos);
749 }
750 }
751
752 return wpa_supplicant_ap_wps_pin(wpa_s, _bssid, pin,
753 buf, buflen, timeout);
754 }
755 #endif /* CONFIG_AP */
756
757 if (pin) {
758 ret = wpas_wps_start_pin(wpa_s, _bssid, pin, 0,
759 DEV_PW_DEFAULT);
760 if (ret < 0)
761 return -1;
762 ret = os_snprintf(buf, buflen, "%s", pin);
763 if (ret < 0 || (size_t) ret >= buflen)
764 return -1;
765 return ret;
766 }
767
768 ret = wpas_wps_start_pin(wpa_s, _bssid, NULL, 0, DEV_PW_DEFAULT);
769 if (ret < 0)
770 return -1;
771
772 done:
773 /* Return the generated PIN */
774 ret = os_snprintf(buf, buflen, "%08d", ret);
775 if (ret < 0 || (size_t) ret >= buflen)
776 return -1;
777 return ret;
778 }
779
780
781 static int wpa_supplicant_ctrl_iface_wps_check_pin(
782 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
783 {
784 char pin[9];
785 size_t len;
786 char *pos;
787 int ret;
788
789 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
790 (u8 *) cmd, os_strlen(cmd));
791 for (pos = cmd, len = 0; *pos != '\0'; pos++) {
792 if (*pos < '0' || *pos > '9')
793 continue;
794 pin[len++] = *pos;
795 if (len == 9) {
796 wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
797 return -1;
798 }
799 }
800 if (len != 4 && len != 8) {
801 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
802 return -1;
803 }
804 pin[len] = '\0';
805
806 if (len == 8) {
807 unsigned int pin_val;
808 pin_val = atoi(pin);
809 if (!wps_pin_valid(pin_val)) {
810 wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
811 ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
812 if (ret < 0 || (size_t) ret >= buflen)
813 return -1;
814 return ret;
815 }
816 }
817
818 ret = os_snprintf(buf, buflen, "%s", pin);
819 if (ret < 0 || (size_t) ret >= buflen)
820 return -1;
821
822 return ret;
823 }
824
825
826 #ifdef CONFIG_WPS_NFC
827
828 static int wpa_supplicant_ctrl_iface_wps_nfc(struct wpa_supplicant *wpa_s,
829 char *cmd)
830 {
831 u8 bssid[ETH_ALEN], *_bssid = bssid;
832
833 if (cmd == NULL || cmd[0] == '\0')
834 _bssid = NULL;
835 else if (hwaddr_aton(cmd, bssid))
836 return -1;
837
838 return wpas_wps_start_nfc(wpa_s, NULL, _bssid, NULL, 0, 0, NULL, NULL,
839 0, 0);
840 }
841
842
843 static int wpa_supplicant_ctrl_iface_wps_nfc_config_token(
844 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
845 {
846 int ndef;
847 struct wpabuf *buf;
848 int res;
849 char *pos;
850
851 pos = os_strchr(cmd, ' ');
852 if (pos)
853 *pos++ = '\0';
854 if (os_strcmp(cmd, "WPS") == 0)
855 ndef = 0;
856 else if (os_strcmp(cmd, "NDEF") == 0)
857 ndef = 1;
858 else
859 return -1;
860
861 buf = wpas_wps_nfc_config_token(wpa_s, ndef, pos);
862 if (buf == NULL)
863 return -1;
864
865 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
866 wpabuf_len(buf));
867 reply[res++] = '\n';
868 reply[res] = '\0';
869
870 wpabuf_free(buf);
871
872 return res;
873 }
874
875
876 static int wpa_supplicant_ctrl_iface_wps_nfc_token(
877 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
878 {
879 int ndef;
880 struct wpabuf *buf;
881 int res;
882
883 if (os_strcmp(cmd, "WPS") == 0)
884 ndef = 0;
885 else if (os_strcmp(cmd, "NDEF") == 0)
886 ndef = 1;
887 else
888 return -1;
889
890 buf = wpas_wps_nfc_token(wpa_s, ndef);
891 if (buf == NULL)
892 return -1;
893
894 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
895 wpabuf_len(buf));
896 reply[res++] = '\n';
897 reply[res] = '\0';
898
899 wpabuf_free(buf);
900
901 return res;
902 }
903
904
905 static int wpa_supplicant_ctrl_iface_wps_nfc_tag_read(
906 struct wpa_supplicant *wpa_s, char *pos)
907 {
908 size_t len;
909 struct wpabuf *buf;
910 int ret;
911 char *freq;
912 int forced_freq = 0;
913
914 freq = strstr(pos, " freq=");
915 if (freq) {
916 *freq = '\0';
917 freq += 6;
918 forced_freq = atoi(freq);
919 }
920
921 len = os_strlen(pos);
922 if (len & 0x01)
923 return -1;
924 len /= 2;
925
926 buf = wpabuf_alloc(len);
927 if (buf == NULL)
928 return -1;
929 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
930 wpabuf_free(buf);
931 return -1;
932 }
933
934 ret = wpas_wps_nfc_tag_read(wpa_s, buf, forced_freq);
935 wpabuf_free(buf);
936
937 return ret;
938 }
939
940
941 static int wpas_ctrl_nfc_get_handover_req_wps(struct wpa_supplicant *wpa_s,
942 char *reply, size_t max_len,
943 int ndef)
944 {
945 struct wpabuf *buf;
946 int res;
947
948 buf = wpas_wps_nfc_handover_req(wpa_s, ndef);
949 if (buf == NULL)
950 return -1;
951
952 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
953 wpabuf_len(buf));
954 reply[res++] = '\n';
955 reply[res] = '\0';
956
957 wpabuf_free(buf);
958
959 return res;
960 }
961
962
963 #ifdef CONFIG_P2P
964 static int wpas_ctrl_nfc_get_handover_req_p2p(struct wpa_supplicant *wpa_s,
965 char *reply, size_t max_len,
966 int ndef)
967 {
968 struct wpabuf *buf;
969 int res;
970
971 buf = wpas_p2p_nfc_handover_req(wpa_s, ndef);
972 if (buf == NULL) {
973 wpa_printf(MSG_DEBUG, "P2P: Could not generate NFC handover request");
974 return -1;
975 }
976
977 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
978 wpabuf_len(buf));
979 reply[res++] = '\n';
980 reply[res] = '\0';
981
982 wpabuf_free(buf);
983
984 return res;
985 }
986 #endif /* CONFIG_P2P */
987
988
989 static int wpas_ctrl_nfc_get_handover_req(struct wpa_supplicant *wpa_s,
990 char *cmd, char *reply,
991 size_t max_len)
992 {
993 char *pos;
994 int ndef;
995
996 pos = os_strchr(cmd, ' ');
997 if (pos == NULL)
998 return -1;
999 *pos++ = '\0';
1000
1001 if (os_strcmp(cmd, "WPS") == 0)
1002 ndef = 0;
1003 else if (os_strcmp(cmd, "NDEF") == 0)
1004 ndef = 1;
1005 else
1006 return -1;
1007
1008 if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
1009 if (!ndef)
1010 return -1;
1011 return wpas_ctrl_nfc_get_handover_req_wps(
1012 wpa_s, reply, max_len, ndef);
1013 }
1014
1015 #ifdef CONFIG_P2P
1016 if (os_strcmp(pos, "P2P-CR") == 0) {
1017 return wpas_ctrl_nfc_get_handover_req_p2p(
1018 wpa_s, reply, max_len, ndef);
1019 }
1020 #endif /* CONFIG_P2P */
1021
1022 return -1;
1023 }
1024
1025
1026 static int wpas_ctrl_nfc_get_handover_sel_wps(struct wpa_supplicant *wpa_s,
1027 char *reply, size_t max_len,
1028 int ndef, int cr, char *uuid)
1029 {
1030 struct wpabuf *buf;
1031 int res;
1032
1033 buf = wpas_wps_nfc_handover_sel(wpa_s, ndef, cr, uuid);
1034 if (buf == NULL)
1035 return -1;
1036
1037 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1038 wpabuf_len(buf));
1039 reply[res++] = '\n';
1040 reply[res] = '\0';
1041
1042 wpabuf_free(buf);
1043
1044 return res;
1045 }
1046
1047
1048 #ifdef CONFIG_P2P
1049 static int wpas_ctrl_nfc_get_handover_sel_p2p(struct wpa_supplicant *wpa_s,
1050 char *reply, size_t max_len,
1051 int ndef, int tag)
1052 {
1053 struct wpabuf *buf;
1054 int res;
1055
1056 buf = wpas_p2p_nfc_handover_sel(wpa_s, ndef, tag);
1057 if (buf == NULL)
1058 return -1;
1059
1060 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1061 wpabuf_len(buf));
1062 reply[res++] = '\n';
1063 reply[res] = '\0';
1064
1065 wpabuf_free(buf);
1066
1067 return res;
1068 }
1069 #endif /* CONFIG_P2P */
1070
1071
1072 static int wpas_ctrl_nfc_get_handover_sel(struct wpa_supplicant *wpa_s,
1073 char *cmd, char *reply,
1074 size_t max_len)
1075 {
1076 char *pos, *pos2;
1077 int ndef;
1078
1079 pos = os_strchr(cmd, ' ');
1080 if (pos == NULL)
1081 return -1;
1082 *pos++ = '\0';
1083
1084 if (os_strcmp(cmd, "WPS") == 0)
1085 ndef = 0;
1086 else if (os_strcmp(cmd, "NDEF") == 0)
1087 ndef = 1;
1088 else
1089 return -1;
1090
1091 pos2 = os_strchr(pos, ' ');
1092 if (pos2)
1093 *pos2++ = '\0';
1094 if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
1095 if (!ndef)
1096 return -1;
1097 return wpas_ctrl_nfc_get_handover_sel_wps(
1098 wpa_s, reply, max_len, ndef,
1099 os_strcmp(pos, "WPS-CR") == 0, pos2);
1100 }
1101
1102 #ifdef CONFIG_P2P
1103 if (os_strcmp(pos, "P2P-CR") == 0) {
1104 return wpas_ctrl_nfc_get_handover_sel_p2p(
1105 wpa_s, reply, max_len, ndef, 0);
1106 }
1107
1108 if (os_strcmp(pos, "P2P-CR-TAG") == 0) {
1109 return wpas_ctrl_nfc_get_handover_sel_p2p(
1110 wpa_s, reply, max_len, ndef, 1);
1111 }
1112 #endif /* CONFIG_P2P */
1113
1114 return -1;
1115 }
1116
1117
1118 static int wpas_ctrl_nfc_report_handover(struct wpa_supplicant *wpa_s,
1119 char *cmd)
1120 {
1121 size_t len;
1122 struct wpabuf *req, *sel;
1123 int ret;
1124 char *pos, *role, *type, *pos2;
1125 #ifdef CONFIG_P2P
1126 char *freq;
1127 int forced_freq = 0;
1128
1129 freq = strstr(cmd, " freq=");
1130 if (freq) {
1131 *freq = '\0';
1132 freq += 6;
1133 forced_freq = atoi(freq);
1134 }
1135 #endif /* CONFIG_P2P */
1136
1137 role = cmd;
1138 pos = os_strchr(role, ' ');
1139 if (pos == NULL) {
1140 wpa_printf(MSG_DEBUG, "NFC: Missing type in handover report");
1141 return -1;
1142 }
1143 *pos++ = '\0';
1144
1145 type = pos;
1146 pos = os_strchr(type, ' ');
1147 if (pos == NULL) {
1148 wpa_printf(MSG_DEBUG, "NFC: Missing request message in handover report");
1149 return -1;
1150 }
1151 *pos++ = '\0';
1152
1153 pos2 = os_strchr(pos, ' ');
1154 if (pos2 == NULL) {
1155 wpa_printf(MSG_DEBUG, "NFC: Missing select message in handover report");
1156 return -1;
1157 }
1158 *pos2++ = '\0';
1159
1160 len = os_strlen(pos);
1161 if (len & 0x01) {
1162 wpa_printf(MSG_DEBUG, "NFC: Invalid request message length in handover report");
1163 return -1;
1164 }
1165 len /= 2;
1166
1167 req = wpabuf_alloc(len);
1168 if (req == NULL) {
1169 wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for request message");
1170 return -1;
1171 }
1172 if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
1173 wpa_printf(MSG_DEBUG, "NFC: Invalid request message hexdump in handover report");
1174 wpabuf_free(req);
1175 return -1;
1176 }
1177
1178 len = os_strlen(pos2);
1179 if (len & 0x01) {
1180 wpa_printf(MSG_DEBUG, "NFC: Invalid select message length in handover report");
1181 wpabuf_free(req);
1182 return -1;
1183 }
1184 len /= 2;
1185
1186 sel = wpabuf_alloc(len);
1187 if (sel == NULL) {
1188 wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for select message");
1189 wpabuf_free(req);
1190 return -1;
1191 }
1192 if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
1193 wpa_printf(MSG_DEBUG, "NFC: Invalid select message hexdump in handover report");
1194 wpabuf_free(req);
1195 wpabuf_free(sel);
1196 return -1;
1197 }
1198
1199 wpa_printf(MSG_DEBUG, "NFC: Connection handover reported - role=%s type=%s req_len=%d sel_len=%d",
1200 role, type, (int) wpabuf_len(req), (int) wpabuf_len(sel));
1201
1202 if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "WPS") == 0) {
1203 ret = wpas_wps_nfc_report_handover(wpa_s, req, sel);
1204 #ifdef CONFIG_AP
1205 } else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0)
1206 {
1207 ret = wpas_ap_wps_nfc_report_handover(wpa_s, req, sel);
1208 if (ret < 0)
1209 ret = wpas_er_wps_nfc_report_handover(wpa_s, req, sel);
1210 #endif /* CONFIG_AP */
1211 #ifdef CONFIG_P2P
1212 } else if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "P2P") == 0)
1213 {
1214 ret = wpas_p2p_nfc_report_handover(wpa_s, 1, req, sel, 0);
1215 } else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "P2P") == 0)
1216 {
1217 ret = wpas_p2p_nfc_report_handover(wpa_s, 0, req, sel,
1218 forced_freq);
1219 #endif /* CONFIG_P2P */
1220 } else {
1221 wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
1222 "reported: role=%s type=%s", role, type);
1223 ret = -1;
1224 }
1225 wpabuf_free(req);
1226 wpabuf_free(sel);
1227
1228 if (ret)
1229 wpa_printf(MSG_DEBUG, "NFC: Failed to process reported handover messages");
1230
1231 return ret;
1232 }
1233
1234 #endif /* CONFIG_WPS_NFC */
1235
1236
1237 static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
1238 char *cmd)
1239 {
1240 u8 bssid[ETH_ALEN];
1241 char *pin;
1242 char *new_ssid;
1243 char *new_auth;
1244 char *new_encr;
1245 char *new_key;
1246 struct wps_new_ap_settings ap;
1247
1248 pin = os_strchr(cmd, ' ');
1249 if (pin == NULL)
1250 return -1;
1251 *pin++ = '\0';
1252
1253 if (hwaddr_aton(cmd, bssid)) {
1254 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
1255 cmd);
1256 return -1;
1257 }
1258
1259 new_ssid = os_strchr(pin, ' ');
1260 if (new_ssid == NULL)
1261 return wpas_wps_start_reg(wpa_s, bssid, pin, NULL);
1262 *new_ssid++ = '\0';
1263
1264 new_auth = os_strchr(new_ssid, ' ');
1265 if (new_auth == NULL)
1266 return -1;
1267 *new_auth++ = '\0';
1268
1269 new_encr = os_strchr(new_auth, ' ');
1270 if (new_encr == NULL)
1271 return -1;
1272 *new_encr++ = '\0';
1273
1274 new_key = os_strchr(new_encr, ' ');
1275 if (new_key == NULL)
1276 return -1;
1277 *new_key++ = '\0';
1278
1279 os_memset(&ap, 0, sizeof(ap));
1280 ap.ssid_hex = new_ssid;
1281 ap.auth = new_auth;
1282 ap.encr = new_encr;
1283 ap.key_hex = new_key;
1284 return wpas_wps_start_reg(wpa_s, bssid, pin, &ap);
1285 }
1286
1287
1288 #ifdef CONFIG_AP
1289 static int wpa_supplicant_ctrl_iface_wps_ap_pin(struct wpa_supplicant *wpa_s,
1290 char *cmd, char *buf,
1291 size_t buflen)
1292 {
1293 int timeout = 300;
1294 char *pos;
1295 const char *pin_txt;
1296
1297 if (!wpa_s->ap_iface)
1298 return -1;
1299
1300 pos = os_strchr(cmd, ' ');
1301 if (pos)
1302 *pos++ = '\0';
1303
1304 if (os_strcmp(cmd, "disable") == 0) {
1305 wpas_wps_ap_pin_disable(wpa_s);
1306 return os_snprintf(buf, buflen, "OK\n");
1307 }
1308
1309 if (os_strcmp(cmd, "random") == 0) {
1310 if (pos)
1311 timeout = atoi(pos);
1312 pin_txt = wpas_wps_ap_pin_random(wpa_s, timeout);
1313 if (pin_txt == NULL)
1314 return -1;
1315 return os_snprintf(buf, buflen, "%s", pin_txt);
1316 }
1317
1318 if (os_strcmp(cmd, "get") == 0) {
1319 pin_txt = wpas_wps_ap_pin_get(wpa_s);
1320 if (pin_txt == NULL)
1321 return -1;
1322 return os_snprintf(buf, buflen, "%s", pin_txt);
1323 }
1324
1325 if (os_strcmp(cmd, "set") == 0) {
1326 char *pin;
1327 if (pos == NULL)
1328 return -1;
1329 pin = pos;
1330 pos = os_strchr(pos, ' ');
1331 if (pos) {
1332 *pos++ = '\0';
1333 timeout = atoi(pos);
1334 }
1335 if (os_strlen(pin) > buflen)
1336 return -1;
1337 if (wpas_wps_ap_pin_set(wpa_s, pin, timeout) < 0)
1338 return -1;
1339 return os_snprintf(buf, buflen, "%s", pin);
1340 }
1341
1342 return -1;
1343 }
1344 #endif /* CONFIG_AP */
1345
1346
1347 #ifdef CONFIG_WPS_ER
1348 static int wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant *wpa_s,
1349 char *cmd)
1350 {
1351 char *uuid = cmd, *pin, *pos;
1352 u8 addr_buf[ETH_ALEN], *addr = NULL;
1353 pin = os_strchr(uuid, ' ');
1354 if (pin == NULL)
1355 return -1;
1356 *pin++ = '\0';
1357 pos = os_strchr(pin, ' ');
1358 if (pos) {
1359 *pos++ = '\0';
1360 if (hwaddr_aton(pos, addr_buf) == 0)
1361 addr = addr_buf;
1362 }
1363 return wpas_wps_er_add_pin(wpa_s, addr, uuid, pin);
1364 }
1365
1366
1367 static int wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant *wpa_s,
1368 char *cmd)
1369 {
1370 char *uuid = cmd, *pin;
1371 pin = os_strchr(uuid, ' ');
1372 if (pin == NULL)
1373 return -1;
1374 *pin++ = '\0';
1375 return wpas_wps_er_learn(wpa_s, uuid, pin);
1376 }
1377
1378
1379 static int wpa_supplicant_ctrl_iface_wps_er_set_config(
1380 struct wpa_supplicant *wpa_s, char *cmd)
1381 {
1382 char *uuid = cmd, *id;
1383 id = os_strchr(uuid, ' ');
1384 if (id == NULL)
1385 return -1;
1386 *id++ = '\0';
1387 return wpas_wps_er_set_config(wpa_s, uuid, atoi(id));
1388 }
1389
1390
1391 static int wpa_supplicant_ctrl_iface_wps_er_config(
1392 struct wpa_supplicant *wpa_s, char *cmd)
1393 {
1394 char *pin;
1395 char *new_ssid;
1396 char *new_auth;
1397 char *new_encr;
1398 char *new_key;
1399 struct wps_new_ap_settings ap;
1400
1401 pin = os_strchr(cmd, ' ');
1402 if (pin == NULL)
1403 return -1;
1404 *pin++ = '\0';
1405
1406 new_ssid = os_strchr(pin, ' ');
1407 if (new_ssid == NULL)
1408 return -1;
1409 *new_ssid++ = '\0';
1410
1411 new_auth = os_strchr(new_ssid, ' ');
1412 if (new_auth == NULL)
1413 return -1;
1414 *new_auth++ = '\0';
1415
1416 new_encr = os_strchr(new_auth, ' ');
1417 if (new_encr == NULL)
1418 return -1;
1419 *new_encr++ = '\0';
1420
1421 new_key = os_strchr(new_encr, ' ');
1422 if (new_key == NULL)
1423 return -1;
1424 *new_key++ = '\0';
1425
1426 os_memset(&ap, 0, sizeof(ap));
1427 ap.ssid_hex = new_ssid;
1428 ap.auth = new_auth;
1429 ap.encr = new_encr;
1430 ap.key_hex = new_key;
1431 return wpas_wps_er_config(wpa_s, cmd, pin, &ap);
1432 }
1433
1434
1435 #ifdef CONFIG_WPS_NFC
1436 static int wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
1437 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
1438 {
1439 int ndef;
1440 struct wpabuf *buf;
1441 int res;
1442 char *uuid;
1443
1444 uuid = os_strchr(cmd, ' ');
1445 if (uuid == NULL)
1446 return -1;
1447 *uuid++ = '\0';
1448
1449 if (os_strcmp(cmd, "WPS") == 0)
1450 ndef = 0;
1451 else if (os_strcmp(cmd, "NDEF") == 0)
1452 ndef = 1;
1453 else
1454 return -1;
1455
1456 buf = wpas_wps_er_nfc_config_token(wpa_s, ndef, uuid);
1457 if (buf == NULL)
1458 return -1;
1459
1460 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1461 wpabuf_len(buf));
1462 reply[res++] = '\n';
1463 reply[res] = '\0';
1464
1465 wpabuf_free(buf);
1466
1467 return res;
1468 }
1469 #endif /* CONFIG_WPS_NFC */
1470 #endif /* CONFIG_WPS_ER */
1471
1472 #endif /* CONFIG_WPS */
1473
1474
1475 #ifdef CONFIG_IBSS_RSN
1476 static int wpa_supplicant_ctrl_iface_ibss_rsn(
1477 struct wpa_supplicant *wpa_s, char *addr)
1478 {
1479 u8 peer[ETH_ALEN];
1480
1481 if (hwaddr_aton(addr, peer)) {
1482 wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
1483 "address '%s'", addr);
1484 return -1;
1485 }
1486
1487 wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
1488 MAC2STR(peer));
1489
1490 return ibss_rsn_start(wpa_s->ibss_rsn, peer);
1491 }
1492 #endif /* CONFIG_IBSS_RSN */
1493
1494
1495 static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
1496 char *rsp)
1497 {
1498 #ifdef IEEE8021X_EAPOL
1499 char *pos, *id_pos;
1500 int id;
1501 struct wpa_ssid *ssid;
1502
1503 pos = os_strchr(rsp, '-');
1504 if (pos == NULL)
1505 return -1;
1506 *pos++ = '\0';
1507 id_pos = pos;
1508 pos = os_strchr(pos, ':');
1509 if (pos == NULL)
1510 return -1;
1511 *pos++ = '\0';
1512 id = atoi(id_pos);
1513 wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
1514 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
1515 (u8 *) pos, os_strlen(pos));
1516
1517 ssid = wpa_config_get_network(wpa_s->conf, id);
1518 if (ssid == NULL) {
1519 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
1520 "to update", id);
1521 return -1;
1522 }
1523
1524 return wpa_supplicant_ctrl_iface_ctrl_rsp_handle(wpa_s, ssid, rsp,
1525 pos);
1526 #else /* IEEE8021X_EAPOL */
1527 wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
1528 return -1;
1529 #endif /* IEEE8021X_EAPOL */
1530 }
1531
1532
1533 static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
1534 const char *params,
1535 char *buf, size_t buflen)
1536 {
1537 char *pos, *end, tmp[30];
1538 int res, verbose, wps, ret;
1539 #ifdef CONFIG_HS20
1540 const u8 *hs20;
1541 #endif /* CONFIG_HS20 */
1542
1543 if (os_strcmp(params, "-DRIVER") == 0)
1544 return wpa_drv_status(wpa_s, buf, buflen);
1545 verbose = os_strcmp(params, "-VERBOSE") == 0;
1546 wps = os_strcmp(params, "-WPS") == 0;
1547 pos = buf;
1548 end = buf + buflen;
1549 if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
1550 struct wpa_ssid *ssid = wpa_s->current_ssid;
1551 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
1552 MAC2STR(wpa_s->bssid));
1553 if (ret < 0 || ret >= end - pos)
1554 return pos - buf;
1555 pos += ret;
1556 ret = os_snprintf(pos, end - pos, "freq=%u\n",
1557 wpa_s->assoc_freq);
1558 if (ret < 0 || ret >= end - pos)
1559 return pos - buf;
1560 pos += ret;
1561 if (ssid) {
1562 u8 *_ssid = ssid->ssid;
1563 size_t ssid_len = ssid->ssid_len;
1564 u8 ssid_buf[MAX_SSID_LEN];
1565 if (ssid_len == 0) {
1566 int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
1567 if (_res < 0)
1568 ssid_len = 0;
1569 else
1570 ssid_len = _res;
1571 _ssid = ssid_buf;
1572 }
1573 ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
1574 wpa_ssid_txt(_ssid, ssid_len),
1575 ssid->id);
1576 if (ret < 0 || ret >= end - pos)
1577 return pos - buf;
1578 pos += ret;
1579
1580 if (wps && ssid->passphrase &&
1581 wpa_key_mgmt_wpa_psk(ssid->key_mgmt) &&
1582 (ssid->mode == WPAS_MODE_AP ||
1583 ssid->mode == WPAS_MODE_P2P_GO)) {
1584 ret = os_snprintf(pos, end - pos,
1585 "passphrase=%s\n",
1586 ssid->passphrase);
1587 if (ret < 0 || ret >= end - pos)
1588 return pos - buf;
1589 pos += ret;
1590 }
1591 if (ssid->id_str) {
1592 ret = os_snprintf(pos, end - pos,
1593 "id_str=%s\n",
1594 ssid->id_str);
1595 if (ret < 0 || ret >= end - pos)
1596 return pos - buf;
1597 pos += ret;
1598 }
1599
1600 switch (ssid->mode) {
1601 case WPAS_MODE_INFRA:
1602 ret = os_snprintf(pos, end - pos,
1603 "mode=station\n");
1604 break;
1605 case WPAS_MODE_IBSS:
1606 ret = os_snprintf(pos, end - pos,
1607 "mode=IBSS\n");
1608 break;
1609 case WPAS_MODE_AP:
1610 ret = os_snprintf(pos, end - pos,
1611 "mode=AP\n");
1612 break;
1613 case WPAS_MODE_P2P_GO:
1614 ret = os_snprintf(pos, end - pos,
1615 "mode=P2P GO\n");
1616 break;
1617 case WPAS_MODE_P2P_GROUP_FORMATION:
1618 ret = os_snprintf(pos, end - pos,
1619 "mode=P2P GO - group "
1620 "formation\n");
1621 break;
1622 default:
1623 ret = 0;
1624 break;
1625 }
1626 if (ret < 0 || ret >= end - pos)
1627 return pos - buf;
1628 pos += ret;
1629 }
1630
1631 #ifdef CONFIG_AP
1632 if (wpa_s->ap_iface) {
1633 pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
1634 end - pos,
1635 verbose);
1636 } else
1637 #endif /* CONFIG_AP */
1638 pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
1639 }
1640 #ifdef CONFIG_SAE
1641 if (wpa_s->wpa_state >= WPA_ASSOCIATED &&
1642 #ifdef CONFIG_AP
1643 !wpa_s->ap_iface &&
1644 #endif /* CONFIG_AP */
1645 wpa_s->sme.sae.state == SAE_ACCEPTED) {
1646 ret = os_snprintf(pos, end - pos, "sae_group=%d\n",
1647 wpa_s->sme.sae.group);
1648 if (ret < 0 || ret >= end - pos)
1649 return pos - buf;
1650 pos += ret;
1651 }
1652 #endif /* CONFIG_SAE */
1653 ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
1654 wpa_supplicant_state_txt(wpa_s->wpa_state));
1655 if (ret < 0 || ret >= end - pos)
1656 return pos - buf;
1657 pos += ret;
1658
1659 if (wpa_s->l2 &&
1660 l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
1661 ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
1662 if (ret < 0 || ret >= end - pos)
1663 return pos - buf;
1664 pos += ret;
1665 }
1666
1667 #ifdef CONFIG_P2P
1668 if (wpa_s->global->p2p) {
1669 ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
1670 "\n", MAC2STR(wpa_s->global->p2p_dev_addr));
1671 if (ret < 0 || ret >= end - pos)
1672 return pos - buf;
1673 pos += ret;
1674 }
1675 #endif /* CONFIG_P2P */
1676
1677 ret = os_snprintf(pos, end - pos, "address=" MACSTR "\n",
1678 MAC2STR(wpa_s->own_addr));
1679 if (ret < 0 || ret >= end - pos)
1680 return pos - buf;
1681 pos += ret;
1682
1683 #ifdef CONFIG_HS20
1684 if (wpa_s->current_bss &&
1685 (hs20 = wpa_bss_get_vendor_ie(wpa_s->current_bss,
1686 HS20_IE_VENDOR_TYPE)) &&
1687 wpa_s->wpa_proto == WPA_PROTO_RSN &&
1688 wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1689 int release = 1;
1690 if (hs20[1] >= 5) {
1691 u8 rel_num = (hs20[6] & 0xf0) >> 4;
1692 release = rel_num + 1;
1693 }
1694 ret = os_snprintf(pos, end - pos, "hs20=%d\n", release);
1695 if (ret < 0 || ret >= end - pos)
1696 return pos - buf;
1697 pos += ret;
1698 }
1699
1700 if (wpa_s->current_ssid) {
1701 struct wpa_cred *cred;
1702 char *type;
1703
1704 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
1705 size_t i;
1706
1707 if (wpa_s->current_ssid->parent_cred != cred)
1708 continue;
1709
1710 if (cred->provisioning_sp) {
1711 ret = os_snprintf(pos, end - pos,
1712 "provisioning_sp=%s\n",
1713 cred->provisioning_sp);
1714 if (ret < 0 || ret >= end - pos)
1715 return pos - buf;
1716 pos += ret;
1717 }
1718
1719 if (!cred->domain)
1720 goto no_domain;
1721
1722 i = 0;
1723 if (wpa_s->current_bss && wpa_s->current_bss->anqp) {
1724 struct wpabuf *names =
1725 wpa_s->current_bss->anqp->domain_name;
1726 for (i = 0; names && i < cred->num_domain; i++)
1727 {
1728 if (domain_name_list_contains(
1729 names, cred->domain[i], 1))
1730 break;
1731 }
1732 if (i == cred->num_domain)
1733 i = 0; /* show first entry by default */
1734 }
1735 ret = os_snprintf(pos, end - pos, "home_sp=%s\n",
1736 cred->domain[i]);
1737 if (ret < 0 || ret >= end - pos)
1738 return pos - buf;
1739 pos += ret;
1740
1741 no_domain:
1742 if (wpa_s->current_bss == NULL ||
1743 wpa_s->current_bss->anqp == NULL)
1744 res = -1;
1745 else
1746 res = interworking_home_sp_cred(
1747 wpa_s, cred,
1748 wpa_s->current_bss->anqp->domain_name);
1749 if (res > 0)
1750 type = "home";
1751 else if (res == 0)
1752 type = "roaming";
1753 else
1754 type = "unknown";
1755
1756 ret = os_snprintf(pos, end - pos, "sp_type=%s\n", type);
1757 if (ret < 0 || ret >= end - pos)
1758 return pos - buf;
1759 pos += ret;
1760
1761 break;
1762 }
1763 }
1764 #endif /* CONFIG_HS20 */
1765
1766 if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
1767 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1768 res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
1769 verbose);
1770 if (res >= 0)
1771 pos += res;
1772 }
1773
1774 res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
1775 if (res >= 0)
1776 pos += res;
1777
1778 #ifdef CONFIG_WPS
1779 {
1780 char uuid_str[100];
1781 uuid_bin2str(wpa_s->wps->uuid, uuid_str, sizeof(uuid_str));
1782 ret = os_snprintf(pos, end - pos, "uuid=%s\n", uuid_str);
1783 if (ret < 0 || ret >= end - pos)
1784 return pos - buf;
1785 pos += ret;
1786 }
1787 #endif /* CONFIG_WPS */
1788
1789 #ifdef ANDROID
1790 /*
1791 * Allow using the STATUS command with default behavior, say for debug,
1792 * i.e., don't generate a "fake" CONNECTION and SUPPLICANT_STATE_CHANGE
1793 * events with STATUS-NO_EVENTS.
1794 */
1795 if (os_strcmp(params, "-NO_EVENTS")) {
1796 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_STATE_CHANGE
1797 "id=%d state=%d BSSID=" MACSTR " SSID=%s",
1798 wpa_s->current_ssid ? wpa_s->current_ssid->id : -1,
1799 wpa_s->wpa_state,
1800 MAC2STR(wpa_s->bssid),
1801 wpa_s->current_ssid && wpa_s->current_ssid->ssid ?
1802 wpa_ssid_txt(wpa_s->current_ssid->ssid,
1803 wpa_s->current_ssid->ssid_len) : "");
1804 if (wpa_s->wpa_state == WPA_COMPLETED) {
1805 struct wpa_ssid *ssid = wpa_s->current_ssid;
1806 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED
1807 "- connection to " MACSTR
1808 " completed %s [id=%d id_str=%s]",
1809 MAC2STR(wpa_s->bssid), "(auth)",
1810 ssid ? ssid->id : -1,
1811 ssid && ssid->id_str ? ssid->id_str : "");
1812 }
1813 }
1814 #endif /* ANDROID */
1815
1816 return pos - buf;
1817 }
1818
1819
1820 static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
1821 char *cmd)
1822 {
1823 char *pos;
1824 int id;
1825 struct wpa_ssid *ssid;
1826 u8 bssid[ETH_ALEN];
1827
1828 /* cmd: "<network id> <BSSID>" */
1829 pos = os_strchr(cmd, ' ');
1830 if (pos == NULL)
1831 return -1;
1832 *pos++ = '\0';
1833 id = atoi(cmd);
1834 wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
1835 if (hwaddr_aton(pos, bssid)) {
1836 wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
1837 return -1;
1838 }
1839
1840 ssid = wpa_config_get_network(wpa_s->conf, id);
1841 if (ssid == NULL) {
1842 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
1843 "to update", id);
1844 return -1;
1845 }
1846
1847 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
1848 ssid->bssid_set = !is_zero_ether_addr(bssid);
1849
1850 return 0;
1851 }
1852
1853
1854 static int wpa_supplicant_ctrl_iface_blacklist(struct wpa_supplicant *wpa_s,
1855 char *cmd, char *buf,
1856 size_t buflen)
1857 {
1858 u8 bssid[ETH_ALEN];
1859 struct wpa_blacklist *e;
1860 char *pos, *end;
1861 int ret;
1862
1863 /* cmd: "BLACKLIST [<BSSID>]" */
1864 if (*cmd == '\0') {
1865 pos = buf;
1866 end = buf + buflen;
1867 e = wpa_s->blacklist;
1868 while (e) {
1869 ret = os_snprintf(pos, end - pos, MACSTR "\n",
1870 MAC2STR(e->bssid));
1871 if (ret < 0 || ret >= end - pos)
1872 return pos - buf;
1873 pos += ret;
1874 e = e->next;
1875 }
1876 return pos - buf;
1877 }
1878
1879 cmd++;
1880 if (os_strncmp(cmd, "clear", 5) == 0) {
1881 wpa_blacklist_clear(wpa_s);
1882 os_memcpy(buf, "OK\n", 3);
1883 return 3;
1884 }
1885
1886 wpa_printf(MSG_DEBUG, "CTRL_IFACE: BLACKLIST bssid='%s'", cmd);
1887 if (hwaddr_aton(cmd, bssid)) {
1888 wpa_printf(MSG_DEBUG, "CTRL_IFACE: invalid BSSID '%s'", cmd);
1889 return -1;
1890 }
1891
1892 /*
1893 * Add the BSSID twice, so its count will be 2, causing it to be
1894 * skipped when processing scan results.
1895 */
1896 ret = wpa_blacklist_add(wpa_s, bssid);
1897 if (ret < 0)
1898 return -1;
1899 ret = wpa_blacklist_add(wpa_s, bssid);
1900 if (ret < 0)
1901 return -1;
1902 os_memcpy(buf, "OK\n", 3);
1903 return 3;
1904 }
1905
1906
1907 static const char * debug_level_str(int level)
1908 {
1909 switch (level) {
1910 case MSG_EXCESSIVE:
1911 return "EXCESSIVE";
1912 case MSG_MSGDUMP:
1913 return "MSGDUMP";
1914 case MSG_DEBUG:
1915 return "DEBUG";
1916 case MSG_INFO:
1917 return "INFO";
1918 case MSG_WARNING:
1919 return "WARNING";
1920 case MSG_ERROR:
1921 return "ERROR";
1922 default:
1923 return "?";
1924 }
1925 }
1926
1927
1928 static int str_to_debug_level(const char *s)
1929 {
1930 if (os_strcasecmp(s, "EXCESSIVE") == 0)
1931 return MSG_EXCESSIVE;
1932 if (os_strcasecmp(s, "MSGDUMP") == 0)
1933 return MSG_MSGDUMP;
1934 if (os_strcasecmp(s, "DEBUG") == 0)
1935 return MSG_DEBUG;
1936 if (os_strcasecmp(s, "INFO") == 0)
1937 return MSG_INFO;
1938 if (os_strcasecmp(s, "WARNING") == 0)
1939 return MSG_WARNING;
1940 if (os_strcasecmp(s, "ERROR") == 0)
1941 return MSG_ERROR;
1942 return -1;
1943 }
1944
1945
1946 static int wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant *wpa_s,
1947 char *cmd, char *buf,
1948 size_t buflen)
1949 {
1950 char *pos, *end, *stamp;
1951 int ret;
1952
1953 if (cmd == NULL) {
1954 return -1;
1955 }
1956
1957 /* cmd: "LOG_LEVEL [<level>]" */
1958 if (*cmd == '\0') {
1959 pos = buf;
1960 end = buf + buflen;
1961 ret = os_snprintf(pos, end - pos, "Current level: %s\n"
1962 "Timestamp: %d\n",
1963 debug_level_str(wpa_debug_level),
1964 wpa_debug_timestamp);
1965 if (ret < 0 || ret >= end - pos)
1966 ret = 0;
1967
1968 return ret;
1969 }
1970
1971 while (*cmd == ' ')
1972 cmd++;
1973
1974 stamp = os_strchr(cmd, ' ');
1975 if (stamp) {
1976 *stamp++ = '\0';
1977 while (*stamp == ' ') {
1978 stamp++;
1979 }
1980 }
1981
1982 if (cmd && os_strlen(cmd)) {
1983 int level = str_to_debug_level(cmd);
1984 if (level < 0)
1985 return -1;
1986 wpa_debug_level = level;
1987 }
1988
1989 if (stamp && os_strlen(stamp))
1990 wpa_debug_timestamp = atoi(stamp);
1991
1992 os_memcpy(buf, "OK\n", 3);
1993 return 3;
1994 }
1995
1996
1997 static int wpa_supplicant_ctrl_iface_list_networks(
1998 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
1999 {
2000 char *pos, *end;
2001 struct wpa_ssid *ssid;
2002 int ret;
2003
2004 pos = buf;
2005 end = buf + buflen;
2006 ret = os_snprintf(pos, end - pos,
2007 "network id / ssid / bssid / flags\n");
2008 if (ret < 0 || ret >= end - pos)
2009 return pos - buf;
2010 pos += ret;
2011
2012 ssid = wpa_s->conf->ssid;
2013 while (ssid) {
2014 ret = os_snprintf(pos, end - pos, "%d\t%s",
2015 ssid->id,
2016 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
2017 if (ret < 0 || ret >= end - pos)
2018 return pos - buf;
2019 pos += ret;
2020 if (ssid->bssid_set) {
2021 ret = os_snprintf(pos, end - pos, "\t" MACSTR,
2022 MAC2STR(ssid->bssid));
2023 } else {
2024 ret = os_snprintf(pos, end - pos, "\tany");
2025 }
2026 if (ret < 0 || ret >= end - pos)
2027 return pos - buf;
2028 pos += ret;
2029 ret = os_snprintf(pos, end - pos, "\t%s%s%s%s",
2030 ssid == wpa_s->current_ssid ?
2031 "[CURRENT]" : "",
2032 ssid->disabled ? "[DISABLED]" : "",
2033 ssid->disabled_until.sec ?
2034 "[TEMP-DISABLED]" : "",
2035 ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
2036 "");
2037 if (ret < 0 || ret >= end - pos)
2038 return pos - buf;
2039 pos += ret;
2040 ret = os_snprintf(pos, end - pos, "\n");
2041 if (ret < 0 || ret >= end - pos)
2042 return pos - buf;
2043 pos += ret;
2044
2045 ssid = ssid->next;
2046 }
2047
2048 return pos - buf;
2049 }
2050
2051
2052 static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
2053 {
2054 int ret;
2055 ret = os_snprintf(pos, end - pos, "-");
2056 if (ret < 0 || ret >= end - pos)
2057 return pos;
2058 pos += ret;
2059 ret = wpa_write_ciphers(pos, end, cipher, "+");
2060 if (ret < 0)
2061 return pos;
2062 pos += ret;
2063 return pos;
2064 }
2065
2066
2067 static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
2068 const u8 *ie, size_t ie_len)
2069 {
2070 struct wpa_ie_data data;
2071 char *start;
2072 int ret;
2073
2074 ret = os_snprintf(pos, end - pos, "[%s-", proto);
2075 if (ret < 0 || ret >= end - pos)
2076 return pos;
2077 pos += ret;
2078
2079 if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
2080 ret = os_snprintf(pos, end - pos, "?]");
2081 if (ret < 0 || ret >= end - pos)
2082 return pos;
2083 pos += ret;
2084 return pos;
2085 }
2086
2087 start = pos;
2088 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
2089 ret = os_snprintf(pos, end - pos, "%sEAP",
2090 pos == start ? "" : "+");
2091 if (ret < 0 || ret >= end - pos)
2092 return pos;
2093 pos += ret;
2094 }
2095 if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
2096 ret = os_snprintf(pos, end - pos, "%sPSK",
2097 pos == start ? "" : "+");
2098 if (ret < 0 || ret >= end - pos)
2099 return pos;
2100 pos += ret;
2101 }
2102 if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
2103 ret = os_snprintf(pos, end - pos, "%sNone",
2104 pos == start ? "" : "+");
2105 if (ret < 0 || ret >= end - pos)
2106 return pos;
2107 pos += ret;
2108 }
2109 if (data.key_mgmt & WPA_KEY_MGMT_SAE) {
2110 ret = os_snprintf(pos, end - pos, "%sSAE",
2111 pos == start ? "" : "+");
2112 if (ret < 0 || ret >= end - pos)
2113 return pos;
2114 pos += ret;
2115 }
2116 #ifdef CONFIG_IEEE80211R
2117 if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
2118 ret = os_snprintf(pos, end - pos, "%sFT/EAP",
2119 pos == start ? "" : "+");
2120 if (ret < 0 || ret >= end - pos)
2121 return pos;
2122 pos += ret;
2123 }
2124 if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
2125 ret = os_snprintf(pos, end - pos, "%sFT/PSK",
2126 pos == start ? "" : "+");
2127 if (ret < 0 || ret >= end - pos)
2128 return pos;
2129 pos += ret;
2130 }
2131 if (data.key_mgmt & WPA_KEY_MGMT_FT_SAE) {
2132 ret = os_snprintf(pos, end - pos, "%sFT/SAE",
2133 pos == start ? "" : "+");
2134 if (ret < 0 || ret >= end - pos)
2135 return pos;
2136 pos += ret;
2137 }
2138 #endif /* CONFIG_IEEE80211R */
2139 #ifdef CONFIG_IEEE80211W
2140 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
2141 ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
2142 pos == start ? "" : "+");
2143 if (ret < 0 || ret >= end - pos)
2144 return pos;
2145 pos += ret;
2146 }
2147 if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
2148 ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
2149 pos == start ? "" : "+");
2150 if (ret < 0 || ret >= end - pos)
2151 return pos;
2152 pos += ret;
2153 }
2154 #endif /* CONFIG_IEEE80211W */
2155
2156 pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
2157
2158 if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
2159 ret = os_snprintf(pos, end - pos, "-preauth");
2160 if (ret < 0 || ret >= end - pos)
2161 return pos;
2162 pos += ret;
2163 }
2164
2165 ret = os_snprintf(pos, end - pos, "]");
2166 if (ret < 0 || ret >= end - pos)
2167 return pos;
2168 pos += ret;
2169
2170 return pos;
2171 }
2172
2173
2174 #ifdef CONFIG_WPS
2175 static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
2176 char *pos, char *end,
2177 struct wpabuf *wps_ie)
2178 {
2179 int ret;
2180 const char *txt;
2181
2182 if (wps_ie == NULL)
2183 return pos;
2184 if (wps_is_selected_pbc_registrar(wps_ie))
2185 txt = "[WPS-PBC]";
2186 else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
2187 txt = "[WPS-AUTH]";
2188 else if (wps_is_selected_pin_registrar(wps_ie))
2189 txt = "[WPS-PIN]";
2190 else
2191 txt = "[WPS]";
2192
2193 ret = os_snprintf(pos, end - pos, "%s", txt);
2194 if (ret >= 0 && ret < end - pos)
2195 pos += ret;
2196 wpabuf_free(wps_ie);
2197 return pos;
2198 }
2199 #endif /* CONFIG_WPS */
2200
2201
2202 static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
2203 char *pos, char *end,
2204 const struct wpa_bss *bss)
2205 {
2206 #ifdef CONFIG_WPS
2207 struct wpabuf *wps_ie;
2208 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
2209 return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
2210 #else /* CONFIG_WPS */
2211 return pos;
2212 #endif /* CONFIG_WPS */
2213 }
2214
2215
2216 /* Format one result on one text line into a buffer. */
2217 static int wpa_supplicant_ctrl_iface_scan_result(
2218 struct wpa_supplicant *wpa_s,
2219 const struct wpa_bss *bss, char *buf, size_t buflen)
2220 {
2221 char *pos, *end;
2222 int ret;
2223 const u8 *ie, *ie2, *p2p;
2224
2225 p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
2226 if (!p2p)
2227 p2p = wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE);
2228 if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
2229 os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
2230 0)
2231 return 0; /* Do not show P2P listen discovery results here */
2232
2233 pos = buf;
2234 end = buf + buflen;
2235
2236 ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
2237 MAC2STR(bss->bssid), bss->freq, bss->level);
2238 if (ret < 0 || ret >= end - pos)
2239 return -1;
2240 pos += ret;
2241 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
2242 if (ie)
2243 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
2244 ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
2245 if (ie2)
2246 pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
2247 pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
2248 if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
2249 ret = os_snprintf(pos, end - pos, "[WEP]");
2250 if (ret < 0 || ret >= end - pos)
2251 return -1;
2252 pos += ret;
2253 }
2254 if (bss_is_dmg(bss)) {
2255 const char *s;
2256 ret = os_snprintf(pos, end - pos, "[DMG]");
2257 if (ret < 0 || ret >= end - pos)
2258 return -1;
2259 pos += ret;
2260 switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
2261 case IEEE80211_CAP_DMG_IBSS:
2262 s = "[IBSS]";
2263 break;
2264 case IEEE80211_CAP_DMG_AP:
2265 s = "[ESS]";
2266 break;
2267 case IEEE80211_CAP_DMG_PBSS:
2268 s = "[PBSS]";
2269 break;
2270 default:
2271 s = "";
2272 break;
2273 }
2274 ret = os_snprintf(pos, end - pos, "%s", s);
2275 if (ret < 0 || ret >= end - pos)
2276 return -1;
2277 pos += ret;
2278 } else {
2279 if (bss->caps & IEEE80211_CAP_IBSS) {
2280 ret = os_snprintf(pos, end - pos, "[IBSS]");
2281 if (ret < 0 || ret >= end - pos)
2282 return -1;
2283 pos += ret;
2284 }
2285 if (bss->caps & IEEE80211_CAP_ESS) {
2286 ret = os_snprintf(pos, end - pos, "[ESS]");
2287 if (ret < 0 || ret >= end - pos)
2288 return -1;
2289 pos += ret;
2290 }
2291 }
2292 if (p2p) {
2293 ret = os_snprintf(pos, end - pos, "[P2P]");
2294 if (ret < 0 || ret >= end - pos)
2295 return -1;
2296 pos += ret;
2297 }
2298 #ifdef CONFIG_HS20
2299 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE) && ie2) {
2300 ret = os_snprintf(pos, end - pos, "[HS20]");
2301 if (ret < 0 || ret >= end - pos)
2302 return -1;
2303 pos += ret;
2304 }
2305 #endif /* CONFIG_HS20 */
2306
2307 ret = os_snprintf(pos, end - pos, "\t%s",
2308 wpa_ssid_txt(bss->ssid, bss->ssid_len));
2309 if (ret < 0 || ret >= end - pos)
2310 return -1;
2311 pos += ret;
2312
2313 ret = os_snprintf(pos, end - pos, "\n");
2314 if (ret < 0 || ret >= end - pos)
2315 return -1;
2316 pos += ret;
2317
2318 return pos - buf;
2319 }
2320
2321
2322 static int wpa_supplicant_ctrl_iface_scan_results(
2323 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
2324 {
2325 char *pos, *end;
2326 struct wpa_bss *bss;
2327 int ret;
2328
2329 pos = buf;
2330 end = buf + buflen;
2331 ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
2332 "flags / ssid\n");
2333 if (ret < 0 || ret >= end - pos)
2334 return pos - buf;
2335 pos += ret;
2336
2337 dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
2338 ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
2339 end - pos);
2340 if (ret < 0 || ret >= end - pos)
2341 return pos - buf;
2342 pos += ret;
2343 }
2344
2345 return pos - buf;
2346 }
2347
2348
2349 static int wpa_supplicant_ctrl_iface_select_network(
2350 struct wpa_supplicant *wpa_s, char *cmd)
2351 {
2352 int id;
2353 struct wpa_ssid *ssid;
2354 char *pos;
2355
2356 /* cmd: "<network id>" or "any" */
2357 if (os_strncmp(cmd, "any", 3) == 0) {
2358 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
2359 ssid = NULL;
2360 } else {
2361 id = atoi(cmd);
2362 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
2363
2364 ssid = wpa_config_get_network(wpa_s->conf, id);
2365 if (ssid == NULL) {
2366 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2367 "network id=%d", id);
2368 return -1;
2369 }
2370 if (ssid->disabled == 2) {
2371 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2372 "SELECT_NETWORK with persistent P2P group");
2373 return -1;
2374 }
2375 }
2376
2377 pos = os_strstr(cmd, " freq=");
2378 if (pos) {
2379 int *freqs = freq_range_to_channel_list(wpa_s, pos + 6);
2380 if (freqs) {
2381 wpa_s->scan_req = MANUAL_SCAN_REQ;
2382 os_free(wpa_s->manual_scan_freqs);
2383 wpa_s->manual_scan_freqs = freqs;
2384 }
2385 }
2386
2387 wpa_supplicant_select_network(wpa_s, ssid);
2388
2389 return 0;
2390 }
2391
2392
2393 static int wpa_supplicant_ctrl_iface_enable_network(
2394 struct wpa_supplicant *wpa_s, char *cmd)
2395 {
2396 int id;
2397 struct wpa_ssid *ssid;
2398
2399 /* cmd: "<network id>" or "all" */
2400 if (os_strcmp(cmd, "all") == 0) {
2401 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
2402 ssid = NULL;
2403 } else {
2404 id = atoi(cmd);
2405 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
2406
2407 ssid = wpa_config_get_network(wpa_s->conf, id);
2408 if (ssid == NULL) {
2409 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2410 "network id=%d", id);
2411 return -1;
2412 }
2413 if (ssid->disabled == 2) {
2414 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2415 "ENABLE_NETWORK with persistent P2P group");
2416 return -1;
2417 }
2418
2419 if (os_strstr(cmd, " no-connect")) {
2420 ssid->disabled = 0;
2421 return 0;
2422 }
2423 }
2424 wpa_supplicant_enable_network(wpa_s, ssid);
2425
2426 return 0;
2427 }
2428
2429
2430 static int wpa_supplicant_ctrl_iface_disable_network(
2431 struct wpa_supplicant *wpa_s, char *cmd)
2432 {
2433 int id;
2434 struct wpa_ssid *ssid;
2435
2436 /* cmd: "<network id>" or "all" */
2437 if (os_strcmp(cmd, "all") == 0) {
2438 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
2439 ssid = NULL;
2440 } else {
2441 id = atoi(cmd);
2442 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
2443
2444 ssid = wpa_config_get_network(wpa_s->conf, id);
2445 if (ssid == NULL) {
2446 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2447 "network id=%d", id);
2448 return -1;
2449 }
2450 if (ssid->disabled == 2) {
2451 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2452 "DISABLE_NETWORK with persistent P2P "
2453 "group");
2454 return -1;
2455 }
2456 }
2457 wpa_supplicant_disable_network(wpa_s, ssid);
2458
2459 return 0;
2460 }
2461
2462
2463 static int wpa_supplicant_ctrl_iface_add_network(
2464 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
2465 {
2466 struct wpa_ssid *ssid;
2467 int ret;
2468
2469 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
2470
2471 ssid = wpa_config_add_network(wpa_s->conf);
2472 if (ssid == NULL)
2473 return -1;
2474
2475 wpas_notify_network_added(wpa_s, ssid);
2476
2477 ssid->disabled = 1;
2478 wpa_config_set_network_defaults(ssid);
2479
2480 ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
2481 if (ret < 0 || (size_t) ret >= buflen)
2482 return -1;
2483 return ret;
2484 }
2485
2486
2487 static int wpa_supplicant_ctrl_iface_remove_network(
2488 struct wpa_supplicant *wpa_s, char *cmd)
2489 {
2490 int id;
2491 struct wpa_ssid *ssid;
2492 int was_disabled;
2493
2494 /* cmd: "<network id>" or "all" */
2495 if (os_strcmp(cmd, "all") == 0) {
2496 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
2497 if (wpa_s->sched_scanning)
2498 wpa_supplicant_cancel_sched_scan(wpa_s);
2499
2500 eapol_sm_invalidate_cached_session(wpa_s->eapol);
2501 if (wpa_s->current_ssid) {
2502 #ifdef CONFIG_SME
2503 wpa_s->sme.prev_bssid_set = 0;
2504 #endif /* CONFIG_SME */
2505 wpa_sm_set_config(wpa_s->wpa, NULL);
2506 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
2507 wpa_supplicant_deauthenticate(
2508 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2509 }
2510 ssid = wpa_s->conf->ssid;
2511 while (ssid) {
2512 struct wpa_ssid *remove_ssid = ssid;
2513 id = ssid->id;
2514 ssid = ssid->next;
2515 if (wpa_s->last_ssid == remove_ssid)
2516 wpa_s->last_ssid = NULL;
2517 wpas_notify_network_removed(wpa_s, remove_ssid);
2518 wpa_config_remove_network(wpa_s->conf, id);
2519 }
2520 return 0;
2521 }
2522
2523 id = atoi(cmd);
2524 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
2525
2526 ssid = wpa_config_get_network(wpa_s->conf, id);
2527 if (ssid)
2528 wpas_notify_network_removed(wpa_s, ssid);
2529 if (ssid == NULL) {
2530 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2531 "id=%d", id);
2532 return -1;
2533 }
2534
2535 if (wpa_s->last_ssid == ssid)
2536 wpa_s->last_ssid = NULL;
2537
2538 if (ssid == wpa_s->current_ssid || wpa_s->current_ssid == NULL) {
2539 #ifdef CONFIG_SME
2540 wpa_s->sme.prev_bssid_set = 0;
2541 #endif /* CONFIG_SME */
2542 /*
2543 * Invalidate the EAP session cache if the current or
2544 * previously used network is removed.
2545 */
2546 eapol_sm_invalidate_cached_session(wpa_s->eapol);
2547 }
2548
2549 if (ssid == wpa_s->current_ssid) {
2550 wpa_sm_set_config(wpa_s->wpa, NULL);
2551 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
2552
2553 wpa_supplicant_deauthenticate(wpa_s,
2554 WLAN_REASON_DEAUTH_LEAVING);
2555 }
2556
2557 was_disabled = ssid->disabled;
2558
2559 if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
2560 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Not able to remove the "
2561 "network id=%d", id);
2562 return -1;
2563 }
2564
2565 if (!was_disabled && wpa_s->sched_scanning) {
2566 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to remove "
2567 "network from filters");
2568 wpa_supplicant_cancel_sched_scan(wpa_s);
2569 wpa_supplicant_req_scan(wpa_s, 0, 0);
2570 }
2571
2572 return 0;
2573 }
2574
2575
2576 static int wpa_supplicant_ctrl_iface_update_network(
2577 struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
2578 char *name, char *value)
2579 {
2580 if (wpa_config_set(ssid, name, value, 0) < 0) {
2581 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
2582 "variable '%s'", name);
2583 return -1;
2584 }
2585
2586 if (os_strcmp(name, "bssid") != 0 &&
2587 os_strcmp(name, "priority") != 0)
2588 wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
2589
2590 if (wpa_s->current_ssid == ssid || wpa_s->current_ssid == NULL) {
2591 /*
2592 * Invalidate the EAP session cache if anything in the current
2593 * or previously used configuration changes.
2594 */
2595 eapol_sm_invalidate_cached_session(wpa_s->eapol);
2596 }
2597
2598 if ((os_strcmp(name, "psk") == 0 &&
2599 value[0] == '"' && ssid->ssid_len) ||
2600 (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
2601 wpa_config_update_psk(ssid);
2602 else if (os_strcmp(name, "priority") == 0)
2603 wpa_config_update_prio_list(wpa_s->conf);
2604
2605 return 0;
2606 }
2607
2608
2609 static int wpa_supplicant_ctrl_iface_set_network(
2610 struct wpa_supplicant *wpa_s, char *cmd)
2611 {
2612 int id, ret, prev_bssid_set;
2613 struct wpa_ssid *ssid;
2614 char *name, *value;
2615 u8 prev_bssid[ETH_ALEN];
2616
2617 /* cmd: "<network id> <variable name> <value>" */
2618 name = os_strchr(cmd, ' ');
2619 if (name == NULL)
2620 return -1;
2621 *name++ = '\0';
2622
2623 value = os_strchr(name, ' ');
2624 if (value == NULL)
2625 return -1;
2626 *value++ = '\0';
2627
2628 id = atoi(cmd);
2629 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
2630 id, name);
2631 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
2632 (u8 *) value, os_strlen(value));
2633
2634 ssid = wpa_config_get_network(wpa_s->conf, id);
2635 if (ssid == NULL) {
2636 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2637 "id=%d", id);
2638 return -1;
2639 }
2640
2641 prev_bssid_set = ssid->bssid_set;
2642 os_memcpy(prev_bssid, ssid->bssid, ETH_ALEN);
2643 ret = wpa_supplicant_ctrl_iface_update_network(wpa_s, ssid, name,
2644 value);
2645 if (ret == 0 &&
2646 (ssid->bssid_set != prev_bssid_set ||
2647 os_memcmp(ssid->bssid, prev_bssid, ETH_ALEN) != 0))
2648 wpas_notify_network_bssid_set_changed(wpa_s, ssid);
2649 return ret;
2650 }
2651
2652
2653 static int wpa_supplicant_ctrl_iface_get_network(
2654 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
2655 {
2656 int id;
2657 size_t res;
2658 struct wpa_ssid *ssid;
2659 char *name, *value;
2660
2661 /* cmd: "<network id> <variable name>" */
2662 name = os_strchr(cmd, ' ');
2663 if (name == NULL || buflen == 0)
2664 return -1;
2665 *name++ = '\0';
2666
2667 id = atoi(cmd);
2668 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
2669 id, name);
2670
2671 ssid = wpa_config_get_network(wpa_s->conf, id);
2672 if (ssid == NULL) {
2673 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2674 "id=%d", id);
2675 return -1;
2676 }
2677
2678 value = wpa_config_get_no_key(ssid, name);
2679 if (value == NULL) {
2680 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
2681 "variable '%s'", name);
2682 return -1;
2683 }
2684
2685 res = os_strlcpy(buf, value, buflen);
2686 if (res >= buflen) {
2687 os_free(value);
2688 return -1;
2689 }
2690
2691 os_free(value);
2692
2693 return res;
2694 }
2695
2696
2697 static int wpa_supplicant_ctrl_iface_dup_network(
2698 struct wpa_supplicant *wpa_s, char *cmd)
2699 {
2700 struct wpa_ssid *ssid_s, *ssid_d;
2701 char *name, *id, *value;
2702 int id_s, id_d, ret;
2703
2704 /* cmd: "<src network id> <dst network id> <variable name>" */
2705 id = os_strchr(cmd, ' ');
2706 if (id == NULL)
2707 return -1;
2708 *id++ = '\0';
2709
2710 name = os_strchr(id, ' ');
2711 if (name == NULL)
2712 return -1;
2713 *name++ = '\0';
2714
2715 id_s = atoi(cmd);
2716 id_d = atoi(id);
2717 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DUP_NETWORK id=%d -> %d name='%s'",
2718 id_s, id_d, name);
2719
2720 ssid_s = wpa_config_get_network(wpa_s->conf, id_s);
2721 if (ssid_s == NULL) {
2722 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2723 "network id=%d", id_s);
2724 return -1;
2725 }
2726
2727 ssid_d = wpa_config_get_network(wpa_s->conf, id_d);
2728 if (ssid_d == NULL) {
2729 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2730 "network id=%d", id_s);
2731 return -1;
2732 }
2733
2734 value = wpa_config_get(ssid_s, name);
2735 if (value == NULL) {
2736 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
2737 "variable '%s'", name);
2738 return -1;
2739 }
2740
2741 ret = wpa_supplicant_ctrl_iface_update_network(wpa_s, ssid_d, name,
2742 value);
2743
2744 os_free(value);
2745
2746 return ret;
2747 }
2748
2749
2750 static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
2751 char *buf, size_t buflen)
2752 {
2753 char *pos, *end;
2754 struct wpa_cred *cred;
2755 int ret;
2756
2757 pos = buf;
2758 end = buf + buflen;
2759 ret = os_snprintf(pos, end - pos,
2760 "cred id / realm / username / domain / imsi\n");
2761 if (ret < 0 || ret >= end - pos)
2762 return pos - buf;
2763 pos += ret;
2764
2765 cred = wpa_s->conf->cred;
2766 while (cred) {
2767 ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
2768 cred->id, cred->realm ? cred->realm : "",
2769 cred->username ? cred->username : "",
2770 cred->domain ? cred->domain[0] : "",
2771 cred->imsi ? cred->imsi : "");
2772 if (ret < 0 || ret >= end - pos)
2773 return pos - buf;
2774 pos += ret;
2775
2776 cred = cred->next;
2777 }
2778
2779 return pos - buf;
2780 }
2781
2782
2783 static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
2784 char *buf, size_t buflen)
2785 {
2786 struct wpa_cred *cred;
2787 int ret;
2788
2789 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
2790
2791 cred = wpa_config_add_cred(wpa_s->conf);
2792 if (cred == NULL)
2793 return -1;
2794
2795 wpa_msg(wpa_s, MSG_INFO, CRED_ADDED "%d", cred->id);
2796
2797 ret = os_snprintf(buf, buflen, "%d\n", cred->id);
2798 if (ret < 0 || (size_t) ret >= buflen)
2799 return -1;
2800 return ret;
2801 }
2802
2803
2804 static int wpas_ctrl_remove_cred(struct wpa_supplicant *wpa_s,
2805 struct wpa_cred *cred)
2806 {
2807 struct wpa_ssid *ssid;
2808 char str[20];
2809 int id;
2810
2811 if (cred == NULL) {
2812 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
2813 return -1;
2814 }
2815
2816 id = cred->id;
2817 if (wpa_config_remove_cred(wpa_s->conf, id) < 0) {
2818 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
2819 return -1;
2820 }
2821
2822 wpa_msg(wpa_s, MSG_INFO, CRED_REMOVED "%d", id);
2823
2824 /* Remove any network entry created based on the removed credential */
2825 ssid = wpa_s->conf->ssid;
2826 while (ssid) {
2827 if (ssid->parent_cred == cred) {
2828 wpa_printf(MSG_DEBUG, "Remove network id %d since it "
2829 "used the removed credential", ssid->id);
2830 os_snprintf(str, sizeof(str), "%d", ssid->id);
2831 ssid = ssid->next;
2832 wpa_supplicant_ctrl_iface_remove_network(wpa_s, str);
2833 } else
2834 ssid = ssid->next;
2835 }
2836
2837 return 0;
2838 }
2839
2840
2841 static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
2842 char *cmd)
2843 {
2844 int id;
2845 struct wpa_cred *cred, *prev;
2846
2847 /* cmd: "<cred id>", "all", "sp_fqdn=<FQDN>", or
2848 * "provisioning_sp=<FQDN> */
2849 if (os_strcmp(cmd, "all") == 0) {
2850 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
2851 cred = wpa_s->conf->cred;
2852 while (cred) {
2853 prev = cred;
2854 cred = cred->next;
2855 wpas_ctrl_remove_cred(wpa_s, prev);
2856 }
2857 return 0;
2858 }
2859
2860 if (os_strncmp(cmd, "sp_fqdn=", 8) == 0) {
2861 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED SP FQDN '%s'",
2862 cmd + 8);
2863 cred = wpa_s->conf->cred;
2864 while (cred) {
2865 prev = cred;
2866 cred = cred->next;
2867 if (prev->domain) {
2868 size_t i;
2869 for (i = 0; i < prev->num_domain; i++) {
2870 if (os_strcmp(prev->domain[i], cmd + 8)
2871 != 0)
2872 continue;
2873 wpas_ctrl_remove_cred(wpa_s, prev);
2874 break;
2875 }
2876 }
2877 }
2878 return 0;
2879 }
2880
2881 if (os_strncmp(cmd, "provisioning_sp=", 16) == 0) {
2882 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED provisioning SP FQDN '%s'",
2883 cmd + 16);
2884 cred = wpa_s->conf->cred;
2885 while (cred) {
2886 prev = cred;
2887 cred = cred->next;
2888 if (prev->provisioning_sp &&
2889 os_strcmp(prev->provisioning_sp, cmd + 16) == 0)
2890 wpas_ctrl_remove_cred(wpa_s, prev);
2891 }
2892 return 0;
2893 }
2894
2895 id = atoi(cmd);
2896 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
2897
2898 cred = wpa_config_get_cred(wpa_s->conf, id);
2899 return wpas_ctrl_remove_cred(wpa_s, cred);
2900 }
2901
2902
2903 static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
2904 char *cmd)
2905 {
2906 int id;
2907 struct wpa_cred *cred;
2908 char *name, *value;
2909
2910 /* cmd: "<cred id> <variable name> <value>" */
2911 name = os_strchr(cmd, ' ');
2912 if (name == NULL)
2913 return -1;
2914 *name++ = '\0';
2915
2916 value = os_strchr(name, ' ');
2917 if (value == NULL)
2918 return -1;
2919 *value++ = '\0';
2920
2921 id = atoi(cmd);
2922 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
2923 id, name);
2924 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
2925 (u8 *) value, os_strlen(value));
2926
2927 cred = wpa_config_get_cred(wpa_s->conf, id);
2928 if (cred == NULL) {
2929 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
2930 id);
2931 return -1;
2932 }
2933
2934 if (wpa_config_set_cred(cred, name, value, 0) < 0) {
2935 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
2936 "variable '%s'", name);
2937 return -1;
2938 }
2939
2940 wpa_msg(wpa_s, MSG_INFO, CRED_MODIFIED "%d %s", cred->id, name);
2941
2942 return 0;
2943 }
2944
2945
2946 static int wpa_supplicant_ctrl_iface_get_cred(struct wpa_supplicant *wpa_s,
2947 char *cmd, char *buf,
2948 size_t buflen)
2949 {
2950 int id;
2951 size_t res;
2952 struct wpa_cred *cred;
2953 char *name, *value;
2954
2955 /* cmd: "<cred id> <variable name>" */
2956 name = os_strchr(cmd, ' ');
2957 if (name == NULL)
2958 return -1;
2959 *name++ = '\0';
2960
2961 id = atoi(cmd);
2962 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CRED id=%d name='%s'",
2963 id, name);
2964
2965 cred = wpa_config_get_cred(wpa_s->conf, id);
2966 if (cred == NULL) {
2967 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
2968 id);
2969 return -1;
2970 }
2971
2972 value = wpa_config_get_cred_no_key(cred, name);
2973 if (value == NULL) {
2974 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get cred variable '%s'",
2975 name);
2976 return -1;
2977 }
2978
2979 res = os_strlcpy(buf, value, buflen);
2980 if (res >= buflen) {
2981 os_free(value);
2982 return -1;
2983 }
2984
2985 os_free(value);
2986
2987 return res;
2988 }
2989
2990
2991 #ifndef CONFIG_NO_CONFIG_WRITE
2992 static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
2993 {
2994 int ret;
2995
2996 if (!wpa_s->conf->update_config) {
2997 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
2998 "to update configuration (update_config=0)");
2999 return -1;
3000 }
3001
3002 ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
3003 if (ret) {
3004 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
3005 "update configuration");
3006 } else {
3007 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
3008 " updated");
3009 }
3010
3011 return ret;
3012 }
3013 #endif /* CONFIG_NO_CONFIG_WRITE */
3014
3015
3016 struct cipher_info {
3017 unsigned int capa;
3018 const char *name;
3019 int group_only;
3020 };
3021
3022 static const struct cipher_info ciphers[] = {
3023 { WPA_DRIVER_CAPA_ENC_CCMP_256, "CCMP-256", 0 },
3024 { WPA_DRIVER_CAPA_ENC_GCMP_256, "GCMP-256", 0 },
3025 { WPA_DRIVER_CAPA_ENC_CCMP, "CCMP", 0 },
3026 { WPA_DRIVER_CAPA_ENC_GCMP, "GCMP", 0 },
3027 { WPA_DRIVER_CAPA_ENC_TKIP, "TKIP", 0 },
3028 { WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE, "NONE", 0 },
3029 { WPA_DRIVER_CAPA_ENC_WEP104, "WEP104", 1 },
3030 { WPA_DRIVER_CAPA_ENC_WEP40, "WEP40", 1 }
3031 };
3032
3033
3034 static int ctrl_iface_get_capability_pairwise(int res, char *strict,
3035 struct wpa_driver_capa *capa,
3036 char *buf, size_t buflen)
3037 {
3038 int ret;
3039 char *pos, *end;
3040 size_t len;
3041 unsigned int i;
3042
3043 pos = buf;
3044 end = pos + buflen;
3045
3046 if (res < 0) {
3047 if (strict)
3048 return 0;
3049 len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
3050 if (len >= buflen)
3051 return -1;
3052 return len;
3053 }
3054
3055 for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
3056 if (!ciphers[i].group_only && capa->enc & ciphers[i].capa) {
3057 ret = os_snprintf(pos, end - pos, "%s%s",
3058 pos == buf ? "" : " ",
3059 ciphers[i].name);
3060 if (ret < 0 || ret >= end - pos)
3061 return pos - buf;
3062 pos += ret;
3063 }
3064 }
3065
3066 return pos - buf;
3067 }
3068
3069
3070 static int ctrl_iface_get_capability_group(int res, char *strict,
3071 struct wpa_driver_capa *capa,
3072 char *buf, size_t buflen)
3073 {
3074 int ret;
3075 char *pos, *end;
3076 size_t len;
3077 unsigned int i;
3078
3079 pos = buf;
3080 end = pos + buflen;
3081
3082 if (res < 0) {
3083 if (strict)
3084 return 0;
3085 len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
3086 if (len >= buflen)
3087 return -1;
3088 return len;
3089 }
3090
3091 for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
3092 if (capa->enc & ciphers[i].capa) {
3093 ret = os_snprintf(pos, end - pos, "%s%s",
3094 pos == buf ? "" : " ",
3095 ciphers[i].name);
3096 if (ret < 0 || ret >= end - pos)
3097 return pos - buf;
3098 pos += ret;
3099 }
3100 }
3101
3102 return pos - buf;
3103 }
3104
3105
3106 static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
3107 struct wpa_driver_capa *capa,
3108 char *buf, size_t buflen)
3109 {
3110 int ret;
3111 char *pos, *end;
3112 size_t len;
3113
3114 pos = buf;
3115 end = pos + buflen;
3116
3117 if (res < 0) {
3118 if (strict)
3119 return 0;
3120 len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
3121 "NONE", buflen);
3122 if (len >= buflen)
3123 return -1;
3124 return len;
3125 }
3126
3127 ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
3128 if (ret < 0 || ret >= end - pos)
3129 return pos - buf;
3130 pos += ret;
3131
3132 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
3133 WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
3134 ret = os_snprintf(pos, end - pos, " WPA-EAP");
3135 if (ret < 0 || ret >= end - pos)
3136 return pos - buf;
3137 pos += ret;
3138 }
3139
3140 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
3141 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
3142 ret = os_snprintf(pos, end - pos, " WPA-PSK");
3143 if (ret < 0 || ret >= end - pos)
3144 return pos - buf;
3145 pos += ret;
3146 }
3147
3148 if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
3149 ret = os_snprintf(pos, end - pos, " WPA-NONE");
3150 if (ret < 0 || ret >= end - pos)
3151 return pos - buf;
3152 pos += ret;
3153 }
3154
3155 return pos - buf;
3156 }
3157
3158
3159 static int ctrl_iface_get_capability_proto(int res, char *strict,
3160 struct wpa_driver_capa *capa,
3161 char *buf, size_t buflen)
3162 {
3163 int ret;
3164 char *pos, *end;
3165 size_t len;
3166
3167 pos = buf;
3168 end = pos + buflen;
3169
3170 if (res < 0) {
3171 if (strict)
3172 return 0;
3173 len = os_strlcpy(buf, "RSN WPA", buflen);
3174 if (len >= buflen)
3175 return -1;
3176 return len;
3177 }
3178
3179 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
3180 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
3181 ret = os_snprintf(pos, end - pos, "%sRSN",
3182 pos == buf ? "" : " ");
3183 if (ret < 0 || ret >= end - pos)
3184 return pos - buf;
3185 pos += ret;
3186 }
3187
3188 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
3189 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
3190 ret = os_snprintf(pos, end - pos, "%sWPA",
3191 pos == buf ? "" : " ");
3192 if (ret < 0 || ret >= end - pos)
3193 return pos - buf;
3194 pos += ret;
3195 }
3196
3197 return pos - buf;
3198 }
3199
3200
3201 static int ctrl_iface_get_capability_auth_alg(int res, char *strict,
3202 struct wpa_driver_capa *capa,
3203 char *buf, size_t buflen)
3204 {
3205 int ret;
3206 char *pos, *end;
3207 size_t len;
3208
3209 pos = buf;
3210 end = pos + buflen;
3211
3212 if (res < 0) {
3213 if (strict)
3214 return 0;
3215 len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
3216 if (len >= buflen)
3217 return -1;
3218 return len;
3219 }
3220
3221 if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
3222 ret = os_snprintf(pos, end - pos, "%sOPEN",
3223 pos == buf ? "" : " ");
3224 if (ret < 0 || ret >= end - pos)
3225 return pos - buf;
3226 pos += ret;
3227 }
3228
3229 if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
3230 ret = os_snprintf(pos, end - pos, "%sSHARED",
3231 pos == buf ? "" : " ");
3232 if (ret < 0 || ret >= end - pos)
3233 return pos - buf;
3234 pos += ret;
3235 }
3236
3237 if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
3238 ret = os_snprintf(pos, end - pos, "%sLEAP",
3239 pos == buf ? "" : " ");
3240 if (ret < 0 || ret >= end - pos)
3241 return pos - buf;
3242 pos += ret;
3243 }
3244
3245 return pos - buf;
3246 }
3247
3248
3249 static int ctrl_iface_get_capability_modes(int res, char *strict,
3250 struct wpa_driver_capa *capa,
3251 char *buf, size_t buflen)
3252 {
3253 int ret;
3254 char *pos, *end;
3255 size_t len;
3256
3257 pos = buf;
3258 end = pos + buflen;
3259
3260 if (res < 0) {
3261 if (strict)
3262 return 0;
3263 len = os_strlcpy(buf, "IBSS AP", buflen);
3264 if (len >= buflen)
3265 return -1;
3266 return len;
3267 }
3268
3269 if (capa->flags & WPA_DRIVER_FLAGS_IBSS) {
3270 ret = os_snprintf(pos, end - pos, "%sIBSS",
3271 pos == buf ? "" : " ");
3272 if (ret < 0 || ret >= end - pos)
3273 return pos - buf;
3274 pos += ret;
3275 }
3276
3277 if (capa->flags & WPA_DRIVER_FLAGS_AP) {
3278 ret = os_snprintf(pos, end - pos, "%sAP",
3279 pos == buf ? "" : " ");
3280 if (ret < 0 || ret >= end - pos)
3281 return pos - buf;
3282 pos += ret;
3283 }
3284
3285 return pos - buf;
3286 }
3287
3288
3289 static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
3290 char *buf, size_t buflen)
3291 {
3292 struct hostapd_channel_data *chnl;
3293 int ret, i, j;
3294 char *pos, *end, *hmode;
3295
3296 pos = buf;
3297 end = pos + buflen;
3298
3299 for (j = 0; j < wpa_s->hw.num_modes; j++) {
3300 switch (wpa_s->hw.modes[j].mode) {
3301 case HOSTAPD_MODE_IEEE80211B:
3302 hmode = "B";
3303 break;
3304 case HOSTAPD_MODE_IEEE80211G:
3305 hmode = "G";
3306 break;
3307 case HOSTAPD_MODE_IEEE80211A:
3308 hmode = "A";
3309 break;
3310 case HOSTAPD_MODE_IEEE80211AD:
3311 hmode = "AD";
3312 break;
3313 default:
3314 continue;
3315 }
3316 ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
3317 if (ret < 0 || ret >= end - pos)
3318 return pos - buf;
3319 pos += ret;
3320 chnl = wpa_s->hw.modes[j].channels;
3321 for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
3322 if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
3323 continue;
3324 ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
3325 if (ret < 0 || ret >= end - pos)
3326 return pos - buf;
3327 pos += ret;
3328 }
3329 ret = os_snprintf(pos, end - pos, "\n");
3330 if (ret < 0 || ret >= end - pos)
3331 return pos - buf;
3332 pos += ret;
3333 }
3334
3335 return pos - buf;
3336 }
3337
3338
3339 static int ctrl_iface_get_capability_freq(struct wpa_supplicant *wpa_s,
3340 char *buf, size_t buflen)
3341 {
3342 struct hostapd_channel_data *chnl;
3343 int ret, i, j;
3344 char *pos, *end, *hmode;
3345
3346 pos = buf;
3347 end = pos + buflen;
3348
3349 for (j = 0; j < wpa_s->hw.num_modes; j++) {
3350 switch (wpa_s->hw.modes[j].mode) {
3351 case HOSTAPD_MODE_IEEE80211B:
3352 hmode = "B";
3353 break;
3354 case HOSTAPD_MODE_IEEE80211G:
3355 hmode = "G";
3356 break;
3357 case HOSTAPD_MODE_IEEE80211A:
3358 hmode = "A";
3359 break;
3360 case HOSTAPD_MODE_IEEE80211AD:
3361 hmode = "AD";
3362 break;
3363 default:
3364 continue;
3365 }
3366 ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:\n",
3367 hmode);
3368 if (ret < 0 || ret >= end - pos)
3369 return pos - buf;
3370 pos += ret;
3371 chnl = wpa_s->hw.modes[j].channels;
3372 for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
3373 if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
3374 continue;
3375 ret = os_snprintf(pos, end - pos, " %d = %d MHz%s%s\n",
3376 chnl[i].chan, chnl[i].freq,
3377 chnl[i].flag & HOSTAPD_CHAN_NO_IBSS ?
3378 " (NO_IBSS)" : "",
3379 chnl[i].flag & HOSTAPD_CHAN_RADAR ?
3380 " (DFS)" : "");
3381
3382 if (ret < 0 || ret >= end - pos)
3383 return pos - buf;
3384 pos += ret;
3385 }
3386 ret = os_snprintf(pos, end - pos, "\n");
3387 if (ret < 0 || ret >= end - pos)
3388 return pos - buf;
3389 pos += ret;
3390 }
3391
3392 return pos - buf;
3393 }
3394
3395
3396 static int wpa_supplicant_ctrl_iface_get_capability(
3397 struct wpa_supplicant *wpa_s, const char *_field, char *buf,
3398 size_t buflen)
3399 {
3400 struct wpa_driver_capa capa;
3401 int res;
3402 char *strict;
3403 char field[30];
3404 size_t len;
3405
3406 /* Determine whether or not strict checking was requested */
3407 len = os_strlcpy(field, _field, sizeof(field));
3408 if (len >= sizeof(field))
3409 return -1;
3410 strict = os_strchr(field, ' ');
3411 if (strict != NULL) {
3412 *strict++ = '\0';
3413 if (os_strcmp(strict, "strict") != 0)
3414 return -1;
3415 }
3416
3417 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
3418 field, strict ? strict : "");
3419
3420 if (os_strcmp(field, "eap") == 0) {
3421 return eap_get_names(buf, buflen);
3422 }
3423
3424 res = wpa_drv_get_capa(wpa_s, &capa);
3425
3426 if (os_strcmp(field, "pairwise") == 0)
3427 return ctrl_iface_get_capability_pairwise(res, strict, &capa,
3428 buf, buflen);
3429
3430 if (os_strcmp(field, "group") == 0)
3431 return ctrl_iface_get_capability_group(res, strict, &capa,
3432 buf, buflen);
3433
3434 if (os_strcmp(field, "key_mgmt") == 0)
3435 return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
3436 buf, buflen);
3437
3438 if (os_strcmp(field, "proto") == 0)
3439 return ctrl_iface_get_capability_proto(res, strict, &capa,
3440 buf, buflen);
3441
3442 if (os_strcmp(field, "auth_alg") == 0)
3443 return ctrl_iface_get_capability_auth_alg(res, strict, &capa,
3444 buf, buflen);
3445
3446 if (os_strcmp(field, "modes") == 0)
3447 return ctrl_iface_get_capability_modes(res, strict, &capa,
3448 buf, buflen);
3449
3450 if (os_strcmp(field, "channels") == 0)
3451 return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
3452
3453 if (os_strcmp(field, "freq") == 0)
3454 return ctrl_iface_get_capability_freq(wpa_s, buf, buflen);
3455
3456 #ifdef CONFIG_TDLS
3457 if (os_strcmp(field, "tdls") == 0)
3458 return ctrl_iface_get_capability_tdls(wpa_s, buf, buflen);
3459 #endif /* CONFIG_TDLS */
3460
3461 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
3462 field);
3463
3464 return -1;
3465 }
3466
3467
3468 #ifdef CONFIG_INTERWORKING
3469 static char * anqp_add_hex(char *pos, char *end, const char *title,
3470 struct wpabuf *data)
3471 {
3472 char *start = pos;
3473 size_t i;
3474 int ret;
3475 const u8 *d;
3476
3477 if (data == NULL)
3478 return start;
3479
3480 ret = os_snprintf(pos, end - pos, "%s=", title);
3481 if (ret < 0 || ret >= end - pos)
3482 return start;
3483 pos += ret;
3484
3485 d = wpabuf_head_u8(data);
3486 for (i = 0; i < wpabuf_len(data); i++) {
3487 ret = os_snprintf(pos, end - pos, "%02x", *d++);
3488 if (ret < 0 || ret >= end - pos)
3489 return start;
3490 pos += ret;
3491 }
3492
3493 ret = os_snprintf(pos, end - pos, "\n");
3494 if (ret < 0 || ret >= end - pos)
3495 return start;
3496 pos += ret;
3497
3498 return pos;
3499 }
3500 #endif /* CONFIG_INTERWORKING */
3501
3502
3503 static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
3504 unsigned long mask, char *buf, size_t buflen)
3505 {
3506 size_t i;
3507 int ret;
3508 char *pos, *end;
3509 const u8 *ie, *ie2;
3510
3511 pos = buf;
3512 end = buf + buflen;
3513
3514 if (mask & WPA_BSS_MASK_ID) {
3515 ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
3516 if (ret < 0 || ret >= end - pos)
3517 return 0;
3518 pos += ret;
3519 }
3520
3521 if (mask & WPA_BSS_MASK_BSSID) {
3522 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
3523 MAC2STR(bss->bssid));
3524 if (ret < 0 || ret >= end - pos)
3525 return 0;
3526 pos += ret;
3527 }
3528
3529 if (mask & WPA_BSS_MASK_FREQ) {
3530 ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
3531 if (ret < 0 || ret >= end - pos)
3532 return 0;
3533 pos += ret;
3534 }
3535
3536 if (mask & WPA_BSS_MASK_BEACON_INT) {
3537 ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
3538 bss->beacon_int);
3539 if (ret < 0 || ret >= end - pos)
3540 return 0;
3541 pos += ret;
3542 }
3543
3544 if (mask & WPA_BSS_MASK_CAPABILITIES) {
3545 ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
3546 bss->caps);
3547 if (ret < 0 || ret >= end - pos)
3548 return 0;
3549 pos += ret;
3550 }
3551
3552 if (mask & WPA_BSS_MASK_QUAL) {
3553 ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
3554 if (ret < 0 || ret >= end - pos)
3555 return 0;
3556 pos += ret;
3557 }
3558
3559 if (mask & WPA_BSS_MASK_NOISE) {
3560 ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
3561 if (ret < 0 || ret >= end - pos)
3562 return 0;
3563 pos += ret;
3564 }
3565
3566 if (mask & WPA_BSS_MASK_LEVEL) {
3567 ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
3568 if (ret < 0 || ret >= end - pos)
3569 return 0;
3570 pos += ret;
3571 }
3572
3573 if (mask & WPA_BSS_MASK_TSF) {
3574 ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
3575 (unsigned long long) bss->tsf);
3576 if (ret < 0 || ret >= end - pos)
3577 return 0;
3578 pos += ret;
3579 }
3580
3581 if (mask & WPA_BSS_MASK_AGE) {
3582 struct os_reltime now;
3583
3584 os_get_reltime(&now);
3585 ret = os_snprintf(pos, end - pos, "age=%d\n",
3586 (int) (now.sec - bss->last_update.sec));
3587 if (ret < 0 || ret >= end - pos)
3588 return 0;
3589 pos += ret;
3590 }
3591
3592 if (mask & WPA_BSS_MASK_IE) {
3593 ret = os_snprintf(pos, end - pos, "ie=");
3594 if (ret < 0 || ret >= end - pos)
3595 return 0;
3596 pos += ret;
3597
3598 ie = (const u8 *) (bss + 1);
3599 for (i = 0; i < bss->ie_len; i++) {
3600 ret = os_snprintf(pos, end - pos, "%02x", *ie++);
3601 if (ret < 0 || ret >= end - pos)
3602 return 0;
3603 pos += ret;
3604 }
3605
3606 ret = os_snprintf(pos, end - pos, "\n");
3607 if (ret < 0 || ret >= end - pos)
3608 return 0;
3609 pos += ret;
3610 }
3611
3612 if (mask & WPA_BSS_MASK_FLAGS) {
3613 ret = os_snprintf(pos, end - pos, "flags=");
3614 if (ret < 0 || ret >= end - pos)
3615 return 0;
3616 pos += ret;
3617
3618 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
3619 if (ie)
3620 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
3621 2 + ie[1]);
3622 ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
3623 if (ie2)
3624 pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2,
3625 2 + ie2[1]);
3626 pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
3627 if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
3628 ret = os_snprintf(pos, end - pos, "[WEP]");
3629 if (ret < 0 || ret >= end - pos)
3630 return 0;
3631 pos += ret;
3632 }
3633 if (bss_is_dmg(bss)) {
3634 const char *s;
3635 ret = os_snprintf(pos, end - pos, "[DMG]");
3636 if (ret < 0 || ret >= end - pos)
3637 return 0;
3638 pos += ret;
3639 switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
3640 case IEEE80211_CAP_DMG_IBSS:
3641 s = "[IBSS]";
3642 break;
3643 case IEEE80211_CAP_DMG_AP:
3644 s = "[ESS]";
3645 break;
3646 case IEEE80211_CAP_DMG_PBSS:
3647 s = "[PBSS]";
3648 break;
3649 default:
3650 s = "";
3651 break;
3652 }
3653 ret = os_snprintf(pos, end - pos, "%s", s);
3654 if (ret < 0 || ret >= end - pos)
3655 return 0;
3656 pos += ret;
3657 } else {
3658 if (bss->caps & IEEE80211_CAP_IBSS) {
3659 ret = os_snprintf(pos, end - pos, "[IBSS]");
3660 if (ret < 0 || ret >= end - pos)
3661 return 0;
3662 pos += ret;
3663 }
3664 if (bss->caps & IEEE80211_CAP_ESS) {
3665 ret = os_snprintf(pos, end - pos, "[ESS]");
3666 if (ret < 0 || ret >= end - pos)
3667 return 0;
3668 pos += ret;
3669 }
3670 }
3671 if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
3672 wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
3673 ret = os_snprintf(pos, end - pos, "[P2P]");
3674 if (ret < 0 || ret >= end - pos)
3675 return 0;
3676 pos += ret;
3677 }
3678 #ifdef CONFIG_HS20
3679 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
3680 ret = os_snprintf(pos, end - pos, "[HS20]");
3681 if (ret < 0 || ret >= end - pos)
3682 return 0;
3683 pos += ret;
3684 }
3685 #endif /* CONFIG_HS20 */
3686
3687 ret = os_snprintf(pos, end - pos, "\n");
3688 if (ret < 0 || ret >= end - pos)
3689 return 0;
3690 pos += ret;
3691 }
3692
3693 if (mask & WPA_BSS_MASK_SSID) {
3694 ret = os_snprintf(pos, end - pos, "ssid=%s\n",
3695 wpa_ssid_txt(bss->ssid, bss->ssid_len));
3696 if (ret < 0 || ret >= end - pos)
3697 return 0;
3698 pos += ret;
3699 }
3700
3701 #ifdef CONFIG_WPS
3702 if (mask & WPA_BSS_MASK_WPS_SCAN) {
3703 ie = (const u8 *) (bss + 1);
3704 ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
3705 if (ret < 0 || ret >= end - pos)
3706 return 0;
3707 pos += ret;
3708 }
3709 #endif /* CONFIG_WPS */
3710
3711 #ifdef CONFIG_P2P
3712 if (mask & WPA_BSS_MASK_P2P_SCAN) {
3713 ie = (const u8 *) (bss + 1);
3714 ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
3715 if (ret < 0 || ret >= end - pos)
3716 return 0;
3717 pos += ret;
3718 }
3719 #endif /* CONFIG_P2P */
3720
3721 #ifdef CONFIG_WIFI_DISPLAY
3722 if (mask & WPA_BSS_MASK_WIFI_DISPLAY) {
3723 struct wpabuf *wfd;
3724 ie = (const u8 *) (bss + 1);
3725 wfd = ieee802_11_vendor_ie_concat(ie, bss->ie_len,
3726 WFD_IE_VENDOR_TYPE);
3727 if (wfd) {
3728 ret = os_snprintf(pos, end - pos, "wfd_subelems=");
3729 if (ret < 0 || ret >= end - pos) {
3730 wpabuf_free(wfd);
3731 return 0;
3732 }
3733 pos += ret;
3734
3735 pos += wpa_snprintf_hex(pos, end - pos,
3736 wpabuf_head(wfd),
3737 wpabuf_len(wfd));
3738 wpabuf_free(wfd);
3739
3740 ret = os_snprintf(pos, end - pos, "\n");
3741 if (ret < 0 || ret >= end - pos)
3742 return 0;
3743 pos += ret;
3744 }
3745 }
3746 #endif /* CONFIG_WIFI_DISPLAY */
3747
3748 #ifdef CONFIG_INTERWORKING
3749 if ((mask & WPA_BSS_MASK_INTERNETW) && bss->anqp) {
3750 struct wpa_bss_anqp *anqp = bss->anqp;
3751 pos = anqp_add_hex(pos, end, "anqp_venue_name",
3752 anqp->venue_name);
3753 pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
3754 anqp->network_auth_type);
3755 pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
3756 anqp->roaming_consortium);
3757 pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
3758 anqp->ip_addr_type_availability);
3759 pos = anqp_add_hex(pos, end, "anqp_nai_realm",
3760 anqp->nai_realm);
3761 pos = anqp_add_hex(pos, end, "anqp_3gpp", anqp->anqp_3gpp);
3762 pos = anqp_add_hex(pos, end, "anqp_domain_name",
3763 anqp->domain_name);
3764 #ifdef CONFIG_HS20
3765 pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
3766 anqp->hs20_operator_friendly_name);
3767 pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
3768 anqp->hs20_wan_metrics);
3769 pos = anqp_add_hex(pos, end, "hs20_connection_capability",
3770 anqp->hs20_connection_capability);
3771 pos = anqp_add_hex(pos, end, "hs20_operating_class",
3772 anqp->hs20_operating_class);
3773 pos = anqp_add_hex(pos, end, "hs20_osu_providers_list",
3774 anqp->hs20_osu_providers_list);
3775 #endif /* CONFIG_HS20 */
3776 }
3777 #endif /* CONFIG_INTERWORKING */
3778
3779 if (mask & WPA_BSS_MASK_DELIM) {
3780 ret = os_snprintf(pos, end - pos, "====\n");
3781 if (ret < 0 || ret >= end - pos)
3782 return 0;
3783 pos += ret;
3784 }
3785
3786 return pos - buf;
3787 }
3788
3789
3790 static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
3791 const char *cmd, char *buf,
3792 size_t buflen)
3793 {
3794 u8 bssid[ETH_ALEN];
3795 size_t i;
3796 struct wpa_bss *bss;
3797 struct wpa_bss *bsslast = NULL;
3798 struct dl_list *next;
3799 int ret = 0;
3800 int len;
3801 char *ctmp;
3802 unsigned long mask = WPA_BSS_MASK_ALL;
3803
3804 if (os_strncmp(cmd, "RANGE=", 6) == 0) {
3805 if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
3806 bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
3807 list_id);
3808 bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
3809 list_id);
3810 } else { /* N1-N2 */
3811 unsigned int id1, id2;
3812
3813 if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
3814 wpa_printf(MSG_INFO, "Wrong BSS range "
3815 "format");
3816 return 0;
3817 }
3818
3819 if (*(cmd + 6) == '-')
3820 id1 = 0;
3821 else
3822 id1 = atoi(cmd + 6);
3823 ctmp++;
3824 if (*ctmp >= '0' && *ctmp <= '9')
3825 id2 = atoi(ctmp);
3826 else
3827 id2 = (unsigned int) -1;
3828 bss = wpa_bss_get_id_range(wpa_s, id1, id2);
3829 if (id2 == (unsigned int) -1)
3830 bsslast = dl_list_last(&wpa_s->bss_id,
3831 struct wpa_bss,
3832 list_id);
3833 else {
3834 bsslast = wpa_bss_get_id(wpa_s, id2);
3835 if (bsslast == NULL && bss && id2 > id1) {
3836 struct wpa_bss *tmp = bss;
3837 for (;;) {
3838 next = tmp->list_id.next;
3839 if (next == &wpa_s->bss_id)
3840 break;
3841 tmp = dl_list_entry(
3842 next, struct wpa_bss,
3843 list_id);
3844 if (tmp->id > id2)
3845 break;
3846 bsslast = tmp;
3847 }
3848 }
3849 }
3850 }
3851 } else if (os_strncmp(cmd, "FIRST", 5) == 0)
3852 bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
3853 else if (os_strncmp(cmd, "LAST", 4) == 0)
3854 bss = dl_list_last(&wpa_s->bss_id, struct wpa_bss, list_id);
3855 else if (os_strncmp(cmd, "ID-", 3) == 0) {
3856 i = atoi(cmd + 3);
3857 bss = wpa_bss_get_id(wpa_s, i);
3858 } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
3859 i = atoi(cmd + 5);
3860 bss = wpa_bss_get_id(wpa_s, i);
3861 if (bss) {
3862 next = bss->list_id.next;
3863 if (next == &wpa_s->bss_id)
3864 bss = NULL;
3865 else
3866 bss = dl_list_entry(next, struct wpa_bss,
3867 list_id);
3868 }
3869 #ifdef CONFIG_P2P
3870 } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
3871 if (hwaddr_aton(cmd + 13, bssid) == 0)
3872 bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
3873 else
3874 bss = NULL;
3875 #endif /* CONFIG_P2P */
3876 } else if (hwaddr_aton(cmd, bssid) == 0)
3877 bss = wpa_bss_get_bssid(wpa_s, bssid);
3878 else {
3879 struct wpa_bss *tmp;
3880 i = atoi(cmd);
3881 bss = NULL;
3882 dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
3883 {
3884 if (i-- == 0) {
3885 bss = tmp;
3886 break;
3887 }
3888 }
3889 }
3890
3891 if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
3892 mask = strtoul(ctmp + 5, NULL, 0x10);
3893 if (mask == 0)
3894 mask = WPA_BSS_MASK_ALL;
3895 }
3896
3897 if (bss == NULL)
3898 return 0;
3899
3900 if (bsslast == NULL)
3901 bsslast = bss;
3902 do {
3903 len = print_bss_info(wpa_s, bss, mask, buf, buflen);
3904 ret += len;
3905 buf += len;
3906 buflen -= len;
3907 if (bss == bsslast) {
3908 if ((mask & WPA_BSS_MASK_DELIM) && len &&
3909 (bss == dl_list_last(&wpa_s->bss_id,
3910 struct wpa_bss, list_id)))
3911 os_snprintf(buf - 5, 5, "####\n");
3912 break;
3913 }
3914 next = bss->list_id.next;
3915 if (next == &wpa_s->bss_id)
3916 break;
3917 bss = dl_list_entry(next, struct wpa_bss, list_id);
3918 } while (bss && len);
3919
3920 return ret;
3921 }
3922
3923
3924 static int wpa_supplicant_ctrl_iface_ap_scan(
3925 struct wpa_supplicant *wpa_s, char *cmd)
3926 {
3927 int ap_scan = atoi(cmd);
3928 return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
3929 }
3930
3931
3932 static int wpa_supplicant_ctrl_iface_scan_interval(
3933 struct wpa_supplicant *wpa_s, char *cmd)
3934 {
3935 int scan_int = atoi(cmd);
3936 return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
3937 }
3938
3939
3940 static int wpa_supplicant_ctrl_iface_bss_expire_age(
3941 struct wpa_supplicant *wpa_s, char *cmd)
3942 {
3943 int expire_age = atoi(cmd);
3944 return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
3945 }
3946
3947
3948 static int wpa_supplicant_ctrl_iface_bss_expire_count(
3949 struct wpa_supplicant *wpa_s, char *cmd)
3950 {
3951 int expire_count = atoi(cmd);
3952 return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
3953 }
3954
3955
3956 static int wpa_supplicant_ctrl_iface_bss_flush(
3957 struct wpa_supplicant *wpa_s, char *cmd)
3958 {
3959 int flush_age = atoi(cmd);
3960
3961 if (flush_age == 0)
3962 wpa_bss_flush(wpa_s);
3963 else
3964 wpa_bss_flush_by_age(wpa_s, flush_age);
3965 return 0;
3966 }
3967
3968
3969 #ifdef CONFIG_TESTING_OPTIONS
3970 static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
3971 {
3972 wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
3973 /* MLME-DELETEKEYS.request */
3974 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
3975 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
3976 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
3977 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
3978 #ifdef CONFIG_IEEE80211W
3979 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
3980 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
3981 #endif /* CONFIG_IEEE80211W */
3982
3983 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
3984 0);
3985 /* MLME-SETPROTECTION.request(None) */
3986 wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
3987 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
3988 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
3989 wpa_sm_drop_sa(wpa_s->wpa);
3990 }
3991 #endif /* CONFIG_TESTING_OPTIONS */
3992
3993
3994 static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
3995 char *addr)
3996 {
3997 #ifdef CONFIG_NO_SCAN_PROCESSING
3998 return -1;
3999 #else /* CONFIG_NO_SCAN_PROCESSING */
4000 u8 bssid[ETH_ALEN];
4001 struct wpa_bss *bss;
4002 struct wpa_ssid *ssid = wpa_s->current_ssid;
4003
4004 if (hwaddr_aton(addr, bssid)) {
4005 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
4006 "address '%s'", addr);
4007 return -1;
4008 }
4009
4010 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
4011
4012 if (!ssid) {
4013 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
4014 "configuration known for the target AP");
4015 return -1;
4016 }
4017
4018 bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
4019 if (!bss) {
4020 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
4021 "from BSS table");
4022 return -1;
4023 }
4024
4025 /*
4026 * TODO: Find best network configuration block from configuration to
4027 * allow roaming to other networks
4028 */
4029
4030 wpa_s->reassociate = 1;
4031 wpa_supplicant_connect(wpa_s, bss, ssid);
4032
4033 return 0;
4034 #endif /* CONFIG_NO_SCAN_PROCESSING */
4035 }
4036
4037
4038 #ifdef CONFIG_P2P
4039 static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
4040 {
4041 unsigned int timeout = atoi(cmd);
4042 enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
4043 u8 dev_id[ETH_ALEN], *_dev_id = NULL;
4044 u8 dev_type[WPS_DEV_TYPE_LEN], *_dev_type = NULL;
4045 char *pos;
4046 unsigned int search_delay;
4047
4048 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
4049 wpa_dbg(wpa_s, MSG_INFO,
4050 "Reject P2P_FIND since interface is disabled");
4051 return -1;
4052 }
4053 if (os_strstr(cmd, "type=social"))
4054 type = P2P_FIND_ONLY_SOCIAL;
4055 else if (os_strstr(cmd, "type=progressive"))
4056 type = P2P_FIND_PROGRESSIVE;
4057
4058 pos = os_strstr(cmd, "dev_id=");
4059 if (pos) {
4060 pos += 7;
4061 if (hwaddr_aton(pos, dev_id))
4062 return -1;
4063 _dev_id = dev_id;
4064 }
4065
4066 pos = os_strstr(cmd, "dev_type=");
4067 if (pos) {
4068 pos += 9;
4069 if (wps_dev_type_str2bin(pos, dev_type) < 0)
4070 return -1;
4071 _dev_type = dev_type;
4072 }
4073
4074 pos = os_strstr(cmd, "delay=");
4075 if (pos) {
4076 pos += 6;
4077 search_delay = atoi(pos);
4078 } else
4079 search_delay = wpas_p2p_search_delay(wpa_s);
4080
4081 return wpas_p2p_find(wpa_s, timeout, type, _dev_type != NULL, _dev_type,
4082 _dev_id, search_delay);
4083 }
4084
4085
4086 static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
4087 char *buf, size_t buflen)
4088 {
4089 u8 addr[ETH_ALEN];
4090 char *pos, *pos2;
4091 char *pin = NULL;
4092 enum p2p_wps_method wps_method;
4093 int new_pin;
4094 int ret;
4095 int persistent_group, persistent_id = -1;
4096 int join;
4097 int auth;
4098 int automatic;
4099 int go_intent = -1;
4100 int freq = 0;
4101 int pd;
4102 int ht40, vht;
4103
4104 /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad]
4105 * [persistent|persistent=<network id>]
4106 * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
4107 * [ht40] [vht] */
4108
4109 if (hwaddr_aton(cmd, addr))
4110 return -1;
4111
4112 pos = cmd + 17;
4113 if (*pos != ' ')
4114 return -1;
4115 pos++;
4116
4117 persistent_group = os_strstr(pos, " persistent") != NULL;
4118 pos2 = os_strstr(pos, " persistent=");
4119 if (pos2) {
4120 struct wpa_ssid *ssid;
4121 persistent_id = atoi(pos2 + 12);
4122 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4123 if (ssid == NULL || ssid->disabled != 2 ||
4124 ssid->mode != WPAS_MODE_P2P_GO) {
4125 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
4126 "SSID id=%d for persistent P2P group (GO)",
4127 persistent_id);
4128 return -1;
4129 }
4130 }
4131 join = os_strstr(pos, " join") != NULL;
4132 auth = os_strstr(pos, " auth") != NULL;
4133 automatic = os_strstr(pos, " auto") != NULL;
4134 pd = os_strstr(pos, " provdisc") != NULL;
4135 vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
4136 ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
4137 vht;
4138
4139 pos2 = os_strstr(pos, " go_intent=");
4140 if (pos2) {
4141 pos2 += 11;
4142 go_intent = atoi(pos2);
4143 if (go_intent < 0 || go_intent > 15)
4144 return -1;
4145 }
4146
4147 pos2 = os_strstr(pos, " freq=");
4148 if (pos2) {
4149 pos2 += 6;
4150 freq = atoi(pos2);
4151 if (freq <= 0)
4152 return -1;
4153 }
4154
4155 if (os_strncmp(pos, "pin", 3) == 0) {
4156 /* Request random PIN (to be displayed) and enable the PIN */
4157 wps_method = WPS_PIN_DISPLAY;
4158 } else if (os_strncmp(pos, "pbc", 3) == 0) {
4159 wps_method = WPS_PBC;
4160 } else {
4161 pin = pos;
4162 pos = os_strchr(pin, ' ');
4163 wps_method = WPS_PIN_KEYPAD;
4164 if (pos) {
4165 *pos++ = '\0';
4166 if (os_strncmp(pos, "display", 7) == 0)
4167 wps_method = WPS_PIN_DISPLAY;
4168 }
4169 if (!wps_pin_str_valid(pin)) {
4170 os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
4171 return 17;
4172 }
4173 }
4174
4175 new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
4176 persistent_group, automatic, join,
4177 auth, go_intent, freq, persistent_id, pd,
4178 ht40, vht);
4179 if (new_pin == -2) {
4180 os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
4181 return 25;
4182 }
4183 if (new_pin == -3) {
4184 os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
4185 return 25;
4186 }
4187 if (new_pin < 0)
4188 return -1;
4189 if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
4190 ret = os_snprintf(buf, buflen, "%08d", new_pin);
4191 if (ret < 0 || (size_t) ret >= buflen)
4192 return -1;
4193 return ret;
4194 }
4195
4196 os_memcpy(buf, "OK\n", 3);
4197 return 3;
4198 }
4199
4200
4201 static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
4202 {
4203 unsigned int timeout = atoi(cmd);
4204 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
4205 wpa_dbg(wpa_s, MSG_INFO,
4206 "Reject P2P_LISTEN since interface is disabled");
4207 return -1;
4208 }
4209 return wpas_p2p_listen(wpa_s, timeout);
4210 }
4211
4212
4213 static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
4214 {
4215 u8 addr[ETH_ALEN];
4216 char *pos;
4217 enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
4218
4219 /* <addr> <config method> [join|auto] */
4220
4221 if (hwaddr_aton(cmd, addr))
4222 return -1;
4223
4224 pos = cmd + 17;
4225 if (*pos != ' ')
4226 return -1;
4227 pos++;
4228
4229 if (os_strstr(pos, " join") != NULL)
4230 use = WPAS_P2P_PD_FOR_JOIN;
4231 else if (os_strstr(pos, " auto") != NULL)
4232 use = WPAS_P2P_PD_AUTO;
4233
4234 return wpas_p2p_prov_disc(wpa_s, addr, pos, use);
4235 }
4236
4237
4238 static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
4239 size_t buflen)
4240 {
4241 struct wpa_ssid *ssid = wpa_s->current_ssid;
4242
4243 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
4244 ssid->passphrase == NULL)
4245 return -1;
4246
4247 os_strlcpy(buf, ssid->passphrase, buflen);
4248 return os_strlen(buf);
4249 }
4250
4251
4252 static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
4253 char *buf, size_t buflen)
4254 {
4255 u64 ref;
4256 int res;
4257 u8 dst_buf[ETH_ALEN], *dst;
4258 struct wpabuf *tlvs;
4259 char *pos;
4260 size_t len;
4261
4262 if (hwaddr_aton(cmd, dst_buf))
4263 return -1;
4264 dst = dst_buf;
4265 if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
4266 dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
4267 dst = NULL;
4268 pos = cmd + 17;
4269 if (*pos != ' ')
4270 return -1;
4271 pos++;
4272
4273 if (os_strncmp(pos, "upnp ", 5) == 0) {
4274 u8 version;
4275 pos += 5;
4276 if (hexstr2bin(pos, &version, 1) < 0)
4277 return -1;
4278 pos += 2;
4279 if (*pos != ' ')
4280 return -1;
4281 pos++;
4282 ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
4283 #ifdef CONFIG_WIFI_DISPLAY
4284 } else if (os_strncmp(pos, "wifi-display ", 13) == 0) {
4285 ref = wpas_p2p_sd_request_wifi_display(wpa_s, dst, pos + 13);
4286 #endif /* CONFIG_WIFI_DISPLAY */
4287 } else {
4288 len = os_strlen(pos);
4289 if (len & 1)
4290 return -1;
4291 len /= 2;
4292 tlvs = wpabuf_alloc(len);
4293 if (tlvs == NULL)
4294 return -1;
4295 if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
4296 wpabuf_free(tlvs);
4297 return -1;
4298 }
4299
4300 ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
4301 wpabuf_free(tlvs);
4302 }
4303 if (ref == 0)
4304 return -1;
4305 res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
4306 if (res < 0 || (unsigned) res >= buflen)
4307 return -1;
4308 return res;
4309 }
4310
4311
4312 static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
4313 char *cmd)
4314 {
4315 long long unsigned val;
4316 u64 req;
4317 if (sscanf(cmd, "%llx", &val) != 1)
4318 return -1;
4319 req = val;
4320 return wpas_p2p_sd_cancel_request(wpa_s, req);
4321 }
4322
4323
4324 static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
4325 {
4326 int freq;
4327 u8 dst[ETH_ALEN];
4328 u8 dialog_token;
4329 struct wpabuf *resp_tlvs;
4330 char *pos, *pos2;
4331 size_t len;
4332
4333 pos = os_strchr(cmd, ' ');
4334 if (pos == NULL)
4335 return -1;
4336 *pos++ = '\0';
4337 freq = atoi(cmd);
4338 if (freq == 0)
4339 return -1;
4340
4341 if (hwaddr_aton(pos, dst))
4342 return -1;
4343 pos += 17;
4344 if (*pos != ' ')
4345 return -1;
4346 pos++;
4347
4348 pos2 = os_strchr(pos, ' ');
4349 if (pos2 == NULL)
4350 return -1;
4351 *pos2++ = '\0';
4352 dialog_token = atoi(pos);
4353
4354 len = os_strlen(pos2);
4355 if (len & 1)
4356 return -1;
4357 len /= 2;
4358 resp_tlvs = wpabuf_alloc(len);
4359 if (resp_tlvs == NULL)
4360 return -1;
4361 if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
4362 wpabuf_free(resp_tlvs);
4363 return -1;
4364 }
4365
4366 wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
4367 wpabuf_free(resp_tlvs);
4368 return 0;
4369 }
4370
4371
4372 static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
4373 char *cmd)
4374 {
4375 if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
4376 return -1;
4377 wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
4378 return 0;
4379 }
4380
4381
4382 static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
4383 char *cmd)
4384 {
4385 char *pos;
4386 size_t len;
4387 struct wpabuf *query, *resp;
4388
4389 pos = os_strchr(cmd, ' ');
4390 if (pos == NULL)
4391 return -1;
4392 *pos++ = '\0';
4393
4394 len = os_strlen(cmd);
4395 if (len & 1)
4396 return -1;
4397 len /= 2;
4398 query = wpabuf_alloc(len);
4399 if (query == NULL)
4400 return -1;
4401 if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
4402 wpabuf_free(query);
4403 return -1;
4404 }
4405
4406 len = os_strlen(pos);
4407 if (len & 1) {
4408 wpabuf_free(query);
4409 return -1;
4410 }
4411 len /= 2;
4412 resp = wpabuf_alloc(len);
4413 if (resp == NULL) {
4414 wpabuf_free(query);
4415 return -1;
4416 }
4417 if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
4418 wpabuf_free(query);
4419 wpabuf_free(resp);
4420 return -1;
4421 }
4422
4423 if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
4424 wpabuf_free(query);
4425 wpabuf_free(resp);
4426 return -1;
4427 }
4428 return 0;
4429 }
4430
4431
4432 static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
4433 {
4434 char *pos;
4435 u8 version;
4436
4437 pos = os_strchr(cmd, ' ');
4438 if (pos == NULL)
4439 return -1;
4440 *pos++ = '\0';
4441
4442 if (hexstr2bin(cmd, &version, 1) < 0)
4443 return -1;
4444
4445 return wpas_p2p_service_add_upnp(wpa_s, version, pos);
4446 }
4447
4448
4449 static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
4450 {
4451 char *pos;
4452
4453 pos = os_strchr(cmd, ' ');
4454 if (pos == NULL)
4455 return -1;
4456 *pos++ = '\0';
4457
4458 if (os_strcmp(cmd, "bonjour") == 0)
4459 return p2p_ctrl_service_add_bonjour(wpa_s, pos);
4460 if (os_strcmp(cmd, "upnp") == 0)
4461 return p2p_ctrl_service_add_upnp(wpa_s, pos);
4462 wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
4463 return -1;
4464 }
4465
4466
4467 static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
4468 char *cmd)
4469 {
4470 size_t len;
4471 struct wpabuf *query;
4472 int ret;
4473
4474 len = os_strlen(cmd);
4475 if (len & 1)
4476 return -1;
4477 len /= 2;
4478 query = wpabuf_alloc(len);
4479 if (query == NULL)
4480 return -1;
4481 if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
4482 wpabuf_free(query);
4483 return -1;
4484 }
4485
4486 ret = wpas_p2p_service_del_bonjour(wpa_s, query);
4487 wpabuf_free(query);
4488 return ret;
4489 }
4490
4491
4492 static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
4493 {
4494 char *pos;
4495 u8 version;
4496
4497 pos = os_strchr(cmd, ' ');
4498 if (pos == NULL)
4499 return -1;
4500 *pos++ = '\0';
4501
4502 if (hexstr2bin(cmd, &version, 1) < 0)
4503 return -1;
4504
4505 return wpas_p2p_service_del_upnp(wpa_s, version, pos);
4506 }
4507
4508
4509 static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
4510 {
4511 char *pos;
4512
4513 pos = os_strchr(cmd, ' ');
4514 if (pos == NULL)
4515 return -1;
4516 *pos++ = '\0';
4517
4518 if (os_strcmp(cmd, "bonjour") == 0)
4519 return p2p_ctrl_service_del_bonjour(wpa_s, pos);
4520 if (os_strcmp(cmd, "upnp") == 0)
4521 return p2p_ctrl_service_del_upnp(wpa_s, pos);
4522 wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
4523 return -1;
4524 }
4525
4526
4527 static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
4528 {
4529 u8 addr[ETH_ALEN];
4530
4531 /* <addr> */
4532
4533 if (hwaddr_aton(cmd, addr))
4534 return -1;
4535
4536 return wpas_p2p_reject(wpa_s, addr);
4537 }
4538
4539
4540 static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
4541 {
4542 char *pos;
4543 int id;
4544 struct wpa_ssid *ssid;
4545 u8 *_peer = NULL, peer[ETH_ALEN];
4546 int freq = 0, pref_freq = 0;
4547 int ht40, vht;
4548
4549 id = atoi(cmd);
4550 pos = os_strstr(cmd, " peer=");
4551 if (pos) {
4552 pos += 6;
4553 if (hwaddr_aton(pos, peer))
4554 return -1;
4555 _peer = peer;
4556 }
4557 ssid = wpa_config_get_network(wpa_s->conf, id);
4558 if (ssid == NULL || ssid->disabled != 2) {
4559 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
4560 "for persistent P2P group",
4561 id);
4562 return -1;
4563 }
4564
4565 pos = os_strstr(cmd, " freq=");
4566 if (pos) {
4567 pos += 6;
4568 freq = atoi(pos);
4569 if (freq <= 0)
4570 return -1;
4571 }
4572
4573 pos = os_strstr(cmd, " pref=");
4574 if (pos) {
4575 pos += 6;
4576 pref_freq = atoi(pos);
4577 if (pref_freq <= 0)
4578 return -1;
4579 }
4580
4581 vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
4582 ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
4583 vht;
4584
4585 return wpas_p2p_invite(wpa_s, _peer, ssid, NULL, freq, ht40, vht,
4586 pref_freq);
4587 }
4588
4589
4590 static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
4591 {
4592 char *pos;
4593 u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
4594
4595 pos = os_strstr(cmd, " peer=");
4596 if (!pos)
4597 return -1;
4598
4599 *pos = '\0';
4600 pos += 6;
4601 if (hwaddr_aton(pos, peer)) {
4602 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
4603 return -1;
4604 }
4605
4606 pos = os_strstr(pos, " go_dev_addr=");
4607 if (pos) {
4608 pos += 13;
4609 if (hwaddr_aton(pos, go_dev_addr)) {
4610 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
4611 pos);
4612 return -1;
4613 }
4614 go_dev = go_dev_addr;
4615 }
4616
4617 return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
4618 }
4619
4620
4621 static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
4622 {
4623 if (os_strncmp(cmd, "persistent=", 11) == 0)
4624 return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
4625 if (os_strncmp(cmd, "group=", 6) == 0)
4626 return p2p_ctrl_invite_group(wpa_s, cmd + 6);
4627
4628 return -1;
4629 }
4630
4631
4632 static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
4633 char *cmd, int freq, int ht40,
4634 int vht)
4635 {
4636 int id;
4637 struct wpa_ssid *ssid;
4638
4639 id = atoi(cmd);
4640 ssid = wpa_config_get_network(wpa_s->conf, id);
4641 if (ssid == NULL || ssid->disabled != 2) {
4642 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
4643 "for persistent P2P group",
4644 id);
4645 return -1;
4646 }
4647
4648 return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq, 0, ht40, vht,
4649 NULL, 0);
4650 }
4651
4652
4653 static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
4654 {
4655 int freq = 0, ht40, vht;
4656 char *pos;
4657
4658 pos = os_strstr(cmd, "freq=");
4659 if (pos)
4660 freq = atoi(pos + 5);
4661
4662 vht = (os_strstr(cmd, "vht") != NULL) || wpa_s->conf->p2p_go_vht;
4663 ht40 = (os_strstr(cmd, "ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
4664 vht;
4665
4666 if (os_strncmp(cmd, "persistent=", 11) == 0)
4667 return p2p_ctrl_group_add_persistent(wpa_s, cmd + 11, freq,
4668 ht40, vht);
4669 if (os_strcmp(cmd, "persistent") == 0 ||
4670 os_strncmp(cmd, "persistent ", 11) == 0)
4671 return wpas_p2p_group_add(wpa_s, 1, freq, ht40, vht);
4672 if (os_strncmp(cmd, "freq=", 5) == 0)
4673 return wpas_p2p_group_add(wpa_s, 0, freq, ht40, vht);
4674 if (ht40)
4675 return wpas_p2p_group_add(wpa_s, 0, freq, ht40, vht);
4676
4677 wpa_printf(MSG_DEBUG, "CTRL: Invalid P2P_GROUP_ADD parameters '%s'",
4678 cmd);
4679 return -1;
4680 }
4681
4682
4683 static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
4684 char *buf, size_t buflen)
4685 {
4686 u8 addr[ETH_ALEN], *addr_ptr;
4687 int next, res;
4688 const struct p2p_peer_info *info;
4689 char *pos, *end;
4690 char devtype[WPS_DEV_TYPE_BUFSIZE];
4691 struct wpa_ssid *ssid;
4692 size_t i;
4693
4694 if (!wpa_s->global->p2p)
4695 return -1;
4696
4697 if (os_strcmp(cmd, "FIRST") == 0) {
4698 addr_ptr = NULL;
4699 next = 0;
4700 } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
4701 if (hwaddr_aton(cmd + 5, addr) < 0)
4702 return -1;
4703 addr_ptr = addr;
4704 next = 1;
4705 } else {
4706 if (hwaddr_aton(cmd, addr) < 0)
4707 return -1;
4708 addr_ptr = addr;
4709 next = 0;
4710 }
4711
4712 info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
4713 if (info == NULL)
4714 return -1;
4715
4716 pos = buf;
4717 end = buf + buflen;
4718
4719 res = os_snprintf(pos, end - pos, MACSTR "\n"
4720 "pri_dev_type=%s\n"
4721 "device_name=%s\n"
4722 "manufacturer=%s\n"
4723 "model_name=%s\n"
4724 "model_number=%s\n"
4725 "serial_number=%s\n"
4726 "config_methods=0x%x\n"
4727 "dev_capab=0x%x\n"
4728 "group_capab=0x%x\n"
4729 "level=%d\n",
4730 MAC2STR(info->p2p_device_addr),
4731 wps_dev_type_bin2str(info->pri_dev_type,
4732 devtype, sizeof(devtype)),
4733 info->device_name,
4734 info->manufacturer,
4735 info->model_name,
4736 info->model_number,
4737 info->serial_number,
4738 info->config_methods,
4739 info->dev_capab,
4740 info->group_capab,
4741 info->level);
4742 if (res < 0 || res >= end - pos)
4743 return pos - buf;
4744 pos += res;
4745
4746 for (i = 0; i < info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN; i++)
4747 {
4748 const u8 *t;
4749 t = &info->wps_sec_dev_type_list[i * WPS_DEV_TYPE_LEN];
4750 res = os_snprintf(pos, end - pos, "sec_dev_type=%s\n",
4751 wps_dev_type_bin2str(t, devtype,
4752 sizeof(devtype)));
4753 if (res < 0 || res >= end - pos)
4754 return pos - buf;
4755 pos += res;
4756 }
4757
4758 ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
4759 if (ssid) {
4760 res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
4761 if (res < 0 || res >= end - pos)
4762 return pos - buf;
4763 pos += res;
4764 }
4765
4766 res = p2p_get_peer_info_txt(info, pos, end - pos);
4767 if (res < 0)
4768 return pos - buf;
4769 pos += res;
4770
4771 if (info->vendor_elems) {
4772 res = os_snprintf(pos, end - pos, "vendor_elems=");
4773 if (res < 0 || res >= end - pos)
4774 return pos - buf;
4775 pos += res;
4776
4777 pos += wpa_snprintf_hex(pos, end - pos,
4778 wpabuf_head(info->vendor_elems),
4779 wpabuf_len(info->vendor_elems));
4780
4781 res = os_snprintf(pos, end - pos, "\n");
4782 if (res < 0 || res >= end - pos)
4783 return pos - buf;
4784 pos += res;
4785 }
4786
4787 return pos - buf;
4788 }
4789
4790
4791 static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
4792 const char *param)
4793 {
4794 unsigned int i;
4795
4796 if (wpa_s->global->p2p == NULL)
4797 return -1;
4798
4799 if (freq_range_list_parse(&wpa_s->global->p2p_disallow_freq, param) < 0)
4800 return -1;
4801
4802 for (i = 0; i < wpa_s->global->p2p_disallow_freq.num; i++) {
4803 struct wpa_freq_range *freq;
4804 freq = &wpa_s->global->p2p_disallow_freq.range[i];
4805 wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
4806 freq->min, freq->max);
4807 }
4808
4809 wpas_p2p_update_channel_list(wpa_s);
4810 return 0;
4811 }
4812
4813
4814 static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
4815 {
4816 char *param;
4817
4818 if (wpa_s->global->p2p == NULL)
4819 return -1;
4820
4821 param = os_strchr(cmd, ' ');
4822 if (param == NULL)
4823 return -1;
4824 *param++ = '\0';
4825
4826 if (os_strcmp(cmd, "discoverability") == 0) {
4827 p2p_set_client_discoverability(wpa_s->global->p2p,
4828 atoi(param));
4829 return 0;
4830 }
4831
4832 if (os_strcmp(cmd, "managed") == 0) {
4833 p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
4834 return 0;
4835 }
4836
4837 if (os_strcmp(cmd, "listen_channel") == 0) {
4838 return p2p_set_listen_channel(wpa_s->global->p2p, 81,
4839 atoi(param), 1);
4840 }
4841
4842 if (os_strcmp(cmd, "ssid_postfix") == 0) {
4843 return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
4844 os_strlen(param));
4845 }
4846
4847 if (os_strcmp(cmd, "noa") == 0) {
4848 char *pos;
4849 int count, start, duration;
4850 /* GO NoA parameters: count,start_offset(ms),duration(ms) */
4851 count = atoi(param);
4852 pos = os_strchr(param, ',');
4853 if (pos == NULL)
4854 return -1;
4855 pos++;
4856 start = atoi(pos);
4857 pos = os_strchr(pos, ',');
4858 if (pos == NULL)
4859 return -1;
4860 pos++;
4861 duration = atoi(pos);
4862 if (count < 0 || count > 255 || start < 0 || duration < 0)
4863 return -1;
4864 if (count == 0 && duration > 0)
4865 return -1;
4866 wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
4867 "start=%d duration=%d", count, start, duration);
4868 return wpas_p2p_set_noa(wpa_s, count, start, duration);
4869 }
4870
4871 if (os_strcmp(cmd, "ps") == 0)
4872 return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
4873
4874 if (os_strcmp(cmd, "oppps") == 0)
4875 return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
4876
4877 if (os_strcmp(cmd, "ctwindow") == 0)
4878 return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
4879
4880 if (os_strcmp(cmd, "disabled") == 0) {
4881 wpa_s->global->p2p_disabled = atoi(param);
4882 wpa_printf(MSG_DEBUG, "P2P functionality %s",
4883 wpa_s->global->p2p_disabled ?
4884 "disabled" : "enabled");
4885 if (wpa_s->global->p2p_disabled) {
4886 wpas_p2p_stop_find(wpa_s);
4887 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
4888 p2p_flush(wpa_s->global->p2p);
4889 }
4890 return 0;
4891 }
4892
4893 if (os_strcmp(cmd, "conc_pref") == 0) {
4894 if (os_strcmp(param, "sta") == 0)
4895 wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
4896 else if (os_strcmp(param, "p2p") == 0)
4897 wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
4898 else {
4899 wpa_printf(MSG_INFO, "Invalid conc_pref value");
4900 return -1;
4901 }
4902 wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
4903 "%s", param);
4904 return 0;
4905 }
4906
4907 if (os_strcmp(cmd, "force_long_sd") == 0) {
4908 wpa_s->force_long_sd = atoi(param);
4909 return 0;
4910 }
4911
4912 if (os_strcmp(cmd, "peer_filter") == 0) {
4913 u8 addr[ETH_ALEN];
4914 if (hwaddr_aton(param, addr))
4915 return -1;
4916 p2p_set_peer_filter(wpa_s->global->p2p, addr);
4917 return 0;
4918 }
4919
4920 if (os_strcmp(cmd, "cross_connect") == 0)
4921 return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
4922
4923 if (os_strcmp(cmd, "go_apsd") == 0) {
4924 if (os_strcmp(param, "disable") == 0)
4925 wpa_s->set_ap_uapsd = 0;
4926 else {
4927 wpa_s->set_ap_uapsd = 1;
4928 wpa_s->ap_uapsd = atoi(param);
4929 }
4930 return 0;
4931 }
4932
4933 if (os_strcmp(cmd, "client_apsd") == 0) {
4934 if (os_strcmp(param, "disable") == 0)
4935 wpa_s->set_sta_uapsd = 0;
4936 else {
4937 int be, bk, vi, vo;
4938 char *pos;
4939 /* format: BE,BK,VI,VO;max SP Length */
4940 be = atoi(param);
4941 pos = os_strchr(param, ',');
4942 if (pos == NULL)
4943 return -1;
4944 pos++;
4945 bk = atoi(pos);
4946 pos = os_strchr(pos, ',');
4947 if (pos == NULL)
4948 return -1;
4949 pos++;
4950 vi = atoi(pos);
4951 pos = os_strchr(pos, ',');
4952 if (pos == NULL)
4953 return -1;
4954 pos++;
4955 vo = atoi(pos);
4956 /* ignore max SP Length for now */
4957
4958 wpa_s->set_sta_uapsd = 1;
4959 wpa_s->sta_uapsd = 0;
4960 if (be)
4961 wpa_s->sta_uapsd |= BIT(0);
4962 if (bk)
4963 wpa_s->sta_uapsd |= BIT(1);
4964 if (vi)
4965 wpa_s->sta_uapsd |= BIT(2);
4966 if (vo)
4967 wpa_s->sta_uapsd |= BIT(3);
4968 }
4969 return 0;
4970 }
4971
4972 if (os_strcmp(cmd, "disallow_freq") == 0)
4973 return p2p_ctrl_disallow_freq(wpa_s, param);
4974
4975 if (os_strcmp(cmd, "disc_int") == 0) {
4976 int min_disc_int, max_disc_int, max_disc_tu;
4977 char *pos;
4978
4979 pos = param;
4980
4981 min_disc_int = atoi(pos);
4982 pos = os_strchr(pos, ' ');
4983 if (pos == NULL)
4984 return -1;
4985 *pos++ = '\0';
4986
4987 max_disc_int = atoi(pos);
4988 pos = os_strchr(pos, ' ');
4989 if (pos == NULL)
4990 return -1;
4991 *pos++ = '\0';
4992
4993 max_disc_tu = atoi(pos);
4994
4995 return p2p_set_disc_int(wpa_s->global->p2p, min_disc_int,
4996 max_disc_int, max_disc_tu);
4997 }
4998
4999 if (os_strcmp(cmd, "per_sta_psk") == 0) {
5000 wpa_s->global->p2p_per_sta_psk = !!atoi(param);
5001 return 0;
5002 }
5003
5004 #ifdef CONFIG_WPS_NFC
5005 if (os_strcmp(cmd, "nfc_tag") == 0)
5006 return wpas_p2p_nfc_tag_enabled(wpa_s, !!atoi(param));
5007 #endif /* CONFIG_WPS_NFC */
5008
5009 if (os_strcmp(cmd, "disable_ip_addr_req") == 0) {
5010 wpa_s->p2p_disable_ip_addr_req = !!atoi(param);
5011 return 0;
5012 }
5013
5014 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
5015 cmd);
5016
5017 return -1;
5018 }
5019
5020
5021 static void p2p_ctrl_flush(struct wpa_supplicant *wpa_s)
5022 {
5023 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
5024 wpa_s->force_long_sd = 0;
5025 if (wpa_s->global->p2p)
5026 p2p_flush(wpa_s->global->p2p);
5027 }
5028
5029
5030 static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
5031 {
5032 char *pos, *pos2;
5033 unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
5034
5035 if (cmd[0]) {
5036 pos = os_strchr(cmd, ' ');
5037 if (pos == NULL)
5038 return -1;
5039 *pos++ = '\0';
5040 dur1 = atoi(cmd);
5041
5042 pos2 = os_strchr(pos, ' ');
5043 if (pos2)
5044 *pos2++ = '\0';
5045 int1 = atoi(pos);
5046 } else
5047 pos2 = NULL;
5048
5049 if (pos2) {
5050 pos = os_strchr(pos2, ' ');
5051 if (pos == NULL)
5052 return -1;
5053 *pos++ = '\0';
5054 dur2 = atoi(pos2);
5055 int2 = atoi(pos);
5056 }
5057
5058 return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
5059 }
5060
5061
5062 static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
5063 {
5064 char *pos;
5065 unsigned int period = 0, interval = 0;
5066
5067 if (cmd[0]) {
5068 pos = os_strchr(cmd, ' ');
5069 if (pos == NULL)
5070 return -1;
5071 *pos++ = '\0';
5072 period = atoi(cmd);
5073 interval = atoi(pos);
5074 }
5075
5076 return wpas_p2p_ext_listen(wpa_s, period, interval);
5077 }
5078
5079
5080 static int p2p_ctrl_remove_client(struct wpa_supplicant *wpa_s, const char *cmd)
5081 {
5082 const char *pos;
5083 u8 peer[ETH_ALEN];
5084 int iface_addr = 0;
5085
5086 pos = cmd;
5087 if (os_strncmp(pos, "iface=", 6) == 0) {
5088 iface_addr = 1;
5089 pos += 6;
5090 }
5091 if (hwaddr_aton(pos, peer))
5092 return -1;
5093
5094 wpas_p2p_remove_client(wpa_s, peer, iface_addr);
5095 return 0;
5096 }
5097
5098 #endif /* CONFIG_P2P */
5099
5100
5101 static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s, char *val)
5102 {
5103 struct wpa_freq_range_list ranges;
5104 int *freqs = NULL;
5105 struct hostapd_hw_modes *mode;
5106 u16 i;
5107
5108 if (wpa_s->hw.modes == NULL)
5109 return NULL;
5110
5111 os_memset(&ranges, 0, sizeof(ranges));
5112 if (freq_range_list_parse(&ranges, val) < 0)
5113 return NULL;
5114
5115 for (i = 0; i < wpa_s->hw.num_modes; i++) {
5116 int j;
5117
5118 mode = &wpa_s->hw.modes[i];
5119 for (j = 0; j < mode->num_channels; j++) {
5120 unsigned int freq;
5121
5122 if (mode->channels[j].flag & HOSTAPD_CHAN_DISABLED)
5123 continue;
5124
5125 freq = mode->channels[j].freq;
5126 if (!freq_range_list_includes(&ranges, freq))
5127 continue;
5128
5129 int_array_add_unique(&freqs, freq);
5130 }
5131 }
5132
5133 os_free(ranges.range);
5134 return freqs;
5135 }
5136
5137
5138 #ifdef CONFIG_INTERWORKING
5139
5140 static int ctrl_interworking_select(struct wpa_supplicant *wpa_s, char *param)
5141 {
5142 int auto_sel = 0;
5143 int *freqs = NULL;
5144
5145 if (param) {
5146 char *pos;
5147
5148 auto_sel = os_strstr(param, "auto") != NULL;
5149
5150 pos = os_strstr(param, "freq=");
5151 if (pos) {
5152 freqs = freq_range_to_channel_list(wpa_s, pos + 5);
5153 if (freqs == NULL)
5154 return -1;
5155 }
5156
5157 }
5158
5159 return interworking_select(wpa_s, auto_sel, freqs);
5160 }
5161
5162
5163 static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst)
5164 {
5165 u8 bssid[ETH_ALEN];
5166 struct wpa_bss *bss;
5167
5168 if (hwaddr_aton(dst, bssid)) {
5169 wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
5170 return -1;
5171 }
5172
5173 bss = wpa_bss_get_bssid(wpa_s, bssid);
5174 if (bss == NULL) {
5175 wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
5176 MAC2STR(bssid));
5177 return -1;
5178 }
5179
5180 return interworking_connect(wpa_s, bss);
5181 }
5182
5183
5184 static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
5185 {
5186 u8 dst_addr[ETH_ALEN];
5187 int used;
5188 char *pos;
5189 #define MAX_ANQP_INFO_ID 100
5190 u16 id[MAX_ANQP_INFO_ID];
5191 size_t num_id = 0;
5192 u32 subtypes = 0;
5193
5194 used = hwaddr_aton2(dst, dst_addr);
5195 if (used < 0)
5196 return -1;
5197 pos = dst + used;
5198 while (num_id < MAX_ANQP_INFO_ID) {
5199 if (os_strncmp(pos, "hs20:", 5) == 0) {
5200 #ifdef CONFIG_HS20
5201 int num = atoi(pos + 5);
5202 if (num <= 0 || num > 31)
5203 return -1;
5204 subtypes |= BIT(num);
5205 #else /* CONFIG_HS20 */
5206 return -1;
5207 #endif /* CONFIG_HS20 */
5208 } else {
5209 id[num_id] = atoi(pos);
5210 if (id[num_id])
5211 num_id++;
5212 }
5213 pos = os_strchr(pos + 1, ',');
5214 if (pos == NULL)
5215 break;
5216 pos++;
5217 }
5218
5219 if (num_id == 0)
5220 return -1;
5221
5222 return anqp_send_req(wpa_s, dst_addr, id, num_id, subtypes);
5223 }
5224
5225
5226 static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
5227 {
5228 u8 dst_addr[ETH_ALEN];
5229 struct wpabuf *advproto, *query = NULL;
5230 int used, ret = -1;
5231 char *pos, *end;
5232 size_t len;
5233
5234 used = hwaddr_aton2(cmd, dst_addr);
5235 if (used < 0)
5236 return -1;
5237
5238 pos = cmd + used;
5239 while (*pos == ' ')
5240 pos++;
5241
5242 /* Advertisement Protocol ID */
5243 end = os_strchr(pos, ' ');
5244 if (end)
5245 len = end - pos;
5246 else
5247 len = os_strlen(pos);
5248 if (len & 0x01)
5249 return -1;
5250 len /= 2;
5251 if (len == 0)
5252 return -1;
5253 advproto = wpabuf_alloc(len);
5254 if (advproto == NULL)
5255 return -1;
5256 if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
5257 goto fail;
5258
5259 if (end) {
5260 /* Optional Query Request */
5261 pos = end + 1;
5262 while (*pos == ' ')
5263 pos++;
5264
5265 len = os_strlen(pos);
5266 if (len) {
5267 if (len & 0x01)
5268 goto fail;
5269 len /= 2;
5270 if (len == 0)
5271 goto fail;
5272 query = wpabuf_alloc(len);
5273 if (query == NULL)
5274 goto fail;
5275 if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
5276 goto fail;
5277 }
5278 }
5279
5280 ret = gas_send_request(wpa_s, dst_addr, advproto, query);
5281
5282 fail:
5283 wpabuf_free(advproto);
5284 wpabuf_free(query);
5285
5286 return ret;
5287 }
5288
5289
5290 static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
5291 size_t buflen)
5292 {
5293 u8 addr[ETH_ALEN];
5294 int dialog_token;
5295 int used;
5296 char *pos;
5297 size_t resp_len, start, requested_len;
5298 struct wpabuf *resp;
5299 int ret;
5300
5301 used = hwaddr_aton2(cmd, addr);
5302 if (used < 0)
5303 return -1;
5304
5305 pos = cmd + used;
5306 while (*pos == ' ')
5307 pos++;
5308 dialog_token = atoi(pos);
5309
5310 if (wpa_s->last_gas_resp &&
5311 os_memcmp(addr, wpa_s->last_gas_addr, ETH_ALEN) == 0 &&
5312 dialog_token == wpa_s->last_gas_dialog_token)
5313 resp = wpa_s->last_gas_resp;
5314 else if (wpa_s->prev_gas_resp &&
5315 os_memcmp(addr, wpa_s->prev_gas_addr, ETH_ALEN) == 0 &&
5316 dialog_token == wpa_s->prev_gas_dialog_token)
5317 resp = wpa_s->prev_gas_resp;
5318 else
5319 return -1;
5320
5321 resp_len = wpabuf_len(resp);
5322 start = 0;
5323 requested_len = resp_len;
5324
5325 pos = os_strchr(pos, ' ');
5326 if (pos) {
5327 start = atoi(pos);
5328 if (start > resp_len)
5329 return os_snprintf(buf, buflen, "FAIL-Invalid range");
5330 pos = os_strchr(pos, ',');
5331 if (pos == NULL)
5332 return -1;
5333 pos++;
5334 requested_len = atoi(pos);
5335 if (start + requested_len > resp_len)
5336 return os_snprintf(buf, buflen, "FAIL-Invalid range");
5337 }
5338
5339 if (requested_len * 2 + 1 > buflen)
5340 return os_snprintf(buf, buflen, "FAIL-Too long response");
5341
5342 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(resp) + start,
5343 requested_len);
5344
5345 if (start + requested_len == resp_len) {
5346 /*
5347 * Free memory by dropping the response after it has been
5348 * fetched.
5349 */
5350 if (resp == wpa_s->prev_gas_resp) {
5351 wpabuf_free(wpa_s->prev_gas_resp);
5352 wpa_s->prev_gas_resp = NULL;
5353 } else {
5354 wpabuf_free(wpa_s->last_gas_resp);
5355 wpa_s->last_gas_resp = NULL;
5356 }
5357 }
5358
5359 return ret;
5360 }
5361 #endif /* CONFIG_INTERWORKING */
5362
5363
5364 #ifdef CONFIG_HS20
5365
5366 static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
5367 {
5368 u8 dst_addr[ETH_ALEN];
5369 int used;
5370 char *pos;
5371 u32 subtypes = 0;
5372
5373 used = hwaddr_aton2(dst, dst_addr);
5374 if (used < 0)
5375 return -1;
5376 pos = dst + used;
5377 for (;;) {
5378 int num = atoi(pos);
5379 if (num <= 0 || num > 31)
5380 return -1;
5381 subtypes |= BIT(num);
5382 pos = os_strchr(pos + 1, ',');
5383 if (pos == NULL)
5384 break;
5385 pos++;
5386 }
5387
5388 if (subtypes == 0)
5389 return -1;
5390
5391 return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0);
5392 }
5393
5394
5395 static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
5396 const u8 *addr, const char *realm)
5397 {
5398 u8 *buf;
5399 size_t rlen, len;
5400 int ret;
5401
5402 rlen = os_strlen(realm);
5403 len = 3 + rlen;
5404 buf = os_malloc(len);
5405 if (buf == NULL)
5406 return -1;
5407 buf[0] = 1; /* NAI Home Realm Count */
5408 buf[1] = 0; /* Formatted in accordance with RFC 4282 */
5409 buf[2] = rlen;
5410 os_memcpy(buf + 3, realm, rlen);
5411
5412 ret = hs20_anqp_send_req(wpa_s, addr,
5413 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
5414 buf, len);
5415
5416 os_free(buf);
5417
5418 return ret;
5419 }
5420
5421
5422 static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
5423 char *dst)
5424 {
5425 struct wpa_cred *cred = wpa_s->conf->cred;
5426 u8 dst_addr[ETH_ALEN];
5427 int used;
5428 u8 *buf;
5429 size_t len;
5430 int ret;
5431
5432 used = hwaddr_aton2(dst, dst_addr);
5433 if (used < 0)
5434 return -1;
5435
5436 while (dst[used] == ' ')
5437 used++;
5438 if (os_strncmp(dst + used, "realm=", 6) == 0)
5439 return hs20_nai_home_realm_list(wpa_s, dst_addr,
5440 dst + used + 6);
5441
5442 len = os_strlen(dst + used);
5443
5444 if (len == 0 && cred && cred->realm)
5445 return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
5446
5447 if (len & 1)
5448 return -1;
5449 len /= 2;
5450 buf = os_malloc(len);
5451 if (buf == NULL)
5452 return -1;
5453 if (hexstr2bin(dst + used, buf, len) < 0) {
5454 os_free(buf);
5455 return -1;
5456 }
5457
5458 ret = hs20_anqp_send_req(wpa_s, dst_addr,
5459 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
5460 buf, len);
5461 os_free(buf);
5462
5463 return ret;
5464 }
5465
5466
5467 static int hs20_icon_request(struct wpa_supplicant *wpa_s, char *cmd)
5468 {
5469 u8 dst_addr[ETH_ALEN];
5470 int used;
5471 char *icon;
5472
5473 used = hwaddr_aton2(cmd, dst_addr);
5474 if (used < 0)
5475 return -1;
5476
5477 while (cmd[used] == ' ')
5478 used++;
5479 icon = &cmd[used];
5480
5481 wpa_s->fetch_osu_icon_in_progress = 0;
5482 return hs20_anqp_send_req(wpa_s, dst_addr, BIT(HS20_STYPE_ICON_REQUEST),
5483 (u8 *) icon, os_strlen(icon));
5484 }
5485
5486 #endif /* CONFIG_HS20 */
5487
5488
5489 static int wpa_supplicant_ctrl_iface_sta_autoconnect(
5490 struct wpa_supplicant *wpa_s, char *cmd)
5491 {
5492 wpa_s->auto_reconnect_disabled = atoi(cmd) == 0 ? 1 : 0;
5493 return 0;
5494 }
5495
5496
5497 #ifdef CONFIG_AUTOSCAN
5498
5499 static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
5500 char *cmd)
5501 {
5502 enum wpa_states state = wpa_s->wpa_state;
5503 char *new_params = NULL;
5504
5505 if (os_strlen(cmd) > 0) {
5506 new_params = os_strdup(cmd);
5507 if (new_params == NULL)
5508 return -1;
5509 }
5510
5511 os_free(wpa_s->conf->autoscan);
5512 wpa_s->conf->autoscan = new_params;
5513
5514 if (wpa_s->conf->autoscan == NULL)
5515 autoscan_deinit(wpa_s);
5516 else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
5517 autoscan_init(wpa_s, 1);
5518 else if (state == WPA_SCANNING)
5519 wpa_supplicant_reinit_autoscan(wpa_s);
5520
5521 return 0;
5522 }
5523
5524 #endif /* CONFIG_AUTOSCAN */
5525
5526
5527 #ifdef CONFIG_WNM
5528
5529 static int wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant *wpa_s, char *cmd)
5530 {
5531 int enter;
5532 int intval = 0;
5533 char *pos;
5534 int ret;
5535 struct wpabuf *tfs_req = NULL;
5536
5537 if (os_strncmp(cmd, "enter", 5) == 0)
5538 enter = 1;
5539 else if (os_strncmp(cmd, "exit", 4) == 0)
5540 enter = 0;
5541 else
5542 return -1;
5543
5544 pos = os_strstr(cmd, " interval=");
5545 if (pos)
5546 intval = atoi(pos + 10);
5547
5548 pos = os_strstr(cmd, " tfs_req=");
5549 if (pos) {
5550 char *end;
5551 size_t len;
5552 pos += 9;
5553 end = os_strchr(pos, ' ');
5554 if (end)
5555 len = end - pos;
5556 else
5557 len = os_strlen(pos);
5558 if (len & 1)
5559 return -1;
5560 len /= 2;
5561 tfs_req = wpabuf_alloc(len);
5562 if (tfs_req == NULL)
5563 return -1;
5564 if (hexstr2bin(pos, wpabuf_put(tfs_req, len), len) < 0) {
5565 wpabuf_free(tfs_req);
5566 return -1;
5567 }
5568 }
5569
5570 ret = ieee802_11_send_wnmsleep_req(wpa_s, enter ? WNM_SLEEP_MODE_ENTER :
5571 WNM_SLEEP_MODE_EXIT, intval,
5572 tfs_req);
5573 wpabuf_free(tfs_req);
5574
5575 return ret;
5576 }
5577
5578
5579 static int wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant *wpa_s, char *cmd)
5580 {
5581 int query_reason;
5582
5583 query_reason = atoi(cmd);
5584
5585 wpa_printf(MSG_DEBUG, "CTRL_IFACE: WNM_BSS_QUERY query_reason=%d",
5586 query_reason);
5587
5588 return wnm_send_bss_transition_mgmt_query(wpa_s, query_reason);
5589 }
5590
5591 #endif /* CONFIG_WNM */
5592
5593
5594 static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
5595 size_t buflen)
5596 {
5597 struct wpa_signal_info si;
5598 int ret;
5599 char *pos, *end;
5600
5601 ret = wpa_drv_signal_poll(wpa_s, &si);
5602 if (ret)
5603 return -1;
5604
5605 pos = buf;
5606 end = buf + buflen;
5607
5608 ret = os_snprintf(pos, end - pos, "RSSI=%d\nLINKSPEED=%d\n"
5609 "NOISE=%d\nFREQUENCY=%u\n",
5610 si.current_signal, si.current_txrate / 1000,
5611 si.current_noise, si.frequency);
5612 if (ret < 0 || ret > end - pos)
5613 return -1;
5614 pos += ret;
5615
5616 if (si.chanwidth != CHAN_WIDTH_UNKNOWN) {
5617 ret = os_snprintf(pos, end - pos, "WIDTH=%s\n",
5618 channel_width_to_string(si.chanwidth));
5619 if (ret < 0 || ret > end - pos)
5620 return -1;
5621 pos += ret;
5622 }
5623
5624 if (si.center_frq1 > 0 && si.center_frq2 > 0) {
5625 ret = os_snprintf(pos, end - pos,
5626 "CENTER_FRQ1=%d\nCENTER_FRQ2=%d\n",
5627 si.center_frq1, si.center_frq2);
5628 if (ret < 0 || ret > end - pos)
5629 return -1;
5630 pos += ret;
5631 }
5632
5633 if (si.avg_signal) {
5634 ret = os_snprintf(pos, end - pos,
5635 "AVG_RSSI=%d\n", si.avg_signal);
5636 if (ret < 0 || ret >= end - pos)
5637 return -1;
5638 pos += ret;
5639 }
5640
5641 return pos - buf;
5642 }
5643
5644
5645 static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
5646 size_t buflen)
5647 {
5648 struct hostap_sta_driver_data sta;
5649 int ret;
5650
5651 ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
5652 if (ret)
5653 return -1;
5654
5655 ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
5656 sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
5657 if (ret < 0 || (size_t) ret > buflen)
5658 return -1;
5659 return ret;
5660 }
5661
5662
5663 #ifdef ANDROID
5664 static int wpa_supplicant_driver_cmd(struct wpa_supplicant *wpa_s, char *cmd,
5665 char *buf, size_t buflen)
5666 {
5667 int ret;
5668
5669 ret = wpa_drv_driver_cmd(wpa_s, cmd, buf, buflen);
5670 if (ret == 0) {
5671 if (os_strncasecmp(cmd, "COUNTRY", 7) == 0) {
5672 struct p2p_data *p2p = wpa_s->global->p2p;
5673 if (p2p) {
5674 char country[3];
5675 country[0] = cmd[8];
5676 country[1] = cmd[9];
5677 country[2] = 0x04;
5678 p2p_set_country(p2p, country);
5679 }
5680 }
5681 ret = os_snprintf(buf, buflen, "%s\n", "OK");
5682 }
5683 return ret;
5684 }
5685 #endif /* ANDROID */
5686
5687
5688 static int wpa_supplicant_vendor_cmd(struct wpa_supplicant *wpa_s, char *cmd,
5689 char *buf, size_t buflen)
5690 {
5691 int ret;
5692 char *pos;
5693 u8 *data = NULL;
5694 unsigned int vendor_id, subcmd;
5695 struct wpabuf *reply;
5696 size_t data_len = 0;
5697
5698 /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
5699 vendor_id = strtoul(cmd, &pos, 16);
5700 if (!isblank(*pos))
5701 return -EINVAL;
5702
5703 subcmd = strtoul(pos, &pos, 10);
5704
5705 if (*pos != '\0') {
5706 if (!isblank(*pos++))
5707 return -EINVAL;
5708 data_len = os_strlen(pos);
5709 }
5710
5711 if (data_len) {
5712 data_len /= 2;
5713 data = os_malloc(data_len);
5714 if (!data)
5715 return -1;
5716
5717 if (hexstr2bin(pos, data, data_len)) {
5718 wpa_printf(MSG_DEBUG,
5719 "Vendor command: wrong parameter format");
5720 os_free(data);
5721 return -EINVAL;
5722 }
5723 }
5724
5725 reply = wpabuf_alloc((buflen - 1) / 2);
5726 if (!reply) {
5727 os_free(data);
5728 return -1;
5729 }
5730
5731 ret = wpa_drv_vendor_cmd(wpa_s, vendor_id, subcmd, data, data_len,
5732 reply);
5733
5734 if (ret == 0)
5735 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
5736 wpabuf_len(reply));
5737
5738 wpabuf_free(reply);
5739 os_free(data);
5740
5741 return ret;
5742 }
5743
5744
5745 static void wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant *wpa_s)
5746 {
5747 wpa_dbg(wpa_s, MSG_DEBUG, "Flush all wpa_supplicant state");
5748
5749 #ifdef CONFIG_P2P
5750 wpas_p2p_cancel(wpa_s);
5751 wpas_p2p_stop_find(wpa_s);
5752 p2p_ctrl_flush(wpa_s);
5753 wpas_p2p_group_remove(wpa_s, "*");
5754 wpas_p2p_service_flush(wpa_s);
5755 wpa_s->global->p2p_disabled = 0;
5756 wpa_s->global->p2p_per_sta_psk = 0;
5757 wpa_s->conf->num_sec_device_types = 0;
5758 wpa_s->p2p_disable_ip_addr_req = 0;
5759 os_free(wpa_s->global->p2p_go_avoid_freq.range);
5760 wpa_s->global->p2p_go_avoid_freq.range = NULL;
5761 #endif /* CONFIG_P2P */
5762
5763 #ifdef CONFIG_WPS_TESTING
5764 wps_version_number = 0x20;
5765 wps_testing_dummy_cred = 0;
5766 wps_corrupt_pkhash = 0;
5767 #endif /* CONFIG_WPS_TESTING */
5768 #ifdef CONFIG_WPS
5769 wpa_s->wps_fragment_size = 0;
5770 wpas_wps_cancel(wpa_s);
5771 #endif /* CONFIG_WPS */
5772 wpa_s->after_wps = 0;
5773 wpa_s->known_wps_freq = 0;
5774
5775 #ifdef CONFIG_TDLS
5776 #ifdef CONFIG_TDLS_TESTING
5777 extern unsigned int tdls_testing;
5778 tdls_testing = 0;
5779 #endif /* CONFIG_TDLS_TESTING */
5780 wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL);
5781 wpa_tdls_enable(wpa_s->wpa, 1);
5782 #endif /* CONFIG_TDLS */
5783
5784 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
5785 wpa_supplicant_stop_countermeasures(wpa_s, NULL);
5786
5787 wpa_s->no_keep_alive = 0;
5788
5789 os_free(wpa_s->disallow_aps_bssid);
5790 wpa_s->disallow_aps_bssid = NULL;
5791 wpa_s->disallow_aps_bssid_count = 0;
5792 os_free(wpa_s->disallow_aps_ssid);
5793 wpa_s->disallow_aps_ssid = NULL;
5794 wpa_s->disallow_aps_ssid_count = 0;
5795
5796 wpa_s->set_sta_uapsd = 0;
5797 wpa_s->sta_uapsd = 0;
5798
5799 wpa_drv_radio_disable(wpa_s, 0);
5800
5801 wpa_bss_flush(wpa_s);
5802 wpa_blacklist_clear(wpa_s);
5803 wpa_s->extra_blacklist_count = 0;
5804 wpa_supplicant_ctrl_iface_remove_network(wpa_s, "all");
5805 wpa_supplicant_ctrl_iface_remove_cred(wpa_s, "all");
5806 wpa_config_flush_blobs(wpa_s->conf);
5807 wpa_s->conf->auto_interworking = 0;
5808 wpa_s->conf->okc = 0;
5809
5810 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME, 43200);
5811 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD, 70);
5812 wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, 60);
5813 eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
5814
5815 radio_remove_works(wpa_s, NULL, 1);
5816
5817 wpa_s->next_ssid = NULL;
5818
5819 #ifdef CONFIG_INTERWORKING
5820 hs20_cancel_fetch_osu(wpa_s);
5821 #endif /* CONFIG_INTERWORKING */
5822
5823 wpa_s->ext_mgmt_frame_handling = 0;
5824 wpa_s->ext_eapol_frame_io = 0;
5825 }
5826
5827
5828 static int wpas_ctrl_radio_work_show(struct wpa_supplicant *wpa_s,
5829 char *buf, size_t buflen)
5830 {
5831 struct wpa_radio_work *work;
5832 char *pos, *end;
5833 struct os_reltime now, diff;
5834
5835 pos = buf;
5836 end = buf + buflen;
5837
5838 os_get_reltime(&now);
5839
5840 dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
5841 {
5842 int ret;
5843
5844 os_reltime_sub(&now, &work->time, &diff);
5845 ret = os_snprintf(pos, end - pos, "%s@%s:%u:%u:%ld.%06ld\n",
5846 work->type, work->wpa_s->ifname, work->freq,
5847 work->started, diff.sec, diff.usec);
5848 if (ret < 0 || ret >= end - pos)
5849 break;
5850 pos += ret;
5851 }
5852
5853 return pos - buf;
5854 }
5855
5856
5857 static void wpas_ctrl_radio_work_timeout(void *eloop_ctx, void *timeout_ctx)
5858 {
5859 struct wpa_radio_work *work = eloop_ctx;
5860 struct wpa_external_work *ework = work->ctx;
5861
5862 wpa_dbg(work->wpa_s, MSG_DEBUG,
5863 "Timing out external radio work %u (%s)",
5864 ework->id, work->type);
5865 wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_TIMEOUT "%u", ework->id);
5866 radio_work_done(work);
5867 os_free(ework);
5868 }
5869
5870
5871 static void wpas_ctrl_radio_work_cb(struct wpa_radio_work *work, int deinit)
5872 {
5873 struct wpa_external_work *ework = work->ctx;
5874
5875 if (deinit) {
5876 if (work->started)
5877 eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
5878 work, NULL);
5879
5880 os_free(ework);
5881 return;
5882 }
5883
5884 wpa_dbg(work->wpa_s, MSG_DEBUG, "Starting external radio work %u (%s)",
5885 ework->id, ework->type);
5886 wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_START "%u", ework->id);
5887 if (!ework->timeout)
5888 ework->timeout = 10;
5889 eloop_register_timeout(ework->timeout, 0, wpas_ctrl_radio_work_timeout,
5890 work, NULL);
5891 }
5892
5893
5894 static int wpas_ctrl_radio_work_add(struct wpa_supplicant *wpa_s, char *cmd,
5895 char *buf, size_t buflen)
5896 {
5897 struct wpa_external_work *ework;
5898 char *pos, *pos2;
5899 size_t type_len;
5900 int ret;
5901 unsigned int freq = 0;
5902
5903 /* format: <name> [freq=<MHz>] [timeout=<seconds>] */
5904
5905 ework = os_zalloc(sizeof(*ework));
5906 if (ework == NULL)
5907 return -1;
5908
5909 pos = os_strchr(cmd, ' ');
5910 if (pos) {
5911 type_len = pos - cmd;
5912 pos++;
5913
5914 pos2 = os_strstr(pos, "freq=");
5915 if (pos2)
5916 freq = atoi(pos2 + 5);
5917
5918 pos2 = os_strstr(pos, "timeout=");
5919 if (pos2)
5920 ework->timeout = atoi(pos2 + 8);
5921 } else {
5922 type_len = os_strlen(cmd);
5923 }
5924 if (4 + type_len >= sizeof(ework->type))
5925 type_len = sizeof(ework->type) - 4 - 1;
5926 os_strlcpy(ework->type, "ext:", sizeof(ework->type));
5927 os_memcpy(ework->type + 4, cmd, type_len);
5928 ework->type[4 + type_len] = '\0';
5929
5930 wpa_s->ext_work_id++;
5931 if (wpa_s->ext_work_id == 0)
5932 wpa_s->ext_work_id++;
5933 ework->id = wpa_s->ext_work_id;
5934
5935 if (radio_add_work(wpa_s, freq, ework->type, 0, wpas_ctrl_radio_work_cb,
5936 ework) < 0) {
5937 os_free(ework);
5938 return -1;
5939 }
5940
5941 ret = os_snprintf(buf, buflen, "%u", ework->id);
5942 if (ret < 0 || (size_t) ret >= buflen)
5943 return -1;
5944 return ret;
5945 }
5946
5947
5948 static int wpas_ctrl_radio_work_done(struct wpa_supplicant *wpa_s, char *cmd)
5949 {
5950 struct wpa_radio_work *work;
5951 unsigned int id = atoi(cmd);
5952
5953 dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
5954 {
5955 struct wpa_external_work *ework;
5956
5957 if (os_strncmp(work->type, "ext:", 4) != 0)
5958 continue;
5959 ework = work->ctx;
5960 if (id && ework->id != id)
5961 continue;
5962 wpa_dbg(wpa_s, MSG_DEBUG,
5963 "Completed external radio work %u (%s)",
5964 ework->id, ework->type);
5965 eloop_cancel_timeout(wpas_ctrl_radio_work_timeout, work, NULL);
5966 radio_work_done(work);
5967 os_free(ework);
5968 return 3; /* "OK\n" */
5969 }
5970
5971 return -1;
5972 }
5973
5974
5975 static int wpas_ctrl_radio_work(struct wpa_supplicant *wpa_s, char *cmd,
5976 char *buf, size_t buflen)
5977 {
5978 if (os_strcmp(cmd, "show") == 0)
5979 return wpas_ctrl_radio_work_show(wpa_s, buf, buflen);
5980 if (os_strncmp(cmd, "add ", 4) == 0)
5981 return wpas_ctrl_radio_work_add(wpa_s, cmd + 4, buf, buflen);
5982 if (os_strncmp(cmd, "done ", 5) == 0)
5983 return wpas_ctrl_radio_work_done(wpa_s, cmd + 4);
5984 return -1;
5985 }
5986
5987
5988 void wpas_ctrl_radio_work_flush(struct wpa_supplicant *wpa_s)
5989 {
5990 struct wpa_radio_work *work, *tmp;
5991
5992 if (!wpa_s || !wpa_s->radio)
5993 return;
5994
5995 dl_list_for_each_safe(work, tmp, &wpa_s->radio->work,
5996 struct wpa_radio_work, list) {
5997 struct wpa_external_work *ework;
5998
5999 if (os_strncmp(work->type, "ext:", 4) != 0)
6000 continue;
6001 ework = work->ctx;
6002 wpa_dbg(wpa_s, MSG_DEBUG,
6003 "Flushing%s external radio work %u (%s)",
6004 work->started ? " started" : "", ework->id,
6005 ework->type);
6006 if (work->started)
6007 eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
6008 work, NULL);
6009 radio_work_done(work);
6010 os_free(ework);
6011 }
6012 }
6013
6014
6015 static void wpas_ctrl_eapol_response(void *eloop_ctx, void *timeout_ctx)
6016 {
6017 struct wpa_supplicant *wpa_s = eloop_ctx;
6018 eapol_sm_notify_ctrl_response(wpa_s->eapol);
6019 }
6020
6021
6022 static int set_scan_freqs(struct wpa_supplicant *wpa_s, char *val)
6023 {
6024 int *freqs = NULL;
6025
6026 freqs = freq_range_to_channel_list(wpa_s, val);
6027 if (freqs == NULL)
6028 return -1;
6029
6030 os_free(wpa_s->manual_scan_freqs);
6031 wpa_s->manual_scan_freqs = freqs;
6032
6033 return 0;
6034 }
6035
6036
6037 static int scan_id_list_parse(struct wpa_supplicant *wpa_s, const char *value)
6038 {
6039 const char *pos = value;
6040
6041 while (pos) {
6042 if (*pos == ' ' || *pos == '\0')
6043 break;
6044 if (wpa_s->scan_id_count == MAX_SCAN_ID)
6045 return -1;
6046 wpa_s->scan_id[wpa_s->scan_id_count++] = atoi(pos);
6047 pos = os_strchr(pos, ',');
6048 if (pos)
6049 pos++;
6050 }
6051
6052 return 0;
6053 }
6054
6055
6056 static void wpas_ctrl_scan(struct wpa_supplicant *wpa_s, char *params,
6057 char *reply, int reply_size, int *reply_len)
6058 {
6059 char *pos;
6060
6061 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
6062 *reply_len = -1;
6063 return;
6064 }
6065
6066 wpa_s->manual_scan_passive = 0;
6067 wpa_s->manual_scan_use_id = 0;
6068 wpa_s->manual_scan_only_new = 0;
6069 wpa_s->scan_id_count = 0;
6070
6071 if (params) {
6072 if (os_strncasecmp(params, "TYPE=ONLY", 9) == 0)
6073 wpa_s->scan_res_handler = scan_only_handler;
6074
6075 pos = os_strstr(params, "freq=");
6076 if (pos && set_scan_freqs(wpa_s, pos + 5) < 0) {
6077 *reply_len = -1;
6078 return;
6079 }
6080
6081 pos = os_strstr(params, "passive=");
6082 if (pos)
6083 wpa_s->manual_scan_passive = !!atoi(pos + 8);
6084
6085 pos = os_strstr(params, "use_id=");
6086 if (pos)
6087 wpa_s->manual_scan_use_id = atoi(pos + 7);
6088
6089 pos = os_strstr(params, "only_new=1");
6090 if (pos)
6091 wpa_s->manual_scan_only_new = 1;
6092
6093 pos = os_strstr(params, "scan_id=");
6094 if (pos && scan_id_list_parse(wpa_s, pos + 8) < 0) {
6095 *reply_len = -1;
6096 return;
6097 }
6098 } else {
6099 os_free(wpa_s->manual_scan_freqs);
6100 wpa_s->manual_scan_freqs = NULL;
6101 if (wpa_s->scan_res_handler == scan_only_handler)
6102 wpa_s->scan_res_handler = NULL;
6103 }
6104
6105 if (!wpa_s->sched_scanning && !wpa_s->scanning &&
6106 ((wpa_s->wpa_state <= WPA_SCANNING) ||
6107 (wpa_s->wpa_state == WPA_COMPLETED))) {
6108 wpa_s->normal_scans = 0;
6109 wpa_s->scan_req = MANUAL_SCAN_REQ;
6110 wpa_s->after_wps = 0;
6111 wpa_s->known_wps_freq = 0;
6112 wpa_supplicant_req_scan(wpa_s, 0, 0);
6113 if (wpa_s->manual_scan_use_id) {
6114 wpa_s->manual_scan_id++;
6115 wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
6116 wpa_s->manual_scan_id);
6117 *reply_len = os_snprintf(reply, reply_size, "%u\n",
6118 wpa_s->manual_scan_id);
6119 }
6120 } else if (wpa_s->sched_scanning) {
6121 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to allow requested full scan to proceed");
6122 wpa_supplicant_cancel_sched_scan(wpa_s);
6123 wpa_s->scan_req = MANUAL_SCAN_REQ;
6124 wpa_supplicant_req_scan(wpa_s, 0, 0);
6125 if (wpa_s->manual_scan_use_id) {
6126 wpa_s->manual_scan_id++;
6127 *reply_len = os_snprintf(reply, reply_size, "%u\n",
6128 wpa_s->manual_scan_id);
6129 wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
6130 wpa_s->manual_scan_id);
6131 }
6132 } else {
6133 wpa_printf(MSG_DEBUG, "Ongoing scan action - reject new request");
6134 *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
6135 }
6136 }
6137
6138
6139 #ifdef CONFIG_TESTING_OPTIONS
6140
6141 static void wpas_ctrl_iface_mgmt_tx_cb(struct wpa_supplicant *wpa_s,
6142 unsigned int freq, const u8 *dst,
6143 const u8 *src, const u8 *bssid,
6144 const u8 *data, size_t data_len,
6145 enum offchannel_send_action_result
6146 result)
6147 {
6148 wpa_msg(wpa_s, MSG_INFO, "MGMT-TX-STATUS freq=%u dst=" MACSTR
6149 " src=" MACSTR " bssid=" MACSTR " result=%s",
6150 freq, MAC2STR(dst), MAC2STR(src), MAC2STR(bssid),
6151 result == OFFCHANNEL_SEND_ACTION_SUCCESS ?
6152 "SUCCESS" : (result == OFFCHANNEL_SEND_ACTION_NO_ACK ?
6153 "NO_ACK" : "FAILED"));
6154 }
6155
6156
6157 static int wpas_ctrl_iface_mgmt_tx(struct wpa_supplicant *wpa_s, char *cmd)
6158 {
6159 char *pos, *param;
6160 size_t len;
6161 u8 *buf, da[ETH_ALEN], bssid[ETH_ALEN];
6162 int res, used;
6163 int freq = 0, no_cck = 0, wait_time = 0;
6164
6165 /* <DA> <BSSID> [freq=<MHz>] [wait_time=<ms>] [no_cck=1]
6166 * <action=Action frame payload> */
6167
6168 wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
6169
6170 pos = cmd;
6171 used = hwaddr_aton2(pos, da);
6172 if (used < 0)
6173 return -1;
6174 pos += used;
6175 while (*pos == ' ')
6176 pos++;
6177 used = hwaddr_aton2(pos, bssid);
6178 if (used < 0)
6179 return -1;
6180 pos += used;
6181
6182 param = os_strstr(pos, " freq=");
6183 if (param) {
6184 param += 6;
6185 freq = atoi(param);
6186 }
6187
6188 param = os_strstr(pos, " no_cck=");
6189 if (param) {
6190 param += 8;
6191 no_cck = atoi(param);
6192 }
6193
6194 param = os_strstr(pos, " wait_time=");
6195 if (param) {
6196 param += 11;
6197 wait_time = atoi(param);
6198 }
6199
6200 param = os_strstr(pos, " action=");
6201 if (param == NULL)
6202 return -1;
6203 param += 8;
6204
6205 len = os_strlen(param);
6206 if (len & 1)
6207 return -1;
6208 len /= 2;
6209
6210 buf = os_malloc(len);
6211 if (buf == NULL)
6212 return -1;
6213
6214 if (hexstr2bin(param, buf, len) < 0) {
6215 os_free(buf);
6216 return -1;
6217 }
6218
6219 res = offchannel_send_action(wpa_s, freq, da, wpa_s->own_addr, bssid,
6220 buf, len, wait_time,
6221 wpas_ctrl_iface_mgmt_tx_cb, no_cck);
6222 os_free(buf);
6223 return res;
6224 }
6225
6226
6227 static void wpas_ctrl_iface_mgmt_tx_done(struct wpa_supplicant *wpa_s)
6228 {
6229 wpa_printf(MSG_DEBUG, "External MGMT TX - done waiting");
6230 offchannel_send_action_done(wpa_s);
6231 }
6232
6233
6234 static int wpas_ctrl_iface_driver_event(struct wpa_supplicant *wpa_s, char *cmd)
6235 {
6236 char *pos, *param;
6237 union wpa_event_data event;
6238 enum wpa_event_type ev;
6239
6240 /* <event name> [parameters..] */
6241
6242 wpa_dbg(wpa_s, MSG_DEBUG, "Testing - external driver event: %s", cmd);
6243
6244 pos = cmd;
6245 param = os_strchr(pos, ' ');
6246 if (param)
6247 *param++ = '\0';
6248
6249 os_memset(&event, 0, sizeof(event));
6250
6251 if (os_strcmp(cmd, "INTERFACE_ENABLED") == 0) {
6252 ev = EVENT_INTERFACE_ENABLED;
6253 } else if (os_strcmp(cmd, "INTERFACE_DISABLED") == 0) {
6254 ev = EVENT_INTERFACE_DISABLED;
6255 } else if (os_strcmp(cmd, "AVOID_FREQUENCIES") == 0) {
6256 ev = EVENT_AVOID_FREQUENCIES;
6257 if (param == NULL)
6258 param = "";
6259 if (freq_range_list_parse(&event.freq_range, param) < 0)
6260 return -1;
6261 wpa_supplicant_event(wpa_s, ev, &event);
6262 os_free(event.freq_range.range);
6263 return 0;
6264 } else {
6265 wpa_dbg(wpa_s, MSG_DEBUG, "Testing - unknown driver event: %s",
6266 cmd);
6267 return -1;
6268 }
6269
6270 wpa_supplicant_event(wpa_s, ev, &event);
6271
6272 return 0;
6273 }
6274
6275
6276 static int wpas_ctrl_iface_eapol_rx(struct wpa_supplicant *wpa_s, char *cmd)
6277 {
6278 char *pos;
6279 u8 src[ETH_ALEN], *buf;
6280 int used;
6281 size_t len;
6282
6283 wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
6284
6285 pos = cmd;
6286 used = hwaddr_aton2(pos, src);
6287 if (used < 0)
6288 return -1;
6289 pos += used;
6290 while (*pos == ' ')
6291 pos++;
6292
6293 len = os_strlen(pos);
6294 if (len & 1)
6295 return -1;
6296 len /= 2;
6297
6298 buf = os_malloc(len);
6299 if (buf == NULL)
6300 return -1;
6301
6302 if (hexstr2bin(pos, buf, len) < 0) {
6303 os_free(buf);
6304 return -1;
6305 }
6306
6307 wpa_supplicant_rx_eapol(wpa_s, src, buf, len);
6308 os_free(buf);
6309
6310 return 0;
6311 }
6312
6313
6314 static u16 ipv4_hdr_checksum(const void *buf, size_t len)
6315 {
6316 size_t i;
6317 u32 sum = 0;
6318 const u16 *pos = buf;
6319
6320 for (i = 0; i < len / 2; i++)
6321 sum += *pos++;
6322
6323 while (sum >> 16)
6324 sum = (sum & 0xffff) + (sum >> 16);
6325
6326 return sum ^ 0xffff;
6327 }
6328
6329
6330 #define HWSIM_PACKETLEN 1500
6331 #define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
6332
6333 void wpas_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf, size_t len)
6334 {
6335 struct wpa_supplicant *wpa_s = ctx;
6336 const struct ether_header *eth;
6337 const struct iphdr *ip;
6338 const u8 *pos;
6339 unsigned int i;
6340
6341 if (len != HWSIM_PACKETLEN)
6342 return;
6343
6344 eth = (const struct ether_header *) buf;
6345 ip = (const struct iphdr *) (eth + 1);
6346 pos = (const u8 *) (ip + 1);
6347
6348 if (ip->ihl != 5 || ip->version != 4 ||
6349 ntohs(ip->tot_len) != HWSIM_IP_LEN)
6350 return;
6351
6352 for (i = 0; i < HWSIM_IP_LEN - sizeof(*ip); i++) {
6353 if (*pos != (u8) i)
6354 return;
6355 pos++;
6356 }
6357
6358 wpa_msg(wpa_s, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR,
6359 MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost));
6360 }
6361
6362
6363 static int wpas_ctrl_iface_data_test_config(struct wpa_supplicant *wpa_s,
6364 char *cmd)
6365 {
6366 int enabled = atoi(cmd);
6367
6368 if (!enabled) {
6369 if (wpa_s->l2_test) {
6370 l2_packet_deinit(wpa_s->l2_test);
6371 wpa_s->l2_test = NULL;
6372 wpa_dbg(wpa_s, MSG_DEBUG, "test data: Disabled");
6373 }
6374 return 0;
6375 }
6376
6377 if (wpa_s->l2_test)
6378 return 0;
6379
6380 wpa_s->l2_test = l2_packet_init(wpa_s->ifname, wpa_s->own_addr,
6381 ETHERTYPE_IP, wpas_data_test_rx,
6382 wpa_s, 1);
6383 if (wpa_s->l2_test == NULL)
6384 return -1;
6385
6386 wpa_dbg(wpa_s, MSG_DEBUG, "test data: Enabled");
6387
6388 return 0;
6389 }
6390
6391
6392 static int wpas_ctrl_iface_data_test_tx(struct wpa_supplicant *wpa_s, char *cmd)
6393 {
6394 u8 dst[ETH_ALEN], src[ETH_ALEN];
6395 char *pos;
6396 int used;
6397 long int val;
6398 u8 tos;
6399 u8 buf[HWSIM_PACKETLEN];
6400 struct ether_header *eth;
6401 struct iphdr *ip;
6402 u8 *dpos;
6403 unsigned int i;
6404
6405 if (wpa_s->l2_test == NULL)
6406 return -1;
6407
6408 /* format: <dst> <src> <tos> */
6409
6410 pos = cmd;
6411 used = hwaddr_aton2(pos, dst);
6412 if (used < 0)
6413 return -1;
6414 pos += used;
6415 while (*pos == ' ')
6416 pos++;
6417 used = hwaddr_aton2(pos, src);
6418 if (used < 0)
6419 return -1;
6420 pos += used;
6421
6422 val = strtol(pos, NULL, 0);
6423 if (val < 0 || val > 0xff)
6424 return -1;
6425 tos = val;
6426
6427 eth = (struct ether_header *) buf;
6428 os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
6429 os_memcpy(eth->ether_shost, src, ETH_ALEN);
6430 eth->ether_type = htons(ETHERTYPE_IP);
6431 ip = (struct iphdr *) (eth + 1);
6432 os_memset(ip, 0, sizeof(*ip));
6433 ip->ihl = 5;
6434 ip->version = 4;
6435 ip->ttl = 64;
6436 ip->tos = tos;
6437 ip->tot_len = htons(HWSIM_IP_LEN);
6438 ip->protocol = 1;
6439 ip->saddr = htonl(192 << 24 | 168 << 16 | 1 << 8 | 1);
6440 ip->daddr = htonl(192 << 24 | 168 << 16 | 1 << 8 | 2);
6441 ip->check = ipv4_hdr_checksum(ip, sizeof(*ip));
6442 dpos = (u8 *) (ip + 1);
6443 for (i = 0; i < HWSIM_IP_LEN - sizeof(*ip); i++)
6444 *dpos++ = i;
6445
6446 if (l2_packet_send(wpa_s->l2_test, dst, ETHERTYPE_IP, buf,
6447 HWSIM_PACKETLEN) < 0)
6448 return -1;
6449
6450 wpa_dbg(wpa_s, MSG_DEBUG, "test data: TX dst=" MACSTR " src=" MACSTR
6451 " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
6452
6453 return 0;
6454 }
6455
6456 #endif /* CONFIG_TESTING_OPTIONS */
6457
6458
6459 static void wpas_ctrl_vendor_elem_update(struct wpa_supplicant *wpa_s)
6460 {
6461 unsigned int i;
6462 char buf[30];
6463
6464 wpa_printf(MSG_DEBUG, "Update vendor elements");
6465
6466 for (i = 0; i < NUM_VENDOR_ELEM_FRAMES; i++) {
6467 if (wpa_s->vendor_elem[i]) {
6468 os_snprintf(buf, sizeof(buf), "frame[%u]", i);
6469 wpa_hexdump_buf(MSG_DEBUG, buf, wpa_s->vendor_elem[i]);
6470 }
6471 }
6472
6473 #ifdef CONFIG_P2P
6474 if (wpa_s->parent == wpa_s &&
6475 wpa_s->global->p2p &&
6476 !wpa_s->global->p2p_disabled)
6477 p2p_set_vendor_elems(wpa_s->global->p2p, wpa_s->vendor_elem);
6478 #endif /* CONFIG_P2P */
6479 }
6480
6481
6482 static struct wpa_supplicant *
6483 wpas_ctrl_vendor_elem_iface(struct wpa_supplicant *wpa_s,
6484 enum wpa_vendor_elem_frame frame)
6485 {
6486 switch (frame) {
6487 #ifdef CONFIG_P2P
6488 case VENDOR_ELEM_PROBE_REQ_P2P:
6489 case VENDOR_ELEM_PROBE_RESP_P2P:
6490 case VENDOR_ELEM_PROBE_RESP_P2P_GO:
6491 case VENDOR_ELEM_BEACON_P2P_GO:
6492 case VENDOR_ELEM_P2P_PD_REQ:
6493 case VENDOR_ELEM_P2P_PD_RESP:
6494 case VENDOR_ELEM_P2P_GO_NEG_REQ:
6495 case VENDOR_ELEM_P2P_GO_NEG_RESP:
6496 case VENDOR_ELEM_P2P_GO_NEG_CONF:
6497 case VENDOR_ELEM_P2P_INV_REQ:
6498 case VENDOR_ELEM_P2P_INV_RESP:
6499 case VENDOR_ELEM_P2P_ASSOC_REQ:
6500 return wpa_s->parent;
6501 #endif /* CONFIG_P2P */
6502 default:
6503 return wpa_s;
6504 }
6505 }
6506
6507
6508 static int wpas_ctrl_vendor_elem_add(struct wpa_supplicant *wpa_s, char *cmd)
6509 {
6510 char *pos = cmd;
6511 int frame;
6512 size_t len;
6513 struct wpabuf *buf;
6514 struct ieee802_11_elems elems;
6515
6516 frame = atoi(pos);
6517 if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
6518 return -1;
6519 wpa_s = wpas_ctrl_vendor_elem_iface(wpa_s, frame);
6520
6521 pos = os_strchr(pos, ' ');
6522 if (pos == NULL)
6523 return -1;
6524 pos++;
6525
6526 len = os_strlen(pos);
6527 if (len == 0)
6528 return 0;
6529 if (len & 1)
6530 return -1;
6531 len /= 2;
6532
6533 buf = wpabuf_alloc(len);
6534 if (buf == NULL)
6535 return -1;
6536
6537 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
6538 wpabuf_free(buf);
6539 return -1;
6540 }
6541
6542 if (ieee802_11_parse_elems(wpabuf_head_u8(buf), len, &elems, 0) ==
6543 ParseFailed) {
6544 wpabuf_free(buf);
6545 return -1;
6546 }
6547
6548 if (wpa_s->vendor_elem[frame] == NULL) {
6549 wpa_s->vendor_elem[frame] = buf;
6550 wpas_ctrl_vendor_elem_update(wpa_s);
6551 return 0;
6552 }
6553
6554 if (wpabuf_resize(&wpa_s->vendor_elem[frame], len) < 0) {
6555 wpabuf_free(buf);
6556 return -1;
6557 }
6558
6559 wpabuf_put_buf(wpa_s->vendor_elem[frame], buf);
6560 wpabuf_free(buf);
6561 wpas_ctrl_vendor_elem_update(wpa_s);
6562
6563 return 0;
6564 }
6565
6566
6567 static int wpas_ctrl_vendor_elem_get(struct wpa_supplicant *wpa_s, char *cmd,
6568 char *buf, size_t buflen)
6569 {
6570 int frame = atoi(cmd);
6571
6572 if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
6573 return -1;
6574 wpa_s = wpas_ctrl_vendor_elem_iface(wpa_s, frame);
6575
6576 if (wpa_s->vendor_elem[frame] == NULL)
6577 return 0;
6578
6579 return wpa_snprintf_hex(buf, buflen,
6580 wpabuf_head_u8(wpa_s->vendor_elem[frame]),
6581 wpabuf_len(wpa_s->vendor_elem[frame]));
6582 }
6583
6584
6585 static int wpas_ctrl_vendor_elem_remove(struct wpa_supplicant *wpa_s, char *cmd)
6586 {
6587 char *pos = cmd;
6588 int frame;
6589 size_t len;
6590 u8 *buf;
6591 struct ieee802_11_elems elems;
6592 u8 *ie, *end;
6593
6594 frame = atoi(pos);
6595 if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
6596 return -1;
6597 wpa_s = wpas_ctrl_vendor_elem_iface(wpa_s, frame);
6598
6599 pos = os_strchr(pos, ' ');
6600 if (pos == NULL)
6601 return -1;
6602 pos++;
6603
6604 if (*pos == '*') {
6605 wpabuf_free(wpa_s->vendor_elem[frame]);
6606 wpa_s->vendor_elem[frame] = NULL;
6607 wpas_ctrl_vendor_elem_update(wpa_s);
6608 return 0;
6609 }
6610
6611 if (wpa_s->vendor_elem[frame] == NULL)
6612 return -1;
6613
6614 len = os_strlen(pos);
6615 if (len == 0)
6616 return 0;
6617 if (len & 1)
6618 return -1;
6619 len /= 2;
6620
6621 buf = os_malloc(len);
6622 if (buf == NULL)
6623 return -1;
6624
6625 if (hexstr2bin(pos, buf, len) < 0) {
6626 os_free(buf);
6627 return -1;
6628 }
6629
6630 if (ieee802_11_parse_elems(buf, len, &elems, 0) == ParseFailed) {
6631 os_free(buf);
6632 return -1;
6633 }
6634
6635 ie = wpabuf_mhead_u8(wpa_s->vendor_elem[frame]);
6636 end = ie + wpabuf_len(wpa_s->vendor_elem[frame]);
6637
6638 for (; ie + 1 < end; ie += 2 + ie[1]) {
6639 if (ie + len > end)
6640 break;
6641 if (os_memcmp(ie, buf, len) != 0)
6642 continue;
6643
6644 if (wpabuf_len(wpa_s->vendor_elem[frame]) == len) {
6645 wpabuf_free(wpa_s->vendor_elem[frame]);
6646 wpa_s->vendor_elem[frame] = NULL;
6647 } else {
6648 os_memmove(ie, ie + len,
6649 end - (ie + len));
6650 wpa_s->vendor_elem[frame]->used -= len;
6651 }
6652 os_free(buf);
6653 wpas_ctrl_vendor_elem_update(wpa_s);
6654 return 0;
6655 }
6656
6657 os_free(buf);
6658
6659 return -1;
6660 }
6661
6662
6663 char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
6664 char *buf, size_t *resp_len)
6665 {
6666 char *reply;
6667 const int reply_size = 4096;
6668 int reply_len;
6669
6670 if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
6671 os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
6672 if (wpa_debug_show_keys)
6673 wpa_dbg(wpa_s, MSG_DEBUG,
6674 "Control interface command '%s'", buf);
6675 else
6676 wpa_dbg(wpa_s, MSG_DEBUG,
6677 "Control interface command '%s [REMOVED]'",
6678 os_strncmp(buf, WPA_CTRL_RSP,
6679 os_strlen(WPA_CTRL_RSP)) == 0 ?
6680 WPA_CTRL_RSP : "SET_NETWORK");
6681 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ", 16) == 0 ||
6682 os_strncmp(buf, "NFC_REPORT_HANDOVER", 19) == 0) {
6683 wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
6684 (const u8 *) buf, os_strlen(buf));
6685 } else {
6686 int level = MSG_DEBUG;
6687 if (os_strcmp(buf, "PING") == 0)
6688 level = MSG_EXCESSIVE;
6689 wpa_dbg(wpa_s, level, "Control interface command '%s'", buf);
6690 }
6691
6692 reply = os_malloc(reply_size);
6693 if (reply == NULL) {
6694 *resp_len = 1;
6695 return NULL;
6696 }
6697
6698 os_memcpy(reply, "OK\n", 3);
6699 reply_len = 3;
6700
6701 if (os_strcmp(buf, "PING") == 0) {
6702 os_memcpy(reply, "PONG\n", 5);
6703 reply_len = 5;
6704 } else if (os_strcmp(buf, "IFNAME") == 0) {
6705 reply_len = os_strlen(wpa_s->ifname);
6706 os_memcpy(reply, wpa_s->ifname, reply_len);
6707 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
6708 if (wpa_debug_reopen_file() < 0)
6709 reply_len = -1;
6710 } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
6711 wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
6712 } else if (os_strcmp(buf, "MIB") == 0) {
6713 reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
6714 if (reply_len >= 0) {
6715 int res;
6716 res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
6717 reply_size - reply_len);
6718 if (res < 0)
6719 reply_len = -1;
6720 else
6721 reply_len += res;
6722 }
6723 } else if (os_strncmp(buf, "STATUS", 6) == 0) {
6724 reply_len = wpa_supplicant_ctrl_iface_status(
6725 wpa_s, buf + 6, reply, reply_size);
6726 } else if (os_strcmp(buf, "PMKSA") == 0) {
6727 reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
6728 reply_size);
6729 } else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
6730 wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
6731 } else if (os_strncmp(buf, "SET ", 4) == 0) {
6732 if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
6733 reply_len = -1;
6734 } else if (os_strncmp(buf, "GET ", 4) == 0) {
6735 reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
6736 reply, reply_size);
6737 } else if (os_strcmp(buf, "LOGON") == 0) {
6738 eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
6739 } else if (os_strcmp(buf, "LOGOFF") == 0) {
6740 eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
6741 } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
6742 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
6743 reply_len = -1;
6744 else
6745 wpas_request_connection(wpa_s);
6746 } else if (os_strcmp(buf, "REATTACH") == 0) {
6747 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED ||
6748 !wpa_s->current_ssid)
6749 reply_len = -1;
6750 else {
6751 wpa_s->reattach = 1;
6752 wpas_request_connection(wpa_s);
6753 }
6754 } else if (os_strcmp(buf, "RECONNECT") == 0) {
6755 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
6756 reply_len = -1;
6757 else if (wpa_s->disconnected)
6758 wpas_request_connection(wpa_s);
6759 #ifdef IEEE8021X_EAPOL
6760 } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
6761 if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
6762 reply_len = -1;
6763 #endif /* IEEE8021X_EAPOL */
6764 #ifdef CONFIG_PEERKEY
6765 } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
6766 if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
6767 reply_len = -1;
6768 #endif /* CONFIG_PEERKEY */
6769 #ifdef CONFIG_IEEE80211R
6770 } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
6771 if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
6772 reply_len = -1;
6773 #endif /* CONFIG_IEEE80211R */
6774 #ifdef CONFIG_WPS
6775 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
6776 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
6777 if (res == -2) {
6778 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
6779 reply_len = 17;
6780 } else if (res)
6781 reply_len = -1;
6782 } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
6783 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
6784 if (res == -2) {
6785 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
6786 reply_len = 17;
6787 } else if (res)
6788 reply_len = -1;
6789 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
6790 reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
6791 reply,
6792 reply_size);
6793 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
6794 reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
6795 wpa_s, buf + 14, reply, reply_size);
6796 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
6797 if (wpas_wps_cancel(wpa_s))
6798 reply_len = -1;
6799 #ifdef CONFIG_WPS_NFC
6800 } else if (os_strcmp(buf, "WPS_NFC") == 0) {
6801 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
6802 reply_len = -1;
6803 } else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
6804 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
6805 reply_len = -1;
6806 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
6807 reply_len = wpa_supplicant_ctrl_iface_wps_nfc_config_token(
6808 wpa_s, buf + 21, reply, reply_size);
6809 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
6810 reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
6811 wpa_s, buf + 14, reply, reply_size);
6812 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
6813 if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
6814 buf + 17))
6815 reply_len = -1;
6816 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_REQ ", 21) == 0) {
6817 reply_len = wpas_ctrl_nfc_get_handover_req(
6818 wpa_s, buf + 21, reply, reply_size);
6819 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
6820 reply_len = wpas_ctrl_nfc_get_handover_sel(
6821 wpa_s, buf + 21, reply, reply_size);
6822 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
6823 if (wpas_ctrl_nfc_report_handover(wpa_s, buf + 20))
6824 reply_len = -1;
6825 #endif /* CONFIG_WPS_NFC */
6826 } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
6827 if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
6828 reply_len = -1;
6829 #ifdef CONFIG_AP
6830 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
6831 reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
6832 wpa_s, buf + 11, reply, reply_size);
6833 #endif /* CONFIG_AP */
6834 #ifdef CONFIG_WPS_ER
6835 } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
6836 if (wpas_wps_er_start(wpa_s, NULL))
6837 reply_len = -1;
6838 } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
6839 if (wpas_wps_er_start(wpa_s, buf + 13))
6840 reply_len = -1;
6841 } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
6842 if (wpas_wps_er_stop(wpa_s))
6843 reply_len = -1;
6844 } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
6845 if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
6846 reply_len = -1;
6847 } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
6848 int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
6849 if (ret == -2) {
6850 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
6851 reply_len = 17;
6852 } else if (ret == -3) {
6853 os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
6854 reply_len = 18;
6855 } else if (ret == -4) {
6856 os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
6857 reply_len = 20;
6858 } else if (ret)
6859 reply_len = -1;
6860 } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
6861 if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
6862 reply_len = -1;
6863 } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
6864 if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
6865 buf + 18))
6866 reply_len = -1;
6867 } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
6868 if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
6869 reply_len = -1;
6870 #ifdef CONFIG_WPS_NFC
6871 } else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
6872 reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
6873 wpa_s, buf + 24, reply, reply_size);
6874 #endif /* CONFIG_WPS_NFC */
6875 #endif /* CONFIG_WPS_ER */
6876 #endif /* CONFIG_WPS */
6877 #ifdef CONFIG_IBSS_RSN
6878 } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
6879 if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
6880 reply_len = -1;
6881 #endif /* CONFIG_IBSS_RSN */
6882 #ifdef CONFIG_P2P
6883 } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
6884 if (p2p_ctrl_find(wpa_s, buf + 9))
6885 reply_len = -1;
6886 } else if (os_strcmp(buf, "P2P_FIND") == 0) {
6887 if (p2p_ctrl_find(wpa_s, ""))
6888 reply_len = -1;
6889 } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
6890 wpas_p2p_stop_find(wpa_s);
6891 } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
6892 reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
6893 reply_size);
6894 } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
6895 if (p2p_ctrl_listen(wpa_s, buf + 11))
6896 reply_len = -1;
6897 } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
6898 if (p2p_ctrl_listen(wpa_s, ""))
6899 reply_len = -1;
6900 } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
6901 if (wpas_p2p_group_remove(wpa_s, buf + 17))
6902 reply_len = -1;
6903 } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
6904 if (wpas_p2p_group_add(wpa_s, 0, 0, 0, 0))
6905 reply_len = -1;
6906 } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
6907 if (p2p_ctrl_group_add(wpa_s, buf + 14))
6908 reply_len = -1;
6909 } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
6910 if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
6911 reply_len = -1;
6912 } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
6913 reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
6914 } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
6915 reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
6916 reply_size);
6917 } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
6918 if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
6919 reply_len = -1;
6920 } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
6921 if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
6922 reply_len = -1;
6923 } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
6924 wpas_p2p_sd_service_update(wpa_s);
6925 } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
6926 if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
6927 reply_len = -1;
6928 } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
6929 wpas_p2p_service_flush(wpa_s);
6930 } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
6931 if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
6932 reply_len = -1;
6933 } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
6934 if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
6935 reply_len = -1;
6936 } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
6937 if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
6938 reply_len = -1;
6939 } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
6940 if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
6941 reply_len = -1;
6942 } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
6943 reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
6944 reply_size);
6945 } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
6946 if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
6947 reply_len = -1;
6948 } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
6949 p2p_ctrl_flush(wpa_s);
6950 } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
6951 if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
6952 reply_len = -1;
6953 } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
6954 if (wpas_p2p_cancel(wpa_s))
6955 reply_len = -1;
6956 } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
6957 if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
6958 reply_len = -1;
6959 } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
6960 if (p2p_ctrl_presence_req(wpa_s, "") < 0)
6961 reply_len = -1;
6962 } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
6963 if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
6964 reply_len = -1;
6965 } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
6966 if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
6967 reply_len = -1;
6968 } else if (os_strncmp(buf, "P2P_REMOVE_CLIENT ", 18) == 0) {
6969 if (p2p_ctrl_remove_client(wpa_s, buf + 18) < 0)
6970 reply_len = -1;
6971 #endif /* CONFIG_P2P */
6972 #ifdef CONFIG_WIFI_DISPLAY
6973 } else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
6974 if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
6975 reply_len = -1;
6976 } else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
6977 reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
6978 reply, reply_size);
6979 #endif /* CONFIG_WIFI_DISPLAY */
6980 #ifdef CONFIG_INTERWORKING
6981 } else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
6982 if (interworking_fetch_anqp(wpa_s) < 0)
6983 reply_len = -1;
6984 } else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
6985 interworking_stop_fetch_anqp(wpa_s);
6986 } else if (os_strcmp(buf, "INTERWORKING_SELECT") == 0) {
6987 if (ctrl_interworking_select(wpa_s, NULL) < 0)
6988 reply_len = -1;
6989 } else if (os_strncmp(buf, "INTERWORKING_SELECT ", 20) == 0) {
6990 if (ctrl_interworking_select(wpa_s, buf + 20) < 0)
6991 reply_len = -1;
6992 } else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
6993 if (ctrl_interworking_connect(wpa_s, buf + 21) < 0)
6994 reply_len = -1;
6995 } else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
6996 if (get_anqp(wpa_s, buf + 9) < 0)
6997 reply_len = -1;
6998 } else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
6999 if (gas_request(wpa_s, buf + 12) < 0)
7000 reply_len = -1;
7001 } else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
7002 reply_len = gas_response_get(wpa_s, buf + 17, reply,
7003 reply_size);
7004 #endif /* CONFIG_INTERWORKING */
7005 #ifdef CONFIG_HS20
7006 } else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
7007 if (get_hs20_anqp(wpa_s, buf + 14) < 0)
7008 reply_len = -1;
7009 } else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
7010 if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
7011 reply_len = -1;
7012 } else if (os_strncmp(buf, "HS20_ICON_REQUEST ", 18) == 0) {
7013 if (hs20_icon_request(wpa_s, buf + 18) < 0)
7014 reply_len = -1;
7015 } else if (os_strcmp(buf, "FETCH_OSU") == 0) {
7016 if (hs20_fetch_osu(wpa_s) < 0)
7017 reply_len = -1;
7018 } else if (os_strcmp(buf, "CANCEL_FETCH_OSU") == 0) {
7019 hs20_cancel_fetch_osu(wpa_s);
7020 #endif /* CONFIG_HS20 */
7021 } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
7022 {
7023 if (wpa_supplicant_ctrl_iface_ctrl_rsp(
7024 wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
7025 reply_len = -1;
7026 else {
7027 /*
7028 * Notify response from timeout to allow the control
7029 * interface response to be sent first.
7030 */
7031 eloop_register_timeout(0, 0, wpas_ctrl_eapol_response,
7032 wpa_s, NULL);
7033 }
7034 } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
7035 if (wpa_supplicant_reload_configuration(wpa_s))
7036 reply_len = -1;
7037 } else if (os_strcmp(buf, "TERMINATE") == 0) {
7038 wpa_supplicant_terminate_proc(wpa_s->global);
7039 } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
7040 if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
7041 reply_len = -1;
7042 } else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
7043 reply_len = wpa_supplicant_ctrl_iface_blacklist(
7044 wpa_s, buf + 9, reply, reply_size);
7045 } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
7046 reply_len = wpa_supplicant_ctrl_iface_log_level(
7047 wpa_s, buf + 9, reply, reply_size);
7048 } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
7049 reply_len = wpa_supplicant_ctrl_iface_list_networks(
7050 wpa_s, reply, reply_size);
7051 } else if (os_strcmp(buf, "DISCONNECT") == 0) {
7052 #ifdef CONFIG_SME
7053 wpa_s->sme.prev_bssid_set = 0;
7054 #endif /* CONFIG_SME */
7055 wpa_s->reassociate = 0;
7056 wpa_s->disconnected = 1;
7057 wpa_supplicant_cancel_sched_scan(wpa_s);
7058 wpa_supplicant_cancel_scan(wpa_s);
7059 wpa_supplicant_deauthenticate(wpa_s,
7060 WLAN_REASON_DEAUTH_LEAVING);
7061 } else if (os_strcmp(buf, "SCAN") == 0) {
7062 wpas_ctrl_scan(wpa_s, NULL, reply, reply_size, &reply_len);
7063 } else if (os_strncmp(buf, "SCAN ", 5) == 0) {
7064 wpas_ctrl_scan(wpa_s, buf + 5, reply, reply_size, &reply_len);
7065 } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
7066 reply_len = wpa_supplicant_ctrl_iface_scan_results(
7067 wpa_s, reply, reply_size);
7068 } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
7069 if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
7070 reply_len = -1;
7071 } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
7072 if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
7073 reply_len = -1;
7074 } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
7075 if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
7076 reply_len = -1;
7077 } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
7078 reply_len = wpa_supplicant_ctrl_iface_add_network(
7079 wpa_s, reply, reply_size);
7080 } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
7081 if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
7082 reply_len = -1;
7083 } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
7084 if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
7085 reply_len = -1;
7086 } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
7087 reply_len = wpa_supplicant_ctrl_iface_get_network(
7088 wpa_s, buf + 12, reply, reply_size);
7089 } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
7090 if (wpa_supplicant_ctrl_iface_dup_network(wpa_s, buf + 12))
7091 reply_len = -1;
7092 } else if (os_strcmp(buf, "LIST_CREDS") == 0) {
7093 reply_len = wpa_supplicant_ctrl_iface_list_creds(
7094 wpa_s, reply, reply_size);
7095 } else if (os_strcmp(buf, "ADD_CRED") == 0) {
7096 reply_len = wpa_supplicant_ctrl_iface_add_cred(
7097 wpa_s, reply, reply_size);
7098 } else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
7099 if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
7100 reply_len = -1;
7101 } else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
7102 if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
7103 reply_len = -1;
7104 } else if (os_strncmp(buf, "GET_CRED ", 9) == 0) {
7105 reply_len = wpa_supplicant_ctrl_iface_get_cred(wpa_s, buf + 9,
7106 reply,
7107 reply_size);
7108 #ifndef CONFIG_NO_CONFIG_WRITE
7109 } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
7110 if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
7111 reply_len = -1;
7112 #endif /* CONFIG_NO_CONFIG_WRITE */
7113 } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
7114 reply_len = wpa_supplicant_ctrl_iface_get_capability(
7115 wpa_s, buf + 15, reply, reply_size);
7116 } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
7117 if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
7118 reply_len = -1;
7119 } else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
7120 if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
7121 reply_len = -1;
7122 } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
7123 reply_len = wpa_supplicant_global_iface_list(
7124 wpa_s->global, reply, reply_size);
7125 } else if (os_strcmp(buf, "INTERFACES") == 0) {
7126 reply_len = wpa_supplicant_global_iface_interfaces(
7127 wpa_s->global, reply, reply_size);
7128 } else if (os_strncmp(buf, "BSS ", 4) == 0) {
7129 reply_len = wpa_supplicant_ctrl_iface_bss(
7130 wpa_s, buf + 4, reply, reply_size);
7131 #ifdef CONFIG_AP
7132 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
7133 reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
7134 } else if (os_strncmp(buf, "STA ", 4) == 0) {
7135 reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
7136 reply_size);
7137 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
7138 reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
7139 reply_size);
7140 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
7141 if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
7142 reply_len = -1;
7143 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
7144 if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
7145 reply_len = -1;
7146 } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
7147 if (ap_ctrl_iface_chanswitch(wpa_s, buf + 12))
7148 reply_len = -1;
7149 #endif /* CONFIG_AP */
7150 } else if (os_strcmp(buf, "SUSPEND") == 0) {
7151 wpas_notify_suspend(wpa_s->global);
7152 } else if (os_strcmp(buf, "RESUME") == 0) {
7153 wpas_notify_resume(wpa_s->global);
7154 #ifdef CONFIG_TESTING_OPTIONS
7155 } else if (os_strcmp(buf, "DROP_SA") == 0) {
7156 wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
7157 #endif /* CONFIG_TESTING_OPTIONS */
7158 } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
7159 if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
7160 reply_len = -1;
7161 } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
7162 if (wpa_supplicant_ctrl_iface_sta_autoconnect(wpa_s, buf + 16))
7163 reply_len = -1;
7164 } else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
7165 if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
7166 reply_len = -1;
7167 } else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
7168 if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
7169 buf + 17))
7170 reply_len = -1;
7171 } else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
7172 if (wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10))
7173 reply_len = -1;
7174 #ifdef CONFIG_TDLS
7175 } else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
7176 if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
7177 reply_len = -1;
7178 } else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
7179 if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
7180 reply_len = -1;
7181 } else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
7182 if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
7183 reply_len = -1;
7184 #endif /* CONFIG_TDLS */
7185 } else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
7186 reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
7187 reply_size);
7188 } else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
7189 reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
7190 reply_size);
7191 #ifdef CONFIG_AUTOSCAN
7192 } else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
7193 if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
7194 reply_len = -1;
7195 #endif /* CONFIG_AUTOSCAN */
7196 #ifdef ANDROID
7197 } else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
7198 reply_len = wpa_supplicant_driver_cmd(wpa_s, buf + 7, reply,
7199 reply_size);
7200 #endif /* ANDROID */
7201 } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
7202 reply_len = wpa_supplicant_vendor_cmd(wpa_s, buf + 7, reply,
7203 reply_size);
7204 } else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
7205 pmksa_cache_clear_current(wpa_s->wpa);
7206 eapol_sm_request_reauth(wpa_s->eapol);
7207 #ifdef CONFIG_WNM
7208 } else if (os_strncmp(buf, "WNM_SLEEP ", 10) == 0) {
7209 if (wpas_ctrl_iface_wnm_sleep(wpa_s, buf + 10))
7210 reply_len = -1;
7211 } else if (os_strncmp(buf, "WNM_BSS_QUERY ", 10) == 0) {
7212 if (wpas_ctrl_iface_wnm_bss_query(wpa_s, buf + 10))
7213 reply_len = -1;
7214 #endif /* CONFIG_WNM */
7215 } else if (os_strcmp(buf, "FLUSH") == 0) {
7216 wpa_supplicant_ctrl_iface_flush(wpa_s);
7217 } else if (os_strncmp(buf, "RADIO_WORK ", 11) == 0) {
7218 reply_len = wpas_ctrl_radio_work(wpa_s, buf + 11, reply,
7219 reply_size);
7220 #ifdef CONFIG_TESTING_OPTIONS
7221 } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
7222 if (wpas_ctrl_iface_mgmt_tx(wpa_s, buf + 8) < 0)
7223 reply_len = -1;
7224 } else if (os_strcmp(buf, "MGMT_TX_DONE") == 0) {
7225 wpas_ctrl_iface_mgmt_tx_done(wpa_s);
7226 } else if (os_strncmp(buf, "DRIVER_EVENT ", 13) == 0) {
7227 if (wpas_ctrl_iface_driver_event(wpa_s, buf + 13) < 0)
7228 reply_len = -1;
7229 } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
7230 if (wpas_ctrl_iface_eapol_rx(wpa_s, buf + 9) < 0)
7231 reply_len = -1;
7232 } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
7233 if (wpas_ctrl_iface_data_test_config(wpa_s, buf + 17) < 0)
7234 reply_len = -1;
7235 } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
7236 if (wpas_ctrl_iface_data_test_tx(wpa_s, buf + 13) < 0)
7237 reply_len = -1;
7238 #endif /* CONFIG_TESTING_OPTIONS */
7239 } else if (os_strncmp(buf, "VENDOR_ELEM_ADD ", 16) == 0) {
7240 if (wpas_ctrl_vendor_elem_add(wpa_s, buf + 16) < 0)
7241 reply_len = -1;
7242 } else if (os_strncmp(buf, "VENDOR_ELEM_GET ", 16) == 0) {
7243 reply_len = wpas_ctrl_vendor_elem_get(wpa_s, buf + 16, reply,
7244 reply_size);
7245 } else if (os_strncmp(buf, "VENDOR_ELEM_REMOVE ", 19) == 0) {
7246 if (wpas_ctrl_vendor_elem_remove(wpa_s, buf + 19) < 0)
7247 reply_len = -1;
7248 } else {
7249 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
7250 reply_len = 16;
7251 }
7252
7253 if (reply_len < 0) {
7254 os_memcpy(reply, "FAIL\n", 5);
7255 reply_len = 5;
7256 }
7257
7258 *resp_len = reply_len;
7259 return reply;
7260 }
7261
7262
7263 static int wpa_supplicant_global_iface_add(struct wpa_global *global,
7264 char *cmd)
7265 {
7266 struct wpa_interface iface;
7267 char *pos;
7268
7269 /*
7270 * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
7271 * TAB<bridge_ifname>
7272 */
7273 wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
7274
7275 os_memset(&iface, 0, sizeof(iface));
7276
7277 do {
7278 iface.ifname = pos = cmd;
7279 pos = os_strchr(pos, '\t');
7280 if (pos)
7281 *pos++ = '\0';
7282 if (iface.ifname[0] == '\0')
7283 return -1;
7284 if (pos == NULL)
7285 break;
7286
7287 iface.confname = pos;
7288 pos = os_strchr(pos, '\t');
7289 if (pos)
7290 *pos++ = '\0';
7291 if (iface.confname[0] == '\0')
7292 iface.confname = NULL;
7293 if (pos == NULL)
7294 break;
7295
7296 iface.driver = pos;
7297 pos = os_strchr(pos, '\t');
7298 if (pos)
7299 *pos++ = '\0';
7300 if (iface.driver[0] == '\0')
7301 iface.driver = NULL;
7302 if (pos == NULL)
7303 break;
7304
7305 iface.ctrl_interface = pos;
7306 pos = os_strchr(pos, '\t');
7307 if (pos)
7308 *pos++ = '\0';
7309 if (iface.ctrl_interface[0] == '\0')
7310 iface.ctrl_interface = NULL;
7311 if (pos == NULL)
7312 break;
7313
7314 iface.driver_param = pos;
7315 pos = os_strchr(pos, '\t');
7316 if (pos)
7317 *pos++ = '\0';
7318 if (iface.driver_param[0] == '\0')
7319 iface.driver_param = NULL;
7320 if (pos == NULL)
7321 break;
7322
7323 iface.bridge_ifname = pos;
7324 pos = os_strchr(pos, '\t');
7325 if (pos)
7326 *pos++ = '\0';
7327 if (iface.bridge_ifname[0] == '\0')
7328 iface.bridge_ifname = NULL;
7329 if (pos == NULL)
7330 break;
7331 } while (0);
7332
7333 if (wpa_supplicant_get_iface(global, iface.ifname))
7334 return -1;
7335
7336 return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
7337 }
7338
7339
7340 static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
7341 char *cmd)
7342 {
7343 struct wpa_supplicant *wpa_s;
7344
7345 wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
7346
7347 wpa_s = wpa_supplicant_get_iface(global, cmd);
7348 if (wpa_s == NULL)
7349 return -1;
7350 return wpa_supplicant_remove_iface(global, wpa_s, 0);
7351 }
7352
7353
7354 static void wpa_free_iface_info(struct wpa_interface_info *iface)
7355 {
7356 struct wpa_interface_info *prev;
7357
7358 while (iface) {
7359 prev = iface;
7360 iface = iface->next;
7361
7362 os_free(prev->ifname);
7363 os_free(prev->desc);
7364 os_free(prev);
7365 }
7366 }
7367
7368
7369 static int wpa_supplicant_global_iface_list(struct wpa_global *global,
7370 char *buf, int len)
7371 {
7372 int i, res;
7373 struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
7374 char *pos, *end;
7375
7376 for (i = 0; wpa_drivers[i]; i++) {
7377 struct wpa_driver_ops *drv = wpa_drivers[i];
7378 if (drv->get_interfaces == NULL)
7379 continue;
7380 tmp = drv->get_interfaces(global->drv_priv[i]);
7381 if (tmp == NULL)
7382 continue;
7383
7384 if (last == NULL)
7385 iface = last = tmp;
7386 else
7387 last->next = tmp;
7388 while (last->next)
7389 last = last->next;
7390 }
7391
7392 pos = buf;
7393 end = buf + len;
7394 for (tmp = iface; tmp; tmp = tmp->next) {
7395 res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
7396 tmp->drv_name, tmp->ifname,
7397 tmp->desc ? tmp->desc : "");
7398 if (res < 0 || res >= end - pos) {
7399 *pos = '\0';
7400 break;
7401 }
7402 pos += res;
7403 }
7404
7405 wpa_free_iface_info(iface);
7406
7407 return pos - buf;
7408 }
7409
7410
7411 static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
7412 char *buf, int len)
7413 {
7414 int res;
7415 char *pos, *end;
7416 struct wpa_supplicant *wpa_s;
7417
7418 wpa_s = global->ifaces;
7419 pos = buf;
7420 end = buf + len;
7421
7422 while (wpa_s) {
7423 res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
7424 if (res < 0 || res >= end - pos) {
7425 *pos = '\0';
7426 break;
7427 }
7428 pos += res;
7429 wpa_s = wpa_s->next;
7430 }
7431 return pos - buf;
7432 }
7433
7434
7435 static char * wpas_global_ctrl_iface_ifname(struct wpa_global *global,
7436 const char *ifname,
7437 char *cmd, size_t *resp_len)
7438 {
7439 struct wpa_supplicant *wpa_s;
7440
7441 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7442 if (os_strcmp(ifname, wpa_s->ifname) == 0)
7443 break;
7444 }
7445
7446 if (wpa_s == NULL) {
7447 char *resp = os_strdup("FAIL-NO-IFNAME-MATCH\n");
7448 if (resp)
7449 *resp_len = os_strlen(resp);
7450 else
7451 *resp_len = 1;
7452 return resp;
7453 }
7454
7455 return wpa_supplicant_ctrl_iface_process(wpa_s, cmd, resp_len);
7456 }
7457
7458
7459 static char * wpas_global_ctrl_iface_redir_p2p(struct wpa_global *global,
7460 char *buf, size_t *resp_len)
7461 {
7462 #ifdef CONFIG_P2P
7463 static const char * cmd[] = {
7464 "LIST_NETWORKS",
7465 "P2P_FIND",
7466 "P2P_STOP_FIND",
7467 "P2P_LISTEN",
7468 "P2P_GROUP_ADD",
7469 "P2P_GET_PASSPHRASE",
7470 "P2P_SERVICE_UPDATE",
7471 "P2P_SERVICE_FLUSH",
7472 "P2P_FLUSH",
7473 "P2P_CANCEL",
7474 "P2P_PRESENCE_REQ",
7475 "P2P_EXT_LISTEN",
7476 NULL
7477 };
7478 static const char * prefix[] = {
7479 #ifdef ANDROID
7480 "DRIVER ",
7481 #endif /* ANDROID */
7482 "GET_NETWORK ",
7483 "REMOVE_NETWORK ",
7484 "P2P_FIND ",
7485 "P2P_CONNECT ",
7486 "P2P_LISTEN ",
7487 "P2P_GROUP_REMOVE ",
7488 "P2P_GROUP_ADD ",
7489 "P2P_PROV_DISC ",
7490 "P2P_SERV_DISC_REQ ",
7491 "P2P_SERV_DISC_CANCEL_REQ ",
7492 "P2P_SERV_DISC_RESP ",
7493 "P2P_SERV_DISC_EXTERNAL ",
7494 "P2P_SERVICE_ADD ",
7495 "P2P_SERVICE_DEL ",
7496 "P2P_REJECT ",
7497 "P2P_INVITE ",
7498 "P2P_PEER ",
7499 "P2P_SET ",
7500 "P2P_UNAUTHORIZE ",
7501 "P2P_PRESENCE_REQ ",
7502 "P2P_EXT_LISTEN ",
7503 "P2P_REMOVE_CLIENT ",
7504 "NFC_GET_HANDOVER_SEL ",
7505 "NFC_GET_HANDOVER_REQ ",
7506 "NFC_REPORT_HANDOVER ",
7507 NULL
7508 };
7509 int found = 0;
7510 int i;
7511
7512 if (global->p2p_init_wpa_s == NULL)
7513 return NULL;
7514
7515 for (i = 0; !found && cmd[i]; i++) {
7516 if (os_strcmp(buf, cmd[i]) == 0)
7517 found = 1;
7518 }
7519
7520 for (i = 0; !found && prefix[i]; i++) {
7521 if (os_strncmp(buf, prefix[i], os_strlen(prefix[i])) == 0)
7522 found = 1;
7523 }
7524
7525 if (found)
7526 return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
7527 buf, resp_len);
7528 #endif /* CONFIG_P2P */
7529 return NULL;
7530 }
7531
7532
7533 static char * wpas_global_ctrl_iface_redir_wfd(struct wpa_global *global,
7534 char *buf, size_t *resp_len)
7535 {
7536 #ifdef CONFIG_WIFI_DISPLAY
7537 if (global->p2p_init_wpa_s == NULL)
7538 return NULL;
7539 if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0 ||
7540 os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0)
7541 return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
7542 buf, resp_len);
7543 #endif /* CONFIG_WIFI_DISPLAY */
7544 return NULL;
7545 }
7546
7547
7548 static char * wpas_global_ctrl_iface_redir(struct wpa_global *global,
7549 char *buf, size_t *resp_len)
7550 {
7551 char *ret;
7552
7553 ret = wpas_global_ctrl_iface_redir_p2p(global, buf, resp_len);
7554 if (ret)
7555 return ret;
7556
7557 ret = wpas_global_ctrl_iface_redir_wfd(global, buf, resp_len);
7558 if (ret)
7559 return ret;
7560
7561 return NULL;
7562 }
7563
7564
7565 static int wpas_global_ctrl_iface_set(struct wpa_global *global, char *cmd)
7566 {
7567 char *value;
7568
7569 value = os_strchr(cmd, ' ');
7570 if (value == NULL)
7571 return -1;
7572 *value++ = '\0';
7573
7574 wpa_printf(MSG_DEBUG, "GLOBAL_CTRL_IFACE SET '%s'='%s'", cmd, value);
7575
7576 #ifdef CONFIG_WIFI_DISPLAY
7577 if (os_strcasecmp(cmd, "wifi_display") == 0) {
7578 wifi_display_enable(global, !!atoi(value));
7579 return 0;
7580 }
7581 #endif /* CONFIG_WIFI_DISPLAY */
7582
7583 /* Restore cmd to its original value to allow redirection */
7584 value[-1] = ' ';
7585
7586 return -1;
7587 }
7588
7589
7590 #ifndef CONFIG_NO_CONFIG_WRITE
7591 static int wpas_global_ctrl_iface_save_config(struct wpa_global *global)
7592 {
7593 int ret = 0, saved = 0;
7594 struct wpa_supplicant *wpa_s;
7595
7596 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7597 if (!wpa_s->conf->update_config) {
7598 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed to update configuration (update_config=0)");
7599 continue;
7600 }
7601
7602 if (wpa_config_write(wpa_s->confname, wpa_s->conf)) {
7603 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to update configuration");
7604 ret = 1;
7605 } else {
7606 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration updated");
7607 saved++;
7608 }
7609 }
7610
7611 if (!saved && !ret) {
7612 wpa_dbg(wpa_s, MSG_DEBUG,
7613 "CTRL_IFACE: SAVE_CONFIG - No configuration files could be updated");
7614 ret = 1;
7615 }
7616
7617 return ret;
7618 }
7619 #endif /* CONFIG_NO_CONFIG_WRITE */
7620
7621
7622 static int wpas_global_ctrl_iface_status(struct wpa_global *global,
7623 char *buf, size_t buflen)
7624 {
7625 char *pos, *end;
7626 int ret;
7627 struct wpa_supplicant *wpa_s;
7628
7629 pos = buf;
7630 end = buf + buflen;
7631
7632 #ifdef CONFIG_P2P
7633 if (global->p2p && !global->p2p_disabled) {
7634 ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
7635 "\n"
7636 "p2p_state=%s\n",
7637 MAC2STR(global->p2p_dev_addr),
7638 p2p_get_state_txt(global->p2p));
7639 if (ret < 0 || ret >= end - pos)
7640 return pos - buf;
7641 pos += ret;
7642 } else if (global->p2p) {
7643 ret = os_snprintf(pos, end - pos, "p2p_state=DISABLED\n");
7644 if (ret < 0 || ret >= end - pos)
7645 return pos - buf;
7646 pos += ret;
7647 }
7648 #endif /* CONFIG_P2P */
7649
7650 #ifdef CONFIG_WIFI_DISPLAY
7651 ret = os_snprintf(pos, end - pos, "wifi_display=%d\n",
7652 !!global->wifi_display);
7653 if (ret < 0 || ret >= end - pos)
7654 return pos - buf;
7655 pos += ret;
7656 #endif /* CONFIG_WIFI_DISPLAY */
7657
7658 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7659 ret = os_snprintf(pos, end - pos, "ifname=%s\n"
7660 "address=" MACSTR "\n",
7661 wpa_s->ifname, MAC2STR(wpa_s->own_addr));
7662 if (ret < 0 || ret >= end - pos)
7663 return pos - buf;
7664 pos += ret;
7665 }
7666
7667 return pos - buf;
7668 }
7669
7670
7671 char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
7672 char *buf, size_t *resp_len)
7673 {
7674 char *reply;
7675 const int reply_size = 2048;
7676 int reply_len;
7677 int level = MSG_DEBUG;
7678
7679 if (os_strncmp(buf, "IFNAME=", 7) == 0) {
7680 char *pos = os_strchr(buf + 7, ' ');
7681 if (pos) {
7682 *pos++ = '\0';
7683 return wpas_global_ctrl_iface_ifname(global,
7684 buf + 7, pos,
7685 resp_len);
7686 }
7687 }
7688
7689 reply = wpas_global_ctrl_iface_redir(global, buf, resp_len);
7690 if (reply)
7691 return reply;
7692
7693 if (os_strcmp(buf, "PING") == 0)
7694 level = MSG_EXCESSIVE;
7695 wpa_hexdump_ascii(level, "RX global ctrl_iface",
7696 (const u8 *) buf, os_strlen(buf));
7697
7698 reply = os_malloc(reply_size);
7699 if (reply == NULL) {
7700 *resp_len = 1;
7701 return NULL;
7702 }
7703
7704 os_memcpy(reply, "OK\n", 3);
7705 reply_len = 3;
7706
7707 if (os_strcmp(buf, "PING") == 0) {
7708 os_memcpy(reply, "PONG\n", 5);
7709 reply_len = 5;
7710 } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
7711 if (wpa_supplicant_global_iface_add(global, buf + 14))
7712 reply_len = -1;
7713 } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
7714 if (wpa_supplicant_global_iface_remove(global, buf + 17))
7715 reply_len = -1;
7716 } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
7717 reply_len = wpa_supplicant_global_iface_list(
7718 global, reply, reply_size);
7719 } else if (os_strcmp(buf, "INTERFACES") == 0) {
7720 reply_len = wpa_supplicant_global_iface_interfaces(
7721 global, reply, reply_size);
7722 } else if (os_strcmp(buf, "TERMINATE") == 0) {
7723 wpa_supplicant_terminate_proc(global);
7724 } else if (os_strcmp(buf, "SUSPEND") == 0) {
7725 wpas_notify_suspend(global);
7726 } else if (os_strcmp(buf, "RESUME") == 0) {
7727 wpas_notify_resume(global);
7728 } else if (os_strncmp(buf, "SET ", 4) == 0) {
7729 if (wpas_global_ctrl_iface_set(global, buf + 4)) {
7730 #ifdef CONFIG_P2P
7731 if (global->p2p_init_wpa_s) {
7732 os_free(reply);
7733 /* Check if P2P redirection would work for this
7734 * command. */
7735 return wpa_supplicant_ctrl_iface_process(
7736 global->p2p_init_wpa_s,
7737 buf, resp_len);
7738 }
7739 #endif /* CONFIG_P2P */
7740 reply_len = -1;
7741 }
7742 #ifndef CONFIG_NO_CONFIG_WRITE
7743 } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
7744 if (wpas_global_ctrl_iface_save_config(global))
7745 reply_len = -1;
7746 #endif /* CONFIG_NO_CONFIG_WRITE */
7747 } else if (os_strcmp(buf, "STATUS") == 0) {
7748 reply_len = wpas_global_ctrl_iface_status(global, reply,
7749 reply_size);
7750 #ifdef CONFIG_MODULE_TESTS
7751 } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
7752 int wpas_module_tests(void);
7753 if (wpas_module_tests() < 0)
7754 reply_len = -1;
7755 #endif /* CONFIG_MODULE_TESTS */
7756 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
7757 if (wpa_debug_reopen_file() < 0)
7758 reply_len = -1;
7759 } else {
7760 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
7761 reply_len = 16;
7762 }
7763
7764 if (reply_len < 0) {
7765 os_memcpy(reply, "FAIL\n", 5);
7766 reply_len = 5;
7767 }
7768
7769 *resp_len = reply_len;
7770 return reply;
7771 }