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