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