p2p_invitation.c revision 91c40cdce17424d9a4718876becf0b7811cf68f2
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Wi-Fi Direct - P2P Invitation procedure
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Copyright (c) 2010, Atheros Communications
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
5868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) * This software may be distributed under the terms of the BSD license.
6868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) * See README for more details.
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "includes.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "common.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "common/ieee802_11_defs.h"
13effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "p2p_i.h"
14effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "p2p.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
163551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static struct wpabuf * p2p_build_invitation_req(struct p2p_data *p2p,
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)						struct p2p_device *peer,
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)						const u8 *go_dev_addr)
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct wpabuf *buf;
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	u8 *len;
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	const u8 *dev_addr;
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	size_t extra = 0;
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef CONFIG_WIFI_DISPLAY
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct wpabuf *wfd_ie = p2p->wfd_ie_invitation;
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (wfd_ie && p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) {
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)		size_t i;
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)		for (i = 0; i < p2p->num_groups; i++) {
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)			struct p2p_group *g = p2p->groups[i];
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)			struct wpabuf *ie;
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			if (os_memcmp(p2p_group_get_interface_addr(g),
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)				      p2p->inv_bssid, ETH_ALEN) != 0)
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)				continue;
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			ie = p2p_group_get_wfd_ie(g);
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			if (ie) {
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)				wfd_ie = ie;
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)				break;
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			}
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		}
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (wfd_ie)
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		extra = wpabuf_len(wfd_ie);
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif /* CONFIG_WIFI_DISPLAY */
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	buf = wpabuf_alloc(1000 + extra);
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (buf == NULL)
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		return NULL;
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	peer->dialog_token++;
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	if (peer->dialog_token == 0)
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		peer->dialog_token = 1;
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	p2p_buf_add_public_action_hdr(buf, P2P_INVITATION_REQ,
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)				      peer->dialog_token);
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	len = p2p_buf_add_ie_hdr(buf);
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO || !p2p->inv_persistent)
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		p2p_buf_add_config_timeout(buf, 0, 0);
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	else
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		p2p_buf_add_config_timeout(buf, p2p->go_timeout,
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)					   p2p->client_timeout);
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	p2p_buf_add_invitation_flags(buf, p2p->inv_persistent ?
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)				     P2P_INVITATION_FLAGS_TYPE : 0);
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	p2p_buf_add_operating_channel(buf, p2p->cfg->country,
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)				      p2p->op_reg_class, p2p->op_channel);
672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	if (p2p->inv_bssid_set)
682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)		p2p_buf_add_group_bssid(buf, p2p->inv_bssid);
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifdef ANDROID_P2P
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (p2p->cfg->p2p_concurrency == P2P_SINGLE_CHANNEL_CONCURRENT && p2p->op_channel) {
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "Forcing channel list %d", p2p->op_channel);
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		p2p_buf_add_oper_as_channel_list(buf, p2p->cfg->country, p2p->op_reg_class,
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			p2p->op_channel);
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	} else {
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		p2p_buf_add_channel_list(buf, p2p->cfg->country, &p2p->channels);
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef ANDROID_P2P
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (go_dev_addr)
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		dev_addr = go_dev_addr;
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	else if (p2p->inv_role == P2P_INVITE_ROLE_CLIENT)
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		dev_addr = peer->info.p2p_device_addr;
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	else
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		dev_addr = p2p->cfg->dev_addr;
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	p2p_buf_add_group_id(buf, dev_addr, p2p->inv_ssid, p2p->inv_ssid_len);
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	p2p_buf_add_device_info(buf, p2p, peer);
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	p2p_buf_update_ie_hdr(buf, len);
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef CONFIG_WIFI_DISPLAY
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (wfd_ie)
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		wpabuf_put_buf(buf, wfd_ie);
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif /* CONFIG_WIFI_DISPLAY */
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	return buf;
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static struct wpabuf * p2p_build_invitation_resp(struct p2p_data *p2p,
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)						 struct p2p_device *peer,
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)						 u8 dialog_token, u8 status,
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)						 const u8 *group_bssid,
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)						 u8 reg_class, u8 channel,
104c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)						 struct p2p_channels *channels)
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct wpabuf *buf;
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	u8 *len;
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	size_t extra = 0;
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef CONFIG_WIFI_DISPLAY
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct wpabuf *wfd_ie = p2p->wfd_ie_invitation;
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (wfd_ie && group_bssid) {
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		size_t i;
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		for (i = 0; i < p2p->num_groups; i++) {
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			struct p2p_group *g = p2p->groups[i];
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			struct wpabuf *ie;
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			if (os_memcmp(p2p_group_get_interface_addr(g),
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)				      group_bssid, ETH_ALEN) != 0)
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)				continue;
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			ie = p2p_group_get_wfd_ie(g);
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			if (ie) {
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)				wfd_ie = ie;
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)				break;
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			}
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		}
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (wfd_ie)
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		extra = wpabuf_len(wfd_ie);
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif /* CONFIG_WIFI_DISPLAY */
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	buf = wpabuf_alloc(1000 + extra);
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (buf == NULL)
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		return NULL;
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	p2p_buf_add_public_action_hdr(buf, P2P_INVITATION_RESP,
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)				      dialog_token);
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	len = p2p_buf_add_ie_hdr(buf);
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	p2p_buf_add_status(buf, status);
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	p2p_buf_add_config_timeout(buf, 0, 0); /* FIX */
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (reg_class && channel)
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		p2p_buf_add_operating_channel(buf, p2p->cfg->country,
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)					      reg_class, channel);
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (group_bssid)
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		p2p_buf_add_group_bssid(buf, group_bssid);
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef ANDROID_P2P
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (p2p->cfg->p2p_concurrency == P2P_SINGLE_CHANNEL_CONCURRENT && channel) {
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "Forcing channel list %d", channel);
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		p2p_buf_add_oper_as_channel_list(buf, p2p->cfg->country,
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			reg_class, channel);
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	} else {
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
153c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)		if (channels)
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			p2p_buf_add_channel_list(buf, p2p->cfg->country, channels);
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef ANDROID_P2P
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
158c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)	p2p_buf_update_ie_hdr(buf, len);
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef CONFIG_WIFI_DISPLAY
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (wfd_ie)
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		wpabuf_put_buf(buf, wfd_ie);
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif /* CONFIG_WIFI_DISPLAY */
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	return buf;
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void p2p_process_invitation_req(struct p2p_data *p2p, const u8 *sa,
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)				const u8 *data, size_t len, int rx_freq)
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct p2p_device *dev;
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct p2p_message msg;
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct wpabuf *resp = NULL;
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	u8 status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	int freq;
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	int go = 0;
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	u8 group_bssid[ETH_ALEN], *bssid;
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	int op_freq = 0;
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	u8 reg_class = 0, channel = 0;
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct p2p_channels intersection, *channels = NULL;
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	int persistent;
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	os_memset(group_bssid, 0, sizeof(group_bssid));
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		"P2P: Received Invitation Request from " MACSTR " (freq=%d)",
188c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)		MAC2STR(sa), rx_freq);
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (p2p_parse(data, len, &msg))
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		return;
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	dev = p2p_get_device(p2p, sa);
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			"P2P: Invitation Request from unknown peer "
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			MACSTR, MAC2STR(sa));
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		if (p2p_add_device(p2p, sa, rx_freq, 0, data + 1, len - 1, 0))
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		{
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
202c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)				"P2P: Invitation Request add device failed "
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)				MACSTR, MAC2STR(sa));
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			goto fail;
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		}
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		dev = p2p_get_device(p2p, sa);
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		if (dev == NULL) {
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)				"P2P: Reject Invitation Request from unknown "
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)				"peer " MACSTR, MAC2STR(sa));
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			goto fail;
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		}
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (!msg.group_id || !msg.channel_list) {
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			"P2P: Mandatory attribute missing in Invitation "
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			"Request from " MACSTR, MAC2STR(sa));
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		status = P2P_SC_FAIL_INVALID_PARAMS;
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		goto fail;
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (msg.invitation_flags)
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		persistent = *msg.invitation_flags & P2P_INVITATION_FLAGS_TYPE;
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	else {
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		/* Invitation Flags is a mandatory attribute starting from P2P
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		 * spec 1.06. As a backwards compatibility mechanism, assume
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		 * the request was for a persistent group if the attribute is
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		 * missing.
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		 */
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		wpa_printf(MSG_DEBUG, "P2P: Mandatory Invitation Flags "
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			   "attribute missing from Invitation Request");
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		persistent = 1;
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (p2p_peer_channels_check(p2p, &p2p->cfg->channels, dev,
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)				    msg.channel_list, msg.channel_list_len) <
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	    0) {
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			"P2P: No common channels found");
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		goto fail;
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (p2p->cfg->invitation_process) {
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		status = p2p->cfg->invitation_process(
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			p2p->cfg->cb_ctx, sa, msg.group_bssid, msg.group_id,
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			msg.group_id + ETH_ALEN, msg.group_id_len - ETH_ALEN,
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			&go, group_bssid, &op_freq, persistent);
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (op_freq) {
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Invitation "
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			"processing forced frequency %d MHz", op_freq);
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		if (p2p_freq_to_channel(p2p->cfg->country, op_freq,
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)					&reg_class, &channel) < 0) {
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)				"P2P: Unknown forced freq %d MHz from "
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)				"invitation_process()", op_freq);
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			goto fail;
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		}
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		p2p_channels_intersect(&p2p->cfg->channels, &dev->channels,
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)				       &intersection);
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		if (!p2p_channels_includes(&intersection, reg_class, channel))
270		{
271			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
272				"P2P: forced freq %d MHz not in the supported "
273				"channels interaction", op_freq);
274			status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
275			goto fail;
276		}
277
278		if (status == P2P_SC_SUCCESS)
279			channels = &intersection;
280	} else {
281		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
282			"P2P: No forced channel from invitation processing - "
283			"figure out best one to use");
284
285		p2p_channels_intersect(&p2p->cfg->channels, &dev->channels,
286				       &intersection);
287		/* Default to own configuration as a starting point */
288		p2p->op_reg_class = p2p->cfg->op_reg_class;
289		p2p->op_channel = p2p->cfg->op_channel;
290		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Own default "
291			"op_class %d channel %d",
292			p2p->op_reg_class, p2p->op_channel);
293
294		/* Use peer preference if specified and compatible */
295		if (msg.operating_channel) {
296			int req_freq;
297			req_freq = p2p_channel_to_freq(
298				(const char *) msg.operating_channel,
299				msg.operating_channel[3],
300				msg.operating_channel[4]);
301			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer "
302				"operating channel preference: %d MHz",
303				req_freq);
304			if (req_freq > 0 &&
305			    p2p_channels_includes(&intersection,
306						  msg.operating_channel[3],
307						  msg.operating_channel[4])) {
308				p2p->op_reg_class = msg.operating_channel[3];
309				p2p->op_channel = msg.operating_channel[4];
310				wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
311					"P2P: Use peer preference op_class %d "
312					"channel %d",
313					p2p->op_reg_class, p2p->op_channel);
314			} else {
315				wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
316					"P2P: Cannot use peer channel "
317					"preference");
318			}
319		}
320
321		if (!p2p_channels_includes(&intersection, p2p->op_reg_class,
322					   p2p->op_channel)) {
323			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
324				"P2P: Initially selected channel (op_class %d "
325				"channel %d) not in channel intersection - try "
326				"to reselect",
327				p2p->op_reg_class, p2p->op_channel);
328			p2p_reselect_channel(p2p, &intersection);
329			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
330				"P2P: Re-selection result: op_class %d "
331				"channel %d",
332				p2p->op_reg_class, p2p->op_channel);
333			if (!p2p_channels_includes(&intersection,
334						   p2p->op_reg_class,
335						   p2p->op_channel)) {
336				wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
337					"P2P: Peer does not support selected "
338					"operating channel (reg_class=%u "
339					"channel=%u)",
340					p2p->op_reg_class, p2p->op_channel);
341				status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
342				goto fail;
343			}
344		}
345
346		op_freq = p2p_channel_to_freq(p2p->cfg->country,
347					      p2p->op_reg_class,
348					      p2p->op_channel);
349		if (op_freq < 0) {
350			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
351				"P2P: Unknown operational channel "
352				"(country=%c%c reg_class=%u channel=%u)",
353				p2p->cfg->country[0], p2p->cfg->country[1],
354				p2p->op_reg_class, p2p->op_channel);
355			status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
356			goto fail;
357		}
358		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Selected operating "
359			"channel - %d MHz", op_freq);
360
361		if (status == P2P_SC_SUCCESS) {
362			reg_class = p2p->op_reg_class;
363			channel = p2p->op_channel;
364			channels = &intersection;
365		}
366	}
367
368fail:
369	if (go && status == P2P_SC_SUCCESS && !is_zero_ether_addr(group_bssid))
370		bssid = group_bssid;
371	else
372		bssid = NULL;
373	resp = p2p_build_invitation_resp(p2p, dev, msg.dialog_token, status,
374					 bssid, reg_class, channel, channels);
375
376	if (resp == NULL)
377		goto out;
378
379	if (rx_freq > 0)
380		freq = rx_freq;
381	else
382		freq = p2p_channel_to_freq(p2p->cfg->country,
383					   p2p->cfg->reg_class,
384					   p2p->cfg->channel);
385	if (freq < 0) {
386		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
387			"P2P: Unknown regulatory class/channel");
388		goto out;
389	}
390
391	/*
392	 * Store copy of invitation data to be used when processing TX status
393	 * callback for the Acton frame.
394	 */
395	os_memcpy(p2p->inv_sa, sa, ETH_ALEN);
396	if (msg.group_bssid) {
397		os_memcpy(p2p->inv_group_bssid, msg.group_bssid, ETH_ALEN);
398		p2p->inv_group_bssid_ptr = p2p->inv_group_bssid;
399	} else
400		p2p->inv_group_bssid_ptr = NULL;
401	if (msg.group_id_len - ETH_ALEN <= 32) {
402		os_memcpy(p2p->inv_ssid, msg.group_id + ETH_ALEN,
403			  msg.group_id_len - ETH_ALEN);
404		p2p->inv_ssid_len = msg.group_id_len - ETH_ALEN;
405	}
406	os_memcpy(p2p->inv_go_dev_addr, msg.group_id, ETH_ALEN);
407	p2p->inv_status = status;
408	p2p->inv_op_freq = op_freq;
409
410	p2p->pending_action_state = P2P_PENDING_INVITATION_RESPONSE;
411	if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr,
412			    p2p->cfg->dev_addr,
413			    wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
414		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
415			"P2P: Failed to send Action frame");
416	}
417
418out:
419	wpabuf_free(resp);
420	p2p_parse_free(&msg);
421}
422
423
424void p2p_process_invitation_resp(struct p2p_data *p2p, const u8 *sa,
425				 const u8 *data, size_t len)
426{
427	struct p2p_device *dev;
428	struct p2p_message msg;
429
430	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
431		"P2P: Received Invitation Response from " MACSTR,
432		MAC2STR(sa));
433
434	dev = p2p_get_device(p2p, sa);
435	if (dev == NULL) {
436		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
437			"P2P: Ignore Invitation Response from unknown peer "
438			MACSTR, MAC2STR(sa));
439		return;
440	}
441
442	if (dev != p2p->invite_peer) {
443		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
444			"P2P: Ignore unexpected Invitation Response from peer "
445			MACSTR, MAC2STR(sa));
446		return;
447	}
448
449	if (p2p_parse(data, len, &msg))
450		return;
451
452	if (!msg.status) {
453		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
454			"P2P: Mandatory Status attribute missing in "
455			"Invitation Response from " MACSTR, MAC2STR(sa));
456		p2p_parse_free(&msg);
457		return;
458	}
459
460	if (p2p->cfg->invitation_result)
461		p2p->cfg->invitation_result(p2p->cfg->cb_ctx, *msg.status,
462					    msg.group_bssid);
463
464	p2p_parse_free(&msg);
465
466	p2p_clear_timeout(p2p);
467	p2p_set_state(p2p, P2P_IDLE);
468	p2p->invite_peer = NULL;
469}
470
471
472int p2p_invite_send(struct p2p_data *p2p, struct p2p_device *dev,
473		    const u8 *go_dev_addr)
474{
475	struct wpabuf *req;
476	int freq;
477
478	freq = dev->listen_freq > 0 ? dev->listen_freq : dev->oper_freq;
479	if (freq <= 0) {
480		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
481			"P2P: No Listen/Operating frequency known for the "
482			"peer " MACSTR " to send Invitation Request",
483			MAC2STR(dev->info.p2p_device_addr));
484		return -1;
485	}
486
487	req = p2p_build_invitation_req(p2p, dev, go_dev_addr);
488	if (req == NULL)
489		return -1;
490	if (p2p->state != P2P_IDLE)
491		p2p_stop_listen_for_freq(p2p, freq);
492	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
493		"P2P: Sending Invitation Request");
494	p2p_set_state(p2p, P2P_INVITE);
495	p2p->pending_action_state = P2P_PENDING_INVITATION_REQUEST;
496	p2p->invite_peer = dev;
497	dev->invitation_reqs++;
498	if (p2p_send_action(p2p, freq, dev->info.p2p_device_addr,
499			    p2p->cfg->dev_addr, dev->info.p2p_device_addr,
500			    wpabuf_head(req), wpabuf_len(req), 200) < 0) {
501		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
502			"P2P: Failed to send Action frame");
503		/* Use P2P find to recover and retry */
504		p2p_set_timeout(p2p, 0, 0);
505	}
506
507	wpabuf_free(req);
508
509	return 0;
510}
511
512
513void p2p_invitation_req_cb(struct p2p_data *p2p, int success)
514{
515	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
516		"P2P: Invitation Request TX callback: success=%d", success);
517
518	if (p2p->invite_peer == NULL) {
519		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
520			"P2P: No pending Invite");
521		return;
522	}
523
524	/*
525	 * Use P2P find, if needed, to find the other device from its listen
526	 * channel.
527	 */
528	p2p_set_state(p2p, P2P_INVITE);
529	p2p_set_timeout(p2p, 0, 100000);
530}
531
532
533void p2p_invitation_resp_cb(struct p2p_data *p2p, int success)
534{
535	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
536		"P2P: Invitation Response TX callback: success=%d", success);
537	p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
538
539	if (success && p2p->cfg->invitation_received) {
540		p2p->cfg->invitation_received(p2p->cfg->cb_ctx,
541					      p2p->inv_sa,
542					      p2p->inv_group_bssid_ptr,
543					      p2p->inv_ssid, p2p->inv_ssid_len,
544					      p2p->inv_go_dev_addr,
545					      p2p->inv_status,
546					      p2p->inv_op_freq);
547	}
548}
549
550
551int p2p_invite(struct p2p_data *p2p, const u8 *peer, enum p2p_invite_role role,
552	       const u8 *bssid, const u8 *ssid, size_t ssid_len,
553	       unsigned int force_freq, const u8 *go_dev_addr,
554	       int persistent_group)
555{
556	struct p2p_device *dev;
557
558	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
559		"P2P: Request to invite peer " MACSTR " role=%d persistent=%d "
560		"force_freq=%u",
561		MAC2STR(peer), role, persistent_group, force_freq);
562	if (bssid)
563		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
564			"P2P: Invitation for BSSID " MACSTR, MAC2STR(bssid));
565	if (go_dev_addr) {
566		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
567			"P2P: Invitation for GO Device Address " MACSTR,
568			MAC2STR(go_dev_addr));
569		os_memcpy(p2p->invite_go_dev_addr_buf, go_dev_addr, ETH_ALEN);
570		p2p->invite_go_dev_addr = p2p->invite_go_dev_addr_buf;
571	} else
572		p2p->invite_go_dev_addr = NULL;
573	wpa_hexdump_ascii(MSG_DEBUG, "P2P: Invitation for SSID",
574			  ssid, ssid_len);
575
576	dev = p2p_get_device(p2p, peer);
577	if (dev == NULL || (dev->listen_freq <= 0 && dev->oper_freq <= 0)) {
578		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
579			"P2P: Cannot invite unknown P2P Device " MACSTR,
580			MAC2STR(peer));
581		return -1;
582	}
583
584	if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
585		if (!(dev->info.dev_capab &
586		      P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
587			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
588				"P2P: Cannot invite a P2P Device " MACSTR
589				" that is in a group and is not discoverable",
590				MAC2STR(peer));
591		}
592		/* TODO: use device discoverability request through GO */
593	}
594
595	dev->invitation_reqs = 0;
596
597	if (force_freq) {
598		if (p2p_freq_to_channel(p2p->cfg->country, force_freq,
599					&p2p->op_reg_class, &p2p->op_channel) <
600		    0) {
601			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
602				"P2P: Unsupported frequency %u MHz",
603				force_freq);
604			return -1;
605		}
606		p2p->channels.reg_classes = 1;
607		p2p->channels.reg_class[0].channels = 1;
608		p2p->channels.reg_class[0].reg_class = p2p->op_reg_class;
609		p2p->channels.reg_class[0].channel[0] = p2p->op_channel;
610	} else {
611		p2p->op_reg_class = p2p->cfg->op_reg_class;
612		p2p->op_channel = p2p->cfg->op_channel;
613		os_memcpy(&p2p->channels, &p2p->cfg->channels,
614			  sizeof(struct p2p_channels));
615	}
616
617	if (p2p->state != P2P_IDLE)
618		p2p_stop_find(p2p);
619
620	p2p->inv_role = role;
621	p2p->inv_bssid_set = bssid != NULL;
622	if (bssid)
623		os_memcpy(p2p->inv_bssid, bssid, ETH_ALEN);
624	os_memcpy(p2p->inv_ssid, ssid, ssid_len);
625	p2p->inv_ssid_len = ssid_len;
626	p2p->inv_persistent = persistent_group;
627	return p2p_invite_send(p2p, dev, go_dev_addr);
628}
629