p2p_go_neg.c revision 98f9e76624da6bb96edc1982c423e4a119c5170a
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
101static u16 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
138	buf = wpabuf_alloc(1000);
139	if (buf == NULL)
140		return NULL;
141
142	peer->dialog_token++;
143	if (peer->dialog_token == 0)
144		peer->dialog_token = 1;
145	p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_REQ, peer->dialog_token);
146
147	len = p2p_buf_add_ie_hdr(buf);
148	group_capab = 0;
149	if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
150		group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
151		if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
152			group_capab |= P2P_GROUP_CAPAB_PERSISTENT_RECONN;
153	}
154	if (p2p->cross_connect)
155		group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
156	if (p2p->cfg->p2p_intra_bss)
157		group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
158	p2p_buf_add_capability(buf, p2p->dev_capab, group_capab);
159	p2p_buf_add_go_intent(buf, (p2p->go_intent << 1) |
160			      p2p->next_tie_breaker);
161	p2p->next_tie_breaker = !p2p->next_tie_breaker;
162	p2p_buf_add_config_timeout(buf, 100, 20);
163	p2p_buf_add_listen_channel(buf, p2p->cfg->country, p2p->cfg->reg_class,
164				   p2p->cfg->channel);
165	if (p2p->ext_listen_interval)
166		p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
167					      p2p->ext_listen_interval);
168	p2p_buf_add_intended_addr(buf, p2p->intended_addr);
169	p2p_buf_add_channel_list(buf, p2p->cfg->country, &p2p->channels);
170	p2p_buf_add_device_info(buf, p2p, peer);
171	p2p_buf_add_operating_channel(buf, p2p->cfg->country,
172				      p2p->op_reg_class, p2p->op_channel);
173	p2p_buf_update_ie_hdr(buf, len);
174
175	/* WPS IE with Device Password ID attribute */
176	p2p_build_wps_ie(p2p, buf, p2p_wps_method_pw_id(peer->wps_method), 0);
177
178	return buf;
179}
180
181
182int p2p_connect_send(struct p2p_data *p2p, struct p2p_device *dev)
183{
184	struct wpabuf *req;
185	int freq;
186
187	freq = dev->listen_freq > 0 ? dev->listen_freq : dev->oper_freq;
188	if (freq <= 0) {
189		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
190			"P2P: No Listen/Operating frequency known for the "
191			"peer " MACSTR " to send GO Negotiation Request",
192			MAC2STR(dev->info.p2p_device_addr));
193		return -1;
194	}
195
196	req = p2p_build_go_neg_req(p2p, dev);
197	if (req == NULL)
198		return -1;
199	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
200		"P2P: Sending GO Negotiation Request");
201	p2p_set_state(p2p, P2P_CONNECT);
202	p2p->pending_action_state = P2P_PENDING_GO_NEG_REQUEST;
203	p2p->go_neg_peer = dev;
204	dev->flags |= P2P_DEV_WAIT_GO_NEG_RESPONSE;
205	dev->connect_reqs++;
206#ifdef ANDROID_P2P
207	dev->go_neg_req_sent++;
208#endif
209	if (p2p_send_action(p2p, freq, dev->info.p2p_device_addr,
210			    p2p->cfg->dev_addr, dev->info.p2p_device_addr,
211			    wpabuf_head(req), wpabuf_len(req), 200) < 0) {
212		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
213			"P2P: Failed to send Action frame");
214		/* Use P2P find to recover and retry */
215		p2p_set_timeout(p2p, 0, 0);
216	}
217
218	wpabuf_free(req);
219
220	return 0;
221}
222
223
224static struct wpabuf * p2p_build_go_neg_resp(struct p2p_data *p2p,
225					     struct p2p_device *peer,
226					     u8 dialog_token, u8 status,
227					     u8 tie_breaker)
228{
229	struct wpabuf *buf;
230	u8 *len;
231	u8 group_capab;
232
233	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
234		"P2P: Building GO Negotiation Response");
235	buf = wpabuf_alloc(1000);
236	if (buf == NULL)
237		return NULL;
238
239	p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_RESP, dialog_token);
240
241	len = p2p_buf_add_ie_hdr(buf);
242	p2p_buf_add_status(buf, status);
243	group_capab = 0;
244	if (peer && peer->go_state == LOCAL_GO) {
245		if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
246			group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
247			if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
248				group_capab |=
249					P2P_GROUP_CAPAB_PERSISTENT_RECONN;
250		}
251		if (p2p->cross_connect)
252			group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
253		if (p2p->cfg->p2p_intra_bss)
254			group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
255	}
256	p2p_buf_add_capability(buf, p2p->dev_capab, group_capab);
257	p2p_buf_add_go_intent(buf, (p2p->go_intent << 1) | tie_breaker);
258	p2p_buf_add_config_timeout(buf, 100, 20);
259	if (peer && peer->go_state == REMOTE_GO) {
260		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Omit Operating "
261			"Channel attribute");
262	} else {
263		p2p_buf_add_operating_channel(buf, p2p->cfg->country,
264					      p2p->op_reg_class,
265					      p2p->op_channel);
266	}
267	p2p_buf_add_intended_addr(buf, p2p->intended_addr);
268	if (status || peer == NULL) {
269		p2p_buf_add_channel_list(buf, p2p->cfg->country,
270					 &p2p->channels);
271	} else if (peer->go_state == REMOTE_GO) {
272		p2p_buf_add_channel_list(buf, p2p->cfg->country,
273					 &p2p->channels);
274	} else {
275		struct p2p_channels res;
276		p2p_channels_intersect(&p2p->channels, &peer->channels,
277				       &res);
278		p2p_buf_add_channel_list(buf, p2p->cfg->country, &res);
279	}
280	p2p_buf_add_device_info(buf, p2p, peer);
281	if (peer && peer->go_state == LOCAL_GO) {
282		p2p_buf_add_group_id(buf, p2p->cfg->dev_addr, p2p->ssid,
283				     p2p->ssid_len);
284	}
285	p2p_buf_update_ie_hdr(buf, len);
286
287	/* WPS IE with Device Password ID attribute */
288	p2p_build_wps_ie(p2p, buf,
289			 p2p_wps_method_pw_id(peer ? peer->wps_method :
290					      WPS_NOT_READY), 0);
291
292	return buf;
293}
294
295
296static void p2p_reselect_channel(struct p2p_data *p2p,
297				 struct p2p_channels *intersection)
298{
299	struct p2p_reg_class *cl;
300	int freq;
301	u8 op_reg_class, op_channel;
302
303	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Selected operating "
304		"channel (reg_class %u channel %u) not acceptable to the "
305		"peer", p2p->op_reg_class, p2p->op_channel);
306
307	/* First, try to pick the best channel from another band */
308	freq = p2p_channel_to_freq(p2p->cfg->country, p2p->op_reg_class,
309				   p2p->op_channel);
310	if (freq >= 2400 && freq < 2500 && p2p->best_freq_5 > 0 &&
311	    p2p_freq_to_channel(p2p->cfg->country, p2p->best_freq_5,
312				&op_reg_class, &op_channel) == 0 &&
313	    p2p_channels_includes(intersection, op_reg_class, op_channel)) {
314		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Pick best 5 GHz "
315			"channel (reg_class %u channel %u) from intersection",
316			op_reg_class, op_channel);
317		p2p->op_reg_class = op_reg_class;
318		p2p->op_channel = op_channel;
319		return;
320	}
321
322	if (freq >= 4900 && freq < 6000 && p2p->best_freq_24 > 0 &&
323	    p2p_freq_to_channel(p2p->cfg->country, p2p->best_freq_24,
324				&op_reg_class, &op_channel) == 0 &&
325	    p2p_channels_includes(intersection, op_reg_class, op_channel)) {
326		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Pick best 2.4 GHz "
327			"channel (reg_class %u channel %u) from intersection",
328			op_reg_class, op_channel);
329		p2p->op_reg_class = op_reg_class;
330		p2p->op_channel = op_channel;
331		return;
332	}
333
334	/*
335	 * Fall back to whatever is included in the channel intersection since
336	 * no better options seems to be available.
337	 */
338	cl = &intersection->reg_class[0];
339	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Pick another channel "
340		"(reg_class %u channel %u) from intersection",
341		cl->reg_class, cl->channel[0]);
342	p2p->op_reg_class = cl->reg_class;
343	p2p->op_channel = cl->channel[0];
344}
345
346
347void p2p_process_go_neg_req(struct p2p_data *p2p, const u8 *sa,
348			    const u8 *data, size_t len, int rx_freq)
349{
350	struct p2p_device *dev = NULL;
351	struct wpabuf *resp;
352	struct p2p_message msg;
353	u8 status = P2P_SC_FAIL_INVALID_PARAMS;
354	int tie_breaker = 0;
355	int freq;
356
357	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
358		"P2P: Received GO Negotiation Request from " MACSTR
359		"(freq=%d)", MAC2STR(sa), rx_freq);
360
361	if (p2p_parse(data, len, &msg))
362		return;
363
364	if (!msg.capability) {
365		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
366			"P2P: Mandatory Capability attribute missing from GO "
367			"Negotiation Request");
368#ifdef CONFIG_P2P_STRICT
369		goto fail;
370#endif /* CONFIG_P2P_STRICT */
371	}
372
373	if (msg.go_intent)
374		tie_breaker = *msg.go_intent & 0x01;
375	else {
376		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
377			"P2P: Mandatory GO Intent attribute missing from GO "
378			"Negotiation Request");
379#ifdef CONFIG_P2P_STRICT
380		goto fail;
381#endif /* CONFIG_P2P_STRICT */
382	}
383
384	if (!msg.config_timeout) {
385		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
386			"P2P: Mandatory Configuration Timeout attribute "
387			"missing from GO Negotiation Request");
388#ifdef CONFIG_P2P_STRICT
389		goto fail;
390#endif /* CONFIG_P2P_STRICT */
391	}
392
393	if (!msg.listen_channel) {
394		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
395			"P2P: No Listen Channel attribute received");
396		goto fail;
397	}
398	if (!msg.operating_channel) {
399		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
400			"P2P: No Operating Channel attribute received");
401		goto fail;
402	}
403	if (!msg.channel_list) {
404		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
405			"P2P: No Channel List attribute received");
406		goto fail;
407	}
408	if (!msg.intended_addr) {
409		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
410			"P2P: No Intended P2P Interface Address attribute "
411			"received");
412		goto fail;
413	}
414	if (!msg.p2p_device_info) {
415		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
416			"P2P: No P2P Device Info attribute received");
417		goto fail;
418	}
419
420	if (os_memcmp(msg.p2p_device_addr, sa, ETH_ALEN) != 0) {
421		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
422			"P2P: Unexpected GO Negotiation Request SA=" MACSTR
423			" != dev_addr=" MACSTR,
424			MAC2STR(sa), MAC2STR(msg.p2p_device_addr));
425		goto fail;
426	}
427
428	dev = p2p_get_device(p2p, sa);
429
430	if (msg.status && *msg.status) {
431		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
432			"P2P: Unexpected Status attribute (%d) in GO "
433			"Negotiation Request", *msg.status);
434		goto fail;
435	}
436
437	if (dev == NULL)
438		dev = p2p_add_dev_from_go_neg_req(p2p, sa, &msg);
439	else if (dev->flags & P2P_DEV_PROBE_REQ_ONLY)
440		p2p_add_dev_info(p2p, sa, dev, &msg);
441	if (dev && dev->flags & P2P_DEV_USER_REJECTED) {
442		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
443			"P2P: User has rejected this peer");
444		status = P2P_SC_FAIL_REJECTED_BY_USER;
445	} else if (dev == NULL || dev->wps_method == WPS_NOT_READY) {
446		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
447			"P2P: Not ready for GO negotiation with " MACSTR,
448			MAC2STR(sa));
449		status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
450		if (dev)
451			dev->flags |= P2P_DEV_PEER_WAITING_RESPONSE;
452		p2p->cfg->go_neg_req_rx(p2p->cfg->cb_ctx, sa,
453					msg.dev_password_id);
454	} else if (p2p->go_neg_peer && p2p->go_neg_peer != dev) {
455		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
456			"P2P: Already in Group Formation with another peer");
457		status = P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
458	} else {
459		int go;
460
461		if (!p2p->go_neg_peer) {
462			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting "
463				"GO Negotiation with previously authorized "
464				"peer");
465			if (!(dev->flags & P2P_DEV_FORCE_FREQ)) {
466				wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
467					"P2P: Use default channel settings");
468				p2p->op_reg_class = p2p->cfg->op_reg_class;
469				p2p->op_channel = p2p->cfg->op_channel;
470				os_memcpy(&p2p->channels, &p2p->cfg->channels,
471					  sizeof(struct p2p_channels));
472			} else {
473				wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
474					"P2P: Use previously configured "
475					"forced channel settings");
476			}
477		}
478
479		dev->flags &= ~P2P_DEV_NOT_YET_READY;
480
481		if (!msg.go_intent) {
482			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
483				"P2P: No GO Intent attribute received");
484			goto fail;
485		}
486		if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) {
487			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
488				"P2P: Invalid GO Intent value (%u) received",
489				*msg.go_intent >> 1);
490			goto fail;
491		}
492
493		if (dev->go_neg_req_sent &&
494		    os_memcmp(sa, p2p->cfg->dev_addr, ETH_ALEN) > 0) {
495			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
496				"P2P: Do not reply since peer has higher "
497				"address and GO Neg Request already sent");
498			p2p_parse_free(&msg);
499			return;
500		}
501
502		go = p2p_go_det(p2p->go_intent, *msg.go_intent);
503		if (go < 0) {
504			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
505				"P2P: Incompatible GO Intent");
506			status = P2P_SC_FAIL_BOTH_GO_INTENT_15;
507			goto fail;
508		}
509
510		if (p2p_peer_channels(p2p, dev, msg.channel_list,
511				      msg.channel_list_len) < 0) {
512			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
513				"P2P: No common channels found");
514			status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
515			goto fail;
516		}
517
518		switch (msg.dev_password_id) {
519		case DEV_PW_REGISTRAR_SPECIFIED:
520			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
521				"P2P: PIN from peer Display");
522			if (dev->wps_method != WPS_PIN_KEYPAD) {
523				wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
524					"P2P: We have wps_method=%s -> "
525					"incompatible",
526					p2p_wps_method_str(dev->wps_method));
527				status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
528				goto fail;
529			}
530			break;
531		case DEV_PW_USER_SPECIFIED:
532			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
533				"P2P: Peer entered PIN on Keypad");
534			if (dev->wps_method != WPS_PIN_DISPLAY) {
535				wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
536					"P2P: We have wps_method=%s -> "
537					"incompatible",
538					p2p_wps_method_str(dev->wps_method));
539				status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
540				goto fail;
541			}
542			break;
543		case DEV_PW_PUSHBUTTON:
544			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
545				"P2P: Peer using pushbutton");
546			if (dev->wps_method != WPS_PBC) {
547				wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
548					"P2P: We have wps_method=%s -> "
549					"incompatible",
550					p2p_wps_method_str(dev->wps_method));
551				status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
552				goto fail;
553			}
554			break;
555		default:
556			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
557				"P2P: Unsupported Device Password ID %d",
558				msg.dev_password_id);
559			status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
560			goto fail;
561		}
562
563		if (go) {
564			struct p2p_channels intersection;
565			size_t i;
566			p2p_channels_intersect(&p2p->channels, &dev->channels,
567					       &intersection);
568			if (intersection.reg_classes == 0 ||
569			    intersection.reg_class[0].channels == 0) {
570				status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
571				wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
572					"P2P: No common channels found");
573				goto fail;
574			}
575			for (i = 0; i < intersection.reg_classes; i++) {
576				struct p2p_reg_class *c;
577				c = &intersection.reg_class[i];
578				wpa_printf(MSG_DEBUG, "P2P: reg_class %u",
579					   c->reg_class);
580				wpa_hexdump(MSG_DEBUG, "P2P: channels",
581					    c->channel, c->channels);
582			}
583			if (!p2p_channels_includes(&intersection,
584						   p2p->op_reg_class,
585						   p2p->op_channel))
586				p2p_reselect_channel(p2p, &intersection);
587
588			if (!p2p->ssid_set) {
589				p2p_build_ssid(p2p, p2p->ssid, &p2p->ssid_len);
590				p2p->ssid_set = 1;
591			}
592		}
593
594		dev->go_state = go ? LOCAL_GO : REMOTE_GO;
595		dev->oper_freq = p2p_channel_to_freq((const char *)
596						     msg.operating_channel,
597						     msg.operating_channel[3],
598						     msg.operating_channel[4]);
599		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer operating "
600			"channel preference: %d MHz", dev->oper_freq);
601
602		if (msg.config_timeout) {
603			dev->go_timeout = msg.config_timeout[0];
604			dev->client_timeout = msg.config_timeout[1];
605		}
606
607		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
608			"P2P: GO Negotiation with " MACSTR, MAC2STR(sa));
609		if (p2p->state != P2P_IDLE)
610			p2p_stop_find_for_freq(p2p, rx_freq);
611		p2p_set_state(p2p, P2P_GO_NEG);
612		p2p_clear_timeout(p2p);
613		dev->dialog_token = msg.dialog_token;
614		os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN);
615		p2p->go_neg_peer = dev;
616		status = P2P_SC_SUCCESS;
617	}
618
619fail:
620	if (dev)
621		dev->status = status;
622	resp = p2p_build_go_neg_resp(p2p, dev, msg.dialog_token, status,
623				     !tie_breaker);
624	p2p_parse_free(&msg);
625	if (resp == NULL)
626		return;
627	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
628		"P2P: Sending GO Negotiation Response");
629	if (rx_freq > 0)
630		freq = rx_freq;
631	else
632		freq = p2p_channel_to_freq(p2p->cfg->country,
633					   p2p->cfg->reg_class,
634					   p2p->cfg->channel);
635	if (freq < 0) {
636		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
637			"P2P: Unknown regulatory class/channel");
638		wpabuf_free(resp);
639		return;
640	}
641	if (status == P2P_SC_SUCCESS) {
642		p2p->pending_action_state = P2P_PENDING_GO_NEG_RESPONSE;
643		dev->flags |= P2P_DEV_WAIT_GO_NEG_CONFIRM;
644	} else
645		p2p->pending_action_state =
646			P2P_PENDING_GO_NEG_RESPONSE_FAILURE;
647	if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr,
648			    p2p->cfg->dev_addr,
649			    wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
650		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
651			"P2P: Failed to send Action frame");
652	}
653
654	wpabuf_free(resp);
655}
656
657
658static struct wpabuf * p2p_build_go_neg_conf(struct p2p_data *p2p,
659					     struct p2p_device *peer,
660					     u8 dialog_token, u8 status,
661					     const u8 *resp_chan, int go)
662{
663	struct wpabuf *buf;
664	u8 *len;
665	struct p2p_channels res;
666	u8 group_capab;
667
668	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
669		"P2P: Building GO Negotiation Confirm");
670	buf = wpabuf_alloc(1000);
671	if (buf == NULL)
672		return NULL;
673
674	p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_CONF, dialog_token);
675
676	len = p2p_buf_add_ie_hdr(buf);
677	p2p_buf_add_status(buf, status);
678	group_capab = 0;
679	if (peer->go_state == LOCAL_GO) {
680		if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
681			group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
682			if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
683				group_capab |=
684					P2P_GROUP_CAPAB_PERSISTENT_RECONN;
685		}
686		if (p2p->cross_connect)
687			group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
688		if (p2p->cfg->p2p_intra_bss)
689			group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
690	}
691	p2p_buf_add_capability(buf, p2p->dev_capab, group_capab);
692	if (go || resp_chan == NULL)
693		p2p_buf_add_operating_channel(buf, p2p->cfg->country,
694					      p2p->op_reg_class,
695					      p2p->op_channel);
696	else
697		p2p_buf_add_operating_channel(buf, (const char *) resp_chan,
698					      resp_chan[3], resp_chan[4]);
699	p2p_channels_intersect(&p2p->channels, &peer->channels, &res);
700	p2p_buf_add_channel_list(buf, p2p->cfg->country, &res);
701	if (go) {
702		p2p_buf_add_group_id(buf, p2p->cfg->dev_addr, p2p->ssid,
703				     p2p->ssid_len);
704	}
705	p2p_buf_update_ie_hdr(buf, len);
706
707	return buf;
708}
709
710
711void p2p_process_go_neg_resp(struct p2p_data *p2p, const u8 *sa,
712			     const u8 *data, size_t len, int rx_freq)
713{
714	struct p2p_device *dev;
715	struct wpabuf *conf;
716	int go = -1;
717	struct p2p_message msg;
718	u8 status = P2P_SC_SUCCESS;
719	int freq;
720
721	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
722		"P2P: Received GO Negotiation Response from " MACSTR
723		" (freq=%d)", MAC2STR(sa), rx_freq);
724	dev = p2p_get_device(p2p, sa);
725	if (dev == NULL || dev->wps_method == WPS_NOT_READY ||
726	    dev != p2p->go_neg_peer) {
727		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
728			"P2P: Not ready for GO negotiation with " MACSTR,
729			MAC2STR(sa));
730		return;
731	}
732
733	if (p2p_parse(data, len, &msg))
734		return;
735
736	if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE)) {
737		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
738			"P2P: Was not expecting GO Negotiation Response - "
739			"ignore");
740		p2p_parse_free(&msg);
741		return;
742	}
743	dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
744
745	if (msg.dialog_token != dev->dialog_token) {
746		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
747			"P2P: Unexpected Dialog Token %u (expected %u)",
748			msg.dialog_token, dev->dialog_token);
749		p2p_parse_free(&msg);
750		return;
751	}
752
753	if (!msg.status) {
754		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
755			"P2P: No Status attribute received");
756		status = P2P_SC_FAIL_INVALID_PARAMS;
757		goto fail;
758	}
759	if (*msg.status) {
760		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
761			"P2P: GO Negotiation rejected: status %d",
762			*msg.status);
763		dev->go_neg_req_sent = 0;
764		if (*msg.status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
765			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
766				"P2P: Wait for the peer to become ready for "
767				"GO Negotiation");
768			dev->flags |= P2P_DEV_NOT_YET_READY;
769			dev->wait_count = 0;
770			p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
771			p2p_set_timeout(p2p, 0, 0);
772		} else {
773			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
774				"P2P: Stop GO Negotiation attempt");
775			p2p_go_neg_failed(p2p, dev, *msg.status);
776		}
777		p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
778		p2p_parse_free(&msg);
779		return;
780	}
781
782	if (!msg.capability) {
783		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
784			"P2P: Mandatory Capability attribute missing from GO "
785			"Negotiation Response");
786#ifdef CONFIG_P2P_STRICT
787		status = P2P_SC_FAIL_INVALID_PARAMS;
788		goto fail;
789#endif /* CONFIG_P2P_STRICT */
790	}
791
792	if (!msg.p2p_device_info) {
793		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
794			"P2P: Mandatory P2P Device Info attribute missing "
795			"from GO Negotiation Response");
796#ifdef CONFIG_P2P_STRICT
797		status = P2P_SC_FAIL_INVALID_PARAMS;
798		goto fail;
799#endif /* CONFIG_P2P_STRICT */
800	}
801
802	if (!msg.intended_addr) {
803		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
804			"P2P: No Intended P2P Interface Address attribute "
805			"received");
806		status = P2P_SC_FAIL_INVALID_PARAMS;
807		goto fail;
808	}
809
810	if (!msg.go_intent) {
811		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
812			"P2P: No GO Intent attribute received");
813		status = P2P_SC_FAIL_INVALID_PARAMS;
814		goto fail;
815	}
816	if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) {
817		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
818			"P2P: Invalid GO Intent value (%u) received",
819			*msg.go_intent >> 1);
820		status = P2P_SC_FAIL_INVALID_PARAMS;
821		goto fail;
822	}
823
824	go = p2p_go_det(p2p->go_intent, *msg.go_intent);
825	if (go < 0) {
826		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
827			"P2P: Incompatible GO Intent");
828		status = P2P_SC_FAIL_INCOMPATIBLE_PARAMS;
829		goto fail;
830	}
831
832	if (!go && msg.group_id) {
833		/* Store SSID for Provisioning step */
834		p2p->ssid_len = msg.group_id_len - ETH_ALEN;
835		os_memcpy(p2p->ssid, msg.group_id + ETH_ALEN, p2p->ssid_len);
836	} else if (!go) {
837		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
838			"P2P: Mandatory P2P Group ID attribute missing from "
839			"GO Negotiation Response");
840		p2p->ssid_len = 0;
841#ifdef CONFIG_P2P_STRICT
842		status = P2P_SC_FAIL_INVALID_PARAMS;
843		goto fail;
844#endif /* CONFIG_P2P_STRICT */
845	}
846
847	if (!msg.config_timeout) {
848		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
849			"P2P: Mandatory Configuration Timeout attribute "
850			"missing from GO Negotiation Response");
851#ifdef CONFIG_P2P_STRICT
852		status = P2P_SC_FAIL_INVALID_PARAMS;
853		goto fail;
854#endif /* CONFIG_P2P_STRICT */
855	} else {
856		dev->go_timeout = msg.config_timeout[0];
857		dev->client_timeout = msg.config_timeout[1];
858	}
859
860	if (!msg.operating_channel && !go) {
861		/*
862		 * Note: P2P Client may omit Operating Channel attribute to
863		 * indicate it does not have a preference.
864		 */
865		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
866			"P2P: No Operating Channel attribute received");
867		status = P2P_SC_FAIL_INVALID_PARAMS;
868		goto fail;
869	}
870	if (!msg.channel_list) {
871		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
872			"P2P: No Channel List attribute received");
873		status = P2P_SC_FAIL_INVALID_PARAMS;
874		goto fail;
875	}
876
877	if (p2p_peer_channels(p2p, dev, msg.channel_list,
878			      msg.channel_list_len) < 0) {
879		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
880			"P2P: No common channels found");
881		status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
882		goto fail;
883	}
884
885	if (msg.operating_channel) {
886		dev->oper_freq = p2p_channel_to_freq((const char *)
887						     msg.operating_channel,
888						     msg.operating_channel[3],
889						     msg.operating_channel[4]);
890		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer operating "
891			"channel preference: %d MHz", dev->oper_freq);
892	} else
893		dev->oper_freq = 0;
894
895	switch (msg.dev_password_id) {
896	case DEV_PW_REGISTRAR_SPECIFIED:
897		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
898			"P2P: PIN from peer Display");
899		if (dev->wps_method != WPS_PIN_KEYPAD) {
900			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
901				"P2P: We have wps_method=%s -> "
902				"incompatible",
903				p2p_wps_method_str(dev->wps_method));
904			status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
905			goto fail;
906		}
907		break;
908	case DEV_PW_USER_SPECIFIED:
909		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
910			"P2P: Peer entered PIN on Keypad");
911		if (dev->wps_method != WPS_PIN_DISPLAY) {
912			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
913				"P2P: We have wps_method=%s -> "
914				"incompatible",
915				p2p_wps_method_str(dev->wps_method));
916			status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
917			goto fail;
918		}
919		break;
920	case DEV_PW_PUSHBUTTON:
921		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
922			"P2P: Peer using pushbutton");
923		if (dev->wps_method != WPS_PBC) {
924			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
925				"P2P: We have wps_method=%s -> "
926				"incompatible",
927				p2p_wps_method_str(dev->wps_method));
928			status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
929			goto fail;
930		}
931		break;
932	default:
933		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
934			"P2P: Unsupported Device Password ID %d",
935			msg.dev_password_id);
936		status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
937		goto fail;
938	}
939
940	if (go) {
941		struct p2p_channels intersection;
942		size_t i;
943		p2p_channels_intersect(&p2p->channels, &dev->channels,
944				       &intersection);
945		if (intersection.reg_classes == 0 ||
946		    intersection.reg_class[0].channels == 0) {
947			status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
948			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
949				"P2P: No common channels found");
950			goto fail;
951		}
952		for (i = 0; i < intersection.reg_classes; i++) {
953			struct p2p_reg_class *c;
954			c = &intersection.reg_class[i];
955			wpa_printf(MSG_DEBUG, "P2P: reg_class %u",
956				   c->reg_class);
957			wpa_hexdump(MSG_DEBUG, "P2P: channels",
958				    c->channel, c->channels);
959		}
960		if (!p2p_channels_includes(&intersection, p2p->op_reg_class,
961					   p2p->op_channel))
962			p2p_reselect_channel(p2p, &intersection);
963
964		if (!p2p->ssid_set) {
965			p2p_build_ssid(p2p, p2p->ssid, &p2p->ssid_len);
966			p2p->ssid_set = 1;
967		}
968	}
969
970	p2p_set_state(p2p, P2P_GO_NEG);
971	p2p_clear_timeout(p2p);
972
973	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
974		"P2P: GO Negotiation with " MACSTR, MAC2STR(sa));
975	os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN);
976
977fail:
978	conf = p2p_build_go_neg_conf(p2p, dev, msg.dialog_token, status,
979				     msg.operating_channel, go);
980	p2p_parse_free(&msg);
981	if (conf == NULL)
982		return;
983	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
984		"P2P: Sending GO Negotiation Confirm");
985	if (status == P2P_SC_SUCCESS) {
986		p2p->pending_action_state = P2P_PENDING_GO_NEG_CONFIRM;
987		dev->go_state = go ? LOCAL_GO : REMOTE_GO;
988	} else
989		p2p->pending_action_state = P2P_NO_PENDING_ACTION;
990	if (rx_freq > 0)
991		freq = rx_freq;
992	else
993		freq = dev->listen_freq;
994	if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr, sa,
995			    wpabuf_head(conf), wpabuf_len(conf), 200) < 0) {
996		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
997			"P2P: Failed to send Action frame");
998		p2p_go_neg_failed(p2p, dev, -1);
999	}
1000	wpabuf_free(conf);
1001}
1002
1003
1004void p2p_process_go_neg_conf(struct p2p_data *p2p, const u8 *sa,
1005			     const u8 *data, size_t len)
1006{
1007	struct p2p_device *dev;
1008	struct p2p_message msg;
1009
1010	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1011		"P2P: Received GO Negotiation Confirm from " MACSTR,
1012		MAC2STR(sa));
1013	dev = p2p_get_device(p2p, sa);
1014	if (dev == NULL || dev->wps_method == WPS_NOT_READY ||
1015	    dev != p2p->go_neg_peer) {
1016		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1017			"P2P: Not ready for GO negotiation with " MACSTR,
1018			MAC2STR(sa));
1019		return;
1020	}
1021
1022	if (p2p->pending_action_state == P2P_PENDING_GO_NEG_RESPONSE) {
1023		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Stopped waiting "
1024			"for TX status on GO Negotiation Response since we "
1025			"already received Confirmation");
1026		p2p->pending_action_state = P2P_NO_PENDING_ACTION;
1027	}
1028
1029	if (p2p_parse(data, len, &msg))
1030		return;
1031
1032	if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
1033		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1034			"P2P: Was not expecting GO Negotiation Confirm - "
1035			"ignore");
1036		return;
1037	}
1038	dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
1039
1040	if (msg.dialog_token != dev->dialog_token) {
1041		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1042			"P2P: Unexpected Dialog Token %u (expected %u)",
1043			msg.dialog_token, dev->dialog_token);
1044		p2p_parse_free(&msg);
1045		return;
1046	}
1047
1048	if (!msg.status) {
1049		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1050			"P2P: No Status attribute received");
1051		p2p_parse_free(&msg);
1052		return;
1053	}
1054	if (*msg.status) {
1055		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1056			"P2P: GO Negotiation rejected: status %d",
1057			*msg.status);
1058		p2p_parse_free(&msg);
1059		return;
1060	}
1061
1062	if (dev->go_state == REMOTE_GO && msg.group_id) {
1063		/* Store SSID for Provisioning step */
1064		p2p->ssid_len = msg.group_id_len - ETH_ALEN;
1065		os_memcpy(p2p->ssid, msg.group_id + ETH_ALEN, p2p->ssid_len);
1066	} else if (dev->go_state == REMOTE_GO) {
1067		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1068			"P2P: Mandatory P2P Group ID attribute missing from "
1069			"GO Negotiation Confirmation");
1070		p2p->ssid_len = 0;
1071#ifdef CONFIG_P2P_STRICT
1072		p2p_parse_free(&msg);
1073		return;
1074#endif /* CONFIG_P2P_STRICT */
1075	}
1076
1077	if (!msg.operating_channel) {
1078		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1079			"P2P: Mandatory Operating Channel attribute missing "
1080			"from GO Negotiation Confirmation");
1081#ifdef CONFIG_P2P_STRICT
1082		p2p_parse_free(&msg);
1083		return;
1084#endif /* CONFIG_P2P_STRICT */
1085	}
1086
1087	if (!msg.channel_list) {
1088		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1089			"P2P: Mandatory Operating Channel attribute missing "
1090			"from GO Negotiation Confirmation");
1091#ifdef CONFIG_P2P_STRICT
1092		p2p_parse_free(&msg);
1093		return;
1094#endif /* CONFIG_P2P_STRICT */
1095	}
1096
1097	p2p_parse_free(&msg);
1098
1099	if (dev->go_state == UNKNOWN_GO) {
1100		/*
1101		 * This should not happen since GO negotiation has already
1102		 * been completed.
1103		 */
1104		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1105			"P2P: Unexpected GO Neg state - do not know which end "
1106			"becomes GO");
1107		return;
1108	}
1109
1110	p2p_go_complete(p2p, dev);
1111}
1112