xref: /dflybsd-src/contrib/wpa_supplicant/src/fst/fst_group.c (revision 3a84a4273475ed07d0ab1c2dfeffdfedef35d9cd)
1*a1157835SDaniel Fojt /*
2*a1157835SDaniel Fojt  * FST module - FST group object implementation
3*a1157835SDaniel Fojt  * Copyright (c) 2014, Qualcomm Atheros, Inc.
4*a1157835SDaniel Fojt  *
5*a1157835SDaniel Fojt  * This software may be distributed under the terms of the BSD license.
6*a1157835SDaniel Fojt  * See README for more details.
7*a1157835SDaniel Fojt  */
8*a1157835SDaniel Fojt 
9*a1157835SDaniel Fojt #include "utils/includes.h"
10*a1157835SDaniel Fojt #include "utils/common.h"
11*a1157835SDaniel Fojt #include "common/defs.h"
12*a1157835SDaniel Fojt #include "common/ieee802_11_defs.h"
13*a1157835SDaniel Fojt #include "common/ieee802_11_common.h"
14*a1157835SDaniel Fojt #include "drivers/driver.h"
15*a1157835SDaniel Fojt #include "fst/fst_internal.h"
16*a1157835SDaniel Fojt #include "fst/fst_defs.h"
17*a1157835SDaniel Fojt 
18*a1157835SDaniel Fojt 
19*a1157835SDaniel Fojt struct dl_list fst_global_groups_list;
20*a1157835SDaniel Fojt 
21*a1157835SDaniel Fojt 
fst_dump_mb_ies(const char * group_id,const char * ifname,struct wpabuf * mbies)22*a1157835SDaniel Fojt static void fst_dump_mb_ies(const char *group_id, const char *ifname,
23*a1157835SDaniel Fojt 			    struct wpabuf *mbies)
24*a1157835SDaniel Fojt {
25*a1157835SDaniel Fojt 	const u8 *p = wpabuf_head(mbies);
26*a1157835SDaniel Fojt 	size_t s = wpabuf_len(mbies);
27*a1157835SDaniel Fojt 
28*a1157835SDaniel Fojt 	while (s >= 2) {
29*a1157835SDaniel Fojt 		const struct multi_band_ie *mbie =
30*a1157835SDaniel Fojt 			(const struct multi_band_ie *) p;
31*a1157835SDaniel Fojt 		WPA_ASSERT(mbie->eid == WLAN_EID_MULTI_BAND);
32*a1157835SDaniel Fojt 		WPA_ASSERT(2U + mbie->len >= sizeof(*mbie));
33*a1157835SDaniel Fojt 
34*a1157835SDaniel Fojt 		fst_printf(MSG_WARNING,
35*a1157835SDaniel Fojt 			   "%s: %s: mb_ctrl=%u band_id=%u op_class=%u chan=%u bssid="
36*a1157835SDaniel Fojt 			   MACSTR
37*a1157835SDaniel Fojt 			   " beacon_int=%u tsf_offs=[%u %u %u %u %u %u %u %u] mb_cc=0x%02x tmout=%u",
38*a1157835SDaniel Fojt 			   group_id, ifname,
39*a1157835SDaniel Fojt 			   mbie->mb_ctrl, mbie->band_id, mbie->op_class,
40*a1157835SDaniel Fojt 			   mbie->chan, MAC2STR(mbie->bssid), mbie->beacon_int,
41*a1157835SDaniel Fojt 			   mbie->tsf_offs[0], mbie->tsf_offs[1],
42*a1157835SDaniel Fojt 			   mbie->tsf_offs[2], mbie->tsf_offs[3],
43*a1157835SDaniel Fojt 			   mbie->tsf_offs[4], mbie->tsf_offs[5],
44*a1157835SDaniel Fojt 			   mbie->tsf_offs[6], mbie->tsf_offs[7],
45*a1157835SDaniel Fojt 			   mbie->mb_connection_capability,
46*a1157835SDaniel Fojt 			   mbie->fst_session_tmout);
47*a1157835SDaniel Fojt 
48*a1157835SDaniel Fojt 		p += 2 + mbie->len;
49*a1157835SDaniel Fojt 		s -= 2 + mbie->len;
50*a1157835SDaniel Fojt 	}
51*a1157835SDaniel Fojt }
52*a1157835SDaniel Fojt 
53*a1157835SDaniel Fojt 
fst_fill_mb_ie(struct wpabuf * buf,const u8 * bssid,const u8 * own_addr,enum mb_band_id band,u8 channel)54*a1157835SDaniel Fojt static void fst_fill_mb_ie(struct wpabuf *buf, const u8 *bssid,
55*a1157835SDaniel Fojt 			   const u8 *own_addr, enum mb_band_id band, u8 channel)
56*a1157835SDaniel Fojt {
57*a1157835SDaniel Fojt 	struct multi_band_ie *mbie;
58*a1157835SDaniel Fojt 	size_t len = sizeof(*mbie);
59*a1157835SDaniel Fojt 
60*a1157835SDaniel Fojt 	if (own_addr)
61*a1157835SDaniel Fojt 		len += ETH_ALEN;
62*a1157835SDaniel Fojt 
63*a1157835SDaniel Fojt 	mbie = wpabuf_put(buf, len);
64*a1157835SDaniel Fojt 
65*a1157835SDaniel Fojt 	os_memset(mbie, 0, len);
66*a1157835SDaniel Fojt 
67*a1157835SDaniel Fojt 	mbie->eid = WLAN_EID_MULTI_BAND;
68*a1157835SDaniel Fojt 	mbie->len = len - 2;
69*a1157835SDaniel Fojt #ifdef HOSTAPD
70*a1157835SDaniel Fojt 	mbie->mb_ctrl = MB_STA_ROLE_AP;
71*a1157835SDaniel Fojt 	mbie->mb_connection_capability = MB_CONNECTION_CAPABILITY_AP;
72*a1157835SDaniel Fojt #else /* HOSTAPD */
73*a1157835SDaniel Fojt 	mbie->mb_ctrl = MB_STA_ROLE_NON_PCP_NON_AP;
74*a1157835SDaniel Fojt 	mbie->mb_connection_capability = 0;
75*a1157835SDaniel Fojt #endif /* HOSTAPD */
76*a1157835SDaniel Fojt 	if (bssid)
77*a1157835SDaniel Fojt 		os_memcpy(mbie->bssid, bssid, ETH_ALEN);
78*a1157835SDaniel Fojt 	mbie->band_id = band;
79*a1157835SDaniel Fojt 	mbie->op_class = 0;  /* means all */
80*a1157835SDaniel Fojt 	mbie->chan = channel;
81*a1157835SDaniel Fojt 	mbie->fst_session_tmout = FST_DEFAULT_SESSION_TIMEOUT_TU;
82*a1157835SDaniel Fojt 
83*a1157835SDaniel Fojt 	if (own_addr) {
84*a1157835SDaniel Fojt 		mbie->mb_ctrl |= MB_CTRL_STA_MAC_PRESENT;
85*a1157835SDaniel Fojt 		os_memcpy(&mbie[1], own_addr, ETH_ALEN);
86*a1157835SDaniel Fojt 	}
87*a1157835SDaniel Fojt }
88*a1157835SDaniel Fojt 
89*a1157835SDaniel Fojt 
fst_fill_iface_mb_ies(struct fst_iface * f,struct wpabuf * buf)90*a1157835SDaniel Fojt static unsigned fst_fill_iface_mb_ies(struct fst_iface *f, struct wpabuf *buf)
91*a1157835SDaniel Fojt {
92*a1157835SDaniel Fojt 	const  u8 *bssid;
93*a1157835SDaniel Fojt 
94*a1157835SDaniel Fojt 	bssid = fst_iface_get_bssid(f);
95*a1157835SDaniel Fojt 	if (bssid) {
96*a1157835SDaniel Fojt 		enum hostapd_hw_mode hw_mode;
97*a1157835SDaniel Fojt 		u8 channel;
98*a1157835SDaniel Fojt 
99*a1157835SDaniel Fojt 		if (buf) {
100*a1157835SDaniel Fojt 			fst_iface_get_channel_info(f, &hw_mode, &channel);
101*a1157835SDaniel Fojt 			fst_fill_mb_ie(buf, bssid, fst_iface_get_addr(f),
102*a1157835SDaniel Fojt 				       fst_hw_mode_to_band(hw_mode), channel);
103*a1157835SDaniel Fojt 		}
104*a1157835SDaniel Fojt 		return 1;
105*a1157835SDaniel Fojt 	} else {
106*a1157835SDaniel Fojt 		unsigned bands[MB_BAND_ID_WIFI_60GHZ + 1] = {};
107*a1157835SDaniel Fojt 		struct hostapd_hw_modes *modes;
108*a1157835SDaniel Fojt 		enum mb_band_id b;
109*a1157835SDaniel Fojt 		int num_modes = fst_iface_get_hw_modes(f, &modes);
110*a1157835SDaniel Fojt 		int ret = 0;
111*a1157835SDaniel Fojt 
112*a1157835SDaniel Fojt 		while (num_modes--) {
113*a1157835SDaniel Fojt 			b = fst_hw_mode_to_band(modes->mode);
114*a1157835SDaniel Fojt 			modes++;
115*a1157835SDaniel Fojt 			if (b >= ARRAY_SIZE(bands) || bands[b]++)
116*a1157835SDaniel Fojt 				continue;
117*a1157835SDaniel Fojt 			ret++;
118*a1157835SDaniel Fojt 			if (buf)
119*a1157835SDaniel Fojt 				fst_fill_mb_ie(buf, NULL, fst_iface_get_addr(f),
120*a1157835SDaniel Fojt 					       b, MB_STA_CHANNEL_ALL);
121*a1157835SDaniel Fojt 		}
122*a1157835SDaniel Fojt 		return ret;
123*a1157835SDaniel Fojt 	}
124*a1157835SDaniel Fojt }
125*a1157835SDaniel Fojt 
126*a1157835SDaniel Fojt 
fst_group_create_mb_ie(struct fst_group * g,struct fst_iface * i)127*a1157835SDaniel Fojt static struct wpabuf * fst_group_create_mb_ie(struct fst_group *g,
128*a1157835SDaniel Fojt 					      struct fst_iface *i)
129*a1157835SDaniel Fojt {
130*a1157835SDaniel Fojt 	struct wpabuf *buf;
131*a1157835SDaniel Fojt 	struct fst_iface *f;
132*a1157835SDaniel Fojt 	unsigned int nof_mbies = 0;
133*a1157835SDaniel Fojt 	unsigned int nof_ifaces_added = 0;
134*a1157835SDaniel Fojt 
135*a1157835SDaniel Fojt 	foreach_fst_group_iface(g, f) {
136*a1157835SDaniel Fojt 		if (f == i)
137*a1157835SDaniel Fojt 			continue;
138*a1157835SDaniel Fojt 		nof_mbies += fst_fill_iface_mb_ies(f, NULL);
139*a1157835SDaniel Fojt 	}
140*a1157835SDaniel Fojt 
141*a1157835SDaniel Fojt 	buf = wpabuf_alloc(nof_mbies *
142*a1157835SDaniel Fojt 			   (sizeof(struct multi_band_ie) + ETH_ALEN));
143*a1157835SDaniel Fojt 	if (!buf) {
144*a1157835SDaniel Fojt 		fst_printf_iface(i, MSG_ERROR,
145*a1157835SDaniel Fojt 				 "cannot allocate mem for %u MB IEs",
146*a1157835SDaniel Fojt 				 nof_mbies);
147*a1157835SDaniel Fojt 		return NULL;
148*a1157835SDaniel Fojt 	}
149*a1157835SDaniel Fojt 
150*a1157835SDaniel Fojt 	/* The list is sorted in descending order by priorities, so MB IEs will
151*a1157835SDaniel Fojt 	 * be arranged in the same order, as required by spec (see corresponding
152*a1157835SDaniel Fojt 	 * comment in.fst_attach().
153*a1157835SDaniel Fojt 	 */
154*a1157835SDaniel Fojt 	foreach_fst_group_iface(g, f) {
155*a1157835SDaniel Fojt 		if (f == i)
156*a1157835SDaniel Fojt 			continue;
157*a1157835SDaniel Fojt 
158*a1157835SDaniel Fojt 		fst_fill_iface_mb_ies(f, buf);
159*a1157835SDaniel Fojt 		++nof_ifaces_added;
160*a1157835SDaniel Fojt 
161*a1157835SDaniel Fojt 		fst_printf_iface(i, MSG_DEBUG, "added to MB IE");
162*a1157835SDaniel Fojt 	}
163*a1157835SDaniel Fojt 
164*a1157835SDaniel Fojt 	if (!nof_ifaces_added) {
165*a1157835SDaniel Fojt 		wpabuf_free(buf);
166*a1157835SDaniel Fojt 		buf = NULL;
167*a1157835SDaniel Fojt 		fst_printf_iface(i, MSG_INFO,
168*a1157835SDaniel Fojt 				 "cannot add MB IE: no backup ifaces");
169*a1157835SDaniel Fojt 	} else {
170*a1157835SDaniel Fojt 		fst_dump_mb_ies(fst_group_get_id(g), fst_iface_get_name(i),
171*a1157835SDaniel Fojt 				buf);
172*a1157835SDaniel Fojt 	}
173*a1157835SDaniel Fojt 
174*a1157835SDaniel Fojt 	return buf;
175*a1157835SDaniel Fojt }
176*a1157835SDaniel Fojt 
177*a1157835SDaniel Fojt 
fst_mbie_get_peer_addr(const struct multi_band_ie * mbie)178*a1157835SDaniel Fojt static const u8 * fst_mbie_get_peer_addr(const struct multi_band_ie *mbie)
179*a1157835SDaniel Fojt {
180*a1157835SDaniel Fojt 	const u8 *peer_addr = NULL;
181*a1157835SDaniel Fojt 
182*a1157835SDaniel Fojt 	switch (MB_CTRL_ROLE(mbie->mb_ctrl)) {
183*a1157835SDaniel Fojt 	case MB_STA_ROLE_AP:
184*a1157835SDaniel Fojt 		peer_addr = mbie->bssid;
185*a1157835SDaniel Fojt 		break;
186*a1157835SDaniel Fojt 	case MB_STA_ROLE_NON_PCP_NON_AP:
187*a1157835SDaniel Fojt 		if (mbie->mb_ctrl & MB_CTRL_STA_MAC_PRESENT &&
188*a1157835SDaniel Fojt 		    (size_t) 2 + mbie->len >= sizeof(*mbie) + ETH_ALEN)
189*a1157835SDaniel Fojt 			peer_addr = (const u8 *) &mbie[1];
190*a1157835SDaniel Fojt 		break;
191*a1157835SDaniel Fojt 	default:
192*a1157835SDaniel Fojt 		break;
193*a1157835SDaniel Fojt 	}
194*a1157835SDaniel Fojt 
195*a1157835SDaniel Fojt 	return peer_addr;
196*a1157835SDaniel Fojt }
197*a1157835SDaniel Fojt 
198*a1157835SDaniel Fojt 
fst_mbie_get_peer_addr_for_band(const struct wpabuf * mbies,u8 band_id)199*a1157835SDaniel Fojt static const u8 * fst_mbie_get_peer_addr_for_band(const struct wpabuf *mbies,
200*a1157835SDaniel Fojt 						  u8 band_id)
201*a1157835SDaniel Fojt {
202*a1157835SDaniel Fojt 	const u8 *p = wpabuf_head(mbies);
203*a1157835SDaniel Fojt 	size_t s = wpabuf_len(mbies);
204*a1157835SDaniel Fojt 
205*a1157835SDaniel Fojt 	while (s >= 2) {
206*a1157835SDaniel Fojt 		const struct multi_band_ie *mbie =
207*a1157835SDaniel Fojt 			(const struct multi_band_ie *) p;
208*a1157835SDaniel Fojt 
209*a1157835SDaniel Fojt 		if (mbie->eid != WLAN_EID_MULTI_BAND) {
210*a1157835SDaniel Fojt 			fst_printf(MSG_INFO, "unexpected eid %d", mbie->eid);
211*a1157835SDaniel Fojt 			return NULL;
212*a1157835SDaniel Fojt 		}
213*a1157835SDaniel Fojt 
214*a1157835SDaniel Fojt 		if (mbie->len < sizeof(*mbie) - 2 || mbie->len > s - 2) {
215*a1157835SDaniel Fojt 			fst_printf(MSG_INFO, "invalid mbie len %d",
216*a1157835SDaniel Fojt 				   mbie->len);
217*a1157835SDaniel Fojt 			return NULL;
218*a1157835SDaniel Fojt 		}
219*a1157835SDaniel Fojt 
220*a1157835SDaniel Fojt 		if (mbie->band_id == band_id)
221*a1157835SDaniel Fojt 			return fst_mbie_get_peer_addr(mbie);
222*a1157835SDaniel Fojt 
223*a1157835SDaniel Fojt 		p += 2 + mbie->len;
224*a1157835SDaniel Fojt 		s -= 2 + mbie->len;
225*a1157835SDaniel Fojt 	}
226*a1157835SDaniel Fojt 
227*a1157835SDaniel Fojt 	fst_printf(MSG_INFO, "mbie doesn't contain band %d", band_id);
228*a1157835SDaniel Fojt 	return NULL;
229*a1157835SDaniel Fojt }
230*a1157835SDaniel Fojt 
231*a1157835SDaniel Fojt 
fst_group_get_iface_by_name(struct fst_group * g,const char * ifname)232*a1157835SDaniel Fojt struct fst_iface * fst_group_get_iface_by_name(struct fst_group *g,
233*a1157835SDaniel Fojt 					       const char *ifname)
234*a1157835SDaniel Fojt {
235*a1157835SDaniel Fojt 	struct fst_iface *f;
236*a1157835SDaniel Fojt 
237*a1157835SDaniel Fojt 	foreach_fst_group_iface(g, f) {
238*a1157835SDaniel Fojt 		const char *in = fst_iface_get_name(f);
239*a1157835SDaniel Fojt 
240*a1157835SDaniel Fojt 		if (os_strncmp(in, ifname, os_strlen(in)) == 0)
241*a1157835SDaniel Fojt 			return f;
242*a1157835SDaniel Fojt 	}
243*a1157835SDaniel Fojt 
244*a1157835SDaniel Fojt 	return NULL;
245*a1157835SDaniel Fojt }
246*a1157835SDaniel Fojt 
247*a1157835SDaniel Fojt 
fst_group_assign_dialog_token(struct fst_group * g)248*a1157835SDaniel Fojt u8 fst_group_assign_dialog_token(struct fst_group *g)
249*a1157835SDaniel Fojt {
250*a1157835SDaniel Fojt 	g->dialog_token++;
251*a1157835SDaniel Fojt 	if (g->dialog_token == 0)
252*a1157835SDaniel Fojt 		g->dialog_token++;
253*a1157835SDaniel Fojt 	return g->dialog_token;
254*a1157835SDaniel Fojt }
255*a1157835SDaniel Fojt 
256*a1157835SDaniel Fojt 
fst_group_assign_fsts_id(struct fst_group * g)257*a1157835SDaniel Fojt u32 fst_group_assign_fsts_id(struct fst_group *g)
258*a1157835SDaniel Fojt {
259*a1157835SDaniel Fojt 	g->fsts_id++;
260*a1157835SDaniel Fojt 	return g->fsts_id;
261*a1157835SDaniel Fojt }
262*a1157835SDaniel Fojt 
263*a1157835SDaniel Fojt 
264*a1157835SDaniel Fojt /**
265*a1157835SDaniel Fojt  * fst_group_get_peer_other_connection_1 - Find peer's "other" connection
266*a1157835SDaniel Fojt  * (iface, MAC tuple) by using peer's MB IE on iface.
267*a1157835SDaniel Fojt  *
268*a1157835SDaniel Fojt  * @iface: iface on which FST Setup Request was received
269*a1157835SDaniel Fojt  * @peer_addr: Peer address on iface
270*a1157835SDaniel Fojt  * @band_id: "other" connection band id
271*a1157835SDaniel Fojt  * @other_peer_addr (out): Peer's MAC address on the "other" connection (on the
272*a1157835SDaniel Fojt  *   "other" iface)
273*a1157835SDaniel Fojt  *
274*a1157835SDaniel Fojt  * This function parses peer's MB IE on iface. It looks for peer's MAC address
275*a1157835SDaniel Fojt  * on band_id (tmp_peer_addr). Next all interfaces are iterated to find an
276*a1157835SDaniel Fojt  * interface which correlates with band_id. If such interface is found, peer
277*a1157835SDaniel Fojt  * database is iterated to see if tmp_peer_addr is connected over it.
278*a1157835SDaniel Fojt  */
279*a1157835SDaniel Fojt 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)280*a1157835SDaniel Fojt fst_group_get_peer_other_connection_1(struct fst_iface *iface,
281*a1157835SDaniel Fojt 				      const u8 *peer_addr, u8 band_id,
282*a1157835SDaniel Fojt 				      u8 *other_peer_addr)
283*a1157835SDaniel Fojt {
284*a1157835SDaniel Fojt 	const struct wpabuf *mbies;
285*a1157835SDaniel Fojt 	struct fst_iface *other_iface;
286*a1157835SDaniel Fojt 	const u8 *tmp_peer_addr;
287*a1157835SDaniel Fojt 
288*a1157835SDaniel Fojt 	/* Get peer's MB IEs on iface */
289*a1157835SDaniel Fojt 	mbies = fst_iface_get_peer_mb_ie(iface, peer_addr);
290*a1157835SDaniel Fojt 	if (!mbies)
291*a1157835SDaniel Fojt 		return NULL;
292*a1157835SDaniel Fojt 
293*a1157835SDaniel Fojt 	/* Get peer's MAC address on the "other" interface */
294*a1157835SDaniel Fojt 	tmp_peer_addr = fst_mbie_get_peer_addr_for_band(mbies, band_id);
295*a1157835SDaniel Fojt 	if (!tmp_peer_addr) {
296*a1157835SDaniel Fojt 		fst_printf(MSG_INFO,
297*a1157835SDaniel Fojt 			   "couldn't extract other peer addr from mbies");
298*a1157835SDaniel Fojt 		return NULL;
299*a1157835SDaniel Fojt 	}
300*a1157835SDaniel Fojt 
301*a1157835SDaniel Fojt 	fst_printf(MSG_DEBUG, "found other peer addr from mbies: " MACSTR,
302*a1157835SDaniel Fojt 		   MAC2STR(tmp_peer_addr));
303*a1157835SDaniel Fojt 
304*a1157835SDaniel Fojt 	foreach_fst_group_iface(fst_iface_get_group(iface), other_iface) {
305*a1157835SDaniel Fojt 		if (other_iface == iface ||
306*a1157835SDaniel Fojt 		    band_id != fst_iface_get_band_id(other_iface))
307*a1157835SDaniel Fojt 			continue;
308*a1157835SDaniel Fojt 		if (fst_iface_is_connected(other_iface, tmp_peer_addr, FALSE)) {
309*a1157835SDaniel Fojt 			os_memcpy(other_peer_addr, tmp_peer_addr, ETH_ALEN);
310*a1157835SDaniel Fojt 			return other_iface;
311*a1157835SDaniel Fojt 		}
312*a1157835SDaniel Fojt 	}
313*a1157835SDaniel Fojt 
314*a1157835SDaniel Fojt 	return NULL;
315*a1157835SDaniel Fojt }
316*a1157835SDaniel Fojt 
317*a1157835SDaniel Fojt 
318*a1157835SDaniel Fojt /**
319*a1157835SDaniel Fojt  * fst_group_get_peer_other_connection_2 - Find peer's "other" connection
320*a1157835SDaniel Fojt  * (iface, MAC tuple) by using MB IEs of other peers.
321*a1157835SDaniel Fojt  *
322*a1157835SDaniel Fojt  * @iface: iface on which FST Setup Request was received
323*a1157835SDaniel Fojt  * @peer_addr: Peer address on iface
324*a1157835SDaniel Fojt  * @band_id: "other" connection band id
325*a1157835SDaniel Fojt  * @other_peer_addr (out): Peer's MAC address on the "other" connection (on the
326*a1157835SDaniel Fojt  *   "other" iface)
327*a1157835SDaniel Fojt  *
328*a1157835SDaniel Fojt  * This function iterates all connection (other_iface, cur_peer_addr tuples).
329*a1157835SDaniel Fojt  * For each connection, MB IE (of cur_peer_addr on other_iface) is parsed and
330*a1157835SDaniel Fojt  * MAC address on iface's band_id is extracted (this_peer_addr).
331*a1157835SDaniel Fojt  * this_peer_addr is then compared to peer_addr. A match indicates we have
332*a1157835SDaniel Fojt  * found the "other" connection.
333*a1157835SDaniel Fojt  */
334*a1157835SDaniel Fojt 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)335*a1157835SDaniel Fojt fst_group_get_peer_other_connection_2(struct fst_iface *iface,
336*a1157835SDaniel Fojt 				      const u8 *peer_addr, u8 band_id,
337*a1157835SDaniel Fojt 				      u8 *other_peer_addr)
338*a1157835SDaniel Fojt {
339*a1157835SDaniel Fojt 	u8 this_band_id = fst_iface_get_band_id(iface);
340*a1157835SDaniel Fojt 	const u8 *cur_peer_addr, *this_peer_addr;
341*a1157835SDaniel Fojt 	struct fst_get_peer_ctx *ctx;
342*a1157835SDaniel Fojt 	struct fst_iface *other_iface;
343*a1157835SDaniel Fojt 	const struct wpabuf *cur_mbie;
344*a1157835SDaniel Fojt 
345*a1157835SDaniel Fojt 	foreach_fst_group_iface(fst_iface_get_group(iface), other_iface) {
346*a1157835SDaniel Fojt 		if (other_iface == iface ||
347*a1157835SDaniel Fojt 		    band_id != fst_iface_get_band_id(other_iface))
348*a1157835SDaniel Fojt 			continue;
349*a1157835SDaniel Fojt 		cur_peer_addr = fst_iface_get_peer_first(other_iface, &ctx,
350*a1157835SDaniel Fojt 							 TRUE);
351*a1157835SDaniel Fojt 		for (; cur_peer_addr;
352*a1157835SDaniel Fojt 		     cur_peer_addr = fst_iface_get_peer_next(other_iface, &ctx,
353*a1157835SDaniel Fojt 							     TRUE)) {
354*a1157835SDaniel Fojt 			cur_mbie = fst_iface_get_peer_mb_ie(other_iface,
355*a1157835SDaniel Fojt 							    cur_peer_addr);
356*a1157835SDaniel Fojt 			if (!cur_mbie)
357*a1157835SDaniel Fojt 				continue;
358*a1157835SDaniel Fojt 			this_peer_addr = fst_mbie_get_peer_addr_for_band(
359*a1157835SDaniel Fojt 				cur_mbie, this_band_id);
360*a1157835SDaniel Fojt 			if (!this_peer_addr)
361*a1157835SDaniel Fojt 				continue;
362*a1157835SDaniel Fojt 			if (os_memcmp(this_peer_addr, peer_addr, ETH_ALEN) ==
363*a1157835SDaniel Fojt 			    0) {
364*a1157835SDaniel Fojt 				os_memcpy(other_peer_addr, cur_peer_addr,
365*a1157835SDaniel Fojt 					  ETH_ALEN);
366*a1157835SDaniel Fojt 				return other_iface;
367*a1157835SDaniel Fojt 			}
368*a1157835SDaniel Fojt 		}
369*a1157835SDaniel Fojt 	}
370*a1157835SDaniel Fojt 
371*a1157835SDaniel Fojt 	return NULL;
372*a1157835SDaniel Fojt }
373*a1157835SDaniel Fojt 
374*a1157835SDaniel Fojt 
375*a1157835SDaniel Fojt /**
376*a1157835SDaniel Fojt  * fst_group_get_peer_other_connection - Find peer's "other" connection (iface,
377*a1157835SDaniel Fojt  * MAC tuple).
378*a1157835SDaniel Fojt  *
379*a1157835SDaniel Fojt  * @iface: iface on which FST Setup Request was received
380*a1157835SDaniel Fojt  * @peer_addr: Peer address on iface
381*a1157835SDaniel Fojt  * @band_id: "other" connection band id
382*a1157835SDaniel Fojt  * @other_peer_addr (out): Peer's MAC address on the "other" connection (on the
383*a1157835SDaniel Fojt  *   "other" iface)
384*a1157835SDaniel Fojt  *
385*a1157835SDaniel Fojt  * This function is called upon receiving FST Setup Request from some peer who
386*a1157835SDaniel Fojt  * has peer_addr on iface. It searches for another connection of the same peer
387*a1157835SDaniel Fojt  * on different interface which correlates with band_id. MB IEs received from
388*a1157835SDaniel Fojt  * peer (on the two different interfaces) are used to identify same peer.
389*a1157835SDaniel Fojt  */
390*a1157835SDaniel Fojt struct fst_iface *
fst_group_get_peer_other_connection(struct fst_iface * iface,const u8 * peer_addr,u8 band_id,u8 * other_peer_addr)391*a1157835SDaniel Fojt fst_group_get_peer_other_connection(struct fst_iface *iface,
392*a1157835SDaniel Fojt 				    const u8 *peer_addr, u8 band_id,
393*a1157835SDaniel Fojt 				    u8 *other_peer_addr)
394*a1157835SDaniel Fojt {
395*a1157835SDaniel Fojt 	struct fst_iface *other_iface;
396*a1157835SDaniel Fojt 
397*a1157835SDaniel Fojt 	fst_printf(MSG_DEBUG, "%s: %s:" MACSTR ", %d", __func__,
398*a1157835SDaniel Fojt 		   fst_iface_get_name(iface), MAC2STR(peer_addr), band_id);
399*a1157835SDaniel Fojt 
400*a1157835SDaniel Fojt 	/*
401*a1157835SDaniel Fojt 	 * Two search methods are used:
402*a1157835SDaniel Fojt 	 * 1. Use peer's MB IE on iface to extract peer's MAC address on
403*a1157835SDaniel Fojt 	 *    "other" connection. Then check if such "other" connection exists.
404*a1157835SDaniel Fojt 	 * 2. Iterate peer database, examine each MB IE to see if it points to
405*a1157835SDaniel Fojt 	 *    (iface, peer_addr) tuple
406*a1157835SDaniel Fojt 	 */
407*a1157835SDaniel Fojt 
408*a1157835SDaniel Fojt 	other_iface = fst_group_get_peer_other_connection_1(iface, peer_addr,
409*a1157835SDaniel Fojt 							    band_id,
410*a1157835SDaniel Fojt 							    other_peer_addr);
411*a1157835SDaniel Fojt 	if (other_iface) {
412*a1157835SDaniel Fojt 		fst_printf(MSG_DEBUG, "found by method #1. %s:" MACSTR,
413*a1157835SDaniel Fojt 			   fst_iface_get_name(other_iface),
414*a1157835SDaniel Fojt 			   MAC2STR(other_peer_addr));
415*a1157835SDaniel Fojt 		return other_iface;
416*a1157835SDaniel Fojt 	}
417*a1157835SDaniel Fojt 
418*a1157835SDaniel Fojt 	other_iface = fst_group_get_peer_other_connection_2(iface, peer_addr,
419*a1157835SDaniel Fojt 							    band_id,
420*a1157835SDaniel Fojt 							    other_peer_addr);
421*a1157835SDaniel Fojt 	if (other_iface) {
422*a1157835SDaniel Fojt 		fst_printf(MSG_DEBUG, "found by method #2. %s:" MACSTR,
423*a1157835SDaniel Fojt 			   fst_iface_get_name(other_iface),
424*a1157835SDaniel Fojt 			   MAC2STR(other_peer_addr));
425*a1157835SDaniel Fojt 		return other_iface;
426*a1157835SDaniel Fojt 	}
427*a1157835SDaniel Fojt 
428*a1157835SDaniel Fojt 	fst_printf(MSG_INFO, "%s: other connection not found", __func__);
429*a1157835SDaniel Fojt 	return NULL;
430*a1157835SDaniel Fojt }
431*a1157835SDaniel Fojt 
432*a1157835SDaniel Fojt 
fst_group_create(const char * group_id)433*a1157835SDaniel Fojt struct fst_group * fst_group_create(const char *group_id)
434*a1157835SDaniel Fojt {
435*a1157835SDaniel Fojt 	struct fst_group *g;
436*a1157835SDaniel Fojt 
437*a1157835SDaniel Fojt 	g = os_zalloc(sizeof(*g));
438*a1157835SDaniel Fojt 	if (g == NULL) {
439*a1157835SDaniel Fojt 		fst_printf(MSG_ERROR, "%s: Cannot alloc group", group_id);
440*a1157835SDaniel Fojt 		return NULL;
441*a1157835SDaniel Fojt 	}
442*a1157835SDaniel Fojt 
443*a1157835SDaniel Fojt 	dl_list_init(&g->ifaces);
444*a1157835SDaniel Fojt 	os_strlcpy(g->group_id, group_id, sizeof(g->group_id));
445*a1157835SDaniel Fojt 
446*a1157835SDaniel Fojt 	dl_list_add_tail(&fst_global_groups_list, &g->global_groups_lentry);
447*a1157835SDaniel Fojt 	fst_printf_group(g, MSG_DEBUG, "instance created");
448*a1157835SDaniel Fojt 
449*a1157835SDaniel Fojt 	foreach_fst_ctrl_call(on_group_created, g);
450*a1157835SDaniel Fojt 
451*a1157835SDaniel Fojt 	return g;
452*a1157835SDaniel Fojt }
453*a1157835SDaniel Fojt 
454*a1157835SDaniel Fojt 
fst_group_attach_iface(struct fst_group * g,struct fst_iface * i)455*a1157835SDaniel Fojt void fst_group_attach_iface(struct fst_group *g, struct fst_iface *i)
456*a1157835SDaniel Fojt {
457*a1157835SDaniel Fojt 	struct dl_list *list = &g->ifaces;
458*a1157835SDaniel Fojt 	struct fst_iface *f;
459*a1157835SDaniel Fojt 
460*a1157835SDaniel Fojt 	/*
461*a1157835SDaniel Fojt 	 * Add new interface to the list.
462*a1157835SDaniel Fojt 	 * The list is sorted in descending order by priority to allow
463*a1157835SDaniel Fojt 	 * multiple MB IEs creation according to the spec (see 10.32 Multi-band
464*a1157835SDaniel Fojt 	 * operation, 10.32.1 General), as they should be ordered according to
465*a1157835SDaniel Fojt 	 * priorities.
466*a1157835SDaniel Fojt 	 */
467*a1157835SDaniel Fojt 	foreach_fst_group_iface(g, f) {
468*a1157835SDaniel Fojt 		if (fst_iface_get_priority(f) < fst_iface_get_priority(i))
469*a1157835SDaniel Fojt 			break;
470*a1157835SDaniel Fojt 		list = &f->group_lentry;
471*a1157835SDaniel Fojt 	}
472*a1157835SDaniel Fojt 	dl_list_add(list, &i->group_lentry);
473*a1157835SDaniel Fojt }
474*a1157835SDaniel Fojt 
475*a1157835SDaniel Fojt 
fst_group_detach_iface(struct fst_group * g,struct fst_iface * i)476*a1157835SDaniel Fojt void fst_group_detach_iface(struct fst_group *g, struct fst_iface *i)
477*a1157835SDaniel Fojt {
478*a1157835SDaniel Fojt 	dl_list_del(&i->group_lentry);
479*a1157835SDaniel Fojt }
480*a1157835SDaniel Fojt 
481*a1157835SDaniel Fojt 
fst_group_delete(struct fst_group * group)482*a1157835SDaniel Fojt void fst_group_delete(struct fst_group *group)
483*a1157835SDaniel Fojt {
484*a1157835SDaniel Fojt 	struct fst_session *s;
485*a1157835SDaniel Fojt 
486*a1157835SDaniel Fojt 	dl_list_del(&group->global_groups_lentry);
487*a1157835SDaniel Fojt 	WPA_ASSERT(dl_list_empty(&group->ifaces));
488*a1157835SDaniel Fojt 	foreach_fst_ctrl_call(on_group_deleted, group);
489*a1157835SDaniel Fojt 	fst_printf_group(group, MSG_DEBUG, "instance deleted");
490*a1157835SDaniel Fojt 	while ((s = fst_session_global_get_first_by_group(group)) != NULL)
491*a1157835SDaniel Fojt 		fst_session_delete(s);
492*a1157835SDaniel Fojt 	os_free(group);
493*a1157835SDaniel Fojt }
494*a1157835SDaniel Fojt 
495*a1157835SDaniel Fojt 
fst_group_delete_if_empty(struct fst_group * group)496*a1157835SDaniel Fojt Boolean fst_group_delete_if_empty(struct fst_group *group)
497*a1157835SDaniel Fojt {
498*a1157835SDaniel Fojt 	Boolean is_empty = !fst_group_has_ifaces(group) &&
499*a1157835SDaniel Fojt 		!fst_session_global_get_first_by_group(group);
500*a1157835SDaniel Fojt 
501*a1157835SDaniel Fojt 	if (is_empty)
502*a1157835SDaniel Fojt 		fst_group_delete(group);
503*a1157835SDaniel Fojt 
504*a1157835SDaniel Fojt 	return is_empty;
505*a1157835SDaniel Fojt }
506*a1157835SDaniel Fojt 
507*a1157835SDaniel Fojt 
fst_group_update_ie(struct fst_group * g)508*a1157835SDaniel Fojt void fst_group_update_ie(struct fst_group *g)
509*a1157835SDaniel Fojt {
510*a1157835SDaniel Fojt 	struct fst_iface *i;
511*a1157835SDaniel Fojt 
512*a1157835SDaniel Fojt 	foreach_fst_group_iface(g, i) {
513*a1157835SDaniel Fojt 		struct wpabuf *mbie = fst_group_create_mb_ie(g, i);
514*a1157835SDaniel Fojt 
515*a1157835SDaniel Fojt 		if (!mbie)
516*a1157835SDaniel Fojt 			fst_printf_iface(i, MSG_WARNING, "cannot create MB IE");
517*a1157835SDaniel Fojt 
518*a1157835SDaniel Fojt 		fst_iface_attach_mbie(i, mbie);
519*a1157835SDaniel Fojt 		fst_iface_set_ies(i, mbie);
520*a1157835SDaniel Fojt 		fst_printf_iface(i, MSG_DEBUG, "multi-band IE set to %p", mbie);
521*a1157835SDaniel Fojt 	}
522*a1157835SDaniel Fojt }
523