]> git.ipfire.org Git - thirdparty/hostap.git/blob - wpa_supplicant/ctrl_iface.c
P2P: Add optional "ht40" argument for p2p_connect
[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 ret = os_snprintf(pos, end - pos, "hs20=1\n");
1129 if (ret < 0 || ret >= end - pos)
1130 return pos - buf;
1131 pos += ret;
1132 }
1133 #endif /* CONFIG_HS20 */
1134
1135 if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
1136 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1137 res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
1138 verbose);
1139 if (res >= 0)
1140 pos += res;
1141 }
1142
1143 res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
1144 if (res >= 0)
1145 pos += res;
1146
1147 return pos - buf;
1148 }
1149
1150
1151 static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
1152 char *cmd)
1153 {
1154 char *pos;
1155 int id;
1156 struct wpa_ssid *ssid;
1157 u8 bssid[ETH_ALEN];
1158
1159 /* cmd: "<network id> <BSSID>" */
1160 pos = os_strchr(cmd, ' ');
1161 if (pos == NULL)
1162 return -1;
1163 *pos++ = '\0';
1164 id = atoi(cmd);
1165 wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
1166 if (hwaddr_aton(pos, bssid)) {
1167 wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
1168 return -1;
1169 }
1170
1171 ssid = wpa_config_get_network(wpa_s->conf, id);
1172 if (ssid == NULL) {
1173 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
1174 "to update", id);
1175 return -1;
1176 }
1177
1178 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
1179 ssid->bssid_set = !is_zero_ether_addr(bssid);
1180
1181 return 0;
1182 }
1183
1184
1185 static int wpa_supplicant_ctrl_iface_blacklist(struct wpa_supplicant *wpa_s,
1186 char *cmd, char *buf,
1187 size_t buflen)
1188 {
1189 u8 bssid[ETH_ALEN];
1190 struct wpa_blacklist *e;
1191 char *pos, *end;
1192 int ret;
1193
1194 /* cmd: "BLACKLIST [<BSSID>]" */
1195 if (*cmd == '\0') {
1196 pos = buf;
1197 end = buf + buflen;
1198 e = wpa_s->blacklist;
1199 while (e) {
1200 ret = os_snprintf(pos, end - pos, MACSTR "\n",
1201 MAC2STR(e->bssid));
1202 if (ret < 0 || ret >= end - pos)
1203 return pos - buf;
1204 pos += ret;
1205 e = e->next;
1206 }
1207 return pos - buf;
1208 }
1209
1210 cmd++;
1211 if (os_strncmp(cmd, "clear", 5) == 0) {
1212 wpa_blacklist_clear(wpa_s);
1213 os_memcpy(buf, "OK\n", 3);
1214 return 3;
1215 }
1216
1217 wpa_printf(MSG_DEBUG, "CTRL_IFACE: BLACKLIST bssid='%s'", cmd);
1218 if (hwaddr_aton(cmd, bssid)) {
1219 wpa_printf(MSG_DEBUG, "CTRL_IFACE: invalid BSSID '%s'", cmd);
1220 return -1;
1221 }
1222
1223 /*
1224 * Add the BSSID twice, so its count will be 2, causing it to be
1225 * skipped when processing scan results.
1226 */
1227 ret = wpa_blacklist_add(wpa_s, bssid);
1228 if (ret != 0)
1229 return -1;
1230 ret = wpa_blacklist_add(wpa_s, bssid);
1231 if (ret != 0)
1232 return -1;
1233 os_memcpy(buf, "OK\n", 3);
1234 return 3;
1235 }
1236
1237
1238 extern int wpa_debug_level;
1239 extern int wpa_debug_timestamp;
1240
1241 static const char * debug_level_str(int level)
1242 {
1243 switch (level) {
1244 case MSG_EXCESSIVE:
1245 return "EXCESSIVE";
1246 case MSG_MSGDUMP:
1247 return "MSGDUMP";
1248 case MSG_DEBUG:
1249 return "DEBUG";
1250 case MSG_INFO:
1251 return "INFO";
1252 case MSG_WARNING:
1253 return "WARNING";
1254 case MSG_ERROR:
1255 return "ERROR";
1256 default:
1257 return "?";
1258 }
1259 }
1260
1261
1262 static int str_to_debug_level(const char *s)
1263 {
1264 if (os_strcasecmp(s, "EXCESSIVE") == 0)
1265 return MSG_EXCESSIVE;
1266 if (os_strcasecmp(s, "MSGDUMP") == 0)
1267 return MSG_MSGDUMP;
1268 if (os_strcasecmp(s, "DEBUG") == 0)
1269 return MSG_DEBUG;
1270 if (os_strcasecmp(s, "INFO") == 0)
1271 return MSG_INFO;
1272 if (os_strcasecmp(s, "WARNING") == 0)
1273 return MSG_WARNING;
1274 if (os_strcasecmp(s, "ERROR") == 0)
1275 return MSG_ERROR;
1276 return -1;
1277 }
1278
1279
1280 static int wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant *wpa_s,
1281 char *cmd, char *buf,
1282 size_t buflen)
1283 {
1284 char *pos, *end, *stamp;
1285 int ret;
1286
1287 if (cmd == NULL) {
1288 return -1;
1289 }
1290
1291 /* cmd: "LOG_LEVEL [<level>]" */
1292 if (*cmd == '\0') {
1293 pos = buf;
1294 end = buf + buflen;
1295 ret = os_snprintf(pos, end - pos, "Current level: %s\n"
1296 "Timestamp: %d\n",
1297 debug_level_str(wpa_debug_level),
1298 wpa_debug_timestamp);
1299 if (ret < 0 || ret >= end - pos)
1300 ret = 0;
1301
1302 return ret;
1303 }
1304
1305 while (*cmd == ' ')
1306 cmd++;
1307
1308 stamp = os_strchr(cmd, ' ');
1309 if (stamp) {
1310 *stamp++ = '\0';
1311 while (*stamp == ' ') {
1312 stamp++;
1313 }
1314 }
1315
1316 if (cmd && os_strlen(cmd)) {
1317 int level = str_to_debug_level(cmd);
1318 if (level < 0)
1319 return -1;
1320 wpa_debug_level = level;
1321 }
1322
1323 if (stamp && os_strlen(stamp))
1324 wpa_debug_timestamp = atoi(stamp);
1325
1326 os_memcpy(buf, "OK\n", 3);
1327 return 3;
1328 }
1329
1330
1331 static int wpa_supplicant_ctrl_iface_list_networks(
1332 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
1333 {
1334 char *pos, *end;
1335 struct wpa_ssid *ssid;
1336 int ret;
1337
1338 pos = buf;
1339 end = buf + buflen;
1340 ret = os_snprintf(pos, end - pos,
1341 "network id / ssid / bssid / flags\n");
1342 if (ret < 0 || ret >= end - pos)
1343 return pos - buf;
1344 pos += ret;
1345
1346 ssid = wpa_s->conf->ssid;
1347 while (ssid) {
1348 ret = os_snprintf(pos, end - pos, "%d\t%s",
1349 ssid->id,
1350 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1351 if (ret < 0 || ret >= end - pos)
1352 return pos - buf;
1353 pos += ret;
1354 if (ssid->bssid_set) {
1355 ret = os_snprintf(pos, end - pos, "\t" MACSTR,
1356 MAC2STR(ssid->bssid));
1357 } else {
1358 ret = os_snprintf(pos, end - pos, "\tany");
1359 }
1360 if (ret < 0 || ret >= end - pos)
1361 return pos - buf;
1362 pos += ret;
1363 ret = os_snprintf(pos, end - pos, "\t%s%s%s",
1364 ssid == wpa_s->current_ssid ?
1365 "[CURRENT]" : "",
1366 ssid->disabled ? "[DISABLED]" : "",
1367 ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
1368 "");
1369 if (ret < 0 || ret >= end - pos)
1370 return pos - buf;
1371 pos += ret;
1372 ret = os_snprintf(pos, end - pos, "\n");
1373 if (ret < 0 || ret >= end - pos)
1374 return pos - buf;
1375 pos += ret;
1376
1377 ssid = ssid->next;
1378 }
1379
1380 return pos - buf;
1381 }
1382
1383
1384 static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
1385 {
1386 int first = 1, ret;
1387 ret = os_snprintf(pos, end - pos, "-");
1388 if (ret < 0 || ret >= end - pos)
1389 return pos;
1390 pos += ret;
1391 if (cipher & WPA_CIPHER_NONE) {
1392 ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : "+");
1393 if (ret < 0 || ret >= end - pos)
1394 return pos;
1395 pos += ret;
1396 first = 0;
1397 }
1398 if (cipher & WPA_CIPHER_WEP40) {
1399 ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : "+");
1400 if (ret < 0 || ret >= end - pos)
1401 return pos;
1402 pos += ret;
1403 first = 0;
1404 }
1405 if (cipher & WPA_CIPHER_WEP104) {
1406 ret = os_snprintf(pos, end - pos, "%sWEP104",
1407 first ? "" : "+");
1408 if (ret < 0 || ret >= end - pos)
1409 return pos;
1410 pos += ret;
1411 first = 0;
1412 }
1413 if (cipher & WPA_CIPHER_TKIP) {
1414 ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : "+");
1415 if (ret < 0 || ret >= end - pos)
1416 return pos;
1417 pos += ret;
1418 first = 0;
1419 }
1420 if (cipher & WPA_CIPHER_CCMP) {
1421 ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : "+");
1422 if (ret < 0 || ret >= end - pos)
1423 return pos;
1424 pos += ret;
1425 first = 0;
1426 }
1427 return pos;
1428 }
1429
1430
1431 static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
1432 const u8 *ie, size_t ie_len)
1433 {
1434 struct wpa_ie_data data;
1435 int first, ret;
1436
1437 ret = os_snprintf(pos, end - pos, "[%s-", proto);
1438 if (ret < 0 || ret >= end - pos)
1439 return pos;
1440 pos += ret;
1441
1442 if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
1443 ret = os_snprintf(pos, end - pos, "?]");
1444 if (ret < 0 || ret >= end - pos)
1445 return pos;
1446 pos += ret;
1447 return pos;
1448 }
1449
1450 first = 1;
1451 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
1452 ret = os_snprintf(pos, end - pos, "%sEAP", first ? "" : "+");
1453 if (ret < 0 || ret >= end - pos)
1454 return pos;
1455 pos += ret;
1456 first = 0;
1457 }
1458 if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
1459 ret = os_snprintf(pos, end - pos, "%sPSK", first ? "" : "+");
1460 if (ret < 0 || ret >= end - pos)
1461 return pos;
1462 pos += ret;
1463 first = 0;
1464 }
1465 if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
1466 ret = os_snprintf(pos, end - pos, "%sNone", first ? "" : "+");
1467 if (ret < 0 || ret >= end - pos)
1468 return pos;
1469 pos += ret;
1470 first = 0;
1471 }
1472 #ifdef CONFIG_IEEE80211R
1473 if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
1474 ret = os_snprintf(pos, end - pos, "%sFT/EAP",
1475 first ? "" : "+");
1476 if (ret < 0 || ret >= end - pos)
1477 return pos;
1478 pos += ret;
1479 first = 0;
1480 }
1481 if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
1482 ret = os_snprintf(pos, end - pos, "%sFT/PSK",
1483 first ? "" : "+");
1484 if (ret < 0 || ret >= end - pos)
1485 return pos;
1486 pos += ret;
1487 first = 0;
1488 }
1489 #endif /* CONFIG_IEEE80211R */
1490 #ifdef CONFIG_IEEE80211W
1491 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1492 ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
1493 first ? "" : "+");
1494 if (ret < 0 || ret >= end - pos)
1495 return pos;
1496 pos += ret;
1497 first = 0;
1498 }
1499 if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
1500 ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
1501 first ? "" : "+");
1502 if (ret < 0 || ret >= end - pos)
1503 return pos;
1504 pos += ret;
1505 first = 0;
1506 }
1507 #endif /* CONFIG_IEEE80211W */
1508
1509 pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
1510
1511 if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
1512 ret = os_snprintf(pos, end - pos, "-preauth");
1513 if (ret < 0 || ret >= end - pos)
1514 return pos;
1515 pos += ret;
1516 }
1517
1518 ret = os_snprintf(pos, end - pos, "]");
1519 if (ret < 0 || ret >= end - pos)
1520 return pos;
1521 pos += ret;
1522
1523 return pos;
1524 }
1525
1526
1527 #ifdef CONFIG_WPS
1528 static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
1529 char *pos, char *end,
1530 struct wpabuf *wps_ie)
1531 {
1532 int ret;
1533 const char *txt;
1534
1535 if (wps_ie == NULL)
1536 return pos;
1537 if (wps_is_selected_pbc_registrar(wps_ie))
1538 txt = "[WPS-PBC]";
1539 #ifdef CONFIG_WPS2
1540 else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
1541 txt = "[WPS-AUTH]";
1542 #endif /* CONFIG_WPS2 */
1543 else if (wps_is_selected_pin_registrar(wps_ie))
1544 txt = "[WPS-PIN]";
1545 else
1546 txt = "[WPS]";
1547
1548 ret = os_snprintf(pos, end - pos, "%s", txt);
1549 if (ret >= 0 && ret < end - pos)
1550 pos += ret;
1551 wpabuf_free(wps_ie);
1552 return pos;
1553 }
1554 #endif /* CONFIG_WPS */
1555
1556
1557 static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
1558 char *pos, char *end,
1559 const struct wpa_bss *bss)
1560 {
1561 #ifdef CONFIG_WPS
1562 struct wpabuf *wps_ie;
1563 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1564 return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
1565 #else /* CONFIG_WPS */
1566 return pos;
1567 #endif /* CONFIG_WPS */
1568 }
1569
1570
1571 /* Format one result on one text line into a buffer. */
1572 static int wpa_supplicant_ctrl_iface_scan_result(
1573 struct wpa_supplicant *wpa_s,
1574 const struct wpa_bss *bss, char *buf, size_t buflen)
1575 {
1576 char *pos, *end;
1577 int ret;
1578 const u8 *ie, *ie2, *p2p;
1579
1580 p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
1581 if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
1582 os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
1583 0)
1584 return 0; /* Do not show P2P listen discovery results here */
1585
1586 pos = buf;
1587 end = buf + buflen;
1588
1589 ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
1590 MAC2STR(bss->bssid), bss->freq, bss->level);
1591 if (ret < 0 || ret >= end - pos)
1592 return -1;
1593 pos += ret;
1594 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
1595 if (ie)
1596 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
1597 ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
1598 if (ie2)
1599 pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
1600 pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
1601 if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
1602 ret = os_snprintf(pos, end - pos, "[WEP]");
1603 if (ret < 0 || ret >= end - pos)
1604 return -1;
1605 pos += ret;
1606 }
1607 if (bss->caps & IEEE80211_CAP_IBSS) {
1608 ret = os_snprintf(pos, end - pos, "[IBSS]");
1609 if (ret < 0 || ret >= end - pos)
1610 return -1;
1611 pos += ret;
1612 }
1613 if (bss->caps & IEEE80211_CAP_ESS) {
1614 ret = os_snprintf(pos, end - pos, "[ESS]");
1615 if (ret < 0 || ret >= end - pos)
1616 return -1;
1617 pos += ret;
1618 }
1619 if (p2p) {
1620 ret = os_snprintf(pos, end - pos, "[P2P]");
1621 if (ret < 0 || ret >= end - pos)
1622 return -1;
1623 pos += ret;
1624 }
1625 #ifdef CONFIG_HS20
1626 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
1627 ret = os_snprintf(pos, end - pos, "[HS20]");
1628 if (ret < 0 || ret >= end - pos)
1629 return -1;
1630 pos += ret;
1631 }
1632 #endif /* CONFIG_HS20 */
1633
1634 ret = os_snprintf(pos, end - pos, "\t%s",
1635 wpa_ssid_txt(bss->ssid, bss->ssid_len));
1636 if (ret < 0 || ret >= end - pos)
1637 return -1;
1638 pos += ret;
1639
1640 ret = os_snprintf(pos, end - pos, "\n");
1641 if (ret < 0 || ret >= end - pos)
1642 return -1;
1643 pos += ret;
1644
1645 return pos - buf;
1646 }
1647
1648
1649 static int wpa_supplicant_ctrl_iface_scan_results(
1650 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
1651 {
1652 char *pos, *end;
1653 struct wpa_bss *bss;
1654 int ret;
1655
1656 pos = buf;
1657 end = buf + buflen;
1658 ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
1659 "flags / ssid\n");
1660 if (ret < 0 || ret >= end - pos)
1661 return pos - buf;
1662 pos += ret;
1663
1664 dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
1665 ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
1666 end - pos);
1667 if (ret < 0 || ret >= end - pos)
1668 return pos - buf;
1669 pos += ret;
1670 }
1671
1672 return pos - buf;
1673 }
1674
1675
1676 static int wpa_supplicant_ctrl_iface_select_network(
1677 struct wpa_supplicant *wpa_s, char *cmd)
1678 {
1679 int id;
1680 struct wpa_ssid *ssid;
1681
1682 /* cmd: "<network id>" or "any" */
1683 if (os_strcmp(cmd, "any") == 0) {
1684 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
1685 ssid = NULL;
1686 } else {
1687 id = atoi(cmd);
1688 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
1689
1690 ssid = wpa_config_get_network(wpa_s->conf, id);
1691 if (ssid == NULL) {
1692 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
1693 "network id=%d", id);
1694 return -1;
1695 }
1696 if (ssid->disabled == 2) {
1697 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
1698 "SELECT_NETWORK with persistent P2P group");
1699 return -1;
1700 }
1701 }
1702
1703 wpa_supplicant_select_network(wpa_s, ssid);
1704
1705 return 0;
1706 }
1707
1708
1709 static int wpa_supplicant_ctrl_iface_enable_network(
1710 struct wpa_supplicant *wpa_s, char *cmd)
1711 {
1712 int id;
1713 struct wpa_ssid *ssid;
1714
1715 /* cmd: "<network id>" or "all" */
1716 if (os_strcmp(cmd, "all") == 0) {
1717 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
1718 ssid = NULL;
1719 } else {
1720 id = atoi(cmd);
1721 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
1722
1723 ssid = wpa_config_get_network(wpa_s->conf, id);
1724 if (ssid == NULL) {
1725 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
1726 "network id=%d", id);
1727 return -1;
1728 }
1729 if (ssid->disabled == 2) {
1730 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
1731 "ENABLE_NETWORK with persistent P2P group");
1732 return -1;
1733 }
1734
1735 if (os_strstr(cmd, " no-connect")) {
1736 ssid->disabled = 0;
1737 return 0;
1738 }
1739 }
1740 wpa_supplicant_enable_network(wpa_s, ssid);
1741
1742 return 0;
1743 }
1744
1745
1746 static int wpa_supplicant_ctrl_iface_disable_network(
1747 struct wpa_supplicant *wpa_s, char *cmd)
1748 {
1749 int id;
1750 struct wpa_ssid *ssid;
1751
1752 /* cmd: "<network id>" or "all" */
1753 if (os_strcmp(cmd, "all") == 0) {
1754 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
1755 ssid = NULL;
1756 } else {
1757 id = atoi(cmd);
1758 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
1759
1760 ssid = wpa_config_get_network(wpa_s->conf, id);
1761 if (ssid == NULL) {
1762 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
1763 "network id=%d", id);
1764 return -1;
1765 }
1766 if (ssid->disabled == 2) {
1767 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
1768 "DISABLE_NETWORK with persistent P2P "
1769 "group");
1770 return -1;
1771 }
1772 }
1773 wpa_supplicant_disable_network(wpa_s, ssid);
1774
1775 return 0;
1776 }
1777
1778
1779 static int wpa_supplicant_ctrl_iface_add_network(
1780 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
1781 {
1782 struct wpa_ssid *ssid;
1783 int ret;
1784
1785 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
1786
1787 ssid = wpa_config_add_network(wpa_s->conf);
1788 if (ssid == NULL)
1789 return -1;
1790
1791 wpas_notify_network_added(wpa_s, ssid);
1792
1793 ssid->disabled = 1;
1794 wpa_config_set_network_defaults(ssid);
1795
1796 ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
1797 if (ret < 0 || (size_t) ret >= buflen)
1798 return -1;
1799 return ret;
1800 }
1801
1802
1803 static int wpa_supplicant_ctrl_iface_remove_network(
1804 struct wpa_supplicant *wpa_s, char *cmd)
1805 {
1806 int id;
1807 struct wpa_ssid *ssid;
1808
1809 /* cmd: "<network id>" or "all" */
1810 if (os_strcmp(cmd, "all") == 0) {
1811 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
1812 ssid = wpa_s->conf->ssid;
1813 while (ssid) {
1814 struct wpa_ssid *remove_ssid = ssid;
1815 id = ssid->id;
1816 ssid = ssid->next;
1817 wpas_notify_network_removed(wpa_s, remove_ssid);
1818 wpa_config_remove_network(wpa_s->conf, id);
1819 }
1820 eapol_sm_invalidate_cached_session(wpa_s->eapol);
1821 if (wpa_s->current_ssid) {
1822 #ifdef CONFIG_SME
1823 wpa_s->sme.prev_bssid_set = 0;
1824 #endif /* CONFIG_SME */
1825 wpa_sm_set_config(wpa_s->wpa, NULL);
1826 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1827 wpa_supplicant_disassociate(wpa_s,
1828 WLAN_REASON_DEAUTH_LEAVING);
1829 }
1830 return 0;
1831 }
1832
1833 id = atoi(cmd);
1834 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
1835
1836 ssid = wpa_config_get_network(wpa_s->conf, id);
1837 if (ssid)
1838 wpas_notify_network_removed(wpa_s, ssid);
1839 if (ssid == NULL ||
1840 wpa_config_remove_network(wpa_s->conf, id) < 0) {
1841 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
1842 "id=%d", id);
1843 return -1;
1844 }
1845
1846 if (ssid == wpa_s->current_ssid || wpa_s->current_ssid == NULL) {
1847 #ifdef CONFIG_SME
1848 wpa_s->sme.prev_bssid_set = 0;
1849 #endif /* CONFIG_SME */
1850 /*
1851 * Invalidate the EAP session cache if the current or
1852 * previously used network is removed.
1853 */
1854 eapol_sm_invalidate_cached_session(wpa_s->eapol);
1855 }
1856
1857 if (ssid == wpa_s->current_ssid) {
1858 wpa_sm_set_config(wpa_s->wpa, NULL);
1859 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1860
1861 wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1862 }
1863
1864 return 0;
1865 }
1866
1867
1868 static int wpa_supplicant_ctrl_iface_set_network(
1869 struct wpa_supplicant *wpa_s, char *cmd)
1870 {
1871 int id;
1872 struct wpa_ssid *ssid;
1873 char *name, *value;
1874
1875 /* cmd: "<network id> <variable name> <value>" */
1876 name = os_strchr(cmd, ' ');
1877 if (name == NULL)
1878 return -1;
1879 *name++ = '\0';
1880
1881 value = os_strchr(name, ' ');
1882 if (value == NULL)
1883 return -1;
1884 *value++ = '\0';
1885
1886 id = atoi(cmd);
1887 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
1888 id, name);
1889 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
1890 (u8 *) value, os_strlen(value));
1891
1892 ssid = wpa_config_get_network(wpa_s->conf, id);
1893 if (ssid == NULL) {
1894 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
1895 "id=%d", id);
1896 return -1;
1897 }
1898
1899 if (wpa_config_set(ssid, name, value, 0) < 0) {
1900 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
1901 "variable '%s'", name);
1902 return -1;
1903 }
1904
1905 wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
1906
1907 if (wpa_s->current_ssid == ssid || wpa_s->current_ssid == NULL) {
1908 /*
1909 * Invalidate the EAP session cache if anything in the current
1910 * or previously used configuration changes.
1911 */
1912 eapol_sm_invalidate_cached_session(wpa_s->eapol);
1913 }
1914
1915 if ((os_strcmp(name, "psk") == 0 &&
1916 value[0] == '"' && ssid->ssid_len) ||
1917 (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
1918 wpa_config_update_psk(ssid);
1919 else if (os_strcmp(name, "priority") == 0)
1920 wpa_config_update_prio_list(wpa_s->conf);
1921
1922 return 0;
1923 }
1924
1925
1926 static int wpa_supplicant_ctrl_iface_get_network(
1927 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
1928 {
1929 int id;
1930 size_t res;
1931 struct wpa_ssid *ssid;
1932 char *name, *value;
1933
1934 /* cmd: "<network id> <variable name>" */
1935 name = os_strchr(cmd, ' ');
1936 if (name == NULL || buflen == 0)
1937 return -1;
1938 *name++ = '\0';
1939
1940 id = atoi(cmd);
1941 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
1942 id, name);
1943
1944 ssid = wpa_config_get_network(wpa_s->conf, id);
1945 if (ssid == NULL) {
1946 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
1947 "id=%d", id);
1948 return -1;
1949 }
1950
1951 value = wpa_config_get_no_key(ssid, name);
1952 if (value == NULL) {
1953 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
1954 "variable '%s'", name);
1955 return -1;
1956 }
1957
1958 res = os_strlcpy(buf, value, buflen);
1959 if (res >= buflen) {
1960 os_free(value);
1961 return -1;
1962 }
1963
1964 os_free(value);
1965
1966 return res;
1967 }
1968
1969
1970 static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
1971 char *buf, size_t buflen)
1972 {
1973 char *pos, *end;
1974 struct wpa_cred *cred;
1975 int ret;
1976
1977 pos = buf;
1978 end = buf + buflen;
1979 ret = os_snprintf(pos, end - pos,
1980 "cred id / realm / username / domain / imsi\n");
1981 if (ret < 0 || ret >= end - pos)
1982 return pos - buf;
1983 pos += ret;
1984
1985 cred = wpa_s->conf->cred;
1986 while (cred) {
1987 ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
1988 cred->id, cred->realm ? cred->realm : "",
1989 cred->username ? cred->username : "",
1990 cred->domain ? cred->domain : "",
1991 cred->imsi ? cred->imsi : "");
1992 if (ret < 0 || ret >= end - pos)
1993 return pos - buf;
1994 pos += ret;
1995
1996 cred = cred->next;
1997 }
1998
1999 return pos - buf;
2000 }
2001
2002
2003 static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
2004 char *buf, size_t buflen)
2005 {
2006 struct wpa_cred *cred;
2007 int ret;
2008
2009 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
2010
2011 cred = wpa_config_add_cred(wpa_s->conf);
2012 if (cred == NULL)
2013 return -1;
2014
2015 ret = os_snprintf(buf, buflen, "%d\n", cred->id);
2016 if (ret < 0 || (size_t) ret >= buflen)
2017 return -1;
2018 return ret;
2019 }
2020
2021
2022 static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
2023 char *cmd)
2024 {
2025 int id;
2026 struct wpa_cred *cred;
2027
2028 /* cmd: "<cred id>" or "all" */
2029 if (os_strcmp(cmd, "all") == 0) {
2030 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
2031 cred = wpa_s->conf->cred;
2032 while (cred) {
2033 id = cred->id;
2034 cred = cred->next;
2035 wpa_config_remove_cred(wpa_s->conf, id);
2036 }
2037 return 0;
2038 }
2039
2040 id = atoi(cmd);
2041 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
2042
2043 cred = wpa_config_get_cred(wpa_s->conf, id);
2044 if (cred == NULL ||
2045 wpa_config_remove_cred(wpa_s->conf, id) < 0) {
2046 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
2047 id);
2048 return -1;
2049 }
2050
2051 return 0;
2052 }
2053
2054
2055 static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
2056 char *cmd)
2057 {
2058 int id;
2059 struct wpa_cred *cred;
2060 char *name, *value;
2061
2062 /* cmd: "<cred id> <variable name> <value>" */
2063 name = os_strchr(cmd, ' ');
2064 if (name == NULL)
2065 return -1;
2066 *name++ = '\0';
2067
2068 value = os_strchr(name, ' ');
2069 if (value == NULL)
2070 return -1;
2071 *value++ = '\0';
2072
2073 id = atoi(cmd);
2074 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
2075 id, name);
2076 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
2077 (u8 *) value, os_strlen(value));
2078
2079 cred = wpa_config_get_cred(wpa_s->conf, id);
2080 if (cred == NULL) {
2081 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
2082 id);
2083 return -1;
2084 }
2085
2086 if (wpa_config_set_cred(cred, name, value, 0) < 0) {
2087 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
2088 "variable '%s'", name);
2089 return -1;
2090 }
2091
2092 return 0;
2093 }
2094
2095
2096 #ifndef CONFIG_NO_CONFIG_WRITE
2097 static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
2098 {
2099 int ret;
2100
2101 if (!wpa_s->conf->update_config) {
2102 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
2103 "to update configuration (update_config=0)");
2104 return -1;
2105 }
2106
2107 ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
2108 if (ret) {
2109 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
2110 "update configuration");
2111 } else {
2112 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
2113 " updated");
2114 }
2115
2116 return ret;
2117 }
2118 #endif /* CONFIG_NO_CONFIG_WRITE */
2119
2120
2121 static int ctrl_iface_get_capability_pairwise(int res, char *strict,
2122 struct wpa_driver_capa *capa,
2123 char *buf, size_t buflen)
2124 {
2125 int ret, first = 1;
2126 char *pos, *end;
2127 size_t len;
2128
2129 pos = buf;
2130 end = pos + buflen;
2131
2132 if (res < 0) {
2133 if (strict)
2134 return 0;
2135 len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
2136 if (len >= buflen)
2137 return -1;
2138 return len;
2139 }
2140
2141 if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
2142 ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
2143 if (ret < 0 || ret >= end - pos)
2144 return pos - buf;
2145 pos += ret;
2146 first = 0;
2147 }
2148
2149 if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
2150 ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
2151 if (ret < 0 || ret >= end - pos)
2152 return pos - buf;
2153 pos += ret;
2154 first = 0;
2155 }
2156
2157 if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
2158 ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : " ");
2159 if (ret < 0 || ret >= end - pos)
2160 return pos - buf;
2161 pos += ret;
2162 first = 0;
2163 }
2164
2165 return pos - buf;
2166 }
2167
2168
2169 static int ctrl_iface_get_capability_group(int res, char *strict,
2170 struct wpa_driver_capa *capa,
2171 char *buf, size_t buflen)
2172 {
2173 int ret, first = 1;
2174 char *pos, *end;
2175 size_t len;
2176
2177 pos = buf;
2178 end = pos + buflen;
2179
2180 if (res < 0) {
2181 if (strict)
2182 return 0;
2183 len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
2184 if (len >= buflen)
2185 return -1;
2186 return len;
2187 }
2188
2189 if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
2190 ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
2191 if (ret < 0 || ret >= end - pos)
2192 return pos - buf;
2193 pos += ret;
2194 first = 0;
2195 }
2196
2197 if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
2198 ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
2199 if (ret < 0 || ret >= end - pos)
2200 return pos - buf;
2201 pos += ret;
2202 first = 0;
2203 }
2204
2205 if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP104) {
2206 ret = os_snprintf(pos, end - pos, "%sWEP104",
2207 first ? "" : " ");
2208 if (ret < 0 || ret >= end - pos)
2209 return pos - buf;
2210 pos += ret;
2211 first = 0;
2212 }
2213
2214 if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP40) {
2215 ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : " ");
2216 if (ret < 0 || ret >= end - pos)
2217 return pos - buf;
2218 pos += ret;
2219 first = 0;
2220 }
2221
2222 return pos - buf;
2223 }
2224
2225
2226 static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
2227 struct wpa_driver_capa *capa,
2228 char *buf, size_t buflen)
2229 {
2230 int ret;
2231 char *pos, *end;
2232 size_t len;
2233
2234 pos = buf;
2235 end = pos + buflen;
2236
2237 if (res < 0) {
2238 if (strict)
2239 return 0;
2240 len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
2241 "NONE", buflen);
2242 if (len >= buflen)
2243 return -1;
2244 return len;
2245 }
2246
2247 ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
2248 if (ret < 0 || ret >= end - pos)
2249 return pos - buf;
2250 pos += ret;
2251
2252 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
2253 WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
2254 ret = os_snprintf(pos, end - pos, " WPA-EAP");
2255 if (ret < 0 || ret >= end - pos)
2256 return pos - buf;
2257 pos += ret;
2258 }
2259
2260 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
2261 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
2262 ret = os_snprintf(pos, end - pos, " WPA-PSK");
2263 if (ret < 0 || ret >= end - pos)
2264 return pos - buf;
2265 pos += ret;
2266 }
2267
2268 if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
2269 ret = os_snprintf(pos, end - pos, " WPA-NONE");
2270 if (ret < 0 || ret >= end - pos)
2271 return pos - buf;
2272 pos += ret;
2273 }
2274
2275 return pos - buf;
2276 }
2277
2278
2279 static int ctrl_iface_get_capability_proto(int res, char *strict,
2280 struct wpa_driver_capa *capa,
2281 char *buf, size_t buflen)
2282 {
2283 int ret, first = 1;
2284 char *pos, *end;
2285 size_t len;
2286
2287 pos = buf;
2288 end = pos + buflen;
2289
2290 if (res < 0) {
2291 if (strict)
2292 return 0;
2293 len = os_strlcpy(buf, "RSN WPA", buflen);
2294 if (len >= buflen)
2295 return -1;
2296 return len;
2297 }
2298
2299 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
2300 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
2301 ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
2302 if (ret < 0 || ret >= end - pos)
2303 return pos - buf;
2304 pos += ret;
2305 first = 0;
2306 }
2307
2308 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
2309 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
2310 ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
2311 if (ret < 0 || ret >= end - pos)
2312 return pos - buf;
2313 pos += ret;
2314 first = 0;
2315 }
2316
2317 return pos - buf;
2318 }
2319
2320
2321 static int ctrl_iface_get_capability_auth_alg(int res, char *strict,
2322 struct wpa_driver_capa *capa,
2323 char *buf, size_t buflen)
2324 {
2325 int ret, first = 1;
2326 char *pos, *end;
2327 size_t len;
2328
2329 pos = buf;
2330 end = pos + buflen;
2331
2332 if (res < 0) {
2333 if (strict)
2334 return 0;
2335 len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
2336 if (len >= buflen)
2337 return -1;
2338 return len;
2339 }
2340
2341 if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
2342 ret = os_snprintf(pos, end - pos, "%sOPEN", first ? "" : " ");
2343 if (ret < 0 || ret >= end - pos)
2344 return pos - buf;
2345 pos += ret;
2346 first = 0;
2347 }
2348
2349 if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
2350 ret = os_snprintf(pos, end - pos, "%sSHARED",
2351 first ? "" : " ");
2352 if (ret < 0 || ret >= end - pos)
2353 return pos - buf;
2354 pos += ret;
2355 first = 0;
2356 }
2357
2358 if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
2359 ret = os_snprintf(pos, end - pos, "%sLEAP", first ? "" : " ");
2360 if (ret < 0 || ret >= end - pos)
2361 return pos - buf;
2362 pos += ret;
2363 first = 0;
2364 }
2365
2366 return pos - buf;
2367 }
2368
2369
2370 static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
2371 char *buf, size_t buflen)
2372 {
2373 struct hostapd_channel_data *chnl;
2374 int ret, i, j;
2375 char *pos, *end, *hmode;
2376
2377 pos = buf;
2378 end = pos + buflen;
2379
2380 for (j = 0; j < wpa_s->hw.num_modes; j++) {
2381 switch (wpa_s->hw.modes[j].mode) {
2382 case HOSTAPD_MODE_IEEE80211B:
2383 hmode = "B";
2384 break;
2385 case HOSTAPD_MODE_IEEE80211G:
2386 hmode = "G";
2387 break;
2388 case HOSTAPD_MODE_IEEE80211A:
2389 hmode = "A";
2390 break;
2391 default:
2392 continue;
2393 }
2394 ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
2395 if (ret < 0 || ret >= end - pos)
2396 return pos - buf;
2397 pos += ret;
2398 chnl = wpa_s->hw.modes[j].channels;
2399 for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
2400 if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
2401 continue;
2402 ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
2403 if (ret < 0 || ret >= end - pos)
2404 return pos - buf;
2405 pos += ret;
2406 }
2407 ret = os_snprintf(pos, end - pos, "\n");
2408 if (ret < 0 || ret >= end - pos)
2409 return pos - buf;
2410 pos += ret;
2411 }
2412
2413 return pos - buf;
2414 }
2415
2416
2417 static int wpa_supplicant_ctrl_iface_get_capability(
2418 struct wpa_supplicant *wpa_s, const char *_field, char *buf,
2419 size_t buflen)
2420 {
2421 struct wpa_driver_capa capa;
2422 int res;
2423 char *strict;
2424 char field[30];
2425 size_t len;
2426
2427 /* Determine whether or not strict checking was requested */
2428 len = os_strlcpy(field, _field, sizeof(field));
2429 if (len >= sizeof(field))
2430 return -1;
2431 strict = os_strchr(field, ' ');
2432 if (strict != NULL) {
2433 *strict++ = '\0';
2434 if (os_strcmp(strict, "strict") != 0)
2435 return -1;
2436 }
2437
2438 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
2439 field, strict ? strict : "");
2440
2441 if (os_strcmp(field, "eap") == 0) {
2442 return eap_get_names(buf, buflen);
2443 }
2444
2445 res = wpa_drv_get_capa(wpa_s, &capa);
2446
2447 if (os_strcmp(field, "pairwise") == 0)
2448 return ctrl_iface_get_capability_pairwise(res, strict, &capa,
2449 buf, buflen);
2450
2451 if (os_strcmp(field, "group") == 0)
2452 return ctrl_iface_get_capability_group(res, strict, &capa,
2453 buf, buflen);
2454
2455 if (os_strcmp(field, "key_mgmt") == 0)
2456 return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
2457 buf, buflen);
2458
2459 if (os_strcmp(field, "proto") == 0)
2460 return ctrl_iface_get_capability_proto(res, strict, &capa,
2461 buf, buflen);
2462
2463 if (os_strcmp(field, "auth_alg") == 0)
2464 return ctrl_iface_get_capability_auth_alg(res, strict, &capa,
2465 buf, buflen);
2466
2467 if (os_strcmp(field, "channels") == 0)
2468 return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
2469
2470 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
2471 field);
2472
2473 return -1;
2474 }
2475
2476
2477 #ifdef CONFIG_INTERWORKING
2478 static char * anqp_add_hex(char *pos, char *end, const char *title,
2479 struct wpabuf *data)
2480 {
2481 char *start = pos;
2482 size_t i;
2483 int ret;
2484 const u8 *d;
2485
2486 if (data == NULL)
2487 return start;
2488
2489 ret = os_snprintf(pos, end - pos, "%s=", title);
2490 if (ret < 0 || ret >= end - pos)
2491 return start;
2492 pos += ret;
2493
2494 d = wpabuf_head_u8(data);
2495 for (i = 0; i < wpabuf_len(data); i++) {
2496 ret = os_snprintf(pos, end - pos, "%02x", *d++);
2497 if (ret < 0 || ret >= end - pos)
2498 return start;
2499 pos += ret;
2500 }
2501
2502 ret = os_snprintf(pos, end - pos, "\n");
2503 if (ret < 0 || ret >= end - pos)
2504 return start;
2505 pos += ret;
2506
2507 return pos;
2508 }
2509 #endif /* CONFIG_INTERWORKING */
2510
2511
2512 static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
2513 unsigned long mask, char *buf, size_t buflen)
2514 {
2515 size_t i;
2516 int ret;
2517 char *pos, *end;
2518 const u8 *ie, *ie2;
2519
2520 pos = buf;
2521 end = buf + buflen;
2522
2523 if (mask & WPA_BSS_MASK_ID) {
2524 ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
2525 if (ret < 0 || ret >= end - pos)
2526 return 0;
2527 pos += ret;
2528 }
2529
2530 if (mask & WPA_BSS_MASK_BSSID) {
2531 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
2532 MAC2STR(bss->bssid));
2533 if (ret < 0 || ret >= end - pos)
2534 return 0;
2535 pos += ret;
2536 }
2537
2538 if (mask & WPA_BSS_MASK_FREQ) {
2539 ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
2540 if (ret < 0 || ret >= end - pos)
2541 return 0;
2542 pos += ret;
2543 }
2544
2545 if (mask & WPA_BSS_MASK_BEACON_INT) {
2546 ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
2547 bss->beacon_int);
2548 if (ret < 0 || ret >= end - pos)
2549 return 0;
2550 pos += ret;
2551 }
2552
2553 if (mask & WPA_BSS_MASK_CAPABILITIES) {
2554 ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
2555 bss->caps);
2556 if (ret < 0 || ret >= end - pos)
2557 return 0;
2558 pos += ret;
2559 }
2560
2561 if (mask & WPA_BSS_MASK_QUAL) {
2562 ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
2563 if (ret < 0 || ret >= end - pos)
2564 return 0;
2565 pos += ret;
2566 }
2567
2568 if (mask & WPA_BSS_MASK_NOISE) {
2569 ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
2570 if (ret < 0 || ret >= end - pos)
2571 return 0;
2572 pos += ret;
2573 }
2574
2575 if (mask & WPA_BSS_MASK_LEVEL) {
2576 ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
2577 if (ret < 0 || ret >= end - pos)
2578 return 0;
2579 pos += ret;
2580 }
2581
2582 if (mask & WPA_BSS_MASK_TSF) {
2583 ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
2584 (unsigned long long) bss->tsf);
2585 if (ret < 0 || ret >= end - pos)
2586 return 0;
2587 pos += ret;
2588 }
2589
2590 if (mask & WPA_BSS_MASK_AGE) {
2591 struct os_time now;
2592
2593 os_get_time(&now);
2594 ret = os_snprintf(pos, end - pos, "age=%d\n",
2595 (int) (now.sec - bss->last_update.sec));
2596 if (ret < 0 || ret >= end - pos)
2597 return 0;
2598 pos += ret;
2599 }
2600
2601 if (mask & WPA_BSS_MASK_IE) {
2602 ret = os_snprintf(pos, end - pos, "ie=");
2603 if (ret < 0 || ret >= end - pos)
2604 return 0;
2605 pos += ret;
2606
2607 ie = (const u8 *) (bss + 1);
2608 for (i = 0; i < bss->ie_len; i++) {
2609 ret = os_snprintf(pos, end - pos, "%02x", *ie++);
2610 if (ret < 0 || ret >= end - pos)
2611 return 0;
2612 pos += ret;
2613 }
2614
2615 ret = os_snprintf(pos, end - pos, "\n");
2616 if (ret < 0 || ret >= end - pos)
2617 return 0;
2618 pos += ret;
2619 }
2620
2621 if (mask & WPA_BSS_MASK_FLAGS) {
2622 ret = os_snprintf(pos, end - pos, "flags=");
2623 if (ret < 0 || ret >= end - pos)
2624 return 0;
2625 pos += ret;
2626
2627 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
2628 if (ie)
2629 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
2630 2 + ie[1]);
2631 ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
2632 if (ie2)
2633 pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2,
2634 2 + ie2[1]);
2635 pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
2636 if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
2637 ret = os_snprintf(pos, end - pos, "[WEP]");
2638 if (ret < 0 || ret >= end - pos)
2639 return 0;
2640 pos += ret;
2641 }
2642 if (bss->caps & IEEE80211_CAP_IBSS) {
2643 ret = os_snprintf(pos, end - pos, "[IBSS]");
2644 if (ret < 0 || ret >= end - pos)
2645 return 0;
2646 pos += ret;
2647 }
2648 if (bss->caps & IEEE80211_CAP_ESS) {
2649 ret = os_snprintf(pos, end - pos, "[ESS]");
2650 if (ret < 0 || ret >= end - pos)
2651 return 0;
2652 pos += ret;
2653 }
2654 if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE)) {
2655 ret = os_snprintf(pos, end - pos, "[P2P]");
2656 if (ret < 0 || ret >= end - pos)
2657 return 0;
2658 pos += ret;
2659 }
2660 #ifdef CONFIG_HS20
2661 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
2662 ret = os_snprintf(pos, end - pos, "[HS20]");
2663 if (ret < 0 || ret >= end - pos)
2664 return -1;
2665 pos += ret;
2666 }
2667 #endif /* CONFIG_HS20 */
2668
2669 ret = os_snprintf(pos, end - pos, "\n");
2670 if (ret < 0 || ret >= end - pos)
2671 return 0;
2672 pos += ret;
2673 }
2674
2675 if (mask & WPA_BSS_MASK_SSID) {
2676 ret = os_snprintf(pos, end - pos, "ssid=%s\n",
2677 wpa_ssid_txt(bss->ssid, bss->ssid_len));
2678 if (ret < 0 || ret >= end - pos)
2679 return 0;
2680 pos += ret;
2681 }
2682
2683 #ifdef CONFIG_WPS
2684 if (mask & WPA_BSS_MASK_WPS_SCAN) {
2685 ie = (const u8 *) (bss + 1);
2686 ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
2687 if (ret < 0 || ret >= end - pos)
2688 return 0;
2689 pos += ret;
2690 }
2691 #endif /* CONFIG_WPS */
2692
2693 #ifdef CONFIG_P2P
2694 if (mask & WPA_BSS_MASK_P2P_SCAN) {
2695 ie = (const u8 *) (bss + 1);
2696 ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
2697 if (ret < 0 || ret >= end - pos)
2698 return 0;
2699 pos += ret;
2700 }
2701 #endif /* CONFIG_P2P */
2702
2703 #ifdef CONFIG_INTERWORKING
2704 if (mask & WPA_BSS_MASK_INTERNETW) {
2705 pos = anqp_add_hex(pos, end, "anqp_venue_name",
2706 bss->anqp_venue_name);
2707 pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
2708 bss->anqp_network_auth_type);
2709 pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
2710 bss->anqp_roaming_consortium);
2711 pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
2712 bss->anqp_ip_addr_type_availability);
2713 pos = anqp_add_hex(pos, end, "anqp_nai_realm",
2714 bss->anqp_nai_realm);
2715 pos = anqp_add_hex(pos, end, "anqp_3gpp", bss->anqp_3gpp);
2716 pos = anqp_add_hex(pos, end, "anqp_domain_name",
2717 bss->anqp_domain_name);
2718 #ifdef CONFIG_HS20
2719 pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
2720 bss->hs20_operator_friendly_name);
2721 pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
2722 bss->hs20_wan_metrics);
2723 pos = anqp_add_hex(pos, end, "hs20_connection_capability",
2724 bss->hs20_connection_capability);
2725 #endif /* CONFIG_HS20 */
2726 }
2727 #endif /* CONFIG_INTERWORKING */
2728
2729 return pos - buf;
2730 }
2731
2732
2733 static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
2734 const char *cmd, char *buf,
2735 size_t buflen)
2736 {
2737 u8 bssid[ETH_ALEN];
2738 size_t i;
2739 struct wpa_bss *bss;
2740 struct wpa_bss *bsslast = NULL;
2741 struct dl_list *next;
2742 int ret = 0;
2743 int len;
2744 char *ctmp;
2745 unsigned long mask = WPA_BSS_MASK_ALL;
2746
2747 if (os_strncmp(cmd, "RANGE=", 6) == 0) {
2748 if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
2749 bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
2750 list_id);
2751 bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
2752 list_id);
2753 } else { /* N1-N2 */
2754 unsigned int id1, id2;
2755
2756 if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
2757 wpa_printf(MSG_INFO, "Wrong BSS range "
2758 "format");
2759 return 0;
2760 }
2761
2762 id1 = atoi(cmd + 6);
2763 bss = wpa_bss_get_id(wpa_s, id1);
2764 id2 = atoi(ctmp + 1);
2765 if (id2 == 0)
2766 bsslast = dl_list_last(&wpa_s->bss_id,
2767 struct wpa_bss,
2768 list_id);
2769 else {
2770 bsslast = wpa_bss_get_id(wpa_s, id2);
2771 if (bsslast == NULL && bss && id2 > id1) {
2772 struct wpa_bss *tmp = bss;
2773 for (;;) {
2774 next = tmp->list_id.next;
2775 if (next == &wpa_s->bss_id)
2776 break;
2777 tmp = dl_list_entry(
2778 next, struct wpa_bss,
2779 list_id);
2780 if (tmp->id > id2)
2781 break;
2782 bsslast = tmp;
2783 }
2784 }
2785 }
2786 }
2787 } else if (os_strcmp(cmd, "FIRST") == 0)
2788 bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
2789 else if (os_strncmp(cmd, "ID-", 3) == 0) {
2790 i = atoi(cmd + 3);
2791 bss = wpa_bss_get_id(wpa_s, i);
2792 } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
2793 i = atoi(cmd + 5);
2794 bss = wpa_bss_get_id(wpa_s, i);
2795 if (bss) {
2796 next = bss->list_id.next;
2797 if (next == &wpa_s->bss_id)
2798 bss = NULL;
2799 else
2800 bss = dl_list_entry(next, struct wpa_bss,
2801 list_id);
2802 }
2803 #ifdef CONFIG_P2P
2804 } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
2805 if (hwaddr_aton(cmd + 13, bssid) == 0)
2806 bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
2807 else
2808 bss = NULL;
2809 #endif /* CONFIG_P2P */
2810 } else if (hwaddr_aton(cmd, bssid) == 0)
2811 bss = wpa_bss_get_bssid(wpa_s, bssid);
2812 else {
2813 struct wpa_bss *tmp;
2814 i = atoi(cmd);
2815 bss = NULL;
2816 dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
2817 {
2818 if (i-- == 0) {
2819 bss = tmp;
2820 break;
2821 }
2822 }
2823 }
2824
2825 if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
2826 mask = strtoul(ctmp + 5, NULL, 0x10);
2827 if (mask == 0)
2828 mask = WPA_BSS_MASK_ALL;
2829 }
2830
2831 if (bss == NULL)
2832 return 0;
2833
2834 if (bsslast == NULL)
2835 bsslast = bss;
2836 do {
2837 len = print_bss_info(wpa_s, bss, mask, buf, buflen);
2838 ret += len;
2839 buf += len;
2840 buflen -= len;
2841 if (bss == bsslast)
2842 break;
2843 next = bss->list_id.next;
2844 if (next == &wpa_s->bss_id)
2845 break;
2846 bss = dl_list_entry(next, struct wpa_bss, list_id);
2847 } while (bss && len);
2848
2849 return ret;
2850 }
2851
2852
2853 static int wpa_supplicant_ctrl_iface_ap_scan(
2854 struct wpa_supplicant *wpa_s, char *cmd)
2855 {
2856 int ap_scan = atoi(cmd);
2857 return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
2858 }
2859
2860
2861 static int wpa_supplicant_ctrl_iface_scan_interval(
2862 struct wpa_supplicant *wpa_s, char *cmd)
2863 {
2864 int scan_int = atoi(cmd);
2865 return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
2866 }
2867
2868
2869 static int wpa_supplicant_ctrl_iface_bss_expire_age(
2870 struct wpa_supplicant *wpa_s, char *cmd)
2871 {
2872 int expire_age = atoi(cmd);
2873 return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
2874 }
2875
2876
2877 static int wpa_supplicant_ctrl_iface_bss_expire_count(
2878 struct wpa_supplicant *wpa_s, char *cmd)
2879 {
2880 int expire_count = atoi(cmd);
2881 return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
2882 }
2883
2884
2885 static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
2886 {
2887 wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
2888 /* MLME-DELETEKEYS.request */
2889 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
2890 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
2891 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
2892 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
2893 #ifdef CONFIG_IEEE80211W
2894 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
2895 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
2896 #endif /* CONFIG_IEEE80211W */
2897
2898 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
2899 0);
2900 /* MLME-SETPROTECTION.request(None) */
2901 wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
2902 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
2903 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
2904 wpa_sm_drop_sa(wpa_s->wpa);
2905 }
2906
2907
2908 static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
2909 char *addr)
2910 {
2911 #ifdef CONFIG_NO_SCAN_PROCESSING
2912 return -1;
2913 #else /* CONFIG_NO_SCAN_PROCESSING */
2914 u8 bssid[ETH_ALEN];
2915 struct wpa_bss *bss;
2916 struct wpa_ssid *ssid = wpa_s->current_ssid;
2917
2918 if (hwaddr_aton(addr, bssid)) {
2919 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
2920 "address '%s'", addr);
2921 return -1;
2922 }
2923
2924 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
2925
2926 bss = wpa_bss_get_bssid(wpa_s, bssid);
2927 if (!bss) {
2928 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
2929 "from BSS table");
2930 return -1;
2931 }
2932
2933 /*
2934 * TODO: Find best network configuration block from configuration to
2935 * allow roaming to other networks
2936 */
2937
2938 if (!ssid) {
2939 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
2940 "configuration known for the target AP");
2941 return -1;
2942 }
2943
2944 wpa_s->reassociate = 1;
2945 wpa_supplicant_connect(wpa_s, bss, ssid);
2946
2947 return 0;
2948 #endif /* CONFIG_NO_SCAN_PROCESSING */
2949 }
2950
2951
2952 #ifdef CONFIG_P2P
2953 static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
2954 {
2955 unsigned int timeout = atoi(cmd);
2956 enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
2957 u8 dev_id[ETH_ALEN], *_dev_id = NULL;
2958 char *pos;
2959
2960 if (os_strstr(cmd, "type=social"))
2961 type = P2P_FIND_ONLY_SOCIAL;
2962 else if (os_strstr(cmd, "type=progressive"))
2963 type = P2P_FIND_PROGRESSIVE;
2964
2965 pos = os_strstr(cmd, "dev_id=");
2966 if (pos) {
2967 pos += 7;
2968 if (hwaddr_aton(pos, dev_id))
2969 return -1;
2970 _dev_id = dev_id;
2971 }
2972
2973 return wpas_p2p_find(wpa_s, timeout, type, 0, NULL, _dev_id);
2974 }
2975
2976
2977 static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
2978 char *buf, size_t buflen)
2979 {
2980 u8 addr[ETH_ALEN];
2981 char *pos, *pos2;
2982 char *pin = NULL;
2983 enum p2p_wps_method wps_method;
2984 int new_pin;
2985 int ret;
2986 int persistent_group, persistent_id = -1;
2987 int join;
2988 int auth;
2989 int automatic;
2990 int go_intent = -1;
2991 int freq = 0;
2992 int pd;
2993 int ht40;
2994
2995 /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad]
2996 * [persistent|persistent=<network id>]
2997 * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
2998 * [ht40] */
2999
3000 if (hwaddr_aton(cmd, addr))
3001 return -1;
3002
3003 pos = cmd + 17;
3004 if (*pos != ' ')
3005 return -1;
3006 pos++;
3007
3008 persistent_group = os_strstr(pos, " persistent") != NULL;
3009 pos2 = os_strstr(pos, " persistent=");
3010 if (pos2) {
3011 struct wpa_ssid *ssid;
3012 persistent_id = atoi(pos2 + 12);
3013 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3014 if (ssid == NULL || ssid->disabled != 2 ||
3015 ssid->mode != WPAS_MODE_P2P_GO) {
3016 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3017 "SSID id=%d for persistent P2P group (GO)",
3018 persistent_id);
3019 return -1;
3020 }
3021 }
3022 join = os_strstr(pos, " join") != NULL;
3023 auth = os_strstr(pos, " auth") != NULL;
3024 automatic = os_strstr(pos, " auto") != NULL;
3025 pd = os_strstr(pos, " provdisc") != NULL;
3026 ht40 = os_strstr(pos, " ht40") != NULL;
3027
3028 pos2 = os_strstr(pos, " go_intent=");
3029 if (pos2) {
3030 pos2 += 11;
3031 go_intent = atoi(pos2);
3032 if (go_intent < 0 || go_intent > 15)
3033 return -1;
3034 }
3035
3036 pos2 = os_strstr(pos, " freq=");
3037 if (pos2) {
3038 pos2 += 6;
3039 freq = atoi(pos2);
3040 if (freq <= 0)
3041 return -1;
3042 }
3043
3044 if (os_strncmp(pos, "pin", 3) == 0) {
3045 /* Request random PIN (to be displayed) and enable the PIN */
3046 wps_method = WPS_PIN_DISPLAY;
3047 } else if (os_strncmp(pos, "pbc", 3) == 0) {
3048 wps_method = WPS_PBC;
3049 } else {
3050 pin = pos;
3051 pos = os_strchr(pin, ' ');
3052 wps_method = WPS_PIN_KEYPAD;
3053 if (pos) {
3054 *pos++ = '\0';
3055 if (os_strncmp(pos, "display", 7) == 0)
3056 wps_method = WPS_PIN_DISPLAY;
3057 }
3058 if (!wps_pin_str_valid(pin)) {
3059 os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
3060 return 17;
3061 }
3062 }
3063
3064 new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
3065 persistent_group, automatic, join,
3066 auth, go_intent, freq, persistent_id, pd,
3067 ht40);
3068 if (new_pin == -2) {
3069 os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
3070 return 25;
3071 }
3072 if (new_pin == -3) {
3073 os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
3074 return 25;
3075 }
3076 if (new_pin < 0)
3077 return -1;
3078 if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
3079 ret = os_snprintf(buf, buflen, "%08d", new_pin);
3080 if (ret < 0 || (size_t) ret >= buflen)
3081 return -1;
3082 return ret;
3083 }
3084
3085 os_memcpy(buf, "OK\n", 3);
3086 return 3;
3087 }
3088
3089
3090 static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
3091 {
3092 unsigned int timeout = atoi(cmd);
3093 return wpas_p2p_listen(wpa_s, timeout);
3094 }
3095
3096
3097 static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
3098 {
3099 u8 addr[ETH_ALEN];
3100 char *pos;
3101 enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
3102
3103 /* <addr> <config method> [join|auto] */
3104
3105 if (hwaddr_aton(cmd, addr))
3106 return -1;
3107
3108 pos = cmd + 17;
3109 if (*pos != ' ')
3110 return -1;
3111 pos++;
3112
3113 if (os_strstr(pos, " join") != NULL)
3114 use = WPAS_P2P_PD_FOR_JOIN;
3115 else if (os_strstr(pos, " auto") != NULL)
3116 use = WPAS_P2P_PD_AUTO;
3117
3118 return wpas_p2p_prov_disc(wpa_s, addr, pos, use);
3119 }
3120
3121
3122 static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
3123 size_t buflen)
3124 {
3125 struct wpa_ssid *ssid = wpa_s->current_ssid;
3126
3127 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3128 ssid->passphrase == NULL)
3129 return -1;
3130
3131 os_strlcpy(buf, ssid->passphrase, buflen);
3132 return os_strlen(buf);
3133 }
3134
3135
3136 static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
3137 char *buf, size_t buflen)
3138 {
3139 u64 ref;
3140 int res;
3141 u8 dst_buf[ETH_ALEN], *dst;
3142 struct wpabuf *tlvs;
3143 char *pos;
3144 size_t len;
3145
3146 if (hwaddr_aton(cmd, dst_buf))
3147 return -1;
3148 dst = dst_buf;
3149 if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
3150 dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
3151 dst = NULL;
3152 pos = cmd + 17;
3153 if (*pos != ' ')
3154 return -1;
3155 pos++;
3156
3157 if (os_strncmp(pos, "upnp ", 5) == 0) {
3158 u8 version;
3159 pos += 5;
3160 if (hexstr2bin(pos, &version, 1) < 0)
3161 return -1;
3162 pos += 2;
3163 if (*pos != ' ')
3164 return -1;
3165 pos++;
3166 ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
3167 } else {
3168 len = os_strlen(pos);
3169 if (len & 1)
3170 return -1;
3171 len /= 2;
3172 tlvs = wpabuf_alloc(len);
3173 if (tlvs == NULL)
3174 return -1;
3175 if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
3176 wpabuf_free(tlvs);
3177 return -1;
3178 }
3179
3180 ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
3181 wpabuf_free(tlvs);
3182 }
3183 if (ref == 0)
3184 return -1;
3185 res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
3186 if (res < 0 || (unsigned) res >= buflen)
3187 return -1;
3188 return res;
3189 }
3190
3191
3192 static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
3193 char *cmd)
3194 {
3195 long long unsigned val;
3196 u64 req;
3197 if (sscanf(cmd, "%llx", &val) != 1)
3198 return -1;
3199 req = val;
3200 return wpas_p2p_sd_cancel_request(wpa_s, req);
3201 }
3202
3203
3204 static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
3205 {
3206 int freq;
3207 u8 dst[ETH_ALEN];
3208 u8 dialog_token;
3209 struct wpabuf *resp_tlvs;
3210 char *pos, *pos2;
3211 size_t len;
3212
3213 pos = os_strchr(cmd, ' ');
3214 if (pos == NULL)
3215 return -1;
3216 *pos++ = '\0';
3217 freq = atoi(cmd);
3218 if (freq == 0)
3219 return -1;
3220
3221 if (hwaddr_aton(pos, dst))
3222 return -1;
3223 pos += 17;
3224 if (*pos != ' ')
3225 return -1;
3226 pos++;
3227
3228 pos2 = os_strchr(pos, ' ');
3229 if (pos2 == NULL)
3230 return -1;
3231 *pos2++ = '\0';
3232 dialog_token = atoi(pos);
3233
3234 len = os_strlen(pos2);
3235 if (len & 1)
3236 return -1;
3237 len /= 2;
3238 resp_tlvs = wpabuf_alloc(len);
3239 if (resp_tlvs == NULL)
3240 return -1;
3241 if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
3242 wpabuf_free(resp_tlvs);
3243 return -1;
3244 }
3245
3246 wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
3247 wpabuf_free(resp_tlvs);
3248 return 0;
3249 }
3250
3251
3252 static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
3253 char *cmd)
3254 {
3255 if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
3256 return -1;
3257 wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
3258 return 0;
3259 }
3260
3261
3262 static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
3263 char *cmd)
3264 {
3265 char *pos;
3266 size_t len;
3267 struct wpabuf *query, *resp;
3268
3269 pos = os_strchr(cmd, ' ');
3270 if (pos == NULL)
3271 return -1;
3272 *pos++ = '\0';
3273
3274 len = os_strlen(cmd);
3275 if (len & 1)
3276 return -1;
3277 len /= 2;
3278 query = wpabuf_alloc(len);
3279 if (query == NULL)
3280 return -1;
3281 if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
3282 wpabuf_free(query);
3283 return -1;
3284 }
3285
3286 len = os_strlen(pos);
3287 if (len & 1) {
3288 wpabuf_free(query);
3289 return -1;
3290 }
3291 len /= 2;
3292 resp = wpabuf_alloc(len);
3293 if (resp == NULL) {
3294 wpabuf_free(query);
3295 return -1;
3296 }
3297 if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
3298 wpabuf_free(query);
3299 wpabuf_free(resp);
3300 return -1;
3301 }
3302
3303 if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
3304 wpabuf_free(query);
3305 wpabuf_free(resp);
3306 return -1;
3307 }
3308 return 0;
3309 }
3310
3311
3312 static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
3313 {
3314 char *pos;
3315 u8 version;
3316
3317 pos = os_strchr(cmd, ' ');
3318 if (pos == NULL)
3319 return -1;
3320 *pos++ = '\0';
3321
3322 if (hexstr2bin(cmd, &version, 1) < 0)
3323 return -1;
3324
3325 return wpas_p2p_service_add_upnp(wpa_s, version, pos);
3326 }
3327
3328
3329 static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
3330 {
3331 char *pos;
3332
3333 pos = os_strchr(cmd, ' ');
3334 if (pos == NULL)
3335 return -1;
3336 *pos++ = '\0';
3337
3338 if (os_strcmp(cmd, "bonjour") == 0)
3339 return p2p_ctrl_service_add_bonjour(wpa_s, pos);
3340 if (os_strcmp(cmd, "upnp") == 0)
3341 return p2p_ctrl_service_add_upnp(wpa_s, pos);
3342 wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
3343 return -1;
3344 }
3345
3346
3347 static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
3348 char *cmd)
3349 {
3350 size_t len;
3351 struct wpabuf *query;
3352 int ret;
3353
3354 len = os_strlen(cmd);
3355 if (len & 1)
3356 return -1;
3357 len /= 2;
3358 query = wpabuf_alloc(len);
3359 if (query == NULL)
3360 return -1;
3361 if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
3362 wpabuf_free(query);
3363 return -1;
3364 }
3365
3366 ret = wpas_p2p_service_del_bonjour(wpa_s, query);
3367 wpabuf_free(query);
3368 return ret;
3369 }
3370
3371
3372 static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
3373 {
3374 char *pos;
3375 u8 version;
3376
3377 pos = os_strchr(cmd, ' ');
3378 if (pos == NULL)
3379 return -1;
3380 *pos++ = '\0';
3381
3382 if (hexstr2bin(cmd, &version, 1) < 0)
3383 return -1;
3384
3385 return wpas_p2p_service_del_upnp(wpa_s, version, pos);
3386 }
3387
3388
3389 static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
3390 {
3391 char *pos;
3392
3393 pos = os_strchr(cmd, ' ');
3394 if (pos == NULL)
3395 return -1;
3396 *pos++ = '\0';
3397
3398 if (os_strcmp(cmd, "bonjour") == 0)
3399 return p2p_ctrl_service_del_bonjour(wpa_s, pos);
3400 if (os_strcmp(cmd, "upnp") == 0)
3401 return p2p_ctrl_service_del_upnp(wpa_s, pos);
3402 wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
3403 return -1;
3404 }
3405
3406
3407 static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
3408 {
3409 u8 addr[ETH_ALEN];
3410
3411 /* <addr> */
3412
3413 if (hwaddr_aton(cmd, addr))
3414 return -1;
3415
3416 return wpas_p2p_reject(wpa_s, addr);
3417 }
3418
3419
3420 static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
3421 {
3422 char *pos;
3423 int id;
3424 struct wpa_ssid *ssid;
3425 u8 peer[ETH_ALEN];
3426
3427 id = atoi(cmd);
3428 pos = os_strstr(cmd, " peer=");
3429 if (pos) {
3430 pos += 6;
3431 if (hwaddr_aton(pos, peer))
3432 return -1;
3433 }
3434 ssid = wpa_config_get_network(wpa_s->conf, id);
3435 if (ssid == NULL || ssid->disabled != 2) {
3436 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
3437 "for persistent P2P group",
3438 id);
3439 return -1;
3440 }
3441
3442 return wpas_p2p_invite(wpa_s, pos ? peer : NULL, ssid, NULL);
3443 }
3444
3445
3446 static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
3447 {
3448 char *pos;
3449 u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
3450
3451 pos = os_strstr(cmd, " peer=");
3452 if (!pos)
3453 return -1;
3454
3455 *pos = '\0';
3456 pos += 6;
3457 if (hwaddr_aton(pos, peer)) {
3458 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
3459 return -1;
3460 }
3461
3462 pos = os_strstr(pos, " go_dev_addr=");
3463 if (pos) {
3464 pos += 13;
3465 if (hwaddr_aton(pos, go_dev_addr)) {
3466 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
3467 pos);
3468 return -1;
3469 }
3470 go_dev = go_dev_addr;
3471 }
3472
3473 return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
3474 }
3475
3476
3477 static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
3478 {
3479 if (os_strncmp(cmd, "persistent=", 11) == 0)
3480 return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
3481 if (os_strncmp(cmd, "group=", 6) == 0)
3482 return p2p_ctrl_invite_group(wpa_s, cmd + 6);
3483
3484 return -1;
3485 }
3486
3487
3488 static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
3489 char *cmd, int freq, int ht40)
3490 {
3491 int id;
3492 struct wpa_ssid *ssid;
3493
3494 id = atoi(cmd);
3495 ssid = wpa_config_get_network(wpa_s->conf, id);
3496 if (ssid == NULL || ssid->disabled != 2) {
3497 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
3498 "for persistent P2P group",
3499 id);
3500 return -1;
3501 }
3502
3503 return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq, ht40);
3504 }
3505
3506
3507 static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
3508 {
3509 int freq = 0, ht40;
3510 char *pos;
3511
3512 pos = os_strstr(cmd, "freq=");
3513 if (pos)
3514 freq = atoi(pos + 5);
3515
3516 ht40 = os_strstr(cmd, "ht40") != NULL;
3517
3518 if (os_strncmp(cmd, "persistent=", 11) == 0)
3519 return p2p_ctrl_group_add_persistent(wpa_s, cmd + 11, freq,
3520 ht40);
3521 if (os_strcmp(cmd, "persistent") == 0 ||
3522 os_strncmp(cmd, "persistent ", 11) == 0)
3523 return wpas_p2p_group_add(wpa_s, 1, freq, ht40);
3524 if (os_strncmp(cmd, "freq=", 5) == 0)
3525 return wpas_p2p_group_add(wpa_s, 0, freq, ht40);
3526 if (ht40)
3527 return wpas_p2p_group_add(wpa_s, 0, freq, ht40);
3528
3529 wpa_printf(MSG_DEBUG, "CTRL: Invalid P2P_GROUP_ADD parameters '%s'",
3530 cmd);
3531 return -1;
3532 }
3533
3534
3535 static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
3536 char *buf, size_t buflen)
3537 {
3538 u8 addr[ETH_ALEN], *addr_ptr;
3539 int next, res;
3540 const struct p2p_peer_info *info;
3541 char *pos, *end;
3542 char devtype[WPS_DEV_TYPE_BUFSIZE];
3543 struct wpa_ssid *ssid;
3544
3545 if (!wpa_s->global->p2p)
3546 return -1;
3547
3548 if (os_strcmp(cmd, "FIRST") == 0) {
3549 addr_ptr = NULL;
3550 next = 0;
3551 } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
3552 if (hwaddr_aton(cmd + 5, addr) < 0)
3553 return -1;
3554 addr_ptr = addr;
3555 next = 1;
3556 } else {
3557 if (hwaddr_aton(cmd, addr) < 0)
3558 return -1;
3559 addr_ptr = addr;
3560 next = 0;
3561 }
3562
3563 info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
3564 if (info == NULL)
3565 return -1;
3566
3567 pos = buf;
3568 end = buf + buflen;
3569
3570 res = os_snprintf(pos, end - pos, MACSTR "\n"
3571 "pri_dev_type=%s\n"
3572 "device_name=%s\n"
3573 "manufacturer=%s\n"
3574 "model_name=%s\n"
3575 "model_number=%s\n"
3576 "serial_number=%s\n"
3577 "config_methods=0x%x\n"
3578 "dev_capab=0x%x\n"
3579 "group_capab=0x%x\n"
3580 "level=%d\n",
3581 MAC2STR(info->p2p_device_addr),
3582 wps_dev_type_bin2str(info->pri_dev_type,
3583 devtype, sizeof(devtype)),
3584 info->device_name,
3585 info->manufacturer,
3586 info->model_name,
3587 info->model_number,
3588 info->serial_number,
3589 info->config_methods,
3590 info->dev_capab,
3591 info->group_capab,
3592 info->level);
3593 if (res < 0 || res >= end - pos)
3594 return pos - buf;
3595 pos += res;
3596
3597 ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
3598 if (ssid) {
3599 res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
3600 if (res < 0 || res >= end - pos)
3601 return pos - buf;
3602 pos += res;
3603 }
3604
3605 res = p2p_get_peer_info_txt(info, pos, end - pos);
3606 if (res < 0)
3607 return pos - buf;
3608 pos += res;
3609
3610 return pos - buf;
3611 }
3612
3613
3614 static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
3615 const char *param)
3616 {
3617 struct wpa_freq_range *freq = NULL, *n;
3618 unsigned int count = 0, i;
3619 const char *pos, *pos2, *pos3;
3620
3621 if (wpa_s->global->p2p == NULL)
3622 return -1;
3623
3624 /*
3625 * param includes comma separated frequency range.
3626 * For example: 2412-2432,2462,5000-6000
3627 */
3628 pos = param;
3629 while (pos && pos[0]) {
3630 n = os_realloc_array(freq, count + 1,
3631 sizeof(struct wpa_freq_range));
3632 if (n == NULL) {
3633 os_free(freq);
3634 return -1;
3635 }
3636 freq = n;
3637 freq[count].min = atoi(pos);
3638 pos2 = os_strchr(pos, '-');
3639 pos3 = os_strchr(pos, ',');
3640 if (pos2 && (!pos3 || pos2 < pos3)) {
3641 pos2++;
3642 freq[count].max = atoi(pos2);
3643 } else
3644 freq[count].max = freq[count].min;
3645 pos = pos3;
3646 if (pos)
3647 pos++;
3648 count++;
3649 }
3650
3651 for (i = 0; i < count; i++) {
3652 wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
3653 freq[i].min, freq[i].max);
3654 }
3655
3656 os_free(wpa_s->global->p2p_disallow_freq);
3657 wpa_s->global->p2p_disallow_freq = freq;
3658 wpa_s->global->num_p2p_disallow_freq = count;
3659 wpas_p2p_update_channel_list(wpa_s);
3660 return 0;
3661 }
3662
3663
3664 static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
3665 {
3666 char *param;
3667
3668 if (wpa_s->global->p2p == NULL)
3669 return -1;
3670
3671 param = os_strchr(cmd, ' ');
3672 if (param == NULL)
3673 return -1;
3674 *param++ = '\0';
3675
3676 if (os_strcmp(cmd, "discoverability") == 0) {
3677 p2p_set_client_discoverability(wpa_s->global->p2p,
3678 atoi(param));
3679 return 0;
3680 }
3681
3682 if (os_strcmp(cmd, "managed") == 0) {
3683 p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
3684 return 0;
3685 }
3686
3687 if (os_strcmp(cmd, "listen_channel") == 0) {
3688 return p2p_set_listen_channel(wpa_s->global->p2p, 81,
3689 atoi(param));
3690 }
3691
3692 if (os_strcmp(cmd, "ssid_postfix") == 0) {
3693 return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
3694 os_strlen(param));
3695 }
3696
3697 if (os_strcmp(cmd, "noa") == 0) {
3698 char *pos;
3699 int count, start, duration;
3700 /* GO NoA parameters: count,start_offset(ms),duration(ms) */
3701 count = atoi(param);
3702 pos = os_strchr(param, ',');
3703 if (pos == NULL)
3704 return -1;
3705 pos++;
3706 start = atoi(pos);
3707 pos = os_strchr(pos, ',');
3708 if (pos == NULL)
3709 return -1;
3710 pos++;
3711 duration = atoi(pos);
3712 if (count < 0 || count > 255 || start < 0 || duration < 0)
3713 return -1;
3714 if (count == 0 && duration > 0)
3715 return -1;
3716 wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
3717 "start=%d duration=%d", count, start, duration);
3718 return wpas_p2p_set_noa(wpa_s, count, start, duration);
3719 }
3720
3721 if (os_strcmp(cmd, "ps") == 0)
3722 return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
3723
3724 if (os_strcmp(cmd, "oppps") == 0)
3725 return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
3726
3727 if (os_strcmp(cmd, "ctwindow") == 0)
3728 return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
3729
3730 if (os_strcmp(cmd, "disabled") == 0) {
3731 wpa_s->global->p2p_disabled = atoi(param);
3732 wpa_printf(MSG_DEBUG, "P2P functionality %s",
3733 wpa_s->global->p2p_disabled ?
3734 "disabled" : "enabled");
3735 if (wpa_s->global->p2p_disabled) {
3736 wpas_p2p_stop_find(wpa_s);
3737 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
3738 p2p_flush(wpa_s->global->p2p);
3739 }
3740 return 0;
3741 }
3742
3743 if (os_strcmp(cmd, "conc_pref") == 0) {
3744 if (os_strcmp(param, "sta") == 0)
3745 wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
3746 else if (os_strcmp(param, "p2p") == 0)
3747 wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
3748 else {
3749 wpa_printf(MSG_INFO, "Invalid conc_pref value");
3750 return -1;
3751 }
3752 wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
3753 "%s", param);
3754 return 0;
3755 }
3756
3757 if (os_strcmp(cmd, "force_long_sd") == 0) {
3758 wpa_s->force_long_sd = atoi(param);
3759 return 0;
3760 }
3761
3762 if (os_strcmp(cmd, "peer_filter") == 0) {
3763 u8 addr[ETH_ALEN];
3764 if (hwaddr_aton(param, addr))
3765 return -1;
3766 p2p_set_peer_filter(wpa_s->global->p2p, addr);
3767 return 0;
3768 }
3769
3770 if (os_strcmp(cmd, "cross_connect") == 0)
3771 return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
3772
3773 if (os_strcmp(cmd, "go_apsd") == 0) {
3774 if (os_strcmp(param, "disable") == 0)
3775 wpa_s->set_ap_uapsd = 0;
3776 else {
3777 wpa_s->set_ap_uapsd = 1;
3778 wpa_s->ap_uapsd = atoi(param);
3779 }
3780 return 0;
3781 }
3782
3783 if (os_strcmp(cmd, "client_apsd") == 0) {
3784 if (os_strcmp(param, "disable") == 0)
3785 wpa_s->set_sta_uapsd = 0;
3786 else {
3787 int be, bk, vi, vo;
3788 char *pos;
3789 /* format: BE,BK,VI,VO;max SP Length */
3790 be = atoi(param);
3791 pos = os_strchr(param, ',');
3792 if (pos == NULL)
3793 return -1;
3794 pos++;
3795 bk = atoi(pos);
3796 pos = os_strchr(pos, ',');
3797 if (pos == NULL)
3798 return -1;
3799 pos++;
3800 vi = atoi(pos);
3801 pos = os_strchr(pos, ',');
3802 if (pos == NULL)
3803 return -1;
3804 pos++;
3805 vo = atoi(pos);
3806 /* ignore max SP Length for now */
3807
3808 wpa_s->set_sta_uapsd = 1;
3809 wpa_s->sta_uapsd = 0;
3810 if (be)
3811 wpa_s->sta_uapsd |= BIT(0);
3812 if (bk)
3813 wpa_s->sta_uapsd |= BIT(1);
3814 if (vi)
3815 wpa_s->sta_uapsd |= BIT(2);
3816 if (vo)
3817 wpa_s->sta_uapsd |= BIT(3);
3818 }
3819 return 0;
3820 }
3821
3822 if (os_strcmp(cmd, "disallow_freq") == 0)
3823 return p2p_ctrl_disallow_freq(wpa_s, param);
3824
3825 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
3826 cmd);
3827
3828 return -1;
3829 }
3830
3831
3832 static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
3833 {
3834 char *pos, *pos2;
3835 unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
3836
3837 if (cmd[0]) {
3838 pos = os_strchr(cmd, ' ');
3839 if (pos == NULL)
3840 return -1;
3841 *pos++ = '\0';
3842 dur1 = atoi(cmd);
3843
3844 pos2 = os_strchr(pos, ' ');
3845 if (pos2)
3846 *pos2++ = '\0';
3847 int1 = atoi(pos);
3848 } else
3849 pos2 = NULL;
3850
3851 if (pos2) {
3852 pos = os_strchr(pos2, ' ');
3853 if (pos == NULL)
3854 return -1;
3855 *pos++ = '\0';
3856 dur2 = atoi(pos2);
3857 int2 = atoi(pos);
3858 }
3859
3860 return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
3861 }
3862
3863
3864 static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
3865 {
3866 char *pos;
3867 unsigned int period = 0, interval = 0;
3868
3869 if (cmd[0]) {
3870 pos = os_strchr(cmd, ' ');
3871 if (pos == NULL)
3872 return -1;
3873 *pos++ = '\0';
3874 period = atoi(cmd);
3875 interval = atoi(pos);
3876 }
3877
3878 return wpas_p2p_ext_listen(wpa_s, period, interval);
3879 }
3880
3881 #endif /* CONFIG_P2P */
3882
3883
3884 #ifdef CONFIG_INTERWORKING
3885 static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst)
3886 {
3887 u8 bssid[ETH_ALEN];
3888 struct wpa_bss *bss;
3889
3890 if (hwaddr_aton(dst, bssid)) {
3891 wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
3892 return -1;
3893 }
3894
3895 bss = wpa_bss_get_bssid(wpa_s, bssid);
3896 if (bss == NULL) {
3897 wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
3898 MAC2STR(bssid));
3899 return -1;
3900 }
3901
3902 return interworking_connect(wpa_s, bss);
3903 }
3904
3905
3906 static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
3907 {
3908 u8 dst_addr[ETH_ALEN];
3909 int used;
3910 char *pos;
3911 #define MAX_ANQP_INFO_ID 100
3912 u16 id[MAX_ANQP_INFO_ID];
3913 size_t num_id = 0;
3914
3915 used = hwaddr_aton2(dst, dst_addr);
3916 if (used < 0)
3917 return -1;
3918 pos = dst + used;
3919 while (num_id < MAX_ANQP_INFO_ID) {
3920 id[num_id] = atoi(pos);
3921 if (id[num_id])
3922 num_id++;
3923 pos = os_strchr(pos + 1, ',');
3924 if (pos == NULL)
3925 break;
3926 pos++;
3927 }
3928
3929 if (num_id == 0)
3930 return -1;
3931
3932 return anqp_send_req(wpa_s, dst_addr, id, num_id);
3933 }
3934 #endif /* CONFIG_INTERWORKING */
3935
3936
3937 #ifdef CONFIG_HS20
3938
3939 static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
3940 {
3941 u8 dst_addr[ETH_ALEN];
3942 int used;
3943 char *pos;
3944 u32 subtypes = 0;
3945
3946 used = hwaddr_aton2(dst, dst_addr);
3947 if (used < 0)
3948 return -1;
3949 pos = dst + used;
3950 for (;;) {
3951 int num = atoi(pos);
3952 if (num <= 0 || num > 31)
3953 return -1;
3954 subtypes |= BIT(num);
3955 pos = os_strchr(pos + 1, ',');
3956 if (pos == NULL)
3957 break;
3958 pos++;
3959 }
3960
3961 if (subtypes == 0)
3962 return -1;
3963
3964 return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0);
3965 }
3966
3967
3968 static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
3969 const u8 *addr, const char *realm)
3970 {
3971 u8 *buf;
3972 size_t rlen, len;
3973 int ret;
3974
3975 rlen = os_strlen(realm);
3976 len = 3 + rlen;
3977 buf = os_malloc(len);
3978 if (buf == NULL)
3979 return -1;
3980 buf[0] = 1; /* NAI Home Realm Count */
3981 buf[1] = 0; /* Formatted in accordance with RFC 4282 */
3982 buf[2] = rlen;
3983 os_memcpy(buf + 3, realm, rlen);
3984
3985 ret = hs20_anqp_send_req(wpa_s, addr,
3986 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
3987 buf, len);
3988
3989 os_free(buf);
3990
3991 return ret;
3992 }
3993
3994
3995 static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
3996 char *dst)
3997 {
3998 struct wpa_cred *cred = wpa_s->conf->cred;
3999 u8 dst_addr[ETH_ALEN];
4000 int used;
4001 u8 *buf;
4002 size_t len;
4003 int ret;
4004
4005 used = hwaddr_aton2(dst, dst_addr);
4006 if (used < 0)
4007 return -1;
4008
4009 while (dst[used] == ' ')
4010 used++;
4011 if (os_strncmp(dst + used, "realm=", 6) == 0)
4012 return hs20_nai_home_realm_list(wpa_s, dst_addr,
4013 dst + used + 6);
4014
4015 len = os_strlen(dst + used);
4016
4017 if (len == 0 && cred && cred->realm)
4018 return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
4019
4020 if (len % 1)
4021 return -1;
4022 len /= 2;
4023 buf = os_malloc(len);
4024 if (buf == NULL)
4025 return -1;
4026 if (hexstr2bin(dst + used, buf, len) < 0) {
4027 os_free(buf);
4028 return -1;
4029 }
4030
4031 ret = hs20_anqp_send_req(wpa_s, dst_addr,
4032 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
4033 buf, len);
4034 os_free(buf);
4035
4036 return ret;
4037 }
4038
4039 #endif /* CONFIG_HS20 */
4040
4041
4042 static int wpa_supplicant_ctrl_iface_sta_autoconnect(
4043 struct wpa_supplicant *wpa_s, char *cmd)
4044 {
4045 wpa_s->auto_reconnect_disabled = atoi(cmd) == 0 ? 1 : 0;
4046 return 0;
4047 }
4048
4049
4050 #ifdef CONFIG_AUTOSCAN
4051
4052 static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
4053 char *cmd)
4054 {
4055 enum wpa_states state = wpa_s->wpa_state;
4056 char *new_params = NULL;
4057
4058 if (os_strlen(cmd) > 0) {
4059 new_params = os_strdup(cmd);
4060 if (new_params == NULL)
4061 return -1;
4062 }
4063
4064 os_free(wpa_s->conf->autoscan);
4065 wpa_s->conf->autoscan = new_params;
4066
4067 if (wpa_s->conf->autoscan == NULL)
4068 autoscan_deinit(wpa_s);
4069 else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
4070 autoscan_init(wpa_s, 1);
4071 else if (state == WPA_SCANNING)
4072 wpa_supplicant_reinit_autoscan(wpa_s);
4073
4074 return 0;
4075 }
4076
4077 #endif /* CONFIG_AUTOSCAN */
4078
4079
4080 static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
4081 size_t buflen)
4082 {
4083 struct wpa_signal_info si;
4084 int ret;
4085
4086 ret = wpa_drv_signal_poll(wpa_s, &si);
4087 if (ret)
4088 return -1;
4089
4090 ret = os_snprintf(buf, buflen, "RSSI=%d\nLINKSPEED=%d\n"
4091 "NOISE=%d\nFREQUENCY=%u\n",
4092 si.current_signal, si.current_txrate / 1000,
4093 si.current_noise, si.frequency);
4094 if (ret < 0 || (unsigned int) ret > buflen)
4095 return -1;
4096 return ret;
4097 }
4098
4099
4100 char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
4101 char *buf, size_t *resp_len)
4102 {
4103 char *reply;
4104 const int reply_size = 4096;
4105 int ctrl_rsp = 0;
4106 int reply_len;
4107
4108 if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
4109 os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
4110 wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
4111 (const u8 *) buf, os_strlen(buf));
4112 } else {
4113 int level = MSG_DEBUG;
4114 if (os_strcmp(buf, "PING") == 0)
4115 level = MSG_EXCESSIVE;
4116 wpa_hexdump_ascii(level, "RX ctrl_iface",
4117 (const u8 *) buf, os_strlen(buf));
4118 }
4119
4120 reply = os_malloc(reply_size);
4121 if (reply == NULL) {
4122 *resp_len = 1;
4123 return NULL;
4124 }
4125
4126 os_memcpy(reply, "OK\n", 3);
4127 reply_len = 3;
4128
4129 if (os_strcmp(buf, "PING") == 0) {
4130 os_memcpy(reply, "PONG\n", 5);
4131 reply_len = 5;
4132 } else if (os_strcmp(buf, "IFNAME") == 0) {
4133 reply_len = os_strlen(wpa_s->ifname);
4134 os_memcpy(reply, wpa_s->ifname, reply_len);
4135 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
4136 if (wpa_debug_reopen_file() < 0)
4137 reply_len = -1;
4138 } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
4139 wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
4140 } else if (os_strcmp(buf, "MIB") == 0) {
4141 reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
4142 if (reply_len >= 0) {
4143 int res;
4144 res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
4145 reply_size - reply_len);
4146 if (res < 0)
4147 reply_len = -1;
4148 else
4149 reply_len += res;
4150 }
4151 } else if (os_strncmp(buf, "STATUS", 6) == 0) {
4152 reply_len = wpa_supplicant_ctrl_iface_status(
4153 wpa_s, buf + 6, reply, reply_size);
4154 } else if (os_strcmp(buf, "PMKSA") == 0) {
4155 reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
4156 reply_size);
4157 } else if (os_strncmp(buf, "SET ", 4) == 0) {
4158 if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
4159 reply_len = -1;
4160 } else if (os_strncmp(buf, "GET ", 4) == 0) {
4161 reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
4162 reply, reply_size);
4163 } else if (os_strcmp(buf, "LOGON") == 0) {
4164 eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
4165 } else if (os_strcmp(buf, "LOGOFF") == 0) {
4166 eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
4167 } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
4168 wpa_s->normal_scans = 0;
4169 wpa_supplicant_reinit_autoscan(wpa_s);
4170 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
4171 reply_len = -1;
4172 else {
4173 wpa_s->disconnected = 0;
4174 wpa_s->reassociate = 1;
4175 wpa_supplicant_req_scan(wpa_s, 0, 0);
4176 }
4177 } else if (os_strcmp(buf, "RECONNECT") == 0) {
4178 wpa_s->normal_scans = 0;
4179 wpa_supplicant_reinit_autoscan(wpa_s);
4180 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
4181 reply_len = -1;
4182 else if (wpa_s->disconnected) {
4183 wpa_s->disconnected = 0;
4184 wpa_s->reassociate = 1;
4185 wpa_supplicant_req_scan(wpa_s, 0, 0);
4186 }
4187 #ifdef IEEE8021X_EAPOL
4188 } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
4189 if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
4190 reply_len = -1;
4191 #endif /* IEEE8021X_EAPOL */
4192 #ifdef CONFIG_PEERKEY
4193 } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
4194 if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
4195 reply_len = -1;
4196 #endif /* CONFIG_PEERKEY */
4197 #ifdef CONFIG_IEEE80211R
4198 } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
4199 if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
4200 reply_len = -1;
4201 #endif /* CONFIG_IEEE80211R */
4202 #ifdef CONFIG_WPS
4203 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
4204 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
4205 if (res == -2) {
4206 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
4207 reply_len = 17;
4208 } else if (res)
4209 reply_len = -1;
4210 } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
4211 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
4212 if (res == -2) {
4213 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
4214 reply_len = 17;
4215 } else if (res)
4216 reply_len = -1;
4217 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
4218 reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
4219 reply,
4220 reply_size);
4221 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
4222 reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
4223 wpa_s, buf + 14, reply, reply_size);
4224 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
4225 if (wpas_wps_cancel(wpa_s))
4226 reply_len = -1;
4227 #ifdef CONFIG_WPS_OOB
4228 } else if (os_strncmp(buf, "WPS_OOB ", 8) == 0) {
4229 if (wpa_supplicant_ctrl_iface_wps_oob(wpa_s, buf + 8))
4230 reply_len = -1;
4231 #endif /* CONFIG_WPS_OOB */
4232 #ifdef CONFIG_WPS_NFC
4233 } else if (os_strcmp(buf, "WPS_NFC") == 0) {
4234 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
4235 reply_len = -1;
4236 } else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
4237 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
4238 reply_len = -1;
4239 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
4240 reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
4241 wpa_s, buf + 14, reply, reply_size);
4242 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
4243 if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
4244 buf + 17))
4245 reply_len = -1;
4246 #endif /* CONFIG_WPS_NFC */
4247 } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
4248 if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
4249 reply_len = -1;
4250 #ifdef CONFIG_AP
4251 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
4252 reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
4253 wpa_s, buf + 11, reply, reply_size);
4254 #endif /* CONFIG_AP */
4255 #ifdef CONFIG_WPS_ER
4256 } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
4257 if (wpas_wps_er_start(wpa_s, NULL))
4258 reply_len = -1;
4259 } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
4260 if (wpas_wps_er_start(wpa_s, buf + 13))
4261 reply_len = -1;
4262 } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
4263 if (wpas_wps_er_stop(wpa_s))
4264 reply_len = -1;
4265 } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
4266 if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
4267 reply_len = -1;
4268 } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
4269 int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
4270 if (ret == -2) {
4271 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
4272 reply_len = 17;
4273 } else if (ret == -3) {
4274 os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
4275 reply_len = 18;
4276 } else if (ret == -4) {
4277 os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
4278 reply_len = 20;
4279 } else if (ret)
4280 reply_len = -1;
4281 } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
4282 if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
4283 reply_len = -1;
4284 } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
4285 if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
4286 buf + 18))
4287 reply_len = -1;
4288 } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
4289 if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
4290 reply_len = -1;
4291 #ifdef CONFIG_WPS_NFC
4292 } else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
4293 reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
4294 wpa_s, buf + 24, reply, reply_size);
4295 #endif /* CONFIG_WPS_NFC */
4296 #endif /* CONFIG_WPS_ER */
4297 #endif /* CONFIG_WPS */
4298 #ifdef CONFIG_IBSS_RSN
4299 } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
4300 if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
4301 reply_len = -1;
4302 #endif /* CONFIG_IBSS_RSN */
4303 #ifdef CONFIG_P2P
4304 } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
4305 if (p2p_ctrl_find(wpa_s, buf + 9))
4306 reply_len = -1;
4307 } else if (os_strcmp(buf, "P2P_FIND") == 0) {
4308 if (p2p_ctrl_find(wpa_s, ""))
4309 reply_len = -1;
4310 } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
4311 wpas_p2p_stop_find(wpa_s);
4312 } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
4313 reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
4314 reply_size);
4315 } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
4316 if (p2p_ctrl_listen(wpa_s, buf + 11))
4317 reply_len = -1;
4318 } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
4319 if (p2p_ctrl_listen(wpa_s, ""))
4320 reply_len = -1;
4321 } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
4322 if (wpas_p2p_group_remove(wpa_s, buf + 17))
4323 reply_len = -1;
4324 } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
4325 if (wpas_p2p_group_add(wpa_s, 0, 0, 0))
4326 reply_len = -1;
4327 } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
4328 if (p2p_ctrl_group_add(wpa_s, buf + 14))
4329 reply_len = -1;
4330 } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
4331 if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
4332 reply_len = -1;
4333 } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
4334 reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
4335 } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
4336 reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
4337 reply_size);
4338 } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
4339 if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
4340 reply_len = -1;
4341 } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
4342 if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
4343 reply_len = -1;
4344 } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
4345 wpas_p2p_sd_service_update(wpa_s);
4346 } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
4347 if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
4348 reply_len = -1;
4349 } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
4350 wpas_p2p_service_flush(wpa_s);
4351 } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
4352 if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
4353 reply_len = -1;
4354 } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
4355 if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
4356 reply_len = -1;
4357 } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
4358 if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
4359 reply_len = -1;
4360 } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
4361 if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
4362 reply_len = -1;
4363 } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
4364 reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
4365 reply_size);
4366 } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
4367 if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
4368 reply_len = -1;
4369 } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
4370 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
4371 wpa_s->force_long_sd = 0;
4372 if (wpa_s->global->p2p)
4373 p2p_flush(wpa_s->global->p2p);
4374 } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
4375 if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
4376 reply_len = -1;
4377 } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
4378 if (wpas_p2p_cancel(wpa_s))
4379 reply_len = -1;
4380 } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
4381 if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
4382 reply_len = -1;
4383 } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
4384 if (p2p_ctrl_presence_req(wpa_s, "") < 0)
4385 reply_len = -1;
4386 } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
4387 if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
4388 reply_len = -1;
4389 } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
4390 if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
4391 reply_len = -1;
4392 #endif /* CONFIG_P2P */
4393 #ifdef CONFIG_INTERWORKING
4394 } else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
4395 if (interworking_fetch_anqp(wpa_s) < 0)
4396 reply_len = -1;
4397 } else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
4398 interworking_stop_fetch_anqp(wpa_s);
4399 } else if (os_strncmp(buf, "INTERWORKING_SELECT", 19) == 0) {
4400 if (interworking_select(wpa_s, os_strstr(buf + 19, "auto") !=
4401 NULL) < 0)
4402 reply_len = -1;
4403 } else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
4404 if (ctrl_interworking_connect(wpa_s, buf + 21) < 0)
4405 reply_len = -1;
4406 } else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
4407 if (get_anqp(wpa_s, buf + 9) < 0)
4408 reply_len = -1;
4409 #endif /* CONFIG_INTERWORKING */
4410 #ifdef CONFIG_HS20
4411 } else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
4412 if (get_hs20_anqp(wpa_s, buf + 14) < 0)
4413 reply_len = -1;
4414 } else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
4415 if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
4416 reply_len = -1;
4417 #endif /* CONFIG_HS20 */
4418 } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
4419 {
4420 if (wpa_supplicant_ctrl_iface_ctrl_rsp(
4421 wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
4422 reply_len = -1;
4423 else
4424 ctrl_rsp = 1;
4425 } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
4426 if (wpa_supplicant_reload_configuration(wpa_s))
4427 reply_len = -1;
4428 } else if (os_strcmp(buf, "TERMINATE") == 0) {
4429 wpa_supplicant_terminate_proc(wpa_s->global);
4430 } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
4431 if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
4432 reply_len = -1;
4433 } else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
4434 reply_len = wpa_supplicant_ctrl_iface_blacklist(
4435 wpa_s, buf + 9, reply, reply_size);
4436 } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
4437 reply_len = wpa_supplicant_ctrl_iface_log_level(
4438 wpa_s, buf + 9, reply, reply_size);
4439 } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
4440 reply_len = wpa_supplicant_ctrl_iface_list_networks(
4441 wpa_s, reply, reply_size);
4442 } else if (os_strcmp(buf, "DISCONNECT") == 0) {
4443 #ifdef CONFIG_SME
4444 wpa_s->sme.prev_bssid_set = 0;
4445 #endif /* CONFIG_SME */
4446 wpa_s->reassociate = 0;
4447 wpa_s->disconnected = 1;
4448 wpa_supplicant_cancel_sched_scan(wpa_s);
4449 wpa_supplicant_cancel_scan(wpa_s);
4450 wpa_supplicant_deauthenticate(wpa_s,
4451 WLAN_REASON_DEAUTH_LEAVING);
4452 } else if (os_strcmp(buf, "SCAN") == 0) {
4453 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
4454 reply_len = -1;
4455 else {
4456 if (!wpa_s->scanning &&
4457 ((wpa_s->wpa_state <= WPA_SCANNING) ||
4458 (wpa_s->wpa_state == WPA_COMPLETED))) {
4459 wpa_s->normal_scans = 0;
4460 wpa_s->scan_req = 2;
4461 wpa_supplicant_req_scan(wpa_s, 0, 0);
4462 } else if (wpa_s->sched_scanning) {
4463 wpa_printf(MSG_DEBUG, "Stop ongoing "
4464 "sched_scan to allow requested "
4465 "full scan to proceed");
4466 wpa_supplicant_cancel_sched_scan(wpa_s);
4467 wpa_s->scan_req = 2;
4468 wpa_supplicant_req_scan(wpa_s, 0, 0);
4469 } else {
4470 wpa_printf(MSG_DEBUG, "Ongoing scan action - "
4471 "reject new request");
4472 reply_len = os_snprintf(reply, reply_size,
4473 "FAIL-BUSY\n");
4474 }
4475 }
4476 } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
4477 reply_len = wpa_supplicant_ctrl_iface_scan_results(
4478 wpa_s, reply, reply_size);
4479 } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
4480 if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
4481 reply_len = -1;
4482 } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
4483 if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
4484 reply_len = -1;
4485 } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
4486 if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
4487 reply_len = -1;
4488 } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
4489 reply_len = wpa_supplicant_ctrl_iface_add_network(
4490 wpa_s, reply, reply_size);
4491 } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
4492 if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
4493 reply_len = -1;
4494 } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
4495 if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
4496 reply_len = -1;
4497 } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
4498 reply_len = wpa_supplicant_ctrl_iface_get_network(
4499 wpa_s, buf + 12, reply, reply_size);
4500 } else if (os_strcmp(buf, "LIST_CREDS") == 0) {
4501 reply_len = wpa_supplicant_ctrl_iface_list_creds(
4502 wpa_s, reply, reply_size);
4503 } else if (os_strcmp(buf, "ADD_CRED") == 0) {
4504 reply_len = wpa_supplicant_ctrl_iface_add_cred(
4505 wpa_s, reply, reply_size);
4506 } else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
4507 if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
4508 reply_len = -1;
4509 } else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
4510 if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
4511 reply_len = -1;
4512 #ifndef CONFIG_NO_CONFIG_WRITE
4513 } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
4514 if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
4515 reply_len = -1;
4516 #endif /* CONFIG_NO_CONFIG_WRITE */
4517 } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
4518 reply_len = wpa_supplicant_ctrl_iface_get_capability(
4519 wpa_s, buf + 15, reply, reply_size);
4520 } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
4521 if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
4522 reply_len = -1;
4523 } else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
4524 if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
4525 reply_len = -1;
4526 } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
4527 reply_len = wpa_supplicant_global_iface_list(
4528 wpa_s->global, reply, reply_size);
4529 } else if (os_strcmp(buf, "INTERFACES") == 0) {
4530 reply_len = wpa_supplicant_global_iface_interfaces(
4531 wpa_s->global, reply, reply_size);
4532 } else if (os_strncmp(buf, "BSS ", 4) == 0) {
4533 reply_len = wpa_supplicant_ctrl_iface_bss(
4534 wpa_s, buf + 4, reply, reply_size);
4535 #ifdef CONFIG_AP
4536 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
4537 reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
4538 } else if (os_strncmp(buf, "STA ", 4) == 0) {
4539 reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
4540 reply_size);
4541 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
4542 reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
4543 reply_size);
4544 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
4545 if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
4546 reply_len = -1;
4547 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
4548 if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
4549 reply_len = -1;
4550 #endif /* CONFIG_AP */
4551 } else if (os_strcmp(buf, "SUSPEND") == 0) {
4552 wpas_notify_suspend(wpa_s->global);
4553 } else if (os_strcmp(buf, "RESUME") == 0) {
4554 wpas_notify_resume(wpa_s->global);
4555 } else if (os_strcmp(buf, "DROP_SA") == 0) {
4556 wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
4557 } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
4558 if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
4559 reply_len = -1;
4560 } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
4561 if (wpa_supplicant_ctrl_iface_sta_autoconnect(wpa_s, buf + 16))
4562 reply_len = -1;
4563 } else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
4564 if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
4565 reply_len = -1;
4566 } else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
4567 if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
4568 buf + 17))
4569 reply_len = -1;
4570 #ifdef CONFIG_TDLS
4571 } else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
4572 if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
4573 reply_len = -1;
4574 } else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
4575 if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
4576 reply_len = -1;
4577 } else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
4578 if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
4579 reply_len = -1;
4580 #endif /* CONFIG_TDLS */
4581 } else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
4582 reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
4583 reply_size);
4584 #ifdef CONFIG_AUTOSCAN
4585 } else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
4586 if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
4587 reply_len = -1;
4588 #endif /* CONFIG_AUTOSCAN */
4589 } else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
4590 eapol_sm_request_reauth(wpa_s->eapol);
4591 } else {
4592 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
4593 reply_len = 16;
4594 }
4595
4596 if (reply_len < 0) {
4597 os_memcpy(reply, "FAIL\n", 5);
4598 reply_len = 5;
4599 }
4600
4601 if (ctrl_rsp)
4602 eapol_sm_notify_ctrl_response(wpa_s->eapol);
4603
4604 *resp_len = reply_len;
4605 return reply;
4606 }
4607
4608
4609 static int wpa_supplicant_global_iface_add(struct wpa_global *global,
4610 char *cmd)
4611 {
4612 struct wpa_interface iface;
4613 char *pos;
4614
4615 /*
4616 * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
4617 * TAB<bridge_ifname>
4618 */
4619 wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
4620
4621 os_memset(&iface, 0, sizeof(iface));
4622
4623 do {
4624 iface.ifname = pos = cmd;
4625 pos = os_strchr(pos, '\t');
4626 if (pos)
4627 *pos++ = '\0';
4628 if (iface.ifname[0] == '\0')
4629 return -1;
4630 if (pos == NULL)
4631 break;
4632
4633 iface.confname = pos;
4634 pos = os_strchr(pos, '\t');
4635 if (pos)
4636 *pos++ = '\0';
4637 if (iface.confname[0] == '\0')
4638 iface.confname = NULL;
4639 if (pos == NULL)
4640 break;
4641
4642 iface.driver = pos;
4643 pos = os_strchr(pos, '\t');
4644 if (pos)
4645 *pos++ = '\0';
4646 if (iface.driver[0] == '\0')
4647 iface.driver = NULL;
4648 if (pos == NULL)
4649 break;
4650
4651 iface.ctrl_interface = pos;
4652 pos = os_strchr(pos, '\t');
4653 if (pos)
4654 *pos++ = '\0';
4655 if (iface.ctrl_interface[0] == '\0')
4656 iface.ctrl_interface = NULL;
4657 if (pos == NULL)
4658 break;
4659
4660 iface.driver_param = pos;
4661 pos = os_strchr(pos, '\t');
4662 if (pos)
4663 *pos++ = '\0';
4664 if (iface.driver_param[0] == '\0')
4665 iface.driver_param = NULL;
4666 if (pos == NULL)
4667 break;
4668
4669 iface.bridge_ifname = pos;
4670 pos = os_strchr(pos, '\t');
4671 if (pos)
4672 *pos++ = '\0';
4673 if (iface.bridge_ifname[0] == '\0')
4674 iface.bridge_ifname = NULL;
4675 if (pos == NULL)
4676 break;
4677 } while (0);
4678
4679 if (wpa_supplicant_get_iface(global, iface.ifname))
4680 return -1;
4681
4682 return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
4683 }
4684
4685
4686 static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
4687 char *cmd)
4688 {
4689 struct wpa_supplicant *wpa_s;
4690
4691 wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
4692
4693 wpa_s = wpa_supplicant_get_iface(global, cmd);
4694 if (wpa_s == NULL)
4695 return -1;
4696 return wpa_supplicant_remove_iface(global, wpa_s, 0);
4697 }
4698
4699
4700 static void wpa_free_iface_info(struct wpa_interface_info *iface)
4701 {
4702 struct wpa_interface_info *prev;
4703
4704 while (iface) {
4705 prev = iface;
4706 iface = iface->next;
4707
4708 os_free(prev->ifname);
4709 os_free(prev->desc);
4710 os_free(prev);
4711 }
4712 }
4713
4714
4715 static int wpa_supplicant_global_iface_list(struct wpa_global *global,
4716 char *buf, int len)
4717 {
4718 int i, res;
4719 struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
4720 char *pos, *end;
4721
4722 for (i = 0; wpa_drivers[i]; i++) {
4723 struct wpa_driver_ops *drv = wpa_drivers[i];
4724 if (drv->get_interfaces == NULL)
4725 continue;
4726 tmp = drv->get_interfaces(global->drv_priv[i]);
4727 if (tmp == NULL)
4728 continue;
4729
4730 if (last == NULL)
4731 iface = last = tmp;
4732 else
4733 last->next = tmp;
4734 while (last->next)
4735 last = last->next;
4736 }
4737
4738 pos = buf;
4739 end = buf + len;
4740 for (tmp = iface; tmp; tmp = tmp->next) {
4741 res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
4742 tmp->drv_name, tmp->ifname,
4743 tmp->desc ? tmp->desc : "");
4744 if (res < 0 || res >= end - pos) {
4745 *pos = '\0';
4746 break;
4747 }
4748 pos += res;
4749 }
4750
4751 wpa_free_iface_info(iface);
4752
4753 return pos - buf;
4754 }
4755
4756
4757 static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
4758 char *buf, int len)
4759 {
4760 int res;
4761 char *pos, *end;
4762 struct wpa_supplicant *wpa_s;
4763
4764 wpa_s = global->ifaces;
4765 pos = buf;
4766 end = buf + len;
4767
4768 while (wpa_s) {
4769 res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
4770 if (res < 0 || res >= end - pos) {
4771 *pos = '\0';
4772 break;
4773 }
4774 pos += res;
4775 wpa_s = wpa_s->next;
4776 }
4777 return pos - buf;
4778 }
4779
4780
4781 char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
4782 char *buf, size_t *resp_len)
4783 {
4784 char *reply;
4785 const int reply_size = 2048;
4786 int reply_len;
4787 int level = MSG_DEBUG;
4788
4789 if (os_strcmp(buf, "PING") == 0)
4790 level = MSG_EXCESSIVE;
4791 wpa_hexdump_ascii(level, "RX global ctrl_iface",
4792 (const u8 *) buf, os_strlen(buf));
4793
4794 reply = os_malloc(reply_size);
4795 if (reply == NULL) {
4796 *resp_len = 1;
4797 return NULL;
4798 }
4799
4800 os_memcpy(reply, "OK\n", 3);
4801 reply_len = 3;
4802
4803 if (os_strcmp(buf, "PING") == 0) {
4804 os_memcpy(reply, "PONG\n", 5);
4805 reply_len = 5;
4806 } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
4807 if (wpa_supplicant_global_iface_add(global, buf + 14))
4808 reply_len = -1;
4809 } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
4810 if (wpa_supplicant_global_iface_remove(global, buf + 17))
4811 reply_len = -1;
4812 } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
4813 reply_len = wpa_supplicant_global_iface_list(
4814 global, reply, reply_size);
4815 } else if (os_strcmp(buf, "INTERFACES") == 0) {
4816 reply_len = wpa_supplicant_global_iface_interfaces(
4817 global, reply, reply_size);
4818 } else if (os_strcmp(buf, "TERMINATE") == 0) {
4819 wpa_supplicant_terminate_proc(global);
4820 } else if (os_strcmp(buf, "SUSPEND") == 0) {
4821 wpas_notify_suspend(global);
4822 } else if (os_strcmp(buf, "RESUME") == 0) {
4823 wpas_notify_resume(global);
4824 } else {
4825 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
4826 reply_len = 16;
4827 }
4828
4829 if (reply_len < 0) {
4830 os_memcpy(reply, "FAIL\n", 5);
4831 reply_len = 5;
4832 }
4833
4834 *resp_len = reply_len;
4835 return reply;
4836 }