blob: bcf7fee53b2c3196d640ebb2ffecfbc5b1c71a05 [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>
17#include "ieee80211_i.h"
18#include "mesh.h"
19
Javier Cardona76468872011-08-09 16:45:04 -070020#ifdef CONFIG_MAC80211_VERBOSE_MPATH_DEBUG
21#define mpath_dbg(fmt, args...) printk(KERN_DEBUG fmt, ##args)
22#else
23#define mpath_dbg(fmt, args...) do { (void)(0); } while (0)
24#endif
25
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +010026/* There will be initially 2^INIT_PATHS_SIZE_ORDER buckets */
27#define INIT_PATHS_SIZE_ORDER 2
28
29/* Keep the mean chain length below this constant */
30#define MEAN_CHAIN_LEN 2
31
32#define MPATH_EXPIRED(mpath) ((mpath->flags & MESH_PATH_ACTIVE) && \
33 time_after(jiffies, mpath->exp_time) && \
34 !(mpath->flags & MESH_PATH_FIXED))
35
36struct mpath_node {
37 struct hlist_node list;
38 struct rcu_head rcu;
39 /* This indirection allows two different tables to point to the same
40 * mesh_path structure, useful when resizing
41 */
42 struct mesh_path *mpath;
43};
44
Johannes Berg349eb8c2011-05-14 11:56:16 +020045static struct mesh_table __rcu *mesh_paths;
46static struct mesh_table __rcu *mpp_paths; /* Store paths for MPP&MAP */
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +010047
Johannes Bergf5ea9122009-08-07 16:17:38 +020048int mesh_paths_generation;
Johannes Berg6b86bd62011-05-12 13:38:50 +020049
50/* This lock will have the grow table function as writer and add / delete nodes
51 * as readers. When reading the table (i.e. doing lookups) we are well protected
52 * by RCU
53 */
54static DEFINE_RWLOCK(pathtbl_resize_lock);
55
56
Johannes Berg349eb8c2011-05-14 11:56:16 +020057static inline struct mesh_table *resize_dereference_mesh_paths(void)
58{
59 return rcu_dereference_protected(mesh_paths,
60 lockdep_is_held(&pathtbl_resize_lock));
61}
62
63static inline struct mesh_table *resize_dereference_mpp_paths(void)
64{
65 return rcu_dereference_protected(mpp_paths,
66 lockdep_is_held(&pathtbl_resize_lock));
67}
68
69/*
70 * CAREFUL -- "tbl" must not be an expression,
71 * in particular not an rcu_dereference(), since
72 * it's used twice. So it is illegal to do
73 * for_each_mesh_entry(rcu_dereference(...), ...)
74 */
75#define for_each_mesh_entry(tbl, p, node, i) \
76 for (i = 0; i <= tbl->hash_mask; i++) \
77 hlist_for_each_entry_rcu(node, p, &tbl->hash_buckets[i], list)
78
79
Johannes Berg6b86bd62011-05-12 13:38:50 +020080static struct mesh_table *mesh_table_alloc(int size_order)
81{
82 int i;
83 struct mesh_table *newtbl;
84
Javier Cardonad676ff42011-05-17 16:13:34 -070085 newtbl = kmalloc(sizeof(struct mesh_table), GFP_ATOMIC);
Johannes Berg6b86bd62011-05-12 13:38:50 +020086 if (!newtbl)
87 return NULL;
88
89 newtbl->hash_buckets = kzalloc(sizeof(struct hlist_head) *
Javier Cardonad676ff42011-05-17 16:13:34 -070090 (1 << size_order), GFP_ATOMIC);
Johannes Berg6b86bd62011-05-12 13:38:50 +020091
92 if (!newtbl->hash_buckets) {
93 kfree(newtbl);
94 return NULL;
95 }
96
97 newtbl->hashwlock = kmalloc(sizeof(spinlock_t) *
Javier Cardonad676ff42011-05-17 16:13:34 -070098 (1 << size_order), GFP_ATOMIC);
Johannes Berg6b86bd62011-05-12 13:38:50 +020099 if (!newtbl->hashwlock) {
100 kfree(newtbl->hash_buckets);
101 kfree(newtbl);
102 return NULL;
103 }
104
105 newtbl->size_order = size_order;
106 newtbl->hash_mask = (1 << size_order) - 1;
107 atomic_set(&newtbl->entries, 0);
108 get_random_bytes(&newtbl->hash_rnd,
109 sizeof(newtbl->hash_rnd));
110 for (i = 0; i <= newtbl->hash_mask; i++)
111 spin_lock_init(&newtbl->hashwlock[i]);
112
113 return newtbl;
114}
115
Javier Cardona18889232009-08-10 12:15:52 -0700116static void __mesh_table_free(struct mesh_table *tbl)
117{
118 kfree(tbl->hash_buckets);
119 kfree(tbl->hashwlock);
120 kfree(tbl);
121}
122
Johannes Berg6b86bd62011-05-12 13:38:50 +0200123static void mesh_table_free(struct mesh_table *tbl, bool free_leafs)
Javier Cardona18889232009-08-10 12:15:52 -0700124{
125 struct hlist_head *mesh_hash;
126 struct hlist_node *p, *q;
127 int i;
128
129 mesh_hash = tbl->hash_buckets;
130 for (i = 0; i <= tbl->hash_mask; i++) {
Javier Cardona9b84b8082011-05-03 16:57:16 -0700131 spin_lock_bh(&tbl->hashwlock[i]);
Javier Cardona18889232009-08-10 12:15:52 -0700132 hlist_for_each_safe(p, q, &mesh_hash[i]) {
133 tbl->free_node(p, free_leafs);
134 atomic_dec(&tbl->entries);
135 }
Javier Cardona9b84b8082011-05-03 16:57:16 -0700136 spin_unlock_bh(&tbl->hashwlock[i]);
Javier Cardona18889232009-08-10 12:15:52 -0700137 }
138 __mesh_table_free(tbl);
139}
140
cozybit Inca3e6b122011-04-13 11:10:28 -0700141static int mesh_table_grow(struct mesh_table *oldtbl,
Johannes Berg6b86bd62011-05-12 13:38:50 +0200142 struct mesh_table *newtbl)
Javier Cardona18889232009-08-10 12:15:52 -0700143{
Javier Cardona18889232009-08-10 12:15:52 -0700144 struct hlist_head *oldhash;
145 struct hlist_node *p, *q;
146 int i;
147
cozybit Inca3e6b122011-04-13 11:10:28 -0700148 if (atomic_read(&oldtbl->entries)
149 < oldtbl->mean_chain_len * (oldtbl->hash_mask + 1))
150 return -EAGAIN;
Javier Cardona18889232009-08-10 12:15:52 -0700151
cozybit Inca3e6b122011-04-13 11:10:28 -0700152 newtbl->free_node = oldtbl->free_node;
153 newtbl->mean_chain_len = oldtbl->mean_chain_len;
154 newtbl->copy_node = oldtbl->copy_node;
155 atomic_set(&newtbl->entries, atomic_read(&oldtbl->entries));
Javier Cardona18889232009-08-10 12:15:52 -0700156
cozybit Inca3e6b122011-04-13 11:10:28 -0700157 oldhash = oldtbl->hash_buckets;
158 for (i = 0; i <= oldtbl->hash_mask; i++)
Javier Cardona18889232009-08-10 12:15:52 -0700159 hlist_for_each(p, &oldhash[i])
cozybit Inca3e6b122011-04-13 11:10:28 -0700160 if (oldtbl->copy_node(p, newtbl) < 0)
Javier Cardona18889232009-08-10 12:15:52 -0700161 goto errcopy;
162
cozybit Inca3e6b122011-04-13 11:10:28 -0700163 return 0;
Javier Cardona18889232009-08-10 12:15:52 -0700164
165errcopy:
166 for (i = 0; i <= newtbl->hash_mask; i++) {
167 hlist_for_each_safe(p, q, &newtbl->hash_buckets[i])
cozybit Inca3e6b122011-04-13 11:10:28 -0700168 oldtbl->free_node(p, 0);
Javier Cardona18889232009-08-10 12:15:52 -0700169 }
cozybit Inca3e6b122011-04-13 11:10:28 -0700170 return -ENOMEM;
Javier Cardona18889232009-08-10 12:15:52 -0700171}
172
Johannes Berg6b86bd62011-05-12 13:38:50 +0200173static u32 mesh_table_hash(u8 *addr, struct ieee80211_sub_if_data *sdata,
174 struct mesh_table *tbl)
175{
176 /* Use last four bytes of hw addr and interface index as hash index */
177 return jhash_2words(*(u32 *)(addr+2), sdata->dev->ifindex, tbl->hash_rnd)
178 & tbl->hash_mask;
179}
Johannes Bergf5ea9122009-08-07 16:17:38 +0200180
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100181
182/**
183 *
184 * mesh_path_assign_nexthop - update mesh path next hop
185 *
186 * @mpath: mesh path to update
187 * @sta: next hop to assign
188 *
189 * Locking: mpath->state_lock must be held when calling this function
190 */
191void mesh_path_assign_nexthop(struct mesh_path *mpath, struct sta_info *sta)
192{
Javier Cardona10c836d2009-07-09 14:42:16 -0700193 struct sk_buff *skb;
194 struct ieee80211_hdr *hdr;
195 struct sk_buff_head tmpq;
196 unsigned long flags;
197
Johannes Bergd0709a62008-02-25 16:27:46 +0100198 rcu_assign_pointer(mpath->next_hop, sta);
Javier Cardona10c836d2009-07-09 14:42:16 -0700199
200 __skb_queue_head_init(&tmpq);
201
202 spin_lock_irqsave(&mpath->frame_queue.lock, flags);
203
204 while ((skb = __skb_dequeue(&mpath->frame_queue)) != NULL) {
205 hdr = (struct ieee80211_hdr *) skb->data;
206 memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN);
207 __skb_queue_tail(&tmpq, skb);
208 }
209
210 skb_queue_splice(&tmpq, &mpath->frame_queue);
211 spin_unlock_irqrestore(&mpath->frame_queue.lock, flags);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100212}
213
214
215/**
216 * mesh_path_lookup - look up a path in the mesh path table
217 * @dst: hardware address (ETH_ALEN length) of destination
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200218 * @sdata: local subif
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100219 *
220 * Returns: pointer to the mesh path structure, or NULL if not found
221 *
222 * Locking: must be called within a read rcu section.
223 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200224struct mesh_path *mesh_path_lookup(u8 *dst, struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100225{
226 struct mesh_path *mpath;
227 struct hlist_node *n;
228 struct hlist_head *bucket;
229 struct mesh_table *tbl;
230 struct mpath_node *node;
231
232 tbl = rcu_dereference(mesh_paths);
233
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200234 bucket = &tbl->hash_buckets[mesh_table_hash(dst, sdata, tbl)];
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100235 hlist_for_each_entry_rcu(node, n, bucket, list) {
236 mpath = node->mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200237 if (mpath->sdata == sdata &&
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100238 memcmp(dst, mpath->dst, ETH_ALEN) == 0) {
239 if (MPATH_EXPIRED(mpath)) {
240 spin_lock_bh(&mpath->state_lock);
241 if (MPATH_EXPIRED(mpath))
242 mpath->flags &= ~MESH_PATH_ACTIVE;
243 spin_unlock_bh(&mpath->state_lock);
244 }
245 return mpath;
246 }
247 }
248 return NULL;
249}
250
YanBo79617de2008-09-22 13:30:32 +0800251struct mesh_path *mpp_path_lookup(u8 *dst, struct ieee80211_sub_if_data *sdata)
252{
253 struct mesh_path *mpath;
254 struct hlist_node *n;
255 struct hlist_head *bucket;
256 struct mesh_table *tbl;
257 struct mpath_node *node;
258
259 tbl = rcu_dereference(mpp_paths);
260
261 bucket = &tbl->hash_buckets[mesh_table_hash(dst, sdata, tbl)];
262 hlist_for_each_entry_rcu(node, n, bucket, list) {
263 mpath = node->mpath;
264 if (mpath->sdata == sdata &&
265 memcmp(dst, mpath->dst, ETH_ALEN) == 0) {
266 if (MPATH_EXPIRED(mpath)) {
267 spin_lock_bh(&mpath->state_lock);
268 if (MPATH_EXPIRED(mpath))
269 mpath->flags &= ~MESH_PATH_ACTIVE;
270 spin_unlock_bh(&mpath->state_lock);
271 }
272 return mpath;
273 }
274 }
275 return NULL;
276}
277
278
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100279/**
280 * mesh_path_lookup_by_idx - look up a path in the mesh path table by its index
281 * @idx: index
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200282 * @sdata: local subif, or NULL for all entries
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100283 *
284 * Returns: pointer to the mesh path structure, or NULL if not found.
285 *
286 * Locking: must be called within a read rcu section.
287 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200288struct mesh_path *mesh_path_lookup_by_idx(int idx, struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100289{
Johannes Berg349eb8c2011-05-14 11:56:16 +0200290 struct mesh_table *tbl = rcu_dereference(mesh_paths);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100291 struct mpath_node *node;
292 struct hlist_node *p;
293 int i;
294 int j = 0;
295
Johannes Berg349eb8c2011-05-14 11:56:16 +0200296 for_each_mesh_entry(tbl, p, node, i) {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200297 if (sdata && node->mpath->sdata != sdata)
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800298 continue;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100299 if (j++ == idx) {
300 if (MPATH_EXPIRED(node->mpath)) {
301 spin_lock_bh(&node->mpath->state_lock);
302 if (MPATH_EXPIRED(node->mpath))
303 node->mpath->flags &= ~MESH_PATH_ACTIVE;
304 spin_unlock_bh(&node->mpath->state_lock);
305 }
306 return node->mpath;
307 }
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800308 }
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100309
310 return NULL;
311}
312
313/**
314 * mesh_path_add - allocate and add a new path to the mesh path table
315 * @addr: destination address of the path (ETH_ALEN length)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200316 * @sdata: local subif
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100317 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200318 * Returns: 0 on success
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100319 *
320 * State: the initial state of the new path is set to 0
321 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200322int mesh_path_add(u8 *dst, struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100323{
Javier Cardona18889232009-08-10 12:15:52 -0700324 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
325 struct ieee80211_local *local = sdata->local;
Johannes Berg349eb8c2011-05-14 11:56:16 +0200326 struct mesh_table *tbl;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100327 struct mesh_path *mpath, *new_mpath;
328 struct mpath_node *node, *new_node;
329 struct hlist_head *bucket;
330 struct hlist_node *n;
331 int grow = 0;
332 int err = 0;
333 u32 hash_idx;
334
Johannes Berg47846c92009-11-25 17:46:19 +0100335 if (memcmp(dst, sdata->vif.addr, ETH_ALEN) == 0)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100336 /* never add ourselves as neighbours */
337 return -ENOTSUPP;
338
339 if (is_multicast_ether_addr(dst))
340 return -ENOTSUPP;
341
Johannes Berg472dbc42008-09-11 00:01:49 +0200342 if (atomic_add_unless(&sdata->u.mesh.mpaths, 1, MESH_MAX_MPATHS) == 0)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100343 return -ENOSPC;
344
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400345 err = -ENOMEM;
Javier Cardona18889232009-08-10 12:15:52 -0700346 new_mpath = kzalloc(sizeof(struct mesh_path), GFP_ATOMIC);
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400347 if (!new_mpath)
348 goto err_path_alloc;
349
Javier Cardona18889232009-08-10 12:15:52 -0700350 new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400351 if (!new_node)
352 goto err_node_alloc;
Pavel Emelyanovf84e71a2008-05-06 18:46:36 +0400353
Javier Cardona9b84b8082011-05-03 16:57:16 -0700354 read_lock_bh(&pathtbl_resize_lock);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100355 memcpy(new_mpath->dst, dst, ETH_ALEN);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200356 new_mpath->sdata = sdata;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100357 new_mpath->flags = 0;
358 skb_queue_head_init(&new_mpath->frame_queue);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100359 new_node->mpath = new_mpath;
360 new_mpath->timer.data = (unsigned long) new_mpath;
361 new_mpath->timer.function = mesh_path_timer;
362 new_mpath->exp_time = jiffies;
363 spin_lock_init(&new_mpath->state_lock);
364 init_timer(&new_mpath->timer);
365
Johannes Berg349eb8c2011-05-14 11:56:16 +0200366 tbl = resize_dereference_mesh_paths();
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100367
Johannes Berg349eb8c2011-05-14 11:56:16 +0200368 hash_idx = mesh_table_hash(dst, sdata, tbl);
369 bucket = &tbl->hash_buckets[hash_idx];
370
371 spin_lock_bh(&tbl->hashwlock[hash_idx]);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100372
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400373 err = -EEXIST;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100374 hlist_for_each_entry(node, n, bucket, list) {
375 mpath = node->mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200376 if (mpath->sdata == sdata && memcmp(dst, mpath->dst, ETH_ALEN) == 0)
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400377 goto err_exists;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100378 }
379
380 hlist_add_head_rcu(&new_node->list, bucket);
Johannes Berg349eb8c2011-05-14 11:56:16 +0200381 if (atomic_inc_return(&tbl->entries) >=
382 tbl->mean_chain_len * (tbl->hash_mask + 1))
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100383 grow = 1;
384
Johannes Bergf5ea9122009-08-07 16:17:38 +0200385 mesh_paths_generation++;
386
Johannes Berg349eb8c2011-05-14 11:56:16 +0200387 spin_unlock_bh(&tbl->hashwlock[hash_idx]);
Javier Cardona9b84b8082011-05-03 16:57:16 -0700388 read_unlock_bh(&pathtbl_resize_lock);
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400389 if (grow) {
Javier Cardona18889232009-08-10 12:15:52 -0700390 set_bit(MESH_WORK_GROW_MPATH_TABLE, &ifmsh->wrkq_flags);
Johannes Berg64592c82010-06-10 10:21:31 +0200391 ieee80211_queue_work(&local->hw, &sdata->work);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100392 }
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400393 return 0;
394
395err_exists:
Johannes Berg349eb8c2011-05-14 11:56:16 +0200396 spin_unlock_bh(&tbl->hashwlock[hash_idx]);
Javier Cardona9b84b8082011-05-03 16:57:16 -0700397 read_unlock_bh(&pathtbl_resize_lock);
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400398 kfree(new_node);
399err_node_alloc:
400 kfree(new_mpath);
401err_path_alloc:
Johannes Berg472dbc42008-09-11 00:01:49 +0200402 atomic_dec(&sdata->u.mesh.mpaths);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100403 return err;
404}
405
Johannes Berg1928eca2011-05-14 11:00:52 +0200406static void mesh_table_free_rcu(struct rcu_head *rcu)
407{
408 struct mesh_table *tbl = container_of(rcu, struct mesh_table, rcu_head);
409
410 mesh_table_free(tbl, false);
411}
412
Javier Cardona18889232009-08-10 12:15:52 -0700413void mesh_mpath_table_grow(void)
414{
415 struct mesh_table *oldtbl, *newtbl;
416
Javier Cardona9b84b8082011-05-03 16:57:16 -0700417 write_lock_bh(&pathtbl_resize_lock);
Johannes Berg349eb8c2011-05-14 11:56:16 +0200418 oldtbl = resize_dereference_mesh_paths();
419 newtbl = mesh_table_alloc(oldtbl->size_order + 1);
Johannes Berg1928eca2011-05-14 11:00:52 +0200420 if (!newtbl)
421 goto out;
Johannes Berg349eb8c2011-05-14 11:56:16 +0200422 if (mesh_table_grow(oldtbl, newtbl) < 0) {
cozybit Inca3e6b122011-04-13 11:10:28 -0700423 __mesh_table_free(newtbl);
Johannes Berg1928eca2011-05-14 11:00:52 +0200424 goto out;
Javier Cardona18889232009-08-10 12:15:52 -0700425 }
426 rcu_assign_pointer(mesh_paths, newtbl);
Javier Cardona18889232009-08-10 12:15:52 -0700427
Johannes Berg1928eca2011-05-14 11:00:52 +0200428 call_rcu(&oldtbl->rcu_head, mesh_table_free_rcu);
429
430 out:
431 write_unlock_bh(&pathtbl_resize_lock);
Javier Cardona18889232009-08-10 12:15:52 -0700432}
433
434void mesh_mpp_table_grow(void)
435{
436 struct mesh_table *oldtbl, *newtbl;
437
Javier Cardona9b84b8082011-05-03 16:57:16 -0700438 write_lock_bh(&pathtbl_resize_lock);
Johannes Berg349eb8c2011-05-14 11:56:16 +0200439 oldtbl = resize_dereference_mpp_paths();
440 newtbl = mesh_table_alloc(oldtbl->size_order + 1);
Johannes Berg1928eca2011-05-14 11:00:52 +0200441 if (!newtbl)
442 goto out;
Johannes Berg349eb8c2011-05-14 11:56:16 +0200443 if (mesh_table_grow(oldtbl, newtbl) < 0) {
cozybit Inca3e6b122011-04-13 11:10:28 -0700444 __mesh_table_free(newtbl);
Johannes Berg1928eca2011-05-14 11:00:52 +0200445 goto out;
Javier Cardona18889232009-08-10 12:15:52 -0700446 }
447 rcu_assign_pointer(mpp_paths, newtbl);
Johannes Berg1928eca2011-05-14 11:00:52 +0200448 call_rcu(&oldtbl->rcu_head, mesh_table_free_rcu);
Javier Cardona18889232009-08-10 12:15:52 -0700449
Johannes Berg1928eca2011-05-14 11:00:52 +0200450 out:
451 write_unlock_bh(&pathtbl_resize_lock);
Javier Cardona18889232009-08-10 12:15:52 -0700452}
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100453
YanBo79617de2008-09-22 13:30:32 +0800454int mpp_path_add(u8 *dst, u8 *mpp, struct ieee80211_sub_if_data *sdata)
455{
Javier Cardona18889232009-08-10 12:15:52 -0700456 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
457 struct ieee80211_local *local = sdata->local;
Johannes Berg349eb8c2011-05-14 11:56:16 +0200458 struct mesh_table *tbl;
YanBo79617de2008-09-22 13:30:32 +0800459 struct mesh_path *mpath, *new_mpath;
460 struct mpath_node *node, *new_node;
461 struct hlist_head *bucket;
462 struct hlist_node *n;
463 int grow = 0;
464 int err = 0;
465 u32 hash_idx;
466
Johannes Berg47846c92009-11-25 17:46:19 +0100467 if (memcmp(dst, sdata->vif.addr, ETH_ALEN) == 0)
YanBo79617de2008-09-22 13:30:32 +0800468 /* never add ourselves as neighbours */
469 return -ENOTSUPP;
470
471 if (is_multicast_ether_addr(dst))
472 return -ENOTSUPP;
473
474 err = -ENOMEM;
Javier Cardona18889232009-08-10 12:15:52 -0700475 new_mpath = kzalloc(sizeof(struct mesh_path), GFP_ATOMIC);
YanBo79617de2008-09-22 13:30:32 +0800476 if (!new_mpath)
477 goto err_path_alloc;
478
Javier Cardona18889232009-08-10 12:15:52 -0700479 new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
YanBo79617de2008-09-22 13:30:32 +0800480 if (!new_node)
481 goto err_node_alloc;
482
Javier Cardona9b84b8082011-05-03 16:57:16 -0700483 read_lock_bh(&pathtbl_resize_lock);
YanBo79617de2008-09-22 13:30:32 +0800484 memcpy(new_mpath->dst, dst, ETH_ALEN);
485 memcpy(new_mpath->mpp, mpp, ETH_ALEN);
486 new_mpath->sdata = sdata;
487 new_mpath->flags = 0;
488 skb_queue_head_init(&new_mpath->frame_queue);
489 new_node->mpath = new_mpath;
490 new_mpath->exp_time = jiffies;
491 spin_lock_init(&new_mpath->state_lock);
492
Johannes Berg349eb8c2011-05-14 11:56:16 +0200493 tbl = resize_dereference_mpp_paths();
YanBo79617de2008-09-22 13:30:32 +0800494
Johannes Berg349eb8c2011-05-14 11:56:16 +0200495 hash_idx = mesh_table_hash(dst, sdata, tbl);
496 bucket = &tbl->hash_buckets[hash_idx];
497
498 spin_lock_bh(&tbl->hashwlock[hash_idx]);
YanBo79617de2008-09-22 13:30:32 +0800499
500 err = -EEXIST;
501 hlist_for_each_entry(node, n, bucket, list) {
502 mpath = node->mpath;
503 if (mpath->sdata == sdata && memcmp(dst, mpath->dst, ETH_ALEN) == 0)
504 goto err_exists;
505 }
506
507 hlist_add_head_rcu(&new_node->list, bucket);
Johannes Berg349eb8c2011-05-14 11:56:16 +0200508 if (atomic_inc_return(&tbl->entries) >=
509 tbl->mean_chain_len * (tbl->hash_mask + 1))
YanBo79617de2008-09-22 13:30:32 +0800510 grow = 1;
511
Johannes Berg349eb8c2011-05-14 11:56:16 +0200512 spin_unlock_bh(&tbl->hashwlock[hash_idx]);
Javier Cardona9b84b8082011-05-03 16:57:16 -0700513 read_unlock_bh(&pathtbl_resize_lock);
YanBo79617de2008-09-22 13:30:32 +0800514 if (grow) {
Javier Cardona18889232009-08-10 12:15:52 -0700515 set_bit(MESH_WORK_GROW_MPP_TABLE, &ifmsh->wrkq_flags);
Johannes Berg64592c82010-06-10 10:21:31 +0200516 ieee80211_queue_work(&local->hw, &sdata->work);
YanBo79617de2008-09-22 13:30:32 +0800517 }
518 return 0;
519
520err_exists:
Johannes Berg349eb8c2011-05-14 11:56:16 +0200521 spin_unlock_bh(&tbl->hashwlock[hash_idx]);
Javier Cardona9b84b8082011-05-03 16:57:16 -0700522 read_unlock_bh(&pathtbl_resize_lock);
YanBo79617de2008-09-22 13:30:32 +0800523 kfree(new_node);
524err_node_alloc:
525 kfree(new_mpath);
526err_path_alloc:
527 return err;
528}
529
530
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100531/**
532 * mesh_plink_broken - deactivates paths and sends perr when a link breaks
533 *
534 * @sta: broken peer link
535 *
536 * This function must be called from the rate control algorithm if enough
537 * delivery errors suggest that a peer link is no longer usable.
538 */
539void mesh_plink_broken(struct sta_info *sta)
540{
Johannes Berg349eb8c2011-05-14 11:56:16 +0200541 struct mesh_table *tbl;
Johannes Berg15ff6362009-11-17 13:34:04 +0100542 static const u8 bcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100543 struct mesh_path *mpath;
544 struct mpath_node *node;
545 struct hlist_node *p;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200546 struct ieee80211_sub_if_data *sdata = sta->sdata;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100547 int i;
Thomas Pedersen25d49e42011-08-11 19:35:15 -0700548 __le16 reason = cpu_to_le16(WLAN_REASON_MESH_PATH_DEST_UNREACHABLE);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100549
550 rcu_read_lock();
Johannes Berg349eb8c2011-05-14 11:56:16 +0200551 tbl = rcu_dereference(mesh_paths);
552 for_each_mesh_entry(tbl, p, node, i) {
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100553 mpath = node->mpath;
554 spin_lock_bh(&mpath->state_lock);
Johannes Berg349eb8c2011-05-14 11:56:16 +0200555 if (rcu_dereference(mpath->next_hop) == sta &&
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100556 mpath->flags & MESH_PATH_ACTIVE &&
557 !(mpath->flags & MESH_PATH_FIXED)) {
558 mpath->flags &= ~MESH_PATH_ACTIVE;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000559 ++mpath->sn;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100560 spin_unlock_bh(&mpath->state_lock);
Javier Cardona45904f22010-12-03 09:20:40 +0100561 mesh_path_error_tx(sdata->u.mesh.mshcfg.element_ttl,
562 mpath->dst, cpu_to_le32(mpath->sn),
Thomas Pedersen25d49e42011-08-11 19:35:15 -0700563 reason, bcast, sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100564 } else
565 spin_unlock_bh(&mpath->state_lock);
566 }
567 rcu_read_unlock();
568}
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100569
570/**
571 * mesh_path_flush_by_nexthop - Deletes mesh paths if their next hop matches
572 *
573 * @sta - mesh peer to match
574 *
Luis Carlos Cobob4e08ea2008-02-29 15:46:08 -0800575 * RCU notes: this function is called when a mesh plink transitions from
576 * PLINK_ESTAB to any other state, since PLINK_ESTAB state is the only one that
577 * allows path creation. This will happen before the sta can be freed (because
Johannes Bergd0709a62008-02-25 16:27:46 +0100578 * sta_info_destroy() calls this) so any reader in a rcu read block will be
579 * protected against the plink disappearing.
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100580 */
581void mesh_path_flush_by_nexthop(struct sta_info *sta)
582{
Johannes Berg349eb8c2011-05-14 11:56:16 +0200583 struct mesh_table *tbl;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100584 struct mesh_path *mpath;
585 struct mpath_node *node;
586 struct hlist_node *p;
587 int i;
588
Johannes Berg349eb8c2011-05-14 11:56:16 +0200589 rcu_read_lock();
590 tbl = rcu_dereference(mesh_paths);
591 for_each_mesh_entry(tbl, p, node, i) {
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100592 mpath = node->mpath;
Johannes Berg349eb8c2011-05-14 11:56:16 +0200593 if (rcu_dereference(mpath->next_hop) == sta)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200594 mesh_path_del(mpath->dst, mpath->sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100595 }
Johannes Berg349eb8c2011-05-14 11:56:16 +0200596 rcu_read_unlock();
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100597}
598
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200599void mesh_path_flush(struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100600{
Johannes Berg349eb8c2011-05-14 11:56:16 +0200601 struct mesh_table *tbl;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100602 struct mesh_path *mpath;
603 struct mpath_node *node;
604 struct hlist_node *p;
605 int i;
606
Johannes Berg349eb8c2011-05-14 11:56:16 +0200607 rcu_read_lock();
608 tbl = rcu_dereference(mesh_paths);
609 for_each_mesh_entry(tbl, p, node, i) {
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100610 mpath = node->mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200611 if (mpath->sdata == sdata)
612 mesh_path_del(mpath->dst, mpath->sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100613 }
Johannes Berg349eb8c2011-05-14 11:56:16 +0200614 rcu_read_unlock();
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100615}
616
617static void mesh_path_node_reclaim(struct rcu_head *rp)
618{
619 struct mpath_node *node = container_of(rp, struct mpath_node, rcu);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200620 struct ieee80211_sub_if_data *sdata = node->mpath->sdata;
Johannes Bergd0709a62008-02-25 16:27:46 +0100621
Javier Cardona86d7f9f2011-08-09 16:45:05 -0700622 if (node->mpath->timer.function)
623 del_timer_sync(&node->mpath->timer);
Johannes Berg472dbc42008-09-11 00:01:49 +0200624 atomic_dec(&sdata->u.mesh.mpaths);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100625 kfree(node->mpath);
626 kfree(node);
627}
628
629/**
630 * mesh_path_del - delete a mesh path from the table
631 *
632 * @addr: dst address (ETH_ALEN length)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200633 * @sdata: local subif
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100634 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200635 * Returns: 0 if successful
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100636 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200637int mesh_path_del(u8 *addr, struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100638{
Johannes Berg349eb8c2011-05-14 11:56:16 +0200639 struct mesh_table *tbl;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100640 struct mesh_path *mpath;
641 struct mpath_node *node;
642 struct hlist_head *bucket;
643 struct hlist_node *n;
644 int hash_idx;
645 int err = 0;
646
Javier Cardona9b84b8082011-05-03 16:57:16 -0700647 read_lock_bh(&pathtbl_resize_lock);
Johannes Berg349eb8c2011-05-14 11:56:16 +0200648 tbl = resize_dereference_mesh_paths();
649 hash_idx = mesh_table_hash(addr, sdata, tbl);
650 bucket = &tbl->hash_buckets[hash_idx];
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100651
Johannes Berg349eb8c2011-05-14 11:56:16 +0200652 spin_lock_bh(&tbl->hashwlock[hash_idx]);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100653 hlist_for_each_entry(node, n, bucket, list) {
654 mpath = node->mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200655 if (mpath->sdata == sdata &&
Johannes Berg349eb8c2011-05-14 11:56:16 +0200656 memcmp(addr, mpath->dst, ETH_ALEN) == 0) {
Javier Cardonaa6965c42011-08-09 16:45:06 -0700657 spin_lock_bh(&mpath->state_lock);
Luis Carlos Cobocfa22c72008-02-29 15:04:13 -0800658 mpath->flags |= MESH_PATH_RESOLVING;
659 hlist_del_rcu(&node->list);
660 call_rcu(&node->rcu, mesh_path_node_reclaim);
Johannes Berg349eb8c2011-05-14 11:56:16 +0200661 atomic_dec(&tbl->entries);
Javier Cardonaa6965c42011-08-09 16:45:06 -0700662 spin_unlock_bh(&mpath->state_lock);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100663 goto enddel;
664 }
665 }
666
667 err = -ENXIO;
668enddel:
Johannes Bergf5ea9122009-08-07 16:17:38 +0200669 mesh_paths_generation++;
Johannes Berg349eb8c2011-05-14 11:56:16 +0200670 spin_unlock_bh(&tbl->hashwlock[hash_idx]);
Javier Cardona9b84b8082011-05-03 16:57:16 -0700671 read_unlock_bh(&pathtbl_resize_lock);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100672 return err;
673}
674
675/**
676 * mesh_path_tx_pending - sends pending frames in a mesh path queue
677 *
678 * @mpath: mesh path to activate
679 *
680 * Locking: the state_lock of the mpath structure must NOT be held when calling
681 * this function.
682 */
683void mesh_path_tx_pending(struct mesh_path *mpath)
684{
Javier Cardona249b4052009-07-07 10:55:03 -0700685 if (mpath->flags & MESH_PATH_ACTIVE)
686 ieee80211_add_pending_skbs(mpath->sdata->local,
687 &mpath->frame_queue);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100688}
689
690/**
691 * mesh_path_discard_frame - discard a frame whose path could not be resolved
692 *
693 * @skb: frame to discard
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200694 * @sdata: network subif the frame was to be sent through
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100695 *
Javier Cardona35946a52009-07-13 17:00:10 -0700696 * If the frame was being forwarded from another MP, a PERR frame will be sent
697 * to the precursor. The precursor's address (i.e. the previous hop) was saved
698 * in addr1 of the frame-to-be-forwarded, and would only be overwritten once
699 * the destination is successfully resolved.
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100700 *
701 * Locking: the function must me called within a rcu_read_lock region
702 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200703void mesh_path_discard_frame(struct sk_buff *skb,
704 struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100705{
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +0200706 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100707 struct mesh_path *mpath;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000708 u32 sn = 0;
Thomas Pedersen25d49e42011-08-11 19:35:15 -0700709 __le16 reason = cpu_to_le16(WLAN_REASON_MESH_PATH_NOFORWARD);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100710
Johannes Berg47846c92009-11-25 17:46:19 +0100711 if (memcmp(hdr->addr4, sdata->vif.addr, ETH_ALEN) != 0) {
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100712 u8 *ra, *da;
713
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +0200714 da = hdr->addr3;
Javier Cardona35946a52009-07-13 17:00:10 -0700715 ra = hdr->addr1;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200716 mpath = mesh_path_lookup(da, sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100717 if (mpath)
Rui Paulod19b3bf2009-11-09 23:46:55 +0000718 sn = ++mpath->sn;
Javier Cardona45904f22010-12-03 09:20:40 +0100719 mesh_path_error_tx(sdata->u.mesh.mshcfg.element_ttl, skb->data,
Thomas Pedersen25d49e42011-08-11 19:35:15 -0700720 cpu_to_le32(sn), reason, ra, sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100721 }
722
723 kfree_skb(skb);
Johannes Berg472dbc42008-09-11 00:01:49 +0200724 sdata->u.mesh.mshstats.dropped_frames_no_route++;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100725}
726
727/**
728 * mesh_path_flush_pending - free the pending queue of a mesh path
729 *
730 * @mpath: mesh path whose queue has to be freed
731 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300732 * Locking: the function must me called within a rcu_read_lock region
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100733 */
734void mesh_path_flush_pending(struct mesh_path *mpath)
735{
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100736 struct sk_buff *skb;
737
Javier Cardona00e3f252011-08-09 16:45:07 -0700738 while ((skb = skb_dequeue(&mpath->frame_queue)) != NULL)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200739 mesh_path_discard_frame(skb, mpath->sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100740}
741
742/**
743 * mesh_path_fix_nexthop - force a specific next hop for a mesh path
744 *
745 * @mpath: the mesh path to modify
746 * @next_hop: the next hop to force
747 *
748 * Locking: this function must be called holding mpath->state_lock
749 */
750void mesh_path_fix_nexthop(struct mesh_path *mpath, struct sta_info *next_hop)
751{
752 spin_lock_bh(&mpath->state_lock);
753 mesh_path_assign_nexthop(mpath, next_hop);
Rui Paulod19b3bf2009-11-09 23:46:55 +0000754 mpath->sn = 0xffff;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100755 mpath->metric = 0;
756 mpath->hop_count = 0;
757 mpath->exp_time = 0;
758 mpath->flags |= MESH_PATH_FIXED;
759 mesh_path_activate(mpath);
760 spin_unlock_bh(&mpath->state_lock);
761 mesh_path_tx_pending(mpath);
762}
763
764static void mesh_path_node_free(struct hlist_node *p, bool free_leafs)
765{
766 struct mesh_path *mpath;
767 struct mpath_node *node = hlist_entry(p, struct mpath_node, list);
768 mpath = node->mpath;
769 hlist_del_rcu(p);
Javier Cardonad0df9ee2011-05-13 14:16:07 -0700770 if (free_leafs) {
Javier Cardona86d7f9f2011-08-09 16:45:05 -0700771 if (mpath->timer.function)
772 del_timer_sync(&mpath->timer);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100773 kfree(mpath);
Javier Cardonad0df9ee2011-05-13 14:16:07 -0700774 }
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100775 kfree(node);
776}
777
Pavel Emelyanov4caf86c2008-05-07 19:47:01 +0400778static int mesh_path_node_copy(struct hlist_node *p, struct mesh_table *newtbl)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100779{
780 struct mesh_path *mpath;
781 struct mpath_node *node, *new_node;
782 u32 hash_idx;
783
Pavel Emelyanov8566dc32008-05-07 19:51:51 +0400784 new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
Pavel Emelyanov00242c42008-05-07 19:48:14 +0400785 if (new_node == NULL)
786 return -ENOMEM;
787
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100788 node = hlist_entry(p, struct mpath_node, list);
789 mpath = node->mpath;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100790 new_node->mpath = mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200791 hash_idx = mesh_table_hash(mpath->dst, mpath->sdata, newtbl);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100792 hlist_add_head(&new_node->list,
793 &newtbl->hash_buckets[hash_idx]);
Pavel Emelyanov4caf86c2008-05-07 19:47:01 +0400794 return 0;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100795}
796
797int mesh_pathtbl_init(void)
798{
Johannes Berg349eb8c2011-05-14 11:56:16 +0200799 struct mesh_table *tbl_path, *tbl_mpp;
YanBo79617de2008-09-22 13:30:32 +0800800
Johannes Berg349eb8c2011-05-14 11:56:16 +0200801 tbl_path = mesh_table_alloc(INIT_PATHS_SIZE_ORDER);
802 if (!tbl_path)
803 return -ENOMEM;
804 tbl_path->free_node = &mesh_path_node_free;
805 tbl_path->copy_node = &mesh_path_node_copy;
806 tbl_path->mean_chain_len = MEAN_CHAIN_LEN;
807
808 tbl_mpp = mesh_table_alloc(INIT_PATHS_SIZE_ORDER);
809 if (!tbl_mpp) {
810 mesh_table_free(tbl_path, true);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100811 return -ENOMEM;
YanBo79617de2008-09-22 13:30:32 +0800812 }
Johannes Berg349eb8c2011-05-14 11:56:16 +0200813 tbl_mpp->free_node = &mesh_path_node_free;
814 tbl_mpp->copy_node = &mesh_path_node_copy;
815 tbl_mpp->mean_chain_len = MEAN_CHAIN_LEN;
816
817 /* Need no locking since this is during init */
818 RCU_INIT_POINTER(mesh_paths, tbl_path);
819 RCU_INIT_POINTER(mpp_paths, tbl_mpp);
YanBo79617de2008-09-22 13:30:32 +0800820
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100821 return 0;
822}
823
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200824void mesh_path_expire(struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100825{
Johannes Berg349eb8c2011-05-14 11:56:16 +0200826 struct mesh_table *tbl;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100827 struct mesh_path *mpath;
828 struct mpath_node *node;
829 struct hlist_node *p;
830 int i;
831
Johannes Berg349eb8c2011-05-14 11:56:16 +0200832 rcu_read_lock();
833 tbl = rcu_dereference(mesh_paths);
834 for_each_mesh_entry(tbl, p, node, i) {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200835 if (node->mpath->sdata != sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100836 continue;
837 mpath = node->mpath;
838 spin_lock_bh(&mpath->state_lock);
839 if ((!(mpath->flags & MESH_PATH_RESOLVING)) &&
840 (!(mpath->flags & MESH_PATH_FIXED)) &&
Johannes Berg349eb8c2011-05-14 11:56:16 +0200841 time_after(jiffies, mpath->exp_time + MESH_PATH_EXPIRE)) {
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100842 spin_unlock_bh(&mpath->state_lock);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200843 mesh_path_del(mpath->dst, mpath->sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100844 } else
845 spin_unlock_bh(&mpath->state_lock);
846 }
Johannes Berg349eb8c2011-05-14 11:56:16 +0200847 rcu_read_unlock();
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100848}
849
850void mesh_pathtbl_unregister(void)
851{
Johannes Berg349eb8c2011-05-14 11:56:16 +0200852 /* no need for locking during exit path */
853 mesh_table_free(rcu_dereference_raw(mesh_paths), true);
854 mesh_table_free(rcu_dereference_raw(mpp_paths), true);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100855}