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