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