Mention branches and keyring.
[releases.git] / mac80211 / mesh.h
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (c) 2008, 2009 open80211s Ltd.
4  * Copyright (C) 2023 Intel Corporation
5  * Authors:    Luis Carlos Cobo <luisca@cozybit.com>
6  *             Javier Cardona <javier@cozybit.com>
7  */
8
9 #ifndef IEEE80211S_H
10 #define IEEE80211S_H
11
12 #include <linux/types.h>
13 #include <linux/jhash.h>
14 #include "ieee80211_i.h"
15
16
17 /* Data structures */
18
19 /**
20  * enum mesh_path_flags - mac80211 mesh path flags
21  *
22  * @MESH_PATH_ACTIVE: the mesh path can be used for forwarding
23  * @MESH_PATH_RESOLVING: the discovery process is running for this mesh path
24  * @MESH_PATH_SN_VALID: the mesh path contains a valid destination sequence
25  *      number
26  * @MESH_PATH_FIXED: the mesh path has been manually set and should not be
27  *      modified
28  * @MESH_PATH_RESOLVED: the mesh path can has been resolved
29  * @MESH_PATH_REQ_QUEUED: there is an unsent path request for this destination
30  *      already queued up, waiting for the discovery process to start.
31  * @MESH_PATH_DELETED: the mesh path has been deleted and should no longer
32  *      be used
33  *
34  * MESH_PATH_RESOLVED is used by the mesh path timer to
35  * decide when to stop or cancel the mesh path discovery.
36  */
37 enum mesh_path_flags {
38         MESH_PATH_ACTIVE =      BIT(0),
39         MESH_PATH_RESOLVING =   BIT(1),
40         MESH_PATH_SN_VALID =    BIT(2),
41         MESH_PATH_FIXED =       BIT(3),
42         MESH_PATH_RESOLVED =    BIT(4),
43         MESH_PATH_REQ_QUEUED =  BIT(5),
44         MESH_PATH_DELETED =     BIT(6),
45 };
46
47 /**
48  * enum mesh_deferred_task_flags - mac80211 mesh deferred tasks
49  *
50  *
51  *
52  * @MESH_WORK_HOUSEKEEPING: run the periodic mesh housekeeping tasks
53  * @MESH_WORK_ROOT: the mesh root station needs to send a frame
54  * @MESH_WORK_DRIFT_ADJUST: time to compensate for clock drift relative to other
55  * mesh nodes
56  * @MESH_WORK_MBSS_CHANGED: rebuild beacon and notify driver of BSS changes
57  */
58 enum mesh_deferred_task_flags {
59         MESH_WORK_HOUSEKEEPING,
60         MESH_WORK_ROOT,
61         MESH_WORK_DRIFT_ADJUST,
62         MESH_WORK_MBSS_CHANGED,
63 };
64
65 /**
66  * struct mesh_path - mac80211 mesh path structure
67  *
68  * @dst: mesh path destination mac address
69  * @mpp: mesh proxy mac address
70  * @rhash: rhashtable list pointer
71  * @walk_list: linked list containing all mesh_path objects.
72  * @gate_list: list pointer for known gates list
73  * @sdata: mesh subif
74  * @next_hop: mesh neighbor to which frames for this destination will be
75  *      forwarded
76  * @timer: mesh path discovery timer
77  * @frame_queue: pending queue for frames sent to this destination while the
78  *      path is unresolved
79  * @rcu: rcu head for freeing mesh path
80  * @sn: target sequence number
81  * @metric: current metric to this destination
82  * @hop_count: hops to destination
83  * @exp_time: in jiffies, when the path will expire or when it expired
84  * @discovery_timeout: timeout (lapse in jiffies) used for the last discovery
85  *      retry
86  * @discovery_retries: number of discovery retries
87  * @flags: mesh path flags, as specified on &enum mesh_path_flags
88  * @state_lock: mesh path state lock used to protect changes to the
89  * mpath itself.  No need to take this lock when adding or removing
90  * an mpath to a hash bucket on a path table.
91  * @rann_snd_addr: the RANN sender address
92  * @rann_metric: the aggregated path metric towards the root node
93  * @last_preq_to_root: Timestamp of last PREQ sent to root
94  * @is_root: the destination station of this path is a root node
95  * @is_gate: the destination station of this path is a mesh gate
96  * @path_change_count: the number of path changes to destination
97  *
98  *
99  * The dst address is unique in the mesh path table. Since the mesh_path is
100  * protected by RCU, deleting the next_hop STA must remove / substitute the
101  * mesh_path structure and wait until that is no longer reachable before
102  * destroying the STA completely.
103  */
104 struct mesh_path {
105         u8 dst[ETH_ALEN];
106         u8 mpp[ETH_ALEN];       /* used for MPP or MAP */
107         struct rhash_head rhash;
108         struct hlist_node walk_list;
109         struct hlist_node gate_list;
110         struct ieee80211_sub_if_data *sdata;
111         struct sta_info __rcu *next_hop;
112         struct timer_list timer;
113         struct sk_buff_head frame_queue;
114         struct rcu_head rcu;
115         u32 sn;
116         u32 metric;
117         u8 hop_count;
118         unsigned long exp_time;
119         u32 discovery_timeout;
120         u8 discovery_retries;
121         enum mesh_path_flags flags;
122         spinlock_t state_lock;
123         u8 rann_snd_addr[ETH_ALEN];
124         u32 rann_metric;
125         unsigned long last_preq_to_root;
126         unsigned long fast_tx_check;
127         bool is_root;
128         bool is_gate;
129         u32 path_change_count;
130 };
131
132 #define MESH_FAST_TX_CACHE_MAX_SIZE             512
133 #define MESH_FAST_TX_CACHE_THRESHOLD_SIZE       384
134 #define MESH_FAST_TX_CACHE_TIMEOUT              8000 /* msecs */
135
136 /**
137  * enum ieee80211_mesh_fast_tx_type - cached mesh fast tx entry type
138  *
139  * @MESH_FAST_TX_TYPE_LOCAL: tx from the local vif address as SA
140  * @MESH_FAST_TX_TYPE_PROXIED: local tx with a different SA (e.g. bridged)
141  * @MESH_FAST_TX_TYPE_FORWARDED: forwarded from a different mesh point
142  * @NUM_MESH_FAST_TX_TYPE: number of entry types
143  */
144 enum ieee80211_mesh_fast_tx_type {
145         MESH_FAST_TX_TYPE_LOCAL,
146         MESH_FAST_TX_TYPE_PROXIED,
147         MESH_FAST_TX_TYPE_FORWARDED,
148
149         /* must be last */
150         NUM_MESH_FAST_TX_TYPE
151 };
152
153
154 /**
155  * struct ieee80211_mesh_fast_tx_key - cached mesh fast tx entry key
156  *
157  * @addr: The Ethernet DA for this entry
158  * @type: cache entry type
159  */
160 struct ieee80211_mesh_fast_tx_key {
161         u8 addr[ETH_ALEN] __aligned(2);
162         u16 type;
163 };
164
165 /**
166  * struct ieee80211_mesh_fast_tx - cached mesh fast tx entry
167  * @rhash: rhashtable pointer
168  * @key: the lookup key for this cache entry
169  * @fast_tx: base fast_tx data
170  * @hdr: cached mesh and rfc1042 headers
171  * @hdrlen: length of mesh + rfc1042
172  * @walk_list: list containing all the fast tx entries
173  * @mpath: mesh path corresponding to the Mesh DA
174  * @mppath: MPP entry corresponding to this DA
175  * @timestamp: Last used time of this entry
176  */
177 struct ieee80211_mesh_fast_tx {
178         struct rhash_head rhash;
179         struct ieee80211_mesh_fast_tx_key key;
180
181         struct ieee80211_fast_tx fast_tx;
182         u8 hdr[sizeof(struct ieee80211s_hdr) + sizeof(rfc1042_header)];
183         u16 hdrlen;
184
185         struct mesh_path *mpath, *mppath;
186         struct hlist_node walk_list;
187         unsigned long timestamp;
188 };
189
190 /* Recent multicast cache */
191 /* RMC_BUCKETS must be a power of 2, maximum 256 */
192 #define RMC_BUCKETS             256
193 #define RMC_QUEUE_MAX_LEN       4
194 #define RMC_TIMEOUT             (3 * HZ)
195
196 /**
197  * struct rmc_entry - entry in the Recent Multicast Cache
198  *
199  * @seqnum: mesh sequence number of the frame
200  * @exp_time: expiration time of the entry, in jiffies
201  * @sa: source address of the frame
202  * @list: hashtable list pointer
203  *
204  * The Recent Multicast Cache keeps track of the latest multicast frames that
205  * have been received by a mesh interface and discards received multicast frames
206  * that are found in the cache.
207  */
208 struct rmc_entry {
209         struct hlist_node list;
210         unsigned long exp_time;
211         u32 seqnum;
212         u8 sa[ETH_ALEN];
213 };
214
215 struct mesh_rmc {
216         struct hlist_head bucket[RMC_BUCKETS];
217         u32 idx_mask;
218 };
219
220 #define IEEE80211_MESH_HOUSEKEEPING_INTERVAL (60 * HZ)
221
222 #define MESH_PATH_EXPIRE (600 * HZ)
223
224 /* Default maximum number of plinks per interface */
225 #define MESH_MAX_PLINKS         256
226
227 /* Maximum number of paths per interface */
228 #define MESH_MAX_MPATHS         1024
229
230 /* Number of frames buffered per destination for unresolved destinations */
231 #define MESH_FRAME_QUEUE_LEN    10
232
233 /* Public interfaces */
234 /* Various */
235 int ieee80211_fill_mesh_addresses(struct ieee80211_hdr *hdr, __le16 *fc,
236                                   const u8 *da, const u8 *sa);
237 unsigned int ieee80211_new_mesh_header(struct ieee80211_sub_if_data *sdata,
238                                        struct ieee80211s_hdr *meshhdr,
239                                        const char *addr4or5, const char *addr6);
240 int mesh_rmc_check(struct ieee80211_sub_if_data *sdata,
241                    const u8 *addr, struct ieee80211s_hdr *mesh_hdr);
242 bool mesh_matches_local(struct ieee80211_sub_if_data *sdata,
243                         struct ieee802_11_elems *ie);
244 int mesh_add_meshconf_ie(struct ieee80211_sub_if_data *sdata,
245                          struct sk_buff *skb);
246 int mesh_add_meshid_ie(struct ieee80211_sub_if_data *sdata,
247                        struct sk_buff *skb);
248 int mesh_add_rsn_ie(struct ieee80211_sub_if_data *sdata,
249                     struct sk_buff *skb);
250 int mesh_add_vendor_ies(struct ieee80211_sub_if_data *sdata,
251                         struct sk_buff *skb);
252 int mesh_add_ht_cap_ie(struct ieee80211_sub_if_data *sdata,
253                        struct sk_buff *skb);
254 int mesh_add_ht_oper_ie(struct ieee80211_sub_if_data *sdata,
255                         struct sk_buff *skb);
256 int mesh_add_vht_cap_ie(struct ieee80211_sub_if_data *sdata,
257                         struct sk_buff *skb);
258 int mesh_add_vht_oper_ie(struct ieee80211_sub_if_data *sdata,
259                          struct sk_buff *skb);
260 int mesh_add_he_cap_ie(struct ieee80211_sub_if_data *sdata,
261                        struct sk_buff *skb, u8 ie_len);
262 int mesh_add_he_oper_ie(struct ieee80211_sub_if_data *sdata,
263                         struct sk_buff *skb);
264 int mesh_add_he_6ghz_cap_ie(struct ieee80211_sub_if_data *sdata,
265                             struct sk_buff *skb);
266 int mesh_add_eht_cap_ie(struct ieee80211_sub_if_data *sdata,
267                         struct sk_buff *skb, u8 ie_len);
268 int mesh_add_eht_oper_ie(struct ieee80211_sub_if_data *sdata,
269                          struct sk_buff *skb);
270 void mesh_rmc_free(struct ieee80211_sub_if_data *sdata);
271 int mesh_rmc_init(struct ieee80211_sub_if_data *sdata);
272 void ieee80211s_init(void);
273 void ieee80211s_update_metric(struct ieee80211_local *local,
274                               struct sta_info *sta,
275                               struct ieee80211_tx_status *st);
276 void ieee80211_mesh_init_sdata(struct ieee80211_sub_if_data *sdata);
277 void ieee80211_mesh_teardown_sdata(struct ieee80211_sub_if_data *sdata);
278 int ieee80211_start_mesh(struct ieee80211_sub_if_data *sdata);
279 void ieee80211_stop_mesh(struct ieee80211_sub_if_data *sdata);
280 void ieee80211_mesh_root_setup(struct ieee80211_if_mesh *ifmsh);
281 const struct ieee80211_mesh_sync_ops *ieee80211_mesh_sync_ops_get(u8 method);
282 /* wrapper for ieee80211_bss_info_change_notify() */
283 void ieee80211_mbss_info_change_notify(struct ieee80211_sub_if_data *sdata,
284                                        u64 changed);
285
286 /* mesh power save */
287 u64 ieee80211_mps_local_status_update(struct ieee80211_sub_if_data *sdata);
288 u64 ieee80211_mps_set_sta_local_pm(struct sta_info *sta,
289                                    enum nl80211_mesh_power_mode pm);
290 void ieee80211_mps_set_frame_flags(struct ieee80211_sub_if_data *sdata,
291                                    struct sta_info *sta,
292                                    struct ieee80211_hdr *hdr);
293 void ieee80211_mps_sta_status_update(struct sta_info *sta);
294 void ieee80211_mps_rx_h_sta_process(struct sta_info *sta,
295                                     struct ieee80211_hdr *hdr);
296 void ieee80211_mpsp_trigger_process(u8 *qc, struct sta_info *sta,
297                                     bool tx, bool acked);
298 void ieee80211_mps_frame_release(struct sta_info *sta,
299                                  struct ieee802_11_elems *elems);
300
301 /* Mesh paths */
302 int mesh_nexthop_lookup(struct ieee80211_sub_if_data *sdata,
303                         struct sk_buff *skb);
304 int mesh_nexthop_resolve(struct ieee80211_sub_if_data *sdata,
305                          struct sk_buff *skb);
306 void mesh_path_start_discovery(struct ieee80211_sub_if_data *sdata);
307 struct mesh_path *mesh_path_lookup(struct ieee80211_sub_if_data *sdata,
308                                    const u8 *dst);
309 struct mesh_path *mpp_path_lookup(struct ieee80211_sub_if_data *sdata,
310                                   const u8 *dst);
311 int mpp_path_add(struct ieee80211_sub_if_data *sdata,
312                  const u8 *dst, const u8 *mpp);
313 struct mesh_path *
314 mesh_path_lookup_by_idx(struct ieee80211_sub_if_data *sdata, int idx);
315 struct mesh_path *
316 mpp_path_lookup_by_idx(struct ieee80211_sub_if_data *sdata, int idx);
317 void mesh_path_fix_nexthop(struct mesh_path *mpath, struct sta_info *next_hop);
318 void mesh_path_expire(struct ieee80211_sub_if_data *sdata);
319 void mesh_rx_path_sel_frame(struct ieee80211_sub_if_data *sdata,
320                             struct ieee80211_mgmt *mgmt, size_t len);
321 struct mesh_path *
322 mesh_path_add(struct ieee80211_sub_if_data *sdata, const u8 *dst);
323
324 int mesh_path_add_gate(struct mesh_path *mpath);
325 int mesh_path_send_to_gates(struct mesh_path *mpath);
326 int mesh_gate_num(struct ieee80211_sub_if_data *sdata);
327 u32 airtime_link_metric_get(struct ieee80211_local *local,
328                             struct sta_info *sta);
329
330 /* Mesh plinks */
331 void mesh_neighbour_update(struct ieee80211_sub_if_data *sdata,
332                            u8 *hw_addr, struct ieee802_11_elems *ie,
333                            struct ieee80211_rx_status *rx_status);
334 bool mesh_peer_accepts_plinks(struct ieee802_11_elems *ie);
335 u64 mesh_accept_plinks_update(struct ieee80211_sub_if_data *sdata);
336 void mesh_plink_timer(struct timer_list *t);
337 void mesh_plink_broken(struct sta_info *sta);
338 u64 mesh_plink_deactivate(struct sta_info *sta);
339 u64 mesh_plink_open(struct sta_info *sta);
340 u64 mesh_plink_block(struct sta_info *sta);
341 void mesh_rx_plink_frame(struct ieee80211_sub_if_data *sdata,
342                          struct ieee80211_mgmt *mgmt, size_t len,
343                          struct ieee80211_rx_status *rx_status);
344 void mesh_sta_cleanup(struct sta_info *sta);
345
346 /* Private interfaces */
347 /* Mesh paths */
348 int mesh_path_error_tx(struct ieee80211_sub_if_data *sdata,
349                        u8 ttl, const u8 *target, u32 target_sn,
350                        u16 target_rcode, const u8 *ra);
351 void mesh_path_assign_nexthop(struct mesh_path *mpath, struct sta_info *sta);
352 void mesh_path_flush_pending(struct mesh_path *mpath);
353 void mesh_path_tx_pending(struct mesh_path *mpath);
354 void mesh_pathtbl_init(struct ieee80211_sub_if_data *sdata);
355 void mesh_pathtbl_unregister(struct ieee80211_sub_if_data *sdata);
356 int mesh_path_del(struct ieee80211_sub_if_data *sdata, const u8 *addr);
357 void mesh_path_timer(struct timer_list *t);
358 void mesh_path_flush_by_nexthop(struct sta_info *sta);
359 void mesh_path_discard_frame(struct ieee80211_sub_if_data *sdata,
360                              struct sk_buff *skb);
361 void mesh_path_tx_root_frame(struct ieee80211_sub_if_data *sdata);
362
363 bool mesh_action_is_path_sel(struct ieee80211_mgmt *mgmt);
364 struct ieee80211_mesh_fast_tx *
365 mesh_fast_tx_get(struct ieee80211_sub_if_data *sdata,
366                  struct ieee80211_mesh_fast_tx_key *key);
367 bool ieee80211_mesh_xmit_fast(struct ieee80211_sub_if_data *sdata,
368                               struct sk_buff *skb, u32 ctrl_flags);
369 void mesh_fast_tx_cache(struct ieee80211_sub_if_data *sdata,
370                         struct sk_buff *skb, struct mesh_path *mpath);
371 void mesh_fast_tx_gc(struct ieee80211_sub_if_data *sdata);
372 void mesh_fast_tx_flush_addr(struct ieee80211_sub_if_data *sdata,
373                              const u8 *addr);
374 void mesh_fast_tx_flush_mpath(struct mesh_path *mpath);
375 void mesh_fast_tx_flush_sta(struct ieee80211_sub_if_data *sdata,
376                             struct sta_info *sta);
377 void mesh_path_refresh(struct ieee80211_sub_if_data *sdata,
378                        struct mesh_path *mpath, const u8 *addr);
379
380 #ifdef CONFIG_MAC80211_MESH
381 static inline
382 u64 mesh_plink_inc_estab_count(struct ieee80211_sub_if_data *sdata)
383 {
384         atomic_inc(&sdata->u.mesh.estab_plinks);
385         return mesh_accept_plinks_update(sdata) | BSS_CHANGED_BEACON;
386 }
387
388 static inline
389 u64 mesh_plink_dec_estab_count(struct ieee80211_sub_if_data *sdata)
390 {
391         atomic_dec(&sdata->u.mesh.estab_plinks);
392         return mesh_accept_plinks_update(sdata) | BSS_CHANGED_BEACON;
393 }
394
395 static inline int mesh_plink_free_count(struct ieee80211_sub_if_data *sdata)
396 {
397         return sdata->u.mesh.mshcfg.dot11MeshMaxPeerLinks -
398                atomic_read(&sdata->u.mesh.estab_plinks);
399 }
400
401 static inline bool mesh_plink_availables(struct ieee80211_sub_if_data *sdata)
402 {
403         return (min_t(long, mesh_plink_free_count(sdata),
404                    MESH_MAX_PLINKS - sdata->local->num_sta)) > 0;
405 }
406
407 static inline void mesh_path_activate(struct mesh_path *mpath)
408 {
409         mpath->flags |= MESH_PATH_ACTIVE | MESH_PATH_RESOLVED;
410 }
411
412 static inline bool mesh_path_sel_is_hwmp(struct ieee80211_sub_if_data *sdata)
413 {
414         return sdata->u.mesh.mesh_pp_id == IEEE80211_PATH_PROTOCOL_HWMP;
415 }
416
417 void mesh_path_flush_by_iface(struct ieee80211_sub_if_data *sdata);
418 void mesh_sync_adjust_tsf(struct ieee80211_sub_if_data *sdata);
419 void ieee80211s_stop(void);
420 #else
421 static inline bool mesh_path_sel_is_hwmp(struct ieee80211_sub_if_data *sdata)
422 { return false; }
423 static inline void mesh_path_flush_by_iface(struct ieee80211_sub_if_data *sdata)
424 {}
425 static inline void ieee80211s_stop(void) {}
426 #endif
427
428 #endif /* IEEE80211S_H */