xref: /freebsd/contrib/wpa/src/p2p/p2p_go_neg.c (revision 88f578841fd49ff796b62a8d3531bd73afd1a88e)
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 	/* WPS IE with Device Password ID attribute */
189 	pw_id = p2p_wps_method_pw_id(peer->wps_method);
190 	if (peer->oob_pw_id)
191 		pw_id = peer->oob_pw_id;
192 	if (p2p_build_wps_ie(p2p, buf, pw_id, 0) < 0) {
193 		p2p_dbg(p2p, "Failed to build WPS IE for GO Negotiation Request");
194 		wpabuf_free(buf);
195 		return NULL;
196 	}
197 
198 #ifdef CONFIG_WIFI_DISPLAY
199 	if (p2p->wfd_ie_go_neg)
200 		wpabuf_put_buf(buf, p2p->wfd_ie_go_neg);
201 #endif /* CONFIG_WIFI_DISPLAY */
202 
203 	if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_REQ])
204 		wpabuf_put_buf(buf, p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_REQ]);
205 
206 	return buf;
207 }
208 
209 
210 int p2p_connect_send(struct p2p_data *p2p, struct p2p_device *dev)
211 {
212 	struct wpabuf *req;
213 	int freq;
214 
215 	if (dev->flags & P2P_DEV_PD_BEFORE_GO_NEG) {
216 		u16 config_method;
217 		p2p_dbg(p2p, "Use PD-before-GO-Neg workaround for " MACSTR,
218 			MAC2STR(dev->info.p2p_device_addr));
219 		if (dev->wps_method == WPS_PIN_DISPLAY)
220 			config_method = WPS_CONFIG_KEYPAD;
221 		else if (dev->wps_method == WPS_PIN_KEYPAD)
222 			config_method = WPS_CONFIG_DISPLAY;
223 		else if (dev->wps_method == WPS_PBC)
224 			config_method = WPS_CONFIG_PUSHBUTTON;
225 		else if (dev->wps_method == WPS_P2PS)
226 			config_method = WPS_CONFIG_P2PS;
227 		else
228 			return -1;
229 		return p2p_prov_disc_req(p2p, dev->info.p2p_device_addr,
230 					 NULL, config_method, 0, 0, 1);
231 	}
232 
233 	freq = dev->listen_freq > 0 ? dev->listen_freq : dev->oper_freq;
234 	if (dev->oob_go_neg_freq > 0)
235 		freq = dev->oob_go_neg_freq;
236 	if (freq <= 0) {
237 		p2p_dbg(p2p, "No Listen/Operating frequency known for the peer "
238 			MACSTR " to send GO Negotiation Request",
239 			MAC2STR(dev->info.p2p_device_addr));
240 		return -1;
241 	}
242 
243 	req = p2p_build_go_neg_req(p2p, dev);
244 	if (req == NULL)
245 		return -1;
246 	p2p_dbg(p2p, "Sending GO Negotiation Request");
247 	p2p_set_state(p2p, P2P_CONNECT);
248 	p2p->pending_action_state = P2P_PENDING_GO_NEG_REQUEST;
249 	p2p->go_neg_peer = dev;
250 	eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
251 	dev->flags |= P2P_DEV_WAIT_GO_NEG_RESPONSE;
252 	dev->connect_reqs++;
253 	if (p2p_send_action(p2p, freq, dev->info.p2p_device_addr,
254 			    p2p->cfg->dev_addr, dev->info.p2p_device_addr,
255 			    wpabuf_head(req), wpabuf_len(req), 500) < 0) {
256 		p2p_dbg(p2p, "Failed to send Action frame");
257 		/* Use P2P find to recover and retry */
258 		p2p_set_timeout(p2p, 0, 0);
259 	} else
260 		dev->go_neg_req_sent++;
261 
262 	wpabuf_free(req);
263 
264 	return 0;
265 }
266 
267 
268 static struct wpabuf * p2p_build_go_neg_resp(struct p2p_data *p2p,
269 					     struct p2p_device *peer,
270 					     u8 dialog_token, u8 status,
271 					     u8 tie_breaker)
272 {
273 	struct wpabuf *buf;
274 	u8 *len;
275 	u8 group_capab;
276 	size_t extra = 0;
277 	u16 pw_id;
278 
279 	p2p_dbg(p2p, "Building GO Negotiation Response");
280 
281 #ifdef CONFIG_WIFI_DISPLAY
282 	if (p2p->wfd_ie_go_neg)
283 		extra = wpabuf_len(p2p->wfd_ie_go_neg);
284 #endif /* CONFIG_WIFI_DISPLAY */
285 
286 	if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_RESP])
287 		extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_RESP]);
288 
289 	buf = wpabuf_alloc(1000 + extra);
290 	if (buf == NULL)
291 		return NULL;
292 
293 	p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_RESP, dialog_token);
294 
295 	len = p2p_buf_add_ie_hdr(buf);
296 	p2p_buf_add_status(buf, status);
297 	group_capab = 0;
298 	if (peer && peer->go_state == LOCAL_GO) {
299 		if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
300 			group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
301 			if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
302 				group_capab |=
303 					P2P_GROUP_CAPAB_PERSISTENT_RECONN;
304 		}
305 		if (p2p->cross_connect)
306 			group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
307 		if (p2p->cfg->p2p_intra_bss)
308 			group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
309 	}
310 	p2p_buf_add_capability(buf, p2p->dev_capab &
311 			       ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY,
312 			       group_capab);
313 	p2p_buf_add_go_intent(buf, (p2p->go_intent << 1) | tie_breaker);
314 	p2p_buf_add_config_timeout(buf, p2p->go_timeout, p2p->client_timeout);
315 	if (peer && peer->go_state == REMOTE_GO) {
316 		p2p_dbg(p2p, "Omit Operating Channel attribute");
317 	} else {
318 		p2p_buf_add_operating_channel(buf, p2p->cfg->country,
319 					      p2p->op_reg_class,
320 					      p2p->op_channel);
321 	}
322 	p2p_buf_add_intended_addr(buf, p2p->intended_addr);
323 	if (status || peer == NULL) {
324 		p2p_buf_add_channel_list(buf, p2p->cfg->country,
325 					 &p2p->channels);
326 	} else if (peer->go_state == REMOTE_GO) {
327 		p2p_buf_add_channel_list(buf, p2p->cfg->country,
328 					 &p2p->channels);
329 	} else {
330 		struct p2p_channels res;
331 		p2p_channels_intersect(&p2p->channels, &peer->channels,
332 				       &res);
333 		p2p_buf_add_channel_list(buf, p2p->cfg->country, &res);
334 	}
335 	p2p_buf_add_device_info(buf, p2p, peer);
336 	if (peer && peer->go_state == LOCAL_GO) {
337 		p2p_buf_add_group_id(buf, p2p->cfg->dev_addr, p2p->ssid,
338 				     p2p->ssid_len);
339 	}
340 	p2p_buf_update_ie_hdr(buf, len);
341 
342 	/* WPS IE with Device Password ID attribute */
343 	pw_id = p2p_wps_method_pw_id(peer ? peer->wps_method : WPS_NOT_READY);
344 	if (peer && peer->oob_pw_id)
345 		pw_id = peer->oob_pw_id;
346 	if (p2p_build_wps_ie(p2p, buf, pw_id, 0) < 0) {
347 		p2p_dbg(p2p, "Failed to build WPS IE for GO Negotiation Response");
348 		wpabuf_free(buf);
349 		return NULL;
350 	}
351 
352 #ifdef CONFIG_WIFI_DISPLAY
353 	if (p2p->wfd_ie_go_neg)
354 		wpabuf_put_buf(buf, p2p->wfd_ie_go_neg);
355 #endif /* CONFIG_WIFI_DISPLAY */
356 
357 	if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_RESP])
358 		wpabuf_put_buf(buf, p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_RESP]);
359 
360 	return buf;
361 }
362 
363 
364 /**
365  * p2p_reselect_channel - Re-select operating channel based on peer information
366  * @p2p: P2P module context from p2p_init()
367  * @intersection: Support channel list intersection from local and peer
368  *
369  * This function is used to re-select the best channel after having received
370  * information from the peer to allow supported channel lists to be intersected.
371  * This can be used to improve initial channel selection done in
372  * p2p_prepare_channel() prior to the start of GO Negotiation. In addition, this
373  * can be used for Invitation case.
374  */
375 void p2p_reselect_channel(struct p2p_data *p2p,
376 			  struct p2p_channels *intersection)
377 {
378 	struct p2p_reg_class *cl;
379 	int freq;
380 	u8 op_reg_class, op_channel;
381 	unsigned int i;
382 	const int op_classes_5ghz[] = { 124, 115, 0 };
383 	const int op_classes_ht40[] = { 126, 127, 116, 117, 0 };
384 	const int op_classes_vht[] = { 128, 0 };
385 
386 	if (p2p->own_freq_preference > 0 &&
387 	    p2p_freq_to_channel(p2p->own_freq_preference,
388 				&op_reg_class, &op_channel) == 0 &&
389 	    p2p_channels_includes(intersection, op_reg_class, op_channel)) {
390 		p2p_dbg(p2p, "Pick own channel preference (reg_class %u channel %u) from intersection",
391 			op_reg_class, op_channel);
392 		p2p->op_reg_class = op_reg_class;
393 		p2p->op_channel = op_channel;
394 		return;
395 	}
396 
397 	if (p2p->best_freq_overall > 0 &&
398 	    p2p_freq_to_channel(p2p->best_freq_overall,
399 				&op_reg_class, &op_channel) == 0 &&
400 	    p2p_channels_includes(intersection, op_reg_class, op_channel)) {
401 		p2p_dbg(p2p, "Pick best overall channel (reg_class %u channel %u) from intersection",
402 			op_reg_class, op_channel);
403 		p2p->op_reg_class = op_reg_class;
404 		p2p->op_channel = op_channel;
405 		return;
406 	}
407 
408 	/* First, try to pick the best channel from another band */
409 	freq = p2p_channel_to_freq(p2p->op_reg_class, p2p->op_channel);
410 	if (freq >= 2400 && freq < 2500 && p2p->best_freq_5 > 0 &&
411 	    !p2p_channels_includes(intersection, p2p->op_reg_class,
412 				   p2p->op_channel) &&
413 	    p2p_freq_to_channel(p2p->best_freq_5,
414 				&op_reg_class, &op_channel) == 0 &&
415 	    p2p_channels_includes(intersection, op_reg_class, op_channel)) {
416 		p2p_dbg(p2p, "Pick best 5 GHz channel (reg_class %u channel %u) from intersection",
417 			op_reg_class, op_channel);
418 		p2p->op_reg_class = op_reg_class;
419 		p2p->op_channel = op_channel;
420 		return;
421 	}
422 
423 	if (freq >= 4900 && freq < 6000 && p2p->best_freq_24 > 0 &&
424 	    !p2p_channels_includes(intersection, p2p->op_reg_class,
425 				   p2p->op_channel) &&
426 	    p2p_freq_to_channel(p2p->best_freq_24,
427 				&op_reg_class, &op_channel) == 0 &&
428 	    p2p_channels_includes(intersection, op_reg_class, op_channel)) {
429 		p2p_dbg(p2p, "Pick best 2.4 GHz channel (reg_class %u channel %u) from intersection",
430 			op_reg_class, op_channel);
431 		p2p->op_reg_class = op_reg_class;
432 		p2p->op_channel = op_channel;
433 		return;
434 	}
435 
436 	/* Select channel with highest preference if the peer supports it */
437 	for (i = 0; p2p->cfg->pref_chan && i < p2p->cfg->num_pref_chan; i++) {
438 		if (p2p_channels_includes(intersection,
439 					  p2p->cfg->pref_chan[i].op_class,
440 					  p2p->cfg->pref_chan[i].chan)) {
441 			p2p->op_reg_class = p2p->cfg->pref_chan[i].op_class;
442 			p2p->op_channel = p2p->cfg->pref_chan[i].chan;
443 			p2p_dbg(p2p, "Pick highest preferred channel (op_class %u channel %u) from intersection",
444 				p2p->op_reg_class, p2p->op_channel);
445 			return;
446 		}
447 	}
448 
449 	/* Try a channel where we might be able to use VHT */
450 	if (p2p_channel_select(intersection, op_classes_vht,
451 			       &p2p->op_reg_class, &p2p->op_channel) == 0) {
452 		p2p_dbg(p2p, "Pick possible VHT channel (op_class %u channel %u) from intersection",
453 			p2p->op_reg_class, p2p->op_channel);
454 		return;
455 	}
456 
457 	/* Try a channel where we might be able to use HT40 */
458 	if (p2p_channel_select(intersection, op_classes_ht40,
459 			       &p2p->op_reg_class, &p2p->op_channel) == 0) {
460 		p2p_dbg(p2p, "Pick possible HT40 channel (op_class %u channel %u) from intersection",
461 			p2p->op_reg_class, p2p->op_channel);
462 		return;
463 	}
464 
465 	/* Prefer a 5 GHz channel */
466 	if (p2p_channel_select(intersection, op_classes_5ghz,
467 			       &p2p->op_reg_class, &p2p->op_channel) == 0) {
468 		p2p_dbg(p2p, "Pick possible 5 GHz channel (op_class %u channel %u) from intersection",
469 			p2p->op_reg_class, p2p->op_channel);
470 		return;
471 	}
472 
473 	/*
474 	 * Try to see if the original channel is in the intersection. If
475 	 * so, no need to change anything, as it already contains some
476 	 * randomness.
477 	 */
478 	if (p2p_channels_includes(intersection, p2p->op_reg_class,
479 				  p2p->op_channel)) {
480 		p2p_dbg(p2p, "Using original operating class and channel (op_class %u channel %u) from intersection",
481 			p2p->op_reg_class, p2p->op_channel);
482 		return;
483 	}
484 
485 	/*
486 	 * Fall back to whatever is included in the channel intersection since
487 	 * no better options seems to be available.
488 	 */
489 	cl = &intersection->reg_class[0];
490 	p2p_dbg(p2p, "Pick another channel (reg_class %u channel %u) from intersection",
491 		cl->reg_class, cl->channel[0]);
492 	p2p->op_reg_class = cl->reg_class;
493 	p2p->op_channel = cl->channel[0];
494 }
495 
496 
497 int p2p_go_select_channel(struct p2p_data *p2p, struct p2p_device *dev,
498 			  u8 *status)
499 {
500 	struct p2p_channels tmp, intersection;
501 
502 	p2p_channels_dump(p2p, "own channels", &p2p->channels);
503 	p2p_channels_dump(p2p, "peer channels", &dev->channels);
504 	p2p_channels_intersect(&p2p->channels, &dev->channels, &tmp);
505 	p2p_channels_dump(p2p, "intersection", &tmp);
506 	p2p_channels_remove_freqs(&tmp, &p2p->no_go_freq);
507 	p2p_channels_dump(p2p, "intersection after no-GO removal", &tmp);
508 	p2p_channels_intersect(&tmp, &p2p->cfg->channels, &intersection);
509 	p2p_channels_dump(p2p, "intersection with local channel list",
510 			  &intersection);
511 	if (intersection.reg_classes == 0 ||
512 	    intersection.reg_class[0].channels == 0) {
513 		*status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
514 		p2p_dbg(p2p, "No common channels found");
515 		return -1;
516 	}
517 
518 	if (!p2p_channels_includes(&intersection, p2p->op_reg_class,
519 				   p2p->op_channel)) {
520 		if (dev->flags & P2P_DEV_FORCE_FREQ) {
521 			*status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
522 			p2p_dbg(p2p, "Peer does not support the forced channel");
523 			return -1;
524 		}
525 
526 		p2p_dbg(p2p, "Selected operating channel (op_class %u channel %u) not acceptable to the peer",
527 			p2p->op_reg_class, p2p->op_channel);
528 		p2p_reselect_channel(p2p, &intersection);
529 	} else if (!(dev->flags & P2P_DEV_FORCE_FREQ) &&
530 		   !p2p->cfg->cfg_op_channel) {
531 		p2p_dbg(p2p, "Try to optimize channel selection with peer information received; previously selected op_class %u channel %u",
532 			p2p->op_reg_class, p2p->op_channel);
533 		p2p_reselect_channel(p2p, &intersection);
534 	}
535 
536 	if (!p2p->ssid_set) {
537 		p2p_build_ssid(p2p, p2p->ssid, &p2p->ssid_len);
538 		p2p->ssid_set = 1;
539 	}
540 
541 	return 0;
542 }
543 
544 
545 void p2p_process_go_neg_req(struct p2p_data *p2p, const u8 *sa,
546 			    const u8 *data, size_t len, int rx_freq)
547 {
548 	struct p2p_device *dev = NULL;
549 	struct wpabuf *resp;
550 	struct p2p_message msg;
551 	u8 status = P2P_SC_FAIL_INVALID_PARAMS;
552 	int tie_breaker = 0;
553 	int freq;
554 
555 	p2p_dbg(p2p, "Received GO Negotiation Request from " MACSTR "(freq=%d)",
556 		MAC2STR(sa), rx_freq);
557 
558 	if (p2p_parse(data, len, &msg))
559 		return;
560 
561 	if (!msg.capability) {
562 		p2p_dbg(p2p, "Mandatory Capability attribute missing from GO Negotiation Request");
563 #ifdef CONFIG_P2P_STRICT
564 		goto fail;
565 #endif /* CONFIG_P2P_STRICT */
566 	}
567 
568 	if (msg.go_intent)
569 		tie_breaker = *msg.go_intent & 0x01;
570 	else {
571 		p2p_dbg(p2p, "Mandatory GO Intent attribute missing from GO Negotiation Request");
572 #ifdef CONFIG_P2P_STRICT
573 		goto fail;
574 #endif /* CONFIG_P2P_STRICT */
575 	}
576 
577 	if (!msg.config_timeout) {
578 		p2p_dbg(p2p, "Mandatory Configuration Timeout attribute missing from GO Negotiation Request");
579 #ifdef CONFIG_P2P_STRICT
580 		goto fail;
581 #endif /* CONFIG_P2P_STRICT */
582 	}
583 
584 	if (!msg.listen_channel) {
585 		p2p_dbg(p2p, "No Listen Channel attribute received");
586 		goto fail;
587 	}
588 	if (!msg.operating_channel) {
589 		p2p_dbg(p2p, "No Operating Channel attribute received");
590 		goto fail;
591 	}
592 	if (!msg.channel_list) {
593 		p2p_dbg(p2p, "No Channel List attribute received");
594 		goto fail;
595 	}
596 	if (!msg.intended_addr) {
597 		p2p_dbg(p2p, "No Intended P2P Interface Address attribute received");
598 		goto fail;
599 	}
600 	if (!msg.p2p_device_info) {
601 		p2p_dbg(p2p, "No P2P Device Info attribute received");
602 		goto fail;
603 	}
604 
605 	if (os_memcmp(msg.p2p_device_addr, sa, ETH_ALEN) != 0) {
606 		p2p_dbg(p2p, "Unexpected GO Negotiation Request SA=" MACSTR
607 			" != dev_addr=" MACSTR,
608 			MAC2STR(sa), MAC2STR(msg.p2p_device_addr));
609 		goto fail;
610 	}
611 
612 	dev = p2p_get_device(p2p, sa);
613 
614 	if (msg.status && *msg.status) {
615 		p2p_dbg(p2p, "Unexpected Status attribute (%d) in GO Negotiation Request",
616 			*msg.status);
617 		if (dev && p2p->go_neg_peer == dev &&
618 		    *msg.status == P2P_SC_FAIL_REJECTED_BY_USER) {
619 			/*
620 			 * This mechanism for using Status attribute in GO
621 			 * Negotiation Request is not compliant with the P2P
622 			 * specification, but some deployed devices use it to
623 			 * indicate rejection of GO Negotiation in a case where
624 			 * they have sent out GO Negotiation Response with
625 			 * status 1. The P2P specification explicitly disallows
626 			 * this. To avoid unnecessary interoperability issues
627 			 * and extra frames, mark the pending negotiation as
628 			 * failed and do not reply to this GO Negotiation
629 			 * Request frame.
630 			 */
631 			p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
632 			p2p_go_neg_failed(p2p, *msg.status);
633 			p2p_parse_free(&msg);
634 			return;
635 		}
636 		goto fail;
637 	}
638 
639 	if (dev == NULL)
640 		dev = p2p_add_dev_from_go_neg_req(p2p, sa, &msg);
641 	else if ((dev->flags & P2P_DEV_PROBE_REQ_ONLY) ||
642 		  !(dev->flags & P2P_DEV_REPORTED))
643 		p2p_add_dev_info(p2p, sa, dev, &msg);
644 	else if (!dev->listen_freq && !dev->oper_freq) {
645 		/*
646 		 * This may happen if the peer entry was added based on PD
647 		 * Request and no Probe Request/Response frame has been received
648 		 * from this peer (or that information has timed out).
649 		 */
650 		p2p_dbg(p2p, "Update peer " MACSTR
651 			" based on GO Neg Req since listen/oper freq not known",
652 			MAC2STR(dev->info.p2p_device_addr));
653 		p2p_add_dev_info(p2p, sa, dev, &msg);
654 	}
655 
656 	if (p2p->go_neg_peer && p2p->go_neg_peer == dev)
657 		eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
658 
659 	if (dev && dev->flags & P2P_DEV_USER_REJECTED) {
660 		p2p_dbg(p2p, "User has rejected this peer");
661 		status = P2P_SC_FAIL_REJECTED_BY_USER;
662 	} else if (dev == NULL ||
663 		   (dev->wps_method == WPS_NOT_READY &&
664 		    (p2p->authorized_oob_dev_pw_id == 0 ||
665 		     p2p->authorized_oob_dev_pw_id !=
666 		     msg.dev_password_id))) {
667 		p2p_dbg(p2p, "Not ready for GO negotiation with " MACSTR,
668 			MAC2STR(sa));
669 		status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
670 		p2p->cfg->go_neg_req_rx(p2p->cfg->cb_ctx, sa,
671 					msg.dev_password_id);
672 	} else if (p2p->go_neg_peer && p2p->go_neg_peer != dev) {
673 		p2p_dbg(p2p, "Already in Group Formation with another peer");
674 		status = P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
675 	} else {
676 		int go;
677 
678 		if (!p2p->go_neg_peer) {
679 			p2p_dbg(p2p, "Starting GO Negotiation with previously authorized peer");
680 			if (!(dev->flags & P2P_DEV_FORCE_FREQ)) {
681 				p2p_dbg(p2p, "Use default channel settings");
682 				p2p->op_reg_class = p2p->cfg->op_reg_class;
683 				p2p->op_channel = p2p->cfg->op_channel;
684 				os_memcpy(&p2p->channels, &p2p->cfg->channels,
685 					  sizeof(struct p2p_channels));
686 			} else {
687 				p2p_dbg(p2p, "Use previously configured forced channel settings");
688 			}
689 		}
690 
691 		dev->flags &= ~P2P_DEV_NOT_YET_READY;
692 
693 		if (!msg.go_intent) {
694 			p2p_dbg(p2p, "No GO Intent attribute received");
695 			goto fail;
696 		}
697 		if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) {
698 			p2p_dbg(p2p, "Invalid GO Intent value (%u) received",
699 				*msg.go_intent >> 1);
700 			goto fail;
701 		}
702 
703 		if (dev->go_neg_req_sent &&
704 		    os_memcmp(sa, p2p->cfg->dev_addr, ETH_ALEN) > 0) {
705 			p2p_dbg(p2p, "Do not reply since peer has higher address and GO Neg Request already sent");
706 			p2p_parse_free(&msg);
707 			return;
708 		}
709 
710 		go = p2p_go_det(p2p->go_intent, *msg.go_intent);
711 		if (go < 0) {
712 			p2p_dbg(p2p, "Incompatible GO Intent");
713 			status = P2P_SC_FAIL_BOTH_GO_INTENT_15;
714 			goto fail;
715 		}
716 
717 		if (p2p_peer_channels(p2p, dev, msg.channel_list,
718 				      msg.channel_list_len) < 0) {
719 			p2p_dbg(p2p, "No common channels found");
720 			status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
721 			goto fail;
722 		}
723 
724 		switch (msg.dev_password_id) {
725 		case DEV_PW_REGISTRAR_SPECIFIED:
726 			p2p_dbg(p2p, "PIN from peer Display");
727 			if (dev->wps_method != WPS_PIN_KEYPAD) {
728 				p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
729 					p2p_wps_method_str(dev->wps_method));
730 				status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
731 				goto fail;
732 			}
733 			break;
734 		case DEV_PW_USER_SPECIFIED:
735 			p2p_dbg(p2p, "Peer entered PIN on Keypad");
736 			if (dev->wps_method != WPS_PIN_DISPLAY) {
737 				p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
738 					p2p_wps_method_str(dev->wps_method));
739 				status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
740 				goto fail;
741 			}
742 			break;
743 		case DEV_PW_PUSHBUTTON:
744 			p2p_dbg(p2p, "Peer using pushbutton");
745 			if (dev->wps_method != WPS_PBC) {
746 				p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
747 					p2p_wps_method_str(dev->wps_method));
748 				status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
749 				goto fail;
750 			}
751 			break;
752 		case DEV_PW_P2PS_DEFAULT:
753 			p2p_dbg(p2p, "Peer using P2PS pin");
754 			if (dev->wps_method != WPS_P2PS) {
755 				p2p_dbg(p2p,
756 					"We have wps_method=%s -> incompatible",
757 					p2p_wps_method_str(dev->wps_method));
758 				status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
759 				goto fail;
760 			}
761 			break;
762 		default:
763 			if (msg.dev_password_id &&
764 			    msg.dev_password_id == dev->oob_pw_id) {
765 				p2p_dbg(p2p, "Peer using NFC");
766 				if (dev->wps_method != WPS_NFC) {
767 					p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
768 						p2p_wps_method_str(
769 							dev->wps_method));
770 					status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
771 					goto fail;
772 				}
773 				break;
774 			}
775 #ifdef CONFIG_WPS_NFC
776 			if (p2p->authorized_oob_dev_pw_id &&
777 			    msg.dev_password_id ==
778 			    p2p->authorized_oob_dev_pw_id) {
779 				p2p_dbg(p2p, "Using static handover with our device password from NFC Tag");
780 				dev->wps_method = WPS_NFC;
781 				dev->oob_pw_id = p2p->authorized_oob_dev_pw_id;
782 				break;
783 			}
784 #endif /* CONFIG_WPS_NFC */
785 			p2p_dbg(p2p, "Unsupported Device Password ID %d",
786 				msg.dev_password_id);
787 			status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
788 			goto fail;
789 		}
790 
791 		if (go && p2p_go_select_channel(p2p, dev, &status) < 0)
792 			goto fail;
793 
794 		dev->go_state = go ? LOCAL_GO : REMOTE_GO;
795 		dev->oper_freq = p2p_channel_to_freq(msg.operating_channel[3],
796 						     msg.operating_channel[4]);
797 		p2p_dbg(p2p, "Peer operating channel preference: %d MHz",
798 			dev->oper_freq);
799 
800 		if (msg.config_timeout) {
801 			dev->go_timeout = msg.config_timeout[0];
802 			dev->client_timeout = msg.config_timeout[1];
803 		}
804 
805 		p2p_dbg(p2p, "GO Negotiation with " MACSTR, MAC2STR(sa));
806 		if (p2p->state != P2P_IDLE)
807 			p2p_stop_find_for_freq(p2p, rx_freq);
808 		p2p_set_state(p2p, P2P_GO_NEG);
809 		p2p_clear_timeout(p2p);
810 		dev->dialog_token = msg.dialog_token;
811 		os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN);
812 		p2p->go_neg_peer = dev;
813 		eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
814 		status = P2P_SC_SUCCESS;
815 	}
816 
817 fail:
818 	if (dev)
819 		dev->status = status;
820 	resp = p2p_build_go_neg_resp(p2p, dev, msg.dialog_token, status,
821 				     !tie_breaker);
822 	p2p_parse_free(&msg);
823 	if (resp == NULL)
824 		return;
825 	p2p_dbg(p2p, "Sending GO Negotiation Response");
826 	if (rx_freq > 0)
827 		freq = rx_freq;
828 	else
829 		freq = p2p_channel_to_freq(p2p->cfg->reg_class,
830 					   p2p->cfg->channel);
831 	if (freq < 0) {
832 		p2p_dbg(p2p, "Unknown regulatory class/channel");
833 		wpabuf_free(resp);
834 		return;
835 	}
836 	if (status == P2P_SC_SUCCESS) {
837 		p2p->pending_action_state = P2P_PENDING_GO_NEG_RESPONSE;
838 		dev->flags |= P2P_DEV_WAIT_GO_NEG_CONFIRM;
839 		if (os_memcmp(sa, p2p->cfg->dev_addr, ETH_ALEN) < 0) {
840 			/*
841 			 * Peer has smaller address, so the GO Negotiation
842 			 * Response from us is expected to complete
843 			 * negotiation. Ignore a GO Negotiation Response from
844 			 * the peer if it happens to be received after this
845 			 * point due to a race condition in GO Negotiation
846 			 * Request transmission and processing.
847 			 */
848 			dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
849 		}
850 	} else
851 		p2p->pending_action_state =
852 			P2P_PENDING_GO_NEG_RESPONSE_FAILURE;
853 	if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr,
854 			    p2p->cfg->dev_addr,
855 			    wpabuf_head(resp), wpabuf_len(resp), 500) < 0) {
856 		p2p_dbg(p2p, "Failed to send Action frame");
857 	}
858 
859 	wpabuf_free(resp);
860 }
861 
862 
863 static struct wpabuf * p2p_build_go_neg_conf(struct p2p_data *p2p,
864 					     struct p2p_device *peer,
865 					     u8 dialog_token, u8 status,
866 					     const u8 *resp_chan, int go)
867 {
868 	struct wpabuf *buf;
869 	u8 *len;
870 	struct p2p_channels res;
871 	u8 group_capab;
872 	size_t extra = 0;
873 
874 	p2p_dbg(p2p, "Building GO Negotiation Confirm");
875 
876 #ifdef CONFIG_WIFI_DISPLAY
877 	if (p2p->wfd_ie_go_neg)
878 		extra = wpabuf_len(p2p->wfd_ie_go_neg);
879 #endif /* CONFIG_WIFI_DISPLAY */
880 
881 	if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_CONF])
882 		extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_CONF]);
883 
884 	buf = wpabuf_alloc(1000 + extra);
885 	if (buf == NULL)
886 		return NULL;
887 
888 	p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_CONF, dialog_token);
889 
890 	len = p2p_buf_add_ie_hdr(buf);
891 	p2p_buf_add_status(buf, status);
892 	group_capab = 0;
893 	if (peer->go_state == LOCAL_GO) {
894 		if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
895 			group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
896 			if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
897 				group_capab |=
898 					P2P_GROUP_CAPAB_PERSISTENT_RECONN;
899 		}
900 		if (p2p->cross_connect)
901 			group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
902 		if (p2p->cfg->p2p_intra_bss)
903 			group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
904 	}
905 	p2p_buf_add_capability(buf, p2p->dev_capab &
906 			       ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY,
907 			       group_capab);
908 	if (go || resp_chan == NULL)
909 		p2p_buf_add_operating_channel(buf, p2p->cfg->country,
910 					      p2p->op_reg_class,
911 					      p2p->op_channel);
912 	else
913 		p2p_buf_add_operating_channel(buf, (const char *) resp_chan,
914 					      resp_chan[3], resp_chan[4]);
915 	p2p_channels_intersect(&p2p->channels, &peer->channels, &res);
916 	p2p_buf_add_channel_list(buf, p2p->cfg->country, &res);
917 	if (go) {
918 		p2p_buf_add_group_id(buf, p2p->cfg->dev_addr, p2p->ssid,
919 				     p2p->ssid_len);
920 	}
921 	p2p_buf_update_ie_hdr(buf, len);
922 
923 #ifdef CONFIG_WIFI_DISPLAY
924 	if (p2p->wfd_ie_go_neg)
925 		wpabuf_put_buf(buf, p2p->wfd_ie_go_neg);
926 #endif /* CONFIG_WIFI_DISPLAY */
927 
928 	if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_CONF])
929 		wpabuf_put_buf(buf, p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_CONF]);
930 
931 	return buf;
932 }
933 
934 
935 void p2p_process_go_neg_resp(struct p2p_data *p2p, const u8 *sa,
936 			     const u8 *data, size_t len, int rx_freq)
937 {
938 	struct p2p_device *dev;
939 	int go = -1;
940 	struct p2p_message msg;
941 	u8 status = P2P_SC_SUCCESS;
942 	int freq;
943 
944 	p2p_dbg(p2p, "Received GO Negotiation Response from " MACSTR
945 		" (freq=%d)", MAC2STR(sa), rx_freq);
946 	dev = p2p_get_device(p2p, sa);
947 	if (dev == NULL || dev->wps_method == WPS_NOT_READY ||
948 	    dev != p2p->go_neg_peer) {
949 		p2p_dbg(p2p, "Not ready for GO negotiation with " MACSTR,
950 			MAC2STR(sa));
951 		return;
952 	}
953 
954 	if (p2p_parse(data, len, &msg))
955 		return;
956 
957 	if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE)) {
958 		p2p_dbg(p2p, "Was not expecting GO Negotiation Response - ignore");
959 		p2p_parse_free(&msg);
960 		return;
961 	}
962 	dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
963 
964 	if (msg.dialog_token != dev->dialog_token) {
965 		p2p_dbg(p2p, "Unexpected Dialog Token %u (expected %u)",
966 			msg.dialog_token, dev->dialog_token);
967 		p2p_parse_free(&msg);
968 		return;
969 	}
970 
971 	if (!msg.status) {
972 		p2p_dbg(p2p, "No Status attribute received");
973 		status = P2P_SC_FAIL_INVALID_PARAMS;
974 		goto fail;
975 	}
976 	if (*msg.status) {
977 		p2p_dbg(p2p, "GO Negotiation rejected: status %d", *msg.status);
978 		dev->go_neg_req_sent = 0;
979 		if (*msg.status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
980 			p2p_dbg(p2p, "Wait for the peer to become ready for GO Negotiation");
981 			dev->flags |= P2P_DEV_NOT_YET_READY;
982 			eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p,
983 					     NULL);
984 			eloop_register_timeout(120, 0, p2p_go_neg_wait_timeout,
985 					       p2p, NULL);
986 			if (p2p->state == P2P_CONNECT_LISTEN)
987 				p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT);
988 			else
989 				p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
990 			p2p_set_timeout(p2p, 0, 0);
991 		} else {
992 			p2p_dbg(p2p, "Stop GO Negotiation attempt");
993 			p2p_go_neg_failed(p2p, *msg.status);
994 		}
995 		p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
996 		p2p_parse_free(&msg);
997 		return;
998 	}
999 
1000 	if (!msg.capability) {
1001 		p2p_dbg(p2p, "Mandatory Capability attribute missing from GO Negotiation Response");
1002 #ifdef CONFIG_P2P_STRICT
1003 		status = P2P_SC_FAIL_INVALID_PARAMS;
1004 		goto fail;
1005 #endif /* CONFIG_P2P_STRICT */
1006 	}
1007 
1008 	if (!msg.p2p_device_info) {
1009 		p2p_dbg(p2p, "Mandatory P2P Device Info attribute missing from GO Negotiation Response");
1010 #ifdef CONFIG_P2P_STRICT
1011 		status = P2P_SC_FAIL_INVALID_PARAMS;
1012 		goto fail;
1013 #endif /* CONFIG_P2P_STRICT */
1014 	}
1015 
1016 	if (!msg.intended_addr) {
1017 		p2p_dbg(p2p, "No Intended P2P Interface Address attribute received");
1018 		status = P2P_SC_FAIL_INVALID_PARAMS;
1019 		goto fail;
1020 	}
1021 
1022 	if (!msg.go_intent) {
1023 		p2p_dbg(p2p, "No GO Intent attribute received");
1024 		status = P2P_SC_FAIL_INVALID_PARAMS;
1025 		goto fail;
1026 	}
1027 	if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) {
1028 		p2p_dbg(p2p, "Invalid GO Intent value (%u) received",
1029 			*msg.go_intent >> 1);
1030 		status = P2P_SC_FAIL_INVALID_PARAMS;
1031 		goto fail;
1032 	}
1033 
1034 	go = p2p_go_det(p2p->go_intent, *msg.go_intent);
1035 	if (go < 0) {
1036 		p2p_dbg(p2p, "Incompatible GO Intent");
1037 		status = P2P_SC_FAIL_INCOMPATIBLE_PARAMS;
1038 		goto fail;
1039 	}
1040 
1041 	if (!go && msg.group_id) {
1042 		/* Store SSID for Provisioning step */
1043 		p2p->ssid_len = msg.group_id_len - ETH_ALEN;
1044 		os_memcpy(p2p->ssid, msg.group_id + ETH_ALEN, p2p->ssid_len);
1045 	} else if (!go) {
1046 		p2p_dbg(p2p, "Mandatory P2P Group ID attribute missing from GO Negotiation Response");
1047 		p2p->ssid_len = 0;
1048 		status = P2P_SC_FAIL_INVALID_PARAMS;
1049 		goto fail;
1050 	}
1051 
1052 	if (!msg.config_timeout) {
1053 		p2p_dbg(p2p, "Mandatory Configuration Timeout attribute missing from GO Negotiation Response");
1054 #ifdef CONFIG_P2P_STRICT
1055 		status = P2P_SC_FAIL_INVALID_PARAMS;
1056 		goto fail;
1057 #endif /* CONFIG_P2P_STRICT */
1058 	} else {
1059 		dev->go_timeout = msg.config_timeout[0];
1060 		dev->client_timeout = msg.config_timeout[1];
1061 	}
1062 
1063 	if (!msg.operating_channel && !go) {
1064 		/*
1065 		 * Note: P2P Client may omit Operating Channel attribute to
1066 		 * indicate it does not have a preference.
1067 		 */
1068 		p2p_dbg(p2p, "No Operating Channel attribute received");
1069 		status = P2P_SC_FAIL_INVALID_PARAMS;
1070 		goto fail;
1071 	}
1072 	if (!msg.channel_list) {
1073 		p2p_dbg(p2p, "No Channel List attribute received");
1074 		status = P2P_SC_FAIL_INVALID_PARAMS;
1075 		goto fail;
1076 	}
1077 
1078 	if (p2p_peer_channels(p2p, dev, msg.channel_list,
1079 			      msg.channel_list_len) < 0) {
1080 		p2p_dbg(p2p, "No common channels found");
1081 		status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
1082 		goto fail;
1083 	}
1084 
1085 	if (msg.operating_channel) {
1086 		dev->oper_freq = p2p_channel_to_freq(msg.operating_channel[3],
1087 						     msg.operating_channel[4]);
1088 		p2p_dbg(p2p, "Peer operating channel preference: %d MHz",
1089 			dev->oper_freq);
1090 	} else
1091 		dev->oper_freq = 0;
1092 
1093 	switch (msg.dev_password_id) {
1094 	case DEV_PW_REGISTRAR_SPECIFIED:
1095 		p2p_dbg(p2p, "PIN from peer Display");
1096 		if (dev->wps_method != WPS_PIN_KEYPAD) {
1097 			p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
1098 				p2p_wps_method_str(dev->wps_method));
1099 			status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1100 			goto fail;
1101 		}
1102 		break;
1103 	case DEV_PW_USER_SPECIFIED:
1104 		p2p_dbg(p2p, "Peer entered PIN on Keypad");
1105 		if (dev->wps_method != WPS_PIN_DISPLAY) {
1106 			p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
1107 				p2p_wps_method_str(dev->wps_method));
1108 			status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1109 			goto fail;
1110 		}
1111 		break;
1112 	case DEV_PW_PUSHBUTTON:
1113 		p2p_dbg(p2p, "Peer using pushbutton");
1114 		if (dev->wps_method != WPS_PBC) {
1115 			p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
1116 				p2p_wps_method_str(dev->wps_method));
1117 			status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1118 			goto fail;
1119 		}
1120 		break;
1121 	case DEV_PW_P2PS_DEFAULT:
1122 		p2p_dbg(p2p, "P2P: Peer using P2PS default pin");
1123 		if (dev->wps_method != WPS_P2PS) {
1124 			p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
1125 				p2p_wps_method_str(dev->wps_method));
1126 			status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1127 			goto fail;
1128 		}
1129 		break;
1130 	default:
1131 		if (msg.dev_password_id &&
1132 		    msg.dev_password_id == dev->oob_pw_id) {
1133 			p2p_dbg(p2p, "Peer using NFC");
1134 			if (dev->wps_method != WPS_NFC) {
1135 				p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
1136 					p2p_wps_method_str(dev->wps_method));
1137 				status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1138 				goto fail;
1139 			}
1140 			break;
1141 		}
1142 		p2p_dbg(p2p, "Unsupported Device Password ID %d",
1143 			msg.dev_password_id);
1144 		status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1145 		goto fail;
1146 	}
1147 
1148 	if (go && p2p_go_select_channel(p2p, dev, &status) < 0)
1149 		goto fail;
1150 
1151 	p2p_set_state(p2p, P2P_GO_NEG);
1152 	p2p_clear_timeout(p2p);
1153 
1154 	p2p_dbg(p2p, "GO Negotiation with " MACSTR, MAC2STR(sa));
1155 	os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN);
1156 
1157 fail:
1158 	/* Store GO Negotiation Confirmation to allow retransmission */
1159 	wpabuf_free(dev->go_neg_conf);
1160 	dev->go_neg_conf = p2p_build_go_neg_conf(p2p, dev, msg.dialog_token,
1161 						 status, msg.operating_channel,
1162 						 go);
1163 	p2p_parse_free(&msg);
1164 	if (dev->go_neg_conf == NULL)
1165 		return;
1166 	p2p_dbg(p2p, "Sending GO Negotiation Confirm");
1167 	if (status == P2P_SC_SUCCESS) {
1168 		p2p->pending_action_state = P2P_PENDING_GO_NEG_CONFIRM;
1169 		dev->go_state = go ? LOCAL_GO : REMOTE_GO;
1170 	} else
1171 		p2p->pending_action_state = P2P_NO_PENDING_ACTION;
1172 	if (rx_freq > 0)
1173 		freq = rx_freq;
1174 	else
1175 		freq = dev->listen_freq;
1176 
1177 	dev->go_neg_conf_freq = freq;
1178 	dev->go_neg_conf_sent = 0;
1179 
1180 	if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr, sa,
1181 			    wpabuf_head(dev->go_neg_conf),
1182 			    wpabuf_len(dev->go_neg_conf), 200) < 0) {
1183 		p2p_dbg(p2p, "Failed to send Action frame");
1184 		p2p_go_neg_failed(p2p, -1);
1185 		p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
1186 	} else
1187 		dev->go_neg_conf_sent++;
1188 	if (status != P2P_SC_SUCCESS) {
1189 		p2p_dbg(p2p, "GO Negotiation failed");
1190 		p2p_go_neg_failed(p2p, status);
1191 	}
1192 }
1193 
1194 
1195 void p2p_process_go_neg_conf(struct p2p_data *p2p, const u8 *sa,
1196 			     const u8 *data, size_t len)
1197 {
1198 	struct p2p_device *dev;
1199 	struct p2p_message msg;
1200 
1201 	p2p_dbg(p2p, "Received GO Negotiation Confirm from " MACSTR,
1202 		MAC2STR(sa));
1203 	dev = p2p_get_device(p2p, sa);
1204 	if (dev == NULL || dev->wps_method == WPS_NOT_READY ||
1205 	    dev != p2p->go_neg_peer) {
1206 		p2p_dbg(p2p, "Not ready for GO negotiation with " MACSTR,
1207 			MAC2STR(sa));
1208 		return;
1209 	}
1210 
1211 	if (p2p->pending_action_state == P2P_PENDING_GO_NEG_RESPONSE) {
1212 		p2p_dbg(p2p, "Stopped waiting for TX status on GO Negotiation Response since we already received Confirmation");
1213 		p2p->pending_action_state = P2P_NO_PENDING_ACTION;
1214 	}
1215 
1216 	if (p2p_parse(data, len, &msg))
1217 		return;
1218 
1219 	if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
1220 		p2p_dbg(p2p, "Was not expecting GO Negotiation Confirm - ignore");
1221 		p2p_parse_free(&msg);
1222 		return;
1223 	}
1224 	dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
1225 	p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
1226 
1227 	if (msg.dialog_token != dev->dialog_token) {
1228 		p2p_dbg(p2p, "Unexpected Dialog Token %u (expected %u)",
1229 			msg.dialog_token, dev->dialog_token);
1230 		p2p_parse_free(&msg);
1231 		return;
1232 	}
1233 
1234 	if (!msg.status) {
1235 		p2p_dbg(p2p, "No Status attribute received");
1236 		p2p_parse_free(&msg);
1237 		return;
1238 	}
1239 	if (*msg.status) {
1240 		p2p_dbg(p2p, "GO Negotiation rejected: status %d", *msg.status);
1241 		p2p_go_neg_failed(p2p, *msg.status);
1242 		p2p_parse_free(&msg);
1243 		return;
1244 	}
1245 
1246 	if (dev->go_state == REMOTE_GO && msg.group_id) {
1247 		/* Store SSID for Provisioning step */
1248 		p2p->ssid_len = msg.group_id_len - ETH_ALEN;
1249 		os_memcpy(p2p->ssid, msg.group_id + ETH_ALEN, p2p->ssid_len);
1250 	} else if (dev->go_state == REMOTE_GO) {
1251 		p2p_dbg(p2p, "Mandatory P2P Group ID attribute missing from GO Negotiation Confirmation");
1252 		p2p->ssid_len = 0;
1253 		p2p_go_neg_failed(p2p, P2P_SC_FAIL_INVALID_PARAMS);
1254 		p2p_parse_free(&msg);
1255 		return;
1256 	}
1257 
1258 	if (!msg.operating_channel) {
1259 		p2p_dbg(p2p, "Mandatory Operating Channel attribute missing from GO Negotiation Confirmation");
1260 #ifdef CONFIG_P2P_STRICT
1261 		p2p_parse_free(&msg);
1262 		return;
1263 #endif /* CONFIG_P2P_STRICT */
1264 	} else if (dev->go_state == REMOTE_GO) {
1265 		int oper_freq = p2p_channel_to_freq(msg.operating_channel[3],
1266 						    msg.operating_channel[4]);
1267 		if (oper_freq != dev->oper_freq) {
1268 			p2p_dbg(p2p, "Updated peer (GO) operating channel preference from %d MHz to %d MHz",
1269 				dev->oper_freq, oper_freq);
1270 			dev->oper_freq = oper_freq;
1271 		}
1272 	}
1273 
1274 	if (!msg.channel_list) {
1275 		p2p_dbg(p2p, "Mandatory Operating Channel attribute missing from GO Negotiation Confirmation");
1276 #ifdef CONFIG_P2P_STRICT
1277 		p2p_parse_free(&msg);
1278 		return;
1279 #endif /* CONFIG_P2P_STRICT */
1280 	}
1281 
1282 	p2p_parse_free(&msg);
1283 
1284 	if (dev->go_state == UNKNOWN_GO) {
1285 		/*
1286 		 * This should not happen since GO negotiation has already
1287 		 * been completed.
1288 		 */
1289 		p2p_dbg(p2p, "Unexpected GO Neg state - do not know which end becomes GO");
1290 		return;
1291 	}
1292 
1293 	/*
1294 	 * The peer could have missed our ctrl::ack frame for GO Negotiation
1295 	 * Confirm and continue retransmitting the frame. To reduce the
1296 	 * likelihood of the peer not getting successful TX status for the
1297 	 * GO Negotiation Confirm frame, wait a short time here before starting
1298 	 * the group so that we will remain on the current channel to
1299 	 * acknowledge any possible retransmission from the peer.
1300 	 */
1301 	p2p_dbg(p2p, "20 ms wait on current channel before starting group");
1302 	os_sleep(0, 20000);
1303 
1304 	p2p_go_complete(p2p, dev);
1305 }
1306