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