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