2 * Copyright (c) 2008 open80211s Ltd.
3 * Author: Luis Carlos Cobo <luisca@cozybit.com>
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.
10 #include <linux/etherdevice.h>
11 #include <linux/list.h>
12 #include <linux/random.h>
13 #include <linux/spinlock.h>
14 #include <linux/string.h>
15 #include <net/mac80211.h>
16 #include "ieee80211_i.h"
19 /* There will be initially 2^INIT_PATHS_SIZE_ORDER buckets */
20 #define INIT_PATHS_SIZE_ORDER 2
22 /* Keep the mean chain length below this constant */
23 #define MEAN_CHAIN_LEN 2
25 #define MPATH_EXPIRED(mpath) ((mpath->flags & MESH_PATH_ACTIVE) && \
26 time_after(jiffies, mpath->exp_time) && \
27 !(mpath->flags & MESH_PATH_FIXED))
30 struct hlist_node list;
32 /* This indirection allows two different tables to point to the same
33 * mesh_path structure, useful when resizing
35 struct mesh_path *mpath;
38 static struct mesh_table *mesh_paths;
39 static struct mesh_table *mpp_paths; /* Store paths for MPP&MAP */
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
45 static DEFINE_RWLOCK(pathtbl_resize_lock);
49 * mesh_path_assign_nexthop - update mesh path next hop
51 * @mpath: mesh path to update
52 * @sta: next hop to assign
54 * Locking: mpath->state_lock must be held when calling this function
56 void mesh_path_assign_nexthop(struct mesh_path *mpath, struct sta_info *sta)
58 rcu_assign_pointer(mpath->next_hop, sta);
63 * mesh_path_lookup - look up a path in the mesh path table
64 * @dst: hardware address (ETH_ALEN length) of destination
67 * Returns: pointer to the mesh path structure, or NULL if not found
69 * Locking: must be called within a read rcu section.
71 struct mesh_path *mesh_path_lookup(u8 *dst, struct ieee80211_sub_if_data *sdata)
73 struct mesh_path *mpath;
75 struct hlist_head *bucket;
76 struct mesh_table *tbl;
77 struct mpath_node *node;
79 tbl = rcu_dereference(mesh_paths);
81 bucket = &tbl->hash_buckets[mesh_table_hash(dst, sdata, tbl)];
82 hlist_for_each_entry_rcu(node, n, bucket, list) {
84 if (mpath->sdata == sdata &&
85 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);
98 struct mesh_path *mpp_path_lookup(u8 *dst, struct ieee80211_sub_if_data *sdata)
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;
106 tbl = rcu_dereference(mpp_paths);
108 bucket = &tbl->hash_buckets[mesh_table_hash(dst, sdata, tbl)];
109 hlist_for_each_entry_rcu(node, n, bucket, list) {
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);
127 * mesh_path_lookup_by_idx - look up a path in the mesh path table by its index
129 * @sdata: local subif, or NULL for all entries
131 * Returns: pointer to the mesh path structure, or NULL if not found.
133 * Locking: must be called within a read rcu section.
135 struct mesh_path *mesh_path_lookup_by_idx(int idx, struct ieee80211_sub_if_data *sdata)
137 struct mpath_node *node;
138 struct hlist_node *p;
142 for_each_mesh_entry(mesh_paths, p, node, i) {
143 if (sdata && node->mpath->sdata != sdata)
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);
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)
162 * @sdata: local subif
164 * Returns: 0 on sucess
166 * State: the initial state of the new path is set to 0
168 int mesh_path_add(u8 *dst, struct ieee80211_sub_if_data *sdata)
170 struct mesh_path *mpath, *new_mpath;
171 struct mpath_node *node, *new_node;
172 struct hlist_head *bucket;
173 struct hlist_node *n;
178 if (memcmp(dst, sdata->dev->dev_addr, ETH_ALEN) == 0)
179 /* never add ourselves as neighbours */
182 if (is_multicast_ether_addr(dst))
185 if (atomic_add_unless(&sdata->u.mesh.mpaths, 1, MESH_MAX_MPATHS) == 0)
189 new_mpath = kzalloc(sizeof(struct mesh_path), GFP_KERNEL);
193 new_node = kmalloc(sizeof(struct mpath_node), GFP_KERNEL);
197 read_lock(&pathtbl_resize_lock);
198 memcpy(new_mpath->dst, dst, ETH_ALEN);
199 new_mpath->sdata = sdata;
200 new_mpath->flags = 0;
201 skb_queue_head_init(&new_mpath->frame_queue);
202 new_node->mpath = new_mpath;
203 new_mpath->timer.data = (unsigned long) new_mpath;
204 new_mpath->timer.function = mesh_path_timer;
205 new_mpath->exp_time = jiffies;
206 spin_lock_init(&new_mpath->state_lock);
207 init_timer(&new_mpath->timer);
209 hash_idx = mesh_table_hash(dst, sdata, mesh_paths);
210 bucket = &mesh_paths->hash_buckets[hash_idx];
212 spin_lock(&mesh_paths->hashwlock[hash_idx]);
215 hlist_for_each_entry(node, n, bucket, list) {
217 if (mpath->sdata == sdata && memcmp(dst, mpath->dst, ETH_ALEN) == 0)
221 hlist_add_head_rcu(&new_node->list, bucket);
222 if (atomic_inc_return(&mesh_paths->entries) >=
223 mesh_paths->mean_chain_len * (mesh_paths->hash_mask + 1))
226 spin_unlock(&mesh_paths->hashwlock[hash_idx]);
227 read_unlock(&pathtbl_resize_lock);
229 struct mesh_table *oldtbl, *newtbl;
231 write_lock(&pathtbl_resize_lock);
233 newtbl = mesh_table_grow(mesh_paths);
235 write_unlock(&pathtbl_resize_lock);
238 rcu_assign_pointer(mesh_paths, newtbl);
239 write_unlock(&pathtbl_resize_lock);
242 mesh_table_free(oldtbl, false);
247 spin_unlock(&mesh_paths->hashwlock[hash_idx]);
248 read_unlock(&pathtbl_resize_lock);
253 atomic_dec(&sdata->u.mesh.mpaths);
258 int mpp_path_add(u8 *dst, u8 *mpp, struct ieee80211_sub_if_data *sdata)
260 struct mesh_path *mpath, *new_mpath;
261 struct mpath_node *node, *new_node;
262 struct hlist_head *bucket;
263 struct hlist_node *n;
269 if (memcmp(dst, sdata->dev->dev_addr, ETH_ALEN) == 0)
270 /* never add ourselves as neighbours */
273 if (is_multicast_ether_addr(dst))
277 new_mpath = kzalloc(sizeof(struct mesh_path), GFP_KERNEL);
281 new_node = kmalloc(sizeof(struct mpath_node), GFP_KERNEL);
285 read_lock(&pathtbl_resize_lock);
286 memcpy(new_mpath->dst, dst, ETH_ALEN);
287 memcpy(new_mpath->mpp, mpp, ETH_ALEN);
288 new_mpath->sdata = sdata;
289 new_mpath->flags = 0;
290 skb_queue_head_init(&new_mpath->frame_queue);
291 new_node->mpath = new_mpath;
292 new_mpath->exp_time = jiffies;
293 spin_lock_init(&new_mpath->state_lock);
295 hash_idx = mesh_table_hash(dst, sdata, mpp_paths);
296 bucket = &mpp_paths->hash_buckets[hash_idx];
298 spin_lock(&mpp_paths->hashwlock[hash_idx]);
301 hlist_for_each_entry(node, n, bucket, list) {
303 if (mpath->sdata == sdata && memcmp(dst, mpath->dst, ETH_ALEN) == 0)
307 hlist_add_head_rcu(&new_node->list, bucket);
308 if (atomic_inc_return(&mpp_paths->entries) >=
309 mpp_paths->mean_chain_len * (mpp_paths->hash_mask + 1))
312 spin_unlock(&mpp_paths->hashwlock[hash_idx]);
313 read_unlock(&pathtbl_resize_lock);
315 struct mesh_table *oldtbl, *newtbl;
317 write_lock(&pathtbl_resize_lock);
319 newtbl = mesh_table_grow(mpp_paths);
321 write_unlock(&pathtbl_resize_lock);
324 rcu_assign_pointer(mpp_paths, newtbl);
325 write_unlock(&pathtbl_resize_lock);
328 mesh_table_free(oldtbl, false);
333 spin_unlock(&mpp_paths->hashwlock[hash_idx]);
334 read_unlock(&pathtbl_resize_lock);
344 * mesh_plink_broken - deactivates paths and sends perr when a link breaks
346 * @sta: broken peer link
348 * This function must be called from the rate control algorithm if enough
349 * delivery errors suggest that a peer link is no longer usable.
351 void mesh_plink_broken(struct sta_info *sta)
353 struct mesh_path *mpath;
354 struct mpath_node *node;
355 struct hlist_node *p;
356 struct ieee80211_sub_if_data *sdata = sta->sdata;
360 for_each_mesh_entry(mesh_paths, p, node, i) {
362 spin_lock_bh(&mpath->state_lock);
363 if (mpath->next_hop == sta &&
364 mpath->flags & MESH_PATH_ACTIVE &&
365 !(mpath->flags & MESH_PATH_FIXED)) {
366 mpath->flags &= ~MESH_PATH_ACTIVE;
368 spin_unlock_bh(&mpath->state_lock);
369 mesh_path_error_tx(mpath->dst,
370 cpu_to_le32(mpath->dsn),
371 sdata->dev->broadcast, sdata);
373 spin_unlock_bh(&mpath->state_lock);
379 * mesh_path_flush_by_nexthop - Deletes mesh paths if their next hop matches
381 * @sta - mesh peer to match
383 * RCU notes: this function is called when a mesh plink transitions from
384 * PLINK_ESTAB to any other state, since PLINK_ESTAB state is the only one that
385 * allows path creation. This will happen before the sta can be freed (because
386 * sta_info_destroy() calls this) so any reader in a rcu read block will be
387 * protected against the plink disappearing.
389 void mesh_path_flush_by_nexthop(struct sta_info *sta)
391 struct mesh_path *mpath;
392 struct mpath_node *node;
393 struct hlist_node *p;
396 for_each_mesh_entry(mesh_paths, p, node, i) {
398 if (mpath->next_hop == sta)
399 mesh_path_del(mpath->dst, mpath->sdata);
403 void mesh_path_flush(struct ieee80211_sub_if_data *sdata)
405 struct mesh_path *mpath;
406 struct mpath_node *node;
407 struct hlist_node *p;
410 for_each_mesh_entry(mesh_paths, p, node, i) {
412 if (mpath->sdata == sdata)
413 mesh_path_del(mpath->dst, mpath->sdata);
417 static void mesh_path_node_reclaim(struct rcu_head *rp)
419 struct mpath_node *node = container_of(rp, struct mpath_node, rcu);
420 struct ieee80211_sub_if_data *sdata = node->mpath->sdata;
422 del_timer_sync(&node->mpath->timer);
423 atomic_dec(&sdata->u.mesh.mpaths);
429 * mesh_path_del - delete a mesh path from the table
431 * @addr: dst address (ETH_ALEN length)
432 * @sdata: local subif
434 * Returns: 0 if succesful
436 int mesh_path_del(u8 *addr, struct ieee80211_sub_if_data *sdata)
438 struct mesh_path *mpath;
439 struct mpath_node *node;
440 struct hlist_head *bucket;
441 struct hlist_node *n;
445 read_lock(&pathtbl_resize_lock);
446 hash_idx = mesh_table_hash(addr, sdata, mesh_paths);
447 bucket = &mesh_paths->hash_buckets[hash_idx];
449 spin_lock(&mesh_paths->hashwlock[hash_idx]);
450 hlist_for_each_entry(node, n, bucket, list) {
452 if (mpath->sdata == sdata &&
453 memcmp(addr, mpath->dst, ETH_ALEN) == 0) {
454 spin_lock_bh(&mpath->state_lock);
455 mpath->flags |= MESH_PATH_RESOLVING;
456 hlist_del_rcu(&node->list);
457 call_rcu(&node->rcu, mesh_path_node_reclaim);
458 atomic_dec(&mesh_paths->entries);
459 spin_unlock_bh(&mpath->state_lock);
466 spin_unlock(&mesh_paths->hashwlock[hash_idx]);
467 read_unlock(&pathtbl_resize_lock);
472 * mesh_path_tx_pending - sends pending frames in a mesh path queue
474 * @mpath: mesh path to activate
476 * Locking: the state_lock of the mpath structure must NOT be held when calling
479 void mesh_path_tx_pending(struct mesh_path *mpath)
483 while ((skb = skb_dequeue(&mpath->frame_queue)) &&
484 (mpath->flags & MESH_PATH_ACTIVE))
489 * mesh_path_discard_frame - discard a frame whose path could not be resolved
491 * @skb: frame to discard
492 * @sdata: network subif the frame was to be sent through
494 * If the frame was beign forwarded from another MP, a PERR frame will be sent
497 * Locking: the function must me called within a rcu_read_lock region
499 void mesh_path_discard_frame(struct sk_buff *skb,
500 struct ieee80211_sub_if_data *sdata)
502 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
503 struct mesh_path *mpath;
506 if (memcmp(hdr->addr4, sdata->dev->dev_addr, ETH_ALEN) != 0) {
511 mpath = mesh_path_lookup(da, sdata);
514 mesh_path_error_tx(skb->data, cpu_to_le32(dsn), ra, sdata);
518 sdata->u.mesh.mshstats.dropped_frames_no_route++;
522 * mesh_path_flush_pending - free the pending queue of a mesh path
524 * @mpath: mesh path whose queue has to be freed
526 * Locking: the function must me called withing a rcu_read_lock region
528 void mesh_path_flush_pending(struct mesh_path *mpath)
532 while ((skb = skb_dequeue(&mpath->frame_queue)) &&
533 (mpath->flags & MESH_PATH_ACTIVE))
534 mesh_path_discard_frame(skb, mpath->sdata);
538 * mesh_path_fix_nexthop - force a specific next hop for a mesh path
540 * @mpath: the mesh path to modify
541 * @next_hop: the next hop to force
543 * Locking: this function must be called holding mpath->state_lock
545 void mesh_path_fix_nexthop(struct mesh_path *mpath, struct sta_info *next_hop)
547 spin_lock_bh(&mpath->state_lock);
548 mesh_path_assign_nexthop(mpath, next_hop);
551 mpath->hop_count = 0;
553 mpath->flags |= MESH_PATH_FIXED;
554 mesh_path_activate(mpath);
555 spin_unlock_bh(&mpath->state_lock);
556 mesh_path_tx_pending(mpath);
559 static void mesh_path_node_free(struct hlist_node *p, bool free_leafs)
561 struct mesh_path *mpath;
562 struct mpath_node *node = hlist_entry(p, struct mpath_node, list);
570 static int mesh_path_node_copy(struct hlist_node *p, struct mesh_table *newtbl)
572 struct mesh_path *mpath;
573 struct mpath_node *node, *new_node;
576 new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
577 if (new_node == NULL)
580 node = hlist_entry(p, struct mpath_node, list);
582 new_node->mpath = mpath;
583 hash_idx = mesh_table_hash(mpath->dst, mpath->sdata, newtbl);
584 hlist_add_head(&new_node->list,
585 &newtbl->hash_buckets[hash_idx]);
589 int mesh_pathtbl_init(void)
591 mesh_paths = mesh_table_alloc(INIT_PATHS_SIZE_ORDER);
594 mesh_paths->free_node = &mesh_path_node_free;
595 mesh_paths->copy_node = &mesh_path_node_copy;
596 mesh_paths->mean_chain_len = MEAN_CHAIN_LEN;
598 mpp_paths = mesh_table_alloc(INIT_PATHS_SIZE_ORDER);
600 mesh_table_free(mesh_paths, true);
603 mpp_paths->free_node = &mesh_path_node_free;
604 mpp_paths->copy_node = &mesh_path_node_copy;
605 mpp_paths->mean_chain_len = MEAN_CHAIN_LEN;
610 void mesh_path_expire(struct ieee80211_sub_if_data *sdata)
612 struct mesh_path *mpath;
613 struct mpath_node *node;
614 struct hlist_node *p;
617 read_lock(&pathtbl_resize_lock);
618 for_each_mesh_entry(mesh_paths, p, node, i) {
619 if (node->mpath->sdata != sdata)
622 spin_lock_bh(&mpath->state_lock);
623 if ((!(mpath->flags & MESH_PATH_RESOLVING)) &&
624 (!(mpath->flags & MESH_PATH_FIXED)) &&
626 mpath->exp_time + MESH_PATH_EXPIRE)) {
627 spin_unlock_bh(&mpath->state_lock);
628 mesh_path_del(mpath->dst, mpath->sdata);
630 spin_unlock_bh(&mpath->state_lock);
632 read_unlock(&pathtbl_resize_lock);
635 void mesh_pathtbl_unregister(void)
637 mesh_table_free(mesh_paths, true);
638 mesh_table_free(mpp_paths, true);