Home | History | Annotate | Download | only in wpa_supplicant
      1 /*
      2  * wpa_supplicant - P2P
      3  * Copyright (c) 2009-2010, Atheros Communications
      4  *
      5  * This software may be distributed under the terms of the BSD license.
      6  * See README for more details.
      7  */
      8 
      9 #include "includes.h"
     10 
     11 #include "common.h"
     12 #include "eloop.h"
     13 #include "common/ieee802_11_common.h"
     14 #include "common/ieee802_11_defs.h"
     15 #include "common/wpa_ctrl.h"
     16 #include "wps/wps_i.h"
     17 #include "p2p/p2p.h"
     18 #include "ap/hostapd.h"
     19 #include "ap/ap_config.h"
     20 #include "ap/sta_info.h"
     21 #include "ap/ap_drv_ops.h"
     22 #include "ap/p2p_hostapd.h"
     23 #include "eapol_supp/eapol_supp_sm.h"
     24 #include "rsn_supp/wpa.h"
     25 #include "wpa_supplicant_i.h"
     26 #include "driver_i.h"
     27 #include "ap.h"
     28 #include "config_ssid.h"
     29 #include "config.h"
     30 #include "notify.h"
     31 #include "scan.h"
     32 #include "bss.h"
     33 #include "offchannel.h"
     34 #include "wps_supplicant.h"
     35 #include "p2p_supplicant.h"
     36 
     37 
     38 /*
     39  * How many times to try to scan to find the GO before giving up on join
     40  * request.
     41  */
     42 #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
     43 
     44 #define P2P_AUTO_PD_SCAN_ATTEMPTS 5
     45 
     46 #ifndef P2P_MAX_CLIENT_IDLE
     47 /*
     48  * How many seconds to try to reconnect to the GO when connection in P2P client
     49  * role has been lost.
     50  */
     51 #ifdef ANDROID_P2P
     52 #define P2P_MAX_CLIENT_IDLE 20
     53 #else
     54 #define P2P_MAX_CLIENT_IDLE 10
     55 #endif /* ANDROID_P2P */
     56 #endif /* P2P_MAX_CLIENT_IDLE */
     57 
     58 #ifndef P2P_MAX_INITIAL_CONN_WAIT
     59 /*
     60  * How many seconds to wait for initial 4-way handshake to get completed after
     61  * WPS provisioning step.
     62  */
     63 #define P2P_MAX_INITIAL_CONN_WAIT 10
     64 #endif /* P2P_MAX_INITIAL_CONN_WAIT */
     65 
     66 #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
     67 /*
     68  * How many seconds to wait for initial 4-way handshake to get completed after
     69  * WPS provisioning step on the GO. This controls the extra time the P2P
     70  * operation is considered to be in progress (e.g., to delay other scans) after
     71  * WPS provisioning has been completed on the GO during group formation.
     72  */
     73 #define P2P_MAX_INITIAL_CONN_WAIT_GO 10
     74 #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
     75 
     76 #ifndef P2P_CONCURRENT_SEARCH_DELAY
     77 #define P2P_CONCURRENT_SEARCH_DELAY 500
     78 #endif /* P2P_CONCURRENT_SEARCH_DELAY */
     79 
     80 #define P2P_MGMT_DEVICE_PREFIX		"p2p-dev-"
     81 
     82 enum p2p_group_removal_reason {
     83 	P2P_GROUP_REMOVAL_UNKNOWN,
     84 	P2P_GROUP_REMOVAL_SILENT,
     85 	P2P_GROUP_REMOVAL_FORMATION_FAILED,
     86 	P2P_GROUP_REMOVAL_REQUESTED,
     87 	P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
     88 	P2P_GROUP_REMOVAL_UNAVAILABLE,
     89 	P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
     90 	P2P_GROUP_REMOVAL_PSK_FAILURE,
     91 #ifdef ANDROID_P2P
     92 	P2P_GROUP_REMOVAL_FREQ_CONFLICT
     93 #endif
     94 };
     95 
     96 
     97 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
     98 static struct wpa_supplicant *
     99 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
    100 			 int go);
    101 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
    102 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq);
    103 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
    104 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
    105 			 const u8 *dev_addr, enum p2p_wps_method wps_method,
    106 			 int auto_join);
    107 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
    108 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
    109 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
    110 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
    111 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
    112 					     void *timeout_ctx);
    113 #ifdef ANDROID_P2P
    114 static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
    115 #endif
    116 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
    117 					int group_added);
    118 static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
    119 
    120 
    121 /*
    122  * Get the number of concurrent channels that the HW can operate, but that are
    123  * currently not in use by any of the wpa_supplicant interfaces.
    124  */
    125 static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
    126 {
    127 	int *freqs;
    128 	int num;
    129 
    130 	freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
    131 	if (!freqs)
    132 		return -1;
    133 
    134 	num = get_shared_radio_freqs(wpa_s, freqs,
    135 				     wpa_s->num_multichan_concurrent);
    136 	os_free(freqs);
    137 
    138 	return wpa_s->num_multichan_concurrent - num;
    139 }
    140 
    141 
    142 /*
    143  * Get the frequencies that are currently in use by one or more of the virtual
    144  * interfaces, and that are also valid for P2P operation.
    145  */
    146 static int wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
    147 				     int *p2p_freqs, unsigned int len)
    148 {
    149 	int *freqs;
    150 	unsigned int num, i, j;
    151 
    152 	freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
    153 	if (!freqs)
    154 		return -1;
    155 
    156 	num = get_shared_radio_freqs(wpa_s, freqs,
    157 				     wpa_s->num_multichan_concurrent);
    158 
    159 	os_memset(p2p_freqs, 0, sizeof(int) * len);
    160 
    161 	for (i = 0, j = 0; i < num && j < len; i++) {
    162 		if (p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
    163 			p2p_freqs[j++] = freqs[i];
    164 	}
    165 
    166 	os_free(freqs);
    167 
    168 	return j;
    169 }
    170 
    171 
    172 static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
    173 					     int freq)
    174 {
    175 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
    176 		return;
    177 	if (freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
    178 	    wpas_p2p_num_unused_channels(wpa_s) > 0 &&
    179 	    wpa_s->parent->conf->p2p_ignore_shared_freq)
    180 		freq = 0;
    181 	p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
    182 }
    183 
    184 
    185 static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
    186 				      struct wpa_scan_results *scan_res)
    187 {
    188 	size_t i;
    189 
    190 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
    191 		return;
    192 
    193 	wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
    194 		   (int) scan_res->num);
    195 
    196 	for (i = 0; i < scan_res->num; i++) {
    197 		struct wpa_scan_res *bss = scan_res->res[i];
    198 		struct os_time time_tmp_age, entry_ts;
    199 		time_tmp_age.sec = bss->age / 1000;
    200 		time_tmp_age.usec = (bss->age % 1000) * 1000;
    201 		os_time_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
    202 		if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
    203 					 bss->freq, &entry_ts, bss->level,
    204 					 (const u8 *) (bss + 1),
    205 					 bss->ie_len) > 0)
    206 			break;
    207 	}
    208 
    209 	p2p_scan_res_handled(wpa_s->global->p2p);
    210 }
    211 
    212 
    213 static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
    214 			 unsigned int num_req_dev_types,
    215 			 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
    216 {
    217 	struct wpa_supplicant *wpa_s = ctx;
    218 	struct wpa_supplicant *ifs;
    219 	struct wpa_driver_scan_params params;
    220 	int ret;
    221 	struct wpabuf *wps_ie, *ies;
    222 	int social_channels[] = { 2412, 2437, 2462, 0, 0 };
    223 	size_t ielen;
    224 
    225 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
    226 		return -1;
    227 
    228 	for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
    229 		if (ifs->sta_scan_pending &&
    230 		    (wpas_scan_scheduled(ifs) || ifs->scanning) &&
    231 		    wpas_p2p_in_progress(wpa_s) == 2) {
    232 			wpa_printf(MSG_DEBUG, "Delaying P2P scan to allow "
    233 				   "pending station mode scan to be "
    234 				   "completed on interface %s", ifs->ifname);
    235 			wpa_s->global->p2p_cb_on_scan_complete = 1;
    236 			wpa_supplicant_req_scan(ifs, 0, 0);
    237 			return 1;
    238 		}
    239 	}
    240 
    241 	os_memset(&params, 0, sizeof(params));
    242 
    243 	/* P2P Wildcard SSID */
    244 	params.num_ssids = 1;
    245 	params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
    246 	params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
    247 
    248 	wpa_s->wps->dev.p2p = 1;
    249 	wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
    250 					wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
    251 					num_req_dev_types, req_dev_types);
    252 	if (wps_ie == NULL)
    253 		return -1;
    254 
    255 	ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
    256 	ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
    257 	if (ies == NULL) {
    258 		wpabuf_free(wps_ie);
    259 		return -1;
    260 	}
    261 	wpabuf_put_buf(ies, wps_ie);
    262 	wpabuf_free(wps_ie);
    263 
    264 	p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
    265 
    266 	params.p2p_probe = 1;
    267 	params.extra_ies = wpabuf_head(ies);
    268 	params.extra_ies_len = wpabuf_len(ies);
    269 
    270 	switch (type) {
    271 	case P2P_SCAN_SOCIAL:
    272 		params.freqs = social_channels;
    273 		break;
    274 	case P2P_SCAN_FULL:
    275 		break;
    276 	case P2P_SCAN_SOCIAL_PLUS_ONE:
    277 		social_channels[3] = freq;
    278 		params.freqs = social_channels;
    279 		break;
    280 	}
    281 
    282 	ret = wpa_drv_scan(wpa_s, &params);
    283 
    284 	wpabuf_free(ies);
    285 
    286 	if (ret) {
    287 		for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
    288 			if (ifs->scanning ||
    289 			    ifs->scan_res_handler == wpas_p2p_scan_res_handler) {
    290 				wpa_s->global->p2p_cb_on_scan_complete = 1;
    291 				ret = 1;
    292 				break;
    293 			}
    294 		}
    295 	} else {
    296 		os_get_time(&wpa_s->scan_trigger_time);
    297 		wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
    298 	}
    299 
    300 	return ret;
    301 }
    302 
    303 
    304 static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
    305 {
    306 	switch (p2p_group_interface) {
    307 	case P2P_GROUP_INTERFACE_PENDING:
    308 		return WPA_IF_P2P_GROUP;
    309 	case P2P_GROUP_INTERFACE_GO:
    310 		return WPA_IF_P2P_GO;
    311 	case P2P_GROUP_INTERFACE_CLIENT:
    312 		return WPA_IF_P2P_CLIENT;
    313 	}
    314 
    315 	return WPA_IF_P2P_GROUP;
    316 }
    317 
    318 
    319 static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
    320 						  const u8 *ssid,
    321 						  size_t ssid_len, int *go)
    322 {
    323 	struct wpa_ssid *s;
    324 
    325 	for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
    326 		for (s = wpa_s->conf->ssid; s; s = s->next) {
    327 			if (s->disabled != 0 || !s->p2p_group ||
    328 			    s->ssid_len != ssid_len ||
    329 			    os_memcmp(ssid, s->ssid, ssid_len) != 0)
    330 				continue;
    331 			if (s->mode == WPAS_MODE_P2P_GO &&
    332 			    s != wpa_s->current_ssid)
    333 				continue;
    334 			if (go)
    335 				*go = s->mode == WPAS_MODE_P2P_GO;
    336 			return wpa_s;
    337 		}
    338 	}
    339 
    340 	return NULL;
    341 }
    342 
    343 
    344 static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
    345 				 enum p2p_group_removal_reason removal_reason)
    346 {
    347 	struct wpa_ssid *ssid;
    348 	char *gtype;
    349 	const char *reason;
    350 
    351 	ssid = wpa_s->current_ssid;
    352 	if (ssid == NULL) {
    353 		/*
    354 		 * The current SSID was not known, but there may still be a
    355 		 * pending P2P group interface waiting for provisioning or a
    356 		 * P2P group that is trying to reconnect.
    357 		 */
    358 		ssid = wpa_s->conf->ssid;
    359 		while (ssid) {
    360 			if (ssid->p2p_group && ssid->disabled != 2)
    361 				break;
    362 			ssid = ssid->next;
    363 		}
    364 		if (ssid == NULL &&
    365 			wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
    366 		{
    367 			wpa_printf(MSG_ERROR, "P2P: P2P group interface "
    368 				   "not found");
    369 			return -1;
    370 		}
    371 	}
    372 	if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
    373 		gtype = "GO";
    374 	else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
    375 		 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
    376 		wpa_s->reassociate = 0;
    377 		wpa_s->disconnected = 1;
    378 		wpa_supplicant_deauthenticate(wpa_s,
    379 					      WLAN_REASON_DEAUTH_LEAVING);
    380 		gtype = "client";
    381 	} else
    382 		gtype = "GO";
    383 	if (wpa_s->cross_connect_in_use) {
    384 		wpa_s->cross_connect_in_use = 0;
    385 		wpa_msg_global(wpa_s->parent, MSG_INFO,
    386 			       P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
    387 			       wpa_s->ifname, wpa_s->cross_connect_uplink);
    388 	}
    389 	switch (removal_reason) {
    390 	case P2P_GROUP_REMOVAL_REQUESTED:
    391 		reason = " reason=REQUESTED";
    392 		break;
    393 	case P2P_GROUP_REMOVAL_FORMATION_FAILED:
    394 		reason = " reason=FORMATION_FAILED";
    395 		break;
    396 	case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
    397 		reason = " reason=IDLE";
    398 		break;
    399 	case P2P_GROUP_REMOVAL_UNAVAILABLE:
    400 		reason = " reason=UNAVAILABLE";
    401 		break;
    402 	case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
    403 		reason = " reason=GO_ENDING_SESSION";
    404 		break;
    405 	case P2P_GROUP_REMOVAL_PSK_FAILURE:
    406 		reason = " reason=PSK_FAILURE";
    407 		break;
    408 #ifdef ANDROID_P2P
    409 	case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
    410 		reason = " reason=FREQ_CONFLICT";
    411 		break;
    412 #endif
    413 	default:
    414 		reason = "";
    415 		break;
    416 	}
    417 	if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
    418 		wpa_msg_global(wpa_s->parent, MSG_INFO,
    419 			       P2P_EVENT_GROUP_REMOVED "%s %s%s",
    420 			       wpa_s->ifname, gtype, reason);
    421 	}
    422 
    423 #ifdef ANDROID_P2P
    424 	eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
    425 #endif
    426 	if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
    427 		wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
    428 	if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
    429 				 wpa_s->parent, NULL) > 0) {
    430 		wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
    431 			   "timeout");
    432 		wpa_s->p2p_in_provisioning = 0;
    433 	}
    434 
    435 	if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
    436 		wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
    437 
    438 	if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
    439 		struct wpa_global *global;
    440 		char *ifname;
    441 		enum wpa_driver_if_type type;
    442 		wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
    443 			wpa_s->ifname);
    444 		global = wpa_s->global;
    445 		ifname = os_strdup(wpa_s->ifname);
    446 		type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
    447 		wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
    448 		wpa_s = global->ifaces;
    449 		if (wpa_s && ifname)
    450 			wpa_drv_if_remove(wpa_s, type, ifname);
    451 		os_free(ifname);
    452 		return 1;
    453 	}
    454 
    455 	wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
    456 	if (ssid && (ssid->p2p_group ||
    457 		     ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
    458 		     (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
    459 		int id = ssid->id;
    460 		if (ssid == wpa_s->current_ssid) {
    461 			wpa_sm_set_config(wpa_s->wpa, NULL);
    462 			eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
    463 			wpa_s->current_ssid = NULL;
    464 		}
    465 		/*
    466 		 * Networks objects created during any P2P activities are not
    467 		 * exposed out as they might/will confuse certain non-P2P aware
    468 		 * applications since these network objects won't behave like
    469 		 * regular ones.
    470 		 *
    471 		 * Likewise, we don't send out network removed signals for such
    472 		 * network objects.
    473 		 */
    474 		wpa_config_remove_network(wpa_s->conf, id);
    475 		wpa_supplicant_clear_status(wpa_s);
    476 		wpa_supplicant_cancel_sched_scan(wpa_s);
    477 		wpa_s->sta_scan_pending = 0;
    478 	} else {
    479 		wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
    480 			   "found");
    481 	}
    482 	if (wpa_s->ap_iface)
    483 		wpa_supplicant_ap_deinit(wpa_s);
    484 	else
    485 		wpa_drv_deinit_p2p_cli(wpa_s);
    486 
    487 	return 0;
    488 }
    489 
    490 
    491 static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
    492 				     u8 *go_dev_addr,
    493 				     const u8 *ssid, size_t ssid_len)
    494 {
    495 	struct wpa_bss *bss;
    496 	const u8 *bssid;
    497 	struct wpabuf *p2p;
    498 	u8 group_capab;
    499 	const u8 *addr;
    500 
    501 	if (wpa_s->go_params)
    502 		bssid = wpa_s->go_params->peer_interface_addr;
    503 	else
    504 		bssid = wpa_s->bssid;
    505 
    506 	bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
    507 	if (bss == NULL) {
    508 		u8 iface_addr[ETH_ALEN];
    509 		if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
    510 					   iface_addr) == 0)
    511 			bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
    512 	}
    513 	if (bss == NULL) {
    514 		wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
    515 			   "group is persistent - BSS " MACSTR " not found",
    516 			   MAC2STR(bssid));
    517 		return 0;
    518 	}
    519 
    520 	p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
    521 	if (p2p == NULL) {
    522 		wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
    523 			   "group is persistent - BSS " MACSTR
    524 			   " did not include P2P IE", MAC2STR(bssid));
    525 		wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
    526 			    (u8 *) (bss + 1), bss->ie_len);
    527 		wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
    528 			    ((u8 *) bss + 1) + bss->ie_len,
    529 			    bss->beacon_ie_len);
    530 		return 0;
    531 	}
    532 
    533 	group_capab = p2p_get_group_capab(p2p);
    534 	addr = p2p_get_go_dev_addr(p2p);
    535 	wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
    536 		   "group_capab=0x%x", group_capab);
    537 	if (addr) {
    538 		os_memcpy(go_dev_addr, addr, ETH_ALEN);
    539 		wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
    540 			   MAC2STR(addr));
    541 	} else
    542 		os_memset(go_dev_addr, 0, ETH_ALEN);
    543 	wpabuf_free(p2p);
    544 
    545 	wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
    546 		   "go_dev_addr=" MACSTR,
    547 		   MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
    548 
    549 	return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
    550 }
    551 
    552 
    553 static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
    554 					   struct wpa_ssid *ssid,
    555 					   const u8 *go_dev_addr)
    556 {
    557 	struct wpa_ssid *s;
    558 	int changed = 0;
    559 
    560 	wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
    561 		   "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
    562 	for (s = wpa_s->conf->ssid; s; s = s->next) {
    563 		if (s->disabled == 2 &&
    564 		    os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
    565 		    s->ssid_len == ssid->ssid_len &&
    566 		    os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
    567 			break;
    568 	}
    569 
    570 	if (s) {
    571 		wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
    572 			   "entry");
    573 		if (ssid->passphrase && !s->passphrase)
    574 			changed = 1;
    575 		else if (ssid->passphrase && s->passphrase &&
    576 			 os_strcmp(ssid->passphrase, s->passphrase) != 0)
    577 			changed = 1;
    578 	} else {
    579 		wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
    580 			   "entry");
    581 		changed = 1;
    582 		s = wpa_config_add_network(wpa_s->conf);
    583 		if (s == NULL)
    584 			return -1;
    585 
    586 		/*
    587 		 * Instead of network_added we emit persistent_group_added
    588 		 * notification. Also to keep the defense checks in
    589 		 * persistent_group obj registration method, we set the
    590 		 * relevant flags in s to designate it as a persistent group.
    591 		 */
    592 		s->p2p_group = 1;
    593 		s->p2p_persistent_group = 1;
    594 		wpas_notify_persistent_group_added(wpa_s, s);
    595 		wpa_config_set_network_defaults(s);
    596 	}
    597 
    598 	s->p2p_group = 1;
    599 	s->p2p_persistent_group = 1;
    600 	s->disabled = 2;
    601 	s->bssid_set = 1;
    602 	os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
    603 	s->mode = ssid->mode;
    604 	s->auth_alg = WPA_AUTH_ALG_OPEN;
    605 	s->key_mgmt = WPA_KEY_MGMT_PSK;
    606 	s->proto = WPA_PROTO_RSN;
    607 	s->pairwise_cipher = WPA_CIPHER_CCMP;
    608 	s->export_keys = 1;
    609 	if (ssid->passphrase) {
    610 		os_free(s->passphrase);
    611 		s->passphrase = os_strdup(ssid->passphrase);
    612 	}
    613 	if (ssid->psk_set) {
    614 		s->psk_set = 1;
    615 		os_memcpy(s->psk, ssid->psk, 32);
    616 	}
    617 	if (s->passphrase && !s->psk_set)
    618 		wpa_config_update_psk(s);
    619 	if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
    620 		os_free(s->ssid);
    621 		s->ssid = os_malloc(ssid->ssid_len);
    622 	}
    623 	if (s->ssid) {
    624 		s->ssid_len = ssid->ssid_len;
    625 		os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
    626 	}
    627 	if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
    628 		dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
    629 		wpa_s->global->add_psk = NULL;
    630 		changed = 1;
    631 	}
    632 
    633 #ifndef CONFIG_NO_CONFIG_WRITE
    634 	if (changed && wpa_s->conf->update_config &&
    635 	    wpa_config_write(wpa_s->confname, wpa_s->conf)) {
    636 		wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
    637 	}
    638 #endif /* CONFIG_NO_CONFIG_WRITE */
    639 
    640 	return s->id;
    641 }
    642 
    643 
    644 static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
    645 						 const u8 *addr)
    646 {
    647 	struct wpa_ssid *ssid, *s;
    648 	u8 *n;
    649 	size_t i;
    650 	int found = 0;
    651 
    652 	ssid = wpa_s->current_ssid;
    653 	if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
    654 	    !ssid->p2p_persistent_group)
    655 		return;
    656 
    657 	for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
    658 		if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
    659 			continue;
    660 
    661 		if (s->ssid_len == ssid->ssid_len &&
    662 		    os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
    663 			break;
    664 	}
    665 
    666 	if (s == NULL)
    667 		return;
    668 
    669 	for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
    670 		if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
    671 			      ETH_ALEN) != 0)
    672 			continue;
    673 
    674 		if (i == s->num_p2p_clients - 1)
    675 			return; /* already the most recent entry */
    676 
    677 		/* move the entry to mark it most recent */
    678 		os_memmove(s->p2p_client_list + i * ETH_ALEN,
    679 			   s->p2p_client_list + (i + 1) * ETH_ALEN,
    680 			   (s->num_p2p_clients - i - 1) * ETH_ALEN);
    681 		os_memcpy(s->p2p_client_list +
    682 			  (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
    683 		found = 1;
    684 		break;
    685 	}
    686 
    687 	if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
    688 		n = os_realloc_array(s->p2p_client_list,
    689 				     s->num_p2p_clients + 1, ETH_ALEN);
    690 		if (n == NULL)
    691 			return;
    692 		os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
    693 		s->p2p_client_list = n;
    694 		s->num_p2p_clients++;
    695 	} else if (!found) {
    696 		/* Not enough room for an additional entry - drop the oldest
    697 		 * entry */
    698 		os_memmove(s->p2p_client_list,
    699 			   s->p2p_client_list + ETH_ALEN,
    700 			   (s->num_p2p_clients - 1) * ETH_ALEN);
    701 		os_memcpy(s->p2p_client_list +
    702 			  (s->num_p2p_clients - 1) * ETH_ALEN,
    703 			  addr, ETH_ALEN);
    704 	}
    705 
    706 #ifndef CONFIG_NO_CONFIG_WRITE
    707 	if (wpa_s->parent->conf->update_config &&
    708 	    wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
    709 		wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
    710 #endif /* CONFIG_NO_CONFIG_WRITE */
    711 }
    712 
    713 
    714 static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
    715 					   int success)
    716 {
    717 	struct wpa_ssid *ssid;
    718 	const char *ssid_txt;
    719 	int client;
    720 	int persistent;
    721 	u8 go_dev_addr[ETH_ALEN];
    722 	int network_id = -1;
    723 
    724 	/*
    725 	 * This callback is likely called for the main interface. Update wpa_s
    726 	 * to use the group interface if a new interface was created for the
    727 	 * group.
    728 	 */
    729 	if (wpa_s->global->p2p_group_formation)
    730 		wpa_s = wpa_s->global->p2p_group_formation;
    731 	wpa_s->global->p2p_group_formation = NULL;
    732 	wpa_s->p2p_in_provisioning = 0;
    733 
    734 	if (!success) {
    735 		wpa_msg_global(wpa_s->parent, MSG_INFO,
    736 			       P2P_EVENT_GROUP_FORMATION_FAILURE);
    737 		wpas_p2p_group_delete(wpa_s,
    738 				      P2P_GROUP_REMOVAL_FORMATION_FAILED);
    739 		return;
    740 	}
    741 
    742 	wpa_msg_global(wpa_s->parent, MSG_INFO,
    743 		       P2P_EVENT_GROUP_FORMATION_SUCCESS);
    744 
    745 	ssid = wpa_s->current_ssid;
    746 	if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
    747 		ssid->mode = WPAS_MODE_P2P_GO;
    748 		p2p_group_notif_formation_done(wpa_s->p2p_group);
    749 		wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
    750 	}
    751 
    752 	persistent = 0;
    753 	if (ssid) {
    754 		ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
    755 		client = ssid->mode == WPAS_MODE_INFRA;
    756 		if (ssid->mode == WPAS_MODE_P2P_GO) {
    757 			persistent = ssid->p2p_persistent_group;
    758 			os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
    759 				  ETH_ALEN);
    760 		} else
    761 			persistent = wpas_p2p_persistent_group(wpa_s,
    762 							       go_dev_addr,
    763 							       ssid->ssid,
    764 							       ssid->ssid_len);
    765 	} else {
    766 		ssid_txt = "";
    767 		client = wpa_s->p2p_group_interface ==
    768 			P2P_GROUP_INTERFACE_CLIENT;
    769 		os_memset(go_dev_addr, 0, ETH_ALEN);
    770 	}
    771 
    772 	wpa_s->show_group_started = 0;
    773 	if (client) {
    774 		/*
    775 		 * Indicate event only after successfully completed 4-way
    776 		 * handshake, i.e., when the interface is ready for data
    777 		 * packets.
    778 		 */
    779 		wpa_s->show_group_started = 1;
    780 #ifdef ANDROID_P2P
    781 		/* For client Second phase of Group formation (4-way handshake) can be still pending
    782 		 * So we need to restore wpa_s->global->p2p_group_formation */
    783 		wpa_printf(MSG_INFO, "Restoring back wpa_s->global->p2p_group_formation to wpa_s %p\n", wpa_s);
    784 		wpa_s->global->p2p_group_formation = wpa_s;
    785 #endif
    786 
    787 	} else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
    788 		char psk[65];
    789 		wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
    790 		wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
    791 			       "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr="
    792 			       MACSTR "%s",
    793 			       wpa_s->ifname, ssid_txt, ssid->frequency, psk,
    794 			       MAC2STR(go_dev_addr),
    795 			       persistent ? " [PERSISTENT]" : "");
    796 		wpas_p2p_cross_connect_setup(wpa_s);
    797 		wpas_p2p_set_group_idle_timeout(wpa_s);
    798 	} else {
    799 		wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
    800 			       "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
    801 			       "go_dev_addr=" MACSTR "%s",
    802 			       wpa_s->ifname, ssid_txt,
    803 			       ssid ? ssid->frequency : 0,
    804 			       ssid && ssid->passphrase ? ssid->passphrase : "",
    805 			       MAC2STR(go_dev_addr),
    806 			       persistent ? " [PERSISTENT]" : "");
    807 		wpas_p2p_cross_connect_setup(wpa_s);
    808 		wpas_p2p_set_group_idle_timeout(wpa_s);
    809 	}
    810 
    811 	if (persistent)
    812 		network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
    813 							     ssid, go_dev_addr);
    814 	else {
    815 		os_free(wpa_s->global->add_psk);
    816 		wpa_s->global->add_psk = NULL;
    817 	}
    818 	if (network_id < 0 && ssid)
    819 		network_id = ssid->id;
    820 	if (!client) {
    821 		wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
    822 		os_get_time(&wpa_s->global->p2p_go_wait_client);
    823 	}
    824 }
    825 
    826 
    827 static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
    828 					   unsigned int freq,
    829 					   const u8 *dst, const u8 *src,
    830 					   const u8 *bssid,
    831 					   const u8 *data, size_t data_len,
    832 					   enum offchannel_send_action_result
    833 					   result)
    834 {
    835 	enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
    836 
    837 	if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
    838 		return;
    839 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
    840 		return;
    841 
    842 	switch (result) {
    843 	case OFFCHANNEL_SEND_ACTION_SUCCESS:
    844 		res = P2P_SEND_ACTION_SUCCESS;
    845 		break;
    846 	case OFFCHANNEL_SEND_ACTION_NO_ACK:
    847 		res = P2P_SEND_ACTION_NO_ACK;
    848 		break;
    849 	case OFFCHANNEL_SEND_ACTION_FAILED:
    850 		res = P2P_SEND_ACTION_FAILED;
    851 		break;
    852 	}
    853 
    854 	p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
    855 
    856 	if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
    857 	    wpa_s->pending_pd_before_join &&
    858 	    (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
    859 	     os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
    860 	    wpa_s->p2p_fallback_to_go_neg) {
    861 		wpa_s->pending_pd_before_join = 0;
    862 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
    863 			"during p2p_connect-auto");
    864 		wpas_p2p_fallback_to_go_neg(wpa_s, 0);
    865 		return;
    866 	}
    867 }
    868 
    869 
    870 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
    871 			    const u8 *src, const u8 *bssid, const u8 *buf,
    872 			    size_t len, unsigned int wait_time)
    873 {
    874 	struct wpa_supplicant *wpa_s = ctx;
    875 	return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
    876 				      wait_time,
    877 				      wpas_p2p_send_action_tx_status, 1);
    878 }
    879 
    880 
    881 static void wpas_send_action_done(void *ctx)
    882 {
    883 	struct wpa_supplicant *wpa_s = ctx;
    884 	offchannel_send_action_done(wpa_s);
    885 }
    886 
    887 
    888 static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
    889 				    struct p2p_go_neg_results *params)
    890 {
    891 	if (wpa_s->go_params == NULL) {
    892 		wpa_s->go_params = os_malloc(sizeof(*params));
    893 		if (wpa_s->go_params == NULL)
    894 			return -1;
    895 	}
    896 	os_memcpy(wpa_s->go_params, params, sizeof(*params));
    897 	return 0;
    898 }
    899 
    900 
    901 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
    902 				    struct p2p_go_neg_results *res)
    903 {
    904 	wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
    905 		   MAC2STR(res->peer_interface_addr));
    906 	wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
    907 			  res->ssid, res->ssid_len);
    908 	wpa_supplicant_ap_deinit(wpa_s);
    909 	wpas_copy_go_neg_results(wpa_s, res);
    910 	if (res->wps_method == WPS_PBC)
    911 		wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
    912 	else {
    913 		u16 dev_pw_id = DEV_PW_DEFAULT;
    914 		if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
    915 			dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
    916 		wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
    917 				   wpa_s->p2p_pin, 1, dev_pw_id);
    918 	}
    919 }
    920 
    921 
    922 static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
    923 				  struct wpa_ssid *ssid)
    924 {
    925 	struct wpa_ssid *persistent;
    926 	struct psk_list_entry *psk;
    927 	struct hostapd_data *hapd;
    928 
    929 	if (!wpa_s->ap_iface)
    930 		return;
    931 
    932 	persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
    933 					     ssid->ssid_len);
    934 	if (persistent == NULL)
    935 		return;
    936 
    937 	hapd = wpa_s->ap_iface->bss[0];
    938 
    939 	dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
    940 			 list) {
    941 		struct hostapd_wpa_psk *hpsk;
    942 
    943 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
    944 			MACSTR " psk=%d",
    945 			MAC2STR(psk->addr), psk->p2p);
    946 		hpsk = os_zalloc(sizeof(*hpsk));
    947 		if (hpsk == NULL)
    948 			break;
    949 		os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
    950 		if (psk->p2p)
    951 			os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
    952 		else
    953 			os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
    954 		hpsk->next = hapd->conf->ssid.wpa_psk;
    955 		hapd->conf->ssid.wpa_psk = hpsk;
    956 	}
    957 }
    958 
    959 
    960 static void p2p_go_configured(void *ctx, void *data)
    961 {
    962 	struct wpa_supplicant *wpa_s = ctx;
    963 	struct p2p_go_neg_results *params = data;
    964 	struct wpa_ssid *ssid;
    965 	int network_id = -1;
    966 
    967 	ssid = wpa_s->current_ssid;
    968 	if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
    969 		wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
    970 		if (wpa_s->global->p2p_group_formation == wpa_s)
    971 			wpa_s->global->p2p_group_formation = NULL;
    972 		if (os_strlen(params->passphrase) > 0) {
    973 			wpa_msg_global(wpa_s->parent, MSG_INFO,
    974 				       P2P_EVENT_GROUP_STARTED
    975 				       "%s GO ssid=\"%s\" freq=%d "
    976 				       "passphrase=\"%s\" go_dev_addr=" MACSTR
    977 				       "%s", wpa_s->ifname,
    978 				       wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
    979 				       ssid->frequency, params->passphrase,
    980 				       MAC2STR(wpa_s->global->p2p_dev_addr),
    981 				       params->persistent_group ?
    982 				       " [PERSISTENT]" : "");
    983 		} else {
    984 			char psk[65];
    985 			wpa_snprintf_hex(psk, sizeof(psk), params->psk,
    986 					 sizeof(params->psk));
    987 			wpa_msg_global(wpa_s->parent, MSG_INFO,
    988 				       P2P_EVENT_GROUP_STARTED
    989 				       "%s GO ssid=\"%s\" freq=%d psk=%s "
    990 				       "go_dev_addr=" MACSTR "%s",
    991 				       wpa_s->ifname,
    992 				       wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
    993 				       ssid->frequency, psk,
    994 				       MAC2STR(wpa_s->global->p2p_dev_addr),
    995 				       params->persistent_group ?
    996 				       " [PERSISTENT]" : "");
    997 		}
    998 
    999 		if (params->persistent_group) {
   1000 			network_id = wpas_p2p_store_persistent_group(
   1001 				wpa_s->parent, ssid,
   1002 				wpa_s->global->p2p_dev_addr);
   1003 			wpas_p2p_add_psk_list(wpa_s, ssid);
   1004 		}
   1005 		if (network_id < 0)
   1006 			network_id = ssid->id;
   1007 		wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
   1008 		wpas_p2p_cross_connect_setup(wpa_s);
   1009 		wpas_p2p_set_group_idle_timeout(wpa_s);
   1010 		return;
   1011 	}
   1012 
   1013 	wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
   1014 	if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
   1015 					      params->peer_interface_addr)) {
   1016 		wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
   1017 			   "filtering");
   1018 		return;
   1019 	}
   1020 	if (params->wps_method == WPS_PBC)
   1021 		wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
   1022 					  params->peer_device_addr);
   1023 	else if (wpa_s->p2p_pin[0])
   1024 		wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
   1025 					  wpa_s->p2p_pin, NULL, 0, 0);
   1026 	os_free(wpa_s->go_params);
   1027 	wpa_s->go_params = NULL;
   1028 }
   1029 
   1030 
   1031 static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
   1032 			      struct p2p_go_neg_results *params,
   1033 			      int group_formation)
   1034 {
   1035 	struct wpa_ssid *ssid;
   1036 
   1037 	wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
   1038 	if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
   1039 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
   1040 			"results");
   1041 		return;
   1042 	}
   1043 
   1044 	ssid = wpa_config_add_network(wpa_s->conf);
   1045 	if (ssid == NULL) {
   1046 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
   1047 		return;
   1048 	}
   1049 
   1050 	wpa_s->show_group_started = 0;
   1051 
   1052 	wpa_config_set_network_defaults(ssid);
   1053 	ssid->temporary = 1;
   1054 	ssid->p2p_group = 1;
   1055 	ssid->p2p_persistent_group = params->persistent_group;
   1056 	ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
   1057 		WPAS_MODE_P2P_GO;
   1058 	ssid->frequency = params->freq;
   1059 	ssid->ht40 = params->ht40;
   1060 	ssid->ssid = os_zalloc(params->ssid_len + 1);
   1061 	if (ssid->ssid) {
   1062 		os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
   1063 		ssid->ssid_len = params->ssid_len;
   1064 	}
   1065 	ssid->auth_alg = WPA_AUTH_ALG_OPEN;
   1066 	ssid->key_mgmt = WPA_KEY_MGMT_PSK;
   1067 	ssid->proto = WPA_PROTO_RSN;
   1068 	ssid->pairwise_cipher = WPA_CIPHER_CCMP;
   1069 	if (os_strlen(params->passphrase) > 0) {
   1070 		ssid->passphrase = os_strdup(params->passphrase);
   1071 		if (ssid->passphrase == NULL) {
   1072 			wpa_msg_global(wpa_s, MSG_ERROR,
   1073 				       "P2P: Failed to copy passphrase for GO");
   1074 			wpa_config_remove_network(wpa_s->conf, ssid->id);
   1075 			return;
   1076 		}
   1077 	} else
   1078 		ssid->passphrase = NULL;
   1079 	ssid->psk_set = params->psk_set;
   1080 	if (ssid->psk_set)
   1081 		os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
   1082 	else if (ssid->passphrase)
   1083 		wpa_config_update_psk(ssid);
   1084 	ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
   1085 
   1086 	wpa_s->ap_configured_cb = p2p_go_configured;
   1087 	wpa_s->ap_configured_cb_ctx = wpa_s;
   1088 	wpa_s->ap_configured_cb_data = wpa_s->go_params;
   1089 	wpa_s->connect_without_scan = ssid;
   1090 	wpa_s->reassociate = 1;
   1091 	wpa_s->disconnected = 0;
   1092 	wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
   1093 		"start GO)");
   1094 	wpa_supplicant_req_scan(wpa_s, 0, 0);
   1095 }
   1096 
   1097 
   1098 static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
   1099 				  const struct wpa_supplicant *src)
   1100 {
   1101 	struct wpa_config *d;
   1102 	const struct wpa_config *s;
   1103 
   1104 	d = dst->conf;
   1105 	s = src->conf;
   1106 
   1107 #define C(n) if (s->n) d->n = os_strdup(s->n)
   1108 	C(device_name);
   1109 	C(manufacturer);
   1110 	C(model_name);
   1111 	C(model_number);
   1112 	C(serial_number);
   1113 	C(config_methods);
   1114 #undef C
   1115 
   1116 	os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
   1117 	os_memcpy(d->sec_device_type, s->sec_device_type,
   1118 		  sizeof(d->sec_device_type));
   1119 	d->num_sec_device_types = s->num_sec_device_types;
   1120 
   1121 	d->p2p_group_idle = s->p2p_group_idle;
   1122 	d->p2p_intra_bss = s->p2p_intra_bss;
   1123 	d->persistent_reconnect = s->persistent_reconnect;
   1124 	d->max_num_sta = s->max_num_sta;
   1125 	d->pbc_in_m1 = s->pbc_in_m1;
   1126 	d->ignore_old_scan_res = s->ignore_old_scan_res;
   1127 	d->beacon_int = s->beacon_int;
   1128 	d->disassoc_low_ack = s->disassoc_low_ack;
   1129 }
   1130 
   1131 
   1132 static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
   1133 				      char *ifname, size_t len)
   1134 {
   1135 	char *ifname_ptr = wpa_s->ifname;
   1136 
   1137 	if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
   1138 		       os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
   1139 		ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
   1140 	}
   1141 
   1142 	os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
   1143 	if (os_strlen(ifname) >= IFNAMSIZ &&
   1144 	    os_strlen(wpa_s->ifname) < IFNAMSIZ) {
   1145 		/* Try to avoid going over the IFNAMSIZ length limit */
   1146 		os_snprintf(ifname, sizeof(ifname), "p2p-%d",
   1147 			    wpa_s->p2p_group_idx);
   1148 	}
   1149 }
   1150 
   1151 
   1152 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
   1153 					enum wpa_driver_if_type type)
   1154 {
   1155 	char ifname[120], force_ifname[120];
   1156 
   1157 	if (wpa_s->pending_interface_name[0]) {
   1158 		wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
   1159 			   "- skip creation of a new one");
   1160 		if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
   1161 			wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
   1162 				   "unknown?! ifname='%s'",
   1163 				   wpa_s->pending_interface_name);
   1164 			return -1;
   1165 		}
   1166 		return 0;
   1167 	}
   1168 
   1169 	wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
   1170 	force_ifname[0] = '\0';
   1171 
   1172 	wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
   1173 		   ifname);
   1174 	wpa_s->p2p_group_idx++;
   1175 
   1176 	wpa_s->pending_interface_type = type;
   1177 	if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
   1178 			   wpa_s->pending_interface_addr, NULL) < 0) {
   1179 		wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
   1180 			   "interface");
   1181 		return -1;
   1182 	}
   1183 
   1184 	if (force_ifname[0]) {
   1185 		wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
   1186 			   force_ifname);
   1187 		os_strlcpy(wpa_s->pending_interface_name, force_ifname,
   1188 			   sizeof(wpa_s->pending_interface_name));
   1189 	} else
   1190 		os_strlcpy(wpa_s->pending_interface_name, ifname,
   1191 			   sizeof(wpa_s->pending_interface_name));
   1192 	wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
   1193 		   MACSTR, wpa_s->pending_interface_name,
   1194 		   MAC2STR(wpa_s->pending_interface_addr));
   1195 
   1196 	return 0;
   1197 }
   1198 
   1199 
   1200 static void wpas_p2p_remove_pending_group_interface(
   1201 	struct wpa_supplicant *wpa_s)
   1202 {
   1203 	if (!wpa_s->pending_interface_name[0] ||
   1204 	    is_zero_ether_addr(wpa_s->pending_interface_addr))
   1205 		return; /* No pending virtual interface */
   1206 
   1207 	wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
   1208 		   wpa_s->pending_interface_name);
   1209 	wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
   1210 			  wpa_s->pending_interface_name);
   1211 	os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
   1212 	wpa_s->pending_interface_name[0] = '\0';
   1213 }
   1214 
   1215 
   1216 static struct wpa_supplicant *
   1217 wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
   1218 {
   1219 	struct wpa_interface iface;
   1220 	struct wpa_supplicant *group_wpa_s;
   1221 
   1222 	if (!wpa_s->pending_interface_name[0]) {
   1223 		wpa_printf(MSG_ERROR, "P2P: No pending group interface");
   1224 		if (!wpas_p2p_create_iface(wpa_s))
   1225 			return NULL;
   1226 		/*
   1227 		 * Something has forced us to remove the pending interface; try
   1228 		 * to create a new one and hope for the best that we will get
   1229 		 * the same local address.
   1230 		 */
   1231 		if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
   1232 						 WPA_IF_P2P_CLIENT) < 0)
   1233 			return NULL;
   1234 	}
   1235 
   1236 	os_memset(&iface, 0, sizeof(iface));
   1237 	iface.ifname = wpa_s->pending_interface_name;
   1238 	iface.driver = wpa_s->driver->name;
   1239 	if (wpa_s->conf->ctrl_interface == NULL &&
   1240 	    wpa_s->parent != wpa_s &&
   1241 	    wpa_s->p2p_mgmt &&
   1242 	    (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
   1243 		iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
   1244 	else
   1245 		iface.ctrl_interface = wpa_s->conf->ctrl_interface;
   1246 	iface.driver_param = wpa_s->conf->driver_param;
   1247 	group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
   1248 	if (group_wpa_s == NULL) {
   1249 		wpa_printf(MSG_ERROR, "P2P: Failed to create new "
   1250 			   "wpa_supplicant interface");
   1251 		return NULL;
   1252 	}
   1253 	wpa_s->pending_interface_name[0] = '\0';
   1254 	group_wpa_s->parent = wpa_s;
   1255 	group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
   1256 		P2P_GROUP_INTERFACE_CLIENT;
   1257 	wpa_s->global->p2p_group_formation = group_wpa_s;
   1258 
   1259 	wpas_p2p_clone_config(group_wpa_s, wpa_s);
   1260 
   1261 	return group_wpa_s;
   1262 }
   1263 
   1264 
   1265 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
   1266 					     void *timeout_ctx)
   1267 {
   1268 	struct wpa_supplicant *wpa_s = eloop_ctx;
   1269 	wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
   1270 	wpas_p2p_group_formation_failed(wpa_s);
   1271 }
   1272 
   1273 
   1274 void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
   1275 {
   1276 	eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
   1277 			     wpa_s->parent, NULL);
   1278 	if (wpa_s->global->p2p)
   1279 		p2p_group_formation_failed(wpa_s->global->p2p);
   1280 	else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
   1281 		wpa_drv_p2p_group_formation_failed(wpa_s);
   1282 	wpas_group_formation_completed(wpa_s, 0);
   1283 }
   1284 
   1285 
   1286 void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
   1287 {
   1288 	if (wpa_s->global->p2p_group_formation != wpa_s)
   1289 		return;
   1290 	/* Speed up group formation timeout since this cannot succeed */
   1291 	eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
   1292 			     wpa_s->parent, NULL);
   1293 	eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
   1294 			       wpa_s->parent, NULL);
   1295 }
   1296 
   1297 
   1298 void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
   1299 {
   1300 	struct wpa_supplicant *wpa_s = ctx;
   1301 
   1302 	if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
   1303 		wpa_drv_cancel_remain_on_channel(wpa_s);
   1304 		wpa_s->off_channel_freq = 0;
   1305 		wpa_s->roc_waiting_drv_freq = 0;
   1306 	}
   1307 
   1308 	if (res->status) {
   1309 		wpa_msg_global(wpa_s, MSG_INFO,
   1310 			       P2P_EVENT_GO_NEG_FAILURE "status=%d",
   1311 			       res->status);
   1312 		wpas_notify_p2p_go_neg_completed(wpa_s, res);
   1313 		wpas_p2p_remove_pending_group_interface(wpa_s);
   1314 		return;
   1315 	}
   1316 
   1317 	if (wpa_s->p2p_go_ht40)
   1318 		res->ht40 = 1;
   1319 
   1320 	wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
   1321 		       "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
   1322 		       " wps_method=%s",
   1323 		       res->role_go ? "GO" : "client", res->freq, res->ht40,
   1324 		       MAC2STR(res->peer_device_addr),
   1325 		       MAC2STR(res->peer_interface_addr),
   1326 		       p2p_wps_method_text(res->wps_method));
   1327 	wpas_notify_p2p_go_neg_completed(wpa_s, res);
   1328 
   1329 	if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
   1330 		struct wpa_ssid *ssid;
   1331 		ssid = wpa_config_get_network(wpa_s->conf,
   1332 					      wpa_s->p2p_persistent_id);
   1333 		if (ssid && ssid->disabled == 2 &&
   1334 		    ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
   1335 			size_t len = os_strlen(ssid->passphrase);
   1336 			wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
   1337 				   "on requested persistent group");
   1338 			os_memcpy(res->passphrase, ssid->passphrase, len);
   1339 			res->passphrase[len] = '\0';
   1340 		}
   1341 	}
   1342 
   1343 	if (wpa_s->create_p2p_iface) {
   1344 		struct wpa_supplicant *group_wpa_s =
   1345 			wpas_p2p_init_group_interface(wpa_s, res->role_go);
   1346 		if (group_wpa_s == NULL) {
   1347 			wpas_p2p_remove_pending_group_interface(wpa_s);
   1348 			return;
   1349 		}
   1350 		if (group_wpa_s != wpa_s) {
   1351 			os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
   1352 				  sizeof(group_wpa_s->p2p_pin));
   1353 			group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
   1354 		}
   1355 		os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
   1356 		wpa_s->pending_interface_name[0] = '\0';
   1357 		group_wpa_s->p2p_in_provisioning = 1;
   1358 
   1359 		if (res->role_go)
   1360 			wpas_start_wps_go(group_wpa_s, res, 1);
   1361 		else
   1362 			wpas_start_wps_enrollee(group_wpa_s, res);
   1363 	} else {
   1364 		wpa_s->p2p_in_provisioning = 1;
   1365 		wpa_s->global->p2p_group_formation = wpa_s;
   1366 
   1367 		if (res->role_go)
   1368 			wpas_start_wps_go(wpa_s, res, 1);
   1369 		else
   1370 			wpas_start_wps_enrollee(ctx, res);
   1371 	}
   1372 
   1373 	wpa_s->p2p_long_listen = 0;
   1374 	eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
   1375 
   1376 	eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
   1377 	eloop_register_timeout(15 + res->peer_config_timeout / 100,
   1378 			       (res->peer_config_timeout % 100) * 10000,
   1379 			       wpas_p2p_group_formation_timeout, wpa_s, NULL);
   1380 }
   1381 
   1382 
   1383 void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
   1384 {
   1385 	struct wpa_supplicant *wpa_s = ctx;
   1386 	wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
   1387 		       " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
   1388 
   1389 	wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
   1390 }
   1391 
   1392 
   1393 void wpas_dev_found(void *ctx, const u8 *addr,
   1394 		    const struct p2p_peer_info *info,
   1395 		    int new_device)
   1396 {
   1397 #ifndef CONFIG_NO_STDOUT_DEBUG
   1398 	struct wpa_supplicant *wpa_s = ctx;
   1399 	char devtype[WPS_DEV_TYPE_BUFSIZE];
   1400 #define WFD_DEV_INFO_SIZE 9
   1401 	char wfd_dev_info_hex[2 * WFD_DEV_INFO_SIZE + 1];
   1402 	os_memset(wfd_dev_info_hex, 0, sizeof(wfd_dev_info_hex));
   1403 #ifdef CONFIG_WIFI_DISPLAY
   1404 	if (info->wfd_subelems) {
   1405 		wpa_snprintf_hex(wfd_dev_info_hex, sizeof(wfd_dev_info_hex),
   1406 					wpabuf_head(info->wfd_subelems),
   1407 					WFD_DEV_INFO_SIZE);
   1408 	}
   1409 #endif /* CONFIG_WIFI_DISPLAY */
   1410 	wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
   1411 		       " p2p_dev_addr=" MACSTR
   1412 		       " pri_dev_type=%s name='%s' config_methods=0x%x "
   1413 		       "dev_capab=0x%x group_capab=0x%x%s%s",
   1414 		       MAC2STR(addr), MAC2STR(info->p2p_device_addr),
   1415 		       wps_dev_type_bin2str(info->pri_dev_type, devtype,
   1416 					    sizeof(devtype)),
   1417 		       info->device_name, info->config_methods,
   1418 		       info->dev_capab, info->group_capab,
   1419 		       wfd_dev_info_hex[0] ? " wfd_dev_info=0x" : "", wfd_dev_info_hex);
   1420 #endif /* CONFIG_NO_STDOUT_DEBUG */
   1421 
   1422 	wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
   1423 }
   1424 
   1425 
   1426 static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
   1427 {
   1428 	struct wpa_supplicant *wpa_s = ctx;
   1429 
   1430 	wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
   1431 		       "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
   1432 
   1433 	wpas_notify_p2p_device_lost(wpa_s, dev_addr);
   1434 }
   1435 
   1436 
   1437 static void wpas_find_stopped(void *ctx)
   1438 {
   1439 	struct wpa_supplicant *wpa_s = ctx;
   1440 	wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
   1441 }
   1442 
   1443 
   1444 static int wpas_start_listen(void *ctx, unsigned int freq,
   1445 			     unsigned int duration,
   1446 			     const struct wpabuf *probe_resp_ie)
   1447 {
   1448 	struct wpa_supplicant *wpa_s = ctx;
   1449 
   1450 	wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
   1451 
   1452 	if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
   1453 		wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
   1454 			   "report received Probe Request frames");
   1455 		return -1;
   1456 	}
   1457 
   1458 	wpa_s->pending_listen_freq = freq;
   1459 	wpa_s->pending_listen_duration = duration;
   1460 
   1461 	if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
   1462 		wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
   1463 			   "to remain on channel (%u MHz) for Listen "
   1464 			   "state", freq);
   1465 		wpa_s->pending_listen_freq = 0;
   1466 		return -1;
   1467 	}
   1468 	wpa_s->off_channel_freq = 0;
   1469 	wpa_s->roc_waiting_drv_freq = freq;
   1470 
   1471 	return 0;
   1472 }
   1473 
   1474 
   1475 static void wpas_stop_listen(void *ctx)
   1476 {
   1477 	struct wpa_supplicant *wpa_s = ctx;
   1478 	if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
   1479 		wpa_drv_cancel_remain_on_channel(wpa_s);
   1480 		wpa_s->off_channel_freq = 0;
   1481 		wpa_s->roc_waiting_drv_freq = 0;
   1482 	}
   1483 	wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
   1484 	wpa_drv_probe_req_report(wpa_s, 0);
   1485 }
   1486 
   1487 
   1488 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
   1489 {
   1490 	struct wpa_supplicant *wpa_s = ctx;
   1491 	return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
   1492 }
   1493 
   1494 
   1495 /*
   1496  * DNS Header section is used only to calculate compression pointers, so the
   1497  * contents of this data does not matter, but the length needs to be reserved
   1498  * in the virtual packet.
   1499  */
   1500 #define DNS_HEADER_LEN 12
   1501 
   1502 /*
   1503  * 27-octet in-memory packet from P2P specification containing two implied
   1504  * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
   1505  */
   1506 #define P2P_SD_IN_MEMORY_LEN 27
   1507 
   1508 static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
   1509 				       u8 **spos, const u8 *end)
   1510 {
   1511 	while (*spos < end) {
   1512 		u8 val = ((*spos)[0] & 0xc0) >> 6;
   1513 		int len;
   1514 
   1515 		if (val == 1 || val == 2) {
   1516 			/* These are reserved values in RFC 1035 */
   1517 			wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
   1518 				   "sequence starting with 0x%x", val);
   1519 			return -1;
   1520 		}
   1521 
   1522 		if (val == 3) {
   1523 			u16 offset;
   1524 			u8 *spos_tmp;
   1525 
   1526 			/* Offset */
   1527 			if (*spos + 2 > end) {
   1528 				wpa_printf(MSG_DEBUG, "P2P: No room for full "
   1529 					   "DNS offset field");
   1530 				return -1;
   1531 			}
   1532 
   1533 			offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
   1534 			if (offset >= *spos - start) {
   1535 				wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
   1536 					   "pointer offset %u", offset);
   1537 				return -1;
   1538 			}
   1539 
   1540 			(*spos) += 2;
   1541 			spos_tmp = start + offset;
   1542 			return p2p_sd_dns_uncompress_label(upos, uend, start,
   1543 							   &spos_tmp,
   1544 							   *spos - 2);
   1545 		}
   1546 
   1547 		/* Label */
   1548 		len = (*spos)[0] & 0x3f;
   1549 		if (len == 0)
   1550 			return 0;
   1551 
   1552 		(*spos)++;
   1553 		if (*spos + len > end) {
   1554 			wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
   1555 				   "sequence - no room for label with length "
   1556 				   "%u", len);
   1557 			return -1;
   1558 		}
   1559 
   1560 		if (*upos + len + 2 > uend)
   1561 			return -2;
   1562 
   1563 		os_memcpy(*upos, *spos, len);
   1564 		*spos += len;
   1565 		*upos += len;
   1566 		(*upos)[0] = '.';
   1567 		(*upos)++;
   1568 		(*upos)[0] = '\0';
   1569 	}
   1570 
   1571 	return 0;
   1572 }
   1573 
   1574 
   1575 /* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
   1576  * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
   1577  * not large enough */
   1578 static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
   1579 				 size_t msg_len, size_t offset)
   1580 {
   1581 	/* 27-octet in-memory packet from P2P specification */
   1582 	const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
   1583 		"\x04_udp\xC0\x11\x00\x0C\x00\x01";
   1584 	u8 *tmp, *end, *spos;
   1585 	char *upos, *uend;
   1586 	int ret = 0;
   1587 
   1588 	if (buf_len < 2)
   1589 		return -1;
   1590 	if (offset > msg_len)
   1591 		return -1;
   1592 
   1593 	tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
   1594 	if (tmp == NULL)
   1595 		return -1;
   1596 	spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
   1597 	end = spos + msg_len;
   1598 	spos += offset;
   1599 
   1600 	os_memset(tmp, 0, DNS_HEADER_LEN);
   1601 	os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
   1602 	os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
   1603 
   1604 	upos = buf;
   1605 	uend = buf + buf_len;
   1606 
   1607 	ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
   1608 	if (ret) {
   1609 		os_free(tmp);
   1610 		return ret;
   1611 	}
   1612 
   1613 	if (upos == buf) {
   1614 		upos[0] = '.';
   1615 		upos[1] = '\0';
   1616 	} else if (upos[-1] == '.')
   1617 		upos[-1] = '\0';
   1618 
   1619 	os_free(tmp);
   1620 	return 0;
   1621 }
   1622 
   1623 
   1624 static struct p2p_srv_bonjour *
   1625 wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
   1626 			     const struct wpabuf *query)
   1627 {
   1628 	struct p2p_srv_bonjour *bsrv;
   1629 	size_t len;
   1630 
   1631 	len = wpabuf_len(query);
   1632 	dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
   1633 			 struct p2p_srv_bonjour, list) {
   1634 		if (len == wpabuf_len(bsrv->query) &&
   1635 		    os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
   1636 			      len) == 0)
   1637 			return bsrv;
   1638 	}
   1639 	return NULL;
   1640 }
   1641 
   1642 
   1643 static struct p2p_srv_upnp *
   1644 wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
   1645 			  const char *service)
   1646 {
   1647 	struct p2p_srv_upnp *usrv;
   1648 
   1649 	dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
   1650 			 struct p2p_srv_upnp, list) {
   1651 		if (version == usrv->version &&
   1652 		    os_strcmp(service, usrv->service) == 0)
   1653 			return usrv;
   1654 	}
   1655 	return NULL;
   1656 }
   1657 
   1658 
   1659 static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
   1660 					u8 srv_trans_id)
   1661 {
   1662 	u8 *len_pos;
   1663 
   1664 	if (wpabuf_tailroom(resp) < 5)
   1665 		return;
   1666 
   1667 	/* Length (to be filled) */
   1668 	len_pos = wpabuf_put(resp, 2);
   1669 	wpabuf_put_u8(resp, srv_proto);
   1670 	wpabuf_put_u8(resp, srv_trans_id);
   1671 	/* Status Code */
   1672 	wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
   1673 	/* Response Data: empty */
   1674 	WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
   1675 }
   1676 
   1677 
   1678 static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
   1679 				struct wpabuf *resp, u8 srv_trans_id)
   1680 {
   1681 	struct p2p_srv_bonjour *bsrv;
   1682 	u8 *len_pos;
   1683 
   1684 	wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
   1685 
   1686 	if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
   1687 		wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
   1688 		return;
   1689 	}
   1690 
   1691 	dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
   1692 			 struct p2p_srv_bonjour, list) {
   1693 		if (wpabuf_tailroom(resp) <
   1694 		    5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
   1695 			return;
   1696 		/* Length (to be filled) */
   1697 		len_pos = wpabuf_put(resp, 2);
   1698 		wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
   1699 		wpabuf_put_u8(resp, srv_trans_id);
   1700 		/* Status Code */
   1701 		wpabuf_put_u8(resp, P2P_SD_SUCCESS);
   1702 		wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
   1703 				  wpabuf_head(bsrv->resp),
   1704 				  wpabuf_len(bsrv->resp));
   1705 		/* Response Data */
   1706 		wpabuf_put_buf(resp, bsrv->query); /* Key */
   1707 		wpabuf_put_buf(resp, bsrv->resp); /* Value */
   1708 		WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
   1709 			     2);
   1710 	}
   1711 }
   1712 
   1713 
   1714 static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
   1715 			       size_t query_len)
   1716 {
   1717 	char str_rx[256], str_srv[256];
   1718 
   1719 	if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
   1720 		return 0; /* Too short to include DNS Type and Version */
   1721 	if (os_memcmp(query + query_len - 3,
   1722 		      wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
   1723 		      3) != 0)
   1724 		return 0; /* Mismatch in DNS Type or Version */
   1725 	if (query_len == wpabuf_len(bsrv->query) &&
   1726 	    os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
   1727 		return 1; /* Binary match */
   1728 
   1729 	if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
   1730 				  0))
   1731 		return 0; /* Failed to uncompress query */
   1732 	if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
   1733 				  wpabuf_head(bsrv->query),
   1734 				  wpabuf_len(bsrv->query) - 3, 0))
   1735 		return 0; /* Failed to uncompress service */
   1736 
   1737 	return os_strcmp(str_rx, str_srv) == 0;
   1738 }
   1739 
   1740 
   1741 static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
   1742 				struct wpabuf *resp, u8 srv_trans_id,
   1743 				const u8 *query, size_t query_len)
   1744 {
   1745 	struct p2p_srv_bonjour *bsrv;
   1746 	u8 *len_pos;
   1747 	int matches = 0;
   1748 
   1749 	wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
   1750 			  query, query_len);
   1751 	if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
   1752 		wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
   1753 		wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
   1754 					    srv_trans_id);
   1755 		return;
   1756 	}
   1757 
   1758 	if (query_len == 0) {
   1759 		wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
   1760 		return;
   1761 	}
   1762 
   1763 	dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
   1764 			 struct p2p_srv_bonjour, list) {
   1765 		if (!match_bonjour_query(bsrv, query, query_len))
   1766 			continue;
   1767 
   1768 		if (wpabuf_tailroom(resp) <
   1769 		    5 + query_len + wpabuf_len(bsrv->resp))
   1770 			return;
   1771 
   1772 		matches++;
   1773 
   1774 		/* Length (to be filled) */
   1775 		len_pos = wpabuf_put(resp, 2);
   1776 		wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
   1777 		wpabuf_put_u8(resp, srv_trans_id);
   1778 
   1779 		/* Status Code */
   1780 		wpabuf_put_u8(resp, P2P_SD_SUCCESS);
   1781 		wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
   1782 				  wpabuf_head(bsrv->resp),
   1783 				  wpabuf_len(bsrv->resp));
   1784 
   1785 		/* Response Data */
   1786 		wpabuf_put_data(resp, query, query_len); /* Key */
   1787 		wpabuf_put_buf(resp, bsrv->resp); /* Value */
   1788 
   1789 		WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
   1790 	}
   1791 
   1792 	if (matches == 0) {
   1793 		wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
   1794 			   "available");
   1795 		if (wpabuf_tailroom(resp) < 5)
   1796 			return;
   1797 
   1798 		/* Length (to be filled) */
   1799 		len_pos = wpabuf_put(resp, 2);
   1800 		wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
   1801 		wpabuf_put_u8(resp, srv_trans_id);
   1802 
   1803 		/* Status Code */
   1804 		wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
   1805 		/* Response Data: empty */
   1806 		WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
   1807 			     2);
   1808 	}
   1809 }
   1810 
   1811 
   1812 static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
   1813 			     struct wpabuf *resp, u8 srv_trans_id)
   1814 {
   1815 	struct p2p_srv_upnp *usrv;
   1816 	u8 *len_pos;
   1817 
   1818 	wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
   1819 
   1820 	if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
   1821 		wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
   1822 		return;
   1823 	}
   1824 
   1825 	dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
   1826 			 struct p2p_srv_upnp, list) {
   1827 		if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
   1828 			return;
   1829 
   1830 		/* Length (to be filled) */
   1831 		len_pos = wpabuf_put(resp, 2);
   1832 		wpabuf_put_u8(resp, P2P_SERV_UPNP);
   1833 		wpabuf_put_u8(resp, srv_trans_id);
   1834 
   1835 		/* Status Code */
   1836 		wpabuf_put_u8(resp, P2P_SD_SUCCESS);
   1837 		/* Response Data */
   1838 		wpabuf_put_u8(resp, usrv->version);
   1839 		wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
   1840 			   usrv->service);
   1841 		wpabuf_put_str(resp, usrv->service);
   1842 		WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
   1843 			     2);
   1844 	}
   1845 }
   1846 
   1847 
   1848 static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
   1849 			     struct wpabuf *resp, u8 srv_trans_id,
   1850 			     const u8 *query, size_t query_len)
   1851 {
   1852 	struct p2p_srv_upnp *usrv;
   1853 	u8 *len_pos;
   1854 	u8 version;
   1855 	char *str;
   1856 	int count = 0;
   1857 
   1858 	wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
   1859 			  query, query_len);
   1860 
   1861 	if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
   1862 		wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
   1863 		wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
   1864 					    srv_trans_id);
   1865 		return;
   1866 	}
   1867 
   1868 	if (query_len == 0) {
   1869 		wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
   1870 		return;
   1871 	}
   1872 
   1873 	if (wpabuf_tailroom(resp) < 5)
   1874 		return;
   1875 
   1876 	/* Length (to be filled) */
   1877 	len_pos = wpabuf_put(resp, 2);
   1878 	wpabuf_put_u8(resp, P2P_SERV_UPNP);
   1879 	wpabuf_put_u8(resp, srv_trans_id);
   1880 
   1881 	version = query[0];
   1882 	str = os_malloc(query_len);
   1883 	if (str == NULL)
   1884 		return;
   1885 	os_memcpy(str, query + 1, query_len - 1);
   1886 	str[query_len - 1] = '\0';
   1887 
   1888 	dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
   1889 			 struct p2p_srv_upnp, list) {
   1890 		if (version != usrv->version)
   1891 			continue;
   1892 
   1893 		if (os_strcmp(str, "ssdp:all") != 0 &&
   1894 		    os_strstr(usrv->service, str) == NULL)
   1895 			continue;
   1896 
   1897 		if (wpabuf_tailroom(resp) < 2)
   1898 			break;
   1899 		if (count == 0) {
   1900 			/* Status Code */
   1901 			wpabuf_put_u8(resp, P2P_SD_SUCCESS);
   1902 			/* Response Data */
   1903 			wpabuf_put_u8(resp, version);
   1904 		} else
   1905 			wpabuf_put_u8(resp, ',');
   1906 
   1907 		count++;
   1908 
   1909 		wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
   1910 			   usrv->service);
   1911 		if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
   1912 			break;
   1913 		wpabuf_put_str(resp, usrv->service);
   1914 	}
   1915 	os_free(str);
   1916 
   1917 	if (count == 0) {
   1918 		wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
   1919 			   "available");
   1920 		/* Status Code */
   1921 		wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
   1922 		/* Response Data: empty */
   1923 	}
   1924 
   1925 	WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
   1926 }
   1927 
   1928 
   1929 #ifdef CONFIG_WIFI_DISPLAY
   1930 static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
   1931 			    struct wpabuf *resp, u8 srv_trans_id,
   1932 			    const u8 *query, size_t query_len)
   1933 {
   1934 	const u8 *pos;
   1935 	u8 role;
   1936 	u8 *len_pos;
   1937 
   1938 	wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
   1939 
   1940 	if (!wpa_s->global->wifi_display) {
   1941 		wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
   1942 		wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
   1943 					    srv_trans_id);
   1944 		return;
   1945 	}
   1946 
   1947 	if (query_len < 1) {
   1948 		wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
   1949 			   "Role");
   1950 		return;
   1951 	}
   1952 
   1953 	if (wpabuf_tailroom(resp) < 5)
   1954 		return;
   1955 
   1956 	pos = query;
   1957 	role = *pos++;
   1958 	wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
   1959 
   1960 	/* TODO: role specific handling */
   1961 
   1962 	/* Length (to be filled) */
   1963 	len_pos = wpabuf_put(resp, 2);
   1964 	wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
   1965 	wpabuf_put_u8(resp, srv_trans_id);
   1966 	wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
   1967 
   1968 	while (pos < query + query_len) {
   1969 		if (*pos < MAX_WFD_SUBELEMS &&
   1970 		    wpa_s->global->wfd_subelem[*pos] &&
   1971 		    wpabuf_tailroom(resp) >=
   1972 		    wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
   1973 			wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
   1974 				   "subelement %u", *pos);
   1975 			wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
   1976 		}
   1977 		pos++;
   1978 	}
   1979 
   1980 	WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
   1981 }
   1982 #endif /* CONFIG_WIFI_DISPLAY */
   1983 
   1984 
   1985 void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
   1986 		     u16 update_indic, const u8 *tlvs, size_t tlvs_len)
   1987 {
   1988 	struct wpa_supplicant *wpa_s = ctx;
   1989 	const u8 *pos = tlvs;
   1990 	const u8 *end = tlvs + tlvs_len;
   1991 	const u8 *tlv_end;
   1992 	u16 slen;
   1993 	struct wpabuf *resp;
   1994 	u8 srv_proto, srv_trans_id;
   1995 	size_t buf_len;
   1996 	char *buf;
   1997 
   1998 	wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
   1999 		    tlvs, tlvs_len);
   2000 	buf_len = 2 * tlvs_len + 1;
   2001 	buf = os_malloc(buf_len);
   2002 	if (buf) {
   2003 		wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
   2004 		wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
   2005 			     MACSTR " %u %u %s",
   2006 			     freq, MAC2STR(sa), dialog_token, update_indic,
   2007 			     buf);
   2008 		os_free(buf);
   2009 	}
   2010 
   2011 	if (wpa_s->p2p_sd_over_ctrl_iface) {
   2012 		wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
   2013 					   update_indic, tlvs, tlvs_len);
   2014 		return; /* to be processed by an external program */
   2015 	}
   2016 
   2017 	resp = wpabuf_alloc(10000);
   2018 	if (resp == NULL)
   2019 		return;
   2020 
   2021 	while (pos + 1 < end) {
   2022 		wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
   2023 		slen = WPA_GET_LE16(pos);
   2024 		pos += 2;
   2025 		if (pos + slen > end || slen < 2) {
   2026 			wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
   2027 				   "length");
   2028 			wpabuf_free(resp);
   2029 			return;
   2030 		}
   2031 		tlv_end = pos + slen;
   2032 
   2033 		srv_proto = *pos++;
   2034 		wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
   2035 			   srv_proto);
   2036 		srv_trans_id = *pos++;
   2037 		wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
   2038 			   srv_trans_id);
   2039 
   2040 		wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
   2041 			    pos, tlv_end - pos);
   2042 
   2043 
   2044 		if (wpa_s->force_long_sd) {
   2045 			wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
   2046 				   "response");
   2047 			wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
   2048 			wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
   2049 			goto done;
   2050 		}
   2051 
   2052 		switch (srv_proto) {
   2053 		case P2P_SERV_ALL_SERVICES:
   2054 			wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
   2055 				   "for all services");
   2056 			if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
   2057 			    dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
   2058 				wpa_printf(MSG_DEBUG, "P2P: No service "
   2059 					   "discovery protocols available");
   2060 				wpas_sd_add_proto_not_avail(
   2061 					resp, P2P_SERV_ALL_SERVICES,
   2062 					srv_trans_id);
   2063 				break;
   2064 			}
   2065 			wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
   2066 			wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
   2067 			break;
   2068 		case P2P_SERV_BONJOUR:
   2069 			wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
   2070 					    pos, tlv_end - pos);
   2071 			break;
   2072 		case P2P_SERV_UPNP:
   2073 			wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
   2074 					 pos, tlv_end - pos);
   2075 			break;
   2076 #ifdef CONFIG_WIFI_DISPLAY
   2077 		case P2P_SERV_WIFI_DISPLAY:
   2078 			wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
   2079 					pos, tlv_end - pos);
   2080 			break;
   2081 #endif /* CONFIG_WIFI_DISPLAY */
   2082 		default:
   2083 			wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
   2084 				   "protocol %u", srv_proto);
   2085 			wpas_sd_add_proto_not_avail(resp, srv_proto,
   2086 						    srv_trans_id);
   2087 			break;
   2088 		}
   2089 
   2090 		pos = tlv_end;
   2091 	}
   2092 
   2093 done:
   2094 	wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
   2095 				   update_indic, tlvs, tlvs_len);
   2096 
   2097 	wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
   2098 
   2099 	wpabuf_free(resp);
   2100 }
   2101 
   2102 
   2103 void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
   2104 		      const u8 *tlvs, size_t tlvs_len)
   2105 {
   2106 	struct wpa_supplicant *wpa_s = ctx;
   2107 	const u8 *pos = tlvs;
   2108 	const u8 *end = tlvs + tlvs_len;
   2109 	const u8 *tlv_end;
   2110 	u16 slen;
   2111 	size_t buf_len;
   2112 	char *buf;
   2113 
   2114 	wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
   2115 		    tlvs, tlvs_len);
   2116 	if (tlvs_len > 1500) {
   2117 		/* TODO: better way for handling this */
   2118 		wpa_msg_ctrl(wpa_s, MSG_INFO,
   2119 			     P2P_EVENT_SERV_DISC_RESP MACSTR
   2120 			     " %u <long response: %u bytes>",
   2121 			     MAC2STR(sa), update_indic,
   2122 			     (unsigned int) tlvs_len);
   2123 	} else {
   2124 		buf_len = 2 * tlvs_len + 1;
   2125 		buf = os_malloc(buf_len);
   2126 		if (buf) {
   2127 			wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
   2128 			wpa_msg_ctrl(wpa_s, MSG_INFO,
   2129 				     P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
   2130 				     MAC2STR(sa), update_indic, buf);
   2131 			os_free(buf);
   2132 		}
   2133 	}
   2134 
   2135 	while (pos < end) {
   2136 		u8 srv_proto, srv_trans_id, status;
   2137 
   2138 		wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
   2139 		slen = WPA_GET_LE16(pos);
   2140 		pos += 2;
   2141 		if (pos + slen > end || slen < 3) {
   2142 			wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
   2143 				   "length");
   2144 			return;
   2145 		}
   2146 		tlv_end = pos + slen;
   2147 
   2148 		srv_proto = *pos++;
   2149 		wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
   2150 			   srv_proto);
   2151 		srv_trans_id = *pos++;
   2152 		wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
   2153 			   srv_trans_id);
   2154 		status = *pos++;
   2155 		wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
   2156 			   status);
   2157 
   2158 		wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
   2159 			    pos, tlv_end - pos);
   2160 
   2161 		pos = tlv_end;
   2162 	}
   2163 
   2164 	wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
   2165 }
   2166 
   2167 
   2168 u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
   2169 			const struct wpabuf *tlvs)
   2170 {
   2171 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
   2172 		return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
   2173 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
   2174 		return 0;
   2175 	return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
   2176 }
   2177 
   2178 
   2179 u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
   2180 			     u8 version, const char *query)
   2181 {
   2182 	struct wpabuf *tlvs;
   2183 	u64 ret;
   2184 
   2185 	tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
   2186 	if (tlvs == NULL)
   2187 		return 0;
   2188 	wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
   2189 	wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
   2190 	wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
   2191 	wpabuf_put_u8(tlvs, version);
   2192 	wpabuf_put_str(tlvs, query);
   2193 	ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
   2194 	wpabuf_free(tlvs);
   2195 	return ret;
   2196 }
   2197 
   2198 
   2199 #ifdef CONFIG_WIFI_DISPLAY
   2200 
   2201 static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
   2202 				   const struct wpabuf *tlvs)
   2203 {
   2204 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
   2205 		return 0;
   2206 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
   2207 		return 0;
   2208 	return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
   2209 }
   2210 
   2211 
   2212 #define MAX_WFD_SD_SUBELEMS 20
   2213 
   2214 static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
   2215 				const char *subelems)
   2216 {
   2217 	u8 *len;
   2218 	const char *pos;
   2219 	int val;
   2220 	int count = 0;
   2221 
   2222 	len = wpabuf_put(tlvs, 2);
   2223 	wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
   2224 	wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
   2225 
   2226 	wpabuf_put_u8(tlvs, role);
   2227 
   2228 	pos = subelems;
   2229 	while (*pos) {
   2230 		val = atoi(pos);
   2231 		if (val >= 0 && val < 256) {
   2232 			wpabuf_put_u8(tlvs, val);
   2233 			count++;
   2234 			if (count == MAX_WFD_SD_SUBELEMS)
   2235 				break;
   2236 		}
   2237 		pos = os_strchr(pos + 1, ',');
   2238 		if (pos == NULL)
   2239 			break;
   2240 		pos++;
   2241 	}
   2242 
   2243 	WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
   2244 }
   2245 
   2246 
   2247 u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
   2248 				     const u8 *dst, const char *role)
   2249 {
   2250 	struct wpabuf *tlvs;
   2251 	u64 ret;
   2252 	const char *subelems;
   2253 	u8 id = 1;
   2254 
   2255 	subelems = os_strchr(role, ' ');
   2256 	if (subelems == NULL)
   2257 		return 0;
   2258 	subelems++;
   2259 
   2260 	tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
   2261 	if (tlvs == NULL)
   2262 		return 0;
   2263 
   2264 	if (os_strstr(role, "[source]"))
   2265 		wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
   2266 	if (os_strstr(role, "[pri-sink]"))
   2267 		wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
   2268 	if (os_strstr(role, "[sec-sink]"))
   2269 		wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
   2270 	if (os_strstr(role, "[source+sink]"))
   2271 		wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
   2272 
   2273 	ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
   2274 	wpabuf_free(tlvs);
   2275 	return ret;
   2276 }
   2277 
   2278 #endif /* CONFIG_WIFI_DISPLAY */
   2279 
   2280 
   2281 int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
   2282 {
   2283 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
   2284 		return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
   2285 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
   2286 		return -1;
   2287 	return p2p_sd_cancel_request(wpa_s->global->p2p,
   2288 				     (void *) (uintptr_t) req);
   2289 }
   2290 
   2291 
   2292 void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
   2293 			  const u8 *dst, u8 dialog_token,
   2294 			  const struct wpabuf *resp_tlvs)
   2295 {
   2296 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
   2297 		wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
   2298 					resp_tlvs);
   2299 		return;
   2300 	}
   2301 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
   2302 		return;
   2303 	p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
   2304 			resp_tlvs);
   2305 }
   2306 
   2307 
   2308 void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
   2309 {
   2310 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
   2311 		wpa_drv_p2p_service_update(wpa_s);
   2312 		return;
   2313 	}
   2314 	if (wpa_s->global->p2p)
   2315 		p2p_sd_service_update(wpa_s->global->p2p);
   2316 }
   2317 
   2318 
   2319 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
   2320 {
   2321 	dl_list_del(&bsrv->list);
   2322 	wpabuf_free(bsrv->query);
   2323 	wpabuf_free(bsrv->resp);
   2324 	os_free(bsrv);
   2325 }
   2326 
   2327 
   2328 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
   2329 {
   2330 	dl_list_del(&usrv->list);
   2331 	os_free(usrv->service);
   2332 	os_free(usrv);
   2333 }
   2334 
   2335 
   2336 void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
   2337 {
   2338 	struct p2p_srv_bonjour *bsrv, *bn;
   2339 	struct p2p_srv_upnp *usrv, *un;
   2340 
   2341 	dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
   2342 			      struct p2p_srv_bonjour, list)
   2343 		wpas_p2p_srv_bonjour_free(bsrv);
   2344 
   2345 	dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
   2346 			      struct p2p_srv_upnp, list)
   2347 		wpas_p2p_srv_upnp_free(usrv);
   2348 
   2349 	wpas_p2p_sd_service_update(wpa_s);
   2350 }
   2351 
   2352 
   2353 int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
   2354 				 struct wpabuf *query, struct wpabuf *resp)
   2355 {
   2356 	struct p2p_srv_bonjour *bsrv;
   2357 
   2358 	bsrv = os_zalloc(sizeof(*bsrv));
   2359 	if (bsrv == NULL)
   2360 		return -1;
   2361 	bsrv->query = query;
   2362 	bsrv->resp = resp;
   2363 	dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
   2364 
   2365 	wpas_p2p_sd_service_update(wpa_s);
   2366 	return 0;
   2367 }
   2368 
   2369 
   2370 int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
   2371 				 const struct wpabuf *query)
   2372 {
   2373 	struct p2p_srv_bonjour *bsrv;
   2374 
   2375 	bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
   2376 	if (bsrv == NULL)
   2377 		return -1;
   2378 	wpas_p2p_srv_bonjour_free(bsrv);
   2379 	wpas_p2p_sd_service_update(wpa_s);
   2380 	return 0;
   2381 }
   2382 
   2383 
   2384 int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
   2385 			      const char *service)
   2386 {
   2387 	struct p2p_srv_upnp *usrv;
   2388 
   2389 	if (wpas_p2p_service_get_upnp(wpa_s, version, service))
   2390 		return 0; /* Already listed */
   2391 	usrv = os_zalloc(sizeof(*usrv));
   2392 	if (usrv == NULL)
   2393 		return -1;
   2394 	usrv->version = version;
   2395 	usrv->service = os_strdup(service);
   2396 	if (usrv->service == NULL) {
   2397 		os_free(usrv);
   2398 		return -1;
   2399 	}
   2400 	dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
   2401 
   2402 	wpas_p2p_sd_service_update(wpa_s);
   2403 	return 0;
   2404 }
   2405 
   2406 
   2407 int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
   2408 			      const char *service)
   2409 {
   2410 	struct p2p_srv_upnp *usrv;
   2411 
   2412 	usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
   2413 	if (usrv == NULL)
   2414 		return -1;
   2415 	wpas_p2p_srv_upnp_free(usrv);
   2416 	wpas_p2p_sd_service_update(wpa_s);
   2417 	return 0;
   2418 }
   2419 
   2420 
   2421 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
   2422 					 const u8 *peer, const char *params,
   2423 					 unsigned int generated_pin)
   2424 {
   2425 	wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
   2426 		       " %08d%s", MAC2STR(peer), generated_pin, params);
   2427 }
   2428 
   2429 
   2430 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
   2431 					const u8 *peer, const char *params)
   2432 {
   2433 	wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
   2434 		       "%s", MAC2STR(peer), params);
   2435 }
   2436 
   2437 
   2438 void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
   2439 			const u8 *dev_addr, const u8 *pri_dev_type,
   2440 			const char *dev_name, u16 supp_config_methods,
   2441 			u8 dev_capab, u8 group_capab, const u8 *group_id,
   2442 			size_t group_id_len)
   2443 {
   2444 	struct wpa_supplicant *wpa_s = ctx;
   2445 	char devtype[WPS_DEV_TYPE_BUFSIZE];
   2446 	char params[300];
   2447 	u8 empty_dev_type[8];
   2448 	unsigned int generated_pin = 0;
   2449 	struct wpa_supplicant *group = NULL;
   2450 
   2451 	if (group_id) {
   2452 		for (group = wpa_s->global->ifaces; group; group = group->next)
   2453 		{
   2454 			struct wpa_ssid *s = group->current_ssid;
   2455 			if (s != NULL &&
   2456 			    s->mode == WPAS_MODE_P2P_GO &&
   2457 			    group_id_len - ETH_ALEN == s->ssid_len &&
   2458 			    os_memcmp(group_id + ETH_ALEN, s->ssid,
   2459 				      s->ssid_len) == 0)
   2460 				break;
   2461 		}
   2462 	}
   2463 
   2464 	if (pri_dev_type == NULL) {
   2465 		os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
   2466 		pri_dev_type = empty_dev_type;
   2467 	}
   2468 	os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
   2469 		    " pri_dev_type=%s name='%s' config_methods=0x%x "
   2470 		    "dev_capab=0x%x group_capab=0x%x%s%s",
   2471 		    MAC2STR(dev_addr),
   2472 		    wps_dev_type_bin2str(pri_dev_type, devtype,
   2473 					 sizeof(devtype)),
   2474 		    dev_name, supp_config_methods, dev_capab, group_capab,
   2475 		    group ? " group=" : "",
   2476 		    group ? group->ifname : "");
   2477 	params[sizeof(params) - 1] = '\0';
   2478 
   2479 	if (config_methods & WPS_CONFIG_DISPLAY) {
   2480 		generated_pin = wps_generate_pin();
   2481 		wpas_prov_disc_local_display(wpa_s, peer, params,
   2482 					     generated_pin);
   2483 	} else if (config_methods & WPS_CONFIG_KEYPAD)
   2484 		wpas_prov_disc_local_keypad(wpa_s, peer, params);
   2485 	else if (config_methods & WPS_CONFIG_PUSHBUTTON)
   2486 		wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
   2487 			       MACSTR "%s", MAC2STR(peer), params);
   2488 
   2489 	wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
   2490 					    P2P_PROV_DISC_SUCCESS,
   2491 					    config_methods, generated_pin);
   2492 }
   2493 
   2494 
   2495 void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
   2496 {
   2497 	struct wpa_supplicant *wpa_s = ctx;
   2498 	unsigned int generated_pin = 0;
   2499 	char params[20];
   2500 
   2501 	if (wpa_s->pending_pd_before_join &&
   2502 	    (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
   2503 	     os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
   2504 		wpa_s->pending_pd_before_join = 0;
   2505 		wpa_printf(MSG_DEBUG, "P2P: Starting pending "
   2506 			   "join-existing-group operation");
   2507 		wpas_p2p_join_start(wpa_s);
   2508 		return;
   2509 	}
   2510 
   2511 	if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
   2512 	    wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
   2513 		os_snprintf(params, sizeof(params), " peer_go=%d",
   2514 			    wpa_s->pending_pd_use == AUTO_PD_JOIN);
   2515 	else
   2516 		params[0] = '\0';
   2517 
   2518 	if (config_methods & WPS_CONFIG_DISPLAY)
   2519 		wpas_prov_disc_local_keypad(wpa_s, peer, params);
   2520 	else if (config_methods & WPS_CONFIG_KEYPAD) {
   2521 		generated_pin = wps_generate_pin();
   2522 		wpas_prov_disc_local_display(wpa_s, peer, params,
   2523 					     generated_pin);
   2524 	} else if (config_methods & WPS_CONFIG_PUSHBUTTON)
   2525 		wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
   2526 			       MACSTR "%s", MAC2STR(peer), params);
   2527 
   2528 	wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
   2529 					    P2P_PROV_DISC_SUCCESS,
   2530 					    config_methods, generated_pin);
   2531 }
   2532 
   2533 
   2534 static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
   2535 				enum p2p_prov_disc_status status)
   2536 {
   2537 	struct wpa_supplicant *wpa_s = ctx;
   2538 
   2539 	if (wpa_s->p2p_fallback_to_go_neg) {
   2540 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
   2541 			"failed - fall back to GO Negotiation");
   2542 		wpas_p2p_fallback_to_go_neg(wpa_s, 0);
   2543 		return;
   2544 	}
   2545 
   2546 	if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
   2547 		wpa_s->pending_pd_before_join = 0;
   2548 		wpa_printf(MSG_DEBUG, "P2P: Starting pending "
   2549 			   "join-existing-group operation (no ACK for PD "
   2550 			   "Req attempts)");
   2551 		wpas_p2p_join_start(wpa_s);
   2552 		return;
   2553 	}
   2554 
   2555 	wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
   2556 		       " p2p_dev_addr=" MACSTR " status=%d",
   2557 		       MAC2STR(peer), status);
   2558 
   2559 	wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
   2560 					    status, 0, 0);
   2561 }
   2562 
   2563 
   2564 static int freq_included(const struct p2p_channels *channels, unsigned int freq)
   2565 {
   2566 	if (channels == NULL)
   2567 		return 1; /* Assume no restrictions */
   2568 	return p2p_channels_includes_freq(channels, freq);
   2569 
   2570 }
   2571 
   2572 
   2573 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
   2574 				  const u8 *go_dev_addr, const u8 *ssid,
   2575 				  size_t ssid_len, int *go, u8 *group_bssid,
   2576 				  int *force_freq, int persistent_group,
   2577 				  const struct p2p_channels *channels)
   2578 {
   2579 	struct wpa_supplicant *wpa_s = ctx;
   2580 	struct wpa_ssid *s;
   2581 	int res;
   2582 	struct wpa_supplicant *grp;
   2583 
   2584 	if (!persistent_group) {
   2585 		wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
   2586 			   " to join an active group", MAC2STR(sa));
   2587 		if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
   2588 		    (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
   2589 		     == 0 ||
   2590 		     os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
   2591 			wpa_printf(MSG_DEBUG, "P2P: Accept previously "
   2592 				   "authorized invitation");
   2593 			goto accept_inv;
   2594 		}
   2595 		/*
   2596 		 * Do not accept the invitation automatically; notify user and
   2597 		 * request approval.
   2598 		 */
   2599 		return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
   2600 	}
   2601 
   2602 	grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
   2603 	if (grp) {
   2604 		wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
   2605 			   "running persistent group");
   2606 		if (*go)
   2607 			os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
   2608 		goto accept_inv;
   2609 	}
   2610 
   2611 	if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
   2612 	    os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
   2613 		wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
   2614 			   "invitation to re-invoke a persistent group");
   2615 	} else if (!wpa_s->conf->persistent_reconnect)
   2616 		return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
   2617 
   2618 	for (s = wpa_s->conf->ssid; s; s = s->next) {
   2619 		if (s->disabled == 2 &&
   2620 		    os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
   2621 		    s->ssid_len == ssid_len &&
   2622 		    os_memcmp(ssid, s->ssid, ssid_len) == 0)
   2623 			break;
   2624 	}
   2625 
   2626 	if (!s) {
   2627 		wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
   2628 			   " requested reinvocation of an unknown group",
   2629 			   MAC2STR(sa));
   2630 		return P2P_SC_FAIL_UNKNOWN_GROUP;
   2631 	}
   2632 
   2633 	if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
   2634 		*go = 1;
   2635 		if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
   2636 			wpa_printf(MSG_DEBUG, "P2P: The only available "
   2637 				   "interface is already in use - reject "
   2638 				   "invitation");
   2639 			return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
   2640 		}
   2641 		os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
   2642 	} else if (s->mode == WPAS_MODE_P2P_GO) {
   2643 		*go = 1;
   2644 		if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
   2645 		{
   2646 			wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
   2647 				   "interface address for the group");
   2648 			return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
   2649 		}
   2650 		os_memcpy(group_bssid, wpa_s->pending_interface_addr,
   2651 			  ETH_ALEN);
   2652 	}
   2653 
   2654 accept_inv:
   2655 	wpas_p2p_set_own_freq_preference(wpa_s, 0);
   2656 
   2657 	/* Get one of the frequencies currently in use */
   2658 	if (wpas_p2p_valid_oper_freqs(wpa_s, &res, 1) > 0) {
   2659 		wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match a channel already used by one of the interfaces");
   2660 		*force_freq = res;
   2661 		wpas_p2p_set_own_freq_preference(wpa_s, res);
   2662 	}
   2663 
   2664 	if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
   2665 	    wpas_p2p_num_unused_channels(wpa_s) > 0) {
   2666 		if (*go == 0) {
   2667 			/* We are the client */
   2668 			wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
   2669 				   "running a GO but we are capable of MCC, "
   2670 				   "figure out the best channel to use");
   2671 			*force_freq = 0;
   2672 		} else if (!freq_included(channels, *force_freq)) {
   2673 			/* We are the GO, and *force_freq is not in the
   2674 			 * intersection */
   2675 			wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
   2676 				   "in intersection but we are capable of MCC, "
   2677 				   "figure out the best channel to use",
   2678 				   *force_freq);
   2679 			*force_freq = 0;
   2680 		}
   2681 	}
   2682 
   2683 	return P2P_SC_SUCCESS;
   2684 }
   2685 
   2686 
   2687 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
   2688 				     const u8 *ssid, size_t ssid_len,
   2689 				     const u8 *go_dev_addr, u8 status,
   2690 				     int op_freq)
   2691 {
   2692 	struct wpa_supplicant *wpa_s = ctx;
   2693 	struct wpa_ssid *s;
   2694 
   2695 	for (s = wpa_s->conf->ssid; s; s = s->next) {
   2696 		if (s->disabled == 2 &&
   2697 		    s->ssid_len == ssid_len &&
   2698 		    os_memcmp(ssid, s->ssid, ssid_len) == 0)
   2699 			break;
   2700 	}
   2701 
   2702 	if (status == P2P_SC_SUCCESS) {
   2703 		wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
   2704 			   " was accepted; op_freq=%d MHz",
   2705 			   MAC2STR(sa), op_freq);
   2706 		if (s) {
   2707 			int go = s->mode == WPAS_MODE_P2P_GO;
   2708 			wpas_p2p_group_add_persistent(
   2709 				wpa_s, s, go, go ? op_freq : 0, 0, NULL);
   2710 		} else if (bssid) {
   2711 			wpa_s->user_initiated_pd = 0;
   2712 			wpas_p2p_join(wpa_s, bssid, go_dev_addr,
   2713 				      wpa_s->p2p_wps_method, 0);
   2714 		}
   2715 		return;
   2716 	}
   2717 
   2718 	if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
   2719 		wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
   2720 			   " was rejected (status %u)", MAC2STR(sa), status);
   2721 		return;
   2722 	}
   2723 
   2724 	if (!s) {
   2725 		if (bssid) {
   2726 			wpa_msg_global(wpa_s, MSG_INFO,
   2727 				       P2P_EVENT_INVITATION_RECEIVED
   2728 				       "sa=" MACSTR " go_dev_addr=" MACSTR
   2729 				       " bssid=" MACSTR " unknown-network",
   2730 				       MAC2STR(sa), MAC2STR(go_dev_addr),
   2731 				       MAC2STR(bssid));
   2732 		} else {
   2733 			wpa_msg_global(wpa_s, MSG_INFO,
   2734 				       P2P_EVENT_INVITATION_RECEIVED
   2735 				       "sa=" MACSTR " go_dev_addr=" MACSTR
   2736 				       " unknown-network",
   2737 				       MAC2STR(sa), MAC2STR(go_dev_addr));
   2738 		}
   2739 		return;
   2740 	}
   2741 
   2742 	if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
   2743 		wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
   2744 			       "sa=" MACSTR " persistent=%d freq=%d",
   2745 			       MAC2STR(sa), s->id, op_freq);
   2746 	} else {
   2747 		wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
   2748 			       "sa=" MACSTR " persistent=%d",
   2749 			       MAC2STR(sa), s->id);
   2750 	}
   2751 }
   2752 
   2753 
   2754 static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
   2755 					struct wpa_ssid *ssid,
   2756 					const u8 *peer, int inv)
   2757 {
   2758 	size_t i;
   2759 
   2760 	if (ssid == NULL)
   2761 		return;
   2762 
   2763 	for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
   2764 		if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
   2765 			      ETH_ALEN) == 0)
   2766 			break;
   2767 	}
   2768 	if (i >= ssid->num_p2p_clients) {
   2769 		if (ssid->mode != WPAS_MODE_P2P_GO &&
   2770 		    os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
   2771 			wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
   2772 				   "due to invitation result", ssid->id);
   2773 			wpas_notify_network_removed(wpa_s, ssid);
   2774 			wpa_config_remove_network(wpa_s->conf, ssid->id);
   2775 			return;
   2776 		}
   2777 		return; /* Peer not found in client list */
   2778 	}
   2779 
   2780 	wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
   2781 		   "group %d client list%s",
   2782 		   MAC2STR(peer), ssid->id,
   2783 		   inv ? " due to invitation result" : "");
   2784 	os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
   2785 		   ssid->p2p_client_list + (i + 1) * ETH_ALEN,
   2786 		   (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
   2787 	ssid->num_p2p_clients--;
   2788 #ifndef CONFIG_NO_CONFIG_WRITE
   2789 	if (wpa_s->parent->conf->update_config &&
   2790 	    wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
   2791 		wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
   2792 #endif /* CONFIG_NO_CONFIG_WRITE */
   2793 }
   2794 
   2795 
   2796 static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
   2797 					  const u8 *peer)
   2798 {
   2799 	struct wpa_ssid *ssid;
   2800 
   2801 	wpa_s = wpa_s->global->p2p_invite_group;
   2802 	if (wpa_s == NULL)
   2803 		return; /* No known invitation group */
   2804 	ssid = wpa_s->current_ssid;
   2805 	if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
   2806 	    !ssid->p2p_persistent_group)
   2807 		return; /* Not operating as a GO in persistent group */
   2808 	ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
   2809 				       ssid->ssid, ssid->ssid_len);
   2810 	wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
   2811 }
   2812 
   2813 
   2814 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
   2815 				   const struct p2p_channels *channels,
   2816 				   const u8 *peer)
   2817 {
   2818 	struct wpa_supplicant *wpa_s = ctx;
   2819 	struct wpa_ssid *ssid;
   2820 
   2821 	if (bssid) {
   2822 		wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
   2823 			       "status=%d " MACSTR,
   2824 			       status, MAC2STR(bssid));
   2825 	} else {
   2826 		wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
   2827 			       "status=%d ", status);
   2828 	}
   2829 	wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
   2830 
   2831 	wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
   2832 		   status, MAC2STR(peer));
   2833 	if (wpa_s->pending_invite_ssid_id == -1) {
   2834 		if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
   2835 			wpas_remove_persistent_client(wpa_s, peer);
   2836 		return; /* Invitation to active group */
   2837 	}
   2838 
   2839 	if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
   2840 		wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
   2841 			   "invitation exchange to indicate readiness for "
   2842 			   "re-invocation");
   2843 	}
   2844 
   2845 	if (status != P2P_SC_SUCCESS) {
   2846 		if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
   2847 			ssid = wpa_config_get_network(
   2848 				wpa_s->conf, wpa_s->pending_invite_ssid_id);
   2849 			wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
   2850 		}
   2851 		wpas_p2p_remove_pending_group_interface(wpa_s);
   2852 		return;
   2853 	}
   2854 
   2855 	ssid = wpa_config_get_network(wpa_s->conf,
   2856 				      wpa_s->pending_invite_ssid_id);
   2857 	if (ssid == NULL) {
   2858 		wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
   2859 			   "data matching with invitation");
   2860 		return;
   2861 	}
   2862 
   2863 	/*
   2864 	 * The peer could have missed our ctrl::ack frame for Invitation
   2865 	 * Response and continue retransmitting the frame. To reduce the
   2866 	 * likelihood of the peer not getting successful TX status for the
   2867 	 * Invitation Response frame, wait a short time here before starting
   2868 	 * the persistent group so that we will remain on the current channel to
   2869 	 * acknowledge any possible retransmission from the peer.
   2870 	 */
   2871 #ifndef ANDROID_P2P
   2872 	wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
   2873 		"starting persistent group");
   2874 	os_sleep(0, 50000);
   2875 #else
   2876 	wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 100 ms wait on current channel before "
   2877 		"starting persistent group");
   2878 	os_sleep(0, 100000);
   2879 #endif
   2880 
   2881 	wpas_p2p_group_add_persistent(wpa_s, ssid,
   2882 				      ssid->mode == WPAS_MODE_P2P_GO,
   2883 				      wpa_s->p2p_persistent_go_freq,
   2884 				      wpa_s->p2p_go_ht40, channels);
   2885 }
   2886 
   2887 
   2888 static int wpas_p2p_disallowed_freq(struct wpa_global *global,
   2889 				    unsigned int freq)
   2890 {
   2891 	unsigned int i;
   2892 
   2893 	if (global->p2p_disallow_freq == NULL)
   2894 		return 0;
   2895 
   2896 	for (i = 0; i < global->num_p2p_disallow_freq; i++) {
   2897 		if (freq >= global->p2p_disallow_freq[i].min &&
   2898 		    freq <= global->p2p_disallow_freq[i].max)
   2899 			return 1;
   2900 	}
   2901 
   2902 	return 0;
   2903 }
   2904 
   2905 
   2906 static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
   2907 {
   2908 	reg->channel[reg->channels] = chan;
   2909 	reg->channels++;
   2910 }
   2911 
   2912 
   2913 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
   2914 				     struct p2p_channels *chan)
   2915 {
   2916 	int i, cla = 0;
   2917 
   2918 	wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
   2919 		   "band");
   2920 
   2921 	/* Operating class 81 - 2.4 GHz band channels 1..13 */
   2922 	chan->reg_class[cla].reg_class = 81;
   2923 	chan->reg_class[cla].channels = 0;
   2924 	for (i = 0; i < 11; i++) {
   2925 		if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
   2926 			wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
   2927 	}
   2928 	if (chan->reg_class[cla].channels)
   2929 		cla++;
   2930 
   2931 	wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
   2932 		   "band");
   2933 
   2934 	/* Operating class 115 - 5 GHz, channels 36-48 */
   2935 	chan->reg_class[cla].reg_class = 115;
   2936 	chan->reg_class[cla].channels = 0;
   2937 	if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
   2938 		wpas_p2p_add_chan(&chan->reg_class[cla], 36);
   2939 	if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
   2940 		wpas_p2p_add_chan(&chan->reg_class[cla], 40);
   2941 	if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
   2942 		wpas_p2p_add_chan(&chan->reg_class[cla], 44);
   2943 	if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
   2944 		wpas_p2p_add_chan(&chan->reg_class[cla], 48);
   2945 	if (chan->reg_class[cla].channels)
   2946 		cla++;
   2947 
   2948 	wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
   2949 		   "band");
   2950 
   2951 	/* Operating class 124 - 5 GHz, channels 149,153,157,161 */
   2952 	chan->reg_class[cla].reg_class = 124;
   2953 	chan->reg_class[cla].channels = 0;
   2954 	if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
   2955 		wpas_p2p_add_chan(&chan->reg_class[cla], 149);
   2956 	if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
   2957 		wpas_p2p_add_chan(&chan->reg_class[cla], 153);
   2958 	if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
   2959 		wpas_p2p_add_chan(&chan->reg_class[cla], 157);
   2960 	if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
   2961 		wpas_p2p_add_chan(&chan->reg_class[cla], 161);
   2962 	if (chan->reg_class[cla].channels)
   2963 		cla++;
   2964 
   2965 	chan->reg_classes = cla;
   2966 	return 0;
   2967 }
   2968 
   2969 
   2970 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
   2971 					  u16 num_modes,
   2972 					  enum hostapd_hw_mode mode)
   2973 {
   2974 	u16 i;
   2975 
   2976 	for (i = 0; i < num_modes; i++) {
   2977 		if (modes[i].mode == mode)
   2978 			return &modes[i];
   2979 	}
   2980 
   2981 	return NULL;
   2982 }
   2983 
   2984 
   2985 static int has_channel(struct wpa_global *global,
   2986 		       struct hostapd_hw_modes *mode, u8 chan, int *flags)
   2987 {
   2988 	int i;
   2989 	unsigned int freq;
   2990 
   2991 	freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
   2992 		chan * 5;
   2993 	if (wpas_p2p_disallowed_freq(global, freq))
   2994 		return 0;
   2995 
   2996 	for (i = 0; i < mode->num_channels; i++) {
   2997 		if (mode->channels[i].chan == chan) {
   2998 			if (flags)
   2999 				*flags = mode->channels[i].flag;
   3000 			return !(mode->channels[i].flag &
   3001 				 (HOSTAPD_CHAN_DISABLED |
   3002 				  HOSTAPD_CHAN_PASSIVE_SCAN |
   3003 				  HOSTAPD_CHAN_NO_IBSS |
   3004 				  HOSTAPD_CHAN_RADAR));
   3005 		}
   3006 	}
   3007 
   3008 	return 0;
   3009 }
   3010 
   3011 
   3012 struct p2p_oper_class_map {
   3013 	enum hostapd_hw_mode mode;
   3014 	u8 op_class;
   3015 	u8 min_chan;
   3016 	u8 max_chan;
   3017 	u8 inc;
   3018 	enum { BW20, BW40PLUS, BW40MINUS } bw;
   3019 };
   3020 
   3021 static struct p2p_oper_class_map op_class[] = {
   3022 	{ HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
   3023 #if 0 /* Do not enable HT40 on 2 GHz for now */
   3024 	{ HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
   3025 	{ HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
   3026 #endif
   3027 	{ HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
   3028 	{ HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
   3029 	{ HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
   3030 	{ HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
   3031 	{ HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
   3032 	{ HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
   3033 	{ -1, 0, 0, 0, 0, BW20 }
   3034 };
   3035 
   3036 
   3037 static int wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
   3038 				   struct hostapd_hw_modes *mode,
   3039 				   u8 channel, u8 bw)
   3040 {
   3041 	int flag;
   3042 
   3043 	if (!has_channel(wpa_s->global, mode, channel, &flag))
   3044 		return -1;
   3045 	if (bw == BW40MINUS &&
   3046 	    (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
   3047 	     !has_channel(wpa_s->global, mode, channel - 4, NULL)))
   3048 		return 0;
   3049 	if (bw == BW40PLUS &&
   3050 	    (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
   3051 	     !has_channel(wpa_s->global, mode, channel + 4, NULL)))
   3052 		return 0;
   3053 	return 1;
   3054 }
   3055 
   3056 
   3057 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
   3058 				   struct p2p_channels *chan)
   3059 {
   3060 	struct hostapd_hw_modes *mode;
   3061 	int cla, op;
   3062 
   3063 	if (wpa_s->hw.modes == NULL) {
   3064 		wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
   3065 			   "of all supported channels; assume dualband "
   3066 			   "support");
   3067 		return wpas_p2p_default_channels(wpa_s, chan);
   3068 	}
   3069 
   3070 	cla = 0;
   3071 
   3072 	for (op = 0; op_class[op].op_class; op++) {
   3073 		struct p2p_oper_class_map *o = &op_class[op];
   3074 		u8 ch;
   3075 		struct p2p_reg_class *reg = NULL;
   3076 
   3077 		mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
   3078 		if (mode == NULL)
   3079 			continue;
   3080 		for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
   3081 			if (wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw) < 1)
   3082 				continue;
   3083 			if (reg == NULL) {
   3084 				wpa_printf(MSG_DEBUG, "P2P: Add operating "
   3085 					   "class %u", o->op_class);
   3086 				reg = &chan->reg_class[cla];
   3087 				cla++;
   3088 				reg->reg_class = o->op_class;
   3089 			}
   3090 			reg->channel[reg->channels] = ch;
   3091 			reg->channels++;
   3092 		}
   3093 		if (reg) {
   3094 			wpa_hexdump(MSG_DEBUG, "P2P: Channels",
   3095 				    reg->channel, reg->channels);
   3096 		}
   3097 	}
   3098 
   3099 	chan->reg_classes = cla;
   3100 
   3101 	return 0;
   3102 }
   3103 
   3104 
   3105 int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
   3106 			   struct hostapd_hw_modes *mode, u8 channel)
   3107 {
   3108 	int op, ret;
   3109 
   3110 	for (op = 0; op_class[op].op_class; op++) {
   3111 		struct p2p_oper_class_map *o = &op_class[op];
   3112 		u8 ch;
   3113 
   3114 		for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
   3115 			if (o->mode != HOSTAPD_MODE_IEEE80211A ||
   3116 			    o->bw == BW20 || ch != channel)
   3117 				continue;
   3118 			ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
   3119 			if (ret < 0)
   3120 				continue;
   3121 			else if (ret > 0)
   3122 				return (o->bw == BW40MINUS) ? -1 : 1;
   3123 			else
   3124 				return 0;
   3125 		}
   3126 	}
   3127 	return 0;
   3128 }
   3129 
   3130 
   3131 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
   3132 			size_t buf_len)
   3133 {
   3134 	struct wpa_supplicant *wpa_s = ctx;
   3135 
   3136 	for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
   3137 		if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
   3138 			break;
   3139 	}
   3140 	if (wpa_s == NULL)
   3141 		return -1;
   3142 
   3143 	return wpa_drv_get_noa(wpa_s, buf, buf_len);
   3144 }
   3145 
   3146 
   3147 static int wpas_go_connected(void *ctx, const u8 *dev_addr)
   3148 {
   3149 	struct wpa_supplicant *wpa_s = ctx;
   3150 
   3151 	for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
   3152 		struct wpa_ssid *ssid = wpa_s->current_ssid;
   3153 		if (ssid == NULL)
   3154 			continue;
   3155 		if (ssid->mode != WPAS_MODE_INFRA)
   3156 			continue;
   3157 		if (wpa_s->wpa_state != WPA_COMPLETED &&
   3158 		    wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
   3159 			continue;
   3160 		if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
   3161 			return 1;
   3162 	}
   3163 
   3164 	return 0;
   3165 }
   3166 
   3167 
   3168 static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
   3169 {
   3170 	struct wpa_supplicant *wpa_s = ctx;
   3171 	wpa_msg_global(wpa_s, level, "P2P: %s", msg);
   3172 }
   3173 
   3174 
   3175 int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s)
   3176 {
   3177 	struct wpa_interface iface;
   3178 	struct wpa_supplicant *p2pdev_wpa_s;
   3179 	char ifname[100];
   3180 	char force_name[100];
   3181 	int ret;
   3182 
   3183 	os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
   3184 		    wpa_s->ifname);
   3185 	force_name[0] = '\0';
   3186 	wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
   3187 	ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
   3188 			     force_name, wpa_s->pending_interface_addr, NULL);
   3189 	if (ret < 0) {
   3190 		wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
   3191 		return ret;
   3192 	}
   3193 	os_strlcpy(wpa_s->pending_interface_name, ifname,
   3194 		   sizeof(wpa_s->pending_interface_name));
   3195 
   3196 	os_memset(&iface, 0, sizeof(iface));
   3197 	iface.p2p_mgmt = 1;
   3198 	iface.ifname = wpa_s->pending_interface_name;
   3199 	iface.driver = wpa_s->driver->name;
   3200 	iface.driver_param = wpa_s->conf->driver_param;
   3201 	iface.confname = wpa_s->confname;
   3202 	p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
   3203 	if (!p2pdev_wpa_s) {
   3204 		wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
   3205 		return -1;
   3206 	}
   3207 	p2pdev_wpa_s->parent = wpa_s;
   3208 
   3209 	wpa_s->pending_interface_name[0] = '\0';
   3210 	return 0;
   3211 }
   3212 
   3213 
   3214 /**
   3215  * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
   3216  * @global: Pointer to global data from wpa_supplicant_init()
   3217  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
   3218  * Returns: 0 on success, -1 on failure
   3219  */
   3220 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
   3221 {
   3222 	struct p2p_config p2p;
   3223 	unsigned int r;
   3224 	int i;
   3225 
   3226 	if (wpa_s->conf->p2p_disabled)
   3227 		return 0;
   3228 
   3229 	if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
   3230 		return 0;
   3231 
   3232 	if (global->p2p)
   3233 		return 0;
   3234 
   3235 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
   3236 		struct p2p_params params;
   3237 
   3238 		wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
   3239 		os_memset(&params, 0, sizeof(params));
   3240 		params.dev_name = wpa_s->conf->device_name;
   3241 		os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
   3242 			  WPS_DEV_TYPE_LEN);
   3243 		params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
   3244 		os_memcpy(params.sec_dev_type,
   3245 			  wpa_s->conf->sec_device_type,
   3246 			  params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
   3247 
   3248 		if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
   3249 			return -1;
   3250 
   3251 		return 0;
   3252 	}
   3253 
   3254 	os_memset(&p2p, 0, sizeof(p2p));
   3255 	p2p.cb_ctx = wpa_s;
   3256 	p2p.debug_print = wpas_p2p_debug_print;
   3257 	p2p.p2p_scan = wpas_p2p_scan;
   3258 	p2p.send_action = wpas_send_action;
   3259 	p2p.send_action_done = wpas_send_action_done;
   3260 	p2p.go_neg_completed = wpas_go_neg_completed;
   3261 	p2p.go_neg_req_rx = wpas_go_neg_req_rx;
   3262 	p2p.dev_found = wpas_dev_found;
   3263 	p2p.dev_lost = wpas_dev_lost;
   3264 	p2p.find_stopped = wpas_find_stopped;
   3265 	p2p.start_listen = wpas_start_listen;
   3266 	p2p.stop_listen = wpas_stop_listen;
   3267 	p2p.send_probe_resp = wpas_send_probe_resp;
   3268 	p2p.sd_request = wpas_sd_request;
   3269 	p2p.sd_response = wpas_sd_response;
   3270 	p2p.prov_disc_req = wpas_prov_disc_req;
   3271 	p2p.prov_disc_resp = wpas_prov_disc_resp;
   3272 	p2p.prov_disc_fail = wpas_prov_disc_fail;
   3273 	p2p.invitation_process = wpas_invitation_process;
   3274 	p2p.invitation_received = wpas_invitation_received;
   3275 	p2p.invitation_result = wpas_invitation_result;
   3276 	p2p.get_noa = wpas_get_noa;
   3277 	p2p.go_connected = wpas_go_connected;
   3278 
   3279 	os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
   3280 	os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
   3281 	p2p.dev_name = wpa_s->conf->device_name;
   3282 	p2p.manufacturer = wpa_s->conf->manufacturer;
   3283 	p2p.model_name = wpa_s->conf->model_name;
   3284 	p2p.model_number = wpa_s->conf->model_number;
   3285 	p2p.serial_number = wpa_s->conf->serial_number;
   3286 	if (wpa_s->wps) {
   3287 		os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
   3288 		p2p.config_methods = wpa_s->wps->config_methods;
   3289 	}
   3290 
   3291 	if (wpa_s->conf->p2p_listen_reg_class &&
   3292 	    wpa_s->conf->p2p_listen_channel) {
   3293 		p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
   3294 		p2p.channel = wpa_s->conf->p2p_listen_channel;
   3295 	} else {
   3296 		p2p.reg_class = 81;
   3297 		/*
   3298 		 * Pick one of the social channels randomly as the listen
   3299 		 * channel.
   3300 		 */
   3301 		os_get_random((u8 *) &r, sizeof(r));
   3302 		p2p.channel = 1 + (r % 3) * 5;
   3303 	}
   3304 	wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
   3305 
   3306 	if (wpa_s->conf->p2p_oper_reg_class &&
   3307 	    wpa_s->conf->p2p_oper_channel) {
   3308 		p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
   3309 		p2p.op_channel = wpa_s->conf->p2p_oper_channel;
   3310 		p2p.cfg_op_channel = 1;
   3311 		wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
   3312 			   "%d:%d", p2p.op_reg_class, p2p.op_channel);
   3313 
   3314 	} else {
   3315 		p2p.op_reg_class = 81;
   3316 		/*
   3317 		 * Use random operation channel from (1, 6, 11) if no other
   3318 		 * preference is indicated.
   3319 		 */
   3320 		os_get_random((u8 *) &r, sizeof(r));
   3321 		p2p.op_channel = 1 + (r % 3) * 5;
   3322 		p2p.cfg_op_channel = 0;
   3323 		wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
   3324 			   "%d:%d", p2p.op_reg_class, p2p.op_channel);
   3325 	}
   3326 
   3327 	if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
   3328 		p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
   3329 		p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
   3330 	}
   3331 
   3332 	if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
   3333 		os_memcpy(p2p.country, wpa_s->conf->country, 2);
   3334 		p2p.country[2] = 0x04;
   3335 	} else
   3336 		os_memcpy(p2p.country, "XX\x04", 3);
   3337 
   3338 	if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
   3339 		wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
   3340 			   "channel list");
   3341 		return -1;
   3342 	}
   3343 
   3344 	os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
   3345 		  WPS_DEV_TYPE_LEN);
   3346 
   3347 	p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
   3348 	os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
   3349 		  p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
   3350 
   3351 	p2p.concurrent_operations = !!(wpa_s->drv_flags &
   3352 				       WPA_DRIVER_FLAGS_P2P_CONCURRENT);
   3353 
   3354 	p2p.max_peers = 100;
   3355 
   3356 	if (wpa_s->conf->p2p_ssid_postfix) {
   3357 		p2p.ssid_postfix_len =
   3358 			os_strlen(wpa_s->conf->p2p_ssid_postfix);
   3359 		if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
   3360 			p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
   3361 		os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
   3362 			  p2p.ssid_postfix_len);
   3363 	}
   3364 
   3365 	p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
   3366 
   3367 	p2p.max_listen = wpa_s->max_remain_on_chan;
   3368 
   3369 	global->p2p = p2p_init(&p2p);
   3370 	if (global->p2p == NULL)
   3371 		return -1;
   3372 	global->p2p_init_wpa_s = wpa_s;
   3373 
   3374 	for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
   3375 		if (wpa_s->conf->wps_vendor_ext[i] == NULL)
   3376 			continue;
   3377 		p2p_add_wps_vendor_extension(
   3378 			global->p2p, wpa_s->conf->wps_vendor_ext[i]);
   3379 	}
   3380 
   3381 	return 0;
   3382 }
   3383 
   3384 
   3385 /**
   3386  * wpas_p2p_deinit - Deinitialize per-interface P2P data
   3387  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
   3388  *
   3389  * This function deinitialize per-interface P2P data.
   3390  */
   3391 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
   3392 {
   3393 	if (wpa_s->driver && wpa_s->drv_priv)
   3394 		wpa_drv_probe_req_report(wpa_s, 0);
   3395 
   3396 	if (wpa_s->go_params) {
   3397 		/* Clear any stored provisioning info */
   3398 		p2p_clear_provisioning_info(
   3399 			wpa_s->global->p2p,
   3400 			wpa_s->go_params->peer_device_addr);
   3401 	}
   3402 
   3403 	os_free(wpa_s->go_params);
   3404 	wpa_s->go_params = NULL;
   3405 #ifdef ANDROID_P2P
   3406 	eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
   3407 #endif
   3408 	eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
   3409 	eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
   3410 	wpa_s->p2p_long_listen = 0;
   3411 	eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
   3412 	eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
   3413 	wpas_p2p_remove_pending_group_interface(wpa_s);
   3414 
   3415 	/* TODO: remove group interface from the driver if this wpa_s instance
   3416 	 * is on top of a P2P group interface */
   3417 }
   3418 
   3419 
   3420 /**
   3421  * wpas_p2p_deinit_global - Deinitialize global P2P module
   3422  * @global: Pointer to global data from wpa_supplicant_init()
   3423  *
   3424  * This function deinitializes the global (per device) P2P module.
   3425  */
   3426 void wpas_p2p_deinit_global(struct wpa_global *global)
   3427 {
   3428 	struct wpa_supplicant *wpa_s, *tmp;
   3429 
   3430 	wpa_s = global->ifaces;
   3431 	if (wpa_s)
   3432 		wpas_p2p_service_flush(wpa_s);
   3433 
   3434 	if (global->p2p == NULL)
   3435 		return;
   3436 
   3437 	/* Remove remaining P2P group interfaces */
   3438 	while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
   3439 		wpa_s = wpa_s->next;
   3440 	while (wpa_s) {
   3441 		tmp = global->ifaces;
   3442 		while (tmp &&
   3443 		       (tmp == wpa_s ||
   3444 			tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
   3445 			tmp = tmp->next;
   3446 		}
   3447 		if (tmp == NULL)
   3448 			break;
   3449 		/* Disconnect from the P2P group and deinit the interface */
   3450 		wpas_p2p_disconnect(tmp);
   3451 	}
   3452 
   3453 	/*
   3454 	 * Deinit GO data on any possibly remaining interface (if main
   3455 	 * interface is used as GO).
   3456 	 */
   3457 	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
   3458 		if (wpa_s->ap_iface)
   3459 			wpas_p2p_group_deinit(wpa_s);
   3460 	}
   3461 
   3462 	p2p_deinit(global->p2p);
   3463 	global->p2p = NULL;
   3464 	global->p2p_init_wpa_s = NULL;
   3465 }
   3466 
   3467 
   3468 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
   3469 {
   3470 	if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
   3471 	    wpa_s->conf->p2p_no_group_iface)
   3472 		return 0; /* separate interface disabled per configuration */
   3473 	if (wpa_s->drv_flags &
   3474 	    (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
   3475 	     WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
   3476 		return 1; /* P2P group requires a new interface in every case
   3477 			   */
   3478 	if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
   3479 		return 0; /* driver does not support concurrent operations */
   3480 	if (wpa_s->global->ifaces->next)
   3481 		return 1; /* more that one interface already in use */
   3482 	if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
   3483 		return 1; /* this interface is already in use */
   3484 	return 0;
   3485 }
   3486 
   3487 
   3488 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
   3489 				 const u8 *peer_addr,
   3490 				 enum p2p_wps_method wps_method,
   3491 				 int go_intent, const u8 *own_interface_addr,
   3492 				 unsigned int force_freq, int persistent_group,
   3493 				 struct wpa_ssid *ssid, unsigned int pref_freq)
   3494 {
   3495 	if (persistent_group && wpa_s->conf->persistent_reconnect)
   3496 		persistent_group = 2;
   3497 
   3498 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
   3499 		return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
   3500 					   go_intent, own_interface_addr,
   3501 					   force_freq, persistent_group);
   3502 	}
   3503 
   3504 	/*
   3505 	 * Increase GO config timeout if HT40 is used since it takes some time
   3506 	 * to scan channels for coex purposes before the BSS can be started.
   3507 	 */
   3508 	p2p_set_config_timeout(wpa_s->global->p2p,
   3509 			       wpa_s->p2p_go_ht40 ? 255 : 100, 20);
   3510 
   3511 	return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
   3512 			   go_intent, own_interface_addr, force_freq,
   3513 			   persistent_group, ssid ? ssid->ssid : NULL,
   3514 			   ssid ? ssid->ssid_len : 0,
   3515 			   wpa_s->p2p_pd_before_go_neg, pref_freq);
   3516 }
   3517 
   3518 
   3519 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
   3520 				const u8 *peer_addr,
   3521 				enum p2p_wps_method wps_method,
   3522 				int go_intent, const u8 *own_interface_addr,
   3523 				unsigned int force_freq, int persistent_group,
   3524 				struct wpa_ssid *ssid, unsigned int pref_freq)
   3525 {
   3526 	if (persistent_group && wpa_s->conf->persistent_reconnect)
   3527 		persistent_group = 2;
   3528 
   3529 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
   3530 		return -1;
   3531 
   3532 	return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
   3533 			     go_intent, own_interface_addr, force_freq,
   3534 			     persistent_group, ssid ? ssid->ssid : NULL,
   3535 			     ssid ? ssid->ssid_len : 0, pref_freq);
   3536 }
   3537 
   3538 
   3539 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
   3540 {
   3541 	wpa_s->p2p_join_scan_count++;
   3542 	wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
   3543 		   wpa_s->p2p_join_scan_count);
   3544 	if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
   3545 		wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
   3546 			   " for join operationg - stop join attempt",
   3547 			   MAC2STR(wpa_s->pending_join_iface_addr));
   3548 		eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
   3549 		if (wpa_s->p2p_auto_pd) {
   3550 			wpa_s->p2p_auto_pd = 0;
   3551 			wpa_msg_global(wpa_s, MSG_INFO,
   3552 				       P2P_EVENT_PROV_DISC_FAILURE
   3553 				       " p2p_dev_addr=" MACSTR " status=N/A",
   3554 				       MAC2STR(wpa_s->pending_join_dev_addr));
   3555 			return;
   3556 		}
   3557 		wpa_msg_global(wpa_s->parent, MSG_INFO,
   3558 			       P2P_EVENT_GROUP_FORMATION_FAILURE);
   3559 	}
   3560 }
   3561 
   3562 
   3563 static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
   3564 {
   3565 	int *freqs, res, num, i;
   3566 
   3567 	if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
   3568 		/* Multiple channels are supported and not all are in use */
   3569 		return 0;
   3570 	}
   3571 
   3572 	freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
   3573 	if (!freqs)
   3574 		return 1;
   3575 
   3576 	num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
   3577 					wpa_s->num_multichan_concurrent);
   3578 	if (num < 0) {
   3579 		res = 1;
   3580 		goto exit_free;
   3581 	}
   3582 
   3583 	for (i = 0; i < num; i++) {
   3584 		if (freqs[i] == freq) {
   3585 			wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
   3586 				   freq);
   3587 			res = 0;
   3588 			goto exit_free;
   3589 		}
   3590 	}
   3591 
   3592 	res = 1;
   3593 
   3594 exit_free:
   3595 	os_free(freqs);
   3596 	return res;
   3597 }
   3598 
   3599 
   3600 static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
   3601 			    const u8 *peer_dev_addr)
   3602 {
   3603 	struct wpa_bss *bss;
   3604 	int updated;
   3605 
   3606 	bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
   3607 	if (bss == NULL)
   3608 		return -1;
   3609 	if (bss->last_update_idx < wpa_s->bss_update_idx) {
   3610 		wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
   3611 			   "last scan");
   3612 		return 0;
   3613 	}
   3614 
   3615 	updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
   3616 	wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
   3617 		   "%ld.%06ld (%supdated in last scan)",
   3618 		   bss->last_update.sec, bss->last_update.usec,
   3619 		   updated ? "": "not ");
   3620 
   3621 	return updated;
   3622 }
   3623 
   3624 
   3625 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
   3626 				   struct wpa_scan_results *scan_res)
   3627 {
   3628 	struct wpa_bss *bss;
   3629 	int freq;
   3630 	u8 iface_addr[ETH_ALEN];
   3631 
   3632 	eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
   3633 
   3634 	if (wpa_s->global->p2p_disabled)
   3635 		return;
   3636 
   3637 	wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
   3638 		   scan_res ? (int) scan_res->num : -1,
   3639 		   wpa_s->p2p_auto_join ? "auto_" : "");
   3640 
   3641 	if (scan_res)
   3642 		wpas_p2p_scan_res_handler(wpa_s, scan_res);
   3643 
   3644 	if (wpa_s->p2p_auto_pd) {
   3645 		int join = wpas_p2p_peer_go(wpa_s,
   3646 					    wpa_s->pending_join_dev_addr);
   3647 		if (join == 0 &&
   3648 		    wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
   3649 			wpa_s->auto_pd_scan_retry++;
   3650 			bss = wpa_bss_get_bssid_latest(
   3651 				wpa_s, wpa_s->pending_join_dev_addr);
   3652 			if (bss) {
   3653 				freq = bss->freq;
   3654 				wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
   3655 					   "the peer " MACSTR " at %d MHz",
   3656 					   wpa_s->auto_pd_scan_retry,
   3657 					   MAC2STR(wpa_s->
   3658 						   pending_join_dev_addr),
   3659 					   freq);
   3660 				wpas_p2p_join_scan_req(wpa_s, freq);
   3661 				return;
   3662 			}
   3663 		}
   3664 
   3665 		if (join < 0)
   3666 			join = 0;
   3667 
   3668 		wpa_s->p2p_auto_pd = 0;
   3669 		wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
   3670 		wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
   3671 			   MAC2STR(wpa_s->pending_join_dev_addr), join);
   3672 		if (p2p_prov_disc_req(wpa_s->global->p2p,
   3673 				      wpa_s->pending_join_dev_addr,
   3674 				      wpa_s->pending_pd_config_methods, join,
   3675 				      0, wpa_s->user_initiated_pd) < 0) {
   3676 			wpa_s->p2p_auto_pd = 0;
   3677 			wpa_msg_global(wpa_s, MSG_INFO,
   3678 				       P2P_EVENT_PROV_DISC_FAILURE
   3679 				       " p2p_dev_addr=" MACSTR " status=N/A",
   3680 				       MAC2STR(wpa_s->pending_join_dev_addr));
   3681 		}
   3682 		return;
   3683 	}
   3684 
   3685 	if (wpa_s->p2p_auto_join) {
   3686 		int join = wpas_p2p_peer_go(wpa_s,
   3687 					    wpa_s->pending_join_dev_addr);
   3688 		if (join < 0) {
   3689 			wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
   3690 				   "running a GO -> use GO Negotiation");
   3691 			wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
   3692 					 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
   3693 					 wpa_s->p2p_persistent_group, 0, 0, 0,
   3694 					 wpa_s->p2p_go_intent,
   3695 					 wpa_s->p2p_connect_freq,
   3696 					 wpa_s->p2p_persistent_id,
   3697 					 wpa_s->p2p_pd_before_go_neg,
   3698 					 wpa_s->p2p_go_ht40);
   3699 			return;
   3700 		}
   3701 
   3702 		wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
   3703 			   "try to join the group", join ? "" :
   3704 			   " in older scan");
   3705 		if (!join)
   3706 			wpa_s->p2p_fallback_to_go_neg = 1;
   3707 	}
   3708 
   3709 	freq = p2p_get_oper_freq(wpa_s->global->p2p,
   3710 				 wpa_s->pending_join_iface_addr);
   3711 	if (freq < 0 &&
   3712 	    p2p_get_interface_addr(wpa_s->global->p2p,
   3713 				   wpa_s->pending_join_dev_addr,
   3714 				   iface_addr) == 0 &&
   3715 	    os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
   3716 	{
   3717 		wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
   3718 			   "address for join from " MACSTR " to " MACSTR
   3719 			   " based on newly discovered P2P peer entry",
   3720 			   MAC2STR(wpa_s->pending_join_iface_addr),
   3721 			   MAC2STR(iface_addr));
   3722 		os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
   3723 			  ETH_ALEN);
   3724 
   3725 		freq = p2p_get_oper_freq(wpa_s->global->p2p,
   3726 					 wpa_s->pending_join_iface_addr);
   3727 	}
   3728 	if (freq >= 0) {
   3729 		wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
   3730 			   "from P2P peer table: %d MHz", freq);
   3731 	}
   3732 	bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
   3733 	if (bss) {
   3734 		freq = bss->freq;
   3735 		wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
   3736 			   "from BSS table: %d MHz", freq);
   3737 	}
   3738 	if (freq > 0) {
   3739 		u16 method;
   3740 
   3741 		if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
   3742 			wpa_msg_global(wpa_s->parent, MSG_INFO,
   3743 				       P2P_EVENT_GROUP_FORMATION_FAILURE
   3744 				       "reason=FREQ_CONFLICT");
   3745 			return;
   3746 		}
   3747 
   3748 		wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
   3749 			   "prior to joining an existing group (GO " MACSTR
   3750 			   " freq=%u MHz)",
   3751 			   MAC2STR(wpa_s->pending_join_dev_addr), freq);
   3752 		wpa_s->pending_pd_before_join = 1;
   3753 
   3754 		switch (wpa_s->pending_join_wps_method) {
   3755 		case WPS_PIN_DISPLAY:
   3756 			method = WPS_CONFIG_KEYPAD;
   3757 			break;
   3758 		case WPS_PIN_KEYPAD:
   3759 			method = WPS_CONFIG_DISPLAY;
   3760 			break;
   3761 		case WPS_PBC:
   3762 			method = WPS_CONFIG_PUSHBUTTON;
   3763 			break;
   3764 		default:
   3765 			method = 0;
   3766 			break;
   3767 		}
   3768 
   3769 		if ((p2p_get_provisioning_info(wpa_s->global->p2p,
   3770 					       wpa_s->pending_join_dev_addr) ==
   3771 		     method)) {
   3772 			/*
   3773 			 * We have already performed provision discovery for
   3774 			 * joining the group. Proceed directly to join
   3775 			 * operation without duplicated provision discovery. */
   3776 			wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
   3777 				   "with " MACSTR " already done - proceed to "
   3778 				   "join",
   3779 				   MAC2STR(wpa_s->pending_join_dev_addr));
   3780 			wpa_s->pending_pd_before_join = 0;
   3781 			goto start;
   3782 		}
   3783 
   3784 		if (p2p_prov_disc_req(wpa_s->global->p2p,
   3785 				      wpa_s->pending_join_dev_addr, method, 1,
   3786 				      freq, wpa_s->user_initiated_pd) < 0) {
   3787 			wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
   3788 				   "Discovery Request before joining an "
   3789 				   "existing group");
   3790 			wpa_s->pending_pd_before_join = 0;
   3791 			goto start;
   3792 		}
   3793 		return;
   3794 	}
   3795 
   3796 	wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
   3797 	eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
   3798 	eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
   3799 	wpas_p2p_check_join_scan_limit(wpa_s);
   3800 	return;
   3801 
   3802 start:
   3803 	/* Start join operation immediately */
   3804 	wpas_p2p_join_start(wpa_s);
   3805 }
   3806 
   3807 
   3808 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
   3809 {
   3810 	int ret;
   3811 	struct wpa_driver_scan_params params;
   3812 	struct wpabuf *wps_ie, *ies;
   3813 	size_t ielen;
   3814 	int freqs[2] = { 0, 0 };
   3815 #ifdef ANDROID_P2P
   3816 	int oper_freq;
   3817 
   3818 	/* If freq is not provided, check the operating freq of the GO and do a
   3819 	 * a directed scan to save time
   3820 	 */
   3821 	if(!freq) {
   3822 		freq = (oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
   3823 			 wpa_s->pending_join_iface_addr) == -1) ? 0 : oper_freq;
   3824 	}
   3825 #endif
   3826 	os_memset(&params, 0, sizeof(params));
   3827 
   3828 	/* P2P Wildcard SSID */
   3829 	params.num_ssids = 1;
   3830 	params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
   3831 	params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
   3832 
   3833 	wpa_s->wps->dev.p2p = 1;
   3834 	wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
   3835 					wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
   3836 					NULL);
   3837 	if (wps_ie == NULL) {
   3838 		wpas_p2p_scan_res_join(wpa_s, NULL);
   3839 		return;
   3840 	}
   3841 
   3842 	ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
   3843 	ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
   3844 	if (ies == NULL) {
   3845 		wpabuf_free(wps_ie);
   3846 		wpas_p2p_scan_res_join(wpa_s, NULL);
   3847 		return;
   3848 	}
   3849 	wpabuf_put_buf(ies, wps_ie);
   3850 	wpabuf_free(wps_ie);
   3851 
   3852 	p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
   3853 
   3854 	params.p2p_probe = 1;
   3855 	params.extra_ies = wpabuf_head(ies);
   3856 	params.extra_ies_len = wpabuf_len(ies);
   3857 	if (freq > 0) {
   3858 		freqs[0] = freq;
   3859 		params.freqs = freqs;
   3860 	}
   3861 
   3862 	/*
   3863 	 * Run a scan to update BSS table and start Provision Discovery once
   3864 	 * the new scan results become available.
   3865 	 */
   3866 	ret = wpa_drv_scan(wpa_s, &params);
   3867 	if (!ret) {
   3868 		os_get_time(&wpa_s->scan_trigger_time);
   3869 		wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
   3870 	}
   3871 
   3872 	wpabuf_free(ies);
   3873 
   3874 	if (ret) {
   3875 		wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
   3876 			   "try again later");
   3877 		eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
   3878 		eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
   3879 		wpas_p2p_check_join_scan_limit(wpa_s);
   3880 	}
   3881 }
   3882 
   3883 
   3884 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
   3885 {
   3886 	struct wpa_supplicant *wpa_s = eloop_ctx;
   3887 	wpas_p2p_join_scan_req(wpa_s, 0);
   3888 }
   3889 
   3890 
   3891 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
   3892 			 const u8 *dev_addr, enum p2p_wps_method wps_method,
   3893 			 int auto_join)
   3894 {
   3895 	wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
   3896 		   MACSTR " dev " MACSTR ")%s",
   3897 		   MAC2STR(iface_addr), MAC2STR(dev_addr),
   3898 		   auto_join ? " (auto_join)" : "");
   3899 
   3900 	wpa_s->p2p_auto_pd = 0;
   3901 	wpa_s->p2p_auto_join = !!auto_join;
   3902 	os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
   3903 	os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
   3904 	wpa_s->pending_join_wps_method = wps_method;
   3905 
   3906 	/* Make sure we are not running find during connection establishment */
   3907 	wpas_p2p_stop_find(wpa_s);
   3908 
   3909 	wpa_s->p2p_join_scan_count = 0;
   3910 	wpas_p2p_join_scan(wpa_s, NULL);
   3911 	return 0;
   3912 }
   3913 
   3914 
   3915 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
   3916 {
   3917 	struct wpa_supplicant *group;
   3918 	struct p2p_go_neg_results res;
   3919 	struct wpa_bss *bss;
   3920 
   3921 	group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
   3922 	if (group == NULL)
   3923 		return -1;
   3924 	if (group != wpa_s) {
   3925 		os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
   3926 			  sizeof(group->p2p_pin));
   3927 		group->p2p_wps_method = wpa_s->p2p_wps_method;
   3928 	} else {
   3929 		/*
   3930 		 * Need to mark the current interface for p2p_group_formation
   3931 		 * when a separate group interface is not used. This is needed
   3932 		 * to allow p2p_cancel stop a pending p2p_connect-join.
   3933 		 * wpas_p2p_init_group_interface() addresses this for the case
   3934 		 * where a separate group interface is used.
   3935 		 */
   3936 		wpa_s->global->p2p_group_formation = wpa_s;
   3937 	}
   3938 
   3939 	group->p2p_in_provisioning = 1;
   3940 	group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
   3941 
   3942 	os_memset(&res, 0, sizeof(res));
   3943 	os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
   3944 		  ETH_ALEN);
   3945 	res.wps_method = wpa_s->pending_join_wps_method;
   3946 	bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
   3947 	if (bss) {
   3948 		res.freq = bss->freq;
   3949 		res.ssid_len = bss->ssid_len;
   3950 		os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
   3951 	}
   3952 
   3953 	if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
   3954 		wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
   3955 			   "starting client");
   3956 		wpa_drv_cancel_remain_on_channel(wpa_s);
   3957 		wpa_s->off_channel_freq = 0;
   3958 		wpa_s->roc_waiting_drv_freq = 0;
   3959 	}
   3960 	wpas_start_wps_enrollee(group, &res);
   3961 
   3962 	/*
   3963 	 * Allow a longer timeout for join-a-running-group than normal 15
   3964 	 * second group formation timeout since the GO may not have authorized
   3965 	 * our connection yet.
   3966 	 */
   3967 	eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
   3968 	eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
   3969 			       wpa_s, NULL);
   3970 
   3971 	return 0;
   3972 }
   3973 
   3974 
   3975 static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
   3976 				int *force_freq, int *pref_freq)
   3977 {
   3978 	int *freqs, res;
   3979 	unsigned int freq_in_use = 0, num, i;
   3980 
   3981 	freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
   3982 	if (!freqs)
   3983 		return -1;
   3984 
   3985 	num = get_shared_radio_freqs(wpa_s, freqs,
   3986 				     wpa_s->num_multichan_concurrent);
   3987 	wpa_printf(MSG_DEBUG,
   3988 		   "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u",
   3989 		   freq, wpa_s->num_multichan_concurrent, num);
   3990 
   3991 	if (freq > 0) {
   3992 		if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
   3993 			wpa_printf(MSG_DEBUG, "P2P: The forced channel "
   3994 				   "(%u MHz) is not supported for P2P uses",
   3995 				   freq);
   3996 			res = -3;
   3997 			goto exit_free;
   3998 		}
   3999 
   4000 		for (i = 0; i < num; i++) {
   4001 			if (freqs[i] == freq)
   4002 				freq_in_use = 1;
   4003 		}
   4004 
   4005 		if (num == wpa_s->num_multichan_concurrent && !freq_in_use) {
   4006 			wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
   4007 				   freq);
   4008 			res = -2;
   4009 			goto exit_free;
   4010 		}
   4011 		wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
   4012 			   "requested channel (%u MHz)", freq);
   4013 		*force_freq = freq;
   4014 		goto exit_ok;
   4015 	}
   4016 
   4017 	for (i = 0; i < num; i++) {
   4018 		if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
   4019 			continue;
   4020 
   4021 #ifndef ANDROID_P2P
   4022 		wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
   4023 			   *force_freq);
   4024 		*force_freq = freqs[i];
   4025 #endif
   4026 
   4027 		if (*pref_freq == 0 && num < wpa_s->num_multichan_concurrent) {
   4028 			wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency we are already using");
   4029 			*pref_freq = freqs[i];
   4030 #ifdef ANDROID_P2P
   4031 		} else {
   4032 			wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
   4033 				   *force_freq);
   4034 			*force_freq = freqs[i];
   4035 #endif
   4036 		}
   4037 		break;
   4038 	}
   4039 
   4040 	if (i == num) {
   4041 		if (num < wpa_s->num_multichan_concurrent) {
   4042 			wpa_printf(MSG_DEBUG, "P2P: Current operating channels are not available for P2P. Try to use another channel");
   4043 			*force_freq = 0;
   4044 		} else {
   4045 			wpa_printf(MSG_DEBUG, "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
   4046 			res = -2;
   4047 			goto exit_free;
   4048 		}
   4049 	}
   4050 
   4051 exit_ok:
   4052 	res = 0;
   4053 exit_free:
   4054 	os_free(freqs);
   4055 	return res;
   4056 }
   4057 
   4058 
   4059 /**
   4060  * wpas_p2p_connect - Request P2P Group Formation to be started
   4061  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
   4062  * @peer_addr: Address of the peer P2P Device
   4063  * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
   4064  * @persistent_group: Whether to create a persistent group
   4065  * @auto_join: Whether to select join vs. GO Negotiation automatically
   4066  * @join: Whether to join an existing group (as a client) instead of starting
   4067  *	Group Owner negotiation; @peer_addr is BSSID in that case
   4068  * @auth: Whether to only authorize the connection instead of doing that and
   4069  *	initiating Group Owner negotiation
   4070  * @go_intent: GO Intent or -1 to use default
   4071  * @freq: Frequency for the group or 0 for auto-selection
   4072  * @persistent_id: Persistent group credentials to use for forcing GO
   4073  *	parameters or -1 to generate new values (SSID/passphrase)
   4074  * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
   4075  *	interoperability workaround when initiating group formation
   4076  * @ht40: Start GO with 40 MHz channel width
   4077  * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
   4078  *	failure, -2 on failure due to channel not currently available,
   4079  *	-3 if forced channel is not supported
   4080  */
   4081 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
   4082 		     const char *pin, enum p2p_wps_method wps_method,
   4083 		     int persistent_group, int auto_join, int join, int auth,
   4084 		     int go_intent, int freq, int persistent_id, int pd,
   4085 		     int ht40)
   4086 {
   4087 	int force_freq = 0, pref_freq = 0;
   4088 	int ret = 0, res;
   4089 	enum wpa_driver_if_type iftype;
   4090 	const u8 *if_addr;
   4091 	struct wpa_ssid *ssid = NULL;
   4092 
   4093 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
   4094 		return -1;
   4095 
   4096 	if (persistent_id >= 0) {
   4097 		ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
   4098 		if (ssid == NULL || ssid->disabled != 2 ||
   4099 		    ssid->mode != WPAS_MODE_P2P_GO)
   4100 			return -1;
   4101 	}
   4102 
   4103 	os_free(wpa_s->global->add_psk);
   4104 	wpa_s->global->add_psk = NULL;
   4105 
   4106 	if (go_intent < 0)
   4107 		go_intent = wpa_s->conf->p2p_go_intent;
   4108 
   4109 	if (!auth)
   4110 		wpa_s->p2p_long_listen = 0;
   4111 
   4112 	wpa_s->p2p_wps_method = wps_method;
   4113 	wpa_s->p2p_persistent_group = !!persistent_group;
   4114 	wpa_s->p2p_persistent_id = persistent_id;
   4115 	wpa_s->p2p_go_intent = go_intent;
   4116 	wpa_s->p2p_connect_freq = freq;
   4117 	wpa_s->p2p_fallback_to_go_neg = 0;
   4118 	wpa_s->p2p_pd_before_go_neg = !!pd;
   4119 	wpa_s->p2p_go_ht40 = !!ht40;
   4120 
   4121 	if (pin)
   4122 		os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
   4123 	else if (wps_method == WPS_PIN_DISPLAY) {
   4124 		ret = wps_generate_pin();
   4125 		os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
   4126 			    ret);
   4127 		wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
   4128 			   wpa_s->p2p_pin);
   4129 	} else
   4130 		wpa_s->p2p_pin[0] = '\0';
   4131 
   4132 	if (join || auto_join) {
   4133 		u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
   4134 		if (auth) {
   4135 			wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
   4136 				   "connect a running group from " MACSTR,
   4137 				   MAC2STR(peer_addr));
   4138 			os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
   4139 			return ret;
   4140 		}
   4141 		os_memcpy(dev_addr, peer_addr, ETH_ALEN);
   4142 		if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
   4143 					   iface_addr) < 0) {
   4144 			os_memcpy(iface_addr, peer_addr, ETH_ALEN);
   4145 			p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
   4146 					 dev_addr);
   4147 		}
   4148 		if (auto_join) {
   4149 			os_get_time(&wpa_s->p2p_auto_started);
   4150 			wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
   4151 				   "%ld.%06ld",
   4152 				   wpa_s->p2p_auto_started.sec,
   4153 				   wpa_s->p2p_auto_started.usec);
   4154 		}
   4155 		wpa_s->user_initiated_pd = 1;
   4156 		if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
   4157 				  auto_join) < 0)
   4158 			return -1;
   4159 		return ret;
   4160 	}
   4161 
   4162 	res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq);
   4163 	if (res)
   4164 		return res;
   4165 	wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
   4166 
   4167 	wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
   4168 
   4169 	if (wpa_s->create_p2p_iface) {
   4170 		/* Prepare to add a new interface for the group */
   4171 		iftype = WPA_IF_P2P_GROUP;
   4172 		if (go_intent == 15)
   4173 			iftype = WPA_IF_P2P_GO;
   4174 		if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
   4175 			wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
   4176 				   "interface for the group");
   4177 			return -1;
   4178 		}
   4179 
   4180 		if_addr = wpa_s->pending_interface_addr;
   4181 	} else
   4182 		if_addr = wpa_s->own_addr;
   4183 
   4184 	if (auth) {
   4185 		if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
   4186 					 go_intent, if_addr,
   4187 					 force_freq, persistent_group, ssid,
   4188 					 pref_freq) < 0)
   4189 			return -1;
   4190 		return ret;
   4191 	}
   4192 
   4193 	if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
   4194 				  go_intent, if_addr, force_freq,
   4195 				  persistent_group, ssid, pref_freq) < 0) {
   4196 		if (wpa_s->create_p2p_iface)
   4197 			wpas_p2p_remove_pending_group_interface(wpa_s);
   4198 		return -1;
   4199 	}
   4200 	return ret;
   4201 }
   4202 
   4203 
   4204 /**
   4205  * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
   4206  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
   4207  * @freq: Frequency of the channel in MHz
   4208  * @duration: Duration of the stay on the channel in milliseconds
   4209  *
   4210  * This callback is called when the driver indicates that it has started the
   4211  * requested remain-on-channel duration.
   4212  */
   4213 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
   4214 				   unsigned int freq, unsigned int duration)
   4215 {
   4216 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
   4217 		return;
   4218 	if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
   4219 		p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
   4220 			      wpa_s->pending_listen_duration);
   4221 		wpa_s->pending_listen_freq = 0;
   4222 	}
   4223 }
   4224 
   4225 
   4226 static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
   4227 				 unsigned int timeout)
   4228 {
   4229 	/* Limit maximum Listen state time based on driver limitation. */
   4230 	if (timeout > wpa_s->max_remain_on_chan)
   4231 		timeout = wpa_s->max_remain_on_chan;
   4232 
   4233 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
   4234 		return wpa_drv_p2p_listen(wpa_s, timeout);
   4235 
   4236 	return p2p_listen(wpa_s->global->p2p, timeout);
   4237 }
   4238 
   4239 
   4240 /**
   4241  * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
   4242  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
   4243  * @freq: Frequency of the channel in MHz
   4244  *
   4245  * This callback is called when the driver indicates that a remain-on-channel
   4246  * operation has been completed, i.e., the duration on the requested channel
   4247  * has timed out.
   4248  */
   4249 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
   4250 					  unsigned int freq)
   4251 {
   4252 	wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
   4253 		   "(p2p_long_listen=%d ms pending_action_tx=%p)",
   4254 		   wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
   4255 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
   4256 		return;
   4257 	if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
   4258 		return; /* P2P module started a new operation */
   4259 	if (offchannel_pending_action_tx(wpa_s))
   4260 		return;
   4261 	if (wpa_s->p2p_long_listen > 0)
   4262 		wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
   4263 	if (wpa_s->p2p_long_listen > 0) {
   4264 		wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
   4265 		wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
   4266 	} else {
   4267 		/*
   4268 		 * When listen duration is over, stop listen & update p2p_state
   4269 		 * to IDLE.
   4270 		 */
   4271 		p2p_stop_listen(wpa_s->global->p2p);
   4272 	}
   4273 }
   4274 
   4275 
   4276 /**
   4277  * wpas_p2p_group_remove - Remove a P2P group
   4278  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
   4279  * @ifname: Network interface name of the group interface or "*" to remove all
   4280  *	groups
   4281  * Returns: 0 on success, -1 on failure
   4282  *
   4283  * This function is used to remove a P2P group. This can be used to disconnect
   4284  * from a group in which the local end is a P2P Client or to end a P2P Group in
   4285  * case the local end is the Group Owner. If a virtual network interface was
   4286  * created for this group, that interface will be removed. Otherwise, only the
   4287  * configured P2P group network will be removed from the interface.
   4288  */
   4289 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
   4290 {
   4291 	struct wpa_global *global = wpa_s->global;
   4292 
   4293 	if (os_strcmp(ifname, "*") == 0) {
   4294 		struct wpa_supplicant *prev;
   4295 		wpa_s = global->ifaces;
   4296 		while (wpa_s) {
   4297 			prev = wpa_s;
   4298 			wpa_s = wpa_s->next;
   4299 			if (prev->p2p_group_interface !=
   4300 			    NOT_P2P_GROUP_INTERFACE ||
   4301 			    (prev->current_ssid &&
   4302 			     prev->current_ssid->p2p_group))
   4303 				wpas_p2p_disconnect(prev);
   4304 		}
   4305 		return 0;
   4306 	}
   4307 
   4308 	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
   4309 		if (os_strcmp(wpa_s->ifname, ifname) == 0)
   4310 			break;
   4311 	}
   4312 
   4313 	return wpas_p2p_disconnect(wpa_s);
   4314 }
   4315 
   4316 
   4317 static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
   4318 {
   4319 	unsigned int r;
   4320 
   4321 	if (freq == 2) {
   4322 		wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
   4323 			   "band");
   4324 		if (wpa_s->best_24_freq > 0 &&
   4325 		    p2p_supported_freq(wpa_s->global->p2p,
   4326 				       wpa_s->best_24_freq)) {
   4327 			freq = wpa_s->best_24_freq;
   4328 			wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
   4329 				   "channel: %d MHz", freq);
   4330 		} else {
   4331 			os_get_random((u8 *) &r, sizeof(r));
   4332 			freq = 2412 + (r % 3) * 25;
   4333 			wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
   4334 				   "channel: %d MHz", freq);
   4335 		}
   4336 	}
   4337 
   4338 	if (freq == 5) {
   4339 		wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
   4340 			   "band");
   4341 		if (wpa_s->best_5_freq > 0 &&
   4342 		    p2p_supported_freq(wpa_s->global->p2p,
   4343 				       wpa_s->best_5_freq)) {
   4344 			freq = wpa_s->best_5_freq;
   4345 			wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
   4346 				   "channel: %d MHz", freq);
   4347 		} else {
   4348 			os_get_random((u8 *) &r, sizeof(r));
   4349 			freq = 5180 + (r % 4) * 20;
   4350 			if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
   4351 				wpa_printf(MSG_DEBUG, "P2P: Could not select "
   4352 					   "5 GHz channel for P2P group");
   4353 				return -1;
   4354 			}
   4355 			wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
   4356 				   "channel: %d MHz", freq);
   4357 		}
   4358 	}
   4359 
   4360 	if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
   4361 		wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
   4362 			   "(%u MHz) is not supported for P2P uses",
   4363 			   freq);
   4364 		return -1;
   4365 	}
   4366 
   4367 	return freq;
   4368 }
   4369 
   4370 
   4371 static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
   4372 				   struct p2p_go_neg_results *params,
   4373 				   int freq, int ht40,
   4374 				   const struct p2p_channels *channels)
   4375 {
   4376 	int res, *freqs;
   4377 	unsigned int pref_freq;
   4378 	unsigned int num, i;
   4379 
   4380 	os_memset(params, 0, sizeof(*params));
   4381 	params->role_go = 1;
   4382 	params->ht40 = ht40;
   4383 	if (freq) {
   4384 		if (!freq_included(channels, freq)) {
   4385 			wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
   4386 				   "accepted", freq);
   4387 			return -1;
   4388 		}
   4389 		wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
   4390 			   "frequency %d MHz", freq);
   4391 		params->freq = freq;
   4392 	} else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
   4393 		   wpa_s->conf->p2p_oper_channel >= 1 &&
   4394 		   wpa_s->conf->p2p_oper_channel <= 11 &&
   4395 		   freq_included(channels,
   4396 				 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
   4397 		params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
   4398 		wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
   4399 			   "frequency %d MHz", params->freq);
   4400 	} else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
   4401 		    wpa_s->conf->p2p_oper_reg_class == 116 ||
   4402 		    wpa_s->conf->p2p_oper_reg_class == 117 ||
   4403 		    wpa_s->conf->p2p_oper_reg_class == 124 ||
   4404 		    wpa_s->conf->p2p_oper_reg_class == 126 ||
   4405 		    wpa_s->conf->p2p_oper_reg_class == 127) &&
   4406 		   freq_included(channels,
   4407 				 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
   4408 		params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
   4409 		wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
   4410 			   "frequency %d MHz", params->freq);
   4411 	} else if (wpa_s->conf->p2p_oper_channel == 0 &&
   4412 		   wpa_s->best_overall_freq > 0 &&
   4413 		   p2p_supported_freq(wpa_s->global->p2p,
   4414 				      wpa_s->best_overall_freq) &&
   4415 		   freq_included(channels, wpa_s->best_overall_freq)) {
   4416 		params->freq = wpa_s->best_overall_freq;
   4417 		wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
   4418 			   "channel %d MHz", params->freq);
   4419 	} else if (wpa_s->conf->p2p_oper_channel == 0 &&
   4420 		   wpa_s->best_24_freq > 0 &&
   4421 		   p2p_supported_freq(wpa_s->global->p2p,
   4422 				      wpa_s->best_24_freq) &&
   4423 		   freq_included(channels, wpa_s->best_24_freq)) {
   4424 		params->freq = wpa_s->best_24_freq;
   4425 		wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
   4426 			   "channel %d MHz", params->freq);
   4427 	} else if (wpa_s->conf->p2p_oper_channel == 0 &&
   4428 		   wpa_s->best_5_freq > 0 &&
   4429 		   p2p_supported_freq(wpa_s->global->p2p,
   4430 				      wpa_s->best_5_freq) &&
   4431 		   freq_included(channels, wpa_s->best_5_freq)) {
   4432 		params->freq = wpa_s->best_5_freq;
   4433 		wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
   4434 			   "channel %d MHz", params->freq);
   4435 	} else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
   4436 						  channels))) {
   4437 		params->freq = pref_freq;
   4438 		wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
   4439 			   "channels", params->freq);
   4440 	} else {
   4441 		int chan;
   4442 		for (chan = 0; chan < 11; chan++) {
   4443 			params->freq = 2412 + chan * 5;
   4444 			if (!wpas_p2p_disallowed_freq(wpa_s->global,
   4445 						      params->freq) &&
   4446 			    freq_included(channels, params->freq))
   4447 				break;
   4448 		}
   4449 		if (chan == 11) {
   4450 			wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
   4451 				   "allowed");
   4452 			return -1;
   4453 		}
   4454 		wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
   4455 			   "known)", params->freq);
   4456 	}
   4457 
   4458 	freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
   4459 	if (!freqs)
   4460 		return -1;
   4461 
   4462 	res = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
   4463 					wpa_s->num_multichan_concurrent);
   4464 	if (res < 0) {
   4465 		os_free(freqs);
   4466 		return -1;
   4467 	}
   4468 	num = res;
   4469 
   4470 	if (!freq) {
   4471 		for (i = 0; i < num; i++) {
   4472 			if (freq_included(channels, freqs[i])) {
   4473 				wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
   4474 					   freqs[i]);
   4475 				params->freq = freqs[i];
   4476 				break;
   4477 			}
   4478 		}
   4479 
   4480 		if (i == num) {
   4481 			if (num == wpa_s->num_multichan_concurrent) {
   4482 				wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
   4483 				os_free(freqs);
   4484 				return -1;
   4485 			} else {
   4486 				wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
   4487 			}
   4488 		}
   4489 	} else {
   4490 		for (i = 0; i < num; i++) {
   4491 			if (freqs[i] == freq)
   4492 				break;
   4493 		}
   4494 
   4495 		if (i == num) {
   4496 			if (num == wpa_s->num_multichan_concurrent) {
   4497 				wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
   4498 				os_free(freqs);
   4499 				return -1;
   4500 			} else {
   4501 				wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
   4502 			}
   4503 		}
   4504 	}
   4505 	os_free(freqs);
   4506 	return 0;
   4507 }
   4508 
   4509 
   4510 static struct wpa_supplicant *
   4511 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
   4512 			 int go)
   4513 {
   4514 	struct wpa_supplicant *group_wpa_s;
   4515 
   4516 	if (!wpas_p2p_create_iface(wpa_s)) {
   4517 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
   4518 			"operations");
   4519 		return wpa_s;
   4520 	}
   4521 
   4522 	if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
   4523 					 WPA_IF_P2P_CLIENT) < 0) {
   4524 		wpa_msg_global(wpa_s, MSG_ERROR,
   4525 			       "P2P: Failed to add group interface");
   4526 		return NULL;
   4527 	}
   4528 	group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
   4529 	if (group_wpa_s == NULL) {
   4530 		wpa_msg_global(wpa_s, MSG_ERROR,
   4531 			       "P2P: Failed to initialize group interface");
   4532 		wpas_p2p_remove_pending_group_interface(wpa_s);
   4533 		return NULL;
   4534 	}
   4535 
   4536 	wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
   4537 		group_wpa_s->ifname);
   4538 	return group_wpa_s;
   4539 }
   4540 
   4541 
   4542 /**
   4543  * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
   4544  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
   4545  * @persistent_group: Whether to create a persistent group
   4546  * @freq: Frequency for the group or 0 to indicate no hardcoding
   4547  * Returns: 0 on success, -1 on failure
   4548  *
   4549  * This function creates a new P2P group with the local end as the Group Owner,
   4550  * i.e., without using Group Owner Negotiation.
   4551  */
   4552 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
   4553 		       int freq, int ht40)
   4554 {
   4555 	struct p2p_go_neg_results params;
   4556 
   4557 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
   4558 		return -1;
   4559 
   4560 	os_free(wpa_s->global->add_psk);
   4561 	wpa_s->global->add_psk = NULL;
   4562 
   4563 	/* Make sure we are not running find during connection establishment */
   4564 	wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
   4565 	wpas_p2p_stop_find_oper(wpa_s);
   4566 
   4567 	freq = wpas_p2p_select_go_freq(wpa_s, freq);
   4568 	if (freq < 0)
   4569 		return -1;
   4570 
   4571 	if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, NULL))
   4572 		return -1;
   4573 	if (params.freq &&
   4574 	    !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
   4575 		wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
   4576 			   "(%u MHz) is not supported for P2P uses",
   4577 			   params.freq);
   4578 		return -1;
   4579 	}
   4580 	p2p_go_params(wpa_s->global->p2p, &params);
   4581 	params.persistent_group = persistent_group;
   4582 
   4583 	wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
   4584 	if (wpa_s == NULL)
   4585 		return -1;
   4586 	wpas_start_wps_go(wpa_s, &params, 0);
   4587 
   4588 	return 0;
   4589 }
   4590 
   4591 
   4592 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
   4593 				 struct wpa_ssid *params, int addr_allocated)
   4594 {
   4595 	struct wpa_ssid *ssid;
   4596 
   4597 	wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
   4598 	if (wpa_s == NULL)
   4599 		return -1;
   4600 	wpa_s->p2p_last_4way_hs_fail = NULL;
   4601 
   4602 	wpa_supplicant_ap_deinit(wpa_s);
   4603 
   4604 	ssid = wpa_config_add_network(wpa_s->conf);
   4605 	if (ssid == NULL)
   4606 		return -1;
   4607 	wpa_config_set_network_defaults(ssid);
   4608 	ssid->temporary = 1;
   4609 	ssid->proto = WPA_PROTO_RSN;
   4610 	ssid->pairwise_cipher = WPA_CIPHER_CCMP;
   4611 	ssid->group_cipher = WPA_CIPHER_CCMP;
   4612 	ssid->key_mgmt = WPA_KEY_MGMT_PSK;
   4613 	ssid->ssid = os_malloc(params->ssid_len);
   4614 	if (ssid->ssid == NULL) {
   4615 		wpa_config_remove_network(wpa_s->conf, ssid->id);
   4616 		return -1;
   4617 	}
   4618 	os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
   4619 	ssid->ssid_len = params->ssid_len;
   4620 	ssid->p2p_group = 1;
   4621 	ssid->export_keys = 1;
   4622 	if (params->psk_set) {
   4623 		os_memcpy(ssid->psk, params->psk, 32);
   4624 		ssid->psk_set = 1;
   4625 	}
   4626 	if (params->passphrase)
   4627 		ssid->passphrase = os_strdup(params->passphrase);
   4628 
   4629 	wpa_supplicant_select_network(wpa_s, ssid);
   4630 
   4631 	wpa_s->show_group_started = 1;
   4632 
   4633 	return 0;
   4634 }
   4635 
   4636 
   4637 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
   4638 				  struct wpa_ssid *ssid, int addr_allocated,
   4639 				  int freq, int ht40,
   4640 				  const struct p2p_channels *channels)
   4641 {
   4642 	struct p2p_go_neg_results params;
   4643 	int go = 0;
   4644 
   4645 	if (ssid->disabled != 2 || ssid->ssid == NULL)
   4646 		return -1;
   4647 
   4648 	if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
   4649 	    go == (ssid->mode == WPAS_MODE_P2P_GO)) {
   4650 		wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
   4651 			   "already running");
   4652 		return 0;
   4653 	}
   4654 
   4655 	os_free(wpa_s->global->add_psk);
   4656 	wpa_s->global->add_psk = NULL;
   4657 
   4658 	/* Make sure we are not running find during connection establishment */
   4659 	wpas_p2p_stop_find_oper(wpa_s);
   4660 
   4661 	wpa_s->p2p_fallback_to_go_neg = 0;
   4662 
   4663 	if (ssid->mode == WPAS_MODE_INFRA)
   4664 		return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
   4665 
   4666 	if (ssid->mode != WPAS_MODE_P2P_GO)
   4667 		return -1;
   4668 
   4669 	freq = wpas_p2p_select_go_freq(wpa_s, freq);
   4670 	if (freq < 0)
   4671 		return -1;
   4672 
   4673 	if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, channels))
   4674 		return -1;
   4675 
   4676 	params.role_go = 1;
   4677 	params.psk_set = ssid->psk_set;
   4678 	if (params.psk_set)
   4679 		os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
   4680 	if (ssid->passphrase) {
   4681 		if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
   4682 			wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
   4683 				   "persistent group");
   4684 			return -1;
   4685 		}
   4686 		os_strlcpy(params.passphrase, ssid->passphrase,
   4687 			   sizeof(params.passphrase));
   4688 	}
   4689 	os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
   4690 	params.ssid_len = ssid->ssid_len;
   4691 	params.persistent_group = 1;
   4692 
   4693 	wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
   4694 	if (wpa_s == NULL)
   4695 		return -1;
   4696 
   4697 	wpas_start_wps_go(wpa_s, &params, 0);
   4698 
   4699 	return 0;
   4700 }
   4701 
   4702 
   4703 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
   4704 			       struct wpabuf *proberesp_ies)
   4705 {
   4706 	struct wpa_supplicant *wpa_s = ctx;
   4707 	if (wpa_s->ap_iface) {
   4708 		struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
   4709 		if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
   4710 			wpabuf_free(beacon_ies);
   4711 			wpabuf_free(proberesp_ies);
   4712 			return;
   4713 		}
   4714 		if (beacon_ies) {
   4715 			wpabuf_free(hapd->p2p_beacon_ie);
   4716 			hapd->p2p_beacon_ie = beacon_ies;
   4717 		}
   4718 		wpabuf_free(hapd->p2p_probe_resp_ie);
   4719 		hapd->p2p_probe_resp_ie = proberesp_ies;
   4720 	} else {
   4721 		wpabuf_free(beacon_ies);
   4722 		wpabuf_free(proberesp_ies);
   4723 	}
   4724 	wpa_supplicant_ap_update_beacon(wpa_s);
   4725 }
   4726 
   4727 
   4728 static void wpas_p2p_idle_update(void *ctx, int idle)
   4729 {
   4730 	struct wpa_supplicant *wpa_s = ctx;
   4731 	if (!wpa_s->ap_iface)
   4732 		return;
   4733 	wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
   4734 	if (idle)
   4735 		wpas_p2p_set_group_idle_timeout(wpa_s);
   4736 	else
   4737 		eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
   4738 }
   4739 
   4740 
   4741 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
   4742 				       struct wpa_ssid *ssid)
   4743 {
   4744 	struct p2p_group *group;
   4745 	struct p2p_group_config *cfg;
   4746 
   4747 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
   4748 		return NULL;
   4749 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
   4750 		return NULL;
   4751 
   4752 	cfg = os_zalloc(sizeof(*cfg));
   4753 	if (cfg == NULL)
   4754 		return NULL;
   4755 
   4756 	if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
   4757 		cfg->persistent_group = 2;
   4758 	else if (ssid->p2p_persistent_group)
   4759 		cfg->persistent_group = 1;
   4760 	os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
   4761 	if (wpa_s->max_stations &&
   4762 	    wpa_s->max_stations < wpa_s->conf->max_num_sta)
   4763 		cfg->max_clients = wpa_s->max_stations;
   4764 	else
   4765 		cfg->max_clients = wpa_s->conf->max_num_sta;
   4766 	os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
   4767 	cfg->ssid_len = ssid->ssid_len;
   4768 	cfg->cb_ctx = wpa_s;
   4769 	cfg->ie_update = wpas_p2p_ie_update;
   4770 	cfg->idle_update = wpas_p2p_idle_update;
   4771 
   4772 	group = p2p_group_init(wpa_s->global->p2p, cfg);
   4773 	if (group == NULL)
   4774 		os_free(cfg);
   4775 	if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
   4776 		p2p_group_notif_formation_done(group);
   4777 	wpa_s->p2p_group = group;
   4778 	return group;
   4779 }
   4780 
   4781 
   4782 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
   4783 			  int registrar)
   4784 {
   4785 	struct wpa_ssid *ssid = wpa_s->current_ssid;
   4786 
   4787 	if (!wpa_s->p2p_in_provisioning) {
   4788 		wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
   4789 			   "provisioning not in progress");
   4790 		return;
   4791 	}
   4792 
   4793 	if (ssid && ssid->mode == WPAS_MODE_INFRA) {
   4794 		u8 go_dev_addr[ETH_ALEN];
   4795 		os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
   4796 		wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
   4797 					  ssid->ssid_len);
   4798 		/* Clear any stored provisioning info */
   4799 		p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
   4800 	}
   4801 
   4802 	eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
   4803 			     NULL);
   4804 	if (ssid && ssid->mode == WPAS_MODE_INFRA) {
   4805 		/*
   4806 		 * Use a separate timeout for initial data connection to
   4807 		 * complete to allow the group to be removed automatically if
   4808 		 * something goes wrong in this step before the P2P group idle
   4809 		 * timeout mechanism is taken into use.
   4810 		 */
   4811 		eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
   4812 				       wpas_p2p_group_formation_timeout,
   4813 				       wpa_s->parent, NULL);
   4814 	}
   4815 	if (wpa_s->global->p2p)
   4816 		p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
   4817 	else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
   4818 		wpa_drv_wps_success_cb(wpa_s, peer_addr);
   4819 	wpas_group_formation_completed(wpa_s, 1);
   4820 }
   4821 
   4822 
   4823 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
   4824 			 struct wps_event_fail *fail)
   4825 {
   4826 	if (!wpa_s->p2p_in_provisioning) {
   4827 		wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
   4828 			   "provisioning not in progress");
   4829 		return;
   4830 	}
   4831 
   4832 	if (wpa_s->go_params) {
   4833 		p2p_clear_provisioning_info(
   4834 			wpa_s->global->p2p,
   4835 			wpa_s->go_params->peer_device_addr);
   4836 	}
   4837 
   4838 	wpas_notify_p2p_wps_failed(wpa_s, fail);
   4839 }
   4840 
   4841 
   4842 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
   4843 		       const char *config_method,
   4844 		       enum wpas_p2p_prov_disc_use use)
   4845 {
   4846 	u16 config_methods;
   4847 
   4848 	wpa_s->p2p_fallback_to_go_neg = 0;
   4849 	wpa_s->pending_pd_use = NORMAL_PD;
   4850 	if (os_strncmp(config_method, "display", 7) == 0)
   4851 		config_methods = WPS_CONFIG_DISPLAY;
   4852 	else if (os_strncmp(config_method, "keypad", 6) == 0)
   4853 		config_methods = WPS_CONFIG_KEYPAD;
   4854 	else if (os_strncmp(config_method, "pbc", 3) == 0 ||
   4855 		 os_strncmp(config_method, "pushbutton", 10) == 0)
   4856 		config_methods = WPS_CONFIG_PUSHBUTTON;
   4857 	else {
   4858 		wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
   4859 		return -1;
   4860 	}
   4861 
   4862 	if (use == WPAS_P2P_PD_AUTO) {
   4863 		os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
   4864 		wpa_s->pending_pd_config_methods = config_methods;
   4865 		wpa_s->p2p_auto_pd = 1;
   4866 		wpa_s->p2p_auto_join = 0;
   4867 		wpa_s->pending_pd_before_join = 0;
   4868 		wpa_s->auto_pd_scan_retry = 0;
   4869 		wpas_p2p_stop_find(wpa_s);
   4870 		wpa_s->p2p_join_scan_count = 0;
   4871 		os_get_time(&wpa_s->p2p_auto_started);
   4872 		wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
   4873 			   wpa_s->p2p_auto_started.sec,
   4874 			   wpa_s->p2p_auto_started.usec);
   4875 		wpas_p2p_join_scan(wpa_s, NULL);
   4876 		return 0;
   4877 	}
   4878 
   4879 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
   4880 		return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
   4881 						 config_methods,
   4882 						 use == WPAS_P2P_PD_FOR_JOIN);
   4883 	}
   4884 
   4885 	if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
   4886 		return -1;
   4887 
   4888 	return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
   4889 				 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
   4890 				 0, 1);
   4891 }
   4892 
   4893 
   4894 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
   4895 			      char *end)
   4896 {
   4897 	return p2p_scan_result_text(ies, ies_len, buf, end);
   4898 }
   4899 
   4900 
   4901 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
   4902 {
   4903 	if (!offchannel_pending_action_tx(wpa_s))
   4904 		return;
   4905 
   4906 	wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
   4907 		   "operation request");
   4908 	offchannel_clear_pending_action_tx(wpa_s);
   4909 }
   4910 
   4911 
   4912 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
   4913 		  enum p2p_discovery_type type,
   4914 		  unsigned int num_req_dev_types, const u8 *req_dev_types,
   4915 		  const u8 *dev_id, unsigned int search_delay)
   4916 {
   4917 	wpas_p2p_clear_pending_action_tx(wpa_s);
   4918 	wpa_s->p2p_long_listen = 0;
   4919 
   4920 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
   4921 		return wpa_drv_p2p_find(wpa_s, timeout, type);
   4922 
   4923 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
   4924 	    wpa_s->p2p_in_provisioning)
   4925 		return -1;
   4926 
   4927 	wpa_supplicant_cancel_sched_scan(wpa_s);
   4928 
   4929 	return p2p_find(wpa_s->global->p2p, timeout, type,
   4930 			num_req_dev_types, req_dev_types, dev_id,
   4931 			search_delay);
   4932 }
   4933 
   4934 
   4935 static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
   4936 {
   4937 	wpas_p2p_clear_pending_action_tx(wpa_s);
   4938 	wpa_s->p2p_long_listen = 0;
   4939 	eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
   4940 	eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
   4941 	wpa_s->global->p2p_cb_on_scan_complete = 0;
   4942 
   4943 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
   4944 		wpa_drv_p2p_stop_find(wpa_s);
   4945 		return 1;
   4946 	}
   4947 
   4948 	if (wpa_s->global->p2p)
   4949 		p2p_stop_find(wpa_s->global->p2p);
   4950 
   4951 	return 0;
   4952 }
   4953 
   4954 
   4955 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
   4956 {
   4957 	if (wpas_p2p_stop_find_oper(wpa_s) > 0)
   4958 		return;
   4959 	wpas_p2p_remove_pending_group_interface(wpa_s);
   4960 }
   4961 
   4962 
   4963 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
   4964 {
   4965 	struct wpa_supplicant *wpa_s = eloop_ctx;
   4966 	wpa_s->p2p_long_listen = 0;
   4967 }
   4968 
   4969 
   4970 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
   4971 {
   4972 	int res;
   4973 
   4974 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
   4975 		return -1;
   4976 
   4977 	wpa_supplicant_cancel_sched_scan(wpa_s);
   4978 	wpas_p2p_clear_pending_action_tx(wpa_s);
   4979 
   4980 	if (timeout == 0) {
   4981 		/*
   4982 		 * This is a request for unlimited Listen state. However, at
   4983 		 * least for now, this is mapped to a Listen state for one
   4984 		 * hour.
   4985 		 */
   4986 		timeout = 3600;
   4987 	}
   4988 	eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
   4989 	wpa_s->p2p_long_listen = 0;
   4990 
   4991 	/*
   4992 	 * Stop previous find/listen operation to avoid trying to request a new
   4993 	 * remain-on-channel operation while the driver is still running the
   4994 	 * previous one.
   4995 	 */
   4996 	if (wpa_s->global->p2p)
   4997 		p2p_stop_find(wpa_s->global->p2p);
   4998 
   4999 	res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
   5000 	if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
   5001 		wpa_s->p2p_long_listen = timeout * 1000;
   5002 		eloop_register_timeout(timeout, 0,
   5003 				       wpas_p2p_long_listen_timeout,
   5004 				       wpa_s, NULL);
   5005 	}
   5006 
   5007 	return res;
   5008 }
   5009 
   5010 
   5011 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
   5012 			  u8 *buf, size_t len, int p2p_group)
   5013 {
   5014 	struct wpabuf *p2p_ie;
   5015 	int ret;
   5016 
   5017 	if (wpa_s->global->p2p_disabled)
   5018 		return -1;
   5019 	if (wpa_s->global->p2p == NULL)
   5020 		return -1;
   5021 	if (bss == NULL)
   5022 		return -1;
   5023 
   5024 	p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
   5025 	ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
   5026 			       p2p_group, p2p_ie);
   5027 	wpabuf_free(p2p_ie);
   5028 
   5029 	return ret;
   5030 }
   5031 
   5032 
   5033 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
   5034 			  const u8 *dst, const u8 *bssid,
   5035 			  const u8 *ie, size_t ie_len, int ssi_signal)
   5036 {
   5037 	if (wpa_s->global->p2p_disabled)
   5038 		return 0;
   5039 	if (wpa_s->global->p2p == NULL)
   5040 		return 0;
   5041 
   5042 	switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
   5043 				 ie, ie_len)) {
   5044 	case P2P_PREQ_NOT_P2P:
   5045 		wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
   5046 				 ssi_signal);
   5047 		/* fall through */
   5048 	case P2P_PREQ_MALFORMED:
   5049 	case P2P_PREQ_NOT_LISTEN:
   5050 	case P2P_PREQ_NOT_PROCESSED:
   5051 	default: /* make gcc happy */
   5052 		return 0;
   5053 	case P2P_PREQ_PROCESSED:
   5054 		return 1;
   5055 	}
   5056 }
   5057 
   5058 
   5059 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
   5060 			const u8 *sa, const u8 *bssid,
   5061 			u8 category, const u8 *data, size_t len, int freq)
   5062 {
   5063 	if (wpa_s->global->p2p_disabled)
   5064 		return;
   5065 	if (wpa_s->global->p2p == NULL)
   5066 		return;
   5067 
   5068 	p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
   5069 		      freq);
   5070 }
   5071 
   5072 
   5073 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
   5074 {
   5075 	if (wpa_s->global->p2p_disabled)
   5076 		return;
   5077 	if (wpa_s->global->p2p == NULL)
   5078 		return;
   5079 
   5080 	p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
   5081 }
   5082 
   5083 
   5084 void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
   5085 {
   5086 	p2p_group_deinit(wpa_s->p2p_group);
   5087 	wpa_s->p2p_group = NULL;
   5088 
   5089 	wpa_s->ap_configured_cb = NULL;
   5090 	wpa_s->ap_configured_cb_ctx = NULL;
   5091 	wpa_s->ap_configured_cb_data = NULL;
   5092 	wpa_s->connect_without_scan = NULL;
   5093 }
   5094 
   5095 
   5096 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
   5097 {
   5098 	wpa_s->p2p_long_listen = 0;
   5099 
   5100 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
   5101 		return wpa_drv_p2p_reject(wpa_s, addr);
   5102 
   5103 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
   5104 		return -1;
   5105 
   5106 	return p2p_reject(wpa_s->global->p2p, addr);
   5107 }
   5108 
   5109 
   5110 /* Invite to reinvoke a persistent group */
   5111 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
   5112 		    struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
   5113 		    int ht40, int pref_freq)
   5114 {
   5115 	enum p2p_invite_role role;
   5116 	u8 *bssid = NULL;
   5117 	int force_freq = 0;
   5118 	int res;
   5119 
   5120 	wpa_s->global->p2p_invite_group = NULL;
   5121 	if (peer_addr)
   5122 		os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
   5123 	else
   5124 		os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
   5125 
   5126 	wpa_s->p2p_persistent_go_freq = freq;
   5127 	wpa_s->p2p_go_ht40 = !!ht40;
   5128 	if (ssid->mode == WPAS_MODE_P2P_GO) {
   5129 		role = P2P_INVITE_ROLE_GO;
   5130 		if (peer_addr == NULL) {
   5131 			wpa_printf(MSG_DEBUG, "P2P: Missing peer "
   5132 				   "address in invitation command");
   5133 			return -1;
   5134 		}
   5135 		if (wpas_p2p_create_iface(wpa_s)) {
   5136 			if (wpas_p2p_add_group_interface(wpa_s,
   5137 							 WPA_IF_P2P_GO) < 0) {
   5138 				wpa_printf(MSG_ERROR, "P2P: Failed to "
   5139 					   "allocate a new interface for the "
   5140 					   "group");
   5141 				return -1;
   5142 			}
   5143 			bssid = wpa_s->pending_interface_addr;
   5144 		} else
   5145 			bssid = wpa_s->own_addr;
   5146 	} else {
   5147 		role = P2P_INVITE_ROLE_CLIENT;
   5148 		peer_addr = ssid->bssid;
   5149 	}
   5150 	wpa_s->pending_invite_ssid_id = ssid->id;
   5151 
   5152 	res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq);
   5153 	if (res)
   5154 		return res;
   5155 
   5156 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
   5157 		return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
   5158 					  ssid->ssid, ssid->ssid_len,
   5159 					  go_dev_addr, 1);
   5160 
   5161 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
   5162 		return -1;
   5163 
   5164 	return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
   5165 			  ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
   5166 			  1, pref_freq);
   5167 }
   5168 
   5169 
   5170 /* Invite to join an active group */
   5171 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
   5172 			  const u8 *peer_addr, const u8 *go_dev_addr)
   5173 {
   5174 	struct wpa_global *global = wpa_s->global;
   5175 	enum p2p_invite_role role;
   5176 	u8 *bssid = NULL;
   5177 	struct wpa_ssid *ssid;
   5178 	int persistent;
   5179 	int force_freq = 0, pref_freq = 0;
   5180 	int res;
   5181 
   5182 	wpa_s->p2p_persistent_go_freq = 0;
   5183 	wpa_s->p2p_go_ht40 = 0;
   5184 
   5185 	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
   5186 		if (os_strcmp(wpa_s->ifname, ifname) == 0)
   5187 			break;
   5188 	}
   5189 	if (wpa_s == NULL) {
   5190 		wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
   5191 		return -1;
   5192 	}
   5193 
   5194 	ssid = wpa_s->current_ssid;
   5195 	if (ssid == NULL) {
   5196 		wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
   5197 			   "invitation");
   5198 		return -1;
   5199 	}
   5200 
   5201 	wpa_s->global->p2p_invite_group = wpa_s;
   5202 	persistent = ssid->p2p_persistent_group &&
   5203 		wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
   5204 					ssid->ssid, ssid->ssid_len);
   5205 
   5206 	if (ssid->mode == WPAS_MODE_P2P_GO) {
   5207 		role = P2P_INVITE_ROLE_ACTIVE_GO;
   5208 		bssid = wpa_s->own_addr;
   5209 		if (go_dev_addr == NULL)
   5210 			go_dev_addr = wpa_s->global->p2p_dev_addr;
   5211 	} else {
   5212 		role = P2P_INVITE_ROLE_CLIENT;
   5213 		if (wpa_s->wpa_state < WPA_ASSOCIATED) {
   5214 			wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
   5215 				   "invite to current group");
   5216 			return -1;
   5217 		}
   5218 		bssid = wpa_s->bssid;
   5219 		if (go_dev_addr == NULL &&
   5220 		    !is_zero_ether_addr(wpa_s->go_dev_addr))
   5221 			go_dev_addr = wpa_s->go_dev_addr;
   5222 	}
   5223 	wpa_s->parent->pending_invite_ssid_id = -1;
   5224 
   5225 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
   5226 		return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
   5227 					  ssid->ssid, ssid->ssid_len,
   5228 					  go_dev_addr, persistent);
   5229 
   5230 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
   5231 		return -1;
   5232 
   5233 	res = wpas_p2p_setup_freqs(wpa_s, 0, &force_freq, &pref_freq);
   5234 	if (res)
   5235 		return res;
   5236 	wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
   5237 
   5238 	return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
   5239 			  ssid->ssid, ssid->ssid_len, force_freq,
   5240 			  go_dev_addr, persistent, pref_freq);
   5241 }
   5242 
   5243 
   5244 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
   5245 {
   5246 	struct wpa_ssid *ssid = wpa_s->current_ssid;
   5247 	const char *ssid_txt;
   5248 	u8 go_dev_addr[ETH_ALEN];
   5249 	int network_id = -1;
   5250 	int persistent;
   5251 	int freq;
   5252 
   5253 	if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
   5254 		eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
   5255 				     wpa_s->parent, NULL);
   5256 	}
   5257 
   5258 	if (!wpa_s->show_group_started || !ssid)
   5259 		goto done;
   5260 
   5261 	wpa_s->show_group_started = 0;
   5262 
   5263 	ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
   5264 	os_memset(go_dev_addr, 0, ETH_ALEN);
   5265 	if (ssid->bssid_set)
   5266 		os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
   5267 	persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
   5268 					       ssid->ssid_len);
   5269 	os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
   5270 
   5271 	if (wpa_s->global->p2p_group_formation == wpa_s)
   5272 		wpa_s->global->p2p_group_formation = NULL;
   5273 
   5274 	freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
   5275 		(int) wpa_s->assoc_freq;
   5276 	if (ssid->passphrase == NULL && ssid->psk_set) {
   5277 		char psk[65];
   5278 		wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
   5279 		wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
   5280 			       "%s client ssid=\"%s\" freq=%d psk=%s "
   5281 			       "go_dev_addr=" MACSTR "%s",
   5282 			       wpa_s->ifname, ssid_txt, freq, psk,
   5283 			       MAC2STR(go_dev_addr),
   5284 			       persistent ? " [PERSISTENT]" : "");
   5285 	} else {
   5286 		wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
   5287 			       "%s client ssid=\"%s\" freq=%d "
   5288 			       "passphrase=\"%s\" go_dev_addr=" MACSTR "%s",
   5289 			       wpa_s->ifname, ssid_txt, freq,
   5290 			       ssid->passphrase ? ssid->passphrase : "",
   5291 			       MAC2STR(go_dev_addr),
   5292 			       persistent ? " [PERSISTENT]" : "");
   5293 	}
   5294 
   5295 	if (persistent)
   5296 		network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
   5297 							     ssid, go_dev_addr);
   5298 	if (network_id < 0)
   5299 		network_id = ssid->id;
   5300 	wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
   5301 
   5302 done:
   5303 	wpas_p2p_continue_after_scan(wpa_s);
   5304 }
   5305 
   5306 
   5307 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
   5308 			  u32 interval1, u32 duration2, u32 interval2)
   5309 {
   5310 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
   5311 		return -1;
   5312 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
   5313 		return -1;
   5314 
   5315 	if (wpa_s->wpa_state < WPA_ASSOCIATED ||
   5316 	    wpa_s->current_ssid == NULL ||
   5317 	    wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
   5318 		return -1;
   5319 
   5320 	return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
   5321 				wpa_s->own_addr, wpa_s->assoc_freq,
   5322 				duration1, interval1, duration2, interval2);
   5323 }
   5324 
   5325 
   5326 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
   5327 			unsigned int interval)
   5328 {
   5329 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
   5330 		return -1;
   5331 
   5332 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
   5333 		return -1;
   5334 
   5335 	return p2p_ext_listen(wpa_s->global->p2p, period, interval);
   5336 }
   5337 
   5338 
   5339 static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
   5340 {
   5341 	if (wpa_s->current_ssid == NULL) {
   5342 		/*
   5343 		 * current_ssid can be cleared when P2P client interface gets
   5344 		 * disconnected, so assume this interface was used as P2P
   5345 		 * client.
   5346 		 */
   5347 		return 1;
   5348 	}
   5349 	return wpa_s->current_ssid->p2p_group &&
   5350 		wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
   5351 }
   5352 
   5353 
   5354 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
   5355 {
   5356 	struct wpa_supplicant *wpa_s = eloop_ctx;
   5357 
   5358 	if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
   5359 		wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
   5360 			   "disabled");
   5361 		return;
   5362 	}
   5363 
   5364 	wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
   5365 		   "group");
   5366 	wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
   5367 }
   5368 
   5369 
   5370 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
   5371 {
   5372 	int timeout;
   5373 
   5374 	if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
   5375 		wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
   5376 
   5377 	if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
   5378 		return;
   5379 
   5380 	timeout = wpa_s->conf->p2p_group_idle;
   5381 	if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
   5382 	    (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
   5383 	    timeout = P2P_MAX_CLIENT_IDLE;
   5384 
   5385 	if (timeout == 0)
   5386 		return;
   5387 
   5388 	if (timeout < 0) {
   5389 		if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
   5390 			timeout = 0; /* special client mode no-timeout */
   5391 		else
   5392 			return;
   5393 	}
   5394 
   5395 	if (wpa_s->p2p_in_provisioning) {
   5396 		/*
   5397 		 * Use the normal group formation timeout during the
   5398 		 * provisioning phase to avoid terminating this process too
   5399 		 * early due to group idle timeout.
   5400 		 */
   5401 		wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
   5402 			   "during provisioning");
   5403 		return;
   5404 	}
   5405 
   5406 	if (wpa_s->show_group_started) {
   5407 		/*
   5408 		 * Use the normal group formation timeout between the end of
   5409 		 * the provisioning phase and completion of 4-way handshake to
   5410 		 * avoid terminating this process too early due to group idle
   5411 		 * timeout.
   5412 		 */
   5413 		wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
   5414 			   "while waiting for initial 4-way handshake to "
   5415 			   "complete");
   5416 		return;
   5417 	}
   5418 
   5419 	wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
   5420 		   timeout);
   5421 	eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
   5422 			       wpa_s, NULL);
   5423 }
   5424 
   5425 
   5426 /* Returns 1 if the interface was removed */
   5427 int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
   5428 			  u16 reason_code, const u8 *ie, size_t ie_len,
   5429 			  int locally_generated)
   5430 {
   5431 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
   5432 		return 0;
   5433 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
   5434 		return 0;
   5435 
   5436 	if (!locally_generated)
   5437 		p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
   5438 				 ie_len);
   5439 
   5440 	if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
   5441 	    wpa_s->current_ssid &&
   5442 	    wpa_s->current_ssid->p2p_group &&
   5443 	    wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
   5444 		wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
   5445 			   "session is ending");
   5446 		if (wpas_p2p_group_delete(wpa_s,
   5447 					  P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
   5448 		    > 0)
   5449 			return 1;
   5450 	}
   5451 
   5452 	return 0;
   5453 }
   5454 
   5455 
   5456 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
   5457 			     u16 reason_code, const u8 *ie, size_t ie_len,
   5458 			     int locally_generated)
   5459 {
   5460 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
   5461 		return;
   5462 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
   5463 		return;
   5464 
   5465 	if (!locally_generated)
   5466 		p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
   5467 				   ie_len);
   5468 }
   5469 
   5470 
   5471 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
   5472 {
   5473 	struct p2p_data *p2p = wpa_s->global->p2p;
   5474 
   5475 	if (p2p == NULL)
   5476 		return;
   5477 
   5478 	if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
   5479 		return;
   5480 
   5481 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
   5482 		p2p_set_dev_name(p2p, wpa_s->conf->device_name);
   5483 
   5484 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
   5485 		p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
   5486 
   5487 	if (wpa_s->wps &&
   5488 	    (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
   5489 		p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
   5490 
   5491 	if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
   5492 		p2p_set_uuid(p2p, wpa_s->wps->uuid);
   5493 
   5494 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
   5495 		p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
   5496 		p2p_set_model_name(p2p, wpa_s->conf->model_name);
   5497 		p2p_set_model_number(p2p, wpa_s->conf->model_number);
   5498 		p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
   5499 	}
   5500 
   5501 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
   5502 		p2p_set_sec_dev_types(p2p,
   5503 				      (void *) wpa_s->conf->sec_device_type,
   5504 				      wpa_s->conf->num_sec_device_types);
   5505 
   5506 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
   5507 		int i;
   5508 		p2p_remove_wps_vendor_extensions(p2p);
   5509 		for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
   5510 			if (wpa_s->conf->wps_vendor_ext[i] == NULL)
   5511 				continue;
   5512 			p2p_add_wps_vendor_extension(
   5513 				p2p, wpa_s->conf->wps_vendor_ext[i]);
   5514 		}
   5515 	}
   5516 
   5517 	if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
   5518 	    wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
   5519 		char country[3];
   5520 		country[0] = wpa_s->conf->country[0];
   5521 		country[1] = wpa_s->conf->country[1];
   5522 		country[2] = 0x04;
   5523 		p2p_set_country(p2p, country);
   5524 	}
   5525 
   5526 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
   5527 		p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
   5528 				     wpa_s->conf->p2p_ssid_postfix ?
   5529 				     os_strlen(wpa_s->conf->p2p_ssid_postfix) :
   5530 				     0);
   5531 	}
   5532 
   5533 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
   5534 		p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
   5535 
   5536 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
   5537 		u8 reg_class, channel;
   5538 		int ret;
   5539 		unsigned int r;
   5540 		if (wpa_s->conf->p2p_listen_reg_class &&
   5541 		    wpa_s->conf->p2p_listen_channel) {
   5542 			reg_class = wpa_s->conf->p2p_listen_reg_class;
   5543 			channel = wpa_s->conf->p2p_listen_channel;
   5544 		} else {
   5545 			reg_class = 81;
   5546 			/*
   5547 			 * Pick one of the social channels randomly as the
   5548 			 * listen channel.
   5549 			 */
   5550 			os_get_random((u8 *) &r, sizeof(r));
   5551 			channel = 1 + (r % 3) * 5;
   5552 		}
   5553 		ret = p2p_set_listen_channel(p2p, reg_class, channel);
   5554 		if (ret)
   5555 			wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
   5556 				   "failed: %d", ret);
   5557 	}
   5558 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
   5559 		u8 op_reg_class, op_channel, cfg_op_channel;
   5560 		int ret = 0;
   5561 		unsigned int r;
   5562 		if (wpa_s->conf->p2p_oper_reg_class &&
   5563 		    wpa_s->conf->p2p_oper_channel) {
   5564 			op_reg_class = wpa_s->conf->p2p_oper_reg_class;
   5565 			op_channel = wpa_s->conf->p2p_oper_channel;
   5566 			cfg_op_channel = 1;
   5567 		} else {
   5568 			op_reg_class = 81;
   5569 			/*
   5570 			 * Use random operation channel from (1, 6, 11)
   5571 			 *if no other preference is indicated.
   5572 			 */
   5573 			os_get_random((u8 *) &r, sizeof(r));
   5574 			op_channel = 1 + (r % 3) * 5;
   5575 			cfg_op_channel = 0;
   5576 		}
   5577 		ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
   5578 					   cfg_op_channel);
   5579 		if (ret)
   5580 			wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
   5581 				   "failed: %d", ret);
   5582 	}
   5583 
   5584 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
   5585 		if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
   5586 				      wpa_s->conf->p2p_pref_chan) < 0) {
   5587 			wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
   5588 				   "update failed");
   5589 		}
   5590 	}
   5591 }
   5592 
   5593 
   5594 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
   5595 		     int duration)
   5596 {
   5597 	if (!wpa_s->ap_iface)
   5598 		return -1;
   5599 	return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
   5600 				   duration);
   5601 }
   5602 
   5603 
   5604 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
   5605 {
   5606 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
   5607 		return -1;
   5608 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
   5609 		return -1;
   5610 
   5611 	wpa_s->global->cross_connection = enabled;
   5612 	p2p_set_cross_connect(wpa_s->global->p2p, enabled);
   5613 
   5614 	if (!enabled) {
   5615 		struct wpa_supplicant *iface;
   5616 
   5617 		for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
   5618 		{
   5619 			if (iface->cross_connect_enabled == 0)
   5620 				continue;
   5621 
   5622 			iface->cross_connect_enabled = 0;
   5623 			iface->cross_connect_in_use = 0;
   5624 			wpa_msg_global(iface->parent, MSG_INFO,
   5625 				       P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
   5626 				       iface->ifname,
   5627 				       iface->cross_connect_uplink);
   5628 		}
   5629 	}
   5630 
   5631 	return 0;
   5632 }
   5633 
   5634 
   5635 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
   5636 {
   5637 	struct wpa_supplicant *iface;
   5638 
   5639 	if (!uplink->global->cross_connection)
   5640 		return;
   5641 
   5642 	for (iface = uplink->global->ifaces; iface; iface = iface->next) {
   5643 		if (!iface->cross_connect_enabled)
   5644 			continue;
   5645 		if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
   5646 		    0)
   5647 			continue;
   5648 		if (iface->ap_iface == NULL)
   5649 			continue;
   5650 		if (iface->cross_connect_in_use)
   5651 			continue;
   5652 
   5653 		iface->cross_connect_in_use = 1;
   5654 		wpa_msg_global(iface->parent, MSG_INFO,
   5655 			       P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
   5656 			       iface->ifname, iface->cross_connect_uplink);
   5657 	}
   5658 }
   5659 
   5660 
   5661 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
   5662 {
   5663 	struct wpa_supplicant *iface;
   5664 
   5665 	for (iface = uplink->global->ifaces; iface; iface = iface->next) {
   5666 		if (!iface->cross_connect_enabled)
   5667 			continue;
   5668 		if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
   5669 		    0)
   5670 			continue;
   5671 		if (!iface->cross_connect_in_use)
   5672 			continue;
   5673 
   5674 		wpa_msg_global(iface->parent, MSG_INFO,
   5675 			       P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
   5676 			       iface->ifname, iface->cross_connect_uplink);
   5677 		iface->cross_connect_in_use = 0;
   5678 	}
   5679 }
   5680 
   5681 
   5682 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
   5683 {
   5684 	if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
   5685 	    wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
   5686 	    wpa_s->cross_connect_disallowed)
   5687 		wpas_p2p_disable_cross_connect(wpa_s);
   5688 	else
   5689 		wpas_p2p_enable_cross_connect(wpa_s);
   5690 	if (!wpa_s->ap_iface &&
   5691 	    eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
   5692 		wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
   5693 }
   5694 
   5695 
   5696 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
   5697 {
   5698 	wpas_p2p_disable_cross_connect(wpa_s);
   5699 	if (!wpa_s->ap_iface &&
   5700 	    !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
   5701 					 wpa_s, NULL))
   5702 		wpas_p2p_set_group_idle_timeout(wpa_s);
   5703 }
   5704 
   5705 
   5706 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
   5707 {
   5708 	struct wpa_supplicant *iface;
   5709 
   5710 	if (!wpa_s->global->cross_connection)
   5711 		return;
   5712 
   5713 	for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
   5714 		if (iface == wpa_s)
   5715 			continue;
   5716 		if (iface->drv_flags &
   5717 		    WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
   5718 			continue;
   5719 		if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
   5720 			continue;
   5721 
   5722 		wpa_s->cross_connect_enabled = 1;
   5723 		os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
   5724 			   sizeof(wpa_s->cross_connect_uplink));
   5725 		wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
   5726 			   "%s to %s whenever uplink is available",
   5727 			   wpa_s->ifname, wpa_s->cross_connect_uplink);
   5728 
   5729 		if (iface->ap_iface || iface->current_ssid == NULL ||
   5730 		    iface->current_ssid->mode != WPAS_MODE_INFRA ||
   5731 		    iface->cross_connect_disallowed ||
   5732 		    iface->wpa_state != WPA_COMPLETED)
   5733 			break;
   5734 
   5735 		wpa_s->cross_connect_in_use = 1;
   5736 		wpa_msg_global(wpa_s->parent, MSG_INFO,
   5737 			       P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
   5738 			       wpa_s->ifname, wpa_s->cross_connect_uplink);
   5739 		break;
   5740 	}
   5741 }
   5742 
   5743 
   5744 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
   5745 {
   5746 	if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
   5747 	    !wpa_s->p2p_in_provisioning)
   5748 		return 0; /* not P2P client operation */
   5749 
   5750 	wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
   5751 		   "session overlap");
   5752 	if (wpa_s != wpa_s->parent)
   5753 		wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
   5754 	wpas_p2p_group_formation_failed(wpa_s);
   5755 	return 1;
   5756 }
   5757 
   5758 
   5759 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
   5760 {
   5761 	struct p2p_channels chan;
   5762 
   5763 	if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
   5764 		return;
   5765 
   5766 	os_memset(&chan, 0, sizeof(chan));
   5767 	if (wpas_p2p_setup_channels(wpa_s, &chan)) {
   5768 		wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
   5769 			   "channel list");
   5770 		return;
   5771 	}
   5772 
   5773 	p2p_update_channel_list(wpa_s->global->p2p, &chan);
   5774 }
   5775 
   5776 
   5777 static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
   5778 				     struct wpa_scan_results *scan_res)
   5779 {
   5780 	wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
   5781 }
   5782 
   5783 
   5784 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
   5785 {
   5786 	struct wpa_global *global = wpa_s->global;
   5787 	int found = 0;
   5788 	const u8 *peer;
   5789 
   5790 	if (global->p2p == NULL)
   5791 		return -1;
   5792 
   5793 	wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
   5794 
   5795 	if (wpa_s->pending_interface_name[0] &&
   5796 	    !is_zero_ether_addr(wpa_s->pending_interface_addr))
   5797 		found = 1;
   5798 
   5799 	peer = p2p_get_go_neg_peer(global->p2p);
   5800 	if (peer) {
   5801 		wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
   5802 			   MACSTR, MAC2STR(peer));
   5803 		p2p_unauthorize(global->p2p, peer);
   5804 		found = 1;
   5805 	}
   5806 
   5807 	if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
   5808 		wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
   5809 		wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
   5810 		found = 1;
   5811 	}
   5812 
   5813 	if (wpa_s->pending_pd_before_join) {
   5814 		wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
   5815 		wpa_s->pending_pd_before_join = 0;
   5816 		found = 1;
   5817 	}
   5818 
   5819 	wpas_p2p_stop_find(wpa_s);
   5820 
   5821 	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
   5822 		if (wpa_s == global->p2p_group_formation &&
   5823 		    (wpa_s->p2p_in_provisioning ||
   5824 		     wpa_s->parent->pending_interface_type ==
   5825 		     WPA_IF_P2P_CLIENT)) {
   5826 			wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
   5827 				   "formation found - cancelling",
   5828 				   wpa_s->ifname);
   5829 			found = 1;
   5830 			eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
   5831 					     wpa_s->parent, NULL);
   5832 			if (wpa_s->p2p_in_provisioning) {
   5833 				wpas_group_formation_completed(wpa_s, 0);
   5834 				break;
   5835 			}
   5836 			wpas_p2p_group_delete(wpa_s,
   5837 					      P2P_GROUP_REMOVAL_REQUESTED);
   5838 			break;
   5839 		}
   5840 	}
   5841 
   5842 	if (!found) {
   5843 		wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
   5844 		return -1;
   5845 	}
   5846 
   5847 	return 0;
   5848 }
   5849 
   5850 
   5851 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
   5852 {
   5853 	if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
   5854 		return;
   5855 
   5856 	wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
   5857 		   "being available anymore");
   5858 	wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
   5859 }
   5860 
   5861 
   5862 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
   5863 				   int freq_24, int freq_5, int freq_overall)
   5864 {
   5865 	struct p2p_data *p2p = wpa_s->global->p2p;
   5866 	if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
   5867 		return;
   5868 	p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
   5869 }
   5870 
   5871 
   5872 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
   5873 {
   5874 	u8 peer[ETH_ALEN];
   5875 	struct p2p_data *p2p = wpa_s->global->p2p;
   5876 
   5877 	if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
   5878 		return -1;
   5879 
   5880 	if (hwaddr_aton(addr, peer))
   5881 		return -1;
   5882 
   5883 	return p2p_unauthorize(p2p, peer);
   5884 }
   5885 
   5886 
   5887 /**
   5888  * wpas_p2p_disconnect - Disconnect from a P2P Group
   5889  * @wpa_s: Pointer to wpa_supplicant data
   5890  * Returns: 0 on success, -1 on failure
   5891  *
   5892  * This can be used to disconnect from a group in which the local end is a P2P
   5893  * Client or to end a P2P Group in case the local end is the Group Owner. If a
   5894  * virtual network interface was created for this group, that interface will be
   5895  * removed. Otherwise, only the configured P2P group network will be removed
   5896  * from the interface.
   5897  */
   5898 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
   5899 {
   5900 
   5901 	if (wpa_s == NULL)
   5902 		return -1;
   5903 
   5904 	return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
   5905 		-1 : 0;
   5906 }
   5907 
   5908 
   5909 int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
   5910 {
   5911 	int ret;
   5912 
   5913 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
   5914 		return 0;
   5915 
   5916 	ret = p2p_in_progress(wpa_s->global->p2p);
   5917 	if (ret == 0) {
   5918 		/*
   5919 		 * Check whether there is an ongoing WPS provisioning step (or
   5920 		 * other parts of group formation) on another interface since
   5921 		 * p2p_in_progress() does not report this to avoid issues for
   5922 		 * scans during such provisioning step.
   5923 		 */
   5924 		if (wpa_s->global->p2p_group_formation &&
   5925 		    wpa_s->global->p2p_group_formation != wpa_s) {
   5926 			wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
   5927 				"in group formation",
   5928 				wpa_s->global->p2p_group_formation->ifname);
   5929 			ret = 1;
   5930 		}
   5931 	}
   5932 
   5933 	if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
   5934 		struct os_time now;
   5935 		os_get_time(&now);
   5936 		if (now.sec > wpa_s->global->p2p_go_wait_client.sec +
   5937 		    P2P_MAX_INITIAL_CONN_WAIT_GO) {
   5938 			/* Wait for the first client has expired */
   5939 			wpa_s->global->p2p_go_wait_client.sec = 0;
   5940 		} else {
   5941 			wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
   5942 			ret = 1;
   5943 		}
   5944 	}
   5945 
   5946 	return ret;
   5947 }
   5948 
   5949 
   5950 void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
   5951 			      struct wpa_ssid *ssid)
   5952 {
   5953 	if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
   5954 	    eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
   5955 				 wpa_s->parent, NULL) > 0) {
   5956 		/**
   5957 		 * Remove the network by scheduling the group formation
   5958 		 * timeout to happen immediately. The teardown code
   5959 		 * needs to be scheduled to run asynch later so that we
   5960 		 * don't delete data from under ourselves unexpectedly.
   5961 		 * Calling wpas_p2p_group_formation_timeout directly
   5962 		 * causes a series of crashes in WPS failure scenarios.
   5963 		 */
   5964 		wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
   5965 			   "P2P group network getting removed");
   5966 		eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
   5967 				       wpa_s->parent, NULL);
   5968 	}
   5969 }
   5970 
   5971 
   5972 struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
   5973 					  const u8 *addr, const u8 *ssid,
   5974 					  size_t ssid_len)
   5975 {
   5976 	struct wpa_ssid *s;
   5977 	size_t i;
   5978 
   5979 	for (s = wpa_s->conf->ssid; s; s = s->next) {
   5980 		if (s->disabled != 2)
   5981 			continue;
   5982 		if (ssid &&
   5983 		    (ssid_len != s->ssid_len ||
   5984 		     os_memcmp(ssid, s->ssid, ssid_len) != 0))
   5985 			continue;
   5986 		if (addr == NULL) {
   5987 			if (s->mode == WPAS_MODE_P2P_GO)
   5988 				return s;
   5989 			continue;
   5990 		}
   5991 		if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
   5992 			return s; /* peer is GO in the persistent group */
   5993 		if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
   5994 			continue;
   5995 		for (i = 0; i < s->num_p2p_clients; i++) {
   5996 			if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
   5997 				      addr, ETH_ALEN) == 0)
   5998 				return s; /* peer is P2P client in persistent
   5999 					   * group */
   6000 		}
   6001 	}
   6002 
   6003 	return NULL;
   6004 }
   6005 
   6006 
   6007 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
   6008 				       const u8 *addr)
   6009 {
   6010 	wpa_s->global->p2p_go_wait_client.sec = 0;
   6011 	if (addr == NULL)
   6012 		return;
   6013 	wpas_p2p_add_persistent_group_client(wpa_s, addr);
   6014 }
   6015 
   6016 
   6017 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
   6018 					int group_added)
   6019 {
   6020 	struct wpa_supplicant *group = wpa_s;
   6021 	if (wpa_s->global->p2p_group_formation)
   6022 		group = wpa_s->global->p2p_group_formation;
   6023 	wpa_s = wpa_s->parent;
   6024 	offchannel_send_action_done(wpa_s);
   6025 	if (group_added)
   6026 		wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
   6027 	wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
   6028 	wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
   6029 			 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
   6030 			 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
   6031 			 wpa_s->p2p_persistent_id,
   6032 			 wpa_s->p2p_pd_before_go_neg,
   6033 			 wpa_s->p2p_go_ht40);
   6034 }
   6035 
   6036 
   6037 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
   6038 {
   6039 	if (!wpa_s->p2p_fallback_to_go_neg ||
   6040 	    wpa_s->p2p_in_provisioning <= 5)
   6041 		return 0;
   6042 
   6043 	if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
   6044 		return 0; /* peer operating as a GO */
   6045 
   6046 	wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
   6047 		"fallback to GO Negotiation");
   6048 	wpas_p2p_fallback_to_go_neg(wpa_s, 1);
   6049 
   6050 	return 1;
   6051 }
   6052 
   6053 
   6054 unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
   6055 {
   6056 	const char *rn, *rn2;
   6057 	struct wpa_supplicant *ifs;
   6058 
   6059 	if (wpa_s->wpa_state > WPA_SCANNING) {
   6060 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
   6061 			"concurrent operation",
   6062 			P2P_CONCURRENT_SEARCH_DELAY);
   6063 		return P2P_CONCURRENT_SEARCH_DELAY;
   6064 	}
   6065 
   6066 	if (!wpa_s->driver->get_radio_name)
   6067 		return 0;
   6068 	rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
   6069 	if (rn == NULL || rn[0] == '\0')
   6070 		return 0;
   6071 
   6072 	for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
   6073 		if (ifs == wpa_s || !ifs->driver->get_radio_name)
   6074 			continue;
   6075 
   6076 		rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
   6077 		if (!rn2 || os_strcmp(rn, rn2) != 0)
   6078 			continue;
   6079 		if (ifs->wpa_state > WPA_SCANNING) {
   6080 			wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
   6081 				"delay due to concurrent operation on "
   6082 				"interface %s",
   6083 				P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
   6084 			return P2P_CONCURRENT_SEARCH_DELAY;
   6085 		}
   6086 	}
   6087 
   6088 	return 0;
   6089 }
   6090 
   6091 
   6092 void wpas_p2p_continue_after_scan(struct wpa_supplicant *wpa_s)
   6093 {
   6094 	wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Station mode scan operation not "
   6095 		"pending anymore (sta_scan_pending=%d "
   6096 		"p2p_cb_on_scan_complete=%d)", wpa_s->sta_scan_pending,
   6097 		wpa_s->global->p2p_cb_on_scan_complete);
   6098 	wpa_s->sta_scan_pending = 0;
   6099 
   6100 	if (!wpa_s->global->p2p_cb_on_scan_complete)
   6101 		return;
   6102 	wpa_s->global->p2p_cb_on_scan_complete = 0;
   6103 
   6104 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
   6105 		return;
   6106 
   6107 	if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
   6108 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
   6109 			"continued after successful connection");
   6110 		p2p_increase_search_delay(wpa_s->global->p2p,
   6111 					  wpas_p2p_search_delay(wpa_s));
   6112 	}
   6113 }
   6114 
   6115 
   6116 static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
   6117 				     struct wpa_ssid *s, const u8 *addr,
   6118 				     int iface_addr)
   6119 {
   6120 	struct psk_list_entry *psk, *tmp;
   6121 	int changed = 0;
   6122 
   6123 	dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
   6124 			      list) {
   6125 		if ((iface_addr && !psk->p2p &&
   6126 		     os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
   6127 		    (!iface_addr && psk->p2p &&
   6128 		     os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
   6129 			wpa_dbg(wpa_s, MSG_DEBUG,
   6130 				"P2P: Remove persistent group PSK list entry for "
   6131 				MACSTR " p2p=%u",
   6132 				MAC2STR(psk->addr), psk->p2p);
   6133 			dl_list_del(&psk->list);
   6134 			os_free(psk);
   6135 			changed++;
   6136 		}
   6137 	}
   6138 
   6139 	return changed;
   6140 }
   6141 
   6142 
   6143 void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
   6144 			 const u8 *p2p_dev_addr,
   6145 			 const u8 *psk, size_t psk_len)
   6146 {
   6147 	struct wpa_ssid *ssid = wpa_s->current_ssid;
   6148 	struct wpa_ssid *persistent;
   6149 	struct psk_list_entry *p;
   6150 
   6151 	if (psk_len != sizeof(p->psk))
   6152 		return;
   6153 
   6154 	if (p2p_dev_addr) {
   6155 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
   6156 			" p2p_dev_addr=" MACSTR,
   6157 			MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
   6158 		if (is_zero_ether_addr(p2p_dev_addr))
   6159 			p2p_dev_addr = NULL;
   6160 	} else {
   6161 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
   6162 			MAC2STR(mac_addr));
   6163 	}
   6164 
   6165 	if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
   6166 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
   6167 		/* To be added to persistent group once created */
   6168 		if (wpa_s->global->add_psk == NULL) {
   6169 			wpa_s->global->add_psk = os_zalloc(sizeof(*p));
   6170 			if (wpa_s->global->add_psk == NULL)
   6171 				return;
   6172 		}
   6173 		p = wpa_s->global->add_psk;
   6174 		if (p2p_dev_addr) {
   6175 			p->p2p = 1;
   6176 			os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
   6177 		} else {
   6178 			p->p2p = 0;
   6179 			os_memcpy(p->addr, mac_addr, ETH_ALEN);
   6180 		}
   6181 		os_memcpy(p->psk, psk, psk_len);
   6182 		return;
   6183 	}
   6184 
   6185 	if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
   6186 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
   6187 		return;
   6188 	}
   6189 
   6190 	persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
   6191 					     ssid->ssid_len);
   6192 	if (!persistent) {
   6193 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
   6194 		return;
   6195 	}
   6196 
   6197 	p = os_zalloc(sizeof(*p));
   6198 	if (p == NULL)
   6199 		return;
   6200 	if (p2p_dev_addr) {
   6201 		p->p2p = 1;
   6202 		os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
   6203 	} else {
   6204 		p->p2p = 0;
   6205 		os_memcpy(p->addr, mac_addr, ETH_ALEN);
   6206 	}
   6207 	os_memcpy(p->psk, psk, psk_len);
   6208 
   6209 	if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS) {
   6210 		struct psk_list_entry *last;
   6211 		last = dl_list_last(&persistent->psk_list,
   6212 				    struct psk_list_entry, list);
   6213 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
   6214 			MACSTR " (p2p=%u) to make room for a new one",
   6215 			MAC2STR(last->addr), last->p2p);
   6216 		dl_list_del(&last->list);
   6217 		os_free(last);
   6218 	}
   6219 
   6220 	wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
   6221 				  p2p_dev_addr ? p2p_dev_addr : mac_addr,
   6222 				  p2p_dev_addr == NULL);
   6223 	if (p2p_dev_addr) {
   6224 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
   6225 			MACSTR, MAC2STR(p2p_dev_addr));
   6226 	} else {
   6227 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
   6228 			MAC2STR(mac_addr));
   6229 	}
   6230 	dl_list_add(&persistent->psk_list, &p->list);
   6231 
   6232 #ifndef CONFIG_NO_CONFIG_WRITE
   6233 	if (wpa_s->parent->conf->update_config &&
   6234 	    wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
   6235 		wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
   6236 #endif /* CONFIG_NO_CONFIG_WRITE */
   6237 }
   6238 
   6239 
   6240 static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
   6241 				struct wpa_ssid *s, const u8 *addr,
   6242 				int iface_addr)
   6243 {
   6244 	int res;
   6245 
   6246 	res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
   6247 	if (res > 0) {
   6248 #ifndef CONFIG_NO_CONFIG_WRITE
   6249 		if (wpa_s->conf->update_config &&
   6250 		    wpa_config_write(wpa_s->confname, wpa_s->conf))
   6251 			wpa_dbg(wpa_s, MSG_DEBUG,
   6252 				"P2P: Failed to update configuration");
   6253 #endif /* CONFIG_NO_CONFIG_WRITE */
   6254 	}
   6255 }
   6256 
   6257 
   6258 static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
   6259 				      const u8 *peer, int iface_addr)
   6260 {
   6261 	struct hostapd_data *hapd;
   6262 	struct hostapd_wpa_psk *psk, *prev, *rem;
   6263 	struct sta_info *sta;
   6264 
   6265 	if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
   6266 	    wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
   6267 		return;
   6268 
   6269 	/* Remove per-station PSK entry */
   6270 	hapd = wpa_s->ap_iface->bss[0];
   6271 	prev = NULL;
   6272 	psk = hapd->conf->ssid.wpa_psk;
   6273 	while (psk) {
   6274 		if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
   6275 		    (!iface_addr &&
   6276 		     os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
   6277 			wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
   6278 				MACSTR " iface_addr=%d",
   6279 				MAC2STR(peer), iface_addr);
   6280 			if (prev)
   6281 				prev->next = psk->next;
   6282 			else
   6283 				hapd->conf->ssid.wpa_psk = psk->next;
   6284 			rem = psk;
   6285 			psk = psk->next;
   6286 			os_free(rem);
   6287 		} else {
   6288 			prev = psk;
   6289 			psk = psk->next;
   6290 		}
   6291 	}
   6292 
   6293 	/* Disconnect from group */
   6294 	if (iface_addr)
   6295 		sta = ap_get_sta(hapd, peer);
   6296 	else
   6297 		sta = ap_get_sta_p2p(hapd, peer);
   6298 	if (sta) {
   6299 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
   6300 			" (iface_addr=%d) from group",
   6301 			MAC2STR(peer), iface_addr);
   6302 		hostapd_drv_sta_deauth(hapd, sta->addr,
   6303 				       WLAN_REASON_DEAUTH_LEAVING);
   6304 		ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
   6305 	}
   6306 }
   6307 
   6308 
   6309 void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
   6310 			    int iface_addr)
   6311 {
   6312 	struct wpa_ssid *s;
   6313 	struct wpa_supplicant *w;
   6314 
   6315 	wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
   6316 
   6317 	/* Remove from any persistent group */
   6318 	for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
   6319 		if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
   6320 			continue;
   6321 		if (!iface_addr)
   6322 			wpas_remove_persistent_peer(wpa_s, s, peer, 0);
   6323 		wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr);
   6324 	}
   6325 
   6326 	/* Remove from any operating group */
   6327 	for (w = wpa_s->global->ifaces; w; w = w->next)
   6328 		wpas_p2p_remove_client_go(w, peer, iface_addr);
   6329 }
   6330 
   6331 
   6332 static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
   6333 {
   6334 	struct wpa_supplicant *wpa_s = eloop_ctx;
   6335 	wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
   6336 }
   6337 
   6338 
   6339 int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
   6340 {
   6341 	struct wpa_ssid *ssid = wpa_s->current_ssid;
   6342 
   6343 	if (ssid == NULL || !ssid->p2p_group)
   6344 		return 0;
   6345 
   6346 	if (wpa_s->p2p_last_4way_hs_fail &&
   6347 	    wpa_s->p2p_last_4way_hs_fail == ssid) {
   6348 		u8 go_dev_addr[ETH_ALEN];
   6349 		struct wpa_ssid *persistent;
   6350 
   6351 		if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
   6352 					      ssid->ssid,
   6353 					      ssid->ssid_len) <= 0) {
   6354 			wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
   6355 			goto disconnect;
   6356 		}
   6357 
   6358 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
   6359 			MACSTR, MAC2STR(go_dev_addr));
   6360 		persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
   6361 						     ssid->ssid,
   6362 						     ssid->ssid_len);
   6363 		if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
   6364 			wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
   6365 			goto disconnect;
   6366 		}
   6367 		wpa_msg_global(wpa_s->parent, MSG_INFO,
   6368 			       P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
   6369 			       persistent->id);
   6370 	disconnect:
   6371 		wpa_s->p2p_last_4way_hs_fail = NULL;
   6372 		/*
   6373 		 * Remove the group from a timeout to avoid issues with caller
   6374 		 * continuing to use the interface if this is on a P2P group
   6375 		 * interface.
   6376 		 */
   6377 		eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
   6378 				       wpa_s, NULL);
   6379 		return 1;
   6380 	}
   6381 
   6382 	wpa_s->p2p_last_4way_hs_fail = ssid;
   6383 	return 0;
   6384 }
   6385 
   6386 #ifdef ANDROID_P2P
   6387 static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
   6388 {
   6389 	struct wpa_supplicant *wpa_s = eloop_ctx;
   6390 
   6391 	wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
   6392 	wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
   6393 }
   6394 
   6395 int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
   6396 	struct wpa_ssid *ssid)
   6397 {
   6398 	struct wpa_supplicant *iface = NULL;
   6399 	struct p2p_data *p2p = wpa_s->global->p2p;
   6400 
   6401 	for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
   6402 		if ((iface->current_ssid) &&
   6403 		    (iface->current_ssid->frequency != freq) &&
   6404 		    ((iface->p2p_group_interface) ||
   6405 		     (iface->current_ssid->p2p_group))) {
   6406 
   6407 			if ((iface->p2p_group_interface == P2P_GROUP_INTERFACE_GO)  ||
   6408 			    (iface->current_ssid->mode == WPAS_MODE_P2P_GO)) {
   6409 				/* Try to see whether we can move the GO. If it
   6410 				 * is not possible, remove the GO interface
   6411 				 */
   6412 				if (wpa_drv_switch_channel(iface, freq) == 0) {
   6413 					wpa_printf(MSG_ERROR, "P2P: GO Moved to freq(%d)", freq);
   6414 					iface->current_ssid->frequency = freq;
   6415 					continue;
   6416 				}
   6417 			}
   6418 
   6419 			/* If GO cannot be moved or if the conflicting interface is a
   6420 			 * P2P Client, remove the interface depending up on the connection
   6421 			 * priority */
   6422 			if(!wpas_is_p2p_prioritized(iface)) {
   6423 				/* STA connection has priority over existing
   6424 				 * P2P connection. So remove the interface */
   6425 				wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to Single channel"
   6426 						"concurrent mode frequency conflict");
   6427 				eloop_register_timeout(0, 0, wpas_p2p_group_freq_conflict,
   6428 						       iface, NULL);
   6429 				/* If connection in progress is p2p connection, do not proceed for the connection */
   6430 				if (wpa_s == iface)
   6431 					return -1;
   6432 				else
   6433 					/* If connection in progress is STA connection, proceed for the connection */
   6434 					return 0;
   6435 			} else {
   6436 				/* P2p connection has priority, disable the STA network*/
   6437 				wpa_supplicant_disable_network(wpa_s->global->ifaces, ssid);
   6438 				wpa_msg(wpa_s->global->ifaces, MSG_INFO, WPA_EVENT_FREQ_CONFLICT
   6439 					" id=%d", ssid->id);
   6440 				os_memset(wpa_s->global->ifaces->pending_bssid, 0, ETH_ALEN);
   6441 				if (wpa_s == iface) {
   6442 					/* p2p connection is in progress, continue connecting...*/
   6443 					return 0;
   6444 				}
   6445 				else {
   6446 					/* STA connection is in progress, do not allow to continue */
   6447 					return -1;
   6448 				}
   6449 			}
   6450 		}
   6451 	}
   6452 	return 0;
   6453 }
   6454 #endif
   6455