Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 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 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 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(2 + 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; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 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 struct fst_iface * |
| 200 | fst_group_get_new_iface_by_mbie_and_band_id(struct fst_group *g, |
| 201 | const u8 *mb_ies_buff, |
| 202 | size_t mb_ies_size, |
| 203 | u8 band_id, |
| 204 | u8 *iface_peer_addr) |
| 205 | { |
| 206 | while (mb_ies_size >= 2) { |
| 207 | const struct multi_band_ie *mbie = |
| 208 | (const struct multi_band_ie *) mb_ies_buff; |
| 209 | |
| 210 | if (mbie->eid != WLAN_EID_MULTI_BAND || |
| 211 | (size_t) 2 + mbie->len < sizeof(*mbie)) |
| 212 | break; |
| 213 | |
| 214 | if (mbie->band_id == band_id) { |
| 215 | struct fst_iface *iface; |
| 216 | |
| 217 | foreach_fst_group_iface(g, iface) { |
| 218 | const u8 *peer_addr = |
| 219 | fst_mbie_get_peer_addr(mbie); |
| 220 | |
| 221 | if (peer_addr && |
Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 222 | fst_iface_is_connected(iface, peer_addr, |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 223 | FALSE) && |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 224 | band_id == fst_iface_get_band_id(iface)) { |
| 225 | os_memcpy(iface_peer_addr, peer_addr, |
| 226 | ETH_ALEN); |
| 227 | return iface; |
| 228 | } |
| 229 | } |
| 230 | break; |
| 231 | } |
| 232 | |
| 233 | mb_ies_buff += 2 + mbie->len; |
| 234 | mb_ies_size -= 2 + mbie->len; |
| 235 | } |
| 236 | |
| 237 | return NULL; |
| 238 | } |
| 239 | |
| 240 | |
| 241 | struct fst_iface * fst_group_get_iface_by_name(struct fst_group *g, |
| 242 | const char *ifname) |
| 243 | { |
| 244 | struct fst_iface *f; |
| 245 | |
| 246 | foreach_fst_group_iface(g, f) { |
| 247 | const char *in = fst_iface_get_name(f); |
| 248 | |
| 249 | if (os_strncmp(in, ifname, os_strlen(in)) == 0) |
| 250 | return f; |
| 251 | } |
| 252 | |
| 253 | return NULL; |
| 254 | } |
| 255 | |
| 256 | |
| 257 | u8 fst_group_assign_dialog_token(struct fst_group *g) |
| 258 | { |
| 259 | g->dialog_token++; |
| 260 | if (g->dialog_token == 0) |
| 261 | g->dialog_token++; |
| 262 | return g->dialog_token; |
| 263 | } |
| 264 | |
| 265 | |
| 266 | u32 fst_group_assign_fsts_id(struct fst_group *g) |
| 267 | { |
| 268 | g->fsts_id++; |
| 269 | return g->fsts_id; |
| 270 | } |
| 271 | |
| 272 | |
| 273 | static Boolean |
| 274 | fst_group_does_iface_appear_in_other_mbies(struct fst_group *g, |
| 275 | struct fst_iface *iface, |
| 276 | struct fst_iface *other, |
| 277 | u8 *peer_addr) |
| 278 | { |
| 279 | struct fst_get_peer_ctx *ctx; |
| 280 | const u8 *addr; |
| 281 | const u8 *iface_addr; |
| 282 | enum mb_band_id iface_band_id; |
| 283 | |
| 284 | WPA_ASSERT(g == fst_iface_get_group(iface)); |
| 285 | WPA_ASSERT(g == fst_iface_get_group(other)); |
| 286 | |
| 287 | iface_addr = fst_iface_get_addr(iface); |
| 288 | iface_band_id = fst_iface_get_band_id(iface); |
| 289 | |
| 290 | addr = fst_iface_get_peer_first(other, &ctx, TRUE); |
| 291 | for (; addr; addr = fst_iface_get_peer_next(other, &ctx, TRUE)) { |
| 292 | const struct wpabuf *mbies; |
| 293 | u8 other_iface_peer_addr[ETH_ALEN]; |
| 294 | struct fst_iface *other_new_iface; |
| 295 | |
| 296 | mbies = fst_iface_get_peer_mb_ie(other, addr); |
| 297 | if (!mbies) |
| 298 | continue; |
| 299 | |
| 300 | other_new_iface = fst_group_get_new_iface_by_mbie_and_band_id( |
| 301 | g, wpabuf_head(mbies), wpabuf_len(mbies), |
| 302 | iface_band_id, other_iface_peer_addr); |
| 303 | if (other_new_iface == iface && |
| 304 | os_memcmp(iface_addr, other_iface_peer_addr, |
| 305 | ETH_ALEN) != 0) { |
| 306 | os_memcpy(peer_addr, addr, ETH_ALEN); |
| 307 | return TRUE; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | return FALSE; |
| 312 | } |
| 313 | |
| 314 | |
| 315 | struct fst_iface * |
| 316 | fst_group_find_new_iface_by_stie(struct fst_group *g, |
| 317 | struct fst_iface *iface, |
| 318 | const u8 *peer_addr, |
| 319 | const struct session_transition_ie *stie, |
| 320 | u8 *iface_peer_addr) |
| 321 | { |
| 322 | struct fst_iface *i; |
| 323 | |
| 324 | foreach_fst_group_iface(g, i) { |
| 325 | if (i == iface || |
| 326 | stie->new_band_id != fst_iface_get_band_id(i)) |
| 327 | continue; |
| 328 | if (fst_group_does_iface_appear_in_other_mbies(g, iface, i, |
| 329 | iface_peer_addr)) |
| 330 | return i; |
| 331 | break; |
| 332 | } |
| 333 | return NULL; |
| 334 | } |
| 335 | |
| 336 | |
| 337 | struct fst_iface * |
| 338 | fst_group_get_new_iface_by_stie_and_mbie( |
| 339 | struct fst_group *g, const u8 *mb_ies_buff, size_t mb_ies_size, |
| 340 | const struct session_transition_ie *stie, u8 *iface_peer_addr) |
| 341 | { |
| 342 | return fst_group_get_new_iface_by_mbie_and_band_id( |
| 343 | g, mb_ies_buff, mb_ies_size, stie->new_band_id, |
| 344 | iface_peer_addr); |
| 345 | } |
| 346 | |
| 347 | |
| 348 | struct fst_group * fst_group_create(const char *group_id) |
| 349 | { |
| 350 | struct fst_group *g; |
| 351 | |
| 352 | g = os_zalloc(sizeof(*g)); |
| 353 | if (g == NULL) { |
| 354 | fst_printf(MSG_ERROR, "%s: Cannot alloc group", group_id); |
| 355 | return NULL; |
| 356 | } |
| 357 | |
| 358 | dl_list_init(&g->ifaces); |
| 359 | os_strlcpy(g->group_id, group_id, sizeof(g->group_id)); |
| 360 | |
| 361 | dl_list_add_tail(&fst_global_groups_list, &g->global_groups_lentry); |
| 362 | fst_printf_group(g, MSG_DEBUG, "instance created"); |
| 363 | |
| 364 | foreach_fst_ctrl_call(on_group_created, g); |
| 365 | |
| 366 | return g; |
| 367 | } |
| 368 | |
| 369 | |
| 370 | void fst_group_attach_iface(struct fst_group *g, struct fst_iface *i) |
| 371 | { |
| 372 | struct dl_list *list = &g->ifaces; |
| 373 | struct fst_iface *f; |
| 374 | |
| 375 | /* |
| 376 | * Add new interface to the list. |
| 377 | * The list is sorted in descending order by priority to allow |
| 378 | * multiple MB IEs creation according to the spec (see 10.32 Multi-band |
| 379 | * operation, 10.32.1 General), as they should be ordered according to |
| 380 | * priorities. |
| 381 | */ |
| 382 | foreach_fst_group_iface(g, f) { |
| 383 | if (fst_iface_get_priority(f) < fst_iface_get_priority(i)) |
| 384 | break; |
| 385 | list = &f->group_lentry; |
| 386 | } |
| 387 | dl_list_add(list, &i->group_lentry); |
| 388 | } |
| 389 | |
| 390 | |
| 391 | void fst_group_detach_iface(struct fst_group *g, struct fst_iface *i) |
| 392 | { |
| 393 | dl_list_del(&i->group_lentry); |
| 394 | } |
| 395 | |
| 396 | |
| 397 | void fst_group_delete(struct fst_group *group) |
| 398 | { |
| 399 | struct fst_session *s; |
| 400 | |
| 401 | dl_list_del(&group->global_groups_lentry); |
| 402 | WPA_ASSERT(dl_list_empty(&group->ifaces)); |
| 403 | foreach_fst_ctrl_call(on_group_deleted, group); |
| 404 | fst_printf_group(group, MSG_DEBUG, "instance deleted"); |
| 405 | while ((s = fst_session_global_get_first_by_group(group)) != NULL) |
| 406 | fst_session_delete(s); |
| 407 | os_free(group); |
| 408 | } |
| 409 | |
| 410 | |
| 411 | Boolean fst_group_delete_if_empty(struct fst_group *group) |
| 412 | { |
| 413 | Boolean is_empty = !fst_group_has_ifaces(group) && |
| 414 | !fst_session_global_get_first_by_group(group); |
| 415 | |
| 416 | if (is_empty) |
| 417 | fst_group_delete(group); |
| 418 | |
| 419 | return is_empty; |
| 420 | } |
| 421 | |
| 422 | |
| 423 | void fst_group_update_ie(struct fst_group *g) |
| 424 | { |
| 425 | struct fst_iface *i; |
| 426 | |
| 427 | foreach_fst_group_iface(g, i) { |
| 428 | struct wpabuf *mbie = fst_group_create_mb_ie(g, i); |
| 429 | |
| 430 | if (!mbie) |
| 431 | fst_printf_iface(i, MSG_WARNING, "cannot create MB IE"); |
| 432 | |
| 433 | fst_iface_attach_mbie(i, mbie); |
| 434 | fst_iface_set_ies(i, mbie); |
| 435 | fst_printf_iface(i, MSG_DEBUG, "multi-band IE set to %p", mbie); |
| 436 | } |
| 437 | } |