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