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