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