1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2022-2023 Intel Corporation
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"
13 #include "debugfs_netdev.h"
15 void ieee80211_link_setup(struct ieee80211_link_data *link)
17 if (link->sdata->vif.type == NL80211_IFTYPE_STATION)
18 ieee80211_mgd_setup_link(link);
21 void ieee80211_link_init(struct ieee80211_sub_if_data *sdata,
23 struct ieee80211_link_data *link,
24 struct ieee80211_bss_conf *link_conf)
26 bool deflink = link_id < 0;
31 rcu_assign_pointer(sdata->vif.link_conf[link_id], link_conf);
32 rcu_assign_pointer(sdata->link[link_id], link);
35 link->link_id = link_id;
36 link->conf = link_conf;
37 link_conf->link_id = link_id;
38 link_conf->vif = &sdata->vif;
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);
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)));
59 case NL80211_IFTYPE_STATION:
60 /* station sets the bssid in ieee80211_mgd_setup_link */
66 ieee80211_link_debugfs_add(link);
70 void ieee80211_link_stop(struct ieee80211_link_data *link)
72 if (link->sdata->vif.type == NL80211_IFTYPE_STATION)
73 ieee80211_mgd_stop_link(link);
75 cancel_delayed_work_sync(&link->color_collision_detect_work);
76 ieee80211_link_release_channel(link);
79 struct link_container {
80 struct ieee80211_link_data data;
81 struct ieee80211_bss_conf conf;
84 static void ieee80211_tear_down_links(struct ieee80211_sub_if_data *sdata,
85 struct link_container **links, u16 mask)
87 struct ieee80211_link_data *link;
91 for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
92 if (!(mask & BIT(link_id)))
94 link = &links[link_id]->data;
95 if (link_id == 0 && !link)
96 link = &sdata->deflink;
99 ieee80211_remove_link_keys(link, &keys);
100 ieee80211_link_debugfs_remove(link);
101 ieee80211_link_stop(link);
106 ieee80211_free_key_list(sdata->local, &keys);
109 static void ieee80211_free_links(struct ieee80211_sub_if_data *sdata,
110 struct link_container **links)
112 unsigned int link_id;
114 for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++)
115 kfree(links[link_id]);
118 static int ieee80211_check_dup_link_addrs(struct ieee80211_sub_if_data *sdata)
122 for (i = 0; i < IEEE80211_MLD_MAX_NUM_LINKS; i++) {
123 struct ieee80211_link_data *link1;
125 link1 = sdata_dereference(sdata->link[i], sdata);
128 for (j = i + 1; j < IEEE80211_MLD_MAX_NUM_LINKS; j++) {
129 struct ieee80211_link_data *link2;
131 link2 = sdata_dereference(sdata->link[j], sdata);
135 if (ether_addr_equal(link1->conf->addr,
144 static void ieee80211_set_vif_links_bitmaps(struct ieee80211_sub_if_data *sdata,
145 u16 valid_links, u16 dormant_links)
147 sdata->vif.valid_links = valid_links;
148 sdata->vif.dormant_links = dormant_links;
151 WARN((~valid_links & dormant_links) ||
152 !(valid_links & ~dormant_links),
153 "Invalid links: valid=0x%x, dormant=0x%x",
154 valid_links, dormant_links)) {
155 sdata->vif.active_links = 0;
156 sdata->vif.dormant_links = 0;
160 switch (sdata->vif.type) {
161 case NL80211_IFTYPE_AP:
162 /* in an AP all links are always active */
163 sdata->vif.active_links = valid_links;
165 /* AP links are not expected to be disabled */
166 WARN_ON(dormant_links);
168 case NL80211_IFTYPE_STATION:
169 if (sdata->vif.active_links)
171 sdata->vif.active_links = valid_links & ~dormant_links;
172 WARN_ON(hweight16(sdata->vif.active_links) > 1);
179 static int ieee80211_vif_update_links(struct ieee80211_sub_if_data *sdata,
180 struct link_container **to_free,
181 u16 new_links, u16 dormant_links)
183 u16 old_links = sdata->vif.valid_links;
184 u16 old_active = sdata->vif.active_links;
185 unsigned long add = new_links & ~old_links;
186 unsigned long rem = old_links & ~new_links;
187 unsigned int link_id;
189 struct link_container *links[IEEE80211_MLD_MAX_NUM_LINKS] = {}, *link;
190 struct ieee80211_bss_conf *old[IEEE80211_MLD_MAX_NUM_LINKS];
191 struct ieee80211_link_data *old_data[IEEE80211_MLD_MAX_NUM_LINKS];
192 bool use_deflink = old_links == 0; /* set for error case */
194 sdata_assert_lock(sdata);
196 memset(to_free, 0, sizeof(links));
198 if (old_links == new_links)
201 /* if there were no old links, need to clear the pointers to deflink */
205 /* allocate new link structures first */
206 for_each_set_bit(link_id, &add, IEEE80211_MLD_MAX_NUM_LINKS) {
207 link = kzalloc(sizeof(*link), GFP_KERNEL);
212 links[link_id] = link;
215 /* keep track of the old pointers for the driver */
216 BUILD_BUG_ON(sizeof(old) != sizeof(sdata->vif.link_conf));
217 memcpy(old, sdata->vif.link_conf, sizeof(old));
218 /* and for us in error cases */
219 BUILD_BUG_ON(sizeof(old_data) != sizeof(sdata->link));
220 memcpy(old_data, sdata->link, sizeof(old_data));
222 /* grab old links to free later */
223 for_each_set_bit(link_id, &rem, IEEE80211_MLD_MAX_NUM_LINKS) {
224 if (rcu_access_pointer(sdata->link[link_id]) != &sdata->deflink) {
226 * we must have allocated the data through this path so
227 * we know we can free both at the same time
229 to_free[link_id] = container_of(rcu_access_pointer(sdata->link[link_id]),
230 typeof(*links[link_id]),
234 RCU_INIT_POINTER(sdata->link[link_id], NULL);
235 RCU_INIT_POINTER(sdata->vif.link_conf[link_id], NULL);
238 /* link them into data structures */
239 for_each_set_bit(link_id, &add, IEEE80211_MLD_MAX_NUM_LINKS) {
240 WARN_ON(!use_deflink &&
241 rcu_access_pointer(sdata->link[link_id]) == &sdata->deflink);
243 link = links[link_id];
244 ieee80211_link_init(sdata, link_id, &link->data, &link->conf);
245 ieee80211_link_setup(&link->data);
249 ieee80211_link_init(sdata, -1, &sdata->deflink,
250 &sdata->vif.bss_conf);
252 ret = ieee80211_check_dup_link_addrs(sdata);
254 /* for keys we will not be able to undo this */
255 ieee80211_tear_down_links(sdata, to_free, rem);
257 ieee80211_set_vif_links_bitmaps(sdata, new_links, dormant_links);
259 /* tell the driver */
260 ret = drv_change_vif_links(sdata->local, sdata,
261 old_links & old_active,
262 new_links & sdata->vif.active_links,
268 memcpy(sdata->link, old_data, sizeof(old_data));
269 memcpy(sdata->vif.link_conf, old, sizeof(old));
270 ieee80211_set_vif_links_bitmaps(sdata, old_links, dormant_links);
271 /* and free (only) the newly allocated links */
272 memset(to_free, 0, sizeof(links));
276 /* use deflink/bss_conf again if and only if there are no more links */
277 use_deflink = new_links == 0;
281 /* if we failed during allocation, only free all */
282 for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
283 kfree(links[link_id]);
284 links[link_id] = NULL;
288 ieee80211_link_init(sdata, -1, &sdata->deflink,
289 &sdata->vif.bss_conf);
293 int ieee80211_vif_set_links(struct ieee80211_sub_if_data *sdata,
294 u16 new_links, u16 dormant_links)
296 struct link_container *links[IEEE80211_MLD_MAX_NUM_LINKS];
299 ret = ieee80211_vif_update_links(sdata, links, new_links,
301 ieee80211_free_links(sdata, links);
306 void ieee80211_vif_clear_links(struct ieee80211_sub_if_data *sdata)
308 struct link_container *links[IEEE80211_MLD_MAX_NUM_LINKS];
311 * The locking here is different because when we free links
312 * in the station case we need to be able to cancel_work_sync()
313 * something that also takes the lock.
317 ieee80211_vif_update_links(sdata, links, 0, 0);
320 ieee80211_free_links(sdata, links);
323 static int _ieee80211_set_active_links(struct ieee80211_sub_if_data *sdata,
326 struct ieee80211_bss_conf *link_confs[IEEE80211_MLD_MAX_NUM_LINKS];
327 struct ieee80211_local *local = sdata->local;
328 u16 old_active = sdata->vif.active_links;
329 unsigned long rem = old_active & ~active_links;
330 unsigned long add = active_links & ~old_active;
331 struct sta_info *sta;
332 unsigned int link_id;
335 if (!ieee80211_sdata_running(sdata))
338 if (sdata->vif.type != NL80211_IFTYPE_STATION)
341 if (active_links & ~ieee80211_vif_usable_links(&sdata->vif))
345 if (old_active == active_links)
348 for (i = 0; i < IEEE80211_MLD_MAX_NUM_LINKS; i++)
349 link_confs[i] = sdata_dereference(sdata->vif.link_conf[i],
353 sdata->vif.active_links |= active_links;
354 ret = drv_change_vif_links(local, sdata,
356 sdata->vif.active_links,
359 sdata->vif.active_links = old_active;
364 for_each_set_bit(link_id, &rem, IEEE80211_MLD_MAX_NUM_LINKS) {
365 struct ieee80211_link_data *link;
367 link = sdata_dereference(sdata->link[link_id], sdata);
369 /* FIXME: kill TDLS connections on the link */
371 ieee80211_link_release_channel(link);
374 list_for_each_entry(sta, &local->sta_list, list) {
375 if (sdata != sta->sdata)
378 /* this is very temporary, but do it anyway */
379 __ieee80211_sta_recalc_aggregates(sta,
380 old_active | active_links);
382 ret = drv_change_sta_links(local, sdata, &sta->sta,
384 old_active | active_links);
388 ret = ieee80211_key_switch_links(sdata, rem, add);
391 list_for_each_entry(sta, &local->sta_list, list) {
392 if (sdata != sta->sdata)
395 __ieee80211_sta_recalc_aggregates(sta, active_links);
397 ret = drv_change_sta_links(local, sdata, &sta->sta,
398 old_active | active_links,
403 * Do it again, just in case - the driver might very
404 * well have called ieee80211_sta_recalc_aggregates()
405 * from there when filling in the new links, which
406 * would set it wrong since the vif's active links are
407 * not switched yet...
409 __ieee80211_sta_recalc_aggregates(sta, active_links);
412 for_each_set_bit(link_id, &add, IEEE80211_MLD_MAX_NUM_LINKS) {
413 struct ieee80211_link_data *link;
415 link = sdata_dereference(sdata->link[link_id], sdata);
417 ret = ieee80211_link_use_channel(link, &link->conf->chandef,
418 IEEE80211_CHANCTX_SHARED);
421 ieee80211_mgd_set_link_qos_params(link);
422 ieee80211_link_info_change_notify(sdata, link,
423 BSS_CHANGED_ERP_CTS_PROT |
424 BSS_CHANGED_ERP_PREAMBLE |
425 BSS_CHANGED_ERP_SLOT |
427 BSS_CHANGED_BASIC_RATES |
431 BSS_CHANGED_TXPOWER |
432 BSS_CHANGED_BANDWIDTH |
434 BSS_CHANGED_HE_OBSS_PD |
435 BSS_CHANGED_HE_BSS_COLOR);
438 old_active = sdata->vif.active_links;
439 sdata->vif.active_links = active_links;
442 ret = drv_change_vif_links(local, sdata, old_active,
443 active_links, link_confs);
450 int __ieee80211_set_active_links(struct ieee80211_vif *vif, u16 active_links)
452 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
453 struct ieee80211_local *local = sdata->local;
457 sdata_assert_lock(sdata);
458 mutex_lock(&local->sta_mtx);
459 mutex_lock(&local->mtx);
460 mutex_lock(&local->key_mtx);
461 old_active = sdata->vif.active_links;
462 if (old_active & active_links) {
464 * if there's at least one link that stays active across
465 * the change then switch to it (to those) first, and
466 * then enable the additional links
468 ret = _ieee80211_set_active_links(sdata,
469 old_active & active_links);
471 ret = _ieee80211_set_active_links(sdata, active_links);
473 /* otherwise switch directly */
474 ret = _ieee80211_set_active_links(sdata, active_links);
476 mutex_unlock(&local->key_mtx);
477 mutex_unlock(&local->mtx);
478 mutex_unlock(&local->sta_mtx);
483 int ieee80211_set_active_links(struct ieee80211_vif *vif, u16 active_links)
485 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
489 ret = __ieee80211_set_active_links(vif, active_links);
494 EXPORT_SYMBOL_GPL(ieee80211_set_active_links);
496 void ieee80211_set_active_links_async(struct ieee80211_vif *vif,
499 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
501 if (!ieee80211_sdata_running(sdata))
504 if (sdata->vif.type != NL80211_IFTYPE_STATION)
507 if (active_links & ~ieee80211_vif_usable_links(&sdata->vif))
511 if (sdata->vif.active_links == active_links)
514 sdata->desired_active_links = active_links;
515 schedule_work(&sdata->activate_links_work);
517 EXPORT_SYMBOL_GPL(ieee80211_set_active_links_async);