xref: /openbmc/linux/net/mac80211/mesh.c (revision 9b68f30b)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2008, 2009 open80211s Ltd.
4  * Copyright (C) 2018 - 2022 Intel Corporation
5  * Authors:    Luis Carlos Cobo <luisca@cozybit.com>
6  * 	       Javier Cardona <javier@cozybit.com>
7  */
8 
9 #include <linux/slab.h>
10 #include <asm/unaligned.h>
11 #include "ieee80211_i.h"
12 #include "mesh.h"
13 #include "wme.h"
14 #include "driver-ops.h"
15 
16 static int mesh_allocated;
17 static struct kmem_cache *rm_cache;
18 
19 bool mesh_action_is_path_sel(struct ieee80211_mgmt *mgmt)
20 {
21 	return (mgmt->u.action.u.mesh_action.action_code ==
22 			WLAN_MESH_ACTION_HWMP_PATH_SELECTION);
23 }
24 
25 void ieee80211s_init(void)
26 {
27 	mesh_allocated = 1;
28 	rm_cache = kmem_cache_create("mesh_rmc", sizeof(struct rmc_entry),
29 				     0, 0, NULL);
30 }
31 
32 void ieee80211s_stop(void)
33 {
34 	if (!mesh_allocated)
35 		return;
36 	kmem_cache_destroy(rm_cache);
37 }
38 
39 static void ieee80211_mesh_housekeeping_timer(struct timer_list *t)
40 {
41 	struct ieee80211_sub_if_data *sdata =
42 		from_timer(sdata, t, u.mesh.housekeeping_timer);
43 	struct ieee80211_local *local = sdata->local;
44 	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
45 
46 	set_bit(MESH_WORK_HOUSEKEEPING, &ifmsh->wrkq_flags);
47 
48 	ieee80211_queue_work(&local->hw, &sdata->work);
49 }
50 
51 /**
52  * mesh_matches_local - check if the config of a mesh point matches ours
53  *
54  * @sdata: local mesh subif
55  * @ie: information elements of a management frame from the mesh peer
56  *
57  * This function checks if the mesh configuration of a mesh point matches the
58  * local mesh configuration, i.e. if both nodes belong to the same mesh network.
59  */
60 bool mesh_matches_local(struct ieee80211_sub_if_data *sdata,
61 			struct ieee802_11_elems *ie)
62 {
63 	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
64 	u32 basic_rates = 0;
65 	struct cfg80211_chan_def sta_chan_def;
66 	struct ieee80211_supported_band *sband;
67 	u32 vht_cap_info = 0;
68 
69 	/*
70 	 * As support for each feature is added, check for matching
71 	 * - On mesh config capabilities
72 	 *   - Power Save Support En
73 	 *   - Sync support enabled
74 	 *   - Sync support active
75 	 *   - Sync support required from peer
76 	 *   - MDA enabled
77 	 * - Power management control on fc
78 	 */
79 	if (!(ifmsh->mesh_id_len == ie->mesh_id_len &&
80 	     memcmp(ifmsh->mesh_id, ie->mesh_id, ie->mesh_id_len) == 0 &&
81 	     (ifmsh->mesh_pp_id == ie->mesh_config->meshconf_psel) &&
82 	     (ifmsh->mesh_pm_id == ie->mesh_config->meshconf_pmetric) &&
83 	     (ifmsh->mesh_cc_id == ie->mesh_config->meshconf_congest) &&
84 	     (ifmsh->mesh_sp_id == ie->mesh_config->meshconf_synch) &&
85 	     (ifmsh->mesh_auth_id == ie->mesh_config->meshconf_auth)))
86 		return false;
87 
88 	sband = ieee80211_get_sband(sdata);
89 	if (!sband)
90 		return false;
91 
92 	ieee80211_sta_get_rates(sdata, ie, sband->band,
93 				&basic_rates);
94 
95 	if (sdata->vif.bss_conf.basic_rates != basic_rates)
96 		return false;
97 
98 	cfg80211_chandef_create(&sta_chan_def, sdata->vif.bss_conf.chandef.chan,
99 				NL80211_CHAN_NO_HT);
100 	ieee80211_chandef_ht_oper(ie->ht_operation, &sta_chan_def);
101 
102 	if (ie->vht_cap_elem)
103 		vht_cap_info = le32_to_cpu(ie->vht_cap_elem->vht_cap_info);
104 
105 	ieee80211_chandef_vht_oper(&sdata->local->hw, vht_cap_info,
106 				   ie->vht_operation, ie->ht_operation,
107 				   &sta_chan_def);
108 	ieee80211_chandef_he_6ghz_oper(sdata, ie->he_operation, ie->eht_operation,
109 				       &sta_chan_def);
110 
111 	if (!cfg80211_chandef_compatible(&sdata->vif.bss_conf.chandef,
112 					 &sta_chan_def))
113 		return false;
114 
115 	return true;
116 }
117 
118 /**
119  * mesh_peer_accepts_plinks - check if an mp is willing to establish peer links
120  *
121  * @ie: information elements of a management frame from the mesh peer
122  */
123 bool mesh_peer_accepts_plinks(struct ieee802_11_elems *ie)
124 {
125 	return (ie->mesh_config->meshconf_cap &
126 			IEEE80211_MESHCONF_CAPAB_ACCEPT_PLINKS) != 0;
127 }
128 
129 /**
130  * mesh_accept_plinks_update - update accepting_plink in local mesh beacons
131  *
132  * @sdata: mesh interface in which mesh beacons are going to be updated
133  *
134  * Returns: beacon changed flag if the beacon content changed.
135  */
136 u32 mesh_accept_plinks_update(struct ieee80211_sub_if_data *sdata)
137 {
138 	bool free_plinks;
139 	u32 changed = 0;
140 
141 	/* In case mesh_plink_free_count > 0 and mesh_plinktbl_capacity == 0,
142 	 * the mesh interface might be able to establish plinks with peers that
143 	 * are already on the table but are not on PLINK_ESTAB state. However,
144 	 * in general the mesh interface is not accepting peer link requests
145 	 * from new peers, and that must be reflected in the beacon
146 	 */
147 	free_plinks = mesh_plink_availables(sdata);
148 
149 	if (free_plinks != sdata->u.mesh.accepting_plinks) {
150 		sdata->u.mesh.accepting_plinks = free_plinks;
151 		changed = BSS_CHANGED_BEACON;
152 	}
153 
154 	return changed;
155 }
156 
157 /*
158  * mesh_sta_cleanup - clean up any mesh sta state
159  *
160  * @sta: mesh sta to clean up.
161  */
162 void mesh_sta_cleanup(struct sta_info *sta)
163 {
164 	struct ieee80211_sub_if_data *sdata = sta->sdata;
165 	u32 changed = mesh_plink_deactivate(sta);
166 
167 	if (changed)
168 		ieee80211_mbss_info_change_notify(sdata, changed);
169 }
170 
171 int mesh_rmc_init(struct ieee80211_sub_if_data *sdata)
172 {
173 	int i;
174 
175 	sdata->u.mesh.rmc = kmalloc(sizeof(struct mesh_rmc), GFP_KERNEL);
176 	if (!sdata->u.mesh.rmc)
177 		return -ENOMEM;
178 	sdata->u.mesh.rmc->idx_mask = RMC_BUCKETS - 1;
179 	for (i = 0; i < RMC_BUCKETS; i++)
180 		INIT_HLIST_HEAD(&sdata->u.mesh.rmc->bucket[i]);
181 	return 0;
182 }
183 
184 void mesh_rmc_free(struct ieee80211_sub_if_data *sdata)
185 {
186 	struct mesh_rmc *rmc = sdata->u.mesh.rmc;
187 	struct rmc_entry *p;
188 	struct hlist_node *n;
189 	int i;
190 
191 	if (!sdata->u.mesh.rmc)
192 		return;
193 
194 	for (i = 0; i < RMC_BUCKETS; i++) {
195 		hlist_for_each_entry_safe(p, n, &rmc->bucket[i], list) {
196 			hlist_del(&p->list);
197 			kmem_cache_free(rm_cache, p);
198 		}
199 	}
200 
201 	kfree(rmc);
202 	sdata->u.mesh.rmc = NULL;
203 }
204 
205 /**
206  * mesh_rmc_check - Check frame in recent multicast cache and add if absent.
207  *
208  * @sdata:	interface
209  * @sa:		source address
210  * @mesh_hdr:	mesh_header
211  *
212  * Returns: 0 if the frame is not in the cache, nonzero otherwise.
213  *
214  * Checks using the source address and the mesh sequence number if we have
215  * received this frame lately. If the frame is not in the cache, it is added to
216  * it.
217  */
218 int mesh_rmc_check(struct ieee80211_sub_if_data *sdata,
219 		   const u8 *sa, struct ieee80211s_hdr *mesh_hdr)
220 {
221 	struct mesh_rmc *rmc = sdata->u.mesh.rmc;
222 	u32 seqnum = 0;
223 	int entries = 0;
224 	u8 idx;
225 	struct rmc_entry *p;
226 	struct hlist_node *n;
227 
228 	if (!rmc)
229 		return -1;
230 
231 	/* Don't care about endianness since only match matters */
232 	memcpy(&seqnum, &mesh_hdr->seqnum, sizeof(mesh_hdr->seqnum));
233 	idx = le32_to_cpu(mesh_hdr->seqnum) & rmc->idx_mask;
234 	hlist_for_each_entry_safe(p, n, &rmc->bucket[idx], list) {
235 		++entries;
236 		if (time_after(jiffies, p->exp_time) ||
237 		    entries == RMC_QUEUE_MAX_LEN) {
238 			hlist_del(&p->list);
239 			kmem_cache_free(rm_cache, p);
240 			--entries;
241 		} else if ((seqnum == p->seqnum) && ether_addr_equal(sa, p->sa))
242 			return -1;
243 	}
244 
245 	p = kmem_cache_alloc(rm_cache, GFP_ATOMIC);
246 	if (!p)
247 		return 0;
248 
249 	p->seqnum = seqnum;
250 	p->exp_time = jiffies + RMC_TIMEOUT;
251 	memcpy(p->sa, sa, ETH_ALEN);
252 	hlist_add_head(&p->list, &rmc->bucket[idx]);
253 	return 0;
254 }
255 
256 int mesh_add_meshconf_ie(struct ieee80211_sub_if_data *sdata,
257 			 struct sk_buff *skb)
258 {
259 	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
260 	u8 *pos, neighbors;
261 	u8 meshconf_len = sizeof(struct ieee80211_meshconf_ie);
262 	bool is_connected_to_gate = ifmsh->num_gates > 0 ||
263 		ifmsh->mshcfg.dot11MeshGateAnnouncementProtocol ||
264 		ifmsh->mshcfg.dot11MeshConnectedToMeshGate;
265 	bool is_connected_to_as = ifmsh->mshcfg.dot11MeshConnectedToAuthServer;
266 
267 	if (skb_tailroom(skb) < 2 + meshconf_len)
268 		return -ENOMEM;
269 
270 	pos = skb_put(skb, 2 + meshconf_len);
271 	*pos++ = WLAN_EID_MESH_CONFIG;
272 	*pos++ = meshconf_len;
273 
274 	/* save a pointer for quick updates in pre-tbtt */
275 	ifmsh->meshconf_offset = pos - skb->data;
276 
277 	/* Active path selection protocol ID */
278 	*pos++ = ifmsh->mesh_pp_id;
279 	/* Active path selection metric ID   */
280 	*pos++ = ifmsh->mesh_pm_id;
281 	/* Congestion control mode identifier */
282 	*pos++ = ifmsh->mesh_cc_id;
283 	/* Synchronization protocol identifier */
284 	*pos++ = ifmsh->mesh_sp_id;
285 	/* Authentication Protocol identifier */
286 	*pos++ = ifmsh->mesh_auth_id;
287 	/* Mesh Formation Info - number of neighbors */
288 	neighbors = atomic_read(&ifmsh->estab_plinks);
289 	neighbors = min_t(int, neighbors, IEEE80211_MAX_MESH_PEERINGS);
290 	*pos++ = (is_connected_to_as << 7) |
291 		 (neighbors << 1) |
292 		 is_connected_to_gate;
293 	/* Mesh capability */
294 	*pos = 0x00;
295 	*pos |= ifmsh->mshcfg.dot11MeshForwarding ?
296 			IEEE80211_MESHCONF_CAPAB_FORWARDING : 0x00;
297 	*pos |= ifmsh->accepting_plinks ?
298 			IEEE80211_MESHCONF_CAPAB_ACCEPT_PLINKS : 0x00;
299 	/* Mesh PS mode. See IEEE802.11-2012 8.4.2.100.8 */
300 	*pos |= ifmsh->ps_peers_deep_sleep ?
301 			IEEE80211_MESHCONF_CAPAB_POWER_SAVE_LEVEL : 0x00;
302 	return 0;
303 }
304 
305 int mesh_add_meshid_ie(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
306 {
307 	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
308 	u8 *pos;
309 
310 	if (skb_tailroom(skb) < 2 + ifmsh->mesh_id_len)
311 		return -ENOMEM;
312 
313 	pos = skb_put(skb, 2 + ifmsh->mesh_id_len);
314 	*pos++ = WLAN_EID_MESH_ID;
315 	*pos++ = ifmsh->mesh_id_len;
316 	if (ifmsh->mesh_id_len)
317 		memcpy(pos, ifmsh->mesh_id, ifmsh->mesh_id_len);
318 
319 	return 0;
320 }
321 
322 static int mesh_add_awake_window_ie(struct ieee80211_sub_if_data *sdata,
323 				    struct sk_buff *skb)
324 {
325 	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
326 	u8 *pos;
327 
328 	/* see IEEE802.11-2012 13.14.6 */
329 	if (ifmsh->ps_peers_light_sleep == 0 &&
330 	    ifmsh->ps_peers_deep_sleep == 0 &&
331 	    ifmsh->nonpeer_pm == NL80211_MESH_POWER_ACTIVE)
332 		return 0;
333 
334 	if (skb_tailroom(skb) < 4)
335 		return -ENOMEM;
336 
337 	pos = skb_put(skb, 2 + 2);
338 	*pos++ = WLAN_EID_MESH_AWAKE_WINDOW;
339 	*pos++ = 2;
340 	put_unaligned_le16(ifmsh->mshcfg.dot11MeshAwakeWindowDuration, pos);
341 
342 	return 0;
343 }
344 
345 int mesh_add_vendor_ies(struct ieee80211_sub_if_data *sdata,
346 			struct sk_buff *skb)
347 {
348 	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
349 	u8 offset, len;
350 	const u8 *data;
351 
352 	if (!ifmsh->ie || !ifmsh->ie_len)
353 		return 0;
354 
355 	/* fast-forward to vendor IEs */
356 	offset = ieee80211_ie_split_vendor(ifmsh->ie, ifmsh->ie_len, 0);
357 
358 	if (offset < ifmsh->ie_len) {
359 		len = ifmsh->ie_len - offset;
360 		data = ifmsh->ie + offset;
361 		if (skb_tailroom(skb) < len)
362 			return -ENOMEM;
363 		skb_put_data(skb, data, len);
364 	}
365 
366 	return 0;
367 }
368 
369 int mesh_add_rsn_ie(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
370 {
371 	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
372 	u8 len = 0;
373 	const u8 *data;
374 
375 	if (!ifmsh->ie || !ifmsh->ie_len)
376 		return 0;
377 
378 	/* find RSN IE */
379 	data = cfg80211_find_ie(WLAN_EID_RSN, ifmsh->ie, ifmsh->ie_len);
380 	if (!data)
381 		return 0;
382 
383 	len = data[1] + 2;
384 
385 	if (skb_tailroom(skb) < len)
386 		return -ENOMEM;
387 	skb_put_data(skb, data, len);
388 
389 	return 0;
390 }
391 
392 static int mesh_add_ds_params_ie(struct ieee80211_sub_if_data *sdata,
393 				 struct sk_buff *skb)
394 {
395 	struct ieee80211_chanctx_conf *chanctx_conf;
396 	struct ieee80211_channel *chan;
397 	u8 *pos;
398 
399 	if (skb_tailroom(skb) < 3)
400 		return -ENOMEM;
401 
402 	rcu_read_lock();
403 	chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
404 	if (WARN_ON(!chanctx_conf)) {
405 		rcu_read_unlock();
406 		return -EINVAL;
407 	}
408 	chan = chanctx_conf->def.chan;
409 	rcu_read_unlock();
410 
411 	pos = skb_put(skb, 2 + 1);
412 	*pos++ = WLAN_EID_DS_PARAMS;
413 	*pos++ = 1;
414 	*pos++ = ieee80211_frequency_to_channel(chan->center_freq);
415 
416 	return 0;
417 }
418 
419 int mesh_add_ht_cap_ie(struct ieee80211_sub_if_data *sdata,
420 		       struct sk_buff *skb)
421 {
422 	struct ieee80211_supported_band *sband;
423 	u8 *pos;
424 
425 	sband = ieee80211_get_sband(sdata);
426 	if (!sband)
427 		return -EINVAL;
428 
429 	/* HT not allowed in 6 GHz */
430 	if (sband->band == NL80211_BAND_6GHZ)
431 		return 0;
432 
433 	if (!sband->ht_cap.ht_supported ||
434 	    sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT ||
435 	    sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_5 ||
436 	    sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_10)
437 		return 0;
438 
439 	if (skb_tailroom(skb) < 2 + sizeof(struct ieee80211_ht_cap))
440 		return -ENOMEM;
441 
442 	pos = skb_put(skb, 2 + sizeof(struct ieee80211_ht_cap));
443 	ieee80211_ie_build_ht_cap(pos, &sband->ht_cap, sband->ht_cap.cap);
444 
445 	return 0;
446 }
447 
448 int mesh_add_ht_oper_ie(struct ieee80211_sub_if_data *sdata,
449 			struct sk_buff *skb)
450 {
451 	struct ieee80211_local *local = sdata->local;
452 	struct ieee80211_chanctx_conf *chanctx_conf;
453 	struct ieee80211_channel *channel;
454 	struct ieee80211_supported_band *sband;
455 	struct ieee80211_sta_ht_cap *ht_cap;
456 	u8 *pos;
457 
458 	rcu_read_lock();
459 	chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
460 	if (WARN_ON(!chanctx_conf)) {
461 		rcu_read_unlock();
462 		return -EINVAL;
463 	}
464 	channel = chanctx_conf->def.chan;
465 	rcu_read_unlock();
466 
467 	sband = local->hw.wiphy->bands[channel->band];
468 	ht_cap = &sband->ht_cap;
469 
470 	/* HT not allowed in 6 GHz */
471 	if (sband->band == NL80211_BAND_6GHZ)
472 		return 0;
473 
474 	if (!ht_cap->ht_supported ||
475 	    sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT ||
476 	    sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_5 ||
477 	    sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_10)
478 		return 0;
479 
480 	if (skb_tailroom(skb) < 2 + sizeof(struct ieee80211_ht_operation))
481 		return -ENOMEM;
482 
483 	pos = skb_put(skb, 2 + sizeof(struct ieee80211_ht_operation));
484 	ieee80211_ie_build_ht_oper(pos, ht_cap, &sdata->vif.bss_conf.chandef,
485 				   sdata->vif.bss_conf.ht_operation_mode,
486 				   false);
487 
488 	return 0;
489 }
490 
491 int mesh_add_vht_cap_ie(struct ieee80211_sub_if_data *sdata,
492 			struct sk_buff *skb)
493 {
494 	struct ieee80211_supported_band *sband;
495 	u8 *pos;
496 
497 	sband = ieee80211_get_sband(sdata);
498 	if (!sband)
499 		return -EINVAL;
500 
501 	/* VHT not allowed in 6 GHz */
502 	if (sband->band == NL80211_BAND_6GHZ)
503 		return 0;
504 
505 	if (!sband->vht_cap.vht_supported ||
506 	    sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT ||
507 	    sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_5 ||
508 	    sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_10)
509 		return 0;
510 
511 	if (skb_tailroom(skb) < 2 + sizeof(struct ieee80211_vht_cap))
512 		return -ENOMEM;
513 
514 	pos = skb_put(skb, 2 + sizeof(struct ieee80211_vht_cap));
515 	ieee80211_ie_build_vht_cap(pos, &sband->vht_cap, sband->vht_cap.cap);
516 
517 	return 0;
518 }
519 
520 int mesh_add_vht_oper_ie(struct ieee80211_sub_if_data *sdata,
521 			 struct sk_buff *skb)
522 {
523 	struct ieee80211_local *local = sdata->local;
524 	struct ieee80211_chanctx_conf *chanctx_conf;
525 	struct ieee80211_channel *channel;
526 	struct ieee80211_supported_band *sband;
527 	struct ieee80211_sta_vht_cap *vht_cap;
528 	u8 *pos;
529 
530 	rcu_read_lock();
531 	chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
532 	if (WARN_ON(!chanctx_conf)) {
533 		rcu_read_unlock();
534 		return -EINVAL;
535 	}
536 	channel = chanctx_conf->def.chan;
537 	rcu_read_unlock();
538 
539 	sband = local->hw.wiphy->bands[channel->band];
540 	vht_cap = &sband->vht_cap;
541 
542 	/* VHT not allowed in 6 GHz */
543 	if (sband->band == NL80211_BAND_6GHZ)
544 		return 0;
545 
546 	if (!vht_cap->vht_supported ||
547 	    sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT ||
548 	    sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_5 ||
549 	    sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_10)
550 		return 0;
551 
552 	if (skb_tailroom(skb) < 2 + sizeof(struct ieee80211_vht_operation))
553 		return -ENOMEM;
554 
555 	pos = skb_put(skb, 2 + sizeof(struct ieee80211_vht_operation));
556 	ieee80211_ie_build_vht_oper(pos, vht_cap,
557 				    &sdata->vif.bss_conf.chandef);
558 
559 	return 0;
560 }
561 
562 int mesh_add_he_cap_ie(struct ieee80211_sub_if_data *sdata,
563 		       struct sk_buff *skb, u8 ie_len)
564 {
565 	const struct ieee80211_sta_he_cap *he_cap;
566 	struct ieee80211_supported_band *sband;
567 	u8 *pos;
568 
569 	sband = ieee80211_get_sband(sdata);
570 	if (!sband)
571 		return -EINVAL;
572 
573 	he_cap = ieee80211_get_he_iftype_cap(sband, NL80211_IFTYPE_MESH_POINT);
574 
575 	if (!he_cap ||
576 	    sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT ||
577 	    sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_5 ||
578 	    sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_10)
579 		return 0;
580 
581 	if (skb_tailroom(skb) < ie_len)
582 		return -ENOMEM;
583 
584 	pos = skb_put(skb, ie_len);
585 	ieee80211_ie_build_he_cap(0, pos, he_cap, pos + ie_len);
586 
587 	return 0;
588 }
589 
590 int mesh_add_he_oper_ie(struct ieee80211_sub_if_data *sdata,
591 			struct sk_buff *skb)
592 {
593 	const struct ieee80211_sta_he_cap *he_cap;
594 	struct ieee80211_supported_band *sband;
595 	u32 len;
596 	u8 *pos;
597 
598 	sband = ieee80211_get_sband(sdata);
599 	if (!sband)
600 		return -EINVAL;
601 
602 	he_cap = ieee80211_get_he_iftype_cap(sband, NL80211_IFTYPE_MESH_POINT);
603 	if (!he_cap ||
604 	    sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT ||
605 	    sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_5 ||
606 	    sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_10)
607 		return 0;
608 
609 	len = 2 + 1 + sizeof(struct ieee80211_he_operation);
610 	if (sdata->vif.bss_conf.chandef.chan->band == NL80211_BAND_6GHZ)
611 		len += sizeof(struct ieee80211_he_6ghz_oper);
612 
613 	if (skb_tailroom(skb) < len)
614 		return -ENOMEM;
615 
616 	pos = skb_put(skb, len);
617 	ieee80211_ie_build_he_oper(pos, &sdata->vif.bss_conf.chandef);
618 
619 	return 0;
620 }
621 
622 int mesh_add_he_6ghz_cap_ie(struct ieee80211_sub_if_data *sdata,
623 			    struct sk_buff *skb)
624 {
625 	struct ieee80211_supported_band *sband;
626 	const struct ieee80211_sband_iftype_data *iftd;
627 
628 	sband = ieee80211_get_sband(sdata);
629 	if (!sband)
630 		return -EINVAL;
631 
632 	iftd = ieee80211_get_sband_iftype_data(sband,
633 					       NL80211_IFTYPE_MESH_POINT);
634 	/* The device doesn't support HE in mesh mode or at all */
635 	if (!iftd)
636 		return 0;
637 
638 	ieee80211_ie_build_he_6ghz_cap(sdata, sdata->deflink.smps_mode, skb);
639 	return 0;
640 }
641 
642 int mesh_add_eht_cap_ie(struct ieee80211_sub_if_data *sdata,
643 			struct sk_buff *skb, u8 ie_len)
644 {
645 	const struct ieee80211_sta_he_cap *he_cap;
646 	const struct ieee80211_sta_eht_cap *eht_cap;
647 	struct ieee80211_supported_band *sband;
648 	u8 *pos;
649 
650 	sband = ieee80211_get_sband(sdata);
651 	if (!sband)
652 		return -EINVAL;
653 
654 	he_cap = ieee80211_get_he_iftype_cap(sband, NL80211_IFTYPE_MESH_POINT);
655 	eht_cap = ieee80211_get_eht_iftype_cap(sband, NL80211_IFTYPE_MESH_POINT);
656 	if (!he_cap || !eht_cap ||
657 	    sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT ||
658 	    sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_5 ||
659 	    sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_10)
660 		return 0;
661 
662 	if (skb_tailroom(skb) < ie_len)
663 		return -ENOMEM;
664 
665 	pos = skb_put(skb, ie_len);
666 	ieee80211_ie_build_eht_cap(pos, he_cap, eht_cap, pos + ie_len, false);
667 
668 	return 0;
669 }
670 
671 int mesh_add_eht_oper_ie(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
672 {
673 	const struct ieee80211_sta_eht_cap *eht_cap;
674 	struct ieee80211_supported_band *sband;
675 	u32 len;
676 	u8 *pos;
677 
678 	sband = ieee80211_get_sband(sdata);
679 	if (!sband)
680 		return -EINVAL;
681 
682 	eht_cap = ieee80211_get_eht_iftype_cap(sband, NL80211_IFTYPE_MESH_POINT);
683 	if (!eht_cap ||
684 	    sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT ||
685 	    sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_5 ||
686 	    sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_10)
687 		return 0;
688 
689 	len = 2 + 1 + offsetof(struct ieee80211_eht_operation, optional) +
690 		      offsetof(struct ieee80211_eht_operation_info, optional);
691 
692 	if (skb_tailroom(skb) < len)
693 		return -ENOMEM;
694 
695 	pos = skb_put(skb, len);
696 	ieee80211_ie_build_eht_oper(pos, &sdata->vif.bss_conf.chandef, eht_cap);
697 
698 	return 0;
699 }
700 
701 static void ieee80211_mesh_path_timer(struct timer_list *t)
702 {
703 	struct ieee80211_sub_if_data *sdata =
704 		from_timer(sdata, t, u.mesh.mesh_path_timer);
705 
706 	ieee80211_queue_work(&sdata->local->hw, &sdata->work);
707 }
708 
709 static void ieee80211_mesh_path_root_timer(struct timer_list *t)
710 {
711 	struct ieee80211_sub_if_data *sdata =
712 		from_timer(sdata, t, u.mesh.mesh_path_root_timer);
713 	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
714 
715 	set_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags);
716 
717 	ieee80211_queue_work(&sdata->local->hw, &sdata->work);
718 }
719 
720 void ieee80211_mesh_root_setup(struct ieee80211_if_mesh *ifmsh)
721 {
722 	if (ifmsh->mshcfg.dot11MeshHWMPRootMode > IEEE80211_ROOTMODE_ROOT)
723 		set_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags);
724 	else {
725 		clear_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags);
726 		/* stop running timer */
727 		del_timer_sync(&ifmsh->mesh_path_root_timer);
728 	}
729 }
730 
731 static void
732 ieee80211_mesh_update_bss_params(struct ieee80211_sub_if_data *sdata,
733 				 u8 *ie, u8 ie_len)
734 {
735 	struct ieee80211_supported_band *sband;
736 	const struct element *cap;
737 	const struct ieee80211_he_operation *he_oper = NULL;
738 
739 	sband = ieee80211_get_sband(sdata);
740 	if (!sband)
741 		return;
742 
743 	if (!ieee80211_get_he_iftype_cap(sband, NL80211_IFTYPE_MESH_POINT) ||
744 	    sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT ||
745 	    sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_5 ||
746 	    sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_10)
747 		return;
748 
749 	sdata->vif.bss_conf.he_support = true;
750 
751 	cap = cfg80211_find_ext_elem(WLAN_EID_EXT_HE_OPERATION, ie, ie_len);
752 	if (cap && cap->datalen >= 1 + sizeof(*he_oper) &&
753 	    cap->datalen >= 1 + ieee80211_he_oper_size(cap->data + 1))
754 		he_oper = (void *)(cap->data + 1);
755 
756 	if (he_oper)
757 		sdata->vif.bss_conf.he_oper.params =
758 			__le32_to_cpu(he_oper->he_oper_params);
759 
760 	sdata->vif.bss_conf.eht_support =
761 		!!ieee80211_get_eht_iftype_cap(sband, NL80211_IFTYPE_MESH_POINT);
762 }
763 
764 bool ieee80211_mesh_xmit_fast(struct ieee80211_sub_if_data *sdata,
765 			      struct sk_buff *skb, u32 ctrl_flags)
766 {
767 	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
768 	struct ieee80211_mesh_fast_tx *entry;
769 	struct ieee80211s_hdr *meshhdr;
770 	u8 sa[ETH_ALEN] __aligned(2);
771 	struct tid_ampdu_tx *tid_tx;
772 	struct sta_info *sta;
773 	bool copy_sa = false;
774 	u16 ethertype;
775 	u8 tid;
776 
777 	if (ctrl_flags & IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP)
778 		return false;
779 
780 	if (ifmsh->mshcfg.dot11MeshNolearn)
781 		return false;
782 
783 	/* Add support for these cases later */
784 	if (ifmsh->ps_peers_light_sleep || ifmsh->ps_peers_deep_sleep)
785 		return false;
786 
787 	if (is_multicast_ether_addr(skb->data))
788 		return false;
789 
790 	ethertype = (skb->data[12] << 8) | skb->data[13];
791 	if (ethertype < ETH_P_802_3_MIN)
792 		return false;
793 
794 	if (skb->sk && skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS)
795 		return false;
796 
797 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
798 		skb_set_transport_header(skb, skb_checksum_start_offset(skb));
799 		if (skb_checksum_help(skb))
800 			return false;
801 	}
802 
803 	entry = mesh_fast_tx_get(sdata, skb->data);
804 	if (!entry)
805 		return false;
806 
807 	if (skb_headroom(skb) < entry->hdrlen + entry->fast_tx.hdr_len)
808 		return false;
809 
810 	sta = rcu_dereference(entry->mpath->next_hop);
811 	if (!sta)
812 		return false;
813 
814 	tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
815 	tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
816 	if (tid_tx) {
817 		if (!test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state))
818 			return false;
819 		if (tid_tx->timeout)
820 			tid_tx->last_tx = jiffies;
821 	}
822 
823 	skb = skb_share_check(skb, GFP_ATOMIC);
824 	if (!skb)
825 		return true;
826 
827 	skb_set_queue_mapping(skb, ieee80211_select_queue(sdata, sta, skb));
828 
829 	meshhdr = (struct ieee80211s_hdr *)entry->hdr;
830 	if ((meshhdr->flags & MESH_FLAGS_AE) == MESH_FLAGS_AE_A5_A6) {
831 		/* preserve SA from eth header for 6-addr frames */
832 		ether_addr_copy(sa, skb->data + ETH_ALEN);
833 		copy_sa = true;
834 	}
835 
836 	memcpy(skb_push(skb, entry->hdrlen - 2 * ETH_ALEN), entry->hdr,
837 	       entry->hdrlen);
838 
839 	meshhdr = (struct ieee80211s_hdr *)skb->data;
840 	put_unaligned_le32(atomic_inc_return(&sdata->u.mesh.mesh_seqnum),
841 			   &meshhdr->seqnum);
842 	meshhdr->ttl = sdata->u.mesh.mshcfg.dot11MeshTTL;
843 	if (copy_sa)
844 	    ether_addr_copy(meshhdr->eaddr2, sa);
845 
846 	skb_push(skb, 2 * ETH_ALEN);
847 	__ieee80211_xmit_fast(sdata, sta, &entry->fast_tx, skb, tid_tx,
848 			      entry->mpath->dst, sdata->vif.addr);
849 
850 	return true;
851 }
852 
853 /**
854  * ieee80211_fill_mesh_addresses - fill addresses of a locally originated mesh frame
855  * @hdr:	802.11 frame header
856  * @fc:		frame control field
857  * @meshda:	destination address in the mesh
858  * @meshsa:	source address in the mesh.  Same as TA, as frame is
859  *              locally originated.
860  *
861  * Return the length of the 802.11 (does not include a mesh control header)
862  */
863 int ieee80211_fill_mesh_addresses(struct ieee80211_hdr *hdr, __le16 *fc,
864 				  const u8 *meshda, const u8 *meshsa)
865 {
866 	if (is_multicast_ether_addr(meshda)) {
867 		*fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
868 		/* DA TA SA */
869 		memcpy(hdr->addr1, meshda, ETH_ALEN);
870 		memcpy(hdr->addr2, meshsa, ETH_ALEN);
871 		memcpy(hdr->addr3, meshsa, ETH_ALEN);
872 		return 24;
873 	} else {
874 		*fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
875 		/* RA TA DA SA */
876 		eth_zero_addr(hdr->addr1);   /* RA is resolved later */
877 		memcpy(hdr->addr2, meshsa, ETH_ALEN);
878 		memcpy(hdr->addr3, meshda, ETH_ALEN);
879 		memcpy(hdr->addr4, meshsa, ETH_ALEN);
880 		return 30;
881 	}
882 }
883 
884 /**
885  * ieee80211_new_mesh_header - create a new mesh header
886  * @sdata:	mesh interface to be used
887  * @meshhdr:    uninitialized mesh header
888  * @addr4or5:   1st address in the ae header, which may correspond to address 4
889  *              (if addr6 is NULL) or address 5 (if addr6 is present). It may
890  *              be NULL.
891  * @addr6:	2nd address in the ae header, which corresponds to addr6 of the
892  *              mesh frame
893  *
894  * Return the header length.
895  */
896 unsigned int ieee80211_new_mesh_header(struct ieee80211_sub_if_data *sdata,
897 				       struct ieee80211s_hdr *meshhdr,
898 				       const char *addr4or5, const char *addr6)
899 {
900 	if (WARN_ON(!addr4or5 && addr6))
901 		return 0;
902 
903 	memset(meshhdr, 0, sizeof(*meshhdr));
904 
905 	meshhdr->ttl = sdata->u.mesh.mshcfg.dot11MeshTTL;
906 
907 	put_unaligned_le32(atomic_inc_return(&sdata->u.mesh.mesh_seqnum),
908 			   &meshhdr->seqnum);
909 	if (addr4or5 && !addr6) {
910 		meshhdr->flags |= MESH_FLAGS_AE_A4;
911 		memcpy(meshhdr->eaddr1, addr4or5, ETH_ALEN);
912 		return 2 * ETH_ALEN;
913 	} else if (addr4or5 && addr6) {
914 		meshhdr->flags |= MESH_FLAGS_AE_A5_A6;
915 		memcpy(meshhdr->eaddr1, addr4or5, ETH_ALEN);
916 		memcpy(meshhdr->eaddr2, addr6, ETH_ALEN);
917 		return 3 * ETH_ALEN;
918 	}
919 
920 	return ETH_ALEN;
921 }
922 
923 static void ieee80211_mesh_housekeeping(struct ieee80211_sub_if_data *sdata)
924 {
925 	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
926 	u32 changed;
927 
928 	if (ifmsh->mshcfg.plink_timeout > 0)
929 		ieee80211_sta_expire(sdata, ifmsh->mshcfg.plink_timeout * HZ);
930 	mesh_path_expire(sdata);
931 
932 	changed = mesh_accept_plinks_update(sdata);
933 	ieee80211_mbss_info_change_notify(sdata, changed);
934 
935 	mesh_fast_tx_gc(sdata);
936 
937 	mod_timer(&ifmsh->housekeeping_timer,
938 		  round_jiffies(jiffies +
939 				IEEE80211_MESH_HOUSEKEEPING_INTERVAL));
940 }
941 
942 static void ieee80211_mesh_rootpath(struct ieee80211_sub_if_data *sdata)
943 {
944 	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
945 	u32 interval;
946 
947 	mesh_path_tx_root_frame(sdata);
948 
949 	if (ifmsh->mshcfg.dot11MeshHWMPRootMode == IEEE80211_PROACTIVE_RANN)
950 		interval = ifmsh->mshcfg.dot11MeshHWMPRannInterval;
951 	else
952 		interval = ifmsh->mshcfg.dot11MeshHWMProotInterval;
953 
954 	mod_timer(&ifmsh->mesh_path_root_timer,
955 		  round_jiffies(TU_TO_EXP_TIME(interval)));
956 }
957 
958 static int
959 ieee80211_mesh_build_beacon(struct ieee80211_if_mesh *ifmsh)
960 {
961 	struct beacon_data *bcn;
962 	int head_len, tail_len;
963 	struct sk_buff *skb;
964 	struct ieee80211_mgmt *mgmt;
965 	struct ieee80211_chanctx_conf *chanctx_conf;
966 	struct mesh_csa_settings *csa;
967 	enum nl80211_band band;
968 	u8 ie_len_he_cap, ie_len_eht_cap;
969 	u8 *pos;
970 	struct ieee80211_sub_if_data *sdata;
971 	int hdr_len = offsetofend(struct ieee80211_mgmt, u.beacon);
972 
973 	sdata = container_of(ifmsh, struct ieee80211_sub_if_data, u.mesh);
974 	rcu_read_lock();
975 	chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
976 	band = chanctx_conf->def.chan->band;
977 	rcu_read_unlock();
978 
979 	ie_len_he_cap = ieee80211_ie_len_he_cap(sdata,
980 						NL80211_IFTYPE_MESH_POINT);
981 	ie_len_eht_cap = ieee80211_ie_len_eht_cap(sdata,
982 						  NL80211_IFTYPE_MESH_POINT);
983 	head_len = hdr_len +
984 		   2 + /* NULL SSID */
985 		   /* Channel Switch Announcement */
986 		   2 + sizeof(struct ieee80211_channel_sw_ie) +
987 		   /* Mesh Channel Switch Parameters */
988 		   2 + sizeof(struct ieee80211_mesh_chansw_params_ie) +
989 		   /* Channel Switch Wrapper + Wide Bandwidth CSA IE */
990 		   2 + 2 + sizeof(struct ieee80211_wide_bw_chansw_ie) +
991 		   2 + sizeof(struct ieee80211_sec_chan_offs_ie) +
992 		   2 + 8 + /* supported rates */
993 		   2 + 3; /* DS params */
994 	tail_len = 2 + (IEEE80211_MAX_SUPP_RATES - 8) +
995 		   2 + sizeof(struct ieee80211_ht_cap) +
996 		   2 + sizeof(struct ieee80211_ht_operation) +
997 		   2 + ifmsh->mesh_id_len +
998 		   2 + sizeof(struct ieee80211_meshconf_ie) +
999 		   2 + sizeof(__le16) + /* awake window */
1000 		   2 + sizeof(struct ieee80211_vht_cap) +
1001 		   2 + sizeof(struct ieee80211_vht_operation) +
1002 		   ie_len_he_cap +
1003 		   2 + 1 + sizeof(struct ieee80211_he_operation) +
1004 			   sizeof(struct ieee80211_he_6ghz_oper) +
1005 		   2 + 1 + sizeof(struct ieee80211_he_6ghz_capa) +
1006 		   ie_len_eht_cap +
1007 		   2 + 1 + offsetof(struct ieee80211_eht_operation, optional) +
1008 			   offsetof(struct ieee80211_eht_operation_info, optional) +
1009 		   ifmsh->ie_len;
1010 
1011 	bcn = kzalloc(sizeof(*bcn) + head_len + tail_len, GFP_KERNEL);
1012 	/* need an skb for IE builders to operate on */
1013 	skb = __dev_alloc_skb(max(head_len, tail_len), GFP_KERNEL);
1014 
1015 	if (!bcn || !skb)
1016 		goto out_free;
1017 
1018 	/*
1019 	 * pointers go into the block we allocated,
1020 	 * memory is | beacon_data | head | tail |
1021 	 */
1022 	bcn->head = ((u8 *) bcn) + sizeof(*bcn);
1023 
1024 	/* fill in the head */
1025 	mgmt = skb_put_zero(skb, hdr_len);
1026 	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1027 					  IEEE80211_STYPE_BEACON);
1028 	eth_broadcast_addr(mgmt->da);
1029 	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
1030 	memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
1031 	ieee80211_mps_set_frame_flags(sdata, NULL, (void *) mgmt);
1032 	mgmt->u.beacon.beacon_int =
1033 		cpu_to_le16(sdata->vif.bss_conf.beacon_int);
1034 	mgmt->u.beacon.capab_info |= cpu_to_le16(
1035 		sdata->u.mesh.security ? WLAN_CAPABILITY_PRIVACY : 0);
1036 
1037 	pos = skb_put(skb, 2);
1038 	*pos++ = WLAN_EID_SSID;
1039 	*pos++ = 0x0;
1040 
1041 	rcu_read_lock();
1042 	csa = rcu_dereference(ifmsh->csa);
1043 	if (csa) {
1044 		enum nl80211_channel_type ct;
1045 		struct cfg80211_chan_def *chandef;
1046 		int ie_len = 2 + sizeof(struct ieee80211_channel_sw_ie) +
1047 			     2 + sizeof(struct ieee80211_mesh_chansw_params_ie);
1048 
1049 		pos = skb_put_zero(skb, ie_len);
1050 		*pos++ = WLAN_EID_CHANNEL_SWITCH;
1051 		*pos++ = 3;
1052 		*pos++ = 0x0;
1053 		*pos++ = ieee80211_frequency_to_channel(
1054 				csa->settings.chandef.chan->center_freq);
1055 		bcn->cntdwn_current_counter = csa->settings.count;
1056 		bcn->cntdwn_counter_offsets[0] = hdr_len + 6;
1057 		*pos++ = csa->settings.count;
1058 		*pos++ = WLAN_EID_CHAN_SWITCH_PARAM;
1059 		*pos++ = 6;
1060 		if (ifmsh->csa_role == IEEE80211_MESH_CSA_ROLE_INIT) {
1061 			*pos++ = ifmsh->mshcfg.dot11MeshTTL;
1062 			*pos |= WLAN_EID_CHAN_SWITCH_PARAM_INITIATOR;
1063 		} else {
1064 			*pos++ = ifmsh->chsw_ttl;
1065 		}
1066 		*pos++ |= csa->settings.block_tx ?
1067 			  WLAN_EID_CHAN_SWITCH_PARAM_TX_RESTRICT : 0x00;
1068 		put_unaligned_le16(WLAN_REASON_MESH_CHAN, pos);
1069 		pos += 2;
1070 		put_unaligned_le16(ifmsh->pre_value, pos);
1071 		pos += 2;
1072 
1073 		switch (csa->settings.chandef.width) {
1074 		case NL80211_CHAN_WIDTH_40:
1075 			ie_len = 2 + sizeof(struct ieee80211_sec_chan_offs_ie);
1076 			pos = skb_put_zero(skb, ie_len);
1077 
1078 			*pos++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET; /* EID */
1079 			*pos++ = 1;				    /* len */
1080 			ct = cfg80211_get_chandef_type(&csa->settings.chandef);
1081 			if (ct == NL80211_CHAN_HT40PLUS)
1082 				*pos++ = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
1083 			else
1084 				*pos++ = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
1085 			break;
1086 		case NL80211_CHAN_WIDTH_80:
1087 		case NL80211_CHAN_WIDTH_80P80:
1088 		case NL80211_CHAN_WIDTH_160:
1089 			/* Channel Switch Wrapper + Wide Bandwidth CSA IE */
1090 			ie_len = 2 + 2 +
1091 				 sizeof(struct ieee80211_wide_bw_chansw_ie);
1092 			pos = skb_put_zero(skb, ie_len);
1093 
1094 			*pos++ = WLAN_EID_CHANNEL_SWITCH_WRAPPER; /* EID */
1095 			*pos++ = 5;				  /* len */
1096 			/* put sub IE */
1097 			chandef = &csa->settings.chandef;
1098 			ieee80211_ie_build_wide_bw_cs(pos, chandef);
1099 			break;
1100 		default:
1101 			break;
1102 		}
1103 	}
1104 	rcu_read_unlock();
1105 
1106 	if (ieee80211_add_srates_ie(sdata, skb, true, band) ||
1107 	    mesh_add_ds_params_ie(sdata, skb))
1108 		goto out_free;
1109 
1110 	bcn->head_len = skb->len;
1111 	memcpy(bcn->head, skb->data, bcn->head_len);
1112 
1113 	/* now the tail */
1114 	skb_trim(skb, 0);
1115 	bcn->tail = bcn->head + bcn->head_len;
1116 
1117 	if (ieee80211_add_ext_srates_ie(sdata, skb, true, band) ||
1118 	    mesh_add_rsn_ie(sdata, skb) ||
1119 	    mesh_add_ht_cap_ie(sdata, skb) ||
1120 	    mesh_add_ht_oper_ie(sdata, skb) ||
1121 	    mesh_add_meshid_ie(sdata, skb) ||
1122 	    mesh_add_meshconf_ie(sdata, skb) ||
1123 	    mesh_add_awake_window_ie(sdata, skb) ||
1124 	    mesh_add_vht_cap_ie(sdata, skb) ||
1125 	    mesh_add_vht_oper_ie(sdata, skb) ||
1126 	    mesh_add_he_cap_ie(sdata, skb, ie_len_he_cap) ||
1127 	    mesh_add_he_oper_ie(sdata, skb) ||
1128 	    mesh_add_he_6ghz_cap_ie(sdata, skb) ||
1129 	    mesh_add_eht_cap_ie(sdata, skb, ie_len_eht_cap) ||
1130 	    mesh_add_eht_oper_ie(sdata, skb) ||
1131 	    mesh_add_vendor_ies(sdata, skb))
1132 		goto out_free;
1133 
1134 	bcn->tail_len = skb->len;
1135 	memcpy(bcn->tail, skb->data, bcn->tail_len);
1136 	ieee80211_mesh_update_bss_params(sdata, bcn->tail, bcn->tail_len);
1137 	bcn->meshconf = (struct ieee80211_meshconf_ie *)
1138 					(bcn->tail + ifmsh->meshconf_offset);
1139 
1140 	dev_kfree_skb(skb);
1141 	rcu_assign_pointer(ifmsh->beacon, bcn);
1142 	return 0;
1143 out_free:
1144 	kfree(bcn);
1145 	dev_kfree_skb(skb);
1146 	return -ENOMEM;
1147 }
1148 
1149 static int
1150 ieee80211_mesh_rebuild_beacon(struct ieee80211_sub_if_data *sdata)
1151 {
1152 	struct beacon_data *old_bcn;
1153 	int ret;
1154 
1155 	old_bcn = sdata_dereference(sdata->u.mesh.beacon, sdata);
1156 	ret = ieee80211_mesh_build_beacon(&sdata->u.mesh);
1157 	if (ret)
1158 		/* just reuse old beacon */
1159 		return ret;
1160 
1161 	if (old_bcn)
1162 		kfree_rcu(old_bcn, rcu_head);
1163 	return 0;
1164 }
1165 
1166 void ieee80211_mbss_info_change_notify(struct ieee80211_sub_if_data *sdata,
1167 				       u32 changed)
1168 {
1169 	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1170 	unsigned long bits = changed;
1171 	u32 bit;
1172 
1173 	if (!bits)
1174 		return;
1175 
1176 	/* if we race with running work, worst case this work becomes a noop */
1177 	for_each_set_bit(bit, &bits, sizeof(changed) * BITS_PER_BYTE)
1178 		set_bit(bit, &ifmsh->mbss_changed);
1179 	set_bit(MESH_WORK_MBSS_CHANGED, &ifmsh->wrkq_flags);
1180 	ieee80211_queue_work(&sdata->local->hw, &sdata->work);
1181 }
1182 
1183 int ieee80211_start_mesh(struct ieee80211_sub_if_data *sdata)
1184 {
1185 	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1186 	struct ieee80211_local *local = sdata->local;
1187 	u32 changed = BSS_CHANGED_BEACON |
1188 		      BSS_CHANGED_BEACON_ENABLED |
1189 		      BSS_CHANGED_HT |
1190 		      BSS_CHANGED_BASIC_RATES |
1191 		      BSS_CHANGED_BEACON_INT |
1192 		      BSS_CHANGED_MCAST_RATE;
1193 
1194 	local->fif_other_bss++;
1195 	/* mesh ifaces must set allmulti to forward mcast traffic */
1196 	atomic_inc(&local->iff_allmultis);
1197 	ieee80211_configure_filter(local);
1198 
1199 	ifmsh->mesh_cc_id = 0;	/* Disabled */
1200 	/* register sync ops from extensible synchronization framework */
1201 	ifmsh->sync_ops = ieee80211_mesh_sync_ops_get(ifmsh->mesh_sp_id);
1202 	ifmsh->sync_offset_clockdrift_max = 0;
1203 	set_bit(MESH_WORK_HOUSEKEEPING, &ifmsh->wrkq_flags);
1204 	ieee80211_mesh_root_setup(ifmsh);
1205 	ieee80211_queue_work(&local->hw, &sdata->work);
1206 	sdata->vif.bss_conf.ht_operation_mode =
1207 				ifmsh->mshcfg.ht_opmode;
1208 	sdata->vif.bss_conf.enable_beacon = true;
1209 
1210 	changed |= ieee80211_mps_local_status_update(sdata);
1211 
1212 	if (ieee80211_mesh_build_beacon(ifmsh)) {
1213 		ieee80211_stop_mesh(sdata);
1214 		return -ENOMEM;
1215 	}
1216 
1217 	ieee80211_recalc_dtim(local, sdata);
1218 	ieee80211_link_info_change_notify(sdata, &sdata->deflink, changed);
1219 
1220 	netif_carrier_on(sdata->dev);
1221 	return 0;
1222 }
1223 
1224 void ieee80211_stop_mesh(struct ieee80211_sub_if_data *sdata)
1225 {
1226 	struct ieee80211_local *local = sdata->local;
1227 	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1228 	struct beacon_data *bcn;
1229 
1230 	netif_carrier_off(sdata->dev);
1231 
1232 	/* flush STAs and mpaths on this iface */
1233 	sta_info_flush(sdata);
1234 	ieee80211_free_keys(sdata, true);
1235 	mesh_path_flush_by_iface(sdata);
1236 
1237 	/* stop the beacon */
1238 	ifmsh->mesh_id_len = 0;
1239 	sdata->vif.bss_conf.enable_beacon = false;
1240 	sdata->beacon_rate_set = false;
1241 	clear_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED, &sdata->state);
1242 	ieee80211_link_info_change_notify(sdata, &sdata->deflink,
1243 					  BSS_CHANGED_BEACON_ENABLED);
1244 
1245 	/* remove beacon */
1246 	bcn = sdata_dereference(ifmsh->beacon, sdata);
1247 	RCU_INIT_POINTER(ifmsh->beacon, NULL);
1248 	kfree_rcu(bcn, rcu_head);
1249 
1250 	/* free all potentially still buffered group-addressed frames */
1251 	local->total_ps_buffered -= skb_queue_len(&ifmsh->ps.bc_buf);
1252 	skb_queue_purge(&ifmsh->ps.bc_buf);
1253 
1254 	del_timer_sync(&sdata->u.mesh.housekeeping_timer);
1255 	del_timer_sync(&sdata->u.mesh.mesh_path_root_timer);
1256 	del_timer_sync(&sdata->u.mesh.mesh_path_timer);
1257 
1258 	/* clear any mesh work (for next join) we may have accrued */
1259 	ifmsh->wrkq_flags = 0;
1260 	ifmsh->mbss_changed = 0;
1261 
1262 	local->fif_other_bss--;
1263 	atomic_dec(&local->iff_allmultis);
1264 	ieee80211_configure_filter(local);
1265 }
1266 
1267 static void ieee80211_mesh_csa_mark_radar(struct ieee80211_sub_if_data *sdata)
1268 {
1269 	int err;
1270 
1271 	/* if the current channel is a DFS channel, mark the channel as
1272 	 * unavailable.
1273 	 */
1274 	err = cfg80211_chandef_dfs_required(sdata->local->hw.wiphy,
1275 					    &sdata->vif.bss_conf.chandef,
1276 					    NL80211_IFTYPE_MESH_POINT);
1277 	if (err > 0)
1278 		cfg80211_radar_event(sdata->local->hw.wiphy,
1279 				     &sdata->vif.bss_conf.chandef, GFP_ATOMIC);
1280 }
1281 
1282 static bool
1283 ieee80211_mesh_process_chnswitch(struct ieee80211_sub_if_data *sdata,
1284 				 struct ieee802_11_elems *elems, bool beacon)
1285 {
1286 	struct cfg80211_csa_settings params;
1287 	struct ieee80211_csa_ie csa_ie;
1288 	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1289 	struct ieee80211_supported_band *sband;
1290 	int err;
1291 	ieee80211_conn_flags_t conn_flags = 0;
1292 	u32 vht_cap_info = 0;
1293 
1294 	sdata_assert_lock(sdata);
1295 
1296 	sband = ieee80211_get_sband(sdata);
1297 	if (!sband)
1298 		return false;
1299 
1300 	switch (sdata->vif.bss_conf.chandef.width) {
1301 	case NL80211_CHAN_WIDTH_20_NOHT:
1302 		conn_flags |= IEEE80211_CONN_DISABLE_HT;
1303 		fallthrough;
1304 	case NL80211_CHAN_WIDTH_20:
1305 		conn_flags |= IEEE80211_CONN_DISABLE_40MHZ;
1306 		fallthrough;
1307 	case NL80211_CHAN_WIDTH_40:
1308 		conn_flags |= IEEE80211_CONN_DISABLE_VHT;
1309 		break;
1310 	default:
1311 		break;
1312 	}
1313 
1314 	if (elems->vht_cap_elem)
1315 		vht_cap_info =
1316 			le32_to_cpu(elems->vht_cap_elem->vht_cap_info);
1317 
1318 	memset(&params, 0, sizeof(params));
1319 	err = ieee80211_parse_ch_switch_ie(sdata, elems, sband->band,
1320 					   vht_cap_info,
1321 					   conn_flags, sdata->vif.addr,
1322 					   &csa_ie);
1323 	if (err < 0)
1324 		return false;
1325 	if (err)
1326 		return false;
1327 
1328 	/* Mark the channel unavailable if the reason for the switch is
1329 	 * regulatory.
1330 	 */
1331 	if (csa_ie.reason_code == WLAN_REASON_MESH_CHAN_REGULATORY)
1332 		ieee80211_mesh_csa_mark_radar(sdata);
1333 
1334 	params.chandef = csa_ie.chandef;
1335 	params.count = csa_ie.count;
1336 
1337 	if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, &params.chandef,
1338 				     IEEE80211_CHAN_DISABLED) ||
1339 	    !cfg80211_reg_can_beacon(sdata->local->hw.wiphy, &params.chandef,
1340 				     NL80211_IFTYPE_MESH_POINT)) {
1341 		sdata_info(sdata,
1342 			   "mesh STA %pM switches to unsupported channel (%d MHz, width:%d, CF1/2: %d/%d MHz), aborting\n",
1343 			   sdata->vif.addr,
1344 			   params.chandef.chan->center_freq,
1345 			   params.chandef.width,
1346 			   params.chandef.center_freq1,
1347 			   params.chandef.center_freq2);
1348 		return false;
1349 	}
1350 
1351 	err = cfg80211_chandef_dfs_required(sdata->local->hw.wiphy,
1352 					    &params.chandef,
1353 					    NL80211_IFTYPE_MESH_POINT);
1354 	if (err < 0)
1355 		return false;
1356 	if (err > 0 && !ifmsh->userspace_handles_dfs) {
1357 		sdata_info(sdata,
1358 			   "mesh STA %pM switches to channel requiring DFS (%d MHz, width:%d, CF1/2: %d/%d MHz), aborting\n",
1359 			   sdata->vif.addr,
1360 			   params.chandef.chan->center_freq,
1361 			   params.chandef.width,
1362 			   params.chandef.center_freq1,
1363 			   params.chandef.center_freq2);
1364 		return false;
1365 	}
1366 
1367 	params.radar_required = err;
1368 
1369 	if (cfg80211_chandef_identical(&params.chandef,
1370 				       &sdata->vif.bss_conf.chandef)) {
1371 		mcsa_dbg(sdata,
1372 			 "received csa with an identical chandef, ignoring\n");
1373 		return true;
1374 	}
1375 
1376 	mcsa_dbg(sdata,
1377 		 "received channel switch announcement to go to channel %d MHz\n",
1378 		 params.chandef.chan->center_freq);
1379 
1380 	params.block_tx = csa_ie.mode & WLAN_EID_CHAN_SWITCH_PARAM_TX_RESTRICT;
1381 	if (beacon) {
1382 		ifmsh->chsw_ttl = csa_ie.ttl - 1;
1383 		if (ifmsh->pre_value >= csa_ie.pre_value)
1384 			return false;
1385 		ifmsh->pre_value = csa_ie.pre_value;
1386 	}
1387 
1388 	if (ifmsh->chsw_ttl >= ifmsh->mshcfg.dot11MeshTTL)
1389 		return false;
1390 
1391 	ifmsh->csa_role = IEEE80211_MESH_CSA_ROLE_REPEATER;
1392 
1393 	if (ieee80211_channel_switch(sdata->local->hw.wiphy, sdata->dev,
1394 				     &params) < 0)
1395 		return false;
1396 
1397 	return true;
1398 }
1399 
1400 static void
1401 ieee80211_mesh_rx_probe_req(struct ieee80211_sub_if_data *sdata,
1402 			    struct ieee80211_mgmt *mgmt, size_t len)
1403 {
1404 	struct ieee80211_local *local = sdata->local;
1405 	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1406 	struct sk_buff *presp;
1407 	struct beacon_data *bcn;
1408 	struct ieee80211_mgmt *hdr;
1409 	struct ieee802_11_elems *elems;
1410 	size_t baselen;
1411 	u8 *pos;
1412 
1413 	pos = mgmt->u.probe_req.variable;
1414 	baselen = (u8 *) pos - (u8 *) mgmt;
1415 	if (baselen > len)
1416 		return;
1417 
1418 	elems = ieee802_11_parse_elems(pos, len - baselen, false, NULL);
1419 	if (!elems)
1420 		return;
1421 
1422 	if (!elems->mesh_id)
1423 		goto free;
1424 
1425 	/* 802.11-2012 10.1.4.3.2 */
1426 	if ((!ether_addr_equal(mgmt->da, sdata->vif.addr) &&
1427 	     !is_broadcast_ether_addr(mgmt->da)) ||
1428 	    elems->ssid_len != 0)
1429 		goto free;
1430 
1431 	if (elems->mesh_id_len != 0 &&
1432 	    (elems->mesh_id_len != ifmsh->mesh_id_len ||
1433 	     memcmp(elems->mesh_id, ifmsh->mesh_id, ifmsh->mesh_id_len)))
1434 		goto free;
1435 
1436 	rcu_read_lock();
1437 	bcn = rcu_dereference(ifmsh->beacon);
1438 
1439 	if (!bcn)
1440 		goto out;
1441 
1442 	presp = dev_alloc_skb(local->tx_headroom +
1443 			      bcn->head_len + bcn->tail_len);
1444 	if (!presp)
1445 		goto out;
1446 
1447 	skb_reserve(presp, local->tx_headroom);
1448 	skb_put_data(presp, bcn->head, bcn->head_len);
1449 	skb_put_data(presp, bcn->tail, bcn->tail_len);
1450 	hdr = (struct ieee80211_mgmt *) presp->data;
1451 	hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1452 					 IEEE80211_STYPE_PROBE_RESP);
1453 	memcpy(hdr->da, mgmt->sa, ETH_ALEN);
1454 	IEEE80211_SKB_CB(presp)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
1455 	ieee80211_tx_skb(sdata, presp);
1456 out:
1457 	rcu_read_unlock();
1458 free:
1459 	kfree(elems);
1460 }
1461 
1462 static void ieee80211_mesh_rx_bcn_presp(struct ieee80211_sub_if_data *sdata,
1463 					u16 stype,
1464 					struct ieee80211_mgmt *mgmt,
1465 					size_t len,
1466 					struct ieee80211_rx_status *rx_status)
1467 {
1468 	struct ieee80211_local *local = sdata->local;
1469 	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1470 	struct ieee802_11_elems *elems;
1471 	struct ieee80211_channel *channel;
1472 	size_t baselen;
1473 	int freq;
1474 	enum nl80211_band band = rx_status->band;
1475 
1476 	/* ignore ProbeResp to foreign address */
1477 	if (stype == IEEE80211_STYPE_PROBE_RESP &&
1478 	    !ether_addr_equal(mgmt->da, sdata->vif.addr))
1479 		return;
1480 
1481 	baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
1482 	if (baselen > len)
1483 		return;
1484 
1485 	elems = ieee802_11_parse_elems(mgmt->u.probe_resp.variable,
1486 				       len - baselen,
1487 				       false, NULL);
1488 	if (!elems)
1489 		return;
1490 
1491 	/* ignore non-mesh or secure / unsecure mismatch */
1492 	if ((!elems->mesh_id || !elems->mesh_config) ||
1493 	    (elems->rsn && sdata->u.mesh.security == IEEE80211_MESH_SEC_NONE) ||
1494 	    (!elems->rsn && sdata->u.mesh.security != IEEE80211_MESH_SEC_NONE))
1495 		goto free;
1496 
1497 	if (elems->ds_params)
1498 		freq = ieee80211_channel_to_frequency(elems->ds_params[0], band);
1499 	else
1500 		freq = rx_status->freq;
1501 
1502 	channel = ieee80211_get_channel(local->hw.wiphy, freq);
1503 
1504 	if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
1505 		goto free;
1506 
1507 	if (mesh_matches_local(sdata, elems)) {
1508 		mpl_dbg(sdata, "rssi_threshold=%d,rx_status->signal=%d\n",
1509 			sdata->u.mesh.mshcfg.rssi_threshold, rx_status->signal);
1510 		if (!sdata->u.mesh.user_mpm ||
1511 		    sdata->u.mesh.mshcfg.rssi_threshold == 0 ||
1512 		    sdata->u.mesh.mshcfg.rssi_threshold < rx_status->signal)
1513 			mesh_neighbour_update(sdata, mgmt->sa, elems,
1514 					      rx_status);
1515 
1516 		if (ifmsh->csa_role != IEEE80211_MESH_CSA_ROLE_INIT &&
1517 		    !sdata->vif.bss_conf.csa_active)
1518 			ieee80211_mesh_process_chnswitch(sdata, elems, true);
1519 	}
1520 
1521 	if (ifmsh->sync_ops)
1522 		ifmsh->sync_ops->rx_bcn_presp(sdata, stype, mgmt, len,
1523 					      elems->mesh_config, rx_status);
1524 free:
1525 	kfree(elems);
1526 }
1527 
1528 int ieee80211_mesh_finish_csa(struct ieee80211_sub_if_data *sdata)
1529 {
1530 	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1531 	struct mesh_csa_settings *tmp_csa_settings;
1532 	int ret = 0;
1533 	int changed = 0;
1534 
1535 	/* Reset the TTL value and Initiator flag */
1536 	ifmsh->csa_role = IEEE80211_MESH_CSA_ROLE_NONE;
1537 	ifmsh->chsw_ttl = 0;
1538 
1539 	/* Remove the CSA and MCSP elements from the beacon */
1540 	tmp_csa_settings = sdata_dereference(ifmsh->csa, sdata);
1541 	RCU_INIT_POINTER(ifmsh->csa, NULL);
1542 	if (tmp_csa_settings)
1543 		kfree_rcu(tmp_csa_settings, rcu_head);
1544 	ret = ieee80211_mesh_rebuild_beacon(sdata);
1545 	if (ret)
1546 		return -EINVAL;
1547 
1548 	changed |= BSS_CHANGED_BEACON;
1549 
1550 	mcsa_dbg(sdata, "complete switching to center freq %d MHz",
1551 		 sdata->vif.bss_conf.chandef.chan->center_freq);
1552 	return changed;
1553 }
1554 
1555 int ieee80211_mesh_csa_beacon(struct ieee80211_sub_if_data *sdata,
1556 			      struct cfg80211_csa_settings *csa_settings)
1557 {
1558 	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1559 	struct mesh_csa_settings *tmp_csa_settings;
1560 	int ret = 0;
1561 
1562 	lockdep_assert_held(&sdata->wdev.mtx);
1563 
1564 	tmp_csa_settings = kmalloc(sizeof(*tmp_csa_settings),
1565 				   GFP_ATOMIC);
1566 	if (!tmp_csa_settings)
1567 		return -ENOMEM;
1568 
1569 	memcpy(&tmp_csa_settings->settings, csa_settings,
1570 	       sizeof(struct cfg80211_csa_settings));
1571 
1572 	rcu_assign_pointer(ifmsh->csa, tmp_csa_settings);
1573 
1574 	ret = ieee80211_mesh_rebuild_beacon(sdata);
1575 	if (ret) {
1576 		tmp_csa_settings = rcu_dereference(ifmsh->csa);
1577 		RCU_INIT_POINTER(ifmsh->csa, NULL);
1578 		kfree_rcu(tmp_csa_settings, rcu_head);
1579 		return ret;
1580 	}
1581 
1582 	return BSS_CHANGED_BEACON;
1583 }
1584 
1585 static int mesh_fwd_csa_frame(struct ieee80211_sub_if_data *sdata,
1586 			       struct ieee80211_mgmt *mgmt, size_t len,
1587 			       struct ieee802_11_elems *elems)
1588 {
1589 	struct ieee80211_mgmt *mgmt_fwd;
1590 	struct sk_buff *skb;
1591 	struct ieee80211_local *local = sdata->local;
1592 
1593 	skb = dev_alloc_skb(local->tx_headroom + len);
1594 	if (!skb)
1595 		return -ENOMEM;
1596 	skb_reserve(skb, local->tx_headroom);
1597 	mgmt_fwd = skb_put(skb, len);
1598 
1599 	elems->mesh_chansw_params_ie->mesh_ttl--;
1600 	elems->mesh_chansw_params_ie->mesh_flags &=
1601 		~WLAN_EID_CHAN_SWITCH_PARAM_INITIATOR;
1602 
1603 	memcpy(mgmt_fwd, mgmt, len);
1604 	eth_broadcast_addr(mgmt_fwd->da);
1605 	memcpy(mgmt_fwd->sa, sdata->vif.addr, ETH_ALEN);
1606 	memcpy(mgmt_fwd->bssid, sdata->vif.addr, ETH_ALEN);
1607 
1608 	ieee80211_tx_skb(sdata, skb);
1609 	return 0;
1610 }
1611 
1612 static void mesh_rx_csa_frame(struct ieee80211_sub_if_data *sdata,
1613 			      struct ieee80211_mgmt *mgmt, size_t len)
1614 {
1615 	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1616 	struct ieee802_11_elems *elems;
1617 	u16 pre_value;
1618 	bool fwd_csa = true;
1619 	size_t baselen;
1620 	u8 *pos;
1621 
1622 	if (mgmt->u.action.u.measurement.action_code !=
1623 	    WLAN_ACTION_SPCT_CHL_SWITCH)
1624 		return;
1625 
1626 	pos = mgmt->u.action.u.chan_switch.variable;
1627 	baselen = offsetof(struct ieee80211_mgmt,
1628 			   u.action.u.chan_switch.variable);
1629 	elems = ieee802_11_parse_elems(pos, len - baselen, true, NULL);
1630 	if (!elems)
1631 		return;
1632 
1633 	if (!mesh_matches_local(sdata, elems))
1634 		goto free;
1635 
1636 	ifmsh->chsw_ttl = elems->mesh_chansw_params_ie->mesh_ttl;
1637 	if (!--ifmsh->chsw_ttl)
1638 		fwd_csa = false;
1639 
1640 	pre_value = le16_to_cpu(elems->mesh_chansw_params_ie->mesh_pre_value);
1641 	if (ifmsh->pre_value >= pre_value)
1642 		goto free;
1643 
1644 	ifmsh->pre_value = pre_value;
1645 
1646 	if (!sdata->vif.bss_conf.csa_active &&
1647 	    !ieee80211_mesh_process_chnswitch(sdata, elems, false)) {
1648 		mcsa_dbg(sdata, "Failed to process CSA action frame");
1649 		goto free;
1650 	}
1651 
1652 	/* forward or re-broadcast the CSA frame */
1653 	if (fwd_csa) {
1654 		if (mesh_fwd_csa_frame(sdata, mgmt, len, elems) < 0)
1655 			mcsa_dbg(sdata, "Failed to forward the CSA frame");
1656 	}
1657 free:
1658 	kfree(elems);
1659 }
1660 
1661 static void ieee80211_mesh_rx_mgmt_action(struct ieee80211_sub_if_data *sdata,
1662 					  struct ieee80211_mgmt *mgmt,
1663 					  size_t len,
1664 					  struct ieee80211_rx_status *rx_status)
1665 {
1666 	switch (mgmt->u.action.category) {
1667 	case WLAN_CATEGORY_SELF_PROTECTED:
1668 		switch (mgmt->u.action.u.self_prot.action_code) {
1669 		case WLAN_SP_MESH_PEERING_OPEN:
1670 		case WLAN_SP_MESH_PEERING_CLOSE:
1671 		case WLAN_SP_MESH_PEERING_CONFIRM:
1672 			mesh_rx_plink_frame(sdata, mgmt, len, rx_status);
1673 			break;
1674 		}
1675 		break;
1676 	case WLAN_CATEGORY_MESH_ACTION:
1677 		if (mesh_action_is_path_sel(mgmt))
1678 			mesh_rx_path_sel_frame(sdata, mgmt, len);
1679 		break;
1680 	case WLAN_CATEGORY_SPECTRUM_MGMT:
1681 		mesh_rx_csa_frame(sdata, mgmt, len);
1682 		break;
1683 	}
1684 }
1685 
1686 void ieee80211_mesh_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
1687 				   struct sk_buff *skb)
1688 {
1689 	struct ieee80211_rx_status *rx_status;
1690 	struct ieee80211_mgmt *mgmt;
1691 	u16 stype;
1692 
1693 	sdata_lock(sdata);
1694 
1695 	/* mesh already went down */
1696 	if (!sdata->u.mesh.mesh_id_len)
1697 		goto out;
1698 
1699 	rx_status = IEEE80211_SKB_RXCB(skb);
1700 	mgmt = (struct ieee80211_mgmt *) skb->data;
1701 	stype = le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE;
1702 
1703 	switch (stype) {
1704 	case IEEE80211_STYPE_PROBE_RESP:
1705 	case IEEE80211_STYPE_BEACON:
1706 		ieee80211_mesh_rx_bcn_presp(sdata, stype, mgmt, skb->len,
1707 					    rx_status);
1708 		break;
1709 	case IEEE80211_STYPE_PROBE_REQ:
1710 		ieee80211_mesh_rx_probe_req(sdata, mgmt, skb->len);
1711 		break;
1712 	case IEEE80211_STYPE_ACTION:
1713 		ieee80211_mesh_rx_mgmt_action(sdata, mgmt, skb->len, rx_status);
1714 		break;
1715 	}
1716 out:
1717 	sdata_unlock(sdata);
1718 }
1719 
1720 static void mesh_bss_info_changed(struct ieee80211_sub_if_data *sdata)
1721 {
1722 	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1723 	u32 bit, changed = 0;
1724 
1725 	for_each_set_bit(bit, &ifmsh->mbss_changed,
1726 			 sizeof(changed) * BITS_PER_BYTE) {
1727 		clear_bit(bit, &ifmsh->mbss_changed);
1728 		changed |= BIT(bit);
1729 	}
1730 
1731 	if (sdata->vif.bss_conf.enable_beacon &&
1732 	    (changed & (BSS_CHANGED_BEACON |
1733 			BSS_CHANGED_HT |
1734 			BSS_CHANGED_BASIC_RATES |
1735 			BSS_CHANGED_BEACON_INT)))
1736 		if (ieee80211_mesh_rebuild_beacon(sdata))
1737 			return;
1738 
1739 	ieee80211_link_info_change_notify(sdata, &sdata->deflink, changed);
1740 }
1741 
1742 void ieee80211_mesh_work(struct ieee80211_sub_if_data *sdata)
1743 {
1744 	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1745 
1746 	sdata_lock(sdata);
1747 
1748 	/* mesh already went down */
1749 	if (!sdata->u.mesh.mesh_id_len)
1750 		goto out;
1751 
1752 	if (ifmsh->preq_queue_len &&
1753 	    time_after(jiffies,
1754 		       ifmsh->last_preq + msecs_to_jiffies(ifmsh->mshcfg.dot11MeshHWMPpreqMinInterval)))
1755 		mesh_path_start_discovery(sdata);
1756 
1757 	if (test_and_clear_bit(MESH_WORK_HOUSEKEEPING, &ifmsh->wrkq_flags))
1758 		ieee80211_mesh_housekeeping(sdata);
1759 
1760 	if (test_and_clear_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags))
1761 		ieee80211_mesh_rootpath(sdata);
1762 
1763 	if (test_and_clear_bit(MESH_WORK_DRIFT_ADJUST, &ifmsh->wrkq_flags))
1764 		mesh_sync_adjust_tsf(sdata);
1765 
1766 	if (test_and_clear_bit(MESH_WORK_MBSS_CHANGED, &ifmsh->wrkq_flags))
1767 		mesh_bss_info_changed(sdata);
1768 out:
1769 	sdata_unlock(sdata);
1770 }
1771 
1772 
1773 void ieee80211_mesh_init_sdata(struct ieee80211_sub_if_data *sdata)
1774 {
1775 	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1776 	static u8 zero_addr[ETH_ALEN] = {};
1777 
1778 	timer_setup(&ifmsh->housekeeping_timer,
1779 		    ieee80211_mesh_housekeeping_timer, 0);
1780 
1781 	ifmsh->accepting_plinks = true;
1782 	atomic_set(&ifmsh->mpaths, 0);
1783 	mesh_rmc_init(sdata);
1784 	ifmsh->last_preq = jiffies;
1785 	ifmsh->next_perr = jiffies;
1786 	ifmsh->csa_role = IEEE80211_MESH_CSA_ROLE_NONE;
1787 	/* Allocate all mesh structures when creating the first mesh interface. */
1788 	if (!mesh_allocated)
1789 		ieee80211s_init();
1790 
1791 	mesh_pathtbl_init(sdata);
1792 
1793 	timer_setup(&ifmsh->mesh_path_timer, ieee80211_mesh_path_timer, 0);
1794 	timer_setup(&ifmsh->mesh_path_root_timer,
1795 		    ieee80211_mesh_path_root_timer, 0);
1796 	INIT_LIST_HEAD(&ifmsh->preq_queue.list);
1797 	skb_queue_head_init(&ifmsh->ps.bc_buf);
1798 	spin_lock_init(&ifmsh->mesh_preq_queue_lock);
1799 	spin_lock_init(&ifmsh->sync_offset_lock);
1800 	RCU_INIT_POINTER(ifmsh->beacon, NULL);
1801 
1802 	sdata->vif.bss_conf.bssid = zero_addr;
1803 }
1804 
1805 void ieee80211_mesh_teardown_sdata(struct ieee80211_sub_if_data *sdata)
1806 {
1807 	mesh_rmc_free(sdata);
1808 	mesh_pathtbl_unregister(sdata);
1809 }
1810