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