1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2022-2024 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 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
);
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 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
;
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
)))
108 link
= &links
[link_id
]->data
;
109 if (link_id
== 0 && !link
)
110 link
= &sdata
->deflink
;
113 ieee80211_remove_link_keys(link
, &keys
);
114 ieee80211_link_debugfs_remove(link
);
115 ieee80211_link_stop(link
);
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
)
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
);
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
);
149 if (ether_addr_equal(link1
->conf
->addr
,
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
;
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;
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
);
182 case NL80211_IFTYPE_STATION
:
183 if (sdata
->vif
.active_links
)
185 sdata
->vif
.active_links
= valid_links
& ~dormant_links
;
186 WARN_ON(hweight16(sdata
->vif
.active_links
) > 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
;
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
)
215 /* if there were no old links, need to clear the pointers to deflink */
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
);
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
]),
248 RCU_INIT_POINTER(sdata
->link
[link_id
], NULL
);
249 RCU_INIT_POINTER(sdata
->vif
.link_conf
[link_id
], NULL
);
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
);
266 ieee80211_link_init(sdata
, -1, &sdata
->deflink
,
267 &sdata
->vif
.bss_conf
);
269 ret
= ieee80211_check_dup_link_addrs(sdata
);
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
,
282 ieee80211_debugfs_recreate_netdev(sdata
, false);
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
));
295 /* use deflink/bss_conf again if and only if there are no more links */
296 use_deflink
= new_links
== 0;
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
;
307 ieee80211_link_init(sdata
, -1, &sdata
->deflink
,
308 &sdata
->vif
.bss_conf
);
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
];
318 ret
= ieee80211_vif_update_links(sdata
, links
, new_links
,
320 ieee80211_free_links(sdata
, links
);
325 static int _ieee80211_set_active_links(struct ieee80211_sub_if_data
*sdata
,
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
;
337 if (!ieee80211_sdata_running(sdata
))
340 if (sdata
->vif
.type
!= NL80211_IFTYPE_STATION
)
343 if (active_links
& ~ieee80211_vif_usable_links(&sdata
->vif
))
347 if (old_active
== active_links
)
350 for (i
= 0; i
< IEEE80211_MLD_MAX_NUM_LINKS
; i
++)
351 link_confs
[i
] = sdata_dereference(sdata
->vif
.link_conf
[i
],
355 sdata
->vif
.active_links
|= active_links
;
356 ret
= drv_change_vif_links(local
, sdata
,
358 sdata
->vif
.active_links
,
361 sdata
->vif
.active_links
= old_active
;
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
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
-
388 list_for_each_entry(sta
, &local
->sta_list
, list
) {
389 if (sdata
!= sta
->sdata
)
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
,
398 old_active
| active_links
);
402 ret
= ieee80211_key_switch_links(sdata
, rem
, add
);
405 list_for_each_entry(sta
, &local
->sta_list
, list
) {
406 if (sdata
!= sta
->sdata
)
409 __ieee80211_sta_recalc_aggregates(sta
, active_links
);
411 ret
= drv_change_sta_links(local
, sdata
, &sta
->sta
,
412 old_active
| active_links
,
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
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
,
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
|
457 BSS_CHANGED_BASIC_RATES
|
461 BSS_CHANGED_TXPOWER
|
462 BSS_CHANGED_BANDWIDTH
|
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
;
472 ret
= drv_change_vif_links(local
, sdata
, old_active
,
473 active_links
, link_confs
);
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
;
487 lockdep_assert_wiphy(local
->hw
.wiphy
);
489 if (WARN_ON(!active_links
))
492 old_active
= sdata
->vif
.active_links
;
493 if (old_active
== active_links
)
496 if (!drv_can_activate_links(local
, sdata
, active_links
))
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
);
508 ret
= _ieee80211_set_active_links(sdata
, active_links
);
510 /* otherwise switch directly */
511 ret
= _ieee80211_set_active_links(sdata
, active_links
);
516 EXPORT_SYMBOL_GPL(ieee80211_set_active_links
);
518 void ieee80211_set_active_links_async(struct ieee80211_vif
*vif
,
521 struct ieee80211_sub_if_data
*sdata
= vif_to_sdata(vif
);
523 if (WARN_ON(!active_links
))
526 if (!ieee80211_sdata_running(sdata
))
529 if (sdata
->vif
.type
!= NL80211_IFTYPE_STATION
)
532 if (active_links
& ~ieee80211_vif_usable_links(&sdata
->vif
))
536 if (sdata
->vif
.active_links
== active_links
)
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
);