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