• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Wi-Fi Direct - P2P Group Owner Negotiation
3  * Copyright (c) 2009-2010, Atheros Communications
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "includes.h"
10 
11 #include "common.h"
12 #include "utils/eloop.h"
13 #include "common/ieee802_11_defs.h"
14 #include "common/ieee802_11_common.h"
15 #include "common/wpa_ctrl.h"
16 #include "wps/wps_defs.h"
17 #include "p2p_i.h"
18 #include "p2p.h"
19 
20 #ifdef CONFIG_OPEN_HARMONY_PATCH
21 #include "wpa_supplicant_i.h"
22 #define LTECOEX_SAFE_CHANNEL 6
23 #ifdef CONFIG_MIRACAST_SINK_OPT
24 #include "securec.h"
25 #include "p2p_supplicant.h"
26 #include "hm_miracast_sink.h"
27 #endif
28 #endif
29 
30 #ifdef CONFIG_OPEN_HARMONY_P2P_DFH_CONNECT
31 #include "p2p_harmony.h"
32 #endif
33 
34 #ifdef CONFIG_P2P_CHR
35 #include "wpa_hw_p2p_chr.h"
36 #endif
37 
38 #ifdef CONFIG_LIBWPA_VENDOR
39 #include "wpa_client.h"
40 #endif
41 
42 #ifdef HARMONY_P2P_CONNECTIVITY_PATCH
43 #define HM_SHARE_FREQ_5G_MIN 4900
44 #define HM_SHARE_FREQ_5G_MAX 6000
45 #define HM_SHARE_FREQ_2G_MIN 2400
46 #define HM_SHARE_FREQ_2G_MAX 2500
47 #endif
48 
p2p_go_det(u8 own_intent,u8 peer_value)49 static int p2p_go_det(u8 own_intent, u8 peer_value)
50 {
51 	u8 peer_intent = peer_value >> 1;
52 	if (own_intent == peer_intent) {
53 		if (own_intent == P2P_MAX_GO_INTENT)
54 			return -1; /* both devices want to become GO */
55 
56 		/* Use tie breaker bit to determine GO */
57 		return (peer_value & 0x01) ? 0 : 1;
58 	}
59 
60 	return own_intent > peer_intent;
61 }
62 
63 
p2p_peer_channels_check(struct p2p_data * p2p,struct p2p_channels * own,struct p2p_device * dev,const u8 * channel_list,size_t channel_list_len)64 int p2p_peer_channels_check(struct p2p_data *p2p, struct p2p_channels *own,
65 			    struct p2p_device *dev,
66 			    const u8 *channel_list, size_t channel_list_len)
67 {
68 	const u8 *pos, *end;
69 	struct p2p_channels *ch;
70 	u8 channels;
71 	struct p2p_channels intersection;
72 
73 	ch = &dev->channels;
74 	os_memset(ch, 0, sizeof(*ch));
75 	pos = channel_list;
76 	end = channel_list + channel_list_len;
77 
78 	if (end - pos < 3)
79 		return -1;
80 	os_memcpy(dev->country, pos, 3);
81 	if (pos[2] != 0x04 && os_memcmp(pos, p2p->cfg->country, 2) != 0) {
82 		p2p_info(p2p, "Mismatching country (ours=** peer's=**)");
83 		return -1;
84 	}
85 	pos += 3;
86 
87 	while (end - pos > 2) {
88 		struct p2p_reg_class *cl = &ch->reg_class[ch->reg_classes];
89 		cl->reg_class = *pos++;
90 		channels = *pos++;
91 		if (channels > end - pos) {
92 			p2p_info(p2p, "Invalid peer Channel List");
93 			return -1;
94 		}
95 		cl->channels = channels > P2P_MAX_REG_CLASS_CHANNELS ?
96 			P2P_MAX_REG_CLASS_CHANNELS : channels;
97 		os_memcpy(cl->channel, pos, cl->channels);
98 		pos += channels;
99 		ch->reg_classes++;
100 		if (ch->reg_classes == P2P_MAX_REG_CLASSES)
101 			break;
102 	}
103 
104 	p2p_channels_intersect(own, &dev->channels, &intersection);
105 	p2p_dbg(p2p, "Own reg_classes %d peer reg_classes %d intersection reg_classes %d",
106 		(int) own->reg_classes,
107 		(int) dev->channels.reg_classes,
108 		(int) intersection.reg_classes);
109 	if (intersection.reg_classes == 0) {
110 		p2p_info(p2p, "No common channels found");
111 		return -1;
112 	}
113 	return 0;
114 }
115 
116 
p2p_peer_channels(struct p2p_data * p2p,struct p2p_device * dev,const u8 * channel_list,size_t channel_list_len)117 static int p2p_peer_channels(struct p2p_data *p2p, struct p2p_device *dev,
118 			     const u8 *channel_list, size_t channel_list_len)
119 {
120 	return p2p_peer_channels_check(p2p, &p2p->channels, dev,
121 				       channel_list, channel_list_len);
122 }
123 
124 
p2p_wps_method_pw_id(enum p2p_wps_method wps_method)125 u16 p2p_wps_method_pw_id(enum p2p_wps_method wps_method)
126 {
127 	switch (wps_method) {
128 	case WPS_PIN_DISPLAY:
129 		return DEV_PW_REGISTRAR_SPECIFIED;
130 	case WPS_PIN_KEYPAD:
131 		return DEV_PW_USER_SPECIFIED;
132 	case WPS_PBC:
133 		return DEV_PW_PUSHBUTTON;
134 	case WPS_NFC:
135 		return DEV_PW_NFC_CONNECTION_HANDOVER;
136 	case WPS_P2PS:
137 		return DEV_PW_P2PS_DEFAULT;
138 	default:
139 		return DEV_PW_DEFAULT;
140 	}
141 }
142 
143 
p2p_wps_method_str(enum p2p_wps_method wps_method)144 static const char * p2p_wps_method_str(enum p2p_wps_method wps_method)
145 {
146 	switch (wps_method) {
147 	case WPS_PIN_DISPLAY:
148 		return "Display";
149 	case WPS_PIN_KEYPAD:
150 		return "Keypad";
151 	case WPS_PBC:
152 		return "PBC";
153 	case WPS_NFC:
154 		return "NFC";
155 	case WPS_P2PS:
156 		return "P2PS";
157 	default:
158 		return "??";
159 	}
160 }
161 
162 
p2p_build_go_neg_req(struct p2p_data * p2p,struct p2p_device * peer)163 static struct wpabuf * p2p_build_go_neg_req(struct p2p_data *p2p,
164 					    struct p2p_device *peer)
165 {
166 	struct wpabuf *buf;
167 	u8 *len;
168 	u8 group_capab;
169 	size_t extra = 0;
170 	u16 pw_id;
171 	bool is_6ghz_capab;
172 
173 #ifdef CONFIG_WIFI_DISPLAY
174 	if (p2p->wfd_ie_go_neg)
175 		extra = wpabuf_len(p2p->wfd_ie_go_neg);
176 #endif /* CONFIG_WIFI_DISPLAY */
177 
178 	if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_REQ])
179 		extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_REQ]);
180 
181 	buf = wpabuf_alloc(1000 + extra);
182 	if (buf == NULL)
183 		return NULL;
184 
185 	p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_REQ, peer->dialog_token);
186 
187 	len = p2p_buf_add_ie_hdr(buf);
188 #ifdef CONFIG_P2P_USER_REJECT
189 	if (peer->flags & P2P_DEV_USER_REJECTED) {
190 		p2p_dbg(p2p, "user reject peer " MACSTR " status=%d", MAC2STR(peer->info.p2p_device_addr),
191 			peer->status);
192 		p2p_buf_add_status(buf, peer->status);
193 	}
194 #endif
195 	group_capab = 0;
196 	if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
197 		group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
198 		if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
199 			group_capab |= P2P_GROUP_CAPAB_PERSISTENT_RECONN;
200 	}
201 	if (p2p->cross_connect)
202 		group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
203 	if (p2p->cfg->p2p_intra_bss)
204 		group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
205 	p2p_buf_add_capability(buf, p2p->dev_capab &
206 			       ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY,
207 			       group_capab);
208 	p2p_buf_add_go_intent(buf, (p2p->go_intent << 1) | peer->tie_breaker);
209 	p2p_buf_add_config_timeout(buf, p2p->go_timeout, p2p->client_timeout);
210 	p2p_buf_add_listen_channel(buf, p2p->cfg->country, p2p->cfg->reg_class,
211 				   p2p->cfg->channel);
212 	if (p2p->ext_listen_interval)
213 		p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
214 					      p2p->ext_listen_interval);
215 	p2p_buf_add_intended_addr(buf, p2p->intended_addr);
216 	is_6ghz_capab = is_p2p_6ghz_capable(p2p) &&
217 		p2p_is_peer_6ghz_capab(p2p, peer->info.p2p_device_addr);
218 	if (p2p->num_pref_freq) {
219 		bool go = p2p->go_intent == 15;
220 		struct p2p_channels pref_chanlist;
221 
222 		p2p_pref_channel_filter(&p2p->channels, p2p->pref_freq_list,
223 					p2p->num_pref_freq, &pref_chanlist, go);
224 		p2p_channels_dump(p2p, "channel list after filtering",
225 				  &pref_chanlist);
226 		p2p_buf_add_channel_list(buf, p2p->cfg->country,
227 					 &pref_chanlist, is_6ghz_capab);
228 	} else {
229 		p2p_buf_add_channel_list(buf, p2p->cfg->country,
230 					 &p2p->channels, is_6ghz_capab);
231 	}
232 	p2p_buf_add_device_info(buf, p2p, peer);
233 	p2p_buf_add_operating_channel(buf, p2p->cfg->country,
234 				      p2p->op_reg_class, p2p->op_channel);
235 	p2p_buf_update_ie_hdr(buf, len);
236 
237 	p2p_buf_add_pref_channel_list(buf, p2p->pref_freq_list,
238 				      p2p->num_pref_freq);
239 
240 	/* WPS IE with Device Password ID attribute */
241 	pw_id = p2p_wps_method_pw_id(peer->wps_method);
242 	if (peer->oob_pw_id)
243 		pw_id = peer->oob_pw_id;
244 	if (p2p_build_wps_ie(p2p, buf, pw_id, 0) < 0) {
245 		p2p_dbg(p2p, "Failed to build WPS IE for GO Negotiation Request");
246 		wpabuf_free(buf);
247 		return NULL;
248 	}
249 
250 #ifdef CONFIG_WIFI_DISPLAY
251 	if (p2p->wfd_ie_go_neg)
252 		wpabuf_put_buf(buf, p2p->wfd_ie_go_neg);
253 #endif /* CONFIG_WIFI_DISPLAY */
254 
255 	if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_REQ])
256 		wpabuf_put_buf(buf, p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_REQ]);
257 
258 	return buf;
259 }
260 
261 
p2p_connect_send(struct p2p_data * p2p,struct p2p_device * dev)262 int p2p_connect_send(struct p2p_data *p2p, struct p2p_device *dev)
263 {
264 	struct wpabuf *req;
265 	int freq;
266 
267 	if (dev->flags & P2P_DEV_PD_BEFORE_GO_NEG) {
268 		u16 config_method;
269 		p2p_dbg(p2p, "Use PD-before-GO-Neg workaround for " MACSTR_SEC,
270 			MAC2STR_SEC(dev->info.p2p_device_addr));
271 		if (dev->wps_method == WPS_PIN_DISPLAY)
272 			config_method = WPS_CONFIG_KEYPAD;
273 		else if (dev->wps_method == WPS_PIN_KEYPAD)
274 			config_method = WPS_CONFIG_DISPLAY;
275 		else if (dev->wps_method == WPS_PBC)
276 			config_method = WPS_CONFIG_PUSHBUTTON;
277 		else if (dev->wps_method == WPS_P2PS)
278 			config_method = WPS_CONFIG_P2PS;
279 		else
280 			return -1;
281 		return p2p_prov_disc_req(p2p, dev->info.p2p_device_addr,
282 					 NULL, config_method, 0, 0, 1);
283 	}
284 
285 	freq = dev->listen_freq > 0 ? dev->listen_freq : dev->oper_freq;
286 	if (dev->oob_go_neg_freq > 0)
287 		freq = dev->oob_go_neg_freq;
288 	if (freq <= 0) {
289 		p2p_dbg(p2p, "No Listen/Operating frequency known for the peer "
290 			MACSTR_SEC " to send GO Negotiation Request",
291 			MAC2STR_SEC(dev->info.p2p_device_addr));
292 		return -1;
293 	}
294 
295 	req = p2p_build_go_neg_req(p2p, dev);
296 	if (req == NULL)
297 		return -1;
298 	p2p_info(p2p, "Sending GO Negotiation Request");
299 	p2p_set_state(p2p, P2P_CONNECT);
300 	p2p->pending_action_state = P2P_PENDING_GO_NEG_REQUEST;
301 	p2p->go_neg_peer = dev;
302 	eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
303 	dev->flags |= P2P_DEV_WAIT_GO_NEG_RESPONSE;
304 	dev->connect_reqs++;
305 	if (p2p_send_action(p2p, freq, dev->info.p2p_device_addr,
306 			    p2p->cfg->dev_addr, dev->info.p2p_device_addr,
307 			    wpabuf_head(req), wpabuf_len(req), 500) < 0) {
308 		p2p_dbg(p2p, "Failed to send Action frame");
309 		/* Use P2P find to recover and retry */
310 		p2p_set_timeout(p2p, 0, 0);
311 	} else
312 		dev->go_neg_req_sent++;
313 
314 	wpabuf_free(req);
315 
316 	return 0;
317 }
318 
319 
p2p_build_go_neg_resp(struct p2p_data * p2p,struct p2p_device * peer,u8 dialog_token,u8 status,u8 tie_breaker)320 static struct wpabuf * p2p_build_go_neg_resp(struct p2p_data *p2p,
321 					     struct p2p_device *peer,
322 					     u8 dialog_token, u8 status,
323 					     u8 tie_breaker)
324 {
325 	struct wpabuf *buf;
326 	u8 *len;
327 	u8 group_capab;
328 	size_t extra = 0;
329 	u16 pw_id;
330 	bool is_6ghz_capab;
331 	struct p2p_channels pref_chanlist;
332 
333 	p2p_dbg(p2p, "Building GO Negotiation Response");
334 
335 #ifdef CONFIG_WIFI_DISPLAY
336 	if (p2p->wfd_ie_go_neg)
337 		extra = wpabuf_len(p2p->wfd_ie_go_neg);
338 #endif /* CONFIG_WIFI_DISPLAY */
339 
340 	if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_RESP])
341 		extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_RESP]);
342 
343 	buf = wpabuf_alloc(1000 + extra);
344 	if (buf == NULL)
345 		return NULL;
346 
347 	p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_RESP, dialog_token);
348 
349 	len = p2p_buf_add_ie_hdr(buf);
350 	p2p_buf_add_status(buf, status);
351 	group_capab = 0;
352 	if (peer && peer->go_state == LOCAL_GO) {
353 		if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
354 			group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
355 			if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
356 				group_capab |=
357 					P2P_GROUP_CAPAB_PERSISTENT_RECONN;
358 		}
359 		if (p2p->cross_connect)
360 			group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
361 		if (p2p->cfg->p2p_intra_bss)
362 			group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
363 	}
364 	p2p_buf_add_capability(buf, p2p->dev_capab &
365 			       ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY,
366 			       group_capab);
367 	p2p_buf_add_go_intent(buf, (p2p->go_intent << 1) | tie_breaker);
368 	p2p_buf_add_config_timeout(buf, p2p->go_timeout, p2p->client_timeout);
369 	if (p2p->override_pref_op_class) {
370 		p2p_dbg(p2p, "Override operating channel preference");
371 		p2p_buf_add_operating_channel(buf, p2p->cfg->country,
372 					      p2p->override_pref_op_class,
373 					      p2p->override_pref_channel);
374 	} else if (peer && peer->go_state == REMOTE_GO && !p2p->num_pref_freq) {
375 		p2p_dbg(p2p, "Omit Operating Channel attribute");
376 	} else {
377 		p2p_buf_add_operating_channel(buf, p2p->cfg->country,
378 					      p2p->op_reg_class,
379 					      p2p->op_channel);
380 	}
381 	p2p_buf_add_intended_addr(buf, p2p->intended_addr);
382 	if (p2p->num_pref_freq) {
383 		bool go = (peer && peer->go_state == LOCAL_GO) ||
384 			p2p->go_intent == 15;
385 
386 		p2p_pref_channel_filter(&p2p->channels, p2p->pref_freq_list,
387 					p2p->num_pref_freq, &pref_chanlist, go);
388 		p2p_channels_dump(p2p, "channel list after filtering",
389 				  &pref_chanlist);
390 	} else {
391 		p2p_copy_channels(&pref_chanlist, &p2p->channels,
392 				  p2p->allow_6ghz);
393 	}
394 	if (status || peer == NULL) {
395 		p2p_buf_add_channel_list(buf, p2p->cfg->country,
396 					 &pref_chanlist, false);
397 	} else if (peer->go_state == REMOTE_GO) {
398 		is_6ghz_capab = is_p2p_6ghz_capable(p2p) &&
399 			p2p_is_peer_6ghz_capab(p2p, peer->info.p2p_device_addr);
400 		p2p_buf_add_channel_list(buf, p2p->cfg->country,
401 					 &pref_chanlist, is_6ghz_capab);
402 	} else {
403 		struct p2p_channels res;
404 
405 		is_6ghz_capab = is_p2p_6ghz_capable(p2p) &&
406 			p2p_is_peer_6ghz_capab(p2p, peer->info.p2p_device_addr);
407 		p2p_channels_intersect(&pref_chanlist, &peer->channels,
408 				       &res);
409 		p2p_buf_add_channel_list(buf, p2p->cfg->country, &res,
410 				       is_6ghz_capab);
411 	}
412 	p2p_buf_add_device_info(buf, p2p, peer);
413 	if (peer && peer->go_state == LOCAL_GO) {
414 		p2p_buf_add_group_id(buf, p2p->cfg->dev_addr, p2p->ssid,
415 				     p2p->ssid_len);
416 	}
417 	p2p_buf_update_ie_hdr(buf, len);
418 
419 	/* WPS IE with Device Password ID attribute */
420 	pw_id = p2p_wps_method_pw_id(peer ? peer->wps_method : WPS_NOT_READY);
421 	if (peer && peer->oob_pw_id)
422 		pw_id = peer->oob_pw_id;
423 	if (p2p_build_wps_ie(p2p, buf, pw_id, 0) < 0) {
424 		p2p_dbg(p2p, "Failed to build WPS IE for GO Negotiation Response");
425 		wpabuf_free(buf);
426 		return NULL;
427 	}
428 
429 #ifdef CONFIG_WIFI_DISPLAY
430 	if (p2p->wfd_ie_go_neg)
431 		wpabuf_put_buf(buf, p2p->wfd_ie_go_neg);
432 #endif /* CONFIG_WIFI_DISPLAY */
433 
434 	if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_RESP])
435 		wpabuf_put_buf(buf, p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_RESP]);
436 
437 	return buf;
438 }
439 
440 #ifdef HARMONY_P2P_CONNECTIVITY_PATCH
hm_get_share_freq(struct p2p_data * p2p,int * share_freq)441 int hm_get_share_freq(struct p2p_data *p2p, int *share_freq)
442 {
443 	struct wpa_supplicant *wpa_s = p2p->cfg->cb_ctx;
444 	struct wpa_used_freq_data *freqs;
445 	int num;
446 	freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(struct wpa_used_freq_data));
447 	if (!freqs) {
448 		return 0;
449 	}
450 	num = get_shared_radio_freqs_data(wpa_s, freqs, wpa_s->num_multichan_concurrent);
451 	if (num > 0) {
452 		*share_freq = freqs[0].freq;
453 	}
454 	for (u8 i = 0; i < num; i++) {
455 		if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
456 			*share_freq = freqs[i].freq;
457 			break;
458 		}
459 		if (freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT) {
460 			*share_freq = freqs[i].freq;
461 		}
462 	}
463 	os_free(freqs);
464 	return num;
465 }
466 #endif
467 
468 /**
469  * p2p_reselect_channel - Re-select operating channel based on peer information
470  * @p2p: P2P module context from p2p_init()
471  * @intersection: Support channel list intersection from local and peer
472  *
473  * This function is used to re-select the best channel after having received
474  * information from the peer to allow supported channel lists to be intersected.
475  * This can be used to improve initial channel selection done in
476  * p2p_prepare_channel() prior to the start of GO Negotiation. In addition, this
477  * can be used for Invitation case.
478  */
p2p_reselect_channel(struct p2p_data * p2p,struct p2p_channels * intersection)479 void p2p_reselect_channel(struct p2p_data *p2p,
480 			  struct p2p_channels *intersection)
481 {
482 	struct p2p_reg_class *cl;
483 	int freq;
484 	u8 op_reg_class, op_channel;
485 	unsigned int i;
486 	const int op_classes_5ghz[] = { 124, 125, 115, 0 };
487 	const int op_classes_ht40[] = { 126, 127, 116, 117, 0 };
488 	const int op_classes_vht[] = { 128, 129, 130, 0 };
489 	const int op_classes_edmg[] = { 181, 182, 183, 0 };
490 
491 	if (p2p->own_freq_preference > 0 &&
492 	    p2p_freq_to_channel(p2p->own_freq_preference,
493 				&op_reg_class, &op_channel) == 0 &&
494 	    p2p_channels_includes(intersection, op_reg_class, op_channel)) {
495 		p2p_dbg(p2p, "Pick own channel preference (reg_class %u channel %u) from intersection",
496 			op_reg_class, op_channel);
497 		p2p->op_reg_class = op_reg_class;
498 		p2p->op_channel = op_channel;
499 		return;
500 	}
501 
502 	if (p2p->best_freq_overall > 0 &&
503 	    p2p_freq_to_channel(p2p->best_freq_overall,
504 				&op_reg_class, &op_channel) == 0 &&
505 	    p2p_channels_includes(intersection, op_reg_class, op_channel)) {
506 		p2p_dbg(p2p, "Pick best overall channel (reg_class %u channel %u) from intersection",
507 			op_reg_class, op_channel);
508 		p2p->op_reg_class = op_reg_class;
509 		p2p->op_channel = op_channel;
510 		return;
511 	}
512 
513 	/* First, try to pick the best channel from another band */
514 	freq = p2p_channel_to_freq(p2p->op_reg_class, p2p->op_channel);
515 	if (freq >= 2400 && freq < 2500 && p2p->best_freq_5 > 0 &&
516 	    !p2p_channels_includes(intersection, p2p->op_reg_class,
517 				   p2p->op_channel) &&
518 	    p2p_freq_to_channel(p2p->best_freq_5,
519 				&op_reg_class, &op_channel) == 0 &&
520 	    p2p_channels_includes(intersection, op_reg_class, op_channel)) {
521 		p2p_dbg(p2p, "Pick best 5 GHz channel (reg_class %u channel %u) from intersection",
522 			op_reg_class, op_channel);
523 		p2p->op_reg_class = op_reg_class;
524 		p2p->op_channel = op_channel;
525 		return;
526 	}
527 
528 	if (freq >= 4900 && freq < 6000 && p2p->best_freq_24 > 0 &&
529 	    !p2p_channels_includes(intersection, p2p->op_reg_class,
530 				   p2p->op_channel) &&
531 	    p2p_freq_to_channel(p2p->best_freq_24,
532 				&op_reg_class, &op_channel) == 0 &&
533 	    p2p_channels_includes(intersection, op_reg_class, op_channel)) {
534 		p2p_dbg(p2p, "Pick best 2.4 GHz channel (reg_class %u channel %u) from intersection",
535 			op_reg_class, op_channel);
536 		p2p->op_reg_class = op_reg_class;
537 		p2p->op_channel = op_channel;
538 		return;
539 	}
540 
541 	/* Select channel with highest preference if the peer supports it */
542 	for (i = 0; p2p->cfg->pref_chan && i < p2p->cfg->num_pref_chan; i++) {
543 		if (p2p_channels_includes(intersection,
544 					  p2p->cfg->pref_chan[i].op_class,
545 					  p2p->cfg->pref_chan[i].chan)) {
546 			p2p->op_reg_class = p2p->cfg->pref_chan[i].op_class;
547 			p2p->op_channel = p2p->cfg->pref_chan[i].chan;
548 			p2p_dbg(p2p, "Pick highest preferred channel (op_class %u channel %u) from intersection",
549 				p2p->op_reg_class, p2p->op_channel);
550 			return;
551 		}
552 	}
553 
554 #ifdef HARMONY_P2P_CONNECTIVITY_PATCH
555 	struct wpa_supplicant *wpa_s = p2p->cfg->cb_ctx;
556 	int num = 0;
557 	int share_freq = 0;
558 #ifdef OPEN_HARMONY_MIRACAST_SINK_OPT
559 	int best_freq = 0;
560 	int ret;
561 	/* If the wifi is associated wifi 5G, the channel of wlan0 is preferred as the working channel of the GO. */
562 	ret = hm_p2p_pick_op_channel_in_5g_assoc(p2p, intersection, wpa_s, &best_freq, &num);
563 	if (ret == HM_SUCC) {
564 		p2p_dbg(p2p, "Pick our sta channel (reg_class %u channel %u) as p2p op channel",
565 			p2p->op_reg_class, p2p->op_channel);
566 		return;
567 	}
568 #else
569 	num = hm_get_share_freq(p2p, &share_freq);
570 	p2p_dbg(p2p, "p2p_reselect_channel:num_MCC %d, shared_freq_num %u", wpa_s->num_multichan_concurrent, num);
571 	/* follow share freq 5G */
572 	if (num > 0 && share_freq >= HM_SHARE_FREQ_5G_MIN && share_freq < HM_SHARE_FREQ_5G_MAX) {
573 		if (p2p_freq_to_channel(share_freq, &op_reg_class, &op_channel) == 0 &&
574 			p2p_channels_includes(intersection, op_reg_class, op_channel)) {
575 			p2p_dbg(p2p, "share freq on 5G (reg_class %u channel %u)", op_reg_class, op_channel);
576 			p2p->op_reg_class = op_reg_class;
577 			p2p->op_channel = op_channel;
578 			return;
579 		}
580 	}
581 #endif
582 #endif
583 
584 	/* Try a channel where we might be able to use EDMG */
585 	if (p2p_channel_select(intersection, op_classes_edmg,
586 			       &p2p->op_reg_class, &p2p->op_channel) == 0) {
587 		p2p_dbg(p2p, "Pick possible EDMG channel (op_class %u channel %u) from intersection",
588 			p2p->op_reg_class, p2p->op_channel);
589 		return;
590 	}
591 
592 	/* Try a channel where we might be able to use VHT */
593 	if (p2p_channel_select(intersection, op_classes_vht,
594 			       &p2p->op_reg_class, &p2p->op_channel) == 0) {
595 		p2p_dbg(p2p, "Pick possible VHT channel (op_class %u channel %u) from intersection",
596 			p2p->op_reg_class, p2p->op_channel);
597 		return;
598 	}
599 
600 	/* Try a channel where we might be able to use HT40 */
601 	if (p2p_channel_select(intersection, op_classes_ht40,
602 			       &p2p->op_reg_class, &p2p->op_channel) == 0) {
603 		p2p_dbg(p2p, "Pick possible HT40 channel (op_class %u channel %u) from intersection",
604 			p2p->op_reg_class, p2p->op_channel);
605 		return;
606 	}
607 
608 	/* Prefer a 5 GHz channel */
609 	if (p2p_channel_select(intersection, op_classes_5ghz,
610 			       &p2p->op_reg_class, &p2p->op_channel) == 0) {
611 		p2p_dbg(p2p, "Pick possible 5 GHz channel (op_class %u channel %u) from intersection",
612 			p2p->op_reg_class, p2p->op_channel);
613 		return;
614 	}
615 
616 #ifdef HARMONY_P2P_CONNECTIVITY_PATCH
617 #ifdef OPEN_HARMONY_MIRACAST_SINK_OPT
618 	/* intersection not find 5G channel, so only support 2.4G channel */
619 	if (hm_pick_2g_op_channel(p2p, intersection, num, best_freq) == HM_SUCC) {
620 		p2p_dbg(p2p, "p2p_reselect_channel Pick 2.4g channel (op_class %u channel %u) ok",
621 			p2p->op_reg_class, p2p->op_channel);
622 		return;
623 	}
624 #else
625 	/* follow share freq 2G */
626 	if (num > 0 && share_freq >= HM_SHARE_FREQ_2G_MIN && share_freq < HM_SHARE_FREQ_2G_MAX) {
627 		if (p2p_freq_to_channel(share_freq, &op_reg_class, &op_channel) == 0 &&
628 			p2p_channels_includes(intersection, op_reg_class, op_channel)) {
629 			p2p_dbg(p2p, "share freq on 2G (reg_class %u channel %u)", op_reg_class, op_channel);
630 			p2p->op_reg_class = op_reg_class;
631 			p2p->op_channel = op_channel;
632 			return;
633 		}
634 	}
635 #endif
636 #endif
637 
638 	/*
639 	 * Try to see if the original channel is in the intersection. If
640 	 * so, no need to change anything, as it already contains some
641 	 * randomness.
642 	 */
643 	if (p2p_channels_includes(intersection, p2p->op_reg_class,
644 				  p2p->op_channel)) {
645 		p2p_dbg(p2p, "Using original operating class and channel (op_class %u channel %u) from intersection",
646 			p2p->op_reg_class, p2p->op_channel);
647 		return;
648 	}
649 
650 	/*
651 	 * Fall back to whatever is included in the channel intersection since
652 	 * no better options seems to be available.
653 	 */
654 	cl = &intersection->reg_class[0];
655 	p2p_dbg(p2p, "Pick another channel (reg_class %u channel %u) from intersection",
656 		cl->reg_class, cl->channel[0]);
657 	p2p->op_reg_class = cl->reg_class;
658 	p2p->op_channel = cl->channel[0];
659 }
660 
661 
p2p_go_select_channel(struct p2p_data * p2p,struct p2p_device * dev,u8 * status)662 int p2p_go_select_channel(struct p2p_data *p2p, struct p2p_device *dev,
663 			  u8 *status)
664 {
665 	struct p2p_channels tmp, intersection;
666 #if defined(CONFIG_OPEN_HARMONY_PATCH) && defined(CONFIG_MIRACAST_SINK_OPT)
667 	int pvt_peer = FALSE;
668 	struct hm_p2p_pvt_peer pvt_peer_info;
669 #endif
670 
671 	p2p_channels_dump(p2p, "own channels", &p2p->channels);
672 	p2p_channels_dump(p2p, "peer channels", &dev->channels);
673 	p2p_channels_intersect(&p2p->channels, &dev->channels, &tmp);
674 	p2p_channels_dump(p2p, "intersection", &tmp);
675 	p2p_channels_remove_freqs(&tmp, &p2p->no_go_freq);
676 	p2p_channels_dump(p2p, "intersection after no-GO removal", &tmp);
677 	p2p_channels_intersect(&tmp, &p2p->cfg->channels, &intersection);
678 	p2p_channels_dump(p2p, "intersection with local channel list",
679 			  &intersection);
680 	if (intersection.reg_classes == 0 ||
681 	    intersection.reg_class[0].channels == 0) {
682 		*status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
683 		p2p_dbg(p2p, "No common channels found");
684 		return -1;
685 	}
686 
687 #if defined(CONFIG_OPEN_HARMONY_PATCH) && defined(CONFIG_MIRACAST_SINK_OPT)
688 	pvt_peer = hm_p2p_get_peer_info(dev, &pvt_peer_info);
689 
690 	p2p_dbg(p2p, "origin pepare operating channel (op_class %u channel %u)",
691 		p2p->op_reg_class, p2p->op_channel);
692 
693 	if (pvt_peer == TRUE) {
694 		hm_p2p_pvt_peer_select_channel(p2p, &intersection, &pvt_peer_info);
695 	} else {
696 #endif
697 	if (!p2p_channels_includes(&intersection, p2p->op_reg_class,
698 				   p2p->op_channel)) {
699 		if (dev->flags & P2P_DEV_FORCE_FREQ) {
700 			*status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
701 			p2p_dbg(p2p, "Peer does not support the forced channel");
702 			return -1;
703 		}
704 
705 		p2p_dbg(p2p, "Selected operating channel (op_class %u channel %u) not acceptable to the peer",
706 			p2p->op_reg_class, p2p->op_channel);
707 		p2p_reselect_channel(p2p, &intersection);
708 	} else if (!(dev->flags & P2P_DEV_FORCE_FREQ) &&
709 		   !p2p->cfg->cfg_op_channel) {
710 		p2p_dbg(p2p, "Try to optimize channel selection with peer information received; previously selected op_class %u channel %u",
711 			p2p->op_reg_class, p2p->op_channel);
712 		p2p_reselect_channel(p2p, &intersection);
713 	}
714 #if defined(CONFIG_OPEN_HARMONY_PATCH) && defined(CONFIG_MIRACAST_SINK_OPT)
715 	}
716 #endif
717 
718 	if (!p2p->ssid_set) {
719 		p2p_build_ssid(p2p, p2p->ssid, &p2p->ssid_len);
720 		p2p->ssid_set = 1;
721 	}
722 
723 	return 0;
724 }
725 
726 
p2p_check_pref_chan_no_recv(struct p2p_data * p2p,int go,struct p2p_device * dev,struct p2p_message * msg,const struct weighted_pcl freq_list[],unsigned int size)727 static void p2p_check_pref_chan_no_recv(struct p2p_data *p2p, int go,
728 					struct p2p_device *dev,
729 					struct p2p_message *msg,
730 					const struct weighted_pcl freq_list[],
731 					unsigned int size)
732 {
733 	u8 op_class, op_channel;
734 	unsigned int oper_freq = 0, i, j;
735 	int found = 0;
736 
737 	p2p_dbg(p2p,
738 		"Peer didn't provide a preferred frequency list, see if any of our preferred channels are supported by peer device");
739 
740 	/*
741 	 * Search for a common channel in our preferred frequency list which is
742 	 * also supported by the peer device.
743 	 */
744 	for (i = 0; i < size && !found; i++) {
745 		/* Make sure that the common frequency is supported by peer. */
746 		oper_freq = freq_list[i].freq;
747 		if (p2p_freq_to_channel(oper_freq, &op_class,
748 					&op_channel) < 0 ||
749 		    !p2p_pref_freq_allowed(&freq_list[i], go))
750 			continue;
751 		for (j = 0; j < msg->channel_list_len; j++) {
752 			if (!msg->channel_list ||
753 			    op_channel != msg->channel_list[j])
754 				continue;
755 
756 			p2p->op_reg_class = op_class;
757 			p2p->op_channel = op_channel;
758 			os_memcpy(&p2p->channels, &p2p->cfg->channels,
759 				  sizeof(struct p2p_channels));
760 			found = 1;
761 			break;
762 		}
763 	}
764 
765 	if (found) {
766 		p2p_dbg(p2p,
767 			"Freq %d MHz is a preferred channel and is also supported by peer, use it as the operating channel",
768 			oper_freq);
769 	} else {
770 		p2p_dbg(p2p,
771 			"None of our preferred channels are supported by peer!");
772 	}
773 }
774 
775 
p2p_check_pref_chan_recv(struct p2p_data * p2p,int go,struct p2p_device * dev,struct p2p_message * msg,const struct weighted_pcl freq_list[],unsigned int size)776 static void p2p_check_pref_chan_recv(struct p2p_data *p2p, int go,
777 				     struct p2p_device *dev,
778 				     struct p2p_message *msg,
779 				     const struct weighted_pcl freq_list[],
780 				     unsigned int size)
781 {
782 	u8 op_class, op_channel;
783 	unsigned int oper_freq = 0, i, j;
784 	int found = 0;
785 
786 	/*
787 	 * Peer device supports a Preferred Frequency List.
788 	 * Search for a common channel in the preferred frequency lists
789 	 * of both peer and local devices.
790 	 */
791 	for (i = 0; i < size && !found; i++) {
792 		for (j = 2; j < (msg->pref_freq_list_len / 2); j++) {
793 			oper_freq = p2p_channel_to_freq(
794 				msg->pref_freq_list[2 * j],
795 				msg->pref_freq_list[2 * j + 1]);
796 			if (freq_list[i].freq != oper_freq)
797 				continue;
798 			if (p2p_freq_to_channel(oper_freq, &op_class,
799 						&op_channel) < 0)
800 				continue; /* cannot happen */
801 			if (!p2p_pref_freq_allowed(&freq_list[i], go))
802 				break;
803 			p2p->op_reg_class = op_class;
804 			p2p->op_channel = op_channel;
805 			os_memcpy(&p2p->channels, &p2p->cfg->channels,
806 				  sizeof(struct p2p_channels));
807 			found = 1;
808 			break;
809 		}
810 	}
811 
812 	if (found) {
813 		p2p_dbg(p2p,
814 			"Freq %d MHz is a common preferred channel for both peer and local, use it as operating channel",
815 			oper_freq);
816 	} else {
817 		p2p_dbg(p2p, "No common preferred channels found!");
818 	}
819 }
820 
821 
p2p_check_pref_chan(struct p2p_data * p2p,int go,struct p2p_device * dev,struct p2p_message * msg)822 void p2p_check_pref_chan(struct p2p_data *p2p, int go,
823 			 struct p2p_device *dev, struct p2p_message *msg)
824 {
825 	unsigned int size;
826 	unsigned int i;
827 	u8 op_class, op_channel;
828 	char txt[100], *pos, *end;
829 	bool is_6ghz_capab;
830 	int res;
831 
832 	/*
833 	 * Use the preferred channel list from the driver only if there is no
834 	 * forced_freq, e.g., P2P_CONNECT freq=..., and no preferred operating
835 	 * channel hardcoded in the configuration file.
836 	 */
837 	if (!p2p->cfg->get_pref_freq_list || p2p->cfg->num_pref_chan ||
838 	    (dev->flags & P2P_DEV_FORCE_FREQ) || p2p->cfg->cfg_op_channel)
839 		return;
840 
841 	/* Obtain our preferred frequency list from driver based on P2P role. */
842 	size = P2P_MAX_PREF_CHANNELS;
843 	if (p2p->cfg->get_pref_freq_list(p2p->cfg->cb_ctx, go,
844 					 &p2p->num_pref_freq,
845 					 p2p->pref_freq_list))
846 		return;
847 	size = p2p->num_pref_freq;
848 	if (!size)
849 		return;
850 	/* Filter out frequencies that are not acceptable for P2P use */
851 	is_6ghz_capab = is_p2p_6ghz_capable(p2p) &&
852 		p2p_is_peer_6ghz_capab(p2p, dev->info.p2p_device_addr);
853 	i = 0;
854 	while (i < size) {
855 		if (p2p_freq_to_channel(p2p->pref_freq_list[i].freq,
856 					&op_class, &op_channel) < 0 ||
857 		    (!p2p_channels_includes(&p2p->cfg->channels,
858 					    op_class, op_channel) &&
859 		     (go || !p2p_channels_includes(&p2p->cfg->cli_channels,
860 						   op_class, op_channel))) ||
861 		    (is_6ghz_freq(p2p->pref_freq_list[i].freq) &&
862 		     !is_6ghz_capab)) {
863 			p2p_dbg(p2p,
864 				"Ignore local driver frequency preference %u MHz since it is not acceptable for P2P use (go=%d)",
865 				p2p->pref_freq_list[i].freq, go);
866 			if (size - i - 1 > 0)
867 				os_memmove(&p2p->pref_freq_list[i],
868 					   &p2p->pref_freq_list[i + 1],
869 					   (size - i - 1) *
870 					   sizeof(struct weighted_pcl));
871 			size--;
872 			continue;
873 		}
874 
875 		/* Preferred frequency is acceptable for P2P use */
876 		i++;
877 	}
878 
879 	pos = txt;
880 	end = pos + sizeof(txt);
881 	for (i = 0; i < size; i++) {
882 		res = os_snprintf(pos, end - pos, " %u",
883 				  p2p->pref_freq_list[i].freq);
884 		if (os_snprintf_error(end - pos, res))
885 			break;
886 		pos += res;
887 	}
888 	*pos = '\0';
889 	p2p_dbg(p2p, "Local driver frequency preference (size=%u):%s",
890 		size, txt);
891 
892 	/*
893 	 * Check if peer's preference of operating channel is in
894 	 * our preferred channel list.
895 	 */
896 	for (i = 0; i < size; i++) {
897 		if (p2p->pref_freq_list[i].freq ==
898 		    (unsigned int) dev->oper_freq &&
899 		    p2p_pref_freq_allowed(&p2p->pref_freq_list[i], go))
900 			break;
901 	}
902 	if (i != size &&
903 	    p2p_freq_to_channel(p2p->pref_freq_list[i].freq, &op_class,
904 				&op_channel) == 0) {
905 		/* Peer operating channel preference matches our preference */
906 		p2p->op_reg_class = op_class;
907 		p2p->op_channel = op_channel;
908 		os_memcpy(&p2p->channels, &p2p->cfg->channels,
909 			  sizeof(struct p2p_channels));
910 		return;
911 	}
912 
913 	p2p_dbg(p2p,
914 		"Peer operating channel preference: %d MHz is not in our preferred channel list",
915 		dev->oper_freq);
916 
917 	/*
918 	  Check if peer's preferred channel list is
919 	  * _not_ included in the GO Negotiation Request or Invitation Request.
920 	  */
921 	if (msg->pref_freq_list_len == 0)
922 		p2p_check_pref_chan_no_recv(p2p, go, dev, msg,
923 					    p2p->pref_freq_list, size);
924 	else
925 		p2p_check_pref_chan_recv(p2p, go, dev, msg,
926 					 p2p->pref_freq_list, size);
927 }
928 
929 
p2p_process_go_neg_req(struct p2p_data * p2p,const u8 * sa,const u8 * data,size_t len,int rx_freq)930 void p2p_process_go_neg_req(struct p2p_data *p2p, const u8 *sa,
931 			    const u8 *data, size_t len, int rx_freq)
932 {
933 	struct p2p_device *dev = NULL;
934 	struct wpabuf *resp;
935 	struct p2p_message msg;
936 	u8 status = P2P_SC_FAIL_INVALID_PARAMS;
937 	int tie_breaker = 0;
938 	int freq;
939 
940 	p2p_info(p2p, "Received GO Negotiation Request from " MACSTR_SEC "(freq=%d)",
941 		MAC2STR_SEC(sa), rx_freq);
942 
943 	if (p2p_parse(data, len, &msg))
944 		return;
945 
946 	if (!msg.capability) {
947 		p2p_dbg(p2p, "Mandatory Capability attribute missing from GO Negotiation Request");
948 #ifdef CONFIG_P2P_STRICT
949 		goto fail;
950 #endif /* CONFIG_P2P_STRICT */
951 	}
952 
953 	if (msg.go_intent)
954 		tie_breaker = *msg.go_intent & 0x01;
955 	else {
956 		p2p_dbg(p2p, "Mandatory GO Intent attribute missing from GO Negotiation Request");
957 #ifdef CONFIG_P2P_STRICT
958 		goto fail;
959 #endif /* CONFIG_P2P_STRICT */
960 	}
961 
962 	if (!msg.config_timeout) {
963 		p2p_dbg(p2p, "Mandatory Configuration Timeout attribute missing from GO Negotiation Request");
964 #ifdef CONFIG_P2P_STRICT
965 		goto fail;
966 #endif /* CONFIG_P2P_STRICT */
967 	}
968 
969 	if (!msg.listen_channel) {
970 		p2p_dbg(p2p, "No Listen Channel attribute received");
971 		goto fail;
972 	}
973 	if (!msg.operating_channel) {
974 		p2p_dbg(p2p, "No Operating Channel attribute received");
975 		goto fail;
976 	}
977 	if (!msg.channel_list) {
978 		p2p_dbg(p2p, "No Channel List attribute received");
979 		goto fail;
980 	}
981 	if (!msg.intended_addr) {
982 		p2p_dbg(p2p, "No Intended P2P Interface Address attribute received");
983 		goto fail;
984 	}
985 	if (!msg.p2p_device_info) {
986 		p2p_dbg(p2p, "No P2P Device Info attribute received");
987 		goto fail;
988 	}
989 
990 	if (!ether_addr_equal(msg.p2p_device_addr, sa)) {
991 		p2p_dbg(p2p, "Unexpected GO Negotiation Request SA=" MACSTR_SEC
992 			" != dev_addr=" MACSTR_SEC,
993 			MAC2STR_SEC(sa), MAC2STR_SEC(msg.p2p_device_addr));
994 		goto fail;
995 	}
996 
997 	dev = p2p_get_device(p2p, sa);
998 #ifdef HARMONY_P2P_CONNECTIVITY_PATCH
999 	if (dev && p2p->cfg && p2p->cfg->dev_found)
1000 		p2p->cfg->dev_found(p2p->cfg->cb_ctx, sa, &dev->info,
1001 					!(dev->flags & P2P_DEV_REPORTED_ONCE));
1002 #endif
1003 	if (msg.status && *msg.status) {
1004 		p2p_dbg(p2p, "Unexpected Status attribute (%d) in GO Negotiation Request",
1005 			*msg.status);
1006 		if (dev && p2p->go_neg_peer == dev &&
1007 		    *msg.status == P2P_SC_FAIL_REJECTED_BY_USER) {
1008 			/*
1009 			 * This mechanism for using Status attribute in GO
1010 			 * Negotiation Request is not compliant with the P2P
1011 			 * specification, but some deployed devices use it to
1012 			 * indicate rejection of GO Negotiation in a case where
1013 			 * they have sent out GO Negotiation Response with
1014 			 * status 1. The P2P specification explicitly disallows
1015 			 * this. To avoid unnecessary interoperability issues
1016 			 * and extra frames, mark the pending negotiation as
1017 			 * failed and do not reply to this GO Negotiation
1018 			 * Request frame.
1019 			 */
1020 #ifdef CONFIG_P2P_CHR
1021 			wpa_supplicant_upload_chr_statistics_event(GO_NEG_PEER_REJECT_CNT);
1022 #ifdef CONFIG_LIBWPA_VENDOR
1023 			char buf[CHR_BUFFER_SIZE] = {0};
1024 			os_snprintf(buf, CHR_BUFFER_SIZE, "04:%serrCode=%d", WPA_EVENT_CHR_REPORT,
1025 				GO_NEGOTIATION_PEER_REJECT);
1026 			WpaEventReport("p2p0", WPA_EVENT_STA_NOTIFY, (void *)buf);
1027 #endif
1028 #endif
1029 			p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
1030 			p2p_go_neg_failed(p2p, *msg.status);
1031 			p2p_parse_free(&msg);
1032 			return;
1033 		}
1034 		goto fail;
1035 	}
1036 
1037 	if (dev == NULL)
1038 		dev = p2p_add_dev_from_go_neg_req(p2p, sa, &msg);
1039 	else if ((dev->flags & P2P_DEV_PROBE_REQ_ONLY) ||
1040 		  !(dev->flags & P2P_DEV_REPORTED))
1041 		p2p_add_dev_info(p2p, sa, dev, &msg);
1042 	else if (!dev->listen_freq && !dev->oper_freq) {
1043 		/*
1044 		 * This may happen if the peer entry was added based on PD
1045 		 * Request and no Probe Request/Response frame has been received
1046 		 * from this peer (or that information has timed out).
1047 		 */
1048 		p2p_dbg(p2p, "Update peer " MACSTR_SEC
1049 			" based on GO Neg Req since listen/oper freq not known",
1050 			MAC2STR_SEC(dev->info.p2p_device_addr));
1051 		p2p_add_dev_info(p2p, sa, dev, &msg);
1052 #ifdef HARMONY_P2P_CONNECTIVITY_PATCH
1053 	} else if (dev->info.group_capab != msg.capability[1]) {
1054 		p2p_add_dev_info(p2p, sa, dev, &msg);
1055 #endif /* HARMONY_P2P_CONNECTIVITY_PATCH */
1056 	}
1057 
1058 	if (dev)
1059 		p2p_update_peer_6ghz_capab(dev, &msg);
1060 
1061 	if (p2p->go_neg_peer && p2p->go_neg_peer == dev)
1062 		eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
1063 
1064 	if (dev && dev->flags & P2P_DEV_USER_REJECTED) {
1065 		p2p_dbg(p2p, "User has rejected this peer");
1066 		status = P2P_SC_FAIL_REJECTED_BY_USER;
1067 	} else if (dev == NULL ||
1068 		   (dev->wps_method == WPS_NOT_READY &&
1069 		    (p2p->authorized_oob_dev_pw_id == 0 ||
1070 		     p2p->authorized_oob_dev_pw_id !=
1071 		     msg.dev_password_id))) {
1072 		p2p_dbg(p2p, "Not ready for GO negotiation with " MACSTR_SEC,
1073 			MAC2STR_SEC(sa));
1074 
1075 #if defined(CONFIG_OPEN_HARMONY_PATCH) && defined(OPEN_HARMONY_MIRACAST_SINK_OPT)
1076 	if (dev != NULL) {
1077 		/*
1078 		 * If the intent value of the peer end off the screen is not the maximum value.
1079 		 * Set the local intent value to the maximum value,
1080 		 * and the purpose is to make the big scree try to do GO.
1081 		*/
1082 		u8 operating_channel = ((msg.operating_channel == NULL) ? 0 :
1083 			msg.operating_channel[HM_OPERATING_CHANNEL_POS]);
1084 		hm_p2p_update_peer_info(dev->info.p2p_device_addr,
1085 			HM_GO_NEG_REQ, operating_channel ,NULL);
1086 		hm_p2p_save_go_req_count(dev->info.p2p_device_addr, HM_GO_NEG_REQ, &msg);
1087 		hm_p2p_calculate_go_intent(p2p, &msg, dev->info.p2p_device_addr);
1088 	}
1089 #endif
1090 		status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
1091 		p2p->cfg->go_neg_req_rx(p2p->cfg->cb_ctx, sa,
1092 					msg.dev_password_id,
1093 					msg.go_intent ? (*msg.go_intent >> 1) :
1094 					0);
1095 	} else if (p2p->go_neg_peer && p2p->go_neg_peer != dev) {
1096 		p2p_dbg(p2p, "Already in Group Formation with another peer");
1097 		status = P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
1098 	} else {
1099 		int go;
1100 
1101 		if (!p2p->go_neg_peer) {
1102 			p2p_dbg(p2p, "Starting GO Negotiation with previously authorized peer");
1103 			if (!(dev->flags & P2P_DEV_FORCE_FREQ)) {
1104 				p2p_dbg(p2p, "Use default channel settings");
1105 				p2p->op_reg_class = p2p->cfg->op_reg_class;
1106 				p2p->op_channel = p2p->cfg->op_channel;
1107 				os_memcpy(&p2p->channels, &p2p->cfg->channels,
1108 					  sizeof(struct p2p_channels));
1109 			} else {
1110 				p2p_dbg(p2p, "Use previously configured forced channel settings");
1111 			}
1112 		}
1113 
1114 		dev->flags &= ~P2P_DEV_NOT_YET_READY;
1115 
1116 		if (!msg.go_intent) {
1117 			p2p_dbg(p2p, "No GO Intent attribute received");
1118 			goto fail;
1119 		}
1120 		if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) {
1121 			p2p_dbg(p2p, "Invalid GO Intent value (%u) received",
1122 				*msg.go_intent >> 1);
1123 			goto fail;
1124 		}
1125 
1126 		if (dev->go_neg_req_sent &&
1127 		    os_memcmp(sa, p2p->cfg->dev_addr, ETH_ALEN) > 0) {
1128 			p2p_dbg(p2p, "Do not reply since peer has higher address and GO Neg Request already sent");
1129 			p2p_parse_free(&msg);
1130 			return;
1131 		}
1132 
1133 		if (dev->go_neg_req_sent &&
1134 		    (dev->flags & P2P_DEV_PEER_WAITING_RESPONSE)) {
1135 			p2p_dbg(p2p,
1136 				"Do not reply since peer is waiting for us to start a new GO Negotiation and GO Neg Request already sent");
1137 			p2p_parse_free(&msg);
1138 			return;
1139 		}
1140 
1141 		go = p2p_go_det(p2p->go_intent, *msg.go_intent);
1142 		if (go < 0) {
1143 			p2p_dbg(p2p, "Incompatible GO Intent");
1144 			status = P2P_SC_FAIL_BOTH_GO_INTENT_15;
1145 			goto fail;
1146 		}
1147 
1148 		if (p2p_peer_channels(p2p, dev, msg.channel_list,
1149 				      msg.channel_list_len) < 0) {
1150 			p2p_dbg(p2p, "No common channels found");
1151 			status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
1152 			goto fail;
1153 		}
1154 
1155 		switch (msg.dev_password_id) {
1156 		case DEV_PW_REGISTRAR_SPECIFIED:
1157 			p2p_dbg(p2p, "PIN from peer Display");
1158 			if (dev->wps_method != WPS_PIN_KEYPAD) {
1159 				p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
1160 					p2p_wps_method_str(dev->wps_method));
1161 				status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1162 				goto fail;
1163 			}
1164 			break;
1165 		case DEV_PW_USER_SPECIFIED:
1166 			p2p_dbg(p2p, "Peer entered PIN on Keypad");
1167 			if (dev->wps_method != WPS_PIN_DISPLAY) {
1168 				p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
1169 					p2p_wps_method_str(dev->wps_method));
1170 				status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1171 				goto fail;
1172 			}
1173 			break;
1174 		case DEV_PW_PUSHBUTTON:
1175 			p2p_dbg(p2p, "Peer using pushbutton");
1176 			if (dev->wps_method != WPS_PBC) {
1177 				p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
1178 					p2p_wps_method_str(dev->wps_method));
1179 				status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1180 				goto fail;
1181 			}
1182 			break;
1183 		case DEV_PW_P2PS_DEFAULT:
1184 			p2p_dbg(p2p, "Peer using P2PS pin");
1185 			if (dev->wps_method != WPS_P2PS) {
1186 				p2p_dbg(p2p,
1187 					"We have wps_method=%s -> incompatible",
1188 					p2p_wps_method_str(dev->wps_method));
1189 				status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1190 				goto fail;
1191 			}
1192 			break;
1193 		default:
1194 			if (msg.dev_password_id &&
1195 			    msg.dev_password_id == dev->oob_pw_id) {
1196 				p2p_dbg(p2p, "Peer using NFC");
1197 				if (dev->wps_method != WPS_NFC) {
1198 					p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
1199 						p2p_wps_method_str(
1200 							dev->wps_method));
1201 					status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1202 					goto fail;
1203 				}
1204 				break;
1205 			}
1206 #ifdef CONFIG_WPS_NFC
1207 			if (p2p->authorized_oob_dev_pw_id &&
1208 			    msg.dev_password_id ==
1209 			    p2p->authorized_oob_dev_pw_id) {
1210 				p2p_dbg(p2p, "Using static handover with our device password from NFC Tag");
1211 				dev->wps_method = WPS_NFC;
1212 				dev->oob_pw_id = p2p->authorized_oob_dev_pw_id;
1213 				break;
1214 			}
1215 #endif /* CONFIG_WPS_NFC */
1216 			p2p_dbg(p2p, "Unsupported Device Password ID %d",
1217 				msg.dev_password_id);
1218 			status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1219 			goto fail;
1220 		}
1221 
1222 		if (go && p2p_go_select_channel(p2p, dev, &status) < 0)
1223 			goto fail;
1224 
1225 		dev->go_state = go ? LOCAL_GO : REMOTE_GO;
1226 		dev->oper_freq = p2p_channel_to_freq(msg.operating_channel[3],
1227 						     msg.operating_channel[4]);
1228 		p2p_dbg(p2p, "Peer operating channel preference: %d MHz",
1229 			dev->oper_freq);
1230 
1231 		/*
1232 		 * Use the driver preferred frequency list extension if
1233 		 * supported.
1234 		 */
1235 		p2p_check_pref_chan(p2p, go, dev, &msg);
1236 
1237 #ifdef CONFIG_OPEN_HARMONY_P2P_DFH_CONNECT
1238 	if (go) {
1239 		pvt_p2p_adjust_channel(p2p, dev);
1240 	}
1241 #endif
1242 
1243 		if (msg.config_timeout) {
1244 			dev->go_timeout = msg.config_timeout[0];
1245 			dev->client_timeout = msg.config_timeout[1];
1246 		}
1247 
1248 		p2p_dbg(p2p, "GO Negotiation with " MACSTR_SEC, MAC2STR_SEC(sa));
1249 		if (p2p->state != P2P_IDLE)
1250 			p2p_stop_find_for_freq(p2p, rx_freq);
1251 		p2p_set_state(p2p, P2P_GO_NEG);
1252 		p2p_clear_timeout(p2p);
1253 		dev->dialog_token = msg.dialog_token;
1254 		os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN);
1255 		p2p->go_neg_peer = dev;
1256 		eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
1257 		status = P2P_SC_SUCCESS;
1258 	}
1259 
1260 fail:
1261 	if (dev)
1262 		dev->status = status;
1263 	resp = p2p_build_go_neg_resp(p2p, dev, msg.dialog_token, status,
1264 				     !tie_breaker);
1265 	p2p_parse_free(&msg);
1266 	if (resp == NULL)
1267 		return;
1268 	p2p_info(p2p, "Sending GO Negotiation Response");
1269 
1270 #ifdef CONFIG_MIRACAST_SOURCE_OPT
1271 	p2p_dbg(p2p, "Use cfg channel for sending action frame Tx");
1272 	freq = p2p_channel_to_freq(p2p->cfg->reg_class,
1273 					p2p->cfg->channel);
1274 #else
1275 	if (rx_freq > 0)
1276 		freq = rx_freq;
1277 	else
1278 		freq = p2p_channel_to_freq(p2p->cfg->reg_class,
1279 					   p2p->cfg->channel);
1280 #endif
1281 	if (freq < 0) {
1282 		p2p_dbg(p2p, "Unknown regulatory class/channel");
1283 		wpabuf_free(resp);
1284 		return;
1285 	}
1286 	if (status == P2P_SC_SUCCESS) {
1287 		p2p->pending_action_state = P2P_PENDING_GO_NEG_RESPONSE;
1288 		dev->flags |= P2P_DEV_WAIT_GO_NEG_CONFIRM;
1289 		if (os_memcmp(sa, p2p->cfg->dev_addr, ETH_ALEN) < 0) {
1290 			/*
1291 			 * Peer has smaller address, so the GO Negotiation
1292 			 * Response from us is expected to complete
1293 			 * negotiation. Ignore a GO Negotiation Response from
1294 			 * the peer if it happens to be received after this
1295 			 * point due to a race condition in GO Negotiation
1296 			 * Request transmission and processing.
1297 			 */
1298 			dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
1299 		}
1300 	} else
1301 		p2p->pending_action_state =
1302 			P2P_PENDING_GO_NEG_RESPONSE_FAILURE;
1303 	if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr,
1304 			    p2p->cfg->dev_addr,
1305 			    wpabuf_head(resp), wpabuf_len(resp), 100) < 0) {
1306 		p2p_dbg(p2p, "Failed to send Action frame");
1307 	}
1308 
1309 	wpabuf_free(resp);
1310 }
1311 
1312 
p2p_build_go_neg_conf(struct p2p_data * p2p,struct p2p_device * peer,u8 dialog_token,u8 status,const u8 * resp_chan,int go)1313 static struct wpabuf * p2p_build_go_neg_conf(struct p2p_data *p2p,
1314 					     struct p2p_device *peer,
1315 					     u8 dialog_token, u8 status,
1316 					     const u8 *resp_chan, int go)
1317 {
1318 	struct wpabuf *buf;
1319 	u8 *len;
1320 	struct p2p_channels res;
1321 	u8 group_capab;
1322 	size_t extra = 0;
1323 	bool is_6ghz_capab;
1324 
1325 	p2p_dbg(p2p, "Building GO Negotiation Confirm");
1326 
1327 #ifdef CONFIG_WIFI_DISPLAY
1328 	if (p2p->wfd_ie_go_neg)
1329 		extra = wpabuf_len(p2p->wfd_ie_go_neg);
1330 #endif /* CONFIG_WIFI_DISPLAY */
1331 
1332 	if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_CONF])
1333 		extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_CONF]);
1334 
1335 	buf = wpabuf_alloc(1000 + extra);
1336 	if (buf == NULL)
1337 		return NULL;
1338 
1339 	p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_CONF, dialog_token);
1340 
1341 	len = p2p_buf_add_ie_hdr(buf);
1342 	p2p_buf_add_status(buf, status);
1343 	group_capab = 0;
1344 	if (peer->go_state == LOCAL_GO) {
1345 		if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
1346 			group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
1347 			if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
1348 				group_capab |=
1349 					P2P_GROUP_CAPAB_PERSISTENT_RECONN;
1350 		}
1351 		if (p2p->cross_connect)
1352 			group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
1353 		if (p2p->cfg->p2p_intra_bss)
1354 			group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
1355 	}
1356 	p2p_buf_add_capability(buf, p2p->dev_capab &
1357 			       ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY,
1358 			       group_capab);
1359 	if (go || resp_chan == NULL)
1360 		p2p_buf_add_operating_channel(buf, p2p->cfg->country,
1361 					      p2p->op_reg_class,
1362 					      p2p->op_channel);
1363 	else
1364 		p2p_buf_add_operating_channel(buf, (const char *) resp_chan,
1365 					      resp_chan[3], resp_chan[4]);
1366 	p2p_channels_intersect(&p2p->channels, &peer->channels, &res);
1367 	is_6ghz_capab = is_p2p_6ghz_capable(p2p) &&
1368 		p2p_is_peer_6ghz_capab(p2p, peer->info.p2p_device_addr);
1369 	p2p_buf_add_channel_list(buf, p2p->cfg->country, &res, is_6ghz_capab);
1370 	if (go) {
1371 		p2p_buf_add_group_id(buf, p2p->cfg->dev_addr, p2p->ssid,
1372 				     p2p->ssid_len);
1373 	}
1374 	p2p_buf_update_ie_hdr(buf, len);
1375 
1376 #ifdef CONFIG_WIFI_DISPLAY
1377 	if (p2p->wfd_ie_go_neg)
1378 		wpabuf_put_buf(buf, p2p->wfd_ie_go_neg);
1379 #endif /* CONFIG_WIFI_DISPLAY */
1380 
1381 	if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_CONF])
1382 		wpabuf_put_buf(buf, p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_CONF]);
1383 
1384 	return buf;
1385 }
1386 
1387 
p2p_process_go_neg_resp(struct p2p_data * p2p,const u8 * sa,const u8 * data,size_t len,int rx_freq)1388 void p2p_process_go_neg_resp(struct p2p_data *p2p, const u8 *sa,
1389 			     const u8 *data, size_t len, int rx_freq)
1390 {
1391 	struct p2p_device *dev;
1392 	int go = -1;
1393 	struct p2p_message msg;
1394 	u8 status = P2P_SC_SUCCESS;
1395 	int freq;
1396 
1397 	p2p_dbg(p2p, "Received GO Negotiation Response from " MACSTR_SEC
1398 		" (freq=%d)", MAC2STR_SEC(sa), rx_freq);
1399 	dev = p2p_get_device(p2p, sa);
1400 	if (dev == NULL || dev->wps_method == WPS_NOT_READY ||
1401 	    dev != p2p->go_neg_peer) {
1402 		p2p_dbg(p2p, "Not ready for GO negotiation with " MACSTR_SEC,
1403 			MAC2STR_SEC(sa));
1404 		return;
1405 	}
1406 
1407 #if defined(CONFIG_OPEN_HARMONY_PATCH) && defined(OPEN_HARMONY_MIRACAST_SINK_OPT)
1408 	if (dev->flags & P2P_DEV_USER_REJECTED) {
1409 		p2p_dbg(p2p, "user has rejected, do not go on go neg");
1410 		p2p_go_neg_failed(p2p, -1);
1411 		return;
1412 	}
1413 #endif
1414 
1415 	if (p2p_parse(data, len, &msg))
1416 		return;
1417 
1418 	if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE)) {
1419 		p2p_dbg(p2p, "Was not expecting GO Negotiation Response - ignore");
1420 		p2p_parse_free(&msg);
1421 		return;
1422 	}
1423 	dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
1424 	p2p_update_peer_6ghz_capab(dev, &msg);
1425 
1426 	if (msg.dialog_token != dev->dialog_token) {
1427 		p2p_dbg(p2p, "Unexpected Dialog Token %u (expected %u)",
1428 			msg.dialog_token, dev->dialog_token);
1429 		p2p_parse_free(&msg);
1430 		return;
1431 	}
1432 
1433 	if (!msg.status) {
1434 		p2p_dbg(p2p, "No Status attribute received");
1435 		status = P2P_SC_FAIL_INVALID_PARAMS;
1436 		goto fail;
1437 	}
1438 	if (*msg.status) {
1439 		p2p_info(p2p, "GO Negotiation rejected: status %d", *msg.status);
1440 		dev->go_neg_req_sent = 0;
1441 		if (*msg.status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
1442 			p2p_dbg(p2p, "Wait for the peer to become ready for GO Negotiation");
1443 			dev->flags |= P2P_DEV_NOT_YET_READY;
1444 			eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p,
1445 					     NULL);
1446 			eloop_register_timeout(55, 0, p2p_go_neg_wait_timeout,
1447 					       p2p, NULL);
1448 			if (p2p->state == P2P_CONNECT_LISTEN)
1449 				p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT);
1450 			else
1451 				p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
1452 			p2p_set_timeout(p2p, 0, 0);
1453 		} else {
1454 			p2p_dbg(p2p, "Stop GO Negotiation attempt");
1455 			p2p_go_neg_failed(p2p, *msg.status);
1456 		}
1457 		p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
1458 		p2p_parse_free(&msg);
1459 		return;
1460 	}
1461 
1462 	if (!msg.capability) {
1463 		p2p_dbg(p2p, "Mandatory Capability attribute missing from GO Negotiation Response");
1464 #ifdef CONFIG_P2P_STRICT
1465 		status = P2P_SC_FAIL_INVALID_PARAMS;
1466 		goto fail;
1467 #endif /* CONFIG_P2P_STRICT */
1468 	}
1469 
1470 	if (!msg.p2p_device_info) {
1471 		p2p_dbg(p2p, "Mandatory P2P Device Info attribute missing from GO Negotiation Response");
1472 #ifdef CONFIG_P2P_STRICT
1473 		status = P2P_SC_FAIL_INVALID_PARAMS;
1474 		goto fail;
1475 #endif /* CONFIG_P2P_STRICT */
1476 	}
1477 
1478 	if (!msg.intended_addr) {
1479 		p2p_dbg(p2p, "No Intended P2P Interface Address attribute received");
1480 		status = P2P_SC_FAIL_INVALID_PARAMS;
1481 		goto fail;
1482 	}
1483 
1484 	if (!msg.go_intent) {
1485 		p2p_dbg(p2p, "No GO Intent attribute received");
1486 		status = P2P_SC_FAIL_INVALID_PARAMS;
1487 		goto fail;
1488 	}
1489 	if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) {
1490 		p2p_dbg(p2p, "Invalid GO Intent value (%u) received",
1491 			*msg.go_intent >> 1);
1492 		status = P2P_SC_FAIL_INVALID_PARAMS;
1493 		goto fail;
1494 	}
1495 
1496 	go = p2p_go_det(p2p->go_intent, *msg.go_intent);
1497 	if (go < 0) {
1498 		p2p_dbg(p2p, "Incompatible GO Intent");
1499 		status = P2P_SC_FAIL_INCOMPATIBLE_PARAMS;
1500 		goto fail;
1501 	}
1502 
1503 	if (!go && msg.group_id) {
1504 		/* Store SSID for Provisioning step */
1505 		p2p->ssid_len = msg.group_id_len - ETH_ALEN;
1506 		os_memcpy(p2p->ssid, msg.group_id + ETH_ALEN, p2p->ssid_len);
1507 	} else if (!go) {
1508 		p2p_dbg(p2p, "Mandatory P2P Group ID attribute missing from GO Negotiation Response");
1509 		p2p->ssid_len = 0;
1510 		status = P2P_SC_FAIL_INVALID_PARAMS;
1511 		goto fail;
1512 	}
1513 
1514 	if (!msg.config_timeout) {
1515 		p2p_dbg(p2p, "Mandatory Configuration Timeout attribute missing from GO Negotiation Response");
1516 #ifdef CONFIG_P2P_STRICT
1517 		status = P2P_SC_FAIL_INVALID_PARAMS;
1518 		goto fail;
1519 #endif /* CONFIG_P2P_STRICT */
1520 	} else {
1521 		dev->go_timeout = msg.config_timeout[0];
1522 		dev->client_timeout = msg.config_timeout[1];
1523 	}
1524 
1525 	if (msg.wfd_subelems) {
1526 		wpabuf_free(dev->info.wfd_subelems);
1527 		dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
1528 	}
1529 
1530 	if (!msg.operating_channel && !go) {
1531 		/*
1532 		 * Note: P2P Client may omit Operating Channel attribute to
1533 		 * indicate it does not have a preference.
1534 		 */
1535 		p2p_dbg(p2p, "No Operating Channel attribute received");
1536 		status = P2P_SC_FAIL_INVALID_PARAMS;
1537 		goto fail;
1538 	}
1539 	if (!msg.channel_list) {
1540 		p2p_dbg(p2p, "No Channel List attribute received");
1541 		status = P2P_SC_FAIL_INVALID_PARAMS;
1542 		goto fail;
1543 	}
1544 
1545 	if (p2p_peer_channels(p2p, dev, msg.channel_list,
1546 			      msg.channel_list_len) < 0) {
1547 		p2p_dbg(p2p, "No common channels found");
1548 		status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
1549 		goto fail;
1550 	}
1551 
1552 	if (msg.operating_channel) {
1553 		dev->oper_freq = p2p_channel_to_freq(msg.operating_channel[3],
1554 						     msg.operating_channel[4]);
1555 		p2p_dbg(p2p, "Peer operating channel preference: %d MHz",
1556 			dev->oper_freq);
1557 	} else
1558 		dev->oper_freq = 0;
1559 
1560 	switch (msg.dev_password_id) {
1561 	case DEV_PW_REGISTRAR_SPECIFIED:
1562 		p2p_dbg(p2p, "PIN from peer Display");
1563 		if (dev->wps_method != WPS_PIN_KEYPAD) {
1564 			p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
1565 				p2p_wps_method_str(dev->wps_method));
1566 			status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1567 			goto fail;
1568 		}
1569 		break;
1570 	case DEV_PW_USER_SPECIFIED:
1571 		p2p_dbg(p2p, "Peer entered PIN on Keypad");
1572 		if (dev->wps_method != WPS_PIN_DISPLAY) {
1573 			p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
1574 				p2p_wps_method_str(dev->wps_method));
1575 			status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1576 			goto fail;
1577 		}
1578 		break;
1579 	case DEV_PW_PUSHBUTTON:
1580 		p2p_dbg(p2p, "Peer using pushbutton");
1581 		if (dev->wps_method != WPS_PBC) {
1582 			p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
1583 				p2p_wps_method_str(dev->wps_method));
1584 			status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1585 			goto fail;
1586 		}
1587 		break;
1588 	case DEV_PW_P2PS_DEFAULT:
1589 		p2p_dbg(p2p, "P2P: Peer using P2PS default pin");
1590 		if (dev->wps_method != WPS_P2PS) {
1591 			p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
1592 				p2p_wps_method_str(dev->wps_method));
1593 			status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1594 			goto fail;
1595 		}
1596 		break;
1597 	default:
1598 		if (msg.dev_password_id &&
1599 		    msg.dev_password_id == dev->oob_pw_id) {
1600 			p2p_dbg(p2p, "Peer using NFC");
1601 			if (dev->wps_method != WPS_NFC) {
1602 				p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
1603 					p2p_wps_method_str(dev->wps_method));
1604 				status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1605 				goto fail;
1606 			}
1607 			break;
1608 		}
1609 		p2p_dbg(p2p, "Unsupported Device Password ID %d",
1610 			msg.dev_password_id);
1611 		status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1612 		goto fail;
1613 	}
1614 
1615 #if defined(CONFIG_OPEN_HARMONY_PATCH) && defined(OPEN_HARMONY_MIRACAST_SINK_OPT)
1616 	u8 operating_channel = ((msg.operating_channel == NULL) ? 0 : msg.operating_channel[HM_OPERATING_CHANNEL_POS]);
1617 	hm_p2p_update_peer_info(dev->info.p2p_device_addr,
1618 		HM_GO_NEG_RESP, operating_channel, dev);
1619 #endif
1620 
1621 	if (go && p2p_go_select_channel(p2p, dev, &status) < 0)
1622 		goto fail;
1623 
1624 	/*
1625 	 * Use the driver preferred frequency list extension if local device is
1626 	 * GO.
1627 	 */
1628 	if (go)
1629 		p2p_check_pref_chan(p2p, go, dev, &msg);
1630 
1631 #ifdef CONFIG_OPEN_HARMONY_P2P_DFH_CONNECT
1632 	if (go) {
1633 		pvt_p2p_adjust_channel(p2p, dev);
1634 	}
1635 #endif
1636 
1637 	p2p_set_state(p2p, P2P_GO_NEG);
1638 	p2p_clear_timeout(p2p);
1639 
1640 	p2p_dbg(p2p, "GO Negotiation with " MACSTR_SEC, MAC2STR_SEC(sa));
1641 	os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN);
1642 
1643 fail:
1644 	/* Store GO Negotiation Confirmation to allow retransmission */
1645 	wpabuf_free(dev->go_neg_conf);
1646 	dev->go_neg_conf = p2p_build_go_neg_conf(p2p, dev, msg.dialog_token,
1647 						 status, msg.operating_channel,
1648 						 go);
1649 	p2p_parse_free(&msg);
1650 	if (dev->go_neg_conf == NULL)
1651 		return;
1652 	p2p_dbg(p2p, "Sending GO Negotiation Confirm");
1653 	if (status == P2P_SC_SUCCESS) {
1654 		p2p->pending_action_state = P2P_PENDING_GO_NEG_CONFIRM;
1655 		dev->go_state = go ? LOCAL_GO : REMOTE_GO;
1656 	} else
1657 		p2p->pending_action_state = P2P_NO_PENDING_ACTION;
1658 	if (rx_freq > 0)
1659 		freq = rx_freq;
1660 	else
1661 		freq = dev->listen_freq;
1662 
1663 	dev->go_neg_conf_freq = freq;
1664 	dev->go_neg_conf_sent = 0;
1665 
1666 	if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr, sa,
1667 			    wpabuf_head(dev->go_neg_conf),
1668 			    wpabuf_len(dev->go_neg_conf), 50) < 0) {
1669 		p2p_dbg(p2p, "Failed to send Action frame");
1670 		p2p_go_neg_failed(p2p, -1);
1671 		p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
1672 	} else
1673 		dev->go_neg_conf_sent++;
1674 	if (status != P2P_SC_SUCCESS) {
1675 		p2p_dbg(p2p, "GO Negotiation failed");
1676 		p2p_go_neg_failed(p2p, status);
1677 	}
1678 }
1679 
1680 
p2p_process_go_neg_conf(struct p2p_data * p2p,const u8 * sa,const u8 * data,size_t len)1681 void p2p_process_go_neg_conf(struct p2p_data *p2p, const u8 *sa,
1682 			     const u8 *data, size_t len)
1683 {
1684 	struct p2p_device *dev;
1685 	struct p2p_message msg;
1686 
1687 	p2p_info(p2p, "Received GO Negotiation Confirm from " MACSTR_SEC,
1688 		MAC2STR_SEC(sa));
1689 	dev = p2p_get_device(p2p, sa);
1690 	if (dev == NULL || dev->wps_method == WPS_NOT_READY ||
1691 	    dev != p2p->go_neg_peer) {
1692 		p2p_dbg(p2p, "Not ready for GO negotiation with " MACSTR_SEC,
1693 			MAC2STR_SEC(sa));
1694 		return;
1695 	}
1696 
1697 	if (p2p->pending_action_state == P2P_PENDING_GO_NEG_RESPONSE) {
1698 		p2p_dbg(p2p, "Stopped waiting for TX status on GO Negotiation Response since we already received Confirmation");
1699 		p2p->pending_action_state = P2P_NO_PENDING_ACTION;
1700 	}
1701 
1702 	if (p2p_parse(data, len, &msg))
1703 		return;
1704 
1705 	if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
1706 		p2p_dbg(p2p, "Was not expecting GO Negotiation Confirm - ignore");
1707 		p2p_parse_free(&msg);
1708 		return;
1709 	}
1710 	dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
1711 	p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
1712 
1713 	if (msg.dialog_token != dev->dialog_token) {
1714 		p2p_dbg(p2p, "Unexpected Dialog Token %u (expected %u)",
1715 			msg.dialog_token, dev->dialog_token);
1716 		p2p_parse_free(&msg);
1717 		return;
1718 	}
1719 
1720 	if (!msg.status) {
1721 		p2p_dbg(p2p, "No Status attribute received");
1722 		p2p_parse_free(&msg);
1723 		return;
1724 	}
1725 	if (*msg.status) {
1726 		p2p_warning(p2p, "GO Negotiation rejected: status %d", *msg.status);
1727 		p2p_go_neg_failed(p2p, *msg.status);
1728 		p2p_parse_free(&msg);
1729 		return;
1730 	}
1731 
1732 	p2p_update_peer_6ghz_capab(dev, &msg);
1733 
1734 	if (dev->go_state == REMOTE_GO && msg.group_id) {
1735 		/* Store SSID for Provisioning step */
1736 		p2p->ssid_len = msg.group_id_len - ETH_ALEN;
1737 		os_memcpy(p2p->ssid, msg.group_id + ETH_ALEN, p2p->ssid_len);
1738 	} else if (dev->go_state == REMOTE_GO) {
1739 		p2p_dbg(p2p, "Mandatory P2P Group ID attribute missing from GO Negotiation Confirmation");
1740 		p2p->ssid_len = 0;
1741 		p2p_go_neg_failed(p2p, P2P_SC_FAIL_INVALID_PARAMS);
1742 		p2p_parse_free(&msg);
1743 		return;
1744 	}
1745 
1746 	if (!msg.operating_channel) {
1747 		p2p_dbg(p2p, "Mandatory Operating Channel attribute missing from GO Negotiation Confirmation");
1748 #ifdef CONFIG_P2P_STRICT
1749 		p2p_parse_free(&msg);
1750 		return;
1751 #endif /* CONFIG_P2P_STRICT */
1752 	} else if (dev->go_state == REMOTE_GO) {
1753 		int oper_freq = p2p_channel_to_freq(msg.operating_channel[3],
1754 						    msg.operating_channel[4]);
1755 		if (oper_freq != dev->oper_freq) {
1756 			p2p_dbg(p2p, "Updated peer (GO) operating channel preference from %d MHz to %d MHz",
1757 				dev->oper_freq, oper_freq);
1758 			dev->oper_freq = oper_freq;
1759 		}
1760 	}
1761 
1762 	if (!msg.channel_list) {
1763 		p2p_dbg(p2p, "Mandatory Operating Channel attribute missing from GO Negotiation Confirmation");
1764 #ifdef CONFIG_P2P_STRICT
1765 		p2p_parse_free(&msg);
1766 		return;
1767 #endif /* CONFIG_P2P_STRICT */
1768 	}
1769 
1770 	p2p_parse_free(&msg);
1771 
1772 	if (dev->go_state == UNKNOWN_GO) {
1773 		/*
1774 		 * This should not happen since GO negotiation has already
1775 		 * been completed.
1776 		 */
1777 		p2p_dbg(p2p, "Unexpected GO Neg state - do not know which end becomes GO");
1778 		return;
1779 	}
1780 
1781 	/*
1782 	 * The peer could have missed our ctrl::ack frame for GO Negotiation
1783 	 * Confirm and continue retransmitting the frame. To reduce the
1784 	 * likelihood of the peer not getting successful TX status for the
1785 	 * GO Negotiation Confirm frame, wait a short time here before starting
1786 	 * the group so that we will remain on the current channel to
1787 	 * acknowledge any possible retransmission from the peer.
1788 	 */
1789 	p2p_dbg(p2p, "20 ms wait on current channel before starting group");
1790 	os_sleep(0, 20000);
1791 
1792 	p2p_go_complete(p2p, dev);
1793 }
1794