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