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