blob: 2ba7aa56b11c0ec121d7851665c989e1a2b3b2d1 [file] [log] [blame]
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001/*
Rui Paulo264d9b72009-11-09 23:46:58 +00002 * Copyright (c) 2008, 2009 open80211s Ltd.
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01003 * Author: Luis Carlos Cobo <luisca@cozybit.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
9
10#include <linux/etherdevice.h>
11#include <linux/list.h>
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +010012#include <linux/random.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +010014#include <linux/spinlock.h>
15#include <linux/string.h>
16#include <net/mac80211.h>
Javier Cardona4777be42011-09-07 17:49:52 -070017#include "wme.h"
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +010018#include "ieee80211_i.h"
19#include "mesh.h"
20
21/* There will be initially 2^INIT_PATHS_SIZE_ORDER buckets */
22#define INIT_PATHS_SIZE_ORDER 2
23
24/* Keep the mean chain length below this constant */
25#define MEAN_CHAIN_LEN 2
26
Johannes Bergbf7cd942013-02-15 14:40:31 +010027static inline bool mpath_expired(struct mesh_path *mpath)
28{
29 return (mpath->flags & MESH_PATH_ACTIVE) &&
30 time_after(jiffies, mpath->exp_time) &&
31 !(mpath->flags & MESH_PATH_FIXED);
32}
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +010033
34struct mpath_node {
35 struct hlist_node list;
36 struct rcu_head rcu;
37 /* This indirection allows two different tables to point to the same
38 * mesh_path structure, useful when resizing
39 */
40 struct mesh_path *mpath;
41};
42
Johannes Berg349eb8c2011-05-14 11:56:16 +020043static struct mesh_table __rcu *mesh_paths;
44static struct mesh_table __rcu *mpp_paths; /* Store paths for MPP&MAP */
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +010045
Johannes Bergf5ea9122009-08-07 16:17:38 +020046int mesh_paths_generation;
Henning Roggea2db2ed2014-09-12 08:58:50 +020047int mpp_paths_generation;
Johannes Berg6b86bd62011-05-12 13:38:50 +020048
49/* This lock will have the grow table function as writer and add / delete nodes
Javier Cardona239289e2011-08-29 13:23:09 -070050 * as readers. RCU provides sufficient protection only when reading the table
51 * (i.e. doing lookups). Adding or adding or removing nodes requires we take
52 * the read lock or we risk operating on an old table. The write lock is only
53 * needed when modifying the number of buckets a table.
Johannes Berg6b86bd62011-05-12 13:38:50 +020054 */
55static DEFINE_RWLOCK(pathtbl_resize_lock);
56
57
Henning Rogge4cc955d2016-02-03 13:58:38 +010058static inline struct mesh_table *resize_dereference_paths(
59 struct mesh_table __rcu *table)
60{
61 return rcu_dereference_protected(table,
62 lockdep_is_held(&pathtbl_resize_lock));
63}
64
Johannes Berg349eb8c2011-05-14 11:56:16 +020065static inline struct mesh_table *resize_dereference_mesh_paths(void)
66{
Henning Rogge4cc955d2016-02-03 13:58:38 +010067 return resize_dereference_paths(mesh_paths);
Johannes Berg349eb8c2011-05-14 11:56:16 +020068}
69
70static inline struct mesh_table *resize_dereference_mpp_paths(void)
71{
Henning Rogge4cc955d2016-02-03 13:58:38 +010072 return resize_dereference_paths(mpp_paths);
Johannes Berg349eb8c2011-05-14 11:56:16 +020073}
74
75/*
76 * CAREFUL -- "tbl" must not be an expression,
77 * in particular not an rcu_dereference(), since
78 * it's used twice. So it is illegal to do
79 * for_each_mesh_entry(rcu_dereference(...), ...)
80 */
Sasha Levinb67bfe02013-02-27 17:06:00 -080081#define for_each_mesh_entry(tbl, node, i) \
Johannes Berg349eb8c2011-05-14 11:56:16 +020082 for (i = 0; i <= tbl->hash_mask; i++) \
Sasha Levinb67bfe02013-02-27 17:06:00 -080083 hlist_for_each_entry_rcu(node, &tbl->hash_buckets[i], list)
Johannes Berg349eb8c2011-05-14 11:56:16 +020084
85
Johannes Berg6b86bd62011-05-12 13:38:50 +020086static struct mesh_table *mesh_table_alloc(int size_order)
87{
88 int i;
89 struct mesh_table *newtbl;
90
Javier Cardonad676ff42011-05-17 16:13:34 -070091 newtbl = kmalloc(sizeof(struct mesh_table), GFP_ATOMIC);
Johannes Berg6b86bd62011-05-12 13:38:50 +020092 if (!newtbl)
93 return NULL;
94
95 newtbl->hash_buckets = kzalloc(sizeof(struct hlist_head) *
Javier Cardonad676ff42011-05-17 16:13:34 -070096 (1 << size_order), GFP_ATOMIC);
Johannes Berg6b86bd62011-05-12 13:38:50 +020097
98 if (!newtbl->hash_buckets) {
99 kfree(newtbl);
100 return NULL;
101 }
102
103 newtbl->hashwlock = kmalloc(sizeof(spinlock_t) *
Javier Cardonad676ff42011-05-17 16:13:34 -0700104 (1 << size_order), GFP_ATOMIC);
Johannes Berg6b86bd62011-05-12 13:38:50 +0200105 if (!newtbl->hashwlock) {
106 kfree(newtbl->hash_buckets);
107 kfree(newtbl);
108 return NULL;
109 }
110
111 newtbl->size_order = size_order;
112 newtbl->hash_mask = (1 << size_order) - 1;
113 atomic_set(&newtbl->entries, 0);
114 get_random_bytes(&newtbl->hash_rnd,
115 sizeof(newtbl->hash_rnd));
116 for (i = 0; i <= newtbl->hash_mask; i++)
117 spin_lock_init(&newtbl->hashwlock[i]);
Javier Cardona5ee68e52011-08-09 16:45:08 -0700118 spin_lock_init(&newtbl->gates_lock);
Johannes Berg6b86bd62011-05-12 13:38:50 +0200119
120 return newtbl;
121}
122
Javier Cardona18889232009-08-10 12:15:52 -0700123static void __mesh_table_free(struct mesh_table *tbl)
124{
125 kfree(tbl->hash_buckets);
126 kfree(tbl->hashwlock);
127 kfree(tbl);
128}
129
Johannes Berg6b86bd62011-05-12 13:38:50 +0200130static void mesh_table_free(struct mesh_table *tbl, bool free_leafs)
Javier Cardona18889232009-08-10 12:15:52 -0700131{
132 struct hlist_head *mesh_hash;
133 struct hlist_node *p, *q;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700134 struct mpath_node *gate;
Javier Cardona18889232009-08-10 12:15:52 -0700135 int i;
136
137 mesh_hash = tbl->hash_buckets;
138 for (i = 0; i <= tbl->hash_mask; i++) {
Javier Cardona9b84b8082011-05-03 16:57:16 -0700139 spin_lock_bh(&tbl->hashwlock[i]);
Javier Cardona18889232009-08-10 12:15:52 -0700140 hlist_for_each_safe(p, q, &mesh_hash[i]) {
141 tbl->free_node(p, free_leafs);
142 atomic_dec(&tbl->entries);
143 }
Javier Cardona9b84b8082011-05-03 16:57:16 -0700144 spin_unlock_bh(&tbl->hashwlock[i]);
Javier Cardona18889232009-08-10 12:15:52 -0700145 }
Javier Cardona5ee68e52011-08-09 16:45:08 -0700146 if (free_leafs) {
147 spin_lock_bh(&tbl->gates_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800148 hlist_for_each_entry_safe(gate, q,
Javier Cardona5ee68e52011-08-09 16:45:08 -0700149 tbl->known_gates, list) {
150 hlist_del(&gate->list);
151 kfree(gate);
152 }
153 kfree(tbl->known_gates);
154 spin_unlock_bh(&tbl->gates_lock);
155 }
156
Javier Cardona18889232009-08-10 12:15:52 -0700157 __mesh_table_free(tbl);
158}
159
cozybit Inca3e6b122011-04-13 11:10:28 -0700160static int mesh_table_grow(struct mesh_table *oldtbl,
Johannes Berg6b86bd62011-05-12 13:38:50 +0200161 struct mesh_table *newtbl)
Javier Cardona18889232009-08-10 12:15:52 -0700162{
Javier Cardona18889232009-08-10 12:15:52 -0700163 struct hlist_head *oldhash;
164 struct hlist_node *p, *q;
165 int i;
166
cozybit Inca3e6b122011-04-13 11:10:28 -0700167 if (atomic_read(&oldtbl->entries)
Bob Copeland3f73fe92016-01-30 13:17:03 -0500168 < MEAN_CHAIN_LEN * (oldtbl->hash_mask + 1))
cozybit Inca3e6b122011-04-13 11:10:28 -0700169 return -EAGAIN;
Javier Cardona18889232009-08-10 12:15:52 -0700170
cozybit Inca3e6b122011-04-13 11:10:28 -0700171 newtbl->free_node = oldtbl->free_node;
cozybit Inca3e6b122011-04-13 11:10:28 -0700172 newtbl->copy_node = oldtbl->copy_node;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700173 newtbl->known_gates = oldtbl->known_gates;
cozybit Inca3e6b122011-04-13 11:10:28 -0700174 atomic_set(&newtbl->entries, atomic_read(&oldtbl->entries));
Javier Cardona18889232009-08-10 12:15:52 -0700175
cozybit Inca3e6b122011-04-13 11:10:28 -0700176 oldhash = oldtbl->hash_buckets;
177 for (i = 0; i <= oldtbl->hash_mask; i++)
Javier Cardona18889232009-08-10 12:15:52 -0700178 hlist_for_each(p, &oldhash[i])
cozybit Inca3e6b122011-04-13 11:10:28 -0700179 if (oldtbl->copy_node(p, newtbl) < 0)
Javier Cardona18889232009-08-10 12:15:52 -0700180 goto errcopy;
181
cozybit Inca3e6b122011-04-13 11:10:28 -0700182 return 0;
Javier Cardona18889232009-08-10 12:15:52 -0700183
184errcopy:
185 for (i = 0; i <= newtbl->hash_mask; i++) {
186 hlist_for_each_safe(p, q, &newtbl->hash_buckets[i])
cozybit Inca3e6b122011-04-13 11:10:28 -0700187 oldtbl->free_node(p, 0);
Javier Cardona18889232009-08-10 12:15:52 -0700188 }
cozybit Inca3e6b122011-04-13 11:10:28 -0700189 return -ENOMEM;
Javier Cardona18889232009-08-10 12:15:52 -0700190}
191
Johannes Berg4a3cb702013-02-12 16:43:19 +0100192static u32 mesh_table_hash(const u8 *addr, struct ieee80211_sub_if_data *sdata,
Johannes Berg6b86bd62011-05-12 13:38:50 +0200193 struct mesh_table *tbl)
194{
195 /* Use last four bytes of hw addr and interface index as hash index */
Johannes Bergbf7cd942013-02-15 14:40:31 +0100196 return jhash_2words(*(u32 *)(addr+2), sdata->dev->ifindex,
197 tbl->hash_rnd) & tbl->hash_mask;
Johannes Berg6b86bd62011-05-12 13:38:50 +0200198}
Johannes Bergf5ea9122009-08-07 16:17:38 +0200199
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100200
201/**
202 *
203 * mesh_path_assign_nexthop - update mesh path next hop
204 *
205 * @mpath: mesh path to update
206 * @sta: next hop to assign
207 *
208 * Locking: mpath->state_lock must be held when calling this function
209 */
210void mesh_path_assign_nexthop(struct mesh_path *mpath, struct sta_info *sta)
211{
Javier Cardona10c836d2009-07-09 14:42:16 -0700212 struct sk_buff *skb;
213 struct ieee80211_hdr *hdr;
Javier Cardona10c836d2009-07-09 14:42:16 -0700214 unsigned long flags;
215
Johannes Bergd0709a62008-02-25 16:27:46 +0100216 rcu_assign_pointer(mpath->next_hop, sta);
Javier Cardona10c836d2009-07-09 14:42:16 -0700217
Javier Cardona10c836d2009-07-09 14:42:16 -0700218 spin_lock_irqsave(&mpath->frame_queue.lock, flags);
Thomas Pedersenb22bd522012-08-09 18:15:39 -0700219 skb_queue_walk(&mpath->frame_queue, skb) {
Javier Cardona10c836d2009-07-09 14:42:16 -0700220 hdr = (struct ieee80211_hdr *) skb->data;
221 memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN);
Thomas Pedersen7e3c8862011-11-24 17:15:21 -0800222 memcpy(hdr->addr2, mpath->sdata->vif.addr, ETH_ALEN);
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100223 ieee80211_mps_set_frame_flags(sta->sdata, sta, hdr);
Javier Cardona10c836d2009-07-09 14:42:16 -0700224 }
225
Javier Cardona10c836d2009-07-09 14:42:16 -0700226 spin_unlock_irqrestore(&mpath->frame_queue.lock, flags);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100227}
228
Javier Cardona5ee68e52011-08-09 16:45:08 -0700229static void prepare_for_gate(struct sk_buff *skb, char *dst_addr,
230 struct mesh_path *gate_mpath)
231{
232 struct ieee80211_hdr *hdr;
233 struct ieee80211s_hdr *mshdr;
234 int mesh_hdrlen, hdrlen;
235 char *next_hop;
236
237 hdr = (struct ieee80211_hdr *) skb->data;
238 hdrlen = ieee80211_hdrlen(hdr->frame_control);
239 mshdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
240
241 if (!(mshdr->flags & MESH_FLAGS_AE)) {
242 /* size of the fixed part of the mesh header */
243 mesh_hdrlen = 6;
244
245 /* make room for the two extended addresses */
246 skb_push(skb, 2 * ETH_ALEN);
247 memmove(skb->data, hdr, hdrlen + mesh_hdrlen);
248
249 hdr = (struct ieee80211_hdr *) skb->data;
250
251 /* we preserve the previous mesh header and only add
252 * the new addreses */
253 mshdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
254 mshdr->flags = MESH_FLAGS_AE_A5_A6;
255 memcpy(mshdr->eaddr1, hdr->addr3, ETH_ALEN);
256 memcpy(mshdr->eaddr2, hdr->addr4, ETH_ALEN);
257 }
258
259 /* update next hop */
260 hdr = (struct ieee80211_hdr *) skb->data;
261 rcu_read_lock();
262 next_hop = rcu_dereference(gate_mpath->next_hop)->sta.addr;
263 memcpy(hdr->addr1, next_hop, ETH_ALEN);
264 rcu_read_unlock();
Thomas Pedersen7e3c8862011-11-24 17:15:21 -0800265 memcpy(hdr->addr2, gate_mpath->sdata->vif.addr, ETH_ALEN);
Javier Cardona5ee68e52011-08-09 16:45:08 -0700266 memcpy(hdr->addr3, dst_addr, ETH_ALEN);
267}
268
269/**
270 *
271 * mesh_path_move_to_queue - Move or copy frames from one mpath queue to another
272 *
273 * This function is used to transfer or copy frames from an unresolved mpath to
274 * a gate mpath. The function also adds the Address Extension field and
275 * updates the next hop.
276 *
277 * If a frame already has an Address Extension field, only the next hop and
278 * destination addresses are updated.
279 *
280 * The gate mpath must be an active mpath with a valid mpath->next_hop.
281 *
282 * @mpath: An active mpath the frames will be sent to (i.e. the gate)
283 * @from_mpath: The failed mpath
284 * @copy: When true, copy all the frames to the new mpath queue. When false,
285 * move them.
286 */
287static void mesh_path_move_to_queue(struct mesh_path *gate_mpath,
288 struct mesh_path *from_mpath,
289 bool copy)
290{
Thomas Pedersen4bd4c2d2012-08-09 18:15:40 -0700291 struct sk_buff *skb, *fskb, *tmp;
292 struct sk_buff_head failq;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700293 unsigned long flags;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700294
Johannes Berg8c5bb1f2014-04-29 17:55:26 +0200295 if (WARN_ON(gate_mpath == from_mpath))
296 return;
297 if (WARN_ON(!gate_mpath->next_hop))
298 return;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700299
Javier Cardona5ee68e52011-08-09 16:45:08 -0700300 __skb_queue_head_init(&failq);
301
302 spin_lock_irqsave(&from_mpath->frame_queue.lock, flags);
303 skb_queue_splice_init(&from_mpath->frame_queue, &failq);
304 spin_unlock_irqrestore(&from_mpath->frame_queue.lock, flags);
305
Thomas Pedersen4bd4c2d2012-08-09 18:15:40 -0700306 skb_queue_walk_safe(&failq, fskb, tmp) {
307 if (skb_queue_len(&gate_mpath->frame_queue) >=
308 MESH_FRAME_QUEUE_LEN) {
309 mpath_dbg(gate_mpath->sdata, "mpath queue full!\n");
310 break;
John W. Linville817a53d2011-08-24 15:12:41 -0400311 }
Javier Cardona5ee68e52011-08-09 16:45:08 -0700312
Thomas Pedersen4bd4c2d2012-08-09 18:15:40 -0700313 skb = skb_copy(fskb, GFP_ATOMIC);
314 if (WARN_ON(!skb))
315 break;
316
Javier Cardona5ee68e52011-08-09 16:45:08 -0700317 prepare_for_gate(skb, gate_mpath->dst, gate_mpath);
Thomas Pedersen4bd4c2d2012-08-09 18:15:40 -0700318 skb_queue_tail(&gate_mpath->frame_queue, skb);
319
320 if (copy)
321 continue;
322
323 __skb_unlink(fskb, &failq);
324 kfree_skb(fskb);
Javier Cardona5ee68e52011-08-09 16:45:08 -0700325 }
326
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200327 mpath_dbg(gate_mpath->sdata, "Mpath queue for gate %pM has %d frames\n",
328 gate_mpath->dst, skb_queue_len(&gate_mpath->frame_queue));
Javier Cardona5ee68e52011-08-09 16:45:08 -0700329
330 if (!copy)
331 return;
332
333 spin_lock_irqsave(&from_mpath->frame_queue.lock, flags);
334 skb_queue_splice(&failq, &from_mpath->frame_queue);
335 spin_unlock_irqrestore(&from_mpath->frame_queue.lock, flags);
336}
337
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100338
Johannes Berg4a3cb702013-02-12 16:43:19 +0100339static struct mesh_path *mpath_lookup(struct mesh_table *tbl, const u8 *dst,
340 struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100341{
342 struct mesh_path *mpath;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100343 struct hlist_head *bucket;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100344 struct mpath_node *node;
345
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200346 bucket = &tbl->hash_buckets[mesh_table_hash(dst, sdata, tbl)];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800347 hlist_for_each_entry_rcu(node, bucket, list) {
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100348 mpath = node->mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200349 if (mpath->sdata == sdata &&
Joe Perchesb203ca32012-05-08 18:56:52 +0000350 ether_addr_equal(dst, mpath->dst)) {
Johannes Bergbf7cd942013-02-15 14:40:31 +0100351 if (mpath_expired(mpath)) {
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100352 spin_lock_bh(&mpath->state_lock);
Javier Cardonaad99d142011-08-29 13:23:06 -0700353 mpath->flags &= ~MESH_PATH_ACTIVE;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100354 spin_unlock_bh(&mpath->state_lock);
355 }
356 return mpath;
357 }
358 }
359 return NULL;
360}
361
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100362/**
363 * mesh_path_lookup - look up a path in the mesh path table
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100364 * @sdata: local subif
Johannes Bergbf7cd942013-02-15 14:40:31 +0100365 * @dst: hardware address (ETH_ALEN length) of destination
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100366 *
367 * Returns: pointer to the mesh path structure, or NULL if not found
368 *
369 * Locking: must be called within a read rcu section.
370 */
Johannes Bergbf7cd942013-02-15 14:40:31 +0100371struct mesh_path *
372mesh_path_lookup(struct ieee80211_sub_if_data *sdata, const u8 *dst)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100373{
Luis R. Rodriguez5ad20dd2012-02-07 21:09:25 -0800374 return mpath_lookup(rcu_dereference(mesh_paths), dst, sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100375}
376
Johannes Bergbf7cd942013-02-15 14:40:31 +0100377struct mesh_path *
378mpp_path_lookup(struct ieee80211_sub_if_data *sdata, const u8 *dst)
YanBo79617de2008-09-22 13:30:32 +0800379{
Luis R. Rodriguez5ad20dd2012-02-07 21:09:25 -0800380 return mpath_lookup(rcu_dereference(mpp_paths), dst, sdata);
YanBo79617de2008-09-22 13:30:32 +0800381}
382
383
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100384/**
385 * mesh_path_lookup_by_idx - look up a path in the mesh path table by its index
386 * @idx: index
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200387 * @sdata: local subif, or NULL for all entries
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100388 *
389 * Returns: pointer to the mesh path structure, or NULL if not found.
390 *
391 * Locking: must be called within a read rcu section.
392 */
Johannes Bergbf7cd942013-02-15 14:40:31 +0100393struct mesh_path *
394mesh_path_lookup_by_idx(struct ieee80211_sub_if_data *sdata, int idx)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100395{
Johannes Berg349eb8c2011-05-14 11:56:16 +0200396 struct mesh_table *tbl = rcu_dereference(mesh_paths);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100397 struct mpath_node *node;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100398 int i;
399 int j = 0;
400
Sasha Levinb67bfe02013-02-27 17:06:00 -0800401 for_each_mesh_entry(tbl, node, i) {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200402 if (sdata && node->mpath->sdata != sdata)
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800403 continue;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100404 if (j++ == idx) {
Johannes Bergbf7cd942013-02-15 14:40:31 +0100405 if (mpath_expired(node->mpath)) {
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100406 spin_lock_bh(&node->mpath->state_lock);
Javier Cardonaad99d142011-08-29 13:23:06 -0700407 node->mpath->flags &= ~MESH_PATH_ACTIVE;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100408 spin_unlock_bh(&node->mpath->state_lock);
409 }
410 return node->mpath;
411 }
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800412 }
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100413
414 return NULL;
415}
416
Javier Cardona5ee68e52011-08-09 16:45:08 -0700417/**
Henning Roggea2db2ed2014-09-12 08:58:50 +0200418 * mpp_path_lookup_by_idx - look up a path in the proxy path table by its index
419 * @idx: index
420 * @sdata: local subif, or NULL for all entries
421 *
422 * Returns: pointer to the proxy path structure, or NULL if not found.
423 *
424 * Locking: must be called within a read rcu section.
425 */
426struct mesh_path *
427mpp_path_lookup_by_idx(struct ieee80211_sub_if_data *sdata, int idx)
428{
429 struct mesh_table *tbl = rcu_dereference(mpp_paths);
430 struct mpath_node *node;
431 int i;
432 int j = 0;
433
434 for_each_mesh_entry(tbl, node, i) {
435 if (sdata && node->mpath->sdata != sdata)
436 continue;
437 if (j++ == idx)
438 return node->mpath;
439 }
440
441 return NULL;
442}
443
444/**
Johannes Berg30be52e2011-11-21 11:23:50 +0100445 * mesh_path_add_gate - add the given mpath to a mesh gate to our path table
446 * @mpath: gate path to add to table
Javier Cardona5ee68e52011-08-09 16:45:08 -0700447 */
Johannes Berg30be52e2011-11-21 11:23:50 +0100448int mesh_path_add_gate(struct mesh_path *mpath)
Javier Cardona5ee68e52011-08-09 16:45:08 -0700449{
Johannes Berg30be52e2011-11-21 11:23:50 +0100450 struct mesh_table *tbl;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700451 struct mpath_node *gate, *new_gate;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700452 int err;
453
454 rcu_read_lock();
Johannes Berg30be52e2011-11-21 11:23:50 +0100455 tbl = rcu_dereference(mesh_paths);
Javier Cardona5ee68e52011-08-09 16:45:08 -0700456
Sasha Levinb67bfe02013-02-27 17:06:00 -0800457 hlist_for_each_entry_rcu(gate, tbl->known_gates, list)
Javier Cardona5ee68e52011-08-09 16:45:08 -0700458 if (gate->mpath == mpath) {
459 err = -EEXIST;
460 goto err_rcu;
461 }
462
463 new_gate = kzalloc(sizeof(struct mpath_node), GFP_ATOMIC);
464 if (!new_gate) {
465 err = -ENOMEM;
466 goto err_rcu;
467 }
468
469 mpath->is_gate = true;
470 mpath->sdata->u.mesh.num_gates++;
471 new_gate->mpath = mpath;
472 spin_lock_bh(&tbl->gates_lock);
473 hlist_add_head_rcu(&new_gate->list, tbl->known_gates);
474 spin_unlock_bh(&tbl->gates_lock);
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200475 mpath_dbg(mpath->sdata,
476 "Mesh path: Recorded new gate: %pM. %d known gates\n",
477 mpath->dst, mpath->sdata->u.mesh.num_gates);
Johannes Bergbf7cd942013-02-15 14:40:31 +0100478 err = 0;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700479err_rcu:
480 rcu_read_unlock();
481 return err;
482}
483
484/**
485 * mesh_gate_del - remove a mesh gate from the list of known gates
486 * @tbl: table which holds our list of known gates
487 * @mpath: gate mpath
488 *
Javier Cardona5ee68e52011-08-09 16:45:08 -0700489 * Locking: must be called inside rcu_read_lock() section
490 */
Johannes Bergbf7cd942013-02-15 14:40:31 +0100491static void mesh_gate_del(struct mesh_table *tbl, struct mesh_path *mpath)
Javier Cardona5ee68e52011-08-09 16:45:08 -0700492{
493 struct mpath_node *gate;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800494 struct hlist_node *q;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700495
Sasha Levinb67bfe02013-02-27 17:06:00 -0800496 hlist_for_each_entry_safe(gate, q, tbl->known_gates, list) {
Johannes Bergbf7cd942013-02-15 14:40:31 +0100497 if (gate->mpath != mpath)
498 continue;
499 spin_lock_bh(&tbl->gates_lock);
500 hlist_del_rcu(&gate->list);
501 kfree_rcu(gate, rcu);
502 spin_unlock_bh(&tbl->gates_lock);
503 mpath->sdata->u.mesh.num_gates--;
504 mpath->is_gate = false;
505 mpath_dbg(mpath->sdata,
506 "Mesh path: Deleted gate: %pM. %d known gates\n",
507 mpath->dst, mpath->sdata->u.mesh.num_gates);
508 break;
509 }
Javier Cardona5ee68e52011-08-09 16:45:08 -0700510}
511
512/**
Javier Cardona5ee68e52011-08-09 16:45:08 -0700513 * mesh_gate_num - number of gates known to this interface
514 * @sdata: subif data
515 */
516int mesh_gate_num(struct ieee80211_sub_if_data *sdata)
517{
518 return sdata->u.mesh.num_gates;
519}
520
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100521/**
522 * mesh_path_add - allocate and add a new path to the mesh path table
Johannes Bergbf7cd942013-02-15 14:40:31 +0100523 * @dst: destination address of the path (ETH_ALEN length)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200524 * @sdata: local subif
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100525 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200526 * Returns: 0 on success
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100527 *
528 * State: the initial state of the new path is set to 0
529 */
Bob Copelandae76eef2013-03-29 09:38:39 -0400530struct mesh_path *mesh_path_add(struct ieee80211_sub_if_data *sdata,
531 const u8 *dst)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100532{
Javier Cardona18889232009-08-10 12:15:52 -0700533 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
534 struct ieee80211_local *local = sdata->local;
Johannes Berg349eb8c2011-05-14 11:56:16 +0200535 struct mesh_table *tbl;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100536 struct mesh_path *mpath, *new_mpath;
537 struct mpath_node *node, *new_node;
538 struct hlist_head *bucket;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100539 int grow = 0;
Bob Copelandae76eef2013-03-29 09:38:39 -0400540 int err;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100541 u32 hash_idx;
542
Joe Perchesb203ca32012-05-08 18:56:52 +0000543 if (ether_addr_equal(dst, sdata->vif.addr))
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100544 /* never add ourselves as neighbours */
Bob Copelandae76eef2013-03-29 09:38:39 -0400545 return ERR_PTR(-ENOTSUPP);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100546
547 if (is_multicast_ether_addr(dst))
Bob Copelandae76eef2013-03-29 09:38:39 -0400548 return ERR_PTR(-ENOTSUPP);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100549
Johannes Berg472dbc42008-09-11 00:01:49 +0200550 if (atomic_add_unless(&sdata->u.mesh.mpaths, 1, MESH_MAX_MPATHS) == 0)
Bob Copelandae76eef2013-03-29 09:38:39 -0400551 return ERR_PTR(-ENOSPC);
552
553 read_lock_bh(&pathtbl_resize_lock);
554 tbl = resize_dereference_mesh_paths();
555
556 hash_idx = mesh_table_hash(dst, sdata, tbl);
557 bucket = &tbl->hash_buckets[hash_idx];
558
559 spin_lock(&tbl->hashwlock[hash_idx]);
560
561 hlist_for_each_entry(node, bucket, list) {
562 mpath = node->mpath;
563 if (mpath->sdata == sdata &&
564 ether_addr_equal(dst, mpath->dst))
565 goto found;
566 }
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100567
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400568 err = -ENOMEM;
Javier Cardona18889232009-08-10 12:15:52 -0700569 new_mpath = kzalloc(sizeof(struct mesh_path), GFP_ATOMIC);
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400570 if (!new_mpath)
571 goto err_path_alloc;
572
Javier Cardona18889232009-08-10 12:15:52 -0700573 new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400574 if (!new_node)
575 goto err_node_alloc;
Pavel Emelyanovf84e71a2008-05-06 18:46:36 +0400576
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100577 memcpy(new_mpath->dst, dst, ETH_ALEN);
Johannes Berge83e6542012-07-13 16:23:07 +0200578 eth_broadcast_addr(new_mpath->rann_snd_addr);
Chun-Yeow Yeoh35bcd592012-04-10 12:31:56 +0800579 new_mpath->is_root = false;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200580 new_mpath->sdata = sdata;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100581 new_mpath->flags = 0;
582 skb_queue_head_init(&new_mpath->frame_queue);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100583 new_node->mpath = new_mpath;
584 new_mpath->timer.data = (unsigned long) new_mpath;
585 new_mpath->timer.function = mesh_path_timer;
586 new_mpath->exp_time = jiffies;
587 spin_lock_init(&new_mpath->state_lock);
588 init_timer(&new_mpath->timer);
589
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100590 hlist_add_head_rcu(&new_node->list, bucket);
Johannes Berg349eb8c2011-05-14 11:56:16 +0200591 if (atomic_inc_return(&tbl->entries) >=
Bob Copeland3f73fe92016-01-30 13:17:03 -0500592 MEAN_CHAIN_LEN * (tbl->hash_mask + 1))
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100593 grow = 1;
594
Johannes Bergf5ea9122009-08-07 16:17:38 +0200595 mesh_paths_generation++;
596
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400597 if (grow) {
Javier Cardona18889232009-08-10 12:15:52 -0700598 set_bit(MESH_WORK_GROW_MPATH_TABLE, &ifmsh->wrkq_flags);
Johannes Berg64592c82010-06-10 10:21:31 +0200599 ieee80211_queue_work(&local->hw, &sdata->work);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100600 }
Bob Copelandae76eef2013-03-29 09:38:39 -0400601 mpath = new_mpath;
602found:
Thomas Pedersenf06c7882012-03-06 16:42:09 -0800603 spin_unlock(&tbl->hashwlock[hash_idx]);
Javier Cardona9b84b8082011-05-03 16:57:16 -0700604 read_unlock_bh(&pathtbl_resize_lock);
Bob Copelandae76eef2013-03-29 09:38:39 -0400605 return mpath;
606
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400607err_node_alloc:
608 kfree(new_mpath);
609err_path_alloc:
Johannes Berg472dbc42008-09-11 00:01:49 +0200610 atomic_dec(&sdata->u.mesh.mpaths);
Bob Copelandae76eef2013-03-29 09:38:39 -0400611 spin_unlock(&tbl->hashwlock[hash_idx]);
612 read_unlock_bh(&pathtbl_resize_lock);
613 return ERR_PTR(err);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100614}
615
Johannes Berg1928eca2011-05-14 11:00:52 +0200616static void mesh_table_free_rcu(struct rcu_head *rcu)
617{
618 struct mesh_table *tbl = container_of(rcu, struct mesh_table, rcu_head);
619
620 mesh_table_free(tbl, false);
621}
622
Javier Cardona18889232009-08-10 12:15:52 -0700623void mesh_mpath_table_grow(void)
624{
625 struct mesh_table *oldtbl, *newtbl;
626
Javier Cardona9b84b8082011-05-03 16:57:16 -0700627 write_lock_bh(&pathtbl_resize_lock);
Johannes Berg349eb8c2011-05-14 11:56:16 +0200628 oldtbl = resize_dereference_mesh_paths();
629 newtbl = mesh_table_alloc(oldtbl->size_order + 1);
Johannes Berg1928eca2011-05-14 11:00:52 +0200630 if (!newtbl)
631 goto out;
Johannes Berg349eb8c2011-05-14 11:56:16 +0200632 if (mesh_table_grow(oldtbl, newtbl) < 0) {
cozybit Inca3e6b122011-04-13 11:10:28 -0700633 __mesh_table_free(newtbl);
Johannes Berg1928eca2011-05-14 11:00:52 +0200634 goto out;
Javier Cardona18889232009-08-10 12:15:52 -0700635 }
636 rcu_assign_pointer(mesh_paths, newtbl);
Javier Cardona18889232009-08-10 12:15:52 -0700637
Johannes Berg1928eca2011-05-14 11:00:52 +0200638 call_rcu(&oldtbl->rcu_head, mesh_table_free_rcu);
639
640 out:
641 write_unlock_bh(&pathtbl_resize_lock);
Javier Cardona18889232009-08-10 12:15:52 -0700642}
643
644void mesh_mpp_table_grow(void)
645{
646 struct mesh_table *oldtbl, *newtbl;
647
Javier Cardona9b84b8082011-05-03 16:57:16 -0700648 write_lock_bh(&pathtbl_resize_lock);
Johannes Berg349eb8c2011-05-14 11:56:16 +0200649 oldtbl = resize_dereference_mpp_paths();
650 newtbl = mesh_table_alloc(oldtbl->size_order + 1);
Johannes Berg1928eca2011-05-14 11:00:52 +0200651 if (!newtbl)
652 goto out;
Johannes Berg349eb8c2011-05-14 11:56:16 +0200653 if (mesh_table_grow(oldtbl, newtbl) < 0) {
cozybit Inca3e6b122011-04-13 11:10:28 -0700654 __mesh_table_free(newtbl);
Johannes Berg1928eca2011-05-14 11:00:52 +0200655 goto out;
Javier Cardona18889232009-08-10 12:15:52 -0700656 }
657 rcu_assign_pointer(mpp_paths, newtbl);
Johannes Berg1928eca2011-05-14 11:00:52 +0200658 call_rcu(&oldtbl->rcu_head, mesh_table_free_rcu);
Javier Cardona18889232009-08-10 12:15:52 -0700659
Johannes Berg1928eca2011-05-14 11:00:52 +0200660 out:
661 write_unlock_bh(&pathtbl_resize_lock);
Javier Cardona18889232009-08-10 12:15:52 -0700662}
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100663
Johannes Bergbf7cd942013-02-15 14:40:31 +0100664int mpp_path_add(struct ieee80211_sub_if_data *sdata,
665 const u8 *dst, const u8 *mpp)
YanBo79617de2008-09-22 13:30:32 +0800666{
Javier Cardona18889232009-08-10 12:15:52 -0700667 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
668 struct ieee80211_local *local = sdata->local;
Johannes Berg349eb8c2011-05-14 11:56:16 +0200669 struct mesh_table *tbl;
YanBo79617de2008-09-22 13:30:32 +0800670 struct mesh_path *mpath, *new_mpath;
671 struct mpath_node *node, *new_node;
672 struct hlist_head *bucket;
YanBo79617de2008-09-22 13:30:32 +0800673 int grow = 0;
674 int err = 0;
675 u32 hash_idx;
676
Joe Perchesb203ca32012-05-08 18:56:52 +0000677 if (ether_addr_equal(dst, sdata->vif.addr))
YanBo79617de2008-09-22 13:30:32 +0800678 /* never add ourselves as neighbours */
679 return -ENOTSUPP;
680
681 if (is_multicast_ether_addr(dst))
682 return -ENOTSUPP;
683
684 err = -ENOMEM;
Javier Cardona18889232009-08-10 12:15:52 -0700685 new_mpath = kzalloc(sizeof(struct mesh_path), GFP_ATOMIC);
YanBo79617de2008-09-22 13:30:32 +0800686 if (!new_mpath)
687 goto err_path_alloc;
688
Javier Cardona18889232009-08-10 12:15:52 -0700689 new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
YanBo79617de2008-09-22 13:30:32 +0800690 if (!new_node)
691 goto err_node_alloc;
692
Javier Cardona9b84b8082011-05-03 16:57:16 -0700693 read_lock_bh(&pathtbl_resize_lock);
YanBo79617de2008-09-22 13:30:32 +0800694 memcpy(new_mpath->dst, dst, ETH_ALEN);
695 memcpy(new_mpath->mpp, mpp, ETH_ALEN);
696 new_mpath->sdata = sdata;
697 new_mpath->flags = 0;
698 skb_queue_head_init(&new_mpath->frame_queue);
699 new_node->mpath = new_mpath;
Thomas Pedersenc61336612011-08-25 10:36:14 -0700700 init_timer(&new_mpath->timer);
YanBo79617de2008-09-22 13:30:32 +0800701 new_mpath->exp_time = jiffies;
702 spin_lock_init(&new_mpath->state_lock);
703
Johannes Berg349eb8c2011-05-14 11:56:16 +0200704 tbl = resize_dereference_mpp_paths();
YanBo79617de2008-09-22 13:30:32 +0800705
Johannes Berg349eb8c2011-05-14 11:56:16 +0200706 hash_idx = mesh_table_hash(dst, sdata, tbl);
707 bucket = &tbl->hash_buckets[hash_idx];
708
Thomas Pedersenf06c7882012-03-06 16:42:09 -0800709 spin_lock(&tbl->hashwlock[hash_idx]);
YanBo79617de2008-09-22 13:30:32 +0800710
711 err = -EEXIST;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800712 hlist_for_each_entry(node, bucket, list) {
YanBo79617de2008-09-22 13:30:32 +0800713 mpath = node->mpath;
Felix Fietkau888d04d2012-03-01 15:22:09 +0100714 if (mpath->sdata == sdata &&
Joe Perchesb203ca32012-05-08 18:56:52 +0000715 ether_addr_equal(dst, mpath->dst))
YanBo79617de2008-09-22 13:30:32 +0800716 goto err_exists;
717 }
718
719 hlist_add_head_rcu(&new_node->list, bucket);
Johannes Berg349eb8c2011-05-14 11:56:16 +0200720 if (atomic_inc_return(&tbl->entries) >=
Bob Copeland3f73fe92016-01-30 13:17:03 -0500721 MEAN_CHAIN_LEN * (tbl->hash_mask + 1))
YanBo79617de2008-09-22 13:30:32 +0800722 grow = 1;
723
Thomas Pedersenf06c7882012-03-06 16:42:09 -0800724 spin_unlock(&tbl->hashwlock[hash_idx]);
Javier Cardona9b84b8082011-05-03 16:57:16 -0700725 read_unlock_bh(&pathtbl_resize_lock);
Henning Roggea2db2ed2014-09-12 08:58:50 +0200726
727 mpp_paths_generation++;
728
YanBo79617de2008-09-22 13:30:32 +0800729 if (grow) {
Javier Cardona18889232009-08-10 12:15:52 -0700730 set_bit(MESH_WORK_GROW_MPP_TABLE, &ifmsh->wrkq_flags);
Johannes Berg64592c82010-06-10 10:21:31 +0200731 ieee80211_queue_work(&local->hw, &sdata->work);
YanBo79617de2008-09-22 13:30:32 +0800732 }
733 return 0;
734
735err_exists:
Thomas Pedersenf06c7882012-03-06 16:42:09 -0800736 spin_unlock(&tbl->hashwlock[hash_idx]);
Javier Cardona9b84b8082011-05-03 16:57:16 -0700737 read_unlock_bh(&pathtbl_resize_lock);
YanBo79617de2008-09-22 13:30:32 +0800738 kfree(new_node);
739err_node_alloc:
740 kfree(new_mpath);
741err_path_alloc:
742 return err;
743}
744
745
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100746/**
747 * mesh_plink_broken - deactivates paths and sends perr when a link breaks
748 *
749 * @sta: broken peer link
750 *
751 * This function must be called from the rate control algorithm if enough
752 * delivery errors suggest that a peer link is no longer usable.
753 */
754void mesh_plink_broken(struct sta_info *sta)
755{
Johannes Berg349eb8c2011-05-14 11:56:16 +0200756 struct mesh_table *tbl;
Johannes Berg15ff6362009-11-17 13:34:04 +0100757 static const u8 bcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100758 struct mesh_path *mpath;
759 struct mpath_node *node;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200760 struct ieee80211_sub_if_data *sdata = sta->sdata;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100761 int i;
762
763 rcu_read_lock();
Johannes Berg349eb8c2011-05-14 11:56:16 +0200764 tbl = rcu_dereference(mesh_paths);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800765 for_each_mesh_entry(tbl, node, i) {
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100766 mpath = node->mpath;
Andreea-Cristina Bernat2688eba2014-08-17 16:18:02 +0300767 if (rcu_access_pointer(mpath->next_hop) == sta &&
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100768 mpath->flags & MESH_PATH_ACTIVE &&
769 !(mpath->flags & MESH_PATH_FIXED)) {
Javier Cardonaf5e50cd2011-08-29 13:23:05 -0700770 spin_lock_bh(&mpath->state_lock);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100771 mpath->flags &= ~MESH_PATH_ACTIVE;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000772 ++mpath->sn;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100773 spin_unlock_bh(&mpath->state_lock);
Johannes Bergbf7cd942013-02-15 14:40:31 +0100774 mesh_path_error_tx(sdata,
Chun-Yeow Yeohf63f8422013-11-13 15:39:12 +0800775 sdata->u.mesh.mshcfg.element_ttl,
776 mpath->dst, mpath->sn,
777 WLAN_REASON_MESH_PATH_DEST_UNREACHABLE, bcast);
Javier Cardonaf5e50cd2011-08-29 13:23:05 -0700778 }
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100779 }
780 rcu_read_unlock();
781}
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100782
Javier Cardona19c50b32011-08-29 13:23:07 -0700783static void mesh_path_node_reclaim(struct rcu_head *rp)
784{
785 struct mpath_node *node = container_of(rp, struct mpath_node, rcu);
Javier Cardona19c50b32011-08-29 13:23:07 -0700786
787 del_timer_sync(&node->mpath->timer);
Javier Cardona19c50b32011-08-29 13:23:07 -0700788 kfree(node->mpath);
789 kfree(node);
790}
791
792/* needs to be called with the corresponding hashwlock taken */
793static void __mesh_path_del(struct mesh_table *tbl, struct mpath_node *node)
794{
Johannes Bergc2e703a2015-11-17 14:25:21 +0100795 struct mesh_path *mpath = node->mpath;
796 struct ieee80211_sub_if_data *sdata = node->mpath->sdata;
797
Javier Cardona19c50b32011-08-29 13:23:07 -0700798 spin_lock(&mpath->state_lock);
799 mpath->flags |= MESH_PATH_RESOLVING;
800 if (mpath->is_gate)
801 mesh_gate_del(tbl, mpath);
802 hlist_del_rcu(&node->list);
803 call_rcu(&node->rcu, mesh_path_node_reclaim);
804 spin_unlock(&mpath->state_lock);
Johannes Bergc2e703a2015-11-17 14:25:21 +0100805 atomic_dec(&sdata->u.mesh.mpaths);
Javier Cardona19c50b32011-08-29 13:23:07 -0700806 atomic_dec(&tbl->entries);
807}
808
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100809/**
810 * mesh_path_flush_by_nexthop - Deletes mesh paths if their next hop matches
811 *
Ben Hutchings2c530402012-07-10 10:55:09 +0000812 * @sta: mesh peer to match
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100813 *
Luis Carlos Cobob4e08ea2008-02-29 15:46:08 -0800814 * RCU notes: this function is called when a mesh plink transitions from
815 * PLINK_ESTAB to any other state, since PLINK_ESTAB state is the only one that
816 * allows path creation. This will happen before the sta can be freed (because
Johannes Bergd0709a62008-02-25 16:27:46 +0100817 * sta_info_destroy() calls this) so any reader in a rcu read block will be
818 * protected against the plink disappearing.
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100819 */
820void mesh_path_flush_by_nexthop(struct sta_info *sta)
821{
Johannes Berg349eb8c2011-05-14 11:56:16 +0200822 struct mesh_table *tbl;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100823 struct mesh_path *mpath;
824 struct mpath_node *node;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100825 int i;
826
Johannes Berg349eb8c2011-05-14 11:56:16 +0200827 rcu_read_lock();
Javier Cardona239289e2011-08-29 13:23:09 -0700828 read_lock_bh(&pathtbl_resize_lock);
829 tbl = resize_dereference_mesh_paths();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800830 for_each_mesh_entry(tbl, node, i) {
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100831 mpath = node->mpath;
Andreea-Cristina Bernat2688eba2014-08-17 16:18:02 +0300832 if (rcu_access_pointer(mpath->next_hop) == sta) {
Thomas Pedersenf06c7882012-03-06 16:42:09 -0800833 spin_lock(&tbl->hashwlock[i]);
Javier Cardona19c50b32011-08-29 13:23:07 -0700834 __mesh_path_del(tbl, node);
Thomas Pedersenf06c7882012-03-06 16:42:09 -0800835 spin_unlock(&tbl->hashwlock[i]);
Javier Cardona19c50b32011-08-29 13:23:07 -0700836 }
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100837 }
Javier Cardona239289e2011-08-29 13:23:09 -0700838 read_unlock_bh(&pathtbl_resize_lock);
Johannes Berg349eb8c2011-05-14 11:56:16 +0200839 rcu_read_unlock();
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100840}
841
Henning Roggebf5a70e2016-02-03 13:58:36 +0100842static void mpp_flush_by_proxy(struct ieee80211_sub_if_data *sdata,
843 const u8 *proxy)
844{
845 struct mesh_table *tbl;
846 struct mesh_path *mpp;
847 struct mpath_node *node;
848 int i;
849
850 rcu_read_lock();
851 read_lock_bh(&pathtbl_resize_lock);
852 tbl = resize_dereference_mpp_paths();
853 for_each_mesh_entry(tbl, node, i) {
854 mpp = node->mpath;
855 if (ether_addr_equal(mpp->mpp, proxy)) {
856 spin_lock(&tbl->hashwlock[i]);
857 __mesh_path_del(tbl, node);
858 spin_unlock(&tbl->hashwlock[i]);
859 }
860 }
861 read_unlock_bh(&pathtbl_resize_lock);
862 rcu_read_unlock();
863}
864
Javier Cardonacd72e812011-08-29 13:23:08 -0700865static void table_flush_by_iface(struct mesh_table *tbl,
866 struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100867{
868 struct mesh_path *mpath;
869 struct mpath_node *node;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100870 int i;
871
Javier Cardonacd72e812011-08-29 13:23:08 -0700872 WARN_ON(!rcu_read_lock_held());
Sasha Levinb67bfe02013-02-27 17:06:00 -0800873 for_each_mesh_entry(tbl, node, i) {
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100874 mpath = node->mpath;
Javier Cardonacd72e812011-08-29 13:23:08 -0700875 if (mpath->sdata != sdata)
876 continue;
Javier Cardonaece1a2e2011-08-29 13:23:04 -0700877 spin_lock_bh(&tbl->hashwlock[i]);
Javier Cardona19c50b32011-08-29 13:23:07 -0700878 __mesh_path_del(tbl, node);
Javier Cardonaece1a2e2011-08-29 13:23:04 -0700879 spin_unlock_bh(&tbl->hashwlock[i]);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100880 }
881}
882
Javier Cardonaece1a2e2011-08-29 13:23:04 -0700883/**
884 * mesh_path_flush_by_iface - Deletes all mesh paths associated with a given iface
885 *
886 * This function deletes both mesh paths as well as mesh portal paths.
887 *
Ben Hutchings2c530402012-07-10 10:55:09 +0000888 * @sdata: interface data to match
Javier Cardonaece1a2e2011-08-29 13:23:04 -0700889 *
890 */
891void mesh_path_flush_by_iface(struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100892{
Javier Cardonacd72e812011-08-29 13:23:08 -0700893 struct mesh_table *tbl;
Johannes Bergd0709a62008-02-25 16:27:46 +0100894
Javier Cardonacd72e812011-08-29 13:23:08 -0700895 rcu_read_lock();
Javier Cardona239289e2011-08-29 13:23:09 -0700896 read_lock_bh(&pathtbl_resize_lock);
897 tbl = resize_dereference_mesh_paths();
Javier Cardonacd72e812011-08-29 13:23:08 -0700898 table_flush_by_iface(tbl, sdata);
Javier Cardona239289e2011-08-29 13:23:09 -0700899 tbl = resize_dereference_mpp_paths();
Javier Cardonacd72e812011-08-29 13:23:08 -0700900 table_flush_by_iface(tbl, sdata);
Javier Cardona239289e2011-08-29 13:23:09 -0700901 read_unlock_bh(&pathtbl_resize_lock);
Javier Cardonacd72e812011-08-29 13:23:08 -0700902 rcu_read_unlock();
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100903}
904
905/**
Henning Rogge4cc955d2016-02-03 13:58:38 +0100906 * table_path_del - delete a path from the mesh or mpp table
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100907 *
Henning Rogge4cc955d2016-02-03 13:58:38 +0100908 * @tbl: mesh or mpp path table
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200909 * @sdata: local subif
Henning Rogge4cc955d2016-02-03 13:58:38 +0100910 * @addr: dst address (ETH_ALEN length)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100911 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200912 * Returns: 0 if successful
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100913 */
Henning Rogge4cc955d2016-02-03 13:58:38 +0100914static int table_path_del(struct mesh_table __rcu *rcu_tbl,
915 struct ieee80211_sub_if_data *sdata,
916 const u8 *addr)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100917{
Johannes Berg349eb8c2011-05-14 11:56:16 +0200918 struct mesh_table *tbl;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100919 struct mesh_path *mpath;
920 struct mpath_node *node;
921 struct hlist_head *bucket;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100922 int hash_idx;
923 int err = 0;
924
Henning Rogge4cc955d2016-02-03 13:58:38 +0100925 tbl = resize_dereference_paths(rcu_tbl);
Johannes Berg349eb8c2011-05-14 11:56:16 +0200926 hash_idx = mesh_table_hash(addr, sdata, tbl);
927 bucket = &tbl->hash_buckets[hash_idx];
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100928
Thomas Pedersenf06c7882012-03-06 16:42:09 -0800929 spin_lock(&tbl->hashwlock[hash_idx]);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800930 hlist_for_each_entry(node, bucket, list) {
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100931 mpath = node->mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200932 if (mpath->sdata == sdata &&
Joe Perchesb203ca32012-05-08 18:56:52 +0000933 ether_addr_equal(addr, mpath->dst)) {
Javier Cardona19c50b32011-08-29 13:23:07 -0700934 __mesh_path_del(tbl, node);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100935 goto enddel;
936 }
937 }
938
939 err = -ENXIO;
940enddel:
Thomas Pedersenf06c7882012-03-06 16:42:09 -0800941 spin_unlock(&tbl->hashwlock[hash_idx]);
Henning Rogge4cc955d2016-02-03 13:58:38 +0100942 return err;
943}
944
945/**
946 * mesh_path_del - delete a mesh path from the table
947 *
948 * @addr: dst address (ETH_ALEN length)
949 * @sdata: local subif
950 *
951 * Returns: 0 if successful
952 */
953int mesh_path_del(struct ieee80211_sub_if_data *sdata, const u8 *addr)
954{
955 int err = 0;
956
957 /* flush relevant mpp entries first */
958 mpp_flush_by_proxy(sdata, addr);
959
960 read_lock_bh(&pathtbl_resize_lock);
961 err = table_path_del(mesh_paths, sdata, addr);
962 mesh_paths_generation++;
Javier Cardona9b84b8082011-05-03 16:57:16 -0700963 read_unlock_bh(&pathtbl_resize_lock);
Henning Rogge4cc955d2016-02-03 13:58:38 +0100964
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100965 return err;
966}
967
968/**
Henning Roggeab1c7902016-02-03 13:58:37 +0100969 * mpp_path_del - delete a mesh proxy path from the table
970 *
971 * @addr: addr address (ETH_ALEN length)
972 * @sdata: local subif
973 *
974 * Returns: 0 if successful
975 */
976static int mpp_path_del(struct ieee80211_sub_if_data *sdata, const u8 *addr)
977{
Henning Roggeab1c7902016-02-03 13:58:37 +0100978 int err = 0;
979
980 read_lock_bh(&pathtbl_resize_lock);
Henning Rogge4cc955d2016-02-03 13:58:38 +0100981 err = table_path_del(mpp_paths, sdata, addr);
982 mpp_paths_generation++;
Henning Roggeab1c7902016-02-03 13:58:37 +0100983 read_unlock_bh(&pathtbl_resize_lock);
Henning Rogge4cc955d2016-02-03 13:58:38 +0100984
Henning Roggeab1c7902016-02-03 13:58:37 +0100985 return err;
986}
987
988/**
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100989 * mesh_path_tx_pending - sends pending frames in a mesh path queue
990 *
991 * @mpath: mesh path to activate
992 *
993 * Locking: the state_lock of the mpath structure must NOT be held when calling
994 * this function.
995 */
996void mesh_path_tx_pending(struct mesh_path *mpath)
997{
Javier Cardona249b4052009-07-07 10:55:03 -0700998 if (mpath->flags & MESH_PATH_ACTIVE)
999 ieee80211_add_pending_skbs(mpath->sdata->local,
1000 &mpath->frame_queue);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001001}
1002
1003/**
Javier Cardona5ee68e52011-08-09 16:45:08 -07001004 * mesh_path_send_to_gates - sends pending frames to all known mesh gates
1005 *
1006 * @mpath: mesh path whose queue will be emptied
1007 *
1008 * If there is only one gate, the frames are transferred from the failed mpath
1009 * queue to that gate's queue. If there are more than one gates, the frames
1010 * are copied from each gate to the next. After frames are copied, the
1011 * mpath queues are emptied onto the transmission queue.
1012 */
1013int mesh_path_send_to_gates(struct mesh_path *mpath)
1014{
1015 struct ieee80211_sub_if_data *sdata = mpath->sdata;
Javier Cardona5ee68e52011-08-09 16:45:08 -07001016 struct mesh_table *tbl;
1017 struct mesh_path *from_mpath = mpath;
1018 struct mpath_node *gate = NULL;
1019 bool copy = false;
1020 struct hlist_head *known_gates;
1021
1022 rcu_read_lock();
1023 tbl = rcu_dereference(mesh_paths);
1024 known_gates = tbl->known_gates;
1025 rcu_read_unlock();
1026
1027 if (!known_gates)
1028 return -EHOSTUNREACH;
1029
Sasha Levinb67bfe02013-02-27 17:06:00 -08001030 hlist_for_each_entry_rcu(gate, known_gates, list) {
Javier Cardona5ee68e52011-08-09 16:45:08 -07001031 if (gate->mpath->sdata != sdata)
1032 continue;
1033
1034 if (gate->mpath->flags & MESH_PATH_ACTIVE) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001035 mpath_dbg(sdata, "Forwarding to %pM\n", gate->mpath->dst);
Javier Cardona5ee68e52011-08-09 16:45:08 -07001036 mesh_path_move_to_queue(gate->mpath, from_mpath, copy);
1037 from_mpath = gate->mpath;
1038 copy = true;
1039 } else {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001040 mpath_dbg(sdata,
Johannes Bergd671b2a2015-11-06 11:30:46 +01001041 "Not forwarding to %pM (flags %#x)\n",
1042 gate->mpath->dst, gate->mpath->flags);
Javier Cardona5ee68e52011-08-09 16:45:08 -07001043 }
1044 }
1045
Sasha Levinb67bfe02013-02-27 17:06:00 -08001046 hlist_for_each_entry_rcu(gate, known_gates, list)
Javier Cardona5ee68e52011-08-09 16:45:08 -07001047 if (gate->mpath->sdata == sdata) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001048 mpath_dbg(sdata, "Sending to %pM\n", gate->mpath->dst);
Javier Cardona5ee68e52011-08-09 16:45:08 -07001049 mesh_path_tx_pending(gate->mpath);
1050 }
1051
1052 return (from_mpath == mpath) ? -EHOSTUNREACH : 0;
1053}
1054
1055/**
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001056 * mesh_path_discard_frame - discard a frame whose path could not be resolved
1057 *
1058 * @skb: frame to discard
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001059 * @sdata: network subif the frame was to be sent through
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001060 *
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001061 * Locking: the function must me called within a rcu_read_lock region
1062 */
Johannes Bergbf7cd942013-02-15 14:40:31 +01001063void mesh_path_discard_frame(struct ieee80211_sub_if_data *sdata,
1064 struct sk_buff *skb)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001065{
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001066 kfree_skb(skb);
Johannes Berg472dbc42008-09-11 00:01:49 +02001067 sdata->u.mesh.mshstats.dropped_frames_no_route++;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001068}
1069
1070/**
1071 * mesh_path_flush_pending - free the pending queue of a mesh path
1072 *
1073 * @mpath: mesh path whose queue has to be freed
1074 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001075 * Locking: the function must me called within a rcu_read_lock region
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001076 */
1077void mesh_path_flush_pending(struct mesh_path *mpath)
1078{
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001079 struct sk_buff *skb;
1080
Javier Cardona00e3f252011-08-09 16:45:07 -07001081 while ((skb = skb_dequeue(&mpath->frame_queue)) != NULL)
Johannes Bergbf7cd942013-02-15 14:40:31 +01001082 mesh_path_discard_frame(mpath->sdata, skb);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001083}
1084
1085/**
1086 * mesh_path_fix_nexthop - force a specific next hop for a mesh path
1087 *
1088 * @mpath: the mesh path to modify
1089 * @next_hop: the next hop to force
1090 *
1091 * Locking: this function must be called holding mpath->state_lock
1092 */
1093void mesh_path_fix_nexthop(struct mesh_path *mpath, struct sta_info *next_hop)
1094{
1095 spin_lock_bh(&mpath->state_lock);
1096 mesh_path_assign_nexthop(mpath, next_hop);
Rui Paulod19b3bf2009-11-09 23:46:55 +00001097 mpath->sn = 0xffff;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001098 mpath->metric = 0;
1099 mpath->hop_count = 0;
1100 mpath->exp_time = 0;
1101 mpath->flags |= MESH_PATH_FIXED;
1102 mesh_path_activate(mpath);
1103 spin_unlock_bh(&mpath->state_lock);
1104 mesh_path_tx_pending(mpath);
1105}
1106
1107static void mesh_path_node_free(struct hlist_node *p, bool free_leafs)
1108{
1109 struct mesh_path *mpath;
1110 struct mpath_node *node = hlist_entry(p, struct mpath_node, list);
1111 mpath = node->mpath;
1112 hlist_del_rcu(p);
Javier Cardonad0df9ee2011-05-13 14:16:07 -07001113 if (free_leafs) {
1114 del_timer_sync(&mpath->timer);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001115 kfree(mpath);
Javier Cardonad0df9ee2011-05-13 14:16:07 -07001116 }
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001117 kfree(node);
1118}
1119
Pavel Emelyanov4caf86c2008-05-07 19:47:01 +04001120static int mesh_path_node_copy(struct hlist_node *p, struct mesh_table *newtbl)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001121{
1122 struct mesh_path *mpath;
1123 struct mpath_node *node, *new_node;
1124 u32 hash_idx;
1125
Pavel Emelyanov8566dc32008-05-07 19:51:51 +04001126 new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
Pavel Emelyanov00242c42008-05-07 19:48:14 +04001127 if (new_node == NULL)
1128 return -ENOMEM;
1129
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001130 node = hlist_entry(p, struct mpath_node, list);
1131 mpath = node->mpath;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001132 new_node->mpath = mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001133 hash_idx = mesh_table_hash(mpath->dst, mpath->sdata, newtbl);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001134 hlist_add_head(&new_node->list,
1135 &newtbl->hash_buckets[hash_idx]);
Pavel Emelyanov4caf86c2008-05-07 19:47:01 +04001136 return 0;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001137}
1138
1139int mesh_pathtbl_init(void)
1140{
Johannes Berg349eb8c2011-05-14 11:56:16 +02001141 struct mesh_table *tbl_path, *tbl_mpp;
Dan Carpenter4c5ade42011-08-30 22:16:15 +03001142 int ret;
YanBo79617de2008-09-22 13:30:32 +08001143
Johannes Berg349eb8c2011-05-14 11:56:16 +02001144 tbl_path = mesh_table_alloc(INIT_PATHS_SIZE_ORDER);
1145 if (!tbl_path)
1146 return -ENOMEM;
1147 tbl_path->free_node = &mesh_path_node_free;
1148 tbl_path->copy_node = &mesh_path_node_copy;
Javier Cardona5ee68e52011-08-09 16:45:08 -07001149 tbl_path->known_gates = kzalloc(sizeof(struct hlist_head), GFP_ATOMIC);
Dan Carpenter4c5ade42011-08-30 22:16:15 +03001150 if (!tbl_path->known_gates) {
1151 ret = -ENOMEM;
1152 goto free_path;
1153 }
Javier Cardona5ee68e52011-08-09 16:45:08 -07001154 INIT_HLIST_HEAD(tbl_path->known_gates);
1155
Johannes Berg349eb8c2011-05-14 11:56:16 +02001156
1157 tbl_mpp = mesh_table_alloc(INIT_PATHS_SIZE_ORDER);
1158 if (!tbl_mpp) {
Dan Carpenter4c5ade42011-08-30 22:16:15 +03001159 ret = -ENOMEM;
1160 goto free_path;
YanBo79617de2008-09-22 13:30:32 +08001161 }
Johannes Berg349eb8c2011-05-14 11:56:16 +02001162 tbl_mpp->free_node = &mesh_path_node_free;
1163 tbl_mpp->copy_node = &mesh_path_node_copy;
Javier Cardona5ee68e52011-08-09 16:45:08 -07001164 tbl_mpp->known_gates = kzalloc(sizeof(struct hlist_head), GFP_ATOMIC);
Dan Carpenter4c5ade42011-08-30 22:16:15 +03001165 if (!tbl_mpp->known_gates) {
1166 ret = -ENOMEM;
1167 goto free_mpp;
1168 }
Javier Cardona5ee68e52011-08-09 16:45:08 -07001169 INIT_HLIST_HEAD(tbl_mpp->known_gates);
Johannes Berg349eb8c2011-05-14 11:56:16 +02001170
1171 /* Need no locking since this is during init */
1172 RCU_INIT_POINTER(mesh_paths, tbl_path);
1173 RCU_INIT_POINTER(mpp_paths, tbl_mpp);
YanBo79617de2008-09-22 13:30:32 +08001174
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001175 return 0;
Dan Carpenter4c5ade42011-08-30 22:16:15 +03001176
1177free_mpp:
1178 mesh_table_free(tbl_mpp, true);
1179free_path:
1180 mesh_table_free(tbl_path, true);
1181 return ret;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001182}
1183
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001184void mesh_path_expire(struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001185{
Johannes Berg349eb8c2011-05-14 11:56:16 +02001186 struct mesh_table *tbl;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001187 struct mesh_path *mpath;
1188 struct mpath_node *node;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001189 int i;
1190
Johannes Berg349eb8c2011-05-14 11:56:16 +02001191 rcu_read_lock();
1192 tbl = rcu_dereference(mesh_paths);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001193 for_each_mesh_entry(tbl, node, i) {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001194 if (node->mpath->sdata != sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001195 continue;
1196 mpath = node->mpath;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001197 if ((!(mpath->flags & MESH_PATH_RESOLVING)) &&
1198 (!(mpath->flags & MESH_PATH_FIXED)) &&
Javier Cardonaf5e50cd2011-08-29 13:23:05 -07001199 time_after(jiffies, mpath->exp_time + MESH_PATH_EXPIRE))
Johannes Bergbf7cd942013-02-15 14:40:31 +01001200 mesh_path_del(mpath->sdata, mpath->dst);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001201 }
Henning Roggeab1c7902016-02-03 13:58:37 +01001202
1203 tbl = rcu_dereference(mpp_paths);
1204 for_each_mesh_entry(tbl, node, i) {
1205 if (node->mpath->sdata != sdata)
1206 continue;
1207 mpath = node->mpath;
1208 if ((!(mpath->flags & MESH_PATH_FIXED)) &&
1209 time_after(jiffies, mpath->exp_time + MESH_PATH_EXPIRE))
1210 mpp_path_del(mpath->sdata, mpath->dst);
1211 }
1212
Johannes Berg349eb8c2011-05-14 11:56:16 +02001213 rcu_read_unlock();
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001214}
1215
1216void mesh_pathtbl_unregister(void)
1217{
Johannes Berg349eb8c2011-05-14 11:56:16 +02001218 /* no need for locking during exit path */
Eric Dumazet33d480c2011-08-11 19:30:52 +00001219 mesh_table_free(rcu_dereference_protected(mesh_paths, 1), true);
1220 mesh_table_free(rcu_dereference_protected(mpp_paths, 1), true);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001221}