]> git.ipfire.org Git - thirdparty/hostap.git/blob - wpa_supplicant/ctrl_iface.c
wpa_supplicant: Add bss_flush command to invalidate scan results
[thirdparty/hostap.git] / wpa_supplicant / ctrl_iface.c
1 /*
2 * WPA Supplicant / Control interface (shared code for all backends)
3 * Copyright (c) 2004-2012, 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
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "common/version.h"
14 #include "common/ieee802_11_defs.h"
15 #include "common/wpa_ctrl.h"
16 #include "eap_peer/eap.h"
17 #include "eapol_supp/eapol_supp_sm.h"
18 #include "rsn_supp/wpa.h"
19 #include "rsn_supp/preauth.h"
20 #include "rsn_supp/pmksa_cache.h"
21 #include "l2_packet/l2_packet.h"
22 #include "wps/wps.h"
23 #include "config.h"
24 #include "wpa_supplicant_i.h"
25 #include "driver_i.h"
26 #include "wps_supplicant.h"
27 #include "ibss_rsn.h"
28 #include "ap.h"
29 #include "p2p_supplicant.h"
30 #include "p2p/p2p.h"
31 #include "hs20_supplicant.h"
32 #include "notify.h"
33 #include "bss.h"
34 #include "scan.h"
35 #include "ctrl_iface.h"
36 #include "interworking.h"
37 #include "blacklist.h"
38 #include "wpas_glue.h"
39 #include "autoscan.h"
40
41 extern struct wpa_driver_ops *wpa_drivers[];
42
43 static int wpa_supplicant_global_iface_list(struct wpa_global *global,
44 char *buf, int len);
45 static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
46 char *buf, int len);
47
48
49 static int pno_start(struct wpa_supplicant *wpa_s)
50 {
51 int ret;
52 size_t i, num_ssid;
53 struct wpa_ssid *ssid;
54 struct wpa_driver_scan_params params;
55
56 if (wpa_s->pno)
57 return 0;
58
59 os_memset(&params, 0, sizeof(params));
60
61 num_ssid = 0;
62 ssid = wpa_s->conf->ssid;
63 while (ssid) {
64 if (!wpas_network_disabled(wpa_s, ssid))
65 num_ssid++;
66 ssid = ssid->next;
67 }
68 if (num_ssid > WPAS_MAX_SCAN_SSIDS) {
69 wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from "
70 "%u", WPAS_MAX_SCAN_SSIDS, (unsigned int) num_ssid);
71 num_ssid = WPAS_MAX_SCAN_SSIDS;
72 }
73
74 if (num_ssid == 0) {
75 wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
76 return -1;
77 }
78
79 params.filter_ssids = os_malloc(sizeof(struct wpa_driver_scan_filter) *
80 num_ssid);
81 if (params.filter_ssids == NULL)
82 return -1;
83 i = 0;
84 ssid = wpa_s->conf->ssid;
85 while (ssid) {
86 if (!wpas_network_disabled(wpa_s, ssid)) {
87 params.ssids[i].ssid = ssid->ssid;
88 params.ssids[i].ssid_len = ssid->ssid_len;
89 params.num_ssids++;
90 os_memcpy(params.filter_ssids[i].ssid, ssid->ssid,
91 ssid->ssid_len);
92 params.filter_ssids[i].ssid_len = ssid->ssid_len;
93 params.num_filter_ssids++;
94 i++;
95 if (i == num_ssid)
96 break;
97 }
98 ssid = ssid->next;
99 }
100
101 if (wpa_s->conf->filter_rssi)
102 params.filter_rssi = wpa_s->conf->filter_rssi;
103
104 ret = wpa_drv_sched_scan(wpa_s, &params, 10 * 1000);
105 os_free(params.filter_ssids);
106 if (ret == 0)
107 wpa_s->pno = 1;
108 return ret;
109 }
110
111
112 static int pno_stop(struct wpa_supplicant *wpa_s)
113 {
114 if (wpa_s->pno) {
115 wpa_s->pno = 0;
116 return wpa_drv_stop_sched_scan(wpa_s);
117 }
118 return 0;
119 }
120
121
122 static int set_bssid_filter(struct wpa_supplicant *wpa_s, char *val)
123 {
124 char *pos;
125 u8 addr[ETH_ALEN], *filter = NULL, *n;
126 size_t count = 0;
127
128 pos = val;
129 while (pos) {
130 if (*pos == '\0')
131 break;
132 if (hwaddr_aton(pos, addr)) {
133 os_free(filter);
134 return -1;
135 }
136 n = os_realloc_array(filter, count + 1, ETH_ALEN);
137 if (n == NULL) {
138 os_free(filter);
139 return -1;
140 }
141 filter = n;
142 os_memcpy(filter + count * ETH_ALEN, addr, ETH_ALEN);
143 count++;
144
145 pos = os_strchr(pos, ' ');
146 if (pos)
147 pos++;
148 }
149
150 wpa_hexdump(MSG_DEBUG, "bssid_filter", filter, count * ETH_ALEN);
151 os_free(wpa_s->bssid_filter);
152 wpa_s->bssid_filter = filter;
153 wpa_s->bssid_filter_count = count;
154
155 return 0;
156 }
157
158
159 static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
160 char *cmd)
161 {
162 char *value;
163 int ret = 0;
164
165 value = os_strchr(cmd, ' ');
166 if (value == NULL)
167 return -1;
168 *value++ = '\0';
169
170 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
171 if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
172 eapol_sm_configure(wpa_s->eapol,
173 atoi(value), -1, -1, -1);
174 } else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
175 eapol_sm_configure(wpa_s->eapol,
176 -1, atoi(value), -1, -1);
177 } else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
178 eapol_sm_configure(wpa_s->eapol,
179 -1, -1, atoi(value), -1);
180 } else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
181 eapol_sm_configure(wpa_s->eapol,
182 -1, -1, -1, atoi(value));
183 } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
184 if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
185 atoi(value)))
186 ret = -1;
187 } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
188 0) {
189 if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
190 atoi(value)))
191 ret = -1;
192 } else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
193 if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, atoi(value)))
194 ret = -1;
195 } else if (os_strcasecmp(cmd, "wps_fragment_size") == 0) {
196 wpa_s->wps_fragment_size = atoi(value);
197 #ifdef CONFIG_WPS_TESTING
198 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
199 long int val;
200 val = strtol(value, NULL, 0);
201 if (val < 0 || val > 0xff) {
202 ret = -1;
203 wpa_printf(MSG_DEBUG, "WPS: Invalid "
204 "wps_version_number %ld", val);
205 } else {
206 wps_version_number = val;
207 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
208 "version %u.%u",
209 (wps_version_number & 0xf0) >> 4,
210 wps_version_number & 0x0f);
211 }
212 } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
213 wps_testing_dummy_cred = atoi(value);
214 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
215 wps_testing_dummy_cred);
216 #endif /* CONFIG_WPS_TESTING */
217 } else if (os_strcasecmp(cmd, "ampdu") == 0) {
218 if (wpa_drv_ampdu(wpa_s, atoi(value)) < 0)
219 ret = -1;
220 #ifdef CONFIG_TDLS_TESTING
221 } else if (os_strcasecmp(cmd, "tdls_testing") == 0) {
222 extern unsigned int tdls_testing;
223 tdls_testing = strtol(value, NULL, 0);
224 wpa_printf(MSG_DEBUG, "TDLS: tdls_testing=0x%x", tdls_testing);
225 #endif /* CONFIG_TDLS_TESTING */
226 #ifdef CONFIG_TDLS
227 } else if (os_strcasecmp(cmd, "tdls_disabled") == 0) {
228 int disabled = atoi(value);
229 wpa_printf(MSG_DEBUG, "TDLS: tdls_disabled=%d", disabled);
230 if (disabled) {
231 if (wpa_drv_tdls_oper(wpa_s, TDLS_DISABLE, NULL) < 0)
232 ret = -1;
233 } else if (wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL) < 0)
234 ret = -1;
235 wpa_tdls_enable(wpa_s->wpa, !disabled);
236 #endif /* CONFIG_TDLS */
237 } else if (os_strcasecmp(cmd, "pno") == 0) {
238 if (atoi(value))
239 ret = pno_start(wpa_s);
240 else
241 ret = pno_stop(wpa_s);
242 } else if (os_strcasecmp(cmd, "radio_disabled") == 0) {
243 int disabled = atoi(value);
244 if (wpa_drv_radio_disable(wpa_s, disabled) < 0)
245 ret = -1;
246 else if (disabled)
247 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
248 } else if (os_strcasecmp(cmd, "uapsd") == 0) {
249 if (os_strcmp(value, "disable") == 0)
250 wpa_s->set_sta_uapsd = 0;
251 else {
252 int be, bk, vi, vo;
253 char *pos;
254 /* format: BE,BK,VI,VO;max SP Length */
255 be = atoi(value);
256 pos = os_strchr(value, ',');
257 if (pos == NULL)
258 return -1;
259 pos++;
260 bk = atoi(pos);
261 pos = os_strchr(pos, ',');
262 if (pos == NULL)
263 return -1;
264 pos++;
265 vi = atoi(pos);
266 pos = os_strchr(pos, ',');
267 if (pos == NULL)
268 return -1;
269 pos++;
270 vo = atoi(pos);
271 /* ignore max SP Length for now */
272
273 wpa_s->set_sta_uapsd = 1;
274 wpa_s->sta_uapsd = 0;
275 if (be)
276 wpa_s->sta_uapsd |= BIT(0);
277 if (bk)
278 wpa_s->sta_uapsd |= BIT(1);
279 if (vi)
280 wpa_s->sta_uapsd |= BIT(2);
281 if (vo)
282 wpa_s->sta_uapsd |= BIT(3);
283 }
284 } else if (os_strcasecmp(cmd, "ps") == 0) {
285 ret = wpa_drv_set_p2p_powersave(wpa_s, atoi(value), -1, -1);
286 } else if (os_strcasecmp(cmd, "bssid_filter") == 0) {
287 ret = set_bssid_filter(wpa_s, value);
288 } else {
289 value[-1] = '=';
290 ret = wpa_config_process_global(wpa_s->conf, cmd, -1);
291 if (ret == 0)
292 wpa_supplicant_update_config(wpa_s);
293 }
294
295 return ret;
296 }
297
298
299 static int wpa_supplicant_ctrl_iface_get(struct wpa_supplicant *wpa_s,
300 char *cmd, char *buf, size_t buflen)
301 {
302 int res = -1;
303
304 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
305
306 if (os_strcmp(cmd, "version") == 0) {
307 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
308 } else if (os_strcasecmp(cmd, "country") == 0) {
309 if (wpa_s->conf->country[0] && wpa_s->conf->country[1])
310 res = os_snprintf(buf, buflen, "%c%c",
311 wpa_s->conf->country[0],
312 wpa_s->conf->country[1]);
313 }
314
315 if (res < 0 || (unsigned int) res >= buflen)
316 return -1;
317 return res;
318 }
319
320
321 #ifdef IEEE8021X_EAPOL
322 static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
323 char *addr)
324 {
325 u8 bssid[ETH_ALEN];
326 struct wpa_ssid *ssid = wpa_s->current_ssid;
327
328 if (hwaddr_aton(addr, bssid)) {
329 wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
330 "'%s'", addr);
331 return -1;
332 }
333
334 wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
335 rsn_preauth_deinit(wpa_s->wpa);
336 if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
337 return -1;
338
339 return 0;
340 }
341 #endif /* IEEE8021X_EAPOL */
342
343
344 #ifdef CONFIG_PEERKEY
345 /* MLME-STKSTART.request(peer) */
346 static int wpa_supplicant_ctrl_iface_stkstart(
347 struct wpa_supplicant *wpa_s, char *addr)
348 {
349 u8 peer[ETH_ALEN];
350
351 if (hwaddr_aton(addr, peer)) {
352 wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART: invalid "
353 "address '%s'", addr);
354 return -1;
355 }
356
357 wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART " MACSTR,
358 MAC2STR(peer));
359
360 return wpa_sm_stkstart(wpa_s->wpa, peer);
361 }
362 #endif /* CONFIG_PEERKEY */
363
364
365 #ifdef CONFIG_TDLS
366
367 static int wpa_supplicant_ctrl_iface_tdls_discover(
368 struct wpa_supplicant *wpa_s, char *addr)
369 {
370 u8 peer[ETH_ALEN];
371 int ret;
372
373 if (hwaddr_aton(addr, peer)) {
374 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER: invalid "
375 "address '%s'", addr);
376 return -1;
377 }
378
379 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER " MACSTR,
380 MAC2STR(peer));
381
382 if (wpa_tdls_is_external_setup(wpa_s->wpa))
383 ret = wpa_tdls_send_discovery_request(wpa_s->wpa, peer);
384 else
385 ret = wpa_drv_tdls_oper(wpa_s, TDLS_DISCOVERY_REQ, peer);
386
387 return ret;
388 }
389
390
391 static int wpa_supplicant_ctrl_iface_tdls_setup(
392 struct wpa_supplicant *wpa_s, char *addr)
393 {
394 u8 peer[ETH_ALEN];
395 int ret;
396
397 if (hwaddr_aton(addr, peer)) {
398 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP: invalid "
399 "address '%s'", addr);
400 return -1;
401 }
402
403 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP " MACSTR,
404 MAC2STR(peer));
405
406 ret = wpa_tdls_reneg(wpa_s->wpa, peer);
407 if (ret) {
408 if (wpa_tdls_is_external_setup(wpa_s->wpa))
409 ret = wpa_tdls_start(wpa_s->wpa, peer);
410 else
411 ret = wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
412 }
413
414 return ret;
415 }
416
417
418 static int wpa_supplicant_ctrl_iface_tdls_teardown(
419 struct wpa_supplicant *wpa_s, char *addr)
420 {
421 u8 peer[ETH_ALEN];
422
423 if (hwaddr_aton(addr, peer)) {
424 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN: invalid "
425 "address '%s'", addr);
426 return -1;
427 }
428
429 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN " MACSTR,
430 MAC2STR(peer));
431
432 return wpa_tdls_teardown_link(wpa_s->wpa, peer,
433 WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
434 }
435
436 #endif /* CONFIG_TDLS */
437
438
439 #ifdef CONFIG_IEEE80211R
440 static int wpa_supplicant_ctrl_iface_ft_ds(
441 struct wpa_supplicant *wpa_s, char *addr)
442 {
443 u8 target_ap[ETH_ALEN];
444 struct wpa_bss *bss;
445 const u8 *mdie;
446
447 if (hwaddr_aton(addr, target_ap)) {
448 wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
449 "address '%s'", addr);
450 return -1;
451 }
452
453 wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
454
455 bss = wpa_bss_get_bssid(wpa_s, target_ap);
456 if (bss)
457 mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
458 else
459 mdie = NULL;
460
461 return wpa_ft_start_over_ds(wpa_s->wpa, target_ap, mdie);
462 }
463 #endif /* CONFIG_IEEE80211R */
464
465
466 #ifdef CONFIG_WPS
467 static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
468 char *cmd)
469 {
470 u8 bssid[ETH_ALEN], *_bssid = bssid;
471 #ifdef CONFIG_P2P
472 u8 p2p_dev_addr[ETH_ALEN];
473 #endif /* CONFIG_P2P */
474 #ifdef CONFIG_AP
475 u8 *_p2p_dev_addr = NULL;
476 #endif /* CONFIG_AP */
477
478 if (cmd == NULL || os_strcmp(cmd, "any") == 0) {
479 _bssid = NULL;
480 #ifdef CONFIG_P2P
481 } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
482 if (hwaddr_aton(cmd + 13, p2p_dev_addr)) {
483 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid "
484 "P2P Device Address '%s'",
485 cmd + 13);
486 return -1;
487 }
488 _p2p_dev_addr = p2p_dev_addr;
489 #endif /* CONFIG_P2P */
490 } else if (hwaddr_aton(cmd, bssid)) {
491 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid BSSID '%s'",
492 cmd);
493 return -1;
494 }
495
496 #ifdef CONFIG_AP
497 if (wpa_s->ap_iface)
498 return wpa_supplicant_ap_wps_pbc(wpa_s, _bssid, _p2p_dev_addr);
499 #endif /* CONFIG_AP */
500
501 return wpas_wps_start_pbc(wpa_s, _bssid, 0);
502 }
503
504
505 static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
506 char *cmd, char *buf,
507 size_t buflen)
508 {
509 u8 bssid[ETH_ALEN], *_bssid = bssid;
510 char *pin;
511 int ret;
512
513 pin = os_strchr(cmd, ' ');
514 if (pin)
515 *pin++ = '\0';
516
517 if (os_strcmp(cmd, "any") == 0)
518 _bssid = NULL;
519 else if (os_strcmp(cmd, "get") == 0) {
520 ret = wps_generate_pin();
521 goto done;
522 } else if (hwaddr_aton(cmd, bssid)) {
523 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PIN: invalid BSSID '%s'",
524 cmd);
525 return -1;
526 }
527
528 #ifdef CONFIG_AP
529 if (wpa_s->ap_iface)
530 return wpa_supplicant_ap_wps_pin(wpa_s, _bssid, pin,
531 buf, buflen);
532 #endif /* CONFIG_AP */
533
534 if (pin) {
535 ret = wpas_wps_start_pin(wpa_s, _bssid, pin, 0,
536 DEV_PW_DEFAULT);
537 if (ret < 0)
538 return -1;
539 ret = os_snprintf(buf, buflen, "%s", pin);
540 if (ret < 0 || (size_t) ret >= buflen)
541 return -1;
542 return ret;
543 }
544
545 ret = wpas_wps_start_pin(wpa_s, _bssid, NULL, 0, DEV_PW_DEFAULT);
546 if (ret < 0)
547 return -1;
548
549 done:
550 /* Return the generated PIN */
551 ret = os_snprintf(buf, buflen, "%08d", ret);
552 if (ret < 0 || (size_t) ret >= buflen)
553 return -1;
554 return ret;
555 }
556
557
558 static int wpa_supplicant_ctrl_iface_wps_check_pin(
559 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
560 {
561 char pin[9];
562 size_t len;
563 char *pos;
564 int ret;
565
566 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
567 (u8 *) cmd, os_strlen(cmd));
568 for (pos = cmd, len = 0; *pos != '\0'; pos++) {
569 if (*pos < '0' || *pos > '9')
570 continue;
571 pin[len++] = *pos;
572 if (len == 9) {
573 wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
574 return -1;
575 }
576 }
577 if (len != 4 && len != 8) {
578 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
579 return -1;
580 }
581 pin[len] = '\0';
582
583 if (len == 8) {
584 unsigned int pin_val;
585 pin_val = atoi(pin);
586 if (!wps_pin_valid(pin_val)) {
587 wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
588 ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
589 if (ret < 0 || (size_t) ret >= buflen)
590 return -1;
591 return ret;
592 }
593 }
594
595 ret = os_snprintf(buf, buflen, "%s", pin);
596 if (ret < 0 || (size_t) ret >= buflen)
597 return -1;
598
599 return ret;
600 }
601
602
603 #ifdef CONFIG_WPS_OOB
604 static int wpa_supplicant_ctrl_iface_wps_oob(struct wpa_supplicant *wpa_s,
605 char *cmd)
606 {
607 char *path, *method, *name;
608
609 path = os_strchr(cmd, ' ');
610 if (path == NULL)
611 return -1;
612 *path++ = '\0';
613
614 method = os_strchr(path, ' ');
615 if (method == NULL)
616 return -1;
617 *method++ = '\0';
618
619 name = os_strchr(method, ' ');
620 if (name != NULL)
621 *name++ = '\0';
622
623 return wpas_wps_start_oob(wpa_s, cmd, path, method, name);
624 }
625 #endif /* CONFIG_WPS_OOB */
626
627
628 #ifdef CONFIG_WPS_NFC
629
630 static int wpa_supplicant_ctrl_iface_wps_nfc(struct wpa_supplicant *wpa_s,
631 char *cmd)
632 {
633 u8 bssid[ETH_ALEN], *_bssid = bssid;
634
635 if (cmd == NULL || cmd[0] == '\0')
636 _bssid = NULL;
637 else if (hwaddr_aton(cmd, bssid))
638 return -1;
639
640 return wpas_wps_start_nfc(wpa_s, _bssid);
641 }
642
643
644 static int wpa_supplicant_ctrl_iface_wps_nfc_token(
645 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
646 {
647 int ndef;
648 struct wpabuf *buf;
649 int res;
650
651 if (os_strcmp(cmd, "WPS") == 0)
652 ndef = 0;
653 else if (os_strcmp(cmd, "NDEF") == 0)
654 ndef = 1;
655 else
656 return -1;
657
658 buf = wpas_wps_nfc_token(wpa_s, ndef);
659 if (buf == NULL)
660 return -1;
661
662 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
663 wpabuf_len(buf));
664 reply[res++] = '\n';
665 reply[res] = '\0';
666
667 wpabuf_free(buf);
668
669 return res;
670 }
671
672
673 static int wpa_supplicant_ctrl_iface_wps_nfc_tag_read(
674 struct wpa_supplicant *wpa_s, char *pos)
675 {
676 size_t len;
677 struct wpabuf *buf;
678 int ret;
679
680 len = os_strlen(pos);
681 if (len & 0x01)
682 return -1;
683 len /= 2;
684
685 buf = wpabuf_alloc(len);
686 if (buf == NULL)
687 return -1;
688 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
689 wpabuf_free(buf);
690 return -1;
691 }
692
693 ret = wpas_wps_nfc_tag_read(wpa_s, buf);
694 wpabuf_free(buf);
695
696 return ret;
697 }
698
699 #endif /* CONFIG_WPS_NFC */
700
701
702 static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
703 char *cmd)
704 {
705 u8 bssid[ETH_ALEN];
706 char *pin;
707 char *new_ssid;
708 char *new_auth;
709 char *new_encr;
710 char *new_key;
711 struct wps_new_ap_settings ap;
712
713 pin = os_strchr(cmd, ' ');
714 if (pin == NULL)
715 return -1;
716 *pin++ = '\0';
717
718 if (hwaddr_aton(cmd, bssid)) {
719 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
720 cmd);
721 return -1;
722 }
723
724 new_ssid = os_strchr(pin, ' ');
725 if (new_ssid == NULL)
726 return wpas_wps_start_reg(wpa_s, bssid, pin, NULL);
727 *new_ssid++ = '\0';
728
729 new_auth = os_strchr(new_ssid, ' ');
730 if (new_auth == NULL)
731 return -1;
732 *new_auth++ = '\0';
733
734 new_encr = os_strchr(new_auth, ' ');
735 if (new_encr == NULL)
736 return -1;
737 *new_encr++ = '\0';
738
739 new_key = os_strchr(new_encr, ' ');
740 if (new_key == NULL)
741 return -1;
742 *new_key++ = '\0';
743
744 os_memset(&ap, 0, sizeof(ap));
745 ap.ssid_hex = new_ssid;
746 ap.auth = new_auth;
747 ap.encr = new_encr;
748 ap.key_hex = new_key;
749 return wpas_wps_start_reg(wpa_s, bssid, pin, &ap);
750 }
751
752
753 #ifdef CONFIG_AP
754 static int wpa_supplicant_ctrl_iface_wps_ap_pin(struct wpa_supplicant *wpa_s,
755 char *cmd, char *buf,
756 size_t buflen)
757 {
758 int timeout = 300;
759 char *pos;
760 const char *pin_txt;
761
762 if (!wpa_s->ap_iface)
763 return -1;
764
765 pos = os_strchr(cmd, ' ');
766 if (pos)
767 *pos++ = '\0';
768
769 if (os_strcmp(cmd, "disable") == 0) {
770 wpas_wps_ap_pin_disable(wpa_s);
771 return os_snprintf(buf, buflen, "OK\n");
772 }
773
774 if (os_strcmp(cmd, "random") == 0) {
775 if (pos)
776 timeout = atoi(pos);
777 pin_txt = wpas_wps_ap_pin_random(wpa_s, timeout);
778 if (pin_txt == NULL)
779 return -1;
780 return os_snprintf(buf, buflen, "%s", pin_txt);
781 }
782
783 if (os_strcmp(cmd, "get") == 0) {
784 pin_txt = wpas_wps_ap_pin_get(wpa_s);
785 if (pin_txt == NULL)
786 return -1;
787 return os_snprintf(buf, buflen, "%s", pin_txt);
788 }
789
790 if (os_strcmp(cmd, "set") == 0) {
791 char *pin;
792 if (pos == NULL)
793 return -1;
794 pin = pos;
795 pos = os_strchr(pos, ' ');
796 if (pos) {
797 *pos++ = '\0';
798 timeout = atoi(pos);
799 }
800 if (os_strlen(pin) > buflen)
801 return -1;
802 if (wpas_wps_ap_pin_set(wpa_s, pin, timeout) < 0)
803 return -1;
804 return os_snprintf(buf, buflen, "%s", pin);
805 }
806
807 return -1;
808 }
809 #endif /* CONFIG_AP */
810
811
812 #ifdef CONFIG_WPS_ER
813 static int wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant *wpa_s,
814 char *cmd)
815 {
816 char *uuid = cmd, *pin, *pos;
817 u8 addr_buf[ETH_ALEN], *addr = NULL;
818 pin = os_strchr(uuid, ' ');
819 if (pin == NULL)
820 return -1;
821 *pin++ = '\0';
822 pos = os_strchr(pin, ' ');
823 if (pos) {
824 *pos++ = '\0';
825 if (hwaddr_aton(pos, addr_buf) == 0)
826 addr = addr_buf;
827 }
828 return wpas_wps_er_add_pin(wpa_s, addr, uuid, pin);
829 }
830
831
832 static int wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant *wpa_s,
833 char *cmd)
834 {
835 char *uuid = cmd, *pin;
836 pin = os_strchr(uuid, ' ');
837 if (pin == NULL)
838 return -1;
839 *pin++ = '\0';
840 return wpas_wps_er_learn(wpa_s, uuid, pin);
841 }
842
843
844 static int wpa_supplicant_ctrl_iface_wps_er_set_config(
845 struct wpa_supplicant *wpa_s, char *cmd)
846 {
847 char *uuid = cmd, *id;
848 id = os_strchr(uuid, ' ');
849 if (id == NULL)
850 return -1;
851 *id++ = '\0';
852 return wpas_wps_er_set_config(wpa_s, uuid, atoi(id));
853 }
854
855
856 static int wpa_supplicant_ctrl_iface_wps_er_config(
857 struct wpa_supplicant *wpa_s, char *cmd)
858 {
859 char *pin;
860 char *new_ssid;
861 char *new_auth;
862 char *new_encr;
863 char *new_key;
864 struct wps_new_ap_settings ap;
865
866 pin = os_strchr(cmd, ' ');
867 if (pin == NULL)
868 return -1;
869 *pin++ = '\0';
870
871 new_ssid = os_strchr(pin, ' ');
872 if (new_ssid == NULL)
873 return -1;
874 *new_ssid++ = '\0';
875
876 new_auth = os_strchr(new_ssid, ' ');
877 if (new_auth == NULL)
878 return -1;
879 *new_auth++ = '\0';
880
881 new_encr = os_strchr(new_auth, ' ');
882 if (new_encr == NULL)
883 return -1;
884 *new_encr++ = '\0';
885
886 new_key = os_strchr(new_encr, ' ');
887 if (new_key == NULL)
888 return -1;
889 *new_key++ = '\0';
890
891 os_memset(&ap, 0, sizeof(ap));
892 ap.ssid_hex = new_ssid;
893 ap.auth = new_auth;
894 ap.encr = new_encr;
895 ap.key_hex = new_key;
896 return wpas_wps_er_config(wpa_s, cmd, pin, &ap);
897 }
898
899
900 #ifdef CONFIG_WPS_NFC
901 static int wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
902 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
903 {
904 int ndef;
905 struct wpabuf *buf;
906 int res;
907 char *uuid;
908
909 uuid = os_strchr(cmd, ' ');
910 if (uuid == NULL)
911 return -1;
912 *uuid++ = '\0';
913
914 if (os_strcmp(cmd, "WPS") == 0)
915 ndef = 0;
916 else if (os_strcmp(cmd, "NDEF") == 0)
917 ndef = 1;
918 else
919 return -1;
920
921 buf = wpas_wps_er_nfc_config_token(wpa_s, ndef, uuid);
922 if (buf == NULL)
923 return -1;
924
925 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
926 wpabuf_len(buf));
927 reply[res++] = '\n';
928 reply[res] = '\0';
929
930 wpabuf_free(buf);
931
932 return res;
933 }
934 #endif /* CONFIG_WPS_NFC */
935 #endif /* CONFIG_WPS_ER */
936
937 #endif /* CONFIG_WPS */
938
939
940 #ifdef CONFIG_IBSS_RSN
941 static int wpa_supplicant_ctrl_iface_ibss_rsn(
942 struct wpa_supplicant *wpa_s, char *addr)
943 {
944 u8 peer[ETH_ALEN];
945
946 if (hwaddr_aton(addr, peer)) {
947 wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
948 "address '%s'", addr);
949 return -1;
950 }
951
952 wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
953 MAC2STR(peer));
954
955 return ibss_rsn_start(wpa_s->ibss_rsn, peer);
956 }
957 #endif /* CONFIG_IBSS_RSN */
958
959
960 static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
961 char *rsp)
962 {
963 #ifdef IEEE8021X_EAPOL
964 char *pos, *id_pos;
965 int id;
966 struct wpa_ssid *ssid;
967
968 pos = os_strchr(rsp, '-');
969 if (pos == NULL)
970 return -1;
971 *pos++ = '\0';
972 id_pos = pos;
973 pos = os_strchr(pos, ':');
974 if (pos == NULL)
975 return -1;
976 *pos++ = '\0';
977 id = atoi(id_pos);
978 wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
979 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
980 (u8 *) pos, os_strlen(pos));
981
982 ssid = wpa_config_get_network(wpa_s->conf, id);
983 if (ssid == NULL) {
984 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
985 "to update", id);
986 return -1;
987 }
988
989 return wpa_supplicant_ctrl_iface_ctrl_rsp_handle(wpa_s, ssid, rsp,
990 pos);
991 #else /* IEEE8021X_EAPOL */
992 wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
993 return -1;
994 #endif /* IEEE8021X_EAPOL */
995 }
996
997
998 static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
999 const char *params,
1000 char *buf, size_t buflen)
1001 {
1002 char *pos, *end, tmp[30];
1003 int res, verbose, wps, ret;
1004
1005 verbose = os_strcmp(params, "-VERBOSE") == 0;
1006 wps = os_strcmp(params, "-WPS") == 0;
1007 pos = buf;
1008 end = buf + buflen;
1009 if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
1010 struct wpa_ssid *ssid = wpa_s->current_ssid;
1011 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
1012 MAC2STR(wpa_s->bssid));
1013 if (ret < 0 || ret >= end - pos)
1014 return pos - buf;
1015 pos += ret;
1016 if (ssid) {
1017 u8 *_ssid = ssid->ssid;
1018 size_t ssid_len = ssid->ssid_len;
1019 u8 ssid_buf[MAX_SSID_LEN];
1020 if (ssid_len == 0) {
1021 int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
1022 if (_res < 0)
1023 ssid_len = 0;
1024 else
1025 ssid_len = _res;
1026 _ssid = ssid_buf;
1027 }
1028 ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
1029 wpa_ssid_txt(_ssid, ssid_len),
1030 ssid->id);
1031 if (ret < 0 || ret >= end - pos)
1032 return pos - buf;
1033 pos += ret;
1034
1035 if (wps && ssid->passphrase &&
1036 wpa_key_mgmt_wpa_psk(ssid->key_mgmt) &&
1037 (ssid->mode == WPAS_MODE_AP ||
1038 ssid->mode == WPAS_MODE_P2P_GO)) {
1039 ret = os_snprintf(pos, end - pos,
1040 "passphrase=%s\n",
1041 ssid->passphrase);
1042 if (ret < 0 || ret >= end - pos)
1043 return pos - buf;
1044 pos += ret;
1045 }
1046 if (ssid->id_str) {
1047 ret = os_snprintf(pos, end - pos,
1048 "id_str=%s\n",
1049 ssid->id_str);
1050 if (ret < 0 || ret >= end - pos)
1051 return pos - buf;
1052 pos += ret;
1053 }
1054
1055 switch (ssid->mode) {
1056 case WPAS_MODE_INFRA:
1057 ret = os_snprintf(pos, end - pos,
1058 "mode=station\n");
1059 break;
1060 case WPAS_MODE_IBSS:
1061 ret = os_snprintf(pos, end - pos,
1062 "mode=IBSS\n");
1063 break;
1064 case WPAS_MODE_AP:
1065 ret = os_snprintf(pos, end - pos,
1066 "mode=AP\n");
1067 break;
1068 case WPAS_MODE_P2P_GO:
1069 ret = os_snprintf(pos, end - pos,
1070 "mode=P2P GO\n");
1071 break;
1072 case WPAS_MODE_P2P_GROUP_FORMATION:
1073 ret = os_snprintf(pos, end - pos,
1074 "mode=P2P GO - group "
1075 "formation\n");
1076 break;
1077 default:
1078 ret = 0;
1079 break;
1080 }
1081 if (ret < 0 || ret >= end - pos)
1082 return pos - buf;
1083 pos += ret;
1084 }
1085
1086 #ifdef CONFIG_AP
1087 if (wpa_s->ap_iface) {
1088 pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
1089 end - pos,
1090 verbose);
1091 } else
1092 #endif /* CONFIG_AP */
1093 pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
1094 }
1095 ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
1096 wpa_supplicant_state_txt(wpa_s->wpa_state));
1097 if (ret < 0 || ret >= end - pos)
1098 return pos - buf;
1099 pos += ret;
1100
1101 if (wpa_s->l2 &&
1102 l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
1103 ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
1104 if (ret < 0 || ret >= end - pos)
1105 return pos - buf;
1106 pos += ret;
1107 }
1108
1109 #ifdef CONFIG_P2P
1110 if (wpa_s->global->p2p) {
1111 ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
1112 "\n", MAC2STR(wpa_s->global->p2p_dev_addr));
1113 if (ret < 0 || ret >= end - pos)
1114 return pos - buf;
1115 pos += ret;
1116 }
1117 #endif /* CONFIG_P2P */
1118
1119 ret = os_snprintf(pos, end - pos, "address=" MACSTR "\n",
1120 MAC2STR(wpa_s->own_addr));
1121 if (ret < 0 || ret >= end - pos)
1122 return pos - buf;
1123 pos += ret;
1124
1125 #ifdef CONFIG_HS20
1126 if (wpa_s->current_bss &&
1127 wpa_bss_get_vendor_ie(wpa_s->current_bss, HS20_IE_VENDOR_TYPE) &&
1128 wpa_s->wpa_proto == WPA_PROTO_RSN &&
1129 wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1130 ret = os_snprintf(pos, end - pos, "hs20=1\n");
1131 if (ret < 0 || ret >= end - pos)
1132 return pos - buf;
1133 pos += ret;
1134 }
1135 #endif /* CONFIG_HS20 */
1136
1137 if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
1138 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1139 res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
1140 verbose);
1141 if (res >= 0)
1142 pos += res;
1143 }
1144
1145 res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
1146 if (res >= 0)
1147 pos += res;
1148
1149 return pos - buf;
1150 }
1151
1152
1153 static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
1154 char *cmd)
1155 {
1156 char *pos;
1157 int id;
1158 struct wpa_ssid *ssid;
1159 u8 bssid[ETH_ALEN];
1160
1161 /* cmd: "<network id> <BSSID>" */
1162 pos = os_strchr(cmd, ' ');
1163 if (pos == NULL)
1164 return -1;
1165 *pos++ = '\0';
1166 id = atoi(cmd);
1167 wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
1168 if (hwaddr_aton(pos, bssid)) {
1169 wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
1170 return -1;
1171 }
1172
1173 ssid = wpa_config_get_network(wpa_s->conf, id);
1174 if (ssid == NULL) {
1175 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
1176 "to update", id);
1177 return -1;
1178 }
1179
1180 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
1181 ssid->bssid_set = !is_zero_ether_addr(bssid);
1182
1183 return 0;
1184 }
1185
1186
1187 static int wpa_supplicant_ctrl_iface_blacklist(struct wpa_supplicant *wpa_s,
1188 char *cmd, char *buf,
1189 size_t buflen)
1190 {
1191 u8 bssid[ETH_ALEN];
1192 struct wpa_blacklist *e;
1193 char *pos, *end;
1194 int ret;
1195
1196 /* cmd: "BLACKLIST [<BSSID>]" */
1197 if (*cmd == '\0') {
1198 pos = buf;
1199 end = buf + buflen;
1200 e = wpa_s->blacklist;
1201 while (e) {
1202 ret = os_snprintf(pos, end - pos, MACSTR "\n",
1203 MAC2STR(e->bssid));
1204 if (ret < 0 || ret >= end - pos)
1205 return pos - buf;
1206 pos += ret;
1207 e = e->next;
1208 }
1209 return pos - buf;
1210 }
1211
1212 cmd++;
1213 if (os_strncmp(cmd, "clear", 5) == 0) {
1214 wpa_blacklist_clear(wpa_s);
1215 os_memcpy(buf, "OK\n", 3);
1216 return 3;
1217 }
1218
1219 wpa_printf(MSG_DEBUG, "CTRL_IFACE: BLACKLIST bssid='%s'", cmd);
1220 if (hwaddr_aton(cmd, bssid)) {
1221 wpa_printf(MSG_DEBUG, "CTRL_IFACE: invalid BSSID '%s'", cmd);
1222 return -1;
1223 }
1224
1225 /*
1226 * Add the BSSID twice, so its count will be 2, causing it to be
1227 * skipped when processing scan results.
1228 */
1229 ret = wpa_blacklist_add(wpa_s, bssid);
1230 if (ret != 0)
1231 return -1;
1232 ret = wpa_blacklist_add(wpa_s, bssid);
1233 if (ret != 0)
1234 return -1;
1235 os_memcpy(buf, "OK\n", 3);
1236 return 3;
1237 }
1238
1239
1240 extern int wpa_debug_level;
1241 extern int wpa_debug_timestamp;
1242
1243 static const char * debug_level_str(int level)
1244 {
1245 switch (level) {
1246 case MSG_EXCESSIVE:
1247 return "EXCESSIVE";
1248 case MSG_MSGDUMP:
1249 return "MSGDUMP";
1250 case MSG_DEBUG:
1251 return "DEBUG";
1252 case MSG_INFO:
1253 return "INFO";
1254 case MSG_WARNING:
1255 return "WARNING";
1256 case MSG_ERROR:
1257 return "ERROR";
1258 default:
1259 return "?";
1260 }
1261 }
1262
1263
1264 static int str_to_debug_level(const char *s)
1265 {
1266 if (os_strcasecmp(s, "EXCESSIVE") == 0)
1267 return MSG_EXCESSIVE;
1268 if (os_strcasecmp(s, "MSGDUMP") == 0)
1269 return MSG_MSGDUMP;
1270 if (os_strcasecmp(s, "DEBUG") == 0)
1271 return MSG_DEBUG;
1272 if (os_strcasecmp(s, "INFO") == 0)
1273 return MSG_INFO;
1274 if (os_strcasecmp(s, "WARNING") == 0)
1275 return MSG_WARNING;
1276 if (os_strcasecmp(s, "ERROR") == 0)
1277 return MSG_ERROR;
1278 return -1;
1279 }
1280
1281
1282 static int wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant *wpa_s,
1283 char *cmd, char *buf,
1284 size_t buflen)
1285 {
1286 char *pos, *end, *stamp;
1287 int ret;
1288
1289 if (cmd == NULL) {
1290 return -1;
1291 }
1292
1293 /* cmd: "LOG_LEVEL [<level>]" */
1294 if (*cmd == '\0') {
1295 pos = buf;
1296 end = buf + buflen;
1297 ret = os_snprintf(pos, end - pos, "Current level: %s\n"
1298 "Timestamp: %d\n",
1299 debug_level_str(wpa_debug_level),
1300 wpa_debug_timestamp);
1301 if (ret < 0 || ret >= end - pos)
1302 ret = 0;
1303
1304 return ret;
1305 }
1306
1307 while (*cmd == ' ')
1308 cmd++;
1309
1310 stamp = os_strchr(cmd, ' ');
1311 if (stamp) {
1312 *stamp++ = '\0';
1313 while (*stamp == ' ') {
1314 stamp++;
1315 }
1316 }
1317
1318 if (cmd && os_strlen(cmd)) {
1319 int level = str_to_debug_level(cmd);
1320 if (level < 0)
1321 return -1;
1322 wpa_debug_level = level;
1323 }
1324
1325 if (stamp && os_strlen(stamp))
1326 wpa_debug_timestamp = atoi(stamp);
1327
1328 os_memcpy(buf, "OK\n", 3);
1329 return 3;
1330 }
1331
1332
1333 static int wpa_supplicant_ctrl_iface_list_networks(
1334 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
1335 {
1336 char *pos, *end;
1337 struct wpa_ssid *ssid;
1338 int ret;
1339
1340 pos = buf;
1341 end = buf + buflen;
1342 ret = os_snprintf(pos, end - pos,
1343 "network id / ssid / bssid / flags\n");
1344 if (ret < 0 || ret >= end - pos)
1345 return pos - buf;
1346 pos += ret;
1347
1348 ssid = wpa_s->conf->ssid;
1349 while (ssid) {
1350 ret = os_snprintf(pos, end - pos, "%d\t%s",
1351 ssid->id,
1352 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1353 if (ret < 0 || ret >= end - pos)
1354 return pos - buf;
1355 pos += ret;
1356 if (ssid->bssid_set) {
1357 ret = os_snprintf(pos, end - pos, "\t" MACSTR,
1358 MAC2STR(ssid->bssid));
1359 } else {
1360 ret = os_snprintf(pos, end - pos, "\tany");
1361 }
1362 if (ret < 0 || ret >= end - pos)
1363 return pos - buf;
1364 pos += ret;
1365 ret = os_snprintf(pos, end - pos, "\t%s%s%s",
1366 ssid == wpa_s->current_ssid ?
1367 "[CURRENT]" : "",
1368 ssid->disabled ? "[DISABLED]" : "",
1369 ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
1370 "");
1371 if (ret < 0 || ret >= end - pos)
1372 return pos - buf;
1373 pos += ret;
1374 ret = os_snprintf(pos, end - pos, "\n");
1375 if (ret < 0 || ret >= end - pos)
1376 return pos - buf;
1377 pos += ret;
1378
1379 ssid = ssid->next;
1380 }
1381
1382 return pos - buf;
1383 }
1384
1385
1386 static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
1387 {
1388 int first = 1, ret;
1389 ret = os_snprintf(pos, end - pos, "-");
1390 if (ret < 0 || ret >= end - pos)
1391 return pos;
1392 pos += ret;
1393 if (cipher & WPA_CIPHER_NONE) {
1394 ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : "+");
1395 if (ret < 0 || ret >= end - pos)
1396 return pos;
1397 pos += ret;
1398 first = 0;
1399 }
1400 if (cipher & WPA_CIPHER_WEP40) {
1401 ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : "+");
1402 if (ret < 0 || ret >= end - pos)
1403 return pos;
1404 pos += ret;
1405 first = 0;
1406 }
1407 if (cipher & WPA_CIPHER_WEP104) {
1408 ret = os_snprintf(pos, end - pos, "%sWEP104",
1409 first ? "" : "+");
1410 if (ret < 0 || ret >= end - pos)
1411 return pos;
1412 pos += ret;
1413 first = 0;
1414 }
1415 if (cipher & WPA_CIPHER_TKIP) {
1416 ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : "+");
1417 if (ret < 0 || ret >= end - pos)
1418 return pos;
1419 pos += ret;
1420 first = 0;
1421 }
1422 if (cipher & WPA_CIPHER_CCMP) {
1423 ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : "+");
1424 if (ret < 0 || ret >= end - pos)
1425 return pos;
1426 pos += ret;
1427 first = 0;
1428 }
1429 return pos;
1430 }
1431
1432
1433 static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
1434 const u8 *ie, size_t ie_len)
1435 {
1436 struct wpa_ie_data data;
1437 int first, ret;
1438
1439 ret = os_snprintf(pos, end - pos, "[%s-", proto);
1440 if (ret < 0 || ret >= end - pos)
1441 return pos;
1442 pos += ret;
1443
1444 if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
1445 ret = os_snprintf(pos, end - pos, "?]");
1446 if (ret < 0 || ret >= end - pos)
1447 return pos;
1448 pos += ret;
1449 return pos;
1450 }
1451
1452 first = 1;
1453 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
1454 ret = os_snprintf(pos, end - pos, "%sEAP", first ? "" : "+");
1455 if (ret < 0 || ret >= end - pos)
1456 return pos;
1457 pos += ret;
1458 first = 0;
1459 }
1460 if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
1461 ret = os_snprintf(pos, end - pos, "%sPSK", first ? "" : "+");
1462 if (ret < 0 || ret >= end - pos)
1463 return pos;
1464 pos += ret;
1465 first = 0;
1466 }
1467 if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
1468 ret = os_snprintf(pos, end - pos, "%sNone", first ? "" : "+");
1469 if (ret < 0 || ret >= end - pos)
1470 return pos;
1471 pos += ret;
1472 first = 0;
1473 }
1474 #ifdef CONFIG_IEEE80211R
1475 if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
1476 ret = os_snprintf(pos, end - pos, "%sFT/EAP",
1477 first ? "" : "+");
1478 if (ret < 0 || ret >= end - pos)
1479 return pos;
1480 pos += ret;
1481 first = 0;
1482 }
1483 if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
1484 ret = os_snprintf(pos, end - pos, "%sFT/PSK",
1485 first ? "" : "+");
1486 if (ret < 0 || ret >= end - pos)
1487 return pos;
1488 pos += ret;
1489 first = 0;
1490 }
1491 #endif /* CONFIG_IEEE80211R */
1492 #ifdef CONFIG_IEEE80211W
1493 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1494 ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
1495 first ? "" : "+");
1496 if (ret < 0 || ret >= end - pos)
1497 return pos;
1498 pos += ret;
1499 first = 0;
1500 }
1501 if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
1502 ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
1503 first ? "" : "+");
1504 if (ret < 0 || ret >= end - pos)
1505 return pos;
1506 pos += ret;
1507 first = 0;
1508 }
1509 #endif /* CONFIG_IEEE80211W */
1510
1511 pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
1512
1513 if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
1514 ret = os_snprintf(pos, end - pos, "-preauth");
1515 if (ret < 0 || ret >= end - pos)
1516 return pos;
1517 pos += ret;
1518 }
1519
1520 ret = os_snprintf(pos, end - pos, "]");
1521 if (ret < 0 || ret >= end - pos)
1522 return pos;
1523 pos += ret;
1524
1525 return pos;
1526 }
1527
1528
1529 #ifdef CONFIG_WPS
1530 static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
1531 char *pos, char *end,
1532 struct wpabuf *wps_ie)
1533 {
1534 int ret;
1535 const char *txt;
1536
1537 if (wps_ie == NULL)
1538 return pos;
1539 if (wps_is_selected_pbc_registrar(wps_ie))
1540 txt = "[WPS-PBC]";
1541 #ifdef CONFIG_WPS2
1542 else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
1543 txt = "[WPS-AUTH]";
1544 #endif /* CONFIG_WPS2 */
1545 else if (wps_is_selected_pin_registrar(wps_ie))
1546 txt = "[WPS-PIN]";
1547 else
1548 txt = "[WPS]";
1549
1550 ret = os_snprintf(pos, end - pos, "%s", txt);
1551 if (ret >= 0 && ret < end - pos)
1552 pos += ret;
1553 wpabuf_free(wps_ie);
1554 return pos;
1555 }
1556 #endif /* CONFIG_WPS */
1557
1558
1559 static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
1560 char *pos, char *end,
1561 const struct wpa_bss *bss)
1562 {
1563 #ifdef CONFIG_WPS
1564 struct wpabuf *wps_ie;
1565 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1566 return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
1567 #else /* CONFIG_WPS */
1568 return pos;
1569 #endif /* CONFIG_WPS */
1570 }
1571
1572
1573 /* Format one result on one text line into a buffer. */
1574 static int wpa_supplicant_ctrl_iface_scan_result(
1575 struct wpa_supplicant *wpa_s,
1576 const struct wpa_bss *bss, char *buf, size_t buflen)
1577 {
1578 char *pos, *end;
1579 int ret;
1580 const u8 *ie, *ie2, *p2p;
1581
1582 p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
1583 if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
1584 os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
1585 0)
1586 return 0; /* Do not show P2P listen discovery results here */
1587
1588 pos = buf;
1589 end = buf + buflen;
1590
1591 ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
1592 MAC2STR(bss->bssid), bss->freq, bss->level);
1593 if (ret < 0 || ret >= end - pos)
1594 return -1;
1595 pos += ret;
1596 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
1597 if (ie)
1598 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
1599 ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
1600 if (ie2)
1601 pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
1602 pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
1603 if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
1604 ret = os_snprintf(pos, end - pos, "[WEP]");
1605 if (ret < 0 || ret >= end - pos)
1606 return -1;
1607 pos += ret;
1608 }
1609 if (bss->caps & IEEE80211_CAP_IBSS) {
1610 ret = os_snprintf(pos, end - pos, "[IBSS]");
1611 if (ret < 0 || ret >= end - pos)
1612 return -1;
1613 pos += ret;
1614 }
1615 if (bss->caps & IEEE80211_CAP_ESS) {
1616 ret = os_snprintf(pos, end - pos, "[ESS]");
1617 if (ret < 0 || ret >= end - pos)
1618 return -1;
1619 pos += ret;
1620 }
1621 if (p2p) {
1622 ret = os_snprintf(pos, end - pos, "[P2P]");
1623 if (ret < 0 || ret >= end - pos)
1624 return -1;
1625 pos += ret;
1626 }
1627 #ifdef CONFIG_HS20
1628 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE) && ie2) {
1629 ret = os_snprintf(pos, end - pos, "[HS20]");
1630 if (ret < 0 || ret >= end - pos)
1631 return -1;
1632 pos += ret;
1633 }
1634 #endif /* CONFIG_HS20 */
1635
1636 ret = os_snprintf(pos, end - pos, "\t%s",
1637 wpa_ssid_txt(bss->ssid, bss->ssid_len));
1638 if (ret < 0 || ret >= end - pos)
1639 return -1;
1640 pos += ret;
1641
1642 ret = os_snprintf(pos, end - pos, "\n");
1643 if (ret < 0 || ret >= end - pos)
1644 return -1;
1645 pos += ret;
1646
1647 return pos - buf;
1648 }
1649
1650
1651 static int wpa_supplicant_ctrl_iface_scan_results(
1652 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
1653 {
1654 char *pos, *end;
1655 struct wpa_bss *bss;
1656 int ret;
1657
1658 pos = buf;
1659 end = buf + buflen;
1660 ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
1661 "flags / ssid\n");
1662 if (ret < 0 || ret >= end - pos)
1663 return pos - buf;
1664 pos += ret;
1665
1666 dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
1667 ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
1668 end - pos);
1669 if (ret < 0 || ret >= end - pos)
1670 return pos - buf;
1671 pos += ret;
1672 }
1673
1674 return pos - buf;
1675 }
1676
1677
1678 static int wpa_supplicant_ctrl_iface_select_network(
1679 struct wpa_supplicant *wpa_s, char *cmd)
1680 {
1681 int id;
1682 struct wpa_ssid *ssid;
1683
1684 /* cmd: "<network id>" or "any" */
1685 if (os_strcmp(cmd, "any") == 0) {
1686 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
1687 ssid = NULL;
1688 } else {
1689 id = atoi(cmd);
1690 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
1691
1692 ssid = wpa_config_get_network(wpa_s->conf, id);
1693 if (ssid == NULL) {
1694 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
1695 "network id=%d", id);
1696 return -1;
1697 }
1698 if (ssid->disabled == 2) {
1699 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
1700 "SELECT_NETWORK with persistent P2P group");
1701 return -1;
1702 }
1703 }
1704
1705 wpa_supplicant_select_network(wpa_s, ssid);
1706
1707 return 0;
1708 }
1709
1710
1711 static int wpa_supplicant_ctrl_iface_enable_network(
1712 struct wpa_supplicant *wpa_s, char *cmd)
1713 {
1714 int id;
1715 struct wpa_ssid *ssid;
1716
1717 /* cmd: "<network id>" or "all" */
1718 if (os_strcmp(cmd, "all") == 0) {
1719 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
1720 ssid = NULL;
1721 } else {
1722 id = atoi(cmd);
1723 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
1724
1725 ssid = wpa_config_get_network(wpa_s->conf, id);
1726 if (ssid == NULL) {
1727 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
1728 "network id=%d", id);
1729 return -1;
1730 }
1731 if (ssid->disabled == 2) {
1732 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
1733 "ENABLE_NETWORK with persistent P2P group");
1734 return -1;
1735 }
1736
1737 if (os_strstr(cmd, " no-connect")) {
1738 ssid->disabled = 0;
1739 return 0;
1740 }
1741 }
1742 wpa_supplicant_enable_network(wpa_s, ssid);
1743
1744 return 0;
1745 }
1746
1747
1748 static int wpa_supplicant_ctrl_iface_disable_network(
1749 struct wpa_supplicant *wpa_s, char *cmd)
1750 {
1751 int id;
1752 struct wpa_ssid *ssid;
1753
1754 /* cmd: "<network id>" or "all" */
1755 if (os_strcmp(cmd, "all") == 0) {
1756 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
1757 ssid = NULL;
1758 } else {
1759 id = atoi(cmd);
1760 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
1761
1762 ssid = wpa_config_get_network(wpa_s->conf, id);
1763 if (ssid == NULL) {
1764 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
1765 "network id=%d", id);
1766 return -1;
1767 }
1768 if (ssid->disabled == 2) {
1769 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
1770 "DISABLE_NETWORK with persistent P2P "
1771 "group");
1772 return -1;
1773 }
1774 }
1775 wpa_supplicant_disable_network(wpa_s, ssid);
1776
1777 return 0;
1778 }
1779
1780
1781 static int wpa_supplicant_ctrl_iface_add_network(
1782 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
1783 {
1784 struct wpa_ssid *ssid;
1785 int ret;
1786
1787 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
1788
1789 ssid = wpa_config_add_network(wpa_s->conf);
1790 if (ssid == NULL)
1791 return -1;
1792
1793 wpas_notify_network_added(wpa_s, ssid);
1794
1795 ssid->disabled = 1;
1796 wpa_config_set_network_defaults(ssid);
1797
1798 ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
1799 if (ret < 0 || (size_t) ret >= buflen)
1800 return -1;
1801 return ret;
1802 }
1803
1804
1805 static int wpa_supplicant_ctrl_iface_remove_network(
1806 struct wpa_supplicant *wpa_s, char *cmd)
1807 {
1808 int id;
1809 struct wpa_ssid *ssid;
1810
1811 /* cmd: "<network id>" or "all" */
1812 if (os_strcmp(cmd, "all") == 0) {
1813 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
1814 ssid = wpa_s->conf->ssid;
1815 while (ssid) {
1816 struct wpa_ssid *remove_ssid = ssid;
1817 id = ssid->id;
1818 ssid = ssid->next;
1819 wpas_notify_network_removed(wpa_s, remove_ssid);
1820 wpa_config_remove_network(wpa_s->conf, id);
1821 }
1822 eapol_sm_invalidate_cached_session(wpa_s->eapol);
1823 if (wpa_s->current_ssid) {
1824 #ifdef CONFIG_SME
1825 wpa_s->sme.prev_bssid_set = 0;
1826 #endif /* CONFIG_SME */
1827 wpa_sm_set_config(wpa_s->wpa, NULL);
1828 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1829 wpa_supplicant_disassociate(wpa_s,
1830 WLAN_REASON_DEAUTH_LEAVING);
1831 }
1832 return 0;
1833 }
1834
1835 id = atoi(cmd);
1836 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
1837
1838 ssid = wpa_config_get_network(wpa_s->conf, id);
1839 if (ssid)
1840 wpas_notify_network_removed(wpa_s, ssid);
1841 if (ssid == NULL ||
1842 wpa_config_remove_network(wpa_s->conf, id) < 0) {
1843 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
1844 "id=%d", id);
1845 return -1;
1846 }
1847
1848 if (ssid == wpa_s->current_ssid || wpa_s->current_ssid == NULL) {
1849 #ifdef CONFIG_SME
1850 wpa_s->sme.prev_bssid_set = 0;
1851 #endif /* CONFIG_SME */
1852 /*
1853 * Invalidate the EAP session cache if the current or
1854 * previously used network is removed.
1855 */
1856 eapol_sm_invalidate_cached_session(wpa_s->eapol);
1857 }
1858
1859 if (ssid == wpa_s->current_ssid) {
1860 wpa_sm_set_config(wpa_s->wpa, NULL);
1861 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1862
1863 wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1864 }
1865
1866 return 0;
1867 }
1868
1869
1870 static int wpa_supplicant_ctrl_iface_set_network(
1871 struct wpa_supplicant *wpa_s, char *cmd)
1872 {
1873 int id;
1874 struct wpa_ssid *ssid;
1875 char *name, *value;
1876
1877 /* cmd: "<network id> <variable name> <value>" */
1878 name = os_strchr(cmd, ' ');
1879 if (name == NULL)
1880 return -1;
1881 *name++ = '\0';
1882
1883 value = os_strchr(name, ' ');
1884 if (value == NULL)
1885 return -1;
1886 *value++ = '\0';
1887
1888 id = atoi(cmd);
1889 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
1890 id, name);
1891 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
1892 (u8 *) value, os_strlen(value));
1893
1894 ssid = wpa_config_get_network(wpa_s->conf, id);
1895 if (ssid == NULL) {
1896 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
1897 "id=%d", id);
1898 return -1;
1899 }
1900
1901 if (wpa_config_set(ssid, name, value, 0) < 0) {
1902 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
1903 "variable '%s'", name);
1904 return -1;
1905 }
1906
1907 wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
1908
1909 if (wpa_s->current_ssid == ssid || wpa_s->current_ssid == NULL) {
1910 /*
1911 * Invalidate the EAP session cache if anything in the current
1912 * or previously used configuration changes.
1913 */
1914 eapol_sm_invalidate_cached_session(wpa_s->eapol);
1915 }
1916
1917 if ((os_strcmp(name, "psk") == 0 &&
1918 value[0] == '"' && ssid->ssid_len) ||
1919 (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
1920 wpa_config_update_psk(ssid);
1921 else if (os_strcmp(name, "priority") == 0)
1922 wpa_config_update_prio_list(wpa_s->conf);
1923
1924 return 0;
1925 }
1926
1927
1928 static int wpa_supplicant_ctrl_iface_get_network(
1929 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
1930 {
1931 int id;
1932 size_t res;
1933 struct wpa_ssid *ssid;
1934 char *name, *value;
1935
1936 /* cmd: "<network id> <variable name>" */
1937 name = os_strchr(cmd, ' ');
1938 if (name == NULL || buflen == 0)
1939 return -1;
1940 *name++ = '\0';
1941
1942 id = atoi(cmd);
1943 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
1944 id, name);
1945
1946 ssid = wpa_config_get_network(wpa_s->conf, id);
1947 if (ssid == NULL) {
1948 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
1949 "id=%d", id);
1950 return -1;
1951 }
1952
1953 value = wpa_config_get_no_key(ssid, name);
1954 if (value == NULL) {
1955 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
1956 "variable '%s'", name);
1957 return -1;
1958 }
1959
1960 res = os_strlcpy(buf, value, buflen);
1961 if (res >= buflen) {
1962 os_free(value);
1963 return -1;
1964 }
1965
1966 os_free(value);
1967
1968 return res;
1969 }
1970
1971
1972 static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
1973 char *buf, size_t buflen)
1974 {
1975 char *pos, *end;
1976 struct wpa_cred *cred;
1977 int ret;
1978
1979 pos = buf;
1980 end = buf + buflen;
1981 ret = os_snprintf(pos, end - pos,
1982 "cred id / realm / username / domain / imsi\n");
1983 if (ret < 0 || ret >= end - pos)
1984 return pos - buf;
1985 pos += ret;
1986
1987 cred = wpa_s->conf->cred;
1988 while (cred) {
1989 ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
1990 cred->id, cred->realm ? cred->realm : "",
1991 cred->username ? cred->username : "",
1992 cred->domain ? cred->domain : "",
1993 cred->imsi ? cred->imsi : "");
1994 if (ret < 0 || ret >= end - pos)
1995 return pos - buf;
1996 pos += ret;
1997
1998 cred = cred->next;
1999 }
2000
2001 return pos - buf;
2002 }
2003
2004
2005 static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
2006 char *buf, size_t buflen)
2007 {
2008 struct wpa_cred *cred;
2009 int ret;
2010
2011 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
2012
2013 cred = wpa_config_add_cred(wpa_s->conf);
2014 if (cred == NULL)
2015 return -1;
2016
2017 ret = os_snprintf(buf, buflen, "%d\n", cred->id);
2018 if (ret < 0 || (size_t) ret >= buflen)
2019 return -1;
2020 return ret;
2021 }
2022
2023
2024 static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
2025 char *cmd)
2026 {
2027 int id;
2028 struct wpa_cred *cred;
2029
2030 /* cmd: "<cred id>" or "all" */
2031 if (os_strcmp(cmd, "all") == 0) {
2032 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
2033 cred = wpa_s->conf->cred;
2034 while (cred) {
2035 id = cred->id;
2036 cred = cred->next;
2037 wpa_config_remove_cred(wpa_s->conf, id);
2038 }
2039 return 0;
2040 }
2041
2042 id = atoi(cmd);
2043 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
2044
2045 cred = wpa_config_get_cred(wpa_s->conf, id);
2046 if (cred == NULL ||
2047 wpa_config_remove_cred(wpa_s->conf, id) < 0) {
2048 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
2049 id);
2050 return -1;
2051 }
2052
2053 return 0;
2054 }
2055
2056
2057 static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
2058 char *cmd)
2059 {
2060 int id;
2061 struct wpa_cred *cred;
2062 char *name, *value;
2063
2064 /* cmd: "<cred id> <variable name> <value>" */
2065 name = os_strchr(cmd, ' ');
2066 if (name == NULL)
2067 return -1;
2068 *name++ = '\0';
2069
2070 value = os_strchr(name, ' ');
2071 if (value == NULL)
2072 return -1;
2073 *value++ = '\0';
2074
2075 id = atoi(cmd);
2076 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
2077 id, name);
2078 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
2079 (u8 *) value, os_strlen(value));
2080
2081 cred = wpa_config_get_cred(wpa_s->conf, id);
2082 if (cred == NULL) {
2083 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
2084 id);
2085 return -1;
2086 }
2087
2088 if (wpa_config_set_cred(cred, name, value, 0) < 0) {
2089 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
2090 "variable '%s'", name);
2091 return -1;
2092 }
2093
2094 return 0;
2095 }
2096
2097
2098 #ifndef CONFIG_NO_CONFIG_WRITE
2099 static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
2100 {
2101 int ret;
2102
2103 if (!wpa_s->conf->update_config) {
2104 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
2105 "to update configuration (update_config=0)");
2106 return -1;
2107 }
2108
2109 ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
2110 if (ret) {
2111 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
2112 "update configuration");
2113 } else {
2114 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
2115 " updated");
2116 }
2117
2118 return ret;
2119 }
2120 #endif /* CONFIG_NO_CONFIG_WRITE */
2121
2122
2123 static int ctrl_iface_get_capability_pairwise(int res, char *strict,
2124 struct wpa_driver_capa *capa,
2125 char *buf, size_t buflen)
2126 {
2127 int ret, first = 1;
2128 char *pos, *end;
2129 size_t len;
2130
2131 pos = buf;
2132 end = pos + buflen;
2133
2134 if (res < 0) {
2135 if (strict)
2136 return 0;
2137 len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
2138 if (len >= buflen)
2139 return -1;
2140 return len;
2141 }
2142
2143 if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
2144 ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
2145 if (ret < 0 || ret >= end - pos)
2146 return pos - buf;
2147 pos += ret;
2148 first = 0;
2149 }
2150
2151 if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
2152 ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
2153 if (ret < 0 || ret >= end - pos)
2154 return pos - buf;
2155 pos += ret;
2156 first = 0;
2157 }
2158
2159 if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
2160 ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : " ");
2161 if (ret < 0 || ret >= end - pos)
2162 return pos - buf;
2163 pos += ret;
2164 first = 0;
2165 }
2166
2167 return pos - buf;
2168 }
2169
2170
2171 static int ctrl_iface_get_capability_group(int res, char *strict,
2172 struct wpa_driver_capa *capa,
2173 char *buf, size_t buflen)
2174 {
2175 int ret, first = 1;
2176 char *pos, *end;
2177 size_t len;
2178
2179 pos = buf;
2180 end = pos + buflen;
2181
2182 if (res < 0) {
2183 if (strict)
2184 return 0;
2185 len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
2186 if (len >= buflen)
2187 return -1;
2188 return len;
2189 }
2190
2191 if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
2192 ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
2193 if (ret < 0 || ret >= end - pos)
2194 return pos - buf;
2195 pos += ret;
2196 first = 0;
2197 }
2198
2199 if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
2200 ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
2201 if (ret < 0 || ret >= end - pos)
2202 return pos - buf;
2203 pos += ret;
2204 first = 0;
2205 }
2206
2207 if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP104) {
2208 ret = os_snprintf(pos, end - pos, "%sWEP104",
2209 first ? "" : " ");
2210 if (ret < 0 || ret >= end - pos)
2211 return pos - buf;
2212 pos += ret;
2213 first = 0;
2214 }
2215
2216 if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP40) {
2217 ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : " ");
2218 if (ret < 0 || ret >= end - pos)
2219 return pos - buf;
2220 pos += ret;
2221 first = 0;
2222 }
2223
2224 return pos - buf;
2225 }
2226
2227
2228 static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
2229 struct wpa_driver_capa *capa,
2230 char *buf, size_t buflen)
2231 {
2232 int ret;
2233 char *pos, *end;
2234 size_t len;
2235
2236 pos = buf;
2237 end = pos + buflen;
2238
2239 if (res < 0) {
2240 if (strict)
2241 return 0;
2242 len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
2243 "NONE", buflen);
2244 if (len >= buflen)
2245 return -1;
2246 return len;
2247 }
2248
2249 ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
2250 if (ret < 0 || ret >= end - pos)
2251 return pos - buf;
2252 pos += ret;
2253
2254 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
2255 WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
2256 ret = os_snprintf(pos, end - pos, " WPA-EAP");
2257 if (ret < 0 || ret >= end - pos)
2258 return pos - buf;
2259 pos += ret;
2260 }
2261
2262 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
2263 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
2264 ret = os_snprintf(pos, end - pos, " WPA-PSK");
2265 if (ret < 0 || ret >= end - pos)
2266 return pos - buf;
2267 pos += ret;
2268 }
2269
2270 if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
2271 ret = os_snprintf(pos, end - pos, " WPA-NONE");
2272 if (ret < 0 || ret >= end - pos)
2273 return pos - buf;
2274 pos += ret;
2275 }
2276
2277 return pos - buf;
2278 }
2279
2280
2281 static int ctrl_iface_get_capability_proto(int res, char *strict,
2282 struct wpa_driver_capa *capa,
2283 char *buf, size_t buflen)
2284 {
2285 int ret, first = 1;
2286 char *pos, *end;
2287 size_t len;
2288
2289 pos = buf;
2290 end = pos + buflen;
2291
2292 if (res < 0) {
2293 if (strict)
2294 return 0;
2295 len = os_strlcpy(buf, "RSN WPA", buflen);
2296 if (len >= buflen)
2297 return -1;
2298 return len;
2299 }
2300
2301 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
2302 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
2303 ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
2304 if (ret < 0 || ret >= end - pos)
2305 return pos - buf;
2306 pos += ret;
2307 first = 0;
2308 }
2309
2310 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
2311 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
2312 ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
2313 if (ret < 0 || ret >= end - pos)
2314 return pos - buf;
2315 pos += ret;
2316 first = 0;
2317 }
2318
2319 return pos - buf;
2320 }
2321
2322
2323 static int ctrl_iface_get_capability_auth_alg(int res, char *strict,
2324 struct wpa_driver_capa *capa,
2325 char *buf, size_t buflen)
2326 {
2327 int ret, first = 1;
2328 char *pos, *end;
2329 size_t len;
2330
2331 pos = buf;
2332 end = pos + buflen;
2333
2334 if (res < 0) {
2335 if (strict)
2336 return 0;
2337 len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
2338 if (len >= buflen)
2339 return -1;
2340 return len;
2341 }
2342
2343 if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
2344 ret = os_snprintf(pos, end - pos, "%sOPEN", first ? "" : " ");
2345 if (ret < 0 || ret >= end - pos)
2346 return pos - buf;
2347 pos += ret;
2348 first = 0;
2349 }
2350
2351 if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
2352 ret = os_snprintf(pos, end - pos, "%sSHARED",
2353 first ? "" : " ");
2354 if (ret < 0 || ret >= end - pos)
2355 return pos - buf;
2356 pos += ret;
2357 first = 0;
2358 }
2359
2360 if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
2361 ret = os_snprintf(pos, end - pos, "%sLEAP", first ? "" : " ");
2362 if (ret < 0 || ret >= end - pos)
2363 return pos - buf;
2364 pos += ret;
2365 first = 0;
2366 }
2367
2368 return pos - buf;
2369 }
2370
2371
2372 static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
2373 char *buf, size_t buflen)
2374 {
2375 struct hostapd_channel_data *chnl;
2376 int ret, i, j;
2377 char *pos, *end, *hmode;
2378
2379 pos = buf;
2380 end = pos + buflen;
2381
2382 for (j = 0; j < wpa_s->hw.num_modes; j++) {
2383 switch (wpa_s->hw.modes[j].mode) {
2384 case HOSTAPD_MODE_IEEE80211B:
2385 hmode = "B";
2386 break;
2387 case HOSTAPD_MODE_IEEE80211G:
2388 hmode = "G";
2389 break;
2390 case HOSTAPD_MODE_IEEE80211A:
2391 hmode = "A";
2392 break;
2393 default:
2394 continue;
2395 }
2396 ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
2397 if (ret < 0 || ret >= end - pos)
2398 return pos - buf;
2399 pos += ret;
2400 chnl = wpa_s->hw.modes[j].channels;
2401 for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
2402 if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
2403 continue;
2404 ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
2405 if (ret < 0 || ret >= end - pos)
2406 return pos - buf;
2407 pos += ret;
2408 }
2409 ret = os_snprintf(pos, end - pos, "\n");
2410 if (ret < 0 || ret >= end - pos)
2411 return pos - buf;
2412 pos += ret;
2413 }
2414
2415 return pos - buf;
2416 }
2417
2418
2419 static int wpa_supplicant_ctrl_iface_get_capability(
2420 struct wpa_supplicant *wpa_s, const char *_field, char *buf,
2421 size_t buflen)
2422 {
2423 struct wpa_driver_capa capa;
2424 int res;
2425 char *strict;
2426 char field[30];
2427 size_t len;
2428
2429 /* Determine whether or not strict checking was requested */
2430 len = os_strlcpy(field, _field, sizeof(field));
2431 if (len >= sizeof(field))
2432 return -1;
2433 strict = os_strchr(field, ' ');
2434 if (strict != NULL) {
2435 *strict++ = '\0';
2436 if (os_strcmp(strict, "strict") != 0)
2437 return -1;
2438 }
2439
2440 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
2441 field, strict ? strict : "");
2442
2443 if (os_strcmp(field, "eap") == 0) {
2444 return eap_get_names(buf, buflen);
2445 }
2446
2447 res = wpa_drv_get_capa(wpa_s, &capa);
2448
2449 if (os_strcmp(field, "pairwise") == 0)
2450 return ctrl_iface_get_capability_pairwise(res, strict, &capa,
2451 buf, buflen);
2452
2453 if (os_strcmp(field, "group") == 0)
2454 return ctrl_iface_get_capability_group(res, strict, &capa,
2455 buf, buflen);
2456
2457 if (os_strcmp(field, "key_mgmt") == 0)
2458 return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
2459 buf, buflen);
2460
2461 if (os_strcmp(field, "proto") == 0)
2462 return ctrl_iface_get_capability_proto(res, strict, &capa,
2463 buf, buflen);
2464
2465 if (os_strcmp(field, "auth_alg") == 0)
2466 return ctrl_iface_get_capability_auth_alg(res, strict, &capa,
2467 buf, buflen);
2468
2469 if (os_strcmp(field, "channels") == 0)
2470 return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
2471
2472 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
2473 field);
2474
2475 return -1;
2476 }
2477
2478
2479 #ifdef CONFIG_INTERWORKING
2480 static char * anqp_add_hex(char *pos, char *end, const char *title,
2481 struct wpabuf *data)
2482 {
2483 char *start = pos;
2484 size_t i;
2485 int ret;
2486 const u8 *d;
2487
2488 if (data == NULL)
2489 return start;
2490
2491 ret = os_snprintf(pos, end - pos, "%s=", title);
2492 if (ret < 0 || ret >= end - pos)
2493 return start;
2494 pos += ret;
2495
2496 d = wpabuf_head_u8(data);
2497 for (i = 0; i < wpabuf_len(data); i++) {
2498 ret = os_snprintf(pos, end - pos, "%02x", *d++);
2499 if (ret < 0 || ret >= end - pos)
2500 return start;
2501 pos += ret;
2502 }
2503
2504 ret = os_snprintf(pos, end - pos, "\n");
2505 if (ret < 0 || ret >= end - pos)
2506 return start;
2507 pos += ret;
2508
2509 return pos;
2510 }
2511 #endif /* CONFIG_INTERWORKING */
2512
2513
2514 static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
2515 unsigned long mask, char *buf, size_t buflen)
2516 {
2517 size_t i;
2518 int ret;
2519 char *pos, *end;
2520 const u8 *ie, *ie2;
2521
2522 pos = buf;
2523 end = buf + buflen;
2524
2525 if (mask & WPA_BSS_MASK_ID) {
2526 ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
2527 if (ret < 0 || ret >= end - pos)
2528 return 0;
2529 pos += ret;
2530 }
2531
2532 if (mask & WPA_BSS_MASK_BSSID) {
2533 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
2534 MAC2STR(bss->bssid));
2535 if (ret < 0 || ret >= end - pos)
2536 return 0;
2537 pos += ret;
2538 }
2539
2540 if (mask & WPA_BSS_MASK_FREQ) {
2541 ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
2542 if (ret < 0 || ret >= end - pos)
2543 return 0;
2544 pos += ret;
2545 }
2546
2547 if (mask & WPA_BSS_MASK_BEACON_INT) {
2548 ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
2549 bss->beacon_int);
2550 if (ret < 0 || ret >= end - pos)
2551 return 0;
2552 pos += ret;
2553 }
2554
2555 if (mask & WPA_BSS_MASK_CAPABILITIES) {
2556 ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
2557 bss->caps);
2558 if (ret < 0 || ret >= end - pos)
2559 return 0;
2560 pos += ret;
2561 }
2562
2563 if (mask & WPA_BSS_MASK_QUAL) {
2564 ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
2565 if (ret < 0 || ret >= end - pos)
2566 return 0;
2567 pos += ret;
2568 }
2569
2570 if (mask & WPA_BSS_MASK_NOISE) {
2571 ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
2572 if (ret < 0 || ret >= end - pos)
2573 return 0;
2574 pos += ret;
2575 }
2576
2577 if (mask & WPA_BSS_MASK_LEVEL) {
2578 ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
2579 if (ret < 0 || ret >= end - pos)
2580 return 0;
2581 pos += ret;
2582 }
2583
2584 if (mask & WPA_BSS_MASK_TSF) {
2585 ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
2586 (unsigned long long) bss->tsf);
2587 if (ret < 0 || ret >= end - pos)
2588 return 0;
2589 pos += ret;
2590 }
2591
2592 if (mask & WPA_BSS_MASK_AGE) {
2593 struct os_time now;
2594
2595 os_get_time(&now);
2596 ret = os_snprintf(pos, end - pos, "age=%d\n",
2597 (int) (now.sec - bss->last_update.sec));
2598 if (ret < 0 || ret >= end - pos)
2599 return 0;
2600 pos += ret;
2601 }
2602
2603 if (mask & WPA_BSS_MASK_IE) {
2604 ret = os_snprintf(pos, end - pos, "ie=");
2605 if (ret < 0 || ret >= end - pos)
2606 return 0;
2607 pos += ret;
2608
2609 ie = (const u8 *) (bss + 1);
2610 for (i = 0; i < bss->ie_len; i++) {
2611 ret = os_snprintf(pos, end - pos, "%02x", *ie++);
2612 if (ret < 0 || ret >= end - pos)
2613 return 0;
2614 pos += ret;
2615 }
2616
2617 ret = os_snprintf(pos, end - pos, "\n");
2618 if (ret < 0 || ret >= end - pos)
2619 return 0;
2620 pos += ret;
2621 }
2622
2623 if (mask & WPA_BSS_MASK_FLAGS) {
2624 ret = os_snprintf(pos, end - pos, "flags=");
2625 if (ret < 0 || ret >= end - pos)
2626 return 0;
2627 pos += ret;
2628
2629 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
2630 if (ie)
2631 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
2632 2 + ie[1]);
2633 ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
2634 if (ie2)
2635 pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2,
2636 2 + ie2[1]);
2637 pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
2638 if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
2639 ret = os_snprintf(pos, end - pos, "[WEP]");
2640 if (ret < 0 || ret >= end - pos)
2641 return 0;
2642 pos += ret;
2643 }
2644 if (bss->caps & IEEE80211_CAP_IBSS) {
2645 ret = os_snprintf(pos, end - pos, "[IBSS]");
2646 if (ret < 0 || ret >= end - pos)
2647 return 0;
2648 pos += ret;
2649 }
2650 if (bss->caps & IEEE80211_CAP_ESS) {
2651 ret = os_snprintf(pos, end - pos, "[ESS]");
2652 if (ret < 0 || ret >= end - pos)
2653 return 0;
2654 pos += ret;
2655 }
2656 if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE)) {
2657 ret = os_snprintf(pos, end - pos, "[P2P]");
2658 if (ret < 0 || ret >= end - pos)
2659 return 0;
2660 pos += ret;
2661 }
2662 #ifdef CONFIG_HS20
2663 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
2664 ret = os_snprintf(pos, end - pos, "[HS20]");
2665 if (ret < 0 || ret >= end - pos)
2666 return -1;
2667 pos += ret;
2668 }
2669 #endif /* CONFIG_HS20 */
2670
2671 ret = os_snprintf(pos, end - pos, "\n");
2672 if (ret < 0 || ret >= end - pos)
2673 return 0;
2674 pos += ret;
2675 }
2676
2677 if (mask & WPA_BSS_MASK_SSID) {
2678 ret = os_snprintf(pos, end - pos, "ssid=%s\n",
2679 wpa_ssid_txt(bss->ssid, bss->ssid_len));
2680 if (ret < 0 || ret >= end - pos)
2681 return 0;
2682 pos += ret;
2683 }
2684
2685 #ifdef CONFIG_WPS
2686 if (mask & WPA_BSS_MASK_WPS_SCAN) {
2687 ie = (const u8 *) (bss + 1);
2688 ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
2689 if (ret < 0 || ret >= end - pos)
2690 return 0;
2691 pos += ret;
2692 }
2693 #endif /* CONFIG_WPS */
2694
2695 #ifdef CONFIG_P2P
2696 if (mask & WPA_BSS_MASK_P2P_SCAN) {
2697 ie = (const u8 *) (bss + 1);
2698 ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
2699 if (ret < 0 || ret >= end - pos)
2700 return 0;
2701 pos += ret;
2702 }
2703 #endif /* CONFIG_P2P */
2704
2705 #ifdef CONFIG_INTERWORKING
2706 if (mask & WPA_BSS_MASK_INTERNETW) {
2707 pos = anqp_add_hex(pos, end, "anqp_venue_name",
2708 bss->anqp_venue_name);
2709 pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
2710 bss->anqp_network_auth_type);
2711 pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
2712 bss->anqp_roaming_consortium);
2713 pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
2714 bss->anqp_ip_addr_type_availability);
2715 pos = anqp_add_hex(pos, end, "anqp_nai_realm",
2716 bss->anqp_nai_realm);
2717 pos = anqp_add_hex(pos, end, "anqp_3gpp", bss->anqp_3gpp);
2718 pos = anqp_add_hex(pos, end, "anqp_domain_name",
2719 bss->anqp_domain_name);
2720 #ifdef CONFIG_HS20
2721 pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
2722 bss->hs20_operator_friendly_name);
2723 pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
2724 bss->hs20_wan_metrics);
2725 pos = anqp_add_hex(pos, end, "hs20_connection_capability",
2726 bss->hs20_connection_capability);
2727 #endif /* CONFIG_HS20 */
2728 }
2729 #endif /* CONFIG_INTERWORKING */
2730
2731 return pos - buf;
2732 }
2733
2734
2735 static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
2736 const char *cmd, char *buf,
2737 size_t buflen)
2738 {
2739 u8 bssid[ETH_ALEN];
2740 size_t i;
2741 struct wpa_bss *bss;
2742 struct wpa_bss *bsslast = NULL;
2743 struct dl_list *next;
2744 int ret = 0;
2745 int len;
2746 char *ctmp;
2747 unsigned long mask = WPA_BSS_MASK_ALL;
2748
2749 if (os_strncmp(cmd, "RANGE=", 6) == 0) {
2750 if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
2751 bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
2752 list_id);
2753 bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
2754 list_id);
2755 } else { /* N1-N2 */
2756 unsigned int id1, id2;
2757
2758 if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
2759 wpa_printf(MSG_INFO, "Wrong BSS range "
2760 "format");
2761 return 0;
2762 }
2763
2764 id1 = atoi(cmd + 6);
2765 bss = wpa_bss_get_id(wpa_s, id1);
2766 id2 = atoi(ctmp + 1);
2767 if (id2 == 0)
2768 bsslast = dl_list_last(&wpa_s->bss_id,
2769 struct wpa_bss,
2770 list_id);
2771 else {
2772 bsslast = wpa_bss_get_id(wpa_s, id2);
2773 if (bsslast == NULL && bss && id2 > id1) {
2774 struct wpa_bss *tmp = bss;
2775 for (;;) {
2776 next = tmp->list_id.next;
2777 if (next == &wpa_s->bss_id)
2778 break;
2779 tmp = dl_list_entry(
2780 next, struct wpa_bss,
2781 list_id);
2782 if (tmp->id > id2)
2783 break;
2784 bsslast = tmp;
2785 }
2786 }
2787 }
2788 }
2789 } else if (os_strcmp(cmd, "FIRST") == 0)
2790 bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
2791 else if (os_strncmp(cmd, "ID-", 3) == 0) {
2792 i = atoi(cmd + 3);
2793 bss = wpa_bss_get_id(wpa_s, i);
2794 } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
2795 i = atoi(cmd + 5);
2796 bss = wpa_bss_get_id(wpa_s, i);
2797 if (bss) {
2798 next = bss->list_id.next;
2799 if (next == &wpa_s->bss_id)
2800 bss = NULL;
2801 else
2802 bss = dl_list_entry(next, struct wpa_bss,
2803 list_id);
2804 }
2805 #ifdef CONFIG_P2P
2806 } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
2807 if (hwaddr_aton(cmd + 13, bssid) == 0)
2808 bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
2809 else
2810 bss = NULL;
2811 #endif /* CONFIG_P2P */
2812 } else if (hwaddr_aton(cmd, bssid) == 0)
2813 bss = wpa_bss_get_bssid(wpa_s, bssid);
2814 else {
2815 struct wpa_bss *tmp;
2816 i = atoi(cmd);
2817 bss = NULL;
2818 dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
2819 {
2820 if (i-- == 0) {
2821 bss = tmp;
2822 break;
2823 }
2824 }
2825 }
2826
2827 if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
2828 mask = strtoul(ctmp + 5, NULL, 0x10);
2829 if (mask == 0)
2830 mask = WPA_BSS_MASK_ALL;
2831 }
2832
2833 if (bss == NULL)
2834 return 0;
2835
2836 if (bsslast == NULL)
2837 bsslast = bss;
2838 do {
2839 len = print_bss_info(wpa_s, bss, mask, buf, buflen);
2840 ret += len;
2841 buf += len;
2842 buflen -= len;
2843 if (bss == bsslast)
2844 break;
2845 next = bss->list_id.next;
2846 if (next == &wpa_s->bss_id)
2847 break;
2848 bss = dl_list_entry(next, struct wpa_bss, list_id);
2849 } while (bss && len);
2850
2851 return ret;
2852 }
2853
2854
2855 static int wpa_supplicant_ctrl_iface_ap_scan(
2856 struct wpa_supplicant *wpa_s, char *cmd)
2857 {
2858 int ap_scan = atoi(cmd);
2859 return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
2860 }
2861
2862
2863 static int wpa_supplicant_ctrl_iface_scan_interval(
2864 struct wpa_supplicant *wpa_s, char *cmd)
2865 {
2866 int scan_int = atoi(cmd);
2867 return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
2868 }
2869
2870
2871 static int wpa_supplicant_ctrl_iface_bss_expire_age(
2872 struct wpa_supplicant *wpa_s, char *cmd)
2873 {
2874 int expire_age = atoi(cmd);
2875 return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
2876 }
2877
2878
2879 static int wpa_supplicant_ctrl_iface_bss_expire_count(
2880 struct wpa_supplicant *wpa_s, char *cmd)
2881 {
2882 int expire_count = atoi(cmd);
2883 return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
2884 }
2885
2886
2887 static int wpa_supplicant_ctrl_iface_bss_flush(
2888 struct wpa_supplicant *wpa_s, char *cmd)
2889 {
2890 int flush_age = atoi(cmd);
2891
2892 if (flush_age == 0)
2893 wpa_bss_flush(wpa_s);
2894 else
2895 wpa_bss_flush_by_age(wpa_s, flush_age);
2896 return 0;
2897 }
2898
2899
2900 static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
2901 {
2902 wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
2903 /* MLME-DELETEKEYS.request */
2904 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
2905 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
2906 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
2907 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
2908 #ifdef CONFIG_IEEE80211W
2909 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
2910 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
2911 #endif /* CONFIG_IEEE80211W */
2912
2913 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
2914 0);
2915 /* MLME-SETPROTECTION.request(None) */
2916 wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
2917 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
2918 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
2919 wpa_sm_drop_sa(wpa_s->wpa);
2920 }
2921
2922
2923 static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
2924 char *addr)
2925 {
2926 #ifdef CONFIG_NO_SCAN_PROCESSING
2927 return -1;
2928 #else /* CONFIG_NO_SCAN_PROCESSING */
2929 u8 bssid[ETH_ALEN];
2930 struct wpa_bss *bss;
2931 struct wpa_ssid *ssid = wpa_s->current_ssid;
2932
2933 if (hwaddr_aton(addr, bssid)) {
2934 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
2935 "address '%s'", addr);
2936 return -1;
2937 }
2938
2939 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
2940
2941 bss = wpa_bss_get_bssid(wpa_s, bssid);
2942 if (!bss) {
2943 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
2944 "from BSS table");
2945 return -1;
2946 }
2947
2948 /*
2949 * TODO: Find best network configuration block from configuration to
2950 * allow roaming to other networks
2951 */
2952
2953 if (!ssid) {
2954 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
2955 "configuration known for the target AP");
2956 return -1;
2957 }
2958
2959 wpa_s->reassociate = 1;
2960 wpa_supplicant_connect(wpa_s, bss, ssid);
2961
2962 return 0;
2963 #endif /* CONFIG_NO_SCAN_PROCESSING */
2964 }
2965
2966
2967 #ifdef CONFIG_P2P
2968 static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
2969 {
2970 unsigned int timeout = atoi(cmd);
2971 enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
2972 u8 dev_id[ETH_ALEN], *_dev_id = NULL;
2973 char *pos;
2974 unsigned int search_delay;
2975
2976 if (os_strstr(cmd, "type=social"))
2977 type = P2P_FIND_ONLY_SOCIAL;
2978 else if (os_strstr(cmd, "type=progressive"))
2979 type = P2P_FIND_PROGRESSIVE;
2980
2981 pos = os_strstr(cmd, "dev_id=");
2982 if (pos) {
2983 pos += 7;
2984 if (hwaddr_aton(pos, dev_id))
2985 return -1;
2986 _dev_id = dev_id;
2987 }
2988
2989 pos = os_strstr(cmd, "delay=");
2990 if (pos) {
2991 pos += 6;
2992 search_delay = atoi(pos);
2993 } else
2994 search_delay = wpas_p2p_search_delay(wpa_s);
2995
2996 return wpas_p2p_find(wpa_s, timeout, type, 0, NULL, _dev_id,
2997 search_delay);
2998 }
2999
3000
3001 static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
3002 char *buf, size_t buflen)
3003 {
3004 u8 addr[ETH_ALEN];
3005 char *pos, *pos2;
3006 char *pin = NULL;
3007 enum p2p_wps_method wps_method;
3008 int new_pin;
3009 int ret;
3010 int persistent_group, persistent_id = -1;
3011 int join;
3012 int auth;
3013 int automatic;
3014 int go_intent = -1;
3015 int freq = 0;
3016 int pd;
3017 int ht40;
3018
3019 /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad]
3020 * [persistent|persistent=<network id>]
3021 * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
3022 * [ht40] */
3023
3024 if (hwaddr_aton(cmd, addr))
3025 return -1;
3026
3027 pos = cmd + 17;
3028 if (*pos != ' ')
3029 return -1;
3030 pos++;
3031
3032 persistent_group = os_strstr(pos, " persistent") != NULL;
3033 pos2 = os_strstr(pos, " persistent=");
3034 if (pos2) {
3035 struct wpa_ssid *ssid;
3036 persistent_id = atoi(pos2 + 12);
3037 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3038 if (ssid == NULL || ssid->disabled != 2 ||
3039 ssid->mode != WPAS_MODE_P2P_GO) {
3040 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3041 "SSID id=%d for persistent P2P group (GO)",
3042 persistent_id);
3043 return -1;
3044 }
3045 }
3046 join = os_strstr(pos, " join") != NULL;
3047 auth = os_strstr(pos, " auth") != NULL;
3048 automatic = os_strstr(pos, " auto") != NULL;
3049 pd = os_strstr(pos, " provdisc") != NULL;
3050 ht40 = os_strstr(pos, " ht40") != NULL;
3051
3052 pos2 = os_strstr(pos, " go_intent=");
3053 if (pos2) {
3054 pos2 += 11;
3055 go_intent = atoi(pos2);
3056 if (go_intent < 0 || go_intent > 15)
3057 return -1;
3058 }
3059
3060 pos2 = os_strstr(pos, " freq=");
3061 if (pos2) {
3062 pos2 += 6;
3063 freq = atoi(pos2);
3064 if (freq <= 0)
3065 return -1;
3066 }
3067
3068 if (os_strncmp(pos, "pin", 3) == 0) {
3069 /* Request random PIN (to be displayed) and enable the PIN */
3070 wps_method = WPS_PIN_DISPLAY;
3071 } else if (os_strncmp(pos, "pbc", 3) == 0) {
3072 wps_method = WPS_PBC;
3073 } else {
3074 pin = pos;
3075 pos = os_strchr(pin, ' ');
3076 wps_method = WPS_PIN_KEYPAD;
3077 if (pos) {
3078 *pos++ = '\0';
3079 if (os_strncmp(pos, "display", 7) == 0)
3080 wps_method = WPS_PIN_DISPLAY;
3081 }
3082 if (!wps_pin_str_valid(pin)) {
3083 os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
3084 return 17;
3085 }
3086 }
3087
3088 new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
3089 persistent_group, automatic, join,
3090 auth, go_intent, freq, persistent_id, pd,
3091 ht40);
3092 if (new_pin == -2) {
3093 os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
3094 return 25;
3095 }
3096 if (new_pin == -3) {
3097 os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
3098 return 25;
3099 }
3100 if (new_pin < 0)
3101 return -1;
3102 if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
3103 ret = os_snprintf(buf, buflen, "%08d", new_pin);
3104 if (ret < 0 || (size_t) ret >= buflen)
3105 return -1;
3106 return ret;
3107 }
3108
3109 os_memcpy(buf, "OK\n", 3);
3110 return 3;
3111 }
3112
3113
3114 static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
3115 {
3116 unsigned int timeout = atoi(cmd);
3117 return wpas_p2p_listen(wpa_s, timeout);
3118 }
3119
3120
3121 static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
3122 {
3123 u8 addr[ETH_ALEN];
3124 char *pos;
3125 enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
3126
3127 /* <addr> <config method> [join|auto] */
3128
3129 if (hwaddr_aton(cmd, addr))
3130 return -1;
3131
3132 pos = cmd + 17;
3133 if (*pos != ' ')
3134 return -1;
3135 pos++;
3136
3137 if (os_strstr(pos, " join") != NULL)
3138 use = WPAS_P2P_PD_FOR_JOIN;
3139 else if (os_strstr(pos, " auto") != NULL)
3140 use = WPAS_P2P_PD_AUTO;
3141
3142 return wpas_p2p_prov_disc(wpa_s, addr, pos, use);
3143 }
3144
3145
3146 static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
3147 size_t buflen)
3148 {
3149 struct wpa_ssid *ssid = wpa_s->current_ssid;
3150
3151 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3152 ssid->passphrase == NULL)
3153 return -1;
3154
3155 os_strlcpy(buf, ssid->passphrase, buflen);
3156 return os_strlen(buf);
3157 }
3158
3159
3160 static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
3161 char *buf, size_t buflen)
3162 {
3163 u64 ref;
3164 int res;
3165 u8 dst_buf[ETH_ALEN], *dst;
3166 struct wpabuf *tlvs;
3167 char *pos;
3168 size_t len;
3169
3170 if (hwaddr_aton(cmd, dst_buf))
3171 return -1;
3172 dst = dst_buf;
3173 if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
3174 dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
3175 dst = NULL;
3176 pos = cmd + 17;
3177 if (*pos != ' ')
3178 return -1;
3179 pos++;
3180
3181 if (os_strncmp(pos, "upnp ", 5) == 0) {
3182 u8 version;
3183 pos += 5;
3184 if (hexstr2bin(pos, &version, 1) < 0)
3185 return -1;
3186 pos += 2;
3187 if (*pos != ' ')
3188 return -1;
3189 pos++;
3190 ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
3191 } else {
3192 len = os_strlen(pos);
3193 if (len & 1)
3194 return -1;
3195 len /= 2;
3196 tlvs = wpabuf_alloc(len);
3197 if (tlvs == NULL)
3198 return -1;
3199 if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
3200 wpabuf_free(tlvs);
3201 return -1;
3202 }
3203
3204 ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
3205 wpabuf_free(tlvs);
3206 }
3207 if (ref == 0)
3208 return -1;
3209 res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
3210 if (res < 0 || (unsigned) res >= buflen)
3211 return -1;
3212 return res;
3213 }
3214
3215
3216 static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
3217 char *cmd)
3218 {
3219 long long unsigned val;
3220 u64 req;
3221 if (sscanf(cmd, "%llx", &val) != 1)
3222 return -1;
3223 req = val;
3224 return wpas_p2p_sd_cancel_request(wpa_s, req);
3225 }
3226
3227
3228 static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
3229 {
3230 int freq;
3231 u8 dst[ETH_ALEN];
3232 u8 dialog_token;
3233 struct wpabuf *resp_tlvs;
3234 char *pos, *pos2;
3235 size_t len;
3236
3237 pos = os_strchr(cmd, ' ');
3238 if (pos == NULL)
3239 return -1;
3240 *pos++ = '\0';
3241 freq = atoi(cmd);
3242 if (freq == 0)
3243 return -1;
3244
3245 if (hwaddr_aton(pos, dst))
3246 return -1;
3247 pos += 17;
3248 if (*pos != ' ')
3249 return -1;
3250 pos++;
3251
3252 pos2 = os_strchr(pos, ' ');
3253 if (pos2 == NULL)
3254 return -1;
3255 *pos2++ = '\0';
3256 dialog_token = atoi(pos);
3257
3258 len = os_strlen(pos2);
3259 if (len & 1)
3260 return -1;
3261 len /= 2;
3262 resp_tlvs = wpabuf_alloc(len);
3263 if (resp_tlvs == NULL)
3264 return -1;
3265 if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
3266 wpabuf_free(resp_tlvs);
3267 return -1;
3268 }
3269
3270 wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
3271 wpabuf_free(resp_tlvs);
3272 return 0;
3273 }
3274
3275
3276 static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
3277 char *cmd)
3278 {
3279 if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
3280 return -1;
3281 wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
3282 return 0;
3283 }
3284
3285
3286 static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
3287 char *cmd)
3288 {
3289 char *pos;
3290 size_t len;
3291 struct wpabuf *query, *resp;
3292
3293 pos = os_strchr(cmd, ' ');
3294 if (pos == NULL)
3295 return -1;
3296 *pos++ = '\0';
3297
3298 len = os_strlen(cmd);
3299 if (len & 1)
3300 return -1;
3301 len /= 2;
3302 query = wpabuf_alloc(len);
3303 if (query == NULL)
3304 return -1;
3305 if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
3306 wpabuf_free(query);
3307 return -1;
3308 }
3309
3310 len = os_strlen(pos);
3311 if (len & 1) {
3312 wpabuf_free(query);
3313 return -1;
3314 }
3315 len /= 2;
3316 resp = wpabuf_alloc(len);
3317 if (resp == NULL) {
3318 wpabuf_free(query);
3319 return -1;
3320 }
3321 if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
3322 wpabuf_free(query);
3323 wpabuf_free(resp);
3324 return -1;
3325 }
3326
3327 if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
3328 wpabuf_free(query);
3329 wpabuf_free(resp);
3330 return -1;
3331 }
3332 return 0;
3333 }
3334
3335
3336 static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
3337 {
3338 char *pos;
3339 u8 version;
3340
3341 pos = os_strchr(cmd, ' ');
3342 if (pos == NULL)
3343 return -1;
3344 *pos++ = '\0';
3345
3346 if (hexstr2bin(cmd, &version, 1) < 0)
3347 return -1;
3348
3349 return wpas_p2p_service_add_upnp(wpa_s, version, pos);
3350 }
3351
3352
3353 static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
3354 {
3355 char *pos;
3356
3357 pos = os_strchr(cmd, ' ');
3358 if (pos == NULL)
3359 return -1;
3360 *pos++ = '\0';
3361
3362 if (os_strcmp(cmd, "bonjour") == 0)
3363 return p2p_ctrl_service_add_bonjour(wpa_s, pos);
3364 if (os_strcmp(cmd, "upnp") == 0)
3365 return p2p_ctrl_service_add_upnp(wpa_s, pos);
3366 wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
3367 return -1;
3368 }
3369
3370
3371 static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
3372 char *cmd)
3373 {
3374 size_t len;
3375 struct wpabuf *query;
3376 int ret;
3377
3378 len = os_strlen(cmd);
3379 if (len & 1)
3380 return -1;
3381 len /= 2;
3382 query = wpabuf_alloc(len);
3383 if (query == NULL)
3384 return -1;
3385 if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
3386 wpabuf_free(query);
3387 return -1;
3388 }
3389
3390 ret = wpas_p2p_service_del_bonjour(wpa_s, query);
3391 wpabuf_free(query);
3392 return ret;
3393 }
3394
3395
3396 static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
3397 {
3398 char *pos;
3399 u8 version;
3400
3401 pos = os_strchr(cmd, ' ');
3402 if (pos == NULL)
3403 return -1;
3404 *pos++ = '\0';
3405
3406 if (hexstr2bin(cmd, &version, 1) < 0)
3407 return -1;
3408
3409 return wpas_p2p_service_del_upnp(wpa_s, version, pos);
3410 }
3411
3412
3413 static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
3414 {
3415 char *pos;
3416
3417 pos = os_strchr(cmd, ' ');
3418 if (pos == NULL)
3419 return -1;
3420 *pos++ = '\0';
3421
3422 if (os_strcmp(cmd, "bonjour") == 0)
3423 return p2p_ctrl_service_del_bonjour(wpa_s, pos);
3424 if (os_strcmp(cmd, "upnp") == 0)
3425 return p2p_ctrl_service_del_upnp(wpa_s, pos);
3426 wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
3427 return -1;
3428 }
3429
3430
3431 static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
3432 {
3433 u8 addr[ETH_ALEN];
3434
3435 /* <addr> */
3436
3437 if (hwaddr_aton(cmd, addr))
3438 return -1;
3439
3440 return wpas_p2p_reject(wpa_s, addr);
3441 }
3442
3443
3444 static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
3445 {
3446 char *pos;
3447 int id;
3448 struct wpa_ssid *ssid;
3449 u8 peer[ETH_ALEN];
3450
3451 id = atoi(cmd);
3452 pos = os_strstr(cmd, " peer=");
3453 if (pos) {
3454 pos += 6;
3455 if (hwaddr_aton(pos, peer))
3456 return -1;
3457 }
3458 ssid = wpa_config_get_network(wpa_s->conf, id);
3459 if (ssid == NULL || ssid->disabled != 2) {
3460 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
3461 "for persistent P2P group",
3462 id);
3463 return -1;
3464 }
3465
3466 return wpas_p2p_invite(wpa_s, pos ? peer : NULL, ssid, NULL);
3467 }
3468
3469
3470 static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
3471 {
3472 char *pos;
3473 u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
3474
3475 pos = os_strstr(cmd, " peer=");
3476 if (!pos)
3477 return -1;
3478
3479 *pos = '\0';
3480 pos += 6;
3481 if (hwaddr_aton(pos, peer)) {
3482 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
3483 return -1;
3484 }
3485
3486 pos = os_strstr(pos, " go_dev_addr=");
3487 if (pos) {
3488 pos += 13;
3489 if (hwaddr_aton(pos, go_dev_addr)) {
3490 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
3491 pos);
3492 return -1;
3493 }
3494 go_dev = go_dev_addr;
3495 }
3496
3497 return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
3498 }
3499
3500
3501 static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
3502 {
3503 if (os_strncmp(cmd, "persistent=", 11) == 0)
3504 return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
3505 if (os_strncmp(cmd, "group=", 6) == 0)
3506 return p2p_ctrl_invite_group(wpa_s, cmd + 6);
3507
3508 return -1;
3509 }
3510
3511
3512 static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
3513 char *cmd, int freq, int ht40)
3514 {
3515 int id;
3516 struct wpa_ssid *ssid;
3517
3518 id = atoi(cmd);
3519 ssid = wpa_config_get_network(wpa_s->conf, id);
3520 if (ssid == NULL || ssid->disabled != 2) {
3521 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
3522 "for persistent P2P group",
3523 id);
3524 return -1;
3525 }
3526
3527 return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq, ht40);
3528 }
3529
3530
3531 static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
3532 {
3533 int freq = 0, ht40;
3534 char *pos;
3535
3536 pos = os_strstr(cmd, "freq=");
3537 if (pos)
3538 freq = atoi(pos + 5);
3539
3540 ht40 = os_strstr(cmd, "ht40") != NULL;
3541
3542 if (os_strncmp(cmd, "persistent=", 11) == 0)
3543 return p2p_ctrl_group_add_persistent(wpa_s, cmd + 11, freq,
3544 ht40);
3545 if (os_strcmp(cmd, "persistent") == 0 ||
3546 os_strncmp(cmd, "persistent ", 11) == 0)
3547 return wpas_p2p_group_add(wpa_s, 1, freq, ht40);
3548 if (os_strncmp(cmd, "freq=", 5) == 0)
3549 return wpas_p2p_group_add(wpa_s, 0, freq, ht40);
3550 if (ht40)
3551 return wpas_p2p_group_add(wpa_s, 0, freq, ht40);
3552
3553 wpa_printf(MSG_DEBUG, "CTRL: Invalid P2P_GROUP_ADD parameters '%s'",
3554 cmd);
3555 return -1;
3556 }
3557
3558
3559 static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
3560 char *buf, size_t buflen)
3561 {
3562 u8 addr[ETH_ALEN], *addr_ptr;
3563 int next, res;
3564 const struct p2p_peer_info *info;
3565 char *pos, *end;
3566 char devtype[WPS_DEV_TYPE_BUFSIZE];
3567 struct wpa_ssid *ssid;
3568
3569 if (!wpa_s->global->p2p)
3570 return -1;
3571
3572 if (os_strcmp(cmd, "FIRST") == 0) {
3573 addr_ptr = NULL;
3574 next = 0;
3575 } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
3576 if (hwaddr_aton(cmd + 5, addr) < 0)
3577 return -1;
3578 addr_ptr = addr;
3579 next = 1;
3580 } else {
3581 if (hwaddr_aton(cmd, addr) < 0)
3582 return -1;
3583 addr_ptr = addr;
3584 next = 0;
3585 }
3586
3587 info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
3588 if (info == NULL)
3589 return -1;
3590
3591 pos = buf;
3592 end = buf + buflen;
3593
3594 res = os_snprintf(pos, end - pos, MACSTR "\n"
3595 "pri_dev_type=%s\n"
3596 "device_name=%s\n"
3597 "manufacturer=%s\n"
3598 "model_name=%s\n"
3599 "model_number=%s\n"
3600 "serial_number=%s\n"
3601 "config_methods=0x%x\n"
3602 "dev_capab=0x%x\n"
3603 "group_capab=0x%x\n"
3604 "level=%d\n",
3605 MAC2STR(info->p2p_device_addr),
3606 wps_dev_type_bin2str(info->pri_dev_type,
3607 devtype, sizeof(devtype)),
3608 info->device_name,
3609 info->manufacturer,
3610 info->model_name,
3611 info->model_number,
3612 info->serial_number,
3613 info->config_methods,
3614 info->dev_capab,
3615 info->group_capab,
3616 info->level);
3617 if (res < 0 || res >= end - pos)
3618 return pos - buf;
3619 pos += res;
3620
3621 ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
3622 if (ssid) {
3623 res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
3624 if (res < 0 || res >= end - pos)
3625 return pos - buf;
3626 pos += res;
3627 }
3628
3629 res = p2p_get_peer_info_txt(info, pos, end - pos);
3630 if (res < 0)
3631 return pos - buf;
3632 pos += res;
3633
3634 return pos - buf;
3635 }
3636
3637
3638 static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
3639 const char *param)
3640 {
3641 struct wpa_freq_range *freq = NULL, *n;
3642 unsigned int count = 0, i;
3643 const char *pos, *pos2, *pos3;
3644
3645 if (wpa_s->global->p2p == NULL)
3646 return -1;
3647
3648 /*
3649 * param includes comma separated frequency range.
3650 * For example: 2412-2432,2462,5000-6000
3651 */
3652 pos = param;
3653 while (pos && pos[0]) {
3654 n = os_realloc_array(freq, count + 1,
3655 sizeof(struct wpa_freq_range));
3656 if (n == NULL) {
3657 os_free(freq);
3658 return -1;
3659 }
3660 freq = n;
3661 freq[count].min = atoi(pos);
3662 pos2 = os_strchr(pos, '-');
3663 pos3 = os_strchr(pos, ',');
3664 if (pos2 && (!pos3 || pos2 < pos3)) {
3665 pos2++;
3666 freq[count].max = atoi(pos2);
3667 } else
3668 freq[count].max = freq[count].min;
3669 pos = pos3;
3670 if (pos)
3671 pos++;
3672 count++;
3673 }
3674
3675 for (i = 0; i < count; i++) {
3676 wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
3677 freq[i].min, freq[i].max);
3678 }
3679
3680 os_free(wpa_s->global->p2p_disallow_freq);
3681 wpa_s->global->p2p_disallow_freq = freq;
3682 wpa_s->global->num_p2p_disallow_freq = count;
3683 wpas_p2p_update_channel_list(wpa_s);
3684 return 0;
3685 }
3686
3687
3688 static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
3689 {
3690 char *param;
3691
3692 if (wpa_s->global->p2p == NULL)
3693 return -1;
3694
3695 param = os_strchr(cmd, ' ');
3696 if (param == NULL)
3697 return -1;
3698 *param++ = '\0';
3699
3700 if (os_strcmp(cmd, "discoverability") == 0) {
3701 p2p_set_client_discoverability(wpa_s->global->p2p,
3702 atoi(param));
3703 return 0;
3704 }
3705
3706 if (os_strcmp(cmd, "managed") == 0) {
3707 p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
3708 return 0;
3709 }
3710
3711 if (os_strcmp(cmd, "listen_channel") == 0) {
3712 return p2p_set_listen_channel(wpa_s->global->p2p, 81,
3713 atoi(param));
3714 }
3715
3716 if (os_strcmp(cmd, "ssid_postfix") == 0) {
3717 return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
3718 os_strlen(param));
3719 }
3720
3721 if (os_strcmp(cmd, "noa") == 0) {
3722 char *pos;
3723 int count, start, duration;
3724 /* GO NoA parameters: count,start_offset(ms),duration(ms) */
3725 count = atoi(param);
3726 pos = os_strchr(param, ',');
3727 if (pos == NULL)
3728 return -1;
3729 pos++;
3730 start = atoi(pos);
3731 pos = os_strchr(pos, ',');
3732 if (pos == NULL)
3733 return -1;
3734 pos++;
3735 duration = atoi(pos);
3736 if (count < 0 || count > 255 || start < 0 || duration < 0)
3737 return -1;
3738 if (count == 0 && duration > 0)
3739 return -1;
3740 wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
3741 "start=%d duration=%d", count, start, duration);
3742 return wpas_p2p_set_noa(wpa_s, count, start, duration);
3743 }
3744
3745 if (os_strcmp(cmd, "ps") == 0)
3746 return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
3747
3748 if (os_strcmp(cmd, "oppps") == 0)
3749 return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
3750
3751 if (os_strcmp(cmd, "ctwindow") == 0)
3752 return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
3753
3754 if (os_strcmp(cmd, "disabled") == 0) {
3755 wpa_s->global->p2p_disabled = atoi(param);
3756 wpa_printf(MSG_DEBUG, "P2P functionality %s",
3757 wpa_s->global->p2p_disabled ?
3758 "disabled" : "enabled");
3759 if (wpa_s->global->p2p_disabled) {
3760 wpas_p2p_stop_find(wpa_s);
3761 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
3762 p2p_flush(wpa_s->global->p2p);
3763 }
3764 return 0;
3765 }
3766
3767 if (os_strcmp(cmd, "conc_pref") == 0) {
3768 if (os_strcmp(param, "sta") == 0)
3769 wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
3770 else if (os_strcmp(param, "p2p") == 0)
3771 wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
3772 else {
3773 wpa_printf(MSG_INFO, "Invalid conc_pref value");
3774 return -1;
3775 }
3776 wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
3777 "%s", param);
3778 return 0;
3779 }
3780
3781 if (os_strcmp(cmd, "force_long_sd") == 0) {
3782 wpa_s->force_long_sd = atoi(param);
3783 return 0;
3784 }
3785
3786 if (os_strcmp(cmd, "peer_filter") == 0) {
3787 u8 addr[ETH_ALEN];
3788 if (hwaddr_aton(param, addr))
3789 return -1;
3790 p2p_set_peer_filter(wpa_s->global->p2p, addr);
3791 return 0;
3792 }
3793
3794 if (os_strcmp(cmd, "cross_connect") == 0)
3795 return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
3796
3797 if (os_strcmp(cmd, "go_apsd") == 0) {
3798 if (os_strcmp(param, "disable") == 0)
3799 wpa_s->set_ap_uapsd = 0;
3800 else {
3801 wpa_s->set_ap_uapsd = 1;
3802 wpa_s->ap_uapsd = atoi(param);
3803 }
3804 return 0;
3805 }
3806
3807 if (os_strcmp(cmd, "client_apsd") == 0) {
3808 if (os_strcmp(param, "disable") == 0)
3809 wpa_s->set_sta_uapsd = 0;
3810 else {
3811 int be, bk, vi, vo;
3812 char *pos;
3813 /* format: BE,BK,VI,VO;max SP Length */
3814 be = atoi(param);
3815 pos = os_strchr(param, ',');
3816 if (pos == NULL)
3817 return -1;
3818 pos++;
3819 bk = atoi(pos);
3820 pos = os_strchr(pos, ',');
3821 if (pos == NULL)
3822 return -1;
3823 pos++;
3824 vi = atoi(pos);
3825 pos = os_strchr(pos, ',');
3826 if (pos == NULL)
3827 return -1;
3828 pos++;
3829 vo = atoi(pos);
3830 /* ignore max SP Length for now */
3831
3832 wpa_s->set_sta_uapsd = 1;
3833 wpa_s->sta_uapsd = 0;
3834 if (be)
3835 wpa_s->sta_uapsd |= BIT(0);
3836 if (bk)
3837 wpa_s->sta_uapsd |= BIT(1);
3838 if (vi)
3839 wpa_s->sta_uapsd |= BIT(2);
3840 if (vo)
3841 wpa_s->sta_uapsd |= BIT(3);
3842 }
3843 return 0;
3844 }
3845
3846 if (os_strcmp(cmd, "disallow_freq") == 0)
3847 return p2p_ctrl_disallow_freq(wpa_s, param);
3848
3849 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
3850 cmd);
3851
3852 return -1;
3853 }
3854
3855
3856 static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
3857 {
3858 char *pos, *pos2;
3859 unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
3860
3861 if (cmd[0]) {
3862 pos = os_strchr(cmd, ' ');
3863 if (pos == NULL)
3864 return -1;
3865 *pos++ = '\0';
3866 dur1 = atoi(cmd);
3867
3868 pos2 = os_strchr(pos, ' ');
3869 if (pos2)
3870 *pos2++ = '\0';
3871 int1 = atoi(pos);
3872 } else
3873 pos2 = NULL;
3874
3875 if (pos2) {
3876 pos = os_strchr(pos2, ' ');
3877 if (pos == NULL)
3878 return -1;
3879 *pos++ = '\0';
3880 dur2 = atoi(pos2);
3881 int2 = atoi(pos);
3882 }
3883
3884 return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
3885 }
3886
3887
3888 static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
3889 {
3890 char *pos;
3891 unsigned int period = 0, interval = 0;
3892
3893 if (cmd[0]) {
3894 pos = os_strchr(cmd, ' ');
3895 if (pos == NULL)
3896 return -1;
3897 *pos++ = '\0';
3898 period = atoi(cmd);
3899 interval = atoi(pos);
3900 }
3901
3902 return wpas_p2p_ext_listen(wpa_s, period, interval);
3903 }
3904
3905 #endif /* CONFIG_P2P */
3906
3907
3908 #ifdef CONFIG_INTERWORKING
3909 static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst)
3910 {
3911 u8 bssid[ETH_ALEN];
3912 struct wpa_bss *bss;
3913
3914 if (hwaddr_aton(dst, bssid)) {
3915 wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
3916 return -1;
3917 }
3918
3919 bss = wpa_bss_get_bssid(wpa_s, bssid);
3920 if (bss == NULL) {
3921 wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
3922 MAC2STR(bssid));
3923 return -1;
3924 }
3925
3926 return interworking_connect(wpa_s, bss);
3927 }
3928
3929
3930 static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
3931 {
3932 u8 dst_addr[ETH_ALEN];
3933 int used;
3934 char *pos;
3935 #define MAX_ANQP_INFO_ID 100
3936 u16 id[MAX_ANQP_INFO_ID];
3937 size_t num_id = 0;
3938
3939 used = hwaddr_aton2(dst, dst_addr);
3940 if (used < 0)
3941 return -1;
3942 pos = dst + used;
3943 while (num_id < MAX_ANQP_INFO_ID) {
3944 id[num_id] = atoi(pos);
3945 if (id[num_id])
3946 num_id++;
3947 pos = os_strchr(pos + 1, ',');
3948 if (pos == NULL)
3949 break;
3950 pos++;
3951 }
3952
3953 if (num_id == 0)
3954 return -1;
3955
3956 return anqp_send_req(wpa_s, dst_addr, id, num_id);
3957 }
3958 #endif /* CONFIG_INTERWORKING */
3959
3960
3961 #ifdef CONFIG_HS20
3962
3963 static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
3964 {
3965 u8 dst_addr[ETH_ALEN];
3966 int used;
3967 char *pos;
3968 u32 subtypes = 0;
3969
3970 used = hwaddr_aton2(dst, dst_addr);
3971 if (used < 0)
3972 return -1;
3973 pos = dst + used;
3974 for (;;) {
3975 int num = atoi(pos);
3976 if (num <= 0 || num > 31)
3977 return -1;
3978 subtypes |= BIT(num);
3979 pos = os_strchr(pos + 1, ',');
3980 if (pos == NULL)
3981 break;
3982 pos++;
3983 }
3984
3985 if (subtypes == 0)
3986 return -1;
3987
3988 return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0);
3989 }
3990
3991
3992 static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
3993 const u8 *addr, const char *realm)
3994 {
3995 u8 *buf;
3996 size_t rlen, len;
3997 int ret;
3998
3999 rlen = os_strlen(realm);
4000 len = 3 + rlen;
4001 buf = os_malloc(len);
4002 if (buf == NULL)
4003 return -1;
4004 buf[0] = 1; /* NAI Home Realm Count */
4005 buf[1] = 0; /* Formatted in accordance with RFC 4282 */
4006 buf[2] = rlen;
4007 os_memcpy(buf + 3, realm, rlen);
4008
4009 ret = hs20_anqp_send_req(wpa_s, addr,
4010 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
4011 buf, len);
4012
4013 os_free(buf);
4014
4015 return ret;
4016 }
4017
4018
4019 static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
4020 char *dst)
4021 {
4022 struct wpa_cred *cred = wpa_s->conf->cred;
4023 u8 dst_addr[ETH_ALEN];
4024 int used;
4025 u8 *buf;
4026 size_t len;
4027 int ret;
4028
4029 used = hwaddr_aton2(dst, dst_addr);
4030 if (used < 0)
4031 return -1;
4032
4033 while (dst[used] == ' ')
4034 used++;
4035 if (os_strncmp(dst + used, "realm=", 6) == 0)
4036 return hs20_nai_home_realm_list(wpa_s, dst_addr,
4037 dst + used + 6);
4038
4039 len = os_strlen(dst + used);
4040
4041 if (len == 0 && cred && cred->realm)
4042 return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
4043
4044 if (len % 1)
4045 return -1;
4046 len /= 2;
4047 buf = os_malloc(len);
4048 if (buf == NULL)
4049 return -1;
4050 if (hexstr2bin(dst + used, buf, len) < 0) {
4051 os_free(buf);
4052 return -1;
4053 }
4054
4055 ret = hs20_anqp_send_req(wpa_s, dst_addr,
4056 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
4057 buf, len);
4058 os_free(buf);
4059
4060 return ret;
4061 }
4062
4063 #endif /* CONFIG_HS20 */
4064
4065
4066 static int wpa_supplicant_ctrl_iface_sta_autoconnect(
4067 struct wpa_supplicant *wpa_s, char *cmd)
4068 {
4069 wpa_s->auto_reconnect_disabled = atoi(cmd) == 0 ? 1 : 0;
4070 return 0;
4071 }
4072
4073
4074 #ifdef CONFIG_AUTOSCAN
4075
4076 static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
4077 char *cmd)
4078 {
4079 enum wpa_states state = wpa_s->wpa_state;
4080 char *new_params = NULL;
4081
4082 if (os_strlen(cmd) > 0) {
4083 new_params = os_strdup(cmd);
4084 if (new_params == NULL)
4085 return -1;
4086 }
4087
4088 os_free(wpa_s->conf->autoscan);
4089 wpa_s->conf->autoscan = new_params;
4090
4091 if (wpa_s->conf->autoscan == NULL)
4092 autoscan_deinit(wpa_s);
4093 else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
4094 autoscan_init(wpa_s, 1);
4095 else if (state == WPA_SCANNING)
4096 wpa_supplicant_reinit_autoscan(wpa_s);
4097
4098 return 0;
4099 }
4100
4101 #endif /* CONFIG_AUTOSCAN */
4102
4103
4104 static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
4105 size_t buflen)
4106 {
4107 struct wpa_signal_info si;
4108 int ret;
4109
4110 ret = wpa_drv_signal_poll(wpa_s, &si);
4111 if (ret)
4112 return -1;
4113
4114 ret = os_snprintf(buf, buflen, "RSSI=%d\nLINKSPEED=%d\n"
4115 "NOISE=%d\nFREQUENCY=%u\n",
4116 si.current_signal, si.current_txrate / 1000,
4117 si.current_noise, si.frequency);
4118 if (ret < 0 || (unsigned int) ret > buflen)
4119 return -1;
4120 return ret;
4121 }
4122
4123
4124 char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
4125 char *buf, size_t *resp_len)
4126 {
4127 char *reply;
4128 const int reply_size = 4096;
4129 int ctrl_rsp = 0;
4130 int reply_len;
4131
4132 if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
4133 os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
4134 wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
4135 (const u8 *) buf, os_strlen(buf));
4136 } else {
4137 int level = MSG_DEBUG;
4138 if (os_strcmp(buf, "PING") == 0)
4139 level = MSG_EXCESSIVE;
4140 wpa_hexdump_ascii(level, "RX ctrl_iface",
4141 (const u8 *) buf, os_strlen(buf));
4142 }
4143
4144 reply = os_malloc(reply_size);
4145 if (reply == NULL) {
4146 *resp_len = 1;
4147 return NULL;
4148 }
4149
4150 os_memcpy(reply, "OK\n", 3);
4151 reply_len = 3;
4152
4153 if (os_strcmp(buf, "PING") == 0) {
4154 os_memcpy(reply, "PONG\n", 5);
4155 reply_len = 5;
4156 } else if (os_strcmp(buf, "IFNAME") == 0) {
4157 reply_len = os_strlen(wpa_s->ifname);
4158 os_memcpy(reply, wpa_s->ifname, reply_len);
4159 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
4160 if (wpa_debug_reopen_file() < 0)
4161 reply_len = -1;
4162 } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
4163 wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
4164 } else if (os_strcmp(buf, "MIB") == 0) {
4165 reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
4166 if (reply_len >= 0) {
4167 int res;
4168 res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
4169 reply_size - reply_len);
4170 if (res < 0)
4171 reply_len = -1;
4172 else
4173 reply_len += res;
4174 }
4175 } else if (os_strncmp(buf, "STATUS", 6) == 0) {
4176 reply_len = wpa_supplicant_ctrl_iface_status(
4177 wpa_s, buf + 6, reply, reply_size);
4178 } else if (os_strcmp(buf, "PMKSA") == 0) {
4179 reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
4180 reply_size);
4181 } else if (os_strncmp(buf, "SET ", 4) == 0) {
4182 if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
4183 reply_len = -1;
4184 } else if (os_strncmp(buf, "GET ", 4) == 0) {
4185 reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
4186 reply, reply_size);
4187 } else if (os_strcmp(buf, "LOGON") == 0) {
4188 eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
4189 } else if (os_strcmp(buf, "LOGOFF") == 0) {
4190 eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
4191 } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
4192 wpa_s->normal_scans = 0;
4193 wpa_supplicant_reinit_autoscan(wpa_s);
4194 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
4195 reply_len = -1;
4196 else {
4197 wpa_s->disconnected = 0;
4198 wpa_s->reassociate = 1;
4199 wpa_supplicant_req_scan(wpa_s, 0, 0);
4200 }
4201 } else if (os_strcmp(buf, "RECONNECT") == 0) {
4202 wpa_s->normal_scans = 0;
4203 wpa_supplicant_reinit_autoscan(wpa_s);
4204 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
4205 reply_len = -1;
4206 else if (wpa_s->disconnected) {
4207 wpa_s->disconnected = 0;
4208 wpa_s->reassociate = 1;
4209 wpa_supplicant_req_scan(wpa_s, 0, 0);
4210 }
4211 #ifdef IEEE8021X_EAPOL
4212 } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
4213 if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
4214 reply_len = -1;
4215 #endif /* IEEE8021X_EAPOL */
4216 #ifdef CONFIG_PEERKEY
4217 } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
4218 if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
4219 reply_len = -1;
4220 #endif /* CONFIG_PEERKEY */
4221 #ifdef CONFIG_IEEE80211R
4222 } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
4223 if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
4224 reply_len = -1;
4225 #endif /* CONFIG_IEEE80211R */
4226 #ifdef CONFIG_WPS
4227 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
4228 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
4229 if (res == -2) {
4230 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
4231 reply_len = 17;
4232 } else if (res)
4233 reply_len = -1;
4234 } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
4235 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
4236 if (res == -2) {
4237 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
4238 reply_len = 17;
4239 } else if (res)
4240 reply_len = -1;
4241 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
4242 reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
4243 reply,
4244 reply_size);
4245 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
4246 reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
4247 wpa_s, buf + 14, reply, reply_size);
4248 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
4249 if (wpas_wps_cancel(wpa_s))
4250 reply_len = -1;
4251 #ifdef CONFIG_WPS_OOB
4252 } else if (os_strncmp(buf, "WPS_OOB ", 8) == 0) {
4253 if (wpa_supplicant_ctrl_iface_wps_oob(wpa_s, buf + 8))
4254 reply_len = -1;
4255 #endif /* CONFIG_WPS_OOB */
4256 #ifdef CONFIG_WPS_NFC
4257 } else if (os_strcmp(buf, "WPS_NFC") == 0) {
4258 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
4259 reply_len = -1;
4260 } else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
4261 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
4262 reply_len = -1;
4263 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
4264 reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
4265 wpa_s, buf + 14, reply, reply_size);
4266 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
4267 if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
4268 buf + 17))
4269 reply_len = -1;
4270 #endif /* CONFIG_WPS_NFC */
4271 } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
4272 if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
4273 reply_len = -1;
4274 #ifdef CONFIG_AP
4275 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
4276 reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
4277 wpa_s, buf + 11, reply, reply_size);
4278 #endif /* CONFIG_AP */
4279 #ifdef CONFIG_WPS_ER
4280 } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
4281 if (wpas_wps_er_start(wpa_s, NULL))
4282 reply_len = -1;
4283 } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
4284 if (wpas_wps_er_start(wpa_s, buf + 13))
4285 reply_len = -1;
4286 } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
4287 if (wpas_wps_er_stop(wpa_s))
4288 reply_len = -1;
4289 } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
4290 if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
4291 reply_len = -1;
4292 } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
4293 int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
4294 if (ret == -2) {
4295 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
4296 reply_len = 17;
4297 } else if (ret == -3) {
4298 os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
4299 reply_len = 18;
4300 } else if (ret == -4) {
4301 os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
4302 reply_len = 20;
4303 } else if (ret)
4304 reply_len = -1;
4305 } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
4306 if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
4307 reply_len = -1;
4308 } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
4309 if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
4310 buf + 18))
4311 reply_len = -1;
4312 } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
4313 if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
4314 reply_len = -1;
4315 #ifdef CONFIG_WPS_NFC
4316 } else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
4317 reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
4318 wpa_s, buf + 24, reply, reply_size);
4319 #endif /* CONFIG_WPS_NFC */
4320 #endif /* CONFIG_WPS_ER */
4321 #endif /* CONFIG_WPS */
4322 #ifdef CONFIG_IBSS_RSN
4323 } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
4324 if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
4325 reply_len = -1;
4326 #endif /* CONFIG_IBSS_RSN */
4327 #ifdef CONFIG_P2P
4328 } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
4329 if (p2p_ctrl_find(wpa_s, buf + 9))
4330 reply_len = -1;
4331 } else if (os_strcmp(buf, "P2P_FIND") == 0) {
4332 if (p2p_ctrl_find(wpa_s, ""))
4333 reply_len = -1;
4334 } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
4335 wpas_p2p_stop_find(wpa_s);
4336 } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
4337 reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
4338 reply_size);
4339 } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
4340 if (p2p_ctrl_listen(wpa_s, buf + 11))
4341 reply_len = -1;
4342 } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
4343 if (p2p_ctrl_listen(wpa_s, ""))
4344 reply_len = -1;
4345 } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
4346 if (wpas_p2p_group_remove(wpa_s, buf + 17))
4347 reply_len = -1;
4348 } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
4349 if (wpas_p2p_group_add(wpa_s, 0, 0, 0))
4350 reply_len = -1;
4351 } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
4352 if (p2p_ctrl_group_add(wpa_s, buf + 14))
4353 reply_len = -1;
4354 } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
4355 if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
4356 reply_len = -1;
4357 } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
4358 reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
4359 } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
4360 reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
4361 reply_size);
4362 } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
4363 if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
4364 reply_len = -1;
4365 } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
4366 if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
4367 reply_len = -1;
4368 } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
4369 wpas_p2p_sd_service_update(wpa_s);
4370 } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
4371 if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
4372 reply_len = -1;
4373 } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
4374 wpas_p2p_service_flush(wpa_s);
4375 } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
4376 if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
4377 reply_len = -1;
4378 } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
4379 if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
4380 reply_len = -1;
4381 } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
4382 if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
4383 reply_len = -1;
4384 } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
4385 if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
4386 reply_len = -1;
4387 } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
4388 reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
4389 reply_size);
4390 } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
4391 if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
4392 reply_len = -1;
4393 } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
4394 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
4395 wpa_s->force_long_sd = 0;
4396 if (wpa_s->global->p2p)
4397 p2p_flush(wpa_s->global->p2p);
4398 } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
4399 if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
4400 reply_len = -1;
4401 } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
4402 if (wpas_p2p_cancel(wpa_s))
4403 reply_len = -1;
4404 } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
4405 if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
4406 reply_len = -1;
4407 } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
4408 if (p2p_ctrl_presence_req(wpa_s, "") < 0)
4409 reply_len = -1;
4410 } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
4411 if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
4412 reply_len = -1;
4413 } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
4414 if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
4415 reply_len = -1;
4416 #endif /* CONFIG_P2P */
4417 #ifdef CONFIG_INTERWORKING
4418 } else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
4419 if (interworking_fetch_anqp(wpa_s) < 0)
4420 reply_len = -1;
4421 } else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
4422 interworking_stop_fetch_anqp(wpa_s);
4423 } else if (os_strncmp(buf, "INTERWORKING_SELECT", 19) == 0) {
4424 if (interworking_select(wpa_s, os_strstr(buf + 19, "auto") !=
4425 NULL) < 0)
4426 reply_len = -1;
4427 } else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
4428 if (ctrl_interworking_connect(wpa_s, buf + 21) < 0)
4429 reply_len = -1;
4430 } else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
4431 if (get_anqp(wpa_s, buf + 9) < 0)
4432 reply_len = -1;
4433 #endif /* CONFIG_INTERWORKING */
4434 #ifdef CONFIG_HS20
4435 } else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
4436 if (get_hs20_anqp(wpa_s, buf + 14) < 0)
4437 reply_len = -1;
4438 } else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
4439 if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
4440 reply_len = -1;
4441 #endif /* CONFIG_HS20 */
4442 } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
4443 {
4444 if (wpa_supplicant_ctrl_iface_ctrl_rsp(
4445 wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
4446 reply_len = -1;
4447 else
4448 ctrl_rsp = 1;
4449 } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
4450 if (wpa_supplicant_reload_configuration(wpa_s))
4451 reply_len = -1;
4452 } else if (os_strcmp(buf, "TERMINATE") == 0) {
4453 wpa_supplicant_terminate_proc(wpa_s->global);
4454 } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
4455 if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
4456 reply_len = -1;
4457 } else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
4458 reply_len = wpa_supplicant_ctrl_iface_blacklist(
4459 wpa_s, buf + 9, reply, reply_size);
4460 } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
4461 reply_len = wpa_supplicant_ctrl_iface_log_level(
4462 wpa_s, buf + 9, reply, reply_size);
4463 } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
4464 reply_len = wpa_supplicant_ctrl_iface_list_networks(
4465 wpa_s, reply, reply_size);
4466 } else if (os_strcmp(buf, "DISCONNECT") == 0) {
4467 #ifdef CONFIG_SME
4468 wpa_s->sme.prev_bssid_set = 0;
4469 #endif /* CONFIG_SME */
4470 wpa_s->reassociate = 0;
4471 wpa_s->disconnected = 1;
4472 wpa_supplicant_cancel_sched_scan(wpa_s);
4473 wpa_supplicant_cancel_scan(wpa_s);
4474 wpa_supplicant_deauthenticate(wpa_s,
4475 WLAN_REASON_DEAUTH_LEAVING);
4476 } else if (os_strcmp(buf, "SCAN") == 0) {
4477 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
4478 reply_len = -1;
4479 else {
4480 if (!wpa_s->scanning &&
4481 ((wpa_s->wpa_state <= WPA_SCANNING) ||
4482 (wpa_s->wpa_state == WPA_COMPLETED))) {
4483 wpa_s->normal_scans = 0;
4484 wpa_s->scan_req = 2;
4485 wpa_supplicant_req_scan(wpa_s, 0, 0);
4486 } else if (wpa_s->sched_scanning) {
4487 wpa_printf(MSG_DEBUG, "Stop ongoing "
4488 "sched_scan to allow requested "
4489 "full scan to proceed");
4490 wpa_supplicant_cancel_sched_scan(wpa_s);
4491 wpa_s->scan_req = 2;
4492 wpa_supplicant_req_scan(wpa_s, 0, 0);
4493 } else {
4494 wpa_printf(MSG_DEBUG, "Ongoing scan action - "
4495 "reject new request");
4496 reply_len = os_snprintf(reply, reply_size,
4497 "FAIL-BUSY\n");
4498 }
4499 }
4500 } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
4501 reply_len = wpa_supplicant_ctrl_iface_scan_results(
4502 wpa_s, reply, reply_size);
4503 } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
4504 if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
4505 reply_len = -1;
4506 } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
4507 if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
4508 reply_len = -1;
4509 } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
4510 if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
4511 reply_len = -1;
4512 } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
4513 reply_len = wpa_supplicant_ctrl_iface_add_network(
4514 wpa_s, reply, reply_size);
4515 } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
4516 if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
4517 reply_len = -1;
4518 } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
4519 if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
4520 reply_len = -1;
4521 } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
4522 reply_len = wpa_supplicant_ctrl_iface_get_network(
4523 wpa_s, buf + 12, reply, reply_size);
4524 } else if (os_strcmp(buf, "LIST_CREDS") == 0) {
4525 reply_len = wpa_supplicant_ctrl_iface_list_creds(
4526 wpa_s, reply, reply_size);
4527 } else if (os_strcmp(buf, "ADD_CRED") == 0) {
4528 reply_len = wpa_supplicant_ctrl_iface_add_cred(
4529 wpa_s, reply, reply_size);
4530 } else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
4531 if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
4532 reply_len = -1;
4533 } else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
4534 if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
4535 reply_len = -1;
4536 #ifndef CONFIG_NO_CONFIG_WRITE
4537 } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
4538 if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
4539 reply_len = -1;
4540 #endif /* CONFIG_NO_CONFIG_WRITE */
4541 } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
4542 reply_len = wpa_supplicant_ctrl_iface_get_capability(
4543 wpa_s, buf + 15, reply, reply_size);
4544 } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
4545 if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
4546 reply_len = -1;
4547 } else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
4548 if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
4549 reply_len = -1;
4550 } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
4551 reply_len = wpa_supplicant_global_iface_list(
4552 wpa_s->global, reply, reply_size);
4553 } else if (os_strcmp(buf, "INTERFACES") == 0) {
4554 reply_len = wpa_supplicant_global_iface_interfaces(
4555 wpa_s->global, reply, reply_size);
4556 } else if (os_strncmp(buf, "BSS ", 4) == 0) {
4557 reply_len = wpa_supplicant_ctrl_iface_bss(
4558 wpa_s, buf + 4, reply, reply_size);
4559 #ifdef CONFIG_AP
4560 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
4561 reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
4562 } else if (os_strncmp(buf, "STA ", 4) == 0) {
4563 reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
4564 reply_size);
4565 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
4566 reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
4567 reply_size);
4568 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
4569 if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
4570 reply_len = -1;
4571 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
4572 if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
4573 reply_len = -1;
4574 #endif /* CONFIG_AP */
4575 } else if (os_strcmp(buf, "SUSPEND") == 0) {
4576 wpas_notify_suspend(wpa_s->global);
4577 } else if (os_strcmp(buf, "RESUME") == 0) {
4578 wpas_notify_resume(wpa_s->global);
4579 } else if (os_strcmp(buf, "DROP_SA") == 0) {
4580 wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
4581 } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
4582 if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
4583 reply_len = -1;
4584 } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
4585 if (wpa_supplicant_ctrl_iface_sta_autoconnect(wpa_s, buf + 16))
4586 reply_len = -1;
4587 } else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
4588 if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
4589 reply_len = -1;
4590 } else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
4591 if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
4592 buf + 17))
4593 reply_len = -1;
4594 } else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
4595 if (wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10))
4596 reply_len = -1;
4597 #ifdef CONFIG_TDLS
4598 } else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
4599 if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
4600 reply_len = -1;
4601 } else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
4602 if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
4603 reply_len = -1;
4604 } else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
4605 if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
4606 reply_len = -1;
4607 #endif /* CONFIG_TDLS */
4608 } else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
4609 reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
4610 reply_size);
4611 #ifdef CONFIG_AUTOSCAN
4612 } else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
4613 if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
4614 reply_len = -1;
4615 #endif /* CONFIG_AUTOSCAN */
4616 } else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
4617 eapol_sm_request_reauth(wpa_s->eapol);
4618 } else {
4619 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
4620 reply_len = 16;
4621 }
4622
4623 if (reply_len < 0) {
4624 os_memcpy(reply, "FAIL\n", 5);
4625 reply_len = 5;
4626 }
4627
4628 if (ctrl_rsp)
4629 eapol_sm_notify_ctrl_response(wpa_s->eapol);
4630
4631 *resp_len = reply_len;
4632 return reply;
4633 }
4634
4635
4636 static int wpa_supplicant_global_iface_add(struct wpa_global *global,
4637 char *cmd)
4638 {
4639 struct wpa_interface iface;
4640 char *pos;
4641
4642 /*
4643 * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
4644 * TAB<bridge_ifname>
4645 */
4646 wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
4647
4648 os_memset(&iface, 0, sizeof(iface));
4649
4650 do {
4651 iface.ifname = pos = cmd;
4652 pos = os_strchr(pos, '\t');
4653 if (pos)
4654 *pos++ = '\0';
4655 if (iface.ifname[0] == '\0')
4656 return -1;
4657 if (pos == NULL)
4658 break;
4659
4660 iface.confname = pos;
4661 pos = os_strchr(pos, '\t');
4662 if (pos)
4663 *pos++ = '\0';
4664 if (iface.confname[0] == '\0')
4665 iface.confname = NULL;
4666 if (pos == NULL)
4667 break;
4668
4669 iface.driver = pos;
4670 pos = os_strchr(pos, '\t');
4671 if (pos)
4672 *pos++ = '\0';
4673 if (iface.driver[0] == '\0')
4674 iface.driver = NULL;
4675 if (pos == NULL)
4676 break;
4677
4678 iface.ctrl_interface = pos;
4679 pos = os_strchr(pos, '\t');
4680 if (pos)
4681 *pos++ = '\0';
4682 if (iface.ctrl_interface[0] == '\0')
4683 iface.ctrl_interface = NULL;
4684 if (pos == NULL)
4685 break;
4686
4687 iface.driver_param = pos;
4688 pos = os_strchr(pos, '\t');
4689 if (pos)
4690 *pos++ = '\0';
4691 if (iface.driver_param[0] == '\0')
4692 iface.driver_param = NULL;
4693 if (pos == NULL)
4694 break;
4695
4696 iface.bridge_ifname = pos;
4697 pos = os_strchr(pos, '\t');
4698 if (pos)
4699 *pos++ = '\0';
4700 if (iface.bridge_ifname[0] == '\0')
4701 iface.bridge_ifname = NULL;
4702 if (pos == NULL)
4703 break;
4704 } while (0);
4705
4706 if (wpa_supplicant_get_iface(global, iface.ifname))
4707 return -1;
4708
4709 return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
4710 }
4711
4712
4713 static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
4714 char *cmd)
4715 {
4716 struct wpa_supplicant *wpa_s;
4717
4718 wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
4719
4720 wpa_s = wpa_supplicant_get_iface(global, cmd);
4721 if (wpa_s == NULL)
4722 return -1;
4723 return wpa_supplicant_remove_iface(global, wpa_s, 0);
4724 }
4725
4726
4727 static void wpa_free_iface_info(struct wpa_interface_info *iface)
4728 {
4729 struct wpa_interface_info *prev;
4730
4731 while (iface) {
4732 prev = iface;
4733 iface = iface->next;
4734
4735 os_free(prev->ifname);
4736 os_free(prev->desc);
4737 os_free(prev);
4738 }
4739 }
4740
4741
4742 static int wpa_supplicant_global_iface_list(struct wpa_global *global,
4743 char *buf, int len)
4744 {
4745 int i, res;
4746 struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
4747 char *pos, *end;
4748
4749 for (i = 0; wpa_drivers[i]; i++) {
4750 struct wpa_driver_ops *drv = wpa_drivers[i];
4751 if (drv->get_interfaces == NULL)
4752 continue;
4753 tmp = drv->get_interfaces(global->drv_priv[i]);
4754 if (tmp == NULL)
4755 continue;
4756
4757 if (last == NULL)
4758 iface = last = tmp;
4759 else
4760 last->next = tmp;
4761 while (last->next)
4762 last = last->next;
4763 }
4764
4765 pos = buf;
4766 end = buf + len;
4767 for (tmp = iface; tmp; tmp = tmp->next) {
4768 res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
4769 tmp->drv_name, tmp->ifname,
4770 tmp->desc ? tmp->desc : "");
4771 if (res < 0 || res >= end - pos) {
4772 *pos = '\0';
4773 break;
4774 }
4775 pos += res;
4776 }
4777
4778 wpa_free_iface_info(iface);
4779
4780 return pos - buf;
4781 }
4782
4783
4784 static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
4785 char *buf, int len)
4786 {
4787 int res;
4788 char *pos, *end;
4789 struct wpa_supplicant *wpa_s;
4790
4791 wpa_s = global->ifaces;
4792 pos = buf;
4793 end = buf + len;
4794
4795 while (wpa_s) {
4796 res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
4797 if (res < 0 || res >= end - pos) {
4798 *pos = '\0';
4799 break;
4800 }
4801 pos += res;
4802 wpa_s = wpa_s->next;
4803 }
4804 return pos - buf;
4805 }
4806
4807
4808 char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
4809 char *buf, size_t *resp_len)
4810 {
4811 char *reply;
4812 const int reply_size = 2048;
4813 int reply_len;
4814 int level = MSG_DEBUG;
4815
4816 if (os_strcmp(buf, "PING") == 0)
4817 level = MSG_EXCESSIVE;
4818 wpa_hexdump_ascii(level, "RX global ctrl_iface",
4819 (const u8 *) buf, os_strlen(buf));
4820
4821 reply = os_malloc(reply_size);
4822 if (reply == NULL) {
4823 *resp_len = 1;
4824 return NULL;
4825 }
4826
4827 os_memcpy(reply, "OK\n", 3);
4828 reply_len = 3;
4829
4830 if (os_strcmp(buf, "PING") == 0) {
4831 os_memcpy(reply, "PONG\n", 5);
4832 reply_len = 5;
4833 } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
4834 if (wpa_supplicant_global_iface_add(global, buf + 14))
4835 reply_len = -1;
4836 } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
4837 if (wpa_supplicant_global_iface_remove(global, buf + 17))
4838 reply_len = -1;
4839 } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
4840 reply_len = wpa_supplicant_global_iface_list(
4841 global, reply, reply_size);
4842 } else if (os_strcmp(buf, "INTERFACES") == 0) {
4843 reply_len = wpa_supplicant_global_iface_interfaces(
4844 global, reply, reply_size);
4845 } else if (os_strcmp(buf, "TERMINATE") == 0) {
4846 wpa_supplicant_terminate_proc(global);
4847 } else if (os_strcmp(buf, "SUSPEND") == 0) {
4848 wpas_notify_suspend(global);
4849 } else if (os_strcmp(buf, "RESUME") == 0) {
4850 wpas_notify_resume(global);
4851 } else {
4852 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
4853 reply_len = 16;
4854 }
4855
4856 if (reply_len < 0) {
4857 os_memcpy(reply, "FAIL\n", 5);
4858 reply_len = 5;
4859 }
4860
4861 *resp_len = reply_len;
4862 return reply;
4863 }