blob: 04b9e4d61b8ecbd7aa957364c2d4ec48b3af6aba [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
41/* This lock will have the grow table function as writer and add / delete nodes
42 * as readers. When reading the table (i.e. doing lookups) we are well protected
43 * by RCU
44 */
45static DEFINE_RWLOCK(pathtbl_resize_lock);
46
47/**
48 *
49 * mesh_path_assign_nexthop - update mesh path next hop
50 *
51 * @mpath: mesh path to update
52 * @sta: next hop to assign
53 *
54 * Locking: mpath->state_lock must be held when calling this function
55 */
56void mesh_path_assign_nexthop(struct mesh_path *mpath, struct sta_info *sta)
57{
Javier Cardona10c836d2009-07-09 14:42:16 -070058 struct sk_buff *skb;
59 struct ieee80211_hdr *hdr;
60 struct sk_buff_head tmpq;
61 unsigned long flags;
62
Johannes Bergd0709a62008-02-25 16:27:46 +010063 rcu_assign_pointer(mpath->next_hop, sta);
Javier Cardona10c836d2009-07-09 14:42:16 -070064
65 __skb_queue_head_init(&tmpq);
66
67 spin_lock_irqsave(&mpath->frame_queue.lock, flags);
68
69 while ((skb = __skb_dequeue(&mpath->frame_queue)) != NULL) {
70 hdr = (struct ieee80211_hdr *) skb->data;
71 memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN);
72 __skb_queue_tail(&tmpq, skb);
73 }
74
75 skb_queue_splice(&tmpq, &mpath->frame_queue);
76 spin_unlock_irqrestore(&mpath->frame_queue.lock, flags);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +010077}
78
79
80/**
81 * mesh_path_lookup - look up a path in the mesh path table
82 * @dst: hardware address (ETH_ALEN length) of destination
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +120083 * @sdata: local subif
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +010084 *
85 * Returns: pointer to the mesh path structure, or NULL if not found
86 *
87 * Locking: must be called within a read rcu section.
88 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +120089struct mesh_path *mesh_path_lookup(u8 *dst, struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +010090{
91 struct mesh_path *mpath;
92 struct hlist_node *n;
93 struct hlist_head *bucket;
94 struct mesh_table *tbl;
95 struct mpath_node *node;
96
97 tbl = rcu_dereference(mesh_paths);
98
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +120099 bucket = &tbl->hash_buckets[mesh_table_hash(dst, sdata, tbl)];
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100100 hlist_for_each_entry_rcu(node, n, bucket, list) {
101 mpath = node->mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200102 if (mpath->sdata == sdata &&
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100103 memcmp(dst, mpath->dst, ETH_ALEN) == 0) {
104 if (MPATH_EXPIRED(mpath)) {
105 spin_lock_bh(&mpath->state_lock);
106 if (MPATH_EXPIRED(mpath))
107 mpath->flags &= ~MESH_PATH_ACTIVE;
108 spin_unlock_bh(&mpath->state_lock);
109 }
110 return mpath;
111 }
112 }
113 return NULL;
114}
115
YanBo79617de2008-09-22 13:30:32 +0800116struct mesh_path *mpp_path_lookup(u8 *dst, struct ieee80211_sub_if_data *sdata)
117{
118 struct mesh_path *mpath;
119 struct hlist_node *n;
120 struct hlist_head *bucket;
121 struct mesh_table *tbl;
122 struct mpath_node *node;
123
124 tbl = rcu_dereference(mpp_paths);
125
126 bucket = &tbl->hash_buckets[mesh_table_hash(dst, sdata, tbl)];
127 hlist_for_each_entry_rcu(node, n, bucket, list) {
128 mpath = node->mpath;
129 if (mpath->sdata == sdata &&
130 memcmp(dst, mpath->dst, ETH_ALEN) == 0) {
131 if (MPATH_EXPIRED(mpath)) {
132 spin_lock_bh(&mpath->state_lock);
133 if (MPATH_EXPIRED(mpath))
134 mpath->flags &= ~MESH_PATH_ACTIVE;
135 spin_unlock_bh(&mpath->state_lock);
136 }
137 return mpath;
138 }
139 }
140 return NULL;
141}
142
143
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100144/**
145 * mesh_path_lookup_by_idx - look up a path in the mesh path table by its index
146 * @idx: index
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200147 * @sdata: local subif, or NULL for all entries
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100148 *
149 * Returns: pointer to the mesh path structure, or NULL if not found.
150 *
151 * Locking: must be called within a read rcu section.
152 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200153struct mesh_path *mesh_path_lookup_by_idx(int idx, struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100154{
155 struct mpath_node *node;
156 struct hlist_node *p;
157 int i;
158 int j = 0;
159
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800160 for_each_mesh_entry(mesh_paths, p, node, i) {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200161 if (sdata && node->mpath->sdata != sdata)
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800162 continue;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100163 if (j++ == idx) {
164 if (MPATH_EXPIRED(node->mpath)) {
165 spin_lock_bh(&node->mpath->state_lock);
166 if (MPATH_EXPIRED(node->mpath))
167 node->mpath->flags &= ~MESH_PATH_ACTIVE;
168 spin_unlock_bh(&node->mpath->state_lock);
169 }
170 return node->mpath;
171 }
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800172 }
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100173
174 return NULL;
175}
176
177/**
178 * mesh_path_add - allocate and add a new path to the mesh path table
179 * @addr: destination address of the path (ETH_ALEN length)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200180 * @sdata: local subif
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100181 *
182 * Returns: 0 on sucess
183 *
184 * State: the initial state of the new path is set to 0
185 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200186int mesh_path_add(u8 *dst, struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100187{
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100188 struct mesh_path *mpath, *new_mpath;
189 struct mpath_node *node, *new_node;
190 struct hlist_head *bucket;
191 struct hlist_node *n;
192 int grow = 0;
193 int err = 0;
194 u32 hash_idx;
195
Johannes Berge2e414d2009-07-10 11:38:14 +0200196 might_sleep();
197
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200198 if (memcmp(dst, sdata->dev->dev_addr, ETH_ALEN) == 0)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100199 /* never add ourselves as neighbours */
200 return -ENOTSUPP;
201
202 if (is_multicast_ether_addr(dst))
203 return -ENOTSUPP;
204
Johannes Berg472dbc42008-09-11 00:01:49 +0200205 if (atomic_add_unless(&sdata->u.mesh.mpaths, 1, MESH_MAX_MPATHS) == 0)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100206 return -ENOSPC;
207
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400208 err = -ENOMEM;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100209 new_mpath = kzalloc(sizeof(struct mesh_path), GFP_KERNEL);
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400210 if (!new_mpath)
211 goto err_path_alloc;
212
Pavel Emelyanov0eb03d52008-05-06 18:49:02 +0400213 new_node = kmalloc(sizeof(struct mpath_node), GFP_KERNEL);
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400214 if (!new_node)
215 goto err_node_alloc;
Pavel Emelyanovf84e71a2008-05-06 18:46:36 +0400216
217 read_lock(&pathtbl_resize_lock);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100218 memcpy(new_mpath->dst, dst, ETH_ALEN);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200219 new_mpath->sdata = sdata;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100220 new_mpath->flags = 0;
221 skb_queue_head_init(&new_mpath->frame_queue);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100222 new_node->mpath = new_mpath;
223 new_mpath->timer.data = (unsigned long) new_mpath;
224 new_mpath->timer.function = mesh_path_timer;
225 new_mpath->exp_time = jiffies;
226 spin_lock_init(&new_mpath->state_lock);
227 init_timer(&new_mpath->timer);
228
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200229 hash_idx = mesh_table_hash(dst, sdata, mesh_paths);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100230 bucket = &mesh_paths->hash_buckets[hash_idx];
231
232 spin_lock(&mesh_paths->hashwlock[hash_idx]);
233
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400234 err = -EEXIST;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100235 hlist_for_each_entry(node, n, bucket, list) {
236 mpath = node->mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200237 if (mpath->sdata == sdata && memcmp(dst, mpath->dst, ETH_ALEN) == 0)
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400238 goto err_exists;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100239 }
240
241 hlist_add_head_rcu(&new_node->list, bucket);
242 if (atomic_inc_return(&mesh_paths->entries) >=
243 mesh_paths->mean_chain_len * (mesh_paths->hash_mask + 1))
244 grow = 1;
245
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100246 spin_unlock(&mesh_paths->hashwlock[hash_idx]);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100247 read_unlock(&pathtbl_resize_lock);
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400248 if (grow) {
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100249 struct mesh_table *oldtbl, *newtbl;
250
251 write_lock(&pathtbl_resize_lock);
252 oldtbl = mesh_paths;
253 newtbl = mesh_table_grow(mesh_paths);
254 if (!newtbl) {
255 write_unlock(&pathtbl_resize_lock);
Pavel Emelyanov3282aea2008-05-07 19:54:04 +0400256 return 0;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100257 }
258 rcu_assign_pointer(mesh_paths, newtbl);
Pavel Emelyanov6d6936e2008-05-06 18:51:31 +0400259 write_unlock(&pathtbl_resize_lock);
260
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100261 synchronize_rcu();
262 mesh_table_free(oldtbl, false);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100263 }
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400264 return 0;
265
266err_exists:
267 spin_unlock(&mesh_paths->hashwlock[hash_idx]);
268 read_unlock(&pathtbl_resize_lock);
269 kfree(new_node);
270err_node_alloc:
271 kfree(new_mpath);
272err_path_alloc:
Johannes Berg472dbc42008-09-11 00:01:49 +0200273 atomic_dec(&sdata->u.mesh.mpaths);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100274 return err;
275}
276
277
YanBo79617de2008-09-22 13:30:32 +0800278int mpp_path_add(u8 *dst, u8 *mpp, struct ieee80211_sub_if_data *sdata)
279{
280 struct mesh_path *mpath, *new_mpath;
281 struct mpath_node *node, *new_node;
282 struct hlist_head *bucket;
283 struct hlist_node *n;
284 int grow = 0;
285 int err = 0;
286 u32 hash_idx;
287
Johannes Berge2e414d2009-07-10 11:38:14 +0200288 might_sleep();
YanBo79617de2008-09-22 13:30:32 +0800289
290 if (memcmp(dst, sdata->dev->dev_addr, ETH_ALEN) == 0)
291 /* never add ourselves as neighbours */
292 return -ENOTSUPP;
293
294 if (is_multicast_ether_addr(dst))
295 return -ENOTSUPP;
296
297 err = -ENOMEM;
298 new_mpath = kzalloc(sizeof(struct mesh_path), GFP_KERNEL);
299 if (!new_mpath)
300 goto err_path_alloc;
301
302 new_node = kmalloc(sizeof(struct mpath_node), GFP_KERNEL);
303 if (!new_node)
304 goto err_node_alloc;
305
306 read_lock(&pathtbl_resize_lock);
307 memcpy(new_mpath->dst, dst, ETH_ALEN);
308 memcpy(new_mpath->mpp, mpp, ETH_ALEN);
309 new_mpath->sdata = sdata;
310 new_mpath->flags = 0;
311 skb_queue_head_init(&new_mpath->frame_queue);
312 new_node->mpath = new_mpath;
313 new_mpath->exp_time = jiffies;
314 spin_lock_init(&new_mpath->state_lock);
315
316 hash_idx = mesh_table_hash(dst, sdata, mpp_paths);
317 bucket = &mpp_paths->hash_buckets[hash_idx];
318
319 spin_lock(&mpp_paths->hashwlock[hash_idx]);
320
321 err = -EEXIST;
322 hlist_for_each_entry(node, n, bucket, list) {
323 mpath = node->mpath;
324 if (mpath->sdata == sdata && memcmp(dst, mpath->dst, ETH_ALEN) == 0)
325 goto err_exists;
326 }
327
328 hlist_add_head_rcu(&new_node->list, bucket);
329 if (atomic_inc_return(&mpp_paths->entries) >=
330 mpp_paths->mean_chain_len * (mpp_paths->hash_mask + 1))
331 grow = 1;
332
333 spin_unlock(&mpp_paths->hashwlock[hash_idx]);
334 read_unlock(&pathtbl_resize_lock);
335 if (grow) {
336 struct mesh_table *oldtbl, *newtbl;
337
338 write_lock(&pathtbl_resize_lock);
339 oldtbl = mpp_paths;
340 newtbl = mesh_table_grow(mpp_paths);
341 if (!newtbl) {
342 write_unlock(&pathtbl_resize_lock);
343 return 0;
344 }
345 rcu_assign_pointer(mpp_paths, newtbl);
346 write_unlock(&pathtbl_resize_lock);
347
348 synchronize_rcu();
349 mesh_table_free(oldtbl, false);
350 }
351 return 0;
352
353err_exists:
354 spin_unlock(&mpp_paths->hashwlock[hash_idx]);
355 read_unlock(&pathtbl_resize_lock);
356 kfree(new_node);
357err_node_alloc:
358 kfree(new_mpath);
359err_path_alloc:
360 return err;
361}
362
363
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100364/**
365 * mesh_plink_broken - deactivates paths and sends perr when a link breaks
366 *
367 * @sta: broken peer link
368 *
369 * This function must be called from the rate control algorithm if enough
370 * delivery errors suggest that a peer link is no longer usable.
371 */
372void mesh_plink_broken(struct sta_info *sta)
373{
374 struct mesh_path *mpath;
375 struct mpath_node *node;
376 struct hlist_node *p;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200377 struct ieee80211_sub_if_data *sdata = sta->sdata;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100378 int i;
379
380 rcu_read_lock();
381 for_each_mesh_entry(mesh_paths, p, node, i) {
382 mpath = node->mpath;
383 spin_lock_bh(&mpath->state_lock);
384 if (mpath->next_hop == sta &&
385 mpath->flags & MESH_PATH_ACTIVE &&
386 !(mpath->flags & MESH_PATH_FIXED)) {
387 mpath->flags &= ~MESH_PATH_ACTIVE;
388 ++mpath->dsn;
389 spin_unlock_bh(&mpath->state_lock);
390 mesh_path_error_tx(mpath->dst,
391 cpu_to_le32(mpath->dsn),
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200392 sdata->dev->broadcast, sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100393 } else
394 spin_unlock_bh(&mpath->state_lock);
395 }
396 rcu_read_unlock();
397}
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100398
399/**
400 * mesh_path_flush_by_nexthop - Deletes mesh paths if their next hop matches
401 *
402 * @sta - mesh peer to match
403 *
Luis Carlos Cobob4e08ea2008-02-29 15:46:08 -0800404 * RCU notes: this function is called when a mesh plink transitions from
405 * PLINK_ESTAB to any other state, since PLINK_ESTAB state is the only one that
406 * allows path creation. This will happen before the sta can be freed (because
Johannes Bergd0709a62008-02-25 16:27:46 +0100407 * sta_info_destroy() calls this) so any reader in a rcu read block will be
408 * protected against the plink disappearing.
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100409 */
410void mesh_path_flush_by_nexthop(struct sta_info *sta)
411{
412 struct mesh_path *mpath;
413 struct mpath_node *node;
414 struct hlist_node *p;
415 int i;
416
417 for_each_mesh_entry(mesh_paths, p, node, i) {
418 mpath = node->mpath;
419 if (mpath->next_hop == sta)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200420 mesh_path_del(mpath->dst, mpath->sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100421 }
422}
423
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200424void mesh_path_flush(struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100425{
426 struct mesh_path *mpath;
427 struct mpath_node *node;
428 struct hlist_node *p;
429 int i;
430
431 for_each_mesh_entry(mesh_paths, p, node, i) {
432 mpath = node->mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200433 if (mpath->sdata == sdata)
434 mesh_path_del(mpath->dst, mpath->sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100435 }
436}
437
438static void mesh_path_node_reclaim(struct rcu_head *rp)
439{
440 struct mpath_node *node = container_of(rp, struct mpath_node, rcu);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200441 struct ieee80211_sub_if_data *sdata = node->mpath->sdata;
Johannes Bergd0709a62008-02-25 16:27:46 +0100442
Luis Carlos Cobo89a1ad62008-02-29 14:49:37 -0800443 del_timer_sync(&node->mpath->timer);
Johannes Berg472dbc42008-09-11 00:01:49 +0200444 atomic_dec(&sdata->u.mesh.mpaths);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100445 kfree(node->mpath);
446 kfree(node);
447}
448
449/**
450 * mesh_path_del - delete a mesh path from the table
451 *
452 * @addr: dst address (ETH_ALEN length)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200453 * @sdata: local subif
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100454 *
455 * Returns: 0 if succesful
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100456 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200457int mesh_path_del(u8 *addr, struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100458{
459 struct mesh_path *mpath;
460 struct mpath_node *node;
461 struct hlist_head *bucket;
462 struct hlist_node *n;
463 int hash_idx;
464 int err = 0;
465
466 read_lock(&pathtbl_resize_lock);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200467 hash_idx = mesh_table_hash(addr, sdata, mesh_paths);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100468 bucket = &mesh_paths->hash_buckets[hash_idx];
469
470 spin_lock(&mesh_paths->hashwlock[hash_idx]);
471 hlist_for_each_entry(node, n, bucket, list) {
472 mpath = node->mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200473 if (mpath->sdata == sdata &&
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100474 memcmp(addr, mpath->dst, ETH_ALEN) == 0) {
475 spin_lock_bh(&mpath->state_lock);
Luis Carlos Cobocfa22c72008-02-29 15:04:13 -0800476 mpath->flags |= MESH_PATH_RESOLVING;
477 hlist_del_rcu(&node->list);
478 call_rcu(&node->rcu, mesh_path_node_reclaim);
479 atomic_dec(&mesh_paths->entries);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100480 spin_unlock_bh(&mpath->state_lock);
481 goto enddel;
482 }
483 }
484
485 err = -ENXIO;
486enddel:
487 spin_unlock(&mesh_paths->hashwlock[hash_idx]);
488 read_unlock(&pathtbl_resize_lock);
489 return err;
490}
491
492/**
493 * mesh_path_tx_pending - sends pending frames in a mesh path queue
494 *
495 * @mpath: mesh path to activate
496 *
497 * Locking: the state_lock of the mpath structure must NOT be held when calling
498 * this function.
499 */
500void mesh_path_tx_pending(struct mesh_path *mpath)
501{
Javier Cardona249b4052009-07-07 10:55:03 -0700502 if (mpath->flags & MESH_PATH_ACTIVE)
503 ieee80211_add_pending_skbs(mpath->sdata->local,
504 &mpath->frame_queue);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100505}
506
507/**
508 * mesh_path_discard_frame - discard a frame whose path could not be resolved
509 *
510 * @skb: frame to discard
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200511 * @sdata: network subif the frame was to be sent through
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100512 *
Javier Cardona35946a52009-07-13 17:00:10 -0700513 * If the frame was being forwarded from another MP, a PERR frame will be sent
514 * to the precursor. The precursor's address (i.e. the previous hop) was saved
515 * in addr1 of the frame-to-be-forwarded, and would only be overwritten once
516 * the destination is successfully resolved.
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100517 *
518 * Locking: the function must me called within a rcu_read_lock region
519 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200520void mesh_path_discard_frame(struct sk_buff *skb,
521 struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100522{
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +0200523 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100524 struct mesh_path *mpath;
525 u32 dsn = 0;
526
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200527 if (memcmp(hdr->addr4, sdata->dev->dev_addr, ETH_ALEN) != 0) {
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100528 u8 *ra, *da;
529
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +0200530 da = hdr->addr3;
Javier Cardona35946a52009-07-13 17:00:10 -0700531 ra = hdr->addr1;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200532 mpath = mesh_path_lookup(da, sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100533 if (mpath)
534 dsn = ++mpath->dsn;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200535 mesh_path_error_tx(skb->data, cpu_to_le32(dsn), ra, sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100536 }
537
538 kfree_skb(skb);
Johannes Berg472dbc42008-09-11 00:01:49 +0200539 sdata->u.mesh.mshstats.dropped_frames_no_route++;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100540}
541
542/**
543 * mesh_path_flush_pending - free the pending queue of a mesh path
544 *
545 * @mpath: mesh path whose queue has to be freed
546 *
547 * Locking: the function must me called withing a rcu_read_lock region
548 */
549void mesh_path_flush_pending(struct mesh_path *mpath)
550{
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100551 struct sk_buff *skb;
552
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100553 while ((skb = skb_dequeue(&mpath->frame_queue)) &&
554 (mpath->flags & MESH_PATH_ACTIVE))
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200555 mesh_path_discard_frame(skb, mpath->sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100556}
557
558/**
559 * mesh_path_fix_nexthop - force a specific next hop for a mesh path
560 *
561 * @mpath: the mesh path to modify
562 * @next_hop: the next hop to force
563 *
564 * Locking: this function must be called holding mpath->state_lock
565 */
566void mesh_path_fix_nexthop(struct mesh_path *mpath, struct sta_info *next_hop)
567{
568 spin_lock_bh(&mpath->state_lock);
569 mesh_path_assign_nexthop(mpath, next_hop);
570 mpath->dsn = 0xffff;
571 mpath->metric = 0;
572 mpath->hop_count = 0;
573 mpath->exp_time = 0;
574 mpath->flags |= MESH_PATH_FIXED;
575 mesh_path_activate(mpath);
576 spin_unlock_bh(&mpath->state_lock);
577 mesh_path_tx_pending(mpath);
578}
579
580static void mesh_path_node_free(struct hlist_node *p, bool free_leafs)
581{
582 struct mesh_path *mpath;
583 struct mpath_node *node = hlist_entry(p, struct mpath_node, list);
584 mpath = node->mpath;
585 hlist_del_rcu(p);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100586 if (free_leafs)
587 kfree(mpath);
588 kfree(node);
589}
590
Pavel Emelyanov4caf86c2008-05-07 19:47:01 +0400591static int mesh_path_node_copy(struct hlist_node *p, struct mesh_table *newtbl)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100592{
593 struct mesh_path *mpath;
594 struct mpath_node *node, *new_node;
595 u32 hash_idx;
596
Pavel Emelyanov8566dc32008-05-07 19:51:51 +0400597 new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
Pavel Emelyanov00242c42008-05-07 19:48:14 +0400598 if (new_node == NULL)
599 return -ENOMEM;
600
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100601 node = hlist_entry(p, struct mpath_node, list);
602 mpath = node->mpath;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100603 new_node->mpath = mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200604 hash_idx = mesh_table_hash(mpath->dst, mpath->sdata, newtbl);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100605 hlist_add_head(&new_node->list,
606 &newtbl->hash_buckets[hash_idx]);
Pavel Emelyanov4caf86c2008-05-07 19:47:01 +0400607 return 0;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100608}
609
610int mesh_pathtbl_init(void)
611{
612 mesh_paths = mesh_table_alloc(INIT_PATHS_SIZE_ORDER);
YanBo79617de2008-09-22 13:30:32 +0800613 if (!mesh_paths)
614 return -ENOMEM;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100615 mesh_paths->free_node = &mesh_path_node_free;
616 mesh_paths->copy_node = &mesh_path_node_copy;
617 mesh_paths->mean_chain_len = MEAN_CHAIN_LEN;
YanBo79617de2008-09-22 13:30:32 +0800618
619 mpp_paths = mesh_table_alloc(INIT_PATHS_SIZE_ORDER);
620 if (!mpp_paths) {
621 mesh_table_free(mesh_paths, true);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100622 return -ENOMEM;
YanBo79617de2008-09-22 13:30:32 +0800623 }
624 mpp_paths->free_node = &mesh_path_node_free;
625 mpp_paths->copy_node = &mesh_path_node_copy;
626 mpp_paths->mean_chain_len = MEAN_CHAIN_LEN;
627
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100628 return 0;
629}
630
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200631void mesh_path_expire(struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100632{
633 struct mesh_path *mpath;
634 struct mpath_node *node;
635 struct hlist_node *p;
636 int i;
637
638 read_lock(&pathtbl_resize_lock);
639 for_each_mesh_entry(mesh_paths, p, node, i) {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200640 if (node->mpath->sdata != sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100641 continue;
642 mpath = node->mpath;
643 spin_lock_bh(&mpath->state_lock);
644 if ((!(mpath->flags & MESH_PATH_RESOLVING)) &&
645 (!(mpath->flags & MESH_PATH_FIXED)) &&
646 time_after(jiffies,
647 mpath->exp_time + MESH_PATH_EXPIRE)) {
648 spin_unlock_bh(&mpath->state_lock);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200649 mesh_path_del(mpath->dst, mpath->sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100650 } else
651 spin_unlock_bh(&mpath->state_lock);
652 }
653 read_unlock(&pathtbl_resize_lock);
654}
655
656void mesh_pathtbl_unregister(void)
657{
658 mesh_table_free(mesh_paths, true);
YanBo79617de2008-09-22 13:30:32 +0800659 mesh_table_free(mpp_paths, true);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100660}