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