xref: /linux/net/mac80211/link.c (revision 89713ce5518eda6b370c7a17edbcab4f97a39f68)
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 	wiphy_delayed_work_init(&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 	wiphy_delayed_work_cancel(link->sdata->local->hw.wiphy,
76 				  &link->color_collision_detect_work);
77 	wiphy_work_cancel(link->sdata->local->hw.wiphy,
78 			  &link->color_change_finalize_work);
79 	wiphy_work_cancel(link->sdata->local->hw.wiphy,
80 			  &link->csa.finalize_work);
81 
82 	if (link->sdata->wdev.links[link->link_id].cac_started) {
83 		wiphy_delayed_work_cancel(link->sdata->local->hw.wiphy,
84 					  &link->dfs_cac_timer_work);
85 		cfg80211_cac_event(link->sdata->dev,
86 				   &link->conf->chanreq.oper,
87 				   NL80211_RADAR_CAC_ABORTED,
88 				   GFP_KERNEL, link->link_id);
89 	}
90 
91 	ieee80211_link_release_channel(link);
92 }
93 
94 struct link_container {
95 	struct ieee80211_link_data data;
96 	struct ieee80211_bss_conf conf;
97 };
98 
99 static void ieee80211_tear_down_links(struct ieee80211_sub_if_data *sdata,
100 				      struct link_container **links, u16 mask)
101 {
102 	struct ieee80211_link_data *link;
103 	LIST_HEAD(keys);
104 	unsigned int link_id;
105 
106 	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
107 		if (!(mask & BIT(link_id)))
108 			continue;
109 		link = &links[link_id]->data;
110 		if (link_id == 0 && !link)
111 			link = &sdata->deflink;
112 		if (WARN_ON(!link))
113 			continue;
114 		ieee80211_remove_link_keys(link, &keys);
115 		ieee80211_link_debugfs_remove(link);
116 		ieee80211_link_stop(link);
117 	}
118 
119 	synchronize_rcu();
120 
121 	ieee80211_free_key_list(sdata->local, &keys);
122 }
123 
124 static void ieee80211_free_links(struct ieee80211_sub_if_data *sdata,
125 				 struct link_container **links)
126 {
127 	unsigned int link_id;
128 
129 	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++)
130 		kfree(links[link_id]);
131 }
132 
133 static int ieee80211_check_dup_link_addrs(struct ieee80211_sub_if_data *sdata)
134 {
135 	unsigned int i, j;
136 
137 	for (i = 0; i < IEEE80211_MLD_MAX_NUM_LINKS; i++) {
138 		struct ieee80211_link_data *link1;
139 
140 		link1 = sdata_dereference(sdata->link[i], sdata);
141 		if (!link1)
142 			continue;
143 		for (j = i + 1; j < IEEE80211_MLD_MAX_NUM_LINKS; j++) {
144 			struct ieee80211_link_data *link2;
145 
146 			link2 = sdata_dereference(sdata->link[j], sdata);
147 			if (!link2)
148 				continue;
149 
150 			if (ether_addr_equal(link1->conf->addr,
151 					     link2->conf->addr))
152 				return -EALREADY;
153 		}
154 	}
155 
156 	return 0;
157 }
158 
159 static void ieee80211_set_vif_links_bitmaps(struct ieee80211_sub_if_data *sdata,
160 					    u16 valid_links, u16 dormant_links)
161 {
162 	sdata->vif.valid_links = valid_links;
163 	sdata->vif.dormant_links = dormant_links;
164 
165 	if (!valid_links ||
166 	    WARN((~valid_links & dormant_links) ||
167 		 !(valid_links & ~dormant_links),
168 		 "Invalid links: valid=0x%x, dormant=0x%x",
169 		 valid_links, dormant_links)) {
170 		sdata->vif.active_links = 0;
171 		sdata->vif.dormant_links = 0;
172 		return;
173 	}
174 
175 	switch (sdata->vif.type) {
176 	case NL80211_IFTYPE_AP:
177 		/* in an AP all links are always active */
178 		sdata->vif.active_links = valid_links;
179 
180 		/* AP links are not expected to be disabled */
181 		WARN_ON(dormant_links);
182 		break;
183 	case NL80211_IFTYPE_STATION:
184 		if (sdata->vif.active_links)
185 			break;
186 		sdata->vif.active_links = valid_links & ~dormant_links;
187 		WARN_ON(hweight16(sdata->vif.active_links) > 1);
188 		break;
189 	default:
190 		WARN_ON(1);
191 	}
192 }
193 
194 static int ieee80211_vif_update_links(struct ieee80211_sub_if_data *sdata,
195 				      struct link_container **to_free,
196 				      u16 new_links, u16 dormant_links)
197 {
198 	u16 old_links = sdata->vif.valid_links;
199 	u16 old_active = sdata->vif.active_links;
200 	unsigned long add = new_links & ~old_links;
201 	unsigned long rem = old_links & ~new_links;
202 	unsigned int link_id;
203 	int ret;
204 	struct link_container *links[IEEE80211_MLD_MAX_NUM_LINKS] = {}, *link;
205 	struct ieee80211_bss_conf *old[IEEE80211_MLD_MAX_NUM_LINKS];
206 	struct ieee80211_link_data *old_data[IEEE80211_MLD_MAX_NUM_LINKS];
207 	bool use_deflink = old_links == 0; /* set for error case */
208 
209 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
210 
211 	memset(to_free, 0, sizeof(links));
212 
213 	if (old_links == new_links && dormant_links == sdata->vif.dormant_links)
214 		return 0;
215 
216 	/* if there were no old links, need to clear the pointers to deflink */
217 	if (!old_links)
218 		rem |= BIT(0);
219 
220 	/* allocate new link structures first */
221 	for_each_set_bit(link_id, &add, IEEE80211_MLD_MAX_NUM_LINKS) {
222 		link = kzalloc(sizeof(*link), GFP_KERNEL);
223 		if (!link) {
224 			ret = -ENOMEM;
225 			goto free;
226 		}
227 		links[link_id] = link;
228 	}
229 
230 	/* keep track of the old pointers for the driver */
231 	BUILD_BUG_ON(sizeof(old) != sizeof(sdata->vif.link_conf));
232 	memcpy(old, sdata->vif.link_conf, sizeof(old));
233 	/* and for us in error cases */
234 	BUILD_BUG_ON(sizeof(old_data) != sizeof(sdata->link));
235 	memcpy(old_data, sdata->link, sizeof(old_data));
236 
237 	/* grab old links to free later */
238 	for_each_set_bit(link_id, &rem, IEEE80211_MLD_MAX_NUM_LINKS) {
239 		if (rcu_access_pointer(sdata->link[link_id]) != &sdata->deflink) {
240 			/*
241 			 * we must have allocated the data through this path so
242 			 * we know we can free both at the same time
243 			 */
244 			to_free[link_id] = container_of(rcu_access_pointer(sdata->link[link_id]),
245 							typeof(*links[link_id]),
246 							data);
247 		}
248 
249 		RCU_INIT_POINTER(sdata->link[link_id], NULL);
250 		RCU_INIT_POINTER(sdata->vif.link_conf[link_id], NULL);
251 	}
252 
253 	if (!old_links)
254 		ieee80211_debugfs_recreate_netdev(sdata, true);
255 
256 	/* link them into data structures */
257 	for_each_set_bit(link_id, &add, IEEE80211_MLD_MAX_NUM_LINKS) {
258 		WARN_ON(!use_deflink &&
259 			rcu_access_pointer(sdata->link[link_id]) == &sdata->deflink);
260 
261 		link = links[link_id];
262 		ieee80211_link_init(sdata, link_id, &link->data, &link->conf);
263 		ieee80211_link_setup(&link->data);
264 	}
265 
266 	if (new_links == 0)
267 		ieee80211_link_init(sdata, -1, &sdata->deflink,
268 				    &sdata->vif.bss_conf);
269 
270 	ret = ieee80211_check_dup_link_addrs(sdata);
271 	if (!ret) {
272 		/* for keys we will not be able to undo this */
273 		ieee80211_tear_down_links(sdata, to_free, rem);
274 
275 		ieee80211_set_vif_links_bitmaps(sdata, new_links, dormant_links);
276 
277 		/* tell the driver */
278 		ret = drv_change_vif_links(sdata->local, sdata,
279 					   old_links & old_active,
280 					   new_links & sdata->vif.active_links,
281 					   old);
282 		if (!new_links)
283 			ieee80211_debugfs_recreate_netdev(sdata, false);
284 	}
285 
286 	if (ret) {
287 		/* restore config */
288 		memcpy(sdata->link, old_data, sizeof(old_data));
289 		memcpy(sdata->vif.link_conf, old, sizeof(old));
290 		ieee80211_set_vif_links_bitmaps(sdata, old_links, dormant_links);
291 		/* and free (only) the newly allocated links */
292 		memset(to_free, 0, sizeof(links));
293 		goto free;
294 	}
295 
296 	/* use deflink/bss_conf again if and only if there are no more links */
297 	use_deflink = new_links == 0;
298 
299 	goto deinit;
300 free:
301 	/* if we failed during allocation, only free all */
302 	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
303 		kfree(links[link_id]);
304 		links[link_id] = NULL;
305 	}
306 deinit:
307 	if (use_deflink)
308 		ieee80211_link_init(sdata, -1, &sdata->deflink,
309 				    &sdata->vif.bss_conf);
310 	return ret;
311 }
312 
313 int ieee80211_vif_set_links(struct ieee80211_sub_if_data *sdata,
314 			    u16 new_links, u16 dormant_links)
315 {
316 	struct link_container *links[IEEE80211_MLD_MAX_NUM_LINKS];
317 	int ret;
318 
319 	ret = ieee80211_vif_update_links(sdata, links, new_links,
320 					 dormant_links);
321 	ieee80211_free_links(sdata, links);
322 
323 	return ret;
324 }
325 
326 static int _ieee80211_set_active_links(struct ieee80211_sub_if_data *sdata,
327 				       u16 active_links)
328 {
329 	struct ieee80211_bss_conf *link_confs[IEEE80211_MLD_MAX_NUM_LINKS];
330 	struct ieee80211_local *local = sdata->local;
331 	u16 old_active = sdata->vif.active_links;
332 	unsigned long rem = old_active & ~active_links;
333 	unsigned long add = active_links & ~old_active;
334 	struct sta_info *sta;
335 	unsigned int link_id;
336 	int ret, i;
337 
338 	if (!ieee80211_sdata_running(sdata))
339 		return -ENETDOWN;
340 
341 	if (sdata->vif.type != NL80211_IFTYPE_STATION)
342 		return -EINVAL;
343 
344 	if (active_links & ~ieee80211_vif_usable_links(&sdata->vif))
345 		return -EINVAL;
346 
347 	/* nothing to do */
348 	if (old_active == active_links)
349 		return 0;
350 
351 	for (i = 0; i < IEEE80211_MLD_MAX_NUM_LINKS; i++)
352 		link_confs[i] = sdata_dereference(sdata->vif.link_conf[i],
353 						  sdata);
354 
355 	if (add) {
356 		sdata->vif.active_links |= active_links;
357 		ret = drv_change_vif_links(local, sdata,
358 					   old_active,
359 					   sdata->vif.active_links,
360 					   link_confs);
361 		if (ret) {
362 			sdata->vif.active_links = old_active;
363 			return ret;
364 		}
365 	}
366 
367 	for_each_set_bit(link_id, &rem, IEEE80211_MLD_MAX_NUM_LINKS) {
368 		struct ieee80211_link_data *link;
369 
370 		link = sdata_dereference(sdata->link[link_id], sdata);
371 
372 		ieee80211_teardown_tdls_peers(link);
373 
374 		__ieee80211_link_release_channel(link, true);
375 
376 		/*
377 		 * If CSA is (still) active while the link is deactivated,
378 		 * just schedule the channel switch work for the time we
379 		 * had previously calculated, and we'll take the process
380 		 * from there.
381 		 */
382 		if (link->conf->csa_active)
383 			wiphy_delayed_work_queue(local->hw.wiphy,
384 						 &link->u.mgd.csa.switch_work,
385 						 link->u.mgd.csa.time -
386 						 jiffies);
387 	}
388 
389 	list_for_each_entry(sta, &local->sta_list, list) {
390 		if (sdata != sta->sdata)
391 			continue;
392 
393 		/* this is very temporary, but do it anyway */
394 		__ieee80211_sta_recalc_aggregates(sta,
395 						  old_active | active_links);
396 
397 		ret = drv_change_sta_links(local, sdata, &sta->sta,
398 					   old_active,
399 					   old_active | active_links);
400 		WARN_ON_ONCE(ret);
401 	}
402 
403 	ret = ieee80211_key_switch_links(sdata, rem, add);
404 	WARN_ON_ONCE(ret);
405 
406 	list_for_each_entry(sta, &local->sta_list, list) {
407 		if (sdata != sta->sdata)
408 			continue;
409 
410 		__ieee80211_sta_recalc_aggregates(sta, active_links);
411 
412 		ret = drv_change_sta_links(local, sdata, &sta->sta,
413 					   old_active | active_links,
414 					   active_links);
415 		WARN_ON_ONCE(ret);
416 
417 		/*
418 		 * Do it again, just in case - the driver might very
419 		 * well have called ieee80211_sta_recalc_aggregates()
420 		 * from there when filling in the new links, which
421 		 * would set it wrong since the vif's active links are
422 		 * not switched yet...
423 		 */
424 		__ieee80211_sta_recalc_aggregates(sta, active_links);
425 	}
426 
427 	for_each_set_bit(link_id, &add, IEEE80211_MLD_MAX_NUM_LINKS) {
428 		struct ieee80211_link_data *link;
429 
430 		link = sdata_dereference(sdata->link[link_id], sdata);
431 
432 		/*
433 		 * This call really should not fail. Unfortunately, it appears
434 		 * that this may happen occasionally with some drivers. Should
435 		 * it happen, we are stuck in a bad place as going backwards is
436 		 * not really feasible.
437 		 *
438 		 * So lets just tell link_use_channel that it must not fail to
439 		 * assign the channel context (from mac80211's perspective) and
440 		 * assume the driver is going to trigger a recovery flow if it
441 		 * had a failure.
442 		 * That really is not great nor guaranteed to work. But at least
443 		 * the internal mac80211 state remains consistent and there is
444 		 * a chance that we can recover.
445 		 */
446 		ret = _ieee80211_link_use_channel(link,
447 						  &link->conf->chanreq,
448 						  IEEE80211_CHANCTX_SHARED,
449 						  true);
450 		WARN_ON_ONCE(ret);
451 
452 		ieee80211_mgd_set_link_qos_params(link);
453 		ieee80211_link_info_change_notify(sdata, link,
454 						  BSS_CHANGED_ERP_CTS_PROT |
455 						  BSS_CHANGED_ERP_PREAMBLE |
456 						  BSS_CHANGED_ERP_SLOT |
457 						  BSS_CHANGED_HT |
458 						  BSS_CHANGED_BASIC_RATES |
459 						  BSS_CHANGED_BSSID |
460 						  BSS_CHANGED_CQM |
461 						  BSS_CHANGED_QOS |
462 						  BSS_CHANGED_TXPOWER |
463 						  BSS_CHANGED_BANDWIDTH |
464 						  BSS_CHANGED_TWT |
465 						  BSS_CHANGED_HE_OBSS_PD |
466 						  BSS_CHANGED_HE_BSS_COLOR);
467 	}
468 
469 	old_active = sdata->vif.active_links;
470 	sdata->vif.active_links = active_links;
471 
472 	if (rem) {
473 		ret = drv_change_vif_links(local, sdata, old_active,
474 					   active_links, link_confs);
475 		WARN_ON_ONCE(ret);
476 	}
477 
478 	return 0;
479 }
480 
481 int ieee80211_set_active_links(struct ieee80211_vif *vif, u16 active_links)
482 {
483 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
484 	struct ieee80211_local *local = sdata->local;
485 	u16 old_active;
486 	int ret;
487 
488 	lockdep_assert_wiphy(local->hw.wiphy);
489 
490 	if (WARN_ON(!active_links))
491 		return -EINVAL;
492 
493 	old_active = sdata->vif.active_links;
494 	if (old_active == active_links)
495 		return 0;
496 
497 	if (!drv_can_activate_links(local, sdata, active_links))
498 		return -EINVAL;
499 
500 	if (old_active & active_links) {
501 		/*
502 		 * if there's at least one link that stays active across
503 		 * the change then switch to it (to those) first, and
504 		 * then enable the additional links
505 		 */
506 		ret = _ieee80211_set_active_links(sdata,
507 						  old_active & active_links);
508 		if (!ret)
509 			ret = _ieee80211_set_active_links(sdata, active_links);
510 	} else {
511 		/* otherwise switch directly */
512 		ret = _ieee80211_set_active_links(sdata, active_links);
513 	}
514 
515 	return ret;
516 }
517 EXPORT_SYMBOL_GPL(ieee80211_set_active_links);
518 
519 void ieee80211_set_active_links_async(struct ieee80211_vif *vif,
520 				      u16 active_links)
521 {
522 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
523 
524 	if (WARN_ON(!active_links))
525 		return;
526 
527 	if (!ieee80211_sdata_running(sdata))
528 		return;
529 
530 	if (sdata->vif.type != NL80211_IFTYPE_STATION)
531 		return;
532 
533 	if (active_links & ~ieee80211_vif_usable_links(&sdata->vif))
534 		return;
535 
536 	/* nothing to do */
537 	if (sdata->vif.active_links == active_links)
538 		return;
539 
540 	sdata->desired_active_links = active_links;
541 	wiphy_work_queue(sdata->local->hw.wiphy, &sdata->activate_links_work);
542 }
543 EXPORT_SYMBOL_GPL(ieee80211_set_active_links_async);
544