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