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