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