xref: /freebsd/contrib/wpa/src/fst/fst_group.c (revision a90b9d0159070121c221b966469c3e36d912bf82)
1325151a3SRui Paulo /*
2325151a3SRui Paulo  * FST module - FST group object implementation
3325151a3SRui Paulo  * Copyright (c) 2014, Qualcomm Atheros, Inc.
4325151a3SRui Paulo  *
5325151a3SRui Paulo  * This software may be distributed under the terms of the BSD license.
6325151a3SRui Paulo  * See README for more details.
7325151a3SRui Paulo  */
8325151a3SRui Paulo 
9325151a3SRui Paulo #include "utils/includes.h"
10325151a3SRui Paulo #include "utils/common.h"
11325151a3SRui Paulo #include "common/defs.h"
12325151a3SRui Paulo #include "common/ieee802_11_defs.h"
13325151a3SRui Paulo #include "common/ieee802_11_common.h"
14325151a3SRui Paulo #include "drivers/driver.h"
15325151a3SRui Paulo #include "fst/fst_internal.h"
16325151a3SRui Paulo #include "fst/fst_defs.h"
17325151a3SRui Paulo 
18325151a3SRui Paulo 
19325151a3SRui Paulo struct dl_list fst_global_groups_list;
20325151a3SRui Paulo 
21325151a3SRui Paulo 
fst_dump_mb_ies(const char * group_id,const char * ifname,struct wpabuf * mbies)22325151a3SRui Paulo static void fst_dump_mb_ies(const char *group_id, const char *ifname,
23325151a3SRui Paulo 			    struct wpabuf *mbies)
24325151a3SRui Paulo {
25325151a3SRui Paulo 	const u8 *p = wpabuf_head(mbies);
26325151a3SRui Paulo 	size_t s = wpabuf_len(mbies);
27325151a3SRui Paulo 
28325151a3SRui Paulo 	while (s >= 2) {
29325151a3SRui Paulo 		const struct multi_band_ie *mbie =
30325151a3SRui Paulo 			(const struct multi_band_ie *) p;
31*a90b9d01SCy Schubert 		size_t len;
32*a90b9d01SCy Schubert 
33325151a3SRui Paulo 		WPA_ASSERT(mbie->eid == WLAN_EID_MULTI_BAND);
3485732ac8SCy Schubert 		WPA_ASSERT(2U + mbie->len >= sizeof(*mbie));
35*a90b9d01SCy Schubert 		len = 2 + mbie->len;
36*a90b9d01SCy Schubert 		if (len > s)
37*a90b9d01SCy Schubert 			break;
38325151a3SRui Paulo 
39325151a3SRui Paulo 		fst_printf(MSG_WARNING,
40325151a3SRui Paulo 			   "%s: %s: mb_ctrl=%u band_id=%u op_class=%u chan=%u bssid="
41325151a3SRui Paulo 			   MACSTR
42325151a3SRui Paulo 			   " beacon_int=%u tsf_offs=[%u %u %u %u %u %u %u %u] mb_cc=0x%02x tmout=%u",
43325151a3SRui Paulo 			   group_id, ifname,
44325151a3SRui Paulo 			   mbie->mb_ctrl, mbie->band_id, mbie->op_class,
45325151a3SRui Paulo 			   mbie->chan, MAC2STR(mbie->bssid), mbie->beacon_int,
46325151a3SRui Paulo 			   mbie->tsf_offs[0], mbie->tsf_offs[1],
47325151a3SRui Paulo 			   mbie->tsf_offs[2], mbie->tsf_offs[3],
48325151a3SRui Paulo 			   mbie->tsf_offs[4], mbie->tsf_offs[5],
49325151a3SRui Paulo 			   mbie->tsf_offs[6], mbie->tsf_offs[7],
50325151a3SRui Paulo 			   mbie->mb_connection_capability,
51325151a3SRui Paulo 			   mbie->fst_session_tmout);
52325151a3SRui Paulo 
53*a90b9d01SCy Schubert 		p += len;
54*a90b9d01SCy Schubert 		s -= len;
55325151a3SRui Paulo 	}
56325151a3SRui Paulo }
57325151a3SRui Paulo 
58325151a3SRui Paulo 
fst_fill_mb_ie(struct wpabuf * buf,const u8 * bssid,const u8 * own_addr,enum mb_band_id band,u8 channel)59325151a3SRui Paulo static void fst_fill_mb_ie(struct wpabuf *buf, const u8 *bssid,
60325151a3SRui Paulo 			   const u8 *own_addr, enum mb_band_id band, u8 channel)
61325151a3SRui Paulo {
62325151a3SRui Paulo 	struct multi_band_ie *mbie;
63325151a3SRui Paulo 	size_t len = sizeof(*mbie);
64325151a3SRui Paulo 
65325151a3SRui Paulo 	if (own_addr)
66325151a3SRui Paulo 		len += ETH_ALEN;
67325151a3SRui Paulo 
68325151a3SRui Paulo 	mbie = wpabuf_put(buf, len);
69325151a3SRui Paulo 
70325151a3SRui Paulo 	os_memset(mbie, 0, len);
71325151a3SRui Paulo 
72325151a3SRui Paulo 	mbie->eid = WLAN_EID_MULTI_BAND;
73325151a3SRui Paulo 	mbie->len = len - 2;
74325151a3SRui Paulo #ifdef HOSTAPD
75325151a3SRui Paulo 	mbie->mb_ctrl = MB_STA_ROLE_AP;
76325151a3SRui Paulo 	mbie->mb_connection_capability = MB_CONNECTION_CAPABILITY_AP;
77325151a3SRui Paulo #else /* HOSTAPD */
78325151a3SRui Paulo 	mbie->mb_ctrl = MB_STA_ROLE_NON_PCP_NON_AP;
79325151a3SRui Paulo 	mbie->mb_connection_capability = 0;
80325151a3SRui Paulo #endif /* HOSTAPD */
81325151a3SRui Paulo 	if (bssid)
82325151a3SRui Paulo 		os_memcpy(mbie->bssid, bssid, ETH_ALEN);
83325151a3SRui Paulo 	mbie->band_id = band;
84325151a3SRui Paulo 	mbie->op_class = 0;  /* means all */
85325151a3SRui Paulo 	mbie->chan = channel;
86325151a3SRui Paulo 	mbie->fst_session_tmout = FST_DEFAULT_SESSION_TIMEOUT_TU;
87325151a3SRui Paulo 
88325151a3SRui Paulo 	if (own_addr) {
89325151a3SRui Paulo 		mbie->mb_ctrl |= MB_CTRL_STA_MAC_PRESENT;
90325151a3SRui Paulo 		os_memcpy(&mbie[1], own_addr, ETH_ALEN);
91325151a3SRui Paulo 	}
92325151a3SRui Paulo }
93325151a3SRui Paulo 
94325151a3SRui Paulo 
fst_fill_iface_mb_ies(struct fst_iface * f,struct wpabuf * buf)95325151a3SRui Paulo static unsigned fst_fill_iface_mb_ies(struct fst_iface *f, struct wpabuf *buf)
96325151a3SRui Paulo {
97325151a3SRui Paulo 	const  u8 *bssid;
98325151a3SRui Paulo 
99325151a3SRui Paulo 	bssid = fst_iface_get_bssid(f);
100325151a3SRui Paulo 	if (bssid) {
101325151a3SRui Paulo 		enum hostapd_hw_mode hw_mode;
102325151a3SRui Paulo 		u8 channel;
103325151a3SRui Paulo 
104325151a3SRui Paulo 		if (buf) {
105325151a3SRui Paulo 			fst_iface_get_channel_info(f, &hw_mode, &channel);
106325151a3SRui Paulo 			fst_fill_mb_ie(buf, bssid, fst_iface_get_addr(f),
107325151a3SRui Paulo 				       fst_hw_mode_to_band(hw_mode), channel);
108325151a3SRui Paulo 		}
109325151a3SRui Paulo 		return 1;
110325151a3SRui Paulo 	} else {
111325151a3SRui Paulo 		unsigned bands[MB_BAND_ID_WIFI_60GHZ + 1] = {};
112325151a3SRui Paulo 		struct hostapd_hw_modes *modes;
113325151a3SRui Paulo 		enum mb_band_id b;
114325151a3SRui Paulo 		int num_modes = fst_iface_get_hw_modes(f, &modes);
115325151a3SRui Paulo 		int ret = 0;
116325151a3SRui Paulo 
117325151a3SRui Paulo 		while (num_modes--) {
118325151a3SRui Paulo 			b = fst_hw_mode_to_band(modes->mode);
119325151a3SRui Paulo 			modes++;
120325151a3SRui Paulo 			if (b >= ARRAY_SIZE(bands) || bands[b]++)
121325151a3SRui Paulo 				continue;
122325151a3SRui Paulo 			ret++;
123325151a3SRui Paulo 			if (buf)
124325151a3SRui Paulo 				fst_fill_mb_ie(buf, NULL, fst_iface_get_addr(f),
125325151a3SRui Paulo 					       b, MB_STA_CHANNEL_ALL);
126325151a3SRui Paulo 		}
127325151a3SRui Paulo 		return ret;
128325151a3SRui Paulo 	}
129325151a3SRui Paulo }
130325151a3SRui Paulo 
131325151a3SRui Paulo 
fst_group_create_mb_ie(struct fst_group * g,struct fst_iface * i)132325151a3SRui Paulo static struct wpabuf * fst_group_create_mb_ie(struct fst_group *g,
133325151a3SRui Paulo 					      struct fst_iface *i)
134325151a3SRui Paulo {
135325151a3SRui Paulo 	struct wpabuf *buf;
136325151a3SRui Paulo 	struct fst_iface *f;
137325151a3SRui Paulo 	unsigned int nof_mbies = 0;
138325151a3SRui Paulo 	unsigned int nof_ifaces_added = 0;
139325151a3SRui Paulo 
140325151a3SRui Paulo 	foreach_fst_group_iface(g, f) {
141325151a3SRui Paulo 		if (f == i)
142325151a3SRui Paulo 			continue;
143325151a3SRui Paulo 		nof_mbies += fst_fill_iface_mb_ies(f, NULL);
144325151a3SRui Paulo 	}
145325151a3SRui Paulo 
146325151a3SRui Paulo 	buf = wpabuf_alloc(nof_mbies *
147325151a3SRui Paulo 			   (sizeof(struct multi_band_ie) + ETH_ALEN));
148325151a3SRui Paulo 	if (!buf) {
149325151a3SRui Paulo 		fst_printf_iface(i, MSG_ERROR,
150325151a3SRui Paulo 				 "cannot allocate mem for %u MB IEs",
151325151a3SRui Paulo 				 nof_mbies);
152325151a3SRui Paulo 		return NULL;
153325151a3SRui Paulo 	}
154325151a3SRui Paulo 
155325151a3SRui Paulo 	/* The list is sorted in descending order by priorities, so MB IEs will
156325151a3SRui Paulo 	 * be arranged in the same order, as required by spec (see corresponding
157325151a3SRui Paulo 	 * comment in.fst_attach().
158325151a3SRui Paulo 	 */
159325151a3SRui Paulo 	foreach_fst_group_iface(g, f) {
160325151a3SRui Paulo 		if (f == i)
161325151a3SRui Paulo 			continue;
162325151a3SRui Paulo 
163325151a3SRui Paulo 		fst_fill_iface_mb_ies(f, buf);
164325151a3SRui Paulo 		++nof_ifaces_added;
165325151a3SRui Paulo 
166325151a3SRui Paulo 		fst_printf_iface(i, MSG_DEBUG, "added to MB IE");
167325151a3SRui Paulo 	}
168325151a3SRui Paulo 
169325151a3SRui Paulo 	if (!nof_ifaces_added) {
170325151a3SRui Paulo 		wpabuf_free(buf);
171325151a3SRui Paulo 		buf = NULL;
172325151a3SRui Paulo 		fst_printf_iface(i, MSG_INFO,
173325151a3SRui Paulo 				 "cannot add MB IE: no backup ifaces");
174325151a3SRui Paulo 	} else {
175325151a3SRui Paulo 		fst_dump_mb_ies(fst_group_get_id(g), fst_iface_get_name(i),
176325151a3SRui Paulo 				buf);
177325151a3SRui Paulo 	}
178325151a3SRui Paulo 
179325151a3SRui Paulo 	return buf;
180325151a3SRui Paulo }
181325151a3SRui Paulo 
182325151a3SRui Paulo 
fst_mbie_get_peer_addr(const struct multi_band_ie * mbie)183325151a3SRui Paulo static const u8 * fst_mbie_get_peer_addr(const struct multi_band_ie *mbie)
184325151a3SRui Paulo {
185325151a3SRui Paulo 	const u8 *peer_addr = NULL;
186325151a3SRui Paulo 
187325151a3SRui Paulo 	switch (MB_CTRL_ROLE(mbie->mb_ctrl)) {
188325151a3SRui Paulo 	case MB_STA_ROLE_AP:
189325151a3SRui Paulo 		peer_addr = mbie->bssid;
190325151a3SRui Paulo 		break;
191325151a3SRui Paulo 	case MB_STA_ROLE_NON_PCP_NON_AP:
192325151a3SRui Paulo 		if (mbie->mb_ctrl & MB_CTRL_STA_MAC_PRESENT &&
193325151a3SRui Paulo 		    (size_t) 2 + mbie->len >= sizeof(*mbie) + ETH_ALEN)
194325151a3SRui Paulo 			peer_addr = (const u8 *) &mbie[1];
195325151a3SRui Paulo 		break;
196325151a3SRui Paulo 	default:
197325151a3SRui Paulo 		break;
198325151a3SRui Paulo 	}
199325151a3SRui Paulo 
200325151a3SRui Paulo 	return peer_addr;
201325151a3SRui Paulo }
202325151a3SRui Paulo 
203325151a3SRui Paulo 
fst_mbie_get_peer_addr_for_band(const struct wpabuf * mbies,u8 band_id)204780fb4a2SCy Schubert static const u8 * fst_mbie_get_peer_addr_for_band(const struct wpabuf *mbies,
205780fb4a2SCy Schubert 						  u8 band_id)
206325151a3SRui Paulo {
207780fb4a2SCy Schubert 	const u8 *p = wpabuf_head(mbies);
208780fb4a2SCy Schubert 	size_t s = wpabuf_len(mbies);
209780fb4a2SCy Schubert 
210780fb4a2SCy Schubert 	while (s >= 2) {
211325151a3SRui Paulo 		const struct multi_band_ie *mbie =
212780fb4a2SCy Schubert 			(const struct multi_band_ie *) p;
213325151a3SRui Paulo 
214780fb4a2SCy Schubert 		if (mbie->eid != WLAN_EID_MULTI_BAND) {
215780fb4a2SCy Schubert 			fst_printf(MSG_INFO, "unexpected eid %d", mbie->eid);
216780fb4a2SCy Schubert 			return NULL;
217325151a3SRui Paulo 		}
218325151a3SRui Paulo 
219780fb4a2SCy Schubert 		if (mbie->len < sizeof(*mbie) - 2 || mbie->len > s - 2) {
220780fb4a2SCy Schubert 			fst_printf(MSG_INFO, "invalid mbie len %d",
221780fb4a2SCy Schubert 				   mbie->len);
222780fb4a2SCy Schubert 			return NULL;
223325151a3SRui Paulo 		}
224325151a3SRui Paulo 
225780fb4a2SCy Schubert 		if (mbie->band_id == band_id)
226780fb4a2SCy Schubert 			return fst_mbie_get_peer_addr(mbie);
227780fb4a2SCy Schubert 
228780fb4a2SCy Schubert 		p += 2 + mbie->len;
229780fb4a2SCy Schubert 		s -= 2 + mbie->len;
230780fb4a2SCy Schubert 	}
231780fb4a2SCy Schubert 
232780fb4a2SCy Schubert 	fst_printf(MSG_INFO, "mbie doesn't contain band %d", band_id);
233325151a3SRui Paulo 	return NULL;
234325151a3SRui Paulo }
235325151a3SRui Paulo 
236325151a3SRui Paulo 
fst_group_get_iface_by_name(struct fst_group * g,const char * ifname)237325151a3SRui Paulo struct fst_iface * fst_group_get_iface_by_name(struct fst_group *g,
238325151a3SRui Paulo 					       const char *ifname)
239325151a3SRui Paulo {
240325151a3SRui Paulo 	struct fst_iface *f;
241325151a3SRui Paulo 
242325151a3SRui Paulo 	foreach_fst_group_iface(g, f) {
243325151a3SRui Paulo 		const char *in = fst_iface_get_name(f);
244325151a3SRui Paulo 
245325151a3SRui Paulo 		if (os_strncmp(in, ifname, os_strlen(in)) == 0)
246325151a3SRui Paulo 			return f;
247325151a3SRui Paulo 	}
248325151a3SRui Paulo 
249325151a3SRui Paulo 	return NULL;
250325151a3SRui Paulo }
251325151a3SRui Paulo 
252325151a3SRui Paulo 
fst_group_assign_dialog_token(struct fst_group * g)253325151a3SRui Paulo u8 fst_group_assign_dialog_token(struct fst_group *g)
254325151a3SRui Paulo {
255325151a3SRui Paulo 	g->dialog_token++;
256325151a3SRui Paulo 	if (g->dialog_token == 0)
257325151a3SRui Paulo 		g->dialog_token++;
258325151a3SRui Paulo 	return g->dialog_token;
259325151a3SRui Paulo }
260325151a3SRui Paulo 
261325151a3SRui Paulo 
fst_group_assign_fsts_id(struct fst_group * g)262325151a3SRui Paulo u32 fst_group_assign_fsts_id(struct fst_group *g)
263325151a3SRui Paulo {
264325151a3SRui Paulo 	g->fsts_id++;
265325151a3SRui Paulo 	return g->fsts_id;
266325151a3SRui Paulo }
267325151a3SRui Paulo 
268325151a3SRui Paulo 
269780fb4a2SCy Schubert /**
270780fb4a2SCy Schubert  * fst_group_get_peer_other_connection_1 - Find peer's "other" connection
271780fb4a2SCy Schubert  * (iface, MAC tuple) by using peer's MB IE on iface.
272780fb4a2SCy Schubert  *
273780fb4a2SCy Schubert  * @iface: iface on which FST Setup Request was received
274780fb4a2SCy Schubert  * @peer_addr: Peer address on iface
275780fb4a2SCy Schubert  * @band_id: "other" connection band id
276780fb4a2SCy Schubert  * @other_peer_addr (out): Peer's MAC address on the "other" connection (on the
277780fb4a2SCy Schubert  *   "other" iface)
278780fb4a2SCy Schubert  *
279780fb4a2SCy Schubert  * This function parses peer's MB IE on iface. It looks for peer's MAC address
280780fb4a2SCy Schubert  * on band_id (tmp_peer_addr). Next all interfaces are iterated to find an
281780fb4a2SCy Schubert  * interface which correlates with band_id. If such interface is found, peer
282780fb4a2SCy Schubert  * database is iterated to see if tmp_peer_addr is connected over it.
283780fb4a2SCy Schubert  */
284780fb4a2SCy Schubert static struct fst_iface *
fst_group_get_peer_other_connection_1(struct fst_iface * iface,const u8 * peer_addr,u8 band_id,u8 * other_peer_addr)285780fb4a2SCy Schubert fst_group_get_peer_other_connection_1(struct fst_iface *iface,
286780fb4a2SCy Schubert 				      const u8 *peer_addr, u8 band_id,
287780fb4a2SCy Schubert 				      u8 *other_peer_addr)
288325151a3SRui Paulo {
289325151a3SRui Paulo 	const struct wpabuf *mbies;
290780fb4a2SCy Schubert 	struct fst_iface *other_iface;
291780fb4a2SCy Schubert 	const u8 *tmp_peer_addr;
292325151a3SRui Paulo 
293780fb4a2SCy Schubert 	/* Get peer's MB IEs on iface */
294780fb4a2SCy Schubert 	mbies = fst_iface_get_peer_mb_ie(iface, peer_addr);
295325151a3SRui Paulo 	if (!mbies)
296780fb4a2SCy Schubert 		return NULL;
297780fb4a2SCy Schubert 
298780fb4a2SCy Schubert 	/* Get peer's MAC address on the "other" interface */
299780fb4a2SCy Schubert 	tmp_peer_addr = fst_mbie_get_peer_addr_for_band(mbies, band_id);
300780fb4a2SCy Schubert 	if (!tmp_peer_addr) {
301780fb4a2SCy Schubert 		fst_printf(MSG_INFO,
302780fb4a2SCy Schubert 			   "couldn't extract other peer addr from mbies");
303780fb4a2SCy Schubert 		return NULL;
304780fb4a2SCy Schubert 	}
305780fb4a2SCy Schubert 
306780fb4a2SCy Schubert 	fst_printf(MSG_DEBUG, "found other peer addr from mbies: " MACSTR,
307780fb4a2SCy Schubert 		   MAC2STR(tmp_peer_addr));
308780fb4a2SCy Schubert 
309780fb4a2SCy Schubert 	foreach_fst_group_iface(fst_iface_get_group(iface), other_iface) {
310780fb4a2SCy Schubert 		if (other_iface == iface ||
311780fb4a2SCy Schubert 		    band_id != fst_iface_get_band_id(other_iface))
312325151a3SRui Paulo 			continue;
313c1d255d3SCy Schubert 		if (fst_iface_is_connected(other_iface, tmp_peer_addr, false)) {
314780fb4a2SCy Schubert 			os_memcpy(other_peer_addr, tmp_peer_addr, ETH_ALEN);
315780fb4a2SCy Schubert 			return other_iface;
316325151a3SRui Paulo 		}
317325151a3SRui Paulo 	}
318325151a3SRui Paulo 
319325151a3SRui Paulo 	return NULL;
320325151a3SRui Paulo }
321325151a3SRui Paulo 
322325151a3SRui Paulo 
323780fb4a2SCy Schubert /**
324780fb4a2SCy Schubert  * fst_group_get_peer_other_connection_2 - Find peer's "other" connection
325780fb4a2SCy Schubert  * (iface, MAC tuple) by using MB IEs of other peers.
326780fb4a2SCy Schubert  *
327780fb4a2SCy Schubert  * @iface: iface on which FST Setup Request was received
328780fb4a2SCy Schubert  * @peer_addr: Peer address on iface
329780fb4a2SCy Schubert  * @band_id: "other" connection band id
330780fb4a2SCy Schubert  * @other_peer_addr (out): Peer's MAC address on the "other" connection (on the
331780fb4a2SCy Schubert  *   "other" iface)
332780fb4a2SCy Schubert  *
333780fb4a2SCy Schubert  * This function iterates all connection (other_iface, cur_peer_addr tuples).
334780fb4a2SCy Schubert  * For each connection, MB IE (of cur_peer_addr on other_iface) is parsed and
335780fb4a2SCy Schubert  * MAC address on iface's band_id is extracted (this_peer_addr).
336780fb4a2SCy Schubert  * this_peer_addr is then compared to peer_addr. A match indicates we have
337780fb4a2SCy Schubert  * found the "other" connection.
338780fb4a2SCy Schubert  */
339780fb4a2SCy Schubert static struct fst_iface *
fst_group_get_peer_other_connection_2(struct fst_iface * iface,const u8 * peer_addr,u8 band_id,u8 * other_peer_addr)340780fb4a2SCy Schubert fst_group_get_peer_other_connection_2(struct fst_iface *iface,
341780fb4a2SCy Schubert 				      const u8 *peer_addr, u8 band_id,
342780fb4a2SCy Schubert 				      u8 *other_peer_addr)
343325151a3SRui Paulo {
344780fb4a2SCy Schubert 	u8 this_band_id = fst_iface_get_band_id(iface);
345780fb4a2SCy Schubert 	const u8 *cur_peer_addr, *this_peer_addr;
346780fb4a2SCy Schubert 	struct fst_get_peer_ctx *ctx;
347780fb4a2SCy Schubert 	struct fst_iface *other_iface;
348780fb4a2SCy Schubert 	const struct wpabuf *cur_mbie;
349780fb4a2SCy Schubert 
350780fb4a2SCy Schubert 	foreach_fst_group_iface(fst_iface_get_group(iface), other_iface) {
351780fb4a2SCy Schubert 		if (other_iface == iface ||
352780fb4a2SCy Schubert 		    band_id != fst_iface_get_band_id(other_iface))
353780fb4a2SCy Schubert 			continue;
354780fb4a2SCy Schubert 		cur_peer_addr = fst_iface_get_peer_first(other_iface, &ctx,
355c1d255d3SCy Schubert 							 true);
356780fb4a2SCy Schubert 		for (; cur_peer_addr;
357780fb4a2SCy Schubert 		     cur_peer_addr = fst_iface_get_peer_next(other_iface, &ctx,
358c1d255d3SCy Schubert 							     true)) {
359780fb4a2SCy Schubert 			cur_mbie = fst_iface_get_peer_mb_ie(other_iface,
360780fb4a2SCy Schubert 							    cur_peer_addr);
361780fb4a2SCy Schubert 			if (!cur_mbie)
362780fb4a2SCy Schubert 				continue;
363780fb4a2SCy Schubert 			this_peer_addr = fst_mbie_get_peer_addr_for_band(
364780fb4a2SCy Schubert 				cur_mbie, this_band_id);
365780fb4a2SCy Schubert 			if (!this_peer_addr)
366780fb4a2SCy Schubert 				continue;
367*a90b9d01SCy Schubert 			if (ether_addr_equal(this_peer_addr, peer_addr)) {
368780fb4a2SCy Schubert 				os_memcpy(other_peer_addr, cur_peer_addr,
369780fb4a2SCy Schubert 					  ETH_ALEN);
370780fb4a2SCy Schubert 				return other_iface;
371780fb4a2SCy Schubert 			}
372780fb4a2SCy Schubert 		}
373780fb4a2SCy Schubert 	}
374780fb4a2SCy Schubert 
375780fb4a2SCy Schubert 	return NULL;
376780fb4a2SCy Schubert }
377780fb4a2SCy Schubert 
378780fb4a2SCy Schubert 
379780fb4a2SCy Schubert /**
380780fb4a2SCy Schubert  * fst_group_get_peer_other_connection - Find peer's "other" connection (iface,
381780fb4a2SCy Schubert  * MAC tuple).
382780fb4a2SCy Schubert  *
383780fb4a2SCy Schubert  * @iface: iface on which FST Setup Request was received
384780fb4a2SCy Schubert  * @peer_addr: Peer address on iface
385780fb4a2SCy Schubert  * @band_id: "other" connection band id
386780fb4a2SCy Schubert  * @other_peer_addr (out): Peer's MAC address on the "other" connection (on the
387780fb4a2SCy Schubert  *   "other" iface)
388780fb4a2SCy Schubert  *
389780fb4a2SCy Schubert  * This function is called upon receiving FST Setup Request from some peer who
390780fb4a2SCy Schubert  * has peer_addr on iface. It searches for another connection of the same peer
391780fb4a2SCy Schubert  * on different interface which correlates with band_id. MB IEs received from
392780fb4a2SCy Schubert  * peer (on the two different interfaces) are used to identify same peer.
393780fb4a2SCy Schubert  */
394780fb4a2SCy Schubert struct fst_iface *
fst_group_get_peer_other_connection(struct fst_iface * iface,const u8 * peer_addr,u8 band_id,u8 * other_peer_addr)395780fb4a2SCy Schubert fst_group_get_peer_other_connection(struct fst_iface *iface,
396780fb4a2SCy Schubert 				    const u8 *peer_addr, u8 band_id,
397780fb4a2SCy Schubert 				    u8 *other_peer_addr)
398780fb4a2SCy Schubert {
399780fb4a2SCy Schubert 	struct fst_iface *other_iface;
400780fb4a2SCy Schubert 
401780fb4a2SCy Schubert 	fst_printf(MSG_DEBUG, "%s: %s:" MACSTR ", %d", __func__,
402780fb4a2SCy Schubert 		   fst_iface_get_name(iface), MAC2STR(peer_addr), band_id);
403780fb4a2SCy Schubert 
404780fb4a2SCy Schubert 	/*
405780fb4a2SCy Schubert 	 * Two search methods are used:
406780fb4a2SCy Schubert 	 * 1. Use peer's MB IE on iface to extract peer's MAC address on
407780fb4a2SCy Schubert 	 *    "other" connection. Then check if such "other" connection exists.
408780fb4a2SCy Schubert 	 * 2. Iterate peer database, examine each MB IE to see if it points to
409780fb4a2SCy Schubert 	 *    (iface, peer_addr) tuple
410780fb4a2SCy Schubert 	 */
411780fb4a2SCy Schubert 
412780fb4a2SCy Schubert 	other_iface = fst_group_get_peer_other_connection_1(iface, peer_addr,
413780fb4a2SCy Schubert 							    band_id,
414780fb4a2SCy Schubert 							    other_peer_addr);
415780fb4a2SCy Schubert 	if (other_iface) {
416780fb4a2SCy Schubert 		fst_printf(MSG_DEBUG, "found by method #1. %s:" MACSTR,
417780fb4a2SCy Schubert 			   fst_iface_get_name(other_iface),
418780fb4a2SCy Schubert 			   MAC2STR(other_peer_addr));
419780fb4a2SCy Schubert 		return other_iface;
420780fb4a2SCy Schubert 	}
421780fb4a2SCy Schubert 
422780fb4a2SCy Schubert 	other_iface = fst_group_get_peer_other_connection_2(iface, peer_addr,
423780fb4a2SCy Schubert 							    band_id,
424780fb4a2SCy Schubert 							    other_peer_addr);
425780fb4a2SCy Schubert 	if (other_iface) {
426780fb4a2SCy Schubert 		fst_printf(MSG_DEBUG, "found by method #2. %s:" MACSTR,
427780fb4a2SCy Schubert 			   fst_iface_get_name(other_iface),
428780fb4a2SCy Schubert 			   MAC2STR(other_peer_addr));
429780fb4a2SCy Schubert 		return other_iface;
430780fb4a2SCy Schubert 	}
431780fb4a2SCy Schubert 
432780fb4a2SCy Schubert 	fst_printf(MSG_INFO, "%s: other connection not found", __func__);
433780fb4a2SCy Schubert 	return NULL;
434325151a3SRui Paulo }
435325151a3SRui Paulo 
436325151a3SRui Paulo 
fst_group_create(const char * group_id)437325151a3SRui Paulo struct fst_group * fst_group_create(const char *group_id)
438325151a3SRui Paulo {
439325151a3SRui Paulo 	struct fst_group *g;
440325151a3SRui Paulo 
441325151a3SRui Paulo 	g = os_zalloc(sizeof(*g));
442325151a3SRui Paulo 	if (g == NULL) {
443325151a3SRui Paulo 		fst_printf(MSG_ERROR, "%s: Cannot alloc group", group_id);
444325151a3SRui Paulo 		return NULL;
445325151a3SRui Paulo 	}
446325151a3SRui Paulo 
447325151a3SRui Paulo 	dl_list_init(&g->ifaces);
448325151a3SRui Paulo 	os_strlcpy(g->group_id, group_id, sizeof(g->group_id));
449325151a3SRui Paulo 
450325151a3SRui Paulo 	dl_list_add_tail(&fst_global_groups_list, &g->global_groups_lentry);
451325151a3SRui Paulo 	fst_printf_group(g, MSG_DEBUG, "instance created");
452325151a3SRui Paulo 
453325151a3SRui Paulo 	foreach_fst_ctrl_call(on_group_created, g);
454325151a3SRui Paulo 
455325151a3SRui Paulo 	return g;
456325151a3SRui Paulo }
457325151a3SRui Paulo 
458325151a3SRui Paulo 
fst_group_attach_iface(struct fst_group * g,struct fst_iface * i)459325151a3SRui Paulo void fst_group_attach_iface(struct fst_group *g, struct fst_iface *i)
460325151a3SRui Paulo {
461325151a3SRui Paulo 	struct dl_list *list = &g->ifaces;
462325151a3SRui Paulo 	struct fst_iface *f;
463325151a3SRui Paulo 
464325151a3SRui Paulo 	/*
465325151a3SRui Paulo 	 * Add new interface to the list.
466325151a3SRui Paulo 	 * The list is sorted in descending order by priority to allow
467325151a3SRui Paulo 	 * multiple MB IEs creation according to the spec (see 10.32 Multi-band
468325151a3SRui Paulo 	 * operation, 10.32.1 General), as they should be ordered according to
469325151a3SRui Paulo 	 * priorities.
470325151a3SRui Paulo 	 */
471325151a3SRui Paulo 	foreach_fst_group_iface(g, f) {
472325151a3SRui Paulo 		if (fst_iface_get_priority(f) < fst_iface_get_priority(i))
473325151a3SRui Paulo 			break;
474325151a3SRui Paulo 		list = &f->group_lentry;
475325151a3SRui Paulo 	}
476325151a3SRui Paulo 	dl_list_add(list, &i->group_lentry);
477325151a3SRui Paulo }
478325151a3SRui Paulo 
479325151a3SRui Paulo 
fst_group_detach_iface(struct fst_group * g,struct fst_iface * i)480325151a3SRui Paulo void fst_group_detach_iface(struct fst_group *g, struct fst_iface *i)
481325151a3SRui Paulo {
482325151a3SRui Paulo 	dl_list_del(&i->group_lentry);
483325151a3SRui Paulo }
484325151a3SRui Paulo 
485325151a3SRui Paulo 
fst_group_delete(struct fst_group * group)486325151a3SRui Paulo void fst_group_delete(struct fst_group *group)
487325151a3SRui Paulo {
488325151a3SRui Paulo 	struct fst_session *s;
489325151a3SRui Paulo 
490325151a3SRui Paulo 	dl_list_del(&group->global_groups_lentry);
491325151a3SRui Paulo 	WPA_ASSERT(dl_list_empty(&group->ifaces));
492325151a3SRui Paulo 	foreach_fst_ctrl_call(on_group_deleted, group);
493325151a3SRui Paulo 	fst_printf_group(group, MSG_DEBUG, "instance deleted");
494325151a3SRui Paulo 	while ((s = fst_session_global_get_first_by_group(group)) != NULL)
495325151a3SRui Paulo 		fst_session_delete(s);
496325151a3SRui Paulo 	os_free(group);
497325151a3SRui Paulo }
498325151a3SRui Paulo 
499325151a3SRui Paulo 
fst_group_delete_if_empty(struct fst_group * group)500c1d255d3SCy Schubert bool fst_group_delete_if_empty(struct fst_group *group)
501325151a3SRui Paulo {
502c1d255d3SCy Schubert 	bool is_empty = !fst_group_has_ifaces(group) &&
503325151a3SRui Paulo 		!fst_session_global_get_first_by_group(group);
504325151a3SRui Paulo 
505325151a3SRui Paulo 	if (is_empty)
506325151a3SRui Paulo 		fst_group_delete(group);
507325151a3SRui Paulo 
508325151a3SRui Paulo 	return is_empty;
509325151a3SRui Paulo }
510325151a3SRui Paulo 
511325151a3SRui Paulo 
fst_group_update_ie(struct fst_group * g)512325151a3SRui Paulo void fst_group_update_ie(struct fst_group *g)
513325151a3SRui Paulo {
514325151a3SRui Paulo 	struct fst_iface *i;
515325151a3SRui Paulo 
516325151a3SRui Paulo 	foreach_fst_group_iface(g, i) {
517325151a3SRui Paulo 		struct wpabuf *mbie = fst_group_create_mb_ie(g, i);
518325151a3SRui Paulo 
519325151a3SRui Paulo 		if (!mbie)
520325151a3SRui Paulo 			fst_printf_iface(i, MSG_WARNING, "cannot create MB IE");
521325151a3SRui Paulo 
522325151a3SRui Paulo 		fst_iface_attach_mbie(i, mbie);
523325151a3SRui Paulo 		fst_iface_set_ies(i, mbie);
524325151a3SRui Paulo 		fst_printf_iface(i, MSG_DEBUG, "multi-band IE set to %p", mbie);
525325151a3SRui Paulo 	}
526325151a3SRui Paulo }
527