xref: /linux/net/mac80211/link.c (revision b1a54551dd9ed5ef1763b97b35a0999ca002b95c)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * MLO link handling
4  *
5  * Copyright (C) 2022-2023 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 	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 		/* FIXME: kill TDLS connections on the link */
358 
359 		ieee80211_link_release_channel(link);
360 	}
361 
362 	list_for_each_entry(sta, &local->sta_list, list) {
363 		if (sdata != sta->sdata)
364 			continue;
365 
366 		/* this is very temporary, but do it anyway */
367 		__ieee80211_sta_recalc_aggregates(sta,
368 						  old_active | active_links);
369 
370 		ret = drv_change_sta_links(local, sdata, &sta->sta,
371 					   old_active,
372 					   old_active | active_links);
373 		WARN_ON_ONCE(ret);
374 	}
375 
376 	ret = ieee80211_key_switch_links(sdata, rem, add);
377 	WARN_ON_ONCE(ret);
378 
379 	list_for_each_entry(sta, &local->sta_list, list) {
380 		if (sdata != sta->sdata)
381 			continue;
382 
383 		__ieee80211_sta_recalc_aggregates(sta, active_links);
384 
385 		ret = drv_change_sta_links(local, sdata, &sta->sta,
386 					   old_active | active_links,
387 					   active_links);
388 		WARN_ON_ONCE(ret);
389 
390 		/*
391 		 * Do it again, just in case - the driver might very
392 		 * well have called ieee80211_sta_recalc_aggregates()
393 		 * from there when filling in the new links, which
394 		 * would set it wrong since the vif's active links are
395 		 * not switched yet...
396 		 */
397 		__ieee80211_sta_recalc_aggregates(sta, active_links);
398 	}
399 
400 	for_each_set_bit(link_id, &add, IEEE80211_MLD_MAX_NUM_LINKS) {
401 		struct ieee80211_link_data *link;
402 
403 		link = sdata_dereference(sdata->link[link_id], sdata);
404 
405 		ret = ieee80211_link_use_channel(link, &link->conf->chandef,
406 						 IEEE80211_CHANCTX_SHARED);
407 		WARN_ON_ONCE(ret);
408 
409 		ieee80211_mgd_set_link_qos_params(link);
410 		ieee80211_link_info_change_notify(sdata, link,
411 						  BSS_CHANGED_ERP_CTS_PROT |
412 						  BSS_CHANGED_ERP_PREAMBLE |
413 						  BSS_CHANGED_ERP_SLOT |
414 						  BSS_CHANGED_HT |
415 						  BSS_CHANGED_BASIC_RATES |
416 						  BSS_CHANGED_BSSID |
417 						  BSS_CHANGED_CQM |
418 						  BSS_CHANGED_QOS |
419 						  BSS_CHANGED_TXPOWER |
420 						  BSS_CHANGED_BANDWIDTH |
421 						  BSS_CHANGED_TWT |
422 						  BSS_CHANGED_HE_OBSS_PD |
423 						  BSS_CHANGED_HE_BSS_COLOR);
424 	}
425 
426 	old_active = sdata->vif.active_links;
427 	sdata->vif.active_links = active_links;
428 
429 	if (rem) {
430 		ret = drv_change_vif_links(local, sdata, old_active,
431 					   active_links, link_confs);
432 		WARN_ON_ONCE(ret);
433 	}
434 
435 	return 0;
436 }
437 
438 int ieee80211_set_active_links(struct ieee80211_vif *vif, u16 active_links)
439 {
440 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
441 	struct ieee80211_local *local = sdata->local;
442 	u16 old_active;
443 	int ret;
444 
445 	lockdep_assert_wiphy(local->hw.wiphy);
446 
447 	if (!drv_can_activate_links(local, sdata, active_links))
448 		return -EINVAL;
449 
450 	old_active = sdata->vif.active_links;
451 	if (old_active & active_links) {
452 		/*
453 		 * if there's at least one link that stays active across
454 		 * the change then switch to it (to those) first, and
455 		 * then enable the additional links
456 		 */
457 		ret = _ieee80211_set_active_links(sdata,
458 						  old_active & active_links);
459 		if (!ret)
460 			ret = _ieee80211_set_active_links(sdata, active_links);
461 	} else {
462 		/* otherwise switch directly */
463 		ret = _ieee80211_set_active_links(sdata, active_links);
464 	}
465 
466 	return ret;
467 }
468 EXPORT_SYMBOL_GPL(ieee80211_set_active_links);
469 
470 void ieee80211_set_active_links_async(struct ieee80211_vif *vif,
471 				      u16 active_links)
472 {
473 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
474 
475 	if (!ieee80211_sdata_running(sdata))
476 		return;
477 
478 	if (sdata->vif.type != NL80211_IFTYPE_STATION)
479 		return;
480 
481 	if (active_links & ~ieee80211_vif_usable_links(&sdata->vif))
482 		return;
483 
484 	/* nothing to do */
485 	if (sdata->vif.active_links == active_links)
486 		return;
487 
488 	sdata->desired_active_links = active_links;
489 	wiphy_work_queue(sdata->local->hw.wiphy, &sdata->activate_links_work);
490 }
491 EXPORT_SYMBOL_GPL(ieee80211_set_active_links_async);
492