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