• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
ieee80211_link_setup(struct ieee80211_link_data * link)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 
ieee80211_link_init(struct ieee80211_sub_if_data * sdata,int link_id,struct ieee80211_link_data * link,struct ieee80211_bss_conf * link_conf)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 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
32 		struct ieee80211_sub_if_data *ap_bss;
33 		struct ieee80211_bss_conf *ap_bss_conf;
34 
35 		ap_bss = container_of(sdata->bss,
36 				      struct ieee80211_sub_if_data, u.ap);
37 		ap_bss_conf = sdata_dereference(ap_bss->vif.link_conf[link_id],
38 						ap_bss);
39 		memcpy(link_conf, ap_bss_conf, sizeof(*link_conf));
40 	}
41 
42 	link->sdata = sdata;
43 	link->link_id = link_id;
44 	link->conf = link_conf;
45 	link_conf->link_id = link_id;
46 	link_conf->vif = &sdata->vif;
47 
48 	INIT_WORK(&link->csa_finalize_work,
49 		  ieee80211_csa_finalize_work);
50 	INIT_WORK(&link->color_change_finalize_work,
51 		  ieee80211_color_change_finalize_work);
52 	INIT_DELAYED_WORK(&link->color_collision_detect_work,
53 			  ieee80211_color_collision_detection_work);
54 	INIT_LIST_HEAD(&link->assigned_chanctx_list);
55 	INIT_LIST_HEAD(&link->reserved_chanctx_list);
56 	INIT_DELAYED_WORK(&link->dfs_cac_timer_work,
57 			  ieee80211_dfs_cac_timer_work);
58 
59 	if (!deflink) {
60 		switch (sdata->vif.type) {
61 		case NL80211_IFTYPE_AP:
62 			ether_addr_copy(link_conf->addr,
63 					sdata->wdev.links[link_id].addr);
64 			link_conf->bssid = link_conf->addr;
65 			WARN_ON(!(sdata->wdev.valid_links & BIT(link_id)));
66 			break;
67 		case NL80211_IFTYPE_STATION:
68 			/* station sets the bssid in ieee80211_mgd_setup_link */
69 			break;
70 		default:
71 			WARN_ON(1);
72 		}
73 
74 		ieee80211_link_debugfs_add(link);
75 	}
76 
77 	rcu_assign_pointer(sdata->vif.link_conf[link_id], link_conf);
78 	rcu_assign_pointer(sdata->link[link_id], link);
79 }
80 
ieee80211_link_stop(struct ieee80211_link_data * link)81 void ieee80211_link_stop(struct ieee80211_link_data *link)
82 {
83 	if (link->sdata->vif.type == NL80211_IFTYPE_STATION)
84 		ieee80211_mgd_stop_link(link);
85 
86 	cancel_delayed_work_sync(&link->color_collision_detect_work);
87 	ieee80211_link_release_channel(link);
88 }
89 
90 struct link_container {
91 	struct ieee80211_link_data data;
92 	struct ieee80211_bss_conf conf;
93 };
94 
ieee80211_tear_down_links(struct ieee80211_sub_if_data * sdata,struct link_container ** links,u16 mask)95 static void ieee80211_tear_down_links(struct ieee80211_sub_if_data *sdata,
96 				      struct link_container **links, u16 mask)
97 {
98 	struct ieee80211_link_data *link;
99 	LIST_HEAD(keys);
100 	unsigned int link_id;
101 
102 	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
103 		if (!(mask & BIT(link_id)))
104 			continue;
105 		link = &links[link_id]->data;
106 		if (link_id == 0 && !link)
107 			link = &sdata->deflink;
108 		if (WARN_ON(!link))
109 			continue;
110 		ieee80211_remove_link_keys(link, &keys);
111 		ieee80211_link_debugfs_remove(link);
112 		ieee80211_link_stop(link);
113 	}
114 
115 	synchronize_rcu();
116 
117 	ieee80211_free_key_list(sdata->local, &keys);
118 }
119 
ieee80211_free_links(struct ieee80211_sub_if_data * sdata,struct link_container ** links)120 static void ieee80211_free_links(struct ieee80211_sub_if_data *sdata,
121 				 struct link_container **links)
122 {
123 	unsigned int link_id;
124 
125 	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++)
126 		kfree(links[link_id]);
127 }
128 
ieee80211_check_dup_link_addrs(struct ieee80211_sub_if_data * sdata)129 static int ieee80211_check_dup_link_addrs(struct ieee80211_sub_if_data *sdata)
130 {
131 	unsigned int i, j;
132 
133 	for (i = 0; i < IEEE80211_MLD_MAX_NUM_LINKS; i++) {
134 		struct ieee80211_link_data *link1;
135 
136 		link1 = sdata_dereference(sdata->link[i], sdata);
137 		if (!link1)
138 			continue;
139 		for (j = i + 1; j < IEEE80211_MLD_MAX_NUM_LINKS; j++) {
140 			struct ieee80211_link_data *link2;
141 
142 			link2 = sdata_dereference(sdata->link[j], sdata);
143 			if (!link2)
144 				continue;
145 
146 			if (ether_addr_equal(link1->conf->addr,
147 					     link2->conf->addr))
148 				return -EALREADY;
149 		}
150 	}
151 
152 	return 0;
153 }
154 
ieee80211_set_vif_links_bitmaps(struct ieee80211_sub_if_data * sdata,u16 valid_links,u16 dormant_links)155 static void ieee80211_set_vif_links_bitmaps(struct ieee80211_sub_if_data *sdata,
156 					    u16 valid_links, u16 dormant_links)
157 {
158 	sdata->vif.valid_links = valid_links;
159 	sdata->vif.dormant_links = dormant_links;
160 
161 	if (!valid_links ||
162 	    WARN((~valid_links & dormant_links) ||
163 		 !(valid_links & ~dormant_links),
164 		 "Invalid links: valid=0x%x, dormant=0x%x",
165 		 valid_links, dormant_links)) {
166 		sdata->vif.active_links = 0;
167 		sdata->vif.dormant_links = 0;
168 		return;
169 	}
170 
171 	switch (sdata->vif.type) {
172 	case NL80211_IFTYPE_AP:
173 		/* in an AP all links are always active */
174 		sdata->vif.active_links = valid_links;
175 
176 		/* AP links are not expected to be disabled */
177 		WARN_ON(dormant_links);
178 		break;
179 	case NL80211_IFTYPE_STATION:
180 		if (sdata->vif.active_links)
181 			break;
182 		sdata->vif.active_links = valid_links & ~dormant_links;
183 		WARN_ON(hweight16(sdata->vif.active_links) > 1);
184 		break;
185 	default:
186 		WARN_ON(1);
187 	}
188 }
189 
ieee80211_vif_update_links(struct ieee80211_sub_if_data * sdata,struct link_container ** to_free,u16 new_links,u16 dormant_links)190 static int ieee80211_vif_update_links(struct ieee80211_sub_if_data *sdata,
191 				      struct link_container **to_free,
192 				      u16 new_links, u16 dormant_links)
193 {
194 	u16 old_links = sdata->vif.valid_links;
195 	u16 old_active = sdata->vif.active_links;
196 	unsigned long add = new_links & ~old_links;
197 	unsigned long rem = old_links & ~new_links;
198 	unsigned int link_id;
199 	int ret;
200 	struct link_container *links[IEEE80211_MLD_MAX_NUM_LINKS] = {}, *link;
201 	struct ieee80211_bss_conf *old[IEEE80211_MLD_MAX_NUM_LINKS];
202 	struct ieee80211_link_data *old_data[IEEE80211_MLD_MAX_NUM_LINKS];
203 	bool use_deflink = old_links == 0; /* set for error case */
204 
205 	sdata_assert_lock(sdata);
206 
207 	memset(to_free, 0, sizeof(links));
208 
209 	if (old_links == new_links && dormant_links == sdata->vif.dormant_links)
210 		return 0;
211 
212 	/* if there were no old links, need to clear the pointers to deflink */
213 	if (!old_links)
214 		rem |= BIT(0);
215 
216 	/* allocate new link structures first */
217 	for_each_set_bit(link_id, &add, IEEE80211_MLD_MAX_NUM_LINKS) {
218 		link = kzalloc(sizeof(*link), GFP_KERNEL);
219 		if (!link) {
220 			ret = -ENOMEM;
221 			goto free;
222 		}
223 		links[link_id] = link;
224 	}
225 
226 	/* keep track of the old pointers for the driver */
227 	BUILD_BUG_ON(sizeof(old) != sizeof(sdata->vif.link_conf));
228 	memcpy(old, sdata->vif.link_conf, sizeof(old));
229 	/* and for us in error cases */
230 	BUILD_BUG_ON(sizeof(old_data) != sizeof(sdata->link));
231 	memcpy(old_data, sdata->link, sizeof(old_data));
232 
233 	/* grab old links to free later */
234 	for_each_set_bit(link_id, &rem, IEEE80211_MLD_MAX_NUM_LINKS) {
235 		if (rcu_access_pointer(sdata->link[link_id]) != &sdata->deflink) {
236 			/*
237 			 * we must have allocated the data through this path so
238 			 * we know we can free both at the same time
239 			 */
240 			to_free[link_id] = container_of(rcu_access_pointer(sdata->link[link_id]),
241 							typeof(*links[link_id]),
242 							data);
243 		}
244 
245 		RCU_INIT_POINTER(sdata->link[link_id], NULL);
246 		RCU_INIT_POINTER(sdata->vif.link_conf[link_id], NULL);
247 	}
248 
249 	/* link them into data structures */
250 	for_each_set_bit(link_id, &add, IEEE80211_MLD_MAX_NUM_LINKS) {
251 		WARN_ON(!use_deflink &&
252 			rcu_access_pointer(sdata->link[link_id]) == &sdata->deflink);
253 
254 		link = links[link_id];
255 		ieee80211_link_init(sdata, link_id, &link->data, &link->conf);
256 		ieee80211_link_setup(&link->data);
257 	}
258 
259 	if (new_links == 0)
260 		ieee80211_link_init(sdata, -1, &sdata->deflink,
261 				    &sdata->vif.bss_conf);
262 
263 	ret = ieee80211_check_dup_link_addrs(sdata);
264 	if (!ret) {
265 		/* for keys we will not be able to undo this */
266 		ieee80211_tear_down_links(sdata, to_free, rem);
267 
268 		ieee80211_set_vif_links_bitmaps(sdata, new_links, dormant_links);
269 
270 		/* tell the driver */
271 		ret = drv_change_vif_links(sdata->local, sdata,
272 					   old_links & old_active,
273 					   new_links & sdata->vif.active_links,
274 					   old);
275 	}
276 
277 	if (ret) {
278 		/* restore config */
279 		memcpy(sdata->link, old_data, sizeof(old_data));
280 		memcpy(sdata->vif.link_conf, old, sizeof(old));
281 		ieee80211_set_vif_links_bitmaps(sdata, old_links, dormant_links);
282 		/* and free (only) the newly allocated links */
283 		memset(to_free, 0, sizeof(links));
284 		goto free;
285 	}
286 
287 	/* use deflink/bss_conf again if and only if there are no more links */
288 	use_deflink = new_links == 0;
289 
290 	goto deinit;
291 free:
292 	/* if we failed during allocation, only free all */
293 	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
294 		kfree(links[link_id]);
295 		links[link_id] = NULL;
296 	}
297 deinit:
298 	if (use_deflink)
299 		ieee80211_link_init(sdata, -1, &sdata->deflink,
300 				    &sdata->vif.bss_conf);
301 	return ret;
302 }
303 
ieee80211_vif_set_links(struct ieee80211_sub_if_data * sdata,u16 new_links,u16 dormant_links)304 int ieee80211_vif_set_links(struct ieee80211_sub_if_data *sdata,
305 			    u16 new_links, u16 dormant_links)
306 {
307 	struct link_container *links[IEEE80211_MLD_MAX_NUM_LINKS];
308 	int ret;
309 
310 	ret = ieee80211_vif_update_links(sdata, links, new_links,
311 					 dormant_links);
312 	ieee80211_free_links(sdata, links);
313 
314 	return ret;
315 }
316 
ieee80211_vif_clear_links(struct ieee80211_sub_if_data * sdata)317 void ieee80211_vif_clear_links(struct ieee80211_sub_if_data *sdata)
318 {
319 	struct link_container *links[IEEE80211_MLD_MAX_NUM_LINKS];
320 
321 	/*
322 	 * The locking here is different because when we free links
323 	 * in the station case we need to be able to cancel_work_sync()
324 	 * something that also takes the lock.
325 	 */
326 
327 	sdata_lock(sdata);
328 	ieee80211_vif_update_links(sdata, links, 0, 0);
329 	sdata_unlock(sdata);
330 
331 	ieee80211_free_links(sdata, links);
332 }
333 
_ieee80211_set_active_links(struct ieee80211_sub_if_data * sdata,u16 active_links)334 static int _ieee80211_set_active_links(struct ieee80211_sub_if_data *sdata,
335 				       u16 active_links)
336 {
337 	struct ieee80211_bss_conf *link_confs[IEEE80211_MLD_MAX_NUM_LINKS];
338 	struct ieee80211_local *local = sdata->local;
339 	u16 old_active = sdata->vif.active_links;
340 	unsigned long rem = old_active & ~active_links;
341 	unsigned long add = active_links & ~old_active;
342 	struct sta_info *sta;
343 	unsigned int link_id;
344 	int ret, i;
345 
346 	if (!ieee80211_sdata_running(sdata))
347 		return -ENETDOWN;
348 
349 	if (sdata->vif.type != NL80211_IFTYPE_STATION)
350 		return -EINVAL;
351 
352 	if (active_links & ~ieee80211_vif_usable_links(&sdata->vif))
353 		return -EINVAL;
354 
355 	/* nothing to do */
356 	if (old_active == active_links)
357 		return 0;
358 
359 	for (i = 0; i < IEEE80211_MLD_MAX_NUM_LINKS; i++)
360 		link_confs[i] = sdata_dereference(sdata->vif.link_conf[i],
361 						  sdata);
362 
363 	if (add) {
364 		sdata->vif.active_links |= active_links;
365 		ret = drv_change_vif_links(local, sdata,
366 					   old_active,
367 					   sdata->vif.active_links,
368 					   link_confs);
369 		if (ret) {
370 			sdata->vif.active_links = old_active;
371 			return ret;
372 		}
373 	}
374 
375 	for_each_set_bit(link_id, &rem, IEEE80211_MLD_MAX_NUM_LINKS) {
376 		struct ieee80211_link_data *link;
377 
378 		link = sdata_dereference(sdata->link[link_id], sdata);
379 
380 		/* FIXME: kill TDLS connections on the link */
381 
382 		ieee80211_link_release_channel(link);
383 	}
384 
385 	list_for_each_entry(sta, &local->sta_list, list) {
386 		if (sdata != sta->sdata)
387 			continue;
388 
389 		/* this is very temporary, but do it anyway */
390 		__ieee80211_sta_recalc_aggregates(sta,
391 						  old_active | active_links);
392 
393 		ret = drv_change_sta_links(local, sdata, &sta->sta,
394 					   old_active,
395 					   old_active | active_links);
396 		WARN_ON_ONCE(ret);
397 	}
398 
399 	ret = ieee80211_key_switch_links(sdata, rem, add);
400 	WARN_ON_ONCE(ret);
401 
402 	list_for_each_entry(sta, &local->sta_list, list) {
403 		if (sdata != sta->sdata)
404 			continue;
405 
406 		__ieee80211_sta_recalc_aggregates(sta, active_links);
407 
408 		ret = drv_change_sta_links(local, sdata, &sta->sta,
409 					   old_active | active_links,
410 					   active_links);
411 		WARN_ON_ONCE(ret);
412 
413 		/*
414 		 * Do it again, just in case - the driver might very
415 		 * well have called ieee80211_sta_recalc_aggregates()
416 		 * from there when filling in the new links, which
417 		 * would set it wrong since the vif's active links are
418 		 * not switched yet...
419 		 */
420 		__ieee80211_sta_recalc_aggregates(sta, active_links);
421 	}
422 
423 	for_each_set_bit(link_id, &add, IEEE80211_MLD_MAX_NUM_LINKS) {
424 		struct ieee80211_link_data *link;
425 
426 		link = sdata_dereference(sdata->link[link_id], sdata);
427 
428 		ret = ieee80211_link_use_channel(link, &link->conf->chandef,
429 						 IEEE80211_CHANCTX_SHARED);
430 		WARN_ON_ONCE(ret);
431 
432 		ieee80211_mgd_set_link_qos_params(link);
433 		ieee80211_link_info_change_notify(sdata, link,
434 						  BSS_CHANGED_ERP_CTS_PROT |
435 						  BSS_CHANGED_ERP_PREAMBLE |
436 						  BSS_CHANGED_ERP_SLOT |
437 						  BSS_CHANGED_HT |
438 						  BSS_CHANGED_BASIC_RATES |
439 						  BSS_CHANGED_BSSID |
440 						  BSS_CHANGED_CQM |
441 						  BSS_CHANGED_QOS |
442 						  BSS_CHANGED_TXPOWER |
443 						  BSS_CHANGED_BANDWIDTH |
444 						  BSS_CHANGED_TWT |
445 						  BSS_CHANGED_HE_OBSS_PD |
446 						  BSS_CHANGED_HE_BSS_COLOR);
447 	}
448 
449 	old_active = sdata->vif.active_links;
450 	sdata->vif.active_links = active_links;
451 
452 	if (rem) {
453 		ret = drv_change_vif_links(local, sdata, old_active,
454 					   active_links, link_confs);
455 		WARN_ON_ONCE(ret);
456 	}
457 
458 	return 0;
459 }
460 
__ieee80211_set_active_links(struct ieee80211_vif * vif,u16 active_links)461 int __ieee80211_set_active_links(struct ieee80211_vif *vif, u16 active_links)
462 {
463 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
464 	struct ieee80211_local *local = sdata->local;
465 	u16 old_active;
466 	int ret;
467 
468 	sdata_assert_lock(sdata);
469 	mutex_lock(&local->sta_mtx);
470 	mutex_lock(&local->mtx);
471 	mutex_lock(&local->key_mtx);
472 	old_active = sdata->vif.active_links;
473 	if (old_active & active_links) {
474 		/*
475 		 * if there's at least one link that stays active across
476 		 * the change then switch to it (to those) first, and
477 		 * then enable the additional links
478 		 */
479 		ret = _ieee80211_set_active_links(sdata,
480 						  old_active & active_links);
481 		if (!ret)
482 			ret = _ieee80211_set_active_links(sdata, active_links);
483 	} else {
484 		/* otherwise switch directly */
485 		ret = _ieee80211_set_active_links(sdata, active_links);
486 	}
487 	mutex_unlock(&local->key_mtx);
488 	mutex_unlock(&local->mtx);
489 	mutex_unlock(&local->sta_mtx);
490 
491 	return ret;
492 }
493 
ieee80211_set_active_links(struct ieee80211_vif * vif,u16 active_links)494 int ieee80211_set_active_links(struct ieee80211_vif *vif, u16 active_links)
495 {
496 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
497 	int ret;
498 
499 	sdata_lock(sdata);
500 	ret = __ieee80211_set_active_links(vif, active_links);
501 	sdata_unlock(sdata);
502 
503 	return ret;
504 }
505 EXPORT_SYMBOL_GPL(ieee80211_set_active_links);
506 
ieee80211_set_active_links_async(struct ieee80211_vif * vif,u16 active_links)507 void ieee80211_set_active_links_async(struct ieee80211_vif *vif,
508 				      u16 active_links)
509 {
510 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
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 	schedule_work(&sdata->activate_links_work);
527 }
528 EXPORT_SYMBOL_GPL(ieee80211_set_active_links_async);
529