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