Home | History | Annotate | Line # | Download | only in p2p
      1 /*
      2  * Wi-Fi Direct - P2P Invitation procedure
      3  * Copyright (c) 2010, Atheros Communications
      4  *
      5  * This software may be distributed under the terms of the BSD license.
      6  * See README for more details.
      7  */
      8 
      9 #include "includes.h"
     10 
     11 #include "common.h"
     12 #include "common/ieee802_11_defs.h"
     13 #include "common/wpa_ctrl.h"
     14 #include "p2p_i.h"
     15 #include "p2p.h"
     16 
     17 
     18 static struct wpabuf * p2p_build_invitation_req(struct p2p_data *p2p,
     19 						struct p2p_device *peer,
     20 						const u8 *go_dev_addr,
     21 						int dev_pw_id)
     22 {
     23 	struct wpabuf *buf;
     24 	u8 *len;
     25 	const u8 *dev_addr;
     26 	size_t extra = 0;
     27 	bool is_6ghz_capab;
     28 
     29 #ifdef CONFIG_WIFI_DISPLAY
     30 	struct wpabuf *wfd_ie = p2p->wfd_ie_invitation;
     31 	if (wfd_ie && p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) {
     32 		size_t i;
     33 		for (i = 0; i < p2p->num_groups; i++) {
     34 			struct p2p_group *g = p2p->groups[i];
     35 			struct wpabuf *ie;
     36 			if (!ether_addr_equal(p2p_group_get_interface_addr(g),
     37 					      p2p->inv_bssid))
     38 				continue;
     39 			ie = p2p_group_get_wfd_ie(g);
     40 			if (ie) {
     41 				wfd_ie = ie;
     42 				break;
     43 			}
     44 		}
     45 	}
     46 	if (wfd_ie)
     47 		extra = wpabuf_len(wfd_ie);
     48 #endif /* CONFIG_WIFI_DISPLAY */
     49 
     50 	if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_INV_REQ])
     51 		extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_INV_REQ]);
     52 
     53 	buf = wpabuf_alloc(1000 + extra);
     54 	if (buf == NULL)
     55 		return NULL;
     56 
     57 	peer->dialog_token++;
     58 	if (peer->dialog_token == 0)
     59 		peer->dialog_token = 1;
     60 	p2p_buf_add_public_action_hdr(buf, P2P_INVITATION_REQ,
     61 				      peer->dialog_token);
     62 
     63 	len = p2p_buf_add_ie_hdr(buf);
     64 	if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO || !p2p->inv_persistent)
     65 		p2p_buf_add_config_timeout(buf, 0, 0);
     66 	else
     67 		p2p_buf_add_config_timeout(buf, p2p->go_timeout,
     68 					   p2p->client_timeout);
     69 	p2p_buf_add_invitation_flags(buf, p2p->inv_persistent ?
     70 				     P2P_INVITATION_FLAGS_TYPE : 0);
     71 	if (p2p->inv_role != P2P_INVITE_ROLE_CLIENT ||
     72 	    !(peer->flags & P2P_DEV_NO_PREF_CHAN))
     73 		p2p_buf_add_operating_channel(buf, p2p->cfg->country,
     74 					      p2p->op_reg_class,
     75 					      p2p->op_channel);
     76 	if (p2p->inv_bssid_set)
     77 		p2p_buf_add_group_bssid(buf, p2p->inv_bssid);
     78 	is_6ghz_capab = is_p2p_6ghz_capable(p2p) &&
     79 		p2p_is_peer_6ghz_capab(p2p, peer->info.p2p_device_addr);
     80 	p2p_buf_add_channel_list(buf, p2p->cfg->country, &p2p->channels,
     81 				 is_6ghz_capab);
     82 	if (go_dev_addr)
     83 		dev_addr = go_dev_addr;
     84 	else if (p2p->inv_role == P2P_INVITE_ROLE_CLIENT)
     85 		dev_addr = peer->info.p2p_device_addr;
     86 	else
     87 		dev_addr = p2p->cfg->dev_addr;
     88 	p2p_buf_add_group_id(buf, dev_addr, p2p->inv_ssid, p2p->inv_ssid_len);
     89 	p2p_buf_add_device_info(buf, p2p, peer);
     90 	p2p_buf_update_ie_hdr(buf, len);
     91 
     92 	p2p_buf_add_pref_channel_list(buf, p2p->pref_freq_list,
     93 				      p2p->num_pref_freq);
     94 
     95 #ifdef CONFIG_WIFI_DISPLAY
     96 	if (wfd_ie)
     97 		wpabuf_put_buf(buf, wfd_ie);
     98 #endif /* CONFIG_WIFI_DISPLAY */
     99 
    100 	if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_INV_REQ])
    101 		wpabuf_put_buf(buf, p2p->vendor_elem[VENDOR_ELEM_P2P_INV_REQ]);
    102 
    103 	if (dev_pw_id >= 0) {
    104 		/* WSC IE in Invitation Request for NFC static handover */
    105 		p2p_build_wps_ie(p2p, buf, dev_pw_id, 0);
    106 	}
    107 
    108 	return buf;
    109 }
    110 
    111 
    112 static struct wpabuf * p2p_build_invitation_resp(struct p2p_data *p2p,
    113 						 struct p2p_device *peer,
    114 						 u8 dialog_token, u8 status,
    115 						 const u8 *group_bssid,
    116 						 u8 reg_class, u8 channel,
    117 						 struct p2p_channels *channels)
    118 {
    119 	struct wpabuf *buf;
    120 	u8 *len;
    121 	size_t extra = 0;
    122 
    123 #ifdef CONFIG_WIFI_DISPLAY
    124 	struct wpabuf *wfd_ie = p2p->wfd_ie_invitation;
    125 	if (wfd_ie && group_bssid) {
    126 		size_t i;
    127 		for (i = 0; i < p2p->num_groups; i++) {
    128 			struct p2p_group *g = p2p->groups[i];
    129 			struct wpabuf *ie;
    130 			if (!ether_addr_equal(p2p_group_get_interface_addr(g),
    131 					      group_bssid))
    132 				continue;
    133 			ie = p2p_group_get_wfd_ie(g);
    134 			if (ie) {
    135 				wfd_ie = ie;
    136 				break;
    137 			}
    138 		}
    139 	}
    140 	if (wfd_ie)
    141 		extra = wpabuf_len(wfd_ie);
    142 #endif /* CONFIG_WIFI_DISPLAY */
    143 
    144 	if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_INV_RESP])
    145 		extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_INV_RESP]);
    146 
    147 	buf = wpabuf_alloc(1000 + extra);
    148 	if (buf == NULL)
    149 		return NULL;
    150 
    151 	p2p_buf_add_public_action_hdr(buf, P2P_INVITATION_RESP,
    152 				      dialog_token);
    153 
    154 	len = p2p_buf_add_ie_hdr(buf);
    155 	p2p_buf_add_status(buf, status);
    156 	p2p_buf_add_config_timeout(buf, 0, 0); /* FIX */
    157 	if (reg_class && channel)
    158 		p2p_buf_add_operating_channel(buf, p2p->cfg->country,
    159 					      reg_class, channel);
    160 	if (group_bssid)
    161 		p2p_buf_add_group_bssid(buf, group_bssid);
    162 	if (channels) {
    163 		bool is_6ghz_capab;
    164 
    165 		is_6ghz_capab = is_p2p_6ghz_capable(p2p) &&
    166 			p2p_is_peer_6ghz_capab(p2p, peer->info.p2p_device_addr);
    167 		p2p_buf_add_channel_list(buf, p2p->cfg->country, channels,
    168 					 is_6ghz_capab);
    169 	}
    170 	p2p_buf_update_ie_hdr(buf, len);
    171 
    172 #ifdef CONFIG_WIFI_DISPLAY
    173 	if (wfd_ie)
    174 		wpabuf_put_buf(buf, wfd_ie);
    175 #endif /* CONFIG_WIFI_DISPLAY */
    176 
    177 	if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_INV_RESP])
    178 		wpabuf_put_buf(buf, p2p->vendor_elem[VENDOR_ELEM_P2P_INV_RESP]);
    179 
    180 	return buf;
    181 }
    182 
    183 
    184 void p2p_process_invitation_req(struct p2p_data *p2p, const u8 *sa,
    185 				const u8 *data, size_t len, int rx_freq)
    186 {
    187 	struct p2p_device *dev;
    188 	struct p2p_message msg;
    189 	struct wpabuf *resp = NULL;
    190 	u8 status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
    191 	int freq;
    192 	int go = 0;
    193 	u8 group_bssid[ETH_ALEN], *bssid;
    194 	int op_freq = 0;
    195 	u8 reg_class = 0, channel = 0;
    196 	struct p2p_channels all_channels, intersection, *channels = NULL;
    197 	int persistent;
    198 
    199 	os_memset(group_bssid, 0, sizeof(group_bssid));
    200 
    201 	p2p_dbg(p2p, "Received Invitation Request from " MACSTR " (freq=%d)",
    202 		MAC2STR(sa), rx_freq);
    203 
    204 	if (p2p_parse(data, len, &msg))
    205 		return;
    206 
    207 	dev = p2p_get_device(p2p, sa);
    208 	if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
    209 		p2p_dbg(p2p, "Invitation Request from unknown peer " MACSTR,
    210 			MAC2STR(sa));
    211 
    212 		if (p2p_add_device(p2p, sa, rx_freq, NULL, 0, data + 1, len - 1,
    213 				   0)) {
    214 			p2p_dbg(p2p, "Invitation Request add device failed "
    215 				MACSTR, MAC2STR(sa));
    216 			status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
    217 			goto fail;
    218 		}
    219 
    220 		dev = p2p_get_device(p2p, sa);
    221 		if (dev == NULL) {
    222 			p2p_dbg(p2p, "Reject Invitation Request from unknown peer "
    223 				MACSTR, MAC2STR(sa));
    224 			status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
    225 			goto fail;
    226 		}
    227 	}
    228 
    229 	if (!msg.group_id || !msg.channel_list) {
    230 		p2p_dbg(p2p, "Mandatory attribute missing in Invitation Request from "
    231 			MACSTR, MAC2STR(sa));
    232 		status = P2P_SC_FAIL_INVALID_PARAMS;
    233 		goto fail;
    234 	}
    235 
    236 	if (msg.invitation_flags)
    237 		persistent = *msg.invitation_flags & P2P_INVITATION_FLAGS_TYPE;
    238 	else {
    239 		/* Invitation Flags is a mandatory attribute starting from P2P
    240 		 * spec 1.06. As a backwards compatibility mechanism, assume
    241 		 * the request was for a persistent group if the attribute is
    242 		 * missing.
    243 		 */
    244 		p2p_dbg(p2p, "Mandatory Invitation Flags attribute missing from Invitation Request");
    245 		persistent = 1;
    246 	}
    247 
    248 	p2p_channels_union(&p2p->cfg->channels, &p2p->cfg->cli_channels,
    249 			   &all_channels);
    250 
    251 	if (p2p_peer_channels_check(p2p, &all_channels, dev,
    252 				    msg.channel_list, msg.channel_list_len) <
    253 	    0) {
    254 		p2p_dbg(p2p, "No common channels found");
    255 		status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
    256 		goto fail;
    257 	}
    258 
    259 	p2p_channels_dump(p2p, "own channels", &p2p->cfg->channels);
    260 	p2p_channels_dump(p2p, "own client channels", &all_channels);
    261 	p2p_channels_dump(p2p, "peer channels", &dev->channels);
    262 	p2p_channels_intersect(&all_channels, &dev->channels,
    263 			       &intersection);
    264 	p2p_channels_dump(p2p, "intersection", &intersection);
    265 
    266 	if (p2p->cfg->invitation_process) {
    267 		status = p2p->cfg->invitation_process(
    268 			p2p->cfg->cb_ctx, sa, msg.group_bssid, msg.group_id,
    269 			msg.group_id + ETH_ALEN, msg.group_id_len - ETH_ALEN,
    270 			&go, group_bssid, &op_freq, persistent, &intersection,
    271 			msg.dev_password_id_present ? msg.dev_password_id : -1);
    272 	}
    273 
    274 	if (go) {
    275 		p2p_channels_intersect(&p2p->cfg->channels, &dev->channels,
    276 				       &intersection);
    277 		p2p_channels_dump(p2p, "intersection(GO)", &intersection);
    278 		if (intersection.reg_classes == 0) {
    279 			p2p_dbg(p2p, "No common channels found (GO)");
    280 			status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
    281 			goto fail;
    282 		}
    283 	}
    284 
    285 	if (op_freq) {
    286 		p2p_dbg(p2p, "Invitation processing forced frequency %d MHz",
    287 			op_freq);
    288 		if (p2p_freq_to_channel(op_freq, &reg_class, &channel) < 0) {
    289 			p2p_dbg(p2p, "Unknown forced freq %d MHz from invitation_process()",
    290 				op_freq);
    291 			status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
    292 			goto fail;
    293 		}
    294 
    295 		if (!p2p_channels_includes(&intersection, reg_class, channel))
    296 		{
    297 			p2p_dbg(p2p, "forced freq %d MHz not in the supported channels intersection",
    298 				op_freq);
    299 			status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
    300 			goto fail;
    301 		}
    302 
    303 		if (status == P2P_SC_SUCCESS)
    304 			channels = &intersection;
    305 	} else {
    306 		p2p_dbg(p2p, "No forced channel from invitation processing - figure out best one to use");
    307 
    308 		/* Default to own configuration as a starting point */
    309 		p2p->op_reg_class = p2p->cfg->op_reg_class;
    310 		p2p->op_channel = p2p->cfg->op_channel;
    311 		p2p_dbg(p2p, "Own default op_class %d channel %d",
    312 			p2p->op_reg_class, p2p->op_channel);
    313 
    314 		/* Use peer preference if specified and compatible */
    315 		if (msg.operating_channel) {
    316 			int req_freq;
    317 			req_freq = p2p_channel_to_freq(
    318 				msg.operating_channel[3],
    319 				msg.operating_channel[4]);
    320 			p2p_dbg(p2p, "Peer operating channel preference: %d MHz",
    321 				req_freq);
    322 			if (req_freq > 0 &&
    323 			    p2p_channels_includes(&intersection,
    324 						  msg.operating_channel[3],
    325 						  msg.operating_channel[4])) {
    326 				p2p->op_reg_class = msg.operating_channel[3];
    327 				p2p->op_channel = msg.operating_channel[4];
    328 				p2p_dbg(p2p, "Use peer preference op_class %d channel %d",
    329 					p2p->op_reg_class, p2p->op_channel);
    330 			} else {
    331 				p2p_dbg(p2p, "Cannot use peer channel preference");
    332 			}
    333 		}
    334 
    335 		/* Reselect the channel only for the case of the GO */
    336 		if (go &&
    337 		    !p2p_channels_includes(&intersection, p2p->op_reg_class,
    338 					   p2p->op_channel)) {
    339 			p2p_dbg(p2p, "Initially selected channel (op_class %d channel %d) not in channel intersection - try to reselect",
    340 				p2p->op_reg_class, p2p->op_channel);
    341 			p2p_reselect_channel(p2p, &intersection);
    342 			p2p_dbg(p2p, "Re-selection result: op_class %d channel %d",
    343 				p2p->op_reg_class, p2p->op_channel);
    344 			if (!p2p_channels_includes(&intersection,
    345 						   p2p->op_reg_class,
    346 						   p2p->op_channel)) {
    347 				p2p_dbg(p2p, "Peer does not support selected operating channel (reg_class=%u channel=%u)",
    348 					p2p->op_reg_class, p2p->op_channel);
    349 				status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
    350 				goto fail;
    351 			}
    352 		} else if (go && !(dev->flags & P2P_DEV_FORCE_FREQ) &&
    353 			   !p2p->cfg->cfg_op_channel) {
    354 			p2p_dbg(p2p, "Try to reselect channel selection with peer information received; previously selected op_class %u channel %u",
    355 				p2p->op_reg_class, p2p->op_channel);
    356 			p2p_reselect_channel(p2p, &intersection);
    357 		}
    358 
    359 		/*
    360 		 * Use the driver preferred frequency list extension if
    361 		 * supported.
    362 		 */
    363 		p2p_check_pref_chan(p2p, go, dev, &msg);
    364 
    365 		op_freq = p2p_channel_to_freq(p2p->op_reg_class,
    366 					      p2p->op_channel);
    367 		if (op_freq < 0) {
    368 			p2p_dbg(p2p, "Unknown operational channel (country=%c%c reg_class=%u channel=%u)",
    369 				p2p->cfg->country[0], p2p->cfg->country[1],
    370 				p2p->op_reg_class, p2p->op_channel);
    371 			status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
    372 			goto fail;
    373 		}
    374 		p2p_dbg(p2p, "Selected operating channel - %d MHz", op_freq);
    375 
    376 		if (status == P2P_SC_SUCCESS) {
    377 			reg_class = p2p->op_reg_class;
    378 			channel = p2p->op_channel;
    379 			channels = &intersection;
    380 		}
    381 	}
    382 
    383 fail:
    384 	if (go && status == P2P_SC_SUCCESS && !is_zero_ether_addr(group_bssid))
    385 		bssid = group_bssid;
    386 	else
    387 		bssid = NULL;
    388 	resp = p2p_build_invitation_resp(p2p, dev, msg.dialog_token, status,
    389 					 bssid, reg_class, channel, channels);
    390 
    391 	if (resp == NULL)
    392 		goto out;
    393 
    394 	if (rx_freq > 0)
    395 		freq = rx_freq;
    396 	else
    397 		freq = p2p_channel_to_freq(p2p->cfg->reg_class,
    398 					   p2p->cfg->channel);
    399 	if (freq < 0) {
    400 		p2p_dbg(p2p, "Unknown regulatory class/channel");
    401 		goto out;
    402 	}
    403 
    404 	/*
    405 	 * Store copy of invitation data to be used when processing TX status
    406 	 * callback for the Acton frame.
    407 	 */
    408 	os_memcpy(p2p->inv_sa, sa, ETH_ALEN);
    409 	if (msg.group_bssid) {
    410 		os_memcpy(p2p->inv_group_bssid, msg.group_bssid, ETH_ALEN);
    411 		p2p->inv_group_bssid_ptr = p2p->inv_group_bssid;
    412 	} else
    413 		p2p->inv_group_bssid_ptr = NULL;
    414 	if (msg.group_id) {
    415 		if (msg.group_id_len - ETH_ALEN <= SSID_MAX_LEN) {
    416 			os_memcpy(p2p->inv_ssid, msg.group_id + ETH_ALEN,
    417 				  msg.group_id_len - ETH_ALEN);
    418 			p2p->inv_ssid_len = msg.group_id_len - ETH_ALEN;
    419 		}
    420 		os_memcpy(p2p->inv_go_dev_addr, msg.group_id, ETH_ALEN);
    421 	} else {
    422 		p2p->inv_ssid_len = 0;
    423 		os_memset(p2p->inv_go_dev_addr, 0, ETH_ALEN);
    424 	}
    425 	p2p->inv_status = status;
    426 	p2p->inv_op_freq = op_freq;
    427 
    428 	p2p->pending_action_state = P2P_PENDING_INVITATION_RESPONSE;
    429 	if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr,
    430 			    p2p->cfg->dev_addr,
    431 			    wpabuf_head(resp), wpabuf_len(resp), 50) < 0) {
    432 		p2p_dbg(p2p, "Failed to send Action frame");
    433 	}
    434 
    435 out:
    436 	wpabuf_free(resp);
    437 	p2p_parse_free(&msg);
    438 }
    439 
    440 
    441 void p2p_process_invitation_resp(struct p2p_data *p2p, const u8 *sa,
    442 				 const u8 *data, size_t len)
    443 {
    444 	struct p2p_device *dev;
    445 	struct p2p_message msg;
    446 	struct p2p_channels intersection, *channels = NULL;
    447 
    448 	p2p_dbg(p2p, "Received Invitation Response from " MACSTR,
    449 		MAC2STR(sa));
    450 
    451 	dev = p2p_get_device(p2p, sa);
    452 	if (dev == NULL) {
    453 		p2p_dbg(p2p, "Ignore Invitation Response from unknown peer "
    454 			MACSTR, MAC2STR(sa));
    455 		p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
    456 		return;
    457 	}
    458 
    459 	if (dev != p2p->invite_peer) {
    460 		p2p_dbg(p2p, "Ignore unexpected Invitation Response from peer "
    461 			MACSTR, MAC2STR(sa));
    462 		p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
    463 		return;
    464 	}
    465 
    466 	if (p2p_parse(data, len, &msg)) {
    467 		p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
    468 		return;
    469 	}
    470 
    471 	if (!msg.status) {
    472 		p2p_dbg(p2p, "Mandatory Status attribute missing in Invitation Response from "
    473 			MACSTR, MAC2STR(sa));
    474 		p2p_parse_free(&msg);
    475 		p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
    476 		return;
    477 	}
    478 
    479 	/*
    480 	 * We should not really receive a replayed response twice since
    481 	 * duplicate frames are supposed to be dropped. However, not all drivers
    482 	 * do that for pre-association frames. We did not use to verify dialog
    483 	 * token matches for invitation response frames, but that check can be
    484 	 * safely used to drop a replayed response to the previous Invitation
    485 	 * Request in case the suggested operating channel was changed. This
    486 	 * allows a duplicated reject frame to be dropped with the assumption
    487 	 * that the real response follows after it.
    488 	 */
    489 	if (*msg.status == P2P_SC_FAIL_NO_COMMON_CHANNELS &&
    490 	    p2p->retry_invite_req_sent &&
    491 	    msg.dialog_token != dev->dialog_token) {
    492 		p2p_dbg(p2p, "Unexpected Dialog Token %u (expected %u)",
    493 			msg.dialog_token, dev->dialog_token);
    494 		p2p_parse_free(&msg);
    495 		return;
    496 	}
    497 
    498 	if (*msg.status == P2P_SC_FAIL_NO_COMMON_CHANNELS &&
    499 	    p2p->retry_invite_req &&
    500 	    p2p_channel_random_social(&p2p->cfg->channels, &p2p->op_reg_class,
    501 				      &p2p->op_channel, NULL, NULL) == 0) {
    502 		p2p->retry_invite_req = 0;
    503 		p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
    504 		p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
    505 		p2p_set_state(p2p, P2P_INVITE);
    506 		p2p_dbg(p2p, "Resend Invitation Request setting op_class %u channel %u as operating channel",
    507 			p2p->op_reg_class, p2p->op_channel);
    508 		p2p->retry_invite_req_sent = 1;
    509 		p2p_invite_send(p2p, p2p->invite_peer, p2p->invite_go_dev_addr,
    510 				p2p->invite_dev_pw_id);
    511 		p2p_parse_free(&msg);
    512 		return;
    513 	}
    514 	p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
    515 	p2p->retry_invite_req = 0;
    516 
    517 	if (!msg.channel_list && *msg.status == P2P_SC_SUCCESS) {
    518 		p2p_dbg(p2p, "Mandatory Channel List attribute missing in Invitation Response from "
    519 			MACSTR, MAC2STR(sa));
    520 #ifdef CONFIG_P2P_STRICT
    521 		p2p_parse_free(&msg);
    522 		return;
    523 #endif /* CONFIG_P2P_STRICT */
    524 		/* Try to survive without peer channel list */
    525 		channels = &p2p->channels;
    526 	} else if (!msg.channel_list) {
    527 		/* Non-success cases are not required to include Channel List */
    528 		channels = &p2p->channels;
    529 	} else if (p2p_peer_channels_check(p2p, &p2p->channels, dev,
    530 					   msg.channel_list,
    531 					   msg.channel_list_len) < 0) {
    532 		p2p_dbg(p2p, "No common channels found");
    533 		p2p_parse_free(&msg);
    534 		return;
    535 	} else {
    536 		p2p_channels_intersect(&p2p->channels, &dev->channels,
    537 				       &intersection);
    538 		channels = &intersection;
    539 	}
    540 
    541 	if (p2p->cfg->invitation_result) {
    542 		int peer_oper_freq = 0;
    543 		int freq = p2p_channel_to_freq(p2p->op_reg_class,
    544 					       p2p->op_channel);
    545 		if (freq < 0)
    546 			freq = 0;
    547 
    548 		if (msg.operating_channel) {
    549 			peer_oper_freq = p2p_channel_to_freq(
    550 				msg.operating_channel[3],
    551 				msg.operating_channel[4]);
    552 			if (peer_oper_freq < 0)
    553 				peer_oper_freq = 0;
    554 		}
    555 
    556 		/*
    557 		 * Use the driver preferred frequency list extension if
    558 		 * supported.
    559 		 */
    560 		p2p_check_pref_chan(p2p, 0, dev, &msg);
    561 
    562 		p2p->cfg->invitation_result(p2p->cfg->cb_ctx, *msg.status,
    563 					    msg.group_bssid, channels, sa,
    564 					    freq, peer_oper_freq);
    565 	}
    566 
    567 	p2p_parse_free(&msg);
    568 
    569 	p2p_clear_timeout(p2p);
    570 	p2p_set_state(p2p, P2P_IDLE);
    571 	p2p->invite_peer = NULL;
    572 }
    573 
    574 
    575 int p2p_invite_send(struct p2p_data *p2p, struct p2p_device *dev,
    576 		    const u8 *go_dev_addr, int dev_pw_id)
    577 {
    578 	struct wpabuf *req;
    579 	int freq;
    580 
    581 	freq = dev->listen_freq > 0 ? dev->listen_freq : dev->oper_freq;
    582 	if (freq <= 0)
    583 		freq = dev->oob_go_neg_freq;
    584 	if (freq <= 0) {
    585 		p2p_dbg(p2p, "No Listen/Operating frequency known for the peer "
    586 			MACSTR " to send Invitation Request",
    587 			MAC2STR(dev->info.p2p_device_addr));
    588 		return -1;
    589 	}
    590 
    591 	req = p2p_build_invitation_req(p2p, dev, go_dev_addr, dev_pw_id);
    592 	if (req == NULL)
    593 		return -1;
    594 	if (p2p->state != P2P_IDLE)
    595 		p2p_stop_listen_for_freq(p2p, freq);
    596 	p2p_dbg(p2p, "Sending Invitation Request");
    597 	p2p_set_state(p2p, P2P_INVITE);
    598 	p2p->pending_action_state = P2P_PENDING_INVITATION_REQUEST;
    599 	p2p->invite_peer = dev;
    600 	dev->invitation_reqs++;
    601 
    602 	/* In case of an active P2P GO use a shorter wait time to avoid
    603 	 * issues if not sending out multiple consecutive Beacon frames. */
    604 	if (p2p_send_action(p2p, freq, dev->info.p2p_device_addr,
    605 			    p2p->cfg->dev_addr, dev->info.p2p_device_addr,
    606 			    wpabuf_head(req), wpabuf_len(req),
    607 			    p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO ?
    608 			    150 : 500) < 0) {
    609 		p2p_dbg(p2p, "Failed to send Action frame");
    610 		/* Use P2P find to recover and retry */
    611 		p2p_set_timeout(p2p, 0, 0);
    612 	} else {
    613 		dev->flags |= P2P_DEV_WAIT_INV_REQ_ACK;
    614 	}
    615 
    616 	wpabuf_free(req);
    617 
    618 	return 0;
    619 }
    620 
    621 
    622 void p2p_invitation_req_cb(struct p2p_data *p2p, int success)
    623 {
    624 	p2p_dbg(p2p, "Invitation Request TX callback: success=%d", success);
    625 
    626 	if (p2p->invite_peer == NULL) {
    627 		p2p_dbg(p2p, "No pending Invite");
    628 		return;
    629 	}
    630 
    631 	if (success)
    632 		p2p->invite_peer->flags &= ~P2P_DEV_WAIT_INV_REQ_ACK;
    633 
    634 	/*
    635 	 * Use P2P find, if needed, to find the other device from its listen
    636 	 * channel.
    637 	 */
    638 	p2p_set_state(p2p, P2P_INVITE);
    639 	p2p_set_timeout(p2p, 0, success ? 500000 : 100000);
    640 }
    641 
    642 
    643 void p2p_invitation_resp_cb(struct p2p_data *p2p, int success)
    644 {
    645 	p2p_dbg(p2p, "Invitation Response TX callback: success=%d", success);
    646 	p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
    647 
    648 	if (!success)
    649 		p2p_dbg(p2p, "Assume Invitation Response was actually received by the peer even though Ack was not reported");
    650 
    651 	if (p2p->cfg->invitation_received) {
    652 		p2p->cfg->invitation_received(p2p->cfg->cb_ctx,
    653 					      p2p->inv_sa,
    654 					      p2p->inv_group_bssid_ptr,
    655 					      p2p->inv_ssid, p2p->inv_ssid_len,
    656 					      p2p->inv_go_dev_addr,
    657 					      p2p->inv_status,
    658 					      p2p->inv_op_freq);
    659 	}
    660 }
    661 
    662 
    663 int p2p_invite(struct p2p_data *p2p, const u8 *peer, enum p2p_invite_role role,
    664 	       const u8 *bssid, const u8 *ssid, size_t ssid_len,
    665 	       unsigned int force_freq, const u8 *go_dev_addr,
    666 	       int persistent_group, unsigned int pref_freq, int dev_pw_id)
    667 {
    668 	struct p2p_device *dev;
    669 
    670 	p2p_dbg(p2p, "Request to invite peer " MACSTR " role=%d persistent=%d "
    671 		"force_freq=%u allow_6ghz=%d",
    672 		MAC2STR(peer), role, persistent_group, force_freq,
    673 		p2p->allow_6ghz);
    674 	if (bssid)
    675 		p2p_dbg(p2p, "Invitation for BSSID " MACSTR, MAC2STR(bssid));
    676 	if (go_dev_addr) {
    677 		p2p_dbg(p2p, "Invitation for GO Device Address " MACSTR,
    678 			MAC2STR(go_dev_addr));
    679 		os_memcpy(p2p->invite_go_dev_addr_buf, go_dev_addr, ETH_ALEN);
    680 		p2p->invite_go_dev_addr = p2p->invite_go_dev_addr_buf;
    681 	} else
    682 		p2p->invite_go_dev_addr = NULL;
    683 	wpa_hexdump_ascii(MSG_DEBUG, "Invitation for SSID",
    684 			  ssid, ssid_len);
    685 	if (dev_pw_id >= 0) {
    686 		p2p_dbg(p2p, "Invitation to use Device Password ID %d",
    687 			dev_pw_id);
    688 	}
    689 	p2p->invite_dev_pw_id = dev_pw_id;
    690 	p2p->retry_invite_req = role == P2P_INVITE_ROLE_GO &&
    691 		persistent_group && !force_freq;
    692 	p2p->retry_invite_req_sent = 0;
    693 
    694 	dev = p2p_get_device(p2p, peer);
    695 	if (dev == NULL || (dev->listen_freq <= 0 && dev->oper_freq <= 0 &&
    696 			    dev->oob_go_neg_freq <= 0)) {
    697 		p2p_dbg(p2p, "Cannot invite unknown P2P Device " MACSTR,
    698 			MAC2STR(peer));
    699 		return -1;
    700 	}
    701 
    702 	if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq,
    703 				role != P2P_INVITE_ROLE_CLIENT) < 0)
    704 		return -1;
    705 
    706 	if (persistent_group && role == P2P_INVITE_ROLE_CLIENT && !force_freq &&
    707 	    !pref_freq)
    708 		dev->flags |= P2P_DEV_NO_PREF_CHAN;
    709 	else
    710 		dev->flags &= ~P2P_DEV_NO_PREF_CHAN;
    711 
    712 	if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
    713 		if (!(dev->info.dev_capab &
    714 		      P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
    715 			p2p_dbg(p2p, "Cannot invite a P2P Device " MACSTR
    716 				" that is in a group and is not discoverable",
    717 				MAC2STR(peer));
    718 		}
    719 		/* TODO: use device discoverability request through GO */
    720 	}
    721 
    722 	dev->invitation_reqs = 0;
    723 
    724 	if (p2p->state != P2P_IDLE)
    725 		p2p_stop_find(p2p);
    726 
    727 	p2p->inv_role = role;
    728 	p2p->inv_bssid_set = bssid != NULL;
    729 	if (bssid)
    730 		os_memcpy(p2p->inv_bssid, bssid, ETH_ALEN);
    731 	os_memcpy(p2p->inv_ssid, ssid, ssid_len);
    732 	p2p->inv_ssid_len = ssid_len;
    733 	p2p->inv_persistent = persistent_group;
    734 	return p2p_invite_send(p2p, dev, go_dev_addr, dev_pw_id);
    735 }
    736