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