xref: /linux/net/mac80211/mesh.c (revision be54f8c558027a218423134dd9b8c7c46d92204a)
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 
mesh_action_is_path_sel(struct ieee80211_mgmt * mgmt)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 
ieee80211s_init(void)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 
ieee80211s_stop(void)33 void ieee80211s_stop(void)
34 {
35 	if (!mesh_allocated)
36 		return;
37 	kmem_cache_destroy(rm_cache);
38 }
39 
ieee80211_mesh_housekeeping_timer(struct timer_list * t)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  */
mesh_matches_local(struct ieee80211_sub_if_data * sdata,struct ieee802_11_elems * ie)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  */
mesh_peer_accepts_plinks(struct ieee802_11_elems * ie)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  */
mesh_accept_plinks_update(struct ieee80211_sub_if_data * sdata)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  */
mesh_sta_cleanup(struct sta_info * sta)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 
mesh_rmc_init(struct ieee80211_sub_if_data * sdata)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 
mesh_rmc_free(struct ieee80211_sub_if_data * sdata)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  */
mesh_rmc_check(struct ieee80211_sub_if_data * sdata,const u8 * sa,struct ieee80211s_hdr * mesh_hdr)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 
mesh_add_meshconf_ie(struct ieee80211_sub_if_data * sdata,struct sk_buff * skb)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 
mesh_add_meshid_ie(struct ieee80211_sub_if_data * sdata,struct sk_buff * skb)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 
mesh_add_awake_window_ie(struct ieee80211_sub_if_data * sdata,struct sk_buff * skb)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 
mesh_add_vendor_ies(struct ieee80211_sub_if_data * sdata,struct sk_buff * skb)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 
mesh_add_rsn_ie(struct ieee80211_sub_if_data * sdata,struct sk_buff * skb)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 
mesh_add_ds_params_ie(struct ieee80211_sub_if_data * sdata,struct sk_buff * skb)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 
mesh_add_ht_cap_ie(struct ieee80211_sub_if_data * sdata,struct sk_buff * skb)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 
mesh_add_ht_oper_ie(struct ieee80211_sub_if_data * sdata,struct sk_buff * skb)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 
mesh_add_vht_cap_ie(struct ieee80211_sub_if_data * sdata,struct sk_buff * skb)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 
mesh_add_vht_oper_ie(struct ieee80211_sub_if_data * sdata,struct sk_buff * skb)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 
mesh_add_he_cap_ie(struct ieee80211_sub_if_data * sdata,struct sk_buff * skb,u8 ie_len)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 
mesh_add_he_oper_ie(struct ieee80211_sub_if_data * sdata,struct sk_buff * skb)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 
mesh_add_he_6ghz_cap_ie(struct ieee80211_sub_if_data * sdata,struct sk_buff * skb)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 	iftd = ieee80211_get_sband_iftype_data(sband,
628 					       NL80211_IFTYPE_MESH_POINT);
629 	/* The device doesn't support HE in mesh mode or at all */
630 	if (!iftd)
631 		return 0;
632 
633 	ieee80211_put_he_6ghz_cap(skb, sdata, sdata->deflink.smps_mode);
634 	return 0;
635 }
636 
mesh_add_eht_cap_ie(struct ieee80211_sub_if_data * sdata,struct sk_buff * skb,u8 ie_len)637 int mesh_add_eht_cap_ie(struct ieee80211_sub_if_data *sdata,
638 			struct sk_buff *skb, u8 ie_len)
639 {
640 	struct ieee80211_supported_band *sband;
641 
642 	sband = ieee80211_get_sband(sdata);
643 	if (!sband)
644 		return -EINVAL;
645 
646 	if (sdata->vif.bss_conf.chanreq.oper.width == NL80211_CHAN_WIDTH_20_NOHT ||
647 	    sdata->vif.bss_conf.chanreq.oper.width == NL80211_CHAN_WIDTH_5 ||
648 	    sdata->vif.bss_conf.chanreq.oper.width == NL80211_CHAN_WIDTH_10)
649 		return 0;
650 
651 	return ieee80211_put_eht_cap(skb, sdata, sband, NULL);
652 }
653 
mesh_add_eht_oper_ie(struct ieee80211_sub_if_data * sdata,struct sk_buff * skb)654 int mesh_add_eht_oper_ie(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
655 {
656 	const struct ieee80211_sta_eht_cap *eht_cap;
657 	struct ieee80211_supported_band *sband;
658 	u32 len;
659 	u8 *pos;
660 
661 	sband = ieee80211_get_sband(sdata);
662 	if (!sband)
663 		return -EINVAL;
664 
665 	eht_cap = ieee80211_get_eht_iftype_cap(sband, NL80211_IFTYPE_MESH_POINT);
666 	if (!eht_cap ||
667 	    sdata->vif.bss_conf.chanreq.oper.width == NL80211_CHAN_WIDTH_20_NOHT ||
668 	    sdata->vif.bss_conf.chanreq.oper.width == NL80211_CHAN_WIDTH_5 ||
669 	    sdata->vif.bss_conf.chanreq.oper.width == NL80211_CHAN_WIDTH_10)
670 		return 0;
671 
672 	len = 2 + 1 + offsetof(struct ieee80211_eht_operation, optional) +
673 		      offsetof(struct ieee80211_eht_operation_info, optional);
674 
675 	if (skb_tailroom(skb) < len)
676 		return -ENOMEM;
677 
678 	pos = skb_put(skb, len);
679 	ieee80211_ie_build_eht_oper(pos, &sdata->vif.bss_conf.chanreq.oper, eht_cap);
680 
681 	return 0;
682 }
683 
ieee80211_mesh_path_timer(struct timer_list * t)684 static void ieee80211_mesh_path_timer(struct timer_list *t)
685 {
686 	struct ieee80211_sub_if_data *sdata =
687 		timer_container_of(sdata, t, u.mesh.mesh_path_timer);
688 
689 	wiphy_work_queue(sdata->local->hw.wiphy, &sdata->work);
690 }
691 
ieee80211_mesh_path_root_timer(struct timer_list * t)692 static void ieee80211_mesh_path_root_timer(struct timer_list *t)
693 {
694 	struct ieee80211_sub_if_data *sdata =
695 		timer_container_of(sdata, t, u.mesh.mesh_path_root_timer);
696 	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
697 
698 	set_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags);
699 
700 	wiphy_work_queue(sdata->local->hw.wiphy, &sdata->work);
701 }
702 
ieee80211_mesh_root_setup(struct ieee80211_if_mesh * ifmsh)703 void ieee80211_mesh_root_setup(struct ieee80211_if_mesh *ifmsh)
704 {
705 	if (ifmsh->mshcfg.dot11MeshHWMPRootMode > IEEE80211_ROOTMODE_ROOT)
706 		set_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags);
707 	else {
708 		clear_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags);
709 		/* stop running timer */
710 		timer_delete_sync(&ifmsh->mesh_path_root_timer);
711 	}
712 }
713 
714 static void
ieee80211_mesh_update_bss_params(struct ieee80211_sub_if_data * sdata,u8 * ie,u8 ie_len)715 ieee80211_mesh_update_bss_params(struct ieee80211_sub_if_data *sdata,
716 				 u8 *ie, u8 ie_len)
717 {
718 	struct ieee80211_supported_band *sband;
719 	const struct element *cap;
720 	const struct ieee80211_he_operation *he_oper = NULL;
721 
722 	sband = ieee80211_get_sband(sdata);
723 	if (!sband)
724 		return;
725 
726 	if (!ieee80211_get_he_iftype_cap(sband, NL80211_IFTYPE_MESH_POINT) ||
727 	    sdata->vif.bss_conf.chanreq.oper.width == NL80211_CHAN_WIDTH_20_NOHT ||
728 	    sdata->vif.bss_conf.chanreq.oper.width == NL80211_CHAN_WIDTH_5 ||
729 	    sdata->vif.bss_conf.chanreq.oper.width == NL80211_CHAN_WIDTH_10)
730 		return;
731 
732 	sdata->vif.bss_conf.he_support = true;
733 
734 	cap = cfg80211_find_ext_elem(WLAN_EID_EXT_HE_OPERATION, ie, ie_len);
735 	if (cap && cap->datalen >= 1 + sizeof(*he_oper) &&
736 	    cap->datalen >= 1 + ieee80211_he_oper_size(cap->data + 1))
737 		he_oper = (void *)(cap->data + 1);
738 
739 	if (he_oper)
740 		sdata->vif.bss_conf.he_oper.params =
741 			__le32_to_cpu(he_oper->he_oper_params);
742 
743 	sdata->vif.bss_conf.eht_support =
744 		!!ieee80211_get_eht_iftype_cap(sband, NL80211_IFTYPE_MESH_POINT);
745 }
746 
ieee80211_mesh_xmit_fast(struct ieee80211_sub_if_data * sdata,struct sk_buff * skb,u32 ctrl_flags)747 bool ieee80211_mesh_xmit_fast(struct ieee80211_sub_if_data *sdata,
748 			      struct sk_buff *skb, u32 ctrl_flags)
749 {
750 	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
751 	struct ieee80211_mesh_fast_tx_key key = {
752 		.type = MESH_FAST_TX_TYPE_LOCAL
753 	};
754 	struct ieee80211_mesh_fast_tx *entry;
755 	struct ieee80211s_hdr *meshhdr;
756 	u8 sa[ETH_ALEN] __aligned(2);
757 	struct tid_ampdu_tx *tid_tx;
758 	struct sta_info *sta;
759 	bool copy_sa = false;
760 	u16 ethertype;
761 	u8 tid;
762 
763 	if (ctrl_flags & IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP)
764 		return false;
765 
766 	if (ifmsh->mshcfg.dot11MeshNolearn)
767 		return false;
768 
769 	/* Add support for these cases later */
770 	if (ifmsh->ps_peers_light_sleep || ifmsh->ps_peers_deep_sleep)
771 		return false;
772 
773 	if (is_multicast_ether_addr(skb->data))
774 		return false;
775 
776 	ethertype = (skb->data[12] << 8) | skb->data[13];
777 	if (ethertype < ETH_P_802_3_MIN)
778 		return false;
779 
780 	if (sk_requests_wifi_status(skb->sk))
781 		return false;
782 
783 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
784 		skb_set_transport_header(skb, skb_checksum_start_offset(skb));
785 		if (skb_checksum_help(skb))
786 			return false;
787 	}
788 
789 	ether_addr_copy(key.addr, skb->data);
790 	if (!ether_addr_equal(skb->data + ETH_ALEN, sdata->vif.addr))
791 		key.type = MESH_FAST_TX_TYPE_PROXIED;
792 	entry = mesh_fast_tx_get(sdata, &key);
793 	if (!entry)
794 		return false;
795 
796 	if (skb_headroom(skb) < entry->hdrlen + entry->fast_tx.hdr_len)
797 		return false;
798 
799 	sta = rcu_dereference(entry->mpath->next_hop);
800 	if (!sta)
801 		return false;
802 
803 	tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
804 	tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
805 	if (tid_tx) {
806 		if (!test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state))
807 			return false;
808 		if (tid_tx->timeout)
809 			tid_tx->last_tx = jiffies;
810 	}
811 
812 	skb = skb_share_check(skb, GFP_ATOMIC);
813 	if (!skb)
814 		return true;
815 
816 	skb_set_queue_mapping(skb, ieee80211_select_queue(sdata, sta, skb));
817 
818 	meshhdr = (struct ieee80211s_hdr *)entry->hdr;
819 	if ((meshhdr->flags & MESH_FLAGS_AE) == MESH_FLAGS_AE_A5_A6) {
820 		/* preserve SA from eth header for 6-addr frames */
821 		ether_addr_copy(sa, skb->data + ETH_ALEN);
822 		copy_sa = true;
823 	}
824 
825 	memcpy(skb_push(skb, entry->hdrlen - 2 * ETH_ALEN), entry->hdr,
826 	       entry->hdrlen);
827 
828 	meshhdr = (struct ieee80211s_hdr *)skb->data;
829 	put_unaligned_le32(atomic_inc_return(&sdata->u.mesh.mesh_seqnum),
830 			   &meshhdr->seqnum);
831 	meshhdr->ttl = sdata->u.mesh.mshcfg.dot11MeshTTL;
832 	if (copy_sa)
833 	    ether_addr_copy(meshhdr->eaddr2, sa);
834 
835 	skb_push(skb, 2 * ETH_ALEN);
836 	__ieee80211_xmit_fast(sdata, sta, &entry->fast_tx, skb, tid_tx,
837 			      entry->mpath->dst, sdata->vif.addr);
838 
839 	return true;
840 }
841 
842 /**
843  * ieee80211_fill_mesh_addresses - fill addresses of a locally originated mesh frame
844  * @hdr:	802.11 frame header
845  * @fc:		frame control field
846  * @meshda:	destination address in the mesh
847  * @meshsa:	source address in the mesh.  Same as TA, as frame is
848  *              locally originated.
849  *
850  * Returns: the length of the 802.11 frame header (excludes mesh control header)
851  */
ieee80211_fill_mesh_addresses(struct ieee80211_hdr * hdr,__le16 * fc,const u8 * meshda,const u8 * meshsa)852 int ieee80211_fill_mesh_addresses(struct ieee80211_hdr *hdr, __le16 *fc,
853 				  const u8 *meshda, const u8 *meshsa)
854 {
855 	if (is_multicast_ether_addr(meshda)) {
856 		*fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
857 		/* DA TA SA */
858 		memcpy(hdr->addr1, meshda, ETH_ALEN);
859 		memcpy(hdr->addr2, meshsa, ETH_ALEN);
860 		memcpy(hdr->addr3, meshsa, ETH_ALEN);
861 		return 24;
862 	} else {
863 		*fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
864 		/* RA TA DA SA */
865 		eth_zero_addr(hdr->addr1);   /* RA is resolved later */
866 		memcpy(hdr->addr2, meshsa, ETH_ALEN);
867 		memcpy(hdr->addr3, meshda, ETH_ALEN);
868 		memcpy(hdr->addr4, meshsa, ETH_ALEN);
869 		return 30;
870 	}
871 }
872 
873 /**
874  * ieee80211_new_mesh_header - create a new mesh header
875  * @sdata:	mesh interface to be used
876  * @meshhdr:    uninitialized mesh header
877  * @addr4or5:   1st address in the ae header, which may correspond to address 4
878  *              (if addr6 is NULL) or address 5 (if addr6 is present). It may
879  *              be NULL.
880  * @addr6:	2nd address in the ae header, which corresponds to addr6 of the
881  *              mesh frame
882  *
883  * Returns: the header length
884  */
ieee80211_new_mesh_header(struct ieee80211_sub_if_data * sdata,struct ieee80211s_hdr * meshhdr,const char * addr4or5,const char * addr6)885 unsigned int ieee80211_new_mesh_header(struct ieee80211_sub_if_data *sdata,
886 				       struct ieee80211s_hdr *meshhdr,
887 				       const char *addr4or5, const char *addr6)
888 {
889 	if (WARN_ON(!addr4or5 && addr6))
890 		return 0;
891 
892 	memset(meshhdr, 0, sizeof(*meshhdr));
893 
894 	meshhdr->ttl = sdata->u.mesh.mshcfg.dot11MeshTTL;
895 
896 	put_unaligned_le32(atomic_inc_return(&sdata->u.mesh.mesh_seqnum),
897 			   &meshhdr->seqnum);
898 	if (addr4or5 && !addr6) {
899 		meshhdr->flags |= MESH_FLAGS_AE_A4;
900 		memcpy(meshhdr->eaddr1, addr4or5, ETH_ALEN);
901 		return 2 * ETH_ALEN;
902 	} else if (addr4or5 && addr6) {
903 		meshhdr->flags |= MESH_FLAGS_AE_A5_A6;
904 		memcpy(meshhdr->eaddr1, addr4or5, ETH_ALEN);
905 		memcpy(meshhdr->eaddr2, addr6, ETH_ALEN);
906 		return 3 * ETH_ALEN;
907 	}
908 
909 	return ETH_ALEN;
910 }
911 
ieee80211_mesh_housekeeping(struct ieee80211_sub_if_data * sdata)912 static void ieee80211_mesh_housekeeping(struct ieee80211_sub_if_data *sdata)
913 {
914 	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
915 	u64 changed;
916 
917 	if (ifmsh->mshcfg.plink_timeout > 0)
918 		ieee80211_sta_expire(sdata, ifmsh->mshcfg.plink_timeout * HZ);
919 	mesh_path_expire(sdata);
920 
921 	changed = mesh_accept_plinks_update(sdata);
922 	ieee80211_mbss_info_change_notify(sdata, changed);
923 
924 	mesh_fast_tx_gc(sdata);
925 
926 	mod_timer(&ifmsh->housekeeping_timer,
927 		  round_jiffies(jiffies +
928 				IEEE80211_MESH_HOUSEKEEPING_INTERVAL));
929 }
930 
ieee80211_mesh_rootpath(struct ieee80211_sub_if_data * sdata)931 static void ieee80211_mesh_rootpath(struct ieee80211_sub_if_data *sdata)
932 {
933 	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
934 	u32 interval;
935 
936 	mesh_path_tx_root_frame(sdata);
937 
938 	if (ifmsh->mshcfg.dot11MeshHWMPRootMode == IEEE80211_PROACTIVE_RANN)
939 		interval = ifmsh->mshcfg.dot11MeshHWMPRannInterval;
940 	else
941 		interval = ifmsh->mshcfg.dot11MeshHWMProotInterval;
942 
943 	mod_timer(&ifmsh->mesh_path_root_timer,
944 		  round_jiffies(TU_TO_EXP_TIME(interval)));
945 }
946 
947 static int
ieee80211_mesh_build_beacon(struct ieee80211_if_mesh * ifmsh)948 ieee80211_mesh_build_beacon(struct ieee80211_if_mesh *ifmsh)
949 {
950 	struct beacon_data *bcn;
951 	int head_len, tail_len;
952 	struct sk_buff *skb;
953 	struct ieee80211_mgmt *mgmt;
954 	struct mesh_csa_settings *csa;
955 	const struct ieee80211_supported_band *sband;
956 	u8 ie_len_he_cap, ie_len_eht_cap;
957 	u8 *pos;
958 	struct ieee80211_sub_if_data *sdata;
959 	int hdr_len = offsetofend(struct ieee80211_mgmt, u.beacon);
960 
961 	sdata = container_of(ifmsh, struct ieee80211_sub_if_data, u.mesh);
962 
963 	sband = ieee80211_get_sband(sdata);
964 
965 	ie_len_he_cap = ieee80211_ie_len_he_cap(sdata);
966 	ie_len_eht_cap = ieee80211_ie_len_eht_cap(sdata);
967 	head_len = hdr_len +
968 		   2 + /* NULL SSID */
969 		   /* Channel Switch Announcement */
970 		   2 + sizeof(struct ieee80211_channel_sw_ie) +
971 		   /* Mesh Channel Switch Parameters */
972 		   2 + sizeof(struct ieee80211_mesh_chansw_params_ie) +
973 		   /* Channel Switch Wrapper + Wide Bandwidth CSA IE */
974 		   2 + 2 + sizeof(struct ieee80211_wide_bw_chansw_ie) +
975 		   2 + sizeof(struct ieee80211_sec_chan_offs_ie) +
976 		   2 + 8 + /* supported rates */
977 		   2 + 3; /* DS params */
978 	tail_len = 2 + (IEEE80211_MAX_SUPP_RATES - 8) +
979 		   2 + sizeof(struct ieee80211_ht_cap) +
980 		   2 + sizeof(struct ieee80211_ht_operation) +
981 		   2 + ifmsh->mesh_id_len +
982 		   2 + sizeof(struct ieee80211_meshconf_ie) +
983 		   2 + sizeof(__le16) + /* awake window */
984 		   2 + sizeof(struct ieee80211_vht_cap) +
985 		   2 + sizeof(struct ieee80211_vht_operation) +
986 		   ie_len_he_cap +
987 		   2 + 1 + sizeof(struct ieee80211_he_operation) +
988 			   sizeof(struct ieee80211_he_6ghz_oper) +
989 		   2 + 1 + sizeof(struct ieee80211_he_6ghz_capa) +
990 		   ie_len_eht_cap +
991 		   2 + 1 + offsetof(struct ieee80211_eht_operation, optional) +
992 			   offsetof(struct ieee80211_eht_operation_info, optional) +
993 		   ifmsh->ie_len;
994 
995 	bcn = kzalloc(sizeof(*bcn) + head_len + tail_len, GFP_KERNEL);
996 	/* need an skb for IE builders to operate on */
997 	skb = __dev_alloc_skb(max(head_len, tail_len), GFP_KERNEL);
998 
999 	if (!bcn || !skb)
1000 		goto out_free;
1001 
1002 	/*
1003 	 * pointers go into the block we allocated,
1004 	 * memory is | beacon_data | head | tail |
1005 	 */
1006 	bcn->head = ((u8 *) bcn) + sizeof(*bcn);
1007 
1008 	/* fill in the head */
1009 	mgmt = skb_put_zero(skb, hdr_len);
1010 	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1011 					  IEEE80211_STYPE_BEACON);
1012 	eth_broadcast_addr(mgmt->da);
1013 	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
1014 	memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
1015 	ieee80211_mps_set_frame_flags(sdata, NULL, (void *) mgmt);
1016 	mgmt->u.beacon.beacon_int =
1017 		cpu_to_le16(sdata->vif.bss_conf.beacon_int);
1018 	mgmt->u.beacon.capab_info |= cpu_to_le16(
1019 		sdata->u.mesh.security ? WLAN_CAPABILITY_PRIVACY : 0);
1020 
1021 	pos = skb_put(skb, 2);
1022 	*pos++ = WLAN_EID_SSID;
1023 	*pos++ = 0x0;
1024 
1025 	rcu_read_lock();
1026 	csa = rcu_dereference(ifmsh->csa);
1027 	if (csa) {
1028 		enum nl80211_channel_type ct;
1029 		struct cfg80211_chan_def *chandef;
1030 		int ie_len = 2 + sizeof(struct ieee80211_channel_sw_ie) +
1031 			     2 + sizeof(struct ieee80211_mesh_chansw_params_ie);
1032 
1033 		pos = skb_put_zero(skb, ie_len);
1034 		*pos++ = WLAN_EID_CHANNEL_SWITCH;
1035 		*pos++ = 3;
1036 		*pos++ = 0x0;
1037 		*pos++ = ieee80211_frequency_to_channel(
1038 				csa->settings.chandef.chan->center_freq);
1039 		bcn->cntdwn_current_counter = csa->settings.count;
1040 		bcn->cntdwn_counter_offsets[0] = hdr_len + 6;
1041 		*pos++ = csa->settings.count;
1042 		*pos++ = WLAN_EID_CHAN_SWITCH_PARAM;
1043 		*pos++ = 6;
1044 		if (ifmsh->csa_role == IEEE80211_MESH_CSA_ROLE_INIT) {
1045 			*pos++ = ifmsh->mshcfg.dot11MeshTTL;
1046 			*pos |= WLAN_EID_CHAN_SWITCH_PARAM_INITIATOR;
1047 		} else {
1048 			*pos++ = ifmsh->chsw_ttl;
1049 		}
1050 		*pos++ |= csa->settings.block_tx ?
1051 			  WLAN_EID_CHAN_SWITCH_PARAM_TX_RESTRICT : 0x00;
1052 		put_unaligned_le16(WLAN_REASON_MESH_CHAN, pos);
1053 		pos += 2;
1054 		put_unaligned_le16(ifmsh->pre_value, pos);
1055 		pos += 2;
1056 
1057 		switch (csa->settings.chandef.width) {
1058 		case NL80211_CHAN_WIDTH_40:
1059 			ie_len = 2 + sizeof(struct ieee80211_sec_chan_offs_ie);
1060 			pos = skb_put_zero(skb, ie_len);
1061 
1062 			*pos++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET; /* EID */
1063 			*pos++ = 1;				    /* len */
1064 			ct = cfg80211_get_chandef_type(&csa->settings.chandef);
1065 			if (ct == NL80211_CHAN_HT40PLUS)
1066 				*pos++ = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
1067 			else
1068 				*pos++ = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
1069 			break;
1070 		case NL80211_CHAN_WIDTH_80:
1071 		case NL80211_CHAN_WIDTH_80P80:
1072 		case NL80211_CHAN_WIDTH_160:
1073 			/* Channel Switch Wrapper + Wide Bandwidth CSA IE */
1074 			ie_len = 2 + 2 +
1075 				 sizeof(struct ieee80211_wide_bw_chansw_ie);
1076 			pos = skb_put_zero(skb, ie_len);
1077 
1078 			*pos++ = WLAN_EID_CHANNEL_SWITCH_WRAPPER; /* EID */
1079 			*pos++ = 5;				  /* len */
1080 			/* put sub IE */
1081 			chandef = &csa->settings.chandef;
1082 			ieee80211_ie_build_wide_bw_cs(pos, chandef);
1083 			break;
1084 		default:
1085 			break;
1086 		}
1087 	}
1088 	rcu_read_unlock();
1089 
1090 	if (ieee80211_put_srates_elem(skb, sband,
1091 				      sdata->vif.bss_conf.basic_rates,
1092 				      0, WLAN_EID_SUPP_RATES) ||
1093 	    mesh_add_ds_params_ie(sdata, skb))
1094 		goto out_free;
1095 
1096 	bcn->head_len = skb->len;
1097 	memcpy(bcn->head, skb->data, bcn->head_len);
1098 
1099 	/* now the tail */
1100 	skb_trim(skb, 0);
1101 	bcn->tail = bcn->head + bcn->head_len;
1102 
1103 	if (ieee80211_put_srates_elem(skb, sband,
1104 				      sdata->vif.bss_conf.basic_rates,
1105 				      0, WLAN_EID_EXT_SUPP_RATES) ||
1106 	    mesh_add_rsn_ie(sdata, skb) ||
1107 	    mesh_add_ht_cap_ie(sdata, skb) ||
1108 	    mesh_add_ht_oper_ie(sdata, skb) ||
1109 	    mesh_add_meshid_ie(sdata, skb) ||
1110 	    mesh_add_meshconf_ie(sdata, skb) ||
1111 	    mesh_add_awake_window_ie(sdata, skb) ||
1112 	    mesh_add_vht_cap_ie(sdata, skb) ||
1113 	    mesh_add_vht_oper_ie(sdata, skb) ||
1114 	    mesh_add_he_cap_ie(sdata, skb, ie_len_he_cap) ||
1115 	    mesh_add_he_oper_ie(sdata, skb) ||
1116 	    mesh_add_he_6ghz_cap_ie(sdata, skb) ||
1117 	    mesh_add_eht_cap_ie(sdata, skb, ie_len_eht_cap) ||
1118 	    mesh_add_eht_oper_ie(sdata, skb) ||
1119 	    mesh_add_vendor_ies(sdata, skb))
1120 		goto out_free;
1121 
1122 	bcn->tail_len = skb->len;
1123 	memcpy(bcn->tail, skb->data, bcn->tail_len);
1124 	ieee80211_mesh_update_bss_params(sdata, bcn->tail, bcn->tail_len);
1125 	bcn->meshconf = (struct ieee80211_meshconf_ie *)
1126 					(bcn->tail + ifmsh->meshconf_offset);
1127 
1128 	dev_kfree_skb(skb);
1129 	rcu_assign_pointer(ifmsh->beacon, bcn);
1130 	return 0;
1131 out_free:
1132 	kfree(bcn);
1133 	dev_kfree_skb(skb);
1134 	return -ENOMEM;
1135 }
1136 
1137 static int
ieee80211_mesh_rebuild_beacon(struct ieee80211_sub_if_data * sdata)1138 ieee80211_mesh_rebuild_beacon(struct ieee80211_sub_if_data *sdata)
1139 {
1140 	struct beacon_data *old_bcn;
1141 	int ret;
1142 
1143 	old_bcn = sdata_dereference(sdata->u.mesh.beacon, sdata);
1144 	ret = ieee80211_mesh_build_beacon(&sdata->u.mesh);
1145 	if (ret)
1146 		/* just reuse old beacon */
1147 		return ret;
1148 
1149 	if (old_bcn)
1150 		kfree_rcu(old_bcn, rcu_head);
1151 	return 0;
1152 }
1153 
ieee80211_mbss_info_change_notify(struct ieee80211_sub_if_data * sdata,u64 changed)1154 void ieee80211_mbss_info_change_notify(struct ieee80211_sub_if_data *sdata,
1155 				       u64 changed)
1156 {
1157 	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1158 	unsigned long bits[] = { BITMAP_FROM_U64(changed) };
1159 	u32 bit;
1160 
1161 	if (!changed)
1162 		return;
1163 
1164 	/* if we race with running work, worst case this work becomes a noop */
1165 	for_each_set_bit(bit, bits, sizeof(changed) * BITS_PER_BYTE)
1166 		set_bit(bit, ifmsh->mbss_changed);
1167 	set_bit(MESH_WORK_MBSS_CHANGED, &ifmsh->wrkq_flags);
1168 	wiphy_work_queue(sdata->local->hw.wiphy, &sdata->work);
1169 }
1170 
ieee80211_start_mesh(struct ieee80211_sub_if_data * sdata)1171 int ieee80211_start_mesh(struct ieee80211_sub_if_data *sdata)
1172 {
1173 	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1174 	struct ieee80211_local *local = sdata->local;
1175 	u64 changed = BSS_CHANGED_BEACON |
1176 		      BSS_CHANGED_BEACON_ENABLED |
1177 		      BSS_CHANGED_HT |
1178 		      BSS_CHANGED_BASIC_RATES |
1179 		      BSS_CHANGED_BEACON_INT |
1180 		      BSS_CHANGED_MCAST_RATE;
1181 
1182 	local->fif_other_bss++;
1183 	/* mesh ifaces must set allmulti to forward mcast traffic */
1184 	atomic_inc(&local->iff_allmultis);
1185 	ieee80211_configure_filter(local);
1186 
1187 	ifmsh->mesh_cc_id = 0;	/* Disabled */
1188 	/* register sync ops from extensible synchronization framework */
1189 	ifmsh->sync_ops = ieee80211_mesh_sync_ops_get(ifmsh->mesh_sp_id);
1190 	ifmsh->sync_offset_clockdrift_max = 0;
1191 	set_bit(MESH_WORK_HOUSEKEEPING, &ifmsh->wrkq_flags);
1192 	ieee80211_mesh_root_setup(ifmsh);
1193 	wiphy_work_queue(local->hw.wiphy, &sdata->work);
1194 	sdata->vif.bss_conf.ht_operation_mode =
1195 				ifmsh->mshcfg.ht_opmode;
1196 	sdata->vif.bss_conf.enable_beacon = true;
1197 
1198 	changed |= ieee80211_mps_local_status_update(sdata);
1199 
1200 	if (ieee80211_mesh_build_beacon(ifmsh)) {
1201 		ieee80211_stop_mesh(sdata);
1202 		return -ENOMEM;
1203 	}
1204 
1205 	ieee80211_recalc_dtim(local, sdata);
1206 	ieee80211_link_info_change_notify(sdata, &sdata->deflink, changed);
1207 
1208 	netif_carrier_on(sdata->dev);
1209 	return 0;
1210 }
1211 
ieee80211_stop_mesh(struct ieee80211_sub_if_data * sdata)1212 void ieee80211_stop_mesh(struct ieee80211_sub_if_data *sdata)
1213 {
1214 	struct ieee80211_local *local = sdata->local;
1215 	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1216 	struct beacon_data *bcn;
1217 
1218 	netif_carrier_off(sdata->dev);
1219 
1220 	/* flush STAs and mpaths on this iface */
1221 	sta_info_flush(sdata, -1);
1222 	ieee80211_free_keys(sdata, true);
1223 	mesh_path_flush_by_iface(sdata);
1224 
1225 	/* stop the beacon */
1226 	ifmsh->mesh_id_len = 0;
1227 	sdata->vif.bss_conf.enable_beacon = false;
1228 	sdata->beacon_rate_set = false;
1229 	clear_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED, &sdata->state);
1230 	ieee80211_link_info_change_notify(sdata, &sdata->deflink,
1231 					  BSS_CHANGED_BEACON_ENABLED);
1232 
1233 	/* remove beacon */
1234 	bcn = sdata_dereference(ifmsh->beacon, sdata);
1235 	RCU_INIT_POINTER(ifmsh->beacon, NULL);
1236 	kfree_rcu(bcn, rcu_head);
1237 
1238 	/* free all potentially still buffered group-addressed frames */
1239 	local->total_ps_buffered -= skb_queue_len(&ifmsh->ps.bc_buf);
1240 	skb_queue_purge(&ifmsh->ps.bc_buf);
1241 
1242 	timer_delete_sync(&sdata->u.mesh.housekeeping_timer);
1243 	timer_delete_sync(&sdata->u.mesh.mesh_path_root_timer);
1244 	timer_delete_sync(&sdata->u.mesh.mesh_path_timer);
1245 
1246 	/* clear any mesh work (for next join) we may have accrued */
1247 	ifmsh->wrkq_flags = 0;
1248 	memset(ifmsh->mbss_changed, 0, sizeof(ifmsh->mbss_changed));
1249 
1250 	local->fif_other_bss--;
1251 	atomic_dec(&local->iff_allmultis);
1252 	ieee80211_configure_filter(local);
1253 }
1254 
ieee80211_mesh_csa_mark_radar(struct ieee80211_sub_if_data * sdata)1255 static void ieee80211_mesh_csa_mark_radar(struct ieee80211_sub_if_data *sdata)
1256 {
1257 	int err;
1258 
1259 	/* if the current channel is a DFS channel, mark the channel as
1260 	 * unavailable.
1261 	 */
1262 	err = cfg80211_chandef_dfs_required(sdata->local->hw.wiphy,
1263 					    &sdata->vif.bss_conf.chanreq.oper,
1264 					    NL80211_IFTYPE_MESH_POINT);
1265 	if (err > 0)
1266 		cfg80211_radar_event(sdata->local->hw.wiphy,
1267 				     &sdata->vif.bss_conf.chanreq.oper,
1268 				     GFP_ATOMIC);
1269 }
1270 
1271 static bool
ieee80211_mesh_process_chnswitch(struct ieee80211_sub_if_data * sdata,struct ieee802_11_elems * elems,bool beacon)1272 ieee80211_mesh_process_chnswitch(struct ieee80211_sub_if_data *sdata,
1273 				 struct ieee802_11_elems *elems, bool beacon)
1274 {
1275 	struct cfg80211_csa_settings params;
1276 	struct ieee80211_csa_ie csa_ie;
1277 	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1278 	struct ieee80211_supported_band *sband;
1279 	int err;
1280 	struct ieee80211_conn_settings conn = ieee80211_conn_settings_unlimited;
1281 	u32 vht_cap_info = 0;
1282 
1283 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
1284 
1285 	sband = ieee80211_get_sband(sdata);
1286 	if (!sband)
1287 		return false;
1288 
1289 	switch (sdata->vif.bss_conf.chanreq.oper.width) {
1290 	case NL80211_CHAN_WIDTH_20_NOHT:
1291 		conn.mode = IEEE80211_CONN_MODE_LEGACY;
1292 		conn.bw_limit = IEEE80211_CONN_BW_LIMIT_20;
1293 		break;
1294 	case NL80211_CHAN_WIDTH_20:
1295 		conn.mode = IEEE80211_CONN_MODE_HT;
1296 		conn.bw_limit = IEEE80211_CONN_BW_LIMIT_20;
1297 		break;
1298 	case NL80211_CHAN_WIDTH_40:
1299 		conn.mode = IEEE80211_CONN_MODE_HT;
1300 		conn.bw_limit = IEEE80211_CONN_BW_LIMIT_40;
1301 		break;
1302 	default:
1303 		break;
1304 	}
1305 
1306 	if (elems->vht_cap_elem)
1307 		vht_cap_info =
1308 			le32_to_cpu(elems->vht_cap_elem->vht_cap_info);
1309 
1310 	memset(&params, 0, sizeof(params));
1311 	err = ieee80211_parse_ch_switch_ie(sdata, elems, sband->band,
1312 					   vht_cap_info, &conn,
1313 					   sdata->vif.addr, false,
1314 					   &csa_ie);
1315 	if (err < 0)
1316 		return false;
1317 	if (err)
1318 		return false;
1319 
1320 	/* Mark the channel unavailable if the reason for the switch is
1321 	 * regulatory.
1322 	 */
1323 	if (csa_ie.reason_code == WLAN_REASON_MESH_CHAN_REGULATORY)
1324 		ieee80211_mesh_csa_mark_radar(sdata);
1325 
1326 	params.chandef = csa_ie.chanreq.oper;
1327 	params.count = csa_ie.count;
1328 
1329 	if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, &params.chandef,
1330 				     IEEE80211_CHAN_DISABLED) ||
1331 	    !cfg80211_reg_can_beacon(sdata->local->hw.wiphy, &params.chandef,
1332 				     NL80211_IFTYPE_MESH_POINT)) {
1333 		sdata_info(sdata,
1334 			   "mesh STA %pM switches to unsupported channel (%d MHz, width:%d, CF1/2: %d/%d MHz), aborting\n",
1335 			   sdata->vif.addr,
1336 			   params.chandef.chan->center_freq,
1337 			   params.chandef.width,
1338 			   params.chandef.center_freq1,
1339 			   params.chandef.center_freq2);
1340 		return false;
1341 	}
1342 
1343 	err = cfg80211_chandef_dfs_required(sdata->local->hw.wiphy,
1344 					    &params.chandef,
1345 					    NL80211_IFTYPE_MESH_POINT);
1346 	if (err < 0)
1347 		return false;
1348 	if (err > 0 && !ifmsh->userspace_handles_dfs) {
1349 		sdata_info(sdata,
1350 			   "mesh STA %pM switches to channel requiring DFS (%d MHz, width:%d, CF1/2: %d/%d MHz), aborting\n",
1351 			   sdata->vif.addr,
1352 			   params.chandef.chan->center_freq,
1353 			   params.chandef.width,
1354 			   params.chandef.center_freq1,
1355 			   params.chandef.center_freq2);
1356 		return false;
1357 	}
1358 
1359 	params.radar_required = err;
1360 
1361 	if (cfg80211_chandef_identical(&params.chandef,
1362 				       &sdata->vif.bss_conf.chanreq.oper)) {
1363 		mcsa_dbg(sdata,
1364 			 "received csa with an identical chandef, ignoring\n");
1365 		return true;
1366 	}
1367 
1368 	mcsa_dbg(sdata,
1369 		 "received channel switch announcement to go to channel %d MHz\n",
1370 		 params.chandef.chan->center_freq);
1371 
1372 	params.block_tx = csa_ie.mode & WLAN_EID_CHAN_SWITCH_PARAM_TX_RESTRICT;
1373 	if (beacon) {
1374 		ifmsh->chsw_ttl = csa_ie.ttl - 1;
1375 		if (ifmsh->pre_value >= csa_ie.pre_value)
1376 			return false;
1377 		ifmsh->pre_value = csa_ie.pre_value;
1378 	}
1379 
1380 	if (ifmsh->chsw_ttl >= ifmsh->mshcfg.dot11MeshTTL)
1381 		return false;
1382 
1383 	ifmsh->csa_role = IEEE80211_MESH_CSA_ROLE_REPEATER;
1384 
1385 	if (ieee80211_channel_switch(sdata->local->hw.wiphy, sdata->dev,
1386 				     &params) < 0)
1387 		return false;
1388 
1389 	return true;
1390 }
1391 
1392 static void
ieee80211_mesh_rx_probe_req(struct ieee80211_sub_if_data * sdata,struct ieee80211_mgmt * mgmt,size_t len)1393 ieee80211_mesh_rx_probe_req(struct ieee80211_sub_if_data *sdata,
1394 			    struct ieee80211_mgmt *mgmt, size_t len)
1395 {
1396 	struct ieee80211_local *local = sdata->local;
1397 	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1398 	struct sk_buff *presp;
1399 	struct beacon_data *bcn;
1400 	struct ieee80211_mgmt *hdr;
1401 	struct ieee802_11_elems *elems;
1402 	size_t baselen;
1403 	u8 *pos;
1404 
1405 	pos = mgmt->u.probe_req.variable;
1406 	baselen = (u8 *) pos - (u8 *) mgmt;
1407 	if (baselen > len)
1408 		return;
1409 
1410 	elems = ieee802_11_parse_elems(pos, len - baselen, false, NULL);
1411 	if (!elems)
1412 		return;
1413 
1414 	if (!elems->mesh_id)
1415 		goto free;
1416 
1417 	/* 802.11-2012 10.1.4.3.2 */
1418 	if ((!ether_addr_equal(mgmt->da, sdata->vif.addr) &&
1419 	     !is_broadcast_ether_addr(mgmt->da)) ||
1420 	    elems->ssid_len != 0)
1421 		goto free;
1422 
1423 	if (elems->mesh_id_len != 0 &&
1424 	    (elems->mesh_id_len != ifmsh->mesh_id_len ||
1425 	     memcmp(elems->mesh_id, ifmsh->mesh_id, ifmsh->mesh_id_len)))
1426 		goto free;
1427 
1428 	rcu_read_lock();
1429 	bcn = rcu_dereference(ifmsh->beacon);
1430 
1431 	if (!bcn)
1432 		goto out;
1433 
1434 	presp = dev_alloc_skb(local->tx_headroom +
1435 			      bcn->head_len + bcn->tail_len);
1436 	if (!presp)
1437 		goto out;
1438 
1439 	skb_reserve(presp, local->tx_headroom);
1440 	skb_put_data(presp, bcn->head, bcn->head_len);
1441 	skb_put_data(presp, bcn->tail, bcn->tail_len);
1442 	hdr = (struct ieee80211_mgmt *) presp->data;
1443 	hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1444 					 IEEE80211_STYPE_PROBE_RESP);
1445 	memcpy(hdr->da, mgmt->sa, ETH_ALEN);
1446 	IEEE80211_SKB_CB(presp)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
1447 	ieee80211_tx_skb(sdata, presp);
1448 out:
1449 	rcu_read_unlock();
1450 free:
1451 	kfree(elems);
1452 }
1453 
ieee80211_mesh_rx_bcn_presp(struct ieee80211_sub_if_data * sdata,u16 stype,struct ieee80211_mgmt * mgmt,size_t len,struct ieee80211_rx_status * rx_status)1454 static void ieee80211_mesh_rx_bcn_presp(struct ieee80211_sub_if_data *sdata,
1455 					u16 stype,
1456 					struct ieee80211_mgmt *mgmt,
1457 					size_t len,
1458 					struct ieee80211_rx_status *rx_status)
1459 {
1460 	struct ieee80211_local *local = sdata->local;
1461 	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1462 	struct ieee802_11_elems *elems;
1463 	struct ieee80211_channel *channel;
1464 	size_t baselen;
1465 	int freq;
1466 	enum nl80211_band band = rx_status->band;
1467 
1468 	/* ignore ProbeResp to foreign address */
1469 	if (stype == IEEE80211_STYPE_PROBE_RESP &&
1470 	    !ether_addr_equal(mgmt->da, sdata->vif.addr))
1471 		return;
1472 
1473 	baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
1474 	if (baselen > len)
1475 		return;
1476 
1477 	elems = ieee802_11_parse_elems(mgmt->u.probe_resp.variable,
1478 				       len - baselen,
1479 				       false, NULL);
1480 	if (!elems)
1481 		return;
1482 
1483 	/* ignore non-mesh or secure / insecure mismatch */
1484 	if ((!elems->mesh_id || !elems->mesh_config) ||
1485 	    (elems->rsn && sdata->u.mesh.security == IEEE80211_MESH_SEC_NONE) ||
1486 	    (!elems->rsn && sdata->u.mesh.security != IEEE80211_MESH_SEC_NONE))
1487 		goto free;
1488 
1489 	if (elems->ds_params)
1490 		freq = ieee80211_channel_to_frequency(elems->ds_params[0], band);
1491 	else
1492 		freq = rx_status->freq;
1493 
1494 	channel = ieee80211_get_channel(local->hw.wiphy, freq);
1495 
1496 	if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
1497 		goto free;
1498 
1499 	if (mesh_matches_local(sdata, elems)) {
1500 		mpl_dbg(sdata, "rssi_threshold=%d,rx_status->signal=%d\n",
1501 			sdata->u.mesh.mshcfg.rssi_threshold, rx_status->signal);
1502 		if (!sdata->u.mesh.user_mpm ||
1503 		    sdata->u.mesh.mshcfg.rssi_threshold == 0 ||
1504 		    sdata->u.mesh.mshcfg.rssi_threshold < rx_status->signal)
1505 			mesh_neighbour_update(sdata, mgmt->sa, elems,
1506 					      rx_status);
1507 
1508 		if (ifmsh->csa_role != IEEE80211_MESH_CSA_ROLE_INIT &&
1509 		    !sdata->vif.bss_conf.csa_active)
1510 			ieee80211_mesh_process_chnswitch(sdata, elems, true);
1511 	}
1512 
1513 	if (ifmsh->sync_ops)
1514 		ifmsh->sync_ops->rx_bcn_presp(sdata, stype, mgmt, len,
1515 					      elems->mesh_config, rx_status);
1516 free:
1517 	kfree(elems);
1518 }
1519 
ieee80211_mesh_finish_csa(struct ieee80211_sub_if_data * sdata,u64 * changed)1520 int ieee80211_mesh_finish_csa(struct ieee80211_sub_if_data *sdata, u64 *changed)
1521 {
1522 	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1523 	struct mesh_csa_settings *tmp_csa_settings;
1524 	int ret = 0;
1525 
1526 	/* Reset the TTL value and Initiator flag */
1527 	ifmsh->csa_role = IEEE80211_MESH_CSA_ROLE_NONE;
1528 	ifmsh->chsw_ttl = 0;
1529 
1530 	/* Remove the CSA and MCSP elements from the beacon */
1531 	tmp_csa_settings = sdata_dereference(ifmsh->csa, sdata);
1532 	RCU_INIT_POINTER(ifmsh->csa, NULL);
1533 	if (tmp_csa_settings)
1534 		kfree_rcu(tmp_csa_settings, rcu_head);
1535 	ret = ieee80211_mesh_rebuild_beacon(sdata);
1536 	if (ret)
1537 		return -EINVAL;
1538 
1539 	*changed |= BSS_CHANGED_BEACON;
1540 
1541 	mcsa_dbg(sdata, "complete switching to center freq %d MHz",
1542 		 sdata->vif.bss_conf.chanreq.oper.chan->center_freq);
1543 	return 0;
1544 }
1545 
ieee80211_mesh_csa_beacon(struct ieee80211_sub_if_data * sdata,struct cfg80211_csa_settings * csa_settings,u64 * changed)1546 int ieee80211_mesh_csa_beacon(struct ieee80211_sub_if_data *sdata,
1547 			      struct cfg80211_csa_settings *csa_settings,
1548 			      u64 *changed)
1549 {
1550 	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1551 	struct mesh_csa_settings *tmp_csa_settings;
1552 	int ret = 0;
1553 
1554 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
1555 
1556 	tmp_csa_settings = kmalloc(sizeof(*tmp_csa_settings),
1557 				   GFP_ATOMIC);
1558 	if (!tmp_csa_settings)
1559 		return -ENOMEM;
1560 
1561 	memcpy(&tmp_csa_settings->settings, csa_settings,
1562 	       sizeof(struct cfg80211_csa_settings));
1563 
1564 	rcu_assign_pointer(ifmsh->csa, tmp_csa_settings);
1565 
1566 	ret = ieee80211_mesh_rebuild_beacon(sdata);
1567 	if (ret) {
1568 		tmp_csa_settings = rcu_dereference(ifmsh->csa);
1569 		RCU_INIT_POINTER(ifmsh->csa, NULL);
1570 		kfree_rcu(tmp_csa_settings, rcu_head);
1571 		return ret;
1572 	}
1573 
1574 	*changed |= BSS_CHANGED_BEACON;
1575 	return 0;
1576 }
1577 
mesh_fwd_csa_frame(struct ieee80211_sub_if_data * sdata,struct ieee80211_mgmt * mgmt,size_t len,struct ieee802_11_elems * elems)1578 static int mesh_fwd_csa_frame(struct ieee80211_sub_if_data *sdata,
1579 			       struct ieee80211_mgmt *mgmt, size_t len,
1580 			       struct ieee802_11_elems *elems)
1581 {
1582 	struct ieee80211_mgmt *mgmt_fwd;
1583 	struct sk_buff *skb;
1584 	struct ieee80211_local *local = sdata->local;
1585 
1586 	skb = dev_alloc_skb(local->tx_headroom + len);
1587 	if (!skb)
1588 		return -ENOMEM;
1589 	skb_reserve(skb, local->tx_headroom);
1590 	mgmt_fwd = skb_put(skb, len);
1591 
1592 	elems->mesh_chansw_params_ie->mesh_ttl--;
1593 	elems->mesh_chansw_params_ie->mesh_flags &=
1594 		~WLAN_EID_CHAN_SWITCH_PARAM_INITIATOR;
1595 
1596 	memcpy(mgmt_fwd, mgmt, len);
1597 	eth_broadcast_addr(mgmt_fwd->da);
1598 	memcpy(mgmt_fwd->sa, sdata->vif.addr, ETH_ALEN);
1599 	memcpy(mgmt_fwd->bssid, sdata->vif.addr, ETH_ALEN);
1600 
1601 	ieee80211_tx_skb(sdata, skb);
1602 	return 0;
1603 }
1604 
mesh_rx_csa_frame(struct ieee80211_sub_if_data * sdata,struct ieee80211_mgmt * mgmt,size_t len)1605 static void mesh_rx_csa_frame(struct ieee80211_sub_if_data *sdata,
1606 			      struct ieee80211_mgmt *mgmt, size_t len)
1607 {
1608 	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1609 	struct ieee802_11_elems *elems;
1610 	u16 pre_value;
1611 	bool fwd_csa = true;
1612 	size_t baselen;
1613 	u8 *pos;
1614 
1615 	if (mgmt->u.action.u.measurement.action_code !=
1616 	    WLAN_ACTION_SPCT_CHL_SWITCH)
1617 		return;
1618 
1619 	pos = mgmt->u.action.u.chan_switch.variable;
1620 	baselen = offsetof(struct ieee80211_mgmt,
1621 			   u.action.u.chan_switch.variable);
1622 	elems = ieee802_11_parse_elems(pos, len - baselen, true, NULL);
1623 	if (!elems)
1624 		return;
1625 
1626 	if (!mesh_matches_local(sdata, elems))
1627 		goto free;
1628 
1629 	ifmsh->chsw_ttl = elems->mesh_chansw_params_ie->mesh_ttl;
1630 	if (!--ifmsh->chsw_ttl)
1631 		fwd_csa = false;
1632 
1633 	pre_value = le16_to_cpu(elems->mesh_chansw_params_ie->mesh_pre_value);
1634 	if (ifmsh->pre_value >= pre_value)
1635 		goto free;
1636 
1637 	ifmsh->pre_value = pre_value;
1638 
1639 	if (!sdata->vif.bss_conf.csa_active &&
1640 	    !ieee80211_mesh_process_chnswitch(sdata, elems, false)) {
1641 		mcsa_dbg(sdata, "Failed to process CSA action frame");
1642 		goto free;
1643 	}
1644 
1645 	/* forward or re-broadcast the CSA frame */
1646 	if (fwd_csa) {
1647 		if (mesh_fwd_csa_frame(sdata, mgmt, len, elems) < 0)
1648 			mcsa_dbg(sdata, "Failed to forward the CSA frame");
1649 	}
1650 free:
1651 	kfree(elems);
1652 }
1653 
ieee80211_mesh_rx_mgmt_action(struct ieee80211_sub_if_data * sdata,struct ieee80211_mgmt * mgmt,size_t len,struct ieee80211_rx_status * rx_status)1654 static void ieee80211_mesh_rx_mgmt_action(struct ieee80211_sub_if_data *sdata,
1655 					  struct ieee80211_mgmt *mgmt,
1656 					  size_t len,
1657 					  struct ieee80211_rx_status *rx_status)
1658 {
1659 	switch (mgmt->u.action.category) {
1660 	case WLAN_CATEGORY_SELF_PROTECTED:
1661 		switch (mgmt->u.action.u.self_prot.action_code) {
1662 		case WLAN_SP_MESH_PEERING_OPEN:
1663 		case WLAN_SP_MESH_PEERING_CLOSE:
1664 		case WLAN_SP_MESH_PEERING_CONFIRM:
1665 			mesh_rx_plink_frame(sdata, mgmt, len, rx_status);
1666 			break;
1667 		}
1668 		break;
1669 	case WLAN_CATEGORY_MESH_ACTION:
1670 		if (mesh_action_is_path_sel(mgmt))
1671 			mesh_rx_path_sel_frame(sdata, mgmt, len);
1672 		break;
1673 	case WLAN_CATEGORY_SPECTRUM_MGMT:
1674 		mesh_rx_csa_frame(sdata, mgmt, len);
1675 		break;
1676 	}
1677 }
1678 
ieee80211_mesh_rx_queued_mgmt(struct ieee80211_sub_if_data * sdata,struct sk_buff * skb)1679 void ieee80211_mesh_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
1680 				   struct sk_buff *skb)
1681 {
1682 	struct ieee80211_rx_status *rx_status;
1683 	struct ieee80211_mgmt *mgmt;
1684 	u16 stype;
1685 
1686 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
1687 
1688 	/* mesh already went down */
1689 	if (!sdata->u.mesh.mesh_id_len)
1690 		return;
1691 
1692 	rx_status = IEEE80211_SKB_RXCB(skb);
1693 	mgmt = (struct ieee80211_mgmt *) skb->data;
1694 	stype = le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE;
1695 
1696 	switch (stype) {
1697 	case IEEE80211_STYPE_PROBE_RESP:
1698 	case IEEE80211_STYPE_BEACON:
1699 		ieee80211_mesh_rx_bcn_presp(sdata, stype, mgmt, skb->len,
1700 					    rx_status);
1701 		break;
1702 	case IEEE80211_STYPE_PROBE_REQ:
1703 		ieee80211_mesh_rx_probe_req(sdata, mgmt, skb->len);
1704 		break;
1705 	case IEEE80211_STYPE_ACTION:
1706 		ieee80211_mesh_rx_mgmt_action(sdata, mgmt, skb->len, rx_status);
1707 		break;
1708 	}
1709 }
1710 
mesh_bss_info_changed(struct ieee80211_sub_if_data * sdata)1711 static void mesh_bss_info_changed(struct ieee80211_sub_if_data *sdata)
1712 {
1713 	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1714 	u32 bit;
1715 	u64 changed = 0;
1716 
1717 	for_each_set_bit(bit, ifmsh->mbss_changed,
1718 			 sizeof(changed) * BITS_PER_BYTE) {
1719 		clear_bit(bit, ifmsh->mbss_changed);
1720 		changed |= BIT(bit);
1721 	}
1722 
1723 	if (sdata->vif.bss_conf.enable_beacon &&
1724 	    (changed & (BSS_CHANGED_BEACON |
1725 			BSS_CHANGED_HT |
1726 			BSS_CHANGED_BASIC_RATES |
1727 			BSS_CHANGED_BEACON_INT)))
1728 		if (ieee80211_mesh_rebuild_beacon(sdata))
1729 			return;
1730 
1731 	ieee80211_link_info_change_notify(sdata, &sdata->deflink, changed);
1732 }
1733 
ieee80211_mesh_work(struct ieee80211_sub_if_data * sdata)1734 void ieee80211_mesh_work(struct ieee80211_sub_if_data *sdata)
1735 {
1736 	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1737 
1738 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
1739 
1740 	/* mesh already went down */
1741 	if (!sdata->u.mesh.mesh_id_len)
1742 		return;
1743 
1744 	if (ifmsh->preq_queue_len &&
1745 	    time_after(jiffies,
1746 		       ifmsh->last_preq + msecs_to_jiffies(ifmsh->mshcfg.dot11MeshHWMPpreqMinInterval)))
1747 		mesh_path_start_discovery(sdata);
1748 
1749 	if (test_and_clear_bit(MESH_WORK_HOUSEKEEPING, &ifmsh->wrkq_flags))
1750 		ieee80211_mesh_housekeeping(sdata);
1751 
1752 	if (test_and_clear_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags))
1753 		ieee80211_mesh_rootpath(sdata);
1754 
1755 	if (test_and_clear_bit(MESH_WORK_DRIFT_ADJUST, &ifmsh->wrkq_flags))
1756 		mesh_sync_adjust_tsf(sdata);
1757 
1758 	if (test_and_clear_bit(MESH_WORK_MBSS_CHANGED, &ifmsh->wrkq_flags))
1759 		mesh_bss_info_changed(sdata);
1760 }
1761 
1762 
ieee80211_mesh_init_sdata(struct ieee80211_sub_if_data * sdata)1763 void ieee80211_mesh_init_sdata(struct ieee80211_sub_if_data *sdata)
1764 {
1765 	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1766 	static u8 zero_addr[ETH_ALEN] = {};
1767 
1768 	timer_setup(&ifmsh->housekeeping_timer,
1769 		    ieee80211_mesh_housekeeping_timer, 0);
1770 
1771 	ifmsh->accepting_plinks = true;
1772 	atomic_set(&ifmsh->mpaths, 0);
1773 	mesh_rmc_init(sdata);
1774 	ifmsh->last_preq = jiffies;
1775 	ifmsh->next_perr = jiffies;
1776 	ifmsh->csa_role = IEEE80211_MESH_CSA_ROLE_NONE;
1777 	ifmsh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
1778 	/* Allocate all mesh structures when creating the first mesh interface. */
1779 	if (!mesh_allocated)
1780 		ieee80211s_init();
1781 
1782 	mesh_pathtbl_init(sdata);
1783 
1784 	timer_setup(&ifmsh->mesh_path_timer, ieee80211_mesh_path_timer, 0);
1785 	timer_setup(&ifmsh->mesh_path_root_timer,
1786 		    ieee80211_mesh_path_root_timer, 0);
1787 	INIT_LIST_HEAD(&ifmsh->preq_queue.list);
1788 	skb_queue_head_init(&ifmsh->ps.bc_buf);
1789 	spin_lock_init(&ifmsh->mesh_preq_queue_lock);
1790 	spin_lock_init(&ifmsh->sync_offset_lock);
1791 	RCU_INIT_POINTER(ifmsh->beacon, NULL);
1792 
1793 	sdata->vif.bss_conf.bssid = zero_addr;
1794 }
1795 
ieee80211_mesh_teardown_sdata(struct ieee80211_sub_if_data * sdata)1796 void ieee80211_mesh_teardown_sdata(struct ieee80211_sub_if_data *sdata)
1797 {
1798 	mesh_rmc_free(sdata);
1799 	mesh_pathtbl_unregister(sdata);
1800 }
1801