blob: 5d541235ca2ffae0b129336e64fe3bd80da9aaf9 [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;
Javier Cardona18889232009-08-10 12:15:52 -070042static void __mesh_table_free(struct mesh_table *tbl)
43{
44 kfree(tbl->hash_buckets);
45 kfree(tbl->hashwlock);
46 kfree(tbl);
47}
48
49void mesh_table_free(struct mesh_table *tbl, bool free_leafs)
50{
51 struct hlist_head *mesh_hash;
52 struct hlist_node *p, *q;
53 int i;
54
55 mesh_hash = tbl->hash_buckets;
56 for (i = 0; i <= tbl->hash_mask; i++) {
57 spin_lock(&tbl->hashwlock[i]);
58 hlist_for_each_safe(p, q, &mesh_hash[i]) {
59 tbl->free_node(p, free_leafs);
60 atomic_dec(&tbl->entries);
61 }
62 spin_unlock(&tbl->hashwlock[i]);
63 }
64 __mesh_table_free(tbl);
65}
66
67static struct mesh_table *mesh_table_grow(struct mesh_table *tbl)
68{
69 struct mesh_table *newtbl;
70 struct hlist_head *oldhash;
71 struct hlist_node *p, *q;
72 int i;
73
74 if (atomic_read(&tbl->entries)
75 < tbl->mean_chain_len * (tbl->hash_mask + 1))
76 goto endgrow;
77
78 newtbl = mesh_table_alloc(tbl->size_order + 1);
79 if (!newtbl)
80 goto endgrow;
81
82 newtbl->free_node = tbl->free_node;
83 newtbl->mean_chain_len = tbl->mean_chain_len;
84 newtbl->copy_node = tbl->copy_node;
85 atomic_set(&newtbl->entries, atomic_read(&tbl->entries));
86
87 oldhash = tbl->hash_buckets;
88 for (i = 0; i <= tbl->hash_mask; i++)
89 hlist_for_each(p, &oldhash[i])
90 if (tbl->copy_node(p, newtbl) < 0)
91 goto errcopy;
92
93 return newtbl;
94
95errcopy:
96 for (i = 0; i <= newtbl->hash_mask; i++) {
97 hlist_for_each_safe(p, q, &newtbl->hash_buckets[i])
98 tbl->free_node(p, 0);
99 }
100 __mesh_table_free(newtbl);
101endgrow:
102 return NULL;
103}
104
Johannes Bergf5ea9122009-08-07 16:17:38 +0200105
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100106/* This lock will have the grow table function as writer and add / delete nodes
107 * as readers. When reading the table (i.e. doing lookups) we are well protected
108 * by RCU
109 */
110static DEFINE_RWLOCK(pathtbl_resize_lock);
111
112/**
113 *
114 * mesh_path_assign_nexthop - update mesh path next hop
115 *
116 * @mpath: mesh path to update
117 * @sta: next hop to assign
118 *
119 * Locking: mpath->state_lock must be held when calling this function
120 */
121void mesh_path_assign_nexthop(struct mesh_path *mpath, struct sta_info *sta)
122{
Javier Cardona10c836d2009-07-09 14:42:16 -0700123 struct sk_buff *skb;
124 struct ieee80211_hdr *hdr;
125 struct sk_buff_head tmpq;
126 unsigned long flags;
127
Johannes Bergd0709a62008-02-25 16:27:46 +0100128 rcu_assign_pointer(mpath->next_hop, sta);
Javier Cardona10c836d2009-07-09 14:42:16 -0700129
130 __skb_queue_head_init(&tmpq);
131
132 spin_lock_irqsave(&mpath->frame_queue.lock, flags);
133
134 while ((skb = __skb_dequeue(&mpath->frame_queue)) != NULL) {
135 hdr = (struct ieee80211_hdr *) skb->data;
136 memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN);
137 __skb_queue_tail(&tmpq, skb);
138 }
139
140 skb_queue_splice(&tmpq, &mpath->frame_queue);
141 spin_unlock_irqrestore(&mpath->frame_queue.lock, flags);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100142}
143
144
145/**
146 * mesh_path_lookup - look up a path in the mesh path table
147 * @dst: hardware address (ETH_ALEN length) of destination
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200148 * @sdata: local subif
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100149 *
150 * Returns: pointer to the mesh path structure, or NULL if not found
151 *
152 * Locking: must be called within a read rcu section.
153 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200154struct mesh_path *mesh_path_lookup(u8 *dst, struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100155{
156 struct mesh_path *mpath;
157 struct hlist_node *n;
158 struct hlist_head *bucket;
159 struct mesh_table *tbl;
160 struct mpath_node *node;
161
162 tbl = rcu_dereference(mesh_paths);
163
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200164 bucket = &tbl->hash_buckets[mesh_table_hash(dst, sdata, tbl)];
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100165 hlist_for_each_entry_rcu(node, n, bucket, list) {
166 mpath = node->mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200167 if (mpath->sdata == sdata &&
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100168 memcmp(dst, mpath->dst, ETH_ALEN) == 0) {
169 if (MPATH_EXPIRED(mpath)) {
170 spin_lock_bh(&mpath->state_lock);
171 if (MPATH_EXPIRED(mpath))
172 mpath->flags &= ~MESH_PATH_ACTIVE;
173 spin_unlock_bh(&mpath->state_lock);
174 }
175 return mpath;
176 }
177 }
178 return NULL;
179}
180
YanBo79617de2008-09-22 13:30:32 +0800181struct mesh_path *mpp_path_lookup(u8 *dst, struct ieee80211_sub_if_data *sdata)
182{
183 struct mesh_path *mpath;
184 struct hlist_node *n;
185 struct hlist_head *bucket;
186 struct mesh_table *tbl;
187 struct mpath_node *node;
188
189 tbl = rcu_dereference(mpp_paths);
190
191 bucket = &tbl->hash_buckets[mesh_table_hash(dst, sdata, tbl)];
192 hlist_for_each_entry_rcu(node, n, bucket, list) {
193 mpath = node->mpath;
194 if (mpath->sdata == sdata &&
195 memcmp(dst, mpath->dst, ETH_ALEN) == 0) {
196 if (MPATH_EXPIRED(mpath)) {
197 spin_lock_bh(&mpath->state_lock);
198 if (MPATH_EXPIRED(mpath))
199 mpath->flags &= ~MESH_PATH_ACTIVE;
200 spin_unlock_bh(&mpath->state_lock);
201 }
202 return mpath;
203 }
204 }
205 return NULL;
206}
207
208
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100209/**
210 * mesh_path_lookup_by_idx - look up a path in the mesh path table by its index
211 * @idx: index
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200212 * @sdata: local subif, or NULL for all entries
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100213 *
214 * Returns: pointer to the mesh path structure, or NULL if not found.
215 *
216 * Locking: must be called within a read rcu section.
217 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200218struct mesh_path *mesh_path_lookup_by_idx(int idx, struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100219{
220 struct mpath_node *node;
221 struct hlist_node *p;
222 int i;
223 int j = 0;
224
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800225 for_each_mesh_entry(mesh_paths, p, node, i) {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200226 if (sdata && node->mpath->sdata != sdata)
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800227 continue;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100228 if (j++ == idx) {
229 if (MPATH_EXPIRED(node->mpath)) {
230 spin_lock_bh(&node->mpath->state_lock);
231 if (MPATH_EXPIRED(node->mpath))
232 node->mpath->flags &= ~MESH_PATH_ACTIVE;
233 spin_unlock_bh(&node->mpath->state_lock);
234 }
235 return node->mpath;
236 }
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800237 }
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100238
239 return NULL;
240}
241
242/**
243 * mesh_path_add - allocate and add a new path to the mesh path table
244 * @addr: destination address of the path (ETH_ALEN length)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200245 * @sdata: local subif
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100246 *
247 * Returns: 0 on sucess
248 *
249 * State: the initial state of the new path is set to 0
250 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200251int mesh_path_add(u8 *dst, struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100252{
Javier Cardona18889232009-08-10 12:15:52 -0700253 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
254 struct ieee80211_local *local = sdata->local;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100255 struct mesh_path *mpath, *new_mpath;
256 struct mpath_node *node, *new_node;
257 struct hlist_head *bucket;
258 struct hlist_node *n;
259 int grow = 0;
260 int err = 0;
261 u32 hash_idx;
262
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200263 if (memcmp(dst, sdata->dev->dev_addr, ETH_ALEN) == 0)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100264 /* never add ourselves as neighbours */
265 return -ENOTSUPP;
266
267 if (is_multicast_ether_addr(dst))
268 return -ENOTSUPP;
269
Johannes Berg472dbc42008-09-11 00:01:49 +0200270 if (atomic_add_unless(&sdata->u.mesh.mpaths, 1, MESH_MAX_MPATHS) == 0)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100271 return -ENOSPC;
272
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400273 err = -ENOMEM;
Javier Cardona18889232009-08-10 12:15:52 -0700274 new_mpath = kzalloc(sizeof(struct mesh_path), GFP_ATOMIC);
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400275 if (!new_mpath)
276 goto err_path_alloc;
277
Javier Cardona18889232009-08-10 12:15:52 -0700278 new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400279 if (!new_node)
280 goto err_node_alloc;
Pavel Emelyanovf84e71a2008-05-06 18:46:36 +0400281
282 read_lock(&pathtbl_resize_lock);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100283 memcpy(new_mpath->dst, dst, ETH_ALEN);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200284 new_mpath->sdata = sdata;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100285 new_mpath->flags = 0;
286 skb_queue_head_init(&new_mpath->frame_queue);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100287 new_node->mpath = new_mpath;
288 new_mpath->timer.data = (unsigned long) new_mpath;
289 new_mpath->timer.function = mesh_path_timer;
290 new_mpath->exp_time = jiffies;
291 spin_lock_init(&new_mpath->state_lock);
292 init_timer(&new_mpath->timer);
293
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200294 hash_idx = mesh_table_hash(dst, sdata, mesh_paths);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100295 bucket = &mesh_paths->hash_buckets[hash_idx];
296
297 spin_lock(&mesh_paths->hashwlock[hash_idx]);
298
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400299 err = -EEXIST;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100300 hlist_for_each_entry(node, n, bucket, list) {
301 mpath = node->mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200302 if (mpath->sdata == sdata && memcmp(dst, mpath->dst, ETH_ALEN) == 0)
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400303 goto err_exists;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100304 }
305
306 hlist_add_head_rcu(&new_node->list, bucket);
307 if (atomic_inc_return(&mesh_paths->entries) >=
308 mesh_paths->mean_chain_len * (mesh_paths->hash_mask + 1))
309 grow = 1;
310
Johannes Bergf5ea9122009-08-07 16:17:38 +0200311 mesh_paths_generation++;
312
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100313 spin_unlock(&mesh_paths->hashwlock[hash_idx]);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100314 read_unlock(&pathtbl_resize_lock);
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400315 if (grow) {
Javier Cardona18889232009-08-10 12:15:52 -0700316 set_bit(MESH_WORK_GROW_MPATH_TABLE, &ifmsh->wrkq_flags);
317 ieee80211_queue_work(&local->hw, &ifmsh->work);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100318 }
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400319 return 0;
320
321err_exists:
322 spin_unlock(&mesh_paths->hashwlock[hash_idx]);
323 read_unlock(&pathtbl_resize_lock);
324 kfree(new_node);
325err_node_alloc:
326 kfree(new_mpath);
327err_path_alloc:
Johannes Berg472dbc42008-09-11 00:01:49 +0200328 atomic_dec(&sdata->u.mesh.mpaths);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100329 return err;
330}
331
Javier Cardona18889232009-08-10 12:15:52 -0700332void mesh_mpath_table_grow(void)
333{
334 struct mesh_table *oldtbl, *newtbl;
335
336 write_lock(&pathtbl_resize_lock);
337 oldtbl = mesh_paths;
338 newtbl = mesh_table_grow(mesh_paths);
339 if (!newtbl) {
340 write_unlock(&pathtbl_resize_lock);
341 return;
342 }
343 rcu_assign_pointer(mesh_paths, newtbl);
344 write_unlock(&pathtbl_resize_lock);
345
346 synchronize_rcu();
347 mesh_table_free(oldtbl, false);
348}
349
350void mesh_mpp_table_grow(void)
351{
352 struct mesh_table *oldtbl, *newtbl;
353
354 write_lock(&pathtbl_resize_lock);
355 oldtbl = mpp_paths;
356 newtbl = mesh_table_grow(mpp_paths);
357 if (!newtbl) {
358 write_unlock(&pathtbl_resize_lock);
359 return;
360 }
361 rcu_assign_pointer(mpp_paths, newtbl);
362 write_unlock(&pathtbl_resize_lock);
363
364 synchronize_rcu();
365 mesh_table_free(oldtbl, false);
366}
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100367
YanBo79617de2008-09-22 13:30:32 +0800368int mpp_path_add(u8 *dst, u8 *mpp, struct ieee80211_sub_if_data *sdata)
369{
Javier Cardona18889232009-08-10 12:15:52 -0700370 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
371 struct ieee80211_local *local = sdata->local;
YanBo79617de2008-09-22 13:30:32 +0800372 struct mesh_path *mpath, *new_mpath;
373 struct mpath_node *node, *new_node;
374 struct hlist_head *bucket;
375 struct hlist_node *n;
376 int grow = 0;
377 int err = 0;
378 u32 hash_idx;
379
YanBo79617de2008-09-22 13:30:32 +0800380 if (memcmp(dst, sdata->dev->dev_addr, ETH_ALEN) == 0)
381 /* never add ourselves as neighbours */
382 return -ENOTSUPP;
383
384 if (is_multicast_ether_addr(dst))
385 return -ENOTSUPP;
386
387 err = -ENOMEM;
Javier Cardona18889232009-08-10 12:15:52 -0700388 new_mpath = kzalloc(sizeof(struct mesh_path), GFP_ATOMIC);
YanBo79617de2008-09-22 13:30:32 +0800389 if (!new_mpath)
390 goto err_path_alloc;
391
Javier Cardona18889232009-08-10 12:15:52 -0700392 new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
YanBo79617de2008-09-22 13:30:32 +0800393 if (!new_node)
394 goto err_node_alloc;
395
396 read_lock(&pathtbl_resize_lock);
397 memcpy(new_mpath->dst, dst, ETH_ALEN);
398 memcpy(new_mpath->mpp, mpp, ETH_ALEN);
399 new_mpath->sdata = sdata;
400 new_mpath->flags = 0;
401 skb_queue_head_init(&new_mpath->frame_queue);
402 new_node->mpath = new_mpath;
403 new_mpath->exp_time = jiffies;
404 spin_lock_init(&new_mpath->state_lock);
405
406 hash_idx = mesh_table_hash(dst, sdata, mpp_paths);
407 bucket = &mpp_paths->hash_buckets[hash_idx];
408
409 spin_lock(&mpp_paths->hashwlock[hash_idx]);
410
411 err = -EEXIST;
412 hlist_for_each_entry(node, n, bucket, list) {
413 mpath = node->mpath;
414 if (mpath->sdata == sdata && memcmp(dst, mpath->dst, ETH_ALEN) == 0)
415 goto err_exists;
416 }
417
418 hlist_add_head_rcu(&new_node->list, bucket);
419 if (atomic_inc_return(&mpp_paths->entries) >=
420 mpp_paths->mean_chain_len * (mpp_paths->hash_mask + 1))
421 grow = 1;
422
423 spin_unlock(&mpp_paths->hashwlock[hash_idx]);
424 read_unlock(&pathtbl_resize_lock);
425 if (grow) {
Javier Cardona18889232009-08-10 12:15:52 -0700426 set_bit(MESH_WORK_GROW_MPP_TABLE, &ifmsh->wrkq_flags);
427 ieee80211_queue_work(&local->hw, &ifmsh->work);
YanBo79617de2008-09-22 13:30:32 +0800428 }
429 return 0;
430
431err_exists:
432 spin_unlock(&mpp_paths->hashwlock[hash_idx]);
433 read_unlock(&pathtbl_resize_lock);
434 kfree(new_node);
435err_node_alloc:
436 kfree(new_mpath);
437err_path_alloc:
438 return err;
439}
440
441
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100442/**
443 * mesh_plink_broken - deactivates paths and sends perr when a link breaks
444 *
445 * @sta: broken peer link
446 *
447 * This function must be called from the rate control algorithm if enough
448 * delivery errors suggest that a peer link is no longer usable.
449 */
450void mesh_plink_broken(struct sta_info *sta)
451{
452 struct mesh_path *mpath;
453 struct mpath_node *node;
454 struct hlist_node *p;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200455 struct ieee80211_sub_if_data *sdata = sta->sdata;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100456 int i;
457
458 rcu_read_lock();
459 for_each_mesh_entry(mesh_paths, p, node, i) {
460 mpath = node->mpath;
461 spin_lock_bh(&mpath->state_lock);
462 if (mpath->next_hop == sta &&
463 mpath->flags & MESH_PATH_ACTIVE &&
464 !(mpath->flags & MESH_PATH_FIXED)) {
465 mpath->flags &= ~MESH_PATH_ACTIVE;
466 ++mpath->dsn;
467 spin_unlock_bh(&mpath->state_lock);
Rui Paulod611f062009-11-09 23:46:50 +0000468 mesh_path_error_tx(MESH_TTL, mpath->dst,
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100469 cpu_to_le32(mpath->dsn),
Rui Paulod611f062009-11-09 23:46:50 +0000470 PERR_RCODE_DEST_UNREACH,
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200471 sdata->dev->broadcast, sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100472 } else
473 spin_unlock_bh(&mpath->state_lock);
474 }
475 rcu_read_unlock();
476}
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100477
478/**
479 * mesh_path_flush_by_nexthop - Deletes mesh paths if their next hop matches
480 *
481 * @sta - mesh peer to match
482 *
Luis Carlos Cobob4e08ea2008-02-29 15:46:08 -0800483 * RCU notes: this function is called when a mesh plink transitions from
484 * PLINK_ESTAB to any other state, since PLINK_ESTAB state is the only one that
485 * allows path creation. This will happen before the sta can be freed (because
Johannes Bergd0709a62008-02-25 16:27:46 +0100486 * sta_info_destroy() calls this) so any reader in a rcu read block will be
487 * protected against the plink disappearing.
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100488 */
489void mesh_path_flush_by_nexthop(struct sta_info *sta)
490{
491 struct mesh_path *mpath;
492 struct mpath_node *node;
493 struct hlist_node *p;
494 int i;
495
496 for_each_mesh_entry(mesh_paths, p, node, i) {
497 mpath = node->mpath;
498 if (mpath->next_hop == sta)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200499 mesh_path_del(mpath->dst, mpath->sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100500 }
501}
502
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200503void mesh_path_flush(struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100504{
505 struct mesh_path *mpath;
506 struct mpath_node *node;
507 struct hlist_node *p;
508 int i;
509
510 for_each_mesh_entry(mesh_paths, p, node, i) {
511 mpath = node->mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200512 if (mpath->sdata == sdata)
513 mesh_path_del(mpath->dst, mpath->sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100514 }
515}
516
517static void mesh_path_node_reclaim(struct rcu_head *rp)
518{
519 struct mpath_node *node = container_of(rp, struct mpath_node, rcu);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200520 struct ieee80211_sub_if_data *sdata = node->mpath->sdata;
Johannes Bergd0709a62008-02-25 16:27:46 +0100521
Luis Carlos Cobo89a1ad62008-02-29 14:49:37 -0800522 del_timer_sync(&node->mpath->timer);
Johannes Berg472dbc42008-09-11 00:01:49 +0200523 atomic_dec(&sdata->u.mesh.mpaths);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100524 kfree(node->mpath);
525 kfree(node);
526}
527
528/**
529 * mesh_path_del - delete a mesh path from the table
530 *
531 * @addr: dst address (ETH_ALEN length)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200532 * @sdata: local subif
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100533 *
534 * Returns: 0 if succesful
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100535 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200536int mesh_path_del(u8 *addr, struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100537{
538 struct mesh_path *mpath;
539 struct mpath_node *node;
540 struct hlist_head *bucket;
541 struct hlist_node *n;
542 int hash_idx;
543 int err = 0;
544
545 read_lock(&pathtbl_resize_lock);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200546 hash_idx = mesh_table_hash(addr, sdata, mesh_paths);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100547 bucket = &mesh_paths->hash_buckets[hash_idx];
548
549 spin_lock(&mesh_paths->hashwlock[hash_idx]);
550 hlist_for_each_entry(node, n, bucket, list) {
551 mpath = node->mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200552 if (mpath->sdata == sdata &&
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100553 memcmp(addr, mpath->dst, ETH_ALEN) == 0) {
554 spin_lock_bh(&mpath->state_lock);
Luis Carlos Cobocfa22c72008-02-29 15:04:13 -0800555 mpath->flags |= MESH_PATH_RESOLVING;
556 hlist_del_rcu(&node->list);
557 call_rcu(&node->rcu, mesh_path_node_reclaim);
558 atomic_dec(&mesh_paths->entries);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100559 spin_unlock_bh(&mpath->state_lock);
560 goto enddel;
561 }
562 }
563
564 err = -ENXIO;
565enddel:
Johannes Bergf5ea9122009-08-07 16:17:38 +0200566 mesh_paths_generation++;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100567 spin_unlock(&mesh_paths->hashwlock[hash_idx]);
568 read_unlock(&pathtbl_resize_lock);
569 return err;
570}
571
572/**
573 * mesh_path_tx_pending - sends pending frames in a mesh path queue
574 *
575 * @mpath: mesh path to activate
576 *
577 * Locking: the state_lock of the mpath structure must NOT be held when calling
578 * this function.
579 */
580void mesh_path_tx_pending(struct mesh_path *mpath)
581{
Javier Cardona249b4052009-07-07 10:55:03 -0700582 if (mpath->flags & MESH_PATH_ACTIVE)
583 ieee80211_add_pending_skbs(mpath->sdata->local,
584 &mpath->frame_queue);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100585}
586
587/**
588 * mesh_path_discard_frame - discard a frame whose path could not be resolved
589 *
590 * @skb: frame to discard
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200591 * @sdata: network subif the frame was to be sent through
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100592 *
Javier Cardona35946a52009-07-13 17:00:10 -0700593 * If the frame was being forwarded from another MP, a PERR frame will be sent
594 * to the precursor. The precursor's address (i.e. the previous hop) was saved
595 * in addr1 of the frame-to-be-forwarded, and would only be overwritten once
596 * the destination is successfully resolved.
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100597 *
598 * Locking: the function must me called within a rcu_read_lock region
599 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200600void mesh_path_discard_frame(struct sk_buff *skb,
601 struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100602{
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +0200603 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100604 struct mesh_path *mpath;
605 u32 dsn = 0;
606
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200607 if (memcmp(hdr->addr4, sdata->dev->dev_addr, ETH_ALEN) != 0) {
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100608 u8 *ra, *da;
609
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +0200610 da = hdr->addr3;
Javier Cardona35946a52009-07-13 17:00:10 -0700611 ra = hdr->addr1;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200612 mpath = mesh_path_lookup(da, sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100613 if (mpath)
614 dsn = ++mpath->dsn;
Rui Paulod611f062009-11-09 23:46:50 +0000615 mesh_path_error_tx(MESH_TTL, skb->data, cpu_to_le32(dsn),
616 PERR_RCODE_NO_ROUTE, ra, sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100617 }
618
619 kfree_skb(skb);
Johannes Berg472dbc42008-09-11 00:01:49 +0200620 sdata->u.mesh.mshstats.dropped_frames_no_route++;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100621}
622
623/**
624 * mesh_path_flush_pending - free the pending queue of a mesh path
625 *
626 * @mpath: mesh path whose queue has to be freed
627 *
628 * Locking: the function must me called withing a rcu_read_lock region
629 */
630void mesh_path_flush_pending(struct mesh_path *mpath)
631{
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100632 struct sk_buff *skb;
633
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100634 while ((skb = skb_dequeue(&mpath->frame_queue)) &&
635 (mpath->flags & MESH_PATH_ACTIVE))
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200636 mesh_path_discard_frame(skb, mpath->sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100637}
638
639/**
640 * mesh_path_fix_nexthop - force a specific next hop for a mesh path
641 *
642 * @mpath: the mesh path to modify
643 * @next_hop: the next hop to force
644 *
645 * Locking: this function must be called holding mpath->state_lock
646 */
647void mesh_path_fix_nexthop(struct mesh_path *mpath, struct sta_info *next_hop)
648{
649 spin_lock_bh(&mpath->state_lock);
650 mesh_path_assign_nexthop(mpath, next_hop);
651 mpath->dsn = 0xffff;
652 mpath->metric = 0;
653 mpath->hop_count = 0;
654 mpath->exp_time = 0;
655 mpath->flags |= MESH_PATH_FIXED;
656 mesh_path_activate(mpath);
657 spin_unlock_bh(&mpath->state_lock);
658 mesh_path_tx_pending(mpath);
659}
660
661static void mesh_path_node_free(struct hlist_node *p, bool free_leafs)
662{
663 struct mesh_path *mpath;
664 struct mpath_node *node = hlist_entry(p, struct mpath_node, list);
665 mpath = node->mpath;
666 hlist_del_rcu(p);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100667 if (free_leafs)
668 kfree(mpath);
669 kfree(node);
670}
671
Pavel Emelyanov4caf86c2008-05-07 19:47:01 +0400672static int mesh_path_node_copy(struct hlist_node *p, struct mesh_table *newtbl)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100673{
674 struct mesh_path *mpath;
675 struct mpath_node *node, *new_node;
676 u32 hash_idx;
677
Pavel Emelyanov8566dc32008-05-07 19:51:51 +0400678 new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
Pavel Emelyanov00242c42008-05-07 19:48:14 +0400679 if (new_node == NULL)
680 return -ENOMEM;
681
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100682 node = hlist_entry(p, struct mpath_node, list);
683 mpath = node->mpath;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100684 new_node->mpath = mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200685 hash_idx = mesh_table_hash(mpath->dst, mpath->sdata, newtbl);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100686 hlist_add_head(&new_node->list,
687 &newtbl->hash_buckets[hash_idx]);
Pavel Emelyanov4caf86c2008-05-07 19:47:01 +0400688 return 0;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100689}
690
691int mesh_pathtbl_init(void)
692{
693 mesh_paths = mesh_table_alloc(INIT_PATHS_SIZE_ORDER);
YanBo79617de2008-09-22 13:30:32 +0800694 if (!mesh_paths)
695 return -ENOMEM;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100696 mesh_paths->free_node = &mesh_path_node_free;
697 mesh_paths->copy_node = &mesh_path_node_copy;
698 mesh_paths->mean_chain_len = MEAN_CHAIN_LEN;
YanBo79617de2008-09-22 13:30:32 +0800699
700 mpp_paths = mesh_table_alloc(INIT_PATHS_SIZE_ORDER);
701 if (!mpp_paths) {
702 mesh_table_free(mesh_paths, true);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100703 return -ENOMEM;
YanBo79617de2008-09-22 13:30:32 +0800704 }
705 mpp_paths->free_node = &mesh_path_node_free;
706 mpp_paths->copy_node = &mesh_path_node_copy;
707 mpp_paths->mean_chain_len = MEAN_CHAIN_LEN;
708
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100709 return 0;
710}
711
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200712void mesh_path_expire(struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100713{
714 struct mesh_path *mpath;
715 struct mpath_node *node;
716 struct hlist_node *p;
717 int i;
718
719 read_lock(&pathtbl_resize_lock);
720 for_each_mesh_entry(mesh_paths, p, node, i) {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200721 if (node->mpath->sdata != sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100722 continue;
723 mpath = node->mpath;
724 spin_lock_bh(&mpath->state_lock);
725 if ((!(mpath->flags & MESH_PATH_RESOLVING)) &&
726 (!(mpath->flags & MESH_PATH_FIXED)) &&
727 time_after(jiffies,
728 mpath->exp_time + MESH_PATH_EXPIRE)) {
729 spin_unlock_bh(&mpath->state_lock);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200730 mesh_path_del(mpath->dst, mpath->sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100731 } else
732 spin_unlock_bh(&mpath->state_lock);
733 }
734 read_unlock(&pathtbl_resize_lock);
735}
736
737void mesh_pathtbl_unregister(void)
738{
739 mesh_table_free(mesh_paths, true);
YanBo79617de2008-09-22 13:30:32 +0800740 mesh_table_free(mpp_paths, true);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100741}