blob: 479597e885832d539f4774e157e48deff8d22da2 [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{
Johannes Bergd0709a62008-02-25 16:27:46 +010058 rcu_assign_pointer(mpath->next_hop, sta);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +010059}
60
61
62/**
63 * mesh_path_lookup - look up a path in the mesh path table
64 * @dst: hardware address (ETH_ALEN length) of destination
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +120065 * @sdata: local subif
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +010066 *
67 * Returns: pointer to the mesh path structure, or NULL if not found
68 *
69 * Locking: must be called within a read rcu section.
70 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +120071struct mesh_path *mesh_path_lookup(u8 *dst, struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +010072{
73 struct mesh_path *mpath;
74 struct hlist_node *n;
75 struct hlist_head *bucket;
76 struct mesh_table *tbl;
77 struct mpath_node *node;
78
79 tbl = rcu_dereference(mesh_paths);
80
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +120081 bucket = &tbl->hash_buckets[mesh_table_hash(dst, sdata, tbl)];
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +010082 hlist_for_each_entry_rcu(node, n, bucket, list) {
83 mpath = node->mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +120084 if (mpath->sdata == sdata &&
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +010085 memcmp(dst, mpath->dst, ETH_ALEN) == 0) {
86 if (MPATH_EXPIRED(mpath)) {
87 spin_lock_bh(&mpath->state_lock);
88 if (MPATH_EXPIRED(mpath))
89 mpath->flags &= ~MESH_PATH_ACTIVE;
90 spin_unlock_bh(&mpath->state_lock);
91 }
92 return mpath;
93 }
94 }
95 return NULL;
96}
97
YanBo79617de2008-09-22 13:30:32 +080098struct mesh_path *mpp_path_lookup(u8 *dst, struct ieee80211_sub_if_data *sdata)
99{
100 struct mesh_path *mpath;
101 struct hlist_node *n;
102 struct hlist_head *bucket;
103 struct mesh_table *tbl;
104 struct mpath_node *node;
105
106 tbl = rcu_dereference(mpp_paths);
107
108 bucket = &tbl->hash_buckets[mesh_table_hash(dst, sdata, tbl)];
109 hlist_for_each_entry_rcu(node, n, bucket, list) {
110 mpath = node->mpath;
111 if (mpath->sdata == sdata &&
112 memcmp(dst, mpath->dst, ETH_ALEN) == 0) {
113 if (MPATH_EXPIRED(mpath)) {
114 spin_lock_bh(&mpath->state_lock);
115 if (MPATH_EXPIRED(mpath))
116 mpath->flags &= ~MESH_PATH_ACTIVE;
117 spin_unlock_bh(&mpath->state_lock);
118 }
119 return mpath;
120 }
121 }
122 return NULL;
123}
124
125
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100126/**
127 * mesh_path_lookup_by_idx - look up a path in the mesh path table by its index
128 * @idx: index
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200129 * @sdata: local subif, or NULL for all entries
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100130 *
131 * Returns: pointer to the mesh path structure, or NULL if not found.
132 *
133 * Locking: must be called within a read rcu section.
134 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200135struct mesh_path *mesh_path_lookup_by_idx(int idx, struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100136{
137 struct mpath_node *node;
138 struct hlist_node *p;
139 int i;
140 int j = 0;
141
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800142 for_each_mesh_entry(mesh_paths, p, node, i) {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200143 if (sdata && node->mpath->sdata != sdata)
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800144 continue;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100145 if (j++ == idx) {
146 if (MPATH_EXPIRED(node->mpath)) {
147 spin_lock_bh(&node->mpath->state_lock);
148 if (MPATH_EXPIRED(node->mpath))
149 node->mpath->flags &= ~MESH_PATH_ACTIVE;
150 spin_unlock_bh(&node->mpath->state_lock);
151 }
152 return node->mpath;
153 }
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800154 }
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100155
156 return NULL;
157}
158
159/**
160 * mesh_path_add - allocate and add a new path to the mesh path table
161 * @addr: destination address of the path (ETH_ALEN length)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200162 * @sdata: local subif
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100163 *
164 * Returns: 0 on sucess
165 *
166 * State: the initial state of the new path is set to 0
167 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200168int mesh_path_add(u8 *dst, struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100169{
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100170 struct mesh_path *mpath, *new_mpath;
171 struct mpath_node *node, *new_node;
172 struct hlist_head *bucket;
173 struct hlist_node *n;
174 int grow = 0;
175 int err = 0;
176 u32 hash_idx;
177
Johannes Berge2e414d2009-07-10 11:38:14 +0200178 might_sleep();
179
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200180 if (memcmp(dst, sdata->dev->dev_addr, ETH_ALEN) == 0)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100181 /* never add ourselves as neighbours */
182 return -ENOTSUPP;
183
184 if (is_multicast_ether_addr(dst))
185 return -ENOTSUPP;
186
Johannes Berg472dbc42008-09-11 00:01:49 +0200187 if (atomic_add_unless(&sdata->u.mesh.mpaths, 1, MESH_MAX_MPATHS) == 0)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100188 return -ENOSPC;
189
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400190 err = -ENOMEM;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100191 new_mpath = kzalloc(sizeof(struct mesh_path), GFP_KERNEL);
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400192 if (!new_mpath)
193 goto err_path_alloc;
194
Pavel Emelyanov0eb03d52008-05-06 18:49:02 +0400195 new_node = kmalloc(sizeof(struct mpath_node), GFP_KERNEL);
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400196 if (!new_node)
197 goto err_node_alloc;
Pavel Emelyanovf84e71a2008-05-06 18:46:36 +0400198
199 read_lock(&pathtbl_resize_lock);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100200 memcpy(new_mpath->dst, dst, ETH_ALEN);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200201 new_mpath->sdata = sdata;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100202 new_mpath->flags = 0;
203 skb_queue_head_init(&new_mpath->frame_queue);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100204 new_node->mpath = new_mpath;
205 new_mpath->timer.data = (unsigned long) new_mpath;
206 new_mpath->timer.function = mesh_path_timer;
207 new_mpath->exp_time = jiffies;
208 spin_lock_init(&new_mpath->state_lock);
209 init_timer(&new_mpath->timer);
210
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200211 hash_idx = mesh_table_hash(dst, sdata, mesh_paths);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100212 bucket = &mesh_paths->hash_buckets[hash_idx];
213
214 spin_lock(&mesh_paths->hashwlock[hash_idx]);
215
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400216 err = -EEXIST;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100217 hlist_for_each_entry(node, n, bucket, list) {
218 mpath = node->mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200219 if (mpath->sdata == sdata && memcmp(dst, mpath->dst, ETH_ALEN) == 0)
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400220 goto err_exists;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100221 }
222
223 hlist_add_head_rcu(&new_node->list, bucket);
224 if (atomic_inc_return(&mesh_paths->entries) >=
225 mesh_paths->mean_chain_len * (mesh_paths->hash_mask + 1))
226 grow = 1;
227
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100228 spin_unlock(&mesh_paths->hashwlock[hash_idx]);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100229 read_unlock(&pathtbl_resize_lock);
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400230 if (grow) {
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100231 struct mesh_table *oldtbl, *newtbl;
232
233 write_lock(&pathtbl_resize_lock);
234 oldtbl = mesh_paths;
235 newtbl = mesh_table_grow(mesh_paths);
236 if (!newtbl) {
237 write_unlock(&pathtbl_resize_lock);
Pavel Emelyanov3282aea2008-05-07 19:54:04 +0400238 return 0;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100239 }
240 rcu_assign_pointer(mesh_paths, newtbl);
Pavel Emelyanov6d6936e2008-05-06 18:51:31 +0400241 write_unlock(&pathtbl_resize_lock);
242
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100243 synchronize_rcu();
244 mesh_table_free(oldtbl, false);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100245 }
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400246 return 0;
247
248err_exists:
249 spin_unlock(&mesh_paths->hashwlock[hash_idx]);
250 read_unlock(&pathtbl_resize_lock);
251 kfree(new_node);
252err_node_alloc:
253 kfree(new_mpath);
254err_path_alloc:
Johannes Berg472dbc42008-09-11 00:01:49 +0200255 atomic_dec(&sdata->u.mesh.mpaths);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100256 return err;
257}
258
259
YanBo79617de2008-09-22 13:30:32 +0800260int mpp_path_add(u8 *dst, u8 *mpp, struct ieee80211_sub_if_data *sdata)
261{
262 struct mesh_path *mpath, *new_mpath;
263 struct mpath_node *node, *new_node;
264 struct hlist_head *bucket;
265 struct hlist_node *n;
266 int grow = 0;
267 int err = 0;
268 u32 hash_idx;
269
Johannes Berge2e414d2009-07-10 11:38:14 +0200270 might_sleep();
YanBo79617de2008-09-22 13:30:32 +0800271
272 if (memcmp(dst, sdata->dev->dev_addr, ETH_ALEN) == 0)
273 /* never add ourselves as neighbours */
274 return -ENOTSUPP;
275
276 if (is_multicast_ether_addr(dst))
277 return -ENOTSUPP;
278
279 err = -ENOMEM;
280 new_mpath = kzalloc(sizeof(struct mesh_path), GFP_KERNEL);
281 if (!new_mpath)
282 goto err_path_alloc;
283
284 new_node = kmalloc(sizeof(struct mpath_node), GFP_KERNEL);
285 if (!new_node)
286 goto err_node_alloc;
287
288 read_lock(&pathtbl_resize_lock);
289 memcpy(new_mpath->dst, dst, ETH_ALEN);
290 memcpy(new_mpath->mpp, mpp, ETH_ALEN);
291 new_mpath->sdata = sdata;
292 new_mpath->flags = 0;
293 skb_queue_head_init(&new_mpath->frame_queue);
294 new_node->mpath = new_mpath;
295 new_mpath->exp_time = jiffies;
296 spin_lock_init(&new_mpath->state_lock);
297
298 hash_idx = mesh_table_hash(dst, sdata, mpp_paths);
299 bucket = &mpp_paths->hash_buckets[hash_idx];
300
301 spin_lock(&mpp_paths->hashwlock[hash_idx]);
302
303 err = -EEXIST;
304 hlist_for_each_entry(node, n, bucket, list) {
305 mpath = node->mpath;
306 if (mpath->sdata == sdata && memcmp(dst, mpath->dst, ETH_ALEN) == 0)
307 goto err_exists;
308 }
309
310 hlist_add_head_rcu(&new_node->list, bucket);
311 if (atomic_inc_return(&mpp_paths->entries) >=
312 mpp_paths->mean_chain_len * (mpp_paths->hash_mask + 1))
313 grow = 1;
314
315 spin_unlock(&mpp_paths->hashwlock[hash_idx]);
316 read_unlock(&pathtbl_resize_lock);
317 if (grow) {
318 struct mesh_table *oldtbl, *newtbl;
319
320 write_lock(&pathtbl_resize_lock);
321 oldtbl = mpp_paths;
322 newtbl = mesh_table_grow(mpp_paths);
323 if (!newtbl) {
324 write_unlock(&pathtbl_resize_lock);
325 return 0;
326 }
327 rcu_assign_pointer(mpp_paths, newtbl);
328 write_unlock(&pathtbl_resize_lock);
329
330 synchronize_rcu();
331 mesh_table_free(oldtbl, false);
332 }
333 return 0;
334
335err_exists:
336 spin_unlock(&mpp_paths->hashwlock[hash_idx]);
337 read_unlock(&pathtbl_resize_lock);
338 kfree(new_node);
339err_node_alloc:
340 kfree(new_mpath);
341err_path_alloc:
342 return err;
343}
344
345
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100346/**
347 * mesh_plink_broken - deactivates paths and sends perr when a link breaks
348 *
349 * @sta: broken peer link
350 *
351 * This function must be called from the rate control algorithm if enough
352 * delivery errors suggest that a peer link is no longer usable.
353 */
354void mesh_plink_broken(struct sta_info *sta)
355{
356 struct mesh_path *mpath;
357 struct mpath_node *node;
358 struct hlist_node *p;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200359 struct ieee80211_sub_if_data *sdata = sta->sdata;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100360 int i;
361
362 rcu_read_lock();
363 for_each_mesh_entry(mesh_paths, p, node, i) {
364 mpath = node->mpath;
365 spin_lock_bh(&mpath->state_lock);
366 if (mpath->next_hop == sta &&
367 mpath->flags & MESH_PATH_ACTIVE &&
368 !(mpath->flags & MESH_PATH_FIXED)) {
369 mpath->flags &= ~MESH_PATH_ACTIVE;
370 ++mpath->dsn;
371 spin_unlock_bh(&mpath->state_lock);
372 mesh_path_error_tx(mpath->dst,
373 cpu_to_le32(mpath->dsn),
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200374 sdata->dev->broadcast, sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100375 } else
376 spin_unlock_bh(&mpath->state_lock);
377 }
378 rcu_read_unlock();
379}
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100380
381/**
382 * mesh_path_flush_by_nexthop - Deletes mesh paths if their next hop matches
383 *
384 * @sta - mesh peer to match
385 *
Luis Carlos Cobob4e08ea2008-02-29 15:46:08 -0800386 * RCU notes: this function is called when a mesh plink transitions from
387 * PLINK_ESTAB to any other state, since PLINK_ESTAB state is the only one that
388 * allows path creation. This will happen before the sta can be freed (because
Johannes Bergd0709a62008-02-25 16:27:46 +0100389 * sta_info_destroy() calls this) so any reader in a rcu read block will be
390 * protected against the plink disappearing.
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100391 */
392void mesh_path_flush_by_nexthop(struct sta_info *sta)
393{
394 struct mesh_path *mpath;
395 struct mpath_node *node;
396 struct hlist_node *p;
397 int i;
398
399 for_each_mesh_entry(mesh_paths, p, node, i) {
400 mpath = node->mpath;
401 if (mpath->next_hop == sta)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200402 mesh_path_del(mpath->dst, mpath->sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100403 }
404}
405
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200406void mesh_path_flush(struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100407{
408 struct mesh_path *mpath;
409 struct mpath_node *node;
410 struct hlist_node *p;
411 int i;
412
413 for_each_mesh_entry(mesh_paths, p, node, i) {
414 mpath = node->mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200415 if (mpath->sdata == sdata)
416 mesh_path_del(mpath->dst, mpath->sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100417 }
418}
419
420static void mesh_path_node_reclaim(struct rcu_head *rp)
421{
422 struct mpath_node *node = container_of(rp, struct mpath_node, rcu);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200423 struct ieee80211_sub_if_data *sdata = node->mpath->sdata;
Johannes Bergd0709a62008-02-25 16:27:46 +0100424
Luis Carlos Cobo89a1ad62008-02-29 14:49:37 -0800425 del_timer_sync(&node->mpath->timer);
Johannes Berg472dbc42008-09-11 00:01:49 +0200426 atomic_dec(&sdata->u.mesh.mpaths);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100427 kfree(node->mpath);
428 kfree(node);
429}
430
431/**
432 * mesh_path_del - delete a mesh path from the table
433 *
434 * @addr: dst address (ETH_ALEN length)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200435 * @sdata: local subif
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100436 *
437 * Returns: 0 if succesful
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100438 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200439int mesh_path_del(u8 *addr, struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100440{
441 struct mesh_path *mpath;
442 struct mpath_node *node;
443 struct hlist_head *bucket;
444 struct hlist_node *n;
445 int hash_idx;
446 int err = 0;
447
448 read_lock(&pathtbl_resize_lock);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200449 hash_idx = mesh_table_hash(addr, sdata, mesh_paths);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100450 bucket = &mesh_paths->hash_buckets[hash_idx];
451
452 spin_lock(&mesh_paths->hashwlock[hash_idx]);
453 hlist_for_each_entry(node, n, bucket, list) {
454 mpath = node->mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200455 if (mpath->sdata == sdata &&
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100456 memcmp(addr, mpath->dst, ETH_ALEN) == 0) {
457 spin_lock_bh(&mpath->state_lock);
Luis Carlos Cobocfa22c72008-02-29 15:04:13 -0800458 mpath->flags |= MESH_PATH_RESOLVING;
459 hlist_del_rcu(&node->list);
460 call_rcu(&node->rcu, mesh_path_node_reclaim);
461 atomic_dec(&mesh_paths->entries);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100462 spin_unlock_bh(&mpath->state_lock);
463 goto enddel;
464 }
465 }
466
467 err = -ENXIO;
468enddel:
469 spin_unlock(&mesh_paths->hashwlock[hash_idx]);
470 read_unlock(&pathtbl_resize_lock);
471 return err;
472}
473
474/**
475 * mesh_path_tx_pending - sends pending frames in a mesh path queue
476 *
477 * @mpath: mesh path to activate
478 *
479 * Locking: the state_lock of the mpath structure must NOT be held when calling
480 * this function.
481 */
482void mesh_path_tx_pending(struct mesh_path *mpath)
483{
484 struct sk_buff *skb;
485
486 while ((skb = skb_dequeue(&mpath->frame_queue)) &&
487 (mpath->flags & MESH_PATH_ACTIVE))
488 dev_queue_xmit(skb);
489}
490
491/**
492 * mesh_path_discard_frame - discard a frame whose path could not be resolved
493 *
494 * @skb: frame to discard
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200495 * @sdata: network subif the frame was to be sent through
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100496 *
Javier Cardona35946a52009-07-13 17:00:10 -0700497 * If the frame was being forwarded from another MP, a PERR frame will be sent
498 * to the precursor. The precursor's address (i.e. the previous hop) was saved
499 * in addr1 of the frame-to-be-forwarded, and would only be overwritten once
500 * the destination is successfully resolved.
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100501 *
502 * Locking: the function must me called within a rcu_read_lock region
503 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200504void mesh_path_discard_frame(struct sk_buff *skb,
505 struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100506{
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +0200507 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100508 struct mesh_path *mpath;
509 u32 dsn = 0;
510
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200511 if (memcmp(hdr->addr4, sdata->dev->dev_addr, ETH_ALEN) != 0) {
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100512 u8 *ra, *da;
513
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +0200514 da = hdr->addr3;
Javier Cardona35946a52009-07-13 17:00:10 -0700515 ra = hdr->addr1;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200516 mpath = mesh_path_lookup(da, sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100517 if (mpath)
518 dsn = ++mpath->dsn;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200519 mesh_path_error_tx(skb->data, cpu_to_le32(dsn), ra, sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100520 }
521
522 kfree_skb(skb);
Johannes Berg472dbc42008-09-11 00:01:49 +0200523 sdata->u.mesh.mshstats.dropped_frames_no_route++;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100524}
525
526/**
527 * mesh_path_flush_pending - free the pending queue of a mesh path
528 *
529 * @mpath: mesh path whose queue has to be freed
530 *
531 * Locking: the function must me called withing a rcu_read_lock region
532 */
533void mesh_path_flush_pending(struct mesh_path *mpath)
534{
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100535 struct sk_buff *skb;
536
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100537 while ((skb = skb_dequeue(&mpath->frame_queue)) &&
538 (mpath->flags & MESH_PATH_ACTIVE))
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200539 mesh_path_discard_frame(skb, mpath->sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100540}
541
542/**
543 * mesh_path_fix_nexthop - force a specific next hop for a mesh path
544 *
545 * @mpath: the mesh path to modify
546 * @next_hop: the next hop to force
547 *
548 * Locking: this function must be called holding mpath->state_lock
549 */
550void mesh_path_fix_nexthop(struct mesh_path *mpath, struct sta_info *next_hop)
551{
552 spin_lock_bh(&mpath->state_lock);
553 mesh_path_assign_nexthop(mpath, next_hop);
554 mpath->dsn = 0xffff;
555 mpath->metric = 0;
556 mpath->hop_count = 0;
557 mpath->exp_time = 0;
558 mpath->flags |= MESH_PATH_FIXED;
559 mesh_path_activate(mpath);
560 spin_unlock_bh(&mpath->state_lock);
561 mesh_path_tx_pending(mpath);
562}
563
564static void mesh_path_node_free(struct hlist_node *p, bool free_leafs)
565{
566 struct mesh_path *mpath;
567 struct mpath_node *node = hlist_entry(p, struct mpath_node, list);
568 mpath = node->mpath;
569 hlist_del_rcu(p);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100570 if (free_leafs)
571 kfree(mpath);
572 kfree(node);
573}
574
Pavel Emelyanov4caf86c2008-05-07 19:47:01 +0400575static int mesh_path_node_copy(struct hlist_node *p, struct mesh_table *newtbl)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100576{
577 struct mesh_path *mpath;
578 struct mpath_node *node, *new_node;
579 u32 hash_idx;
580
Pavel Emelyanov8566dc32008-05-07 19:51:51 +0400581 new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
Pavel Emelyanov00242c42008-05-07 19:48:14 +0400582 if (new_node == NULL)
583 return -ENOMEM;
584
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100585 node = hlist_entry(p, struct mpath_node, list);
586 mpath = node->mpath;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100587 new_node->mpath = mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200588 hash_idx = mesh_table_hash(mpath->dst, mpath->sdata, newtbl);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100589 hlist_add_head(&new_node->list,
590 &newtbl->hash_buckets[hash_idx]);
Pavel Emelyanov4caf86c2008-05-07 19:47:01 +0400591 return 0;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100592}
593
594int mesh_pathtbl_init(void)
595{
596 mesh_paths = mesh_table_alloc(INIT_PATHS_SIZE_ORDER);
YanBo79617de2008-09-22 13:30:32 +0800597 if (!mesh_paths)
598 return -ENOMEM;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100599 mesh_paths->free_node = &mesh_path_node_free;
600 mesh_paths->copy_node = &mesh_path_node_copy;
601 mesh_paths->mean_chain_len = MEAN_CHAIN_LEN;
YanBo79617de2008-09-22 13:30:32 +0800602
603 mpp_paths = mesh_table_alloc(INIT_PATHS_SIZE_ORDER);
604 if (!mpp_paths) {
605 mesh_table_free(mesh_paths, true);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100606 return -ENOMEM;
YanBo79617de2008-09-22 13:30:32 +0800607 }
608 mpp_paths->free_node = &mesh_path_node_free;
609 mpp_paths->copy_node = &mesh_path_node_copy;
610 mpp_paths->mean_chain_len = MEAN_CHAIN_LEN;
611
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100612 return 0;
613}
614
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200615void mesh_path_expire(struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100616{
617 struct mesh_path *mpath;
618 struct mpath_node *node;
619 struct hlist_node *p;
620 int i;
621
622 read_lock(&pathtbl_resize_lock);
623 for_each_mesh_entry(mesh_paths, p, node, i) {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200624 if (node->mpath->sdata != sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100625 continue;
626 mpath = node->mpath;
627 spin_lock_bh(&mpath->state_lock);
628 if ((!(mpath->flags & MESH_PATH_RESOLVING)) &&
629 (!(mpath->flags & MESH_PATH_FIXED)) &&
630 time_after(jiffies,
631 mpath->exp_time + MESH_PATH_EXPIRE)) {
632 spin_unlock_bh(&mpath->state_lock);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200633 mesh_path_del(mpath->dst, mpath->sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100634 } else
635 spin_unlock_bh(&mpath->state_lock);
636 }
637 read_unlock(&pathtbl_resize_lock);
638}
639
640void mesh_pathtbl_unregister(void)
641{
642 mesh_table_free(mesh_paths, true);
YanBo79617de2008-09-22 13:30:32 +0800643 mesh_table_free(mpp_paths, true);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100644}