blob: 431865a58622ffd6519b36638aa0931d485e99b8 [file] [log] [blame]
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001/*
2 * Copyright (c) 2008 open80211s Ltd.
3 * 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>
13#include <linux/spinlock.h>
14#include <linux/string.h>
15#include <net/mac80211.h>
16#include "ieee80211_i.h"
17#include "mesh.h"
18
19/* There will be initially 2^INIT_PATHS_SIZE_ORDER buckets */
20#define INIT_PATHS_SIZE_ORDER 2
21
22/* Keep the mean chain length below this constant */
23#define MEAN_CHAIN_LEN 2
24
25#define MPATH_EXPIRED(mpath) ((mpath->flags & MESH_PATH_ACTIVE) && \
26 time_after(jiffies, mpath->exp_time) && \
27 !(mpath->flags & MESH_PATH_FIXED))
28
29struct mpath_node {
30 struct hlist_node list;
31 struct rcu_head rcu;
32 /* This indirection allows two different tables to point to the same
33 * mesh_path structure, useful when resizing
34 */
35 struct mesh_path *mpath;
36};
37
38static struct mesh_table *mesh_paths;
YanBo79617de2008-09-22 13:30:32 +080039static struct mesh_table *mpp_paths; /* Store paths for MPP&MAP */
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +010040
Johannes Bergf5ea9122009-08-07 16:17:38 +020041int mesh_paths_generation;
42
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +010043/* This lock will have the grow table function as writer and add / delete nodes
44 * as readers. When reading the table (i.e. doing lookups) we are well protected
45 * by RCU
46 */
47static DEFINE_RWLOCK(pathtbl_resize_lock);
48
49/**
50 *
51 * mesh_path_assign_nexthop - update mesh path next hop
52 *
53 * @mpath: mesh path to update
54 * @sta: next hop to assign
55 *
56 * Locking: mpath->state_lock must be held when calling this function
57 */
58void mesh_path_assign_nexthop(struct mesh_path *mpath, struct sta_info *sta)
59{
Javier Cardona10c836d2009-07-09 14:42:16 -070060 struct sk_buff *skb;
61 struct ieee80211_hdr *hdr;
62 struct sk_buff_head tmpq;
63 unsigned long flags;
64
Johannes Bergd0709a62008-02-25 16:27:46 +010065 rcu_assign_pointer(mpath->next_hop, sta);
Javier Cardona10c836d2009-07-09 14:42:16 -070066
67 __skb_queue_head_init(&tmpq);
68
69 spin_lock_irqsave(&mpath->frame_queue.lock, flags);
70
71 while ((skb = __skb_dequeue(&mpath->frame_queue)) != NULL) {
72 hdr = (struct ieee80211_hdr *) skb->data;
73 memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN);
74 __skb_queue_tail(&tmpq, skb);
75 }
76
77 skb_queue_splice(&tmpq, &mpath->frame_queue);
78 spin_unlock_irqrestore(&mpath->frame_queue.lock, flags);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +010079}
80
81
82/**
83 * mesh_path_lookup - look up a path in the mesh path table
84 * @dst: hardware address (ETH_ALEN length) of destination
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +120085 * @sdata: local subif
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +010086 *
87 * Returns: pointer to the mesh path structure, or NULL if not found
88 *
89 * Locking: must be called within a read rcu section.
90 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +120091struct mesh_path *mesh_path_lookup(u8 *dst, struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +010092{
93 struct mesh_path *mpath;
94 struct hlist_node *n;
95 struct hlist_head *bucket;
96 struct mesh_table *tbl;
97 struct mpath_node *node;
98
99 tbl = rcu_dereference(mesh_paths);
100
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200101 bucket = &tbl->hash_buckets[mesh_table_hash(dst, sdata, tbl)];
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100102 hlist_for_each_entry_rcu(node, n, bucket, list) {
103 mpath = node->mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200104 if (mpath->sdata == sdata &&
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100105 memcmp(dst, mpath->dst, ETH_ALEN) == 0) {
106 if (MPATH_EXPIRED(mpath)) {
107 spin_lock_bh(&mpath->state_lock);
108 if (MPATH_EXPIRED(mpath))
109 mpath->flags &= ~MESH_PATH_ACTIVE;
110 spin_unlock_bh(&mpath->state_lock);
111 }
112 return mpath;
113 }
114 }
115 return NULL;
116}
117
YanBo79617de2008-09-22 13:30:32 +0800118struct mesh_path *mpp_path_lookup(u8 *dst, struct ieee80211_sub_if_data *sdata)
119{
120 struct mesh_path *mpath;
121 struct hlist_node *n;
122 struct hlist_head *bucket;
123 struct mesh_table *tbl;
124 struct mpath_node *node;
125
126 tbl = rcu_dereference(mpp_paths);
127
128 bucket = &tbl->hash_buckets[mesh_table_hash(dst, sdata, tbl)];
129 hlist_for_each_entry_rcu(node, n, bucket, list) {
130 mpath = node->mpath;
131 if (mpath->sdata == sdata &&
132 memcmp(dst, mpath->dst, ETH_ALEN) == 0) {
133 if (MPATH_EXPIRED(mpath)) {
134 spin_lock_bh(&mpath->state_lock);
135 if (MPATH_EXPIRED(mpath))
136 mpath->flags &= ~MESH_PATH_ACTIVE;
137 spin_unlock_bh(&mpath->state_lock);
138 }
139 return mpath;
140 }
141 }
142 return NULL;
143}
144
145
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100146/**
147 * mesh_path_lookup_by_idx - look up a path in the mesh path table by its index
148 * @idx: index
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200149 * @sdata: local subif, or NULL for all entries
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100150 *
151 * Returns: pointer to the mesh path structure, or NULL if not found.
152 *
153 * Locking: must be called within a read rcu section.
154 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200155struct mesh_path *mesh_path_lookup_by_idx(int idx, struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100156{
157 struct mpath_node *node;
158 struct hlist_node *p;
159 int i;
160 int j = 0;
161
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800162 for_each_mesh_entry(mesh_paths, p, node, i) {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200163 if (sdata && node->mpath->sdata != sdata)
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800164 continue;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100165 if (j++ == idx) {
166 if (MPATH_EXPIRED(node->mpath)) {
167 spin_lock_bh(&node->mpath->state_lock);
168 if (MPATH_EXPIRED(node->mpath))
169 node->mpath->flags &= ~MESH_PATH_ACTIVE;
170 spin_unlock_bh(&node->mpath->state_lock);
171 }
172 return node->mpath;
173 }
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800174 }
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100175
176 return NULL;
177}
178
179/**
180 * mesh_path_add - allocate and add a new path to the mesh path table
181 * @addr: destination address of the path (ETH_ALEN length)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200182 * @sdata: local subif
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100183 *
184 * Returns: 0 on sucess
185 *
186 * State: the initial state of the new path is set to 0
187 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200188int mesh_path_add(u8 *dst, struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100189{
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100190 struct mesh_path *mpath, *new_mpath;
191 struct mpath_node *node, *new_node;
192 struct hlist_head *bucket;
193 struct hlist_node *n;
194 int grow = 0;
195 int err = 0;
196 u32 hash_idx;
197
Johannes Berge2e414d2009-07-10 11:38:14 +0200198 might_sleep();
199
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200200 if (memcmp(dst, sdata->dev->dev_addr, ETH_ALEN) == 0)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100201 /* never add ourselves as neighbours */
202 return -ENOTSUPP;
203
204 if (is_multicast_ether_addr(dst))
205 return -ENOTSUPP;
206
Johannes Berg472dbc42008-09-11 00:01:49 +0200207 if (atomic_add_unless(&sdata->u.mesh.mpaths, 1, MESH_MAX_MPATHS) == 0)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100208 return -ENOSPC;
209
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400210 err = -ENOMEM;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100211 new_mpath = kzalloc(sizeof(struct mesh_path), GFP_KERNEL);
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400212 if (!new_mpath)
213 goto err_path_alloc;
214
Pavel Emelyanov0eb03d52008-05-06 18:49:02 +0400215 new_node = kmalloc(sizeof(struct mpath_node), GFP_KERNEL);
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400216 if (!new_node)
217 goto err_node_alloc;
Pavel Emelyanovf84e71a2008-05-06 18:46:36 +0400218
219 read_lock(&pathtbl_resize_lock);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100220 memcpy(new_mpath->dst, dst, ETH_ALEN);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200221 new_mpath->sdata = sdata;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100222 new_mpath->flags = 0;
223 skb_queue_head_init(&new_mpath->frame_queue);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100224 new_node->mpath = new_mpath;
225 new_mpath->timer.data = (unsigned long) new_mpath;
226 new_mpath->timer.function = mesh_path_timer;
227 new_mpath->exp_time = jiffies;
228 spin_lock_init(&new_mpath->state_lock);
229 init_timer(&new_mpath->timer);
230
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200231 hash_idx = mesh_table_hash(dst, sdata, mesh_paths);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100232 bucket = &mesh_paths->hash_buckets[hash_idx];
233
234 spin_lock(&mesh_paths->hashwlock[hash_idx]);
235
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400236 err = -EEXIST;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100237 hlist_for_each_entry(node, n, bucket, list) {
238 mpath = node->mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200239 if (mpath->sdata == sdata && memcmp(dst, mpath->dst, ETH_ALEN) == 0)
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400240 goto err_exists;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100241 }
242
243 hlist_add_head_rcu(&new_node->list, bucket);
244 if (atomic_inc_return(&mesh_paths->entries) >=
245 mesh_paths->mean_chain_len * (mesh_paths->hash_mask + 1))
246 grow = 1;
247
Johannes Bergf5ea9122009-08-07 16:17:38 +0200248 mesh_paths_generation++;
249
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100250 spin_unlock(&mesh_paths->hashwlock[hash_idx]);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100251 read_unlock(&pathtbl_resize_lock);
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400252 if (grow) {
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100253 struct mesh_table *oldtbl, *newtbl;
254
255 write_lock(&pathtbl_resize_lock);
256 oldtbl = mesh_paths;
257 newtbl = mesh_table_grow(mesh_paths);
258 if (!newtbl) {
259 write_unlock(&pathtbl_resize_lock);
Pavel Emelyanov3282aea2008-05-07 19:54:04 +0400260 return 0;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100261 }
262 rcu_assign_pointer(mesh_paths, newtbl);
Pavel Emelyanov6d6936e2008-05-06 18:51:31 +0400263 write_unlock(&pathtbl_resize_lock);
264
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100265 synchronize_rcu();
266 mesh_table_free(oldtbl, false);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100267 }
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400268 return 0;
269
270err_exists:
271 spin_unlock(&mesh_paths->hashwlock[hash_idx]);
272 read_unlock(&pathtbl_resize_lock);
273 kfree(new_node);
274err_node_alloc:
275 kfree(new_mpath);
276err_path_alloc:
Johannes Berg472dbc42008-09-11 00:01:49 +0200277 atomic_dec(&sdata->u.mesh.mpaths);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100278 return err;
279}
280
281
YanBo79617de2008-09-22 13:30:32 +0800282int mpp_path_add(u8 *dst, u8 *mpp, struct ieee80211_sub_if_data *sdata)
283{
284 struct mesh_path *mpath, *new_mpath;
285 struct mpath_node *node, *new_node;
286 struct hlist_head *bucket;
287 struct hlist_node *n;
288 int grow = 0;
289 int err = 0;
290 u32 hash_idx;
291
Johannes Berge2e414d2009-07-10 11:38:14 +0200292 might_sleep();
YanBo79617de2008-09-22 13:30:32 +0800293
294 if (memcmp(dst, sdata->dev->dev_addr, ETH_ALEN) == 0)
295 /* never add ourselves as neighbours */
296 return -ENOTSUPP;
297
298 if (is_multicast_ether_addr(dst))
299 return -ENOTSUPP;
300
301 err = -ENOMEM;
302 new_mpath = kzalloc(sizeof(struct mesh_path), GFP_KERNEL);
303 if (!new_mpath)
304 goto err_path_alloc;
305
306 new_node = kmalloc(sizeof(struct mpath_node), GFP_KERNEL);
307 if (!new_node)
308 goto err_node_alloc;
309
310 read_lock(&pathtbl_resize_lock);
311 memcpy(new_mpath->dst, dst, ETH_ALEN);
312 memcpy(new_mpath->mpp, mpp, ETH_ALEN);
313 new_mpath->sdata = sdata;
314 new_mpath->flags = 0;
315 skb_queue_head_init(&new_mpath->frame_queue);
316 new_node->mpath = new_mpath;
317 new_mpath->exp_time = jiffies;
318 spin_lock_init(&new_mpath->state_lock);
319
320 hash_idx = mesh_table_hash(dst, sdata, mpp_paths);
321 bucket = &mpp_paths->hash_buckets[hash_idx];
322
323 spin_lock(&mpp_paths->hashwlock[hash_idx]);
324
325 err = -EEXIST;
326 hlist_for_each_entry(node, n, bucket, list) {
327 mpath = node->mpath;
328 if (mpath->sdata == sdata && memcmp(dst, mpath->dst, ETH_ALEN) == 0)
329 goto err_exists;
330 }
331
332 hlist_add_head_rcu(&new_node->list, bucket);
333 if (atomic_inc_return(&mpp_paths->entries) >=
334 mpp_paths->mean_chain_len * (mpp_paths->hash_mask + 1))
335 grow = 1;
336
337 spin_unlock(&mpp_paths->hashwlock[hash_idx]);
338 read_unlock(&pathtbl_resize_lock);
339 if (grow) {
340 struct mesh_table *oldtbl, *newtbl;
341
342 write_lock(&pathtbl_resize_lock);
343 oldtbl = mpp_paths;
344 newtbl = mesh_table_grow(mpp_paths);
345 if (!newtbl) {
346 write_unlock(&pathtbl_resize_lock);
347 return 0;
348 }
349 rcu_assign_pointer(mpp_paths, newtbl);
350 write_unlock(&pathtbl_resize_lock);
351
352 synchronize_rcu();
353 mesh_table_free(oldtbl, false);
354 }
355 return 0;
356
357err_exists:
358 spin_unlock(&mpp_paths->hashwlock[hash_idx]);
359 read_unlock(&pathtbl_resize_lock);
360 kfree(new_node);
361err_node_alloc:
362 kfree(new_mpath);
363err_path_alloc:
364 return err;
365}
366
367
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100368/**
369 * mesh_plink_broken - deactivates paths and sends perr when a link breaks
370 *
371 * @sta: broken peer link
372 *
373 * This function must be called from the rate control algorithm if enough
374 * delivery errors suggest that a peer link is no longer usable.
375 */
376void mesh_plink_broken(struct sta_info *sta)
377{
378 struct mesh_path *mpath;
379 struct mpath_node *node;
380 struct hlist_node *p;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200381 struct ieee80211_sub_if_data *sdata = sta->sdata;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100382 int i;
383
384 rcu_read_lock();
385 for_each_mesh_entry(mesh_paths, p, node, i) {
386 mpath = node->mpath;
387 spin_lock_bh(&mpath->state_lock);
388 if (mpath->next_hop == sta &&
389 mpath->flags & MESH_PATH_ACTIVE &&
390 !(mpath->flags & MESH_PATH_FIXED)) {
391 mpath->flags &= ~MESH_PATH_ACTIVE;
392 ++mpath->dsn;
393 spin_unlock_bh(&mpath->state_lock);
394 mesh_path_error_tx(mpath->dst,
395 cpu_to_le32(mpath->dsn),
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200396 sdata->dev->broadcast, sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100397 } else
398 spin_unlock_bh(&mpath->state_lock);
399 }
400 rcu_read_unlock();
401}
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100402
403/**
404 * mesh_path_flush_by_nexthop - Deletes mesh paths if their next hop matches
405 *
406 * @sta - mesh peer to match
407 *
Luis Carlos Cobob4e08ea2008-02-29 15:46:08 -0800408 * RCU notes: this function is called when a mesh plink transitions from
409 * PLINK_ESTAB to any other state, since PLINK_ESTAB state is the only one that
410 * allows path creation. This will happen before the sta can be freed (because
Johannes Bergd0709a62008-02-25 16:27:46 +0100411 * sta_info_destroy() calls this) so any reader in a rcu read block will be
412 * protected against the plink disappearing.
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100413 */
414void mesh_path_flush_by_nexthop(struct sta_info *sta)
415{
416 struct mesh_path *mpath;
417 struct mpath_node *node;
418 struct hlist_node *p;
419 int i;
420
421 for_each_mesh_entry(mesh_paths, p, node, i) {
422 mpath = node->mpath;
423 if (mpath->next_hop == sta)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200424 mesh_path_del(mpath->dst, mpath->sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100425 }
426}
427
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200428void mesh_path_flush(struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100429{
430 struct mesh_path *mpath;
431 struct mpath_node *node;
432 struct hlist_node *p;
433 int i;
434
435 for_each_mesh_entry(mesh_paths, p, node, i) {
436 mpath = node->mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200437 if (mpath->sdata == sdata)
438 mesh_path_del(mpath->dst, mpath->sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100439 }
440}
441
442static void mesh_path_node_reclaim(struct rcu_head *rp)
443{
444 struct mpath_node *node = container_of(rp, struct mpath_node, rcu);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200445 struct ieee80211_sub_if_data *sdata = node->mpath->sdata;
Johannes Bergd0709a62008-02-25 16:27:46 +0100446
Luis Carlos Cobo89a1ad62008-02-29 14:49:37 -0800447 del_timer_sync(&node->mpath->timer);
Johannes Berg472dbc42008-09-11 00:01:49 +0200448 atomic_dec(&sdata->u.mesh.mpaths);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100449 kfree(node->mpath);
450 kfree(node);
451}
452
453/**
454 * mesh_path_del - delete a mesh path from the table
455 *
456 * @addr: dst address (ETH_ALEN length)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200457 * @sdata: local subif
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100458 *
459 * Returns: 0 if succesful
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100460 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200461int mesh_path_del(u8 *addr, struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100462{
463 struct mesh_path *mpath;
464 struct mpath_node *node;
465 struct hlist_head *bucket;
466 struct hlist_node *n;
467 int hash_idx;
468 int err = 0;
469
470 read_lock(&pathtbl_resize_lock);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200471 hash_idx = mesh_table_hash(addr, sdata, mesh_paths);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100472 bucket = &mesh_paths->hash_buckets[hash_idx];
473
474 spin_lock(&mesh_paths->hashwlock[hash_idx]);
475 hlist_for_each_entry(node, n, bucket, list) {
476 mpath = node->mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200477 if (mpath->sdata == sdata &&
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100478 memcmp(addr, mpath->dst, ETH_ALEN) == 0) {
479 spin_lock_bh(&mpath->state_lock);
Luis Carlos Cobocfa22c72008-02-29 15:04:13 -0800480 mpath->flags |= MESH_PATH_RESOLVING;
481 hlist_del_rcu(&node->list);
482 call_rcu(&node->rcu, mesh_path_node_reclaim);
483 atomic_dec(&mesh_paths->entries);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100484 spin_unlock_bh(&mpath->state_lock);
485 goto enddel;
486 }
487 }
488
489 err = -ENXIO;
490enddel:
Johannes Bergf5ea9122009-08-07 16:17:38 +0200491 mesh_paths_generation++;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100492 spin_unlock(&mesh_paths->hashwlock[hash_idx]);
493 read_unlock(&pathtbl_resize_lock);
494 return err;
495}
496
497/**
498 * mesh_path_tx_pending - sends pending frames in a mesh path queue
499 *
500 * @mpath: mesh path to activate
501 *
502 * Locking: the state_lock of the mpath structure must NOT be held when calling
503 * this function.
504 */
505void mesh_path_tx_pending(struct mesh_path *mpath)
506{
Javier Cardona249b4052009-07-07 10:55:03 -0700507 if (mpath->flags & MESH_PATH_ACTIVE)
508 ieee80211_add_pending_skbs(mpath->sdata->local,
509 &mpath->frame_queue);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100510}
511
512/**
513 * mesh_path_discard_frame - discard a frame whose path could not be resolved
514 *
515 * @skb: frame to discard
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200516 * @sdata: network subif the frame was to be sent through
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100517 *
Javier Cardona35946a52009-07-13 17:00:10 -0700518 * If the frame was being forwarded from another MP, a PERR frame will be sent
519 * to the precursor. The precursor's address (i.e. the previous hop) was saved
520 * in addr1 of the frame-to-be-forwarded, and would only be overwritten once
521 * the destination is successfully resolved.
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100522 *
523 * Locking: the function must me called within a rcu_read_lock region
524 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200525void mesh_path_discard_frame(struct sk_buff *skb,
526 struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100527{
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +0200528 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100529 struct mesh_path *mpath;
530 u32 dsn = 0;
531
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200532 if (memcmp(hdr->addr4, sdata->dev->dev_addr, ETH_ALEN) != 0) {
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100533 u8 *ra, *da;
534
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +0200535 da = hdr->addr3;
Javier Cardona35946a52009-07-13 17:00:10 -0700536 ra = hdr->addr1;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200537 mpath = mesh_path_lookup(da, sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100538 if (mpath)
539 dsn = ++mpath->dsn;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200540 mesh_path_error_tx(skb->data, cpu_to_le32(dsn), ra, sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100541 }
542
543 kfree_skb(skb);
Johannes Berg472dbc42008-09-11 00:01:49 +0200544 sdata->u.mesh.mshstats.dropped_frames_no_route++;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100545}
546
547/**
548 * mesh_path_flush_pending - free the pending queue of a mesh path
549 *
550 * @mpath: mesh path whose queue has to be freed
551 *
552 * Locking: the function must me called withing a rcu_read_lock region
553 */
554void mesh_path_flush_pending(struct mesh_path *mpath)
555{
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100556 struct sk_buff *skb;
557
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100558 while ((skb = skb_dequeue(&mpath->frame_queue)) &&
559 (mpath->flags & MESH_PATH_ACTIVE))
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200560 mesh_path_discard_frame(skb, mpath->sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100561}
562
563/**
564 * mesh_path_fix_nexthop - force a specific next hop for a mesh path
565 *
566 * @mpath: the mesh path to modify
567 * @next_hop: the next hop to force
568 *
569 * Locking: this function must be called holding mpath->state_lock
570 */
571void mesh_path_fix_nexthop(struct mesh_path *mpath, struct sta_info *next_hop)
572{
573 spin_lock_bh(&mpath->state_lock);
574 mesh_path_assign_nexthop(mpath, next_hop);
575 mpath->dsn = 0xffff;
576 mpath->metric = 0;
577 mpath->hop_count = 0;
578 mpath->exp_time = 0;
579 mpath->flags |= MESH_PATH_FIXED;
580 mesh_path_activate(mpath);
581 spin_unlock_bh(&mpath->state_lock);
582 mesh_path_tx_pending(mpath);
583}
584
585static void mesh_path_node_free(struct hlist_node *p, bool free_leafs)
586{
587 struct mesh_path *mpath;
588 struct mpath_node *node = hlist_entry(p, struct mpath_node, list);
589 mpath = node->mpath;
590 hlist_del_rcu(p);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100591 if (free_leafs)
592 kfree(mpath);
593 kfree(node);
594}
595
Pavel Emelyanov4caf86c2008-05-07 19:47:01 +0400596static int mesh_path_node_copy(struct hlist_node *p, struct mesh_table *newtbl)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100597{
598 struct mesh_path *mpath;
599 struct mpath_node *node, *new_node;
600 u32 hash_idx;
601
Pavel Emelyanov8566dc32008-05-07 19:51:51 +0400602 new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
Pavel Emelyanov00242c42008-05-07 19:48:14 +0400603 if (new_node == NULL)
604 return -ENOMEM;
605
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100606 node = hlist_entry(p, struct mpath_node, list);
607 mpath = node->mpath;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100608 new_node->mpath = mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200609 hash_idx = mesh_table_hash(mpath->dst, mpath->sdata, newtbl);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100610 hlist_add_head(&new_node->list,
611 &newtbl->hash_buckets[hash_idx]);
Pavel Emelyanov4caf86c2008-05-07 19:47:01 +0400612 return 0;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100613}
614
615int mesh_pathtbl_init(void)
616{
617 mesh_paths = mesh_table_alloc(INIT_PATHS_SIZE_ORDER);
YanBo79617de2008-09-22 13:30:32 +0800618 if (!mesh_paths)
619 return -ENOMEM;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100620 mesh_paths->free_node = &mesh_path_node_free;
621 mesh_paths->copy_node = &mesh_path_node_copy;
622 mesh_paths->mean_chain_len = MEAN_CHAIN_LEN;
YanBo79617de2008-09-22 13:30:32 +0800623
624 mpp_paths = mesh_table_alloc(INIT_PATHS_SIZE_ORDER);
625 if (!mpp_paths) {
626 mesh_table_free(mesh_paths, true);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100627 return -ENOMEM;
YanBo79617de2008-09-22 13:30:32 +0800628 }
629 mpp_paths->free_node = &mesh_path_node_free;
630 mpp_paths->copy_node = &mesh_path_node_copy;
631 mpp_paths->mean_chain_len = MEAN_CHAIN_LEN;
632
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100633 return 0;
634}
635
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200636void mesh_path_expire(struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100637{
638 struct mesh_path *mpath;
639 struct mpath_node *node;
640 struct hlist_node *p;
641 int i;
642
643 read_lock(&pathtbl_resize_lock);
644 for_each_mesh_entry(mesh_paths, p, node, i) {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200645 if (node->mpath->sdata != sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100646 continue;
647 mpath = node->mpath;
648 spin_lock_bh(&mpath->state_lock);
649 if ((!(mpath->flags & MESH_PATH_RESOLVING)) &&
650 (!(mpath->flags & MESH_PATH_FIXED)) &&
651 time_after(jiffies,
652 mpath->exp_time + MESH_PATH_EXPIRE)) {
653 spin_unlock_bh(&mpath->state_lock);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200654 mesh_path_del(mpath->dst, mpath->sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100655 } else
656 spin_unlock_bh(&mpath->state_lock);
657 }
658 read_unlock(&pathtbl_resize_lock);
659}
660
661void mesh_pathtbl_unregister(void)
662{
663 mesh_table_free(mesh_paths, true);
YanBo79617de2008-09-22 13:30:32 +0800664 mesh_table_free(mpp_paths, true);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100665}