1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * MLO link handling 4 * 5 * Copyright (C) 2022 Intel Corporation 6 */ 7 #include <linux/slab.h> 8 #include <linux/kernel.h> 9 #include <net/mac80211.h> 10 #include "ieee80211_i.h" 11 #include "driver-ops.h" 12 #include "key.h" 13 #include "debugfs_netdev.h" 14 15 void ieee80211_link_setup(struct ieee80211_link_data *link) 16 { 17 if (link->sdata->vif.type == NL80211_IFTYPE_STATION) 18 ieee80211_mgd_setup_link(link); 19 } 20 21 void ieee80211_link_init(struct ieee80211_sub_if_data *sdata, 22 int link_id, 23 struct ieee80211_link_data *link, 24 struct ieee80211_bss_conf *link_conf) 25 { 26 bool deflink = link_id < 0; 27 28 if (link_id < 0) 29 link_id = 0; 30 31 rcu_assign_pointer(sdata->vif.link_conf[link_id], link_conf); 32 rcu_assign_pointer(sdata->link[link_id], link); 33 34 link->sdata = sdata; 35 link->link_id = link_id; 36 link->conf = link_conf; 37 link_conf->link_id = link_id; 38 link_conf->vif = &sdata->vif; 39 40 INIT_WORK(&link->csa_finalize_work, 41 ieee80211_csa_finalize_work); 42 INIT_WORK(&link->color_change_finalize_work, 43 ieee80211_color_change_finalize_work); 44 INIT_DELAYED_WORK(&link->color_collision_detect_work, 45 ieee80211_color_collision_detection_work); 46 INIT_LIST_HEAD(&link->assigned_chanctx_list); 47 INIT_LIST_HEAD(&link->reserved_chanctx_list); 48 INIT_DELAYED_WORK(&link->dfs_cac_timer_work, 49 ieee80211_dfs_cac_timer_work); 50 51 if (!deflink) { 52 switch (sdata->vif.type) { 53 case NL80211_IFTYPE_AP: 54 ether_addr_copy(link_conf->addr, 55 sdata->wdev.links[link_id].addr); 56 link_conf->bssid = link_conf->addr; 57 WARN_ON(!(sdata->wdev.valid_links & BIT(link_id))); 58 break; 59 case NL80211_IFTYPE_STATION: 60 /* station sets the bssid in ieee80211_mgd_setup_link */ 61 break; 62 default: 63 WARN_ON(1); 64 } 65 66 ieee80211_link_debugfs_add(link); 67 } 68 } 69 70 void ieee80211_link_stop(struct ieee80211_link_data *link) 71 { 72 if (link->sdata->vif.type == NL80211_IFTYPE_STATION) 73 ieee80211_mgd_stop_link(link); 74 75 cancel_delayed_work_sync(&link->color_collision_detect_work); 76 ieee80211_link_release_channel(link); 77 } 78 79 struct link_container { 80 struct ieee80211_link_data data; 81 struct ieee80211_bss_conf conf; 82 }; 83 84 static void ieee80211_tear_down_links(struct ieee80211_sub_if_data *sdata, 85 struct link_container **links, u16 mask) 86 { 87 struct ieee80211_link_data *link; 88 LIST_HEAD(keys); 89 unsigned int link_id; 90 91 for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) { 92 if (!(mask & BIT(link_id))) 93 continue; 94 link = &links[link_id]->data; 95 if (link_id == 0 && !link) 96 link = &sdata->deflink; 97 if (WARN_ON(!link)) 98 continue; 99 ieee80211_remove_link_keys(link, &keys); 100 ieee80211_link_debugfs_remove(link); 101 ieee80211_link_stop(link); 102 } 103 104 synchronize_rcu(); 105 106 ieee80211_free_key_list(sdata->local, &keys); 107 } 108 109 static void ieee80211_free_links(struct ieee80211_sub_if_data *sdata, 110 struct link_container **links) 111 { 112 unsigned int link_id; 113 114 for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) 115 kfree(links[link_id]); 116 } 117 118 static int ieee80211_check_dup_link_addrs(struct ieee80211_sub_if_data *sdata) 119 { 120 unsigned int i, j; 121 122 for (i = 0; i < IEEE80211_MLD_MAX_NUM_LINKS; i++) { 123 struct ieee80211_link_data *link1; 124 125 link1 = sdata_dereference(sdata->link[i], sdata); 126 if (!link1) 127 continue; 128 for (j = i + 1; j < IEEE80211_MLD_MAX_NUM_LINKS; j++) { 129 struct ieee80211_link_data *link2; 130 131 link2 = sdata_dereference(sdata->link[j], sdata); 132 if (!link2) 133 continue; 134 135 if (ether_addr_equal(link1->conf->addr, 136 link2->conf->addr)) 137 return -EALREADY; 138 } 139 } 140 141 return 0; 142 } 143 144 static void ieee80211_set_vif_links_bitmaps(struct ieee80211_sub_if_data *sdata, 145 u16 links) 146 { 147 sdata->vif.valid_links = links; 148 149 if (!links) { 150 sdata->vif.active_links = 0; 151 return; 152 } 153 154 switch (sdata->vif.type) { 155 case NL80211_IFTYPE_AP: 156 /* in an AP all links are always active */ 157 sdata->vif.active_links = links; 158 break; 159 case NL80211_IFTYPE_STATION: 160 if (sdata->vif.active_links) 161 break; 162 WARN_ON(hweight16(links) > 1); 163 sdata->vif.active_links = links; 164 break; 165 default: 166 WARN_ON(1); 167 } 168 } 169 170 static int ieee80211_vif_update_links(struct ieee80211_sub_if_data *sdata, 171 struct link_container **to_free, 172 u16 new_links) 173 { 174 u16 old_links = sdata->vif.valid_links; 175 u16 old_active = sdata->vif.active_links; 176 unsigned long add = new_links & ~old_links; 177 unsigned long rem = old_links & ~new_links; 178 unsigned int link_id; 179 int ret; 180 struct link_container *links[IEEE80211_MLD_MAX_NUM_LINKS] = {}, *link; 181 struct ieee80211_bss_conf *old[IEEE80211_MLD_MAX_NUM_LINKS]; 182 struct ieee80211_link_data *old_data[IEEE80211_MLD_MAX_NUM_LINKS]; 183 bool use_deflink = old_links == 0; /* set for error case */ 184 185 sdata_assert_lock(sdata); 186 187 memset(to_free, 0, sizeof(links)); 188 189 if (old_links == new_links) 190 return 0; 191 192 /* if there were no old links, need to clear the pointers to deflink */ 193 if (!old_links) 194 rem |= BIT(0); 195 196 /* allocate new link structures first */ 197 for_each_set_bit(link_id, &add, IEEE80211_MLD_MAX_NUM_LINKS) { 198 link = kzalloc(sizeof(*link), GFP_KERNEL); 199 if (!link) { 200 ret = -ENOMEM; 201 goto free; 202 } 203 links[link_id] = link; 204 } 205 206 /* keep track of the old pointers for the driver */ 207 BUILD_BUG_ON(sizeof(old) != sizeof(sdata->vif.link_conf)); 208 memcpy(old, sdata->vif.link_conf, sizeof(old)); 209 /* and for us in error cases */ 210 BUILD_BUG_ON(sizeof(old_data) != sizeof(sdata->link)); 211 memcpy(old_data, sdata->link, sizeof(old_data)); 212 213 /* grab old links to free later */ 214 for_each_set_bit(link_id, &rem, IEEE80211_MLD_MAX_NUM_LINKS) { 215 if (rcu_access_pointer(sdata->link[link_id]) != &sdata->deflink) { 216 /* 217 * we must have allocated the data through this path so 218 * we know we can free both at the same time 219 */ 220 to_free[link_id] = container_of(rcu_access_pointer(sdata->link[link_id]), 221 typeof(*links[link_id]), 222 data); 223 } 224 225 RCU_INIT_POINTER(sdata->link[link_id], NULL); 226 RCU_INIT_POINTER(sdata->vif.link_conf[link_id], NULL); 227 } 228 229 /* link them into data structures */ 230 for_each_set_bit(link_id, &add, IEEE80211_MLD_MAX_NUM_LINKS) { 231 WARN_ON(!use_deflink && 232 rcu_access_pointer(sdata->link[link_id]) == &sdata->deflink); 233 234 link = links[link_id]; 235 ieee80211_link_init(sdata, link_id, &link->data, &link->conf); 236 ieee80211_link_setup(&link->data); 237 } 238 239 if (new_links == 0) 240 ieee80211_link_init(sdata, -1, &sdata->deflink, 241 &sdata->vif.bss_conf); 242 243 ret = ieee80211_check_dup_link_addrs(sdata); 244 if (!ret) { 245 /* for keys we will not be able to undo this */ 246 ieee80211_tear_down_links(sdata, to_free, rem); 247 248 ieee80211_set_vif_links_bitmaps(sdata, new_links); 249 250 /* tell the driver */ 251 ret = drv_change_vif_links(sdata->local, sdata, 252 old_links & old_active, 253 new_links & sdata->vif.active_links, 254 old); 255 } 256 257 if (ret) { 258 /* restore config */ 259 memcpy(sdata->link, old_data, sizeof(old_data)); 260 memcpy(sdata->vif.link_conf, old, sizeof(old)); 261 ieee80211_set_vif_links_bitmaps(sdata, old_links); 262 /* and free (only) the newly allocated links */ 263 memset(to_free, 0, sizeof(links)); 264 goto free; 265 } 266 267 /* use deflink/bss_conf again if and only if there are no more links */ 268 use_deflink = new_links == 0; 269 270 goto deinit; 271 free: 272 /* if we failed during allocation, only free all */ 273 for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) { 274 kfree(links[link_id]); 275 links[link_id] = NULL; 276 } 277 deinit: 278 if (use_deflink) 279 ieee80211_link_init(sdata, -1, &sdata->deflink, 280 &sdata->vif.bss_conf); 281 return ret; 282 } 283 284 int ieee80211_vif_set_links(struct ieee80211_sub_if_data *sdata, 285 u16 new_links) 286 { 287 struct link_container *links[IEEE80211_MLD_MAX_NUM_LINKS]; 288 int ret; 289 290 ret = ieee80211_vif_update_links(sdata, links, new_links); 291 ieee80211_free_links(sdata, links); 292 293 return ret; 294 } 295 296 void ieee80211_vif_clear_links(struct ieee80211_sub_if_data *sdata) 297 { 298 struct link_container *links[IEEE80211_MLD_MAX_NUM_LINKS]; 299 300 /* 301 * The locking here is different because when we free links 302 * in the station case we need to be able to cancel_work_sync() 303 * something that also takes the lock. 304 */ 305 306 sdata_lock(sdata); 307 ieee80211_vif_update_links(sdata, links, 0); 308 sdata_unlock(sdata); 309 310 ieee80211_free_links(sdata, links); 311 } 312 313 static int _ieee80211_set_active_links(struct ieee80211_sub_if_data *sdata, 314 u16 active_links) 315 { 316 struct ieee80211_bss_conf *link_confs[IEEE80211_MLD_MAX_NUM_LINKS]; 317 struct ieee80211_local *local = sdata->local; 318 u16 old_active = sdata->vif.active_links; 319 unsigned long rem = old_active & ~active_links; 320 unsigned long add = active_links & ~old_active; 321 struct sta_info *sta; 322 unsigned int link_id; 323 int ret, i; 324 325 if (!ieee80211_sdata_running(sdata)) 326 return -ENETDOWN; 327 328 if (sdata->vif.type != NL80211_IFTYPE_STATION) 329 return -EINVAL; 330 331 /* cannot activate links that don't exist */ 332 if (active_links & ~sdata->vif.valid_links) 333 return -EINVAL; 334 335 /* nothing to do */ 336 if (old_active == active_links) 337 return 0; 338 339 for (i = 0; i < IEEE80211_MLD_MAX_NUM_LINKS; i++) 340 link_confs[i] = sdata_dereference(sdata->vif.link_conf[i], 341 sdata); 342 343 if (add) { 344 sdata->vif.active_links |= active_links; 345 ret = drv_change_vif_links(local, sdata, 346 old_active, 347 sdata->vif.active_links, 348 link_confs); 349 if (ret) { 350 sdata->vif.active_links = old_active; 351 return ret; 352 } 353 } 354 355 for_each_set_bit(link_id, &rem, IEEE80211_MLD_MAX_NUM_LINKS) { 356 struct ieee80211_link_data *link; 357 358 link = sdata_dereference(sdata->link[link_id], sdata); 359 360 /* FIXME: kill TDLS connections on the link */ 361 362 ieee80211_link_release_channel(link); 363 } 364 365 list_for_each_entry(sta, &local->sta_list, list) { 366 if (sdata != sta->sdata) 367 continue; 368 369 /* this is very temporary, but do it anyway */ 370 __ieee80211_sta_recalc_aggregates(sta, 371 old_active | active_links); 372 373 ret = drv_change_sta_links(local, sdata, &sta->sta, 374 old_active, 375 old_active | active_links); 376 WARN_ON_ONCE(ret); 377 } 378 379 ret = ieee80211_key_switch_links(sdata, rem, add); 380 WARN_ON_ONCE(ret); 381 382 list_for_each_entry(sta, &local->sta_list, list) { 383 if (sdata != sta->sdata) 384 continue; 385 386 __ieee80211_sta_recalc_aggregates(sta, active_links); 387 388 ret = drv_change_sta_links(local, sdata, &sta->sta, 389 old_active | active_links, 390 active_links); 391 WARN_ON_ONCE(ret); 392 393 /* 394 * Do it again, just in case - the driver might very 395 * well have called ieee80211_sta_recalc_aggregates() 396 * from there when filling in the new links, which 397 * would set it wrong since the vif's active links are 398 * not switched yet... 399 */ 400 __ieee80211_sta_recalc_aggregates(sta, active_links); 401 } 402 403 for_each_set_bit(link_id, &add, IEEE80211_MLD_MAX_NUM_LINKS) { 404 struct ieee80211_link_data *link; 405 406 link = sdata_dereference(sdata->link[link_id], sdata); 407 408 ret = ieee80211_link_use_channel(link, &link->conf->chandef, 409 IEEE80211_CHANCTX_SHARED); 410 WARN_ON_ONCE(ret); 411 412 ieee80211_link_info_change_notify(sdata, link, 413 BSS_CHANGED_ERP_CTS_PROT | 414 BSS_CHANGED_ERP_PREAMBLE | 415 BSS_CHANGED_ERP_SLOT | 416 BSS_CHANGED_HT | 417 BSS_CHANGED_BASIC_RATES | 418 BSS_CHANGED_BSSID | 419 BSS_CHANGED_CQM | 420 BSS_CHANGED_QOS | 421 BSS_CHANGED_TXPOWER | 422 BSS_CHANGED_BANDWIDTH | 423 BSS_CHANGED_TWT | 424 BSS_CHANGED_HE_OBSS_PD | 425 BSS_CHANGED_HE_BSS_COLOR); 426 ieee80211_mgd_set_link_qos_params(link); 427 } 428 429 old_active = sdata->vif.active_links; 430 sdata->vif.active_links = active_links; 431 432 if (rem) { 433 ret = drv_change_vif_links(local, sdata, old_active, 434 active_links, link_confs); 435 WARN_ON_ONCE(ret); 436 } 437 438 return 0; 439 } 440 441 int ieee80211_set_active_links(struct ieee80211_vif *vif, u16 active_links) 442 { 443 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif); 444 struct ieee80211_local *local = sdata->local; 445 u16 old_active; 446 int ret; 447 448 sdata_lock(sdata); 449 mutex_lock(&local->sta_mtx); 450 mutex_lock(&local->mtx); 451 mutex_lock(&local->key_mtx); 452 old_active = sdata->vif.active_links; 453 if (old_active & active_links) { 454 /* 455 * if there's at least one link that stays active across 456 * the change then switch to it (to those) first, and 457 * then enable the additional links 458 */ 459 ret = _ieee80211_set_active_links(sdata, 460 old_active & active_links); 461 if (!ret) 462 ret = _ieee80211_set_active_links(sdata, active_links); 463 } else { 464 /* otherwise switch directly */ 465 ret = _ieee80211_set_active_links(sdata, active_links); 466 } 467 mutex_unlock(&local->key_mtx); 468 mutex_unlock(&local->mtx); 469 mutex_unlock(&local->sta_mtx); 470 sdata_unlock(sdata); 471 472 return ret; 473 } 474 EXPORT_SYMBOL_GPL(ieee80211_set_active_links); 475 476 void ieee80211_set_active_links_async(struct ieee80211_vif *vif, 477 u16 active_links) 478 { 479 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif); 480 481 if (!ieee80211_sdata_running(sdata)) 482 return; 483 484 if (sdata->vif.type != NL80211_IFTYPE_STATION) 485 return; 486 487 /* cannot activate links that don't exist */ 488 if (active_links & ~sdata->vif.valid_links) 489 return; 490 491 /* nothing to do */ 492 if (sdata->vif.active_links == active_links) 493 return; 494 495 sdata->desired_active_links = active_links; 496 schedule_work(&sdata->activate_links_work); 497 } 498 EXPORT_SYMBOL_GPL(ieee80211_set_active_links_async); 499