blob: 48f31ac9233c8b33558cf41a9b44412c7b1ad591 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01002/*
Rui Paulo264d9b72009-11-09 23:46:58 +00003 * Copyright (c) 2008, 2009 open80211s Ltd.
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01004 * Author: Luis Carlos Cobo <luisca@cozybit.com>
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01005 */
6
7#include <linux/etherdevice.h>
8#include <linux/list.h>
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01009#include <linux/random.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +010011#include <linux/spinlock.h>
12#include <linux/string.h>
13#include <net/mac80211.h>
Javier Cardona4777be42011-09-07 17:49:52 -070014#include "wme.h"
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +010015#include "ieee80211_i.h"
16#include "mesh.h"
17
Bob Copeland74932952016-03-18 22:03:24 -040018static void mesh_path_free_rcu(struct mesh_table *tbl, struct mesh_path *mpath);
19
Bob Copeland60854fd2016-03-02 10:09:20 -050020static u32 mesh_table_hash(const void *addr, u32 len, u32 seed)
21{
22 /* Use last four bytes of hw addr as hash index */
Felix Fietkau40586e32019-03-13 18:54:27 +010023 return jhash_1word(__get_unaligned_cpu32((u8 *)addr + 2), seed);
Bob Copeland60854fd2016-03-02 10:09:20 -050024}
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +010025
Bob Copeland60854fd2016-03-02 10:09:20 -050026static const struct rhashtable_params mesh_rht_params = {
27 .nelem_hint = 2,
28 .automatic_shrinking = true,
29 .key_len = ETH_ALEN,
30 .key_offset = offsetof(struct mesh_path, dst),
31 .head_offset = offsetof(struct mesh_path, rhash),
32 .hashfn = mesh_table_hash,
33};
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +010034
Johannes Bergbf7cd942013-02-15 14:40:31 +010035static inline bool mpath_expired(struct mesh_path *mpath)
36{
37 return (mpath->flags & MESH_PATH_ACTIVE) &&
38 time_after(jiffies, mpath->exp_time) &&
39 !(mpath->flags & MESH_PATH_FIXED);
40}
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +010041
Bob Copeland74932952016-03-18 22:03:24 -040042static void mesh_path_rht_free(void *ptr, void *tblptr)
Johannes Berg349eb8c2011-05-14 11:56:16 +020043{
Bob Copeland60854fd2016-03-02 10:09:20 -050044 struct mesh_path *mpath = ptr;
Bob Copeland74932952016-03-18 22:03:24 -040045 struct mesh_table *tbl = tblptr;
46
47 mesh_path_free_rcu(tbl, mpath);
Johannes Berg349eb8c2011-05-14 11:56:16 +020048}
49
Bob Copeland60854fd2016-03-02 10:09:20 -050050static struct mesh_table *mesh_table_alloc(void)
Johannes Berg349eb8c2011-05-14 11:56:16 +020051{
Johannes Berg6b86bd62011-05-12 13:38:50 +020052 struct mesh_table *newtbl;
53
Javier Cardonad676ff42011-05-17 16:13:34 -070054 newtbl = kmalloc(sizeof(struct mesh_table), GFP_ATOMIC);
Johannes Berg6b86bd62011-05-12 13:38:50 +020055 if (!newtbl)
56 return NULL;
57
Bob Copeland18b27ff2016-03-18 22:11:30 -040058 INIT_HLIST_HEAD(&newtbl->known_gates);
Herbert Xub4c3fbe2019-02-14 22:03:24 +080059 INIT_HLIST_HEAD(&newtbl->walk_head);
Johannes Berg6b86bd62011-05-12 13:38:50 +020060 atomic_set(&newtbl->entries, 0);
Javier Cardona5ee68e52011-08-09 16:45:08 -070061 spin_lock_init(&newtbl->gates_lock);
Herbert Xub4c3fbe2019-02-14 22:03:24 +080062 spin_lock_init(&newtbl->walk_lock);
Johannes Berg6b86bd62011-05-12 13:38:50 +020063
64 return newtbl;
65}
66
Bob Copeland60854fd2016-03-02 10:09:20 -050067static void mesh_table_free(struct mesh_table *tbl)
Javier Cardona18889232009-08-10 12:15:52 -070068{
Bob Copeland60854fd2016-03-02 10:09:20 -050069 rhashtable_free_and_destroy(&tbl->rhead,
Bob Copeland74932952016-03-18 22:03:24 -040070 mesh_path_rht_free, tbl);
Javier Cardona18889232009-08-10 12:15:52 -070071 kfree(tbl);
72}
73
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +010074/**
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +010075 * mesh_path_assign_nexthop - update mesh path next hop
76 *
77 * @mpath: mesh path to update
78 * @sta: next hop to assign
79 *
80 * Locking: mpath->state_lock must be held when calling this function
81 */
82void mesh_path_assign_nexthop(struct mesh_path *mpath, struct sta_info *sta)
83{
Javier Cardona10c836d2009-07-09 14:42:16 -070084 struct sk_buff *skb;
85 struct ieee80211_hdr *hdr;
Javier Cardona10c836d2009-07-09 14:42:16 -070086 unsigned long flags;
87
Johannes Bergd0709a62008-02-25 16:27:46 +010088 rcu_assign_pointer(mpath->next_hop, sta);
Javier Cardona10c836d2009-07-09 14:42:16 -070089
Javier Cardona10c836d2009-07-09 14:42:16 -070090 spin_lock_irqsave(&mpath->frame_queue.lock, flags);
Thomas Pedersenb22bd522012-08-09 18:15:39 -070091 skb_queue_walk(&mpath->frame_queue, skb) {
Javier Cardona10c836d2009-07-09 14:42:16 -070092 hdr = (struct ieee80211_hdr *) skb->data;
93 memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN);
Thomas Pedersen7e3c8862011-11-24 17:15:21 -080094 memcpy(hdr->addr2, mpath->sdata->vif.addr, ETH_ALEN);
Marco Porsch3f52b7e2013-01-30 18:14:08 +010095 ieee80211_mps_set_frame_flags(sta->sdata, sta, hdr);
Javier Cardona10c836d2009-07-09 14:42:16 -070096 }
97
Javier Cardona10c836d2009-07-09 14:42:16 -070098 spin_unlock_irqrestore(&mpath->frame_queue.lock, flags);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +010099}
100
Javier Cardona5ee68e52011-08-09 16:45:08 -0700101static void prepare_for_gate(struct sk_buff *skb, char *dst_addr,
102 struct mesh_path *gate_mpath)
103{
104 struct ieee80211_hdr *hdr;
105 struct ieee80211s_hdr *mshdr;
106 int mesh_hdrlen, hdrlen;
107 char *next_hop;
108
109 hdr = (struct ieee80211_hdr *) skb->data;
110 hdrlen = ieee80211_hdrlen(hdr->frame_control);
111 mshdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
112
113 if (!(mshdr->flags & MESH_FLAGS_AE)) {
114 /* size of the fixed part of the mesh header */
115 mesh_hdrlen = 6;
116
117 /* make room for the two extended addresses */
118 skb_push(skb, 2 * ETH_ALEN);
119 memmove(skb->data, hdr, hdrlen + mesh_hdrlen);
120
121 hdr = (struct ieee80211_hdr *) skb->data;
122
123 /* we preserve the previous mesh header and only add
124 * the new addreses */
125 mshdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
126 mshdr->flags = MESH_FLAGS_AE_A5_A6;
127 memcpy(mshdr->eaddr1, hdr->addr3, ETH_ALEN);
128 memcpy(mshdr->eaddr2, hdr->addr4, ETH_ALEN);
129 }
130
131 /* update next hop */
132 hdr = (struct ieee80211_hdr *) skb->data;
133 rcu_read_lock();
134 next_hop = rcu_dereference(gate_mpath->next_hop)->sta.addr;
135 memcpy(hdr->addr1, next_hop, ETH_ALEN);
136 rcu_read_unlock();
Thomas Pedersen7e3c8862011-11-24 17:15:21 -0800137 memcpy(hdr->addr2, gate_mpath->sdata->vif.addr, ETH_ALEN);
Javier Cardona5ee68e52011-08-09 16:45:08 -0700138 memcpy(hdr->addr3, dst_addr, ETH_ALEN);
139}
140
141/**
Javier Cardona5ee68e52011-08-09 16:45:08 -0700142 * mesh_path_move_to_queue - Move or copy frames from one mpath queue to another
143 *
144 * This function is used to transfer or copy frames from an unresolved mpath to
145 * a gate mpath. The function also adds the Address Extension field and
146 * updates the next hop.
147 *
148 * If a frame already has an Address Extension field, only the next hop and
149 * destination addresses are updated.
150 *
151 * The gate mpath must be an active mpath with a valid mpath->next_hop.
152 *
Andrew Lunn9fd00b42020-07-13 01:15:05 +0200153 * @gate_mpath: An active mpath the frames will be sent to (i.e. the gate)
Javier Cardona5ee68e52011-08-09 16:45:08 -0700154 * @from_mpath: The failed mpath
155 * @copy: When true, copy all the frames to the new mpath queue. When false,
156 * move them.
157 */
158static void mesh_path_move_to_queue(struct mesh_path *gate_mpath,
159 struct mesh_path *from_mpath,
160 bool copy)
161{
Thomas Pedersen4bd4c2d2012-08-09 18:15:40 -0700162 struct sk_buff *skb, *fskb, *tmp;
163 struct sk_buff_head failq;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700164 unsigned long flags;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700165
Johannes Berg8c5bb1f2014-04-29 17:55:26 +0200166 if (WARN_ON(gate_mpath == from_mpath))
167 return;
168 if (WARN_ON(!gate_mpath->next_hop))
169 return;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700170
Javier Cardona5ee68e52011-08-09 16:45:08 -0700171 __skb_queue_head_init(&failq);
172
173 spin_lock_irqsave(&from_mpath->frame_queue.lock, flags);
174 skb_queue_splice_init(&from_mpath->frame_queue, &failq);
175 spin_unlock_irqrestore(&from_mpath->frame_queue.lock, flags);
176
Thomas Pedersen4bd4c2d2012-08-09 18:15:40 -0700177 skb_queue_walk_safe(&failq, fskb, tmp) {
178 if (skb_queue_len(&gate_mpath->frame_queue) >=
179 MESH_FRAME_QUEUE_LEN) {
180 mpath_dbg(gate_mpath->sdata, "mpath queue full!\n");
181 break;
John W. Linville817a53d2011-08-24 15:12:41 -0400182 }
Javier Cardona5ee68e52011-08-09 16:45:08 -0700183
Thomas Pedersen4bd4c2d2012-08-09 18:15:40 -0700184 skb = skb_copy(fskb, GFP_ATOMIC);
185 if (WARN_ON(!skb))
186 break;
187
Javier Cardona5ee68e52011-08-09 16:45:08 -0700188 prepare_for_gate(skb, gate_mpath->dst, gate_mpath);
Thomas Pedersen4bd4c2d2012-08-09 18:15:40 -0700189 skb_queue_tail(&gate_mpath->frame_queue, skb);
190
191 if (copy)
192 continue;
193
194 __skb_unlink(fskb, &failq);
195 kfree_skb(fskb);
Javier Cardona5ee68e52011-08-09 16:45:08 -0700196 }
197
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200198 mpath_dbg(gate_mpath->sdata, "Mpath queue for gate %pM has %d frames\n",
199 gate_mpath->dst, skb_queue_len(&gate_mpath->frame_queue));
Javier Cardona5ee68e52011-08-09 16:45:08 -0700200
201 if (!copy)
202 return;
203
204 spin_lock_irqsave(&from_mpath->frame_queue.lock, flags);
205 skb_queue_splice(&failq, &from_mpath->frame_queue);
206 spin_unlock_irqrestore(&from_mpath->frame_queue.lock, flags);
207}
208
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100209
Johannes Berg4a3cb702013-02-12 16:43:19 +0100210static struct mesh_path *mpath_lookup(struct mesh_table *tbl, const u8 *dst,
211 struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100212{
213 struct mesh_path *mpath;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100214
Felix Fietkauef618b12019-03-16 18:06:30 +0100215 mpath = rhashtable_lookup(&tbl->rhead, dst, mesh_rht_params);
Bob Copeland60854fd2016-03-02 10:09:20 -0500216
217 if (mpath && mpath_expired(mpath)) {
218 spin_lock_bh(&mpath->state_lock);
219 mpath->flags &= ~MESH_PATH_ACTIVE;
220 spin_unlock_bh(&mpath->state_lock);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100221 }
Bob Copeland60854fd2016-03-02 10:09:20 -0500222 return mpath;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100223}
224
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100225/**
226 * mesh_path_lookup - look up a path in the mesh path table
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100227 * @sdata: local subif
Johannes Bergbf7cd942013-02-15 14:40:31 +0100228 * @dst: hardware address (ETH_ALEN length) of destination
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100229 *
230 * Returns: pointer to the mesh path structure, or NULL if not found
231 *
232 * Locking: must be called within a read rcu section.
233 */
Johannes Bergbf7cd942013-02-15 14:40:31 +0100234struct mesh_path *
235mesh_path_lookup(struct ieee80211_sub_if_data *sdata, const u8 *dst)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100236{
Bob Copeland60854fd2016-03-02 10:09:20 -0500237 return mpath_lookup(sdata->u.mesh.mesh_paths, dst, sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100238}
239
Johannes Bergbf7cd942013-02-15 14:40:31 +0100240struct mesh_path *
241mpp_path_lookup(struct ieee80211_sub_if_data *sdata, const u8 *dst)
YanBo79617de2008-09-22 13:30:32 +0800242{
Bob Copeland60854fd2016-03-02 10:09:20 -0500243 return mpath_lookup(sdata->u.mesh.mpp_paths, dst, sdata);
YanBo79617de2008-09-22 13:30:32 +0800244}
245
Bob Copeland60854fd2016-03-02 10:09:20 -0500246static struct mesh_path *
247__mesh_path_lookup_by_idx(struct mesh_table *tbl, int idx)
248{
Herbert Xub4c3fbe2019-02-14 22:03:24 +0800249 int i = 0;
250 struct mesh_path *mpath;
Bob Copeland60854fd2016-03-02 10:09:20 -0500251
Herbert Xub4c3fbe2019-02-14 22:03:24 +0800252 hlist_for_each_entry_rcu(mpath, &tbl->walk_head, walk_list) {
Bob Copeland60854fd2016-03-02 10:09:20 -0500253 if (i++ == idx)
254 break;
255 }
Bob Copeland60854fd2016-03-02 10:09:20 -0500256
Herbert Xub4c3fbe2019-02-14 22:03:24 +0800257 if (!mpath)
Bob Copeland60854fd2016-03-02 10:09:20 -0500258 return NULL;
259
260 if (mpath_expired(mpath)) {
261 spin_lock_bh(&mpath->state_lock);
262 mpath->flags &= ~MESH_PATH_ACTIVE;
263 spin_unlock_bh(&mpath->state_lock);
264 }
265 return mpath;
266}
YanBo79617de2008-09-22 13:30:32 +0800267
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100268/**
269 * mesh_path_lookup_by_idx - look up a path in the mesh path table by its index
270 * @idx: index
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200271 * @sdata: local subif, or NULL for all entries
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100272 *
273 * Returns: pointer to the mesh path structure, or NULL if not found.
274 *
275 * Locking: must be called within a read rcu section.
276 */
Johannes Bergbf7cd942013-02-15 14:40:31 +0100277struct mesh_path *
278mesh_path_lookup_by_idx(struct ieee80211_sub_if_data *sdata, int idx)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100279{
Bob Copeland60854fd2016-03-02 10:09:20 -0500280 return __mesh_path_lookup_by_idx(sdata->u.mesh.mesh_paths, idx);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100281}
282
Javier Cardona5ee68e52011-08-09 16:45:08 -0700283/**
Henning Roggea2db2ed2014-09-12 08:58:50 +0200284 * mpp_path_lookup_by_idx - look up a path in the proxy path table by its index
285 * @idx: index
286 * @sdata: local subif, or NULL for all entries
287 *
288 * Returns: pointer to the proxy path structure, or NULL if not found.
289 *
290 * Locking: must be called within a read rcu section.
291 */
292struct mesh_path *
293mpp_path_lookup_by_idx(struct ieee80211_sub_if_data *sdata, int idx)
294{
Bob Copeland60854fd2016-03-02 10:09:20 -0500295 return __mesh_path_lookup_by_idx(sdata->u.mesh.mpp_paths, idx);
Henning Roggea2db2ed2014-09-12 08:58:50 +0200296}
297
298/**
Johannes Berg30be52e2011-11-21 11:23:50 +0100299 * mesh_path_add_gate - add the given mpath to a mesh gate to our path table
300 * @mpath: gate path to add to table
Javier Cardona5ee68e52011-08-09 16:45:08 -0700301 */
Johannes Berg30be52e2011-11-21 11:23:50 +0100302int mesh_path_add_gate(struct mesh_path *mpath)
Javier Cardona5ee68e52011-08-09 16:45:08 -0700303{
Johannes Berg30be52e2011-11-21 11:23:50 +0100304 struct mesh_table *tbl;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700305 int err;
306
307 rcu_read_lock();
Bob Copeland60854fd2016-03-02 10:09:20 -0500308 tbl = mpath->sdata->u.mesh.mesh_paths;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700309
Bob Copeland947c2a02016-02-28 20:03:59 -0500310 spin_lock_bh(&mpath->state_lock);
311 if (mpath->is_gate) {
312 err = -EEXIST;
313 spin_unlock_bh(&mpath->state_lock);
Javier Cardona5ee68e52011-08-09 16:45:08 -0700314 goto err_rcu;
315 }
Javier Cardona5ee68e52011-08-09 16:45:08 -0700316 mpath->is_gate = true;
317 mpath->sdata->u.mesh.num_gates++;
Bob Copeland947c2a02016-02-28 20:03:59 -0500318
319 spin_lock(&tbl->gates_lock);
Bob Copeland18b27ff2016-03-18 22:11:30 -0400320 hlist_add_head_rcu(&mpath->gate_list, &tbl->known_gates);
Bob Copeland947c2a02016-02-28 20:03:59 -0500321 spin_unlock(&tbl->gates_lock);
322
323 spin_unlock_bh(&mpath->state_lock);
324
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200325 mpath_dbg(mpath->sdata,
326 "Mesh path: Recorded new gate: %pM. %d known gates\n",
327 mpath->dst, mpath->sdata->u.mesh.num_gates);
Johannes Bergbf7cd942013-02-15 14:40:31 +0100328 err = 0;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700329err_rcu:
330 rcu_read_unlock();
331 return err;
332}
333
334/**
335 * mesh_gate_del - remove a mesh gate from the list of known gates
336 * @tbl: table which holds our list of known gates
337 * @mpath: gate mpath
Javier Cardona5ee68e52011-08-09 16:45:08 -0700338 */
Johannes Bergbf7cd942013-02-15 14:40:31 +0100339static void mesh_gate_del(struct mesh_table *tbl, struct mesh_path *mpath)
Javier Cardona5ee68e52011-08-09 16:45:08 -0700340{
Bob Copeland947c2a02016-02-28 20:03:59 -0500341 lockdep_assert_held(&mpath->state_lock);
342 if (!mpath->is_gate)
343 return;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700344
Bob Copeland947c2a02016-02-28 20:03:59 -0500345 mpath->is_gate = false;
346 spin_lock_bh(&tbl->gates_lock);
347 hlist_del_rcu(&mpath->gate_list);
348 mpath->sdata->u.mesh.num_gates--;
349 spin_unlock_bh(&tbl->gates_lock);
350
351 mpath_dbg(mpath->sdata,
352 "Mesh path: Deleted gate: %pM. %d known gates\n",
353 mpath->dst, mpath->sdata->u.mesh.num_gates);
Javier Cardona5ee68e52011-08-09 16:45:08 -0700354}
355
356/**
Javier Cardona5ee68e52011-08-09 16:45:08 -0700357 * mesh_gate_num - number of gates known to this interface
358 * @sdata: subif data
359 */
360int mesh_gate_num(struct ieee80211_sub_if_data *sdata)
361{
362 return sdata->u.mesh.num_gates;
363}
364
Bob Copelandb15dc382016-02-28 20:03:58 -0500365static
366struct mesh_path *mesh_path_new(struct ieee80211_sub_if_data *sdata,
367 const u8 *dst, gfp_t gfp_flags)
368{
369 struct mesh_path *new_mpath;
370
371 new_mpath = kzalloc(sizeof(struct mesh_path), gfp_flags);
372 if (!new_mpath)
373 return NULL;
374
375 memcpy(new_mpath->dst, dst, ETH_ALEN);
376 eth_broadcast_addr(new_mpath->rann_snd_addr);
377 new_mpath->is_root = false;
378 new_mpath->sdata = sdata;
379 new_mpath->flags = 0;
380 skb_queue_head_init(&new_mpath->frame_queue);
Bob Copelandb15dc382016-02-28 20:03:58 -0500381 new_mpath->exp_time = jiffies;
382 spin_lock_init(&new_mpath->state_lock);
Kees Cook34f11cd2017-10-16 16:35:49 -0700383 timer_setup(&new_mpath->timer, mesh_path_timer, 0);
Bob Copelandb15dc382016-02-28 20:03:58 -0500384
385 return new_mpath;
386}
387
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100388/**
389 * mesh_path_add - allocate and add a new path to the mesh path table
Johannes Bergbf7cd942013-02-15 14:40:31 +0100390 * @dst: destination address of the path (ETH_ALEN length)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200391 * @sdata: local subif
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100392 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200393 * Returns: 0 on success
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100394 *
395 * State: the initial state of the new path is set to 0
396 */
Bob Copelandae76eef2013-03-29 09:38:39 -0400397struct mesh_path *mesh_path_add(struct ieee80211_sub_if_data *sdata,
398 const u8 *dst)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100399{
Johannes Berg349eb8c2011-05-14 11:56:16 +0200400 struct mesh_table *tbl;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100401 struct mesh_path *mpath, *new_mpath;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100402
Joe Perchesb203ca32012-05-08 18:56:52 +0000403 if (ether_addr_equal(dst, sdata->vif.addr))
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100404 /* never add ourselves as neighbours */
Bob Copelandae76eef2013-03-29 09:38:39 -0400405 return ERR_PTR(-ENOTSUPP);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100406
407 if (is_multicast_ether_addr(dst))
Bob Copelandae76eef2013-03-29 09:38:39 -0400408 return ERR_PTR(-ENOTSUPP);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100409
Johannes Berg472dbc42008-09-11 00:01:49 +0200410 if (atomic_add_unless(&sdata->u.mesh.mpaths, 1, MESH_MAX_MPATHS) == 0)
Bob Copelandae76eef2013-03-29 09:38:39 -0400411 return ERR_PTR(-ENOSPC);
412
Bob Copelandb15dc382016-02-28 20:03:58 -0500413 new_mpath = mesh_path_new(sdata, dst, GFP_ATOMIC);
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400414 if (!new_mpath)
Bob Copeland60854fd2016-03-02 10:09:20 -0500415 return ERR_PTR(-ENOMEM);
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400416
Bob Copeland60854fd2016-03-02 10:09:20 -0500417 tbl = sdata->u.mesh.mesh_paths;
Herbert Xub4c3fbe2019-02-14 22:03:24 +0800418 spin_lock_bh(&tbl->walk_lock);
Herbert Xu36922932019-02-14 22:03:26 +0800419 mpath = rhashtable_lookup_get_insert_fast(&tbl->rhead,
420 &new_mpath->rhash,
421 mesh_rht_params);
422 if (!mpath)
423 hlist_add_head(&new_mpath->walk_list, &tbl->walk_head);
Herbert Xub4c3fbe2019-02-14 22:03:24 +0800424 spin_unlock_bh(&tbl->walk_lock);
Bob Copeland60854fd2016-03-02 10:09:20 -0500425
Herbert Xu36922932019-02-14 22:03:26 +0800426 if (mpath) {
Bob Copeland60854fd2016-03-02 10:09:20 -0500427 kfree(new_mpath);
Herbert Xu4ff3a9d2019-02-14 22:03:25 +0800428
Herbert Xu36922932019-02-14 22:03:26 +0800429 if (IS_ERR(mpath))
430 return mpath;
Herbert Xu4ff3a9d2019-02-14 22:03:25 +0800431
Bob Copeland60854fd2016-03-02 10:09:20 -0500432 new_mpath = mpath;
433 }
Herbert Xu4ff3a9d2019-02-14 22:03:25 +0800434
Bob Copeland2bdaf382016-02-28 20:03:56 -0500435 sdata->u.mesh.mesh_paths_generation++;
Bob Copeland60854fd2016-03-02 10:09:20 -0500436 return new_mpath;
Javier Cardona18889232009-08-10 12:15:52 -0700437}
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100438
Johannes Bergbf7cd942013-02-15 14:40:31 +0100439int mpp_path_add(struct ieee80211_sub_if_data *sdata,
440 const u8 *dst, const u8 *mpp)
YanBo79617de2008-09-22 13:30:32 +0800441{
Johannes Berg349eb8c2011-05-14 11:56:16 +0200442 struct mesh_table *tbl;
Bob Copeland60854fd2016-03-02 10:09:20 -0500443 struct mesh_path *new_mpath;
444 int ret;
YanBo79617de2008-09-22 13:30:32 +0800445
Joe Perchesb203ca32012-05-08 18:56:52 +0000446 if (ether_addr_equal(dst, sdata->vif.addr))
YanBo79617de2008-09-22 13:30:32 +0800447 /* never add ourselves as neighbours */
448 return -ENOTSUPP;
449
450 if (is_multicast_ether_addr(dst))
451 return -ENOTSUPP;
452
Bob Copelandb15dc382016-02-28 20:03:58 -0500453 new_mpath = mesh_path_new(sdata, dst, GFP_ATOMIC);
YanBo79617de2008-09-22 13:30:32 +0800454
Bob Copeland60854fd2016-03-02 10:09:20 -0500455 if (!new_mpath)
456 return -ENOMEM;
YanBo79617de2008-09-22 13:30:32 +0800457
YanBo79617de2008-09-22 13:30:32 +0800458 memcpy(new_mpath->mpp, mpp, ETH_ALEN);
Bob Copeland60854fd2016-03-02 10:09:20 -0500459 tbl = sdata->u.mesh.mpp_paths;
Herbert Xub4c3fbe2019-02-14 22:03:24 +0800460
461 spin_lock_bh(&tbl->walk_lock);
Bob Copeland60854fd2016-03-02 10:09:20 -0500462 ret = rhashtable_lookup_insert_fast(&tbl->rhead,
463 &new_mpath->rhash,
464 mesh_rht_params);
Herbert Xub4c3fbe2019-02-14 22:03:24 +0800465 if (!ret)
466 hlist_add_head_rcu(&new_mpath->walk_list, &tbl->walk_head);
467 spin_unlock_bh(&tbl->walk_lock);
Henning Roggea2db2ed2014-09-12 08:58:50 +0200468
Herbert Xu4ff3a9d2019-02-14 22:03:25 +0800469 if (ret)
470 kfree(new_mpath);
471
Bob Copeland2bdaf382016-02-28 20:03:56 -0500472 sdata->u.mesh.mpp_paths_generation++;
Bob Copeland60854fd2016-03-02 10:09:20 -0500473 return ret;
YanBo79617de2008-09-22 13:30:32 +0800474}
475
476
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100477/**
478 * mesh_plink_broken - deactivates paths and sends perr when a link breaks
479 *
480 * @sta: broken peer link
481 *
482 * This function must be called from the rate control algorithm if enough
483 * delivery errors suggest that a peer link is no longer usable.
484 */
485void mesh_plink_broken(struct sta_info *sta)
486{
Bob Copeland60854fd2016-03-02 10:09:20 -0500487 struct ieee80211_sub_if_data *sdata = sta->sdata;
488 struct mesh_table *tbl = sdata->u.mesh.mesh_paths;
Johannes Berg15ff6362009-11-17 13:34:04 +0100489 static const u8 bcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100490 struct mesh_path *mpath;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100491
Herbert Xub4c3fbe2019-02-14 22:03:24 +0800492 rcu_read_lock();
493 hlist_for_each_entry_rcu(mpath, &tbl->walk_head, walk_list) {
Andreea-Cristina Bernat2688eba2014-08-17 16:18:02 +0300494 if (rcu_access_pointer(mpath->next_hop) == sta &&
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100495 mpath->flags & MESH_PATH_ACTIVE &&
496 !(mpath->flags & MESH_PATH_FIXED)) {
Javier Cardonaf5e50cd2011-08-29 13:23:05 -0700497 spin_lock_bh(&mpath->state_lock);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100498 mpath->flags &= ~MESH_PATH_ACTIVE;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000499 ++mpath->sn;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100500 spin_unlock_bh(&mpath->state_lock);
Johannes Bergbf7cd942013-02-15 14:40:31 +0100501 mesh_path_error_tx(sdata,
Chun-Yeow Yeohf63f8422013-11-13 15:39:12 +0800502 sdata->u.mesh.mshcfg.element_ttl,
503 mpath->dst, mpath->sn,
504 WLAN_REASON_MESH_PATH_DEST_UNREACHABLE, bcast);
Javier Cardonaf5e50cd2011-08-29 13:23:05 -0700505 }
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100506 }
Herbert Xub4c3fbe2019-02-14 22:03:24 +0800507 rcu_read_unlock();
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100508}
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100509
Bob Copeland74932952016-03-18 22:03:24 -0400510static void mesh_path_free_rcu(struct mesh_table *tbl,
511 struct mesh_path *mpath)
Javier Cardona19c50b32011-08-29 13:23:07 -0700512{
Bob Copeland60854fd2016-03-02 10:09:20 -0500513 struct ieee80211_sub_if_data *sdata = mpath->sdata;
Javier Cardona19c50b32011-08-29 13:23:07 -0700514
Bob Copeland947c2a02016-02-28 20:03:59 -0500515 spin_lock_bh(&mpath->state_lock);
Bob Copeland74932952016-03-18 22:03:24 -0400516 mpath->flags |= MESH_PATH_RESOLVING | MESH_PATH_DELETED;
Bob Copeland60854fd2016-03-02 10:09:20 -0500517 mesh_gate_del(tbl, mpath);
Bob Copeland947c2a02016-02-28 20:03:59 -0500518 spin_unlock_bh(&mpath->state_lock);
Bob Copeland74932952016-03-18 22:03:24 -0400519 del_timer_sync(&mpath->timer);
Johannes Bergc2e703a2015-11-17 14:25:21 +0100520 atomic_dec(&sdata->u.mesh.mpaths);
Javier Cardona19c50b32011-08-29 13:23:07 -0700521 atomic_dec(&tbl->entries);
Remi Pommarel5e435402020-07-04 15:54:19 +0200522 mesh_path_flush_pending(mpath);
Bob Copeland74932952016-03-18 22:03:24 -0400523 kfree_rcu(mpath, rcu);
524}
525
526static void __mesh_path_del(struct mesh_table *tbl, struct mesh_path *mpath)
527{
Herbert Xub4c3fbe2019-02-14 22:03:24 +0800528 hlist_del_rcu(&mpath->walk_list);
Bob Copeland74932952016-03-18 22:03:24 -0400529 rhashtable_remove_fast(&tbl->rhead, &mpath->rhash, mesh_rht_params);
530 mesh_path_free_rcu(tbl, mpath);
Javier Cardona19c50b32011-08-29 13:23:07 -0700531}
532
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100533/**
534 * mesh_path_flush_by_nexthop - Deletes mesh paths if their next hop matches
535 *
Ben Hutchings2c530402012-07-10 10:55:09 +0000536 * @sta: mesh peer to match
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100537 *
Luis Carlos Cobob4e08ea2008-02-29 15:46:08 -0800538 * RCU notes: this function is called when a mesh plink transitions from
539 * PLINK_ESTAB to any other state, since PLINK_ESTAB state is the only one that
540 * allows path creation. This will happen before the sta can be freed (because
Johannes Bergd0709a62008-02-25 16:27:46 +0100541 * sta_info_destroy() calls this) so any reader in a rcu read block will be
542 * protected against the plink disappearing.
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100543 */
544void mesh_path_flush_by_nexthop(struct sta_info *sta)
545{
Bob Copeland2bdaf382016-02-28 20:03:56 -0500546 struct ieee80211_sub_if_data *sdata = sta->sdata;
Bob Copeland60854fd2016-03-02 10:09:20 -0500547 struct mesh_table *tbl = sdata->u.mesh.mesh_paths;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100548 struct mesh_path *mpath;
Herbert Xub4c3fbe2019-02-14 22:03:24 +0800549 struct hlist_node *n;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100550
Herbert Xub4c3fbe2019-02-14 22:03:24 +0800551 spin_lock_bh(&tbl->walk_lock);
552 hlist_for_each_entry_safe(mpath, n, &tbl->walk_head, walk_list) {
Bob Copeland60854fd2016-03-02 10:09:20 -0500553 if (rcu_access_pointer(mpath->next_hop) == sta)
554 __mesh_path_del(tbl, mpath);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100555 }
Herbert Xub4c3fbe2019-02-14 22:03:24 +0800556 spin_unlock_bh(&tbl->walk_lock);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100557}
558
Henning Roggebf5a70e2016-02-03 13:58:36 +0100559static void mpp_flush_by_proxy(struct ieee80211_sub_if_data *sdata,
560 const u8 *proxy)
561{
Bob Copeland60854fd2016-03-02 10:09:20 -0500562 struct mesh_table *tbl = sdata->u.mesh.mpp_paths;
563 struct mesh_path *mpath;
Herbert Xub4c3fbe2019-02-14 22:03:24 +0800564 struct hlist_node *n;
Henning Roggebf5a70e2016-02-03 13:58:36 +0100565
Herbert Xub4c3fbe2019-02-14 22:03:24 +0800566 spin_lock_bh(&tbl->walk_lock);
567 hlist_for_each_entry_safe(mpath, n, &tbl->walk_head, walk_list) {
Bob Copeland60854fd2016-03-02 10:09:20 -0500568 if (ether_addr_equal(mpath->mpp, proxy))
569 __mesh_path_del(tbl, mpath);
Henning Roggebf5a70e2016-02-03 13:58:36 +0100570 }
Herbert Xub4c3fbe2019-02-14 22:03:24 +0800571 spin_unlock_bh(&tbl->walk_lock);
Henning Roggebf5a70e2016-02-03 13:58:36 +0100572}
573
Bob Copeland60854fd2016-03-02 10:09:20 -0500574static void table_flush_by_iface(struct mesh_table *tbl)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100575{
576 struct mesh_path *mpath;
Herbert Xub4c3fbe2019-02-14 22:03:24 +0800577 struct hlist_node *n;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100578
Herbert Xub4c3fbe2019-02-14 22:03:24 +0800579 spin_lock_bh(&tbl->walk_lock);
580 hlist_for_each_entry_safe(mpath, n, &tbl->walk_head, walk_list) {
Bob Copeland60854fd2016-03-02 10:09:20 -0500581 __mesh_path_del(tbl, mpath);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100582 }
Herbert Xub4c3fbe2019-02-14 22:03:24 +0800583 spin_unlock_bh(&tbl->walk_lock);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100584}
585
Javier Cardonaece1a2e2011-08-29 13:23:04 -0700586/**
587 * mesh_path_flush_by_iface - Deletes all mesh paths associated with a given iface
588 *
589 * This function deletes both mesh paths as well as mesh portal paths.
590 *
Ben Hutchings2c530402012-07-10 10:55:09 +0000591 * @sdata: interface data to match
Javier Cardonaece1a2e2011-08-29 13:23:04 -0700592 *
593 */
594void mesh_path_flush_by_iface(struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100595{
Bob Copeland60854fd2016-03-02 10:09:20 -0500596 table_flush_by_iface(sdata->u.mesh.mesh_paths);
597 table_flush_by_iface(sdata->u.mesh.mpp_paths);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100598}
599
600/**
Henning Rogge4cc955d2016-02-03 13:58:38 +0100601 * table_path_del - delete a path from the mesh or mpp table
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100602 *
Henning Rogge4cc955d2016-02-03 13:58:38 +0100603 * @tbl: mesh or mpp path table
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200604 * @sdata: local subif
Henning Rogge4cc955d2016-02-03 13:58:38 +0100605 * @addr: dst address (ETH_ALEN length)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100606 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200607 * Returns: 0 if successful
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100608 */
Bob Copeland60854fd2016-03-02 10:09:20 -0500609static int table_path_del(struct mesh_table *tbl,
Henning Rogge4cc955d2016-02-03 13:58:38 +0100610 struct ieee80211_sub_if_data *sdata,
611 const u8 *addr)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100612{
613 struct mesh_path *mpath;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100614
Herbert Xub4c3fbe2019-02-14 22:03:24 +0800615 spin_lock_bh(&tbl->walk_lock);
Bob Copeland60854fd2016-03-02 10:09:20 -0500616 mpath = rhashtable_lookup_fast(&tbl->rhead, addr, mesh_rht_params);
617 if (!mpath) {
Wei Yongjunf2ffff02019-02-18 11:29:29 +0100618 spin_unlock_bh(&tbl->walk_lock);
Bob Copeland60854fd2016-03-02 10:09:20 -0500619 return -ENXIO;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100620 }
621
Bob Copeland60854fd2016-03-02 10:09:20 -0500622 __mesh_path_del(tbl, mpath);
Herbert Xub4c3fbe2019-02-14 22:03:24 +0800623 spin_unlock_bh(&tbl->walk_lock);
Bob Copeland60854fd2016-03-02 10:09:20 -0500624 return 0;
Henning Rogge4cc955d2016-02-03 13:58:38 +0100625}
626
Bob Copeland60854fd2016-03-02 10:09:20 -0500627
Henning Rogge4cc955d2016-02-03 13:58:38 +0100628/**
629 * mesh_path_del - delete a mesh path from the table
630 *
631 * @addr: dst address (ETH_ALEN length)
632 * @sdata: local subif
633 *
634 * Returns: 0 if successful
635 */
636int mesh_path_del(struct ieee80211_sub_if_data *sdata, const u8 *addr)
637{
Bob Copeland60854fd2016-03-02 10:09:20 -0500638 int err;
Henning Rogge4cc955d2016-02-03 13:58:38 +0100639
640 /* flush relevant mpp entries first */
641 mpp_flush_by_proxy(sdata, addr);
642
Bob Copeland2bdaf382016-02-28 20:03:56 -0500643 err = table_path_del(sdata->u.mesh.mesh_paths, sdata, addr);
644 sdata->u.mesh.mesh_paths_generation++;
Henning Roggeab1c7902016-02-03 13:58:37 +0100645 return err;
646}
647
648/**
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100649 * mesh_path_tx_pending - sends pending frames in a mesh path queue
650 *
651 * @mpath: mesh path to activate
652 *
653 * Locking: the state_lock of the mpath structure must NOT be held when calling
654 * this function.
655 */
656void mesh_path_tx_pending(struct mesh_path *mpath)
657{
Javier Cardona249b4052009-07-07 10:55:03 -0700658 if (mpath->flags & MESH_PATH_ACTIVE)
659 ieee80211_add_pending_skbs(mpath->sdata->local,
660 &mpath->frame_queue);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100661}
662
663/**
Javier Cardona5ee68e52011-08-09 16:45:08 -0700664 * mesh_path_send_to_gates - sends pending frames to all known mesh gates
665 *
666 * @mpath: mesh path whose queue will be emptied
667 *
668 * If there is only one gate, the frames are transferred from the failed mpath
669 * queue to that gate's queue. If there are more than one gates, the frames
670 * are copied from each gate to the next. After frames are copied, the
671 * mpath queues are emptied onto the transmission queue.
672 */
673int mesh_path_send_to_gates(struct mesh_path *mpath)
674{
675 struct ieee80211_sub_if_data *sdata = mpath->sdata;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700676 struct mesh_table *tbl;
677 struct mesh_path *from_mpath = mpath;
Bob Copeland60854fd2016-03-02 10:09:20 -0500678 struct mesh_path *gate;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700679 bool copy = false;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700680
Bob Copeland60854fd2016-03-02 10:09:20 -0500681 tbl = sdata->u.mesh.mesh_paths;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700682
Bob Copeland60854fd2016-03-02 10:09:20 -0500683 rcu_read_lock();
Bob Copeland18b27ff2016-03-18 22:11:30 -0400684 hlist_for_each_entry_rcu(gate, &tbl->known_gates, gate_list) {
Bob Copeland947c2a02016-02-28 20:03:59 -0500685 if (gate->flags & MESH_PATH_ACTIVE) {
686 mpath_dbg(sdata, "Forwarding to %pM\n", gate->dst);
687 mesh_path_move_to_queue(gate, from_mpath, copy);
688 from_mpath = gate;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700689 copy = true;
690 } else {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200691 mpath_dbg(sdata,
Johannes Bergd671b2a2015-11-06 11:30:46 +0100692 "Not forwarding to %pM (flags %#x)\n",
Bob Copeland947c2a02016-02-28 20:03:59 -0500693 gate->dst, gate->flags);
Javier Cardona5ee68e52011-08-09 16:45:08 -0700694 }
695 }
696
Bob Copeland18b27ff2016-03-18 22:11:30 -0400697 hlist_for_each_entry_rcu(gate, &tbl->known_gates, gate_list) {
Bob Copeland947c2a02016-02-28 20:03:59 -0500698 mpath_dbg(sdata, "Sending to %pM\n", gate->dst);
699 mesh_path_tx_pending(gate);
Bob Copeland2bdaf382016-02-28 20:03:56 -0500700 }
Bob Copeland60854fd2016-03-02 10:09:20 -0500701 rcu_read_unlock();
Javier Cardona5ee68e52011-08-09 16:45:08 -0700702
703 return (from_mpath == mpath) ? -EHOSTUNREACH : 0;
704}
705
706/**
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100707 * mesh_path_discard_frame - discard a frame whose path could not be resolved
708 *
709 * @skb: frame to discard
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200710 * @sdata: network subif the frame was to be sent through
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100711 *
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100712 * Locking: the function must me called within a rcu_read_lock region
713 */
Johannes Bergbf7cd942013-02-15 14:40:31 +0100714void mesh_path_discard_frame(struct ieee80211_sub_if_data *sdata,
715 struct sk_buff *skb)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100716{
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100717 kfree_skb(skb);
Johannes Berg472dbc42008-09-11 00:01:49 +0200718 sdata->u.mesh.mshstats.dropped_frames_no_route++;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100719}
720
721/**
722 * mesh_path_flush_pending - free the pending queue of a mesh path
723 *
724 * @mpath: mesh path whose queue has to be freed
725 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300726 * Locking: the function must me called within a rcu_read_lock region
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100727 */
728void mesh_path_flush_pending(struct mesh_path *mpath)
729{
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100730 struct sk_buff *skb;
731
Javier Cardona00e3f252011-08-09 16:45:07 -0700732 while ((skb = skb_dequeue(&mpath->frame_queue)) != NULL)
Johannes Bergbf7cd942013-02-15 14:40:31 +0100733 mesh_path_discard_frame(mpath->sdata, skb);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100734}
735
736/**
737 * mesh_path_fix_nexthop - force a specific next hop for a mesh path
738 *
739 * @mpath: the mesh path to modify
740 * @next_hop: the next hop to force
741 *
742 * Locking: this function must be called holding mpath->state_lock
743 */
744void mesh_path_fix_nexthop(struct mesh_path *mpath, struct sta_info *next_hop)
745{
746 spin_lock_bh(&mpath->state_lock);
747 mesh_path_assign_nexthop(mpath, next_hop);
Rui Paulod19b3bf2009-11-09 23:46:55 +0000748 mpath->sn = 0xffff;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100749 mpath->metric = 0;
750 mpath->hop_count = 0;
751 mpath->exp_time = 0;
Pedersen, Thomas5df20f22016-09-06 11:59:00 -0700752 mpath->flags = MESH_PATH_FIXED | MESH_PATH_SN_VALID;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100753 mesh_path_activate(mpath);
754 spin_unlock_bh(&mpath->state_lock);
Manoharan, Rajkumar3eb09282017-02-14 12:27:16 -0800755 ewma_mesh_fail_avg_init(&next_hop->mesh->fail_avg);
756 /* init it at a low value - 0 start is tricky */
757 ewma_mesh_fail_avg_add(&next_hop->mesh->fail_avg, 1);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100758 mesh_path_tx_pending(mpath);
759}
760
Bob Copeland2bdaf382016-02-28 20:03:56 -0500761int mesh_pathtbl_init(struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100762{
Johannes Berg349eb8c2011-05-14 11:56:16 +0200763 struct mesh_table *tbl_path, *tbl_mpp;
Dan Carpenter4c5ade42011-08-30 22:16:15 +0300764 int ret;
YanBo79617de2008-09-22 13:30:32 +0800765
Bob Copeland60854fd2016-03-02 10:09:20 -0500766 tbl_path = mesh_table_alloc();
Johannes Berg349eb8c2011-05-14 11:56:16 +0200767 if (!tbl_path)
768 return -ENOMEM;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700769
Bob Copeland60854fd2016-03-02 10:09:20 -0500770 tbl_mpp = mesh_table_alloc();
Johannes Berg349eb8c2011-05-14 11:56:16 +0200771 if (!tbl_mpp) {
Dan Carpenter4c5ade42011-08-30 22:16:15 +0300772 ret = -ENOMEM;
773 goto free_path;
YanBo79617de2008-09-22 13:30:32 +0800774 }
Johannes Berg349eb8c2011-05-14 11:56:16 +0200775
Bob Copeland60854fd2016-03-02 10:09:20 -0500776 rhashtable_init(&tbl_path->rhead, &mesh_rht_params);
777 rhashtable_init(&tbl_mpp->rhead, &mesh_rht_params);
Bob Copeland2bdaf382016-02-28 20:03:56 -0500778
Bob Copeland60854fd2016-03-02 10:09:20 -0500779 sdata->u.mesh.mesh_paths = tbl_path;
780 sdata->u.mesh.mpp_paths = tbl_mpp;
YanBo79617de2008-09-22 13:30:32 +0800781
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100782 return 0;
Dan Carpenter4c5ade42011-08-30 22:16:15 +0300783
Dan Carpenter4c5ade42011-08-30 22:16:15 +0300784free_path:
Bob Copeland60854fd2016-03-02 10:09:20 -0500785 mesh_table_free(tbl_path);
Dan Carpenter4c5ade42011-08-30 22:16:15 +0300786 return ret;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100787}
788
Bob Copeland60854fd2016-03-02 10:09:20 -0500789static
790void mesh_path_tbl_expire(struct ieee80211_sub_if_data *sdata,
791 struct mesh_table *tbl)
792{
793 struct mesh_path *mpath;
Herbert Xub4c3fbe2019-02-14 22:03:24 +0800794 struct hlist_node *n;
Bob Copeland60854fd2016-03-02 10:09:20 -0500795
Herbert Xub4c3fbe2019-02-14 22:03:24 +0800796 spin_lock_bh(&tbl->walk_lock);
797 hlist_for_each_entry_safe(mpath, n, &tbl->walk_head, walk_list) {
Bob Copeland60854fd2016-03-02 10:09:20 -0500798 if ((!(mpath->flags & MESH_PATH_RESOLVING)) &&
799 (!(mpath->flags & MESH_PATH_FIXED)) &&
800 time_after(jiffies, mpath->exp_time + MESH_PATH_EXPIRE))
801 __mesh_path_del(tbl, mpath);
802 }
Herbert Xub4c3fbe2019-02-14 22:03:24 +0800803 spin_unlock_bh(&tbl->walk_lock);
Bob Copeland60854fd2016-03-02 10:09:20 -0500804}
805
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200806void mesh_path_expire(struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100807{
Bob Copeland60854fd2016-03-02 10:09:20 -0500808 mesh_path_tbl_expire(sdata, sdata->u.mesh.mesh_paths);
809 mesh_path_tbl_expire(sdata, sdata->u.mesh.mpp_paths);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100810}
811
Bob Copeland2bdaf382016-02-28 20:03:56 -0500812void mesh_pathtbl_unregister(struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100813{
Bob Copeland60854fd2016-03-02 10:09:20 -0500814 mesh_table_free(sdata->u.mesh.mesh_paths);
815 mesh_table_free(sdata->u.mesh.mpp_paths);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100816}