p2p_invitation.c revision 4530cfd4d14a77c58e35393b91e40f8dd9d62697
1/*
2 * Wi-Fi Direct - P2P Invitation procedure
3 * Copyright (c) 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 "p2p_i.h"
14#include "p2p.h"
15
16
17static struct wpabuf * p2p_build_invitation_req(struct p2p_data *p2p,
18						struct p2p_device *peer,
19						const u8 *go_dev_addr)
20{
21	struct wpabuf *buf;
22	u8 *len;
23	const u8 *dev_addr;
24	size_t extra = 0;
25
26#ifdef CONFIG_WIFI_DISPLAY
27	struct wpabuf *wfd_ie = p2p->wfd_ie_invitation;
28	if (wfd_ie && p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) {
29		size_t i;
30		for (i = 0; i < p2p->num_groups; i++) {
31			struct p2p_group *g = p2p->groups[i];
32			struct wpabuf *ie;
33			if (os_memcmp(p2p_group_get_interface_addr(g),
34				      p2p->inv_bssid, ETH_ALEN) != 0)
35				continue;
36			ie = p2p_group_get_wfd_ie(g);
37			if (ie) {
38				wfd_ie = ie;
39				break;
40			}
41		}
42	}
43	if (wfd_ie)
44		extra = wpabuf_len(wfd_ie);
45#endif /* CONFIG_WIFI_DISPLAY */
46
47	buf = wpabuf_alloc(1000 + extra);
48	if (buf == NULL)
49		return NULL;
50
51	peer->dialog_token++;
52	if (peer->dialog_token == 0)
53		peer->dialog_token = 1;
54	p2p_buf_add_public_action_hdr(buf, P2P_INVITATION_REQ,
55				      peer->dialog_token);
56
57	len = p2p_buf_add_ie_hdr(buf);
58	if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO || !p2p->inv_persistent)
59		p2p_buf_add_config_timeout(buf, 0, 0);
60	else
61		p2p_buf_add_config_timeout(buf, p2p->go_timeout,
62					   p2p->client_timeout);
63	p2p_buf_add_invitation_flags(buf, p2p->inv_persistent ?
64				     P2P_INVITATION_FLAGS_TYPE : 0);
65	p2p_buf_add_operating_channel(buf, p2p->cfg->country,
66				      p2p->op_reg_class, p2p->op_channel);
67	if (p2p->inv_bssid_set)
68		p2p_buf_add_group_bssid(buf, p2p->inv_bssid);
69	p2p_buf_add_channel_list(buf, p2p->cfg->country, &p2p->channels);
70	if (go_dev_addr)
71		dev_addr = go_dev_addr;
72	else if (p2p->inv_role == P2P_INVITE_ROLE_CLIENT)
73		dev_addr = peer->info.p2p_device_addr;
74	else
75		dev_addr = p2p->cfg->dev_addr;
76	p2p_buf_add_group_id(buf, dev_addr, p2p->inv_ssid, p2p->inv_ssid_len);
77	p2p_buf_add_device_info(buf, p2p, peer);
78	p2p_buf_update_ie_hdr(buf, len);
79
80#ifdef CONFIG_WIFI_DISPLAY
81	if (wfd_ie)
82		wpabuf_put_buf(buf, wfd_ie);
83#endif /* CONFIG_WIFI_DISPLAY */
84
85	return buf;
86}
87
88
89static struct wpabuf * p2p_build_invitation_resp(struct p2p_data *p2p,
90						 struct p2p_device *peer,
91						 u8 dialog_token, u8 status,
92						 const u8 *group_bssid,
93						 u8 reg_class, u8 channel,
94						 struct p2p_channels *channels)
95{
96	struct wpabuf *buf;
97	u8 *len;
98	size_t extra = 0;
99
100#ifdef CONFIG_WIFI_DISPLAY
101	struct wpabuf *wfd_ie = p2p->wfd_ie_invitation;
102	if (wfd_ie && group_bssid) {
103		size_t i;
104		for (i = 0; i < p2p->num_groups; i++) {
105			struct p2p_group *g = p2p->groups[i];
106			struct wpabuf *ie;
107			if (os_memcmp(p2p_group_get_interface_addr(g),
108				      group_bssid, ETH_ALEN) != 0)
109				continue;
110			ie = p2p_group_get_wfd_ie(g);
111			if (ie) {
112				wfd_ie = ie;
113				break;
114			}
115		}
116	}
117	if (wfd_ie)
118		extra = wpabuf_len(wfd_ie);
119#endif /* CONFIG_WIFI_DISPLAY */
120
121	buf = wpabuf_alloc(1000 + extra);
122	if (buf == NULL)
123		return NULL;
124
125	p2p_buf_add_public_action_hdr(buf, P2P_INVITATION_RESP,
126				      dialog_token);
127
128	len = p2p_buf_add_ie_hdr(buf);
129	p2p_buf_add_status(buf, status);
130	p2p_buf_add_config_timeout(buf, 0, 0); /* FIX */
131	if (reg_class && channel)
132		p2p_buf_add_operating_channel(buf, p2p->cfg->country,
133					      reg_class, channel);
134	if (group_bssid)
135		p2p_buf_add_group_bssid(buf, group_bssid);
136	if (channels)
137		p2p_buf_add_channel_list(buf, p2p->cfg->country, channels);
138	p2p_buf_update_ie_hdr(buf, len);
139
140#ifdef CONFIG_WIFI_DISPLAY
141	if (wfd_ie)
142		wpabuf_put_buf(buf, wfd_ie);
143#endif /* CONFIG_WIFI_DISPLAY */
144
145	return buf;
146}
147
148
149void p2p_process_invitation_req(struct p2p_data *p2p, const u8 *sa,
150				const u8 *data, size_t len, int rx_freq)
151{
152	struct p2p_device *dev;
153	struct p2p_message msg;
154	struct wpabuf *resp = NULL;
155	u8 status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
156	int freq;
157	int go = 0;
158	u8 group_bssid[ETH_ALEN], *bssid;
159	int op_freq = 0;
160	u8 reg_class = 0, channel = 0;
161	struct p2p_channels intersection, *channels = NULL;
162	int persistent;
163
164	os_memset(group_bssid, 0, sizeof(group_bssid));
165
166	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
167		"P2P: Received Invitation Request from " MACSTR " (freq=%d)",
168		MAC2STR(sa), rx_freq);
169
170	if (p2p_parse(data, len, &msg))
171		return;
172
173	dev = p2p_get_device(p2p, sa);
174	if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
175		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
176			"P2P: Invitation Request from unknown peer "
177			MACSTR, MAC2STR(sa));
178
179		if (p2p_add_device(p2p, sa, rx_freq, 0, data + 1, len - 1, 0))
180		{
181			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
182				"P2P: Invitation Request add device failed "
183				MACSTR, MAC2STR(sa));
184			status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
185			goto fail;
186		}
187
188		dev = p2p_get_device(p2p, sa);
189		if (dev == NULL) {
190			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
191				"P2P: Reject Invitation Request from unknown "
192				"peer " MACSTR, MAC2STR(sa));
193			status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
194			goto fail;
195		}
196	}
197
198	if (!msg.group_id || !msg.channel_list) {
199		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
200			"P2P: Mandatory attribute missing in Invitation "
201			"Request from " MACSTR, MAC2STR(sa));
202		status = P2P_SC_FAIL_INVALID_PARAMS;
203		goto fail;
204	}
205
206	if (msg.invitation_flags)
207		persistent = *msg.invitation_flags & P2P_INVITATION_FLAGS_TYPE;
208	else {
209		/* Invitation Flags is a mandatory attribute starting from P2P
210		 * spec 1.06. As a backwards compatibility mechanism, assume
211		 * the request was for a persistent group if the attribute is
212		 * missing.
213		 */
214		wpa_printf(MSG_DEBUG, "P2P: Mandatory Invitation Flags "
215			   "attribute missing from Invitation Request");
216		persistent = 1;
217	}
218
219	if (p2p_peer_channels_check(p2p, &p2p->cfg->channels, dev,
220				    msg.channel_list, msg.channel_list_len) <
221	    0) {
222		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
223			"P2P: No common channels found");
224		status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
225		goto fail;
226	}
227
228	if (p2p->cfg->invitation_process) {
229		status = p2p->cfg->invitation_process(
230			p2p->cfg->cb_ctx, sa, msg.group_bssid, msg.group_id,
231			msg.group_id + ETH_ALEN, msg.group_id_len - ETH_ALEN,
232			&go, group_bssid, &op_freq, persistent);
233	}
234
235	if (op_freq) {
236		if (p2p_freq_to_channel(p2p->cfg->country, op_freq,
237					&reg_class, &channel) < 0) {
238			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
239				"P2P: Unknown forced freq %d MHz from "
240				"invitation_process()", op_freq);
241			status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
242			goto fail;
243		}
244
245		p2p_channels_intersect(&p2p->cfg->channels, &dev->channels,
246				       &intersection);
247		if (!p2p_channels_includes(&intersection, reg_class, channel))
248		{
249			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
250				"P2P: forced freq %d MHz not in the supported "
251				"channels interaction", op_freq);
252			status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
253			goto fail;
254		}
255
256		if (status == P2P_SC_SUCCESS)
257			channels = &intersection;
258	} else {
259		op_freq = p2p_channel_to_freq(p2p->cfg->country,
260					      p2p->cfg->op_reg_class,
261					      p2p->cfg->op_channel);
262		if (op_freq < 0) {
263			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
264				"P2P: Unknown operational channel "
265				"(country=%c%c reg_class=%u channel=%u)",
266				p2p->cfg->country[0], p2p->cfg->country[1],
267				p2p->cfg->op_reg_class, p2p->cfg->op_channel);
268			status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
269			goto fail;
270		}
271
272		p2p_channels_intersect(&p2p->cfg->channels, &dev->channels,
273				       &intersection);
274		if (status == P2P_SC_SUCCESS) {
275			reg_class = p2p->cfg->op_reg_class;
276			channel = p2p->cfg->op_channel;
277			channels = &intersection;
278		}
279	}
280
281fail:
282	if (go && status == P2P_SC_SUCCESS && !is_zero_ether_addr(group_bssid))
283		bssid = group_bssid;
284	else
285		bssid = NULL;
286	resp = p2p_build_invitation_resp(p2p, dev, msg.dialog_token, status,
287					 bssid, reg_class, channel, channels);
288
289	if (resp == NULL)
290		goto out;
291
292	if (rx_freq > 0)
293		freq = rx_freq;
294	else
295		freq = p2p_channel_to_freq(p2p->cfg->country,
296					   p2p->cfg->reg_class,
297					   p2p->cfg->channel);
298	if (freq < 0) {
299		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
300			"P2P: Unknown regulatory class/channel");
301		goto out;
302	}
303
304	/*
305	 * Store copy of invitation data to be used when processing TX status
306	 * callback for the Acton frame.
307	 */
308	os_memcpy(p2p->inv_sa, sa, ETH_ALEN);
309	if (msg.group_bssid) {
310		os_memcpy(p2p->inv_group_bssid, msg.group_bssid, ETH_ALEN);
311		p2p->inv_group_bssid_ptr = p2p->inv_group_bssid;
312	} else
313		p2p->inv_group_bssid_ptr = NULL;
314	if (msg.group_id_len - ETH_ALEN <= 32) {
315		os_memcpy(p2p->inv_ssid, msg.group_id + ETH_ALEN,
316			  msg.group_id_len - ETH_ALEN);
317		p2p->inv_ssid_len = msg.group_id_len - ETH_ALEN;
318	}
319	os_memcpy(p2p->inv_go_dev_addr, msg.group_id, ETH_ALEN);
320	p2p->inv_status = status;
321	p2p->inv_op_freq = op_freq;
322
323	p2p->pending_action_state = P2P_PENDING_INVITATION_RESPONSE;
324	if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr,
325			    p2p->cfg->dev_addr,
326			    wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
327		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
328			"P2P: Failed to send Action frame");
329	}
330
331out:
332	wpabuf_free(resp);
333	p2p_parse_free(&msg);
334}
335
336
337void p2p_process_invitation_resp(struct p2p_data *p2p, const u8 *sa,
338				 const u8 *data, size_t len)
339{
340	struct p2p_device *dev;
341	struct p2p_message msg;
342
343	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
344		"P2P: Received Invitation Response from " MACSTR,
345		MAC2STR(sa));
346
347	dev = p2p_get_device(p2p, sa);
348	if (dev == NULL) {
349		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
350			"P2P: Ignore Invitation Response from unknown peer "
351			MACSTR, MAC2STR(sa));
352		return;
353	}
354
355	if (dev != p2p->invite_peer) {
356		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
357			"P2P: Ignore unexpected Invitation Response from peer "
358			MACSTR, MAC2STR(sa));
359		return;
360	}
361
362	if (p2p_parse(data, len, &msg))
363		return;
364
365	if (!msg.status) {
366		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
367			"P2P: Mandatory Status attribute missing in "
368			"Invitation Response from " MACSTR, MAC2STR(sa));
369		p2p_parse_free(&msg);
370		return;
371	}
372
373	if (p2p->cfg->invitation_result)
374		p2p->cfg->invitation_result(p2p->cfg->cb_ctx, *msg.status,
375					    msg.group_bssid);
376
377	p2p_parse_free(&msg);
378
379	p2p_clear_timeout(p2p);
380	p2p_set_state(p2p, P2P_IDLE);
381	p2p->invite_peer = NULL;
382}
383
384
385int p2p_invite_send(struct p2p_data *p2p, struct p2p_device *dev,
386		    const u8 *go_dev_addr)
387{
388	struct wpabuf *req;
389	int freq;
390
391	freq = dev->listen_freq > 0 ? dev->listen_freq : dev->oper_freq;
392	if (freq <= 0) {
393		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
394			"P2P: No Listen/Operating frequency known for the "
395			"peer " MACSTR " to send Invitation Request",
396			MAC2STR(dev->info.p2p_device_addr));
397		return -1;
398	}
399
400	req = p2p_build_invitation_req(p2p, dev, go_dev_addr);
401	if (req == NULL)
402		return -1;
403	if (p2p->state != P2P_IDLE)
404		p2p_stop_listen_for_freq(p2p, freq);
405	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
406		"P2P: Sending Invitation Request");
407	p2p_set_state(p2p, P2P_INVITE);
408	p2p->pending_action_state = P2P_PENDING_INVITATION_REQUEST;
409	p2p->invite_peer = dev;
410	dev->invitation_reqs++;
411	if (p2p_send_action(p2p, freq, dev->info.p2p_device_addr,
412			    p2p->cfg->dev_addr, dev->info.p2p_device_addr,
413			    wpabuf_head(req), wpabuf_len(req), 200) < 0) {
414		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
415			"P2P: Failed to send Action frame");
416		/* Use P2P find to recover and retry */
417		p2p_set_timeout(p2p, 0, 0);
418	}
419
420	wpabuf_free(req);
421
422	return 0;
423}
424
425
426void p2p_invitation_req_cb(struct p2p_data *p2p, int success)
427{
428	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
429		"P2P: Invitation Request TX callback: success=%d", success);
430
431	if (p2p->invite_peer == NULL) {
432		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
433			"P2P: No pending Invite");
434		return;
435	}
436
437	/*
438	 * Use P2P find, if needed, to find the other device from its listen
439	 * channel.
440	 */
441	p2p_set_state(p2p, P2P_INVITE);
442	p2p_set_timeout(p2p, 0, 100000);
443}
444
445
446void p2p_invitation_resp_cb(struct p2p_data *p2p, int success)
447{
448	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
449		"P2P: Invitation Response TX callback: success=%d", success);
450	p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
451
452	if (success && p2p->cfg->invitation_received) {
453		p2p->cfg->invitation_received(p2p->cfg->cb_ctx,
454					      p2p->inv_sa,
455					      p2p->inv_group_bssid_ptr,
456					      p2p->inv_ssid, p2p->inv_ssid_len,
457					      p2p->inv_go_dev_addr,
458					      p2p->inv_status,
459					      p2p->inv_op_freq);
460	}
461}
462
463
464int p2p_invite(struct p2p_data *p2p, const u8 *peer, enum p2p_invite_role role,
465	       const u8 *bssid, const u8 *ssid, size_t ssid_len,
466	       unsigned int force_freq, const u8 *go_dev_addr,
467	       int persistent_group)
468{
469	struct p2p_device *dev;
470
471	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
472		"P2P: Request to invite peer " MACSTR " role=%d persistent=%d "
473		"force_freq=%u",
474		MAC2STR(peer), role, persistent_group, force_freq);
475	if (bssid)
476		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
477			"P2P: Invitation for BSSID " MACSTR, MAC2STR(bssid));
478	if (go_dev_addr) {
479		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
480			"P2P: Invitation for GO Device Address " MACSTR,
481			MAC2STR(go_dev_addr));
482		os_memcpy(p2p->invite_go_dev_addr_buf, go_dev_addr, ETH_ALEN);
483		p2p->invite_go_dev_addr = p2p->invite_go_dev_addr_buf;
484	} else
485		p2p->invite_go_dev_addr = NULL;
486	wpa_hexdump_ascii(MSG_DEBUG, "P2P: Invitation for SSID",
487			  ssid, ssid_len);
488
489	dev = p2p_get_device(p2p, peer);
490	if (dev == NULL || (dev->listen_freq <= 0 && dev->oper_freq <= 0)) {
491		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
492			"P2P: Cannot invite unknown P2P Device " MACSTR,
493			MAC2STR(peer));
494		return -1;
495	}
496
497	if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
498		if (!(dev->info.dev_capab &
499		      P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
500			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
501				"P2P: Cannot invite a P2P Device " MACSTR
502				" that is in a group and is not discoverable",
503				MAC2STR(peer));
504		}
505		/* TODO: use device discoverability request through GO */
506	}
507
508	dev->invitation_reqs = 0;
509
510	if (force_freq) {
511		if (p2p_freq_to_channel(p2p->cfg->country, force_freq,
512					&p2p->op_reg_class, &p2p->op_channel) <
513		    0) {
514			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
515				"P2P: Unsupported frequency %u MHz",
516				force_freq);
517			return -1;
518		}
519		p2p->channels.reg_classes = 1;
520		p2p->channels.reg_class[0].channels = 1;
521		p2p->channels.reg_class[0].reg_class = p2p->op_reg_class;
522		p2p->channels.reg_class[0].channel[0] = p2p->op_channel;
523	} else {
524		p2p->op_reg_class = p2p->cfg->op_reg_class;
525		p2p->op_channel = p2p->cfg->op_channel;
526		os_memcpy(&p2p->channels, &p2p->cfg->channels,
527			  sizeof(struct p2p_channels));
528	}
529
530	if (p2p->state != P2P_IDLE)
531		p2p_stop_find(p2p);
532
533	p2p->inv_role = role;
534	p2p->inv_bssid_set = bssid != NULL;
535	if (bssid)
536		os_memcpy(p2p->inv_bssid, bssid, ETH_ALEN);
537	os_memcpy(p2p->inv_ssid, ssid, ssid_len);
538	p2p->inv_ssid_len = ssid_len;
539	p2p->inv_persistent = persistent_group;
540	return p2p_invite_send(p2p, dev, go_dev_addr);
541}
542