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/netdevice.h>
13 #include <linux/random.h>
14 #include <linux/spinlock.h>
15 #include <linux/string.h>
16 #include <net/mac80211.h>
17 #include "ieee80211_i.h"
20 /* There will be initially 2^INIT_PATHS_SIZE_ORDER buckets */
21 #define INIT_PATHS_SIZE_ORDER 2
23 /* Keep the mean chain length below this constant */
24 #define MEAN_CHAIN_LEN 2
26 #define MPATH_EXPIRED(mpath) ((mpath->flags & MESH_PATH_ACTIVE) && \
27 time_after(jiffies, mpath->exp_time) && \
28 !(mpath->flags & MESH_PATH_FIXED))
31 struct hlist_node list;
33 /* This indirection allows two different tables to point to the same
34 * mesh_path structure, useful when resizing
36 struct mesh_path *mpath;
39 static struct mesh_table *mesh_paths;
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
65 * @dev: local interface
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 net_device *dev)
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, dev, tbl)];
82 hlist_for_each_entry_rcu(node, n, bucket, list) {
84 if (mpath->dev == dev &&
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);
99 * mesh_path_lookup_by_idx - look up a path in the mesh path table by its index
101 * @dev: local interface, or NULL for all entries
103 * Returns: pointer to the mesh path structure, or NULL if not found.
105 * Locking: must be called within a read rcu section.
107 struct mesh_path *mesh_path_lookup_by_idx(int idx, struct net_device *dev)
109 struct mpath_node *node;
110 struct hlist_node *p;
114 for_each_mesh_entry(mesh_paths, p, node, i) {
115 if (dev && node->mpath->dev != dev)
118 if (MPATH_EXPIRED(node->mpath)) {
119 spin_lock_bh(&node->mpath->state_lock);
120 if (MPATH_EXPIRED(node->mpath))
121 node->mpath->flags &= ~MESH_PATH_ACTIVE;
122 spin_unlock_bh(&node->mpath->state_lock);
132 * mesh_path_add - allocate and add a new path to the mesh path table
133 * @addr: destination address of the path (ETH_ALEN length)
134 * @dev: local interface
136 * Returns: 0 on sucess
138 * State: the initial state of the new path is set to 0
140 int mesh_path_add(u8 *dst, struct net_device *dev)
142 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
143 struct mesh_path *mpath, *new_mpath;
144 struct mpath_node *node, *new_node;
145 struct hlist_head *bucket;
146 struct hlist_node *n;
151 if (memcmp(dst, dev->dev_addr, ETH_ALEN) == 0)
152 /* never add ourselves as neighbours */
155 if (is_multicast_ether_addr(dst))
158 if (atomic_add_unless(&sdata->u.sta.mpaths, 1, MESH_MAX_MPATHS) == 0)
162 new_mpath = kzalloc(sizeof(struct mesh_path), GFP_KERNEL);
166 new_node = kmalloc(sizeof(struct mpath_node), GFP_KERNEL);
170 read_lock(&pathtbl_resize_lock);
171 memcpy(new_mpath->dst, dst, ETH_ALEN);
172 new_mpath->dev = dev;
173 new_mpath->flags = 0;
174 skb_queue_head_init(&new_mpath->frame_queue);
175 new_node->mpath = new_mpath;
176 new_mpath->timer.data = (unsigned long) new_mpath;
177 new_mpath->timer.function = mesh_path_timer;
178 new_mpath->exp_time = jiffies;
179 spin_lock_init(&new_mpath->state_lock);
180 init_timer(&new_mpath->timer);
182 hash_idx = mesh_table_hash(dst, dev, mesh_paths);
183 bucket = &mesh_paths->hash_buckets[hash_idx];
185 spin_lock(&mesh_paths->hashwlock[hash_idx]);
188 hlist_for_each_entry(node, n, bucket, list) {
190 if (mpath->dev == dev && memcmp(dst, mpath->dst, ETH_ALEN) == 0)
194 hlist_add_head_rcu(&new_node->list, bucket);
195 if (atomic_inc_return(&mesh_paths->entries) >=
196 mesh_paths->mean_chain_len * (mesh_paths->hash_mask + 1))
199 spin_unlock(&mesh_paths->hashwlock[hash_idx]);
200 read_unlock(&pathtbl_resize_lock);
202 struct mesh_table *oldtbl, *newtbl;
204 write_lock(&pathtbl_resize_lock);
206 newtbl = mesh_table_grow(mesh_paths);
208 write_unlock(&pathtbl_resize_lock);
211 rcu_assign_pointer(mesh_paths, newtbl);
212 write_unlock(&pathtbl_resize_lock);
215 mesh_table_free(oldtbl, false);
220 spin_unlock(&mesh_paths->hashwlock[hash_idx]);
221 read_unlock(&pathtbl_resize_lock);
226 atomic_dec(&sdata->u.sta.mpaths);
232 * mesh_plink_broken - deactivates paths and sends perr when a link breaks
234 * @sta: broken peer link
236 * This function must be called from the rate control algorithm if enough
237 * delivery errors suggest that a peer link is no longer usable.
239 void mesh_plink_broken(struct sta_info *sta)
241 struct mesh_path *mpath;
242 struct mpath_node *node;
243 struct hlist_node *p;
244 struct net_device *dev = sta->sdata->dev;
248 for_each_mesh_entry(mesh_paths, p, node, i) {
250 spin_lock_bh(&mpath->state_lock);
251 if (mpath->next_hop == sta &&
252 mpath->flags & MESH_PATH_ACTIVE &&
253 !(mpath->flags & MESH_PATH_FIXED)) {
254 mpath->flags &= ~MESH_PATH_ACTIVE;
256 spin_unlock_bh(&mpath->state_lock);
257 mesh_path_error_tx(mpath->dst,
258 cpu_to_le32(mpath->dsn),
259 dev->broadcast, dev);
261 spin_unlock_bh(&mpath->state_lock);
265 EXPORT_SYMBOL(mesh_plink_broken);
268 * mesh_path_flush_by_nexthop - Deletes mesh paths if their next hop matches
270 * @sta - mesh peer to match
272 * RCU notes: this function is called when a mesh plink transitions from
273 * PLINK_ESTAB to any other state, since PLINK_ESTAB state is the only one that
274 * allows path creation. This will happen before the sta can be freed (because
275 * sta_info_destroy() calls this) so any reader in a rcu read block will be
276 * protected against the plink disappearing.
278 void mesh_path_flush_by_nexthop(struct sta_info *sta)
280 struct mesh_path *mpath;
281 struct mpath_node *node;
282 struct hlist_node *p;
285 for_each_mesh_entry(mesh_paths, p, node, i) {
287 if (mpath->next_hop == sta)
288 mesh_path_del(mpath->dst, mpath->dev);
292 void mesh_path_flush(struct net_device *dev)
294 struct mesh_path *mpath;
295 struct mpath_node *node;
296 struct hlist_node *p;
299 for_each_mesh_entry(mesh_paths, p, node, i) {
301 if (mpath->dev == dev)
302 mesh_path_del(mpath->dst, mpath->dev);
306 static void mesh_path_node_reclaim(struct rcu_head *rp)
308 struct mpath_node *node = container_of(rp, struct mpath_node, rcu);
309 struct ieee80211_sub_if_data *sdata =
310 IEEE80211_DEV_TO_SUB_IF(node->mpath->dev);
312 del_timer_sync(&node->mpath->timer);
313 atomic_dec(&sdata->u.sta.mpaths);
319 * mesh_path_del - delete a mesh path from the table
321 * @addr: dst address (ETH_ALEN length)
322 * @dev: local interface
324 * Returns: 0 if succesful
326 int mesh_path_del(u8 *addr, struct net_device *dev)
328 struct mesh_path *mpath;
329 struct mpath_node *node;
330 struct hlist_head *bucket;
331 struct hlist_node *n;
335 read_lock(&pathtbl_resize_lock);
336 hash_idx = mesh_table_hash(addr, dev, mesh_paths);
337 bucket = &mesh_paths->hash_buckets[hash_idx];
339 spin_lock(&mesh_paths->hashwlock[hash_idx]);
340 hlist_for_each_entry(node, n, bucket, list) {
342 if (mpath->dev == dev &&
343 memcmp(addr, mpath->dst, ETH_ALEN) == 0) {
344 spin_lock_bh(&mpath->state_lock);
345 mpath->flags |= MESH_PATH_RESOLVING;
346 hlist_del_rcu(&node->list);
347 call_rcu(&node->rcu, mesh_path_node_reclaim);
348 atomic_dec(&mesh_paths->entries);
349 spin_unlock_bh(&mpath->state_lock);
356 spin_unlock(&mesh_paths->hashwlock[hash_idx]);
357 read_unlock(&pathtbl_resize_lock);
362 * mesh_path_tx_pending - sends pending frames in a mesh path queue
364 * @mpath: mesh path to activate
366 * Locking: the state_lock of the mpath structure must NOT be held when calling
369 void mesh_path_tx_pending(struct mesh_path *mpath)
373 while ((skb = skb_dequeue(&mpath->frame_queue)) &&
374 (mpath->flags & MESH_PATH_ACTIVE))
379 * mesh_path_discard_frame - discard a frame whose path could not be resolved
381 * @skb: frame to discard
382 * @dev: network device the frame was to be sent through
384 * If the frame was beign forwarded from another MP, a PERR frame will be sent
387 * Locking: the function must me called within a rcu_read_lock region
389 void mesh_path_discard_frame(struct sk_buff *skb, struct net_device *dev)
391 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
392 struct mesh_path *mpath;
395 if (skb->pkt_type == PACKET_OTHERHOST) {
396 struct ieee80211s_hdr *prev_meshhdr;
400 prev_meshhdr = ((struct ieee80211s_hdr *)skb->cb);
401 mshhdrlen = ieee80211_get_mesh_hdrlen(prev_meshhdr);
404 mpath = mesh_path_lookup(da, dev);
407 mesh_path_error_tx(skb->data, cpu_to_le32(dsn), ra, dev);
411 sdata->u.sta.mshstats.dropped_frames_no_route++;
415 * mesh_path_flush_pending - free the pending queue of a mesh path
417 * @mpath: mesh path whose queue has to be freed
419 * Locking: the function must me called withing a rcu_read_lock region
421 void mesh_path_flush_pending(struct mesh_path *mpath)
423 struct ieee80211_sub_if_data *sdata;
426 sdata = IEEE80211_DEV_TO_SUB_IF(mpath->dev);
428 while ((skb = skb_dequeue(&mpath->frame_queue)) &&
429 (mpath->flags & MESH_PATH_ACTIVE))
430 mesh_path_discard_frame(skb, mpath->dev);
434 * mesh_path_fix_nexthop - force a specific next hop for a mesh path
436 * @mpath: the mesh path to modify
437 * @next_hop: the next hop to force
439 * Locking: this function must be called holding mpath->state_lock
441 void mesh_path_fix_nexthop(struct mesh_path *mpath, struct sta_info *next_hop)
443 spin_lock_bh(&mpath->state_lock);
444 mesh_path_assign_nexthop(mpath, next_hop);
447 mpath->hop_count = 0;
449 mpath->flags |= MESH_PATH_FIXED;
450 mesh_path_activate(mpath);
451 spin_unlock_bh(&mpath->state_lock);
452 mesh_path_tx_pending(mpath);
455 static void mesh_path_node_free(struct hlist_node *p, bool free_leafs)
457 struct mesh_path *mpath;
458 struct mpath_node *node = hlist_entry(p, struct mpath_node, list);
466 static void mesh_path_node_copy(struct hlist_node *p, struct mesh_table *newtbl)
468 struct mesh_path *mpath;
469 struct mpath_node *node, *new_node;
472 node = hlist_entry(p, struct mpath_node, list);
474 new_node = kmalloc(sizeof(struct mpath_node), GFP_KERNEL);
475 new_node->mpath = mpath;
476 hash_idx = mesh_table_hash(mpath->dst, mpath->dev, newtbl);
477 hlist_add_head(&new_node->list,
478 &newtbl->hash_buckets[hash_idx]);
481 int mesh_pathtbl_init(void)
483 mesh_paths = mesh_table_alloc(INIT_PATHS_SIZE_ORDER);
484 mesh_paths->free_node = &mesh_path_node_free;
485 mesh_paths->copy_node = &mesh_path_node_copy;
486 mesh_paths->mean_chain_len = MEAN_CHAIN_LEN;
492 void mesh_path_expire(struct net_device *dev)
494 struct mesh_path *mpath;
495 struct mpath_node *node;
496 struct hlist_node *p;
499 read_lock(&pathtbl_resize_lock);
500 for_each_mesh_entry(mesh_paths, p, node, i) {
501 if (node->mpath->dev != dev)
504 spin_lock_bh(&mpath->state_lock);
505 if ((!(mpath->flags & MESH_PATH_RESOLVING)) &&
506 (!(mpath->flags & MESH_PATH_FIXED)) &&
508 mpath->exp_time + MESH_PATH_EXPIRE)) {
509 spin_unlock_bh(&mpath->state_lock);
510 mesh_path_del(mpath->dst, mpath->dev);
512 spin_unlock_bh(&mpath->state_lock);
514 read_unlock(&pathtbl_resize_lock);
517 void mesh_pathtbl_unregister(void)
519 mesh_table_free(mesh_paths, true);