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