blob: 5f1ca25b6c97e0f4b3e199c7b7473daacffb24de [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +01002/*
Rui Paulo264d9b72009-11-09 23:46:58 +00003 * Copyright (c) 2008, 2009 open80211s Ltd.
Johannes Berg2a333a02020-05-28 21:34:35 +02004 * Copyright (C) 2018 - 2020 Intel Corporation
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +01005 * Authors: Luis Carlos Cobo <luisca@cozybit.com>
6 * Javier Cardona <javier@cozybit.com>
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +01007 */
8
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
Luis Carlos Cobo51cedda2008-04-23 12:15:29 -070010#include <asm/unaligned.h>
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +010011#include "ieee80211_i.h"
12#include "mesh.h"
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -070013#include "driver-ops.h"
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +010014
Johannes Bergbf7cd942013-02-15 14:40:31 +010015static int mesh_allocated;
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +010016static struct kmem_cache *rm_cache;
17
Thomas Pedersen25d49e42011-08-11 19:35:15 -070018bool mesh_action_is_path_sel(struct ieee80211_mgmt *mgmt)
19{
20 return (mgmt->u.action.u.mesh_action.action_code ==
21 WLAN_MESH_ACTION_HWMP_PATH_SELECTION);
22}
Thomas Pedersen25d49e42011-08-11 19:35:15 -070023
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +010024void ieee80211s_init(void)
25{
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +010026 mesh_allocated = 1;
27 rm_cache = kmem_cache_create("mesh_rmc", sizeof(struct rmc_entry),
28 0, 0, NULL);
29}
30
31void ieee80211s_stop(void)
32{
Johannes Bergbf7cd942013-02-15 14:40:31 +010033 if (!mesh_allocated)
34 return;
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +010035 kmem_cache_destroy(rm_cache);
36}
37
Kees Cook34f11cd2017-10-16 16:35:49 -070038static void ieee80211_mesh_housekeeping_timer(struct timer_list *t)
Johannes Berg472dbc42008-09-11 00:01:49 +020039{
Kees Cook34f11cd2017-10-16 16:35:49 -070040 struct ieee80211_sub_if_data *sdata =
41 from_timer(sdata, t, u.mesh.housekeeping_timer);
Johannes Berg472dbc42008-09-11 00:01:49 +020042 struct ieee80211_local *local = sdata->local;
43 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
44
Rui Paulo6b9ac442009-10-20 21:21:48 +010045 set_bit(MESH_WORK_HOUSEKEEPING, &ifmsh->wrkq_flags);
Johannes Berg5bb644a2009-05-17 11:40:42 +020046
Johannes Berg64592c82010-06-10 10:21:31 +020047 ieee80211_queue_work(&local->hw, &sdata->work);
Johannes Berg472dbc42008-09-11 00:01:49 +020048}
49
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +010050/**
51 * mesh_matches_local - check if the config of a mesh point matches ours
52 *
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +120053 * @sdata: local mesh subif
Thomas Pedersenf743ff42012-04-18 19:23:43 -070054 * @ie: information elements of a management frame from the mesh peer
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +010055 *
56 * This function checks if the mesh configuration of a mesh point matches the
57 * local mesh configuration, i.e. if both nodes belong to the same mesh network.
58 */
Thomas Pedersenf743ff42012-04-18 19:23:43 -070059bool mesh_matches_local(struct ieee80211_sub_if_data *sdata,
60 struct ieee802_11_elems *ie)
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +010061{
Johannes Berg472dbc42008-09-11 00:01:49 +020062 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Thomas Pedersenf743ff42012-04-18 19:23:43 -070063 u32 basic_rates = 0;
Johannes Berg4bf88532012-11-09 11:39:59 +010064 struct cfg80211_chan_def sta_chan_def;
Mohammed Shafi Shajakhan21a8e9d2017-04-27 12:45:38 +053065 struct ieee80211_supported_band *sband;
Johannes Berg2a333a02020-05-28 21:34:35 +020066 u32 vht_cap_info = 0;
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +010067
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +010068 /*
69 * As support for each feature is added, check for matching
70 * - On mesh config capabilities
71 * - Power Save Support En
72 * - Sync support enabled
73 * - Sync support active
74 * - Sync support required from peer
75 * - MDA enabled
76 * - Power management control on fc
77 */
Thomas Pedersen739522b2011-10-26 14:47:28 -070078 if (!(ifmsh->mesh_id_len == ie->mesh_id_len &&
79 memcmp(ifmsh->mesh_id, ie->mesh_id, ie->mesh_id_len) == 0 &&
80 (ifmsh->mesh_pp_id == ie->mesh_config->meshconf_psel) &&
81 (ifmsh->mesh_pm_id == ie->mesh_config->meshconf_pmetric) &&
82 (ifmsh->mesh_cc_id == ie->mesh_config->meshconf_congest) &&
83 (ifmsh->mesh_sp_id == ie->mesh_config->meshconf_synch) &&
84 (ifmsh->mesh_auth_id == ie->mesh_config->meshconf_auth)))
Johannes Bergbf7cd942013-02-15 14:40:31 +010085 return false;
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +010086
Mohammed Shafi Shajakhan21a8e9d2017-04-27 12:45:38 +053087 sband = ieee80211_get_sband(sdata);
88 if (!sband)
89 return false;
90
91 ieee80211_sta_get_rates(sdata, ie, sband->band,
Thomas Pedersenf743ff42012-04-18 19:23:43 -070092 &basic_rates);
93
Ashok Nagarajanfe40cb62012-04-02 21:21:22 -070094 if (sdata->vif.bss_conf.basic_rates != basic_rates)
Johannes Bergbf7cd942013-02-15 14:40:31 +010095 return false;
Ashok Nagarajanfe40cb62012-04-02 21:21:22 -070096
Johannes Berg8ac3c7042015-12-18 15:08:34 +010097 cfg80211_chandef_create(&sta_chan_def, sdata->vif.bss_conf.chandef.chan,
98 NL80211_CHAN_NO_HT);
99 ieee80211_chandef_ht_oper(ie->ht_operation, &sta_chan_def);
Johannes Berg2a333a02020-05-28 21:34:35 +0200100
101 if (ie->vht_cap_elem)
102 vht_cap_info = le32_to_cpu(ie->vht_cap_elem->vht_cap_info);
103
104 ieee80211_chandef_vht_oper(&sdata->local->hw, vht_cap_info,
Johannes Berg7eb26df2018-08-31 11:31:18 +0300105 ie->vht_operation, ie->ht_operation,
106 &sta_chan_def);
Johannes Berg57fa5e82020-05-28 21:34:36 +0200107 ieee80211_chandef_he_6ghz_oper(sdata, ie->he_operation, &sta_chan_def);
Bob Copelandc85fb532015-08-27 09:00:18 -0400108
Johannes Berg4bf88532012-11-09 11:39:59 +0100109 if (!cfg80211_chandef_compatible(&sdata->vif.bss_conf.chandef,
110 &sta_chan_def))
Johannes Bergbf7cd942013-02-15 14:40:31 +0100111 return false;
Thomas Pedersen739522b2011-10-26 14:47:28 -0700112
113 return true;
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100114}
115
116/**
117 * mesh_peer_accepts_plinks - check if an mp is willing to establish peer links
118 *
119 * @ie: information elements of a management frame from the mesh peer
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100120 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200121bool mesh_peer_accepts_plinks(struct ieee802_11_elems *ie)
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100122{
Rui Paulo136cfa22009-11-18 18:40:00 +0000123 return (ie->mesh_config->meshconf_cap &
Johannes Bergbf7cd942013-02-15 14:40:31 +0100124 IEEE80211_MESHCONF_CAPAB_ACCEPT_PLINKS) != 0;
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100125}
126
127/**
Ben Hutchings2c530402012-07-10 10:55:09 +0000128 * mesh_accept_plinks_update - update accepting_plink in local mesh beacons
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100129 *
Johannes Bergd0709a62008-02-25 16:27:46 +0100130 * @sdata: mesh interface in which mesh beacons are going to be updated
Marco Porschdf323812012-08-08 07:58:43 +0200131 *
132 * Returns: beacon changed flag if the beacon content changed.
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100133 */
Marco Porschdf323812012-08-08 07:58:43 +0200134u32 mesh_accept_plinks_update(struct ieee80211_sub_if_data *sdata)
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100135{
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100136 bool free_plinks;
Marco Porschdf323812012-08-08 07:58:43 +0200137 u32 changed = 0;
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100138
139 /* In case mesh_plink_free_count > 0 and mesh_plinktbl_capacity == 0,
140 * the mesh interface might be able to establish plinks with peers that
Luis Carlos Cobob4e08ea2008-02-29 15:46:08 -0800141 * are already on the table but are not on PLINK_ESTAB state. However,
142 * in general the mesh interface is not accepting peer link requests
143 * from new peers, and that must be reflected in the beacon
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100144 */
145 free_plinks = mesh_plink_availables(sdata);
146
Marco Porschdf323812012-08-08 07:58:43 +0200147 if (free_plinks != sdata->u.mesh.accepting_plinks) {
148 sdata->u.mesh.accepting_plinks = free_plinks;
149 changed = BSS_CHANGED_BEACON;
150 }
151
152 return changed;
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100153}
154
Thomas Pedersen45b50282013-02-06 10:17:21 -0800155/*
156 * mesh_sta_cleanup - clean up any mesh sta state
157 *
158 * @sta: mesh sta to clean up.
159 */
160void mesh_sta_cleanup(struct sta_info *sta)
161{
162 struct ieee80211_sub_if_data *sdata = sta->sdata;
Bob Copelandefc401f2016-06-25 19:14:16 -0400163 u32 changed = mesh_plink_deactivate(sta);
Bob Copelandfe7a7c52016-05-15 13:19:16 -0400164
Thomas Pedersenf81a9de2013-06-13 15:54:41 -0700165 if (changed)
Thomas Pedersen2b5e1962013-02-14 11:20:13 -0800166 ieee80211_mbss_info_change_notify(sdata, changed);
Thomas Pedersen45b50282013-02-06 10:17:21 -0800167}
168
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200169int mesh_rmc_init(struct ieee80211_sub_if_data *sdata)
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100170{
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100171 int i;
172
Johannes Berg472dbc42008-09-11 00:01:49 +0200173 sdata->u.mesh.rmc = kmalloc(sizeof(struct mesh_rmc), GFP_KERNEL);
174 if (!sdata->u.mesh.rmc)
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100175 return -ENOMEM;
Johannes Berg472dbc42008-09-11 00:01:49 +0200176 sdata->u.mesh.rmc->idx_mask = RMC_BUCKETS - 1;
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100177 for (i = 0; i < RMC_BUCKETS; i++)
Bob Copeland47a04892016-03-18 22:11:29 -0400178 INIT_HLIST_HEAD(&sdata->u.mesh.rmc->bucket[i]);
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100179 return 0;
180}
181
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200182void mesh_rmc_free(struct ieee80211_sub_if_data *sdata)
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100183{
Johannes Berg472dbc42008-09-11 00:01:49 +0200184 struct mesh_rmc *rmc = sdata->u.mesh.rmc;
Bob Copeland47a04892016-03-18 22:11:29 -0400185 struct rmc_entry *p;
186 struct hlist_node *n;
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100187 int i;
188
Johannes Berg472dbc42008-09-11 00:01:49 +0200189 if (!sdata->u.mesh.rmc)
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100190 return;
191
Johannes Bergbf7cd942013-02-15 14:40:31 +0100192 for (i = 0; i < RMC_BUCKETS; i++) {
Bob Copeland47a04892016-03-18 22:11:29 -0400193 hlist_for_each_entry_safe(p, n, &rmc->bucket[i], list) {
194 hlist_del(&p->list);
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100195 kmem_cache_free(rm_cache, p);
196 }
Johannes Bergbf7cd942013-02-15 14:40:31 +0100197 }
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100198
199 kfree(rmc);
Johannes Berg472dbc42008-09-11 00:01:49 +0200200 sdata->u.mesh.rmc = NULL;
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100201}
202
203/**
204 * mesh_rmc_check - Check frame in recent multicast cache and add if absent.
205 *
Johannes Bergbf7cd942013-02-15 14:40:31 +0100206 * @sdata: interface
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100207 * @sa: source address
208 * @mesh_hdr: mesh_header
209 *
210 * Returns: 0 if the frame is not in the cache, nonzero otherwise.
211 *
212 * Checks using the source address and the mesh sequence number if we have
213 * received this frame lately. If the frame is not in the cache, it is added to
214 * it.
215 */
Johannes Bergbf7cd942013-02-15 14:40:31 +0100216int mesh_rmc_check(struct ieee80211_sub_if_data *sdata,
217 const u8 *sa, struct ieee80211s_hdr *mesh_hdr)
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100218{
Johannes Berg472dbc42008-09-11 00:01:49 +0200219 struct mesh_rmc *rmc = sdata->u.mesh.rmc;
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100220 u32 seqnum = 0;
221 int entries = 0;
222 u8 idx;
Bob Copeland47a04892016-03-18 22:11:29 -0400223 struct rmc_entry *p;
224 struct hlist_node *n;
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100225
Bob Copeland0aa7fab2016-03-18 22:11:28 -0400226 if (!rmc)
227 return -1;
228
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100229 /* Don't care about endianness since only match matters */
Luis Carlos Cobo51cedda2008-04-23 12:15:29 -0700230 memcpy(&seqnum, &mesh_hdr->seqnum, sizeof(mesh_hdr->seqnum));
231 idx = le32_to_cpu(mesh_hdr->seqnum) & rmc->idx_mask;
Bob Copeland47a04892016-03-18 22:11:29 -0400232 hlist_for_each_entry_safe(p, n, &rmc->bucket[idx], list) {
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100233 ++entries;
234 if (time_after(jiffies, p->exp_time) ||
Johannes Bergbf7cd942013-02-15 14:40:31 +0100235 entries == RMC_QUEUE_MAX_LEN) {
Bob Copeland47a04892016-03-18 22:11:29 -0400236 hlist_del(&p->list);
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100237 kmem_cache_free(rm_cache, p);
238 --entries;
Johannes Bergbf7cd942013-02-15 14:40:31 +0100239 } else if ((seqnum == p->seqnum) && ether_addr_equal(sa, p->sa))
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100240 return -1;
241 }
242
243 p = kmem_cache_alloc(rm_cache, GFP_ATOMIC);
Joe Perchesd15b8452011-08-29 14:17:31 -0700244 if (!p)
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100245 return 0;
Joe Perchesd15b8452011-08-29 14:17:31 -0700246
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100247 p->seqnum = seqnum;
248 p->exp_time = jiffies + RMC_TIMEOUT;
249 memcpy(p->sa, sa, ETH_ALEN);
Bob Copeland47a04892016-03-18 22:11:29 -0400250 hlist_add_head(&p->list, &rmc->bucket[idx]);
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100251 return 0;
252}
253
Johannes Bergbf7cd942013-02-15 14:40:31 +0100254int mesh_add_meshconf_ie(struct ieee80211_sub_if_data *sdata,
255 struct sk_buff *skb)
Thomas Pedersen082ebb02011-08-11 19:35:10 -0700256{
257 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
258 u8 *pos, neighbors;
259 u8 meshconf_len = sizeof(struct ieee80211_meshconf_ie);
Bob Copeland4a6ecd32018-10-25 15:48:52 -0400260 bool is_connected_to_gate = ifmsh->num_gates > 0 ||
Bob Copeland01d66fb2018-10-25 17:36:34 -0400261 ifmsh->mshcfg.dot11MeshGateAnnouncementProtocol ||
262 ifmsh->mshcfg.dot11MeshConnectedToMeshGate;
Thomas Pedersen082ebb02011-08-11 19:35:10 -0700263
264 if (skb_tailroom(skb) < 2 + meshconf_len)
265 return -ENOMEM;
266
267 pos = skb_put(skb, 2 + meshconf_len);
268 *pos++ = WLAN_EID_MESH_CONFIG;
269 *pos++ = meshconf_len;
270
Thomas Pedersen43552be2013-12-15 13:14:16 -0800271 /* save a pointer for quick updates in pre-tbtt */
272 ifmsh->meshconf_offset = pos - skb->data;
273
Thomas Pedersen082ebb02011-08-11 19:35:10 -0700274 /* Active path selection protocol ID */
275 *pos++ = ifmsh->mesh_pp_id;
276 /* Active path selection metric ID */
277 *pos++ = ifmsh->mesh_pm_id;
278 /* Congestion control mode identifier */
279 *pos++ = ifmsh->mesh_cc_id;
280 /* Synchronization protocol identifier */
281 *pos++ = ifmsh->mesh_sp_id;
282 /* Authentication Protocol identifier */
283 *pos++ = ifmsh->mesh_auth_id;
284 /* Mesh Formation Info - number of neighbors */
Ashok Nagarajan1258d972012-10-09 13:27:47 -0700285 neighbors = atomic_read(&ifmsh->estab_plinks);
Jacob Minshalle05eccc2013-05-29 14:32:36 -0700286 neighbors = min_t(int, neighbors, IEEE80211_MAX_MESH_PEERINGS);
Bob Copeland4a6ecd32018-10-25 15:48:52 -0400287 *pos++ = (neighbors << 1) | is_connected_to_gate;
Thomas Pedersen082ebb02011-08-11 19:35:10 -0700288 /* Mesh capability */
Chun-Yeow Yeohb60e5272013-07-12 18:55:22 +0800289 *pos = 0x00;
290 *pos |= ifmsh->mshcfg.dot11MeshForwarding ?
291 IEEE80211_MESHCONF_CAPAB_FORWARDING : 0x00;
Javier Cardonadbf498f2012-03-31 11:31:32 -0700292 *pos |= ifmsh->accepting_plinks ?
Johannes Bergbf7cd942013-02-15 14:40:31 +0100293 IEEE80211_MESHCONF_CAPAB_ACCEPT_PLINKS : 0x00;
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100294 /* Mesh PS mode. See IEEE802.11-2012 8.4.2.100.8 */
295 *pos |= ifmsh->ps_peers_deep_sleep ?
Johannes Bergbf7cd942013-02-15 14:40:31 +0100296 IEEE80211_MESHCONF_CAPAB_POWER_SAVE_LEVEL : 0x00;
Thomas Pedersen082ebb02011-08-11 19:35:10 -0700297 return 0;
298}
299
Johannes Bergbf7cd942013-02-15 14:40:31 +0100300int mesh_add_meshid_ie(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
Thomas Pedersen082ebb02011-08-11 19:35:10 -0700301{
302 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
303 u8 *pos;
304
305 if (skb_tailroom(skb) < 2 + ifmsh->mesh_id_len)
306 return -ENOMEM;
307
308 pos = skb_put(skb, 2 + ifmsh->mesh_id_len);
309 *pos++ = WLAN_EID_MESH_ID;
310 *pos++ = ifmsh->mesh_id_len;
311 if (ifmsh->mesh_id_len)
312 memcpy(pos, ifmsh->mesh_id, ifmsh->mesh_id_len);
313
314 return 0;
315}
316
Johannes Bergbf7cd942013-02-15 14:40:31 +0100317static int mesh_add_awake_window_ie(struct ieee80211_sub_if_data *sdata,
318 struct sk_buff *skb)
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100319{
320 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
321 u8 *pos;
322
323 /* see IEEE802.11-2012 13.14.6 */
324 if (ifmsh->ps_peers_light_sleep == 0 &&
325 ifmsh->ps_peers_deep_sleep == 0 &&
326 ifmsh->nonpeer_pm == NL80211_MESH_POWER_ACTIVE)
327 return 0;
328
329 if (skb_tailroom(skb) < 4)
330 return -ENOMEM;
331
332 pos = skb_put(skb, 2 + 2);
333 *pos++ = WLAN_EID_MESH_AWAKE_WINDOW;
334 *pos++ = 2;
335 put_unaligned_le16(ifmsh->mshcfg.dot11MeshAwakeWindowDuration, pos);
336
337 return 0;
338}
339
Johannes Bergbf7cd942013-02-15 14:40:31 +0100340int mesh_add_vendor_ies(struct ieee80211_sub_if_data *sdata,
341 struct sk_buff *skb)
Thomas Pedersen082ebb02011-08-11 19:35:10 -0700342{
343 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
344 u8 offset, len;
345 const u8 *data;
346
347 if (!ifmsh->ie || !ifmsh->ie_len)
348 return 0;
349
350 /* fast-forward to vendor IEs */
351 offset = ieee80211_ie_split_vendor(ifmsh->ie, ifmsh->ie_len, 0);
352
Thorsten Horstmannda7061c2017-02-03 14:38:29 +0100353 if (offset < ifmsh->ie_len) {
Thomas Pedersen082ebb02011-08-11 19:35:10 -0700354 len = ifmsh->ie_len - offset;
355 data = ifmsh->ie + offset;
356 if (skb_tailroom(skb) < len)
357 return -ENOMEM;
Johannes Berg59ae1d12017-06-16 14:29:20 +0200358 skb_put_data(skb, data, len);
Thomas Pedersen082ebb02011-08-11 19:35:10 -0700359 }
360
361 return 0;
362}
363
Johannes Bergbf7cd942013-02-15 14:40:31 +0100364int mesh_add_rsn_ie(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
Thomas Pedersen082ebb02011-08-11 19:35:10 -0700365{
366 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
367 u8 len = 0;
368 const u8 *data;
369
370 if (!ifmsh->ie || !ifmsh->ie_len)
371 return 0;
372
373 /* find RSN IE */
Bob Copelanda40a8c12014-04-15 10:43:07 -0400374 data = cfg80211_find_ie(WLAN_EID_RSN, ifmsh->ie, ifmsh->ie_len);
375 if (!data)
376 return 0;
Thomas Pedersen082ebb02011-08-11 19:35:10 -0700377
Bob Copelanda40a8c12014-04-15 10:43:07 -0400378 len = data[1] + 2;
379
380 if (skb_tailroom(skb) < len)
381 return -ENOMEM;
Johannes Berg59ae1d12017-06-16 14:29:20 +0200382 skb_put_data(skb, data, len);
Thomas Pedersen082ebb02011-08-11 19:35:10 -0700383
384 return 0;
385}
386
Johannes Bergbf7cd942013-02-15 14:40:31 +0100387static int mesh_add_ds_params_ie(struct ieee80211_sub_if_data *sdata,
388 struct sk_buff *skb)
Thomas Pedersen082ebb02011-08-11 19:35:10 -0700389{
Johannes Berg55de9082012-07-26 17:24:39 +0200390 struct ieee80211_chanctx_conf *chanctx_conf;
391 struct ieee80211_channel *chan;
Thomas Pedersen082ebb02011-08-11 19:35:10 -0700392 u8 *pos;
393
394 if (skb_tailroom(skb) < 3)
395 return -ENOMEM;
396
Johannes Berg55de9082012-07-26 17:24:39 +0200397 rcu_read_lock();
398 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
399 if (WARN_ON(!chanctx_conf)) {
400 rcu_read_unlock();
401 return -EINVAL;
402 }
Johannes Berg4bf88532012-11-09 11:39:59 +0100403 chan = chanctx_conf->def.chan;
Johannes Berg55de9082012-07-26 17:24:39 +0200404 rcu_read_unlock();
405
Emanuel Taube601513a2013-02-06 14:17:17 +0100406 pos = skb_put(skb, 2 + 1);
407 *pos++ = WLAN_EID_DS_PARAMS;
408 *pos++ = 1;
409 *pos++ = ieee80211_frequency_to_channel(chan->center_freq);
Rui Paulobe125c62009-11-09 23:46:54 +0000410
Thomas Pedersen082ebb02011-08-11 19:35:10 -0700411 return 0;
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100412}
413
Johannes Bergbf7cd942013-02-15 14:40:31 +0100414int mesh_add_ht_cap_ie(struct ieee80211_sub_if_data *sdata,
415 struct sk_buff *skb)
Thomas Pedersen176f3602011-10-26 14:47:27 -0700416{
Thomas Pedersen176f3602011-10-26 14:47:27 -0700417 struct ieee80211_supported_band *sband;
418 u8 *pos;
419
Mohammed Shafi Shajakhan21a8e9d2017-04-27 12:45:38 +0530420 sband = ieee80211_get_sband(sdata);
421 if (!sband)
422 return -EINVAL;
423
Rajkumar Manoharan607ca9e2020-05-28 21:34:34 +0200424 /* HT not allowed in 6 GHz */
425 if (sband->band == NL80211_BAND_6GHZ)
426 return 0;
427
Thomas Pedersen176f3602011-10-26 14:47:27 -0700428 if (!sband->ht_cap.ht_supported ||
Simon Wunderlich0418a442013-05-16 13:00:31 +0200429 sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT ||
430 sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_5 ||
431 sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_10)
Thomas Pedersen176f3602011-10-26 14:47:27 -0700432 return 0;
433
434 if (skb_tailroom(skb) < 2 + sizeof(struct ieee80211_ht_cap))
435 return -ENOMEM;
436
437 pos = skb_put(skb, 2 + sizeof(struct ieee80211_ht_cap));
Ben Greearef96a8422011-11-18 11:32:00 -0800438 ieee80211_ie_build_ht_cap(pos, &sband->ht_cap, sband->ht_cap.cap);
Thomas Pedersen176f3602011-10-26 14:47:27 -0700439
440 return 0;
441}
442
Johannes Bergbf7cd942013-02-15 14:40:31 +0100443int mesh_add_ht_oper_ie(struct ieee80211_sub_if_data *sdata,
444 struct sk_buff *skb)
Thomas Pedersen176f3602011-10-26 14:47:27 -0700445{
446 struct ieee80211_local *local = sdata->local;
Johannes Berg55de9082012-07-26 17:24:39 +0200447 struct ieee80211_chanctx_conf *chanctx_conf;
448 struct ieee80211_channel *channel;
Johannes Berg55de9082012-07-26 17:24:39 +0200449 struct ieee80211_supported_band *sband;
450 struct ieee80211_sta_ht_cap *ht_cap;
Thomas Pedersen176f3602011-10-26 14:47:27 -0700451 u8 *pos;
452
Johannes Berg55de9082012-07-26 17:24:39 +0200453 rcu_read_lock();
454 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
455 if (WARN_ON(!chanctx_conf)) {
456 rcu_read_unlock();
457 return -EINVAL;
458 }
Johannes Berg4bf88532012-11-09 11:39:59 +0100459 channel = chanctx_conf->def.chan;
Johannes Berg55de9082012-07-26 17:24:39 +0200460 rcu_read_unlock();
461
462 sband = local->hw.wiphy->bands[channel->band];
463 ht_cap = &sband->ht_cap;
464
Rajkumar Manoharan607ca9e2020-05-28 21:34:34 +0200465 /* HT not allowed in 6 GHz */
466 if (sband->band == NL80211_BAND_6GHZ)
467 return 0;
468
Bob Copelandc85fb532015-08-27 09:00:18 -0400469 if (!ht_cap->ht_supported ||
470 sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT ||
471 sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_5 ||
472 sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_10)
Thomas Pedersen176f3602011-10-26 14:47:27 -0700473 return 0;
474
Johannes Berg074d46d2012-03-15 19:45:16 +0100475 if (skb_tailroom(skb) < 2 + sizeof(struct ieee80211_ht_operation))
Thomas Pedersen176f3602011-10-26 14:47:27 -0700476 return -ENOMEM;
477
Johannes Berg074d46d2012-03-15 19:45:16 +0100478 pos = skb_put(skb, 2 + sizeof(struct ieee80211_ht_operation));
Johannes Berg4bf88532012-11-09 11:39:59 +0100479 ieee80211_ie_build_ht_oper(pos, ht_cap, &sdata->vif.bss_conf.chandef,
Arik Nemtsov57f255f2015-10-25 10:59:34 +0200480 sdata->vif.bss_conf.ht_operation_mode,
481 false);
Thomas Pedersen176f3602011-10-26 14:47:27 -0700482
483 return 0;
484}
Johannes Bergbf7cd942013-02-15 14:40:31 +0100485
Bob Copelandc85fb532015-08-27 09:00:18 -0400486int mesh_add_vht_cap_ie(struct ieee80211_sub_if_data *sdata,
487 struct sk_buff *skb)
488{
Bob Copelandc85fb532015-08-27 09:00:18 -0400489 struct ieee80211_supported_band *sband;
490 u8 *pos;
491
Mohammed Shafi Shajakhan21a8e9d2017-04-27 12:45:38 +0530492 sband = ieee80211_get_sband(sdata);
493 if (!sband)
494 return -EINVAL;
495
Rajkumar Manoharan607ca9e2020-05-28 21:34:34 +0200496 /* VHT not allowed in 6 GHz */
497 if (sband->band == NL80211_BAND_6GHZ)
498 return 0;
499
Bob Copelandc85fb532015-08-27 09:00:18 -0400500 if (!sband->vht_cap.vht_supported ||
501 sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT ||
502 sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_5 ||
503 sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_10)
504 return 0;
505
506 if (skb_tailroom(skb) < 2 + sizeof(struct ieee80211_vht_cap))
507 return -ENOMEM;
508
509 pos = skb_put(skb, 2 + sizeof(struct ieee80211_vht_cap));
510 ieee80211_ie_build_vht_cap(pos, &sband->vht_cap, sband->vht_cap.cap);
511
512 return 0;
513}
514
515int mesh_add_vht_oper_ie(struct ieee80211_sub_if_data *sdata,
516 struct sk_buff *skb)
517{
518 struct ieee80211_local *local = sdata->local;
519 struct ieee80211_chanctx_conf *chanctx_conf;
520 struct ieee80211_channel *channel;
521 struct ieee80211_supported_band *sband;
522 struct ieee80211_sta_vht_cap *vht_cap;
523 u8 *pos;
524
525 rcu_read_lock();
526 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
527 if (WARN_ON(!chanctx_conf)) {
528 rcu_read_unlock();
529 return -EINVAL;
530 }
531 channel = chanctx_conf->def.chan;
532 rcu_read_unlock();
533
534 sband = local->hw.wiphy->bands[channel->band];
535 vht_cap = &sband->vht_cap;
536
Rajkumar Manoharan607ca9e2020-05-28 21:34:34 +0200537 /* VHT not allowed in 6 GHz */
538 if (sband->band == NL80211_BAND_6GHZ)
539 return 0;
540
Bob Copelandc85fb532015-08-27 09:00:18 -0400541 if (!vht_cap->vht_supported ||
542 sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT ||
543 sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_5 ||
544 sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_10)
545 return 0;
546
547 if (skb_tailroom(skb) < 2 + sizeof(struct ieee80211_vht_operation))
548 return -ENOMEM;
549
550 pos = skb_put(skb, 2 + sizeof(struct ieee80211_vht_operation));
551 ieee80211_ie_build_vht_oper(pos, vht_cap,
552 &sdata->vif.bss_conf.chandef);
553
554 return 0;
555}
556
Sven Eckelmann60ad72d2019-07-24 18:33:56 +0200557int mesh_add_he_cap_ie(struct ieee80211_sub_if_data *sdata,
558 struct sk_buff *skb, u8 ie_len)
559{
560 const struct ieee80211_sta_he_cap *he_cap;
561 struct ieee80211_supported_band *sband;
562 u8 *pos;
563
564 sband = ieee80211_get_sband(sdata);
565 if (!sband)
566 return -EINVAL;
567
568 he_cap = ieee80211_get_he_iftype_cap(sband, NL80211_IFTYPE_MESH_POINT);
569
570 if (!he_cap ||
571 sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT ||
572 sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_5 ||
573 sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_10)
574 return 0;
575
576 if (skb_tailroom(skb) < ie_len)
577 return -ENOMEM;
578
579 pos = skb_put(skb, ie_len);
580 ieee80211_ie_build_he_cap(pos, he_cap, pos + ie_len);
581
582 return 0;
583}
584
585int mesh_add_he_oper_ie(struct ieee80211_sub_if_data *sdata,
586 struct sk_buff *skb)
587{
588 const struct ieee80211_sta_he_cap *he_cap;
589 struct ieee80211_supported_band *sband;
Rajkumar Manoharand1b75242020-05-28 21:34:33 +0200590 u32 len;
Sven Eckelmann60ad72d2019-07-24 18:33:56 +0200591 u8 *pos;
592
593 sband = ieee80211_get_sband(sdata);
594 if (!sband)
595 return -EINVAL;
596
597 he_cap = ieee80211_get_he_iftype_cap(sband, NL80211_IFTYPE_MESH_POINT);
598 if (!he_cap ||
599 sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT ||
600 sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_5 ||
601 sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_10)
602 return 0;
603
Rajkumar Manoharand1b75242020-05-28 21:34:33 +0200604 len = 2 + 1 + sizeof(struct ieee80211_he_operation);
605 if (sdata->vif.bss_conf.chandef.chan->band == NL80211_BAND_6GHZ)
606 len += sizeof(struct ieee80211_he_6ghz_oper);
607
608 if (skb_tailroom(skb) < len)
Sven Eckelmann60ad72d2019-07-24 18:33:56 +0200609 return -ENOMEM;
610
Rajkumar Manoharand1b75242020-05-28 21:34:33 +0200611 pos = skb_put(skb, len);
612 ieee80211_ie_build_he_oper(pos, &sdata->vif.bss_conf.chandef);
Sven Eckelmann60ad72d2019-07-24 18:33:56 +0200613
614 return 0;
615}
616
Rajkumar Manoharan24a20422020-05-28 21:34:32 +0200617int mesh_add_he_6ghz_cap_ie(struct ieee80211_sub_if_data *sdata,
618 struct sk_buff *skb)
619{
620 ieee80211_ie_build_he_6ghz_cap(sdata, skb);
621 return 0;
622}
623
Kees Cook34f11cd2017-10-16 16:35:49 -0700624static void ieee80211_mesh_path_timer(struct timer_list *t)
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100625{
626 struct ieee80211_sub_if_data *sdata =
Kees Cook34f11cd2017-10-16 16:35:49 -0700627 from_timer(sdata, t, u.mesh.mesh_path_timer);
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100628
Stanislaw Gruszka690205f2013-02-28 10:55:29 +0100629 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Luis Carlos Cobo2e3c8732008-02-23 15:17:09 +0100630}
631
Kees Cook34f11cd2017-10-16 16:35:49 -0700632static void ieee80211_mesh_path_root_timer(struct timer_list *t)
Rui Pauloe304bfd2009-11-09 23:46:56 +0000633{
634 struct ieee80211_sub_if_data *sdata =
Kees Cook34f11cd2017-10-16 16:35:49 -0700635 from_timer(sdata, t, u.mesh.mesh_path_root_timer);
Rui Pauloe304bfd2009-11-09 23:46:56 +0000636 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Rui Pauloe304bfd2009-11-09 23:46:56 +0000637
638 set_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags);
639
Stanislaw Gruszka690205f2013-02-28 10:55:29 +0100640 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Rui Pauloe304bfd2009-11-09 23:46:56 +0000641}
642
Rui Paulo63c57232009-11-09 23:46:57 +0000643void ieee80211_mesh_root_setup(struct ieee80211_if_mesh *ifmsh)
644{
Chun-Yeow Yeohdbb912c2012-06-14 02:06:09 +0800645 if (ifmsh->mshcfg.dot11MeshHWMPRootMode > IEEE80211_ROOTMODE_ROOT)
Rui Paulo63c57232009-11-09 23:46:57 +0000646 set_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags);
647 else {
648 clear_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags);
649 /* stop running timer */
650 del_timer_sync(&ifmsh->mesh_path_root_timer);
651 }
652}
653
Johannes Berg902acc72008-02-23 15:17:19 +0100654/**
Javier Cardona3c5772a2009-08-10 12:15:48 -0700655 * ieee80211_fill_mesh_addresses - fill addresses of a locally originated mesh frame
Johannes Bergbf7cd942013-02-15 14:40:31 +0100656 * @hdr: 802.11 frame header
Javier Cardona3c5772a2009-08-10 12:15:48 -0700657 * @fc: frame control field
658 * @meshda: destination address in the mesh
659 * @meshsa: source address address in the mesh. Same as TA, as frame is
660 * locally originated.
661 *
662 * Return the length of the 802.11 (does not include a mesh control header)
663 */
Johannes Berg15ff6362009-11-17 13:34:04 +0100664int ieee80211_fill_mesh_addresses(struct ieee80211_hdr *hdr, __le16 *fc,
665 const u8 *meshda, const u8 *meshsa)
666{
Javier Cardona3c5772a2009-08-10 12:15:48 -0700667 if (is_multicast_ether_addr(meshda)) {
668 *fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
669 /* DA TA SA */
670 memcpy(hdr->addr1, meshda, ETH_ALEN);
671 memcpy(hdr->addr2, meshsa, ETH_ALEN);
672 memcpy(hdr->addr3, meshsa, ETH_ALEN);
673 return 24;
674 } else {
Javier Cardona2154c81c2011-09-07 17:49:53 -0700675 *fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
Javier Cardona3c5772a2009-08-10 12:15:48 -0700676 /* RA TA DA SA */
Joe Perchesc84a67a2015-03-02 19:54:57 -0800677 eth_zero_addr(hdr->addr1); /* RA is resolved later */
Javier Cardona3c5772a2009-08-10 12:15:48 -0700678 memcpy(hdr->addr2, meshsa, ETH_ALEN);
679 memcpy(hdr->addr3, meshda, ETH_ALEN);
680 memcpy(hdr->addr4, meshsa, ETH_ALEN);
681 return 30;
682 }
683}
684
685/**
Johannes Berg902acc72008-02-23 15:17:19 +0100686 * ieee80211_new_mesh_header - create a new mesh header
Johannes Berg902acc72008-02-23 15:17:19 +0100687 * @sdata: mesh interface to be used
Johannes Bergbf7cd942013-02-15 14:40:31 +0100688 * @meshhdr: uninitialized mesh header
Javier Cardona61ad5392010-12-16 17:23:34 -0800689 * @addr4or5: 1st address in the ae header, which may correspond to address 4
690 * (if addr6 is NULL) or address 5 (if addr6 is present). It may
691 * be NULL.
692 * @addr6: 2nd address in the ae header, which corresponds to addr6 of the
693 * mesh frame
Johannes Berg902acc72008-02-23 15:17:19 +0100694 *
695 * Return the header length.
696 */
Andrzej Hajda5edfcee2015-09-25 08:42:00 +0200697unsigned int ieee80211_new_mesh_header(struct ieee80211_sub_if_data *sdata,
698 struct ieee80211s_hdr *meshhdr,
699 const char *addr4or5, const char *addr6)
Johannes Berg902acc72008-02-23 15:17:19 +0100700{
Johannes Bergbf7cd942013-02-15 14:40:31 +0100701 if (WARN_ON(!addr4or5 && addr6))
702 return 0;
703
Julia Lawall0c3cee72009-12-09 20:25:59 +0100704 memset(meshhdr, 0, sizeof(*meshhdr));
Johannes Bergbf7cd942013-02-15 14:40:31 +0100705
Johannes Berg472dbc42008-09-11 00:01:49 +0200706 meshhdr->ttl = sdata->u.mesh.mshcfg.dot11MeshTTL;
Johannes Bergbf7cd942013-02-15 14:40:31 +0100707
708 /* FIXME: racy -- TX on multiple queues can be concurrent */
Johannes Berg472dbc42008-09-11 00:01:49 +0200709 put_unaligned(cpu_to_le32(sdata->u.mesh.mesh_seqnum), &meshhdr->seqnum);
710 sdata->u.mesh.mesh_seqnum++;
Johannes Bergbf7cd942013-02-15 14:40:31 +0100711
Javier Cardona61ad5392010-12-16 17:23:34 -0800712 if (addr4or5 && !addr6) {
Javier Cardona3c5772a2009-08-10 12:15:48 -0700713 meshhdr->flags |= MESH_FLAGS_AE_A4;
Javier Cardona61ad5392010-12-16 17:23:34 -0800714 memcpy(meshhdr->eaddr1, addr4or5, ETH_ALEN);
Johannes Bergbf7cd942013-02-15 14:40:31 +0100715 return 2 * ETH_ALEN;
Javier Cardona61ad5392010-12-16 17:23:34 -0800716 } else if (addr4or5 && addr6) {
Javier Cardona3c5772a2009-08-10 12:15:48 -0700717 meshhdr->flags |= MESH_FLAGS_AE_A5_A6;
Javier Cardona61ad5392010-12-16 17:23:34 -0800718 memcpy(meshhdr->eaddr1, addr4or5, ETH_ALEN);
719 memcpy(meshhdr->eaddr2, addr6, ETH_ALEN);
Johannes Bergbf7cd942013-02-15 14:40:31 +0100720 return 3 * ETH_ALEN;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700721 }
Johannes Bergbf7cd942013-02-15 14:40:31 +0100722
723 return ETH_ALEN;
Johannes Berg902acc72008-02-23 15:17:19 +0100724}
725
Johannes Bergbf7cd942013-02-15 14:40:31 +0100726static void ieee80211_mesh_housekeeping(struct ieee80211_sub_if_data *sdata)
Johannes Berg472dbc42008-09-11 00:01:49 +0200727{
Johannes Bergbf7cd942013-02-15 14:40:31 +0100728 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Marco Porschdf323812012-08-08 07:58:43 +0200729 u32 changed;
Johannes Berg472dbc42008-09-11 00:01:49 +0200730
Masashi Honma31f909a2015-02-24 22:42:16 +0900731 if (ifmsh->mshcfg.plink_timeout > 0)
732 ieee80211_sta_expire(sdata, ifmsh->mshcfg.plink_timeout * HZ);
Johannes Berg472dbc42008-09-11 00:01:49 +0200733 mesh_path_expire(sdata);
734
Marco Porschdf323812012-08-08 07:58:43 +0200735 changed = mesh_accept_plinks_update(sdata);
Thomas Pedersen2b5e1962013-02-14 11:20:13 -0800736 ieee80211_mbss_info_change_notify(sdata, changed);
Johannes Berg472dbc42008-09-11 00:01:49 +0200737
Johannes Berg472dbc42008-09-11 00:01:49 +0200738 mod_timer(&ifmsh->housekeeping_timer,
Johannes Bergbf7cd942013-02-15 14:40:31 +0100739 round_jiffies(jiffies +
740 IEEE80211_MESH_HOUSEKEEPING_INTERVAL));
Johannes Berg472dbc42008-09-11 00:01:49 +0200741}
742
Rui Pauloe304bfd2009-11-09 23:46:56 +0000743static void ieee80211_mesh_rootpath(struct ieee80211_sub_if_data *sdata)
744{
745 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Chun-Yeow Yeoha69cc442012-06-14 02:06:07 +0800746 u32 interval;
Rui Pauloe304bfd2009-11-09 23:46:56 +0000747
748 mesh_path_tx_root_frame(sdata);
Chun-Yeow Yeoha69cc442012-06-14 02:06:07 +0800749
750 if (ifmsh->mshcfg.dot11MeshHWMPRootMode == IEEE80211_PROACTIVE_RANN)
751 interval = ifmsh->mshcfg.dot11MeshHWMPRannInterval;
752 else
753 interval = ifmsh->mshcfg.dot11MeshHWMProotInterval;
754
Rui Pauloe304bfd2009-11-09 23:46:56 +0000755 mod_timer(&ifmsh->mesh_path_root_timer,
Chun-Yeow Yeoha69cc442012-06-14 02:06:07 +0800756 round_jiffies(TU_TO_EXP_TIME(interval)));
Rui Pauloe304bfd2009-11-09 23:46:56 +0000757}
758
Thomas Pedersen2b5e1962013-02-14 11:20:13 -0800759static int
760ieee80211_mesh_build_beacon(struct ieee80211_if_mesh *ifmsh)
761{
762 struct beacon_data *bcn;
763 int head_len, tail_len;
764 struct sk_buff *skb;
765 struct ieee80211_mgmt *mgmt;
766 struct ieee80211_chanctx_conf *chanctx_conf;
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -0700767 struct mesh_csa_settings *csa;
Johannes Berg57fbcce2016-04-12 15:56:15 +0200768 enum nl80211_band band;
Sven Eckelmann60ad72d2019-07-24 18:33:56 +0200769 u8 ie_len_he_cap;
Thomas Pedersen2b5e1962013-02-14 11:20:13 -0800770 u8 *pos;
771 struct ieee80211_sub_if_data *sdata;
Johannes Berg4c121fd62017-09-08 11:54:46 +0200772 int hdr_len = offsetofend(struct ieee80211_mgmt, u.beacon);
Thomas Pedersen2b5e1962013-02-14 11:20:13 -0800773
774 sdata = container_of(ifmsh, struct ieee80211_sub_if_data, u.mesh);
775 rcu_read_lock();
776 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
777 band = chanctx_conf->def.chan->band;
778 rcu_read_unlock();
779
Sven Eckelmann60ad72d2019-07-24 18:33:56 +0200780 ie_len_he_cap = ieee80211_ie_len_he_cap(sdata,
781 NL80211_IFTYPE_MESH_POINT);
Thomas Pedersen2b5e1962013-02-14 11:20:13 -0800782 head_len = hdr_len +
783 2 + /* NULL SSID */
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -0700784 /* Channel Switch Announcement */
785 2 + sizeof(struct ieee80211_channel_sw_ie) +
Masahiro Yamada08a7e622017-02-27 14:28:41 -0800786 /* Mesh Channel Switch Parameters */
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -0700787 2 + sizeof(struct ieee80211_mesh_chansw_params_ie) +
Simon Wunderlich75d627d2017-05-23 17:00:42 +0200788 /* Channel Switch Wrapper + Wide Bandwidth CSA IE */
789 2 + 2 + sizeof(struct ieee80211_wide_bw_chansw_ie) +
790 2 + sizeof(struct ieee80211_sec_chan_offs_ie) +
Thomas Pedersen2b5e1962013-02-14 11:20:13 -0800791 2 + 8 + /* supported rates */
792 2 + 3; /* DS params */
793 tail_len = 2 + (IEEE80211_MAX_SUPP_RATES - 8) +
794 2 + sizeof(struct ieee80211_ht_cap) +
795 2 + sizeof(struct ieee80211_ht_operation) +
796 2 + ifmsh->mesh_id_len +
797 2 + sizeof(struct ieee80211_meshconf_ie) +
798 2 + sizeof(__le16) + /* awake window */
Bob Copelandc85fb532015-08-27 09:00:18 -0400799 2 + sizeof(struct ieee80211_vht_cap) +
800 2 + sizeof(struct ieee80211_vht_operation) +
Sven Eckelmann60ad72d2019-07-24 18:33:56 +0200801 ie_len_he_cap +
802 2 + 1 + sizeof(struct ieee80211_he_operation) +
Rajkumar Manoharand1b75242020-05-28 21:34:33 +0200803 sizeof(struct ieee80211_he_6ghz_oper) +
Rajkumar Manoharan24a20422020-05-28 21:34:32 +0200804 2 + 1 + sizeof(struct ieee80211_he_6ghz_capa) +
Thomas Pedersen2b5e1962013-02-14 11:20:13 -0800805 ifmsh->ie_len;
806
807 bcn = kzalloc(sizeof(*bcn) + head_len + tail_len, GFP_KERNEL);
808 /* need an skb for IE builders to operate on */
809 skb = dev_alloc_skb(max(head_len, tail_len));
810
811 if (!bcn || !skb)
812 goto out_free;
813
814 /*
815 * pointers go into the block we allocated,
816 * memory is | beacon_data | head | tail |
817 */
818 bcn->head = ((u8 *) bcn) + sizeof(*bcn);
819
820 /* fill in the head */
Johannes Bergb080db52017-06-16 14:29:19 +0200821 mgmt = skb_put_zero(skb, hdr_len);
Thomas Pedersen2b5e1962013-02-14 11:20:13 -0800822 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
823 IEEE80211_STYPE_BEACON);
824 eth_broadcast_addr(mgmt->da);
825 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
826 memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
827 ieee80211_mps_set_frame_flags(sdata, NULL, (void *) mgmt);
828 mgmt->u.beacon.beacon_int =
829 cpu_to_le16(sdata->vif.bss_conf.beacon_int);
830 mgmt->u.beacon.capab_info |= cpu_to_le16(
831 sdata->u.mesh.security ? WLAN_CAPABILITY_PRIVACY : 0);
832
833 pos = skb_put(skb, 2);
834 *pos++ = WLAN_EID_SSID;
835 *pos++ = 0x0;
836
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -0700837 rcu_read_lock();
838 csa = rcu_dereference(ifmsh->csa);
839 if (csa) {
Simon Wunderlich75d627d2017-05-23 17:00:42 +0200840 enum nl80211_channel_type ct;
841 struct cfg80211_chan_def *chandef;
842 int ie_len = 2 + sizeof(struct ieee80211_channel_sw_ie) +
843 2 + sizeof(struct ieee80211_mesh_chansw_params_ie);
844
Johannes Berge45a79d2017-05-24 09:07:47 +0200845 pos = skb_put_zero(skb, ie_len);
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -0700846 *pos++ = WLAN_EID_CHANNEL_SWITCH;
847 *pos++ = 3;
848 *pos++ = 0x0;
849 *pos++ = ieee80211_frequency_to_channel(
850 csa->settings.chandef.chan->center_freq);
Chun-Yeow Yeoh8df734e2015-06-09 13:35:33 +0800851 bcn->csa_current_counter = csa->settings.count;
Michal Kazioraf296bd2014-06-05 14:21:36 +0200852 bcn->csa_counter_offsets[0] = hdr_len + 6;
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -0700853 *pos++ = csa->settings.count;
854 *pos++ = WLAN_EID_CHAN_SWITCH_PARAM;
855 *pos++ = 6;
Luciano Coelho0cb4d4d2014-01-13 19:42:58 +0200856 if (ifmsh->csa_role == IEEE80211_MESH_CSA_ROLE_INIT) {
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -0700857 *pos++ = ifmsh->mshcfg.dot11MeshTTL;
858 *pos |= WLAN_EID_CHAN_SWITCH_PARAM_INITIATOR;
859 } else {
860 *pos++ = ifmsh->chsw_ttl;
861 }
862 *pos++ |= csa->settings.block_tx ?
863 WLAN_EID_CHAN_SWITCH_PARAM_TX_RESTRICT : 0x00;
864 put_unaligned_le16(WLAN_REASON_MESH_CHAN, pos);
865 pos += 2;
Chun-Yeow Yeohca91dc92013-11-12 10:31:48 +0800866 put_unaligned_le16(ifmsh->pre_value, pos);
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -0700867 pos += 2;
Simon Wunderlich75d627d2017-05-23 17:00:42 +0200868
869 switch (csa->settings.chandef.width) {
870 case NL80211_CHAN_WIDTH_40:
871 ie_len = 2 + sizeof(struct ieee80211_sec_chan_offs_ie);
Johannes Berge45a79d2017-05-24 09:07:47 +0200872 pos = skb_put_zero(skb, ie_len);
Simon Wunderlich75d627d2017-05-23 17:00:42 +0200873
874 *pos++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET; /* EID */
875 *pos++ = 1; /* len */
876 ct = cfg80211_get_chandef_type(&csa->settings.chandef);
877 if (ct == NL80211_CHAN_HT40PLUS)
878 *pos++ = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
879 else
880 *pos++ = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
881 break;
882 case NL80211_CHAN_WIDTH_80:
883 case NL80211_CHAN_WIDTH_80P80:
884 case NL80211_CHAN_WIDTH_160:
885 /* Channel Switch Wrapper + Wide Bandwidth CSA IE */
886 ie_len = 2 + 2 +
887 sizeof(struct ieee80211_wide_bw_chansw_ie);
Johannes Berge45a79d2017-05-24 09:07:47 +0200888 pos = skb_put_zero(skb, ie_len);
Simon Wunderlich75d627d2017-05-23 17:00:42 +0200889
890 *pos++ = WLAN_EID_CHANNEL_SWITCH_WRAPPER; /* EID */
891 *pos++ = 5; /* len */
892 /* put sub IE */
893 chandef = &csa->settings.chandef;
894 ieee80211_ie_build_wide_bw_cs(pos, chandef);
895 break;
896 default:
897 break;
898 }
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -0700899 }
900 rcu_read_unlock();
901
Thomas Pedersen2b5e1962013-02-14 11:20:13 -0800902 if (ieee80211_add_srates_ie(sdata, skb, true, band) ||
Johannes Bergbf7cd942013-02-15 14:40:31 +0100903 mesh_add_ds_params_ie(sdata, skb))
Thomas Pedersen2b5e1962013-02-14 11:20:13 -0800904 goto out_free;
905
906 bcn->head_len = skb->len;
907 memcpy(bcn->head, skb->data, bcn->head_len);
908
909 /* now the tail */
910 skb_trim(skb, 0);
911 bcn->tail = bcn->head + bcn->head_len;
912
913 if (ieee80211_add_ext_srates_ie(sdata, skb, true, band) ||
Johannes Bergbf7cd942013-02-15 14:40:31 +0100914 mesh_add_rsn_ie(sdata, skb) ||
915 mesh_add_ht_cap_ie(sdata, skb) ||
916 mesh_add_ht_oper_ie(sdata, skb) ||
917 mesh_add_meshid_ie(sdata, skb) ||
918 mesh_add_meshconf_ie(sdata, skb) ||
919 mesh_add_awake_window_ie(sdata, skb) ||
Bob Copelandc85fb532015-08-27 09:00:18 -0400920 mesh_add_vht_cap_ie(sdata, skb) ||
921 mesh_add_vht_oper_ie(sdata, skb) ||
Sven Eckelmann60ad72d2019-07-24 18:33:56 +0200922 mesh_add_he_cap_ie(sdata, skb, ie_len_he_cap) ||
923 mesh_add_he_oper_ie(sdata, skb) ||
Rajkumar Manoharan24a20422020-05-28 21:34:32 +0200924 mesh_add_he_6ghz_cap_ie(sdata, skb) ||
Johannes Bergbf7cd942013-02-15 14:40:31 +0100925 mesh_add_vendor_ies(sdata, skb))
Thomas Pedersen2b5e1962013-02-14 11:20:13 -0800926 goto out_free;
927
928 bcn->tail_len = skb->len;
929 memcpy(bcn->tail, skb->data, bcn->tail_len);
Thomas Pedersen43552be2013-12-15 13:14:16 -0800930 bcn->meshconf = (struct ieee80211_meshconf_ie *)
931 (bcn->tail + ifmsh->meshconf_offset);
Thomas Pedersen2b5e1962013-02-14 11:20:13 -0800932
933 dev_kfree_skb(skb);
934 rcu_assign_pointer(ifmsh->beacon, bcn);
935 return 0;
936out_free:
937 kfree(bcn);
938 dev_kfree_skb(skb);
939 return -ENOMEM;
940}
941
942static int
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200943ieee80211_mesh_rebuild_beacon(struct ieee80211_sub_if_data *sdata)
Thomas Pedersen2b5e1962013-02-14 11:20:13 -0800944{
Thomas Pedersen2b5e1962013-02-14 11:20:13 -0800945 struct beacon_data *old_bcn;
946 int ret;
Thomas Pedersen2b5e1962013-02-14 11:20:13 -0800947
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200948 old_bcn = rcu_dereference_protected(sdata->u.mesh.beacon,
949 lockdep_is_held(&sdata->wdev.mtx));
950 ret = ieee80211_mesh_build_beacon(&sdata->u.mesh);
Thomas Pedersen2b5e1962013-02-14 11:20:13 -0800951 if (ret)
952 /* just reuse old beacon */
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200953 return ret;
Thomas Pedersen2b5e1962013-02-14 11:20:13 -0800954
955 if (old_bcn)
956 kfree_rcu(old_bcn, rcu_head);
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200957 return 0;
Thomas Pedersen2b5e1962013-02-14 11:20:13 -0800958}
959
960void ieee80211_mbss_info_change_notify(struct ieee80211_sub_if_data *sdata,
961 u32 changed)
962{
Thomas Pedersenf81a9de2013-06-13 15:54:41 -0700963 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
964 unsigned long bits = changed;
965 u32 bit;
966
967 if (!bits)
968 return;
969
970 /* if we race with running work, worst case this work becomes a noop */
971 for_each_set_bit(bit, &bits, sizeof(changed) * BITS_PER_BYTE)
972 set_bit(bit, &ifmsh->mbss_changed);
973 set_bit(MESH_WORK_MBSS_CHANGED, &ifmsh->wrkq_flags);
974 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Thomas Pedersen2b5e1962013-02-14 11:20:13 -0800975}
976
977int ieee80211_start_mesh(struct ieee80211_sub_if_data *sdata)
Johannes Berg472dbc42008-09-11 00:01:49 +0200978{
979 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
980 struct ieee80211_local *local = sdata->local;
Chun-Yeow Yeohf4eabc92012-12-13 18:59:57 +0800981 u32 changed = BSS_CHANGED_BEACON |
982 BSS_CHANGED_BEACON_ENABLED |
983 BSS_CHANGED_HT |
984 BSS_CHANGED_BASIC_RATES |
Pradeep Kumar Chitrapudcbe73c2018-03-22 12:18:03 -0700985 BSS_CHANGED_BEACON_INT |
986 BSS_CHANGED_MCAST_RATE;
Johannes Berg472dbc42008-09-11 00:01:49 +0200987
Johannes Berg09b17472010-12-03 09:20:41 +0100988 local->fif_other_bss++;
989 /* mesh ifaces must set allmulti to forward mcast traffic */
990 atomic_inc(&local->iff_allmultis);
991 ieee80211_configure_filter(local);
992
Javier Cardonac7108a72010-12-16 17:37:50 -0800993 ifmsh->mesh_cc_id = 0; /* Disabled */
Javier Cardonadbf498f2012-03-31 11:31:32 -0700994 /* register sync ops from extensible synchronization framework */
995 ifmsh->sync_ops = ieee80211_mesh_sync_ops_get(ifmsh->mesh_sp_id);
Javier Cardonadbf498f2012-03-31 11:31:32 -0700996 ifmsh->sync_offset_clockdrift_max = 0;
Rui Paulo6b9ac442009-10-20 21:21:48 +0100997 set_bit(MESH_WORK_HOUSEKEEPING, &ifmsh->wrkq_flags);
Rui Paulo63c57232009-11-09 23:46:57 +0000998 ieee80211_mesh_root_setup(ifmsh);
Johannes Berg64592c82010-06-10 10:21:31 +0200999 ieee80211_queue_work(&local->hw, &sdata->work);
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07001000 sdata->vif.bss_conf.ht_operation_mode =
1001 ifmsh->mshcfg.ht_opmode;
Johannes Bergd6a83222012-12-14 14:06:28 +01001002 sdata->vif.bss_conf.enable_beacon = true;
Chun-Yeow Yeohf4eabc92012-12-13 18:59:57 +08001003
Thomas Pedersen39886b62013-02-13 12:14:19 -08001004 changed |= ieee80211_mps_local_status_update(sdata);
Marco Porsch3f52b7e2013-01-30 18:14:08 +01001005
Thomas Pedersen2b5e1962013-02-14 11:20:13 -08001006 if (ieee80211_mesh_build_beacon(ifmsh)) {
1007 ieee80211_stop_mesh(sdata);
1008 return -ENOMEM;
1009 }
1010
Thomas Pedersen057d5f42013-12-19 10:25:15 -08001011 ieee80211_recalc_dtim(local, sdata);
Chun-Yeow Yeohf4eabc92012-12-13 18:59:57 +08001012 ieee80211_bss_info_change_notify(sdata, changed);
Johannes Bergc405c622012-07-30 19:44:12 +02001013
1014 netif_carrier_on(sdata->dev);
Thomas Pedersen2b5e1962013-02-14 11:20:13 -08001015 return 0;
Johannes Berg472dbc42008-09-11 00:01:49 +02001016}
1017
1018void ieee80211_stop_mesh(struct ieee80211_sub_if_data *sdata)
1019{
Johannes Berg09b17472010-12-03 09:20:41 +01001020 struct ieee80211_local *local = sdata->local;
Johannes Berg29cbe682010-12-03 09:20:44 +01001021 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Thomas Pedersen2b5e1962013-02-14 11:20:13 -08001022 struct beacon_data *bcn;
Johannes Berg29cbe682010-12-03 09:20:44 +01001023
Johannes Bergc405c622012-07-30 19:44:12 +02001024 netif_carrier_off(sdata->dev);
1025
Maital Hahnc37a54a2016-07-13 14:44:41 +03001026 /* flush STAs and mpaths on this iface */
1027 sta_info_flush(sdata);
Pradeep Kumar Chitrapu0112fa52019-05-28 16:36:16 -07001028 ieee80211_free_keys(sdata, true);
Maital Hahnc37a54a2016-07-13 14:44:41 +03001029 mesh_path_flush_by_iface(sdata);
1030
Thomas Pedersen0d466b9c2012-08-03 12:21:32 -07001031 /* stop the beacon */
Johannes Berg29cbe682010-12-03 09:20:44 +01001032 ifmsh->mesh_id_len = 0;
Johannes Bergd6a83222012-12-14 14:06:28 +01001033 sdata->vif.bss_conf.enable_beacon = false;
Jouni Malinen08fad432020-04-25 18:57:12 +03001034 sdata->beacon_rate_set = false;
Johannes Bergd6a83222012-12-14 14:06:28 +01001035 clear_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED, &sdata->state);
Johannes Berg29cbe682010-12-03 09:20:44 +01001036 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED);
Maital Hahnc37a54a2016-07-13 14:44:41 +03001037
1038 /* remove beacon */
Thomas Pedersen2b5e1962013-02-14 11:20:13 -08001039 bcn = rcu_dereference_protected(ifmsh->beacon,
Johannes Berg8d61ffa2013-05-10 12:32:47 +02001040 lockdep_is_held(&sdata->wdev.mtx));
Monam Agarwal0c2bef462014-03-24 00:51:43 +05301041 RCU_INIT_POINTER(ifmsh->beacon, NULL);
Thomas Pedersen2b5e1962013-02-14 11:20:13 -08001042 kfree_rcu(bcn, rcu_head);
Thomas Pedersen0d466b9c2012-08-03 12:21:32 -07001043
Marco Porsch3f52b7e2013-01-30 18:14:08 +01001044 /* free all potentially still buffered group-addressed frames */
1045 local->total_ps_buffered -= skb_queue_len(&ifmsh->ps.bc_buf);
1046 skb_queue_purge(&ifmsh->ps.bc_buf);
1047
Johannes Berg472dbc42008-09-11 00:01:49 +02001048 del_timer_sync(&sdata->u.mesh.housekeeping_timer);
Rui Pauloe304bfd2009-11-09 23:46:56 +00001049 del_timer_sync(&sdata->u.mesh.mesh_path_root_timer);
Johannes Bergdd4c9262012-08-01 21:03:21 +02001050 del_timer_sync(&sdata->u.mesh.mesh_path_timer);
Johannes Berg09b17472010-12-03 09:20:41 +01001051
Thomas Pedersenf81a9de2013-06-13 15:54:41 -07001052 /* clear any mesh work (for next join) we may have accrued */
1053 ifmsh->wrkq_flags = 0;
1054 ifmsh->mbss_changed = 0;
1055
Johannes Berg09b17472010-12-03 09:20:41 +01001056 local->fif_other_bss--;
1057 atomic_dec(&local->iff_allmultis);
1058 ieee80211_configure_filter(local);
Johannes Berg472dbc42008-09-11 00:01:49 +02001059}
1060
Benjamin Berg5d553712017-05-16 11:23:10 +02001061static void ieee80211_mesh_csa_mark_radar(struct ieee80211_sub_if_data *sdata)
1062{
1063 int err;
1064
1065 /* if the current channel is a DFS channel, mark the channel as
1066 * unavailable.
1067 */
1068 err = cfg80211_chandef_dfs_required(sdata->local->hw.wiphy,
1069 &sdata->vif.bss_conf.chandef,
1070 NL80211_IFTYPE_MESH_POINT);
1071 if (err > 0)
1072 cfg80211_radar_event(sdata->local->hw.wiphy,
1073 &sdata->vif.bss_conf.chandef, GFP_ATOMIC);
1074}
1075
Chun-Yeow Yeoh33a45862013-10-17 15:55:18 -07001076static bool
1077ieee80211_mesh_process_chnswitch(struct ieee80211_sub_if_data *sdata,
1078 struct ieee802_11_elems *elems, bool beacon)
1079{
1080 struct cfg80211_csa_settings params;
1081 struct ieee80211_csa_ie csa_ie;
Chun-Yeow Yeoh33a45862013-10-17 15:55:18 -07001082 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Mohammed Shafi Shajakhan21a8e9d2017-04-27 12:45:38 +05301083 struct ieee80211_supported_band *sband;
Luciano Coelho0cb4d4d2014-01-13 19:42:58 +02001084 int err;
Johannes Berg2a333a02020-05-28 21:34:35 +02001085 u32 sta_flags, vht_cap_info = 0;
Chun-Yeow Yeoh33a45862013-10-17 15:55:18 -07001086
Michal Kaziordbd72852014-01-29 07:56:21 +01001087 sdata_assert_lock(sdata);
1088
Mohammed Shafi Shajakhan21a8e9d2017-04-27 12:45:38 +05301089 sband = ieee80211_get_sband(sdata);
1090 if (!sband)
1091 return false;
1092
Simon Wunderlich71ec2892017-05-23 17:00:43 +02001093 sta_flags = 0;
Chun-Yeow Yeoh33a45862013-10-17 15:55:18 -07001094 switch (sdata->vif.bss_conf.chandef.width) {
1095 case NL80211_CHAN_WIDTH_20_NOHT:
1096 sta_flags |= IEEE80211_STA_DISABLE_HT;
Gustavo A. R. Silva02049ce2017-10-17 18:14:50 -05001097 /* fall through */
Chun-Yeow Yeoh33a45862013-10-17 15:55:18 -07001098 case NL80211_CHAN_WIDTH_20:
1099 sta_flags |= IEEE80211_STA_DISABLE_40MHZ;
Gustavo A. R. Silva02049ce2017-10-17 18:14:50 -05001100 /* fall through */
Simon Wunderlich71ec2892017-05-23 17:00:43 +02001101 case NL80211_CHAN_WIDTH_40:
1102 sta_flags |= IEEE80211_STA_DISABLE_VHT;
Chun-Yeow Yeoh33a45862013-10-17 15:55:18 -07001103 break;
1104 default:
1105 break;
1106 }
1107
Johannes Berg2a333a02020-05-28 21:34:35 +02001108 if (elems->vht_cap_elem)
1109 vht_cap_info =
1110 le32_to_cpu(elems->vht_cap_elem->vht_cap_info);
1111
Chun-Yeow Yeoh33a45862013-10-17 15:55:18 -07001112 memset(&params, 0, sizeof(params));
Mohammed Shafi Shajakhan21a8e9d2017-04-27 12:45:38 +05301113 err = ieee80211_parse_ch_switch_ie(sdata, elems, sband->band,
Johannes Berg2a333a02020-05-28 21:34:35 +02001114 vht_cap_info,
Chun-Yeow Yeoh33a45862013-10-17 15:55:18 -07001115 sta_flags, sdata->vif.addr,
1116 &csa_ie);
1117 if (err < 0)
1118 return false;
1119 if (err)
1120 return false;
1121
Benjamin Berg5d553712017-05-16 11:23:10 +02001122 /* Mark the channel unavailable if the reason for the switch is
1123 * regulatory.
1124 */
1125 if (csa_ie.reason_code == WLAN_REASON_MESH_CHAN_REGULATORY)
1126 ieee80211_mesh_csa_mark_radar(sdata);
1127
Chun-Yeow Yeoh33a45862013-10-17 15:55:18 -07001128 params.chandef = csa_ie.chandef;
1129 params.count = csa_ie.count;
1130
Chun-Yeow Yeoh33a45862013-10-17 15:55:18 -07001131 if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, &params.chandef,
Benjamin Berg0ab2e552017-05-16 11:23:13 +02001132 IEEE80211_CHAN_DISABLED) ||
1133 !cfg80211_reg_can_beacon(sdata->local->hw.wiphy, &params.chandef,
1134 NL80211_IFTYPE_MESH_POINT)) {
Chun-Yeow Yeoh33a45862013-10-17 15:55:18 -07001135 sdata_info(sdata,
1136 "mesh STA %pM switches to unsupported channel (%d MHz, width:%d, CF1/2: %d/%d MHz), aborting\n",
1137 sdata->vif.addr,
1138 params.chandef.chan->center_freq,
1139 params.chandef.width,
1140 params.chandef.center_freq1,
1141 params.chandef.center_freq2);
1142 return false;
1143 }
1144
1145 err = cfg80211_chandef_dfs_required(sdata->local->hw.wiphy,
Luciano Coelho2beb6dab2014-02-18 11:40:36 +02001146 &params.chandef,
1147 NL80211_IFTYPE_MESH_POINT);
Chun-Yeow Yeoh33a45862013-10-17 15:55:18 -07001148 if (err < 0)
1149 return false;
Benjamin Berg0ab2e552017-05-16 11:23:13 +02001150 if (err > 0 && !ifmsh->userspace_handles_dfs) {
1151 sdata_info(sdata,
1152 "mesh STA %pM switches to channel requiring DFS (%d MHz, width:%d, CF1/2: %d/%d MHz), aborting\n",
1153 sdata->vif.addr,
1154 params.chandef.chan->center_freq,
1155 params.chandef.width,
1156 params.chandef.center_freq1,
1157 params.chandef.center_freq2);
Chun-Yeow Yeoh33a45862013-10-17 15:55:18 -07001158 return false;
Benjamin Berg0ab2e552017-05-16 11:23:13 +02001159 }
Luciano Coelho2beb6dab2014-02-18 11:40:36 +02001160
1161 params.radar_required = err;
Chun-Yeow Yeoh33a45862013-10-17 15:55:18 -07001162
Luciano Coelho0cb4d4d2014-01-13 19:42:58 +02001163 if (cfg80211_chandef_identical(&params.chandef,
1164 &sdata->vif.bss_conf.chandef)) {
1165 mcsa_dbg(sdata,
1166 "received csa with an identical chandef, ignoring\n");
1167 return true;
1168 }
Chun-Yeow Yeoh33a45862013-10-17 15:55:18 -07001169
1170 mcsa_dbg(sdata,
1171 "received channel switch announcement to go to channel %d MHz\n",
1172 params.chandef.chan->center_freq);
1173
1174 params.block_tx = csa_ie.mode & WLAN_EID_CHAN_SWITCH_PARAM_TX_RESTRICT;
Chun-Yeow Yeoh3f718fd2013-11-08 15:09:43 +08001175 if (beacon) {
Chun-Yeow Yeoh33a45862013-10-17 15:55:18 -07001176 ifmsh->chsw_ttl = csa_ie.ttl - 1;
Chun-Yeow Yeoh3f718fd2013-11-08 15:09:43 +08001177 if (ifmsh->pre_value >= csa_ie.pre_value)
1178 return false;
1179 ifmsh->pre_value = csa_ie.pre_value;
1180 }
Chun-Yeow Yeoh33a45862013-10-17 15:55:18 -07001181
Luciano Coelho0cb4d4d2014-01-13 19:42:58 +02001182 if (ifmsh->chsw_ttl >= ifmsh->mshcfg.dot11MeshTTL)
Chun-Yeow Yeoh3f718fd2013-11-08 15:09:43 +08001183 return false;
Chun-Yeow Yeoh33a45862013-10-17 15:55:18 -07001184
Luciano Coelho0cb4d4d2014-01-13 19:42:58 +02001185 ifmsh->csa_role = IEEE80211_MESH_CSA_ROLE_REPEATER;
Chun-Yeow Yeoh33a45862013-10-17 15:55:18 -07001186
Luciano Coelho0cb4d4d2014-01-13 19:42:58 +02001187 if (ieee80211_channel_switch(sdata->local->hw.wiphy, sdata->dev,
1188 &params) < 0)
1189 return false;
Chun-Yeow Yeoh33a45862013-10-17 15:55:18 -07001190
1191 return true;
Chun-Yeow Yeoh33a45862013-10-17 15:55:18 -07001192}
1193
Thomas Pedersen9fb04b52013-02-14 11:20:14 -08001194static void
1195ieee80211_mesh_rx_probe_req(struct ieee80211_sub_if_data *sdata,
1196 struct ieee80211_mgmt *mgmt, size_t len)
1197{
1198 struct ieee80211_local *local = sdata->local;
1199 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1200 struct sk_buff *presp;
1201 struct beacon_data *bcn;
1202 struct ieee80211_mgmt *hdr;
1203 struct ieee802_11_elems elems;
1204 size_t baselen;
Johannes Berg511044e2013-03-07 22:47:00 +01001205 u8 *pos;
Thomas Pedersen9fb04b52013-02-14 11:20:14 -08001206
Thomas Pedersen9fb04b52013-02-14 11:20:14 -08001207 pos = mgmt->u.probe_req.variable;
1208 baselen = (u8 *) pos - (u8 *) mgmt;
1209 if (baselen > len)
1210 return;
1211
Sara Sharon4abb52a2019-01-16 12:14:41 +02001212 ieee802_11_parse_elems(pos, len - baselen, false, &elems, mgmt->bssid,
1213 NULL);
Thomas Pedersen9fb04b52013-02-14 11:20:14 -08001214
Chun-Yeow Yeoha4ef66a2013-08-22 10:28:58 -07001215 if (!elems.mesh_id)
1216 return;
1217
Thomas Pedersen9fb04b52013-02-14 11:20:14 -08001218 /* 802.11-2012 10.1.4.3.2 */
1219 if ((!ether_addr_equal(mgmt->da, sdata->vif.addr) &&
1220 !is_broadcast_ether_addr(mgmt->da)) ||
1221 elems.ssid_len != 0)
1222 return;
1223
1224 if (elems.mesh_id_len != 0 &&
1225 (elems.mesh_id_len != ifmsh->mesh_id_len ||
1226 memcmp(elems.mesh_id, ifmsh->mesh_id, ifmsh->mesh_id_len)))
1227 return;
1228
1229 rcu_read_lock();
1230 bcn = rcu_dereference(ifmsh->beacon);
1231
1232 if (!bcn)
1233 goto out;
1234
1235 presp = dev_alloc_skb(local->tx_headroom +
1236 bcn->head_len + bcn->tail_len);
1237 if (!presp)
1238 goto out;
1239
1240 skb_reserve(presp, local->tx_headroom);
Johannes Berg59ae1d12017-06-16 14:29:20 +02001241 skb_put_data(presp, bcn->head, bcn->head_len);
1242 skb_put_data(presp, bcn->tail, bcn->tail_len);
Thomas Pedersen9fb04b52013-02-14 11:20:14 -08001243 hdr = (struct ieee80211_mgmt *) presp->data;
1244 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1245 IEEE80211_STYPE_PROBE_RESP);
1246 memcpy(hdr->da, mgmt->sa, ETH_ALEN);
Thomas Pedersen9fb04b52013-02-14 11:20:14 -08001247 IEEE80211_SKB_CB(presp)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
1248 ieee80211_tx_skb(sdata, presp);
1249out:
1250 rcu_read_unlock();
1251}
1252
Johannes Berg472dbc42008-09-11 00:01:49 +02001253static void ieee80211_mesh_rx_bcn_presp(struct ieee80211_sub_if_data *sdata,
1254 u16 stype,
1255 struct ieee80211_mgmt *mgmt,
1256 size_t len,
1257 struct ieee80211_rx_status *rx_status)
1258{
Johannes Bergc6a1fa12008-10-07 12:04:32 +02001259 struct ieee80211_local *local = sdata->local;
Javier Cardonadbf498f2012-03-31 11:31:32 -07001260 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Johannes Berg472dbc42008-09-11 00:01:49 +02001261 struct ieee802_11_elems elems;
1262 struct ieee80211_channel *channel;
Johannes Berg472dbc42008-09-11 00:01:49 +02001263 size_t baselen;
1264 int freq;
Johannes Berg57fbcce2016-04-12 15:56:15 +02001265 enum nl80211_band band = rx_status->band;
Johannes Berg472dbc42008-09-11 00:01:49 +02001266
1267 /* ignore ProbeResp to foreign address */
1268 if (stype == IEEE80211_STYPE_PROBE_RESP &&
Joe Perchesb203ca32012-05-08 18:56:52 +00001269 !ether_addr_equal(mgmt->da, sdata->vif.addr))
Johannes Berg472dbc42008-09-11 00:01:49 +02001270 return;
1271
1272 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
1273 if (baselen > len)
1274 return;
1275
1276 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
Sara Sharon4abb52a2019-01-16 12:14:41 +02001277 false, &elems, mgmt->bssid, NULL);
Johannes Berg472dbc42008-09-11 00:01:49 +02001278
Thomas Pedersen9a90bc82012-10-20 19:03:10 -07001279 /* ignore non-mesh or secure / unsecure mismatch */
1280 if ((!elems.mesh_id || !elems.mesh_config) ||
1281 (elems.rsn && sdata->u.mesh.security == IEEE80211_MESH_SEC_NONE) ||
1282 (!elems.rsn && sdata->u.mesh.security != IEEE80211_MESH_SEC_NONE))
Javier Cardona5cff5e02011-04-07 15:08:29 -07001283 return;
1284
Johannes Berg1cd8e882013-03-27 14:30:12 +01001285 if (elems.ds_params)
Bruno Randolf59eb21a2011-01-17 13:37:28 +09001286 freq = ieee80211_channel_to_frequency(elems.ds_params[0], band);
Johannes Berg472dbc42008-09-11 00:01:49 +02001287 else
1288 freq = rx_status->freq;
1289
1290 channel = ieee80211_get_channel(local->hw.wiphy, freq);
1291
1292 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
1293 return;
1294
Masashi Honmaed92a9b2017-03-16 10:57:18 +09001295 if (mesh_matches_local(sdata, &elems)) {
1296 mpl_dbg(sdata, "rssi_threshold=%d,rx_status->signal=%d\n",
1297 sdata->u.mesh.mshcfg.rssi_threshold, rx_status->signal);
1298 if (!sdata->u.mesh.user_mpm ||
1299 sdata->u.mesh.mshcfg.rssi_threshold == 0 ||
1300 sdata->u.mesh.mshcfg.rssi_threshold < rx_status->signal)
Bob Copelandecbc12a2018-10-26 10:03:50 -04001301 mesh_neighbour_update(sdata, mgmt->sa, &elems,
1302 rx_status);
Tamizh chelvam93e2d042020-03-28 19:23:24 +05301303
1304 if (ifmsh->csa_role != IEEE80211_MESH_CSA_ROLE_INIT &&
1305 !sdata->vif.csa_active)
1306 ieee80211_mesh_process_chnswitch(sdata, &elems, true);
Masashi Honmaed92a9b2017-03-16 10:57:18 +09001307 }
Javier Cardonadbf498f2012-03-31 11:31:32 -07001308
1309 if (ifmsh->sync_ops)
1310 ifmsh->sync_ops->rx_bcn_presp(sdata,
1311 stype, mgmt, &elems, rx_status);
Johannes Berg472dbc42008-09-11 00:01:49 +02001312}
1313
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07001314int ieee80211_mesh_finish_csa(struct ieee80211_sub_if_data *sdata)
1315{
1316 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1317 struct mesh_csa_settings *tmp_csa_settings;
1318 int ret = 0;
Michal Kaziorfaf046e2014-01-29 07:56:17 +01001319 int changed = 0;
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07001320
1321 /* Reset the TTL value and Initiator flag */
Luciano Coelho0cb4d4d2014-01-13 19:42:58 +02001322 ifmsh->csa_role = IEEE80211_MESH_CSA_ROLE_NONE;
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07001323 ifmsh->chsw_ttl = 0;
1324
1325 /* Remove the CSA and MCSP elements from the beacon */
Thomas Pedersen55184242019-05-24 21:16:24 -07001326 tmp_csa_settings = rcu_dereference_protected(ifmsh->csa,
1327 lockdep_is_held(&sdata->wdev.mtx));
Monam Agarwal0c2bef462014-03-24 00:51:43 +05301328 RCU_INIT_POINTER(ifmsh->csa, NULL);
Luciano Coelho66e01cf2014-01-13 19:43:00 +02001329 if (tmp_csa_settings)
1330 kfree_rcu(tmp_csa_settings, rcu_head);
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07001331 ret = ieee80211_mesh_rebuild_beacon(sdata);
1332 if (ret)
1333 return -EINVAL;
1334
Michal Kaziorfaf046e2014-01-29 07:56:17 +01001335 changed |= BSS_CHANGED_BEACON;
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07001336
1337 mcsa_dbg(sdata, "complete switching to center freq %d MHz",
1338 sdata->vif.bss_conf.chandef.chan->center_freq);
Michal Kaziorfaf046e2014-01-29 07:56:17 +01001339 return changed;
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07001340}
1341
1342int ieee80211_mesh_csa_beacon(struct ieee80211_sub_if_data *sdata,
Luciano Coelho66e01cf2014-01-13 19:43:00 +02001343 struct cfg80211_csa_settings *csa_settings)
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07001344{
1345 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1346 struct mesh_csa_settings *tmp_csa_settings;
1347 int ret = 0;
1348
Thomas Pedersen55184242019-05-24 21:16:24 -07001349 lockdep_assert_held(&sdata->wdev.mtx);
1350
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07001351 tmp_csa_settings = kmalloc(sizeof(*tmp_csa_settings),
1352 GFP_ATOMIC);
1353 if (!tmp_csa_settings)
1354 return -ENOMEM;
1355
1356 memcpy(&tmp_csa_settings->settings, csa_settings,
1357 sizeof(struct cfg80211_csa_settings));
1358
1359 rcu_assign_pointer(ifmsh->csa, tmp_csa_settings);
1360
1361 ret = ieee80211_mesh_rebuild_beacon(sdata);
1362 if (ret) {
1363 tmp_csa_settings = rcu_dereference(ifmsh->csa);
Monam Agarwal0c2bef462014-03-24 00:51:43 +05301364 RCU_INIT_POINTER(ifmsh->csa, NULL);
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07001365 kfree_rcu(tmp_csa_settings, rcu_head);
1366 return ret;
1367 }
1368
Luciano Coelhob58e81e2014-01-13 19:42:59 +02001369 return BSS_CHANGED_BEACON;
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07001370}
1371
Chun-Yeow Yeoh8f2535b2013-10-14 19:08:27 -07001372static int mesh_fwd_csa_frame(struct ieee80211_sub_if_data *sdata,
Peter Ohc4de37e2018-01-26 14:02:37 -08001373 struct ieee80211_mgmt *mgmt, size_t len,
1374 struct ieee802_11_elems *elems)
Chun-Yeow Yeoh8f2535b2013-10-14 19:08:27 -07001375{
1376 struct ieee80211_mgmt *mgmt_fwd;
1377 struct sk_buff *skb;
1378 struct ieee80211_local *local = sdata->local;
Chun-Yeow Yeoh8f2535b2013-10-14 19:08:27 -07001379
1380 skb = dev_alloc_skb(local->tx_headroom + len);
1381 if (!skb)
1382 return -ENOMEM;
1383 skb_reserve(skb, local->tx_headroom);
Johannes Berg4df864c2017-06-16 14:29:21 +02001384 mgmt_fwd = skb_put(skb, len);
Chun-Yeow Yeoh8f2535b2013-10-14 19:08:27 -07001385
Peter Ohc4de37e2018-01-26 14:02:37 -08001386 elems->mesh_chansw_params_ie->mesh_ttl--;
1387 elems->mesh_chansw_params_ie->mesh_flags &=
1388 ~WLAN_EID_CHAN_SWITCH_PARAM_INITIATOR;
Chun-Yeow Yeoh8f2535b2013-10-14 19:08:27 -07001389
1390 memcpy(mgmt_fwd, mgmt, len);
1391 eth_broadcast_addr(mgmt_fwd->da);
1392 memcpy(mgmt_fwd->sa, sdata->vif.addr, ETH_ALEN);
1393 memcpy(mgmt_fwd->bssid, sdata->vif.addr, ETH_ALEN);
1394
1395 ieee80211_tx_skb(sdata, skb);
1396 return 0;
1397}
1398
1399static void mesh_rx_csa_frame(struct ieee80211_sub_if_data *sdata,
1400 struct ieee80211_mgmt *mgmt, size_t len)
1401{
1402 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1403 struct ieee802_11_elems elems;
1404 u16 pre_value;
Chun-Yeow Yeoh33a45862013-10-17 15:55:18 -07001405 bool fwd_csa = true;
Chun-Yeow Yeoh8f2535b2013-10-14 19:08:27 -07001406 size_t baselen;
Chun-Yeow Yeoh3f718fd2013-11-08 15:09:43 +08001407 u8 *pos;
Chun-Yeow Yeoh8f2535b2013-10-14 19:08:27 -07001408
1409 if (mgmt->u.action.u.measurement.action_code !=
1410 WLAN_ACTION_SPCT_CHL_SWITCH)
1411 return;
1412
1413 pos = mgmt->u.action.u.chan_switch.variable;
1414 baselen = offsetof(struct ieee80211_mgmt,
1415 u.action.u.chan_switch.variable);
Sara Sharon4abb52a2019-01-16 12:14:41 +02001416 ieee802_11_parse_elems(pos, len - baselen, true, &elems,
1417 mgmt->bssid, NULL);
Chun-Yeow Yeoh8f2535b2013-10-14 19:08:27 -07001418
Tamizh chelvam93e2d042020-03-28 19:23:24 +05301419 if (!mesh_matches_local(sdata, &elems))
1420 return;
1421
Chun-Yeow Yeoh3f718fd2013-11-08 15:09:43 +08001422 ifmsh->chsw_ttl = elems.mesh_chansw_params_ie->mesh_ttl;
1423 if (!--ifmsh->chsw_ttl)
Chun-Yeow Yeoh8f2535b2013-10-14 19:08:27 -07001424 fwd_csa = false;
1425
1426 pre_value = le16_to_cpu(elems.mesh_chansw_params_ie->mesh_pre_value);
1427 if (ifmsh->pre_value >= pre_value)
1428 return;
1429
1430 ifmsh->pre_value = pre_value;
1431
Luciano Coelho0cb4d4d2014-01-13 19:42:58 +02001432 if (!sdata->vif.csa_active &&
1433 !ieee80211_mesh_process_chnswitch(sdata, &elems, false)) {
Chun-Yeow Yeoh33a45862013-10-17 15:55:18 -07001434 mcsa_dbg(sdata, "Failed to process CSA action frame");
1435 return;
1436 }
1437
Chun-Yeow Yeoh8f2535b2013-10-14 19:08:27 -07001438 /* forward or re-broadcast the CSA frame */
1439 if (fwd_csa) {
Peter Ohc4de37e2018-01-26 14:02:37 -08001440 if (mesh_fwd_csa_frame(sdata, mgmt, len, &elems) < 0)
Chun-Yeow Yeoh8f2535b2013-10-14 19:08:27 -07001441 mcsa_dbg(sdata, "Failed to forward the CSA frame");
1442 }
Chun-Yeow Yeoh8f2535b2013-10-14 19:08:27 -07001443}
1444
Johannes Berg472dbc42008-09-11 00:01:49 +02001445static void ieee80211_mesh_rx_mgmt_action(struct ieee80211_sub_if_data *sdata,
1446 struct ieee80211_mgmt *mgmt,
1447 size_t len,
1448 struct ieee80211_rx_status *rx_status)
1449{
1450 switch (mgmt->u.action.category) {
Thomas Pedersen8db09852011-08-12 20:01:00 -07001451 case WLAN_CATEGORY_SELF_PROTECTED:
1452 switch (mgmt->u.action.u.self_prot.action_code) {
1453 case WLAN_SP_MESH_PEERING_OPEN:
1454 case WLAN_SP_MESH_PEERING_CLOSE:
1455 case WLAN_SP_MESH_PEERING_CONFIRM:
1456 mesh_rx_plink_frame(sdata, mgmt, len, rx_status);
1457 break;
1458 }
Johannes Berg472dbc42008-09-11 00:01:49 +02001459 break;
Thomas Pedersen25d49e42011-08-11 19:35:15 -07001460 case WLAN_CATEGORY_MESH_ACTION:
1461 if (mesh_action_is_path_sel(mgmt))
1462 mesh_rx_path_sel_frame(sdata, mgmt, len);
Johannes Berg472dbc42008-09-11 00:01:49 +02001463 break;
Chun-Yeow Yeoh8f2535b2013-10-14 19:08:27 -07001464 case WLAN_CATEGORY_SPECTRUM_MGMT:
1465 mesh_rx_csa_frame(sdata, mgmt, len);
1466 break;
Johannes Berg472dbc42008-09-11 00:01:49 +02001467 }
1468}
1469
Johannes Berg1fa57d02010-06-10 10:21:32 +02001470void ieee80211_mesh_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
1471 struct sk_buff *skb)
Johannes Berg472dbc42008-09-11 00:01:49 +02001472{
1473 struct ieee80211_rx_status *rx_status;
Johannes Berg472dbc42008-09-11 00:01:49 +02001474 struct ieee80211_mgmt *mgmt;
1475 u16 stype;
1476
Thomas Pedersenecccd072013-06-10 13:17:21 -07001477 sdata_lock(sdata);
1478
1479 /* mesh already went down */
Johannes Berg1693d342014-01-22 10:08:57 +01001480 if (!sdata->u.mesh.mesh_id_len)
Thomas Pedersenecccd072013-06-10 13:17:21 -07001481 goto out;
1482
Johannes Bergf1d58c22009-06-17 13:13:00 +02001483 rx_status = IEEE80211_SKB_RXCB(skb);
Johannes Berg472dbc42008-09-11 00:01:49 +02001484 mgmt = (struct ieee80211_mgmt *) skb->data;
1485 stype = le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE;
1486
1487 switch (stype) {
1488 case IEEE80211_STYPE_PROBE_RESP:
1489 case IEEE80211_STYPE_BEACON:
1490 ieee80211_mesh_rx_bcn_presp(sdata, stype, mgmt, skb->len,
1491 rx_status);
1492 break;
Thomas Pedersen9fb04b52013-02-14 11:20:14 -08001493 case IEEE80211_STYPE_PROBE_REQ:
1494 ieee80211_mesh_rx_probe_req(sdata, mgmt, skb->len);
1495 break;
Johannes Berg472dbc42008-09-11 00:01:49 +02001496 case IEEE80211_STYPE_ACTION:
1497 ieee80211_mesh_rx_mgmt_action(sdata, mgmt, skb->len, rx_status);
1498 break;
1499 }
Thomas Pedersenecccd072013-06-10 13:17:21 -07001500out:
1501 sdata_unlock(sdata);
Johannes Berg472dbc42008-09-11 00:01:49 +02001502}
1503
Thomas Pedersenf81a9de2013-06-13 15:54:41 -07001504static void mesh_bss_info_changed(struct ieee80211_sub_if_data *sdata)
1505{
1506 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1507 u32 bit, changed = 0;
1508
1509 for_each_set_bit(bit, &ifmsh->mbss_changed,
1510 sizeof(changed) * BITS_PER_BYTE) {
1511 clear_bit(bit, &ifmsh->mbss_changed);
1512 changed |= BIT(bit);
1513 }
1514
1515 if (sdata->vif.bss_conf.enable_beacon &&
1516 (changed & (BSS_CHANGED_BEACON |
1517 BSS_CHANGED_HT |
1518 BSS_CHANGED_BASIC_RATES |
1519 BSS_CHANGED_BEACON_INT)))
1520 if (ieee80211_mesh_rebuild_beacon(sdata))
1521 return;
1522
1523 ieee80211_bss_info_change_notify(sdata, changed);
1524}
1525
Johannes Berg1fa57d02010-06-10 10:21:32 +02001526void ieee80211_mesh_work(struct ieee80211_sub_if_data *sdata)
Johannes Berg472dbc42008-09-11 00:01:49 +02001527{
Johannes Berg472dbc42008-09-11 00:01:49 +02001528 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Johannes Berg472dbc42008-09-11 00:01:49 +02001529
Thomas Pedersenecccd072013-06-10 13:17:21 -07001530 sdata_lock(sdata);
1531
1532 /* mesh already went down */
Johannes Berg1693d342014-01-22 10:08:57 +01001533 if (!sdata->u.mesh.mesh_id_len)
Thomas Pedersenecccd072013-06-10 13:17:21 -07001534 goto out;
1535
Johannes Berg472dbc42008-09-11 00:01:49 +02001536 if (ifmsh->preq_queue_len &&
1537 time_after(jiffies,
1538 ifmsh->last_preq + msecs_to_jiffies(ifmsh->mshcfg.dot11MeshHWMPpreqMinInterval)))
1539 mesh_path_start_discovery(sdata);
1540
Javier Cardona18889232009-08-10 12:15:52 -07001541 if (test_and_clear_bit(MESH_WORK_HOUSEKEEPING, &ifmsh->wrkq_flags))
Johannes Bergbf7cd942013-02-15 14:40:31 +01001542 ieee80211_mesh_housekeeping(sdata);
Rui Pauloe304bfd2009-11-09 23:46:56 +00001543
1544 if (test_and_clear_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags))
1545 ieee80211_mesh_rootpath(sdata);
Javier Cardonadbf498f2012-03-31 11:31:32 -07001546
1547 if (test_and_clear_bit(MESH_WORK_DRIFT_ADJUST, &ifmsh->wrkq_flags))
Masashi Honma445cd452016-12-08 10:15:51 +09001548 mesh_sync_adjust_tsf(sdata);
Thomas Pedersenecccd072013-06-10 13:17:21 -07001549
Thomas Pedersenf81a9de2013-06-13 15:54:41 -07001550 if (test_and_clear_bit(MESH_WORK_MBSS_CHANGED, &ifmsh->wrkq_flags))
1551 mesh_bss_info_changed(sdata);
Thomas Pedersenecccd072013-06-10 13:17:21 -07001552out:
1553 sdata_unlock(sdata);
Johannes Berg472dbc42008-09-11 00:01:49 +02001554}
1555
Johannes Berg472dbc42008-09-11 00:01:49 +02001556
Johannes Berg902acc72008-02-23 15:17:19 +01001557void ieee80211_mesh_init_sdata(struct ieee80211_sub_if_data *sdata)
1558{
Johannes Berg472dbc42008-09-11 00:01:49 +02001559 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Johannes Bergad2d2232012-12-14 14:34:25 +01001560 static u8 zero_addr[ETH_ALEN] = {};
Johannes Berg902acc72008-02-23 15:17:19 +01001561
Kees Cook34f11cd2017-10-16 16:35:49 -07001562 timer_setup(&ifmsh->housekeeping_timer,
1563 ieee80211_mesh_housekeeping_timer, 0);
Johannes Berg472dbc42008-09-11 00:01:49 +02001564
Johannes Berg472dbc42008-09-11 00:01:49 +02001565 ifmsh->accepting_plinks = true;
Johannes Berg472dbc42008-09-11 00:01:49 +02001566 atomic_set(&ifmsh->mpaths, 0);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001567 mesh_rmc_init(sdata);
Johannes Berg472dbc42008-09-11 00:01:49 +02001568 ifmsh->last_preq = jiffies;
Thomas Pedersendca7e942011-11-24 17:15:24 -08001569 ifmsh->next_perr = jiffies;
Luciano Coelho0cb4d4d2014-01-13 19:42:58 +02001570 ifmsh->csa_role = IEEE80211_MESH_CSA_ROLE_NONE;
Johannes Berg902acc72008-02-23 15:17:19 +01001571 /* Allocate all mesh structures when creating the first mesh interface. */
1572 if (!mesh_allocated)
1573 ieee80211s_init();
Bob Copeland2bdaf382016-02-28 20:03:56 -05001574
1575 mesh_pathtbl_init(sdata);
1576
Kees Cook34f11cd2017-10-16 16:35:49 -07001577 timer_setup(&ifmsh->mesh_path_timer, ieee80211_mesh_path_timer, 0);
1578 timer_setup(&ifmsh->mesh_path_root_timer,
1579 ieee80211_mesh_path_root_timer, 0);
Johannes Berg472dbc42008-09-11 00:01:49 +02001580 INIT_LIST_HEAD(&ifmsh->preq_queue.list);
Marco Porsch3f52b7e2013-01-30 18:14:08 +01001581 skb_queue_head_init(&ifmsh->ps.bc_buf);
Johannes Berg472dbc42008-09-11 00:01:49 +02001582 spin_lock_init(&ifmsh->mesh_preq_queue_lock);
Javier Cardonadbf498f2012-03-31 11:31:32 -07001583 spin_lock_init(&ifmsh->sync_offset_lock);
Thomas Pedersen2b5e1962013-02-14 11:20:13 -08001584 RCU_INIT_POINTER(ifmsh->beacon, NULL);
Johannes Bergad2d2232012-12-14 14:34:25 +01001585
1586 sdata->vif.bss_conf.bssid = zero_addr;
Johannes Berg472dbc42008-09-11 00:01:49 +02001587}
Bob Copeland0371a082016-03-26 11:27:18 -04001588
1589void ieee80211_mesh_teardown_sdata(struct ieee80211_sub_if_data *sdata)
1590{
1591 mesh_rmc_free(sdata);
1592 mesh_pathtbl_unregister(sdata);
1593}