Home | History | Annotate | Download | only in wpa_supplicant
      1 /*
      2  * wpa_supplicant - P2P
      3  * Copyright (c) 2009-2010, Atheros Communications
      4  * Copyright (c) 2010-2014, Jouni Malinen <j (at) w1.fi>
      5  *
      6  * This software may be distributed under the terms of the BSD license.
      7  * See README for more details.
      8  */
      9 
     10 #include "includes.h"
     11 
     12 #include "common.h"
     13 #include "eloop.h"
     14 #include "common/ieee802_11_common.h"
     15 #include "common/ieee802_11_defs.h"
     16 #include "common/wpa_ctrl.h"
     17 #include "wps/wps_i.h"
     18 #include "p2p/p2p.h"
     19 #include "ap/hostapd.h"
     20 #include "ap/ap_config.h"
     21 #include "ap/sta_info.h"
     22 #include "ap/ap_drv_ops.h"
     23 #include "ap/wps_hostapd.h"
     24 #include "ap/p2p_hostapd.h"
     25 #include "ap/dfs.h"
     26 #include "eapol_supp/eapol_supp_sm.h"
     27 #include "rsn_supp/wpa.h"
     28 #include "wpa_supplicant_i.h"
     29 #include "driver_i.h"
     30 #include "ap.h"
     31 #include "config_ssid.h"
     32 #include "config.h"
     33 #include "notify.h"
     34 #include "scan.h"
     35 #include "bss.h"
     36 #include "offchannel.h"
     37 #include "wps_supplicant.h"
     38 #include "p2p_supplicant.h"
     39 #include "wifi_display.h"
     40 
     41 
     42 /*
     43  * How many times to try to scan to find the GO before giving up on join
     44  * request.
     45  */
     46 #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
     47 
     48 #define P2P_AUTO_PD_SCAN_ATTEMPTS 5
     49 
     50 /**
     51  * Defines time interval in seconds when a GO needs to evacuate a frequency that
     52  * it is currently using, but is no longer valid for P2P use cases.
     53  */
     54 #define P2P_GO_FREQ_CHANGE_TIME 5
     55 
     56 /**
     57  * Defines CSA parameters which are used when GO evacuates the no longer valid
     58  * channel (and if the driver supports channel switch).
     59  */
     60 #define P2P_GO_CSA_COUNT 7
     61 #define P2P_GO_CSA_BLOCK_TX 0
     62 
     63 #ifndef P2P_MAX_CLIENT_IDLE
     64 /*
     65  * How many seconds to try to reconnect to the GO when connection in P2P client
     66  * role has been lost.
     67  */
     68 #define P2P_MAX_CLIENT_IDLE 10
     69 #endif /* P2P_MAX_CLIENT_IDLE */
     70 
     71 #ifndef P2P_MAX_INITIAL_CONN_WAIT
     72 /*
     73  * How many seconds to wait for initial 4-way handshake to get completed after
     74  * WPS provisioning step or after the re-invocation of a persistent group on a
     75  * P2P Client.
     76  */
     77 #define P2P_MAX_INITIAL_CONN_WAIT 10
     78 #endif /* P2P_MAX_INITIAL_CONN_WAIT */
     79 
     80 #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
     81 /*
     82  * How many seconds to wait for initial 4-way handshake to get completed after
     83  * WPS provisioning step on the GO. This controls the extra time the P2P
     84  * operation is considered to be in progress (e.g., to delay other scans) after
     85  * WPS provisioning has been completed on the GO during group formation.
     86  */
     87 #define P2P_MAX_INITIAL_CONN_WAIT_GO 10
     88 #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
     89 
     90 #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
     91 /*
     92  * How many seconds to wait for initial 4-way handshake to get completed after
     93  * re-invocation of a persistent group on the GO when the client is expected
     94  * to connect automatically (no user interaction).
     95  */
     96 #define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
     97 #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
     98 
     99 #define P2P_MGMT_DEVICE_PREFIX		"p2p-dev-"
    100 
    101 /*
    102  * How many seconds to wait to re-attempt to move GOs, in case previous attempt
    103  * was not possible.
    104  */
    105 #define P2P_RECONSIDER_GO_MOVE_DELAY 30
    106 
    107 enum p2p_group_removal_reason {
    108 	P2P_GROUP_REMOVAL_UNKNOWN,
    109 	P2P_GROUP_REMOVAL_SILENT,
    110 	P2P_GROUP_REMOVAL_FORMATION_FAILED,
    111 	P2P_GROUP_REMOVAL_REQUESTED,
    112 	P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
    113 	P2P_GROUP_REMOVAL_UNAVAILABLE,
    114 	P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
    115 	P2P_GROUP_REMOVAL_PSK_FAILURE,
    116 	P2P_GROUP_REMOVAL_FREQ_CONFLICT,
    117 	P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL
    118 };
    119 
    120 
    121 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
    122 static struct wpa_supplicant *
    123 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
    124 			 int go);
    125 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
    126 			       const u8 *ssid, size_t ssid_len);
    127 static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
    128 				int *force_freq, int *pref_freq, int go,
    129 				unsigned int *pref_freq_list,
    130 				unsigned int *num_pref_freq);
    131 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
    132 				   const u8 *ssid, size_t ssid_len);
    133 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
    134 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
    135 			 const u8 *dev_addr, enum p2p_wps_method wps_method,
    136 			 int auto_join, int freq,
    137 			 const u8 *ssid, size_t ssid_len);
    138 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
    139 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
    140 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
    141 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
    142 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
    143 					     void *timeout_ctx);
    144 static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
    145 static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
    146 				       int group_added);
    147 static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
    148 static void wpas_stop_listen(void *ctx);
    149 static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx);
    150 static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s);
    151 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
    152 					enum wpa_driver_if_type type);
    153 static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
    154 					    int already_deleted);
    155 static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
    156 					     struct wpa_used_freq_data *freqs,
    157 					     unsigned int num);
    158 static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx);
    159 static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq);
    160 static void
    161 wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
    162 			     struct wpa_used_freq_data *freqs, unsigned int num,
    163 			     enum wpas_p2p_channel_update_trig trig);
    164 static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx);
    165 
    166 
    167 /*
    168  * Get the number of concurrent channels that the HW can operate, but that are
    169  * currently not in use by any of the wpa_supplicant interfaces.
    170  */
    171 static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
    172 {
    173 	int *freqs;
    174 	int num, unused;
    175 
    176 	freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
    177 	if (!freqs)
    178 		return -1;
    179 
    180 	num = get_shared_radio_freqs(wpa_s, freqs,
    181 				     wpa_s->num_multichan_concurrent);
    182 	os_free(freqs);
    183 
    184 	unused = wpa_s->num_multichan_concurrent - num;
    185 	wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
    186 	return unused;
    187 }
    188 
    189 
    190 /*
    191  * Get the frequencies that are currently in use by one or more of the virtual
    192  * interfaces, and that are also valid for P2P operation.
    193  */
    194 static unsigned int
    195 wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
    196 			  struct wpa_used_freq_data *p2p_freqs,
    197 			  unsigned int len)
    198 {
    199 	struct wpa_used_freq_data *freqs;
    200 	unsigned int num, i, j;
    201 
    202 	freqs = os_calloc(wpa_s->num_multichan_concurrent,
    203 			  sizeof(struct wpa_used_freq_data));
    204 	if (!freqs)
    205 		return 0;
    206 
    207 	num = get_shared_radio_freqs_data(wpa_s, freqs,
    208 					  wpa_s->num_multichan_concurrent);
    209 
    210 	os_memset(p2p_freqs, 0, sizeof(struct wpa_used_freq_data) * len);
    211 
    212 	for (i = 0, j = 0; i < num && j < len; i++) {
    213 		if (p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
    214 			p2p_freqs[j++] = freqs[i];
    215 	}
    216 
    217 	os_free(freqs);
    218 
    219 	dump_freq_data(wpa_s, "valid for P2P", p2p_freqs, j);
    220 
    221 	return j;
    222 }
    223 
    224 
    225 static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
    226 					     int freq)
    227 {
    228 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
    229 		return;
    230 
    231 	/* Use the wpa_s used to control the P2P Device operation */
    232 	wpa_s = wpa_s->global->p2p_init_wpa_s;
    233 
    234 	if (wpa_s->conf->p2p_ignore_shared_freq &&
    235 	    freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
    236 	    wpas_p2p_num_unused_channels(wpa_s) > 0) {
    237 		wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
    238 			   freq);
    239 		freq = 0;
    240 	}
    241 	p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
    242 }
    243 
    244 
    245 static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
    246 				      struct wpa_scan_results *scan_res)
    247 {
    248 	size_t i;
    249 
    250 	if (wpa_s->p2p_scan_work) {
    251 		struct wpa_radio_work *work = wpa_s->p2p_scan_work;
    252 		wpa_s->p2p_scan_work = NULL;
    253 		radio_work_done(work);
    254 	}
    255 
    256 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
    257 		return;
    258 
    259 	wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
    260 		   (int) scan_res->num);
    261 
    262 	for (i = 0; i < scan_res->num; i++) {
    263 		struct wpa_scan_res *bss = scan_res->res[i];
    264 		struct os_reltime time_tmp_age, entry_ts;
    265 		const u8 *ies;
    266 		size_t ies_len;
    267 
    268 		time_tmp_age.sec = bss->age / 1000;
    269 		time_tmp_age.usec = (bss->age % 1000) * 1000;
    270 		os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
    271 
    272 		ies = (const u8 *) (bss + 1);
    273 		ies_len = bss->ie_len;
    274 		if (bss->beacon_ie_len > 0 &&
    275 		    !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
    276 		    wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
    277 			wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
    278 				   MACSTR, MAC2STR(bss->bssid));
    279 			ies = ies + ies_len;
    280 			ies_len = bss->beacon_ie_len;
    281 		}
    282 
    283 
    284 		if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
    285 					 bss->freq, &entry_ts, bss->level,
    286 					 ies, ies_len) > 0)
    287 			break;
    288 	}
    289 
    290 	p2p_scan_res_handled(wpa_s->global->p2p);
    291 }
    292 
    293 
    294 static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
    295 {
    296 	struct wpa_supplicant *wpa_s = work->wpa_s;
    297 	struct wpa_driver_scan_params *params = work->ctx;
    298 	int ret;
    299 
    300 	if (deinit) {
    301 		if (!work->started) {
    302 			wpa_scan_free_params(params);
    303 			return;
    304 		}
    305 
    306 		wpa_s->p2p_scan_work = NULL;
    307 		return;
    308 	}
    309 
    310 	if (wpa_s->clear_driver_scan_cache) {
    311 		wpa_printf(MSG_DEBUG,
    312 			   "Request driver to clear scan cache due to local BSS flush");
    313 		params->only_new_results = 1;
    314 	}
    315 	ret = wpa_drv_scan(wpa_s, params);
    316 	if (ret == 0)
    317 		wpa_s->curr_scan_cookie = params->scan_cookie;
    318 	wpa_scan_free_params(params);
    319 	work->ctx = NULL;
    320 	if (ret) {
    321 		radio_work_done(work);
    322 		p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
    323 		return;
    324 	}
    325 
    326 	p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
    327 	os_get_reltime(&wpa_s->scan_trigger_time);
    328 	wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
    329 	wpa_s->own_scan_requested = 1;
    330 	wpa_s->clear_driver_scan_cache = 0;
    331 	wpa_s->p2p_scan_work = work;
    332 }
    333 
    334 
    335 static int wpas_p2p_search_social_channel(struct wpa_supplicant *wpa_s,
    336 					  int freq)
    337 {
    338 	if (wpa_s->global->p2p_24ghz_social_channels &&
    339 	    (freq == 2412 || freq == 2437 || freq == 2462)) {
    340 		/*
    341 		 * Search all social channels regardless of whether these have
    342 		 * been disabled for P2P operating channel use to avoid missing
    343 		 * peers.
    344 		 */
    345 		return 1;
    346 	}
    347 	return p2p_supported_freq(wpa_s->global->p2p, freq);
    348 }
    349 
    350 
    351 static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
    352 			 unsigned int num_req_dev_types,
    353 			 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
    354 {
    355 	struct wpa_supplicant *wpa_s = ctx;
    356 	struct wpa_driver_scan_params *params = NULL;
    357 	struct wpabuf *wps_ie, *ies;
    358 	unsigned int num_channels = 0;
    359 	int social_channels_freq[] = { 2412, 2437, 2462, 60480 };
    360 	size_t ielen;
    361 	u8 *n, i;
    362 	unsigned int bands;
    363 
    364 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
    365 		return -1;
    366 
    367 	if (wpa_s->p2p_scan_work) {
    368 		wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
    369 		return -1;
    370 	}
    371 
    372 	params = os_zalloc(sizeof(*params));
    373 	if (params == NULL)
    374 		return -1;
    375 
    376 	/* P2P Wildcard SSID */
    377 	params->num_ssids = 1;
    378 	n = os_malloc(P2P_WILDCARD_SSID_LEN);
    379 	if (n == NULL)
    380 		goto fail;
    381 	os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
    382 	params->ssids[0].ssid = n;
    383 	params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
    384 
    385 	wpa_s->wps->dev.p2p = 1;
    386 	wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
    387 					wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
    388 					num_req_dev_types, req_dev_types);
    389 	if (wps_ie == NULL)
    390 		goto fail;
    391 
    392 	switch (type) {
    393 	case P2P_SCAN_SOCIAL:
    394 		params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 1,
    395 					  sizeof(int));
    396 		if (params->freqs == NULL)
    397 			goto fail;
    398 		for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
    399 			if (wpas_p2p_search_social_channel(
    400 				    wpa_s, social_channels_freq[i]))
    401 				params->freqs[num_channels++] =
    402 					social_channels_freq[i];
    403 		}
    404 		params->freqs[num_channels++] = 0;
    405 		break;
    406 	case P2P_SCAN_FULL:
    407 		break;
    408 	case P2P_SCAN_SPECIFIC:
    409 		params->freqs = os_calloc(2, sizeof(int));
    410 		if (params->freqs == NULL)
    411 			goto fail;
    412 		params->freqs[0] = freq;
    413 		params->freqs[1] = 0;
    414 		break;
    415 	case P2P_SCAN_SOCIAL_PLUS_ONE:
    416 		params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 2,
    417 					  sizeof(int));
    418 		if (params->freqs == NULL)
    419 			goto fail;
    420 		for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
    421 			if (wpas_p2p_search_social_channel(
    422 				    wpa_s, social_channels_freq[i]))
    423 				params->freqs[num_channels++] =
    424 					social_channels_freq[i];
    425 		}
    426 		if (p2p_supported_freq(wpa_s->global->p2p, freq))
    427 			params->freqs[num_channels++] = freq;
    428 		params->freqs[num_channels++] = 0;
    429 		break;
    430 	}
    431 
    432 	ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
    433 	ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
    434 	if (ies == NULL) {
    435 		wpabuf_free(wps_ie);
    436 		goto fail;
    437 	}
    438 	wpabuf_put_buf(ies, wps_ie);
    439 	wpabuf_free(wps_ie);
    440 
    441 	bands = wpas_get_bands(wpa_s, params->freqs);
    442 	p2p_scan_ie(wpa_s->global->p2p, ies, dev_id, bands);
    443 
    444 	params->p2p_probe = 1;
    445 	n = os_malloc(wpabuf_len(ies));
    446 	if (n == NULL) {
    447 		wpabuf_free(ies);
    448 		goto fail;
    449 	}
    450 	os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
    451 	params->extra_ies = n;
    452 	params->extra_ies_len = wpabuf_len(ies);
    453 	wpabuf_free(ies);
    454 
    455 	radio_remove_works(wpa_s, "p2p-scan", 0);
    456 	if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
    457 			   params) < 0)
    458 		goto fail;
    459 	return 0;
    460 
    461 fail:
    462 	wpa_scan_free_params(params);
    463 	return -1;
    464 }
    465 
    466 
    467 static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
    468 {
    469 	switch (p2p_group_interface) {
    470 	case P2P_GROUP_INTERFACE_PENDING:
    471 		return WPA_IF_P2P_GROUP;
    472 	case P2P_GROUP_INTERFACE_GO:
    473 		return WPA_IF_P2P_GO;
    474 	case P2P_GROUP_INTERFACE_CLIENT:
    475 		return WPA_IF_P2P_CLIENT;
    476 	}
    477 
    478 	return WPA_IF_P2P_GROUP;
    479 }
    480 
    481 
    482 static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
    483 						  const u8 *ssid,
    484 						  size_t ssid_len, int *go)
    485 {
    486 	struct wpa_ssid *s;
    487 
    488 	for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
    489 		for (s = wpa_s->conf->ssid; s; s = s->next) {
    490 			if (s->disabled != 0 || !s->p2p_group ||
    491 			    s->ssid_len != ssid_len ||
    492 			    os_memcmp(ssid, s->ssid, ssid_len) != 0)
    493 				continue;
    494 			if (s->mode == WPAS_MODE_P2P_GO &&
    495 			    s != wpa_s->current_ssid)
    496 				continue;
    497 			if (go)
    498 				*go = s->mode == WPAS_MODE_P2P_GO;
    499 			return wpa_s;
    500 		}
    501 	}
    502 
    503 	return NULL;
    504 }
    505 
    506 
    507 static void run_wpas_p2p_disconnect(void *eloop_ctx, void *timeout_ctx)
    508 {
    509 	struct wpa_supplicant *wpa_s = eloop_ctx;
    510 	wpa_printf(MSG_DEBUG,
    511 		   "P2P: Complete previously requested removal of %s",
    512 		   wpa_s->ifname);
    513 	wpas_p2p_disconnect(wpa_s);
    514 }
    515 
    516 
    517 static int wpas_p2p_disconnect_safely(struct wpa_supplicant *wpa_s,
    518 				      struct wpa_supplicant *calling_wpa_s)
    519 {
    520 	if (calling_wpa_s == wpa_s && wpa_s &&
    521 	    wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
    522 		/*
    523 		 * The calling wpa_s instance is going to be removed. Do that
    524 		 * from an eloop callback to keep the instance available until
    525 		 * the caller has returned. This my be needed, e.g., to provide
    526 		 * control interface responses on the per-interface socket.
    527 		 */
    528 		if (eloop_register_timeout(0, 0, run_wpas_p2p_disconnect,
    529 					   wpa_s, NULL) < 0)
    530 			return -1;
    531 		return 0;
    532 	}
    533 
    534 	return wpas_p2p_disconnect(wpa_s);
    535 }
    536 
    537 
    538 /* Determine total number of clients in active groups where we are the GO */
    539 static unsigned int p2p_group_go_member_count(struct wpa_supplicant *wpa_s)
    540 {
    541 	unsigned int count = 0;
    542 	struct wpa_ssid *s;
    543 
    544 	for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
    545 		for (s = wpa_s->conf->ssid; s; s = s->next) {
    546 			wpa_printf(MSG_DEBUG,
    547 				   "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
    548 				   wpa_s, s, s->disabled, s->p2p_group,
    549 				   s->mode);
    550 			if (!s->disabled && s->p2p_group &&
    551 			    s->mode == WPAS_MODE_P2P_GO) {
    552 				count += p2p_get_group_num_members(
    553 					wpa_s->p2p_group);
    554 			}
    555 		}
    556 	}
    557 
    558 	return count;
    559 }
    560 
    561 
    562 static unsigned int p2p_is_active_persistent_group(struct wpa_supplicant *wpa_s)
    563 {
    564 	return !wpa_s->p2p_mgmt && wpa_s->current_ssid &&
    565 		!wpa_s->current_ssid->disabled &&
    566 		wpa_s->current_ssid->p2p_group &&
    567 		wpa_s->current_ssid->p2p_persistent_group;
    568 }
    569 
    570 
    571 static unsigned int p2p_is_active_persistent_go(struct wpa_supplicant *wpa_s)
    572 {
    573 	return p2p_is_active_persistent_group(wpa_s) &&
    574 		wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO;
    575 }
    576 
    577 
    578 /* Find an interface for a P2P group where we are the GO */
    579 static struct wpa_supplicant *
    580 wpas_p2p_get_go_group(struct wpa_supplicant *wpa_s)
    581 {
    582 	struct wpa_supplicant *save = NULL;
    583 
    584 	if (!wpa_s)
    585 		return NULL;
    586 
    587 	for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
    588 		if (!p2p_is_active_persistent_go(wpa_s))
    589 			continue;
    590 
    591 		/* Prefer a group with connected clients */
    592 		if (p2p_get_group_num_members(wpa_s->p2p_group))
    593 			return wpa_s;
    594 		save = wpa_s;
    595 	}
    596 
    597 	/* No group with connected clients, so pick the one without (if any) */
    598 	return save;
    599 }
    600 
    601 
    602 static unsigned int p2p_is_active_persistent_cli(struct wpa_supplicant *wpa_s)
    603 {
    604 	return p2p_is_active_persistent_group(wpa_s) &&
    605 		wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
    606 }
    607 
    608 
    609 /* Find an interface for a P2P group where we are the P2P Client */
    610 static struct wpa_supplicant *
    611 wpas_p2p_get_cli_group(struct wpa_supplicant *wpa_s)
    612 {
    613 	for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
    614 		if (p2p_is_active_persistent_cli(wpa_s))
    615 			return wpa_s;
    616 	}
    617 
    618 	return NULL;
    619 }
    620 
    621 
    622 /* Find a persistent group where we are the GO */
    623 static struct wpa_ssid *
    624 wpas_p2p_get_persistent_go(struct wpa_supplicant *wpa_s)
    625 {
    626 	struct wpa_ssid *s;
    627 
    628 	for (s = wpa_s->conf->ssid; s; s = s->next) {
    629 		if (s->disabled == 2 && s->mode == WPAS_MODE_P2P_GO)
    630 			return s;
    631 	}
    632 
    633 	return NULL;
    634 }
    635 
    636 
    637 static u8 p2ps_group_capability(void *ctx, u8 incoming, u8 role,
    638 				unsigned int *force_freq,
    639 				unsigned int *pref_freq)
    640 {
    641 	struct wpa_supplicant *wpa_s = ctx;
    642 	struct wpa_ssid *s;
    643 	u8 conncap = P2PS_SETUP_NONE;
    644 	unsigned int owned_members = 0;
    645 	struct wpa_supplicant *go_wpa_s, *cli_wpa_s;
    646 	struct wpa_ssid *persistent_go;
    647 	int p2p_no_group_iface;
    648 	unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
    649 
    650 	wpa_printf(MSG_DEBUG, "P2P: Conncap - in:%d role:%d", incoming, role);
    651 
    652 	if (force_freq)
    653 		*force_freq = 0;
    654 	if (pref_freq)
    655 		*pref_freq = 0;
    656 
    657 	size = P2P_MAX_PREF_CHANNELS;
    658 	if (force_freq && pref_freq &&
    659 	    !wpas_p2p_setup_freqs(wpa_s, 0, (int *) force_freq,
    660 				  (int *) pref_freq, 0, pref_freq_list, &size))
    661 		wpas_p2p_set_own_freq_preference(wpa_s,
    662 						 *force_freq ? *force_freq :
    663 						 *pref_freq);
    664 
    665 	/*
    666 	 * For non-concurrent capable devices:
    667 	 * If persistent_go, then no new.
    668 	 * If GO, then no client.
    669 	 * If client, then no GO.
    670 	 */
    671 	go_wpa_s = wpas_p2p_get_go_group(wpa_s);
    672 	if (go_wpa_s)
    673 		owned_members = p2p_get_group_num_members(go_wpa_s->p2p_group);
    674 	persistent_go = wpas_p2p_get_persistent_go(wpa_s);
    675 	p2p_no_group_iface = !wpas_p2p_create_iface(wpa_s);
    676 	cli_wpa_s = wpas_p2p_get_cli_group(wpa_s);
    677 
    678 	wpa_printf(MSG_DEBUG,
    679 		   "P2P: GO(iface)=%p members=%u CLI(iface)=%p persistent(ssid)=%p",
    680 		   go_wpa_s, owned_members, cli_wpa_s, persistent_go);
    681 
    682 	/* If not concurrent, restrict our choices */
    683 	if (p2p_no_group_iface) {
    684 		wpa_printf(MSG_DEBUG, "P2P: p2p_no_group_iface");
    685 
    686 		if (cli_wpa_s)
    687 			return P2PS_SETUP_NONE;
    688 
    689 		if (go_wpa_s) {
    690 			if (role == P2PS_SETUP_CLIENT ||
    691 			    incoming == P2PS_SETUP_GROUP_OWNER ||
    692 			    p2p_client_limit_reached(go_wpa_s->p2p_group))
    693 				return P2PS_SETUP_NONE;
    694 
    695 			return P2PS_SETUP_GROUP_OWNER;
    696 		}
    697 
    698 		if (persistent_go) {
    699 			if (role == P2PS_SETUP_NONE || role == P2PS_SETUP_NEW) {
    700 				if (!incoming)
    701 					return P2PS_SETUP_GROUP_OWNER |
    702 						P2PS_SETUP_CLIENT;
    703 				if (incoming == P2PS_SETUP_NEW) {
    704 					u8 r;
    705 
    706 					if (os_get_random(&r, sizeof(r)) < 0 ||
    707 					    (r & 1))
    708 						return P2PS_SETUP_CLIENT;
    709 					return P2PS_SETUP_GROUP_OWNER;
    710 				}
    711 			}
    712 		}
    713 	}
    714 
    715 	/* If a required role has been specified, handle it here */
    716 	if (role && role != P2PS_SETUP_NEW) {
    717 		switch (incoming) {
    718 		case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
    719 		case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
    720 			/*
    721 			 * Peer has an active GO, so if the role allows it and
    722 			 * we do not have any active roles, become client.
    723 			 */
    724 			if ((role & P2PS_SETUP_CLIENT) && !go_wpa_s &&
    725 			    !cli_wpa_s)
    726 				return P2PS_SETUP_CLIENT;
    727 
    728 			/* fall through */
    729 
    730 		case P2PS_SETUP_NONE:
    731 		case P2PS_SETUP_NEW:
    732 			conncap = role;
    733 			goto grp_owner;
    734 
    735 		case P2PS_SETUP_GROUP_OWNER:
    736 			/*
    737 			 * Must be a complimentary role - cannot be a client to
    738 			 * more than one peer.
    739 			 */
    740 			if (incoming == role || cli_wpa_s)
    741 				return P2PS_SETUP_NONE;
    742 
    743 			return P2PS_SETUP_CLIENT;
    744 
    745 		case P2PS_SETUP_CLIENT:
    746 			/* Must be a complimentary role */
    747 			if (incoming != role) {
    748 				conncap = P2PS_SETUP_GROUP_OWNER;
    749 				goto grp_owner;
    750 			}
    751 
    752 		default:
    753 			return P2PS_SETUP_NONE;
    754 		}
    755 	}
    756 
    757 	/*
    758 	 * For now, we only will support ownership of one group, and being a
    759 	 * client of one group. Therefore, if we have either an existing GO
    760 	 * group, or an existing client group, we will not do a new GO
    761 	 * negotiation, but rather try to re-use the existing groups.
    762 	 */
    763 	switch (incoming) {
    764 	case P2PS_SETUP_NONE:
    765 	case P2PS_SETUP_NEW:
    766 		if (cli_wpa_s)
    767 			conncap = P2PS_SETUP_GROUP_OWNER;
    768 		else if (!owned_members)
    769 			conncap = P2PS_SETUP_NEW;
    770 		else if (incoming == P2PS_SETUP_NONE)
    771 			conncap = P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT;
    772 		else
    773 			conncap = P2PS_SETUP_CLIENT;
    774 		break;
    775 
    776 	case P2PS_SETUP_CLIENT:
    777 		conncap = P2PS_SETUP_GROUP_OWNER;
    778 		break;
    779 
    780 	case P2PS_SETUP_GROUP_OWNER:
    781 		if (!cli_wpa_s)
    782 			conncap = P2PS_SETUP_CLIENT;
    783 		break;
    784 
    785 	case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
    786 	case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
    787 		if (cli_wpa_s)
    788 			conncap = P2PS_SETUP_GROUP_OWNER;
    789 		else {
    790 			u8 r;
    791 
    792 			if (os_get_random(&r, sizeof(r)) < 0 ||
    793 			    (r & 1))
    794 				conncap = P2PS_SETUP_CLIENT;
    795 			else
    796 				conncap = P2PS_SETUP_GROUP_OWNER;
    797 		}
    798 		break;
    799 
    800 	default:
    801 		return P2PS_SETUP_NONE;
    802 	}
    803 
    804 grp_owner:
    805 	if ((conncap & P2PS_SETUP_GROUP_OWNER) ||
    806 	    (!incoming && (conncap & P2PS_SETUP_NEW))) {
    807 		if (go_wpa_s && p2p_client_limit_reached(go_wpa_s->p2p_group))
    808 			conncap &= ~P2PS_SETUP_GROUP_OWNER;
    809 
    810 		s = wpas_p2p_get_persistent_go(wpa_s);
    811 		if (!s && !go_wpa_s && p2p_no_group_iface) {
    812 			p2p_set_intended_addr(wpa_s->global->p2p,
    813 					      wpa_s->p2p_mgmt ?
    814 					      wpa_s->parent->own_addr :
    815 					      wpa_s->own_addr);
    816 		} else if (!s && !go_wpa_s) {
    817 			if (wpas_p2p_add_group_interface(wpa_s,
    818 							 WPA_IF_P2P_GROUP) < 0) {
    819 				wpa_printf(MSG_ERROR,
    820 					   "P2P: Failed to allocate a new interface for the group");
    821 				return P2PS_SETUP_NONE;
    822 			}
    823 			wpa_s->global->pending_group_iface_for_p2ps = 1;
    824 			p2p_set_intended_addr(wpa_s->global->p2p,
    825 					      wpa_s->pending_interface_addr);
    826 		}
    827 	}
    828 
    829 	return conncap;
    830 }
    831 
    832 
    833 static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
    834 				 enum p2p_group_removal_reason removal_reason)
    835 {
    836 	struct wpa_ssid *ssid;
    837 	char *gtype;
    838 	const char *reason;
    839 
    840 	ssid = wpa_s->current_ssid;
    841 	if (ssid == NULL) {
    842 		/*
    843 		 * The current SSID was not known, but there may still be a
    844 		 * pending P2P group interface waiting for provisioning or a
    845 		 * P2P group that is trying to reconnect.
    846 		 */
    847 		ssid = wpa_s->conf->ssid;
    848 		while (ssid) {
    849 			if (ssid->p2p_group && ssid->disabled != 2)
    850 				break;
    851 			ssid = ssid->next;
    852 		}
    853 		if (ssid == NULL &&
    854 			wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
    855 		{
    856 			wpa_printf(MSG_ERROR, "P2P: P2P group interface "
    857 				   "not found");
    858 			return -1;
    859 		}
    860 	}
    861 	if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
    862 		gtype = "GO";
    863 	else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
    864 		 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
    865 		wpa_s->reassociate = 0;
    866 		wpa_s->disconnected = 1;
    867 		gtype = "client";
    868 	} else
    869 		gtype = "GO";
    870 
    871 	if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
    872 		wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
    873 
    874 	if (os_strcmp(gtype, "client") == 0) {
    875 		wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
    876 		if (eloop_is_timeout_registered(wpas_p2p_psk_failure_removal,
    877 						wpa_s, NULL)) {
    878 			wpa_printf(MSG_DEBUG,
    879 				   "P2P: PSK failure removal was scheduled, so use PSK failure as reason for group removal");
    880 			removal_reason = P2P_GROUP_REMOVAL_PSK_FAILURE;
    881 			eloop_cancel_timeout(wpas_p2p_psk_failure_removal,
    882 					     wpa_s, NULL);
    883 		}
    884 	}
    885 
    886 	if (wpa_s->cross_connect_in_use) {
    887 		wpa_s->cross_connect_in_use = 0;
    888 		wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
    889 			       P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
    890 			       wpa_s->ifname, wpa_s->cross_connect_uplink);
    891 	}
    892 	switch (removal_reason) {
    893 	case P2P_GROUP_REMOVAL_REQUESTED:
    894 		reason = " reason=REQUESTED";
    895 		break;
    896 	case P2P_GROUP_REMOVAL_FORMATION_FAILED:
    897 		reason = " reason=FORMATION_FAILED";
    898 		break;
    899 	case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
    900 		reason = " reason=IDLE";
    901 		break;
    902 	case P2P_GROUP_REMOVAL_UNAVAILABLE:
    903 		reason = " reason=UNAVAILABLE";
    904 		break;
    905 	case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
    906 		reason = " reason=GO_ENDING_SESSION";
    907 		break;
    908 	case P2P_GROUP_REMOVAL_PSK_FAILURE:
    909 		reason = " reason=PSK_FAILURE";
    910 		break;
    911 	case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
    912 		reason = " reason=FREQ_CONFLICT";
    913 		break;
    914 	default:
    915 		reason = "";
    916 		break;
    917 	}
    918 	if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
    919 		wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
    920 			       P2P_EVENT_GROUP_REMOVED "%s %s%s",
    921 			       wpa_s->ifname, gtype, reason);
    922 	}
    923 
    924 	if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
    925 		wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
    926 	if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
    927 		wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
    928 	if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
    929 				 wpa_s->p2pdev, NULL) > 0) {
    930 		wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
    931 			   "timeout");
    932 		wpa_s->p2p_in_provisioning = 0;
    933 		wpas_p2p_group_formation_failed(wpa_s, 1);
    934 	}
    935 
    936 	wpa_s->p2p_in_invitation = 0;
    937 	eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
    938 	eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL);
    939 
    940 	/*
    941 	 * Make sure wait for the first client does not remain active after the
    942 	 * group has been removed.
    943 	 */
    944 	wpa_s->global->p2p_go_wait_client.sec = 0;
    945 
    946 	if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
    947 		struct wpa_global *global;
    948 		char *ifname;
    949 		enum wpa_driver_if_type type;
    950 		wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
    951 			wpa_s->ifname);
    952 		global = wpa_s->global;
    953 		ifname = os_strdup(wpa_s->ifname);
    954 		type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
    955 		eloop_cancel_timeout(run_wpas_p2p_disconnect, wpa_s, NULL);
    956 		wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
    957 		wpa_s = global->ifaces;
    958 		if (wpa_s && ifname)
    959 			wpa_drv_if_remove(wpa_s, type, ifname);
    960 		os_free(ifname);
    961 		return 1;
    962 	}
    963 
    964 	/*
    965 	 * The primary interface was used for P2P group operations, so
    966 	 * need to reset its p2pdev.
    967 	 */
    968 	wpa_s->p2pdev = wpa_s->parent;
    969 
    970 	if (!wpa_s->p2p_go_group_formation_completed) {
    971 		wpa_s->global->p2p_group_formation = NULL;
    972 		wpa_s->p2p_in_provisioning = 0;
    973 	}
    974 
    975 	wpa_s->show_group_started = 0;
    976 	os_free(wpa_s->go_params);
    977 	wpa_s->go_params = NULL;
    978 
    979 	os_free(wpa_s->p2p_group_common_freqs);
    980 	wpa_s->p2p_group_common_freqs = NULL;
    981 	wpa_s->p2p_group_common_freqs_num = 0;
    982 
    983 	wpa_s->waiting_presence_resp = 0;
    984 
    985 	wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
    986 	if (ssid && (ssid->p2p_group ||
    987 		     ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
    988 		     (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
    989 		int id = ssid->id;
    990 		if (ssid == wpa_s->current_ssid) {
    991 			wpa_sm_set_config(wpa_s->wpa, NULL);
    992 			eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
    993 			wpa_s->current_ssid = NULL;
    994 		}
    995 		/*
    996 		 * Networks objects created during any P2P activities are not
    997 		 * exposed out as they might/will confuse certain non-P2P aware
    998 		 * applications since these network objects won't behave like
    999 		 * regular ones.
   1000 		 *
   1001 		 * Likewise, we don't send out network removed signals for such
   1002 		 * network objects.
   1003 		 */
   1004 		wpa_config_remove_network(wpa_s->conf, id);
   1005 		wpa_supplicant_clear_status(wpa_s);
   1006 		wpa_supplicant_cancel_sched_scan(wpa_s);
   1007 	} else {
   1008 		wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
   1009 			   "found");
   1010 	}
   1011 	if (wpa_s->ap_iface)
   1012 		wpa_supplicant_ap_deinit(wpa_s);
   1013 	else
   1014 		wpa_drv_deinit_p2p_cli(wpa_s);
   1015 
   1016 	os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
   1017 
   1018 	return 0;
   1019 }
   1020 
   1021 
   1022 static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
   1023 				     u8 *go_dev_addr,
   1024 				     const u8 *ssid, size_t ssid_len)
   1025 {
   1026 	struct wpa_bss *bss;
   1027 	const u8 *bssid;
   1028 	struct wpabuf *p2p;
   1029 	u8 group_capab;
   1030 	const u8 *addr;
   1031 
   1032 	if (wpa_s->go_params)
   1033 		bssid = wpa_s->go_params->peer_interface_addr;
   1034 	else
   1035 		bssid = wpa_s->bssid;
   1036 
   1037 	bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
   1038 	if (bss == NULL && wpa_s->go_params &&
   1039 	    !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
   1040 		bss = wpa_bss_get_p2p_dev_addr(
   1041 			wpa_s, wpa_s->go_params->peer_device_addr);
   1042 	if (bss == NULL) {
   1043 		u8 iface_addr[ETH_ALEN];
   1044 		if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
   1045 					   iface_addr) == 0)
   1046 			bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
   1047 	}
   1048 	if (bss == NULL) {
   1049 		wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
   1050 			   "group is persistent - BSS " MACSTR " not found",
   1051 			   MAC2STR(bssid));
   1052 		return 0;
   1053 	}
   1054 
   1055 	p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
   1056 	if (p2p == NULL)
   1057 		p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
   1058 							 P2P_IE_VENDOR_TYPE);
   1059 	if (p2p == NULL) {
   1060 		wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
   1061 			   "group is persistent - BSS " MACSTR
   1062 			   " did not include P2P IE", MAC2STR(bssid));
   1063 		wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
   1064 			    (u8 *) (bss + 1), bss->ie_len);
   1065 		wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
   1066 			    ((u8 *) bss + 1) + bss->ie_len,
   1067 			    bss->beacon_ie_len);
   1068 		return 0;
   1069 	}
   1070 
   1071 	group_capab = p2p_get_group_capab(p2p);
   1072 	addr = p2p_get_go_dev_addr(p2p);
   1073 	wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
   1074 		   "group_capab=0x%x", group_capab);
   1075 	if (addr) {
   1076 		os_memcpy(go_dev_addr, addr, ETH_ALEN);
   1077 		wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
   1078 			   MAC2STR(addr));
   1079 	} else
   1080 		os_memset(go_dev_addr, 0, ETH_ALEN);
   1081 	wpabuf_free(p2p);
   1082 
   1083 	wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
   1084 		   "go_dev_addr=" MACSTR,
   1085 		   MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
   1086 
   1087 	return !!(group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP);
   1088 }
   1089 
   1090 
   1091 static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
   1092 					   struct wpa_ssid *ssid,
   1093 					   const u8 *go_dev_addr)
   1094 {
   1095 	struct wpa_ssid *s;
   1096 	int changed = 0;
   1097 
   1098 	wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
   1099 		   "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
   1100 	for (s = wpa_s->conf->ssid; s; s = s->next) {
   1101 		if (s->disabled == 2 &&
   1102 		    os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
   1103 		    s->ssid_len == ssid->ssid_len &&
   1104 		    os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
   1105 			break;
   1106 	}
   1107 
   1108 	if (s) {
   1109 		wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
   1110 			   "entry");
   1111 		if (ssid->passphrase && !s->passphrase)
   1112 			changed = 1;
   1113 		else if (ssid->passphrase && s->passphrase &&
   1114 			 os_strcmp(ssid->passphrase, s->passphrase) != 0)
   1115 			changed = 1;
   1116 	} else {
   1117 		wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
   1118 			   "entry");
   1119 		changed = 1;
   1120 		s = wpa_config_add_network(wpa_s->conf);
   1121 		if (s == NULL)
   1122 			return -1;
   1123 
   1124 		/*
   1125 		 * Instead of network_added we emit persistent_group_added
   1126 		 * notification. Also to keep the defense checks in
   1127 		 * persistent_group obj registration method, we set the
   1128 		 * relevant flags in s to designate it as a persistent group.
   1129 		 */
   1130 		s->p2p_group = 1;
   1131 		s->p2p_persistent_group = 1;
   1132 		wpas_notify_persistent_group_added(wpa_s, s);
   1133 		wpa_config_set_network_defaults(s);
   1134 	}
   1135 
   1136 	s->p2p_group = 1;
   1137 	s->p2p_persistent_group = 1;
   1138 	s->disabled = 2;
   1139 	s->bssid_set = 1;
   1140 	os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
   1141 	s->mode = ssid->mode;
   1142 	s->auth_alg = WPA_AUTH_ALG_OPEN;
   1143 	s->key_mgmt = WPA_KEY_MGMT_PSK;
   1144 	s->proto = WPA_PROTO_RSN;
   1145 	s->pbss = ssid->pbss;
   1146 	s->pairwise_cipher = ssid->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
   1147 	s->export_keys = 1;
   1148 	if (ssid->passphrase) {
   1149 		os_free(s->passphrase);
   1150 		s->passphrase = os_strdup(ssid->passphrase);
   1151 	}
   1152 	if (ssid->psk_set) {
   1153 		s->psk_set = 1;
   1154 		os_memcpy(s->psk, ssid->psk, 32);
   1155 	}
   1156 	if (s->passphrase && !s->psk_set)
   1157 		wpa_config_update_psk(s);
   1158 	if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
   1159 		os_free(s->ssid);
   1160 		s->ssid = os_malloc(ssid->ssid_len);
   1161 	}
   1162 	if (s->ssid) {
   1163 		s->ssid_len = ssid->ssid_len;
   1164 		os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
   1165 	}
   1166 	if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
   1167 		dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
   1168 		wpa_s->global->add_psk = NULL;
   1169 		changed = 1;
   1170 	}
   1171 
   1172 	if (changed && wpa_s->conf->update_config &&
   1173 	    wpa_config_write(wpa_s->confname, wpa_s->conf)) {
   1174 		wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
   1175 	}
   1176 
   1177 	return s->id;
   1178 }
   1179 
   1180 
   1181 static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
   1182 						 const u8 *addr)
   1183 {
   1184 	struct wpa_ssid *ssid, *s;
   1185 	u8 *n;
   1186 	size_t i;
   1187 	int found = 0;
   1188 	struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
   1189 
   1190 	ssid = wpa_s->current_ssid;
   1191 	if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
   1192 	    !ssid->p2p_persistent_group)
   1193 		return;
   1194 
   1195 	for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
   1196 		if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
   1197 			continue;
   1198 
   1199 		if (s->ssid_len == ssid->ssid_len &&
   1200 		    os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
   1201 			break;
   1202 	}
   1203 
   1204 	if (s == NULL)
   1205 		return;
   1206 
   1207 	for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
   1208 		if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, addr,
   1209 			      ETH_ALEN) != 0)
   1210 			continue;
   1211 
   1212 		if (i == s->num_p2p_clients - 1)
   1213 			return; /* already the most recent entry */
   1214 
   1215 		/* move the entry to mark it most recent */
   1216 		os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
   1217 			   s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
   1218 			   (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
   1219 		os_memcpy(s->p2p_client_list +
   1220 			  (s->num_p2p_clients - 1) * 2 * ETH_ALEN, addr,
   1221 			  ETH_ALEN);
   1222 		os_memset(s->p2p_client_list +
   1223 			  (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
   1224 			  0xff, ETH_ALEN);
   1225 		found = 1;
   1226 		break;
   1227 	}
   1228 
   1229 	if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
   1230 		n = os_realloc_array(s->p2p_client_list,
   1231 				     s->num_p2p_clients + 1, 2 * ETH_ALEN);
   1232 		if (n == NULL)
   1233 			return;
   1234 		os_memcpy(n + s->num_p2p_clients * 2 * ETH_ALEN, addr,
   1235 			  ETH_ALEN);
   1236 		os_memset(n + s->num_p2p_clients * 2 * ETH_ALEN + ETH_ALEN,
   1237 			  0xff, ETH_ALEN);
   1238 		s->p2p_client_list = n;
   1239 		s->num_p2p_clients++;
   1240 	} else if (!found && s->p2p_client_list) {
   1241 		/* Not enough room for an additional entry - drop the oldest
   1242 		 * entry */
   1243 		os_memmove(s->p2p_client_list,
   1244 			   s->p2p_client_list + 2 * ETH_ALEN,
   1245 			   (s->num_p2p_clients - 1) * 2 * ETH_ALEN);
   1246 		os_memcpy(s->p2p_client_list +
   1247 			  (s->num_p2p_clients - 1) * 2 * ETH_ALEN,
   1248 			  addr, ETH_ALEN);
   1249 		os_memset(s->p2p_client_list +
   1250 			  (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
   1251 			  0xff, ETH_ALEN);
   1252 	}
   1253 
   1254 	if (p2p_wpa_s->conf->update_config &&
   1255 	    wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
   1256 		wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
   1257 }
   1258 
   1259 
   1260 static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
   1261 				   int go, struct wpa_ssid *ssid, int freq,
   1262 				   const u8 *psk, const char *passphrase,
   1263 				   const u8 *go_dev_addr, int persistent,
   1264 				   const char *extra)
   1265 {
   1266 	const char *ssid_txt;
   1267 	char psk_txt[65];
   1268 
   1269 	if (psk)
   1270 		wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
   1271 	else
   1272 		psk_txt[0] = '\0';
   1273 
   1274 	if (ssid)
   1275 		ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
   1276 	else
   1277 		ssid_txt = "";
   1278 
   1279 	if (passphrase && passphrase[0] == '\0')
   1280 		passphrase = NULL;
   1281 
   1282 	/*
   1283 	 * Include PSK/passphrase only in the control interface message and
   1284 	 * leave it out from the debug log entry.
   1285 	 */
   1286 	wpa_msg_global_ctrl(wpa_s->p2pdev, MSG_INFO,
   1287 			    P2P_EVENT_GROUP_STARTED
   1288 			    "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
   1289 			    MACSTR "%s%s",
   1290 			    wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
   1291 			    psk ? " psk=" : "", psk_txt,
   1292 			    passphrase ? " passphrase=\"" : "",
   1293 			    passphrase ? passphrase : "",
   1294 			    passphrase ? "\"" : "",
   1295 			    MAC2STR(go_dev_addr),
   1296 			    persistent ? " [PERSISTENT]" : "", extra);
   1297 	wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
   1298 		   "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
   1299 		   wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
   1300 		   MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
   1301 		   extra);
   1302 }
   1303 
   1304 
   1305 static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
   1306 					   int success, int already_deleted)
   1307 {
   1308 	struct wpa_ssid *ssid;
   1309 	int client;
   1310 	int persistent;
   1311 	u8 go_dev_addr[ETH_ALEN];
   1312 
   1313 	/*
   1314 	 * This callback is likely called for the main interface. Update wpa_s
   1315 	 * to use the group interface if a new interface was created for the
   1316 	 * group.
   1317 	 */
   1318 	if (wpa_s->global->p2p_group_formation)
   1319 		wpa_s = wpa_s->global->p2p_group_formation;
   1320 	if (wpa_s->p2p_go_group_formation_completed) {
   1321 		wpa_s->global->p2p_group_formation = NULL;
   1322 		wpa_s->p2p_in_provisioning = 0;
   1323 	}
   1324 	wpa_s->p2p_in_invitation = 0;
   1325 	wpa_s->group_formation_reported = 1;
   1326 
   1327 	if (!success) {
   1328 		wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
   1329 			       P2P_EVENT_GROUP_FORMATION_FAILURE);
   1330 		wpas_notify_p2p_group_formation_failure(wpa_s, "");
   1331 		if (already_deleted)
   1332 			return;
   1333 		wpas_p2p_group_delete(wpa_s,
   1334 				      P2P_GROUP_REMOVAL_FORMATION_FAILED);
   1335 		return;
   1336 	}
   1337 
   1338 	wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
   1339 		       P2P_EVENT_GROUP_FORMATION_SUCCESS);
   1340 
   1341 	ssid = wpa_s->current_ssid;
   1342 	if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
   1343 		ssid->mode = WPAS_MODE_P2P_GO;
   1344 		p2p_group_notif_formation_done(wpa_s->p2p_group);
   1345 		wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
   1346 	}
   1347 
   1348 	persistent = 0;
   1349 	if (ssid) {
   1350 		client = ssid->mode == WPAS_MODE_INFRA;
   1351 		if (ssid->mode == WPAS_MODE_P2P_GO) {
   1352 			persistent = ssid->p2p_persistent_group;
   1353 			os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
   1354 				  ETH_ALEN);
   1355 		} else
   1356 			persistent = wpas_p2p_persistent_group(wpa_s,
   1357 							       go_dev_addr,
   1358 							       ssid->ssid,
   1359 							       ssid->ssid_len);
   1360 	} else {
   1361 		client = wpa_s->p2p_group_interface ==
   1362 			P2P_GROUP_INTERFACE_CLIENT;
   1363 		os_memset(go_dev_addr, 0, ETH_ALEN);
   1364 	}
   1365 
   1366 	wpa_s->show_group_started = 0;
   1367 	if (client) {
   1368 		/*
   1369 		 * Indicate event only after successfully completed 4-way
   1370 		 * handshake, i.e., when the interface is ready for data
   1371 		 * packets.
   1372 		 */
   1373 		wpa_s->show_group_started = 1;
   1374 	} else {
   1375 		wpas_p2p_group_started(wpa_s, 1, ssid,
   1376 				       ssid ? ssid->frequency : 0,
   1377 				       ssid && ssid->passphrase == NULL &&
   1378 				       ssid->psk_set ? ssid->psk : NULL,
   1379 				       ssid ? ssid->passphrase : NULL,
   1380 				       go_dev_addr, persistent, "");
   1381 		wpas_p2p_cross_connect_setup(wpa_s);
   1382 		wpas_p2p_set_group_idle_timeout(wpa_s);
   1383 	}
   1384 
   1385 	if (persistent)
   1386 		wpas_p2p_store_persistent_group(wpa_s->p2pdev,
   1387 						ssid, go_dev_addr);
   1388 	else {
   1389 		os_free(wpa_s->global->add_psk);
   1390 		wpa_s->global->add_psk = NULL;
   1391 	}
   1392 
   1393 	if (!client) {
   1394 		wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 0, NULL);
   1395 		os_get_reltime(&wpa_s->global->p2p_go_wait_client);
   1396 	}
   1397 }
   1398 
   1399 
   1400 struct send_action_work {
   1401 	unsigned int freq;
   1402 	u8 dst[ETH_ALEN];
   1403 	u8 src[ETH_ALEN];
   1404 	u8 bssid[ETH_ALEN];
   1405 	size_t len;
   1406 	unsigned int wait_time;
   1407 	u8 buf[0];
   1408 };
   1409 
   1410 
   1411 static void wpas_p2p_free_send_action_work(struct wpa_supplicant *wpa_s)
   1412 {
   1413 	struct send_action_work *awork = wpa_s->p2p_send_action_work->ctx;
   1414 
   1415 	wpa_printf(MSG_DEBUG,
   1416 		   "P2P: Free Action frame radio work @%p (freq=%u dst="
   1417 		   MACSTR " src=" MACSTR " bssid=" MACSTR " wait_time=%u)",
   1418 		   wpa_s->p2p_send_action_work, awork->freq,
   1419 		   MAC2STR(awork->dst), MAC2STR(awork->src),
   1420 		   MAC2STR(awork->bssid), awork->wait_time);
   1421 	wpa_hexdump(MSG_DEBUG, "P2P: Freeing pending Action frame",
   1422 		    awork->buf, awork->len);
   1423 	os_free(awork);
   1424 	wpa_s->p2p_send_action_work->ctx = NULL;
   1425 	radio_work_done(wpa_s->p2p_send_action_work);
   1426 	wpa_s->p2p_send_action_work = NULL;
   1427 }
   1428 
   1429 
   1430 static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
   1431 					      void *timeout_ctx)
   1432 {
   1433 	struct wpa_supplicant *wpa_s = eloop_ctx;
   1434 
   1435 	if (!wpa_s->p2p_send_action_work)
   1436 		return;
   1437 
   1438 	wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
   1439 	wpas_p2p_free_send_action_work(wpa_s);
   1440 }
   1441 
   1442 
   1443 static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
   1444 {
   1445 	if (wpa_s->p2p_send_action_work) {
   1446 		struct send_action_work *awork;
   1447 
   1448 		awork = wpa_s->p2p_send_action_work->ctx;
   1449 		wpa_printf(MSG_DEBUG,
   1450 			   "P2P: Clear Action TX work @%p (wait_time=%u)",
   1451 			   wpa_s->p2p_send_action_work, awork->wait_time);
   1452 		if (awork->wait_time == 0) {
   1453 			wpas_p2p_free_send_action_work(wpa_s);
   1454 		} else {
   1455 			/*
   1456 			 * In theory, this should not be needed, but number of
   1457 			 * places in the P2P code is still using non-zero wait
   1458 			 * time for the last Action frame in the sequence and
   1459 			 * some of these do not call send_action_done().
   1460 			 */
   1461 			eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
   1462 					     wpa_s, NULL);
   1463 			eloop_register_timeout(
   1464 				0, awork->wait_time * 1000,
   1465 				wpas_p2p_send_action_work_timeout,
   1466 				wpa_s, NULL);
   1467 		}
   1468 	}
   1469 }
   1470 
   1471 
   1472 static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
   1473 					   unsigned int freq,
   1474 					   const u8 *dst, const u8 *src,
   1475 					   const u8 *bssid,
   1476 					   const u8 *data, size_t data_len,
   1477 					   enum offchannel_send_action_result
   1478 					   result)
   1479 {
   1480 	enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
   1481 
   1482 	wpas_p2p_action_tx_clear(wpa_s);
   1483 
   1484 	if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
   1485 		return;
   1486 
   1487 	switch (result) {
   1488 	case OFFCHANNEL_SEND_ACTION_SUCCESS:
   1489 		res = P2P_SEND_ACTION_SUCCESS;
   1490 		break;
   1491 	case OFFCHANNEL_SEND_ACTION_NO_ACK:
   1492 		res = P2P_SEND_ACTION_NO_ACK;
   1493 		break;
   1494 	case OFFCHANNEL_SEND_ACTION_FAILED:
   1495 		res = P2P_SEND_ACTION_FAILED;
   1496 		break;
   1497 	}
   1498 
   1499 	p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
   1500 
   1501 	if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
   1502 	    wpa_s->pending_pd_before_join &&
   1503 	    (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
   1504 	     os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
   1505 	    wpa_s->p2p_fallback_to_go_neg) {
   1506 		wpa_s->pending_pd_before_join = 0;
   1507 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
   1508 			"during p2p_connect-auto");
   1509 		wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
   1510 			       P2P_EVENT_FALLBACK_TO_GO_NEG
   1511 			       "reason=no-ACK-to-PD-Req");
   1512 		wpas_p2p_fallback_to_go_neg(wpa_s, 0);
   1513 		return;
   1514 	}
   1515 }
   1516 
   1517 
   1518 static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
   1519 {
   1520 	struct wpa_supplicant *wpa_s = work->wpa_s;
   1521 	struct send_action_work *awork = work->ctx;
   1522 
   1523 	if (deinit) {
   1524 		if (work->started) {
   1525 			eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
   1526 					     wpa_s, NULL);
   1527 			wpa_s->p2p_send_action_work = NULL;
   1528 			offchannel_send_action_done(wpa_s);
   1529 		}
   1530 		os_free(awork);
   1531 		return;
   1532 	}
   1533 
   1534 	if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
   1535 				   awork->bssid, awork->buf, awork->len,
   1536 				   awork->wait_time,
   1537 				   wpas_p2p_send_action_tx_status, 1) < 0) {
   1538 		os_free(awork);
   1539 		radio_work_done(work);
   1540 		return;
   1541 	}
   1542 	wpa_s->p2p_send_action_work = work;
   1543 }
   1544 
   1545 
   1546 static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
   1547 				 unsigned int freq, const u8 *dst,
   1548 				 const u8 *src, const u8 *bssid, const u8 *buf,
   1549 				 size_t len, unsigned int wait_time)
   1550 {
   1551 	struct send_action_work *awork;
   1552 
   1553 	if (wpa_s->p2p_send_action_work) {
   1554 		wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
   1555 		return -1;
   1556 	}
   1557 
   1558 	awork = os_zalloc(sizeof(*awork) + len);
   1559 	if (awork == NULL)
   1560 		return -1;
   1561 
   1562 	awork->freq = freq;
   1563 	os_memcpy(awork->dst, dst, ETH_ALEN);
   1564 	os_memcpy(awork->src, src, ETH_ALEN);
   1565 	os_memcpy(awork->bssid, bssid, ETH_ALEN);
   1566 	awork->len = len;
   1567 	awork->wait_time = wait_time;
   1568 	os_memcpy(awork->buf, buf, len);
   1569 
   1570 	if (radio_add_work(wpa_s, freq, "p2p-send-action", 0,
   1571 			   wpas_send_action_cb, awork) < 0) {
   1572 		os_free(awork);
   1573 		return -1;
   1574 	}
   1575 
   1576 	return 0;
   1577 }
   1578 
   1579 
   1580 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
   1581 			    const u8 *src, const u8 *bssid, const u8 *buf,
   1582 			    size_t len, unsigned int wait_time)
   1583 {
   1584 	struct wpa_supplicant *wpa_s = ctx;
   1585 	int listen_freq = -1, send_freq = -1;
   1586 
   1587 	if (wpa_s->p2p_listen_work)
   1588 		listen_freq = wpa_s->p2p_listen_work->freq;
   1589 	if (wpa_s->p2p_send_action_work)
   1590 		send_freq = wpa_s->p2p_send_action_work->freq;
   1591 	if (listen_freq != (int) freq && send_freq != (int) freq) {
   1592 		wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
   1593 			   listen_freq, send_freq);
   1594 		return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
   1595 					     len, wait_time);
   1596 	}
   1597 
   1598 	wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
   1599 	return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
   1600 				      wait_time,
   1601 				      wpas_p2p_send_action_tx_status, 1);
   1602 }
   1603 
   1604 
   1605 static void wpas_send_action_done(void *ctx)
   1606 {
   1607 	struct wpa_supplicant *wpa_s = ctx;
   1608 
   1609 	if (wpa_s->p2p_send_action_work) {
   1610 		eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
   1611 				     wpa_s, NULL);
   1612 		os_free(wpa_s->p2p_send_action_work->ctx);
   1613 		radio_work_done(wpa_s->p2p_send_action_work);
   1614 		wpa_s->p2p_send_action_work = NULL;
   1615 	}
   1616 
   1617 	offchannel_send_action_done(wpa_s);
   1618 }
   1619 
   1620 
   1621 static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
   1622 				    struct p2p_go_neg_results *params)
   1623 {
   1624 	if (wpa_s->go_params == NULL) {
   1625 		wpa_s->go_params = os_malloc(sizeof(*params));
   1626 		if (wpa_s->go_params == NULL)
   1627 			return -1;
   1628 	}
   1629 	os_memcpy(wpa_s->go_params, params, sizeof(*params));
   1630 	return 0;
   1631 }
   1632 
   1633 
   1634 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
   1635 				    struct p2p_go_neg_results *res)
   1636 {
   1637 	wpa_s->group_formation_reported = 0;
   1638 	wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
   1639 		   " dev_addr " MACSTR " wps_method %d",
   1640 		   MAC2STR(res->peer_interface_addr),
   1641 		   MAC2STR(res->peer_device_addr), res->wps_method);
   1642 	wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
   1643 			  res->ssid, res->ssid_len);
   1644 	wpa_supplicant_ap_deinit(wpa_s);
   1645 	wpas_copy_go_neg_results(wpa_s, res);
   1646 	if (res->wps_method == WPS_PBC) {
   1647 		wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
   1648 #ifdef CONFIG_WPS_NFC
   1649 	} else if (res->wps_method == WPS_NFC) {
   1650 		wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
   1651 				   res->peer_interface_addr,
   1652 				   wpa_s->p2pdev->p2p_oob_dev_pw,
   1653 				   wpa_s->p2pdev->p2p_oob_dev_pw_id, 1,
   1654 				   wpa_s->p2pdev->p2p_oob_dev_pw_id ==
   1655 				   DEV_PW_NFC_CONNECTION_HANDOVER ?
   1656 				   wpa_s->p2pdev->p2p_peer_oob_pubkey_hash :
   1657 				   NULL,
   1658 				   NULL, 0, 0);
   1659 #endif /* CONFIG_WPS_NFC */
   1660 	} else {
   1661 		u16 dev_pw_id = DEV_PW_DEFAULT;
   1662 		if (wpa_s->p2p_wps_method == WPS_P2PS)
   1663 			dev_pw_id = DEV_PW_P2PS_DEFAULT;
   1664 		if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
   1665 			dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
   1666 		wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
   1667 				   wpa_s->p2p_pin, 1, dev_pw_id);
   1668 	}
   1669 }
   1670 
   1671 
   1672 static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
   1673 				  struct wpa_ssid *ssid)
   1674 {
   1675 	struct wpa_ssid *persistent;
   1676 	struct psk_list_entry *psk;
   1677 	struct hostapd_data *hapd;
   1678 
   1679 	if (!wpa_s->ap_iface)
   1680 		return;
   1681 
   1682 	persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
   1683 					     ssid->ssid_len);
   1684 	if (persistent == NULL)
   1685 		return;
   1686 
   1687 	hapd = wpa_s->ap_iface->bss[0];
   1688 
   1689 	dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
   1690 			 list) {
   1691 		struct hostapd_wpa_psk *hpsk;
   1692 
   1693 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
   1694 			MACSTR " psk=%d",
   1695 			MAC2STR(psk->addr), psk->p2p);
   1696 		hpsk = os_zalloc(sizeof(*hpsk));
   1697 		if (hpsk == NULL)
   1698 			break;
   1699 		os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
   1700 		if (psk->p2p)
   1701 			os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
   1702 		else
   1703 			os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
   1704 		hpsk->next = hapd->conf->ssid.wpa_psk;
   1705 		hapd->conf->ssid.wpa_psk = hpsk;
   1706 	}
   1707 }
   1708 
   1709 
   1710 static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s)
   1711 {
   1712 	unsigned int i;
   1713 
   1714 	wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies (len=%u):",
   1715 		wpa_s->p2p_group_common_freqs_num);
   1716 
   1717 	for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++)
   1718 		wpa_dbg(wpa_s, MSG_DEBUG, "freq[%u]: %d",
   1719 			i, wpa_s->p2p_group_common_freqs[i]);
   1720 }
   1721 
   1722 
   1723 static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s,
   1724 					   struct p2p_go_neg_results *params)
   1725 {
   1726 	unsigned int i, len = int_array_len(wpa_s->go_params->freq_list);
   1727 
   1728 	wpa_s->p2p_group_common_freqs_num = 0;
   1729 	os_free(wpa_s->p2p_group_common_freqs);
   1730 	wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int));
   1731 	if (!wpa_s->p2p_group_common_freqs)
   1732 		return;
   1733 
   1734 	for (i = 0; i < len; i++) {
   1735 		if (!wpa_s->go_params->freq_list[i])
   1736 			break;
   1737 		wpa_s->p2p_group_common_freqs[i] =
   1738 			wpa_s->go_params->freq_list[i];
   1739 	}
   1740 	wpa_s->p2p_group_common_freqs_num = i;
   1741 }
   1742 
   1743 
   1744 static void p2p_config_write(struct wpa_supplicant *wpa_s)
   1745 {
   1746 #ifndef CONFIG_NO_CONFIG_WRITE
   1747 	if (wpa_s->p2pdev->conf->update_config &&
   1748 	    wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
   1749 		wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
   1750 #endif /* CONFIG_NO_CONFIG_WRITE */
   1751 }
   1752 
   1753 
   1754 static void p2p_go_configured(void *ctx, void *data)
   1755 {
   1756 	struct wpa_supplicant *wpa_s = ctx;
   1757 	struct p2p_go_neg_results *params = data;
   1758 	struct wpa_ssid *ssid;
   1759 
   1760 	wpa_s->ap_configured_cb = NULL;
   1761 	wpa_s->ap_configured_cb_ctx = NULL;
   1762 	wpa_s->ap_configured_cb_data = NULL;
   1763 	if (!wpa_s->go_params) {
   1764 		wpa_printf(MSG_ERROR,
   1765 			   "P2P: p2p_go_configured() called with wpa_s->go_params == NULL");
   1766 		return;
   1767 	}
   1768 
   1769 	p2p_go_save_group_common_freqs(wpa_s, params);
   1770 	p2p_go_dump_common_freqs(wpa_s);
   1771 
   1772 	ssid = wpa_s->current_ssid;
   1773 	if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
   1774 		wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
   1775 		if (wpa_s->global->p2p_group_formation == wpa_s)
   1776 			wpa_s->global->p2p_group_formation = NULL;
   1777 		wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
   1778 				       params->passphrase[0] == '\0' ?
   1779 				       params->psk : NULL,
   1780 				       params->passphrase,
   1781 				       wpa_s->global->p2p_dev_addr,
   1782 				       params->persistent_group, "");
   1783 		wpa_s->group_formation_reported = 1;
   1784 
   1785 		if (wpa_s->p2pdev->p2ps_method_config_any) {
   1786 			if (is_zero_ether_addr(wpa_s->p2pdev->p2ps_join_addr)) {
   1787 				wpa_dbg(wpa_s, MSG_DEBUG,
   1788 					"P2PS: Setting default PIN for ANY");
   1789 				wpa_supplicant_ap_wps_pin(wpa_s, NULL,
   1790 							  "12345670", NULL, 0,
   1791 							  0);
   1792 			} else {
   1793 				wpa_dbg(wpa_s, MSG_DEBUG,
   1794 					"P2PS: Setting default PIN for " MACSTR,
   1795 					MAC2STR(wpa_s->p2pdev->p2ps_join_addr));
   1796 				wpa_supplicant_ap_wps_pin(
   1797 					wpa_s, wpa_s->p2pdev->p2ps_join_addr,
   1798 					"12345670", NULL, 0, 0);
   1799 			}
   1800 			wpa_s->p2pdev->p2ps_method_config_any = 0;
   1801 		}
   1802 
   1803 		os_get_reltime(&wpa_s->global->p2p_go_wait_client);
   1804 		if (params->persistent_group) {
   1805 			wpas_p2p_store_persistent_group(
   1806 				wpa_s->p2pdev, ssid,
   1807 				wpa_s->global->p2p_dev_addr);
   1808 			wpas_p2p_add_psk_list(wpa_s, ssid);
   1809 		}
   1810 
   1811 		wpas_notify_p2p_group_started(wpa_s, ssid,
   1812 					      params->persistent_group, 0,
   1813 					      NULL);
   1814 		wpas_p2p_cross_connect_setup(wpa_s);
   1815 		wpas_p2p_set_group_idle_timeout(wpa_s);
   1816 
   1817 		if (wpa_s->p2p_first_connection_timeout) {
   1818 			wpa_dbg(wpa_s, MSG_DEBUG,
   1819 				"P2P: Start group formation timeout of %d seconds until first data connection on GO",
   1820 				wpa_s->p2p_first_connection_timeout);
   1821 			wpa_s->p2p_go_group_formation_completed = 0;
   1822 			wpa_s->global->p2p_group_formation = wpa_s;
   1823 			eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
   1824 					     wpa_s->p2pdev, NULL);
   1825 			eloop_register_timeout(
   1826 				wpa_s->p2p_first_connection_timeout, 0,
   1827 				wpas_p2p_group_formation_timeout,
   1828 				wpa_s->p2pdev, NULL);
   1829 		}
   1830 
   1831 		return;
   1832 	}
   1833 
   1834 	wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
   1835 	if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
   1836 					      params->peer_interface_addr)) {
   1837 		wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
   1838 			   "filtering");
   1839 		return;
   1840 	}
   1841 	if (params->wps_method == WPS_PBC) {
   1842 		wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
   1843 					  params->peer_device_addr);
   1844 #ifdef CONFIG_WPS_NFC
   1845 	} else if (params->wps_method == WPS_NFC) {
   1846 		if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
   1847 		    DEV_PW_NFC_CONNECTION_HANDOVER &&
   1848 		    !wpa_s->p2pdev->p2p_oob_dev_pw) {
   1849 			wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
   1850 			return;
   1851 		}
   1852 		wpas_ap_wps_add_nfc_pw(
   1853 			wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
   1854 			wpa_s->p2pdev->p2p_oob_dev_pw,
   1855 			wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
   1856 			wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
   1857 #endif /* CONFIG_WPS_NFC */
   1858 	} else if (wpa_s->p2p_pin[0])
   1859 		wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
   1860 					  wpa_s->p2p_pin, NULL, 0, 0);
   1861 	os_free(wpa_s->go_params);
   1862 	wpa_s->go_params = NULL;
   1863 }
   1864 
   1865 
   1866 static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
   1867 			      struct p2p_go_neg_results *params,
   1868 			      int group_formation)
   1869 {
   1870 	struct wpa_ssid *ssid;
   1871 
   1872 	wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
   1873 	if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
   1874 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
   1875 			"results");
   1876 		return;
   1877 	}
   1878 
   1879 	ssid = wpa_config_add_network(wpa_s->conf);
   1880 	if (ssid == NULL) {
   1881 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
   1882 		return;
   1883 	}
   1884 
   1885 	wpa_s->show_group_started = 0;
   1886 	wpa_s->p2p_go_group_formation_completed = 0;
   1887 	wpa_s->group_formation_reported = 0;
   1888 	os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
   1889 
   1890 	wpa_config_set_network_defaults(ssid);
   1891 	ssid->temporary = 1;
   1892 	ssid->p2p_group = 1;
   1893 	ssid->p2p_persistent_group = !!params->persistent_group;
   1894 	ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
   1895 		WPAS_MODE_P2P_GO;
   1896 	ssid->frequency = params->freq;
   1897 	ssid->ht40 = params->ht40;
   1898 	ssid->vht = params->vht;
   1899 	ssid->max_oper_chwidth = params->max_oper_chwidth;
   1900 	ssid->vht_center_freq2 = params->vht_center_freq2;
   1901 	ssid->ssid = os_zalloc(params->ssid_len + 1);
   1902 	if (ssid->ssid) {
   1903 		os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
   1904 		ssid->ssid_len = params->ssid_len;
   1905 	}
   1906 	ssid->auth_alg = WPA_AUTH_ALG_OPEN;
   1907 	ssid->key_mgmt = WPA_KEY_MGMT_PSK;
   1908 	ssid->proto = WPA_PROTO_RSN;
   1909 	ssid->pairwise_cipher = WPA_CIPHER_CCMP;
   1910 	ssid->group_cipher = WPA_CIPHER_CCMP;
   1911 	if (params->freq > 56160) {
   1912 		/*
   1913 		 * Enable GCMP instead of CCMP as pairwise_cipher and
   1914 		 * group_cipher in 60 GHz.
   1915 		 */
   1916 		ssid->pairwise_cipher = WPA_CIPHER_GCMP;
   1917 		ssid->group_cipher = WPA_CIPHER_GCMP;
   1918 		/* P2P GO in 60 GHz is always a PCP (PBSS) */
   1919 		ssid->pbss = 1;
   1920 	}
   1921 	if (os_strlen(params->passphrase) > 0) {
   1922 		ssid->passphrase = os_strdup(params->passphrase);
   1923 		if (ssid->passphrase == NULL) {
   1924 			wpa_msg_global(wpa_s, MSG_ERROR,
   1925 				       "P2P: Failed to copy passphrase for GO");
   1926 			wpa_config_remove_network(wpa_s->conf, ssid->id);
   1927 			return;
   1928 		}
   1929 	} else
   1930 		ssid->passphrase = NULL;
   1931 	ssid->psk_set = params->psk_set;
   1932 	if (ssid->psk_set)
   1933 		os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
   1934 	else if (ssid->passphrase)
   1935 		wpa_config_update_psk(ssid);
   1936 	ssid->ap_max_inactivity = wpa_s->p2pdev->conf->p2p_go_max_inactivity;
   1937 
   1938 	wpa_s->ap_configured_cb = p2p_go_configured;
   1939 	wpa_s->ap_configured_cb_ctx = wpa_s;
   1940 	wpa_s->ap_configured_cb_data = wpa_s->go_params;
   1941 	wpa_s->scan_req = NORMAL_SCAN_REQ;
   1942 	wpa_s->connect_without_scan = ssid;
   1943 	wpa_s->reassociate = 1;
   1944 	wpa_s->disconnected = 0;
   1945 	wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
   1946 		"start GO)");
   1947 	wpa_supplicant_req_scan(wpa_s, 0, 0);
   1948 }
   1949 
   1950 
   1951 static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
   1952 				  const struct wpa_supplicant *src)
   1953 {
   1954 	struct wpa_config *d;
   1955 	const struct wpa_config *s;
   1956 
   1957 	d = dst->conf;
   1958 	s = src->conf;
   1959 
   1960 #define C(n)                            \
   1961 do {                                    \
   1962 	if (s->n && !d->n)              \
   1963 		d->n = os_strdup(s->n); \
   1964 } while (0)
   1965 
   1966 	C(device_name);
   1967 	C(manufacturer);
   1968 	C(model_name);
   1969 	C(model_number);
   1970 	C(serial_number);
   1971 	C(config_methods);
   1972 #undef C
   1973 
   1974 	os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
   1975 	os_memcpy(d->sec_device_type, s->sec_device_type,
   1976 		  sizeof(d->sec_device_type));
   1977 	d->num_sec_device_types = s->num_sec_device_types;
   1978 
   1979 	d->p2p_group_idle = s->p2p_group_idle;
   1980 	d->p2p_go_freq_change_policy = s->p2p_go_freq_change_policy;
   1981 	d->p2p_intra_bss = s->p2p_intra_bss;
   1982 	d->persistent_reconnect = s->persistent_reconnect;
   1983 	d->max_num_sta = s->max_num_sta;
   1984 	d->pbc_in_m1 = s->pbc_in_m1;
   1985 	d->ignore_old_scan_res = s->ignore_old_scan_res;
   1986 	d->beacon_int = s->beacon_int;
   1987 	d->dtim_period = s->dtim_period;
   1988 	d->p2p_go_ctwindow = s->p2p_go_ctwindow;
   1989 	d->disassoc_low_ack = s->disassoc_low_ack;
   1990 	d->disable_scan_offload = s->disable_scan_offload;
   1991 	d->passive_scan = s->passive_scan;
   1992 
   1993 	if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey &&
   1994 	    !d->wps_nfc_pw_from_config) {
   1995 		wpabuf_free(d->wps_nfc_dh_privkey);
   1996 		wpabuf_free(d->wps_nfc_dh_pubkey);
   1997 		d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
   1998 		d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
   1999 	}
   2000 	d->p2p_cli_probe = s->p2p_cli_probe;
   2001 }
   2002 
   2003 
   2004 static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
   2005 				      char *ifname, size_t len)
   2006 {
   2007 	char *ifname_ptr = wpa_s->ifname;
   2008 
   2009 	if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
   2010 		       os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
   2011 		ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
   2012 	}
   2013 
   2014 	os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
   2015 	if (os_strlen(ifname) >= IFNAMSIZ &&
   2016 	    os_strlen(wpa_s->ifname) < IFNAMSIZ) {
   2017 		int res;
   2018 
   2019 		/* Try to avoid going over the IFNAMSIZ length limit */
   2020 		res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
   2021 		if (os_snprintf_error(len, res) && len)
   2022 			ifname[len - 1] = '\0';
   2023 	}
   2024 }
   2025 
   2026 
   2027 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
   2028 					enum wpa_driver_if_type type)
   2029 {
   2030 	char ifname[120], force_ifname[120];
   2031 
   2032 	if (wpa_s->pending_interface_name[0]) {
   2033 		wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
   2034 			   "- skip creation of a new one");
   2035 		if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
   2036 			wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
   2037 				   "unknown?! ifname='%s'",
   2038 				   wpa_s->pending_interface_name);
   2039 			return -1;
   2040 		}
   2041 		return 0;
   2042 	}
   2043 
   2044 	wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
   2045 	force_ifname[0] = '\0';
   2046 
   2047 	wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
   2048 		   ifname);
   2049 	wpa_s->p2p_group_idx++;
   2050 
   2051 	wpa_s->pending_interface_type = type;
   2052 	if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
   2053 			   wpa_s->pending_interface_addr, NULL) < 0) {
   2054 		wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
   2055 			   "interface");
   2056 		return -1;
   2057 	}
   2058 
   2059 	if (force_ifname[0]) {
   2060 		wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
   2061 			   force_ifname);
   2062 		os_strlcpy(wpa_s->pending_interface_name, force_ifname,
   2063 			   sizeof(wpa_s->pending_interface_name));
   2064 	} else
   2065 		os_strlcpy(wpa_s->pending_interface_name, ifname,
   2066 			   sizeof(wpa_s->pending_interface_name));
   2067 	wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
   2068 		   MACSTR, wpa_s->pending_interface_name,
   2069 		   MAC2STR(wpa_s->pending_interface_addr));
   2070 
   2071 	return 0;
   2072 }
   2073 
   2074 
   2075 static void wpas_p2p_remove_pending_group_interface(
   2076 	struct wpa_supplicant *wpa_s)
   2077 {
   2078 	if (!wpa_s->pending_interface_name[0] ||
   2079 	    is_zero_ether_addr(wpa_s->pending_interface_addr))
   2080 		return; /* No pending virtual interface */
   2081 
   2082 	wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
   2083 		   wpa_s->pending_interface_name);
   2084 	wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
   2085 			  wpa_s->pending_interface_name);
   2086 	os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
   2087 	wpa_s->pending_interface_name[0] = '\0';
   2088 	wpa_s->global->pending_group_iface_for_p2ps = 0;
   2089 }
   2090 
   2091 
   2092 static struct wpa_supplicant *
   2093 wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
   2094 {
   2095 	struct wpa_interface iface;
   2096 	struct wpa_supplicant *group_wpa_s;
   2097 
   2098 	if (!wpa_s->pending_interface_name[0]) {
   2099 		wpa_printf(MSG_ERROR, "P2P: No pending group interface");
   2100 		if (!wpas_p2p_create_iface(wpa_s))
   2101 			return NULL;
   2102 		/*
   2103 		 * Something has forced us to remove the pending interface; try
   2104 		 * to create a new one and hope for the best that we will get
   2105 		 * the same local address.
   2106 		 */
   2107 		if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
   2108 						 WPA_IF_P2P_CLIENT) < 0)
   2109 			return NULL;
   2110 	}
   2111 
   2112 	os_memset(&iface, 0, sizeof(iface));
   2113 	iface.ifname = wpa_s->pending_interface_name;
   2114 	iface.driver = wpa_s->driver->name;
   2115 	if (wpa_s->conf->ctrl_interface == NULL &&
   2116 	    wpa_s->parent != wpa_s &&
   2117 	    wpa_s->p2p_mgmt &&
   2118 	    (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
   2119 		iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
   2120 	else
   2121 		iface.ctrl_interface = wpa_s->conf->ctrl_interface;
   2122 	iface.driver_param = wpa_s->conf->driver_param;
   2123 	group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
   2124 	if (group_wpa_s == NULL) {
   2125 		wpa_printf(MSG_ERROR, "P2P: Failed to create new "
   2126 			   "wpa_supplicant interface");
   2127 		return NULL;
   2128 	}
   2129 	wpa_s->pending_interface_name[0] = '\0';
   2130 	group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
   2131 		P2P_GROUP_INTERFACE_CLIENT;
   2132 	wpa_s->global->p2p_group_formation = group_wpa_s;
   2133 	wpa_s->global->pending_group_iface_for_p2ps = 0;
   2134 
   2135 	wpas_p2p_clone_config(group_wpa_s, wpa_s);
   2136 
   2137 	return group_wpa_s;
   2138 }
   2139 
   2140 
   2141 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
   2142 					     void *timeout_ctx)
   2143 {
   2144 	struct wpa_supplicant *wpa_s = eloop_ctx;
   2145 	wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
   2146 	wpas_p2p_group_formation_failed(wpa_s, 0);
   2147 }
   2148 
   2149 
   2150 static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
   2151 					    int already_deleted)
   2152 {
   2153 	eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
   2154 			     wpa_s->p2pdev, NULL);
   2155 	if (wpa_s->global->p2p)
   2156 		p2p_group_formation_failed(wpa_s->global->p2p);
   2157 	wpas_group_formation_completed(wpa_s, 0, already_deleted);
   2158 }
   2159 
   2160 
   2161 static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
   2162 {
   2163 	wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
   2164 	eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
   2165 			     wpa_s->p2pdev, NULL);
   2166 	eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
   2167 			       wpa_s->p2pdev, NULL);
   2168 	wpa_s->global->p2p_fail_on_wps_complete = 0;
   2169 }
   2170 
   2171 
   2172 void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
   2173 {
   2174 	if (wpa_s->global->p2p_group_formation != wpa_s)
   2175 		return;
   2176 	/* Speed up group formation timeout since this cannot succeed */
   2177 	eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
   2178 			     wpa_s->p2pdev, NULL);
   2179 	eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
   2180 			       wpa_s->p2pdev, NULL);
   2181 }
   2182 
   2183 
   2184 static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
   2185 {
   2186 	struct wpa_supplicant *wpa_s = ctx;
   2187 	struct wpa_supplicant *group_wpa_s;
   2188 
   2189 	if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
   2190 		wpa_drv_cancel_remain_on_channel(wpa_s);
   2191 		wpa_s->off_channel_freq = 0;
   2192 		wpa_s->roc_waiting_drv_freq = 0;
   2193 	}
   2194 
   2195 	if (res->status) {
   2196 		wpa_msg_global(wpa_s, MSG_INFO,
   2197 			       P2P_EVENT_GO_NEG_FAILURE "status=%d",
   2198 			       res->status);
   2199 		wpas_notify_p2p_go_neg_completed(wpa_s, res);
   2200 		wpas_p2p_remove_pending_group_interface(wpa_s);
   2201 		return;
   2202 	}
   2203 
   2204 	if (!res->role_go) {
   2205 		/* Inform driver of the operating channel of GO. */
   2206 		wpa_drv_set_prob_oper_freq(wpa_s, res->freq);
   2207 	}
   2208 
   2209 	if (wpa_s->p2p_go_ht40)
   2210 		res->ht40 = 1;
   2211 	if (wpa_s->p2p_go_vht)
   2212 		res->vht = 1;
   2213 	res->max_oper_chwidth = wpa_s->p2p_go_max_oper_chwidth;
   2214 	res->vht_center_freq2 = wpa_s->p2p_go_vht_center_freq2;
   2215 
   2216 	wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
   2217 		       "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
   2218 		       " wps_method=%s",
   2219 		       res->role_go ? "GO" : "client", res->freq, res->ht40,
   2220 		       MAC2STR(res->peer_device_addr),
   2221 		       MAC2STR(res->peer_interface_addr),
   2222 		       p2p_wps_method_text(res->wps_method));
   2223 	wpas_notify_p2p_go_neg_completed(wpa_s, res);
   2224 
   2225 	if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
   2226 		struct wpa_ssid *ssid;
   2227 		ssid = wpa_config_get_network(wpa_s->conf,
   2228 					      wpa_s->p2p_persistent_id);
   2229 		if (ssid && ssid->disabled == 2 &&
   2230 		    ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
   2231 			size_t len = os_strlen(ssid->passphrase);
   2232 			wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
   2233 				   "on requested persistent group");
   2234 			os_memcpy(res->passphrase, ssid->passphrase, len);
   2235 			res->passphrase[len] = '\0';
   2236 		}
   2237 	}
   2238 
   2239 	if (wpa_s->create_p2p_iface) {
   2240 		group_wpa_s =
   2241 			wpas_p2p_init_group_interface(wpa_s, res->role_go);
   2242 		if (group_wpa_s == NULL) {
   2243 			wpas_p2p_remove_pending_group_interface(wpa_s);
   2244 			eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
   2245 					     wpa_s, NULL);
   2246 			wpas_p2p_group_formation_failed(wpa_s, 1);
   2247 			return;
   2248 		}
   2249 		os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
   2250 		wpa_s->pending_interface_name[0] = '\0';
   2251 	} else {
   2252 		group_wpa_s = wpa_s->parent;
   2253 		wpa_s->global->p2p_group_formation = group_wpa_s;
   2254 		if (group_wpa_s != wpa_s)
   2255 			wpas_p2p_clone_config(group_wpa_s, wpa_s);
   2256 	}
   2257 
   2258 	group_wpa_s->p2p_in_provisioning = 1;
   2259 	group_wpa_s->p2pdev = wpa_s;
   2260 	if (group_wpa_s != wpa_s) {
   2261 		os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
   2262 			  sizeof(group_wpa_s->p2p_pin));
   2263 		group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
   2264 	}
   2265 	if (res->role_go) {
   2266 		wpas_start_wps_go(group_wpa_s, res, 1);
   2267 	} else {
   2268 		os_get_reltime(&group_wpa_s->scan_min_time);
   2269 		wpas_start_wps_enrollee(group_wpa_s, res);
   2270 	}
   2271 
   2272 	wpa_s->p2p_long_listen = 0;
   2273 	eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
   2274 
   2275 	eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
   2276 	eloop_register_timeout(15 + res->peer_config_timeout / 100,
   2277 			       (res->peer_config_timeout % 100) * 10000,
   2278 			       wpas_p2p_group_formation_timeout, wpa_s, NULL);
   2279 }
   2280 
   2281 
   2282 static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id,
   2283 			       u8 go_intent)
   2284 {
   2285 	struct wpa_supplicant *wpa_s = ctx;
   2286 	wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
   2287 		       " dev_passwd_id=%u go_intent=%u", MAC2STR(src),
   2288 		       dev_passwd_id, go_intent);
   2289 
   2290 	wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id, go_intent);
   2291 }
   2292 
   2293 
   2294 static void wpas_dev_found(void *ctx, const u8 *addr,
   2295 			   const struct p2p_peer_info *info,
   2296 			   int new_device)
   2297 {
   2298 	u8 *wfd_dev_info = NULL;
   2299 	u8 wfd_dev_info_len = 0;
   2300 #ifndef CONFIG_NO_STDOUT_DEBUG
   2301 	struct wpa_supplicant *wpa_s = ctx;
   2302 	char devtype[WPS_DEV_TYPE_BUFSIZE];
   2303 	char *wfd_dev_info_hex = NULL;
   2304 
   2305 #ifdef CONFIG_WIFI_DISPLAY
   2306 	wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
   2307 						    WFD_SUBELEM_DEVICE_INFO);
   2308 	if (wfd_dev_info_hex) {
   2309 		wfd_dev_info_len = strlen(wfd_dev_info_hex) / 2;
   2310 		wfd_dev_info = os_zalloc(wfd_dev_info_len);
   2311 		// Only used for notification, so not handling error.
   2312 		hexstr2bin(wfd_dev_info_hex, wfd_dev_info, wfd_dev_info_len);
   2313 	}
   2314 #endif /* CONFIG_WIFI_DISPLAY */
   2315 
   2316 	if (info->p2ps_instance) {
   2317 		char str[256];
   2318 		const u8 *buf = wpabuf_head(info->p2ps_instance);
   2319 		size_t len = wpabuf_len(info->p2ps_instance);
   2320 
   2321 		while (len) {
   2322 			u32 id;
   2323 			u16 methods;
   2324 			u8 str_len;
   2325 
   2326 			if (len < 4 + 2 + 1)
   2327 				break;
   2328 			id = WPA_GET_LE32(buf);
   2329 			buf += sizeof(u32);
   2330 			methods = WPA_GET_BE16(buf);
   2331 			buf += sizeof(u16);
   2332 			str_len = *buf++;
   2333 			if (str_len > len - 4 - 2 - 1)
   2334 				break;
   2335 			os_memcpy(str, buf, str_len);
   2336 			str[str_len] = '\0';
   2337 			buf += str_len;
   2338 			len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
   2339 
   2340 			wpa_msg_global(wpa_s, MSG_INFO,
   2341 				       P2P_EVENT_DEVICE_FOUND MACSTR
   2342 				       " p2p_dev_addr=" MACSTR
   2343 				       " pri_dev_type=%s name='%s'"
   2344 				       " config_methods=0x%x"
   2345 				       " dev_capab=0x%x"
   2346 				       " group_capab=0x%x"
   2347 				       " adv_id=%x asp_svc=%s%s",
   2348 				       MAC2STR(addr),
   2349 				       MAC2STR(info->p2p_device_addr),
   2350 				       wps_dev_type_bin2str(
   2351 					       info->pri_dev_type,
   2352 					       devtype, sizeof(devtype)),
   2353 				       info->device_name, methods,
   2354 				       info->dev_capab, info->group_capab,
   2355 				       id, str,
   2356 				       info->vendor_elems ?
   2357 				       " vendor_elems=1" : "");
   2358 		}
   2359 		goto done;
   2360 	}
   2361 
   2362 	wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
   2363 		       " p2p_dev_addr=" MACSTR
   2364 		       " pri_dev_type=%s name='%s' config_methods=0x%x "
   2365 		       "dev_capab=0x%x group_capab=0x%x%s%s%s new=%d",
   2366 		       MAC2STR(addr), MAC2STR(info->p2p_device_addr),
   2367 		       wps_dev_type_bin2str(info->pri_dev_type, devtype,
   2368 					    sizeof(devtype)),
   2369 		       info->device_name, info->config_methods,
   2370 		       info->dev_capab, info->group_capab,
   2371 		       wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
   2372 		       wfd_dev_info_hex ? wfd_dev_info_hex : "",
   2373 		       info->vendor_elems ? " vendor_elems=1" : "",
   2374 		       new_device);
   2375 
   2376 done:
   2377 	os_free(wfd_dev_info_hex);
   2378 #endif /* CONFIG_NO_STDOUT_DEBUG */
   2379 
   2380 	wpas_notify_p2p_device_found(ctx, addr, info, wfd_dev_info,
   2381 				     wfd_dev_info_len, new_device);
   2382 	os_free(wfd_dev_info);
   2383 }
   2384 
   2385 
   2386 static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
   2387 {
   2388 	struct wpa_supplicant *wpa_s = ctx;
   2389 
   2390 	wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
   2391 		       "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
   2392 
   2393 	wpas_notify_p2p_device_lost(wpa_s, dev_addr);
   2394 }
   2395 
   2396 
   2397 static void wpas_find_stopped(void *ctx)
   2398 {
   2399 	struct wpa_supplicant *wpa_s = ctx;
   2400 
   2401 	if (wpa_s->p2p_scan_work && wpas_abort_ongoing_scan(wpa_s) < 0)
   2402 		wpa_printf(MSG_DEBUG, "P2P: Abort ongoing scan failed");
   2403 
   2404 	wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
   2405 	wpas_notify_p2p_find_stopped(wpa_s);
   2406 }
   2407 
   2408 
   2409 struct wpas_p2p_listen_work {
   2410 	unsigned int freq;
   2411 	unsigned int duration;
   2412 	struct wpabuf *probe_resp_ie;
   2413 };
   2414 
   2415 
   2416 static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
   2417 {
   2418 	if (lwork == NULL)
   2419 		return;
   2420 	wpabuf_free(lwork->probe_resp_ie);
   2421 	os_free(lwork);
   2422 }
   2423 
   2424 
   2425 static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
   2426 {
   2427 	struct wpas_p2p_listen_work *lwork;
   2428 
   2429 	if (!wpa_s->p2p_listen_work)
   2430 		return;
   2431 
   2432 	lwork = wpa_s->p2p_listen_work->ctx;
   2433 	wpas_p2p_listen_work_free(lwork);
   2434 	radio_work_done(wpa_s->p2p_listen_work);
   2435 	wpa_s->p2p_listen_work = NULL;
   2436 }
   2437 
   2438 
   2439 static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
   2440 {
   2441 	struct wpa_supplicant *wpa_s = work->wpa_s;
   2442 	struct wpas_p2p_listen_work *lwork = work->ctx;
   2443 	unsigned int duration;
   2444 
   2445 	if (deinit) {
   2446 		if (work->started) {
   2447 			wpa_s->p2p_listen_work = NULL;
   2448 			wpas_stop_listen(wpa_s);
   2449 		}
   2450 		wpas_p2p_listen_work_free(lwork);
   2451 		return;
   2452 	}
   2453 
   2454 	wpa_s->p2p_listen_work = work;
   2455 
   2456 	wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
   2457 
   2458 	if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
   2459 		wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
   2460 			   "report received Probe Request frames");
   2461 		wpas_p2p_listen_work_done(wpa_s);
   2462 		return;
   2463 	}
   2464 
   2465 	wpa_s->pending_listen_freq = lwork->freq;
   2466 	wpa_s->pending_listen_duration = lwork->duration;
   2467 
   2468 	duration = lwork->duration;
   2469 #ifdef CONFIG_TESTING_OPTIONS
   2470 	if (wpa_s->extra_roc_dur) {
   2471 		wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
   2472 			   duration, duration + wpa_s->extra_roc_dur);
   2473 		duration += wpa_s->extra_roc_dur;
   2474 	}
   2475 #endif /* CONFIG_TESTING_OPTIONS */
   2476 
   2477 	if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
   2478 		wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
   2479 			   "to remain on channel (%u MHz) for Listen "
   2480 			   "state", lwork->freq);
   2481 		wpas_p2p_listen_work_done(wpa_s);
   2482 		wpa_s->pending_listen_freq = 0;
   2483 		return;
   2484 	}
   2485 	wpa_s->off_channel_freq = 0;
   2486 	wpa_s->roc_waiting_drv_freq = lwork->freq;
   2487 }
   2488 
   2489 
   2490 static int wpas_start_listen(void *ctx, unsigned int freq,
   2491 			     unsigned int duration,
   2492 			     const struct wpabuf *probe_resp_ie)
   2493 {
   2494 	struct wpa_supplicant *wpa_s = ctx;
   2495 	struct wpas_p2p_listen_work *lwork;
   2496 
   2497 	if (wpa_s->p2p_listen_work) {
   2498 		wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
   2499 		return -1;
   2500 	}
   2501 
   2502 	lwork = os_zalloc(sizeof(*lwork));
   2503 	if (lwork == NULL)
   2504 		return -1;
   2505 	lwork->freq = freq;
   2506 	lwork->duration = duration;
   2507 	if (probe_resp_ie) {
   2508 		lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
   2509 		if (lwork->probe_resp_ie == NULL) {
   2510 			wpas_p2p_listen_work_free(lwork);
   2511 			return -1;
   2512 		}
   2513 	}
   2514 
   2515 	if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
   2516 			   lwork) < 0) {
   2517 		wpas_p2p_listen_work_free(lwork);
   2518 		return -1;
   2519 	}
   2520 
   2521 	return 0;
   2522 }
   2523 
   2524 
   2525 static void wpas_stop_listen(void *ctx)
   2526 {
   2527 	struct wpa_supplicant *wpa_s = ctx;
   2528 	if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
   2529 		wpa_drv_cancel_remain_on_channel(wpa_s);
   2530 		wpa_s->off_channel_freq = 0;
   2531 		wpa_s->roc_waiting_drv_freq = 0;
   2532 	}
   2533 	wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
   2534 
   2535 	/*
   2536 	 * Don't cancel Probe Request RX reporting for a connected P2P Client
   2537 	 * handling Probe Request frames.
   2538 	 */
   2539 	if (!wpa_s->p2p_cli_probe)
   2540 		wpa_drv_probe_req_report(wpa_s, 0);
   2541 
   2542 	wpas_p2p_listen_work_done(wpa_s);
   2543 }
   2544 
   2545 
   2546 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf,
   2547 				unsigned int freq)
   2548 {
   2549 	struct wpa_supplicant *wpa_s = ctx;
   2550 	return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1,
   2551 				 freq);
   2552 }
   2553 
   2554 
   2555 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
   2556 					 const u8 *peer, const char *params,
   2557 					 unsigned int generated_pin)
   2558 {
   2559 	wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
   2560 		       " %08d%s", MAC2STR(peer), generated_pin, params);
   2561 }
   2562 
   2563 
   2564 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
   2565 					const u8 *peer, const char *params)
   2566 {
   2567 	wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
   2568 		       "%s", MAC2STR(peer), params);
   2569 }
   2570 
   2571 
   2572 static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
   2573 			       const u8 *dev_addr, const u8 *pri_dev_type,
   2574 			       const char *dev_name, u16 supp_config_methods,
   2575 			       u8 dev_capab, u8 group_capab, const u8 *group_id,
   2576 			       size_t group_id_len)
   2577 {
   2578 	struct wpa_supplicant *wpa_s = ctx;
   2579 	char devtype[WPS_DEV_TYPE_BUFSIZE];
   2580 	char params[300];
   2581 	u8 empty_dev_type[8];
   2582 	unsigned int generated_pin = 0;
   2583 	struct wpa_supplicant *group = NULL;
   2584 	int res;
   2585 
   2586 	if (group_id) {
   2587 		for (group = wpa_s->global->ifaces; group; group = group->next)
   2588 		{
   2589 			struct wpa_ssid *s = group->current_ssid;
   2590 			if (s != NULL &&
   2591 			    s->mode == WPAS_MODE_P2P_GO &&
   2592 			    group_id_len - ETH_ALEN == s->ssid_len &&
   2593 			    os_memcmp(group_id + ETH_ALEN, s->ssid,
   2594 				      s->ssid_len) == 0)
   2595 				break;
   2596 		}
   2597 	}
   2598 
   2599 	if (pri_dev_type == NULL) {
   2600 		os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
   2601 		pri_dev_type = empty_dev_type;
   2602 	}
   2603 	res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
   2604 			  " pri_dev_type=%s name='%s' config_methods=0x%x "
   2605 			  "dev_capab=0x%x group_capab=0x%x%s%s",
   2606 			  MAC2STR(dev_addr),
   2607 			  wps_dev_type_bin2str(pri_dev_type, devtype,
   2608 					       sizeof(devtype)),
   2609 			  dev_name, supp_config_methods, dev_capab, group_capab,
   2610 			  group ? " group=" : "",
   2611 			  group ? group->ifname : "");
   2612 	if (os_snprintf_error(sizeof(params), res))
   2613 		wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
   2614 	params[sizeof(params) - 1] = '\0';
   2615 
   2616 	if (config_methods & WPS_CONFIG_DISPLAY) {
   2617 		if (wps_generate_pin(&generated_pin) < 0) {
   2618 			wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
   2619 			wpas_notify_p2p_provision_discovery(
   2620 				wpa_s, peer, 0 /* response */,
   2621 				P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
   2622 			return;
   2623 		}
   2624 		wpas_prov_disc_local_display(wpa_s, peer, params,
   2625 					     generated_pin);
   2626 	} else if (config_methods & WPS_CONFIG_KEYPAD)
   2627 		wpas_prov_disc_local_keypad(wpa_s, peer, params);
   2628 	else if (config_methods & WPS_CONFIG_PUSHBUTTON)
   2629 		wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
   2630 			       MACSTR "%s", MAC2STR(peer), params);
   2631 
   2632 	wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
   2633 					    P2P_PROV_DISC_SUCCESS,
   2634 					    config_methods, generated_pin);
   2635 }
   2636 
   2637 
   2638 static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
   2639 {
   2640 	struct wpa_supplicant *wpa_s = ctx;
   2641 	unsigned int generated_pin = 0;
   2642 	char params[20];
   2643 
   2644 	if (wpa_s->pending_pd_before_join &&
   2645 	    (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
   2646 	     os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
   2647 		wpa_s->pending_pd_before_join = 0;
   2648 		wpa_printf(MSG_DEBUG, "P2P: Starting pending "
   2649 			   "join-existing-group operation");
   2650 		wpas_p2p_join_start(wpa_s, 0, NULL, 0);
   2651 		return;
   2652 	}
   2653 
   2654 	if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
   2655 	    wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
   2656 		int res;
   2657 
   2658 		res = os_snprintf(params, sizeof(params), " peer_go=%d",
   2659 				  wpa_s->pending_pd_use == AUTO_PD_JOIN);
   2660 		if (os_snprintf_error(sizeof(params), res))
   2661 			params[sizeof(params) - 1] = '\0';
   2662 	} else
   2663 		params[0] = '\0';
   2664 
   2665 	if (config_methods & WPS_CONFIG_DISPLAY)
   2666 		wpas_prov_disc_local_keypad(wpa_s, peer, params);
   2667 	else if (config_methods & WPS_CONFIG_KEYPAD) {
   2668 		if (wps_generate_pin(&generated_pin) < 0) {
   2669 			wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
   2670 			wpas_notify_p2p_provision_discovery(
   2671 				wpa_s, peer, 0 /* response */,
   2672 				P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
   2673 			return;
   2674 		}
   2675 		wpas_prov_disc_local_display(wpa_s, peer, params,
   2676 					     generated_pin);
   2677 	} else if (config_methods & WPS_CONFIG_PUSHBUTTON)
   2678 		wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
   2679 			       MACSTR "%s", MAC2STR(peer), params);
   2680 
   2681 	wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
   2682 					    P2P_PROV_DISC_SUCCESS,
   2683 					    config_methods, generated_pin);
   2684 }
   2685 
   2686 
   2687 static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
   2688 				enum p2p_prov_disc_status status,
   2689 				u32 adv_id, const u8 *adv_mac,
   2690 				const char *deferred_session_resp)
   2691 {
   2692 	struct wpa_supplicant *wpa_s = ctx;
   2693 
   2694 	if (wpa_s->p2p_fallback_to_go_neg) {
   2695 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
   2696 			"failed - fall back to GO Negotiation");
   2697 		wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
   2698 			       P2P_EVENT_FALLBACK_TO_GO_NEG
   2699 			       "reason=PD-failed");
   2700 		wpas_p2p_fallback_to_go_neg(wpa_s, 0);
   2701 		return;
   2702 	}
   2703 
   2704 	if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
   2705 		wpa_s->pending_pd_before_join = 0;
   2706 		wpa_printf(MSG_DEBUG, "P2P: Starting pending "
   2707 			   "join-existing-group operation (no ACK for PD "
   2708 			   "Req attempts)");
   2709 		wpas_p2p_join_start(wpa_s, 0, NULL, 0);
   2710 		return;
   2711 	}
   2712 
   2713 	if (adv_id && adv_mac && deferred_session_resp) {
   2714 		wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
   2715 			       " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
   2716 			       " deferred_session_resp='%s'",
   2717 			       MAC2STR(peer), status, adv_id,
   2718 			       deferred_session_resp);
   2719 	} else if (adv_id && adv_mac) {
   2720 		wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
   2721 			       " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
   2722 			       MAC2STR(peer), status, adv_id);
   2723 	} else {
   2724 		wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
   2725 			       " p2p_dev_addr=" MACSTR " status=%d",
   2726 			       MAC2STR(peer), status);
   2727 	}
   2728 
   2729 	wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
   2730 					    status, 0, 0);
   2731 }
   2732 
   2733 
   2734 static int freq_included(struct wpa_supplicant *wpa_s,
   2735 			 const struct p2p_channels *channels,
   2736 			 unsigned int freq)
   2737 {
   2738 	if ((channels == NULL || p2p_channels_includes_freq(channels, freq)) &&
   2739 	    wpas_p2p_go_is_peer_freq(wpa_s, freq))
   2740 		return 1;
   2741 	return 0;
   2742 }
   2743 
   2744 
   2745 static void wpas_p2p_go_update_common_freqs(struct wpa_supplicant *wpa_s)
   2746 {
   2747 	unsigned int num = P2P_MAX_CHANNELS;
   2748 	int *common_freqs;
   2749 	int ret;
   2750 
   2751 	p2p_go_dump_common_freqs(wpa_s);
   2752 	common_freqs = os_calloc(num, sizeof(int));
   2753 	if (!common_freqs)
   2754 		return;
   2755 
   2756 	ret = p2p_group_get_common_freqs(wpa_s->p2p_group, common_freqs, &num);
   2757 	if (ret < 0) {
   2758 		wpa_dbg(wpa_s, MSG_DEBUG,
   2759 			"P2P: Failed to get group common freqs");
   2760 		os_free(common_freqs);
   2761 		return;
   2762 	}
   2763 
   2764 	os_free(wpa_s->p2p_group_common_freqs);
   2765 	wpa_s->p2p_group_common_freqs = common_freqs;
   2766 	wpa_s->p2p_group_common_freqs_num = num;
   2767 	p2p_go_dump_common_freqs(wpa_s);
   2768 }
   2769 
   2770 
   2771 /*
   2772  * Check if the given frequency is one of the possible operating frequencies
   2773  * set after the completion of the GO Negotiation.
   2774  */
   2775 static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq)
   2776 {
   2777 	unsigned int i;
   2778 
   2779 	p2p_go_dump_common_freqs(wpa_s);
   2780 
   2781 	/* assume no restrictions */
   2782 	if (!wpa_s->p2p_group_common_freqs_num)
   2783 		return 1;
   2784 
   2785 	for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
   2786 		if (wpa_s->p2p_group_common_freqs[i] == freq)
   2787 			return 1;
   2788 	}
   2789 	return 0;
   2790 }
   2791 
   2792 
   2793 static int wpas_sta_check_ecsa(struct hostapd_data *hapd,
   2794 			       struct sta_info *sta, void *ctx)
   2795 {
   2796 	int *ecsa_support = ctx;
   2797 
   2798 	*ecsa_support &= sta->ecsa_supported;
   2799 
   2800 	return 0;
   2801 }
   2802 
   2803 
   2804 /* Check if all the peers support eCSA */
   2805 static int wpas_p2p_go_clients_support_ecsa(struct wpa_supplicant *wpa_s)
   2806 {
   2807 	int ecsa_support = 1;
   2808 
   2809 	ap_for_each_sta(wpa_s->ap_iface->bss[0], wpas_sta_check_ecsa,
   2810 			&ecsa_support);
   2811 
   2812 	return ecsa_support;
   2813 }
   2814 
   2815 
   2816 /**
   2817  * Pick the best frequency to use from all the currently used frequencies.
   2818  */
   2819 static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
   2820 					struct wpa_used_freq_data *freqs,
   2821 					unsigned int num)
   2822 {
   2823 	unsigned int i, c;
   2824 
   2825 	/* find a candidate freq that is supported by P2P */
   2826 	for (c = 0; c < num; c++)
   2827 		if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
   2828 			break;
   2829 
   2830 	if (c == num)
   2831 		return 0;
   2832 
   2833 	/* once we have a candidate, try to find a 'better' one */
   2834 	for (i = c + 1; i < num; i++) {
   2835 		if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
   2836 			continue;
   2837 
   2838 		/*
   2839 		 * 1. Infrastructure station interfaces have higher preference.
   2840 		 * 2. P2P Clients have higher preference.
   2841 		 * 3. All others.
   2842 		 */
   2843 		if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
   2844 			c = i;
   2845 			break;
   2846 		}
   2847 
   2848 		if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
   2849 			c = i;
   2850 	}
   2851 	return freqs[c].freq;
   2852 }
   2853 
   2854 
   2855 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
   2856 				  const u8 *go_dev_addr, const u8 *ssid,
   2857 				  size_t ssid_len, int *go, u8 *group_bssid,
   2858 				  int *force_freq, int persistent_group,
   2859 				  const struct p2p_channels *channels,
   2860 				  int dev_pw_id)
   2861 {
   2862 	struct wpa_supplicant *wpa_s = ctx;
   2863 	struct wpa_ssid *s;
   2864 	struct wpa_used_freq_data *freqs;
   2865 	struct wpa_supplicant *grp;
   2866 	int best_freq;
   2867 
   2868 	if (!persistent_group) {
   2869 		wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
   2870 			   " to join an active group (SSID: %s)",
   2871 			   MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
   2872 		if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
   2873 		    (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
   2874 		     == 0 ||
   2875 		     os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
   2876 			wpa_printf(MSG_DEBUG, "P2P: Accept previously "
   2877 				   "authorized invitation");
   2878 			goto accept_inv;
   2879 		}
   2880 
   2881 #ifdef CONFIG_WPS_NFC
   2882 		if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
   2883 		    dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
   2884 			wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
   2885 			wpa_s->p2p_wps_method = WPS_NFC;
   2886 			wpa_s->pending_join_wps_method = WPS_NFC;
   2887 			os_memcpy(wpa_s->pending_join_dev_addr,
   2888 				  go_dev_addr, ETH_ALEN);
   2889 			os_memcpy(wpa_s->pending_join_iface_addr,
   2890 				  bssid, ETH_ALEN);
   2891 			goto accept_inv;
   2892 		}
   2893 #endif /* CONFIG_WPS_NFC */
   2894 
   2895 		/*
   2896 		 * Do not accept the invitation automatically; notify user and
   2897 		 * request approval.
   2898 		 */
   2899 		return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
   2900 	}
   2901 
   2902 	grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
   2903 	if (grp) {
   2904 		wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
   2905 			   "running persistent group");
   2906 		if (*go)
   2907 			os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
   2908 		goto accept_inv;
   2909 	}
   2910 
   2911 	if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
   2912 	    os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
   2913 		wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
   2914 			   "invitation to re-invoke a persistent group");
   2915 		os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
   2916 	} else if (!wpa_s->conf->persistent_reconnect)
   2917 		return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
   2918 
   2919 	for (s = wpa_s->conf->ssid; s; s = s->next) {
   2920 		if (s->disabled == 2 &&
   2921 		    os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
   2922 		    s->ssid_len == ssid_len &&
   2923 		    os_memcmp(ssid, s->ssid, ssid_len) == 0)
   2924 			break;
   2925 	}
   2926 
   2927 	if (!s) {
   2928 		wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
   2929 			   " requested reinvocation of an unknown group",
   2930 			   MAC2STR(sa));
   2931 		return P2P_SC_FAIL_UNKNOWN_GROUP;
   2932 	}
   2933 
   2934 	if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
   2935 		*go = 1;
   2936 		if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
   2937 			wpa_printf(MSG_DEBUG, "P2P: The only available "
   2938 				   "interface is already in use - reject "
   2939 				   "invitation");
   2940 			return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
   2941 		}
   2942 		if (wpa_s->p2p_mgmt)
   2943 			os_memcpy(group_bssid, wpa_s->parent->own_addr,
   2944 				  ETH_ALEN);
   2945 		else
   2946 			os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
   2947 	} else if (s->mode == WPAS_MODE_P2P_GO) {
   2948 		*go = 1;
   2949 		if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
   2950 		{
   2951 			wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
   2952 				   "interface address for the group");
   2953 			return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
   2954 		}
   2955 		os_memcpy(group_bssid, wpa_s->pending_interface_addr,
   2956 			  ETH_ALEN);
   2957 	}
   2958 
   2959 accept_inv:
   2960 	wpas_p2p_set_own_freq_preference(wpa_s, 0);
   2961 
   2962 	best_freq = 0;
   2963 	freqs = os_calloc(wpa_s->num_multichan_concurrent,
   2964 			  sizeof(struct wpa_used_freq_data));
   2965 	if (freqs) {
   2966 		int num_channels = wpa_s->num_multichan_concurrent;
   2967 		int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
   2968 		best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
   2969 		os_free(freqs);
   2970 	}
   2971 
   2972 	/* Get one of the frequencies currently in use */
   2973 	if (best_freq > 0) {
   2974 		wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
   2975 		wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
   2976 
   2977 		if (wpa_s->num_multichan_concurrent < 2 ||
   2978 		    wpas_p2p_num_unused_channels(wpa_s) < 1) {
   2979 			wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
   2980 			*force_freq = best_freq;
   2981 		}
   2982 	}
   2983 
   2984 	if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
   2985 	    wpas_p2p_num_unused_channels(wpa_s) > 0) {
   2986 		if (*go == 0) {
   2987 			/* We are the client */
   2988 			wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
   2989 				   "running a GO but we are capable of MCC, "
   2990 				   "figure out the best channel to use");
   2991 			*force_freq = 0;
   2992 		} else if (!freq_included(wpa_s, channels, *force_freq)) {
   2993 			/* We are the GO, and *force_freq is not in the
   2994 			 * intersection */
   2995 			wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
   2996 				   "in intersection but we are capable of MCC, "
   2997 				   "figure out the best channel to use",
   2998 				   *force_freq);
   2999 			*force_freq = 0;
   3000 		}
   3001 	}
   3002 
   3003 	return P2P_SC_SUCCESS;
   3004 }
   3005 
   3006 
   3007 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
   3008 				     const u8 *ssid, size_t ssid_len,
   3009 				     const u8 *go_dev_addr, u8 status,
   3010 				     int op_freq)
   3011 {
   3012 	struct wpa_supplicant *wpa_s = ctx;
   3013 	struct wpa_ssid *s;
   3014 
   3015 	for (s = wpa_s->conf->ssid; s; s = s->next) {
   3016 		if (s->disabled == 2 &&
   3017 		    s->ssid_len == ssid_len &&
   3018 		    os_memcmp(ssid, s->ssid, ssid_len) == 0)
   3019 			break;
   3020 	}
   3021 
   3022 	if (status == P2P_SC_SUCCESS) {
   3023 		wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
   3024 			   " was accepted; op_freq=%d MHz, SSID=%s",
   3025 			   MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
   3026 		if (s) {
   3027 			int go = s->mode == WPAS_MODE_P2P_GO;
   3028 			if (go) {
   3029 				wpa_msg_global(wpa_s, MSG_INFO,
   3030 					       P2P_EVENT_INVITATION_ACCEPTED
   3031 					       "sa=" MACSTR
   3032 					       " persistent=%d freq=%d",
   3033 					       MAC2STR(sa), s->id, op_freq);
   3034 			} else {
   3035 				wpa_msg_global(wpa_s, MSG_INFO,
   3036 					       P2P_EVENT_INVITATION_ACCEPTED
   3037 					       "sa=" MACSTR
   3038 					       " persistent=%d",
   3039 					       MAC2STR(sa), s->id);
   3040 			}
   3041 			wpas_p2p_group_add_persistent(
   3042 				wpa_s, s, go, 0, op_freq, 0, 0, 0, 0, NULL,
   3043 				go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0,
   3044 				1);
   3045 		} else if (bssid) {
   3046 			wpa_s->user_initiated_pd = 0;
   3047 			wpa_msg_global(wpa_s, MSG_INFO,
   3048 				       P2P_EVENT_INVITATION_ACCEPTED
   3049 				       "sa=" MACSTR " go_dev_addr=" MACSTR
   3050 				       " bssid=" MACSTR " unknown-network",
   3051 				       MAC2STR(sa), MAC2STR(go_dev_addr),
   3052 				       MAC2STR(bssid));
   3053 			wpas_p2p_join(wpa_s, bssid, go_dev_addr,
   3054 				      wpa_s->p2p_wps_method, 0, op_freq,
   3055 				      ssid, ssid_len);
   3056 		}
   3057 		return;
   3058 	}
   3059 
   3060 	if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
   3061 		wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
   3062 			   " was rejected (status %u)", MAC2STR(sa), status);
   3063 		return;
   3064 	}
   3065 
   3066 	if (!s) {
   3067 		if (bssid) {
   3068 			wpa_msg_global(wpa_s, MSG_INFO,
   3069 				       P2P_EVENT_INVITATION_RECEIVED
   3070 				       "sa=" MACSTR " go_dev_addr=" MACSTR
   3071 				       " bssid=" MACSTR " unknown-network",
   3072 				       MAC2STR(sa), MAC2STR(go_dev_addr),
   3073 				       MAC2STR(bssid));
   3074 		} else {
   3075 			wpa_msg_global(wpa_s, MSG_INFO,
   3076 				       P2P_EVENT_INVITATION_RECEIVED
   3077 				       "sa=" MACSTR " go_dev_addr=" MACSTR
   3078 				       " unknown-network",
   3079 				       MAC2STR(sa), MAC2STR(go_dev_addr));
   3080 		}
   3081 		wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr,
   3082 						    bssid, 0, op_freq);
   3083 		return;
   3084 	}
   3085 
   3086 	if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
   3087 		wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
   3088 			       "sa=" MACSTR " persistent=%d freq=%d",
   3089 			       MAC2STR(sa), s->id, op_freq);
   3090 	} else {
   3091 		wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
   3092 			       "sa=" MACSTR " persistent=%d",
   3093 			       MAC2STR(sa), s->id);
   3094 	}
   3095 	wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr, bssid,
   3096 					    s->id, op_freq);
   3097 }
   3098 
   3099 
   3100 static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
   3101 					struct wpa_ssid *ssid,
   3102 					const u8 *peer, int inv)
   3103 {
   3104 	size_t i;
   3105 	struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
   3106 
   3107 	if (ssid == NULL)
   3108 		return;
   3109 
   3110 	for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
   3111 		if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
   3112 			      ETH_ALEN) == 0)
   3113 			break;
   3114 	}
   3115 	if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
   3116 		if (ssid->mode != WPAS_MODE_P2P_GO &&
   3117 		    os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
   3118 			wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
   3119 				   "due to invitation result", ssid->id);
   3120 			wpas_notify_network_removed(wpa_s, ssid);
   3121 			wpa_config_remove_network(wpa_s->conf, ssid->id);
   3122 			return;
   3123 		}
   3124 		return; /* Peer not found in client list */
   3125 	}
   3126 
   3127 	wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
   3128 		   "group %d client list%s",
   3129 		   MAC2STR(peer), ssid->id,
   3130 		   inv ? " due to invitation result" : "");
   3131 	os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
   3132 		   ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
   3133 		   (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
   3134 	ssid->num_p2p_clients--;
   3135 	if (p2p_wpa_s->conf->update_config &&
   3136 	    wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
   3137 		wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
   3138 }
   3139 
   3140 
   3141 static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
   3142 					  const u8 *peer)
   3143 {
   3144 	struct wpa_ssid *ssid;
   3145 
   3146 	wpa_s = wpa_s->global->p2p_invite_group;
   3147 	if (wpa_s == NULL)
   3148 		return; /* No known invitation group */
   3149 	ssid = wpa_s->current_ssid;
   3150 	if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
   3151 	    !ssid->p2p_persistent_group)
   3152 		return; /* Not operating as a GO in persistent group */
   3153 	ssid = wpas_p2p_get_persistent(wpa_s->p2pdev, peer,
   3154 				       ssid->ssid, ssid->ssid_len);
   3155 	wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
   3156 }
   3157 
   3158 
   3159 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
   3160 				   const struct p2p_channels *channels,
   3161 				   const u8 *peer, int neg_freq,
   3162 				   int peer_oper_freq)
   3163 {
   3164 	struct wpa_supplicant *wpa_s = ctx;
   3165 	struct wpa_ssid *ssid;
   3166 	int freq;
   3167 
   3168 	if (bssid) {
   3169 		wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
   3170 			       "status=%d " MACSTR,
   3171 			       status, MAC2STR(bssid));
   3172 	} else {
   3173 		wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
   3174 			       "status=%d ", status);
   3175 	}
   3176 	wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
   3177 
   3178 	wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
   3179 		   status, MAC2STR(peer));
   3180 	if (wpa_s->pending_invite_ssid_id == -1) {
   3181 		struct wpa_supplicant *group_if =
   3182 			wpa_s->global->p2p_invite_group;
   3183 
   3184 		if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
   3185 			wpas_remove_persistent_client(wpa_s, peer);
   3186 
   3187 		/*
   3188 		 * Invitation to an active group. If this is successful and we
   3189 		 * are the GO, set the client wait to postpone some concurrent
   3190 		 * operations and to allow provisioning and connection to happen
   3191 		 * more quickly.
   3192 		 */
   3193 		if (status == P2P_SC_SUCCESS &&
   3194 		    group_if && group_if->current_ssid &&
   3195 		    group_if->current_ssid->mode == WPAS_MODE_P2P_GO) {
   3196 			os_get_reltime(&wpa_s->global->p2p_go_wait_client);
   3197 #ifdef CONFIG_TESTING_OPTIONS
   3198 			if (group_if->p2p_go_csa_on_inv) {
   3199 				wpa_printf(MSG_DEBUG,
   3200 					   "Testing: force P2P GO CSA after invitation");
   3201 				eloop_cancel_timeout(
   3202 					wpas_p2p_reconsider_moving_go,
   3203 					wpa_s, NULL);
   3204 				eloop_register_timeout(
   3205 					0, 50000,
   3206 					wpas_p2p_reconsider_moving_go,
   3207 					wpa_s, NULL);
   3208 			}
   3209 #endif /* CONFIG_TESTING_OPTIONS */
   3210 		}
   3211 		return;
   3212 	}
   3213 
   3214 	if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
   3215 		wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
   3216 			   "invitation exchange to indicate readiness for "
   3217 			   "re-invocation");
   3218 	}
   3219 
   3220 	if (status != P2P_SC_SUCCESS) {
   3221 		if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
   3222 			ssid = wpa_config_get_network(
   3223 				wpa_s->conf, wpa_s->pending_invite_ssid_id);
   3224 			wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
   3225 		}
   3226 		wpas_p2p_remove_pending_group_interface(wpa_s);
   3227 		return;
   3228 	}
   3229 
   3230 	ssid = wpa_config_get_network(wpa_s->conf,
   3231 				      wpa_s->pending_invite_ssid_id);
   3232 	if (ssid == NULL) {
   3233 		wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
   3234 			   "data matching with invitation");
   3235 		return;
   3236 	}
   3237 
   3238 	/*
   3239 	 * The peer could have missed our ctrl::ack frame for Invitation
   3240 	 * Response and continue retransmitting the frame. To reduce the
   3241 	 * likelihood of the peer not getting successful TX status for the
   3242 	 * Invitation Response frame, wait a short time here before starting
   3243 	 * the persistent group so that we will remain on the current channel to
   3244 	 * acknowledge any possible retransmission from the peer.
   3245 	 */
   3246 	wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
   3247 		"starting persistent group");
   3248 	os_sleep(0, 50000);
   3249 
   3250 	if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
   3251 	    freq_included(wpa_s, channels, neg_freq))
   3252 		freq = neg_freq;
   3253 	else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
   3254 		 freq_included(wpa_s, channels, peer_oper_freq))
   3255 		freq = peer_oper_freq;
   3256 	else
   3257 		freq = 0;
   3258 
   3259 	wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
   3260 		   freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
   3261 	wpas_p2p_group_add_persistent(wpa_s, ssid,
   3262 				      ssid->mode == WPAS_MODE_P2P_GO,
   3263 				      wpa_s->p2p_persistent_go_freq,
   3264 				      freq,
   3265 				      wpa_s->p2p_go_vht_center_freq2,
   3266 				      wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
   3267 				      wpa_s->p2p_go_max_oper_chwidth,
   3268 				      channels,
   3269 				      ssid->mode == WPAS_MODE_P2P_GO ?
   3270 				      P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
   3271 				      0, 1);
   3272 }
   3273 
   3274 
   3275 static int wpas_p2p_disallowed_freq(struct wpa_global *global,
   3276 				    unsigned int freq)
   3277 {
   3278 	if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
   3279 		return 1;
   3280 	return freq_range_list_includes(&global->p2p_disallow_freq, freq);
   3281 }
   3282 
   3283 
   3284 static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
   3285 {
   3286 	reg->channel[reg->channels] = chan;
   3287 	reg->channels++;
   3288 }
   3289 
   3290 
   3291 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
   3292 				     struct p2p_channels *chan,
   3293 				     struct p2p_channels *cli_chan)
   3294 {
   3295 	int i, cla = 0;
   3296 
   3297 	wpa_s->global->p2p_24ghz_social_channels = 1;
   3298 
   3299 	os_memset(cli_chan, 0, sizeof(*cli_chan));
   3300 
   3301 	wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
   3302 		   "band");
   3303 
   3304 	/* Operating class 81 - 2.4 GHz band channels 1..13 */
   3305 	chan->reg_class[cla].reg_class = 81;
   3306 	chan->reg_class[cla].channels = 0;
   3307 	for (i = 0; i < 11; i++) {
   3308 		if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
   3309 			wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
   3310 	}
   3311 	if (chan->reg_class[cla].channels)
   3312 		cla++;
   3313 
   3314 	wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
   3315 		   "band");
   3316 
   3317 	/* Operating class 115 - 5 GHz, channels 36-48 */
   3318 	chan->reg_class[cla].reg_class = 115;
   3319 	chan->reg_class[cla].channels = 0;
   3320 	if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
   3321 		wpas_p2p_add_chan(&chan->reg_class[cla], 36);
   3322 	if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
   3323 		wpas_p2p_add_chan(&chan->reg_class[cla], 40);
   3324 	if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
   3325 		wpas_p2p_add_chan(&chan->reg_class[cla], 44);
   3326 	if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
   3327 		wpas_p2p_add_chan(&chan->reg_class[cla], 48);
   3328 	if (chan->reg_class[cla].channels)
   3329 		cla++;
   3330 
   3331 	wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
   3332 		   "band");
   3333 
   3334 	/* Operating class 124 - 5 GHz, channels 149,153,157,161 */
   3335 	chan->reg_class[cla].reg_class = 124;
   3336 	chan->reg_class[cla].channels = 0;
   3337 	if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
   3338 		wpas_p2p_add_chan(&chan->reg_class[cla], 149);
   3339 	if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
   3340 		wpas_p2p_add_chan(&chan->reg_class[cla], 153);
   3341 	if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
   3342 		wpas_p2p_add_chan(&chan->reg_class[cla], 157);
   3343 	if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
   3344 		wpas_p2p_add_chan(&chan->reg_class[cla], 161);
   3345 	if (chan->reg_class[cla].channels)
   3346 		cla++;
   3347 
   3348 	chan->reg_classes = cla;
   3349 	return 0;
   3350 }
   3351 
   3352 
   3353 static int has_channel(struct wpa_global *global,
   3354 		       struct hostapd_hw_modes *mode, u8 chan, int *flags)
   3355 {
   3356 	int i;
   3357 	unsigned int freq;
   3358 
   3359 	freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
   3360 		chan * 5;
   3361 	if (wpas_p2p_disallowed_freq(global, freq))
   3362 		return NOT_ALLOWED;
   3363 
   3364 	for (i = 0; i < mode->num_channels; i++) {
   3365 		if (mode->channels[i].chan == chan) {
   3366 			if (flags)
   3367 				*flags = mode->channels[i].flag;
   3368 			if (mode->channels[i].flag &
   3369 			    (HOSTAPD_CHAN_DISABLED |
   3370 			     HOSTAPD_CHAN_RADAR))
   3371 				return NOT_ALLOWED;
   3372 			if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
   3373 				return NO_IR;
   3374 			return ALLOWED;
   3375 		}
   3376 	}
   3377 
   3378 	return NOT_ALLOWED;
   3379 }
   3380 
   3381 
   3382 static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
   3383 				     struct hostapd_hw_modes *mode,
   3384 				     u8 channel)
   3385 {
   3386 	u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
   3387 	size_t i;
   3388 
   3389 	if (mode->mode != HOSTAPD_MODE_IEEE80211A)
   3390 		return 0;
   3391 
   3392 	for (i = 0; i < ARRAY_SIZE(center_channels); i++)
   3393 		/*
   3394 		 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
   3395 		 * so the center channel is 6 channels away from the start/end.
   3396 		 */
   3397 		if (channel >= center_channels[i] - 6 &&
   3398 		    channel <= center_channels[i] + 6)
   3399 			return center_channels[i];
   3400 
   3401 	return 0;
   3402 }
   3403 
   3404 
   3405 static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
   3406 					       struct hostapd_hw_modes *mode,
   3407 					       u8 channel, u8 bw)
   3408 {
   3409 	u8 center_chan;
   3410 	int i, flags;
   3411 	enum chan_allowed res, ret = ALLOWED;
   3412 
   3413 	center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
   3414 	if (!center_chan)
   3415 		return NOT_ALLOWED;
   3416 	if (center_chan >= 58 && center_chan <= 138)
   3417 		return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
   3418 
   3419 	/* check all the channels are available */
   3420 	for (i = 0; i < 4; i++) {
   3421 		int adj_chan = center_chan - 6 + i * 4;
   3422 
   3423 		res = has_channel(wpa_s->global, mode, adj_chan, &flags);
   3424 		if (res == NOT_ALLOWED)
   3425 			return NOT_ALLOWED;
   3426 		if (res == NO_IR)
   3427 			ret = NO_IR;
   3428 
   3429 		if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
   3430 			return NOT_ALLOWED;
   3431 		if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
   3432 			return NOT_ALLOWED;
   3433 		if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
   3434 			return NOT_ALLOWED;
   3435 		if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
   3436 			return NOT_ALLOWED;
   3437 	}
   3438 
   3439 	return ret;
   3440 }
   3441 
   3442 
   3443 static int wpas_p2p_get_center_160mhz(struct wpa_supplicant *wpa_s,
   3444 				     struct hostapd_hw_modes *mode,
   3445 				     u8 channel)
   3446 {
   3447 	u8 center_channels[] = { 50, 114 };
   3448 	unsigned int i;
   3449 
   3450 	if (mode->mode != HOSTAPD_MODE_IEEE80211A)
   3451 		return 0;
   3452 
   3453 	for (i = 0; i < ARRAY_SIZE(center_channels); i++)
   3454 		/*
   3455 		 * In 160 MHz, the bandwidth "spans" 28 channels (e.g., 36-64),
   3456 		 * so the center channel is 14 channels away from the start/end.
   3457 		 */
   3458 		if (channel >= center_channels[i] - 14 &&
   3459 		    channel <= center_channels[i] + 14)
   3460 			return center_channels[i];
   3461 
   3462 	return 0;
   3463 }
   3464 
   3465 
   3466 static enum chan_allowed wpas_p2p_verify_160mhz(struct wpa_supplicant *wpa_s,
   3467 					       struct hostapd_hw_modes *mode,
   3468 					       u8 channel, u8 bw)
   3469 {
   3470 	u8 center_chan;
   3471 	int i, flags;
   3472 	enum chan_allowed res, ret = ALLOWED;
   3473 
   3474 	center_chan = wpas_p2p_get_center_160mhz(wpa_s, mode, channel);
   3475 	if (!center_chan)
   3476 		return NOT_ALLOWED;
   3477 	/* VHT 160 MHz uses DFS channels in most countries. */
   3478 
   3479 	/* Check all the channels are available */
   3480 	for (i = 0; i < 8; i++) {
   3481 		int adj_chan = center_chan - 14 + i * 4;
   3482 
   3483 		res = has_channel(wpa_s->global, mode, adj_chan, &flags);
   3484 		if (res == NOT_ALLOWED)
   3485 			return NOT_ALLOWED;
   3486 
   3487 		if (res == NO_IR)
   3488 			ret = NO_IR;
   3489 
   3490 		if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_150))
   3491 			return NOT_ALLOWED;
   3492 		if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_130))
   3493 			return NOT_ALLOWED;
   3494 		if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_110))
   3495 			return NOT_ALLOWED;
   3496 		if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_90))
   3497 			return NOT_ALLOWED;
   3498 		if (i == 4 && !(flags & HOSTAPD_CHAN_VHT_90_70))
   3499 			return NOT_ALLOWED;
   3500 		if (i == 5 && !(flags & HOSTAPD_CHAN_VHT_110_50))
   3501 			return NOT_ALLOWED;
   3502 		if (i == 6 && !(flags & HOSTAPD_CHAN_VHT_130_30))
   3503 			return NOT_ALLOWED;
   3504 		if (i == 7 && !(flags & HOSTAPD_CHAN_VHT_150_10))
   3505 			return NOT_ALLOWED;
   3506 	}
   3507 
   3508 	return ret;
   3509 }
   3510 
   3511 
   3512 static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
   3513 						 struct hostapd_hw_modes *mode,
   3514 						 u8 channel, u8 bw)
   3515 {
   3516 	int flag = 0;
   3517 	enum chan_allowed res, res2;
   3518 
   3519 	res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
   3520 	if (bw == BW40MINUS) {
   3521 		if (!(flag & HOSTAPD_CHAN_HT40MINUS))
   3522 			return NOT_ALLOWED;
   3523 		res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
   3524 	} else if (bw == BW40PLUS) {
   3525 		if (!(flag & HOSTAPD_CHAN_HT40PLUS))
   3526 			return NOT_ALLOWED;
   3527 		res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
   3528 	} else if (bw == BW80) {
   3529 		res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
   3530 	} else if (bw == BW160) {
   3531 		res2 = wpas_p2p_verify_160mhz(wpa_s, mode, channel, bw);
   3532 	}
   3533 
   3534 	if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
   3535 		return NOT_ALLOWED;
   3536 	if (res == NO_IR || res2 == NO_IR)
   3537 		return NO_IR;
   3538 	return res;
   3539 }
   3540 
   3541 
   3542 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
   3543 				   struct p2p_channels *chan,
   3544 				   struct p2p_channels *cli_chan)
   3545 {
   3546 	struct hostapd_hw_modes *mode;
   3547 	int cla, op, cli_cla;
   3548 
   3549 	if (wpa_s->hw.modes == NULL) {
   3550 		wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
   3551 			   "of all supported channels; assume dualband "
   3552 			   "support");
   3553 		return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
   3554 	}
   3555 
   3556 	cla = cli_cla = 0;
   3557 
   3558 	for (op = 0; global_op_class[op].op_class; op++) {
   3559 		const struct oper_class_map *o = &global_op_class[op];
   3560 		u8 ch;
   3561 		struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
   3562 
   3563 		if (o->p2p == NO_P2P_SUPP)
   3564 			continue;
   3565 
   3566 		mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
   3567 		if (mode == NULL)
   3568 			continue;
   3569 		if (mode->mode == HOSTAPD_MODE_IEEE80211G)
   3570 			wpa_s->global->p2p_24ghz_social_channels = 1;
   3571 		for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
   3572 			enum chan_allowed res;
   3573 			res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
   3574 			if (res == ALLOWED) {
   3575 				if (reg == NULL) {
   3576 					wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
   3577 						   o->op_class);
   3578 					reg = &chan->reg_class[cla];
   3579 					cla++;
   3580 					reg->reg_class = o->op_class;
   3581 				}
   3582 				reg->channel[reg->channels] = ch;
   3583 				reg->channels++;
   3584 			} else if (res == NO_IR &&
   3585 				   wpa_s->conf->p2p_add_cli_chan) {
   3586 				if (cli_reg == NULL) {
   3587 					wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
   3588 						   o->op_class);
   3589 					cli_reg = &cli_chan->reg_class[cli_cla];
   3590 					cli_cla++;
   3591 					cli_reg->reg_class = o->op_class;
   3592 				}
   3593 				cli_reg->channel[cli_reg->channels] = ch;
   3594 				cli_reg->channels++;
   3595 			}
   3596 		}
   3597 		if (reg) {
   3598 			wpa_hexdump(MSG_DEBUG, "P2P: Channels",
   3599 				    reg->channel, reg->channels);
   3600 		}
   3601 		if (cli_reg) {
   3602 			wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
   3603 				    cli_reg->channel, cli_reg->channels);
   3604 		}
   3605 	}
   3606 
   3607 	chan->reg_classes = cla;
   3608 	cli_chan->reg_classes = cli_cla;
   3609 
   3610 	return 0;
   3611 }
   3612 
   3613 
   3614 int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
   3615 			   struct hostapd_hw_modes *mode, u8 channel)
   3616 {
   3617 	int op;
   3618 	enum chan_allowed ret;
   3619 
   3620 	for (op = 0; global_op_class[op].op_class; op++) {
   3621 		const struct oper_class_map *o = &global_op_class[op];
   3622 		u8 ch;
   3623 
   3624 		if (o->p2p == NO_P2P_SUPP)
   3625 			continue;
   3626 
   3627 		for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
   3628 			if (o->mode != HOSTAPD_MODE_IEEE80211A ||
   3629 			    (o->bw != BW40PLUS && o->bw != BW40MINUS) ||
   3630 			    ch != channel)
   3631 				continue;
   3632 			ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
   3633 			if (ret == ALLOWED)
   3634 				return (o->bw == BW40MINUS) ? -1 : 1;
   3635 		}
   3636 	}
   3637 	return 0;
   3638 }
   3639 
   3640 
   3641 int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
   3642 			      struct hostapd_hw_modes *mode, u8 channel)
   3643 {
   3644 	if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
   3645 		return 0;
   3646 
   3647 	return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
   3648 }
   3649 
   3650 
   3651 int wpas_p2p_get_vht160_center(struct wpa_supplicant *wpa_s,
   3652 			       struct hostapd_hw_modes *mode, u8 channel)
   3653 {
   3654 	if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW160))
   3655 		return 0;
   3656 	return wpas_p2p_get_center_160mhz(wpa_s, mode, channel);
   3657 }
   3658 
   3659 
   3660 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
   3661 			size_t buf_len)
   3662 {
   3663 	struct wpa_supplicant *wpa_s = ctx;
   3664 
   3665 	for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
   3666 		if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
   3667 			break;
   3668 	}
   3669 	if (wpa_s == NULL)
   3670 		return -1;
   3671 
   3672 	return wpa_drv_get_noa(wpa_s, buf, buf_len);
   3673 }
   3674 
   3675 
   3676 struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
   3677 					      const u8 *ssid, size_t ssid_len)
   3678 {
   3679 	for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
   3680 		struct wpa_ssid *s = wpa_s->current_ssid;
   3681 		if (s == NULL)
   3682 			continue;
   3683 		if (s->mode != WPAS_MODE_P2P_GO &&
   3684 		    s->mode != WPAS_MODE_AP &&
   3685 		    s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
   3686 			continue;
   3687 		if (s->ssid_len != ssid_len ||
   3688 		    os_memcmp(ssid, s->ssid, ssid_len) != 0)
   3689 			continue;
   3690 		return wpa_s;
   3691 	}
   3692 
   3693 	return NULL;
   3694 
   3695 }
   3696 
   3697 
   3698 struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
   3699 						  const u8 *peer_dev_addr)
   3700 {
   3701 	for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
   3702 		struct wpa_ssid *ssid = wpa_s->current_ssid;
   3703 		if (ssid && (ssid->mode != WPAS_MODE_INFRA || !ssid->p2p_group))
   3704 			continue;
   3705 		if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
   3706 			return wpa_s;
   3707 	}
   3708 
   3709 	return NULL;
   3710 }
   3711 
   3712 
   3713 static int wpas_go_connected(void *ctx, const u8 *dev_addr)
   3714 {
   3715 	struct wpa_supplicant *wpa_s = ctx;
   3716 
   3717 	return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
   3718 }
   3719 
   3720 
   3721 static int wpas_is_concurrent_session_active(void *ctx)
   3722 {
   3723 	struct wpa_supplicant *wpa_s = ctx;
   3724 	struct wpa_supplicant *ifs;
   3725 
   3726 	for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
   3727 		if (ifs == wpa_s)
   3728 			continue;
   3729 		if (ifs->wpa_state > WPA_ASSOCIATED)
   3730 			return 1;
   3731 	}
   3732 	return 0;
   3733 }
   3734 
   3735 
   3736 static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
   3737 {
   3738 	struct wpa_supplicant *wpa_s = ctx;
   3739 	wpa_msg_global(wpa_s, level, "P2P: %s", msg);
   3740 }
   3741 
   3742 
   3743 int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
   3744 				  const char *conf_p2p_dev)
   3745 {
   3746 	struct wpa_interface iface;
   3747 	struct wpa_supplicant *p2pdev_wpa_s;
   3748 	char ifname[100];
   3749 	char force_name[100];
   3750 	int ret;
   3751 
   3752 	ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
   3753 			  wpa_s->ifname);
   3754 	if (os_snprintf_error(sizeof(ifname), ret))
   3755 		return -1;
   3756 	force_name[0] = '\0';
   3757 	wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
   3758 	ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
   3759 			     force_name, wpa_s->pending_interface_addr, NULL);
   3760 	if (ret < 0) {
   3761 		wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
   3762 		return ret;
   3763 	}
   3764 	os_strlcpy(wpa_s->pending_interface_name, ifname,
   3765 		   sizeof(wpa_s->pending_interface_name));
   3766 
   3767 	os_memset(&iface, 0, sizeof(iface));
   3768 	iface.p2p_mgmt = 1;
   3769 	iface.ifname = wpa_s->pending_interface_name;
   3770 	iface.driver = wpa_s->driver->name;
   3771 	iface.driver_param = wpa_s->conf->driver_param;
   3772 
   3773 	/*
   3774 	 * If a P2P Device configuration file was given, use it as the interface
   3775 	 * configuration file (instead of using parent's configuration file.
   3776 	 */
   3777 	if (conf_p2p_dev) {
   3778 		iface.confname = conf_p2p_dev;
   3779 		iface.ctrl_interface = NULL;
   3780 	} else {
   3781 		iface.confname = wpa_s->confname;
   3782 		iface.ctrl_interface = wpa_s->conf->ctrl_interface;
   3783 	}
   3784 
   3785 	p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
   3786 	if (!p2pdev_wpa_s) {
   3787 		wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
   3788 		return -1;
   3789 	}
   3790 
   3791 	p2pdev_wpa_s->p2pdev = p2pdev_wpa_s;
   3792 	wpa_s->pending_interface_name[0] = '\0';
   3793 	return 0;
   3794 }
   3795 
   3796 
   3797 static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
   3798 			       const u8 *noa, size_t noa_len)
   3799 {
   3800 	struct wpa_supplicant *wpa_s, *intf = ctx;
   3801 	char hex[100];
   3802 
   3803 	for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
   3804 		if (wpa_s->waiting_presence_resp)
   3805 			break;
   3806 	}
   3807 	if (!wpa_s) {
   3808 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
   3809 		return;
   3810 	}
   3811 	wpa_s->waiting_presence_resp = 0;
   3812 
   3813 	wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
   3814 	wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
   3815 		" status=%u noa=%s", MAC2STR(src), status, hex);
   3816 }
   3817 
   3818 
   3819 static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
   3820 				     size_t ssid_len, u8 *go_dev_addr,
   3821 				     u8 *ret_ssid, size_t *ret_ssid_len,
   3822 				     u8 *intended_iface_addr)
   3823 {
   3824 	struct wpa_supplicant *wpa_s = ctx;
   3825 	struct wpa_ssid *s;
   3826 
   3827 	s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
   3828 	if (s) {
   3829 		os_memcpy(ret_ssid, s->ssid, s->ssid_len);
   3830 		*ret_ssid_len = s->ssid_len;
   3831 		os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
   3832 
   3833 		if (s->mode != WPAS_MODE_P2P_GO) {
   3834 			os_memset(intended_iface_addr, 0, ETH_ALEN);
   3835 		} else if (wpas_p2p_create_iface(wpa_s)) {
   3836 			if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO))
   3837 				return 0;
   3838 
   3839 			os_memcpy(intended_iface_addr,
   3840 				  wpa_s->pending_interface_addr, ETH_ALEN);
   3841 		} else {
   3842 			os_memcpy(intended_iface_addr, wpa_s->own_addr,
   3843 				  ETH_ALEN);
   3844 		}
   3845 		return 1;
   3846 	}
   3847 
   3848 	return 0;
   3849 }
   3850 
   3851 
   3852 static int wpas_get_go_info(void *ctx, u8 *intended_addr,
   3853 			    u8 *ssid, size_t *ssid_len, int *group_iface,
   3854 			    unsigned int *freq)
   3855 {
   3856 	struct wpa_supplicant *wpa_s = ctx;
   3857 	struct wpa_supplicant *go;
   3858 	struct wpa_ssid *s;
   3859 
   3860 	/*
   3861 	 * group_iface will be set to 1 only if a dedicated interface for P2P
   3862 	 * role is required. First, we try to reuse an active GO. However,
   3863 	 * if it is not present, we will try to reactivate an existing
   3864 	 * persistent group and set group_iface to 1, so the caller will know
   3865 	 * that the pending interface should be used.
   3866 	 */
   3867 	*group_iface = 0;
   3868 
   3869 	if (freq)
   3870 		*freq = 0;
   3871 
   3872 	go = wpas_p2p_get_go_group(wpa_s);
   3873 	if (!go) {
   3874 		s = wpas_p2p_get_persistent_go(wpa_s);
   3875 		*group_iface = wpas_p2p_create_iface(wpa_s);
   3876 		if (s)
   3877 			os_memcpy(intended_addr, s->bssid, ETH_ALEN);
   3878 		else
   3879 			return 0;
   3880 	} else {
   3881 		s = go->current_ssid;
   3882 		os_memcpy(intended_addr, go->own_addr, ETH_ALEN);
   3883 		if (freq)
   3884 			*freq = go->assoc_freq;
   3885 	}
   3886 
   3887 	os_memcpy(ssid, s->ssid, s->ssid_len);
   3888 	*ssid_len = s->ssid_len;
   3889 
   3890 	return 1;
   3891 }
   3892 
   3893 
   3894 static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
   3895 				    const u8 *ssid, size_t ssid_len)
   3896 {
   3897 	struct wpa_supplicant *wpa_s = ctx;
   3898 	struct wpa_ssid *s;
   3899 	int save_config = 0;
   3900 	size_t i;
   3901 
   3902 	/* Start with our first choice of Persistent Groups */
   3903 	while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
   3904 		if (go && ssid && ssid_len &&
   3905 		    s->ssid_len == ssid_len &&
   3906 		    os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
   3907 		    os_memcmp(ssid, s->ssid, ssid_len) == 0)
   3908 			break;
   3909 
   3910 		/* Remove stale persistent group */
   3911 		if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
   3912 			wpa_config_remove_network(wpa_s->conf, s->id);
   3913 			save_config = 1;
   3914 			continue;
   3915 		}
   3916 
   3917 		for (i = 0; i < s->num_p2p_clients; i++) {
   3918 			if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
   3919 				      peer, ETH_ALEN) != 0)
   3920 				continue;
   3921 
   3922 			os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
   3923 				   s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
   3924 				   (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
   3925 			break;
   3926 		}
   3927 		s->num_p2p_clients--;
   3928 		save_config = 1;
   3929 	}
   3930 
   3931 	if (save_config)
   3932 		p2p_config_write(wpa_s);
   3933 
   3934 	/* Return TRUE if valid SSID remains */
   3935 	return s != NULL;
   3936 }
   3937 
   3938 
   3939 static void wpas_p2ps_get_feat_cap_str(char *buf, size_t buf_len,
   3940 				       const u8 *feat_cap, size_t feat_cap_len)
   3941 {
   3942 	static const char pref[] = " feature_cap=";
   3943 	int ret;
   3944 
   3945 	buf[0] = '\0';
   3946 
   3947 	/*
   3948 	 * We expect a feature capability to contain at least one byte to be
   3949 	 * reported. The string buffer provided by the caller function is
   3950 	 * expected to be big enough to contain all bytes of the attribute for
   3951 	 * known specifications. This function truncates the reported bytes if
   3952 	 * the feature capability data exceeds the string buffer size.
   3953 	 */
   3954 	if (!feat_cap || !feat_cap_len || buf_len < sizeof(pref) + 2)
   3955 		return;
   3956 
   3957 	os_memcpy(buf, pref, sizeof(pref));
   3958 	ret = wpa_snprintf_hex(&buf[sizeof(pref) - 1],
   3959 			       buf_len - sizeof(pref) + 1,
   3960 			       feat_cap, feat_cap_len);
   3961 
   3962 	if (ret != (2 * (int) feat_cap_len))
   3963 		wpa_printf(MSG_WARNING, "P2PS feature_cap bytes truncated");
   3964 }
   3965 
   3966 
   3967 static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
   3968 				    const u8 *adv_mac, const u8 *ses_mac,
   3969 				    const u8 *grp_mac, u32 adv_id, u32 ses_id,
   3970 				    u8 conncap, int passwd_id,
   3971 				    const u8 *persist_ssid,
   3972 				    size_t persist_ssid_size, int response_done,
   3973 				    int prov_start, const char *session_info,
   3974 				    const u8 *feat_cap, size_t feat_cap_len,
   3975 				    unsigned int freq,
   3976 				    const u8 *group_ssid, size_t group_ssid_len)
   3977 {
   3978 	struct wpa_supplicant *wpa_s = ctx;
   3979 	u8 mac[ETH_ALEN];
   3980 	struct wpa_ssid *persistent_go, *stale, *s = NULL;
   3981 	int save_config = 0;
   3982 	struct wpa_supplicant *go_wpa_s;
   3983 	char feat_cap_str[256];
   3984 
   3985 	if (!dev)
   3986 		return;
   3987 
   3988 	os_memset(mac, 0, ETH_ALEN);
   3989 	if (!adv_mac)
   3990 		adv_mac = mac;
   3991 	if (!ses_mac)
   3992 		ses_mac = mac;
   3993 	if (!grp_mac)
   3994 		grp_mac = mac;
   3995 
   3996 	wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str),
   3997 				   feat_cap, feat_cap_len);
   3998 
   3999 	if (prov_start) {
   4000 		if (session_info == NULL) {
   4001 			wpa_msg_global(wpa_s, MSG_INFO,
   4002 				       P2P_EVENT_P2PS_PROVISION_START MACSTR
   4003 				       " adv_id=%x conncap=%x"
   4004 				       " adv_mac=" MACSTR
   4005 				       " session=%x mac=" MACSTR
   4006 				       " dev_passwd_id=%d%s",
   4007 				       MAC2STR(dev), adv_id, conncap,
   4008 				       MAC2STR(adv_mac),
   4009 				       ses_id, MAC2STR(ses_mac),
   4010 				       passwd_id, feat_cap_str);
   4011 		} else {
   4012 			wpa_msg_global(wpa_s, MSG_INFO,
   4013 				       P2P_EVENT_P2PS_PROVISION_START MACSTR
   4014 				       " adv_id=%x conncap=%x"
   4015 				       " adv_mac=" MACSTR
   4016 				       " session=%x mac=" MACSTR
   4017 				       " dev_passwd_id=%d info='%s'%s",
   4018 				       MAC2STR(dev), adv_id, conncap,
   4019 				       MAC2STR(adv_mac),
   4020 				       ses_id, MAC2STR(ses_mac),
   4021 				       passwd_id, session_info, feat_cap_str);
   4022 		}
   4023 		return;
   4024 	}
   4025 
   4026 	go_wpa_s = wpas_p2p_get_go_group(wpa_s);
   4027 	persistent_go = wpas_p2p_get_persistent_go(wpa_s);
   4028 
   4029 	if (status && status != P2P_SC_SUCCESS_DEFERRED) {
   4030 		if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
   4031 			wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
   4032 
   4033 		if (persistent_go && !persistent_go->num_p2p_clients) {
   4034 			/* remove empty persistent GO */
   4035 			wpa_config_remove_network(wpa_s->conf,
   4036 						  persistent_go->id);
   4037 		}
   4038 
   4039 		wpa_msg_global(wpa_s, MSG_INFO,
   4040 			       P2P_EVENT_P2PS_PROVISION_DONE MACSTR
   4041 			       " status=%d"
   4042 			       " adv_id=%x adv_mac=" MACSTR
   4043 			       " session=%x mac=" MACSTR "%s",
   4044 			       MAC2STR(dev), status,
   4045 			       adv_id, MAC2STR(adv_mac),
   4046 			       ses_id, MAC2STR(ses_mac), feat_cap_str);
   4047 		return;
   4048 	}
   4049 
   4050 	/* Clean up stale persistent groups with this device */
   4051 	if (persist_ssid && persist_ssid_size)
   4052 		s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
   4053 					    persist_ssid_size);
   4054 
   4055 	if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO &&
   4056 	    is_zero_ether_addr(grp_mac)) {
   4057 		wpa_dbg(wpa_s, MSG_ERROR,
   4058 			"P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address");
   4059 		return;
   4060 	}
   4061 
   4062 	for (;;) {
   4063 		stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
   4064 		if (!stale)
   4065 			break;
   4066 
   4067 		if (s && s->ssid_len == stale->ssid_len &&
   4068 		    os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
   4069 		    os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
   4070 			break;
   4071 
   4072 		/* Remove stale persistent group */
   4073 		if (stale->mode != WPAS_MODE_P2P_GO ||
   4074 		    stale->num_p2p_clients <= 1) {
   4075 			wpa_config_remove_network(wpa_s->conf, stale->id);
   4076 		} else {
   4077 			size_t i;
   4078 
   4079 			for (i = 0; i < stale->num_p2p_clients; i++) {
   4080 				if (os_memcmp(stale->p2p_client_list +
   4081 					      i * ETH_ALEN,
   4082 					      dev, ETH_ALEN) == 0) {
   4083 					os_memmove(stale->p2p_client_list +
   4084 						   i * ETH_ALEN,
   4085 						   stale->p2p_client_list +
   4086 						   (i + 1) * ETH_ALEN,
   4087 						   (stale->num_p2p_clients -
   4088 						    i - 1) * ETH_ALEN);
   4089 					break;
   4090 				}
   4091 			}
   4092 			stale->num_p2p_clients--;
   4093 		}
   4094 		save_config = 1;
   4095 	}
   4096 
   4097 	if (save_config)
   4098 		p2p_config_write(wpa_s);
   4099 
   4100 	if (s) {
   4101 		if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
   4102 			wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
   4103 
   4104 		if (persistent_go && s != persistent_go &&
   4105 		    !persistent_go->num_p2p_clients) {
   4106 			/* remove empty persistent GO */
   4107 			wpa_config_remove_network(wpa_s->conf,
   4108 						  persistent_go->id);
   4109 			/* Save config */
   4110 		}
   4111 
   4112 		wpa_msg_global(wpa_s, MSG_INFO,
   4113 			       P2P_EVENT_P2PS_PROVISION_DONE MACSTR
   4114 			       " status=%d"
   4115 			       " adv_id=%x adv_mac=" MACSTR
   4116 			       " session=%x mac=" MACSTR
   4117 			       " persist=%d%s",
   4118 			       MAC2STR(dev), status,
   4119 			       adv_id, MAC2STR(adv_mac),
   4120 			       ses_id, MAC2STR(ses_mac), s->id, feat_cap_str);
   4121 		return;
   4122 	}
   4123 
   4124 	if (conncap == P2PS_SETUP_GROUP_OWNER) {
   4125 		/*
   4126 		 * We need to copy the interface name. Simply saving a
   4127 		 * pointer isn't enough, since if we use pending_interface_name
   4128 		 * it will be overwritten when the group is added.
   4129 		 */
   4130 		char go_ifname[100];
   4131 
   4132 		go_ifname[0] = '\0';
   4133 		if (!go_wpa_s) {
   4134 			wpa_s->global->pending_p2ps_group = 1;
   4135 			wpa_s->global->pending_p2ps_group_freq = freq;
   4136 
   4137 			if (!wpas_p2p_create_iface(wpa_s))
   4138 				os_memcpy(go_ifname, wpa_s->ifname,
   4139 					  sizeof(go_ifname));
   4140 			else if (wpa_s->pending_interface_name[0])
   4141 				os_memcpy(go_ifname,
   4142 					  wpa_s->pending_interface_name,
   4143 					  sizeof(go_ifname));
   4144 
   4145 			if (!go_ifname[0]) {
   4146 				wpas_p2ps_prov_complete(
   4147 					wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
   4148 					dev, adv_mac, ses_mac,
   4149 					grp_mac, adv_id, ses_id, 0, 0,
   4150 					NULL, 0, 0, 0, NULL, NULL, 0, 0,
   4151 					NULL, 0);
   4152 				return;
   4153 			}
   4154 
   4155 			/* If PD Resp complete, start up the GO */
   4156 			if (response_done && persistent_go) {
   4157 				wpas_p2p_group_add_persistent(
   4158 					wpa_s, persistent_go,
   4159 					0, 0, freq, 0, 0, 0, 0, NULL,
   4160 					persistent_go->mode ==
   4161 					WPAS_MODE_P2P_GO ?
   4162 					P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
   4163 					0, 0);
   4164 			} else if (response_done) {
   4165 				wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0);
   4166 			}
   4167 
   4168 			if (passwd_id == DEV_PW_P2PS_DEFAULT) {
   4169 				os_memcpy(wpa_s->p2ps_join_addr, grp_mac,
   4170 					  ETH_ALEN);
   4171 				wpa_s->p2ps_method_config_any = 1;
   4172 			}
   4173 		} else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
   4174 			os_memcpy(go_ifname, go_wpa_s->ifname,
   4175 				  sizeof(go_ifname));
   4176 
   4177 			if (is_zero_ether_addr(grp_mac)) {
   4178 				wpa_dbg(go_wpa_s, MSG_DEBUG,
   4179 					"P2P: Setting PIN-1 for ANY");
   4180 				wpa_supplicant_ap_wps_pin(go_wpa_s, NULL,
   4181 							  "12345670", NULL, 0,
   4182 							  0);
   4183 			} else {
   4184 				wpa_dbg(go_wpa_s, MSG_DEBUG,
   4185 					"P2P: Setting PIN-1 for " MACSTR,
   4186 					MAC2STR(grp_mac));
   4187 				wpa_supplicant_ap_wps_pin(go_wpa_s, grp_mac,
   4188 							  "12345670", NULL, 0,
   4189 							  0);
   4190 			}
   4191 
   4192 			os_memcpy(wpa_s->p2ps_join_addr, grp_mac, ETH_ALEN);
   4193 			wpa_s->p2ps_method_config_any = 1;
   4194 		}
   4195 
   4196 		wpa_msg_global(wpa_s, MSG_INFO,
   4197 			       P2P_EVENT_P2PS_PROVISION_DONE MACSTR
   4198 			       " status=%d conncap=%x"
   4199 			       " adv_id=%x adv_mac=" MACSTR
   4200 			       " session=%x mac=" MACSTR
   4201 			       " dev_passwd_id=%d go=%s%s",
   4202 			       MAC2STR(dev), status, conncap,
   4203 			       adv_id, MAC2STR(adv_mac),
   4204 			       ses_id, MAC2STR(ses_mac),
   4205 			       passwd_id, go_ifname, feat_cap_str);
   4206 		return;
   4207 	}
   4208 
   4209 	if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
   4210 		wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
   4211 
   4212 	if (persistent_go && !persistent_go->num_p2p_clients) {
   4213 		/* remove empty persistent GO */
   4214 		wpa_config_remove_network(wpa_s->conf, persistent_go->id);
   4215 	}
   4216 
   4217 	if (conncap == P2PS_SETUP_CLIENT) {
   4218 		char ssid_hex[32 * 2 + 1];
   4219 
   4220 		if (group_ssid)
   4221 			wpa_snprintf_hex(ssid_hex, sizeof(ssid_hex),
   4222 					 group_ssid, group_ssid_len);
   4223 		else
   4224 			ssid_hex[0] = '\0';
   4225 		wpa_msg_global(wpa_s, MSG_INFO,
   4226 			       P2P_EVENT_P2PS_PROVISION_DONE MACSTR
   4227 			       " status=%d conncap=%x"
   4228 			       " adv_id=%x adv_mac=" MACSTR
   4229 			       " session=%x mac=" MACSTR
   4230 			       " dev_passwd_id=%d join=" MACSTR "%s%s%s",
   4231 			       MAC2STR(dev), status, conncap,
   4232 			       adv_id, MAC2STR(adv_mac),
   4233 			       ses_id, MAC2STR(ses_mac),
   4234 			       passwd_id, MAC2STR(grp_mac), feat_cap_str,
   4235 			       group_ssid ? " group_ssid=" : "", ssid_hex);
   4236 	} else {
   4237 		wpa_msg_global(wpa_s, MSG_INFO,
   4238 			       P2P_EVENT_P2PS_PROVISION_DONE MACSTR
   4239 			       " status=%d conncap=%x"
   4240 			       " adv_id=%x adv_mac=" MACSTR
   4241 			       " session=%x mac=" MACSTR
   4242 			       " dev_passwd_id=%d%s",
   4243 			       MAC2STR(dev), status, conncap,
   4244 			       adv_id, MAC2STR(adv_mac),
   4245 			       ses_id, MAC2STR(ses_mac),
   4246 			       passwd_id, feat_cap_str);
   4247 	}
   4248 }
   4249 
   4250 
   4251 static int _wpas_p2p_in_progress(void *ctx)
   4252 {
   4253 	struct wpa_supplicant *wpa_s = ctx;
   4254 	return wpas_p2p_in_progress(wpa_s);
   4255 }
   4256 
   4257 
   4258 static int wpas_prov_disc_resp_cb(void *ctx)
   4259 {
   4260 	struct wpa_supplicant *wpa_s = ctx;
   4261 	struct wpa_ssid *persistent_go;
   4262 	unsigned int freq;
   4263 
   4264 	if (!wpa_s->global->pending_p2ps_group)
   4265 		return 0;
   4266 
   4267 	freq = wpa_s->global->pending_p2ps_group_freq;
   4268 	wpa_s->global->pending_p2ps_group_freq = 0;
   4269 	wpa_s->global->pending_p2ps_group = 0;
   4270 
   4271 	if (wpas_p2p_get_go_group(wpa_s))
   4272 		return 0;
   4273 	persistent_go = wpas_p2p_get_persistent_go(wpa_s);
   4274 
   4275 	if (persistent_go) {
   4276 		wpas_p2p_group_add_persistent(
   4277 			wpa_s, persistent_go, 0, 0, 0, 0, 0, 0, 0, NULL,
   4278 			persistent_go->mode == WPAS_MODE_P2P_GO ?
   4279 			P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0, 0);
   4280 	} else {
   4281 		wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0);
   4282 	}
   4283 
   4284 	return 1;
   4285 }
   4286 
   4287 
   4288 static int wpas_p2p_get_pref_freq_list(void *ctx, int go,
   4289 				       unsigned int *len,
   4290 				       unsigned int *freq_list)
   4291 {
   4292 	struct wpa_supplicant *wpa_s = ctx;
   4293 
   4294 	return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO :
   4295 					  WPA_IF_P2P_CLIENT, len, freq_list);
   4296 }
   4297 
   4298 
   4299 /**
   4300  * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
   4301  * @global: Pointer to global data from wpa_supplicant_init()
   4302  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
   4303  * Returns: 0 on success, -1 on failure
   4304  */
   4305 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
   4306 {
   4307 	struct p2p_config p2p;
   4308 	int i;
   4309 
   4310 	if (wpa_s->conf->p2p_disabled)
   4311 		return 0;
   4312 
   4313 	if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
   4314 		return 0;
   4315 
   4316 	if (global->p2p)
   4317 		return 0;
   4318 
   4319 	os_memset(&p2p, 0, sizeof(p2p));
   4320 	p2p.cb_ctx = wpa_s;
   4321 	p2p.debug_print = wpas_p2p_debug_print;
   4322 	p2p.p2p_scan = wpas_p2p_scan;
   4323 	p2p.send_action = wpas_send_action;
   4324 	p2p.send_action_done = wpas_send_action_done;
   4325 	p2p.go_neg_completed = wpas_go_neg_completed;
   4326 	p2p.go_neg_req_rx = wpas_go_neg_req_rx;
   4327 	p2p.dev_found = wpas_dev_found;
   4328 	p2p.dev_lost = wpas_dev_lost;
   4329 	p2p.find_stopped = wpas_find_stopped;
   4330 	p2p.start_listen = wpas_start_listen;
   4331 	p2p.stop_listen = wpas_stop_listen;
   4332 	p2p.send_probe_resp = wpas_send_probe_resp;
   4333 	p2p.sd_request = wpas_sd_request;
   4334 	p2p.sd_response = wpas_sd_response;
   4335 	p2p.prov_disc_req = wpas_prov_disc_req;
   4336 	p2p.prov_disc_resp = wpas_prov_disc_resp;
   4337 	p2p.prov_disc_fail = wpas_prov_disc_fail;
   4338 	p2p.invitation_process = wpas_invitation_process;
   4339 	p2p.invitation_received = wpas_invitation_received;
   4340 	p2p.invitation_result = wpas_invitation_result;
   4341 	p2p.get_noa = wpas_get_noa;
   4342 	p2p.go_connected = wpas_go_connected;
   4343 	p2p.presence_resp = wpas_presence_resp;
   4344 	p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
   4345 	p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
   4346 	p2p.get_persistent_group = wpas_get_persistent_group;
   4347 	p2p.get_go_info = wpas_get_go_info;
   4348 	p2p.remove_stale_groups = wpas_remove_stale_groups;
   4349 	p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
   4350 	p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
   4351 	p2p.p2ps_group_capability = p2ps_group_capability;
   4352 	p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list;
   4353 
   4354 	os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
   4355 	os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
   4356 	p2p.dev_name = wpa_s->conf->device_name;
   4357 	p2p.manufacturer = wpa_s->conf->manufacturer;
   4358 	p2p.model_name = wpa_s->conf->model_name;
   4359 	p2p.model_number = wpa_s->conf->model_number;
   4360 	p2p.serial_number = wpa_s->conf->serial_number;
   4361 	if (wpa_s->wps) {
   4362 		os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
   4363 		p2p.config_methods = wpa_s->wps->config_methods;
   4364 	}
   4365 
   4366 	if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
   4367 		wpa_printf(MSG_ERROR,
   4368 			   "P2P: Failed to configure supported channel list");
   4369 		return -1;
   4370 	}
   4371 
   4372 	if (wpa_s->conf->p2p_listen_reg_class &&
   4373 	    wpa_s->conf->p2p_listen_channel) {
   4374 		p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
   4375 		p2p.channel = wpa_s->conf->p2p_listen_channel;
   4376 		p2p.channel_forced = 1;
   4377 	} else {
   4378 		/*
   4379 		 * Pick one of the social channels randomly as the listen
   4380 		 * channel.
   4381 		 */
   4382 		if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
   4383 						 &p2p.channel) != 0) {
   4384 			wpa_printf(MSG_INFO,
   4385 				   "P2P: No social channels supported by the driver - do not enable P2P");
   4386 			return 0;
   4387 		}
   4388 		p2p.channel_forced = 0;
   4389 	}
   4390 	wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
   4391 		   p2p.reg_class, p2p.channel);
   4392 
   4393 	if (wpa_s->conf->p2p_oper_reg_class &&
   4394 	    wpa_s->conf->p2p_oper_channel) {
   4395 		p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
   4396 		p2p.op_channel = wpa_s->conf->p2p_oper_channel;
   4397 		p2p.cfg_op_channel = 1;
   4398 		wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
   4399 			   "%d:%d", p2p.op_reg_class, p2p.op_channel);
   4400 
   4401 	} else {
   4402 		/*
   4403 		 * Use random operation channel from 2.4 GHz band social
   4404 		 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
   4405 		 * other preference is indicated.
   4406 		 */
   4407 		if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
   4408 						 &p2p.op_channel) != 0) {
   4409 			wpa_printf(MSG_ERROR,
   4410 				   "P2P: Failed to select random social channel as operation channel");
   4411 			return -1;
   4412 		}
   4413 		p2p.cfg_op_channel = 0;
   4414 		wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
   4415 			   "%d:%d", p2p.op_reg_class, p2p.op_channel);
   4416 	}
   4417 
   4418 	if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
   4419 		p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
   4420 		p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
   4421 	}
   4422 
   4423 	if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
   4424 		os_memcpy(p2p.country, wpa_s->conf->country, 2);
   4425 		p2p.country[2] = 0x04;
   4426 	} else
   4427 		os_memcpy(p2p.country, "XX\x04", 3);
   4428 
   4429 	os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
   4430 		  WPS_DEV_TYPE_LEN);
   4431 
   4432 	p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
   4433 	os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
   4434 		  p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
   4435 
   4436 	p2p.concurrent_operations = !!(wpa_s->drv_flags &
   4437 				       WPA_DRIVER_FLAGS_P2P_CONCURRENT);
   4438 
   4439 	p2p.max_peers = 100;
   4440 
   4441 	if (wpa_s->conf->p2p_ssid_postfix) {
   4442 		p2p.ssid_postfix_len =
   4443 			os_strlen(wpa_s->conf->p2p_ssid_postfix);
   4444 		if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
   4445 			p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
   4446 		os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
   4447 			  p2p.ssid_postfix_len);
   4448 	}
   4449 
   4450 	p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
   4451 
   4452 	p2p.max_listen = wpa_s->max_remain_on_chan;
   4453 
   4454 	if (wpa_s->conf->p2p_passphrase_len >= 8 &&
   4455 	    wpa_s->conf->p2p_passphrase_len <= 63)
   4456 		p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
   4457 	else
   4458 		p2p.passphrase_len = 8;
   4459 
   4460 	global->p2p = p2p_init(&p2p);
   4461 	if (global->p2p == NULL)
   4462 		return -1;
   4463 	global->p2p_init_wpa_s = wpa_s;
   4464 
   4465 	for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
   4466 		if (wpa_s->conf->wps_vendor_ext[i] == NULL)
   4467 			continue;
   4468 		p2p_add_wps_vendor_extension(
   4469 			global->p2p, wpa_s->conf->wps_vendor_ext[i]);
   4470 	}
   4471 
   4472 	p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
   4473 
   4474 	return 0;
   4475 }
   4476 
   4477 
   4478 /**
   4479  * wpas_p2p_deinit - Deinitialize per-interface P2P data
   4480  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
   4481  *
   4482  * This function deinitialize per-interface P2P data.
   4483  */
   4484 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
   4485 {
   4486 	if (wpa_s->driver && wpa_s->drv_priv)
   4487 		wpa_drv_probe_req_report(wpa_s, 0);
   4488 
   4489 	if (wpa_s->go_params) {
   4490 		/* Clear any stored provisioning info */
   4491 		p2p_clear_provisioning_info(
   4492 			wpa_s->global->p2p,
   4493 			wpa_s->go_params->peer_device_addr);
   4494 	}
   4495 
   4496 	os_free(wpa_s->go_params);
   4497 	wpa_s->go_params = NULL;
   4498 	eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
   4499 	eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
   4500 	eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
   4501 	wpa_s->p2p_long_listen = 0;
   4502 	eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
   4503 	eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
   4504 	wpas_p2p_remove_pending_group_interface(wpa_s);
   4505 	eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
   4506 	wpas_p2p_listen_work_done(wpa_s);
   4507 	if (wpa_s->p2p_send_action_work) {
   4508 		os_free(wpa_s->p2p_send_action_work->ctx);
   4509 		radio_work_done(wpa_s->p2p_send_action_work);
   4510 		wpa_s->p2p_send_action_work = NULL;
   4511 	}
   4512 	eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
   4513 
   4514 	wpabuf_free(wpa_s->p2p_oob_dev_pw);
   4515 	wpa_s->p2p_oob_dev_pw = NULL;
   4516 
   4517 	os_free(wpa_s->p2p_group_common_freqs);
   4518 	wpa_s->p2p_group_common_freqs = NULL;
   4519 	wpa_s->p2p_group_common_freqs_num = 0;
   4520 
   4521 	/* TODO: remove group interface from the driver if this wpa_s instance
   4522 	 * is on top of a P2P group interface */
   4523 }
   4524 
   4525 
   4526 /**
   4527  * wpas_p2p_deinit_global - Deinitialize global P2P module
   4528  * @global: Pointer to global data from wpa_supplicant_init()
   4529  *
   4530  * This function deinitializes the global (per device) P2P module.
   4531  */
   4532 static void wpas_p2p_deinit_global(struct wpa_global *global)
   4533 {
   4534 	struct wpa_supplicant *wpa_s, *tmp;
   4535 
   4536 	wpa_s = global->ifaces;
   4537 
   4538 	wpas_p2p_service_flush(global->p2p_init_wpa_s);
   4539 
   4540 	/* Remove remaining P2P group interfaces */
   4541 	while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
   4542 		wpa_s = wpa_s->next;
   4543 	while (wpa_s) {
   4544 		tmp = global->ifaces;
   4545 		while (tmp &&
   4546 		       (tmp == wpa_s ||
   4547 			tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
   4548 			tmp = tmp->next;
   4549 		}
   4550 		if (tmp == NULL)
   4551 			break;
   4552 		/* Disconnect from the P2P group and deinit the interface */
   4553 		wpas_p2p_disconnect(tmp);
   4554 	}
   4555 
   4556 	/*
   4557 	 * Deinit GO data on any possibly remaining interface (if main
   4558 	 * interface is used as GO).
   4559 	 */
   4560 	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
   4561 		if (wpa_s->ap_iface)
   4562 			wpas_p2p_group_deinit(wpa_s);
   4563 	}
   4564 
   4565 	p2p_deinit(global->p2p);
   4566 	global->p2p = NULL;
   4567 	global->p2p_init_wpa_s = NULL;
   4568 }
   4569 
   4570 
   4571 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
   4572 {
   4573 	if (wpa_s->conf->p2p_no_group_iface)
   4574 		return 0; /* separate interface disabled per configuration */
   4575 	if (wpa_s->drv_flags &
   4576 	    (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
   4577 	     WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
   4578 		return 1; /* P2P group requires a new interface in every case
   4579 			   */
   4580 	if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
   4581 		return 0; /* driver does not support concurrent operations */
   4582 	if (wpa_s->global->ifaces->next)
   4583 		return 1; /* more that one interface already in use */
   4584 	if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
   4585 		return 1; /* this interface is already in use */
   4586 	return 0;
   4587 }
   4588 
   4589 
   4590 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
   4591 				 const u8 *peer_addr,
   4592 				 enum p2p_wps_method wps_method,
   4593 				 int go_intent, const u8 *own_interface_addr,
   4594 				 unsigned int force_freq, int persistent_group,
   4595 				 struct wpa_ssid *ssid, unsigned int pref_freq)
   4596 {
   4597 	if (persistent_group && wpa_s->conf->persistent_reconnect)
   4598 		persistent_group = 2;
   4599 
   4600 	/*
   4601 	 * Increase GO config timeout if HT40 is used since it takes some time
   4602 	 * to scan channels for coex purposes before the BSS can be started.
   4603 	 */
   4604 	p2p_set_config_timeout(wpa_s->global->p2p,
   4605 			       wpa_s->p2p_go_ht40 ? 255 : 100, 20);
   4606 
   4607 	return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
   4608 			   go_intent, own_interface_addr, force_freq,
   4609 			   persistent_group, ssid ? ssid->ssid : NULL,
   4610 			   ssid ? ssid->ssid_len : 0,
   4611 			   wpa_s->p2p_pd_before_go_neg, pref_freq,
   4612 			   wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
   4613 			   0);
   4614 }
   4615 
   4616 
   4617 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
   4618 				const u8 *peer_addr,
   4619 				enum p2p_wps_method wps_method,
   4620 				int go_intent, const u8 *own_interface_addr,
   4621 				unsigned int force_freq, int persistent_group,
   4622 				struct wpa_ssid *ssid, unsigned int pref_freq)
   4623 {
   4624 	if (persistent_group && wpa_s->conf->persistent_reconnect)
   4625 		persistent_group = 2;
   4626 
   4627 	return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
   4628 			     go_intent, own_interface_addr, force_freq,
   4629 			     persistent_group, ssid ? ssid->ssid : NULL,
   4630 			     ssid ? ssid->ssid_len : 0, pref_freq,
   4631 			     wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
   4632 			     0);
   4633 }
   4634 
   4635 
   4636 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
   4637 {
   4638 	wpa_s->p2p_join_scan_count++;
   4639 	wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
   4640 		   wpa_s->p2p_join_scan_count);
   4641 	if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
   4642 		wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
   4643 			   " for join operationg - stop join attempt",
   4644 			   MAC2STR(wpa_s->pending_join_iface_addr));
   4645 		eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
   4646 		if (wpa_s->p2p_auto_pd) {
   4647 			wpa_s->p2p_auto_pd = 0;
   4648 			wpa_msg_global(wpa_s, MSG_INFO,
   4649 				       P2P_EVENT_PROV_DISC_FAILURE
   4650 				       " p2p_dev_addr=" MACSTR " status=N/A",
   4651 				       MAC2STR(wpa_s->pending_join_dev_addr));
   4652 			return;
   4653 		}
   4654 		wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
   4655 			       P2P_EVENT_GROUP_FORMATION_FAILURE);
   4656 		wpas_notify_p2p_group_formation_failure(wpa_s, "");
   4657 	}
   4658 }
   4659 
   4660 
   4661 static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
   4662 {
   4663 	int res;
   4664 	unsigned int num, i;
   4665 	struct wpa_used_freq_data *freqs;
   4666 
   4667 	if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
   4668 		/* Multiple channels are supported and not all are in use */
   4669 		return 0;
   4670 	}
   4671 
   4672 	freqs = os_calloc(wpa_s->num_multichan_concurrent,
   4673 			  sizeof(struct wpa_used_freq_data));
   4674 	if (!freqs)
   4675 		return 1;
   4676 
   4677 	num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
   4678 					wpa_s->num_multichan_concurrent);
   4679 
   4680 	for (i = 0; i < num; i++) {
   4681 		if (freqs[i].freq == freq) {
   4682 			wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
   4683 				   freq);
   4684 			res = 0;
   4685 			goto exit_free;
   4686 		}
   4687 	}
   4688 
   4689 	wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
   4690 	res = 1;
   4691 
   4692 exit_free:
   4693 	os_free(freqs);
   4694 	return res;
   4695 }
   4696 
   4697 
   4698 static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
   4699 			    const u8 *peer_dev_addr)
   4700 {
   4701 	struct wpa_bss *bss;
   4702 	int updated;
   4703 
   4704 	bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
   4705 	if (bss == NULL)
   4706 		return -1;
   4707 	if (bss->last_update_idx < wpa_s->bss_update_idx) {
   4708 		wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
   4709 			   "last scan");
   4710 		return 0;
   4711 	}
   4712 
   4713 	updated = os_reltime_before(&wpa_s->p2p_auto_started,
   4714 				    &bss->last_update);
   4715 	wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
   4716 		   "%ld.%06ld (%supdated in last scan)",
   4717 		   bss->last_update.sec, bss->last_update.usec,
   4718 		   updated ? "": "not ");
   4719 
   4720 	return updated;
   4721 }
   4722 
   4723 
   4724 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
   4725 				   struct wpa_scan_results *scan_res)
   4726 {
   4727 	struct wpa_bss *bss = NULL;
   4728 	int freq;
   4729 	u8 iface_addr[ETH_ALEN];
   4730 
   4731 	eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
   4732 
   4733 	if (wpa_s->global->p2p_disabled)
   4734 		return;
   4735 
   4736 	wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
   4737 		   scan_res ? (int) scan_res->num : -1,
   4738 		   wpa_s->p2p_auto_join ? "auto_" : "");
   4739 
   4740 	if (scan_res)
   4741 		wpas_p2p_scan_res_handler(wpa_s, scan_res);
   4742 
   4743 	if (wpa_s->p2p_auto_pd) {
   4744 		int join = wpas_p2p_peer_go(wpa_s,
   4745 					    wpa_s->pending_join_dev_addr);
   4746 		if (join == 0 &&
   4747 		    wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
   4748 			wpa_s->auto_pd_scan_retry++;
   4749 			bss = wpa_bss_get_bssid_latest(
   4750 				wpa_s, wpa_s->pending_join_dev_addr);
   4751 			if (bss) {
   4752 				freq = bss->freq;
   4753 				wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
   4754 					   "the peer " MACSTR " at %d MHz",
   4755 					   wpa_s->auto_pd_scan_retry,
   4756 					   MAC2STR(wpa_s->
   4757 						   pending_join_dev_addr),
   4758 					   freq);
   4759 				wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
   4760 				return;
   4761 			}
   4762 		}
   4763 
   4764 		if (join < 0)
   4765 			join = 0;
   4766 
   4767 		wpa_s->p2p_auto_pd = 0;
   4768 		wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
   4769 		wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
   4770 			   MAC2STR(wpa_s->pending_join_dev_addr), join);
   4771 		if (p2p_prov_disc_req(wpa_s->global->p2p,
   4772 				      wpa_s->pending_join_dev_addr, NULL,
   4773 				      wpa_s->pending_pd_config_methods, join,
   4774 				      0, wpa_s->user_initiated_pd) < 0) {
   4775 			wpa_s->p2p_auto_pd = 0;
   4776 			wpa_msg_global(wpa_s, MSG_INFO,
   4777 				       P2P_EVENT_PROV_DISC_FAILURE
   4778 				       " p2p_dev_addr=" MACSTR " status=N/A",
   4779 				       MAC2STR(wpa_s->pending_join_dev_addr));
   4780 		}
   4781 		return;
   4782 	}
   4783 
   4784 	if (wpa_s->p2p_auto_join) {
   4785 		int join = wpas_p2p_peer_go(wpa_s,
   4786 					    wpa_s->pending_join_dev_addr);
   4787 		if (join < 0) {
   4788 			wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
   4789 				   "running a GO -> use GO Negotiation");
   4790 			wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
   4791 				       P2P_EVENT_FALLBACK_TO_GO_NEG
   4792 				       "reason=peer-not-running-GO");
   4793 			wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
   4794 					 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
   4795 					 wpa_s->p2p_persistent_group, 0, 0, 0,
   4796 					 wpa_s->p2p_go_intent,
   4797 					 wpa_s->p2p_connect_freq,
   4798 					 wpa_s->p2p_go_vht_center_freq2,
   4799 					 wpa_s->p2p_persistent_id,
   4800 					 wpa_s->p2p_pd_before_go_neg,
   4801 					 wpa_s->p2p_go_ht40,
   4802 					 wpa_s->p2p_go_vht,
   4803 					 wpa_s->p2p_go_max_oper_chwidth,
   4804 					 NULL, 0);
   4805 			return;
   4806 		}
   4807 
   4808 		wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
   4809 			   "try to join the group", join ? "" :
   4810 			   " in older scan");
   4811 		if (!join) {
   4812 			wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
   4813 				       P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
   4814 			wpa_s->p2p_fallback_to_go_neg = 1;
   4815 		}
   4816 	}
   4817 
   4818 	freq = p2p_get_oper_freq(wpa_s->global->p2p,
   4819 				 wpa_s->pending_join_iface_addr);
   4820 	if (freq < 0 &&
   4821 	    p2p_get_interface_addr(wpa_s->global->p2p,
   4822 				   wpa_s->pending_join_dev_addr,
   4823 				   iface_addr) == 0 &&
   4824 	    os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
   4825 	    && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
   4826 		wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
   4827 			   "address for join from " MACSTR " to " MACSTR
   4828 			   " based on newly discovered P2P peer entry",
   4829 			   MAC2STR(wpa_s->pending_join_iface_addr),
   4830 			   MAC2STR(iface_addr));
   4831 		os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
   4832 			  ETH_ALEN);
   4833 
   4834 		freq = p2p_get_oper_freq(wpa_s->global->p2p,
   4835 					 wpa_s->pending_join_iface_addr);
   4836 	}
   4837 	if (freq >= 0) {
   4838 		wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
   4839 			   "from P2P peer table: %d MHz", freq);
   4840 	}
   4841 	if (wpa_s->p2p_join_ssid_len) {
   4842 		wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
   4843 			   MACSTR " and SSID %s",
   4844 			   MAC2STR(wpa_s->pending_join_iface_addr),
   4845 			   wpa_ssid_txt(wpa_s->p2p_join_ssid,
   4846 					wpa_s->p2p_join_ssid_len));
   4847 		bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
   4848 				  wpa_s->p2p_join_ssid,
   4849 				  wpa_s->p2p_join_ssid_len);
   4850 	} else if (!bss) {
   4851 		wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
   4852 			   MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
   4853 		bss = wpa_bss_get_bssid_latest(wpa_s,
   4854 					       wpa_s->pending_join_iface_addr);
   4855 	}
   4856 	if (bss) {
   4857 		u8 dev_addr[ETH_ALEN];
   4858 
   4859 		freq = bss->freq;
   4860 		wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
   4861 			   "from BSS table: %d MHz (SSID %s)", freq,
   4862 			   wpa_ssid_txt(bss->ssid, bss->ssid_len));
   4863 		if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
   4864 				       dev_addr) == 0 &&
   4865 		    os_memcmp(wpa_s->pending_join_dev_addr,
   4866 			      wpa_s->pending_join_iface_addr, ETH_ALEN) == 0 &&
   4867 		    os_memcmp(dev_addr, wpa_s->pending_join_dev_addr,
   4868 			      ETH_ALEN) != 0) {
   4869 			wpa_printf(MSG_DEBUG,
   4870 				   "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
   4871 				   MAC2STR(dev_addr),
   4872 				   MAC2STR(wpa_s->pending_join_dev_addr));
   4873 			os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
   4874 				  ETH_ALEN);
   4875 		}
   4876 	}
   4877 	if (freq > 0) {
   4878 		u16 method;
   4879 
   4880 		if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
   4881 			wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
   4882 				       P2P_EVENT_GROUP_FORMATION_FAILURE
   4883 				       "reason=FREQ_CONFLICT");
   4884 			wpas_notify_p2p_group_formation_failure(
   4885 				wpa_s, "FREQ_CONFLICT");
   4886 			return;
   4887 		}
   4888 
   4889 		wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
   4890 			   "prior to joining an existing group (GO " MACSTR
   4891 			   " freq=%u MHz)",
   4892 			   MAC2STR(wpa_s->pending_join_dev_addr), freq);
   4893 		wpa_s->pending_pd_before_join = 1;
   4894 
   4895 		switch (wpa_s->pending_join_wps_method) {
   4896 		case WPS_PIN_DISPLAY:
   4897 			method = WPS_CONFIG_KEYPAD;
   4898 			break;
   4899 		case WPS_PIN_KEYPAD:
   4900 			method = WPS_CONFIG_DISPLAY;
   4901 			break;
   4902 		case WPS_PBC:
   4903 			method = WPS_CONFIG_PUSHBUTTON;
   4904 			break;
   4905 		case WPS_P2PS:
   4906 			method = WPS_CONFIG_P2PS;
   4907 			break;
   4908 		default:
   4909 			method = 0;
   4910 			break;
   4911 		}
   4912 
   4913 		if ((p2p_get_provisioning_info(wpa_s->global->p2p,
   4914 					       wpa_s->pending_join_dev_addr) ==
   4915 		     method)) {
   4916 			/*
   4917 			 * We have already performed provision discovery for
   4918 			 * joining the group. Proceed directly to join
   4919 			 * operation without duplicated provision discovery. */
   4920 			wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
   4921 				   "with " MACSTR " already done - proceed to "
   4922 				   "join",
   4923 				   MAC2STR(wpa_s->pending_join_dev_addr));
   4924 			wpa_s->pending_pd_before_join = 0;
   4925 			goto start;
   4926 		}
   4927 
   4928 		if (p2p_prov_disc_req(wpa_s->global->p2p,
   4929 				      wpa_s->pending_join_dev_addr,
   4930 				      NULL, method, 1,
   4931 				      freq, wpa_s->user_initiated_pd) < 0) {
   4932 			wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
   4933 				   "Discovery Request before joining an "
   4934 				   "existing group");
   4935 			wpa_s->pending_pd_before_join = 0;
   4936 			goto start;
   4937 		}
   4938 		return;
   4939 	}
   4940 
   4941 	wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
   4942 	eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
   4943 	eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
   4944 	wpas_p2p_check_join_scan_limit(wpa_s);
   4945 	return;
   4946 
   4947 start:
   4948 	/* Start join operation immediately */
   4949 	wpas_p2p_join_start(wpa_s, 0, wpa_s->p2p_join_ssid,
   4950 			    wpa_s->p2p_join_ssid_len);
   4951 }
   4952 
   4953 
   4954 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
   4955 				   const u8 *ssid, size_t ssid_len)
   4956 {
   4957 	int ret;
   4958 	struct wpa_driver_scan_params params;
   4959 	struct wpabuf *wps_ie, *ies;
   4960 	size_t ielen;
   4961 	int freqs[2] = { 0, 0 };
   4962 	unsigned int bands;
   4963 
   4964 	os_memset(&params, 0, sizeof(params));
   4965 
   4966 	/* P2P Wildcard SSID */
   4967 	params.num_ssids = 1;
   4968 	if (ssid && ssid_len) {
   4969 		params.ssids[0].ssid = ssid;
   4970 		params.ssids[0].ssid_len = ssid_len;
   4971 		os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
   4972 		wpa_s->p2p_join_ssid_len = ssid_len;
   4973 	} else {
   4974 		params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
   4975 		params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
   4976 		wpa_s->p2p_join_ssid_len = 0;
   4977 	}
   4978 
   4979 	wpa_s->wps->dev.p2p = 1;
   4980 	wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
   4981 					wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
   4982 					NULL);
   4983 	if (wps_ie == NULL) {
   4984 		wpas_p2p_scan_res_join(wpa_s, NULL);
   4985 		return;
   4986 	}
   4987 
   4988 	if (!freq) {
   4989 		int oper_freq;
   4990 		/*
   4991 		 * If freq is not provided, check the operating freq of the GO
   4992 		 * and use a single channel scan on if possible.
   4993 		 */
   4994 		oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
   4995 					      wpa_s->pending_join_iface_addr);
   4996 		if (oper_freq > 0)
   4997 			freq = oper_freq;
   4998 	}
   4999 	if (freq > 0) {
   5000 		freqs[0] = freq;
   5001 		params.freqs = freqs;
   5002 	}
   5003 
   5004 	ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
   5005 	ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
   5006 	if (ies == NULL) {
   5007 		wpabuf_free(wps_ie);
   5008 		wpas_p2p_scan_res_join(wpa_s, NULL);
   5009 		return;
   5010 	}
   5011 	wpabuf_put_buf(ies, wps_ie);
   5012 	wpabuf_free(wps_ie);
   5013 
   5014 	bands = wpas_get_bands(wpa_s, freqs);
   5015 	p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
   5016 
   5017 	params.p2p_probe = 1;
   5018 	params.extra_ies = wpabuf_head(ies);
   5019 	params.extra_ies_len = wpabuf_len(ies);
   5020 
   5021 	if (wpa_s->clear_driver_scan_cache) {
   5022 		wpa_printf(MSG_DEBUG,
   5023 			   "Request driver to clear scan cache due to local BSS flush");
   5024 		params.only_new_results = 1;
   5025 	}
   5026 
   5027 	/*
   5028 	 * Run a scan to update BSS table and start Provision Discovery once
   5029 	 * the new scan results become available.
   5030 	 */
   5031 	ret = wpa_drv_scan(wpa_s, &params);
   5032 	if (!ret) {
   5033 		os_get_reltime(&wpa_s->scan_trigger_time);
   5034 		wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
   5035 		wpa_s->own_scan_requested = 1;
   5036 		wpa_s->clear_driver_scan_cache = 0;
   5037 	}
   5038 
   5039 	wpabuf_free(ies);
   5040 
   5041 	if (ret) {
   5042 		wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
   5043 			   "try again later");
   5044 		eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
   5045 		eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
   5046 		wpas_p2p_check_join_scan_limit(wpa_s);
   5047 	}
   5048 }
   5049 
   5050 
   5051 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
   5052 {
   5053 	struct wpa_supplicant *wpa_s = eloop_ctx;
   5054 	wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
   5055 }
   5056 
   5057 
   5058 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
   5059 			 const u8 *dev_addr, enum p2p_wps_method wps_method,
   5060 			 int auto_join, int op_freq,
   5061 			 const u8 *ssid, size_t ssid_len)
   5062 {
   5063 	wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
   5064 		   MACSTR " dev " MACSTR " op_freq=%d)%s",
   5065 		   MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
   5066 		   auto_join ? " (auto_join)" : "");
   5067 	if (ssid && ssid_len) {
   5068 		wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
   5069 			   wpa_ssid_txt(ssid, ssid_len));
   5070 	}
   5071 
   5072 	wpa_s->p2p_auto_pd = 0;
   5073 	wpa_s->p2p_auto_join = !!auto_join;
   5074 	os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
   5075 	os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
   5076 	wpa_s->pending_join_wps_method = wps_method;
   5077 
   5078 	/* Make sure we are not running find during connection establishment */
   5079 	wpas_p2p_stop_find(wpa_s);
   5080 
   5081 	wpa_s->p2p_join_scan_count = 0;
   5082 	wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
   5083 	return 0;
   5084 }
   5085 
   5086 
   5087 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
   5088 			       const u8 *ssid, size_t ssid_len)
   5089 {
   5090 	struct wpa_supplicant *group;
   5091 	struct p2p_go_neg_results res;
   5092 	struct wpa_bss *bss;
   5093 
   5094 	group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
   5095 	if (group == NULL)
   5096 		return -1;
   5097 	if (group != wpa_s) {
   5098 		os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
   5099 			  sizeof(group->p2p_pin));
   5100 		group->p2p_wps_method = wpa_s->p2p_wps_method;
   5101 	} else {
   5102 		/*
   5103 		 * Need to mark the current interface for p2p_group_formation
   5104 		 * when a separate group interface is not used. This is needed
   5105 		 * to allow p2p_cancel stop a pending p2p_connect-join.
   5106 		 * wpas_p2p_init_group_interface() addresses this for the case
   5107 		 * where a separate group interface is used.
   5108 		 */
   5109 		wpa_s->global->p2p_group_formation = wpa_s;
   5110 	}
   5111 
   5112 	group->p2p_in_provisioning = 1;
   5113 	group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
   5114 
   5115 	os_memset(&res, 0, sizeof(res));
   5116 	os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
   5117 	os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
   5118 		  ETH_ALEN);
   5119 	res.wps_method = wpa_s->pending_join_wps_method;
   5120 	if (freq && ssid && ssid_len) {
   5121 		res.freq = freq;
   5122 		res.ssid_len = ssid_len;
   5123 		os_memcpy(res.ssid, ssid, ssid_len);
   5124 	} else {
   5125 		if (ssid && ssid_len) {
   5126 			bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
   5127 					  ssid, ssid_len);
   5128 		} else {
   5129 			bss = wpa_bss_get_bssid_latest(
   5130 				wpa_s, wpa_s->pending_join_iface_addr);
   5131 		}
   5132 		if (bss) {
   5133 			res.freq = bss->freq;
   5134 			res.ssid_len = bss->ssid_len;
   5135 			os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
   5136 			wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
   5137 				   bss->freq,
   5138 				   wpa_ssid_txt(bss->ssid, bss->ssid_len));
   5139 		} else if (ssid && ssid_len) {
   5140 			res.ssid_len = ssid_len;
   5141 			os_memcpy(res.ssid, ssid, ssid_len);
   5142 			wpa_printf(MSG_DEBUG, "P2P: Join target GO (SSID %s)",
   5143 				   wpa_ssid_txt(ssid, ssid_len));
   5144 		}
   5145 	}
   5146 
   5147 	if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
   5148 		wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
   5149 			   "starting client");
   5150 		wpa_drv_cancel_remain_on_channel(wpa_s);
   5151 		wpa_s->off_channel_freq = 0;
   5152 		wpa_s->roc_waiting_drv_freq = 0;
   5153 	}
   5154 	wpas_start_wps_enrollee(group, &res);
   5155 
   5156 	/*
   5157 	 * Allow a longer timeout for join-a-running-group than normal 15
   5158 	 * second group formation timeout since the GO may not have authorized
   5159 	 * our connection yet.
   5160 	 */
   5161 	eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
   5162 	eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
   5163 			       wpa_s, NULL);
   5164 
   5165 	return 0;
   5166 }
   5167 
   5168 
   5169 static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
   5170 				int *force_freq, int *pref_freq, int go,
   5171 				unsigned int *pref_freq_list,
   5172 				unsigned int *num_pref_freq)
   5173 {
   5174 	struct wpa_used_freq_data *freqs;
   5175 	int res, best_freq, num_unused;
   5176 	unsigned int freq_in_use = 0, num, i, max_pref_freq;
   5177 
   5178 	max_pref_freq = *num_pref_freq;
   5179 	*num_pref_freq = 0;
   5180 
   5181 	freqs = os_calloc(wpa_s->num_multichan_concurrent,
   5182 			  sizeof(struct wpa_used_freq_data));
   5183 	if (!freqs)
   5184 		return -1;
   5185 
   5186 	num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
   5187 					wpa_s->num_multichan_concurrent);
   5188 
   5189 	/*
   5190 	 * It is possible that the total number of used frequencies is bigger
   5191 	 * than the number of frequencies used for P2P, so get the system wide
   5192 	 * number of unused frequencies.
   5193 	 */
   5194 	num_unused = wpas_p2p_num_unused_channels(wpa_s);
   5195 
   5196 	wpa_printf(MSG_DEBUG,
   5197 		   "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
   5198 		   freq, wpa_s->num_multichan_concurrent, num, num_unused);
   5199 
   5200 	if (freq > 0) {
   5201 		int ret;
   5202 		if (go)
   5203 			ret = p2p_supported_freq(wpa_s->global->p2p, freq);
   5204 		else
   5205 			ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
   5206 		if (!ret) {
   5207 			if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
   5208 			    ieee80211_is_dfs(freq)) {
   5209 				/*
   5210 				 * If freq is a DFS channel and DFS is offloaded
   5211 				 * to the driver, allow P2P GO to use it.
   5212 				 */
   5213 				wpa_printf(MSG_DEBUG,
   5214 					   "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
   5215 					   freq);
   5216 			} else {
   5217 				wpa_printf(MSG_DEBUG,
   5218 					   "P2P: The forced channel (%u MHz) is not supported for P2P uses",
   5219 					   freq);
   5220 				res = -3;
   5221 				goto exit_free;
   5222 			}
   5223 		}
   5224 
   5225 		for (i = 0; i < num; i++) {
   5226 			if (freqs[i].freq == freq)
   5227 				freq_in_use = 1;
   5228 		}
   5229 
   5230 		if (num_unused <= 0 && !freq_in_use) {
   5231 			wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
   5232 				   freq);
   5233 			res = -2;
   5234 			goto exit_free;
   5235 		}
   5236 		wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
   5237 			   "requested channel (%u MHz)", freq);
   5238 		*force_freq = freq;
   5239 		goto exit_ok;
   5240 	}
   5241 
   5242 	best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
   5243 
   5244 	if (!wpa_s->conf->num_p2p_pref_chan && *pref_freq == 0) {
   5245 		enum wpa_driver_if_type iface_type;
   5246 
   5247 		if (go)
   5248 			iface_type = WPA_IF_P2P_GO;
   5249 		else
   5250 			iface_type = WPA_IF_P2P_CLIENT;
   5251 
   5252 		wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
   5253 			   best_freq, go);
   5254 
   5255 		res = wpa_drv_get_pref_freq_list(wpa_s, iface_type,
   5256 						 &max_pref_freq,
   5257 						 pref_freq_list);
   5258 		if (!res && max_pref_freq > 0) {
   5259 			*num_pref_freq = max_pref_freq;
   5260 			i = 0;
   5261 			while (i < *num_pref_freq &&
   5262 			       (!p2p_supported_freq(wpa_s->global->p2p,
   5263 						    pref_freq_list[i]) ||
   5264 				wpas_p2p_disallowed_freq(wpa_s->global,
   5265 							 pref_freq_list[i]))) {
   5266 				wpa_printf(MSG_DEBUG,
   5267 					   "P2P: preferred_freq_list[%d]=%d is disallowed",
   5268 					   i, pref_freq_list[i]);
   5269 				i++;
   5270 			}
   5271 			if (i != *num_pref_freq) {
   5272 				best_freq = pref_freq_list[i];
   5273 				wpa_printf(MSG_DEBUG,
   5274 					   "P2P: Using preferred_freq_list[%d]=%d",
   5275 					   i, best_freq);
   5276 			} else {
   5277 				wpa_printf(MSG_DEBUG,
   5278 					   "P2P: All driver preferred frequencies are disallowed for P2P use");
   5279 				*num_pref_freq = 0;
   5280 			}
   5281 		} else {
   5282 			wpa_printf(MSG_DEBUG,
   5283 				   "P2P: No preferred frequency list available");
   5284 		}
   5285 	}
   5286 
   5287 	/* We have a candidate frequency to use */
   5288 	if (best_freq > 0) {
   5289 		if (*pref_freq == 0 && num_unused > 0) {
   5290 			wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
   5291 				   best_freq);
   5292 			*pref_freq = best_freq;
   5293 		} else {
   5294 			wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
   5295 				   best_freq);
   5296 			*force_freq = best_freq;
   5297 		}
   5298 	} else if (num_unused > 0) {
   5299 		wpa_printf(MSG_DEBUG,
   5300 			   "P2P: Current operating channels are not available for P2P. Try to use another channel");
   5301 		*force_freq = 0;
   5302 	} else {
   5303 		wpa_printf(MSG_DEBUG,
   5304 			   "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
   5305 		res = -2;
   5306 		goto exit_free;
   5307 	}
   5308 
   5309 exit_ok:
   5310 	res = 0;
   5311 exit_free:
   5312 	os_free(freqs);
   5313 	return res;
   5314 }
   5315 
   5316 
   5317 /**
   5318  * wpas_p2p_connect - Request P2P Group Formation to be started
   5319  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
   5320  * @peer_addr: Address of the peer P2P Device
   5321  * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
   5322  * @persistent_group: Whether to create a persistent group
   5323  * @auto_join: Whether to select join vs. GO Negotiation automatically
   5324  * @join: Whether to join an existing group (as a client) instead of starting
   5325  *	Group Owner negotiation; @peer_addr is BSSID in that case
   5326  * @auth: Whether to only authorize the connection instead of doing that and
   5327  *	initiating Group Owner negotiation
   5328  * @go_intent: GO Intent or -1 to use default
   5329  * @freq: Frequency for the group or 0 for auto-selection
   5330  * @freq2: Center frequency of segment 1 for the GO operating in VHT 80P80 mode
   5331  * @persistent_id: Persistent group credentials to use for forcing GO
   5332  *	parameters or -1 to generate new values (SSID/passphrase)
   5333  * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
   5334  *	interoperability workaround when initiating group formation
   5335  * @ht40: Start GO with 40 MHz channel width
   5336  * @vht:  Start GO with VHT support
   5337  * @vht_chwidth: Channel width supported by GO operating with VHT support
   5338  *	(VHT_CHANWIDTH_*).
   5339  * @group_ssid: Specific Group SSID for join or %NULL if not set
   5340  * @group_ssid_len: Length of @group_ssid in octets
   5341  * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
   5342  *	failure, -2 on failure due to channel not currently available,
   5343  *	-3 if forced channel is not supported
   5344  */
   5345 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
   5346 		     const char *pin, enum p2p_wps_method wps_method,
   5347 		     int persistent_group, int auto_join, int join, int auth,
   5348 		     int go_intent, int freq, unsigned int vht_center_freq2,
   5349 		     int persistent_id, int pd, int ht40, int vht,
   5350 		     unsigned int vht_chwidth, const u8 *group_ssid,
   5351 		     size_t group_ssid_len)
   5352 {
   5353 	int force_freq = 0, pref_freq = 0;
   5354 	int ret = 0, res;
   5355 	enum wpa_driver_if_type iftype;
   5356 	const u8 *if_addr;
   5357 	struct wpa_ssid *ssid = NULL;
   5358 	unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
   5359 
   5360 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
   5361 		return -1;
   5362 
   5363 	if (persistent_id >= 0) {
   5364 		ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
   5365 		if (ssid == NULL || ssid->disabled != 2 ||
   5366 		    ssid->mode != WPAS_MODE_P2P_GO)
   5367 			return -1;
   5368 	}
   5369 
   5370 	os_free(wpa_s->global->add_psk);
   5371 	wpa_s->global->add_psk = NULL;
   5372 
   5373 	wpa_s->global->p2p_fail_on_wps_complete = 0;
   5374 	wpa_s->global->pending_p2ps_group = 0;
   5375 	wpa_s->global->pending_p2ps_group_freq = 0;
   5376 	wpa_s->p2ps_method_config_any = 0;
   5377 
   5378 	if (go_intent < 0)
   5379 		go_intent = wpa_s->conf->p2p_go_intent;
   5380 
   5381 	if (!auth)
   5382 		wpa_s->p2p_long_listen = 0;
   5383 
   5384 	wpa_s->p2p_wps_method = wps_method;
   5385 	wpa_s->p2p_persistent_group = !!persistent_group;
   5386 	wpa_s->p2p_persistent_id = persistent_id;
   5387 	wpa_s->p2p_go_intent = go_intent;
   5388 	wpa_s->p2p_connect_freq = freq;
   5389 	wpa_s->p2p_fallback_to_go_neg = 0;
   5390 	wpa_s->p2p_pd_before_go_neg = !!pd;
   5391 	wpa_s->p2p_go_ht40 = !!ht40;
   5392 	wpa_s->p2p_go_vht = !!vht;
   5393 	wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
   5394 	wpa_s->p2p_go_max_oper_chwidth = vht_chwidth;
   5395 
   5396 	if (pin)
   5397 		os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
   5398 	else if (wps_method == WPS_PIN_DISPLAY) {
   5399 		if (wps_generate_pin((unsigned int *) &ret) < 0)
   5400 			return -1;
   5401 		res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
   5402 				  "%08d", ret);
   5403 		if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
   5404 			wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
   5405 		wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
   5406 			   wpa_s->p2p_pin);
   5407 	} else if (wps_method == WPS_P2PS) {
   5408 		/* Force the P2Ps default PIN to be used */
   5409 		os_strlcpy(wpa_s->p2p_pin, "12345670", sizeof(wpa_s->p2p_pin));
   5410 	} else
   5411 		wpa_s->p2p_pin[0] = '\0';
   5412 
   5413 	if (join || auto_join) {
   5414 		u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
   5415 		if (auth) {
   5416 			wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
   5417 				   "connect a running group from " MACSTR,
   5418 				   MAC2STR(peer_addr));
   5419 			os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
   5420 			return ret;
   5421 		}
   5422 		os_memcpy(dev_addr, peer_addr, ETH_ALEN);
   5423 		if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
   5424 					   iface_addr) < 0) {
   5425 			os_memcpy(iface_addr, peer_addr, ETH_ALEN);
   5426 			p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
   5427 					 dev_addr);
   5428 		}
   5429 		if (auto_join) {
   5430 			os_get_reltime(&wpa_s->p2p_auto_started);
   5431 			wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
   5432 				   "%ld.%06ld",
   5433 				   wpa_s->p2p_auto_started.sec,
   5434 				   wpa_s->p2p_auto_started.usec);
   5435 		}
   5436 		wpa_s->user_initiated_pd = 1;
   5437 		if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
   5438 				  auto_join, freq,
   5439 				  group_ssid, group_ssid_len) < 0)
   5440 			return -1;
   5441 		return ret;
   5442 	}
   5443 
   5444 	size = P2P_MAX_PREF_CHANNELS;
   5445 	res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
   5446 				   go_intent == 15, pref_freq_list, &size);
   5447 	if (res)
   5448 		return res;
   5449 	wpas_p2p_set_own_freq_preference(wpa_s,
   5450 					 force_freq ? force_freq : pref_freq);
   5451 
   5452 	p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
   5453 
   5454 	wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
   5455 
   5456 	if (wpa_s->create_p2p_iface) {
   5457 		/* Prepare to add a new interface for the group */
   5458 		iftype = WPA_IF_P2P_GROUP;
   5459 		if (go_intent == 15)
   5460 			iftype = WPA_IF_P2P_GO;
   5461 		if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
   5462 			wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
   5463 				   "interface for the group");
   5464 			return -1;
   5465 		}
   5466 
   5467 		if_addr = wpa_s->pending_interface_addr;
   5468 	} else {
   5469 		if (wpa_s->p2p_mgmt)
   5470 			if_addr = wpa_s->parent->own_addr;
   5471 		else
   5472 			if_addr = wpa_s->own_addr;
   5473 		os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
   5474 	}
   5475 
   5476 	if (auth) {
   5477 		if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
   5478 					 go_intent, if_addr,
   5479 					 force_freq, persistent_group, ssid,
   5480 					 pref_freq) < 0)
   5481 			return -1;
   5482 		return ret;
   5483 	}
   5484 
   5485 	if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
   5486 				  go_intent, if_addr, force_freq,
   5487 				  persistent_group, ssid, pref_freq) < 0) {
   5488 		if (wpa_s->create_p2p_iface)
   5489 			wpas_p2p_remove_pending_group_interface(wpa_s);
   5490 		return -1;
   5491 	}
   5492 	return ret;
   5493 }
   5494 
   5495 
   5496 /**
   5497  * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
   5498  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
   5499  * @freq: Frequency of the channel in MHz
   5500  * @duration: Duration of the stay on the channel in milliseconds
   5501  *
   5502  * This callback is called when the driver indicates that it has started the
   5503  * requested remain-on-channel duration.
   5504  */
   5505 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
   5506 				   unsigned int freq, unsigned int duration)
   5507 {
   5508 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
   5509 		return;
   5510 	wpa_printf(MSG_DEBUG, "P2P: remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d roc_waiting_drv_freq=%d freq=%u duration=%u)",
   5511 		   wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
   5512 		   wpa_s->roc_waiting_drv_freq, freq, duration);
   5513 	if (wpa_s->off_channel_freq &&
   5514 	    wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
   5515 		p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
   5516 			      wpa_s->pending_listen_duration);
   5517 		wpa_s->pending_listen_freq = 0;
   5518 	} else {
   5519 		wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
   5520 			   wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
   5521 			   freq, duration);
   5522 	}
   5523 }
   5524 
   5525 
   5526 int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
   5527 {
   5528 	/* Limit maximum Listen state time based on driver limitation. */
   5529 	if (timeout > wpa_s->max_remain_on_chan)
   5530 		timeout = wpa_s->max_remain_on_chan;
   5531 
   5532 	return p2p_listen(wpa_s->global->p2p, timeout);
   5533 }
   5534 
   5535 
   5536 /**
   5537  * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
   5538  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
   5539  * @freq: Frequency of the channel in MHz
   5540  *
   5541  * This callback is called when the driver indicates that a remain-on-channel
   5542  * operation has been completed, i.e., the duration on the requested channel
   5543  * has timed out.
   5544  */
   5545 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
   5546 					  unsigned int freq)
   5547 {
   5548 	wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
   5549 		   "(p2p_long_listen=%d ms pending_action_tx=%p)",
   5550 		   wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
   5551 	wpas_p2p_listen_work_done(wpa_s);
   5552 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
   5553 		return;
   5554 	if (wpa_s->p2p_long_listen > 0)
   5555 		wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
   5556 	if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
   5557 		return; /* P2P module started a new operation */
   5558 	if (offchannel_pending_action_tx(wpa_s))
   5559 		return;
   5560 	if (wpa_s->p2p_long_listen > 0) {
   5561 		wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
   5562 		wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
   5563 	} else {
   5564 		/*
   5565 		 * When listen duration is over, stop listen & update p2p_state
   5566 		 * to IDLE.
   5567 		 */
   5568 		p2p_stop_listen(wpa_s->global->p2p);
   5569 	}
   5570 }
   5571 
   5572 
   5573 /**
   5574  * wpas_p2p_group_remove - Remove a P2P group
   5575  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
   5576  * @ifname: Network interface name of the group interface or "*" to remove all
   5577  *	groups
   5578  * Returns: 0 on success, -1 on failure
   5579  *
   5580  * This function is used to remove a P2P group. This can be used to disconnect
   5581  * from a group in which the local end is a P2P Client or to end a P2P Group in
   5582  * case the local end is the Group Owner. If a virtual network interface was
   5583  * created for this group, that interface will be removed. Otherwise, only the
   5584  * configured P2P group network will be removed from the interface.
   5585  */
   5586 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
   5587 {
   5588 	struct wpa_global *global = wpa_s->global;
   5589 	struct wpa_supplicant *calling_wpa_s = wpa_s;
   5590 
   5591 	if (os_strcmp(ifname, "*") == 0) {
   5592 		struct wpa_supplicant *prev;
   5593 		wpa_s = global->ifaces;
   5594 		while (wpa_s) {
   5595 			prev = wpa_s;
   5596 			wpa_s = wpa_s->next;
   5597 			if (prev->p2p_group_interface !=
   5598 			    NOT_P2P_GROUP_INTERFACE ||
   5599 			    (prev->current_ssid &&
   5600 			     prev->current_ssid->p2p_group))
   5601 				wpas_p2p_disconnect_safely(prev, calling_wpa_s);
   5602 		}
   5603 		return 0;
   5604 	}
   5605 
   5606 	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
   5607 		if (os_strcmp(wpa_s->ifname, ifname) == 0)
   5608 			break;
   5609 	}
   5610 
   5611 	return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
   5612 }
   5613 
   5614 
   5615 static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
   5616 {
   5617 	unsigned int r;
   5618 
   5619 	if (!wpa_s->conf->num_p2p_pref_chan && !freq) {
   5620 		unsigned int i, size = P2P_MAX_PREF_CHANNELS;
   5621 		unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS];
   5622 		int res;
   5623 
   5624 		res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO,
   5625 						 &size, pref_freq_list);
   5626 		if (!res && size > 0) {
   5627 			i = 0;
   5628 			while (i < size &&
   5629 			       wpas_p2p_disallowed_freq(wpa_s->global,
   5630 							pref_freq_list[i])) {
   5631 				wpa_printf(MSG_DEBUG,
   5632 					   "P2P: preferred_freq_list[%d]=%d is disallowed",
   5633 					   i, pref_freq_list[i]);
   5634 				i++;
   5635 			}
   5636 			if (i != size) {
   5637 				freq = pref_freq_list[i];
   5638 				wpa_printf(MSG_DEBUG,
   5639 					   "P2P: Using preferred_freq_list[%d]=%d",
   5640 					   i, freq);
   5641 			} else {
   5642 				wpa_printf(MSG_DEBUG,
   5643 					   "P2P: All driver preferred frequencies are disallowed for P2P use");
   5644 			}
   5645 		} else {
   5646 			wpa_printf(MSG_DEBUG,
   5647 				   "P2P: No preferred frequency list available");
   5648 		}
   5649 	}
   5650 
   5651 	if (freq == 2) {
   5652 		wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
   5653 			   "band");
   5654 		if (wpa_s->best_24_freq > 0 &&
   5655 		    p2p_supported_freq_go(wpa_s->global->p2p,
   5656 					  wpa_s->best_24_freq)) {
   5657 			freq = wpa_s->best_24_freq;
   5658 			wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
   5659 				   "channel: %d MHz", freq);
   5660 		} else {
   5661 			if (os_get_random((u8 *) &r, sizeof(r)) < 0)
   5662 				return -1;
   5663 			freq = 2412 + (r % 3) * 25;
   5664 			wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
   5665 				   "channel: %d MHz", freq);
   5666 		}
   5667 	}
   5668 
   5669 	if (freq == 5) {
   5670 		wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
   5671 			   "band");
   5672 		if (wpa_s->best_5_freq > 0 &&
   5673 		    p2p_supported_freq_go(wpa_s->global->p2p,
   5674 				       wpa_s->best_5_freq)) {
   5675 			freq = wpa_s->best_5_freq;
   5676 			wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
   5677 				   "channel: %d MHz", freq);
   5678 		} else {
   5679 			if (os_get_random((u8 *) &r, sizeof(r)) < 0)
   5680 				return -1;
   5681 			freq = 5180 + (r % 4) * 20;
   5682 			if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
   5683 				wpa_printf(MSG_DEBUG, "P2P: Could not select "
   5684 					   "5 GHz channel for P2P group");
   5685 				return -1;
   5686 			}
   5687 			wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
   5688 				   "channel: %d MHz", freq);
   5689 		}
   5690 	}
   5691 
   5692 	if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
   5693 		if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
   5694 		    ieee80211_is_dfs(freq)) {
   5695 			/*
   5696 			 * If freq is a DFS channel and DFS is offloaded to the
   5697 			 * driver, allow P2P GO to use it.
   5698 			 */
   5699 			wpa_printf(MSG_DEBUG, "P2P: "
   5700 				   "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
   5701 				   __func__, freq);
   5702 			return freq;
   5703 		}
   5704 		wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
   5705 			   "(%u MHz) is not supported for P2P uses",
   5706 			   freq);
   5707 		return -1;
   5708 	}
   5709 
   5710 	return freq;
   5711 }
   5712 
   5713 
   5714 static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s,
   5715 				      const struct p2p_channels *channels,
   5716 				      int freq)
   5717 {
   5718 	if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) &&
   5719 	    p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
   5720 	    freq_included(wpa_s, channels, freq))
   5721 		return 1;
   5722 	return 0;
   5723 }
   5724 
   5725 
   5726 static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s,
   5727 					    struct p2p_go_neg_results *params,
   5728 					    const struct p2p_channels *channels)
   5729 {
   5730 	unsigned int i, r;
   5731 
   5732 	/* first try some random selection of the social channels */
   5733 	if (os_get_random((u8 *) &r, sizeof(r)) < 0)
   5734 		return;
   5735 
   5736 	for (i = 0; i < 3; i++) {
   5737 		params->freq = 2412 + ((r + i) % 3) * 25;
   5738 		if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
   5739 			goto out;
   5740 	}
   5741 
   5742 	/* try all other channels in operating class 81 */
   5743 	for (i = 0; i < 11; i++) {
   5744 		params->freq = 2412 + i * 5;
   5745 
   5746 		/* skip social channels; covered in the previous loop */
   5747 		if (params->freq == 2412 ||
   5748 		    params->freq == 2437 ||
   5749 		    params->freq == 2462)
   5750 			continue;
   5751 
   5752 		if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
   5753 			goto out;
   5754 	}
   5755 
   5756 	/* try all channels in operating class 115 */
   5757 	for (i = 0; i < 4; i++) {
   5758 		params->freq = 5180 + i * 20;
   5759 		if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
   5760 		    freq_included(wpa_s, channels, params->freq) &&
   5761 		    p2p_supported_freq(wpa_s->global->p2p, params->freq))
   5762 			goto out;
   5763 	}
   5764 
   5765 	/* try all channels in operating class 124 */
   5766 	for (i = 0; i < 4; i++) {
   5767 		params->freq = 5745 + i * 20;
   5768 		if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
   5769 		    freq_included(wpa_s, channels, params->freq) &&
   5770 		    p2p_supported_freq(wpa_s->global->p2p, params->freq))
   5771 			goto out;
   5772 	}
   5773 
   5774 	/* try social channel class 180 channel 2 */
   5775 	params->freq = 58320 + 1 * 2160;
   5776 	if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
   5777 	    freq_included(wpa_s, channels, params->freq) &&
   5778 	    p2p_supported_freq(wpa_s->global->p2p, params->freq))
   5779 		goto out;
   5780 
   5781 	/* try all channels in reg. class 180 */
   5782 	for (i = 0; i < 4; i++) {
   5783 		params->freq = 58320 + i * 2160;
   5784 		if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
   5785 		    freq_included(wpa_s, channels, params->freq) &&
   5786 		    p2p_supported_freq(wpa_s->global->p2p, params->freq))
   5787 			goto out;
   5788 	}
   5789 
   5790 	params->freq = 0;
   5791 	wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
   5792 	return;
   5793 out:
   5794 	wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
   5795 		   params->freq);
   5796 }
   5797 
   5798 
   5799 static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
   5800 				   struct p2p_go_neg_results *params,
   5801 				   int freq, int vht_center_freq2, int ht40,
   5802 				   int vht, int max_oper_chwidth,
   5803 				   const struct p2p_channels *channels)
   5804 {
   5805 	struct wpa_used_freq_data *freqs;
   5806 	unsigned int cand;
   5807 	unsigned int num, i;
   5808 	int ignore_no_freqs = 0;
   5809 	int unused_channels = wpas_p2p_num_unused_channels(wpa_s) > 0;
   5810 
   5811 	os_memset(params, 0, sizeof(*params));
   5812 	params->role_go = 1;
   5813 	params->ht40 = ht40;
   5814 	params->vht = vht;
   5815 	params->max_oper_chwidth = max_oper_chwidth;
   5816 	params->vht_center_freq2 = vht_center_freq2;
   5817 
   5818 	freqs = os_calloc(wpa_s->num_multichan_concurrent,
   5819 			  sizeof(struct wpa_used_freq_data));
   5820 	if (!freqs)
   5821 		return -1;
   5822 
   5823 	num = get_shared_radio_freqs_data(wpa_s, freqs,
   5824 					  wpa_s->num_multichan_concurrent);
   5825 
   5826 	if (wpa_s->current_ssid &&
   5827 	    wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO &&
   5828 	    wpa_s->wpa_state == WPA_COMPLETED) {
   5829 		wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO",
   5830 			   __func__);
   5831 
   5832 		/*
   5833 		 * If the frequency selection is done for an active P2P GO that
   5834 		 * is not sharing a frequency, allow to select a new frequency
   5835 		 * even if there are no unused frequencies as we are about to
   5836 		 * move the P2P GO so its frequency can be re-used.
   5837 		 */
   5838 		for (i = 0; i < num; i++) {
   5839 			if (freqs[i].freq == wpa_s->current_ssid->frequency &&
   5840 			    freqs[i].flags == 0) {
   5841 				ignore_no_freqs = 1;
   5842 				break;
   5843 			}
   5844 		}
   5845 	}
   5846 
   5847 	/* try using the forced freq */
   5848 	if (freq) {
   5849 		if (!wpas_p2p_supported_freq_go(wpa_s, channels, freq)) {
   5850 			wpa_printf(MSG_DEBUG,
   5851 				   "P2P: Forced GO freq %d MHz not accepted",
   5852 				   freq);
   5853 			goto fail;
   5854 		}
   5855 
   5856 		for (i = 0; i < num; i++) {
   5857 			if (freqs[i].freq == freq) {
   5858 				wpa_printf(MSG_DEBUG,
   5859 					   "P2P: forced freq (%d MHz) is also shared",
   5860 					   freq);
   5861 				params->freq = freq;
   5862 				goto success;
   5863 			}
   5864 		}
   5865 
   5866 		if (!ignore_no_freqs && !unused_channels) {
   5867 			wpa_printf(MSG_DEBUG,
   5868 				   "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use",
   5869 				   freq);
   5870 			goto fail;
   5871 		}
   5872 
   5873 		wpa_printf(MSG_DEBUG,
   5874 			   "P2P: force GO freq (%d MHz) on a free channel",
   5875 			   freq);
   5876 		params->freq = freq;
   5877 		goto success;
   5878 	}
   5879 
   5880 	/* consider using one of the shared frequencies */
   5881 	if (num &&
   5882 	    (!wpa_s->conf->p2p_ignore_shared_freq || !unused_channels)) {
   5883 		cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
   5884 		if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
   5885 			wpa_printf(MSG_DEBUG,
   5886 				   "P2P: Use shared freq (%d MHz) for GO",
   5887 				   cand);
   5888 			params->freq = cand;
   5889 			goto success;
   5890 		}
   5891 
   5892 		/* try using one of the shared freqs */
   5893 		for (i = 0; i < num; i++) {
   5894 			if (wpas_p2p_supported_freq_go(wpa_s, channels,
   5895 						       freqs[i].freq)) {
   5896 				wpa_printf(MSG_DEBUG,
   5897 					   "P2P: Use shared freq (%d MHz) for GO",
   5898 					   freqs[i].freq);
   5899 				params->freq = freqs[i].freq;
   5900 				goto success;
   5901 			}
   5902 		}
   5903 	}
   5904 
   5905 	if (!ignore_no_freqs && !unused_channels) {
   5906 		wpa_printf(MSG_DEBUG,
   5907 			   "P2P: Cannot force GO on any of the channels we are already using");
   5908 		goto fail;
   5909 	}
   5910 
   5911 	/* try using the setting from the configuration file */
   5912 	if (wpa_s->conf->p2p_oper_reg_class == 81 &&
   5913 	    wpa_s->conf->p2p_oper_channel >= 1 &&
   5914 	    wpa_s->conf->p2p_oper_channel <= 11 &&
   5915 	    wpas_p2p_supported_freq_go(
   5916 		    wpa_s, channels,
   5917 		    2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
   5918 		params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
   5919 		wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
   5920 			   "frequency %d MHz", params->freq);
   5921 		goto success;
   5922 	}
   5923 
   5924 	if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
   5925 	     wpa_s->conf->p2p_oper_reg_class == 116 ||
   5926 	     wpa_s->conf->p2p_oper_reg_class == 117 ||
   5927 	     wpa_s->conf->p2p_oper_reg_class == 124 ||
   5928 	     wpa_s->conf->p2p_oper_reg_class == 125 ||
   5929 	     wpa_s->conf->p2p_oper_reg_class == 126 ||
   5930 	     wpa_s->conf->p2p_oper_reg_class == 127) &&
   5931 	    wpas_p2p_supported_freq_go(wpa_s, channels,
   5932 				       5000 +
   5933 				       5 * wpa_s->conf->p2p_oper_channel)) {
   5934 		params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
   5935 		wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
   5936 			   "frequency %d MHz", params->freq);
   5937 		goto success;
   5938 	}
   5939 
   5940 	/* Try using best channels */
   5941 	if (wpa_s->conf->p2p_oper_channel == 0 &&
   5942 	    wpa_s->best_overall_freq > 0 &&
   5943 	    wpas_p2p_supported_freq_go(wpa_s, channels,
   5944 				       wpa_s->best_overall_freq)) {
   5945 		params->freq = wpa_s->best_overall_freq;
   5946 		wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
   5947 			   "channel %d MHz", params->freq);
   5948 		goto success;
   5949 	}
   5950 
   5951 	if (wpa_s->conf->p2p_oper_channel == 0 &&
   5952 	    wpa_s->best_24_freq > 0 &&
   5953 	    wpas_p2p_supported_freq_go(wpa_s, channels,
   5954 				       wpa_s->best_24_freq)) {
   5955 		params->freq = wpa_s->best_24_freq;
   5956 		wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
   5957 			   "channel %d MHz", params->freq);
   5958 		goto success;
   5959 	}
   5960 
   5961 	if (wpa_s->conf->p2p_oper_channel == 0 &&
   5962 	    wpa_s->best_5_freq > 0 &&
   5963 	    wpas_p2p_supported_freq_go(wpa_s, channels,
   5964 				       wpa_s->best_5_freq)) {
   5965 		params->freq = wpa_s->best_5_freq;
   5966 		wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
   5967 			   "channel %d MHz", params->freq);
   5968 		goto success;
   5969 	}
   5970 
   5971 	/* try using preferred channels */
   5972 	cand = p2p_get_pref_freq(wpa_s->global->p2p, channels);
   5973 	if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
   5974 		params->freq = cand;
   5975 		wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
   5976 			   "channels", params->freq);
   5977 		goto success;
   5978 	}
   5979 
   5980 	/* Try using one of the group common freqs */
   5981 	if (wpa_s->p2p_group_common_freqs) {
   5982 		for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
   5983 			cand = wpa_s->p2p_group_common_freqs[i];
   5984 			if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
   5985 				params->freq = cand;
   5986 				wpa_printf(MSG_DEBUG,
   5987 					   "P2P: Use freq %d MHz common with the peer",
   5988 					   params->freq);
   5989 				goto success;
   5990 			}
   5991 		}
   5992 	}
   5993 
   5994 	/* no preference, select some channel */
   5995 	wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels);
   5996 
   5997 	if (params->freq == 0) {
   5998 		wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use");
   5999 		goto fail;
   6000 	}
   6001 
   6002 success:
   6003 	os_free(freqs);
   6004 	return 0;
   6005 fail:
   6006 	os_free(freqs);
   6007 	return -1;
   6008 }
   6009 
   6010 
   6011 static struct wpa_supplicant *
   6012 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
   6013 			 int go)
   6014 {
   6015 	struct wpa_supplicant *group_wpa_s;
   6016 
   6017 	if (!wpas_p2p_create_iface(wpa_s)) {
   6018 		if (wpa_s->p2p_mgmt) {
   6019 			/*
   6020 			 * We may be called on the p2p_dev interface which
   6021 			 * cannot be used for group operations, so always use
   6022 			 * the primary interface.
   6023 			 */
   6024 			wpa_s->parent->p2pdev = wpa_s;
   6025 			wpa_s = wpa_s->parent;
   6026 		}
   6027 		wpa_dbg(wpa_s, MSG_DEBUG,
   6028 			"P2P: Use primary interface for group operations");
   6029 		wpa_s->p2p_first_connection_timeout = 0;
   6030 		if (wpa_s != wpa_s->p2pdev)
   6031 			wpas_p2p_clone_config(wpa_s, wpa_s->p2pdev);
   6032 		return wpa_s;
   6033 	}
   6034 
   6035 	if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
   6036 					 WPA_IF_P2P_CLIENT) < 0) {
   6037 		wpa_msg_global(wpa_s, MSG_ERROR,
   6038 			       "P2P: Failed to add group interface");
   6039 		return NULL;
   6040 	}
   6041 	group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
   6042 	if (group_wpa_s == NULL) {
   6043 		wpa_msg_global(wpa_s, MSG_ERROR,
   6044 			       "P2P: Failed to initialize group interface");
   6045 		wpas_p2p_remove_pending_group_interface(wpa_s);
   6046 		return NULL;
   6047 	}
   6048 
   6049 	wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
   6050 		group_wpa_s->ifname);
   6051 	group_wpa_s->p2p_first_connection_timeout = 0;
   6052 	return group_wpa_s;
   6053 }
   6054 
   6055 
   6056 /**
   6057  * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
   6058  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
   6059  * @persistent_group: Whether to create a persistent group
   6060  * @freq: Frequency for the group or 0 to indicate no hardcoding
   6061  * @vht_center_freq2: segment_1 center frequency for GO operating in VHT 80P80
   6062  * @ht40: Start GO with 40 MHz channel width
   6063  * @vht:  Start GO with VHT support
   6064  * @vht_chwidth: channel bandwidth for GO operating with VHT support
   6065  * Returns: 0 on success, -1 on failure
   6066  *
   6067  * This function creates a new P2P group with the local end as the Group Owner,
   6068  * i.e., without using Group Owner Negotiation.
   6069  */
   6070 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
   6071 		       int freq, int vht_center_freq2, int ht40, int vht,
   6072 		       int max_oper_chwidth)
   6073 {
   6074 	struct p2p_go_neg_results params;
   6075 
   6076 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
   6077 		return -1;
   6078 
   6079 	os_free(wpa_s->global->add_psk);
   6080 	wpa_s->global->add_psk = NULL;
   6081 
   6082 	/* Make sure we are not running find during connection establishment */
   6083 	wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
   6084 	wpas_p2p_stop_find_oper(wpa_s);
   6085 
   6086 	freq = wpas_p2p_select_go_freq(wpa_s, freq);
   6087 	if (freq < 0)
   6088 		return -1;
   6089 
   6090 	if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
   6091 				    ht40, vht, max_oper_chwidth, NULL))
   6092 		return -1;
   6093 	if (params.freq &&
   6094 	    !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
   6095 		if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
   6096 		    ieee80211_is_dfs(params.freq)) {
   6097 			/*
   6098 			 * If freq is a DFS channel and DFS is offloaded to the
   6099 			 * driver, allow P2P GO to use it.
   6100 			 */
   6101 			wpa_printf(MSG_DEBUG,
   6102 				   "P2P: %s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to driver",
   6103 				__func__, params.freq);
   6104 		} else {
   6105 			wpa_printf(MSG_DEBUG,
   6106 				   "P2P: The selected channel for GO (%u MHz) is not supported for P2P uses",
   6107 				   params.freq);
   6108 			return -1;
   6109 		}
   6110 	}
   6111 	p2p_go_params(wpa_s->global->p2p, &params);
   6112 	params.persistent_group = persistent_group;
   6113 
   6114 	wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
   6115 	if (wpa_s == NULL)
   6116 		return -1;
   6117 	wpas_start_wps_go(wpa_s, &params, 0);
   6118 
   6119 	return 0;
   6120 }
   6121 
   6122 
   6123 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
   6124 				 struct wpa_ssid *params, int addr_allocated,
   6125 				 int freq, int force_scan)
   6126 {
   6127 	struct wpa_ssid *ssid;
   6128 
   6129 	wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
   6130 	if (wpa_s == NULL)
   6131 		return -1;
   6132 	if (force_scan)
   6133 		os_get_reltime(&wpa_s->scan_min_time);
   6134 	wpa_s->p2p_last_4way_hs_fail = NULL;
   6135 
   6136 	wpa_supplicant_ap_deinit(wpa_s);
   6137 
   6138 	ssid = wpa_config_add_network(wpa_s->conf);
   6139 	if (ssid == NULL)
   6140 		return -1;
   6141 	os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
   6142 	wpa_config_set_network_defaults(ssid);
   6143 	ssid->temporary = 1;
   6144 	ssid->proto = WPA_PROTO_RSN;
   6145 	ssid->pbss = params->pbss;
   6146 	ssid->pairwise_cipher = params->pbss ? WPA_CIPHER_GCMP :
   6147 		WPA_CIPHER_CCMP;
   6148 	ssid->group_cipher = params->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
   6149 	ssid->key_mgmt = WPA_KEY_MGMT_PSK;
   6150 	ssid->ssid = os_malloc(params->ssid_len);
   6151 	if (ssid->ssid == NULL) {
   6152 		wpa_config_remove_network(wpa_s->conf, ssid->id);
   6153 		return -1;
   6154 	}
   6155 	os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
   6156 	ssid->ssid_len = params->ssid_len;
   6157 	ssid->p2p_group = 1;
   6158 	ssid->export_keys = 1;
   6159 	if (params->psk_set) {
   6160 		os_memcpy(ssid->psk, params->psk, 32);
   6161 		ssid->psk_set = 1;
   6162 	}
   6163 	if (params->passphrase)
   6164 		ssid->passphrase = os_strdup(params->passphrase);
   6165 
   6166 	wpa_s->show_group_started = 1;
   6167 	wpa_s->p2p_in_invitation = 1;
   6168 	wpa_s->p2p_invite_go_freq = freq;
   6169 	wpa_s->p2p_go_group_formation_completed = 0;
   6170 	wpa_s->global->p2p_group_formation = wpa_s;
   6171 
   6172 	eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
   6173 			     NULL);
   6174 	eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
   6175 			       wpas_p2p_group_formation_timeout,
   6176 			       wpa_s->p2pdev, NULL);
   6177 	wpa_supplicant_select_network(wpa_s, ssid);
   6178 
   6179 	return 0;
   6180 }
   6181 
   6182 
   6183 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
   6184 				  struct wpa_ssid *ssid, int addr_allocated,
   6185 				  int force_freq, int neg_freq,
   6186 				  int vht_center_freq2, int ht40,
   6187 				  int vht, int max_oper_chwidth,
   6188 				  const struct p2p_channels *channels,
   6189 				  int connection_timeout, int force_scan)
   6190 {
   6191 	struct p2p_go_neg_results params;
   6192 	int go = 0, freq;
   6193 
   6194 	if (ssid->disabled != 2 || ssid->ssid == NULL)
   6195 		return -1;
   6196 
   6197 	if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
   6198 	    go == (ssid->mode == WPAS_MODE_P2P_GO)) {
   6199 		wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
   6200 			   "already running");
   6201 		if (go == 0 &&
   6202 		    eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
   6203 					 wpa_s->p2pdev, NULL)) {
   6204 			/*
   6205 			 * This can happen if Invitation Response frame was lost
   6206 			 * and the peer (GO of a persistent group) tries to
   6207 			 * invite us again. Reschedule the timeout to avoid
   6208 			 * terminating the wait for the connection too early
   6209 			 * since we now know that the peer is still trying to
   6210 			 * invite us instead of having already started the GO.
   6211 			 */
   6212 			wpa_printf(MSG_DEBUG,
   6213 				   "P2P: Reschedule group formation timeout since peer is still trying to invite us");
   6214 			eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
   6215 					       wpas_p2p_group_formation_timeout,
   6216 					       wpa_s->p2pdev, NULL);
   6217 		}
   6218 		return 0;
   6219 	}
   6220 
   6221 	os_free(wpa_s->global->add_psk);
   6222 	wpa_s->global->add_psk = NULL;
   6223 
   6224 	/* Make sure we are not running find during connection establishment */
   6225 	wpas_p2p_stop_find_oper(wpa_s);
   6226 
   6227 	wpa_s->p2p_fallback_to_go_neg = 0;
   6228 
   6229 	if (ssid->mode == WPAS_MODE_P2P_GO) {
   6230 		if (force_freq > 0) {
   6231 			freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
   6232 			if (freq < 0)
   6233 				return -1;
   6234 		} else {
   6235 			freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
   6236 			if (freq < 0 ||
   6237 			    (freq > 0 && !freq_included(wpa_s, channels, freq)))
   6238 				freq = 0;
   6239 		}
   6240 	} else if (ssid->mode == WPAS_MODE_INFRA) {
   6241 		freq = neg_freq;
   6242 		if (freq <= 0 || !freq_included(wpa_s, channels, freq)) {
   6243 			struct os_reltime now;
   6244 			struct wpa_bss *bss =
   6245 				wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
   6246 
   6247 			os_get_reltime(&now);
   6248 			if (bss &&
   6249 			    !os_reltime_expired(&now, &bss->last_update, 5) &&
   6250 			    freq_included(wpa_s, channels, bss->freq))
   6251 				freq = bss->freq;
   6252 			else
   6253 				freq = 0;
   6254 		}
   6255 
   6256 		return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq,
   6257 					     force_scan);
   6258 	} else {
   6259 		return -1;
   6260 	}
   6261 
   6262 	if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
   6263 				    ht40, vht, max_oper_chwidth, channels))
   6264 		return -1;
   6265 
   6266 	params.role_go = 1;
   6267 	params.psk_set = ssid->psk_set;
   6268 	if (params.psk_set)
   6269 		os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
   6270 	if (ssid->passphrase) {
   6271 		if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
   6272 			wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
   6273 				   "persistent group");
   6274 			return -1;
   6275 		}
   6276 		os_strlcpy(params.passphrase, ssid->passphrase,
   6277 			   sizeof(params.passphrase));
   6278 	}
   6279 	os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
   6280 	params.ssid_len = ssid->ssid_len;
   6281 	params.persistent_group = 1;
   6282 
   6283 	wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
   6284 	if (wpa_s == NULL)
   6285 		return -1;
   6286 
   6287 	p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
   6288 
   6289 	wpa_s->p2p_first_connection_timeout = connection_timeout;
   6290 	wpas_start_wps_go(wpa_s, &params, 0);
   6291 
   6292 	return 0;
   6293 }
   6294 
   6295 
   6296 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
   6297 			       struct wpabuf *proberesp_ies)
   6298 {
   6299 	struct wpa_supplicant *wpa_s = ctx;
   6300 	if (wpa_s->ap_iface) {
   6301 		struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
   6302 		if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
   6303 			wpabuf_free(beacon_ies);
   6304 			wpabuf_free(proberesp_ies);
   6305 			return;
   6306 		}
   6307 		if (beacon_ies) {
   6308 			wpabuf_free(hapd->p2p_beacon_ie);
   6309 			hapd->p2p_beacon_ie = beacon_ies;
   6310 		}
   6311 		wpabuf_free(hapd->p2p_probe_resp_ie);
   6312 		hapd->p2p_probe_resp_ie = proberesp_ies;
   6313 	} else {
   6314 		wpabuf_free(beacon_ies);
   6315 		wpabuf_free(proberesp_ies);
   6316 	}
   6317 	wpa_supplicant_ap_update_beacon(wpa_s);
   6318 }
   6319 
   6320 
   6321 static void wpas_p2p_idle_update(void *ctx, int idle)
   6322 {
   6323 	struct wpa_supplicant *wpa_s = ctx;
   6324 	if (!wpa_s->ap_iface)
   6325 		return;
   6326 	wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
   6327 	if (idle) {
   6328 		if (wpa_s->global->p2p_fail_on_wps_complete &&
   6329 		    wpa_s->p2p_in_provisioning) {
   6330 			wpas_p2p_grpform_fail_after_wps(wpa_s);
   6331 			return;
   6332 		}
   6333 		wpas_p2p_set_group_idle_timeout(wpa_s);
   6334 	} else
   6335 		eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
   6336 }
   6337 
   6338 
   6339 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
   6340 				       struct wpa_ssid *ssid)
   6341 {
   6342 	struct p2p_group *group;
   6343 	struct p2p_group_config *cfg;
   6344 
   6345 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
   6346 	    !ssid->p2p_group)
   6347 		return NULL;
   6348 
   6349 	cfg = os_zalloc(sizeof(*cfg));
   6350 	if (cfg == NULL)
   6351 		return NULL;
   6352 
   6353 	if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
   6354 		cfg->persistent_group = 2;
   6355 	else if (ssid->p2p_persistent_group)
   6356 		cfg->persistent_group = 1;
   6357 	os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
   6358 	if (wpa_s->max_stations &&
   6359 	    wpa_s->max_stations < wpa_s->conf->max_num_sta)
   6360 		cfg->max_clients = wpa_s->max_stations;
   6361 	else
   6362 		cfg->max_clients = wpa_s->conf->max_num_sta;
   6363 	os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
   6364 	cfg->ssid_len = ssid->ssid_len;
   6365 	cfg->freq = ssid->frequency;
   6366 	cfg->cb_ctx = wpa_s;
   6367 	cfg->ie_update = wpas_p2p_ie_update;
   6368 	cfg->idle_update = wpas_p2p_idle_update;
   6369 	cfg->ip_addr_alloc = WPA_GET_BE32(wpa_s->p2pdev->conf->ip_addr_start)
   6370 		!= 0;
   6371 
   6372 	group = p2p_group_init(wpa_s->global->p2p, cfg);
   6373 	if (group == NULL)
   6374 		os_free(cfg);
   6375 	if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
   6376 		p2p_group_notif_formation_done(group);
   6377 	wpa_s->p2p_group = group;
   6378 	return group;
   6379 }
   6380 
   6381 
   6382 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
   6383 			  int registrar)
   6384 {
   6385 	struct wpa_ssid *ssid = wpa_s->current_ssid;
   6386 
   6387 	if (!wpa_s->p2p_in_provisioning) {
   6388 		wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
   6389 			   "provisioning not in progress");
   6390 		return;
   6391 	}
   6392 
   6393 	if (ssid && ssid->mode == WPAS_MODE_INFRA) {
   6394 		u8 go_dev_addr[ETH_ALEN];
   6395 		os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
   6396 		wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
   6397 					  ssid->ssid_len);
   6398 		/* Clear any stored provisioning info */
   6399 		p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
   6400 	}
   6401 
   6402 	eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
   6403 			     NULL);
   6404 	wpa_s->p2p_go_group_formation_completed = 1;
   6405 	if (ssid && ssid->mode == WPAS_MODE_INFRA) {
   6406 		/*
   6407 		 * Use a separate timeout for initial data connection to
   6408 		 * complete to allow the group to be removed automatically if
   6409 		 * something goes wrong in this step before the P2P group idle
   6410 		 * timeout mechanism is taken into use.
   6411 		 */
   6412 		wpa_dbg(wpa_s, MSG_DEBUG,
   6413 			"P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
   6414 			P2P_MAX_INITIAL_CONN_WAIT);
   6415 		eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
   6416 				       wpas_p2p_group_formation_timeout,
   6417 				       wpa_s->p2pdev, NULL);
   6418 		/* Complete group formation on successful data connection. */
   6419 		wpa_s->p2p_go_group_formation_completed = 0;
   6420 	} else if (ssid) {
   6421 		/*
   6422 		 * Use a separate timeout for initial data connection to
   6423 		 * complete to allow the group to be removed automatically if
   6424 		 * the client does not complete data connection successfully.
   6425 		 */
   6426 		wpa_dbg(wpa_s, MSG_DEBUG,
   6427 			"P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
   6428 			P2P_MAX_INITIAL_CONN_WAIT_GO);
   6429 		eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
   6430 				       wpas_p2p_group_formation_timeout,
   6431 				       wpa_s->p2pdev, NULL);
   6432 		/*
   6433 		 * Complete group formation on first successful data connection
   6434 		 */
   6435 		wpa_s->p2p_go_group_formation_completed = 0;
   6436 	}
   6437 	if (wpa_s->global->p2p)
   6438 		p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
   6439 	wpas_group_formation_completed(wpa_s, 1, 0);
   6440 }
   6441 
   6442 
   6443 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
   6444 			 struct wps_event_fail *fail)
   6445 {
   6446 	if (!wpa_s->p2p_in_provisioning) {
   6447 		wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
   6448 			   "provisioning not in progress");
   6449 		return;
   6450 	}
   6451 
   6452 	if (wpa_s->go_params) {
   6453 		p2p_clear_provisioning_info(
   6454 			wpa_s->global->p2p,
   6455 			wpa_s->go_params->peer_device_addr);
   6456 	}
   6457 
   6458 	wpas_notify_p2p_wps_failed(wpa_s, fail);
   6459 
   6460 	if (wpa_s == wpa_s->global->p2p_group_formation) {
   6461 		/*
   6462 		 * Allow some time for the failed WPS negotiation exchange to
   6463 		 * complete, but remove the group since group formation cannot
   6464 		 * succeed after provisioning failure.
   6465 		 */
   6466 		wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
   6467 		wpa_s->global->p2p_fail_on_wps_complete = 1;
   6468 		eloop_deplete_timeout(0, 50000,
   6469 				      wpas_p2p_group_formation_timeout,
   6470 				      wpa_s->p2pdev, NULL);
   6471 	}
   6472 }
   6473 
   6474 
   6475 int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
   6476 {
   6477 	if (!wpa_s->global->p2p_fail_on_wps_complete ||
   6478 	    !wpa_s->p2p_in_provisioning)
   6479 		return 0;
   6480 
   6481 	wpas_p2p_grpform_fail_after_wps(wpa_s);
   6482 
   6483 	return 1;
   6484 }
   6485 
   6486 
   6487 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
   6488 		       const char *config_method,
   6489 		       enum wpas_p2p_prov_disc_use use,
   6490 		       struct p2ps_provision *p2ps_prov)
   6491 {
   6492 	u16 config_methods;
   6493 
   6494 	wpa_s->global->pending_p2ps_group = 0;
   6495 	wpa_s->global->pending_p2ps_group_freq = 0;
   6496 	wpa_s->p2p_fallback_to_go_neg = 0;
   6497 	wpa_s->pending_pd_use = NORMAL_PD;
   6498 	if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
   6499 		p2ps_prov->conncap = p2ps_group_capability(
   6500 			wpa_s, P2PS_SETUP_NONE, p2ps_prov->role,
   6501 			&p2ps_prov->force_freq, &p2ps_prov->pref_freq);
   6502 
   6503 		wpa_printf(MSG_DEBUG,
   6504 			   "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
   6505 			   __func__, p2ps_prov->conncap,
   6506 			   p2ps_prov->adv_id, p2ps_prov->conncap,
   6507 			   p2ps_prov->status, p2ps_prov->info);
   6508 
   6509 		config_methods = 0;
   6510 	} else if (os_strncmp(config_method, "display", 7) == 0)
   6511 		config_methods = WPS_CONFIG_DISPLAY;
   6512 	else if (os_strncmp(config_method, "keypad", 6) == 0)
   6513 		config_methods = WPS_CONFIG_KEYPAD;
   6514 	else if (os_strncmp(config_method, "pbc", 3) == 0 ||
   6515 		 os_strncmp(config_method, "pushbutton", 10) == 0)
   6516 		config_methods = WPS_CONFIG_PUSHBUTTON;
   6517 	else {
   6518 		wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
   6519 		os_free(p2ps_prov);
   6520 		return -1;
   6521 	}
   6522 
   6523 	if (use == WPAS_P2P_PD_AUTO) {
   6524 		os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
   6525 		wpa_s->pending_pd_config_methods = config_methods;
   6526 		wpa_s->p2p_auto_pd = 1;
   6527 		wpa_s->p2p_auto_join = 0;
   6528 		wpa_s->pending_pd_before_join = 0;
   6529 		wpa_s->auto_pd_scan_retry = 0;
   6530 		wpas_p2p_stop_find(wpa_s);
   6531 		wpa_s->p2p_join_scan_count = 0;
   6532 		os_get_reltime(&wpa_s->p2p_auto_started);
   6533 		wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
   6534 			   wpa_s->p2p_auto_started.sec,
   6535 			   wpa_s->p2p_auto_started.usec);
   6536 		wpas_p2p_join_scan(wpa_s, NULL);
   6537 		return 0;
   6538 	}
   6539 
   6540 	if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
   6541 		os_free(p2ps_prov);
   6542 		return -1;
   6543 	}
   6544 
   6545 	return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
   6546 				 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
   6547 				 0, 1);
   6548 }
   6549 
   6550 
   6551 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
   6552 			      char *end)
   6553 {
   6554 	return p2p_scan_result_text(ies, ies_len, buf, end);
   6555 }
   6556 
   6557 
   6558 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
   6559 {
   6560 	if (!offchannel_pending_action_tx(wpa_s))
   6561 		return;
   6562 
   6563 	if (wpa_s->p2p_send_action_work) {
   6564 		wpas_p2p_free_send_action_work(wpa_s);
   6565 		eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
   6566 				     wpa_s, NULL);
   6567 		offchannel_send_action_done(wpa_s);
   6568 	}
   6569 
   6570 	wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
   6571 		   "operation request");
   6572 	offchannel_clear_pending_action_tx(wpa_s);
   6573 }
   6574 
   6575 
   6576 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
   6577 		  enum p2p_discovery_type type,
   6578 		  unsigned int num_req_dev_types, const u8 *req_dev_types,
   6579 		  const u8 *dev_id, unsigned int search_delay,
   6580 		  u8 seek_cnt, const char **seek_string, int freq)
   6581 {
   6582 	wpas_p2p_clear_pending_action_tx(wpa_s);
   6583 	wpa_s->p2p_long_listen = 0;
   6584 
   6585 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
   6586 	    wpa_s->p2p_in_provisioning)
   6587 		return -1;
   6588 
   6589 	wpa_supplicant_cancel_sched_scan(wpa_s);
   6590 
   6591 	return p2p_find(wpa_s->global->p2p, timeout, type,
   6592 			num_req_dev_types, req_dev_types, dev_id,
   6593 			search_delay, seek_cnt, seek_string, freq);
   6594 }
   6595 
   6596 
   6597 static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
   6598 					    struct wpa_scan_results *scan_res)
   6599 {
   6600 	wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
   6601 
   6602 	if (wpa_s->p2p_scan_work) {
   6603 		struct wpa_radio_work *work = wpa_s->p2p_scan_work;
   6604 		wpa_s->p2p_scan_work = NULL;
   6605 		radio_work_done(work);
   6606 	}
   6607 
   6608 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
   6609 		return;
   6610 
   6611 	/*
   6612 	 * Indicate that results have been processed so that the P2P module can
   6613 	 * continue pending tasks.
   6614 	 */
   6615 	p2p_scan_res_handled(wpa_s->global->p2p);
   6616 }
   6617 
   6618 
   6619 static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
   6620 {
   6621 	wpas_p2p_clear_pending_action_tx(wpa_s);
   6622 	wpa_s->p2p_long_listen = 0;
   6623 	eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
   6624 	eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
   6625 
   6626 	if (wpa_s->global->p2p)
   6627 		p2p_stop_find(wpa_s->global->p2p);
   6628 
   6629 	if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
   6630 		wpa_printf(MSG_DEBUG,
   6631 			   "P2P: Do not consider the scan results after stop_find");
   6632 		wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
   6633 	}
   6634 }
   6635 
   6636 
   6637 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
   6638 {
   6639 	wpas_p2p_stop_find_oper(wpa_s);
   6640 	if (!wpa_s->global->pending_group_iface_for_p2ps)
   6641 		wpas_p2p_remove_pending_group_interface(wpa_s);
   6642 }
   6643 
   6644 
   6645 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
   6646 {
   6647 	struct wpa_supplicant *wpa_s = eloop_ctx;
   6648 	wpa_s->p2p_long_listen = 0;
   6649 }
   6650 
   6651 
   6652 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
   6653 {
   6654 	int res;
   6655 
   6656 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
   6657 		return -1;
   6658 
   6659 	if (wpa_s->p2p_lo_started) {
   6660 		wpa_printf(MSG_DEBUG,
   6661 			"P2P: Cannot start P2P listen, it is offloaded");
   6662 		return -1;
   6663 	}
   6664 
   6665 	wpa_supplicant_cancel_sched_scan(wpa_s);
   6666 	wpas_p2p_clear_pending_action_tx(wpa_s);
   6667 
   6668 	if (timeout == 0) {
   6669 		/*
   6670 		 * This is a request for unlimited Listen state. However, at
   6671 		 * least for now, this is mapped to a Listen state for one
   6672 		 * hour.
   6673 		 */
   6674 		timeout = 3600;
   6675 	}
   6676 	eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
   6677 	wpa_s->p2p_long_listen = 0;
   6678 
   6679 	/*
   6680 	 * Stop previous find/listen operation to avoid trying to request a new
   6681 	 * remain-on-channel operation while the driver is still running the
   6682 	 * previous one.
   6683 	 */
   6684 	if (wpa_s->global->p2p)
   6685 		p2p_stop_find(wpa_s->global->p2p);
   6686 
   6687 	res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
   6688 	if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
   6689 		wpa_s->p2p_long_listen = timeout * 1000;
   6690 		eloop_register_timeout(timeout, 0,
   6691 				       wpas_p2p_long_listen_timeout,
   6692 				       wpa_s, NULL);
   6693 	}
   6694 
   6695 	return res;
   6696 }
   6697 
   6698 
   6699 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
   6700 			  u8 *buf, size_t len, int p2p_group)
   6701 {
   6702 	struct wpabuf *p2p_ie;
   6703 	int ret;
   6704 
   6705 	if (wpa_s->global->p2p_disabled)
   6706 		return -1;
   6707 	/*
   6708 	 * Advertize mandatory cross connection capability even on
   6709 	 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
   6710 	 */
   6711 	if (wpa_s->conf->p2p_disabled && p2p_group)
   6712 		return -1;
   6713 	if (wpa_s->global->p2p == NULL)
   6714 		return -1;
   6715 	if (bss == NULL)
   6716 		return -1;
   6717 
   6718 	p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
   6719 	ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
   6720 			       p2p_group, p2p_ie);
   6721 	wpabuf_free(p2p_ie);
   6722 
   6723 	return ret;
   6724 }
   6725 
   6726 
   6727 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
   6728 			  const u8 *dst, const u8 *bssid,
   6729 			  const u8 *ie, size_t ie_len,
   6730 			  unsigned int rx_freq, int ssi_signal)
   6731 {
   6732 	if (wpa_s->global->p2p_disabled)
   6733 		return 0;
   6734 	if (wpa_s->global->p2p == NULL)
   6735 		return 0;
   6736 
   6737 	switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
   6738 				 ie, ie_len, rx_freq, wpa_s->p2p_lo_started)) {
   6739 	case P2P_PREQ_NOT_P2P:
   6740 		wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
   6741 				 ssi_signal);
   6742 		/* fall through */
   6743 	case P2P_PREQ_MALFORMED:
   6744 	case P2P_PREQ_NOT_LISTEN:
   6745 	case P2P_PREQ_NOT_PROCESSED:
   6746 	default: /* make gcc happy */
   6747 		return 0;
   6748 	case P2P_PREQ_PROCESSED:
   6749 		return 1;
   6750 	}
   6751 }
   6752 
   6753 
   6754 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
   6755 			const u8 *sa, const u8 *bssid,
   6756 			u8 category, const u8 *data, size_t len, int freq)
   6757 {
   6758 	if (wpa_s->global->p2p_disabled)
   6759 		return;
   6760 	if (wpa_s->global->p2p == NULL)
   6761 		return;
   6762 
   6763 	p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
   6764 		      freq);
   6765 }
   6766 
   6767 
   6768 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
   6769 {
   6770 	unsigned int bands;
   6771 
   6772 	if (wpa_s->global->p2p_disabled)
   6773 		return;
   6774 	if (wpa_s->global->p2p == NULL)
   6775 		return;
   6776 
   6777 	bands = wpas_get_bands(wpa_s, NULL);
   6778 	p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
   6779 }
   6780 
   6781 
   6782 static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
   6783 {
   6784 	p2p_group_deinit(wpa_s->p2p_group);
   6785 	wpa_s->p2p_group = NULL;
   6786 
   6787 	wpa_s->ap_configured_cb = NULL;
   6788 	wpa_s->ap_configured_cb_ctx = NULL;
   6789 	wpa_s->ap_configured_cb_data = NULL;
   6790 	wpa_s->connect_without_scan = NULL;
   6791 }
   6792 
   6793 
   6794 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
   6795 {
   6796 	wpa_s->p2p_long_listen = 0;
   6797 
   6798 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
   6799 		return -1;
   6800 
   6801 	return p2p_reject(wpa_s->global->p2p, addr);
   6802 }
   6803 
   6804 
   6805 /* Invite to reinvoke a persistent group */
   6806 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
   6807 		    struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
   6808 		    int vht_center_freq2, int ht40, int vht, int max_chwidth,
   6809 		    int pref_freq)
   6810 {
   6811 	enum p2p_invite_role role;
   6812 	u8 *bssid = NULL;
   6813 	int force_freq = 0;
   6814 	int res;
   6815 	int no_pref_freq_given = pref_freq == 0;
   6816 	unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
   6817 
   6818 	wpa_s->global->p2p_invite_group = NULL;
   6819 	if (peer_addr)
   6820 		os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
   6821 	else
   6822 		os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
   6823 
   6824 	wpa_s->p2p_persistent_go_freq = freq;
   6825 	wpa_s->p2p_go_ht40 = !!ht40;
   6826 	wpa_s->p2p_go_vht = !!vht;
   6827 	wpa_s->p2p_go_max_oper_chwidth = max_chwidth;
   6828 	wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
   6829 	if (ssid->mode == WPAS_MODE_P2P_GO) {
   6830 		role = P2P_INVITE_ROLE_GO;
   6831 		if (peer_addr == NULL) {
   6832 			wpa_printf(MSG_DEBUG, "P2P: Missing peer "
   6833 				   "address in invitation command");
   6834 			return -1;
   6835 		}
   6836 		if (wpas_p2p_create_iface(wpa_s)) {
   6837 			if (wpas_p2p_add_group_interface(wpa_s,
   6838 							 WPA_IF_P2P_GO) < 0) {
   6839 				wpa_printf(MSG_ERROR, "P2P: Failed to "
   6840 					   "allocate a new interface for the "
   6841 					   "group");
   6842 				return -1;
   6843 			}
   6844 			bssid = wpa_s->pending_interface_addr;
   6845 		} else if (wpa_s->p2p_mgmt)
   6846 			bssid = wpa_s->parent->own_addr;
   6847 		else
   6848 			bssid = wpa_s->own_addr;
   6849 	} else {
   6850 		role = P2P_INVITE_ROLE_CLIENT;
   6851 		peer_addr = ssid->bssid;
   6852 	}
   6853 	wpa_s->pending_invite_ssid_id = ssid->id;
   6854 
   6855 	size = P2P_MAX_PREF_CHANNELS;
   6856 	res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
   6857 				   role == P2P_INVITE_ROLE_GO,
   6858 				   pref_freq_list, &size);
   6859 	if (res)
   6860 		return res;
   6861 
   6862 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
   6863 		return -1;
   6864 
   6865 	p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
   6866 
   6867 	if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
   6868 	    no_pref_freq_given && pref_freq > 0 &&
   6869 	    wpa_s->num_multichan_concurrent > 1 &&
   6870 	    wpas_p2p_num_unused_channels(wpa_s) > 0) {
   6871 		wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
   6872 			   pref_freq);
   6873 		pref_freq = 0;
   6874 	}
   6875 
   6876 	/*
   6877 	 * Stop any find/listen operations before invitation and possibly
   6878 	 * connection establishment.
   6879 	 */
   6880 	wpas_p2p_stop_find_oper(wpa_s);
   6881 
   6882 	return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
   6883 			  ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
   6884 			  1, pref_freq, -1);
   6885 }
   6886 
   6887 
   6888 /* Invite to join an active group */
   6889 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
   6890 			  const u8 *peer_addr, const u8 *go_dev_addr)
   6891 {
   6892 	struct wpa_global *global = wpa_s->global;
   6893 	enum p2p_invite_role role;
   6894 	u8 *bssid = NULL;
   6895 	struct wpa_ssid *ssid;
   6896 	int persistent;
   6897 	int freq = 0, force_freq = 0, pref_freq = 0;
   6898 	int res;
   6899 	unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
   6900 
   6901 	wpa_s->p2p_persistent_go_freq = 0;
   6902 	wpa_s->p2p_go_ht40 = 0;
   6903 	wpa_s->p2p_go_vht = 0;
   6904 	wpa_s->p2p_go_vht_center_freq2 = 0;
   6905 	wpa_s->p2p_go_max_oper_chwidth = 0;
   6906 
   6907 	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
   6908 		if (os_strcmp(wpa_s->ifname, ifname) == 0)
   6909 			break;
   6910 	}
   6911 	if (wpa_s == NULL) {
   6912 		wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
   6913 		return -1;
   6914 	}
   6915 
   6916 	ssid = wpa_s->current_ssid;
   6917 	if (ssid == NULL) {
   6918 		wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
   6919 			   "invitation");
   6920 		return -1;
   6921 	}
   6922 
   6923 	wpa_s->global->p2p_invite_group = wpa_s;
   6924 	persistent = ssid->p2p_persistent_group &&
   6925 		wpas_p2p_get_persistent(wpa_s->p2pdev, peer_addr,
   6926 					ssid->ssid, ssid->ssid_len);
   6927 
   6928 	if (ssid->mode == WPAS_MODE_P2P_GO) {
   6929 		role = P2P_INVITE_ROLE_ACTIVE_GO;
   6930 		bssid = wpa_s->own_addr;
   6931 		if (go_dev_addr == NULL)
   6932 			go_dev_addr = wpa_s->global->p2p_dev_addr;
   6933 		freq = ssid->frequency;
   6934 	} else {
   6935 		role = P2P_INVITE_ROLE_CLIENT;
   6936 		if (wpa_s->wpa_state < WPA_ASSOCIATED) {
   6937 			wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
   6938 				   "invite to current group");
   6939 			return -1;
   6940 		}
   6941 		bssid = wpa_s->bssid;
   6942 		if (go_dev_addr == NULL &&
   6943 		    !is_zero_ether_addr(wpa_s->go_dev_addr))
   6944 			go_dev_addr = wpa_s->go_dev_addr;
   6945 		freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
   6946 			(int) wpa_s->assoc_freq;
   6947 	}
   6948 	wpa_s->p2pdev->pending_invite_ssid_id = -1;
   6949 
   6950 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
   6951 		return -1;
   6952 
   6953 	size = P2P_MAX_PREF_CHANNELS;
   6954 	res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
   6955 				   role == P2P_INVITE_ROLE_ACTIVE_GO,
   6956 				   pref_freq_list, &size);
   6957 	if (res)
   6958 		return res;
   6959 	wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
   6960 
   6961 	return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
   6962 			  ssid->ssid, ssid->ssid_len, force_freq,
   6963 			  go_dev_addr, persistent, pref_freq, -1);
   6964 }
   6965 
   6966 
   6967 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
   6968 {
   6969 	struct wpa_ssid *ssid = wpa_s->current_ssid;
   6970 	u8 go_dev_addr[ETH_ALEN];
   6971 	int persistent;
   6972 	int freq;
   6973 	u8 ip[3 * 4];
   6974 	char ip_addr[100];
   6975 
   6976 	if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
   6977 		eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
   6978 				     wpa_s->p2pdev, NULL);
   6979 	}
   6980 
   6981 	if (!wpa_s->show_group_started || !ssid)
   6982 		return;
   6983 
   6984 	wpa_s->show_group_started = 0;
   6985 	if (!wpa_s->p2p_go_group_formation_completed &&
   6986 	    wpa_s->global->p2p_group_formation == wpa_s) {
   6987 		wpa_dbg(wpa_s, MSG_DEBUG,
   6988 			"P2P: Marking group formation completed on client on data connection");
   6989 		wpa_s->p2p_go_group_formation_completed = 1;
   6990 		wpa_s->global->p2p_group_formation = NULL;
   6991 		wpa_s->p2p_in_provisioning = 0;
   6992 		wpa_s->p2p_in_invitation = 0;
   6993 	}
   6994 
   6995 	os_memset(go_dev_addr, 0, ETH_ALEN);
   6996 	if (ssid->bssid_set)
   6997 		os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
   6998 	persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
   6999 					       ssid->ssid_len);
   7000 	os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
   7001 
   7002 	if (wpa_s->global->p2p_group_formation == wpa_s)
   7003 		wpa_s->global->p2p_group_formation = NULL;
   7004 
   7005 	freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
   7006 		(int) wpa_s->assoc_freq;
   7007 
   7008 	ip_addr[0] = '\0';
   7009 	if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
   7010 		int res;
   7011 
   7012 		res = os_snprintf(ip_addr, sizeof(ip_addr),
   7013 				  " ip_addr=%u.%u.%u.%u "
   7014 				  "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
   7015 				  ip[0], ip[1], ip[2], ip[3],
   7016 				  ip[4], ip[5], ip[6], ip[7],
   7017 				  ip[8], ip[9], ip[10], ip[11]);
   7018 		if (os_snprintf_error(sizeof(ip_addr), res))
   7019 			ip_addr[0] = '\0';
   7020 	}
   7021 
   7022 	wpas_p2p_group_started(wpa_s, 0, ssid, freq,
   7023 			       ssid->passphrase == NULL && ssid->psk_set ?
   7024 			       ssid->psk : NULL,
   7025 			       ssid->passphrase, go_dev_addr, persistent,
   7026 			       ip_addr);
   7027 
   7028 	if (persistent)
   7029 		wpas_p2p_store_persistent_group(wpa_s->p2pdev,
   7030 						ssid, go_dev_addr);
   7031 
   7032 	wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 1, ip);
   7033 }
   7034 
   7035 
   7036 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
   7037 			  u32 interval1, u32 duration2, u32 interval2)
   7038 {
   7039 	int ret;
   7040 
   7041 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
   7042 		return -1;
   7043 
   7044 	if (wpa_s->wpa_state < WPA_ASSOCIATED ||
   7045 	    wpa_s->current_ssid == NULL ||
   7046 	    wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
   7047 		return -1;
   7048 
   7049 	ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
   7050 			       wpa_s->own_addr, wpa_s->assoc_freq,
   7051 			       duration1, interval1, duration2, interval2);
   7052 	if (ret == 0)
   7053 		wpa_s->waiting_presence_resp = 1;
   7054 
   7055 	return ret;
   7056 }
   7057 
   7058 
   7059 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
   7060 			unsigned int interval)
   7061 {
   7062 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
   7063 		return -1;
   7064 
   7065 	return p2p_ext_listen(wpa_s->global->p2p, period, interval);
   7066 }
   7067 
   7068 
   7069 static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
   7070 {
   7071 	if (wpa_s->current_ssid == NULL) {
   7072 		/*
   7073 		 * current_ssid can be cleared when P2P client interface gets
   7074 		 * disconnected, so assume this interface was used as P2P
   7075 		 * client.
   7076 		 */
   7077 		return 1;
   7078 	}
   7079 	return wpa_s->current_ssid->p2p_group &&
   7080 		wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
   7081 }
   7082 
   7083 
   7084 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
   7085 {
   7086 	struct wpa_supplicant *wpa_s = eloop_ctx;
   7087 
   7088 	if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
   7089 		wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
   7090 			   "disabled");
   7091 		return;
   7092 	}
   7093 
   7094 	wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
   7095 		   "group");
   7096 	wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
   7097 }
   7098 
   7099 
   7100 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
   7101 {
   7102 	int timeout;
   7103 
   7104 	if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
   7105 		wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
   7106 
   7107 	if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
   7108 		return;
   7109 
   7110 	timeout = wpa_s->conf->p2p_group_idle;
   7111 	if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
   7112 	    (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
   7113 	    timeout = P2P_MAX_CLIENT_IDLE;
   7114 
   7115 	if (timeout == 0)
   7116 		return;
   7117 
   7118 	if (timeout < 0) {
   7119 		if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
   7120 			timeout = 0; /* special client mode no-timeout */
   7121 		else
   7122 			return;
   7123 	}
   7124 
   7125 	if (wpa_s->p2p_in_provisioning) {
   7126 		/*
   7127 		 * Use the normal group formation timeout during the
   7128 		 * provisioning phase to avoid terminating this process too
   7129 		 * early due to group idle timeout.
   7130 		 */
   7131 		wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
   7132 			   "during provisioning");
   7133 		return;
   7134 	}
   7135 
   7136 	if (wpa_s->show_group_started) {
   7137 		/*
   7138 		 * Use the normal group formation timeout between the end of
   7139 		 * the provisioning phase and completion of 4-way handshake to
   7140 		 * avoid terminating this process too early due to group idle
   7141 		 * timeout.
   7142 		 */
   7143 		wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
   7144 			   "while waiting for initial 4-way handshake to "
   7145 			   "complete");
   7146 		return;
   7147 	}
   7148 
   7149 	wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
   7150 		   timeout);
   7151 	eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
   7152 			       wpa_s, NULL);
   7153 }
   7154 
   7155 
   7156 /* Returns 1 if the interface was removed */
   7157 int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
   7158 			  u16 reason_code, const u8 *ie, size_t ie_len,
   7159 			  int locally_generated)
   7160 {
   7161 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
   7162 		return 0;
   7163 
   7164 	if (!locally_generated)
   7165 		p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
   7166 				 ie_len);
   7167 
   7168 	if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
   7169 	    wpa_s->current_ssid &&
   7170 	    wpa_s->current_ssid->p2p_group &&
   7171 	    wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
   7172 		wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
   7173 			   "session is ending");
   7174 		if (wpas_p2p_group_delete(wpa_s,
   7175 					  P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
   7176 		    > 0)
   7177 			return 1;
   7178 	}
   7179 
   7180 	return 0;
   7181 }
   7182 
   7183 
   7184 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
   7185 			     u16 reason_code, const u8 *ie, size_t ie_len,
   7186 			     int locally_generated)
   7187 {
   7188 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
   7189 		return;
   7190 
   7191 	if (!locally_generated)
   7192 		p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
   7193 				   ie_len);
   7194 }
   7195 
   7196 
   7197 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
   7198 {
   7199 	struct p2p_data *p2p = wpa_s->global->p2p;
   7200 
   7201 	if (p2p == NULL)
   7202 		return;
   7203 
   7204 	if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
   7205 		return;
   7206 
   7207 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
   7208 		p2p_set_dev_name(p2p, wpa_s->conf->device_name);
   7209 
   7210 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
   7211 		p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
   7212 
   7213 	if (wpa_s->wps &&
   7214 	    (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
   7215 		p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
   7216 
   7217 	if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
   7218 		p2p_set_uuid(p2p, wpa_s->wps->uuid);
   7219 
   7220 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
   7221 		p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
   7222 		p2p_set_model_name(p2p, wpa_s->conf->model_name);
   7223 		p2p_set_model_number(p2p, wpa_s->conf->model_number);
   7224 		p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
   7225 	}
   7226 
   7227 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
   7228 		p2p_set_sec_dev_types(p2p,
   7229 				      (void *) wpa_s->conf->sec_device_type,
   7230 				      wpa_s->conf->num_sec_device_types);
   7231 
   7232 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
   7233 		int i;
   7234 		p2p_remove_wps_vendor_extensions(p2p);
   7235 		for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
   7236 			if (wpa_s->conf->wps_vendor_ext[i] == NULL)
   7237 				continue;
   7238 			p2p_add_wps_vendor_extension(
   7239 				p2p, wpa_s->conf->wps_vendor_ext[i]);
   7240 		}
   7241 	}
   7242 
   7243 	if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
   7244 	    wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
   7245 		char country[3];
   7246 		country[0] = wpa_s->conf->country[0];
   7247 		country[1] = wpa_s->conf->country[1];
   7248 		country[2] = 0x04;
   7249 		p2p_set_country(p2p, country);
   7250 	}
   7251 
   7252 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
   7253 		p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
   7254 				     wpa_s->conf->p2p_ssid_postfix ?
   7255 				     os_strlen(wpa_s->conf->p2p_ssid_postfix) :
   7256 				     0);
   7257 	}
   7258 
   7259 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
   7260 		p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
   7261 
   7262 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
   7263 		u8 reg_class, channel;
   7264 		int ret;
   7265 		unsigned int r;
   7266 		u8 channel_forced;
   7267 
   7268 		if (wpa_s->conf->p2p_listen_reg_class &&
   7269 		    wpa_s->conf->p2p_listen_channel) {
   7270 			reg_class = wpa_s->conf->p2p_listen_reg_class;
   7271 			channel = wpa_s->conf->p2p_listen_channel;
   7272 			channel_forced = 1;
   7273 		} else {
   7274 			reg_class = 81;
   7275 			/*
   7276 			 * Pick one of the social channels randomly as the
   7277 			 * listen channel.
   7278 			 */
   7279 			if (os_get_random((u8 *) &r, sizeof(r)) < 0)
   7280 				channel = 1;
   7281 			else
   7282 				channel = 1 + (r % 3) * 5;
   7283 			channel_forced = 0;
   7284 		}
   7285 		ret = p2p_set_listen_channel(p2p, reg_class, channel,
   7286 					     channel_forced);
   7287 		if (ret)
   7288 			wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
   7289 				   "failed: %d", ret);
   7290 	}
   7291 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
   7292 		u8 op_reg_class, op_channel, cfg_op_channel;
   7293 		int ret = 0;
   7294 		unsigned int r;
   7295 		if (wpa_s->conf->p2p_oper_reg_class &&
   7296 		    wpa_s->conf->p2p_oper_channel) {
   7297 			op_reg_class = wpa_s->conf->p2p_oper_reg_class;
   7298 			op_channel = wpa_s->conf->p2p_oper_channel;
   7299 			cfg_op_channel = 1;
   7300 		} else {
   7301 			op_reg_class = 81;
   7302 			/*
   7303 			 * Use random operation channel from (1, 6, 11)
   7304 			 *if no other preference is indicated.
   7305 			 */
   7306 			if (os_get_random((u8 *) &r, sizeof(r)) < 0)
   7307 				op_channel = 1;
   7308 			else
   7309 				op_channel = 1 + (r % 3) * 5;
   7310 			cfg_op_channel = 0;
   7311 		}
   7312 		ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
   7313 					   cfg_op_channel);
   7314 		if (ret)
   7315 			wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
   7316 				   "failed: %d", ret);
   7317 	}
   7318 
   7319 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
   7320 		if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
   7321 				      wpa_s->conf->p2p_pref_chan) < 0) {
   7322 			wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
   7323 				   "update failed");
   7324 		}
   7325 
   7326 		if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
   7327 			wpa_printf(MSG_ERROR, "P2P: No GO channel list "
   7328 				   "update failed");
   7329 		}
   7330 	}
   7331 
   7332 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
   7333 		p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
   7334 }
   7335 
   7336 
   7337 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
   7338 		     int duration)
   7339 {
   7340 	if (!wpa_s->ap_iface)
   7341 		return -1;
   7342 	return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
   7343 				   duration);
   7344 }
   7345 
   7346 
   7347 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
   7348 {
   7349 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
   7350 		return -1;
   7351 
   7352 	wpa_s->global->cross_connection = enabled;
   7353 	p2p_set_cross_connect(wpa_s->global->p2p, enabled);
   7354 
   7355 	if (!enabled) {
   7356 		struct wpa_supplicant *iface;
   7357 
   7358 		for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
   7359 		{
   7360 			if (iface->cross_connect_enabled == 0)
   7361 				continue;
   7362 
   7363 			iface->cross_connect_enabled = 0;
   7364 			iface->cross_connect_in_use = 0;
   7365 			wpa_msg_global(iface->p2pdev, MSG_INFO,
   7366 				       P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
   7367 				       iface->ifname,
   7368 				       iface->cross_connect_uplink);
   7369 		}
   7370 	}
   7371 
   7372 	return 0;
   7373 }
   7374 
   7375 
   7376 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
   7377 {
   7378 	struct wpa_supplicant *iface;
   7379 
   7380 	if (!uplink->global->cross_connection)
   7381 		return;
   7382 
   7383 	for (iface = uplink->global->ifaces; iface; iface = iface->next) {
   7384 		if (!iface->cross_connect_enabled)
   7385 			continue;
   7386 		if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
   7387 		    0)
   7388 			continue;
   7389 		if (iface->ap_iface == NULL)
   7390 			continue;
   7391 		if (iface->cross_connect_in_use)
   7392 			continue;
   7393 
   7394 		iface->cross_connect_in_use = 1;
   7395 		wpa_msg_global(iface->p2pdev, MSG_INFO,
   7396 			       P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
   7397 			       iface->ifname, iface->cross_connect_uplink);
   7398 	}
   7399 }
   7400 
   7401 
   7402 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
   7403 {
   7404 	struct wpa_supplicant *iface;
   7405 
   7406 	for (iface = uplink->global->ifaces; iface; iface = iface->next) {
   7407 		if (!iface->cross_connect_enabled)
   7408 			continue;
   7409 		if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
   7410 		    0)
   7411 			continue;
   7412 		if (!iface->cross_connect_in_use)
   7413 			continue;
   7414 
   7415 		wpa_msg_global(iface->p2pdev, MSG_INFO,
   7416 			       P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
   7417 			       iface->ifname, iface->cross_connect_uplink);
   7418 		iface->cross_connect_in_use = 0;
   7419 	}
   7420 }
   7421 
   7422 
   7423 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
   7424 {
   7425 	if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
   7426 	    wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
   7427 	    wpa_s->cross_connect_disallowed)
   7428 		wpas_p2p_disable_cross_connect(wpa_s);
   7429 	else
   7430 		wpas_p2p_enable_cross_connect(wpa_s);
   7431 	if (!wpa_s->ap_iface &&
   7432 	    eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
   7433 		wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
   7434 }
   7435 
   7436 
   7437 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
   7438 {
   7439 	wpas_p2p_disable_cross_connect(wpa_s);
   7440 	if (!wpa_s->ap_iface &&
   7441 	    !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
   7442 					 wpa_s, NULL))
   7443 		wpas_p2p_set_group_idle_timeout(wpa_s);
   7444 }
   7445 
   7446 
   7447 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
   7448 {
   7449 	struct wpa_supplicant *iface;
   7450 
   7451 	if (!wpa_s->global->cross_connection)
   7452 		return;
   7453 
   7454 	for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
   7455 		if (iface == wpa_s)
   7456 			continue;
   7457 		if (iface->drv_flags &
   7458 		    WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
   7459 			continue;
   7460 		if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
   7461 		    iface != wpa_s->parent)
   7462 			continue;
   7463 
   7464 		wpa_s->cross_connect_enabled = 1;
   7465 		os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
   7466 			   sizeof(wpa_s->cross_connect_uplink));
   7467 		wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
   7468 			   "%s to %s whenever uplink is available",
   7469 			   wpa_s->ifname, wpa_s->cross_connect_uplink);
   7470 
   7471 		if (iface->ap_iface || iface->current_ssid == NULL ||
   7472 		    iface->current_ssid->mode != WPAS_MODE_INFRA ||
   7473 		    iface->cross_connect_disallowed ||
   7474 		    iface->wpa_state != WPA_COMPLETED)
   7475 			break;
   7476 
   7477 		wpa_s->cross_connect_in_use = 1;
   7478 		wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
   7479 			       P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
   7480 			       wpa_s->ifname, wpa_s->cross_connect_uplink);
   7481 		break;
   7482 	}
   7483 }
   7484 
   7485 
   7486 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
   7487 {
   7488 	if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
   7489 	    !wpa_s->p2p_in_provisioning)
   7490 		return 0; /* not P2P client operation */
   7491 
   7492 	wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
   7493 		   "session overlap");
   7494 	if (wpa_s != wpa_s->p2pdev)
   7495 		wpa_msg_ctrl(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_OVERLAP);
   7496 	wpas_p2p_group_formation_failed(wpa_s, 0);
   7497 	return 1;
   7498 }
   7499 
   7500 
   7501 void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
   7502 {
   7503 	struct wpa_supplicant *wpa_s = eloop_ctx;
   7504 	wpas_p2p_notif_pbc_overlap(wpa_s);
   7505 }
   7506 
   7507 
   7508 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s,
   7509 				  enum wpas_p2p_channel_update_trig trig)
   7510 {
   7511 	struct p2p_channels chan, cli_chan;
   7512 	struct wpa_used_freq_data *freqs = NULL;
   7513 	unsigned int num = wpa_s->num_multichan_concurrent;
   7514 
   7515 	if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
   7516 		return;
   7517 
   7518 	freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
   7519 	if (!freqs)
   7520 		return;
   7521 
   7522 	num = get_shared_radio_freqs_data(wpa_s, freqs, num);
   7523 
   7524 	os_memset(&chan, 0, sizeof(chan));
   7525 	os_memset(&cli_chan, 0, sizeof(cli_chan));
   7526 	if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
   7527 		wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
   7528 			   "channel list");
   7529 		return;
   7530 	}
   7531 
   7532 	p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
   7533 
   7534 	wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
   7535 
   7536 	/*
   7537 	 * The used frequencies map changed, so it is possible that a GO is
   7538 	 * using a channel that is no longer valid for P2P use. It is also
   7539 	 * possible that due to policy consideration, it would be preferable to
   7540 	 * move it to a frequency already used by other station interfaces.
   7541 	 */
   7542 	wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig);
   7543 
   7544 	os_free(freqs);
   7545 }
   7546 
   7547 
   7548 static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
   7549 				     struct wpa_scan_results *scan_res)
   7550 {
   7551 	wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
   7552 }
   7553 
   7554 
   7555 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
   7556 {
   7557 	struct wpa_global *global = wpa_s->global;
   7558 	int found = 0;
   7559 	const u8 *peer;
   7560 
   7561 	if (global->p2p == NULL)
   7562 		return -1;
   7563 
   7564 	wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
   7565 
   7566 	if (wpa_s->pending_interface_name[0] &&
   7567 	    !is_zero_ether_addr(wpa_s->pending_interface_addr))
   7568 		found = 1;
   7569 
   7570 	peer = p2p_get_go_neg_peer(global->p2p);
   7571 	if (peer) {
   7572 		wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
   7573 			   MACSTR, MAC2STR(peer));
   7574 		p2p_unauthorize(global->p2p, peer);
   7575 		found = 1;
   7576 	}
   7577 
   7578 	if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
   7579 		wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
   7580 		wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
   7581 		found = 1;
   7582 	}
   7583 
   7584 	if (wpa_s->pending_pd_before_join) {
   7585 		wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
   7586 		wpa_s->pending_pd_before_join = 0;
   7587 		found = 1;
   7588 	}
   7589 
   7590 	wpas_p2p_stop_find(wpa_s);
   7591 
   7592 	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
   7593 		if (wpa_s == global->p2p_group_formation &&
   7594 		    (wpa_s->p2p_in_provisioning ||
   7595 		     wpa_s->parent->pending_interface_type ==
   7596 		     WPA_IF_P2P_CLIENT)) {
   7597 			wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
   7598 				   "formation found - cancelling",
   7599 				   wpa_s->ifname);
   7600 			found = 1;
   7601 			eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
   7602 					     wpa_s->p2pdev, NULL);
   7603 			if (wpa_s->p2p_in_provisioning) {
   7604 				wpas_group_formation_completed(wpa_s, 0, 0);
   7605 				break;
   7606 			}
   7607 			wpas_p2p_group_delete(wpa_s,
   7608 					      P2P_GROUP_REMOVAL_REQUESTED);
   7609 			break;
   7610 		} else if (wpa_s->p2p_in_invitation) {
   7611 			wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
   7612 				   wpa_s->ifname);
   7613 			found = 1;
   7614 			wpas_p2p_group_formation_failed(wpa_s, 0);
   7615 			break;
   7616 		}
   7617 	}
   7618 
   7619 	if (!found) {
   7620 		wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
   7621 		return -1;
   7622 	}
   7623 
   7624 	return 0;
   7625 }
   7626 
   7627 
   7628 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
   7629 {
   7630 	if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
   7631 		return;
   7632 
   7633 	wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
   7634 		   "being available anymore");
   7635 	wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
   7636 }
   7637 
   7638 
   7639 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
   7640 				   int freq_24, int freq_5, int freq_overall)
   7641 {
   7642 	struct p2p_data *p2p = wpa_s->global->p2p;
   7643 	if (p2p == NULL)
   7644 		return;
   7645 	p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
   7646 }
   7647 
   7648 
   7649 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
   7650 {
   7651 	u8 peer[ETH_ALEN];
   7652 	struct p2p_data *p2p = wpa_s->global->p2p;
   7653 
   7654 	if (p2p == NULL)
   7655 		return -1;
   7656 
   7657 	if (hwaddr_aton(addr, peer))
   7658 		return -1;
   7659 
   7660 	return p2p_unauthorize(p2p, peer);
   7661 }
   7662 
   7663 
   7664 /**
   7665  * wpas_p2p_disconnect - Disconnect from a P2P Group
   7666  * @wpa_s: Pointer to wpa_supplicant data
   7667  * Returns: 0 on success, -1 on failure
   7668  *
   7669  * This can be used to disconnect from a group in which the local end is a P2P
   7670  * Client or to end a P2P Group in case the local end is the Group Owner. If a
   7671  * virtual network interface was created for this group, that interface will be
   7672  * removed. Otherwise, only the configured P2P group network will be removed
   7673  * from the interface.
   7674  */
   7675 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
   7676 {
   7677 
   7678 	if (wpa_s == NULL)
   7679 		return -1;
   7680 
   7681 	return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
   7682 		-1 : 0;
   7683 }
   7684 
   7685 
   7686 int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
   7687 {
   7688 	int ret;
   7689 
   7690 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
   7691 		return 0;
   7692 
   7693 	ret = p2p_in_progress(wpa_s->global->p2p);
   7694 	if (ret == 0) {
   7695 		/*
   7696 		 * Check whether there is an ongoing WPS provisioning step (or
   7697 		 * other parts of group formation) on another interface since
   7698 		 * p2p_in_progress() does not report this to avoid issues for
   7699 		 * scans during such provisioning step.
   7700 		 */
   7701 		if (wpa_s->global->p2p_group_formation &&
   7702 		    wpa_s->global->p2p_group_formation != wpa_s) {
   7703 			wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
   7704 				"in group formation",
   7705 				wpa_s->global->p2p_group_formation->ifname);
   7706 			ret = 1;
   7707 		}
   7708 	}
   7709 
   7710 	if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
   7711 		struct os_reltime now;
   7712 		os_get_reltime(&now);
   7713 		if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
   7714 				       P2P_MAX_INITIAL_CONN_WAIT_GO)) {
   7715 			/* Wait for the first client has expired */
   7716 			wpa_s->global->p2p_go_wait_client.sec = 0;
   7717 		} else {
   7718 			wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
   7719 			ret = 1;
   7720 		}
   7721 	}
   7722 
   7723 	return ret;
   7724 }
   7725 
   7726 
   7727 void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
   7728 			      struct wpa_ssid *ssid)
   7729 {
   7730 	if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
   7731 	    eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
   7732 				 wpa_s->p2pdev, NULL) > 0) {
   7733 		/**
   7734 		 * Remove the network by scheduling the group formation
   7735 		 * timeout to happen immediately. The teardown code
   7736 		 * needs to be scheduled to run asynch later so that we
   7737 		 * don't delete data from under ourselves unexpectedly.
   7738 		 * Calling wpas_p2p_group_formation_timeout directly
   7739 		 * causes a series of crashes in WPS failure scenarios.
   7740 		 */
   7741 		wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
   7742 			   "P2P group network getting removed");
   7743 		eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
   7744 				       wpa_s->p2pdev, NULL);
   7745 	}
   7746 }
   7747 
   7748 
   7749 struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
   7750 					  const u8 *addr, const u8 *ssid,
   7751 					  size_t ssid_len)
   7752 {
   7753 	struct wpa_ssid *s;
   7754 	size_t i;
   7755 
   7756 	for (s = wpa_s->conf->ssid; s; s = s->next) {
   7757 		if (s->disabled != 2)
   7758 			continue;
   7759 		if (ssid &&
   7760 		    (ssid_len != s->ssid_len ||
   7761 		     os_memcmp(ssid, s->ssid, ssid_len) != 0))
   7762 			continue;
   7763 		if (addr == NULL) {
   7764 			if (s->mode == WPAS_MODE_P2P_GO)
   7765 				return s;
   7766 			continue;
   7767 		}
   7768 		if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
   7769 			return s; /* peer is GO in the persistent group */
   7770 		if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
   7771 			continue;
   7772 		for (i = 0; i < s->num_p2p_clients; i++) {
   7773 			if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
   7774 				      addr, ETH_ALEN) == 0)
   7775 				return s; /* peer is P2P client in persistent
   7776 					   * group */
   7777 		}
   7778 	}
   7779 
   7780 	return NULL;
   7781 }
   7782 
   7783 
   7784 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
   7785 				       const u8 *addr)
   7786 {
   7787 	if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
   7788 				 wpa_s->p2pdev, NULL) > 0) {
   7789 		/*
   7790 		 * This can happen if WPS provisioning step is not terminated
   7791 		 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
   7792 		 * peer was able to connect, there is no need to time out group
   7793 		 * formation after this, though. In addition, this is used with
   7794 		 * the initial connection wait on the GO as a separate formation
   7795 		 * timeout and as such, expected to be hit after the initial WPS
   7796 		 * provisioning step.
   7797 		 */
   7798 		wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
   7799 
   7800 		if (!wpa_s->p2p_go_group_formation_completed &&
   7801 		    !wpa_s->group_formation_reported) {
   7802 			/*
   7803 			 * GO has not yet notified group formation success since
   7804 			 * the WPS step was not completed cleanly. Do that
   7805 			 * notification now since the P2P Client was able to
   7806 			 * connect and as such, must have received the
   7807 			 * credential from the WPS step.
   7808 			 */
   7809 			if (wpa_s->global->p2p)
   7810 				p2p_wps_success_cb(wpa_s->global->p2p, addr);
   7811 			wpas_group_formation_completed(wpa_s, 1, 0);
   7812 		}
   7813 	}
   7814 	if (!wpa_s->p2p_go_group_formation_completed) {
   7815 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
   7816 		wpa_s->p2p_go_group_formation_completed = 1;
   7817 		wpa_s->global->p2p_group_formation = NULL;
   7818 		wpa_s->p2p_in_provisioning = 0;
   7819 		wpa_s->p2p_in_invitation = 0;
   7820 	}
   7821 	wpa_s->global->p2p_go_wait_client.sec = 0;
   7822 	if (addr == NULL)
   7823 		return;
   7824 	wpas_p2p_add_persistent_group_client(wpa_s, addr);
   7825 }
   7826 
   7827 
   7828 static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
   7829 				       int group_added)
   7830 {
   7831 	struct wpa_supplicant *group = wpa_s;
   7832 	int ret = 0;
   7833 
   7834 	if (wpa_s->global->p2p_group_formation)
   7835 		group = wpa_s->global->p2p_group_formation;
   7836 	wpa_s = wpa_s->global->p2p_init_wpa_s;
   7837 	offchannel_send_action_done(wpa_s);
   7838 	if (group_added)
   7839 		ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
   7840 	wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
   7841 	wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
   7842 			 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
   7843 			 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
   7844 			 wpa_s->p2p_go_vht_center_freq2,
   7845 			 wpa_s->p2p_persistent_id,
   7846 			 wpa_s->p2p_pd_before_go_neg,
   7847 			 wpa_s->p2p_go_ht40,
   7848 			 wpa_s->p2p_go_vht,
   7849 			 wpa_s->p2p_go_max_oper_chwidth, NULL, 0);
   7850 	return ret;
   7851 }
   7852 
   7853 
   7854 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
   7855 {
   7856 	int res;
   7857 
   7858 	if (!wpa_s->p2p_fallback_to_go_neg ||
   7859 	    wpa_s->p2p_in_provisioning <= 5)
   7860 		return 0;
   7861 
   7862 	if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
   7863 		return 0; /* peer operating as a GO */
   7864 
   7865 	wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
   7866 		"fallback to GO Negotiation");
   7867 	wpa_msg_global(wpa_s->p2pdev, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
   7868 		       "reason=GO-not-found");
   7869 	res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
   7870 
   7871 	return res == 1 ? 2 : 1;
   7872 }
   7873 
   7874 
   7875 unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
   7876 {
   7877 	struct wpa_supplicant *ifs;
   7878 
   7879 	if (wpa_s->wpa_state > WPA_SCANNING) {
   7880 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
   7881 			"concurrent operation",
   7882 			wpa_s->conf->p2p_search_delay);
   7883 		return wpa_s->conf->p2p_search_delay;
   7884 	}
   7885 
   7886 	dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
   7887 			 radio_list) {
   7888 		if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
   7889 			wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
   7890 				"delay due to concurrent operation on "
   7891 				"interface %s",
   7892 				wpa_s->conf->p2p_search_delay,
   7893 				ifs->ifname);
   7894 			return wpa_s->conf->p2p_search_delay;
   7895 		}
   7896 	}
   7897 
   7898 	return 0;
   7899 }
   7900 
   7901 
   7902 static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
   7903 				     struct wpa_ssid *s, const u8 *addr,
   7904 				     int iface_addr)
   7905 {
   7906 	struct psk_list_entry *psk, *tmp;
   7907 	int changed = 0;
   7908 
   7909 	dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
   7910 			      list) {
   7911 		if ((iface_addr && !psk->p2p &&
   7912 		     os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
   7913 		    (!iface_addr && psk->p2p &&
   7914 		     os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
   7915 			wpa_dbg(wpa_s, MSG_DEBUG,
   7916 				"P2P: Remove persistent group PSK list entry for "
   7917 				MACSTR " p2p=%u",
   7918 				MAC2STR(psk->addr), psk->p2p);
   7919 			dl_list_del(&psk->list);
   7920 			os_free(psk);
   7921 			changed++;
   7922 		}
   7923 	}
   7924 
   7925 	return changed;
   7926 }
   7927 
   7928 
   7929 void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
   7930 			 const u8 *p2p_dev_addr,
   7931 			 const u8 *psk, size_t psk_len)
   7932 {
   7933 	struct wpa_ssid *ssid = wpa_s->current_ssid;
   7934 	struct wpa_ssid *persistent;
   7935 	struct psk_list_entry *p, *last;
   7936 
   7937 	if (psk_len != sizeof(p->psk))
   7938 		return;
   7939 
   7940 	if (p2p_dev_addr) {
   7941 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
   7942 			" p2p_dev_addr=" MACSTR,
   7943 			MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
   7944 		if (is_zero_ether_addr(p2p_dev_addr))
   7945 			p2p_dev_addr = NULL;
   7946 	} else {
   7947 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
   7948 			MAC2STR(mac_addr));
   7949 	}
   7950 
   7951 	if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
   7952 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
   7953 		/* To be added to persistent group once created */
   7954 		if (wpa_s->global->add_psk == NULL) {
   7955 			wpa_s->global->add_psk = os_zalloc(sizeof(*p));
   7956 			if (wpa_s->global->add_psk == NULL)
   7957 				return;
   7958 		}
   7959 		p = wpa_s->global->add_psk;
   7960 		if (p2p_dev_addr) {
   7961 			p->p2p = 1;
   7962 			os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
   7963 		} else {
   7964 			p->p2p = 0;
   7965 			os_memcpy(p->addr, mac_addr, ETH_ALEN);
   7966 		}
   7967 		os_memcpy(p->psk, psk, psk_len);
   7968 		return;
   7969 	}
   7970 
   7971 	if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
   7972 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
   7973 		return;
   7974 	}
   7975 
   7976 	persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
   7977 					     ssid->ssid_len);
   7978 	if (!persistent) {
   7979 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
   7980 		return;
   7981 	}
   7982 
   7983 	p = os_zalloc(sizeof(*p));
   7984 	if (p == NULL)
   7985 		return;
   7986 	if (p2p_dev_addr) {
   7987 		p->p2p = 1;
   7988 		os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
   7989 	} else {
   7990 		p->p2p = 0;
   7991 		os_memcpy(p->addr, mac_addr, ETH_ALEN);
   7992 	}
   7993 	os_memcpy(p->psk, psk, psk_len);
   7994 
   7995 	if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
   7996 	    (last = dl_list_last(&persistent->psk_list,
   7997 				 struct psk_list_entry, list))) {
   7998 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
   7999 			MACSTR " (p2p=%u) to make room for a new one",
   8000 			MAC2STR(last->addr), last->p2p);
   8001 		dl_list_del(&last->list);
   8002 		os_free(last);
   8003 	}
   8004 
   8005 	wpas_p2p_remove_psk_entry(wpa_s->p2pdev, persistent,
   8006 				  p2p_dev_addr ? p2p_dev_addr : mac_addr,
   8007 				  p2p_dev_addr == NULL);
   8008 	if (p2p_dev_addr) {
   8009 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
   8010 			MACSTR, MAC2STR(p2p_dev_addr));
   8011 	} else {
   8012 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
   8013 			MAC2STR(mac_addr));
   8014 	}
   8015 	dl_list_add(&persistent->psk_list, &p->list);
   8016 
   8017 	if (wpa_s->p2pdev->conf->update_config &&
   8018 	    wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
   8019 		wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
   8020 }
   8021 
   8022 
   8023 static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
   8024 				struct wpa_ssid *s, const u8 *addr,
   8025 				int iface_addr)
   8026 {
   8027 	int res;
   8028 
   8029 	res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
   8030 	if (res > 0 && wpa_s->conf->update_config &&
   8031 	    wpa_config_write(wpa_s->confname, wpa_s->conf))
   8032 		wpa_dbg(wpa_s, MSG_DEBUG,
   8033 			"P2P: Failed to update configuration");
   8034 }
   8035 
   8036 
   8037 static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
   8038 				      const u8 *peer, int iface_addr)
   8039 {
   8040 	struct hostapd_data *hapd;
   8041 	struct hostapd_wpa_psk *psk, *prev, *rem;
   8042 	struct sta_info *sta;
   8043 
   8044 	if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
   8045 	    wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
   8046 		return;
   8047 
   8048 	/* Remove per-station PSK entry */
   8049 	hapd = wpa_s->ap_iface->bss[0];
   8050 	prev = NULL;
   8051 	psk = hapd->conf->ssid.wpa_psk;
   8052 	while (psk) {
   8053 		if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
   8054 		    (!iface_addr &&
   8055 		     os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
   8056 			wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
   8057 				MACSTR " iface_addr=%d",
   8058 				MAC2STR(peer), iface_addr);
   8059 			if (prev)
   8060 				prev->next = psk->next;
   8061 			else
   8062 				hapd->conf->ssid.wpa_psk = psk->next;
   8063 			rem = psk;
   8064 			psk = psk->next;
   8065 			os_free(rem);
   8066 		} else {
   8067 			prev = psk;
   8068 			psk = psk->next;
   8069 		}
   8070 	}
   8071 
   8072 	/* Disconnect from group */
   8073 	if (iface_addr)
   8074 		sta = ap_get_sta(hapd, peer);
   8075 	else
   8076 		sta = ap_get_sta_p2p(hapd, peer);
   8077 	if (sta) {
   8078 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
   8079 			" (iface_addr=%d) from group",
   8080 			MAC2STR(peer), iface_addr);
   8081 		hostapd_drv_sta_deauth(hapd, sta->addr,
   8082 				       WLAN_REASON_DEAUTH_LEAVING);
   8083 		ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
   8084 	}
   8085 }
   8086 
   8087 
   8088 void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
   8089 			    int iface_addr)
   8090 {
   8091 	struct wpa_ssid *s;
   8092 	struct wpa_supplicant *w;
   8093 	struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
   8094 
   8095 	wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
   8096 
   8097 	/* Remove from any persistent group */
   8098 	for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
   8099 		if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
   8100 			continue;
   8101 		if (!iface_addr)
   8102 			wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
   8103 		wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
   8104 	}
   8105 
   8106 	/* Remove from any operating group */
   8107 	for (w = wpa_s->global->ifaces; w; w = w->next)
   8108 		wpas_p2p_remove_client_go(w, peer, iface_addr);
   8109 }
   8110 
   8111 
   8112 static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
   8113 {
   8114 	struct wpa_supplicant *wpa_s = eloop_ctx;
   8115 	wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
   8116 }
   8117 
   8118 
   8119 static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
   8120 {
   8121 	struct wpa_supplicant *wpa_s = eloop_ctx;
   8122 
   8123 	wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
   8124 	wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
   8125 }
   8126 
   8127 
   8128 int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
   8129 					struct wpa_ssid *ssid)
   8130 {
   8131 	struct wpa_supplicant *iface;
   8132 
   8133 	for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
   8134 		if (!iface->current_ssid ||
   8135 		    iface->current_ssid->frequency == freq ||
   8136 		    (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
   8137 		     !iface->current_ssid->p2p_group))
   8138 			continue;
   8139 
   8140 		/* Remove the connection with least priority */
   8141 		if (!wpas_is_p2p_prioritized(iface)) {
   8142 			/* STA connection has priority over existing
   8143 			 * P2P connection, so remove the interface. */
   8144 			wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
   8145 			eloop_register_timeout(0, 0,
   8146 					       wpas_p2p_group_freq_conflict,
   8147 					       iface, NULL);
   8148 			/* If connection in progress is P2P connection, do not
   8149 			 * proceed for the connection. */
   8150 			if (wpa_s == iface)
   8151 				return -1;
   8152 			else
   8153 				return 0;
   8154 		} else {
   8155 			/* P2P connection has priority, disable the STA network
   8156 			 */
   8157 			wpa_supplicant_disable_network(wpa_s->global->ifaces,
   8158 						       ssid);
   8159 			wpa_msg(wpa_s->global->ifaces, MSG_INFO,
   8160 				WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
   8161 			os_memset(wpa_s->global->ifaces->pending_bssid, 0,
   8162 				  ETH_ALEN);
   8163 			/* If P2P connection is in progress, continue
   8164 			 * connecting...*/
   8165 			if (wpa_s == iface)
   8166 				return 0;
   8167 			else
   8168 				return -1;
   8169 		}
   8170 	}
   8171 
   8172 	return 0;
   8173 }
   8174 
   8175 
   8176 int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
   8177 {
   8178 	struct wpa_ssid *ssid = wpa_s->current_ssid;
   8179 
   8180 	if (ssid == NULL || !ssid->p2p_group)
   8181 		return 0;
   8182 
   8183 	if (wpa_s->p2p_last_4way_hs_fail &&
   8184 	    wpa_s->p2p_last_4way_hs_fail == ssid) {
   8185 		u8 go_dev_addr[ETH_ALEN];
   8186 		struct wpa_ssid *persistent;
   8187 
   8188 		if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
   8189 					      ssid->ssid,
   8190 					      ssid->ssid_len) <= 0) {
   8191 			wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
   8192 			goto disconnect;
   8193 		}
   8194 
   8195 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
   8196 			MACSTR, MAC2STR(go_dev_addr));
   8197 		persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, go_dev_addr,
   8198 						     ssid->ssid,
   8199 						     ssid->ssid_len);
   8200 		if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
   8201 			wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
   8202 			goto disconnect;
   8203 		}
   8204 		wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
   8205 			       P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
   8206 			       persistent->id);
   8207 	disconnect:
   8208 		wpa_s->p2p_last_4way_hs_fail = NULL;
   8209 		/*
   8210 		 * Remove the group from a timeout to avoid issues with caller
   8211 		 * continuing to use the interface if this is on a P2P group
   8212 		 * interface.
   8213 		 */
   8214 		eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
   8215 				       wpa_s, NULL);
   8216 		return 1;
   8217 	}
   8218 
   8219 	wpa_s->p2p_last_4way_hs_fail = ssid;
   8220 	return 0;
   8221 }
   8222 
   8223 
   8224 #ifdef CONFIG_WPS_NFC
   8225 
   8226 static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
   8227 					     struct wpabuf *p2p)
   8228 {
   8229 	struct wpabuf *ret;
   8230 	size_t wsc_len;
   8231 
   8232 	if (p2p == NULL) {
   8233 		wpabuf_free(wsc);
   8234 		wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
   8235 		return NULL;
   8236 	}
   8237 
   8238 	wsc_len = wsc ? wpabuf_len(wsc) : 0;
   8239 	ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
   8240 	if (ret == NULL) {
   8241 		wpabuf_free(wsc);
   8242 		wpabuf_free(p2p);
   8243 		return NULL;
   8244 	}
   8245 
   8246 	wpabuf_put_be16(ret, wsc_len);
   8247 	if (wsc)
   8248 		wpabuf_put_buf(ret, wsc);
   8249 	wpabuf_put_be16(ret, wpabuf_len(p2p));
   8250 	wpabuf_put_buf(ret, p2p);
   8251 
   8252 	wpabuf_free(wsc);
   8253 	wpabuf_free(p2p);
   8254 	wpa_hexdump_buf(MSG_DEBUG,
   8255 			"P2P: Generated NFC connection handover message", ret);
   8256 
   8257 	if (ndef && ret) {
   8258 		struct wpabuf *tmp;
   8259 		tmp = ndef_build_p2p(ret);
   8260 		wpabuf_free(ret);
   8261 		if (tmp == NULL) {
   8262 			wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
   8263 			return NULL;
   8264 		}
   8265 		ret = tmp;
   8266 	}
   8267 
   8268 	return ret;
   8269 }
   8270 
   8271 
   8272 static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
   8273 			     struct wpa_ssid **ssid, u8 *go_dev_addr)
   8274 {
   8275 	struct wpa_supplicant *iface;
   8276 
   8277 	if (go_dev_addr)
   8278 		os_memset(go_dev_addr, 0, ETH_ALEN);
   8279 	if (ssid)
   8280 		*ssid = NULL;
   8281 	for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
   8282 		if (iface->wpa_state < WPA_ASSOCIATING ||
   8283 		    iface->current_ssid == NULL || iface->assoc_freq == 0 ||
   8284 		    !iface->current_ssid->p2p_group ||
   8285 		    iface->current_ssid->mode != WPAS_MODE_INFRA)
   8286 			continue;
   8287 		if (ssid)
   8288 			*ssid = iface->current_ssid;
   8289 		if (go_dev_addr)
   8290 			os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
   8291 		return iface->assoc_freq;
   8292 	}
   8293 	return 0;
   8294 }
   8295 
   8296 
   8297 struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
   8298 					  int ndef)
   8299 {
   8300 	struct wpabuf *wsc, *p2p;
   8301 	struct wpa_ssid *ssid;
   8302 	u8 go_dev_addr[ETH_ALEN];
   8303 	int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
   8304 
   8305 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
   8306 		wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
   8307 		return NULL;
   8308 	}
   8309 
   8310 	if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
   8311 	    wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
   8312 			   &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
   8313 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
   8314 		return NULL;
   8315 	}
   8316 
   8317 	if (cli_freq == 0) {
   8318 		wsc = wps_build_nfc_handover_req_p2p(
   8319 			wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
   8320 	} else
   8321 		wsc = NULL;
   8322 	p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
   8323 					 go_dev_addr, ssid ? ssid->ssid : NULL,
   8324 					 ssid ? ssid->ssid_len : 0);
   8325 
   8326 	return wpas_p2p_nfc_handover(ndef, wsc, p2p);
   8327 }
   8328 
   8329 
   8330 struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
   8331 					  int ndef, int tag)
   8332 {
   8333 	struct wpabuf *wsc, *p2p;
   8334 	struct wpa_ssid *ssid;
   8335 	u8 go_dev_addr[ETH_ALEN];
   8336 	int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
   8337 
   8338 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
   8339 		return NULL;
   8340 
   8341 	if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
   8342 	    wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
   8343 			   &wpa_s->conf->wps_nfc_dh_privkey) < 0)
   8344 		return NULL;
   8345 
   8346 	if (cli_freq == 0) {
   8347 		wsc = wps_build_nfc_handover_sel_p2p(
   8348 			wpa_s->parent->wps,
   8349 			tag ? wpa_s->conf->wps_nfc_dev_pw_id :
   8350 			DEV_PW_NFC_CONNECTION_HANDOVER,
   8351 			wpa_s->conf->wps_nfc_dh_pubkey,
   8352 			tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
   8353 	} else
   8354 		wsc = NULL;
   8355 	p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
   8356 					 go_dev_addr, ssid ? ssid->ssid : NULL,
   8357 					 ssid ? ssid->ssid_len : 0);
   8358 
   8359 	return wpas_p2p_nfc_handover(ndef, wsc, p2p);
   8360 }
   8361 
   8362 
   8363 static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
   8364 				   struct p2p_nfc_params *params)
   8365 {
   8366 	wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
   8367 		   "connection handover (freq=%d)",
   8368 		   params->go_freq);
   8369 
   8370 	if (params->go_freq && params->go_ssid_len) {
   8371 		wpa_s->p2p_wps_method = WPS_NFC;
   8372 		wpa_s->pending_join_wps_method = WPS_NFC;
   8373 		os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
   8374 		os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
   8375 			  ETH_ALEN);
   8376 		return wpas_p2p_join_start(wpa_s, params->go_freq,
   8377 					   params->go_ssid,
   8378 					   params->go_ssid_len);
   8379 	}
   8380 
   8381 	return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
   8382 				WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
   8383 				params->go_freq, wpa_s->p2p_go_vht_center_freq2,
   8384 				-1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
   8385 				params->go_ssid_len ? params->go_ssid : NULL,
   8386 				params->go_ssid_len);
   8387 }
   8388 
   8389 
   8390 static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
   8391 				  struct p2p_nfc_params *params, int tag)
   8392 {
   8393 	int res, persistent;
   8394 	struct wpa_ssid *ssid;
   8395 
   8396 	wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
   8397 		   "connection handover");
   8398 	for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
   8399 		ssid = wpa_s->current_ssid;
   8400 		if (ssid == NULL)
   8401 			continue;
   8402 		if (ssid->mode != WPAS_MODE_P2P_GO)
   8403 			continue;
   8404 		if (wpa_s->ap_iface == NULL)
   8405 			continue;
   8406 		break;
   8407 	}
   8408 	if (wpa_s == NULL) {
   8409 		wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
   8410 		return -1;
   8411 	}
   8412 
   8413 	if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
   8414 	    DEV_PW_NFC_CONNECTION_HANDOVER &&
   8415 	    !wpa_s->p2pdev->p2p_oob_dev_pw) {
   8416 		wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
   8417 		return -1;
   8418 	}
   8419 	res = wpas_ap_wps_add_nfc_pw(
   8420 		wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
   8421 		wpa_s->p2pdev->p2p_oob_dev_pw,
   8422 		wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
   8423 		wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
   8424 	if (res)
   8425 		return res;
   8426 
   8427 	if (!tag) {
   8428 		wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
   8429 		return 0;
   8430 	}
   8431 
   8432 	if (!params->peer ||
   8433 	    !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
   8434 		return 0;
   8435 
   8436 	wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
   8437 		   " to join", MAC2STR(params->peer->p2p_device_addr));
   8438 
   8439 	wpa_s->global->p2p_invite_group = wpa_s;
   8440 	persistent = ssid->p2p_persistent_group &&
   8441 		wpas_p2p_get_persistent(wpa_s->p2pdev,
   8442 					params->peer->p2p_device_addr,
   8443 					ssid->ssid, ssid->ssid_len);
   8444 	wpa_s->p2pdev->pending_invite_ssid_id = -1;
   8445 
   8446 	return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
   8447 			  P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
   8448 			  ssid->ssid, ssid->ssid_len, ssid->frequency,
   8449 			  wpa_s->global->p2p_dev_addr, persistent, 0,
   8450 			  wpa_s->p2pdev->p2p_oob_dev_pw_id);
   8451 }
   8452 
   8453 
   8454 static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
   8455 				    struct p2p_nfc_params *params,
   8456 				    int forced_freq)
   8457 {
   8458 	wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
   8459 		   "connection handover");
   8460 	return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
   8461 				WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
   8462 				forced_freq, wpa_s->p2p_go_vht_center_freq2,
   8463 				-1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
   8464 				NULL, 0);
   8465 }
   8466 
   8467 
   8468 static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
   8469 				    struct p2p_nfc_params *params,
   8470 				    int forced_freq)
   8471 {
   8472 	int res;
   8473 
   8474 	wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
   8475 		   "connection handover");
   8476 	res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
   8477 			       WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
   8478 			       forced_freq, wpa_s->p2p_go_vht_center_freq2,
   8479 			       -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
   8480 			       NULL, 0);
   8481 	if (res)
   8482 		return res;
   8483 
   8484 	res = wpas_p2p_listen(wpa_s, 60);
   8485 	if (res) {
   8486 		p2p_unauthorize(wpa_s->global->p2p,
   8487 				params->peer->p2p_device_addr);
   8488 	}
   8489 
   8490 	return res;
   8491 }
   8492 
   8493 
   8494 static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
   8495 					    const struct wpabuf *data,
   8496 					    int sel, int tag, int forced_freq)
   8497 {
   8498 	const u8 *pos, *end;
   8499 	u16 len, id;
   8500 	struct p2p_nfc_params params;
   8501 	int res;
   8502 
   8503 	os_memset(&params, 0, sizeof(params));
   8504 	params.sel = sel;
   8505 
   8506 	wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
   8507 
   8508 	pos = wpabuf_head(data);
   8509 	end = pos + wpabuf_len(data);
   8510 
   8511 	if (end - pos < 2) {
   8512 		wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
   8513 			   "attributes");
   8514 		return -1;
   8515 	}
   8516 	len = WPA_GET_BE16(pos);
   8517 	pos += 2;
   8518 	if (len > end - pos) {
   8519 		wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
   8520 			   "attributes");
   8521 		return -1;
   8522 	}
   8523 	params.wsc_attr = pos;
   8524 	params.wsc_len = len;
   8525 	pos += len;
   8526 
   8527 	if (end - pos < 2) {
   8528 		wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
   8529 			   "attributes");
   8530 		return -1;
   8531 	}
   8532 	len = WPA_GET_BE16(pos);
   8533 	pos += 2;
   8534 	if (len > end - pos) {
   8535 		wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
   8536 			   "attributes");
   8537 		return -1;
   8538 	}
   8539 	params.p2p_attr = pos;
   8540 	params.p2p_len = len;
   8541 	pos += len;
   8542 
   8543 	wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
   8544 		    params.wsc_attr, params.wsc_len);
   8545 	wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
   8546 		    params.p2p_attr, params.p2p_len);
   8547 	if (pos < end) {
   8548 		wpa_hexdump(MSG_DEBUG,
   8549 			    "P2P: Ignored extra data after P2P attributes",
   8550 			    pos, end - pos);
   8551 	}
   8552 
   8553 	res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
   8554 	if (res)
   8555 		return res;
   8556 
   8557 	if (params.next_step == NO_ACTION)
   8558 		return 0;
   8559 
   8560 	if (params.next_step == BOTH_GO) {
   8561 		wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
   8562 			MAC2STR(params.peer->p2p_device_addr));
   8563 		return 0;
   8564 	}
   8565 
   8566 	if (params.next_step == PEER_CLIENT) {
   8567 		if (!is_zero_ether_addr(params.go_dev_addr)) {
   8568 			wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
   8569 				"peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
   8570 				" ssid=\"%s\"",
   8571 				MAC2STR(params.peer->p2p_device_addr),
   8572 				params.go_freq,
   8573 				MAC2STR(params.go_dev_addr),
   8574 				wpa_ssid_txt(params.go_ssid,
   8575 					     params.go_ssid_len));
   8576 		} else {
   8577 			wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
   8578 				"peer=" MACSTR " freq=%d",
   8579 				MAC2STR(params.peer->p2p_device_addr),
   8580 				params.go_freq);
   8581 		}
   8582 		return 0;
   8583 	}
   8584 
   8585 	if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
   8586 		wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
   8587 			MACSTR, MAC2STR(params.peer->p2p_device_addr));
   8588 		return 0;
   8589 	}
   8590 
   8591 	wpabuf_free(wpa_s->p2p_oob_dev_pw);
   8592 	wpa_s->p2p_oob_dev_pw = NULL;
   8593 
   8594 	if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
   8595 		wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
   8596 			   "received");
   8597 		return -1;
   8598 	}
   8599 
   8600 	id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
   8601 	wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
   8602 	wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
   8603 		    params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
   8604 	os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
   8605 		  params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
   8606 	wpa_s->p2p_peer_oob_pk_hash_known = 1;
   8607 
   8608 	if (tag) {
   8609 		if (id < 0x10) {
   8610 			wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
   8611 				   "peer OOB Device Password Id %u", id);
   8612 			return -1;
   8613 		}
   8614 		wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
   8615 			   "Device Password Id %u", id);
   8616 		wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
   8617 				params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
   8618 				params.oob_dev_pw_len -
   8619 				WPS_OOB_PUBKEY_HASH_LEN - 2);
   8620 		wpa_s->p2p_oob_dev_pw_id = id;
   8621 		wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
   8622 			params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
   8623 			params.oob_dev_pw_len -
   8624 			WPS_OOB_PUBKEY_HASH_LEN - 2);
   8625 		if (wpa_s->p2p_oob_dev_pw == NULL)
   8626 			return -1;
   8627 
   8628 		if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
   8629 		    wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
   8630 				   &wpa_s->conf->wps_nfc_dh_privkey) < 0)
   8631 			return -1;
   8632 	} else {
   8633 		wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
   8634 			   "without Device Password");
   8635 		wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
   8636 	}
   8637 
   8638 	switch (params.next_step) {
   8639 	case NO_ACTION:
   8640 	case BOTH_GO:
   8641 	case PEER_CLIENT:
   8642 		/* already covered above */
   8643 		return 0;
   8644 	case JOIN_GROUP:
   8645 		return wpas_p2p_nfc_join_group(wpa_s, &params);
   8646 	case AUTH_JOIN:
   8647 		return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
   8648 	case INIT_GO_NEG:
   8649 		return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
   8650 	case RESP_GO_NEG:
   8651 		/* TODO: use own OOB Dev Pw */
   8652 		return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
   8653 	}
   8654 
   8655 	return -1;
   8656 }
   8657 
   8658 
   8659 int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
   8660 			     const struct wpabuf *data, int forced_freq)
   8661 {
   8662 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
   8663 		return -1;
   8664 
   8665 	return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
   8666 }
   8667 
   8668 
   8669 int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
   8670 				 const struct wpabuf *req,
   8671 				 const struct wpabuf *sel, int forced_freq)
   8672 {
   8673 	struct wpabuf *tmp;
   8674 	int ret;
   8675 
   8676 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
   8677 		return -1;
   8678 
   8679 	wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
   8680 
   8681 	wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
   8682 			  wpabuf_head(req), wpabuf_len(req));
   8683 	wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
   8684 			  wpabuf_head(sel), wpabuf_len(sel));
   8685 	if (forced_freq)
   8686 		wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
   8687 	tmp = ndef_parse_p2p(init ? sel : req);
   8688 	if (tmp == NULL) {
   8689 		wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
   8690 		return -1;
   8691 	}
   8692 
   8693 	ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
   8694 					       forced_freq);
   8695 	wpabuf_free(tmp);
   8696 
   8697 	return ret;
   8698 }
   8699 
   8700 
   8701 int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
   8702 {
   8703 	const u8 *if_addr;
   8704 	int go_intent = wpa_s->conf->p2p_go_intent;
   8705 	struct wpa_supplicant *iface;
   8706 
   8707 	if (wpa_s->global->p2p == NULL)
   8708 		return -1;
   8709 
   8710 	if (!enabled) {
   8711 		wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
   8712 		for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
   8713 		{
   8714 			if (!iface->ap_iface)
   8715 				continue;
   8716 			hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
   8717 		}
   8718 		p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
   8719 						 0, NULL);
   8720 		if (wpa_s->p2p_nfc_tag_enabled)
   8721 			wpas_p2p_remove_pending_group_interface(wpa_s);
   8722 		wpa_s->p2p_nfc_tag_enabled = 0;
   8723 		return 0;
   8724 	}
   8725 
   8726 	if (wpa_s->global->p2p_disabled)
   8727 		return -1;
   8728 
   8729 	if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
   8730 	    wpa_s->conf->wps_nfc_dh_privkey == NULL ||
   8731 	    wpa_s->conf->wps_nfc_dev_pw == NULL ||
   8732 	    wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
   8733 		wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
   8734 			   "to allow static handover cases");
   8735 		return -1;
   8736 	}
   8737 
   8738 	wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
   8739 
   8740 	wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
   8741 	wpabuf_free(wpa_s->p2p_oob_dev_pw);
   8742 	wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
   8743 	if (wpa_s->p2p_oob_dev_pw == NULL)
   8744 		return -1;
   8745 	wpa_s->p2p_peer_oob_pk_hash_known = 0;
   8746 
   8747 	if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
   8748 	    wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
   8749 		/*
   8750 		 * P2P Group Interface present and the command came on group
   8751 		 * interface, so enable the token for the current interface.
   8752 		 */
   8753 		wpa_s->create_p2p_iface = 0;
   8754 	} else {
   8755 		wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
   8756 	}
   8757 
   8758 	if (wpa_s->create_p2p_iface) {
   8759 		enum wpa_driver_if_type iftype;
   8760 		/* Prepare to add a new interface for the group */
   8761 		iftype = WPA_IF_P2P_GROUP;
   8762 		if (go_intent == 15)
   8763 			iftype = WPA_IF_P2P_GO;
   8764 		if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
   8765 			wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
   8766 				   "interface for the group");
   8767 			return -1;
   8768 		}
   8769 
   8770 		if_addr = wpa_s->pending_interface_addr;
   8771 	} else if (wpa_s->p2p_mgmt)
   8772 		if_addr = wpa_s->parent->own_addr;
   8773 	else
   8774 		if_addr = wpa_s->own_addr;
   8775 
   8776 	wpa_s->p2p_nfc_tag_enabled = enabled;
   8777 
   8778 	for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
   8779 		struct hostapd_data *hapd;
   8780 		if (iface->ap_iface == NULL)
   8781 			continue;
   8782 		hapd = iface->ap_iface->bss[0];
   8783 		wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
   8784 		hapd->conf->wps_nfc_dh_pubkey =
   8785 			wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
   8786 		wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
   8787 		hapd->conf->wps_nfc_dh_privkey =
   8788 			wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
   8789 		wpabuf_free(hapd->conf->wps_nfc_dev_pw);
   8790 		hapd->conf->wps_nfc_dev_pw =
   8791 			wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
   8792 		hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
   8793 
   8794 		if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
   8795 			wpa_dbg(iface, MSG_DEBUG,
   8796 				"P2P: Failed to enable NFC Tag for GO");
   8797 		}
   8798 	}
   8799 	p2p_set_authorized_oob_dev_pw_id(
   8800 		wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
   8801 		if_addr);
   8802 
   8803 	return 0;
   8804 }
   8805 
   8806 #endif /* CONFIG_WPS_NFC */
   8807 
   8808 
   8809 static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
   8810 					     struct wpa_used_freq_data *freqs,
   8811 					     unsigned int num)
   8812 {
   8813 	u8 curr_chan, cand, chan;
   8814 	unsigned int i;
   8815 
   8816 	/*
   8817 	 * If possible, optimize the Listen channel to be a channel that is
   8818 	 * already used by one of the other interfaces.
   8819 	 */
   8820 	if (!wpa_s->conf->p2p_optimize_listen_chan)
   8821 		return;
   8822 
   8823 	if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
   8824 		return;
   8825 
   8826 	curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
   8827 	for (i = 0, cand = 0; i < num; i++) {
   8828 		ieee80211_freq_to_chan(freqs[i].freq, &chan);
   8829 		if (curr_chan == chan) {
   8830 			cand = 0;
   8831 			break;
   8832 		}
   8833 
   8834 		if (chan == 1 || chan == 6 || chan == 11)
   8835 			cand = chan;
   8836 	}
   8837 
   8838 	if (cand) {
   8839 		wpa_dbg(wpa_s, MSG_DEBUG,
   8840 			"P2P: Update Listen channel to %u based on operating channel",
   8841 			cand);
   8842 		p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
   8843 	}
   8844 }
   8845 
   8846 
   8847 static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s)
   8848 {
   8849 	struct hostapd_config *conf;
   8850 	struct p2p_go_neg_results params;
   8851 	struct csa_settings csa_settings;
   8852 	struct wpa_ssid *current_ssid = wpa_s->current_ssid;
   8853 	int old_freq = current_ssid->frequency;
   8854 	int ret;
   8855 
   8856 	if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
   8857 		wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled");
   8858 		return -1;
   8859 	}
   8860 
   8861 	/*
   8862 	 * TODO: This function may not always work correctly. For example,
   8863 	 * when we have a running GO and a BSS on a DFS channel.
   8864 	 */
   8865 	if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, NULL)) {
   8866 		wpa_dbg(wpa_s, MSG_DEBUG,
   8867 			"P2P CSA: Failed to select new frequency for GO");
   8868 		return -1;
   8869 	}
   8870 
   8871 	if (current_ssid->frequency == params.freq) {
   8872 		wpa_dbg(wpa_s, MSG_DEBUG,
   8873 			"P2P CSA: Selected same frequency - not moving GO");
   8874 		return 0;
   8875 	}
   8876 
   8877 	conf = hostapd_config_defaults();
   8878 	if (!conf) {
   8879 		wpa_dbg(wpa_s, MSG_DEBUG,
   8880 			"P2P CSA: Failed to allocate default config");
   8881 		return -1;
   8882 	}
   8883 
   8884 	current_ssid->frequency = params.freq;
   8885 	if (wpa_supplicant_conf_ap_ht(wpa_s, current_ssid, conf)) {
   8886 		wpa_dbg(wpa_s, MSG_DEBUG,
   8887 			"P2P CSA: Failed to create new GO config");
   8888 		ret = -1;
   8889 		goto out;
   8890 	}
   8891 
   8892 	if (conf->hw_mode != wpa_s->ap_iface->current_mode->mode) {
   8893 		wpa_dbg(wpa_s, MSG_DEBUG,
   8894 			"P2P CSA: CSA to a different band is not supported");
   8895 		ret = -1;
   8896 		goto out;
   8897 	}
   8898 
   8899 	os_memset(&csa_settings, 0, sizeof(csa_settings));
   8900 	csa_settings.cs_count = P2P_GO_CSA_COUNT;
   8901 	csa_settings.block_tx = P2P_GO_CSA_BLOCK_TX;
   8902 	csa_settings.freq_params.freq = params.freq;
   8903 	csa_settings.freq_params.sec_channel_offset = conf->secondary_channel;
   8904 	csa_settings.freq_params.ht_enabled = conf->ieee80211n;
   8905 	csa_settings.freq_params.bandwidth = conf->secondary_channel ? 40 : 20;
   8906 
   8907 	if (conf->ieee80211ac) {
   8908 		int freq1 = 0, freq2 = 0;
   8909 		u8 chan, opclass;
   8910 
   8911 		if (ieee80211_freq_to_channel_ext(params.freq,
   8912 						  conf->secondary_channel,
   8913 						  conf->vht_oper_chwidth,
   8914 						  &opclass, &chan) ==
   8915 		    NUM_HOSTAPD_MODES) {
   8916 			wpa_printf(MSG_ERROR, "P2P CSA: Bad freq");
   8917 			ret = -1;
   8918 			goto out;
   8919 		}
   8920 
   8921 		if (conf->vht_oper_centr_freq_seg0_idx)
   8922 			freq1 = ieee80211_chan_to_freq(
   8923 				NULL, opclass,
   8924 				conf->vht_oper_centr_freq_seg0_idx);
   8925 
   8926 		if (conf->vht_oper_centr_freq_seg1_idx)
   8927 			freq2 = ieee80211_chan_to_freq(
   8928 				NULL, opclass,
   8929 				conf->vht_oper_centr_freq_seg1_idx);
   8930 
   8931 		if (freq1 < 0 || freq2 < 0) {
   8932 			wpa_dbg(wpa_s, MSG_DEBUG,
   8933 				"P2P CSA: Selected invalid VHT center freqs");
   8934 			ret = -1;
   8935 			goto out;
   8936 		}
   8937 
   8938 		csa_settings.freq_params.vht_enabled = conf->ieee80211ac;
   8939 		csa_settings.freq_params.center_freq1 = freq1;
   8940 		csa_settings.freq_params.center_freq2 = freq2;
   8941 
   8942 		switch (conf->vht_oper_chwidth) {
   8943 		case VHT_CHANWIDTH_80MHZ:
   8944 		case VHT_CHANWIDTH_80P80MHZ:
   8945 			csa_settings.freq_params.bandwidth = 80;
   8946 			break;
   8947 		case VHT_CHANWIDTH_160MHZ:
   8948 			csa_settings.freq_params.bandwidth = 160;
   8949 			break;
   8950 		}
   8951 	}
   8952 
   8953 	ret = ap_switch_channel(wpa_s, &csa_settings);
   8954 out:
   8955 	current_ssid->frequency = old_freq;
   8956 	hostapd_config_free(conf);
   8957 	return ret;
   8958 }
   8959 
   8960 
   8961 static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s)
   8962 {
   8963 	struct p2p_go_neg_results params;
   8964 	struct wpa_ssid *current_ssid = wpa_s->current_ssid;
   8965 
   8966 	wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
   8967 
   8968 	wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz",
   8969 		current_ssid->frequency);
   8970 
   8971 	/* Stop the AP functionality */
   8972 	/* TODO: Should do this in a way that does not indicated to possible
   8973 	 * P2P Clients in the group that the group is terminated. */
   8974 	wpa_supplicant_ap_deinit(wpa_s);
   8975 
   8976 	/* Reselect the GO frequency */
   8977 	if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, NULL)) {
   8978 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq");
   8979 		wpas_p2p_group_delete(wpa_s,
   8980 				      P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
   8981 		return;
   8982 	}
   8983 	wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)",
   8984 		params.freq);
   8985 
   8986 	if (params.freq &&
   8987 	    !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
   8988 		wpa_printf(MSG_DEBUG,
   8989 			   "P2P: Selected freq (%u MHz) is not valid for P2P",
   8990 			   params.freq);
   8991 		wpas_p2p_group_delete(wpa_s,
   8992 				      P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
   8993 		return;
   8994 	}
   8995 
   8996 	/* Update the frequency */
   8997 	current_ssid->frequency = params.freq;
   8998 	wpa_s->connect_without_scan = current_ssid;
   8999 	wpa_s->reassociate = 1;
   9000 	wpa_s->disconnected = 0;
   9001 	wpa_supplicant_req_scan(wpa_s, 0, 0);
   9002 }
   9003 
   9004 
   9005 static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx)
   9006 {
   9007 	struct wpa_supplicant *wpa_s = eloop_ctx;
   9008 
   9009 	if (!wpa_s->ap_iface || !wpa_s->current_ssid)
   9010 		return;
   9011 
   9012 	wpas_p2p_go_update_common_freqs(wpa_s);
   9013 
   9014 	/* Do not move GO in the middle of a CSA */
   9015 	if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
   9016 		wpa_printf(MSG_DEBUG,
   9017 			   "P2P: CSA is in progress - not moving GO");
   9018 		return;
   9019 	}
   9020 
   9021 	/*
   9022 	 * First, try a channel switch flow. If it is not supported or fails,
   9023 	 * take down the GO and bring it up again.
   9024 	 */
   9025 	if (wpas_p2p_move_go_csa(wpa_s) < 0)
   9026 		wpas_p2p_move_go_no_csa(wpa_s);
   9027 }
   9028 
   9029 
   9030 static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx)
   9031 {
   9032 	struct wpa_supplicant *wpa_s = eloop_ctx;
   9033 	struct wpa_used_freq_data *freqs = NULL;
   9034 	unsigned int num = wpa_s->num_multichan_concurrent;
   9035 
   9036 	freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
   9037 	if (!freqs)
   9038 		return;
   9039 
   9040 	num = get_shared_radio_freqs_data(wpa_s, freqs, num);
   9041 
   9042 	/* Previous attempt to move a GO was not possible -- try again. */
   9043 	wpas_p2p_consider_moving_gos(wpa_s, freqs, num,
   9044 				     WPAS_P2P_CHANNEL_UPDATE_ANY);
   9045 
   9046 	os_free(freqs);
   9047 }
   9048 
   9049 
   9050 /*
   9051  * Consider moving a GO from its currently used frequency:
   9052  * 1. It is possible that due to regulatory consideration the frequency
   9053  *    can no longer be used and there is a need to evacuate the GO.
   9054  * 2. It is possible that due to MCC considerations, it would be preferable
   9055  *    to move the GO to a channel that is currently used by some other
   9056  *    station interface.
   9057  *
   9058  * In case a frequency that became invalid is once again valid, cancel a
   9059  * previously initiated GO frequency change.
   9060  */
   9061 static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s,
   9062 					    struct wpa_used_freq_data *freqs,
   9063 					    unsigned int num)
   9064 {
   9065 	unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0;
   9066 	unsigned int timeout;
   9067 	int freq;
   9068 
   9069 	wpas_p2p_go_update_common_freqs(wpa_s);
   9070 
   9071 	freq = wpa_s->current_ssid->frequency;
   9072 	for (i = 0, invalid_freq = 0; i < num; i++) {
   9073 		if (freqs[i].freq == freq) {
   9074 			flags = freqs[i].flags;
   9075 
   9076 			/* The channel is invalid, must change it */
   9077 			if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
   9078 				wpa_dbg(wpa_s, MSG_DEBUG,
   9079 					"P2P: Freq=%d MHz no longer valid for GO",
   9080 					freq);
   9081 				invalid_freq = 1;
   9082 			}
   9083 		} else if (freqs[i].flags == 0) {
   9084 			/* Freq is not used by any other station interface */
   9085 			continue;
   9086 		} else if (!p2p_supported_freq(wpa_s->global->p2p,
   9087 					       freqs[i].freq)) {
   9088 			/* Freq is not valid for P2P use cases */
   9089 			continue;
   9090 		} else if (wpa_s->conf->p2p_go_freq_change_policy ==
   9091 			   P2P_GO_FREQ_MOVE_SCM) {
   9092 			policy_move = 1;
   9093 		} else if (wpa_s->conf->p2p_go_freq_change_policy ==
   9094 			   P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS &&
   9095 			   wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
   9096 			policy_move = 1;
   9097 		} else if ((wpa_s->conf->p2p_go_freq_change_policy ==
   9098 			    P2P_GO_FREQ_MOVE_SCM_ECSA) &&
   9099 			   wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
   9100 			if (!p2p_get_group_num_members(wpa_s->p2p_group)) {
   9101 				policy_move = 1;
   9102 			} else if ((wpa_s->drv_flags &
   9103 				    WPA_DRIVER_FLAGS_AP_CSA) &&
   9104 				   wpas_p2p_go_clients_support_ecsa(wpa_s)) {
   9105 				u8 chan;
   9106 
   9107 				/*
   9108 				 * We do not support CSA between bands, so move
   9109 				 * GO only within the same band.
   9110 				 */
   9111 				if (wpa_s->ap_iface->current_mode->mode ==
   9112 				    ieee80211_freq_to_chan(freqs[i].freq,
   9113 							   &chan))
   9114 					policy_move = 1;
   9115 			}
   9116 		}
   9117 	}
   9118 
   9119 	wpa_dbg(wpa_s, MSG_DEBUG,
   9120 		"P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X",
   9121 		invalid_freq, policy_move, flags);
   9122 
   9123 	/*
   9124 	 * The channel is valid, or we are going to have a policy move, so
   9125 	 * cancel timeout.
   9126 	 */
   9127 	if (!invalid_freq || policy_move) {
   9128 		wpa_dbg(wpa_s, MSG_DEBUG,
   9129 			"P2P: Cancel a GO move from freq=%d MHz", freq);
   9130 		eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
   9131 
   9132 		if (wpas_p2p_in_progress(wpa_s)) {
   9133 			wpa_dbg(wpa_s, MSG_DEBUG,
   9134 				"P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move");
   9135 			eloop_cancel_timeout(wpas_p2p_reconsider_moving_go,
   9136 					     wpa_s, NULL);
   9137 			eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
   9138 					       wpas_p2p_reconsider_moving_go,
   9139 					       wpa_s, NULL);
   9140 			return;
   9141 		}
   9142 	}
   9143 
   9144 	if (!invalid_freq && (!policy_move || flags != 0)) {
   9145 		wpa_dbg(wpa_s, MSG_DEBUG,
   9146 			"P2P: Not initiating a GO frequency change");
   9147 		return;
   9148 	}
   9149 
   9150 	/*
   9151 	 * Do not consider moving GO if it is in the middle of a CSA. When the
   9152 	 * CSA is finished this flow should be retriggered.
   9153 	 */
   9154 	if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
   9155 		wpa_dbg(wpa_s, MSG_DEBUG,
   9156 			"P2P: Not initiating a GO frequency change - CSA is in progress");
   9157 		return;
   9158 	}
   9159 
   9160 	if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq))
   9161 		timeout = P2P_GO_FREQ_CHANGE_TIME;
   9162 	else
   9163 		timeout = 0;
   9164 
   9165 	wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs",
   9166 		freq, timeout);
   9167 	eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
   9168 	eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL);
   9169 }
   9170 
   9171 
   9172 static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
   9173 					 struct wpa_used_freq_data *freqs,
   9174 					 unsigned int num,
   9175 					 enum wpas_p2p_channel_update_trig trig)
   9176 {
   9177 	struct wpa_supplicant *ifs;
   9178 
   9179 	eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX,
   9180 			     NULL);
   9181 
   9182 	/*
   9183 	 * Travers all the radio interfaces, and for each GO interface, check
   9184 	 * if there is a need to move the GO from the frequency it is using,
   9185 	 * or in case the frequency is valid again, cancel the evacuation flow.
   9186 	 */
   9187 	dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
   9188 			 radio_list) {
   9189 		if (ifs->current_ssid == NULL ||
   9190 		    ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
   9191 			continue;
   9192 
   9193 		/*
   9194 		 * The GO was just started or completed channel switch, no need
   9195 		 * to move it.
   9196 		 */
   9197 		if (wpa_s == ifs &&
   9198 		    (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE ||
   9199 		     trig == WPAS_P2P_CHANNEL_UPDATE_CS)) {
   9200 			wpa_dbg(wpa_s, MSG_DEBUG,
   9201 				"P2P: GO move - schedule re-consideration");
   9202 			eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
   9203 					       wpas_p2p_reconsider_moving_go,
   9204 					       wpa_s, NULL);
   9205 			continue;
   9206 		}
   9207 
   9208 		wpas_p2p_consider_moving_one_go(ifs, freqs, num);
   9209 	}
   9210 }
   9211 
   9212 
   9213 void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
   9214 {
   9215 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
   9216 		return;
   9217 
   9218 	wpas_p2p_update_channel_list(wpa_s,
   9219 				     WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE);
   9220 }
   9221 
   9222 
   9223 void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
   9224 {
   9225 	if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
   9226 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
   9227 			"the management interface is being removed");
   9228 		wpas_p2p_deinit_global(wpa_s->global);
   9229 	}
   9230 }
   9231 
   9232 
   9233 void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
   9234 {
   9235 	if (wpa_s->ap_iface->bss)
   9236 		wpa_s->ap_iface->bss[0]->p2p_group = NULL;
   9237 	wpas_p2p_group_deinit(wpa_s);
   9238 }
   9239 
   9240 
   9241 int wpas_p2p_lo_start(struct wpa_supplicant *wpa_s, unsigned int freq,
   9242 		      unsigned int period, unsigned int interval,
   9243 		      unsigned int count)
   9244 {
   9245 	struct p2p_data *p2p = wpa_s->global->p2p;
   9246 	u8 *device_types;
   9247 	size_t dev_types_len;
   9248 	struct wpabuf *buf;
   9249 	int ret;
   9250 
   9251 	if (wpa_s->p2p_lo_started) {
   9252 		wpa_dbg(wpa_s, MSG_DEBUG,
   9253 			"P2P Listen offload is already started");
   9254 		return 0;
   9255 	}
   9256 
   9257 	if (wpa_s->global->p2p == NULL ||
   9258 	    !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD)) {
   9259 		wpa_printf(MSG_DEBUG, "P2P: Listen offload not supported");
   9260 		return -1;
   9261 	}
   9262 
   9263 	if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
   9264 		wpa_printf(MSG_ERROR, "P2P: Input channel not supported: %u",
   9265 			   freq);
   9266 		return -1;
   9267 	}
   9268 
   9269 	/* Get device type */
   9270 	dev_types_len = (wpa_s->conf->num_sec_device_types + 1) *
   9271 		WPS_DEV_TYPE_LEN;
   9272 	device_types = os_malloc(dev_types_len);
   9273 	if (!device_types)
   9274 		return -1;
   9275 	os_memcpy(device_types, wpa_s->conf->device_type, WPS_DEV_TYPE_LEN);
   9276 	os_memcpy(&device_types[WPS_DEV_TYPE_LEN], wpa_s->conf->sec_device_type,
   9277 		  wpa_s->conf->num_sec_device_types * WPS_DEV_TYPE_LEN);
   9278 
   9279 	/* Get Probe Response IE(s) */
   9280 	buf = p2p_build_probe_resp_template(p2p, freq);
   9281 	if (!buf) {
   9282 		os_free(device_types);
   9283 		return -1;
   9284 	}
   9285 
   9286 	ret = wpa_drv_p2p_lo_start(wpa_s, freq, period, interval, count,
   9287 				   device_types, dev_types_len,
   9288 				   wpabuf_mhead_u8(buf), wpabuf_len(buf));
   9289 	if (ret < 0)
   9290 		wpa_dbg(wpa_s, MSG_DEBUG,
   9291 			"P2P: Failed to start P2P listen offload");
   9292 
   9293 	os_free(device_types);
   9294 	wpabuf_free(buf);
   9295 
   9296 	if (ret == 0) {
   9297 		wpa_s->p2p_lo_started = 1;
   9298 
   9299 		/* Stop current P2P listen if any */
   9300 		wpas_stop_listen(wpa_s);
   9301 	}
   9302 
   9303 	return ret;
   9304 }
   9305 
   9306 
   9307 int wpas_p2p_lo_stop(struct wpa_supplicant *wpa_s)
   9308 {
   9309 	int ret;
   9310 
   9311 	if (!wpa_s->p2p_lo_started)
   9312 		return 0;
   9313 
   9314 	ret = wpa_drv_p2p_lo_stop(wpa_s);
   9315 	if (ret < 0)
   9316 		wpa_dbg(wpa_s, MSG_DEBUG,
   9317 			"P2P: Failed to stop P2P listen offload");
   9318 
   9319 	wpa_s->p2p_lo_started = 0;
   9320 	return ret;
   9321 }
   9322