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