drm/tests: hdmi: Fix memory leaks in drm_display_mode_from_cea_vic()
[drm/drm-misc.git] / net / mac80211 / link.c
blob0bbac64d5fa0132b748f775256d9c93b55ecc07d
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * MLO link handling
5 * Copyright (C) 2022-2024 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"
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,
22 int link_id,
23 struct ieee80211_link_data *link,
24 struct ieee80211_bss_conf *link_conf)
26 bool deflink = link_id < 0;
28 if (link_id < 0)
29 link_id = 0;
31 rcu_assign_pointer(sdata->vif.link_conf[link_id], link_conf);
32 rcu_assign_pointer(sdata->link[link_id], link);
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;
40 wiphy_work_init(&link->csa.finalize_work,
41 ieee80211_csa_finalize_work);
42 wiphy_work_init(&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 wiphy_delayed_work_init(&link->dfs_cac_timer_work,
49 ieee80211_dfs_cac_timer_work);
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);
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 wiphy_work_cancel(link->sdata->local->hw.wiphy,
77 &link->color_change_finalize_work);
78 wiphy_work_cancel(link->sdata->local->hw.wiphy,
79 &link->csa.finalize_work);
81 if (link->sdata->wdev.links[link->link_id].cac_started) {
82 wiphy_delayed_work_cancel(link->sdata->local->hw.wiphy,
83 &link->dfs_cac_timer_work);
84 cfg80211_cac_event(link->sdata->dev,
85 &link->conf->chanreq.oper,
86 NL80211_RADAR_CAC_ABORTED,
87 GFP_KERNEL, link->link_id);
90 ieee80211_link_release_channel(link);
93 struct link_container {
94 struct ieee80211_link_data data;
95 struct ieee80211_bss_conf conf;
98 static void ieee80211_tear_down_links(struct ieee80211_sub_if_data *sdata,
99 struct link_container **links, u16 mask)
101 struct ieee80211_link_data *link;
102 LIST_HEAD(keys);
103 unsigned int link_id;
105 for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
106 if (!(mask & BIT(link_id)))
107 continue;
108 link = &links[link_id]->data;
109 if (link_id == 0 && !link)
110 link = &sdata->deflink;
111 if (WARN_ON(!link))
112 continue;
113 ieee80211_remove_link_keys(link, &keys);
114 ieee80211_link_debugfs_remove(link);
115 ieee80211_link_stop(link);
118 synchronize_rcu();
120 ieee80211_free_key_list(sdata->local, &keys);
123 static void ieee80211_free_links(struct ieee80211_sub_if_data *sdata,
124 struct link_container **links)
126 unsigned int link_id;
128 for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++)
129 kfree(links[link_id]);
132 static int ieee80211_check_dup_link_addrs(struct ieee80211_sub_if_data *sdata)
134 unsigned int i, j;
136 for (i = 0; i < IEEE80211_MLD_MAX_NUM_LINKS; i++) {
137 struct ieee80211_link_data *link1;
139 link1 = sdata_dereference(sdata->link[i], sdata);
140 if (!link1)
141 continue;
142 for (j = i + 1; j < IEEE80211_MLD_MAX_NUM_LINKS; j++) {
143 struct ieee80211_link_data *link2;
145 link2 = sdata_dereference(sdata->link[j], sdata);
146 if (!link2)
147 continue;
149 if (ether_addr_equal(link1->conf->addr,
150 link2->conf->addr))
151 return -EALREADY;
155 return 0;
158 static void ieee80211_set_vif_links_bitmaps(struct ieee80211_sub_if_data *sdata,
159 u16 valid_links, u16 dormant_links)
161 sdata->vif.valid_links = valid_links;
162 sdata->vif.dormant_links = dormant_links;
164 if (!valid_links ||
165 WARN((~valid_links & dormant_links) ||
166 !(valid_links & ~dormant_links),
167 "Invalid links: valid=0x%x, dormant=0x%x",
168 valid_links, dormant_links)) {
169 sdata->vif.active_links = 0;
170 sdata->vif.dormant_links = 0;
171 return;
174 switch (sdata->vif.type) {
175 case NL80211_IFTYPE_AP:
176 /* in an AP all links are always active */
177 sdata->vif.active_links = valid_links;
179 /* AP links are not expected to be disabled */
180 WARN_ON(dormant_links);
181 break;
182 case NL80211_IFTYPE_STATION:
183 if (sdata->vif.active_links)
184 break;
185 sdata->vif.active_links = valid_links & ~dormant_links;
186 WARN_ON(hweight16(sdata->vif.active_links) > 1);
187 break;
188 default:
189 WARN_ON(1);
193 static int ieee80211_vif_update_links(struct ieee80211_sub_if_data *sdata,
194 struct link_container **to_free,
195 u16 new_links, u16 dormant_links)
197 u16 old_links = sdata->vif.valid_links;
198 u16 old_active = sdata->vif.active_links;
199 unsigned long add = new_links & ~old_links;
200 unsigned long rem = old_links & ~new_links;
201 unsigned int link_id;
202 int ret;
203 struct link_container *links[IEEE80211_MLD_MAX_NUM_LINKS] = {}, *link;
204 struct ieee80211_bss_conf *old[IEEE80211_MLD_MAX_NUM_LINKS];
205 struct ieee80211_link_data *old_data[IEEE80211_MLD_MAX_NUM_LINKS];
206 bool use_deflink = old_links == 0; /* set for error case */
208 lockdep_assert_wiphy(sdata->local->hw.wiphy);
210 memset(to_free, 0, sizeof(links));
212 if (old_links == new_links && dormant_links == sdata->vif.dormant_links)
213 return 0;
215 /* if there were no old links, need to clear the pointers to deflink */
216 if (!old_links)
217 rem |= BIT(0);
219 /* allocate new link structures first */
220 for_each_set_bit(link_id, &add, IEEE80211_MLD_MAX_NUM_LINKS) {
221 link = kzalloc(sizeof(*link), GFP_KERNEL);
222 if (!link) {
223 ret = -ENOMEM;
224 goto free;
226 links[link_id] = link;
229 /* keep track of the old pointers for the driver */
230 BUILD_BUG_ON(sizeof(old) != sizeof(sdata->vif.link_conf));
231 memcpy(old, sdata->vif.link_conf, sizeof(old));
232 /* and for us in error cases */
233 BUILD_BUG_ON(sizeof(old_data) != sizeof(sdata->link));
234 memcpy(old_data, sdata->link, sizeof(old_data));
236 /* grab old links to free later */
237 for_each_set_bit(link_id, &rem, IEEE80211_MLD_MAX_NUM_LINKS) {
238 if (rcu_access_pointer(sdata->link[link_id]) != &sdata->deflink) {
240 * we must have allocated the data through this path so
241 * we know we can free both at the same time
243 to_free[link_id] = container_of(rcu_access_pointer(sdata->link[link_id]),
244 typeof(*links[link_id]),
245 data);
248 RCU_INIT_POINTER(sdata->link[link_id], NULL);
249 RCU_INIT_POINTER(sdata->vif.link_conf[link_id], NULL);
252 if (!old_links)
253 ieee80211_debugfs_recreate_netdev(sdata, true);
255 /* link them into data structures */
256 for_each_set_bit(link_id, &add, IEEE80211_MLD_MAX_NUM_LINKS) {
257 WARN_ON(!use_deflink &&
258 rcu_access_pointer(sdata->link[link_id]) == &sdata->deflink);
260 link = links[link_id];
261 ieee80211_link_init(sdata, link_id, &link->data, &link->conf);
262 ieee80211_link_setup(&link->data);
265 if (new_links == 0)
266 ieee80211_link_init(sdata, -1, &sdata->deflink,
267 &sdata->vif.bss_conf);
269 ret = ieee80211_check_dup_link_addrs(sdata);
270 if (!ret) {
271 /* for keys we will not be able to undo this */
272 ieee80211_tear_down_links(sdata, to_free, rem);
274 ieee80211_set_vif_links_bitmaps(sdata, new_links, dormant_links);
276 /* tell the driver */
277 ret = drv_change_vif_links(sdata->local, sdata,
278 old_links & old_active,
279 new_links & sdata->vif.active_links,
280 old);
281 if (!new_links)
282 ieee80211_debugfs_recreate_netdev(sdata, false);
285 if (ret) {
286 /* restore config */
287 memcpy(sdata->link, old_data, sizeof(old_data));
288 memcpy(sdata->vif.link_conf, old, sizeof(old));
289 ieee80211_set_vif_links_bitmaps(sdata, old_links, dormant_links);
290 /* and free (only) the newly allocated links */
291 memset(to_free, 0, sizeof(links));
292 goto free;
295 /* use deflink/bss_conf again if and only if there are no more links */
296 use_deflink = new_links == 0;
298 goto deinit;
299 free:
300 /* if we failed during allocation, only free all */
301 for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
302 kfree(links[link_id]);
303 links[link_id] = NULL;
305 deinit:
306 if (use_deflink)
307 ieee80211_link_init(sdata, -1, &sdata->deflink,
308 &sdata->vif.bss_conf);
309 return ret;
312 int ieee80211_vif_set_links(struct ieee80211_sub_if_data *sdata,
313 u16 new_links, u16 dormant_links)
315 struct link_container *links[IEEE80211_MLD_MAX_NUM_LINKS];
316 int ret;
318 ret = ieee80211_vif_update_links(sdata, links, new_links,
319 dormant_links);
320 ieee80211_free_links(sdata, links);
322 return ret;
325 static int _ieee80211_set_active_links(struct ieee80211_sub_if_data *sdata,
326 u16 active_links)
328 struct ieee80211_bss_conf *link_confs[IEEE80211_MLD_MAX_NUM_LINKS];
329 struct ieee80211_local *local = sdata->local;
330 u16 old_active = sdata->vif.active_links;
331 unsigned long rem = old_active & ~active_links;
332 unsigned long add = active_links & ~old_active;
333 struct sta_info *sta;
334 unsigned int link_id;
335 int ret, i;
337 if (!ieee80211_sdata_running(sdata))
338 return -ENETDOWN;
340 if (sdata->vif.type != NL80211_IFTYPE_STATION)
341 return -EINVAL;
343 if (active_links & ~ieee80211_vif_usable_links(&sdata->vif))
344 return -EINVAL;
346 /* nothing to do */
347 if (old_active == active_links)
348 return 0;
350 for (i = 0; i < IEEE80211_MLD_MAX_NUM_LINKS; i++)
351 link_confs[i] = sdata_dereference(sdata->vif.link_conf[i],
352 sdata);
354 if (add) {
355 sdata->vif.active_links |= active_links;
356 ret = drv_change_vif_links(local, sdata,
357 old_active,
358 sdata->vif.active_links,
359 link_confs);
360 if (ret) {
361 sdata->vif.active_links = old_active;
362 return ret;
366 for_each_set_bit(link_id, &rem, IEEE80211_MLD_MAX_NUM_LINKS) {
367 struct ieee80211_link_data *link;
369 link = sdata_dereference(sdata->link[link_id], sdata);
371 ieee80211_teardown_tdls_peers(link);
373 __ieee80211_link_release_channel(link, true);
376 * If CSA is (still) active while the link is deactivated,
377 * just schedule the channel switch work for the time we
378 * had previously calculated, and we'll take the process
379 * from there.
381 if (link->conf->csa_active)
382 wiphy_delayed_work_queue(local->hw.wiphy,
383 &link->u.mgd.csa.switch_work,
384 link->u.mgd.csa.time -
385 jiffies);
388 list_for_each_entry(sta, &local->sta_list, list) {
389 if (sdata != sta->sdata)
390 continue;
392 /* this is very temporary, but do it anyway */
393 __ieee80211_sta_recalc_aggregates(sta,
394 old_active | active_links);
396 ret = drv_change_sta_links(local, sdata, &sta->sta,
397 old_active,
398 old_active | active_links);
399 WARN_ON_ONCE(ret);
402 ret = ieee80211_key_switch_links(sdata, rem, add);
403 WARN_ON_ONCE(ret);
405 list_for_each_entry(sta, &local->sta_list, list) {
406 if (sdata != sta->sdata)
407 continue;
409 __ieee80211_sta_recalc_aggregates(sta, active_links);
411 ret = drv_change_sta_links(local, sdata, &sta->sta,
412 old_active | active_links,
413 active_links);
414 WARN_ON_ONCE(ret);
417 * Do it again, just in case - the driver might very
418 * well have called ieee80211_sta_recalc_aggregates()
419 * from there when filling in the new links, which
420 * would set it wrong since the vif's active links are
421 * not switched yet...
423 __ieee80211_sta_recalc_aggregates(sta, active_links);
426 for_each_set_bit(link_id, &add, IEEE80211_MLD_MAX_NUM_LINKS) {
427 struct ieee80211_link_data *link;
429 link = sdata_dereference(sdata->link[link_id], sdata);
432 * This call really should not fail. Unfortunately, it appears
433 * that this may happen occasionally with some drivers. Should
434 * it happen, we are stuck in a bad place as going backwards is
435 * not really feasible.
437 * So lets just tell link_use_channel that it must not fail to
438 * assign the channel context (from mac80211's perspective) and
439 * assume the driver is going to trigger a recovery flow if it
440 * had a failure.
441 * That really is not great nor guaranteed to work. But at least
442 * the internal mac80211 state remains consistent and there is
443 * a chance that we can recover.
445 ret = _ieee80211_link_use_channel(link,
446 &link->conf->chanreq,
447 IEEE80211_CHANCTX_SHARED,
448 true);
449 WARN_ON_ONCE(ret);
451 ieee80211_mgd_set_link_qos_params(link);
452 ieee80211_link_info_change_notify(sdata, link,
453 BSS_CHANGED_ERP_CTS_PROT |
454 BSS_CHANGED_ERP_PREAMBLE |
455 BSS_CHANGED_ERP_SLOT |
456 BSS_CHANGED_HT |
457 BSS_CHANGED_BASIC_RATES |
458 BSS_CHANGED_BSSID |
459 BSS_CHANGED_CQM |
460 BSS_CHANGED_QOS |
461 BSS_CHANGED_TXPOWER |
462 BSS_CHANGED_BANDWIDTH |
463 BSS_CHANGED_TWT |
464 BSS_CHANGED_HE_OBSS_PD |
465 BSS_CHANGED_HE_BSS_COLOR);
468 old_active = sdata->vif.active_links;
469 sdata->vif.active_links = active_links;
471 if (rem) {
472 ret = drv_change_vif_links(local, sdata, old_active,
473 active_links, link_confs);
474 WARN_ON_ONCE(ret);
477 return 0;
480 int ieee80211_set_active_links(struct ieee80211_vif *vif, u16 active_links)
482 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
483 struct ieee80211_local *local = sdata->local;
484 u16 old_active;
485 int ret;
487 lockdep_assert_wiphy(local->hw.wiphy);
489 if (WARN_ON(!active_links))
490 return -EINVAL;
492 old_active = sdata->vif.active_links;
493 if (old_active == active_links)
494 return 0;
496 if (!drv_can_activate_links(local, sdata, active_links))
497 return -EINVAL;
499 if (old_active & active_links) {
501 * if there's at least one link that stays active across
502 * the change then switch to it (to those) first, and
503 * then enable the additional links
505 ret = _ieee80211_set_active_links(sdata,
506 old_active & active_links);
507 if (!ret)
508 ret = _ieee80211_set_active_links(sdata, active_links);
509 } else {
510 /* otherwise switch directly */
511 ret = _ieee80211_set_active_links(sdata, active_links);
514 return ret;
516 EXPORT_SYMBOL_GPL(ieee80211_set_active_links);
518 void ieee80211_set_active_links_async(struct ieee80211_vif *vif,
519 u16 active_links)
521 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
523 if (WARN_ON(!active_links))
524 return;
526 if (!ieee80211_sdata_running(sdata))
527 return;
529 if (sdata->vif.type != NL80211_IFTYPE_STATION)
530 return;
532 if (active_links & ~ieee80211_vif_usable_links(&sdata->vif))
533 return;
535 /* nothing to do */
536 if (sdata->vif.active_links == active_links)
537 return;
539 sdata->desired_active_links = active_links;
540 wiphy_work_queue(sdata->local->hw.wiphy, &sdata->activate_links_work);
542 EXPORT_SYMBOL_GPL(ieee80211_set_active_links_async);