1e5b75505Sopenharmony_ci/*
2e5b75505Sopenharmony_ci * Wi-Fi Direct - P2P group operations
3e5b75505Sopenharmony_ci * Copyright (c) 2009-2010, Atheros Communications
4e5b75505Sopenharmony_ci *
5e5b75505Sopenharmony_ci * This software may be distributed under the terms of the BSD license.
6e5b75505Sopenharmony_ci * See README for more details.
7e5b75505Sopenharmony_ci */
8e5b75505Sopenharmony_ci
9e5b75505Sopenharmony_ci#include "includes.h"
10e5b75505Sopenharmony_ci
11e5b75505Sopenharmony_ci#include "common.h"
12e5b75505Sopenharmony_ci#include "common/ieee802_11_defs.h"
13e5b75505Sopenharmony_ci#include "common/ieee802_11_common.h"
14e5b75505Sopenharmony_ci#include "common/wpa_ctrl.h"
15e5b75505Sopenharmony_ci#include "wps/wps_defs.h"
16e5b75505Sopenharmony_ci#include "wps/wps_i.h"
17e5b75505Sopenharmony_ci#include "p2p_i.h"
18e5b75505Sopenharmony_ci#include "p2p.h"
19e5b75505Sopenharmony_ci
20e5b75505Sopenharmony_ci
21e5b75505Sopenharmony_cistruct p2p_group_member {
22e5b75505Sopenharmony_ci	struct p2p_group_member *next;
23e5b75505Sopenharmony_ci	u8 addr[ETH_ALEN]; /* P2P Interface Address */
24e5b75505Sopenharmony_ci	u8 dev_addr[ETH_ALEN]; /* P2P Device Address */
25e5b75505Sopenharmony_ci	struct wpabuf *p2p_ie;
26e5b75505Sopenharmony_ci	struct wpabuf *wfd_ie;
27e5b75505Sopenharmony_ci	struct wpabuf *client_info;
28e5b75505Sopenharmony_ci	u8 dev_capab;
29e5b75505Sopenharmony_ci};
30e5b75505Sopenharmony_ci
31e5b75505Sopenharmony_ci/**
32e5b75505Sopenharmony_ci * struct p2p_group - Internal P2P module per-group data
33e5b75505Sopenharmony_ci */
34e5b75505Sopenharmony_cistruct p2p_group {
35e5b75505Sopenharmony_ci	struct p2p_data *p2p;
36e5b75505Sopenharmony_ci	struct p2p_group_config *cfg;
37e5b75505Sopenharmony_ci	struct p2p_group_member *members;
38e5b75505Sopenharmony_ci	unsigned int num_members;
39e5b75505Sopenharmony_ci	int group_formation;
40e5b75505Sopenharmony_ci	int beacon_update;
41e5b75505Sopenharmony_ci	struct wpabuf *noa;
42e5b75505Sopenharmony_ci	struct wpabuf *wfd_ie;
43e5b75505Sopenharmony_ci};
44e5b75505Sopenharmony_ci
45e5b75505Sopenharmony_ci
46e5b75505Sopenharmony_cistruct p2p_group * p2p_group_init(struct p2p_data *p2p,
47e5b75505Sopenharmony_ci				  struct p2p_group_config *config)
48e5b75505Sopenharmony_ci{
49e5b75505Sopenharmony_ci	struct p2p_group *group, **groups;
50e5b75505Sopenharmony_ci
51e5b75505Sopenharmony_ci	group = os_zalloc(sizeof(*group));
52e5b75505Sopenharmony_ci	if (group == NULL)
53e5b75505Sopenharmony_ci		return NULL;
54e5b75505Sopenharmony_ci
55e5b75505Sopenharmony_ci	groups = os_realloc_array(p2p->groups, p2p->num_groups + 1,
56e5b75505Sopenharmony_ci				  sizeof(struct p2p_group *));
57e5b75505Sopenharmony_ci	if (groups == NULL) {
58e5b75505Sopenharmony_ci		os_free(group);
59e5b75505Sopenharmony_ci		return NULL;
60e5b75505Sopenharmony_ci	}
61e5b75505Sopenharmony_ci	groups[p2p->num_groups++] = group;
62e5b75505Sopenharmony_ci	p2p->groups = groups;
63e5b75505Sopenharmony_ci
64e5b75505Sopenharmony_ci	group->p2p = p2p;
65e5b75505Sopenharmony_ci	group->cfg = config;
66e5b75505Sopenharmony_ci	group->group_formation = 1;
67e5b75505Sopenharmony_ci	group->beacon_update = 1;
68e5b75505Sopenharmony_ci	p2p_group_update_ies(group);
69e5b75505Sopenharmony_ci	group->cfg->idle_update(group->cfg->cb_ctx, 1);
70e5b75505Sopenharmony_ci
71e5b75505Sopenharmony_ci	return group;
72e5b75505Sopenharmony_ci}
73e5b75505Sopenharmony_ci
74e5b75505Sopenharmony_ci
75e5b75505Sopenharmony_cistatic void p2p_group_free_member(struct p2p_group_member *m)
76e5b75505Sopenharmony_ci{
77e5b75505Sopenharmony_ci	wpabuf_free(m->wfd_ie);
78e5b75505Sopenharmony_ci	wpabuf_free(m->p2p_ie);
79e5b75505Sopenharmony_ci	wpabuf_free(m->client_info);
80e5b75505Sopenharmony_ci	os_free(m);
81e5b75505Sopenharmony_ci}
82e5b75505Sopenharmony_ci
83e5b75505Sopenharmony_ci
84e5b75505Sopenharmony_cistatic void p2p_group_free_members(struct p2p_group *group)
85e5b75505Sopenharmony_ci{
86e5b75505Sopenharmony_ci	struct p2p_group_member *m, *prev;
87e5b75505Sopenharmony_ci	m = group->members;
88e5b75505Sopenharmony_ci	group->members = NULL;
89e5b75505Sopenharmony_ci	group->num_members = 0;
90e5b75505Sopenharmony_ci	while (m) {
91e5b75505Sopenharmony_ci		prev = m;
92e5b75505Sopenharmony_ci		m = m->next;
93e5b75505Sopenharmony_ci		p2p_group_free_member(prev);
94e5b75505Sopenharmony_ci	}
95e5b75505Sopenharmony_ci}
96e5b75505Sopenharmony_ci
97e5b75505Sopenharmony_ci
98e5b75505Sopenharmony_civoid p2p_group_deinit(struct p2p_group *group)
99e5b75505Sopenharmony_ci{
100e5b75505Sopenharmony_ci	size_t g;
101e5b75505Sopenharmony_ci	struct p2p_data *p2p;
102e5b75505Sopenharmony_ci
103e5b75505Sopenharmony_ci	if (group == NULL)
104e5b75505Sopenharmony_ci		return;
105e5b75505Sopenharmony_ci
106e5b75505Sopenharmony_ci	p2p = group->p2p;
107e5b75505Sopenharmony_ci
108e5b75505Sopenharmony_ci	for (g = 0; g < p2p->num_groups; g++) {
109e5b75505Sopenharmony_ci		if (p2p->groups[g] == group) {
110e5b75505Sopenharmony_ci			while (g + 1 < p2p->num_groups) {
111e5b75505Sopenharmony_ci				p2p->groups[g] = p2p->groups[g + 1];
112e5b75505Sopenharmony_ci				g++;
113e5b75505Sopenharmony_ci			}
114e5b75505Sopenharmony_ci			p2p->num_groups--;
115e5b75505Sopenharmony_ci			break;
116e5b75505Sopenharmony_ci		}
117e5b75505Sopenharmony_ci	}
118e5b75505Sopenharmony_ci
119e5b75505Sopenharmony_ci	p2p_group_free_members(group);
120e5b75505Sopenharmony_ci	os_free(group->cfg);
121e5b75505Sopenharmony_ci	wpabuf_free(group->noa);
122e5b75505Sopenharmony_ci	wpabuf_free(group->wfd_ie);
123e5b75505Sopenharmony_ci	os_free(group);
124e5b75505Sopenharmony_ci}
125e5b75505Sopenharmony_ci
126e5b75505Sopenharmony_ci
127e5b75505Sopenharmony_cistatic void p2p_client_info(struct wpabuf *ie, struct p2p_group_member *m)
128e5b75505Sopenharmony_ci{
129e5b75505Sopenharmony_ci	if (m->client_info == NULL)
130e5b75505Sopenharmony_ci		return;
131e5b75505Sopenharmony_ci	if (wpabuf_tailroom(ie) < wpabuf_len(m->client_info) + 1)
132e5b75505Sopenharmony_ci		return;
133e5b75505Sopenharmony_ci	wpabuf_put_buf(ie, m->client_info);
134e5b75505Sopenharmony_ci}
135e5b75505Sopenharmony_ci
136e5b75505Sopenharmony_ci
137e5b75505Sopenharmony_cistatic void p2p_group_add_common_ies(struct p2p_group *group,
138e5b75505Sopenharmony_ci				     struct wpabuf *ie)
139e5b75505Sopenharmony_ci{
140e5b75505Sopenharmony_ci	u8 dev_capab = group->p2p->dev_capab, group_capab = 0;
141e5b75505Sopenharmony_ci
142e5b75505Sopenharmony_ci	/* P2P Capability */
143e5b75505Sopenharmony_ci	dev_capab &= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
144e5b75505Sopenharmony_ci	group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER;
145e5b75505Sopenharmony_ci	if (group->cfg->persistent_group) {
146e5b75505Sopenharmony_ci		group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
147e5b75505Sopenharmony_ci		if (group->cfg->persistent_group == 2)
148e5b75505Sopenharmony_ci			group_capab |= P2P_GROUP_CAPAB_PERSISTENT_RECONN;
149e5b75505Sopenharmony_ci	}
150e5b75505Sopenharmony_ci	if (group->p2p->cfg->p2p_intra_bss)
151e5b75505Sopenharmony_ci		group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
152e5b75505Sopenharmony_ci	if (group->group_formation)
153e5b75505Sopenharmony_ci		group_capab |= P2P_GROUP_CAPAB_GROUP_FORMATION;
154e5b75505Sopenharmony_ci	if (group->p2p->cross_connect)
155e5b75505Sopenharmony_ci		group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
156e5b75505Sopenharmony_ci	if (group->num_members >= group->cfg->max_clients)
157e5b75505Sopenharmony_ci		group_capab |= P2P_GROUP_CAPAB_GROUP_LIMIT;
158e5b75505Sopenharmony_ci	if (group->cfg->ip_addr_alloc)
159e5b75505Sopenharmony_ci		group_capab |= P2P_GROUP_CAPAB_IP_ADDR_ALLOCATION;
160e5b75505Sopenharmony_ci	p2p_buf_add_capability(ie, dev_capab, group_capab);
161e5b75505Sopenharmony_ci}
162e5b75505Sopenharmony_ci
163e5b75505Sopenharmony_ci
164e5b75505Sopenharmony_cistatic void p2p_group_add_noa(struct wpabuf *ie, struct wpabuf *noa)
165e5b75505Sopenharmony_ci{
166e5b75505Sopenharmony_ci	if (noa == NULL)
167e5b75505Sopenharmony_ci		return;
168e5b75505Sopenharmony_ci	/* Notice of Absence */
169e5b75505Sopenharmony_ci	wpabuf_put_u8(ie, P2P_ATTR_NOTICE_OF_ABSENCE);
170e5b75505Sopenharmony_ci	wpabuf_put_le16(ie, wpabuf_len(noa));
171e5b75505Sopenharmony_ci	wpabuf_put_buf(ie, noa);
172e5b75505Sopenharmony_ci}
173e5b75505Sopenharmony_ci
174e5b75505Sopenharmony_ci
175e5b75505Sopenharmony_cistatic struct wpabuf * p2p_group_encaps_probe_resp(struct wpabuf *subelems)
176e5b75505Sopenharmony_ci{
177e5b75505Sopenharmony_ci	struct wpabuf *ie;
178e5b75505Sopenharmony_ci	const u8 *pos, *end;
179e5b75505Sopenharmony_ci	size_t len;
180e5b75505Sopenharmony_ci
181e5b75505Sopenharmony_ci	if (subelems == NULL)
182e5b75505Sopenharmony_ci		return NULL;
183e5b75505Sopenharmony_ci
184e5b75505Sopenharmony_ci	len = wpabuf_len(subelems) + 100;
185e5b75505Sopenharmony_ci
186e5b75505Sopenharmony_ci	ie = wpabuf_alloc(len);
187e5b75505Sopenharmony_ci	if (ie == NULL)
188e5b75505Sopenharmony_ci		return NULL;
189e5b75505Sopenharmony_ci
190e5b75505Sopenharmony_ci	pos = wpabuf_head(subelems);
191e5b75505Sopenharmony_ci	end = pos + wpabuf_len(subelems);
192e5b75505Sopenharmony_ci
193e5b75505Sopenharmony_ci	while (end > pos) {
194e5b75505Sopenharmony_ci		size_t frag_len = end - pos;
195e5b75505Sopenharmony_ci		if (frag_len > 251)
196e5b75505Sopenharmony_ci			frag_len = 251;
197e5b75505Sopenharmony_ci		wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
198e5b75505Sopenharmony_ci		wpabuf_put_u8(ie, 4 + frag_len);
199e5b75505Sopenharmony_ci		wpabuf_put_be32(ie, P2P_IE_VENDOR_TYPE);
200e5b75505Sopenharmony_ci		wpabuf_put_data(ie, pos, frag_len);
201e5b75505Sopenharmony_ci		pos += frag_len;
202e5b75505Sopenharmony_ci	}
203e5b75505Sopenharmony_ci
204e5b75505Sopenharmony_ci	return ie;
205e5b75505Sopenharmony_ci}
206e5b75505Sopenharmony_ci
207e5b75505Sopenharmony_ci
208e5b75505Sopenharmony_cistatic struct wpabuf * p2p_group_build_beacon_ie(struct p2p_group *group)
209e5b75505Sopenharmony_ci{
210e5b75505Sopenharmony_ci	struct wpabuf *ie;
211e5b75505Sopenharmony_ci	u8 *len;
212e5b75505Sopenharmony_ci	size_t extra = 0;
213e5b75505Sopenharmony_ci
214e5b75505Sopenharmony_ci#ifdef CONFIG_WIFI_DISPLAY
215e5b75505Sopenharmony_ci	if (group->p2p->wfd_ie_beacon)
216e5b75505Sopenharmony_ci		extra = wpabuf_len(group->p2p->wfd_ie_beacon);
217e5b75505Sopenharmony_ci#endif /* CONFIG_WIFI_DISPLAY */
218e5b75505Sopenharmony_ci
219e5b75505Sopenharmony_ci	if (group->p2p->vendor_elem &&
220e5b75505Sopenharmony_ci	    group->p2p->vendor_elem[VENDOR_ELEM_BEACON_P2P_GO])
221e5b75505Sopenharmony_ci		extra += wpabuf_len(group->p2p->vendor_elem[VENDOR_ELEM_BEACON_P2P_GO]);
222e5b75505Sopenharmony_ci
223e5b75505Sopenharmony_ci	ie = wpabuf_alloc(257 + extra);
224e5b75505Sopenharmony_ci	if (ie == NULL)
225e5b75505Sopenharmony_ci		return NULL;
226e5b75505Sopenharmony_ci
227e5b75505Sopenharmony_ci#ifdef CONFIG_WIFI_DISPLAY
228e5b75505Sopenharmony_ci	if (group->p2p->wfd_ie_beacon)
229e5b75505Sopenharmony_ci		wpabuf_put_buf(ie, group->p2p->wfd_ie_beacon);
230e5b75505Sopenharmony_ci#endif /* CONFIG_WIFI_DISPLAY */
231e5b75505Sopenharmony_ci
232e5b75505Sopenharmony_ci	if (group->p2p->vendor_elem &&
233e5b75505Sopenharmony_ci	    group->p2p->vendor_elem[VENDOR_ELEM_BEACON_P2P_GO])
234e5b75505Sopenharmony_ci		wpabuf_put_buf(ie,
235e5b75505Sopenharmony_ci			       group->p2p->vendor_elem[VENDOR_ELEM_BEACON_P2P_GO]);
236e5b75505Sopenharmony_ci
237e5b75505Sopenharmony_ci	len = p2p_buf_add_ie_hdr(ie);
238e5b75505Sopenharmony_ci	p2p_group_add_common_ies(group, ie);
239e5b75505Sopenharmony_ci	p2p_buf_add_device_id(ie, group->p2p->cfg->dev_addr);
240e5b75505Sopenharmony_ci	p2p_group_add_noa(ie, group->noa);
241e5b75505Sopenharmony_ci	p2p_buf_update_ie_hdr(ie, len);
242e5b75505Sopenharmony_ci
243e5b75505Sopenharmony_ci	return ie;
244e5b75505Sopenharmony_ci}
245e5b75505Sopenharmony_ci
246e5b75505Sopenharmony_ci
247e5b75505Sopenharmony_ci#ifdef CONFIG_WIFI_DISPLAY
248e5b75505Sopenharmony_ci
249e5b75505Sopenharmony_cistruct wpabuf * p2p_group_get_wfd_ie(struct p2p_group *g)
250e5b75505Sopenharmony_ci{
251e5b75505Sopenharmony_ci	return g->wfd_ie;
252e5b75505Sopenharmony_ci}
253e5b75505Sopenharmony_ci
254e5b75505Sopenharmony_ci
255e5b75505Sopenharmony_cistruct wpabuf * wifi_display_encaps(struct wpabuf *subelems)
256e5b75505Sopenharmony_ci{
257e5b75505Sopenharmony_ci	struct wpabuf *ie;
258e5b75505Sopenharmony_ci	const u8 *pos, *end;
259e5b75505Sopenharmony_ci
260e5b75505Sopenharmony_ci	if (subelems == NULL)
261e5b75505Sopenharmony_ci		return NULL;
262e5b75505Sopenharmony_ci
263e5b75505Sopenharmony_ci	ie = wpabuf_alloc(wpabuf_len(subelems) + 100);
264e5b75505Sopenharmony_ci	if (ie == NULL)
265e5b75505Sopenharmony_ci		return NULL;
266e5b75505Sopenharmony_ci
267e5b75505Sopenharmony_ci	pos = wpabuf_head(subelems);
268e5b75505Sopenharmony_ci	end = pos + wpabuf_len(subelems);
269e5b75505Sopenharmony_ci
270e5b75505Sopenharmony_ci	while (end > pos) {
271e5b75505Sopenharmony_ci		size_t frag_len = end - pos;
272e5b75505Sopenharmony_ci		if (frag_len > 251)
273e5b75505Sopenharmony_ci			frag_len = 251;
274e5b75505Sopenharmony_ci		wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
275e5b75505Sopenharmony_ci		wpabuf_put_u8(ie, 4 + frag_len);
276e5b75505Sopenharmony_ci		wpabuf_put_be32(ie, WFD_IE_VENDOR_TYPE);
277e5b75505Sopenharmony_ci		wpabuf_put_data(ie, pos, frag_len);
278e5b75505Sopenharmony_ci		pos += frag_len;
279e5b75505Sopenharmony_ci	}
280e5b75505Sopenharmony_ci
281e5b75505Sopenharmony_ci	return ie;
282e5b75505Sopenharmony_ci}
283e5b75505Sopenharmony_ci
284e5b75505Sopenharmony_ci
285e5b75505Sopenharmony_cistatic int wifi_display_add_dev_info_descr(struct wpabuf *buf,
286e5b75505Sopenharmony_ci					   struct p2p_group_member *m)
287e5b75505Sopenharmony_ci{
288e5b75505Sopenharmony_ci	const u8 *pos, *end;
289e5b75505Sopenharmony_ci	const u8 *dev_info = NULL;
290e5b75505Sopenharmony_ci	const u8 *assoc_bssid = NULL;
291e5b75505Sopenharmony_ci	const u8 *coupled_sink = NULL;
292e5b75505Sopenharmony_ci	u8 zero_addr[ETH_ALEN];
293e5b75505Sopenharmony_ci
294e5b75505Sopenharmony_ci	if (m->wfd_ie == NULL)
295e5b75505Sopenharmony_ci		return 0;
296e5b75505Sopenharmony_ci
297e5b75505Sopenharmony_ci	os_memset(zero_addr, 0, ETH_ALEN);
298e5b75505Sopenharmony_ci	pos = wpabuf_head_u8(m->wfd_ie);
299e5b75505Sopenharmony_ci	end = pos + wpabuf_len(m->wfd_ie);
300e5b75505Sopenharmony_ci	while (end - pos >= 3) {
301e5b75505Sopenharmony_ci		u8 id;
302e5b75505Sopenharmony_ci		u16 len;
303e5b75505Sopenharmony_ci
304e5b75505Sopenharmony_ci		id = *pos++;
305e5b75505Sopenharmony_ci		len = WPA_GET_BE16(pos);
306e5b75505Sopenharmony_ci		pos += 2;
307e5b75505Sopenharmony_ci		if (len > end - pos)
308e5b75505Sopenharmony_ci			break;
309e5b75505Sopenharmony_ci
310e5b75505Sopenharmony_ci		switch (id) {
311e5b75505Sopenharmony_ci		case WFD_SUBELEM_DEVICE_INFO:
312e5b75505Sopenharmony_ci			if (len < 6)
313e5b75505Sopenharmony_ci				break;
314e5b75505Sopenharmony_ci			dev_info = pos;
315e5b75505Sopenharmony_ci			break;
316e5b75505Sopenharmony_ci		case WFD_SUBELEM_ASSOCIATED_BSSID:
317e5b75505Sopenharmony_ci			if (len < ETH_ALEN)
318e5b75505Sopenharmony_ci				break;
319e5b75505Sopenharmony_ci			assoc_bssid = pos;
320e5b75505Sopenharmony_ci			break;
321e5b75505Sopenharmony_ci		case WFD_SUBELEM_COUPLED_SINK:
322e5b75505Sopenharmony_ci			if (len < 1 + ETH_ALEN)
323e5b75505Sopenharmony_ci				break;
324e5b75505Sopenharmony_ci			coupled_sink = pos;
325e5b75505Sopenharmony_ci			break;
326e5b75505Sopenharmony_ci		}
327e5b75505Sopenharmony_ci
328e5b75505Sopenharmony_ci		pos += len;
329e5b75505Sopenharmony_ci	}
330e5b75505Sopenharmony_ci
331e5b75505Sopenharmony_ci	if (dev_info == NULL)
332e5b75505Sopenharmony_ci		return 0;
333e5b75505Sopenharmony_ci
334e5b75505Sopenharmony_ci	wpabuf_put_u8(buf, 23);
335e5b75505Sopenharmony_ci	wpabuf_put_data(buf, m->dev_addr, ETH_ALEN);
336e5b75505Sopenharmony_ci	if (assoc_bssid)
337e5b75505Sopenharmony_ci		wpabuf_put_data(buf, assoc_bssid, ETH_ALEN);
338e5b75505Sopenharmony_ci	else
339e5b75505Sopenharmony_ci		wpabuf_put_data(buf, zero_addr, ETH_ALEN);
340e5b75505Sopenharmony_ci	wpabuf_put_data(buf, dev_info, 2); /* WFD Device Info */
341e5b75505Sopenharmony_ci	wpabuf_put_data(buf, dev_info + 4, 2); /* WFD Device Max Throughput */
342e5b75505Sopenharmony_ci	if (coupled_sink) {
343e5b75505Sopenharmony_ci		wpabuf_put_data(buf, coupled_sink, 1 + ETH_ALEN);
344e5b75505Sopenharmony_ci	} else {
345e5b75505Sopenharmony_ci		wpabuf_put_u8(buf, 0);
346e5b75505Sopenharmony_ci		wpabuf_put_data(buf, zero_addr, ETH_ALEN);
347e5b75505Sopenharmony_ci	}
348e5b75505Sopenharmony_ci
349e5b75505Sopenharmony_ci	return 1;
350e5b75505Sopenharmony_ci}
351e5b75505Sopenharmony_ci
352e5b75505Sopenharmony_ci
353e5b75505Sopenharmony_cistatic struct wpabuf *
354e5b75505Sopenharmony_ciwifi_display_build_go_ie(struct p2p_group *group)
355e5b75505Sopenharmony_ci{
356e5b75505Sopenharmony_ci	struct wpabuf *wfd_subelems, *wfd_ie;
357e5b75505Sopenharmony_ci	struct p2p_group_member *m;
358e5b75505Sopenharmony_ci	u8 *len;
359e5b75505Sopenharmony_ci	unsigned int count = 0;
360e5b75505Sopenharmony_ci
361e5b75505Sopenharmony_ci	if (!group->p2p->wfd_ie_probe_resp)
362e5b75505Sopenharmony_ci		return NULL;
363e5b75505Sopenharmony_ci
364e5b75505Sopenharmony_ci	wfd_subelems = wpabuf_alloc(wpabuf_len(group->p2p->wfd_ie_probe_resp) +
365e5b75505Sopenharmony_ci				    group->num_members * 24 + 100);
366e5b75505Sopenharmony_ci	if (wfd_subelems == NULL)
367e5b75505Sopenharmony_ci		return NULL;
368e5b75505Sopenharmony_ci	if (group->p2p->wfd_dev_info)
369e5b75505Sopenharmony_ci		wpabuf_put_buf(wfd_subelems, group->p2p->wfd_dev_info);
370e5b75505Sopenharmony_ci	if (group->p2p->wfd_r2_dev_info)
371e5b75505Sopenharmony_ci		wpabuf_put_buf(wfd_subelems, group->p2p->wfd_r2_dev_info);
372e5b75505Sopenharmony_ci	if (group->p2p->wfd_assoc_bssid)
373e5b75505Sopenharmony_ci		wpabuf_put_buf(wfd_subelems,
374e5b75505Sopenharmony_ci			       group->p2p->wfd_assoc_bssid);
375e5b75505Sopenharmony_ci	if (group->p2p->wfd_coupled_sink_info)
376e5b75505Sopenharmony_ci		wpabuf_put_buf(wfd_subelems,
377e5b75505Sopenharmony_ci			       group->p2p->wfd_coupled_sink_info);
378e5b75505Sopenharmony_ci
379e5b75505Sopenharmony_ci	/* Build WFD Session Info */
380e5b75505Sopenharmony_ci	wpabuf_put_u8(wfd_subelems, WFD_SUBELEM_SESSION_INFO);
381e5b75505Sopenharmony_ci	len = wpabuf_put(wfd_subelems, 2);
382e5b75505Sopenharmony_ci	m = group->members;
383e5b75505Sopenharmony_ci	while (m) {
384e5b75505Sopenharmony_ci		if (wifi_display_add_dev_info_descr(wfd_subelems, m))
385e5b75505Sopenharmony_ci			count++;
386e5b75505Sopenharmony_ci		m = m->next;
387e5b75505Sopenharmony_ci	}
388e5b75505Sopenharmony_ci
389e5b75505Sopenharmony_ci	if (count == 0) {
390e5b75505Sopenharmony_ci		/* No Wi-Fi Display clients - do not include subelement */
391e5b75505Sopenharmony_ci		wfd_subelems->used -= 3;
392e5b75505Sopenharmony_ci	} else {
393e5b75505Sopenharmony_ci		WPA_PUT_BE16(len, (u8 *) wpabuf_put(wfd_subelems, 0) - len -
394e5b75505Sopenharmony_ci			     2);
395e5b75505Sopenharmony_ci		p2p_dbg(group->p2p, "WFD: WFD Session Info: %u descriptors",
396e5b75505Sopenharmony_ci			count);
397e5b75505Sopenharmony_ci	}
398e5b75505Sopenharmony_ci
399e5b75505Sopenharmony_ci	wfd_ie = wifi_display_encaps(wfd_subelems);
400e5b75505Sopenharmony_ci	wpabuf_free(wfd_subelems);
401e5b75505Sopenharmony_ci
402e5b75505Sopenharmony_ci	return wfd_ie;
403e5b75505Sopenharmony_ci}
404e5b75505Sopenharmony_ci
405e5b75505Sopenharmony_cistatic void wifi_display_group_update(struct p2p_group *group)
406e5b75505Sopenharmony_ci{
407e5b75505Sopenharmony_ci	wpabuf_free(group->wfd_ie);
408e5b75505Sopenharmony_ci	group->wfd_ie = wifi_display_build_go_ie(group);
409e5b75505Sopenharmony_ci}
410e5b75505Sopenharmony_ci
411e5b75505Sopenharmony_ci#endif /* CONFIG_WIFI_DISPLAY */
412e5b75505Sopenharmony_ci
413e5b75505Sopenharmony_ci
414e5b75505Sopenharmony_civoid p2p_buf_add_group_info(struct p2p_group *group, struct wpabuf *buf,
415e5b75505Sopenharmony_ci			    int max_clients)
416e5b75505Sopenharmony_ci{
417e5b75505Sopenharmony_ci	u8 *group_info;
418e5b75505Sopenharmony_ci	int count = 0;
419e5b75505Sopenharmony_ci	struct p2p_group_member *m;
420e5b75505Sopenharmony_ci
421e5b75505Sopenharmony_ci	p2p_dbg(group->p2p, "* P2P Group Info");
422e5b75505Sopenharmony_ci	group_info = wpabuf_put(buf, 0);
423e5b75505Sopenharmony_ci	wpabuf_put_u8(buf, P2P_ATTR_GROUP_INFO);
424e5b75505Sopenharmony_ci	wpabuf_put_le16(buf, 0); /* Length to be filled */
425e5b75505Sopenharmony_ci	for (m = group->members; m; m = m->next) {
426e5b75505Sopenharmony_ci		p2p_client_info(buf, m);
427e5b75505Sopenharmony_ci		count++;
428e5b75505Sopenharmony_ci		if (max_clients >= 0 && count >= max_clients)
429e5b75505Sopenharmony_ci			break;
430e5b75505Sopenharmony_ci	}
431e5b75505Sopenharmony_ci	WPA_PUT_LE16(group_info + 1,
432e5b75505Sopenharmony_ci		     (u8 *) wpabuf_put(buf, 0) - group_info - 3);
433e5b75505Sopenharmony_ci}
434e5b75505Sopenharmony_ci
435e5b75505Sopenharmony_ci
436e5b75505Sopenharmony_civoid p2p_group_buf_add_id(struct p2p_group *group, struct wpabuf *buf)
437e5b75505Sopenharmony_ci{
438e5b75505Sopenharmony_ci	p2p_buf_add_group_id(buf, group->p2p->cfg->dev_addr, group->cfg->ssid,
439e5b75505Sopenharmony_ci			     group->cfg->ssid_len);
440e5b75505Sopenharmony_ci}
441e5b75505Sopenharmony_ci
442e5b75505Sopenharmony_ci
443e5b75505Sopenharmony_cistatic struct wpabuf * p2p_group_build_probe_resp_ie(struct p2p_group *group)
444e5b75505Sopenharmony_ci{
445e5b75505Sopenharmony_ci	struct wpabuf *p2p_subelems, *ie;
446e5b75505Sopenharmony_ci
447e5b75505Sopenharmony_ci	p2p_subelems = wpabuf_alloc(500);
448e5b75505Sopenharmony_ci	if (p2p_subelems == NULL)
449e5b75505Sopenharmony_ci		return NULL;
450e5b75505Sopenharmony_ci
451e5b75505Sopenharmony_ci	p2p_group_add_common_ies(group, p2p_subelems);
452e5b75505Sopenharmony_ci	p2p_group_add_noa(p2p_subelems, group->noa);
453e5b75505Sopenharmony_ci
454e5b75505Sopenharmony_ci	/* P2P Device Info */
455e5b75505Sopenharmony_ci	p2p_buf_add_device_info(p2p_subelems, group->p2p, NULL);
456e5b75505Sopenharmony_ci
457e5b75505Sopenharmony_ci	/* P2P Group Info: Only when at least one P2P Client is connected */
458e5b75505Sopenharmony_ci	if (group->members)
459e5b75505Sopenharmony_ci		p2p_buf_add_group_info(group, p2p_subelems, -1);
460e5b75505Sopenharmony_ci
461e5b75505Sopenharmony_ci	ie = p2p_group_encaps_probe_resp(p2p_subelems);
462e5b75505Sopenharmony_ci	wpabuf_free(p2p_subelems);
463e5b75505Sopenharmony_ci
464e5b75505Sopenharmony_ci	if (group->p2p->vendor_elem &&
465e5b75505Sopenharmony_ci	    group->p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P_GO]) {
466e5b75505Sopenharmony_ci		struct wpabuf *extra;
467e5b75505Sopenharmony_ci		extra = wpabuf_dup(group->p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P_GO]);
468e5b75505Sopenharmony_ci		ie = wpabuf_concat(extra, ie);
469e5b75505Sopenharmony_ci	}
470e5b75505Sopenharmony_ci
471e5b75505Sopenharmony_ci#ifdef CONFIG_WIFI_DISPLAY
472e5b75505Sopenharmony_ci	if (group->wfd_ie) {
473e5b75505Sopenharmony_ci		struct wpabuf *wfd = wpabuf_dup(group->wfd_ie);
474e5b75505Sopenharmony_ci		ie = wpabuf_concat(wfd, ie);
475e5b75505Sopenharmony_ci	}
476e5b75505Sopenharmony_ci#endif /* CONFIG_WIFI_DISPLAY */
477e5b75505Sopenharmony_ci
478e5b75505Sopenharmony_ci	return ie;
479e5b75505Sopenharmony_ci}
480e5b75505Sopenharmony_ci
481e5b75505Sopenharmony_ci
482e5b75505Sopenharmony_civoid p2p_group_update_ies(struct p2p_group *group)
483e5b75505Sopenharmony_ci{
484e5b75505Sopenharmony_ci	struct wpabuf *beacon_ie;
485e5b75505Sopenharmony_ci	struct wpabuf *probe_resp_ie;
486e5b75505Sopenharmony_ci
487e5b75505Sopenharmony_ci#ifdef CONFIG_WIFI_DISPLAY
488e5b75505Sopenharmony_ci	wifi_display_group_update(group);
489e5b75505Sopenharmony_ci#endif /* CONFIG_WIFI_DISPLAY */
490e5b75505Sopenharmony_ci
491e5b75505Sopenharmony_ci	probe_resp_ie = p2p_group_build_probe_resp_ie(group);
492e5b75505Sopenharmony_ci	if (probe_resp_ie == NULL)
493e5b75505Sopenharmony_ci		return;
494e5b75505Sopenharmony_ci	wpa_hexdump_buf(MSG_MSGDUMP, "P2P: Update GO Probe Response P2P IE",
495e5b75505Sopenharmony_ci			probe_resp_ie);
496e5b75505Sopenharmony_ci
497e5b75505Sopenharmony_ci	if (group->beacon_update) {
498e5b75505Sopenharmony_ci		beacon_ie = p2p_group_build_beacon_ie(group);
499e5b75505Sopenharmony_ci		if (beacon_ie)
500e5b75505Sopenharmony_ci			group->beacon_update = 0;
501e5b75505Sopenharmony_ci		wpa_hexdump_buf(MSG_MSGDUMP, "P2P: Update GO Beacon P2P IE",
502e5b75505Sopenharmony_ci				beacon_ie);
503e5b75505Sopenharmony_ci	} else
504e5b75505Sopenharmony_ci		beacon_ie = NULL;
505e5b75505Sopenharmony_ci
506e5b75505Sopenharmony_ci	group->cfg->ie_update(group->cfg->cb_ctx, beacon_ie, probe_resp_ie);
507e5b75505Sopenharmony_ci}
508e5b75505Sopenharmony_ci
509e5b75505Sopenharmony_ci
510e5b75505Sopenharmony_ci/**
511e5b75505Sopenharmony_ci * p2p_build_client_info - Build P2P Client Info Descriptor
512e5b75505Sopenharmony_ci * @addr: MAC address of the peer device
513e5b75505Sopenharmony_ci * @p2p_ie: P2P IE from (Re)Association Request
514e5b75505Sopenharmony_ci * @dev_capab: Buffer for returning Device Capability
515e5b75505Sopenharmony_ci * @dev_addr: Buffer for returning P2P Device Address
516e5b75505Sopenharmony_ci * Returns: P2P Client Info Descriptor or %NULL on failure
517e5b75505Sopenharmony_ci *
518e5b75505Sopenharmony_ci * This function builds P2P Client Info Descriptor based on the information
519e5b75505Sopenharmony_ci * available from (Re)Association Request frame. Group owner can use this to
520e5b75505Sopenharmony_ci * build the P2P Group Info attribute for Probe Response frames.
521e5b75505Sopenharmony_ci */
522e5b75505Sopenharmony_cistatic struct wpabuf * p2p_build_client_info(const u8 *addr,
523e5b75505Sopenharmony_ci					     struct wpabuf *p2p_ie,
524e5b75505Sopenharmony_ci					     u8 *dev_capab, u8 *dev_addr)
525e5b75505Sopenharmony_ci{
526e5b75505Sopenharmony_ci	const u8 *spos;
527e5b75505Sopenharmony_ci	struct p2p_message msg;
528e5b75505Sopenharmony_ci	u8 *len_pos;
529e5b75505Sopenharmony_ci	struct wpabuf *buf;
530e5b75505Sopenharmony_ci
531e5b75505Sopenharmony_ci	if (p2p_ie == NULL)
532e5b75505Sopenharmony_ci		return NULL;
533e5b75505Sopenharmony_ci
534e5b75505Sopenharmony_ci	os_memset(&msg, 0, sizeof(msg));
535e5b75505Sopenharmony_ci	if (p2p_parse_p2p_ie(p2p_ie, &msg) ||
536e5b75505Sopenharmony_ci	    msg.capability == NULL || msg.p2p_device_info == NULL)
537e5b75505Sopenharmony_ci		return NULL;
538e5b75505Sopenharmony_ci
539e5b75505Sopenharmony_ci	buf = wpabuf_alloc(ETH_ALEN + 1 + 1 + msg.p2p_device_info_len);
540e5b75505Sopenharmony_ci	if (buf == NULL)
541e5b75505Sopenharmony_ci		return NULL;
542e5b75505Sopenharmony_ci
543e5b75505Sopenharmony_ci	*dev_capab = msg.capability[0];
544e5b75505Sopenharmony_ci	os_memcpy(dev_addr, msg.p2p_device_addr, ETH_ALEN);
545e5b75505Sopenharmony_ci
546e5b75505Sopenharmony_ci	spos = msg.p2p_device_info; /* P2P Device address */
547e5b75505Sopenharmony_ci
548e5b75505Sopenharmony_ci	/* P2P Client Info Descriptor */
549e5b75505Sopenharmony_ci	/* Length to be set */
550e5b75505Sopenharmony_ci	len_pos = wpabuf_put(buf, 1);
551e5b75505Sopenharmony_ci	/* P2P Device address */
552e5b75505Sopenharmony_ci	wpabuf_put_data(buf, spos, ETH_ALEN);
553e5b75505Sopenharmony_ci	/* P2P Interface address */
554e5b75505Sopenharmony_ci	wpabuf_put_data(buf, addr, ETH_ALEN);
555e5b75505Sopenharmony_ci	/* Device Capability Bitmap */
556e5b75505Sopenharmony_ci	wpabuf_put_u8(buf, msg.capability[0]);
557e5b75505Sopenharmony_ci	/*
558e5b75505Sopenharmony_ci	 * Config Methods, Primary Device Type, Number of Secondary Device
559e5b75505Sopenharmony_ci	 * Types, Secondary Device Type List, Device Name copied from
560e5b75505Sopenharmony_ci	 * Device Info
561e5b75505Sopenharmony_ci	 */
562e5b75505Sopenharmony_ci	wpabuf_put_data(buf, spos + ETH_ALEN,
563e5b75505Sopenharmony_ci			msg.p2p_device_info_len - ETH_ALEN);
564e5b75505Sopenharmony_ci
565e5b75505Sopenharmony_ci	*len_pos = wpabuf_len(buf) - 1;
566e5b75505Sopenharmony_ci
567e5b75505Sopenharmony_ci
568e5b75505Sopenharmony_ci	return buf;
569e5b75505Sopenharmony_ci}
570e5b75505Sopenharmony_ci
571e5b75505Sopenharmony_ci
572e5b75505Sopenharmony_cistatic int p2p_group_remove_member(struct p2p_group *group, const u8 *addr)
573e5b75505Sopenharmony_ci{
574e5b75505Sopenharmony_ci	struct p2p_group_member *m, *prev;
575e5b75505Sopenharmony_ci
576e5b75505Sopenharmony_ci	if (group == NULL)
577e5b75505Sopenharmony_ci		return 0;
578e5b75505Sopenharmony_ci
579e5b75505Sopenharmony_ci	m = group->members;
580e5b75505Sopenharmony_ci	prev = NULL;
581e5b75505Sopenharmony_ci	while (m) {
582e5b75505Sopenharmony_ci		if (os_memcmp(m->addr, addr, ETH_ALEN) == 0)
583e5b75505Sopenharmony_ci			break;
584e5b75505Sopenharmony_ci		prev = m;
585e5b75505Sopenharmony_ci		m = m->next;
586e5b75505Sopenharmony_ci	}
587e5b75505Sopenharmony_ci
588e5b75505Sopenharmony_ci	if (m == NULL)
589e5b75505Sopenharmony_ci		return 0;
590e5b75505Sopenharmony_ci
591e5b75505Sopenharmony_ci	if (prev)
592e5b75505Sopenharmony_ci		prev->next = m->next;
593e5b75505Sopenharmony_ci	else
594e5b75505Sopenharmony_ci		group->members = m->next;
595e5b75505Sopenharmony_ci	p2p_group_free_member(m);
596e5b75505Sopenharmony_ci	group->num_members--;
597e5b75505Sopenharmony_ci
598e5b75505Sopenharmony_ci	return 1;
599e5b75505Sopenharmony_ci}
600e5b75505Sopenharmony_ci
601e5b75505Sopenharmony_ci
602e5b75505Sopenharmony_ciint p2p_group_notif_assoc(struct p2p_group *group, const u8 *addr,
603e5b75505Sopenharmony_ci			  const u8 *ie, size_t len)
604e5b75505Sopenharmony_ci{
605e5b75505Sopenharmony_ci	struct p2p_group_member *m;
606e5b75505Sopenharmony_ci
607e5b75505Sopenharmony_ci	if (group == NULL)
608e5b75505Sopenharmony_ci		return -1;
609e5b75505Sopenharmony_ci
610e5b75505Sopenharmony_ci	p2p_add_device(group->p2p, addr, 0, NULL, 0, ie, len, 0);
611e5b75505Sopenharmony_ci
612e5b75505Sopenharmony_ci	m = os_zalloc(sizeof(*m));
613e5b75505Sopenharmony_ci	if (m == NULL)
614e5b75505Sopenharmony_ci		return -1;
615e5b75505Sopenharmony_ci	os_memcpy(m->addr, addr, ETH_ALEN);
616e5b75505Sopenharmony_ci	m->p2p_ie = ieee802_11_vendor_ie_concat(ie, len, P2P_IE_VENDOR_TYPE);
617e5b75505Sopenharmony_ci	if (m->p2p_ie) {
618e5b75505Sopenharmony_ci		m->client_info = p2p_build_client_info(addr, m->p2p_ie,
619e5b75505Sopenharmony_ci						       &m->dev_capab,
620e5b75505Sopenharmony_ci						       m->dev_addr);
621e5b75505Sopenharmony_ci	}
622e5b75505Sopenharmony_ci#ifdef CONFIG_WIFI_DISPLAY
623e5b75505Sopenharmony_ci	m->wfd_ie = ieee802_11_vendor_ie_concat(ie, len, WFD_IE_VENDOR_TYPE);
624e5b75505Sopenharmony_ci#endif /* CONFIG_WIFI_DISPLAY */
625e5b75505Sopenharmony_ci
626e5b75505Sopenharmony_ci	p2p_group_remove_member(group, addr);
627e5b75505Sopenharmony_ci
628e5b75505Sopenharmony_ci	m->next = group->members;
629e5b75505Sopenharmony_ci	group->members = m;
630e5b75505Sopenharmony_ci	group->num_members++;
631e5b75505Sopenharmony_ci	p2p_dbg(group->p2p,  "Add client " MACSTR
632e5b75505Sopenharmony_ci		" to group (p2p=%d wfd=%d client_info=%d); num_members=%u/%u",
633e5b75505Sopenharmony_ci		MAC2STR(addr), m->p2p_ie ? 1 : 0, m->wfd_ie ? 1 : 0,
634e5b75505Sopenharmony_ci		m->client_info ? 1 : 0,
635e5b75505Sopenharmony_ci		group->num_members, group->cfg->max_clients);
636e5b75505Sopenharmony_ci	if (group->num_members == group->cfg->max_clients)
637e5b75505Sopenharmony_ci		group->beacon_update = 1;
638e5b75505Sopenharmony_ci	p2p_group_update_ies(group);
639e5b75505Sopenharmony_ci	if (group->num_members == 1)
640e5b75505Sopenharmony_ci		group->cfg->idle_update(group->cfg->cb_ctx, 0);
641e5b75505Sopenharmony_ci
642e5b75505Sopenharmony_ci	return 0;
643e5b75505Sopenharmony_ci}
644e5b75505Sopenharmony_ci
645e5b75505Sopenharmony_ci
646e5b75505Sopenharmony_cistruct wpabuf * p2p_group_assoc_resp_ie(struct p2p_group *group, u8 status)
647e5b75505Sopenharmony_ci{
648e5b75505Sopenharmony_ci	struct wpabuf *resp;
649e5b75505Sopenharmony_ci	u8 *rlen;
650e5b75505Sopenharmony_ci	size_t extra = 0;
651e5b75505Sopenharmony_ci
652e5b75505Sopenharmony_ci#ifdef CONFIG_WIFI_DISPLAY
653e5b75505Sopenharmony_ci	if (group->wfd_ie)
654e5b75505Sopenharmony_ci		extra = wpabuf_len(group->wfd_ie);
655e5b75505Sopenharmony_ci#endif /* CONFIG_WIFI_DISPLAY */
656e5b75505Sopenharmony_ci
657e5b75505Sopenharmony_ci	if (group->p2p->vendor_elem &&
658e5b75505Sopenharmony_ci	    group->p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_RESP])
659e5b75505Sopenharmony_ci		extra += wpabuf_len(group->p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_RESP]);
660e5b75505Sopenharmony_ci
661e5b75505Sopenharmony_ci	/*
662e5b75505Sopenharmony_ci	 * (Re)Association Response - P2P IE
663e5b75505Sopenharmony_ci	 * Status attribute (shall be present when association request is
664e5b75505Sopenharmony_ci	 *	denied)
665e5b75505Sopenharmony_ci	 * Extended Listen Timing (may be present)
666e5b75505Sopenharmony_ci	 */
667e5b75505Sopenharmony_ci	resp = wpabuf_alloc(20 + extra);
668e5b75505Sopenharmony_ci	if (resp == NULL)
669e5b75505Sopenharmony_ci		return NULL;
670e5b75505Sopenharmony_ci
671e5b75505Sopenharmony_ci#ifdef CONFIG_WIFI_DISPLAY
672e5b75505Sopenharmony_ci	if (group->wfd_ie)
673e5b75505Sopenharmony_ci		wpabuf_put_buf(resp, group->wfd_ie);
674e5b75505Sopenharmony_ci#endif /* CONFIG_WIFI_DISPLAY */
675e5b75505Sopenharmony_ci
676e5b75505Sopenharmony_ci	if (group->p2p->vendor_elem &&
677e5b75505Sopenharmony_ci	    group->p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_RESP])
678e5b75505Sopenharmony_ci		wpabuf_put_buf(resp,
679e5b75505Sopenharmony_ci			       group->p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_RESP]);
680e5b75505Sopenharmony_ci
681e5b75505Sopenharmony_ci	rlen = p2p_buf_add_ie_hdr(resp);
682e5b75505Sopenharmony_ci	if (status != P2P_SC_SUCCESS)
683e5b75505Sopenharmony_ci		p2p_buf_add_status(resp, status);
684e5b75505Sopenharmony_ci	p2p_buf_update_ie_hdr(resp, rlen);
685e5b75505Sopenharmony_ci
686e5b75505Sopenharmony_ci	return resp;
687e5b75505Sopenharmony_ci}
688e5b75505Sopenharmony_ci
689e5b75505Sopenharmony_ci
690e5b75505Sopenharmony_civoid p2p_group_notif_disassoc(struct p2p_group *group, const u8 *addr)
691e5b75505Sopenharmony_ci{
692e5b75505Sopenharmony_ci	if (p2p_group_remove_member(group, addr)) {
693e5b75505Sopenharmony_ci		p2p_dbg(group->p2p, "Remove client " MACSTR
694e5b75505Sopenharmony_ci			" from group; num_members=%u/%u",
695e5b75505Sopenharmony_ci			MAC2STR(addr), group->num_members,
696e5b75505Sopenharmony_ci			group->cfg->max_clients);
697e5b75505Sopenharmony_ci		if (group->num_members == group->cfg->max_clients - 1)
698e5b75505Sopenharmony_ci			group->beacon_update = 1;
699e5b75505Sopenharmony_ci		p2p_group_update_ies(group);
700e5b75505Sopenharmony_ci		if (group->num_members == 0)
701e5b75505Sopenharmony_ci			group->cfg->idle_update(group->cfg->cb_ctx, 1);
702e5b75505Sopenharmony_ci	}
703e5b75505Sopenharmony_ci}
704e5b75505Sopenharmony_ci
705e5b75505Sopenharmony_ci
706e5b75505Sopenharmony_ci/**
707e5b75505Sopenharmony_ci * p2p_match_dev_type_member - Match client device type with requested type
708e5b75505Sopenharmony_ci * @m: Group member
709e5b75505Sopenharmony_ci * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs)
710e5b75505Sopenharmony_ci * Returns: 1 on match, 0 on mismatch
711e5b75505Sopenharmony_ci *
712e5b75505Sopenharmony_ci * This function can be used to match the Requested Device Type attribute in
713e5b75505Sopenharmony_ci * WPS IE with the device types of a group member for deciding whether a GO
714e5b75505Sopenharmony_ci * should reply to a Probe Request frame.
715e5b75505Sopenharmony_ci */
716e5b75505Sopenharmony_cistatic int p2p_match_dev_type_member(struct p2p_group_member *m,
717e5b75505Sopenharmony_ci				     struct wpabuf *wps)
718e5b75505Sopenharmony_ci{
719e5b75505Sopenharmony_ci	const u8 *pos, *end;
720e5b75505Sopenharmony_ci	struct wps_parse_attr attr;
721e5b75505Sopenharmony_ci	u8 num_sec;
722e5b75505Sopenharmony_ci
723e5b75505Sopenharmony_ci	if (m->client_info == NULL || wps == NULL)
724e5b75505Sopenharmony_ci		return 0;
725e5b75505Sopenharmony_ci
726e5b75505Sopenharmony_ci	pos = wpabuf_head(m->client_info);
727e5b75505Sopenharmony_ci	end = pos + wpabuf_len(m->client_info);
728e5b75505Sopenharmony_ci
729e5b75505Sopenharmony_ci	pos += 1 + 2 * ETH_ALEN + 1 + 2;
730e5b75505Sopenharmony_ci	if (end - pos < WPS_DEV_TYPE_LEN + 1)
731e5b75505Sopenharmony_ci		return 0;
732e5b75505Sopenharmony_ci
733e5b75505Sopenharmony_ci	if (wps_parse_msg(wps, &attr))
734e5b75505Sopenharmony_ci		return 1; /* assume no Requested Device Type attributes */
735e5b75505Sopenharmony_ci
736e5b75505Sopenharmony_ci	if (attr.num_req_dev_type == 0)
737e5b75505Sopenharmony_ci		return 1; /* no Requested Device Type attributes -> match */
738e5b75505Sopenharmony_ci
739e5b75505Sopenharmony_ci	if (dev_type_list_match(pos, attr.req_dev_type, attr.num_req_dev_type))
740e5b75505Sopenharmony_ci		return 1; /* Match with client Primary Device Type */
741e5b75505Sopenharmony_ci
742e5b75505Sopenharmony_ci	pos += WPS_DEV_TYPE_LEN;
743e5b75505Sopenharmony_ci	num_sec = *pos++;
744e5b75505Sopenharmony_ci	if (end - pos < num_sec * WPS_DEV_TYPE_LEN)
745e5b75505Sopenharmony_ci		return 0;
746e5b75505Sopenharmony_ci	while (num_sec > 0) {
747e5b75505Sopenharmony_ci		num_sec--;
748e5b75505Sopenharmony_ci		if (dev_type_list_match(pos, attr.req_dev_type,
749e5b75505Sopenharmony_ci					attr.num_req_dev_type))
750e5b75505Sopenharmony_ci			return 1; /* Match with client Secondary Device Type */
751e5b75505Sopenharmony_ci		pos += WPS_DEV_TYPE_LEN;
752e5b75505Sopenharmony_ci	}
753e5b75505Sopenharmony_ci
754e5b75505Sopenharmony_ci	/* No matching device type found */
755e5b75505Sopenharmony_ci	return 0;
756e5b75505Sopenharmony_ci}
757e5b75505Sopenharmony_ci
758e5b75505Sopenharmony_ci
759e5b75505Sopenharmony_ciint p2p_group_match_dev_type(struct p2p_group *group, struct wpabuf *wps)
760e5b75505Sopenharmony_ci{
761e5b75505Sopenharmony_ci	struct p2p_group_member *m;
762e5b75505Sopenharmony_ci
763e5b75505Sopenharmony_ci	if (p2p_match_dev_type(group->p2p, wps))
764e5b75505Sopenharmony_ci		return 1; /* Match with own device type */
765e5b75505Sopenharmony_ci
766e5b75505Sopenharmony_ci	for (m = group->members; m; m = m->next) {
767e5b75505Sopenharmony_ci		if (p2p_match_dev_type_member(m, wps))
768e5b75505Sopenharmony_ci			return 1; /* Match with group client device type */
769e5b75505Sopenharmony_ci	}
770e5b75505Sopenharmony_ci
771e5b75505Sopenharmony_ci	/* No match with Requested Device Type */
772e5b75505Sopenharmony_ci	return 0;
773e5b75505Sopenharmony_ci}
774e5b75505Sopenharmony_ci
775e5b75505Sopenharmony_ci
776e5b75505Sopenharmony_ciint p2p_group_match_dev_id(struct p2p_group *group, struct wpabuf *p2p)
777e5b75505Sopenharmony_ci{
778e5b75505Sopenharmony_ci	struct p2p_group_member *m;
779e5b75505Sopenharmony_ci	struct p2p_message msg;
780e5b75505Sopenharmony_ci
781e5b75505Sopenharmony_ci	os_memset(&msg, 0, sizeof(msg));
782e5b75505Sopenharmony_ci	if (p2p_parse_p2p_ie(p2p, &msg))
783e5b75505Sopenharmony_ci		return 1; /* Failed to parse - assume no filter on Device ID */
784e5b75505Sopenharmony_ci
785e5b75505Sopenharmony_ci	if (!msg.device_id)
786e5b75505Sopenharmony_ci		return 1; /* No filter on Device ID */
787e5b75505Sopenharmony_ci
788e5b75505Sopenharmony_ci	if (os_memcmp(msg.device_id, group->p2p->cfg->dev_addr, ETH_ALEN) == 0)
789e5b75505Sopenharmony_ci		return 1; /* Match with our P2P Device Address */
790e5b75505Sopenharmony_ci
791e5b75505Sopenharmony_ci	for (m = group->members; m; m = m->next) {
792e5b75505Sopenharmony_ci		if (os_memcmp(msg.device_id, m->dev_addr, ETH_ALEN) == 0)
793e5b75505Sopenharmony_ci			return 1; /* Match with group client P2P Device Address */
794e5b75505Sopenharmony_ci	}
795e5b75505Sopenharmony_ci
796e5b75505Sopenharmony_ci	/* No match with Device ID */
797e5b75505Sopenharmony_ci	return 0;
798e5b75505Sopenharmony_ci}
799e5b75505Sopenharmony_ci
800e5b75505Sopenharmony_ci
801e5b75505Sopenharmony_civoid p2p_group_notif_formation_done(struct p2p_group *group)
802e5b75505Sopenharmony_ci{
803e5b75505Sopenharmony_ci	if (group == NULL)
804e5b75505Sopenharmony_ci		return;
805e5b75505Sopenharmony_ci	group->group_formation = 0;
806e5b75505Sopenharmony_ci	group->beacon_update = 1;
807e5b75505Sopenharmony_ci	p2p_group_update_ies(group);
808e5b75505Sopenharmony_ci}
809e5b75505Sopenharmony_ci
810e5b75505Sopenharmony_ci
811e5b75505Sopenharmony_ciint p2p_group_notif_noa(struct p2p_group *group, const u8 *noa,
812e5b75505Sopenharmony_ci			size_t noa_len)
813e5b75505Sopenharmony_ci{
814e5b75505Sopenharmony_ci	if (noa == NULL) {
815e5b75505Sopenharmony_ci		wpabuf_free(group->noa);
816e5b75505Sopenharmony_ci		group->noa = NULL;
817e5b75505Sopenharmony_ci	} else {
818e5b75505Sopenharmony_ci		if (group->noa) {
819e5b75505Sopenharmony_ci			if (wpabuf_size(group->noa) >= noa_len) {
820e5b75505Sopenharmony_ci				group->noa->used = 0;
821e5b75505Sopenharmony_ci				wpabuf_put_data(group->noa, noa, noa_len);
822e5b75505Sopenharmony_ci			} else {
823e5b75505Sopenharmony_ci				wpabuf_free(group->noa);
824e5b75505Sopenharmony_ci				group->noa = NULL;
825e5b75505Sopenharmony_ci			}
826e5b75505Sopenharmony_ci		}
827e5b75505Sopenharmony_ci
828e5b75505Sopenharmony_ci		if (!group->noa) {
829e5b75505Sopenharmony_ci			group->noa = wpabuf_alloc_copy(noa, noa_len);
830e5b75505Sopenharmony_ci			if (group->noa == NULL)
831e5b75505Sopenharmony_ci				return -1;
832e5b75505Sopenharmony_ci		}
833e5b75505Sopenharmony_ci	}
834e5b75505Sopenharmony_ci
835e5b75505Sopenharmony_ci	group->beacon_update = 1;
836e5b75505Sopenharmony_ci	p2p_group_update_ies(group);
837e5b75505Sopenharmony_ci	return 0;
838e5b75505Sopenharmony_ci}
839e5b75505Sopenharmony_ci
840e5b75505Sopenharmony_ci
841e5b75505Sopenharmony_cistatic struct p2p_group_member * p2p_group_get_client(struct p2p_group *group,
842e5b75505Sopenharmony_ci						      const u8 *dev_id)
843e5b75505Sopenharmony_ci{
844e5b75505Sopenharmony_ci	struct p2p_group_member *m;
845e5b75505Sopenharmony_ci
846e5b75505Sopenharmony_ci	for (m = group->members; m; m = m->next) {
847e5b75505Sopenharmony_ci		if (os_memcmp(dev_id, m->dev_addr, ETH_ALEN) == 0)
848e5b75505Sopenharmony_ci			return m;
849e5b75505Sopenharmony_ci	}
850e5b75505Sopenharmony_ci
851e5b75505Sopenharmony_ci	return NULL;
852e5b75505Sopenharmony_ci}
853e5b75505Sopenharmony_ci
854e5b75505Sopenharmony_ci
855e5b75505Sopenharmony_ciconst u8 * p2p_group_get_client_interface_addr(struct p2p_group *group,
856e5b75505Sopenharmony_ci					       const u8 *dev_addr)
857e5b75505Sopenharmony_ci{
858e5b75505Sopenharmony_ci	struct p2p_group_member *m;
859e5b75505Sopenharmony_ci
860e5b75505Sopenharmony_ci	if (!group)
861e5b75505Sopenharmony_ci		return NULL;
862e5b75505Sopenharmony_ci	m = p2p_group_get_client(group, dev_addr);
863e5b75505Sopenharmony_ci	if (m)
864e5b75505Sopenharmony_ci		return m->addr;
865e5b75505Sopenharmony_ci	return NULL;
866e5b75505Sopenharmony_ci}
867e5b75505Sopenharmony_ci
868e5b75505Sopenharmony_ci
869e5b75505Sopenharmony_cistatic struct p2p_group_member * p2p_group_get_client_iface(
870e5b75505Sopenharmony_ci	struct p2p_group *group, const u8 *interface_addr)
871e5b75505Sopenharmony_ci{
872e5b75505Sopenharmony_ci	struct p2p_group_member *m;
873e5b75505Sopenharmony_ci
874e5b75505Sopenharmony_ci	for (m = group->members; m; m = m->next) {
875e5b75505Sopenharmony_ci		if (os_memcmp(interface_addr, m->addr, ETH_ALEN) == 0)
876e5b75505Sopenharmony_ci			return m;
877e5b75505Sopenharmony_ci	}
878e5b75505Sopenharmony_ci
879e5b75505Sopenharmony_ci	return NULL;
880e5b75505Sopenharmony_ci}
881e5b75505Sopenharmony_ci
882e5b75505Sopenharmony_ci
883e5b75505Sopenharmony_ciconst u8 * p2p_group_get_dev_addr(struct p2p_group *group, const u8 *addr)
884e5b75505Sopenharmony_ci{
885e5b75505Sopenharmony_ci	struct p2p_group_member *m;
886e5b75505Sopenharmony_ci
887e5b75505Sopenharmony_ci	if (group == NULL)
888e5b75505Sopenharmony_ci		return NULL;
889e5b75505Sopenharmony_ci	m = p2p_group_get_client_iface(group, addr);
890e5b75505Sopenharmony_ci	if (m && !is_zero_ether_addr(m->dev_addr))
891e5b75505Sopenharmony_ci		return m->dev_addr;
892e5b75505Sopenharmony_ci	return NULL;
893e5b75505Sopenharmony_ci}
894e5b75505Sopenharmony_ci
895e5b75505Sopenharmony_ci
896e5b75505Sopenharmony_cistatic struct wpabuf * p2p_build_go_disc_req(void)
897e5b75505Sopenharmony_ci{
898e5b75505Sopenharmony_ci	struct wpabuf *buf;
899e5b75505Sopenharmony_ci
900e5b75505Sopenharmony_ci	buf = wpabuf_alloc(100);
901e5b75505Sopenharmony_ci	if (buf == NULL)
902e5b75505Sopenharmony_ci		return NULL;
903e5b75505Sopenharmony_ci
904e5b75505Sopenharmony_ci	p2p_buf_add_action_hdr(buf, P2P_GO_DISC_REQ, 0);
905e5b75505Sopenharmony_ci
906e5b75505Sopenharmony_ci	return buf;
907e5b75505Sopenharmony_ci}
908e5b75505Sopenharmony_ci
909e5b75505Sopenharmony_ci
910e5b75505Sopenharmony_ciint p2p_group_go_discover(struct p2p_group *group, const u8 *dev_id,
911e5b75505Sopenharmony_ci			  const u8 *searching_dev, int rx_freq)
912e5b75505Sopenharmony_ci{
913e5b75505Sopenharmony_ci	struct p2p_group_member *m;
914e5b75505Sopenharmony_ci	struct wpabuf *req;
915e5b75505Sopenharmony_ci	struct p2p_data *p2p = group->p2p;
916e5b75505Sopenharmony_ci	int freq;
917e5b75505Sopenharmony_ci
918e5b75505Sopenharmony_ci	m = p2p_group_get_client(group, dev_id);
919e5b75505Sopenharmony_ci	if (m == NULL || m->client_info == NULL) {
920e5b75505Sopenharmony_ci		p2p_dbg(group->p2p, "Requested client was not in this group "
921e5b75505Sopenharmony_ci			MACSTR, MAC2STR(group->cfg->interface_addr));
922e5b75505Sopenharmony_ci		return -1;
923e5b75505Sopenharmony_ci	}
924e5b75505Sopenharmony_ci
925e5b75505Sopenharmony_ci	if (!(m->dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
926e5b75505Sopenharmony_ci		p2p_dbg(group->p2p, "Requested client does not support client discoverability");
927e5b75505Sopenharmony_ci		return -1;
928e5b75505Sopenharmony_ci	}
929e5b75505Sopenharmony_ci
930e5b75505Sopenharmony_ci	p2p_dbg(group->p2p, "Schedule GO Discoverability Request to be sent to "
931e5b75505Sopenharmony_ci		MACSTR, MAC2STR(dev_id));
932e5b75505Sopenharmony_ci
933e5b75505Sopenharmony_ci	req = p2p_build_go_disc_req();
934e5b75505Sopenharmony_ci	if (req == NULL)
935e5b75505Sopenharmony_ci		return -1;
936e5b75505Sopenharmony_ci
937e5b75505Sopenharmony_ci	/* TODO: Should really use group operating frequency here */
938e5b75505Sopenharmony_ci	freq = rx_freq;
939e5b75505Sopenharmony_ci
940e5b75505Sopenharmony_ci	p2p->pending_action_state = P2P_PENDING_GO_DISC_REQ;
941e5b75505Sopenharmony_ci	if (p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, m->addr,
942e5b75505Sopenharmony_ci				  group->cfg->interface_addr,
943e5b75505Sopenharmony_ci				  group->cfg->interface_addr,
944e5b75505Sopenharmony_ci				  wpabuf_head(req), wpabuf_len(req), 200, NULL)
945e5b75505Sopenharmony_ci	    < 0)
946e5b75505Sopenharmony_ci	{
947e5b75505Sopenharmony_ci		p2p_dbg(p2p, "Failed to send Action frame");
948e5b75505Sopenharmony_ci	}
949e5b75505Sopenharmony_ci
950e5b75505Sopenharmony_ci	wpabuf_free(req);
951e5b75505Sopenharmony_ci
952e5b75505Sopenharmony_ci	return 0;
953e5b75505Sopenharmony_ci}
954e5b75505Sopenharmony_ci
955e5b75505Sopenharmony_ci
956e5b75505Sopenharmony_ciconst u8 * p2p_group_get_interface_addr(struct p2p_group *group)
957e5b75505Sopenharmony_ci{
958e5b75505Sopenharmony_ci	return group->cfg->interface_addr;
959e5b75505Sopenharmony_ci}
960e5b75505Sopenharmony_ci
961e5b75505Sopenharmony_ci
962e5b75505Sopenharmony_ciu8 p2p_group_presence_req(struct p2p_group *group,
963e5b75505Sopenharmony_ci			  const u8 *client_interface_addr,
964e5b75505Sopenharmony_ci			  const u8 *noa, size_t noa_len)
965e5b75505Sopenharmony_ci{
966e5b75505Sopenharmony_ci	struct p2p_group_member *m;
967e5b75505Sopenharmony_ci	u8 curr_noa[50];
968e5b75505Sopenharmony_ci	int curr_noa_len;
969e5b75505Sopenharmony_ci
970e5b75505Sopenharmony_ci	m = p2p_group_get_client_iface(group, client_interface_addr);
971e5b75505Sopenharmony_ci	if (m == NULL || m->client_info == NULL) {
972e5b75505Sopenharmony_ci		p2p_dbg(group->p2p, "Client was not in this group");
973e5b75505Sopenharmony_ci		return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
974e5b75505Sopenharmony_ci	}
975e5b75505Sopenharmony_ci
976e5b75505Sopenharmony_ci	wpa_hexdump(MSG_DEBUG, "P2P: Presence Request NoA", noa, noa_len);
977e5b75505Sopenharmony_ci
978e5b75505Sopenharmony_ci	if (group->p2p->cfg->get_noa)
979e5b75505Sopenharmony_ci		curr_noa_len = group->p2p->cfg->get_noa(
980e5b75505Sopenharmony_ci			group->p2p->cfg->cb_ctx, group->cfg->interface_addr,
981e5b75505Sopenharmony_ci			curr_noa, sizeof(curr_noa));
982e5b75505Sopenharmony_ci	else
983e5b75505Sopenharmony_ci		curr_noa_len = -1;
984e5b75505Sopenharmony_ci	if (curr_noa_len < 0)
985e5b75505Sopenharmony_ci		p2p_dbg(group->p2p, "Failed to fetch current NoA");
986e5b75505Sopenharmony_ci	else if (curr_noa_len == 0)
987e5b75505Sopenharmony_ci		p2p_dbg(group->p2p, "No NoA being advertized");
988e5b75505Sopenharmony_ci	else
989e5b75505Sopenharmony_ci		wpa_hexdump(MSG_DEBUG, "P2P: Current NoA", curr_noa,
990e5b75505Sopenharmony_ci			    curr_noa_len);
991e5b75505Sopenharmony_ci
992e5b75505Sopenharmony_ci	/* TODO: properly process request and store copy */
993e5b75505Sopenharmony_ci	if (curr_noa_len > 0 || curr_noa_len == -1)
994e5b75505Sopenharmony_ci		return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
995e5b75505Sopenharmony_ci
996e5b75505Sopenharmony_ci	return P2P_SC_SUCCESS;
997e5b75505Sopenharmony_ci}
998e5b75505Sopenharmony_ci
999e5b75505Sopenharmony_ci
1000e5b75505Sopenharmony_ciunsigned int p2p_get_group_num_members(struct p2p_group *group)
1001e5b75505Sopenharmony_ci{
1002e5b75505Sopenharmony_ci	if (!group)
1003e5b75505Sopenharmony_ci		return 0;
1004e5b75505Sopenharmony_ci
1005e5b75505Sopenharmony_ci	return group->num_members;
1006e5b75505Sopenharmony_ci}
1007e5b75505Sopenharmony_ci
1008e5b75505Sopenharmony_ci
1009e5b75505Sopenharmony_ciint p2p_client_limit_reached(struct p2p_group *group)
1010e5b75505Sopenharmony_ci{
1011e5b75505Sopenharmony_ci	if (!group || !group->cfg)
1012e5b75505Sopenharmony_ci		return 1;
1013e5b75505Sopenharmony_ci
1014e5b75505Sopenharmony_ci	return group->num_members >= group->cfg->max_clients;
1015e5b75505Sopenharmony_ci}
1016e5b75505Sopenharmony_ci
1017e5b75505Sopenharmony_ci
1018e5b75505Sopenharmony_ciconst u8 * p2p_iterate_group_members(struct p2p_group *group, void **next)
1019e5b75505Sopenharmony_ci{
1020e5b75505Sopenharmony_ci	struct p2p_group_member *iter = *next;
1021e5b75505Sopenharmony_ci
1022e5b75505Sopenharmony_ci	if (!iter)
1023e5b75505Sopenharmony_ci		iter = group->members;
1024e5b75505Sopenharmony_ci	else
1025e5b75505Sopenharmony_ci		iter = iter->next;
1026e5b75505Sopenharmony_ci
1027e5b75505Sopenharmony_ci	*next = iter;
1028e5b75505Sopenharmony_ci
1029e5b75505Sopenharmony_ci	if (!iter)
1030e5b75505Sopenharmony_ci		return NULL;
1031e5b75505Sopenharmony_ci
1032e5b75505Sopenharmony_ci	return iter->dev_addr;
1033e5b75505Sopenharmony_ci}
1034e5b75505Sopenharmony_ci
1035e5b75505Sopenharmony_ci
1036e5b75505Sopenharmony_ciint p2p_group_is_client_connected(struct p2p_group *group, const u8 *dev_addr)
1037e5b75505Sopenharmony_ci{
1038e5b75505Sopenharmony_ci	struct p2p_group_member *m;
1039e5b75505Sopenharmony_ci
1040e5b75505Sopenharmony_ci	for (m = group->members; m; m = m->next) {
1041e5b75505Sopenharmony_ci		if (os_memcmp(m->dev_addr, dev_addr, ETH_ALEN) == 0)
1042e5b75505Sopenharmony_ci			return 1;
1043e5b75505Sopenharmony_ci	}
1044e5b75505Sopenharmony_ci
1045e5b75505Sopenharmony_ci	return 0;
1046e5b75505Sopenharmony_ci}
1047e5b75505Sopenharmony_ci
1048e5b75505Sopenharmony_ci
1049e5b75505Sopenharmony_ciint p2p_group_is_group_id_match(struct p2p_group *group, const u8 *group_id,
1050e5b75505Sopenharmony_ci				size_t group_id_len)
1051e5b75505Sopenharmony_ci{
1052e5b75505Sopenharmony_ci	if (group_id_len != ETH_ALEN + group->cfg->ssid_len)
1053e5b75505Sopenharmony_ci		return 0;
1054e5b75505Sopenharmony_ci	if (os_memcmp(group_id, group->p2p->cfg->dev_addr, ETH_ALEN) != 0)
1055e5b75505Sopenharmony_ci		return 0;
1056e5b75505Sopenharmony_ci	return os_memcmp(group_id + ETH_ALEN, group->cfg->ssid,
1057e5b75505Sopenharmony_ci			 group->cfg->ssid_len) == 0;
1058e5b75505Sopenharmony_ci}
1059e5b75505Sopenharmony_ci
1060e5b75505Sopenharmony_ci
1061e5b75505Sopenharmony_civoid p2p_group_force_beacon_update_ies(struct p2p_group *group)
1062e5b75505Sopenharmony_ci{
1063e5b75505Sopenharmony_ci	group->beacon_update = 1;
1064e5b75505Sopenharmony_ci	p2p_group_update_ies(group);
1065e5b75505Sopenharmony_ci}
1066e5b75505Sopenharmony_ci
1067e5b75505Sopenharmony_ci
1068e5b75505Sopenharmony_ciint p2p_group_get_freq(struct p2p_group *group)
1069e5b75505Sopenharmony_ci{
1070e5b75505Sopenharmony_ci	return group->cfg->freq;
1071e5b75505Sopenharmony_ci}
1072e5b75505Sopenharmony_ci
1073e5b75505Sopenharmony_ci
1074e5b75505Sopenharmony_ciconst struct p2p_group_config * p2p_group_get_config(struct p2p_group *group)
1075e5b75505Sopenharmony_ci{
1076e5b75505Sopenharmony_ci	return group->cfg;
1077e5b75505Sopenharmony_ci}
1078e5b75505Sopenharmony_ci
1079e5b75505Sopenharmony_ci
1080e5b75505Sopenharmony_civoid p2p_loop_on_all_groups(struct p2p_data *p2p,
1081e5b75505Sopenharmony_ci			    int (*group_callback)(struct p2p_group *group,
1082e5b75505Sopenharmony_ci						  void *user_data),
1083e5b75505Sopenharmony_ci			    void *user_data)
1084e5b75505Sopenharmony_ci{
1085e5b75505Sopenharmony_ci	unsigned int i;
1086e5b75505Sopenharmony_ci
1087e5b75505Sopenharmony_ci	for (i = 0; i < p2p->num_groups; i++) {
1088e5b75505Sopenharmony_ci		if (!group_callback(p2p->groups[i], user_data))
1089e5b75505Sopenharmony_ci			break;
1090e5b75505Sopenharmony_ci	}
1091e5b75505Sopenharmony_ci}
1092e5b75505Sopenharmony_ci
1093e5b75505Sopenharmony_ci
1094e5b75505Sopenharmony_ciint p2p_group_get_common_freqs(struct p2p_group *group, int *common_freqs,
1095e5b75505Sopenharmony_ci			       unsigned int *num)
1096e5b75505Sopenharmony_ci
1097e5b75505Sopenharmony_ci{
1098e5b75505Sopenharmony_ci	struct p2p_channels intersect, res;
1099e5b75505Sopenharmony_ci	struct p2p_group_member *m;
1100e5b75505Sopenharmony_ci
1101e5b75505Sopenharmony_ci	if (!group || !common_freqs || !num)
1102e5b75505Sopenharmony_ci		return -1;
1103e5b75505Sopenharmony_ci
1104e5b75505Sopenharmony_ci	os_memset(&intersect, 0, sizeof(intersect));
1105e5b75505Sopenharmony_ci	os_memset(&res, 0, sizeof(res));
1106e5b75505Sopenharmony_ci
1107e5b75505Sopenharmony_ci	p2p_channels_union(&intersect, &group->p2p->cfg->channels,
1108e5b75505Sopenharmony_ci			   &intersect);
1109e5b75505Sopenharmony_ci
1110e5b75505Sopenharmony_ci	p2p_channels_dump(group->p2p,
1111e5b75505Sopenharmony_ci			  "Group common freqs before iterating members",
1112e5b75505Sopenharmony_ci			  &intersect);
1113e5b75505Sopenharmony_ci
1114e5b75505Sopenharmony_ci	for (m = group->members; m; m = m->next) {
1115e5b75505Sopenharmony_ci		struct p2p_device *dev;
1116e5b75505Sopenharmony_ci
1117e5b75505Sopenharmony_ci		dev = p2p_get_device(group->p2p, m->dev_addr);
1118e5b75505Sopenharmony_ci		if (!dev || dev->channels.reg_classes == 0)
1119e5b75505Sopenharmony_ci			continue;
1120e5b75505Sopenharmony_ci
1121e5b75505Sopenharmony_ci		p2p_channels_intersect(&intersect, &dev->channels, &res);
1122e5b75505Sopenharmony_ci		intersect = res;
1123e5b75505Sopenharmony_ci	}
1124e5b75505Sopenharmony_ci
1125e5b75505Sopenharmony_ci	p2p_channels_dump(group->p2p, "Group common channels", &intersect);
1126e5b75505Sopenharmony_ci
1127e5b75505Sopenharmony_ci	os_memset(common_freqs, 0, *num * sizeof(int));
1128e5b75505Sopenharmony_ci	*num = p2p_channels_to_freqs(&intersect, common_freqs, *num);
1129e5b75505Sopenharmony_ci
1130e5b75505Sopenharmony_ci	return 0;
1131e5b75505Sopenharmony_ci}
1132