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