]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - net/mac80211/mesh_pathtbl.c
Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/horms/lvs...
[linux-2.6-omap-h63xx.git] / net / mac80211 / mesh_pathtbl.c
1 /*
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>
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"
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
29 struct 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
38 static struct mesh_table *mesh_paths;
39
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
42  * by RCU
43  */
44 static DEFINE_RWLOCK(pathtbl_resize_lock);
45
46 /**
47  *
48  * mesh_path_assign_nexthop - update mesh path next hop
49  *
50  * @mpath: mesh path to update
51  * @sta: next hop to assign
52  *
53  * Locking: mpath->state_lock must be held when calling this function
54  */
55 void mesh_path_assign_nexthop(struct mesh_path *mpath, struct sta_info *sta)
56 {
57         rcu_assign_pointer(mpath->next_hop, sta);
58 }
59
60
61 /**
62  * mesh_path_lookup - look up a path in the mesh path table
63  * @dst: hardware address (ETH_ALEN length) of destination
64  * @sdata: local subif
65  *
66  * Returns: pointer to the mesh path structure, or NULL if not found
67  *
68  * Locking: must be called within a read rcu section.
69  */
70 struct mesh_path *mesh_path_lookup(u8 *dst, struct ieee80211_sub_if_data *sdata)
71 {
72         struct mesh_path *mpath;
73         struct hlist_node *n;
74         struct hlist_head *bucket;
75         struct mesh_table *tbl;
76         struct mpath_node *node;
77
78         tbl = rcu_dereference(mesh_paths);
79
80         bucket = &tbl->hash_buckets[mesh_table_hash(dst, sdata, tbl)];
81         hlist_for_each_entry_rcu(node, n, bucket, list) {
82                 mpath = node->mpath;
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);
90                         }
91                         return mpath;
92                 }
93         }
94         return NULL;
95 }
96
97 /**
98  * mesh_path_lookup_by_idx - look up a path in the mesh path table by its index
99  * @idx: index
100  * @sdata: local subif, or NULL for all entries
101  *
102  * Returns: pointer to the mesh path structure, or NULL if not found.
103  *
104  * Locking: must be called within a read rcu section.
105  */
106 struct mesh_path *mesh_path_lookup_by_idx(int idx, struct ieee80211_sub_if_data *sdata)
107 {
108         struct mpath_node *node;
109         struct hlist_node *p;
110         int i;
111         int j = 0;
112
113         for_each_mesh_entry(mesh_paths, p, node, i) {
114                 if (sdata && node->mpath->sdata != sdata)
115                         continue;
116                 if (j++ == idx) {
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);
122                         }
123                         return node->mpath;
124                 }
125         }
126
127         return NULL;
128 }
129
130 /**
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
134  *
135  * Returns: 0 on sucess
136  *
137  * State: the initial state of the new path is set to 0
138  */
139 int mesh_path_add(u8 *dst, struct ieee80211_sub_if_data *sdata)
140 {
141         struct mesh_path *mpath, *new_mpath;
142         struct mpath_node *node, *new_node;
143         struct hlist_head *bucket;
144         struct hlist_node *n;
145         int grow = 0;
146         int err = 0;
147         u32 hash_idx;
148
149         if (memcmp(dst, sdata->dev->dev_addr, ETH_ALEN) == 0)
150                 /* never add ourselves as neighbours */
151                 return -ENOTSUPP;
152
153         if (is_multicast_ether_addr(dst))
154                 return -ENOTSUPP;
155
156         if (atomic_add_unless(&sdata->u.sta.mpaths, 1, MESH_MAX_MPATHS) == 0)
157                 return -ENOSPC;
158
159         err = -ENOMEM;
160         new_mpath = kzalloc(sizeof(struct mesh_path), GFP_KERNEL);
161         if (!new_mpath)
162                 goto err_path_alloc;
163
164         new_node = kmalloc(sizeof(struct mpath_node), GFP_KERNEL);
165         if (!new_node)
166                 goto err_node_alloc;
167
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);
179
180         hash_idx = mesh_table_hash(dst, sdata, mesh_paths);
181         bucket = &mesh_paths->hash_buckets[hash_idx];
182
183         spin_lock(&mesh_paths->hashwlock[hash_idx]);
184
185         err = -EEXIST;
186         hlist_for_each_entry(node, n, bucket, list) {
187                 mpath = node->mpath;
188                 if (mpath->sdata == sdata && memcmp(dst, mpath->dst, ETH_ALEN) == 0)
189                         goto err_exists;
190         }
191
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))
195                 grow = 1;
196
197         spin_unlock(&mesh_paths->hashwlock[hash_idx]);
198         read_unlock(&pathtbl_resize_lock);
199         if (grow) {
200                 struct mesh_table *oldtbl, *newtbl;
201
202                 write_lock(&pathtbl_resize_lock);
203                 oldtbl = mesh_paths;
204                 newtbl = mesh_table_grow(mesh_paths);
205                 if (!newtbl) {
206                         write_unlock(&pathtbl_resize_lock);
207                         return 0;
208                 }
209                 rcu_assign_pointer(mesh_paths, newtbl);
210                 write_unlock(&pathtbl_resize_lock);
211
212                 synchronize_rcu();
213                 mesh_table_free(oldtbl, false);
214         }
215         return 0;
216
217 err_exists:
218         spin_unlock(&mesh_paths->hashwlock[hash_idx]);
219         read_unlock(&pathtbl_resize_lock);
220         kfree(new_node);
221 err_node_alloc:
222         kfree(new_mpath);
223 err_path_alloc:
224         atomic_dec(&sdata->u.sta.mpaths);
225         return err;
226 }
227
228
229 /**
230  * mesh_plink_broken - deactivates paths and sends perr when a link breaks
231  *
232  * @sta: broken peer link
233  *
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.
236  */
237 void mesh_plink_broken(struct sta_info *sta)
238 {
239         struct mesh_path *mpath;
240         struct mpath_node *node;
241         struct hlist_node *p;
242         struct ieee80211_sub_if_data *sdata = sta->sdata;
243         int i;
244
245         rcu_read_lock();
246         for_each_mesh_entry(mesh_paths, p, node, i) {
247                 mpath = node->mpath;
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;
253                         ++mpath->dsn;
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);
258                 } else
259                 spin_unlock_bh(&mpath->state_lock);
260         }
261         rcu_read_unlock();
262 }
263
264 /**
265  * mesh_path_flush_by_nexthop - Deletes mesh paths if their next hop matches
266  *
267  * @sta - mesh peer to match
268  *
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.
274  */
275 void mesh_path_flush_by_nexthop(struct sta_info *sta)
276 {
277         struct mesh_path *mpath;
278         struct mpath_node *node;
279         struct hlist_node *p;
280         int i;
281
282         for_each_mesh_entry(mesh_paths, p, node, i) {
283                 mpath = node->mpath;
284                 if (mpath->next_hop == sta)
285                         mesh_path_del(mpath->dst, mpath->sdata);
286         }
287 }
288
289 void mesh_path_flush(struct ieee80211_sub_if_data *sdata)
290 {
291         struct mesh_path *mpath;
292         struct mpath_node *node;
293         struct hlist_node *p;
294         int i;
295
296         for_each_mesh_entry(mesh_paths, p, node, i) {
297                 mpath = node->mpath;
298                 if (mpath->sdata == sdata)
299                         mesh_path_del(mpath->dst, mpath->sdata);
300         }
301 }
302
303 static void mesh_path_node_reclaim(struct rcu_head *rp)
304 {
305         struct mpath_node *node = container_of(rp, struct mpath_node, rcu);
306         struct ieee80211_sub_if_data *sdata = node->mpath->sdata;
307
308         del_timer_sync(&node->mpath->timer);
309         atomic_dec(&sdata->u.sta.mpaths);
310         kfree(node->mpath);
311         kfree(node);
312 }
313
314 /**
315  * mesh_path_del - delete a mesh path from the table
316  *
317  * @addr: dst address (ETH_ALEN length)
318  * @sdata: local subif
319  *
320  * Returns: 0 if succesful
321  */
322 int mesh_path_del(u8 *addr, struct ieee80211_sub_if_data *sdata)
323 {
324         struct mesh_path *mpath;
325         struct mpath_node *node;
326         struct hlist_head *bucket;
327         struct hlist_node *n;
328         int hash_idx;
329         int err = 0;
330
331         read_lock(&pathtbl_resize_lock);
332         hash_idx = mesh_table_hash(addr, sdata, mesh_paths);
333         bucket = &mesh_paths->hash_buckets[hash_idx];
334
335         spin_lock(&mesh_paths->hashwlock[hash_idx]);
336         hlist_for_each_entry(node, n, bucket, list) {
337                 mpath = node->mpath;
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);
346                         goto enddel;
347                 }
348         }
349
350         err = -ENXIO;
351 enddel:
352         spin_unlock(&mesh_paths->hashwlock[hash_idx]);
353         read_unlock(&pathtbl_resize_lock);
354         return err;
355 }
356
357 /**
358  * mesh_path_tx_pending - sends pending frames in a mesh path queue
359  *
360  * @mpath: mesh path to activate
361  *
362  * Locking: the state_lock of the mpath structure must NOT be held when calling
363  * this function.
364  */
365 void mesh_path_tx_pending(struct mesh_path *mpath)
366 {
367         struct sk_buff *skb;
368
369         while ((skb = skb_dequeue(&mpath->frame_queue)) &&
370                         (mpath->flags & MESH_PATH_ACTIVE))
371                 dev_queue_xmit(skb);
372 }
373
374 /**
375  * mesh_path_discard_frame - discard a frame whose path could not be resolved
376  *
377  * @skb: frame to discard
378  * @sdata: network subif the frame was to be sent through
379  *
380  * If the frame was beign forwarded from another MP, a PERR frame will be sent
381  * to the precursor.
382  *
383  * Locking: the function must me called within a rcu_read_lock region
384  */
385 void mesh_path_discard_frame(struct sk_buff *skb,
386                              struct ieee80211_sub_if_data *sdata)
387 {
388         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
389         struct mesh_path *mpath;
390         u32 dsn = 0;
391
392         if (memcmp(hdr->addr4, sdata->dev->dev_addr, ETH_ALEN) != 0) {
393                 u8 *ra, *da;
394
395                 da = hdr->addr3;
396                 ra = hdr->addr2;
397                 mpath = mesh_path_lookup(da, sdata);
398                 if (mpath)
399                         dsn = ++mpath->dsn;
400                 mesh_path_error_tx(skb->data, cpu_to_le32(dsn), ra, sdata);
401         }
402
403         kfree_skb(skb);
404         sdata->u.sta.mshstats.dropped_frames_no_route++;
405 }
406
407 /**
408  * mesh_path_flush_pending - free the pending queue of a mesh path
409  *
410  * @mpath: mesh path whose queue has to be freed
411  *
412  * Locking: the function must me called withing a rcu_read_lock region
413  */
414 void mesh_path_flush_pending(struct mesh_path *mpath)
415 {
416         struct sk_buff *skb;
417
418         while ((skb = skb_dequeue(&mpath->frame_queue)) &&
419                         (mpath->flags & MESH_PATH_ACTIVE))
420                 mesh_path_discard_frame(skb, mpath->sdata);
421 }
422
423 /**
424  * mesh_path_fix_nexthop - force a specific next hop for a mesh path
425  *
426  * @mpath: the mesh path to modify
427  * @next_hop: the next hop to force
428  *
429  * Locking: this function must be called holding mpath->state_lock
430  */
431 void mesh_path_fix_nexthop(struct mesh_path *mpath, struct sta_info *next_hop)
432 {
433         spin_lock_bh(&mpath->state_lock);
434         mesh_path_assign_nexthop(mpath, next_hop);
435         mpath->dsn = 0xffff;
436         mpath->metric = 0;
437         mpath->hop_count = 0;
438         mpath->exp_time = 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);
443 }
444
445 static void mesh_path_node_free(struct hlist_node *p, bool free_leafs)
446 {
447         struct mesh_path *mpath;
448         struct mpath_node *node = hlist_entry(p, struct mpath_node, list);
449         mpath = node->mpath;
450         hlist_del_rcu(p);
451         if (free_leafs)
452                 kfree(mpath);
453         kfree(node);
454 }
455
456 static int mesh_path_node_copy(struct hlist_node *p, struct mesh_table *newtbl)
457 {
458         struct mesh_path *mpath;
459         struct mpath_node *node, *new_node;
460         u32 hash_idx;
461
462         new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
463         if (new_node == NULL)
464                 return -ENOMEM;
465
466         node = hlist_entry(p, struct mpath_node, list);
467         mpath = node->mpath;
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]);
472         return 0;
473 }
474
475 int mesh_pathtbl_init(void)
476 {
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;
481         if (!mesh_paths)
482                 return -ENOMEM;
483         return 0;
484 }
485
486 void mesh_path_expire(struct ieee80211_sub_if_data *sdata)
487 {
488         struct mesh_path *mpath;
489         struct mpath_node *node;
490         struct hlist_node *p;
491         int i;
492
493         read_lock(&pathtbl_resize_lock);
494         for_each_mesh_entry(mesh_paths, p, node, i) {
495                 if (node->mpath->sdata != sdata)
496                         continue;
497                 mpath = node->mpath;
498                 spin_lock_bh(&mpath->state_lock);
499                 if ((!(mpath->flags & MESH_PATH_RESOLVING)) &&
500                     (!(mpath->flags & MESH_PATH_FIXED)) &&
501                         time_after(jiffies,
502                          mpath->exp_time + MESH_PATH_EXPIRE)) {
503                         spin_unlock_bh(&mpath->state_lock);
504                         mesh_path_del(mpath->dst, mpath->sdata);
505                 } else
506                         spin_unlock_bh(&mpath->state_lock);
507         }
508         read_unlock(&pathtbl_resize_lock);
509 }
510
511 void mesh_pathtbl_unregister(void)
512 {
513         mesh_table_free(mesh_paths, true);
514 }