xref: /linux/net/mac80211/link.c (revision 2a52ca7c98960aafb0eca9ef96b2d0c932171357)
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 
49 	if (!deflink) {
50 		switch (sdata->vif.type) {
51 		case NL80211_IFTYPE_AP:
52 			ether_addr_copy(link_conf->addr,
53 					sdata->wdev.links[link_id].addr);
54 			link_conf->bssid = link_conf->addr;
55 			WARN_ON(!(sdata->wdev.valid_links & BIT(link_id)));
56 			break;
57 		case NL80211_IFTYPE_STATION:
58 			/* station sets the bssid in ieee80211_mgd_setup_link */
59 			break;
60 		default:
61 			WARN_ON(1);
62 		}
63 
64 		ieee80211_link_debugfs_add(link);
65 	}
66 }
67 
68 void ieee80211_link_stop(struct ieee80211_link_data *link)
69 {
70 	if (link->sdata->vif.type == NL80211_IFTYPE_STATION)
71 		ieee80211_mgd_stop_link(link);
72 
73 	cancel_delayed_work_sync(&link->color_collision_detect_work);
74 	wiphy_work_cancel(link->sdata->local->hw.wiphy,
75 			  &link->csa.finalize_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 valid_links, u16 dormant_links)
146 {
147 	sdata->vif.valid_links = valid_links;
148 	sdata->vif.dormant_links = dormant_links;
149 
150 	if (!valid_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;
157 		return;
158 	}
159 
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;
164 
165 		/* AP links are not expected to be disabled */
166 		WARN_ON(dormant_links);
167 		break;
168 	case NL80211_IFTYPE_STATION:
169 		if (sdata->vif.active_links)
170 			break;
171 		sdata->vif.active_links = valid_links & ~dormant_links;
172 		WARN_ON(hweight16(sdata->vif.active_links) > 1);
173 		break;
174 	default:
175 		WARN_ON(1);
176 	}
177 }
178 
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)
182 {
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;
188 	int ret;
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 */
193 
194 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
195 
196 	memset(to_free, 0, sizeof(links));
197 
198 	if (old_links == new_links && dormant_links == sdata->vif.dormant_links)
199 		return 0;
200 
201 	/* if there were no old links, need to clear the pointers to deflink */
202 	if (!old_links)
203 		rem |= BIT(0);
204 
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);
208 		if (!link) {
209 			ret = -ENOMEM;
210 			goto free;
211 		}
212 		links[link_id] = link;
213 	}
214 
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));
221 
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) {
225 			/*
226 			 * we must have allocated the data through this path so
227 			 * we know we can free both at the same time
228 			 */
229 			to_free[link_id] = container_of(rcu_access_pointer(sdata->link[link_id]),
230 							typeof(*links[link_id]),
231 							data);
232 		}
233 
234 		RCU_INIT_POINTER(sdata->link[link_id], NULL);
235 		RCU_INIT_POINTER(sdata->vif.link_conf[link_id], NULL);
236 	}
237 
238 	if (!old_links)
239 		ieee80211_debugfs_recreate_netdev(sdata, true);
240 
241 	/* link them into data structures */
242 	for_each_set_bit(link_id, &add, IEEE80211_MLD_MAX_NUM_LINKS) {
243 		WARN_ON(!use_deflink &&
244 			rcu_access_pointer(sdata->link[link_id]) == &sdata->deflink);
245 
246 		link = links[link_id];
247 		ieee80211_link_init(sdata, link_id, &link->data, &link->conf);
248 		ieee80211_link_setup(&link->data);
249 	}
250 
251 	if (new_links == 0)
252 		ieee80211_link_init(sdata, -1, &sdata->deflink,
253 				    &sdata->vif.bss_conf);
254 
255 	ret = ieee80211_check_dup_link_addrs(sdata);
256 	if (!ret) {
257 		/* for keys we will not be able to undo this */
258 		ieee80211_tear_down_links(sdata, to_free, rem);
259 
260 		ieee80211_set_vif_links_bitmaps(sdata, new_links, dormant_links);
261 
262 		/* tell the driver */
263 		ret = drv_change_vif_links(sdata->local, sdata,
264 					   old_links & old_active,
265 					   new_links & sdata->vif.active_links,
266 					   old);
267 		if (!new_links)
268 			ieee80211_debugfs_recreate_netdev(sdata, false);
269 	}
270 
271 	if (ret) {
272 		/* restore config */
273 		memcpy(sdata->link, old_data, sizeof(old_data));
274 		memcpy(sdata->vif.link_conf, old, sizeof(old));
275 		ieee80211_set_vif_links_bitmaps(sdata, old_links, dormant_links);
276 		/* and free (only) the newly allocated links */
277 		memset(to_free, 0, sizeof(links));
278 		goto free;
279 	}
280 
281 	/* use deflink/bss_conf again if and only if there are no more links */
282 	use_deflink = new_links == 0;
283 
284 	goto deinit;
285 free:
286 	/* if we failed during allocation, only free all */
287 	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
288 		kfree(links[link_id]);
289 		links[link_id] = NULL;
290 	}
291 deinit:
292 	if (use_deflink)
293 		ieee80211_link_init(sdata, -1, &sdata->deflink,
294 				    &sdata->vif.bss_conf);
295 	return ret;
296 }
297 
298 int ieee80211_vif_set_links(struct ieee80211_sub_if_data *sdata,
299 			    u16 new_links, u16 dormant_links)
300 {
301 	struct link_container *links[IEEE80211_MLD_MAX_NUM_LINKS];
302 	int ret;
303 
304 	ret = ieee80211_vif_update_links(sdata, links, new_links,
305 					 dormant_links);
306 	ieee80211_free_links(sdata, links);
307 
308 	return ret;
309 }
310 
311 static int _ieee80211_set_active_links(struct ieee80211_sub_if_data *sdata,
312 				       u16 active_links)
313 {
314 	struct ieee80211_bss_conf *link_confs[IEEE80211_MLD_MAX_NUM_LINKS];
315 	struct ieee80211_local *local = sdata->local;
316 	u16 old_active = sdata->vif.active_links;
317 	unsigned long rem = old_active & ~active_links;
318 	unsigned long add = active_links & ~old_active;
319 	struct sta_info *sta;
320 	unsigned int link_id;
321 	int ret, i;
322 
323 	if (!ieee80211_sdata_running(sdata))
324 		return -ENETDOWN;
325 
326 	if (sdata->vif.type != NL80211_IFTYPE_STATION)
327 		return -EINVAL;
328 
329 	if (active_links & ~ieee80211_vif_usable_links(&sdata->vif))
330 		return -EINVAL;
331 
332 	/* nothing to do */
333 	if (old_active == active_links)
334 		return 0;
335 
336 	for (i = 0; i < IEEE80211_MLD_MAX_NUM_LINKS; i++)
337 		link_confs[i] = sdata_dereference(sdata->vif.link_conf[i],
338 						  sdata);
339 
340 	if (add) {
341 		sdata->vif.active_links |= active_links;
342 		ret = drv_change_vif_links(local, sdata,
343 					   old_active,
344 					   sdata->vif.active_links,
345 					   link_confs);
346 		if (ret) {
347 			sdata->vif.active_links = old_active;
348 			return ret;
349 		}
350 	}
351 
352 	for_each_set_bit(link_id, &rem, IEEE80211_MLD_MAX_NUM_LINKS) {
353 		struct ieee80211_link_data *link;
354 
355 		link = sdata_dereference(sdata->link[link_id], sdata);
356 
357 		ieee80211_teardown_tdls_peers(link);
358 
359 		__ieee80211_link_release_channel(link, true);
360 
361 		/*
362 		 * If CSA is (still) active while the link is deactivated,
363 		 * just schedule the channel switch work for the time we
364 		 * had previously calculated, and we'll take the process
365 		 * from there.
366 		 */
367 		if (link->conf->csa_active)
368 			wiphy_delayed_work_queue(local->hw.wiphy,
369 						 &link->u.mgd.csa.switch_work,
370 						 link->u.mgd.csa.time -
371 						 jiffies);
372 	}
373 
374 	list_for_each_entry(sta, &local->sta_list, list) {
375 		if (sdata != sta->sdata)
376 			continue;
377 
378 		/* this is very temporary, but do it anyway */
379 		__ieee80211_sta_recalc_aggregates(sta,
380 						  old_active | active_links);
381 
382 		ret = drv_change_sta_links(local, sdata, &sta->sta,
383 					   old_active,
384 					   old_active | active_links);
385 		WARN_ON_ONCE(ret);
386 	}
387 
388 	ret = ieee80211_key_switch_links(sdata, rem, add);
389 	WARN_ON_ONCE(ret);
390 
391 	list_for_each_entry(sta, &local->sta_list, list) {
392 		if (sdata != sta->sdata)
393 			continue;
394 
395 		__ieee80211_sta_recalc_aggregates(sta, active_links);
396 
397 		ret = drv_change_sta_links(local, sdata, &sta->sta,
398 					   old_active | active_links,
399 					   active_links);
400 		WARN_ON_ONCE(ret);
401 
402 		/*
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...
408 		 */
409 		__ieee80211_sta_recalc_aggregates(sta, active_links);
410 	}
411 
412 	for_each_set_bit(link_id, &add, IEEE80211_MLD_MAX_NUM_LINKS) {
413 		struct ieee80211_link_data *link;
414 
415 		link = sdata_dereference(sdata->link[link_id], sdata);
416 
417 		/*
418 		 * This call really should not fail. Unfortunately, it appears
419 		 * that this may happen occasionally with some drivers. Should
420 		 * it happen, we are stuck in a bad place as going backwards is
421 		 * not really feasible.
422 		 *
423 		 * So lets just tell link_use_channel that it must not fail to
424 		 * assign the channel context (from mac80211's perspective) and
425 		 * assume the driver is going to trigger a recovery flow if it
426 		 * had a failure.
427 		 * That really is not great nor guaranteed to work. But at least
428 		 * the internal mac80211 state remains consistent and there is
429 		 * a chance that we can recover.
430 		 */
431 		ret = _ieee80211_link_use_channel(link,
432 						  &link->conf->chanreq,
433 						  IEEE80211_CHANCTX_SHARED,
434 						  true);
435 		WARN_ON_ONCE(ret);
436 
437 		ieee80211_mgd_set_link_qos_params(link);
438 		ieee80211_link_info_change_notify(sdata, link,
439 						  BSS_CHANGED_ERP_CTS_PROT |
440 						  BSS_CHANGED_ERP_PREAMBLE |
441 						  BSS_CHANGED_ERP_SLOT |
442 						  BSS_CHANGED_HT |
443 						  BSS_CHANGED_BASIC_RATES |
444 						  BSS_CHANGED_BSSID |
445 						  BSS_CHANGED_CQM |
446 						  BSS_CHANGED_QOS |
447 						  BSS_CHANGED_TXPOWER |
448 						  BSS_CHANGED_BANDWIDTH |
449 						  BSS_CHANGED_TWT |
450 						  BSS_CHANGED_HE_OBSS_PD |
451 						  BSS_CHANGED_HE_BSS_COLOR);
452 	}
453 
454 	old_active = sdata->vif.active_links;
455 	sdata->vif.active_links = active_links;
456 
457 	if (rem) {
458 		ret = drv_change_vif_links(local, sdata, old_active,
459 					   active_links, link_confs);
460 		WARN_ON_ONCE(ret);
461 	}
462 
463 	return 0;
464 }
465 
466 int ieee80211_set_active_links(struct ieee80211_vif *vif, u16 active_links)
467 {
468 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
469 	struct ieee80211_local *local = sdata->local;
470 	u16 old_active;
471 	int ret;
472 
473 	lockdep_assert_wiphy(local->hw.wiphy);
474 
475 	if (WARN_ON(!active_links))
476 		return -EINVAL;
477 
478 	old_active = sdata->vif.active_links;
479 	if (old_active == active_links)
480 		return 0;
481 
482 	if (!drv_can_activate_links(local, sdata, active_links))
483 		return -EINVAL;
484 
485 	if (old_active & active_links) {
486 		/*
487 		 * if there's at least one link that stays active across
488 		 * the change then switch to it (to those) first, and
489 		 * then enable the additional links
490 		 */
491 		ret = _ieee80211_set_active_links(sdata,
492 						  old_active & active_links);
493 		if (!ret)
494 			ret = _ieee80211_set_active_links(sdata, active_links);
495 	} else {
496 		/* otherwise switch directly */
497 		ret = _ieee80211_set_active_links(sdata, active_links);
498 	}
499 
500 	return ret;
501 }
502 EXPORT_SYMBOL_GPL(ieee80211_set_active_links);
503 
504 void ieee80211_set_active_links_async(struct ieee80211_vif *vif,
505 				      u16 active_links)
506 {
507 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
508 
509 	if (WARN_ON(!active_links))
510 		return;
511 
512 	if (!ieee80211_sdata_running(sdata))
513 		return;
514 
515 	if (sdata->vif.type != NL80211_IFTYPE_STATION)
516 		return;
517 
518 	if (active_links & ~ieee80211_vif_usable_links(&sdata->vif))
519 		return;
520 
521 	/* nothing to do */
522 	if (sdata->vif.active_links == active_links)
523 		return;
524 
525 	sdata->desired_active_links = active_links;
526 	wiphy_work_queue(sdata->local->hw.wiphy, &sdata->activate_links_work);
527 }
528 EXPORT_SYMBOL_GPL(ieee80211_set_active_links_async);
529