1 // SPDX-License-Identifier: GPL-2.0-only
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>
9 #include <linux/slab.h>
10 #include <asm/unaligned.h>
11 #include "ieee80211_i.h"
14 #include "driver-ops.h"
16 static int mesh_allocated;
17 static struct kmem_cache *rm_cache;
19 bool mesh_action_is_path_sel(struct ieee80211_mgmt *mgmt)
21 return (mgmt->u.action.u.mesh_action.action_code ==
22 WLAN_MESH_ACTION_HWMP_PATH_SELECTION);
25 void ieee80211s_init(void)
28 rm_cache = kmem_cache_create("mesh_rmc", sizeof(struct rmc_entry),
32 void ieee80211s_stop(void)
36 kmem_cache_destroy(rm_cache);
39 static void ieee80211_mesh_housekeeping_timer(struct timer_list *t)
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;
46 set_bit(MESH_WORK_HOUSEKEEPING, &ifmsh->wrkq_flags);
48 wiphy_work_queue(local->hw.wiphy, &sdata->work);
52 * mesh_matches_local - check if the config of a mesh point matches ours
54 * @sdata: local mesh subif
55 * @ie: information elements of a management frame from the mesh peer
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.
60 bool mesh_matches_local(struct ieee80211_sub_if_data *sdata,
61 struct ieee802_11_elems *ie)
63 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
65 struct cfg80211_chan_def sta_chan_def;
66 struct ieee80211_supported_band *sband;
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
77 * - Power management control on fc
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)))
88 sband = ieee80211_get_sband(sdata);
92 ieee80211_sta_get_rates(sdata, ie, sband->band,
95 if (sdata->vif.bss_conf.basic_rates != basic_rates)
98 cfg80211_chandef_create(&sta_chan_def, sdata->vif.bss_conf.chandef.chan,
100 ieee80211_chandef_ht_oper(ie->ht_operation, &sta_chan_def);
102 if (ie->vht_cap_elem)
103 vht_cap_info = le32_to_cpu(ie->vht_cap_elem->vht_cap_info);
105 ieee80211_chandef_vht_oper(&sdata->local->hw, vht_cap_info,
106 ie->vht_operation, ie->ht_operation,
108 ieee80211_chandef_he_6ghz_oper(sdata, ie->he_operation, ie->eht_operation,
111 if (!cfg80211_chandef_compatible(&sdata->vif.bss_conf.chandef,
119 * mesh_peer_accepts_plinks - check if an mp is willing to establish peer links
121 * @ie: information elements of a management frame from the mesh peer
123 bool mesh_peer_accepts_plinks(struct ieee802_11_elems *ie)
125 return (ie->mesh_config->meshconf_cap &
126 IEEE80211_MESHCONF_CAPAB_ACCEPT_PLINKS) != 0;
130 * mesh_accept_plinks_update - update accepting_plink in local mesh beacons
132 * @sdata: mesh interface in which mesh beacons are going to be updated
134 * Returns: beacon changed flag if the beacon content changed.
136 u64 mesh_accept_plinks_update(struct ieee80211_sub_if_data *sdata)
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
147 free_plinks = mesh_plink_availables(sdata);
149 if (free_plinks != sdata->u.mesh.accepting_plinks) {
150 sdata->u.mesh.accepting_plinks = free_plinks;
151 changed = BSS_CHANGED_BEACON;
158 * mesh_sta_cleanup - clean up any mesh sta state
160 * @sta: mesh sta to clean up.
162 void mesh_sta_cleanup(struct sta_info *sta)
164 struct ieee80211_sub_if_data *sdata = sta->sdata;
165 u64 changed = mesh_plink_deactivate(sta);
168 ieee80211_mbss_info_change_notify(sdata, changed);
171 int mesh_rmc_init(struct ieee80211_sub_if_data *sdata)
175 sdata->u.mesh.rmc = kmalloc(sizeof(struct mesh_rmc), GFP_KERNEL);
176 if (!sdata->u.mesh.rmc)
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]);
184 void mesh_rmc_free(struct ieee80211_sub_if_data *sdata)
186 struct mesh_rmc *rmc = sdata->u.mesh.rmc;
188 struct hlist_node *n;
191 if (!sdata->u.mesh.rmc)
194 for (i = 0; i < RMC_BUCKETS; i++) {
195 hlist_for_each_entry_safe(p, n, &rmc->bucket[i], list) {
197 kmem_cache_free(rm_cache, p);
202 sdata->u.mesh.rmc = NULL;
206 * mesh_rmc_check - Check frame in recent multicast cache and add if absent.
209 * @sa: source address
210 * @mesh_hdr: mesh_header
212 * Returns: 0 if the frame is not in the cache, nonzero otherwise.
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
218 int mesh_rmc_check(struct ieee80211_sub_if_data *sdata,
219 const u8 *sa, struct ieee80211s_hdr *mesh_hdr)
221 struct mesh_rmc *rmc = sdata->u.mesh.rmc;
226 struct hlist_node *n;
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) {
236 if (time_after(jiffies, p->exp_time) ||
237 entries == RMC_QUEUE_MAX_LEN) {
239 kmem_cache_free(rm_cache, p);
241 } else if ((seqnum == p->seqnum) && ether_addr_equal(sa, p->sa))
245 p = kmem_cache_alloc(rm_cache, GFP_ATOMIC);
250 p->exp_time = jiffies + RMC_TIMEOUT;
251 memcpy(p->sa, sa, ETH_ALEN);
252 hlist_add_head(&p->list, &rmc->bucket[idx]);
256 int mesh_add_meshconf_ie(struct ieee80211_sub_if_data *sdata,
259 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
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;
267 if (skb_tailroom(skb) < 2 + meshconf_len)
270 pos = skb_put(skb, 2 + meshconf_len);
271 *pos++ = WLAN_EID_MESH_CONFIG;
272 *pos++ = meshconf_len;
274 /* save a pointer for quick updates in pre-tbtt */
275 ifmsh->meshconf_offset = pos - skb->data;
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) |
292 is_connected_to_gate;
293 /* Mesh capability */
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;
305 int mesh_add_meshid_ie(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
307 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
310 if (skb_tailroom(skb) < 2 + ifmsh->mesh_id_len)
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);
322 static int mesh_add_awake_window_ie(struct ieee80211_sub_if_data *sdata,
325 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
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)
334 if (skb_tailroom(skb) < 4)
337 pos = skb_put(skb, 2 + 2);
338 *pos++ = WLAN_EID_MESH_AWAKE_WINDOW;
340 put_unaligned_le16(ifmsh->mshcfg.dot11MeshAwakeWindowDuration, pos);
345 int mesh_add_vendor_ies(struct ieee80211_sub_if_data *sdata,
348 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
352 if (!ifmsh->ie || !ifmsh->ie_len)
355 /* fast-forward to vendor IEs */
356 offset = ieee80211_ie_split_vendor(ifmsh->ie, ifmsh->ie_len, 0);
358 if (offset < ifmsh->ie_len) {
359 len = ifmsh->ie_len - offset;
360 data = ifmsh->ie + offset;
361 if (skb_tailroom(skb) < len)
363 skb_put_data(skb, data, len);
369 int mesh_add_rsn_ie(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
371 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
375 if (!ifmsh->ie || !ifmsh->ie_len)
379 data = cfg80211_find_ie(WLAN_EID_RSN, ifmsh->ie, ifmsh->ie_len);
385 if (skb_tailroom(skb) < len)
387 skb_put_data(skb, data, len);
392 static int mesh_add_ds_params_ie(struct ieee80211_sub_if_data *sdata,
395 struct ieee80211_chanctx_conf *chanctx_conf;
396 struct ieee80211_channel *chan;
399 if (skb_tailroom(skb) < 3)
403 chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
404 if (WARN_ON(!chanctx_conf)) {
408 chan = chanctx_conf->def.chan;
411 pos = skb_put(skb, 2 + 1);
412 *pos++ = WLAN_EID_DS_PARAMS;
414 *pos++ = ieee80211_frequency_to_channel(chan->center_freq);
419 int mesh_add_ht_cap_ie(struct ieee80211_sub_if_data *sdata,
422 struct ieee80211_supported_band *sband;
425 sband = ieee80211_get_sband(sdata);
429 /* HT not allowed in 6 GHz */
430 if (sband->band == NL80211_BAND_6GHZ)
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)
439 if (skb_tailroom(skb) < 2 + sizeof(struct ieee80211_ht_cap))
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);
448 int mesh_add_ht_oper_ie(struct ieee80211_sub_if_data *sdata,
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;
459 chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
460 if (WARN_ON(!chanctx_conf)) {
464 channel = chanctx_conf->def.chan;
467 sband = local->hw.wiphy->bands[channel->band];
468 ht_cap = &sband->ht_cap;
470 /* HT not allowed in 6 GHz */
471 if (sband->band == NL80211_BAND_6GHZ)
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)
480 if (skb_tailroom(skb) < 2 + sizeof(struct ieee80211_ht_operation))
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,
491 int mesh_add_vht_cap_ie(struct ieee80211_sub_if_data *sdata,
494 struct ieee80211_supported_band *sband;
497 sband = ieee80211_get_sband(sdata);
501 /* VHT not allowed in 6 GHz */
502 if (sband->band == NL80211_BAND_6GHZ)
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)
511 if (skb_tailroom(skb) < 2 + sizeof(struct ieee80211_vht_cap))
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);
520 int mesh_add_vht_oper_ie(struct ieee80211_sub_if_data *sdata,
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;
531 chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
532 if (WARN_ON(!chanctx_conf)) {
536 channel = chanctx_conf->def.chan;
539 sband = local->hw.wiphy->bands[channel->band];
540 vht_cap = &sband->vht_cap;
542 /* VHT not allowed in 6 GHz */
543 if (sband->band == NL80211_BAND_6GHZ)
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)
552 if (skb_tailroom(skb) < 2 + sizeof(struct ieee80211_vht_operation))
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);
562 int mesh_add_he_cap_ie(struct ieee80211_sub_if_data *sdata,
563 struct sk_buff *skb, u8 ie_len)
565 const struct ieee80211_sta_he_cap *he_cap;
566 struct ieee80211_supported_band *sband;
569 sband = ieee80211_get_sband(sdata);
573 he_cap = ieee80211_get_he_iftype_cap(sband, NL80211_IFTYPE_MESH_POINT);
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)
581 if (skb_tailroom(skb) < ie_len)
584 pos = skb_put(skb, ie_len);
585 ieee80211_ie_build_he_cap(0, pos, he_cap, pos + ie_len);
590 int mesh_add_he_oper_ie(struct ieee80211_sub_if_data *sdata,
593 const struct ieee80211_sta_he_cap *he_cap;
594 struct ieee80211_supported_band *sband;
598 sband = ieee80211_get_sband(sdata);
602 he_cap = ieee80211_get_he_iftype_cap(sband, NL80211_IFTYPE_MESH_POINT);
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)
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);
613 if (skb_tailroom(skb) < len)
616 pos = skb_put(skb, len);
617 ieee80211_ie_build_he_oper(pos, &sdata->vif.bss_conf.chandef);
622 int mesh_add_he_6ghz_cap_ie(struct ieee80211_sub_if_data *sdata,
625 struct ieee80211_supported_band *sband;
626 const struct ieee80211_sband_iftype_data *iftd;
628 sband = ieee80211_get_sband(sdata);
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 */
638 ieee80211_ie_build_he_6ghz_cap(sdata, sdata->deflink.smps_mode, skb);
642 int mesh_add_eht_cap_ie(struct ieee80211_sub_if_data *sdata,
643 struct sk_buff *skb, u8 ie_len)
645 const struct ieee80211_sta_he_cap *he_cap;
646 const struct ieee80211_sta_eht_cap *eht_cap;
647 struct ieee80211_supported_band *sband;
650 sband = ieee80211_get_sband(sdata);
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)
662 if (skb_tailroom(skb) < ie_len)
665 pos = skb_put(skb, ie_len);
666 ieee80211_ie_build_eht_cap(pos, he_cap, eht_cap, pos + ie_len, false);
671 int mesh_add_eht_oper_ie(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
673 const struct ieee80211_sta_eht_cap *eht_cap;
674 struct ieee80211_supported_band *sband;
678 sband = ieee80211_get_sband(sdata);
682 eht_cap = ieee80211_get_eht_iftype_cap(sband, NL80211_IFTYPE_MESH_POINT);
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)
689 len = 2 + 1 + offsetof(struct ieee80211_eht_operation, optional) +
690 offsetof(struct ieee80211_eht_operation_info, optional);
692 if (skb_tailroom(skb) < len)
695 pos = skb_put(skb, len);
696 ieee80211_ie_build_eht_oper(pos, &sdata->vif.bss_conf.chandef, eht_cap);
701 static void ieee80211_mesh_path_timer(struct timer_list *t)
703 struct ieee80211_sub_if_data *sdata =
704 from_timer(sdata, t, u.mesh.mesh_path_timer);
706 wiphy_work_queue(sdata->local->hw.wiphy, &sdata->work);
709 static void ieee80211_mesh_path_root_timer(struct timer_list *t)
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;
715 set_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags);
717 wiphy_work_queue(sdata->local->hw.wiphy, &sdata->work);
720 void ieee80211_mesh_root_setup(struct ieee80211_if_mesh *ifmsh)
722 if (ifmsh->mshcfg.dot11MeshHWMPRootMode > IEEE80211_ROOTMODE_ROOT)
723 set_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags);
725 clear_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags);
726 /* stop running timer */
727 del_timer_sync(&ifmsh->mesh_path_root_timer);
732 ieee80211_mesh_update_bss_params(struct ieee80211_sub_if_data *sdata,
735 struct ieee80211_supported_band *sband;
736 const struct element *cap;
737 const struct ieee80211_he_operation *he_oper = NULL;
739 sband = ieee80211_get_sband(sdata);
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)
749 sdata->vif.bss_conf.he_support = true;
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);
757 sdata->vif.bss_conf.he_oper.params =
758 __le32_to_cpu(he_oper->he_oper_params);
760 sdata->vif.bss_conf.eht_support =
761 !!ieee80211_get_eht_iftype_cap(sband, NL80211_IFTYPE_MESH_POINT);
764 bool ieee80211_mesh_xmit_fast(struct ieee80211_sub_if_data *sdata,
765 struct sk_buff *skb, u32 ctrl_flags)
767 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
768 struct ieee80211_mesh_fast_tx *entry;
769 struct ieee80211s_hdr *meshhdr;
770 u8 sa[ETH_ALEN] __aligned(2);
771 struct tid_ampdu_tx *tid_tx;
772 struct sta_info *sta;
773 bool copy_sa = false;
777 if (ctrl_flags & IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP)
780 if (ifmsh->mshcfg.dot11MeshNolearn)
783 /* Add support for these cases later */
784 if (ifmsh->ps_peers_light_sleep || ifmsh->ps_peers_deep_sleep)
787 if (is_multicast_ether_addr(skb->data))
790 ethertype = (skb->data[12] << 8) | skb->data[13];
791 if (ethertype < ETH_P_802_3_MIN)
794 if (skb->sk && skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS)
797 if (skb->ip_summed == CHECKSUM_PARTIAL) {
798 skb_set_transport_header(skb, skb_checksum_start_offset(skb));
799 if (skb_checksum_help(skb))
803 entry = mesh_fast_tx_get(sdata, skb->data);
807 if (skb_headroom(skb) < entry->hdrlen + entry->fast_tx.hdr_len)
810 sta = rcu_dereference(entry->mpath->next_hop);
814 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
815 tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
817 if (!test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state))
820 tid_tx->last_tx = jiffies;
823 skb = skb_share_check(skb, GFP_ATOMIC);
827 skb_set_queue_mapping(skb, ieee80211_select_queue(sdata, sta, skb));
829 meshhdr = (struct ieee80211s_hdr *)entry->hdr;
830 if ((meshhdr->flags & MESH_FLAGS_AE) == MESH_FLAGS_AE_A5_A6) {
831 /* preserve SA from eth header for 6-addr frames */
832 ether_addr_copy(sa, skb->data + ETH_ALEN);
836 memcpy(skb_push(skb, entry->hdrlen - 2 * ETH_ALEN), entry->hdr,
839 meshhdr = (struct ieee80211s_hdr *)skb->data;
840 put_unaligned_le32(atomic_inc_return(&sdata->u.mesh.mesh_seqnum),
842 meshhdr->ttl = sdata->u.mesh.mshcfg.dot11MeshTTL;
844 ether_addr_copy(meshhdr->eaddr2, sa);
846 skb_push(skb, 2 * ETH_ALEN);
847 __ieee80211_xmit_fast(sdata, sta, &entry->fast_tx, skb, tid_tx,
848 entry->mpath->dst, sdata->vif.addr);
854 * ieee80211_fill_mesh_addresses - fill addresses of a locally originated mesh frame
855 * @hdr: 802.11 frame header
856 * @fc: frame control field
857 * @meshda: destination address in the mesh
858 * @meshsa: source address in the mesh. Same as TA, as frame is
859 * locally originated.
861 * Return the length of the 802.11 (does not include a mesh control header)
863 int ieee80211_fill_mesh_addresses(struct ieee80211_hdr *hdr, __le16 *fc,
864 const u8 *meshda, const u8 *meshsa)
866 if (is_multicast_ether_addr(meshda)) {
867 *fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
869 memcpy(hdr->addr1, meshda, ETH_ALEN);
870 memcpy(hdr->addr2, meshsa, ETH_ALEN);
871 memcpy(hdr->addr3, meshsa, ETH_ALEN);
874 *fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
876 eth_zero_addr(hdr->addr1); /* RA is resolved later */
877 memcpy(hdr->addr2, meshsa, ETH_ALEN);
878 memcpy(hdr->addr3, meshda, ETH_ALEN);
879 memcpy(hdr->addr4, meshsa, ETH_ALEN);
885 * ieee80211_new_mesh_header - create a new mesh header
886 * @sdata: mesh interface to be used
887 * @meshhdr: uninitialized mesh header
888 * @addr4or5: 1st address in the ae header, which may correspond to address 4
889 * (if addr6 is NULL) or address 5 (if addr6 is present). It may
891 * @addr6: 2nd address in the ae header, which corresponds to addr6 of the
894 * Return the header length.
896 unsigned int ieee80211_new_mesh_header(struct ieee80211_sub_if_data *sdata,
897 struct ieee80211s_hdr *meshhdr,
898 const char *addr4or5, const char *addr6)
900 if (WARN_ON(!addr4or5 && addr6))
903 memset(meshhdr, 0, sizeof(*meshhdr));
905 meshhdr->ttl = sdata->u.mesh.mshcfg.dot11MeshTTL;
907 put_unaligned_le32(atomic_inc_return(&sdata->u.mesh.mesh_seqnum),
909 if (addr4or5 && !addr6) {
910 meshhdr->flags |= MESH_FLAGS_AE_A4;
911 memcpy(meshhdr->eaddr1, addr4or5, ETH_ALEN);
913 } else if (addr4or5 && addr6) {
914 meshhdr->flags |= MESH_FLAGS_AE_A5_A6;
915 memcpy(meshhdr->eaddr1, addr4or5, ETH_ALEN);
916 memcpy(meshhdr->eaddr2, addr6, ETH_ALEN);
923 static void ieee80211_mesh_housekeeping(struct ieee80211_sub_if_data *sdata)
925 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
928 if (ifmsh->mshcfg.plink_timeout > 0)
929 ieee80211_sta_expire(sdata, ifmsh->mshcfg.plink_timeout * HZ);
930 mesh_path_expire(sdata);
932 changed = mesh_accept_plinks_update(sdata);
933 ieee80211_mbss_info_change_notify(sdata, changed);
935 mesh_fast_tx_gc(sdata);
937 mod_timer(&ifmsh->housekeeping_timer,
938 round_jiffies(jiffies +
939 IEEE80211_MESH_HOUSEKEEPING_INTERVAL));
942 static void ieee80211_mesh_rootpath(struct ieee80211_sub_if_data *sdata)
944 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
947 mesh_path_tx_root_frame(sdata);
949 if (ifmsh->mshcfg.dot11MeshHWMPRootMode == IEEE80211_PROACTIVE_RANN)
950 interval = ifmsh->mshcfg.dot11MeshHWMPRannInterval;
952 interval = ifmsh->mshcfg.dot11MeshHWMProotInterval;
954 mod_timer(&ifmsh->mesh_path_root_timer,
955 round_jiffies(TU_TO_EXP_TIME(interval)));
959 ieee80211_mesh_build_beacon(struct ieee80211_if_mesh *ifmsh)
961 struct beacon_data *bcn;
962 int head_len, tail_len;
964 struct ieee80211_mgmt *mgmt;
965 struct ieee80211_chanctx_conf *chanctx_conf;
966 struct mesh_csa_settings *csa;
967 enum nl80211_band band;
968 u8 ie_len_he_cap, ie_len_eht_cap;
970 struct ieee80211_sub_if_data *sdata;
971 int hdr_len = offsetofend(struct ieee80211_mgmt, u.beacon);
973 sdata = container_of(ifmsh, struct ieee80211_sub_if_data, u.mesh);
975 chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
976 band = chanctx_conf->def.chan->band;
979 ie_len_he_cap = ieee80211_ie_len_he_cap(sdata,
980 NL80211_IFTYPE_MESH_POINT);
981 ie_len_eht_cap = ieee80211_ie_len_eht_cap(sdata,
982 NL80211_IFTYPE_MESH_POINT);
985 /* Channel Switch Announcement */
986 2 + sizeof(struct ieee80211_channel_sw_ie) +
987 /* Mesh Channel Switch Parameters */
988 2 + sizeof(struct ieee80211_mesh_chansw_params_ie) +
989 /* Channel Switch Wrapper + Wide Bandwidth CSA IE */
990 2 + 2 + sizeof(struct ieee80211_wide_bw_chansw_ie) +
991 2 + sizeof(struct ieee80211_sec_chan_offs_ie) +
992 2 + 8 + /* supported rates */
993 2 + 3; /* DS params */
994 tail_len = 2 + (IEEE80211_MAX_SUPP_RATES - 8) +
995 2 + sizeof(struct ieee80211_ht_cap) +
996 2 + sizeof(struct ieee80211_ht_operation) +
997 2 + ifmsh->mesh_id_len +
998 2 + sizeof(struct ieee80211_meshconf_ie) +
999 2 + sizeof(__le16) + /* awake window */
1000 2 + sizeof(struct ieee80211_vht_cap) +
1001 2 + sizeof(struct ieee80211_vht_operation) +
1003 2 + 1 + sizeof(struct ieee80211_he_operation) +
1004 sizeof(struct ieee80211_he_6ghz_oper) +
1005 2 + 1 + sizeof(struct ieee80211_he_6ghz_capa) +
1007 2 + 1 + offsetof(struct ieee80211_eht_operation, optional) +
1008 offsetof(struct ieee80211_eht_operation_info, optional) +
1011 bcn = kzalloc(sizeof(*bcn) + head_len + tail_len, GFP_KERNEL);
1012 /* need an skb for IE builders to operate on */
1013 skb = __dev_alloc_skb(max(head_len, tail_len), GFP_KERNEL);
1019 * pointers go into the block we allocated,
1020 * memory is | beacon_data | head | tail |
1022 bcn->head = ((u8 *) bcn) + sizeof(*bcn);
1024 /* fill in the head */
1025 mgmt = skb_put_zero(skb, hdr_len);
1026 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1027 IEEE80211_STYPE_BEACON);
1028 eth_broadcast_addr(mgmt->da);
1029 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
1030 memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
1031 ieee80211_mps_set_frame_flags(sdata, NULL, (void *) mgmt);
1032 mgmt->u.beacon.beacon_int =
1033 cpu_to_le16(sdata->vif.bss_conf.beacon_int);
1034 mgmt->u.beacon.capab_info |= cpu_to_le16(
1035 sdata->u.mesh.security ? WLAN_CAPABILITY_PRIVACY : 0);
1037 pos = skb_put(skb, 2);
1038 *pos++ = WLAN_EID_SSID;
1042 csa = rcu_dereference(ifmsh->csa);
1044 enum nl80211_channel_type ct;
1045 struct cfg80211_chan_def *chandef;
1046 int ie_len = 2 + sizeof(struct ieee80211_channel_sw_ie) +
1047 2 + sizeof(struct ieee80211_mesh_chansw_params_ie);
1049 pos = skb_put_zero(skb, ie_len);
1050 *pos++ = WLAN_EID_CHANNEL_SWITCH;
1053 *pos++ = ieee80211_frequency_to_channel(
1054 csa->settings.chandef.chan->center_freq);
1055 bcn->cntdwn_current_counter = csa->settings.count;
1056 bcn->cntdwn_counter_offsets[0] = hdr_len + 6;
1057 *pos++ = csa->settings.count;
1058 *pos++ = WLAN_EID_CHAN_SWITCH_PARAM;
1060 if (ifmsh->csa_role == IEEE80211_MESH_CSA_ROLE_INIT) {
1061 *pos++ = ifmsh->mshcfg.dot11MeshTTL;
1062 *pos |= WLAN_EID_CHAN_SWITCH_PARAM_INITIATOR;
1064 *pos++ = ifmsh->chsw_ttl;
1066 *pos++ |= csa->settings.block_tx ?
1067 WLAN_EID_CHAN_SWITCH_PARAM_TX_RESTRICT : 0x00;
1068 put_unaligned_le16(WLAN_REASON_MESH_CHAN, pos);
1070 put_unaligned_le16(ifmsh->pre_value, pos);
1073 switch (csa->settings.chandef.width) {
1074 case NL80211_CHAN_WIDTH_40:
1075 ie_len = 2 + sizeof(struct ieee80211_sec_chan_offs_ie);
1076 pos = skb_put_zero(skb, ie_len);
1078 *pos++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET; /* EID */
1079 *pos++ = 1; /* len */
1080 ct = cfg80211_get_chandef_type(&csa->settings.chandef);
1081 if (ct == NL80211_CHAN_HT40PLUS)
1082 *pos++ = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
1084 *pos++ = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
1086 case NL80211_CHAN_WIDTH_80:
1087 case NL80211_CHAN_WIDTH_80P80:
1088 case NL80211_CHAN_WIDTH_160:
1089 /* Channel Switch Wrapper + Wide Bandwidth CSA IE */
1091 sizeof(struct ieee80211_wide_bw_chansw_ie);
1092 pos = skb_put_zero(skb, ie_len);
1094 *pos++ = WLAN_EID_CHANNEL_SWITCH_WRAPPER; /* EID */
1095 *pos++ = 5; /* len */
1097 chandef = &csa->settings.chandef;
1098 ieee80211_ie_build_wide_bw_cs(pos, chandef);
1106 if (ieee80211_add_srates_ie(sdata, skb, true, band) ||
1107 mesh_add_ds_params_ie(sdata, skb))
1110 bcn->head_len = skb->len;
1111 memcpy(bcn->head, skb->data, bcn->head_len);
1115 bcn->tail = bcn->head + bcn->head_len;
1117 if (ieee80211_add_ext_srates_ie(sdata, skb, true, band) ||
1118 mesh_add_rsn_ie(sdata, skb) ||
1119 mesh_add_ht_cap_ie(sdata, skb) ||
1120 mesh_add_ht_oper_ie(sdata, skb) ||
1121 mesh_add_meshid_ie(sdata, skb) ||
1122 mesh_add_meshconf_ie(sdata, skb) ||
1123 mesh_add_awake_window_ie(sdata, skb) ||
1124 mesh_add_vht_cap_ie(sdata, skb) ||
1125 mesh_add_vht_oper_ie(sdata, skb) ||
1126 mesh_add_he_cap_ie(sdata, skb, ie_len_he_cap) ||
1127 mesh_add_he_oper_ie(sdata, skb) ||
1128 mesh_add_he_6ghz_cap_ie(sdata, skb) ||
1129 mesh_add_eht_cap_ie(sdata, skb, ie_len_eht_cap) ||
1130 mesh_add_eht_oper_ie(sdata, skb) ||
1131 mesh_add_vendor_ies(sdata, skb))
1134 bcn->tail_len = skb->len;
1135 memcpy(bcn->tail, skb->data, bcn->tail_len);
1136 ieee80211_mesh_update_bss_params(sdata, bcn->tail, bcn->tail_len);
1137 bcn->meshconf = (struct ieee80211_meshconf_ie *)
1138 (bcn->tail + ifmsh->meshconf_offset);
1141 rcu_assign_pointer(ifmsh->beacon, bcn);
1150 ieee80211_mesh_rebuild_beacon(struct ieee80211_sub_if_data *sdata)
1152 struct beacon_data *old_bcn;
1155 old_bcn = sdata_dereference(sdata->u.mesh.beacon, sdata);
1156 ret = ieee80211_mesh_build_beacon(&sdata->u.mesh);
1158 /* just reuse old beacon */
1162 kfree_rcu(old_bcn, rcu_head);
1166 void ieee80211_mbss_info_change_notify(struct ieee80211_sub_if_data *sdata,
1169 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1170 unsigned long bits = changed;
1176 /* if we race with running work, worst case this work becomes a noop */
1177 for_each_set_bit(bit, &bits, sizeof(changed) * BITS_PER_BYTE)
1178 set_bit(bit, ifmsh->mbss_changed);
1179 set_bit(MESH_WORK_MBSS_CHANGED, &ifmsh->wrkq_flags);
1180 wiphy_work_queue(sdata->local->hw.wiphy, &sdata->work);
1183 int ieee80211_start_mesh(struct ieee80211_sub_if_data *sdata)
1185 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1186 struct ieee80211_local *local = sdata->local;
1187 u64 changed = BSS_CHANGED_BEACON |
1188 BSS_CHANGED_BEACON_ENABLED |
1190 BSS_CHANGED_BASIC_RATES |
1191 BSS_CHANGED_BEACON_INT |
1192 BSS_CHANGED_MCAST_RATE;
1194 local->fif_other_bss++;
1195 /* mesh ifaces must set allmulti to forward mcast traffic */
1196 atomic_inc(&local->iff_allmultis);
1197 ieee80211_configure_filter(local);
1199 ifmsh->mesh_cc_id = 0; /* Disabled */
1200 /* register sync ops from extensible synchronization framework */
1201 ifmsh->sync_ops = ieee80211_mesh_sync_ops_get(ifmsh->mesh_sp_id);
1202 ifmsh->sync_offset_clockdrift_max = 0;
1203 set_bit(MESH_WORK_HOUSEKEEPING, &ifmsh->wrkq_flags);
1204 ieee80211_mesh_root_setup(ifmsh);
1205 wiphy_work_queue(local->hw.wiphy, &sdata->work);
1206 sdata->vif.bss_conf.ht_operation_mode =
1207 ifmsh->mshcfg.ht_opmode;
1208 sdata->vif.bss_conf.enable_beacon = true;
1210 changed |= ieee80211_mps_local_status_update(sdata);
1212 if (ieee80211_mesh_build_beacon(ifmsh)) {
1213 ieee80211_stop_mesh(sdata);
1217 ieee80211_recalc_dtim(local, sdata);
1218 ieee80211_link_info_change_notify(sdata, &sdata->deflink, changed);
1220 netif_carrier_on(sdata->dev);
1224 void ieee80211_stop_mesh(struct ieee80211_sub_if_data *sdata)
1226 struct ieee80211_local *local = sdata->local;
1227 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1228 struct beacon_data *bcn;
1230 netif_carrier_off(sdata->dev);
1232 /* flush STAs and mpaths on this iface */
1233 sta_info_flush(sdata);
1234 ieee80211_free_keys(sdata, true);
1235 mesh_path_flush_by_iface(sdata);
1237 /* stop the beacon */
1238 ifmsh->mesh_id_len = 0;
1239 sdata->vif.bss_conf.enable_beacon = false;
1240 sdata->beacon_rate_set = false;
1241 clear_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED, &sdata->state);
1242 ieee80211_link_info_change_notify(sdata, &sdata->deflink,
1243 BSS_CHANGED_BEACON_ENABLED);
1246 bcn = sdata_dereference(ifmsh->beacon, sdata);
1247 RCU_INIT_POINTER(ifmsh->beacon, NULL);
1248 kfree_rcu(bcn, rcu_head);
1250 /* free all potentially still buffered group-addressed frames */
1251 local->total_ps_buffered -= skb_queue_len(&ifmsh->ps.bc_buf);
1252 skb_queue_purge(&ifmsh->ps.bc_buf);
1254 del_timer_sync(&sdata->u.mesh.housekeeping_timer);
1255 del_timer_sync(&sdata->u.mesh.mesh_path_root_timer);
1256 del_timer_sync(&sdata->u.mesh.mesh_path_timer);
1258 /* clear any mesh work (for next join) we may have accrued */
1259 ifmsh->wrkq_flags = 0;
1260 memset(ifmsh->mbss_changed, 0, sizeof(ifmsh->mbss_changed));
1262 local->fif_other_bss--;
1263 atomic_dec(&local->iff_allmultis);
1264 ieee80211_configure_filter(local);
1267 static void ieee80211_mesh_csa_mark_radar(struct ieee80211_sub_if_data *sdata)
1271 /* if the current channel is a DFS channel, mark the channel as
1274 err = cfg80211_chandef_dfs_required(sdata->local->hw.wiphy,
1275 &sdata->vif.bss_conf.chandef,
1276 NL80211_IFTYPE_MESH_POINT);
1278 cfg80211_radar_event(sdata->local->hw.wiphy,
1279 &sdata->vif.bss_conf.chandef, GFP_ATOMIC);
1283 ieee80211_mesh_process_chnswitch(struct ieee80211_sub_if_data *sdata,
1284 struct ieee802_11_elems *elems, bool beacon)
1286 struct cfg80211_csa_settings params;
1287 struct ieee80211_csa_ie csa_ie;
1288 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1289 struct ieee80211_supported_band *sband;
1291 ieee80211_conn_flags_t conn_flags = 0;
1292 u32 vht_cap_info = 0;
1294 sdata_assert_lock(sdata);
1296 sband = ieee80211_get_sband(sdata);
1300 switch (sdata->vif.bss_conf.chandef.width) {
1301 case NL80211_CHAN_WIDTH_20_NOHT:
1302 conn_flags |= IEEE80211_CONN_DISABLE_HT;
1304 case NL80211_CHAN_WIDTH_20:
1305 conn_flags |= IEEE80211_CONN_DISABLE_40MHZ;
1307 case NL80211_CHAN_WIDTH_40:
1308 conn_flags |= IEEE80211_CONN_DISABLE_VHT;
1314 if (elems->vht_cap_elem)
1316 le32_to_cpu(elems->vht_cap_elem->vht_cap_info);
1318 memset(¶ms, 0, sizeof(params));
1319 err = ieee80211_parse_ch_switch_ie(sdata, elems, sband->band,
1321 conn_flags, sdata->vif.addr,
1328 /* Mark the channel unavailable if the reason for the switch is
1331 if (csa_ie.reason_code == WLAN_REASON_MESH_CHAN_REGULATORY)
1332 ieee80211_mesh_csa_mark_radar(sdata);
1334 params.chandef = csa_ie.chandef;
1335 params.count = csa_ie.count;
1337 if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, ¶ms.chandef,
1338 IEEE80211_CHAN_DISABLED) ||
1339 !cfg80211_reg_can_beacon(sdata->local->hw.wiphy, ¶ms.chandef,
1340 NL80211_IFTYPE_MESH_POINT)) {
1342 "mesh STA %pM switches to unsupported channel (%d MHz, width:%d, CF1/2: %d/%d MHz), aborting\n",
1344 params.chandef.chan->center_freq,
1345 params.chandef.width,
1346 params.chandef.center_freq1,
1347 params.chandef.center_freq2);
1351 err = cfg80211_chandef_dfs_required(sdata->local->hw.wiphy,
1353 NL80211_IFTYPE_MESH_POINT);
1356 if (err > 0 && !ifmsh->userspace_handles_dfs) {
1358 "mesh STA %pM switches to channel requiring DFS (%d MHz, width:%d, CF1/2: %d/%d MHz), aborting\n",
1360 params.chandef.chan->center_freq,
1361 params.chandef.width,
1362 params.chandef.center_freq1,
1363 params.chandef.center_freq2);
1367 params.radar_required = err;
1369 if (cfg80211_chandef_identical(¶ms.chandef,
1370 &sdata->vif.bss_conf.chandef)) {
1372 "received csa with an identical chandef, ignoring\n");
1377 "received channel switch announcement to go to channel %d MHz\n",
1378 params.chandef.chan->center_freq);
1380 params.block_tx = csa_ie.mode & WLAN_EID_CHAN_SWITCH_PARAM_TX_RESTRICT;
1382 ifmsh->chsw_ttl = csa_ie.ttl - 1;
1383 if (ifmsh->pre_value >= csa_ie.pre_value)
1385 ifmsh->pre_value = csa_ie.pre_value;
1388 if (ifmsh->chsw_ttl >= ifmsh->mshcfg.dot11MeshTTL)
1391 ifmsh->csa_role = IEEE80211_MESH_CSA_ROLE_REPEATER;
1393 if (ieee80211_channel_switch(sdata->local->hw.wiphy, sdata->dev,
1401 ieee80211_mesh_rx_probe_req(struct ieee80211_sub_if_data *sdata,
1402 struct ieee80211_mgmt *mgmt, size_t len)
1404 struct ieee80211_local *local = sdata->local;
1405 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1406 struct sk_buff *presp;
1407 struct beacon_data *bcn;
1408 struct ieee80211_mgmt *hdr;
1409 struct ieee802_11_elems *elems;
1413 pos = mgmt->u.probe_req.variable;
1414 baselen = (u8 *) pos - (u8 *) mgmt;
1418 elems = ieee802_11_parse_elems(pos, len - baselen, false, NULL);
1422 if (!elems->mesh_id)
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)
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)))
1437 bcn = rcu_dereference(ifmsh->beacon);
1442 presp = dev_alloc_skb(local->tx_headroom +
1443 bcn->head_len + bcn->tail_len);
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);
1462 static void ieee80211_mesh_rx_bcn_presp(struct ieee80211_sub_if_data *sdata,
1464 struct ieee80211_mgmt *mgmt,
1466 struct ieee80211_rx_status *rx_status)
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;
1474 enum nl80211_band band = rx_status->band;
1476 /* ignore ProbeResp to foreign address */
1477 if (stype == IEEE80211_STYPE_PROBE_RESP &&
1478 !ether_addr_equal(mgmt->da, sdata->vif.addr))
1481 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
1485 elems = ieee802_11_parse_elems(mgmt->u.probe_resp.variable,
1491 /* ignore non-mesh or secure / unsecure mismatch */
1492 if ((!elems->mesh_id || !elems->mesh_config) ||
1493 (elems->rsn && sdata->u.mesh.security == IEEE80211_MESH_SEC_NONE) ||
1494 (!elems->rsn && sdata->u.mesh.security != IEEE80211_MESH_SEC_NONE))
1497 if (elems->ds_params)
1498 freq = ieee80211_channel_to_frequency(elems->ds_params[0], band);
1500 freq = rx_status->freq;
1502 channel = ieee80211_get_channel(local->hw.wiphy, freq);
1504 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
1507 if (mesh_matches_local(sdata, elems)) {
1508 mpl_dbg(sdata, "rssi_threshold=%d,rx_status->signal=%d\n",
1509 sdata->u.mesh.mshcfg.rssi_threshold, rx_status->signal);
1510 if (!sdata->u.mesh.user_mpm ||
1511 sdata->u.mesh.mshcfg.rssi_threshold == 0 ||
1512 sdata->u.mesh.mshcfg.rssi_threshold < rx_status->signal)
1513 mesh_neighbour_update(sdata, mgmt->sa, elems,
1516 if (ifmsh->csa_role != IEEE80211_MESH_CSA_ROLE_INIT &&
1517 !sdata->vif.bss_conf.csa_active)
1518 ieee80211_mesh_process_chnswitch(sdata, elems, true);
1521 if (ifmsh->sync_ops)
1522 ifmsh->sync_ops->rx_bcn_presp(sdata, stype, mgmt, len,
1523 elems->mesh_config, rx_status);
1528 int ieee80211_mesh_finish_csa(struct ieee80211_sub_if_data *sdata, u64 *changed)
1530 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1531 struct mesh_csa_settings *tmp_csa_settings;
1534 /* Reset the TTL value and Initiator flag */
1535 ifmsh->csa_role = IEEE80211_MESH_CSA_ROLE_NONE;
1536 ifmsh->chsw_ttl = 0;
1538 /* Remove the CSA and MCSP elements from the beacon */
1539 tmp_csa_settings = sdata_dereference(ifmsh->csa, sdata);
1540 RCU_INIT_POINTER(ifmsh->csa, NULL);
1541 if (tmp_csa_settings)
1542 kfree_rcu(tmp_csa_settings, rcu_head);
1543 ret = ieee80211_mesh_rebuild_beacon(sdata);
1547 *changed |= BSS_CHANGED_BEACON;
1549 mcsa_dbg(sdata, "complete switching to center freq %d MHz",
1550 sdata->vif.bss_conf.chandef.chan->center_freq);
1554 int ieee80211_mesh_csa_beacon(struct ieee80211_sub_if_data *sdata,
1555 struct cfg80211_csa_settings *csa_settings,
1558 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1559 struct mesh_csa_settings *tmp_csa_settings;
1562 lockdep_assert_held(&sdata->wdev.mtx);
1564 tmp_csa_settings = kmalloc(sizeof(*tmp_csa_settings),
1566 if (!tmp_csa_settings)
1569 memcpy(&tmp_csa_settings->settings, csa_settings,
1570 sizeof(struct cfg80211_csa_settings));
1572 rcu_assign_pointer(ifmsh->csa, tmp_csa_settings);
1574 ret = ieee80211_mesh_rebuild_beacon(sdata);
1576 tmp_csa_settings = rcu_dereference(ifmsh->csa);
1577 RCU_INIT_POINTER(ifmsh->csa, NULL);
1578 kfree_rcu(tmp_csa_settings, rcu_head);
1582 *changed |= BSS_CHANGED_BEACON;
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)
1590 struct ieee80211_mgmt *mgmt_fwd;
1591 struct sk_buff *skb;
1592 struct ieee80211_local *local = sdata->local;
1594 skb = dev_alloc_skb(local->tx_headroom + len);
1597 skb_reserve(skb, local->tx_headroom);
1598 mgmt_fwd = skb_put(skb, len);
1600 elems->mesh_chansw_params_ie->mesh_ttl--;
1601 elems->mesh_chansw_params_ie->mesh_flags &=
1602 ~WLAN_EID_CHAN_SWITCH_PARAM_INITIATOR;
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);
1609 ieee80211_tx_skb(sdata, skb);
1613 static void mesh_rx_csa_frame(struct ieee80211_sub_if_data *sdata,
1614 struct ieee80211_mgmt *mgmt, size_t len)
1616 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1617 struct ieee802_11_elems *elems;
1619 bool fwd_csa = true;
1623 if (mgmt->u.action.u.measurement.action_code !=
1624 WLAN_ACTION_SPCT_CHL_SWITCH)
1627 pos = mgmt->u.action.u.chan_switch.variable;
1628 baselen = offsetof(struct ieee80211_mgmt,
1629 u.action.u.chan_switch.variable);
1630 elems = ieee802_11_parse_elems(pos, len - baselen, true, NULL);
1634 if (!mesh_matches_local(sdata, elems))
1637 ifmsh->chsw_ttl = elems->mesh_chansw_params_ie->mesh_ttl;
1638 if (!--ifmsh->chsw_ttl)
1641 pre_value = le16_to_cpu(elems->mesh_chansw_params_ie->mesh_pre_value);
1642 if (ifmsh->pre_value >= pre_value)
1645 ifmsh->pre_value = pre_value;
1647 if (!sdata->vif.bss_conf.csa_active &&
1648 !ieee80211_mesh_process_chnswitch(sdata, elems, false)) {
1649 mcsa_dbg(sdata, "Failed to process CSA action frame");
1653 /* forward or re-broadcast the CSA frame */
1655 if (mesh_fwd_csa_frame(sdata, mgmt, len, elems) < 0)
1656 mcsa_dbg(sdata, "Failed to forward the CSA frame");
1662 static void ieee80211_mesh_rx_mgmt_action(struct ieee80211_sub_if_data *sdata,
1663 struct ieee80211_mgmt *mgmt,
1665 struct ieee80211_rx_status *rx_status)
1667 switch (mgmt->u.action.category) {
1668 case WLAN_CATEGORY_SELF_PROTECTED:
1669 switch (mgmt->u.action.u.self_prot.action_code) {
1670 case WLAN_SP_MESH_PEERING_OPEN:
1671 case WLAN_SP_MESH_PEERING_CLOSE:
1672 case WLAN_SP_MESH_PEERING_CONFIRM:
1673 mesh_rx_plink_frame(sdata, mgmt, len, rx_status);
1677 case WLAN_CATEGORY_MESH_ACTION:
1678 if (mesh_action_is_path_sel(mgmt))
1679 mesh_rx_path_sel_frame(sdata, mgmt, len);
1681 case WLAN_CATEGORY_SPECTRUM_MGMT:
1682 mesh_rx_csa_frame(sdata, mgmt, len);
1687 void ieee80211_mesh_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
1688 struct sk_buff *skb)
1690 struct ieee80211_rx_status *rx_status;
1691 struct ieee80211_mgmt *mgmt;
1696 /* mesh already went down */
1697 if (!sdata->u.mesh.mesh_id_len)
1700 rx_status = IEEE80211_SKB_RXCB(skb);
1701 mgmt = (struct ieee80211_mgmt *) skb->data;
1702 stype = le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE;
1705 case IEEE80211_STYPE_PROBE_RESP:
1706 case IEEE80211_STYPE_BEACON:
1707 ieee80211_mesh_rx_bcn_presp(sdata, stype, mgmt, skb->len,
1710 case IEEE80211_STYPE_PROBE_REQ:
1711 ieee80211_mesh_rx_probe_req(sdata, mgmt, skb->len);
1713 case IEEE80211_STYPE_ACTION:
1714 ieee80211_mesh_rx_mgmt_action(sdata, mgmt, skb->len, rx_status);
1718 sdata_unlock(sdata);
1721 static void mesh_bss_info_changed(struct ieee80211_sub_if_data *sdata)
1723 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1727 for_each_set_bit(bit, ifmsh->mbss_changed,
1728 sizeof(changed) * BITS_PER_BYTE) {
1729 clear_bit(bit, ifmsh->mbss_changed);
1730 changed |= BIT(bit);
1733 if (sdata->vif.bss_conf.enable_beacon &&
1734 (changed & (BSS_CHANGED_BEACON |
1736 BSS_CHANGED_BASIC_RATES |
1737 BSS_CHANGED_BEACON_INT)))
1738 if (ieee80211_mesh_rebuild_beacon(sdata))
1741 ieee80211_link_info_change_notify(sdata, &sdata->deflink, changed);
1744 void ieee80211_mesh_work(struct ieee80211_sub_if_data *sdata)
1746 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1750 /* mesh already went down */
1751 if (!sdata->u.mesh.mesh_id_len)
1754 if (ifmsh->preq_queue_len &&
1756 ifmsh->last_preq + msecs_to_jiffies(ifmsh->mshcfg.dot11MeshHWMPpreqMinInterval)))
1757 mesh_path_start_discovery(sdata);
1759 if (test_and_clear_bit(MESH_WORK_HOUSEKEEPING, &ifmsh->wrkq_flags))
1760 ieee80211_mesh_housekeeping(sdata);
1762 if (test_and_clear_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags))
1763 ieee80211_mesh_rootpath(sdata);
1765 if (test_and_clear_bit(MESH_WORK_DRIFT_ADJUST, &ifmsh->wrkq_flags))
1766 mesh_sync_adjust_tsf(sdata);
1768 if (test_and_clear_bit(MESH_WORK_MBSS_CHANGED, &ifmsh->wrkq_flags))
1769 mesh_bss_info_changed(sdata);
1771 sdata_unlock(sdata);
1775 void ieee80211_mesh_init_sdata(struct ieee80211_sub_if_data *sdata)
1777 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1778 static u8 zero_addr[ETH_ALEN] = {};
1780 timer_setup(&ifmsh->housekeeping_timer,
1781 ieee80211_mesh_housekeeping_timer, 0);
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 /* Allocate all mesh structures when creating the first mesh interface. */
1790 if (!mesh_allocated)
1793 mesh_pathtbl_init(sdata);
1795 timer_setup(&ifmsh->mesh_path_timer, ieee80211_mesh_path_timer, 0);
1796 timer_setup(&ifmsh->mesh_path_root_timer,
1797 ieee80211_mesh_path_root_timer, 0);
1798 INIT_LIST_HEAD(&ifmsh->preq_queue.list);
1799 skb_queue_head_init(&ifmsh->ps.bc_buf);
1800 spin_lock_init(&ifmsh->mesh_preq_queue_lock);
1801 spin_lock_init(&ifmsh->sync_offset_lock);
1802 RCU_INIT_POINTER(ifmsh->beacon, NULL);
1804 sdata->vif.bss_conf.bssid = zero_addr;
1807 void ieee80211_mesh_teardown_sdata(struct ieee80211_sub_if_data *sdata)
1809 mesh_rmc_free(sdata);
1810 mesh_pathtbl_unregister(sdata);