xref: /linux/net/mac80211/link.c (revision c7546e2c3cb739a3c1a2f5acaf9bb629d401afe5)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * MLO link handling
4  *
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"
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 	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);
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 	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);
80 
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);
88 	}
89 
90 	ieee80211_link_release_channel(link);
91 }
92 
93 struct link_container {
94 	struct ieee80211_link_data data;
95 	struct ieee80211_bss_conf conf;
96 };
97 
98 static void ieee80211_tear_down_links(struct ieee80211_sub_if_data *sdata,
99 				      struct link_container **links, u16 mask)
100 {
101 	struct ieee80211_link_data *link;
102 	LIST_HEAD(keys);
103 	unsigned int link_id;
104 
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);
116 	}
117 
118 	synchronize_rcu();
119 
120 	ieee80211_free_key_list(sdata->local, &keys);
121 }
122 
123 static void ieee80211_free_links(struct ieee80211_sub_if_data *sdata,
124 				 struct link_container **links)
125 {
126 	unsigned int link_id;
127 
128 	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++)
129 		kfree(links[link_id]);
130 }
131 
132 static int ieee80211_check_dup_link_addrs(struct ieee80211_sub_if_data *sdata)
133 {
134 	unsigned int i, j;
135 
136 	for (i = 0; i < IEEE80211_MLD_MAX_NUM_LINKS; i++) {
137 		struct ieee80211_link_data *link1;
138 
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;
144 
145 			link2 = sdata_dereference(sdata->link[j], sdata);
146 			if (!link2)
147 				continue;
148 
149 			if (ether_addr_equal(link1->conf->addr,
150 					     link2->conf->addr))
151 				return -EALREADY;
152 		}
153 	}
154 
155 	return 0;
156 }
157 
158 static void ieee80211_set_vif_links_bitmaps(struct ieee80211_sub_if_data *sdata,
159 					    u16 valid_links, u16 dormant_links)
160 {
161 	sdata->vif.valid_links = valid_links;
162 	sdata->vif.dormant_links = dormant_links;
163 
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;
172 	}
173 
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;
178 
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);
190 	}
191 }
192 
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)
196 {
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 */
207 
208 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
209 
210 	memset(to_free, 0, sizeof(links));
211 
212 	if (old_links == new_links && dormant_links == sdata->vif.dormant_links)
213 		return 0;
214 
215 	/* if there were no old links, need to clear the pointers to deflink */
216 	if (!old_links)
217 		rem |= BIT(0);
218 
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;
225 		}
226 		links[link_id] = link;
227 	}
228 
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));
235 
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) {
239 			/*
240 			 * we must have allocated the data through this path so
241 			 * we know we can free both at the same time
242 			 */
243 			to_free[link_id] = container_of(rcu_access_pointer(sdata->link[link_id]),
244 							typeof(*links[link_id]),
245 							data);
246 		}
247 
248 		RCU_INIT_POINTER(sdata->link[link_id], NULL);
249 		RCU_INIT_POINTER(sdata->vif.link_conf[link_id], NULL);
250 	}
251 
252 	if (!old_links)
253 		ieee80211_debugfs_recreate_netdev(sdata, true);
254 
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);
259 
260 		link = links[link_id];
261 		ieee80211_link_init(sdata, link_id, &link->data, &link->conf);
262 		ieee80211_link_setup(&link->data);
263 	}
264 
265 	if (new_links == 0)
266 		ieee80211_link_init(sdata, -1, &sdata->deflink,
267 				    &sdata->vif.bss_conf);
268 
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);
273 
274 		ieee80211_set_vif_links_bitmaps(sdata, new_links, dormant_links);
275 
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);
283 	}
284 
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;
293 	}
294 
295 	/* use deflink/bss_conf again if and only if there are no more links */
296 	use_deflink = new_links == 0;
297 
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;
304 	}
305 deinit:
306 	if (use_deflink)
307 		ieee80211_link_init(sdata, -1, &sdata->deflink,
308 				    &sdata->vif.bss_conf);
309 	return ret;
310 }
311 
312 int ieee80211_vif_set_links(struct ieee80211_sub_if_data *sdata,
313 			    u16 new_links, u16 dormant_links)
314 {
315 	struct link_container *links[IEEE80211_MLD_MAX_NUM_LINKS];
316 	int ret;
317 
318 	ret = ieee80211_vif_update_links(sdata, links, new_links,
319 					 dormant_links);
320 	ieee80211_free_links(sdata, links);
321 
322 	return ret;
323 }
324 
325 static int _ieee80211_set_active_links(struct ieee80211_sub_if_data *sdata,
326 				       u16 active_links)
327 {
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;
336 
337 	if (!ieee80211_sdata_running(sdata))
338 		return -ENETDOWN;
339 
340 	if (sdata->vif.type != NL80211_IFTYPE_STATION)
341 		return -EINVAL;
342 
343 	if (active_links & ~ieee80211_vif_usable_links(&sdata->vif))
344 		return -EINVAL;
345 
346 	/* nothing to do */
347 	if (old_active == active_links)
348 		return 0;
349 
350 	for (i = 0; i < IEEE80211_MLD_MAX_NUM_LINKS; i++)
351 		link_confs[i] = sdata_dereference(sdata->vif.link_conf[i],
352 						  sdata);
353 
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;
363 		}
364 	}
365 
366 	for_each_set_bit(link_id, &rem, IEEE80211_MLD_MAX_NUM_LINKS) {
367 		struct ieee80211_link_data *link;
368 
369 		link = sdata_dereference(sdata->link[link_id], sdata);
370 
371 		ieee80211_teardown_tdls_peers(link);
372 
373 		__ieee80211_link_release_channel(link, true);
374 
375 		/*
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.
380 		 */
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);
386 	}
387 
388 	list_for_each_entry(sta, &local->sta_list, list) {
389 		if (sdata != sta->sdata)
390 			continue;
391 
392 		/* this is very temporary, but do it anyway */
393 		__ieee80211_sta_recalc_aggregates(sta,
394 						  old_active | active_links);
395 
396 		ret = drv_change_sta_links(local, sdata, &sta->sta,
397 					   old_active,
398 					   old_active | active_links);
399 		WARN_ON_ONCE(ret);
400 	}
401 
402 	ret = ieee80211_key_switch_links(sdata, rem, add);
403 	WARN_ON_ONCE(ret);
404 
405 	list_for_each_entry(sta, &local->sta_list, list) {
406 		if (sdata != sta->sdata)
407 			continue;
408 
409 		__ieee80211_sta_recalc_aggregates(sta, active_links);
410 
411 		ret = drv_change_sta_links(local, sdata, &sta->sta,
412 					   old_active | active_links,
413 					   active_links);
414 		WARN_ON_ONCE(ret);
415 
416 		/*
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...
422 		 */
423 		__ieee80211_sta_recalc_aggregates(sta, active_links);
424 	}
425 
426 	for_each_set_bit(link_id, &add, IEEE80211_MLD_MAX_NUM_LINKS) {
427 		struct ieee80211_link_data *link;
428 
429 		link = sdata_dereference(sdata->link[link_id], sdata);
430 
431 		/*
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.
436 		 *
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.
444 		 */
445 		ret = _ieee80211_link_use_channel(link,
446 						  &link->conf->chanreq,
447 						  IEEE80211_CHANCTX_SHARED,
448 						  true);
449 		WARN_ON_ONCE(ret);
450 
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);
466 	}
467 
468 	old_active = sdata->vif.active_links;
469 	sdata->vif.active_links = active_links;
470 
471 	if (rem) {
472 		ret = drv_change_vif_links(local, sdata, old_active,
473 					   active_links, link_confs);
474 		WARN_ON_ONCE(ret);
475 	}
476 
477 	return 0;
478 }
479 
480 int ieee80211_set_active_links(struct ieee80211_vif *vif, u16 active_links)
481 {
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;
486 
487 	lockdep_assert_wiphy(local->hw.wiphy);
488 
489 	if (WARN_ON(!active_links))
490 		return -EINVAL;
491 
492 	old_active = sdata->vif.active_links;
493 	if (old_active == active_links)
494 		return 0;
495 
496 	if (!drv_can_activate_links(local, sdata, active_links))
497 		return -EINVAL;
498 
499 	if (old_active & active_links) {
500 		/*
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
504 		 */
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);
512 	}
513 
514 	return ret;
515 }
516 EXPORT_SYMBOL_GPL(ieee80211_set_active_links);
517 
518 void ieee80211_set_active_links_async(struct ieee80211_vif *vif,
519 				      u16 active_links)
520 {
521 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
522 
523 	if (WARN_ON(!active_links))
524 		return;
525 
526 	if (!ieee80211_sdata_running(sdata))
527 		return;
528 
529 	if (sdata->vif.type != NL80211_IFTYPE_STATION)
530 		return;
531 
532 	if (active_links & ~ieee80211_vif_usable_links(&sdata->vif))
533 		return;
534 
535 	/* nothing to do */
536 	if (sdata->vif.active_links == active_links)
537 		return;
538 
539 	sdata->desired_active_links = active_links;
540 	wiphy_work_queue(sdata->local->hw.wiphy, &sdata->activate_links_work);
541 }
542 EXPORT_SYMBOL_GPL(ieee80211_set_active_links_async);
543