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;
40 /* This lock will have the grow table function as writer and add / delete nodes
41 * as readers. When reading the table (i.e. doing lookups) we are well protected
44 static DEFINE_RWLOCK(pathtbl_resize_lock);
48 * mesh_path_assign_nexthop - update mesh path next hop
50 * @mpath: mesh path to update
51 * @sta: next hop to assign
53 * Locking: mpath->state_lock must be held when calling this function
55 void mesh_path_assign_nexthop(struct mesh_path *mpath, struct sta_info *sta)
57 rcu_assign_pointer(mpath->next_hop, sta);
62 * mesh_path_lookup - look up a path in the mesh path table
63 * @dst: hardware address (ETH_ALEN length) of destination
66 * Returns: pointer to the mesh path structure, or NULL if not found
68 * Locking: must be called within a read rcu section.
70 struct mesh_path *mesh_path_lookup(u8 *dst, struct ieee80211_sub_if_data *sdata)
72 struct mesh_path *mpath;
74 struct hlist_head *bucket;
75 struct mesh_table *tbl;
76 struct mpath_node *node;
78 tbl = rcu_dereference(mesh_paths);
80 bucket = &tbl->hash_buckets[mesh_table_hash(dst, sdata, tbl)];
81 hlist_for_each_entry_rcu(node, n, bucket, list) {
83 if (mpath->sdata == sdata &&
84 memcmp(dst, mpath->dst, ETH_ALEN) == 0) {
85 if (MPATH_EXPIRED(mpath)) {
86 spin_lock_bh(&mpath->state_lock);
87 if (MPATH_EXPIRED(mpath))
88 mpath->flags &= ~MESH_PATH_ACTIVE;
89 spin_unlock_bh(&mpath->state_lock);
98 * mesh_path_lookup_by_idx - look up a path in the mesh path table by its index
100 * @sdata: local subif, or NULL for all entries
102 * Returns: pointer to the mesh path structure, or NULL if not found.
104 * Locking: must be called within a read rcu section.
106 struct mesh_path *mesh_path_lookup_by_idx(int idx, struct ieee80211_sub_if_data *sdata)
108 struct mpath_node *node;
109 struct hlist_node *p;
113 for_each_mesh_entry(mesh_paths, p, node, i) {
114 if (sdata && node->mpath->sdata != sdata)
117 if (MPATH_EXPIRED(node->mpath)) {
118 spin_lock_bh(&node->mpath->state_lock);
119 if (MPATH_EXPIRED(node->mpath))
120 node->mpath->flags &= ~MESH_PATH_ACTIVE;
121 spin_unlock_bh(&node->mpath->state_lock);
131 * mesh_path_add - allocate and add a new path to the mesh path table
132 * @addr: destination address of the path (ETH_ALEN length)
133 * @sdata: local subif
135 * Returns: 0 on sucess
137 * State: the initial state of the new path is set to 0
139 int mesh_path_add(u8 *dst, struct ieee80211_sub_if_data *sdata)
141 struct mesh_path *mpath, *new_mpath;
142 struct mpath_node *node, *new_node;
143 struct hlist_head *bucket;
144 struct hlist_node *n;
149 if (memcmp(dst, sdata->dev->dev_addr, ETH_ALEN) == 0)
150 /* never add ourselves as neighbours */
153 if (is_multicast_ether_addr(dst))
156 if (atomic_add_unless(&sdata->u.sta.mpaths, 1, MESH_MAX_MPATHS) == 0)
160 new_mpath = kzalloc(sizeof(struct mesh_path), GFP_KERNEL);
164 new_node = kmalloc(sizeof(struct mpath_node), GFP_KERNEL);
168 read_lock(&pathtbl_resize_lock);
169 memcpy(new_mpath->dst, dst, ETH_ALEN);
170 new_mpath->sdata = sdata;
171 new_mpath->flags = 0;
172 skb_queue_head_init(&new_mpath->frame_queue);
173 new_node->mpath = new_mpath;
174 new_mpath->timer.data = (unsigned long) new_mpath;
175 new_mpath->timer.function = mesh_path_timer;
176 new_mpath->exp_time = jiffies;
177 spin_lock_init(&new_mpath->state_lock);
178 init_timer(&new_mpath->timer);
180 hash_idx = mesh_table_hash(dst, sdata, mesh_paths);
181 bucket = &mesh_paths->hash_buckets[hash_idx];
183 spin_lock(&mesh_paths->hashwlock[hash_idx]);
186 hlist_for_each_entry(node, n, bucket, list) {
188 if (mpath->sdata == sdata && memcmp(dst, mpath->dst, ETH_ALEN) == 0)
192 hlist_add_head_rcu(&new_node->list, bucket);
193 if (atomic_inc_return(&mesh_paths->entries) >=
194 mesh_paths->mean_chain_len * (mesh_paths->hash_mask + 1))
197 spin_unlock(&mesh_paths->hashwlock[hash_idx]);
198 read_unlock(&pathtbl_resize_lock);
200 struct mesh_table *oldtbl, *newtbl;
202 write_lock(&pathtbl_resize_lock);
204 newtbl = mesh_table_grow(mesh_paths);
206 write_unlock(&pathtbl_resize_lock);
209 rcu_assign_pointer(mesh_paths, newtbl);
210 write_unlock(&pathtbl_resize_lock);
213 mesh_table_free(oldtbl, false);
218 spin_unlock(&mesh_paths->hashwlock[hash_idx]);
219 read_unlock(&pathtbl_resize_lock);
224 atomic_dec(&sdata->u.sta.mpaths);
230 * mesh_plink_broken - deactivates paths and sends perr when a link breaks
232 * @sta: broken peer link
234 * This function must be called from the rate control algorithm if enough
235 * delivery errors suggest that a peer link is no longer usable.
237 void mesh_plink_broken(struct sta_info *sta)
239 struct mesh_path *mpath;
240 struct mpath_node *node;
241 struct hlist_node *p;
242 struct ieee80211_sub_if_data *sdata = sta->sdata;
246 for_each_mesh_entry(mesh_paths, p, node, i) {
248 spin_lock_bh(&mpath->state_lock);
249 if (mpath->next_hop == sta &&
250 mpath->flags & MESH_PATH_ACTIVE &&
251 !(mpath->flags & MESH_PATH_FIXED)) {
252 mpath->flags &= ~MESH_PATH_ACTIVE;
254 spin_unlock_bh(&mpath->state_lock);
255 mesh_path_error_tx(mpath->dst,
256 cpu_to_le32(mpath->dsn),
257 sdata->dev->broadcast, sdata);
259 spin_unlock_bh(&mpath->state_lock);
265 * mesh_path_flush_by_nexthop - Deletes mesh paths if their next hop matches
267 * @sta - mesh peer to match
269 * RCU notes: this function is called when a mesh plink transitions from
270 * PLINK_ESTAB to any other state, since PLINK_ESTAB state is the only one that
271 * allows path creation. This will happen before the sta can be freed (because
272 * sta_info_destroy() calls this) so any reader in a rcu read block will be
273 * protected against the plink disappearing.
275 void mesh_path_flush_by_nexthop(struct sta_info *sta)
277 struct mesh_path *mpath;
278 struct mpath_node *node;
279 struct hlist_node *p;
282 for_each_mesh_entry(mesh_paths, p, node, i) {
284 if (mpath->next_hop == sta)
285 mesh_path_del(mpath->dst, mpath->sdata);
289 void mesh_path_flush(struct ieee80211_sub_if_data *sdata)
291 struct mesh_path *mpath;
292 struct mpath_node *node;
293 struct hlist_node *p;
296 for_each_mesh_entry(mesh_paths, p, node, i) {
298 if (mpath->sdata == sdata)
299 mesh_path_del(mpath->dst, mpath->sdata);
303 static void mesh_path_node_reclaim(struct rcu_head *rp)
305 struct mpath_node *node = container_of(rp, struct mpath_node, rcu);
306 struct ieee80211_sub_if_data *sdata = node->mpath->sdata;
308 del_timer_sync(&node->mpath->timer);
309 atomic_dec(&sdata->u.sta.mpaths);
315 * mesh_path_del - delete a mesh path from the table
317 * @addr: dst address (ETH_ALEN length)
318 * @sdata: local subif
320 * Returns: 0 if succesful
322 int mesh_path_del(u8 *addr, struct ieee80211_sub_if_data *sdata)
324 struct mesh_path *mpath;
325 struct mpath_node *node;
326 struct hlist_head *bucket;
327 struct hlist_node *n;
331 read_lock(&pathtbl_resize_lock);
332 hash_idx = mesh_table_hash(addr, sdata, mesh_paths);
333 bucket = &mesh_paths->hash_buckets[hash_idx];
335 spin_lock(&mesh_paths->hashwlock[hash_idx]);
336 hlist_for_each_entry(node, n, bucket, list) {
338 if (mpath->sdata == sdata &&
339 memcmp(addr, mpath->dst, ETH_ALEN) == 0) {
340 spin_lock_bh(&mpath->state_lock);
341 mpath->flags |= MESH_PATH_RESOLVING;
342 hlist_del_rcu(&node->list);
343 call_rcu(&node->rcu, mesh_path_node_reclaim);
344 atomic_dec(&mesh_paths->entries);
345 spin_unlock_bh(&mpath->state_lock);
352 spin_unlock(&mesh_paths->hashwlock[hash_idx]);
353 read_unlock(&pathtbl_resize_lock);
358 * mesh_path_tx_pending - sends pending frames in a mesh path queue
360 * @mpath: mesh path to activate
362 * Locking: the state_lock of the mpath structure must NOT be held when calling
365 void mesh_path_tx_pending(struct mesh_path *mpath)
369 while ((skb = skb_dequeue(&mpath->frame_queue)) &&
370 (mpath->flags & MESH_PATH_ACTIVE))
375 * mesh_path_discard_frame - discard a frame whose path could not be resolved
377 * @skb: frame to discard
378 * @sdata: network subif the frame was to be sent through
380 * If the frame was beign forwarded from another MP, a PERR frame will be sent
383 * Locking: the function must me called within a rcu_read_lock region
385 void mesh_path_discard_frame(struct sk_buff *skb,
386 struct ieee80211_sub_if_data *sdata)
388 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
389 struct mesh_path *mpath;
392 if (memcmp(hdr->addr4, sdata->dev->dev_addr, ETH_ALEN) != 0) {
397 mpath = mesh_path_lookup(da, sdata);
400 mesh_path_error_tx(skb->data, cpu_to_le32(dsn), ra, sdata);
404 sdata->u.sta.mshstats.dropped_frames_no_route++;
408 * mesh_path_flush_pending - free the pending queue of a mesh path
410 * @mpath: mesh path whose queue has to be freed
412 * Locking: the function must me called withing a rcu_read_lock region
414 void mesh_path_flush_pending(struct mesh_path *mpath)
418 while ((skb = skb_dequeue(&mpath->frame_queue)) &&
419 (mpath->flags & MESH_PATH_ACTIVE))
420 mesh_path_discard_frame(skb, mpath->sdata);
424 * mesh_path_fix_nexthop - force a specific next hop for a mesh path
426 * @mpath: the mesh path to modify
427 * @next_hop: the next hop to force
429 * Locking: this function must be called holding mpath->state_lock
431 void mesh_path_fix_nexthop(struct mesh_path *mpath, struct sta_info *next_hop)
433 spin_lock_bh(&mpath->state_lock);
434 mesh_path_assign_nexthop(mpath, next_hop);
437 mpath->hop_count = 0;
439 mpath->flags |= MESH_PATH_FIXED;
440 mesh_path_activate(mpath);
441 spin_unlock_bh(&mpath->state_lock);
442 mesh_path_tx_pending(mpath);
445 static void mesh_path_node_free(struct hlist_node *p, bool free_leafs)
447 struct mesh_path *mpath;
448 struct mpath_node *node = hlist_entry(p, struct mpath_node, list);
456 static int mesh_path_node_copy(struct hlist_node *p, struct mesh_table *newtbl)
458 struct mesh_path *mpath;
459 struct mpath_node *node, *new_node;
462 new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
463 if (new_node == NULL)
466 node = hlist_entry(p, struct mpath_node, list);
468 new_node->mpath = mpath;
469 hash_idx = mesh_table_hash(mpath->dst, mpath->sdata, newtbl);
470 hlist_add_head(&new_node->list,
471 &newtbl->hash_buckets[hash_idx]);
475 int mesh_pathtbl_init(void)
477 mesh_paths = mesh_table_alloc(INIT_PATHS_SIZE_ORDER);
478 mesh_paths->free_node = &mesh_path_node_free;
479 mesh_paths->copy_node = &mesh_path_node_copy;
480 mesh_paths->mean_chain_len = MEAN_CHAIN_LEN;
486 void mesh_path_expire(struct ieee80211_sub_if_data *sdata)
488 struct mesh_path *mpath;
489 struct mpath_node *node;
490 struct hlist_node *p;
493 read_lock(&pathtbl_resize_lock);
494 for_each_mesh_entry(mesh_paths, p, node, i) {
495 if (node->mpath->sdata != sdata)
498 spin_lock_bh(&mpath->state_lock);
499 if ((!(mpath->flags & MESH_PATH_RESOLVING)) &&
500 (!(mpath->flags & MESH_PATH_FIXED)) &&
502 mpath->exp_time + MESH_PATH_EXPIRE)) {
503 spin_unlock_bh(&mpath->state_lock);
504 mesh_path_del(mpath->dst, mpath->sdata);
506 spin_unlock_bh(&mpath->state_lock);
508 read_unlock(&pathtbl_resize_lock);
511 void mesh_pathtbl_unregister(void)
513 mesh_table_free(mesh_paths, true);