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