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