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