p2p_go_neg.c revision 9cdf1b905fae57dea42506a1af67f6ece544b261
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), 200) < 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	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Selected operating "
354		"channel (reg_class %u channel %u) not acceptable to the "
355		"peer", p2p->op_reg_class, p2p->op_channel);
356
357	/* First, try to pick the best channel from another band */
358	freq = p2p_channel_to_freq(p2p->cfg->country, p2p->op_reg_class,
359				   p2p->op_channel);
360	if (freq >= 2400 && freq < 2500 && p2p->best_freq_5 > 0 &&
361	    p2p_freq_to_channel(p2p->cfg->country, p2p->best_freq_5,
362				&op_reg_class, &op_channel) == 0 &&
363	    p2p_channels_includes(intersection, op_reg_class, op_channel)) {
364		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Pick best 5 GHz "
365			"channel (reg_class %u channel %u) from intersection",
366			op_reg_class, op_channel);
367		p2p->op_reg_class = op_reg_class;
368		p2p->op_channel = op_channel;
369		return;
370	}
371
372	if (freq >= 4900 && freq < 6000 && p2p->best_freq_24 > 0 &&
373	    p2p_freq_to_channel(p2p->cfg->country, p2p->best_freq_24,
374				&op_reg_class, &op_channel) == 0 &&
375	    p2p_channels_includes(intersection, op_reg_class, op_channel)) {
376		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Pick best 2.4 GHz "
377			"channel (reg_class %u channel %u) from intersection",
378			op_reg_class, op_channel);
379		p2p->op_reg_class = op_reg_class;
380		p2p->op_channel = op_channel;
381		return;
382	}
383
384	/* Select channel with highest preference if the peer supports it */
385	for (i = 0; p2p->cfg->pref_chan && i < p2p->cfg->num_pref_chan; i++) {
386		if (p2p_channels_includes(intersection,
387					  p2p->cfg->pref_chan[i].op_class,
388					  p2p->cfg->pref_chan[i].chan)) {
389			p2p->op_reg_class = p2p->cfg->pref_chan[i].op_class;
390			p2p->op_channel = p2p->cfg->pref_chan[i].chan;
391			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Pick "
392				"highest preferred chnnel (op_class %u "
393				"channel %u) from intersection",
394				p2p->op_reg_class, p2p->op_channel);
395			return;
396		}
397	}
398
399	/* Try a channel where we might be able to use HT40 */
400	for (i = 0; i < intersection->reg_classes; i++) {
401		struct p2p_reg_class *c = &intersection->reg_class[i];
402		if (c->reg_class == 116 || c->reg_class == 117 ||
403		    c->reg_class == 126 || c->reg_class == 127) {
404			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
405				"P2P: Pick possible HT40 channel (reg_class "
406				"%u channel %u) from intersection",
407				c->reg_class, c->channel[0]);
408			p2p->op_reg_class = c->reg_class;
409			p2p->op_channel = c->channel[0];
410			return;
411		}
412	}
413
414	/*
415	 * Try to see if the original channel is in the intersection. If
416	 * so, no need to change anything, as it already contains some
417	 * randomness.
418	 */
419	if (p2p_channels_includes(intersection, p2p->op_reg_class,
420				  p2p->op_channel)) {
421		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
422			"P2P: Using original operating class and channel "
423			"(op_class %u channel %u) from intersection",
424			p2p->op_reg_class, p2p->op_channel);
425		return;
426	}
427
428	/*
429	 * Fall back to whatever is included in the channel intersection since
430	 * no better options seems to be available.
431	 */
432	cl = &intersection->reg_class[0];
433	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Pick another channel "
434		"(reg_class %u channel %u) from intersection",
435		cl->reg_class, cl->channel[0]);
436	p2p->op_reg_class = cl->reg_class;
437	p2p->op_channel = cl->channel[0];
438}
439
440
441static int p2p_go_select_channel(struct p2p_data *p2p, struct p2p_device *dev,
442				 u8 *status)
443{
444	struct p2p_channels intersection;
445	size_t i;
446
447	p2p_channels_intersect(&p2p->channels, &dev->channels, &intersection);
448	if (intersection.reg_classes == 0 ||
449	    intersection.reg_class[0].channels == 0) {
450		*status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
451		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
452			"P2P: No common channels found");
453		return -1;
454	}
455
456	for (i = 0; i < intersection.reg_classes; i++) {
457		struct p2p_reg_class *c;
458		c = &intersection.reg_class[i];
459		wpa_printf(MSG_DEBUG, "P2P: reg_class %u", c->reg_class);
460		wpa_hexdump(MSG_DEBUG, "P2P: channels",
461			    c->channel, c->channels);
462	}
463
464	if (!p2p_channels_includes(&intersection, p2p->op_reg_class,
465				   p2p->op_channel)) {
466		if (dev->flags & P2P_DEV_FORCE_FREQ) {
467			*status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
468			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer does "
469				"not support the forced channel");
470			return -1;
471		}
472
473		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Selected operating "
474			"channel (op_class %u channel %u) not acceptable to "
475			"the peer", p2p->op_reg_class, p2p->op_channel);
476		p2p_reselect_channel(p2p, &intersection);
477	} else if (!(dev->flags & P2P_DEV_FORCE_FREQ) &&
478		   !p2p->cfg->cfg_op_channel) {
479		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Try to optimize "
480			"channel selection with peer information received; "
481			"previously selected op_class %u channel %u",
482			p2p->op_reg_class, p2p->op_channel);
483		p2p_reselect_channel(p2p, &intersection);
484	}
485
486	if (!p2p->ssid_set) {
487		p2p_build_ssid(p2p, p2p->ssid, &p2p->ssid_len);
488		p2p->ssid_set = 1;
489	}
490
491	return 0;
492}
493
494
495void p2p_process_go_neg_req(struct p2p_data *p2p, const u8 *sa,
496			    const u8 *data, size_t len, int rx_freq)
497{
498	struct p2p_device *dev = NULL;
499	struct wpabuf *resp;
500	struct p2p_message msg;
501	u8 status = P2P_SC_FAIL_INVALID_PARAMS;
502	int tie_breaker = 0;
503	int freq;
504
505	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
506		"P2P: Received GO Negotiation Request from " MACSTR
507		"(freq=%d)", MAC2STR(sa), rx_freq);
508
509	if (p2p_parse(data, len, &msg))
510		return;
511
512	if (!msg.capability) {
513		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
514			"P2P: Mandatory Capability attribute missing from GO "
515			"Negotiation Request");
516#ifdef CONFIG_P2P_STRICT
517		goto fail;
518#endif /* CONFIG_P2P_STRICT */
519	}
520
521	if (msg.go_intent)
522		tie_breaker = *msg.go_intent & 0x01;
523	else {
524		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
525			"P2P: Mandatory GO Intent attribute missing from GO "
526			"Negotiation Request");
527#ifdef CONFIG_P2P_STRICT
528		goto fail;
529#endif /* CONFIG_P2P_STRICT */
530	}
531
532	if (!msg.config_timeout) {
533		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
534			"P2P: Mandatory Configuration Timeout attribute "
535			"missing from GO Negotiation Request");
536#ifdef CONFIG_P2P_STRICT
537		goto fail;
538#endif /* CONFIG_P2P_STRICT */
539	}
540
541	if (!msg.listen_channel) {
542		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
543			"P2P: No Listen Channel attribute received");
544		goto fail;
545	}
546	if (!msg.operating_channel) {
547		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
548			"P2P: No Operating Channel attribute received");
549		goto fail;
550	}
551	if (!msg.channel_list) {
552		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
553			"P2P: No Channel List attribute received");
554		goto fail;
555	}
556	if (!msg.intended_addr) {
557		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
558			"P2P: No Intended P2P Interface Address attribute "
559			"received");
560		goto fail;
561	}
562	if (!msg.p2p_device_info) {
563		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
564			"P2P: No P2P Device Info attribute received");
565		goto fail;
566	}
567
568	if (os_memcmp(msg.p2p_device_addr, sa, ETH_ALEN) != 0) {
569		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
570			"P2P: Unexpected GO Negotiation Request SA=" MACSTR
571			" != dev_addr=" MACSTR,
572			MAC2STR(sa), MAC2STR(msg.p2p_device_addr));
573		goto fail;
574	}
575
576	dev = p2p_get_device(p2p, sa);
577
578	if (msg.status && *msg.status) {
579		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
580			"P2P: Unexpected Status attribute (%d) in GO "
581			"Negotiation Request", *msg.status);
582		goto fail;
583	}
584
585	if (dev == NULL)
586		dev = p2p_add_dev_from_go_neg_req(p2p, sa, &msg);
587	else if (dev->flags & P2P_DEV_PROBE_REQ_ONLY)
588		p2p_add_dev_info(p2p, sa, dev, &msg);
589	if (dev && dev->flags & P2P_DEV_USER_REJECTED) {
590		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
591			"P2P: User has rejected this peer");
592		status = P2P_SC_FAIL_REJECTED_BY_USER;
593	} else if (dev == NULL || dev->wps_method == WPS_NOT_READY) {
594		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
595			"P2P: Not ready for GO negotiation with " MACSTR,
596			MAC2STR(sa));
597		status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
598		if (dev)
599			dev->flags |= P2P_DEV_PEER_WAITING_RESPONSE;
600		p2p->cfg->go_neg_req_rx(p2p->cfg->cb_ctx, sa,
601					msg.dev_password_id);
602	} else if (p2p->go_neg_peer && p2p->go_neg_peer != dev) {
603		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
604			"P2P: Already in Group Formation with another peer");
605		status = P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
606	} else {
607		int go;
608
609		if (!p2p->go_neg_peer) {
610			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting "
611				"GO Negotiation with previously authorized "
612				"peer");
613			if (!(dev->flags & P2P_DEV_FORCE_FREQ)) {
614				wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
615					"P2P: Use default channel settings");
616				p2p->op_reg_class = p2p->cfg->op_reg_class;
617				p2p->op_channel = p2p->cfg->op_channel;
618				os_memcpy(&p2p->channels, &p2p->cfg->channels,
619					  sizeof(struct p2p_channels));
620			} else {
621				wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
622					"P2P: Use previously configured "
623					"forced channel settings");
624			}
625		}
626
627		dev->flags &= ~P2P_DEV_NOT_YET_READY;
628
629		if (!msg.go_intent) {
630			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
631				"P2P: No GO Intent attribute received");
632			goto fail;
633		}
634		if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) {
635			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
636				"P2P: Invalid GO Intent value (%u) received",
637				*msg.go_intent >> 1);
638			goto fail;
639		}
640
641		if (dev->go_neg_req_sent &&
642		    os_memcmp(sa, p2p->cfg->dev_addr, ETH_ALEN) > 0) {
643			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
644				"P2P: Do not reply since peer has higher "
645				"address and GO Neg Request already sent");
646			p2p_parse_free(&msg);
647			return;
648		}
649
650		go = p2p_go_det(p2p->go_intent, *msg.go_intent);
651		if (go < 0) {
652			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
653				"P2P: Incompatible GO Intent");
654			status = P2P_SC_FAIL_BOTH_GO_INTENT_15;
655			goto fail;
656		}
657
658		if (p2p_peer_channels(p2p, dev, msg.channel_list,
659				      msg.channel_list_len) < 0) {
660			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
661				"P2P: No common channels found");
662			status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
663			goto fail;
664		}
665
666		switch (msg.dev_password_id) {
667		case DEV_PW_REGISTRAR_SPECIFIED:
668			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
669				"P2P: PIN from peer Display");
670			if (dev->wps_method != WPS_PIN_KEYPAD) {
671				wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
672					"P2P: We have wps_method=%s -> "
673					"incompatible",
674					p2p_wps_method_str(dev->wps_method));
675				status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
676				goto fail;
677			}
678			break;
679		case DEV_PW_USER_SPECIFIED:
680			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
681				"P2P: Peer entered PIN on Keypad");
682			if (dev->wps_method != WPS_PIN_DISPLAY) {
683				wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
684					"P2P: We have wps_method=%s -> "
685					"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_PUSHBUTTON:
692			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
693				"P2P: Peer using pushbutton");
694			if (dev->wps_method != WPS_PBC) {
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		default:
704			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
705				"P2P: Unsupported Device Password ID %d",
706				msg.dev_password_id);
707			status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
708			goto fail;
709		}
710
711		if (go && p2p_go_select_channel(p2p, dev, &status) < 0)
712			goto fail;
713
714		dev->go_state = go ? LOCAL_GO : REMOTE_GO;
715		dev->oper_freq = p2p_channel_to_freq((const char *)
716						     msg.operating_channel,
717						     msg.operating_channel[3],
718						     msg.operating_channel[4]);
719		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer operating "
720			"channel preference: %d MHz", dev->oper_freq);
721
722		if (msg.config_timeout) {
723			dev->go_timeout = msg.config_timeout[0];
724			dev->client_timeout = msg.config_timeout[1];
725		}
726
727		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
728			"P2P: GO Negotiation with " MACSTR, MAC2STR(sa));
729		if (p2p->state != P2P_IDLE)
730			p2p_stop_find_for_freq(p2p, rx_freq);
731		p2p_set_state(p2p, P2P_GO_NEG);
732		p2p_clear_timeout(p2p);
733		dev->dialog_token = msg.dialog_token;
734		os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN);
735		p2p->go_neg_peer = dev;
736		status = P2P_SC_SUCCESS;
737	}
738
739fail:
740	if (dev)
741		dev->status = status;
742	resp = p2p_build_go_neg_resp(p2p, dev, msg.dialog_token, status,
743				     !tie_breaker);
744	p2p_parse_free(&msg);
745	if (resp == NULL)
746		return;
747	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
748		"P2P: Sending GO Negotiation Response");
749	if (rx_freq > 0)
750		freq = rx_freq;
751	else
752		freq = p2p_channel_to_freq(p2p->cfg->country,
753					   p2p->cfg->reg_class,
754					   p2p->cfg->channel);
755	if (freq < 0) {
756		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
757			"P2P: Unknown regulatory class/channel");
758		wpabuf_free(resp);
759		return;
760	}
761	if (status == P2P_SC_SUCCESS) {
762		p2p->pending_action_state = P2P_PENDING_GO_NEG_RESPONSE;
763		dev->flags |= P2P_DEV_WAIT_GO_NEG_CONFIRM;
764		if (os_memcmp(sa, p2p->cfg->dev_addr, ETH_ALEN) < 0) {
765			/*
766			 * Peer has smaller address, so the GO Negotiation
767			 * Response from us is expected to complete
768			 * negotiation. Ignore a GO Negotiation Response from
769			 * the peer if it happens to be received after this
770			 * point due to a race condition in GO Negotiation
771			 * Request transmission and processing.
772			 */
773			dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
774		}
775	} else
776		p2p->pending_action_state =
777			P2P_PENDING_GO_NEG_RESPONSE_FAILURE;
778	if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr,
779			    p2p->cfg->dev_addr,
780			    wpabuf_head(resp), wpabuf_len(resp), 250) < 0) {
781		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
782			"P2P: Failed to send Action frame");
783	}
784
785	wpabuf_free(resp);
786}
787
788
789static struct wpabuf * p2p_build_go_neg_conf(struct p2p_data *p2p,
790					     struct p2p_device *peer,
791					     u8 dialog_token, u8 status,
792					     const u8 *resp_chan, int go)
793{
794	struct wpabuf *buf;
795	u8 *len;
796	struct p2p_channels res;
797	u8 group_capab;
798	size_t extra = 0;
799
800	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
801		"P2P: Building GO Negotiation Confirm");
802
803#ifdef CONFIG_WIFI_DISPLAY
804	if (p2p->wfd_ie_go_neg)
805		extra = wpabuf_len(p2p->wfd_ie_go_neg);
806#endif /* CONFIG_WIFI_DISPLAY */
807
808	buf = wpabuf_alloc(1000 + extra);
809	if (buf == NULL)
810		return NULL;
811
812	p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_CONF, dialog_token);
813
814	len = p2p_buf_add_ie_hdr(buf);
815	p2p_buf_add_status(buf, status);
816	group_capab = 0;
817	if (peer->go_state == LOCAL_GO) {
818		if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
819			group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
820			if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
821				group_capab |=
822					P2P_GROUP_CAPAB_PERSISTENT_RECONN;
823		}
824		if (p2p->cross_connect)
825			group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
826		if (p2p->cfg->p2p_intra_bss)
827			group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
828	}
829	p2p_buf_add_capability(buf, p2p->dev_capab &
830			       ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY,
831			       group_capab);
832	if (go || resp_chan == NULL)
833		p2p_buf_add_operating_channel(buf, p2p->cfg->country,
834					      p2p->op_reg_class,
835					      p2p->op_channel);
836	else
837		p2p_buf_add_operating_channel(buf, (const char *) resp_chan,
838					      resp_chan[3], resp_chan[4]);
839	p2p_channels_intersect(&p2p->channels, &peer->channels, &res);
840	p2p_buf_add_channel_list(buf, p2p->cfg->country, &res);
841	if (go) {
842		p2p_buf_add_group_id(buf, p2p->cfg->dev_addr, p2p->ssid,
843				     p2p->ssid_len);
844	}
845	p2p_buf_update_ie_hdr(buf, len);
846
847#ifdef CONFIG_WIFI_DISPLAY
848	if (p2p->wfd_ie_go_neg)
849		wpabuf_put_buf(buf, p2p->wfd_ie_go_neg);
850#endif /* CONFIG_WIFI_DISPLAY */
851
852	return buf;
853}
854
855
856void p2p_process_go_neg_resp(struct p2p_data *p2p, const u8 *sa,
857			     const u8 *data, size_t len, int rx_freq)
858{
859	struct p2p_device *dev;
860	struct wpabuf *conf;
861	int go = -1;
862	struct p2p_message msg;
863	u8 status = P2P_SC_SUCCESS;
864	int freq;
865
866	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
867		"P2P: Received GO Negotiation Response from " MACSTR
868		" (freq=%d)", MAC2STR(sa), rx_freq);
869	dev = p2p_get_device(p2p, sa);
870	if (dev == NULL || dev->wps_method == WPS_NOT_READY ||
871	    dev != p2p->go_neg_peer) {
872		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
873			"P2P: Not ready for GO negotiation with " MACSTR,
874			MAC2STR(sa));
875		return;
876	}
877
878	if (p2p_parse(data, len, &msg))
879		return;
880
881	if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE)) {
882		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
883			"P2P: Was not expecting GO Negotiation Response - "
884			"ignore");
885		p2p_parse_free(&msg);
886		return;
887	}
888	dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
889
890	if (msg.dialog_token != dev->dialog_token) {
891		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
892			"P2P: Unexpected Dialog Token %u (expected %u)",
893			msg.dialog_token, dev->dialog_token);
894		p2p_parse_free(&msg);
895		return;
896	}
897
898	if (!msg.status) {
899		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
900			"P2P: No Status attribute received");
901		status = P2P_SC_FAIL_INVALID_PARAMS;
902		goto fail;
903	}
904	if (*msg.status) {
905		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
906			"P2P: GO Negotiation rejected: status %d",
907			*msg.status);
908		dev->go_neg_req_sent = 0;
909		if (*msg.status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
910			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
911				"P2P: Wait for the peer to become ready for "
912				"GO Negotiation");
913			dev->flags |= P2P_DEV_NOT_YET_READY;
914			dev->wait_count = 0;
915			p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
916			p2p_set_timeout(p2p, 0, 0);
917		} else {
918			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
919				"P2P: Stop GO Negotiation attempt");
920			p2p_go_neg_failed(p2p, dev, *msg.status);
921		}
922		p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
923		p2p_parse_free(&msg);
924		return;
925	}
926
927	if (!msg.capability) {
928		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
929			"P2P: Mandatory Capability attribute missing from GO "
930			"Negotiation Response");
931#ifdef CONFIG_P2P_STRICT
932		status = P2P_SC_FAIL_INVALID_PARAMS;
933		goto fail;
934#endif /* CONFIG_P2P_STRICT */
935	}
936
937	if (!msg.p2p_device_info) {
938		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
939			"P2P: Mandatory P2P Device Info attribute missing "
940			"from GO Negotiation Response");
941#ifdef CONFIG_P2P_STRICT
942		status = P2P_SC_FAIL_INVALID_PARAMS;
943		goto fail;
944#endif /* CONFIG_P2P_STRICT */
945	}
946
947	if (!msg.intended_addr) {
948		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
949			"P2P: No Intended P2P Interface Address attribute "
950			"received");
951		status = P2P_SC_FAIL_INVALID_PARAMS;
952		goto fail;
953	}
954
955	if (!msg.go_intent) {
956		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
957			"P2P: No GO Intent attribute received");
958		status = P2P_SC_FAIL_INVALID_PARAMS;
959		goto fail;
960	}
961	if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) {
962		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
963			"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		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
972			"P2P: Incompatible GO Intent");
973		status = P2P_SC_FAIL_INCOMPATIBLE_PARAMS;
974		goto fail;
975	}
976
977	if (!go && msg.group_id) {
978		/* Store SSID for Provisioning step */
979		p2p->ssid_len = msg.group_id_len - ETH_ALEN;
980		os_memcpy(p2p->ssid, msg.group_id + ETH_ALEN, p2p->ssid_len);
981	} else if (!go) {
982		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
983			"P2P: Mandatory P2P Group ID attribute missing from "
984			"GO Negotiation Response");
985		p2p->ssid_len = 0;
986		status = P2P_SC_FAIL_INVALID_PARAMS;
987		goto fail;
988	}
989
990	if (!msg.config_timeout) {
991		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
992			"P2P: Mandatory Configuration Timeout attribute "
993			"missing from GO Negotiation Response");
994#ifdef CONFIG_P2P_STRICT
995		status = P2P_SC_FAIL_INVALID_PARAMS;
996		goto fail;
997#endif /* CONFIG_P2P_STRICT */
998	} else {
999		dev->go_timeout = msg.config_timeout[0];
1000		dev->client_timeout = msg.config_timeout[1];
1001	}
1002
1003	if (!msg.operating_channel && !go) {
1004		/*
1005		 * Note: P2P Client may omit Operating Channel attribute to
1006		 * indicate it does not have a preference.
1007		 */
1008		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1009			"P2P: No Operating Channel attribute received");
1010		status = P2P_SC_FAIL_INVALID_PARAMS;
1011		goto fail;
1012	}
1013	if (!msg.channel_list) {
1014		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1015			"P2P: No Channel List attribute received");
1016		status = P2P_SC_FAIL_INVALID_PARAMS;
1017		goto fail;
1018	}
1019
1020	if (p2p_peer_channels(p2p, dev, msg.channel_list,
1021			      msg.channel_list_len) < 0) {
1022		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1023			"P2P: No common channels found");
1024		status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
1025		goto fail;
1026	}
1027
1028	if (msg.operating_channel) {
1029		dev->oper_freq = p2p_channel_to_freq((const char *)
1030						     msg.operating_channel,
1031						     msg.operating_channel[3],
1032						     msg.operating_channel[4]);
1033		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer operating "
1034			"channel preference: %d MHz", dev->oper_freq);
1035	} else
1036		dev->oper_freq = 0;
1037
1038	switch (msg.dev_password_id) {
1039	case DEV_PW_REGISTRAR_SPECIFIED:
1040		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1041			"P2P: PIN from peer Display");
1042		if (dev->wps_method != WPS_PIN_KEYPAD) {
1043			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1044				"P2P: We have wps_method=%s -> "
1045				"incompatible",
1046				p2p_wps_method_str(dev->wps_method));
1047			status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1048			goto fail;
1049		}
1050		break;
1051	case DEV_PW_USER_SPECIFIED:
1052		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1053			"P2P: Peer entered PIN on Keypad");
1054		if (dev->wps_method != WPS_PIN_DISPLAY) {
1055			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1056				"P2P: We have wps_method=%s -> "
1057				"incompatible",
1058				p2p_wps_method_str(dev->wps_method));
1059			status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1060			goto fail;
1061		}
1062		break;
1063	case DEV_PW_PUSHBUTTON:
1064		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1065			"P2P: Peer using pushbutton");
1066		if (dev->wps_method != WPS_PBC) {
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	default:
1076		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1077			"P2P: Unsupported Device Password ID %d",
1078			msg.dev_password_id);
1079		status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1080		goto fail;
1081	}
1082
1083	if (go && p2p_go_select_channel(p2p, dev, &status) < 0)
1084		goto fail;
1085
1086	p2p_set_state(p2p, P2P_GO_NEG);
1087	p2p_clear_timeout(p2p);
1088
1089	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1090		"P2P: GO Negotiation with " MACSTR, MAC2STR(sa));
1091	os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN);
1092
1093fail:
1094	conf = p2p_build_go_neg_conf(p2p, dev, msg.dialog_token, status,
1095				     msg.operating_channel, go);
1096	p2p_parse_free(&msg);
1097	if (conf == NULL)
1098		return;
1099	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1100		"P2P: Sending GO Negotiation Confirm");
1101	if (status == P2P_SC_SUCCESS) {
1102		p2p->pending_action_state = P2P_PENDING_GO_NEG_CONFIRM;
1103		dev->go_state = go ? LOCAL_GO : REMOTE_GO;
1104	} else
1105		p2p->pending_action_state = P2P_NO_PENDING_ACTION;
1106	if (rx_freq > 0)
1107		freq = rx_freq;
1108	else
1109		freq = dev->listen_freq;
1110	if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr, sa,
1111			    wpabuf_head(conf), wpabuf_len(conf), 0) < 0) {
1112		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1113			"P2P: Failed to send Action frame");
1114		p2p_go_neg_failed(p2p, dev, -1);
1115	}
1116	wpabuf_free(conf);
1117	if (status != P2P_SC_SUCCESS) {
1118		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1119			"P2P: GO Negotiation failed");
1120		p2p_go_neg_failed(p2p, dev, status);
1121	}
1122}
1123
1124
1125void p2p_process_go_neg_conf(struct p2p_data *p2p, const u8 *sa,
1126			     const u8 *data, size_t len)
1127{
1128	struct p2p_device *dev;
1129	struct p2p_message msg;
1130
1131	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1132		"P2P: Received GO Negotiation Confirm from " MACSTR,
1133		MAC2STR(sa));
1134	dev = p2p_get_device(p2p, sa);
1135	if (dev == NULL || dev->wps_method == WPS_NOT_READY ||
1136	    dev != p2p->go_neg_peer) {
1137		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1138			"P2P: Not ready for GO negotiation with " MACSTR,
1139			MAC2STR(sa));
1140		return;
1141	}
1142
1143	if (p2p->pending_action_state == P2P_PENDING_GO_NEG_RESPONSE) {
1144		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Stopped waiting "
1145			"for TX status on GO Negotiation Response since we "
1146			"already received Confirmation");
1147		p2p->pending_action_state = P2P_NO_PENDING_ACTION;
1148	}
1149
1150	if (p2p_parse(data, len, &msg))
1151		return;
1152
1153	if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
1154		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1155			"P2P: Was not expecting GO Negotiation Confirm - "
1156			"ignore");
1157		return;
1158	}
1159	dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
1160
1161	if (msg.dialog_token != dev->dialog_token) {
1162		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1163			"P2P: Unexpected Dialog Token %u (expected %u)",
1164			msg.dialog_token, dev->dialog_token);
1165		p2p_parse_free(&msg);
1166		return;
1167	}
1168
1169	if (!msg.status) {
1170		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1171			"P2P: No Status attribute received");
1172		p2p_parse_free(&msg);
1173		return;
1174	}
1175	if (*msg.status) {
1176		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1177			"P2P: GO Negotiation rejected: status %d",
1178			*msg.status);
1179		p2p_go_neg_failed(p2p, dev, *msg.status);
1180		p2p_parse_free(&msg);
1181		return;
1182	}
1183
1184	if (dev->go_state == REMOTE_GO && msg.group_id) {
1185		/* Store SSID for Provisioning step */
1186		p2p->ssid_len = msg.group_id_len - ETH_ALEN;
1187		os_memcpy(p2p->ssid, msg.group_id + ETH_ALEN, p2p->ssid_len);
1188	} else if (dev->go_state == REMOTE_GO) {
1189		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1190			"P2P: Mandatory P2P Group ID attribute missing from "
1191			"GO Negotiation Confirmation");
1192		p2p->ssid_len = 0;
1193		p2p_go_neg_failed(p2p, dev, P2P_SC_FAIL_INVALID_PARAMS);
1194		p2p_parse_free(&msg);
1195		return;
1196	}
1197
1198	if (!msg.operating_channel) {
1199		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1200			"P2P: Mandatory Operating Channel attribute missing "
1201			"from GO Negotiation Confirmation");
1202#ifdef CONFIG_P2P_STRICT
1203		p2p_parse_free(&msg);
1204		return;
1205#endif /* CONFIG_P2P_STRICT */
1206	}
1207
1208#ifdef ANDROID_P2P
1209	if (msg.operating_channel) {
1210		dev->oper_freq = p2p_channel_to_freq((const char *)
1211						     msg.operating_channel,
1212						     msg.operating_channel[3],
1213						     msg.operating_channel[4]);
1214		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer operating "
1215			"channel preference: %d MHz", dev->oper_freq);
1216	} else
1217		dev->oper_freq = 0;
1218#endif
1219
1220	if (!msg.channel_list) {
1221		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1222			"P2P: Mandatory Operating Channel attribute missing "
1223			"from GO Negotiation Confirmation");
1224#ifdef CONFIG_P2P_STRICT
1225		p2p_parse_free(&msg);
1226		return;
1227#endif /* CONFIG_P2P_STRICT */
1228	}
1229
1230	p2p_parse_free(&msg);
1231
1232	if (dev->go_state == UNKNOWN_GO) {
1233		/*
1234		 * This should not happen since GO negotiation has already
1235		 * been completed.
1236		 */
1237		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1238			"P2P: Unexpected GO Neg state - do not know which end "
1239			"becomes GO");
1240		return;
1241	}
1242
1243	/*
1244	 * The peer could have missed our ctrl::ack frame for GO Negotiation
1245	 * Confirm and continue retransmitting the frame. To reduce the
1246	 * likelihood of the peer not getting successful TX status for the
1247	 * GO Negotiation Confirm frame, wait a short time here before starting
1248	 * the group so that we will remain on the current channel to
1249	 * acknowledge any possible retransmission from the peer.
1250	 */
1251	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: 20 ms wait on current "
1252		"channel before starting group");
1253	os_sleep(0, 20000);
1254
1255	p2p_go_complete(p2p, dev);
1256}
1257