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