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