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 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 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 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 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 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 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 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 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 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 * 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 * 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 (ether_addr_equal(this_peer_addr, peer_addr)) { 368 os_memcpy(other_peer_addr, cur_peer_addr, 369 ETH_ALEN); 370 return other_iface; 371 } 372 } 373 } 374 375 return NULL; 376 } 377 378 379 /** 380 * fst_group_get_peer_other_connection - Find peer's "other" connection (iface, 381 * MAC tuple). 382 * 383 * @iface: iface on which FST Setup Request was received 384 * @peer_addr: Peer address on iface 385 * @band_id: "other" connection band id 386 * @other_peer_addr (out): Peer's MAC address on the "other" connection (on the 387 * "other" iface) 388 * 389 * This function is called upon receiving FST Setup Request from some peer who 390 * has peer_addr on iface. It searches for another connection of the same peer 391 * on different interface which correlates with band_id. MB IEs received from 392 * peer (on the two different interfaces) are used to identify same peer. 393 */ 394 struct fst_iface * 395 fst_group_get_peer_other_connection(struct fst_iface *iface, 396 const u8 *peer_addr, u8 band_id, 397 u8 *other_peer_addr) 398 { 399 struct fst_iface *other_iface; 400 401 fst_printf(MSG_DEBUG, "%s: %s:" MACSTR ", %d", __func__, 402 fst_iface_get_name(iface), MAC2STR(peer_addr), band_id); 403 404 /* 405 * Two search methods are used: 406 * 1. Use peer's MB IE on iface to extract peer's MAC address on 407 * "other" connection. Then check if such "other" connection exists. 408 * 2. Iterate peer database, examine each MB IE to see if it points to 409 * (iface, peer_addr) tuple 410 */ 411 412 other_iface = fst_group_get_peer_other_connection_1(iface, peer_addr, 413 band_id, 414 other_peer_addr); 415 if (other_iface) { 416 fst_printf(MSG_DEBUG, "found by method #1. %s:" MACSTR, 417 fst_iface_get_name(other_iface), 418 MAC2STR(other_peer_addr)); 419 return other_iface; 420 } 421 422 other_iface = fst_group_get_peer_other_connection_2(iface, peer_addr, 423 band_id, 424 other_peer_addr); 425 if (other_iface) { 426 fst_printf(MSG_DEBUG, "found by method #2. %s:" MACSTR, 427 fst_iface_get_name(other_iface), 428 MAC2STR(other_peer_addr)); 429 return other_iface; 430 } 431 432 fst_printf(MSG_INFO, "%s: other connection not found", __func__); 433 return NULL; 434 } 435 436 437 struct fst_group * fst_group_create(const char *group_id) 438 { 439 struct fst_group *g; 440 441 g = os_zalloc(sizeof(*g)); 442 if (g == NULL) { 443 fst_printf(MSG_ERROR, "%s: Cannot alloc group", group_id); 444 return NULL; 445 } 446 447 dl_list_init(&g->ifaces); 448 os_strlcpy(g->group_id, group_id, sizeof(g->group_id)); 449 450 dl_list_add_tail(&fst_global_groups_list, &g->global_groups_lentry); 451 fst_printf_group(g, MSG_DEBUG, "instance created"); 452 453 foreach_fst_ctrl_call(on_group_created, g); 454 455 return g; 456 } 457 458 459 void fst_group_attach_iface(struct fst_group *g, struct fst_iface *i) 460 { 461 struct dl_list *list = &g->ifaces; 462 struct fst_iface *f; 463 464 /* 465 * Add new interface to the list. 466 * The list is sorted in descending order by priority to allow 467 * multiple MB IEs creation according to the spec (see 10.32 Multi-band 468 * operation, 10.32.1 General), as they should be ordered according to 469 * priorities. 470 */ 471 foreach_fst_group_iface(g, f) { 472 if (fst_iface_get_priority(f) < fst_iface_get_priority(i)) 473 break; 474 list = &f->group_lentry; 475 } 476 dl_list_add(list, &i->group_lentry); 477 } 478 479 480 void fst_group_detach_iface(struct fst_group *g, struct fst_iface *i) 481 { 482 dl_list_del(&i->group_lentry); 483 } 484 485 486 void fst_group_delete(struct fst_group *group) 487 { 488 struct fst_session *s; 489 490 dl_list_del(&group->global_groups_lentry); 491 WPA_ASSERT(dl_list_empty(&group->ifaces)); 492 foreach_fst_ctrl_call(on_group_deleted, group); 493 fst_printf_group(group, MSG_DEBUG, "instance deleted"); 494 while ((s = fst_session_global_get_first_by_group(group)) != NULL) 495 fst_session_delete(s); 496 os_free(group); 497 } 498 499 500 bool fst_group_delete_if_empty(struct fst_group *group) 501 { 502 bool is_empty = !fst_group_has_ifaces(group) && 503 !fst_session_global_get_first_by_group(group); 504 505 if (is_empty) 506 fst_group_delete(group); 507 508 return is_empty; 509 } 510 511 512 void fst_group_update_ie(struct fst_group *g) 513 { 514 struct fst_iface *i; 515 516 foreach_fst_group_iface(g, i) { 517 struct wpabuf *mbie = fst_group_create_mb_ie(g, i); 518 519 if (!mbie) 520 fst_printf_iface(i, MSG_WARNING, "cannot create MB IE"); 521 522 fst_iface_attach_mbie(i, mbie); 523 fst_iface_set_ies(i, mbie); 524 fst_printf_iface(i, MSG_DEBUG, "multi-band IE set to %p", mbie); 525 } 526 } 527