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