mac80211: don't clear next_hop in path reclaim
[safe/jmp/linux-2.6] / 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/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"
18 #include "mesh.h"
19
20 /* There will be initially 2^INIT_PATHS_SIZE_ORDER buckets */
21 #define INIT_PATHS_SIZE_ORDER   2
22
23 /* Keep the mean chain length below this constant */
24 #define MEAN_CHAIN_LEN          2
25
26 #define MPATH_EXPIRED(mpath) ((mpath->flags & MESH_PATH_ACTIVE) && \
27                                 time_after(jiffies, mpath->exp_time) && \
28                                 !(mpath->flags & MESH_PATH_FIXED))
29
30 struct mpath_node {
31         struct hlist_node list;
32         struct rcu_head rcu;
33         /* This indirection allows two different tables to point to the same
34          * mesh_path structure, useful when resizing
35          */
36         struct mesh_path *mpath;
37 };
38
39 static struct mesh_table *mesh_paths;
40
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  */
45 static 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  */
56 void mesh_path_assign_nexthop(struct mesh_path *mpath, struct sta_info *sta)
57 {
58         rcu_assign_pointer(mpath->next_hop, sta);
59 }
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
65  * @dev: local interface
66  *
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  */
71 struct mesh_path *mesh_path_lookup(u8 *dst, struct net_device *dev)
72 {
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
81         bucket = &tbl->hash_buckets[mesh_table_hash(dst, dev, tbl)];
82         hlist_for_each_entry_rcu(node, n, bucket, list) {
83                 mpath = node->mpath;
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);
91                         }
92                         return mpath;
93                 }
94         }
95         return NULL;
96 }
97
98 /**
99  * mesh_path_lookup_by_idx - look up a path in the mesh path table by its index
100  * @idx: index
101  * @dev: local interface
102  *
103  * Returns: pointer to the mesh path structure, or NULL if not found.
104  *
105  * Locking: must be called within a read rcu section.
106  */
107 struct mesh_path *mesh_path_lookup_by_idx(int idx, struct net_device *dev)
108 {
109         struct mpath_node *node;
110         struct hlist_node *p;
111         int i;
112         int j = 0;
113
114         for_each_mesh_entry(mesh_paths, p, node, i)
115                 if (j++ == idx) {
116                         if (MPATH_EXPIRED(node->mpath)) {
117                                 spin_lock_bh(&node->mpath->state_lock);
118                                 if (MPATH_EXPIRED(node->mpath))
119                                         node->mpath->flags &= ~MESH_PATH_ACTIVE;
120                                 spin_unlock_bh(&node->mpath->state_lock);
121                         }
122                         return node->mpath;
123                 }
124
125         return NULL;
126 }
127
128 /**
129  * mesh_path_add - allocate and add a new path to the mesh path table
130  * @addr: destination address of the path (ETH_ALEN length)
131  * @dev: local interface
132  *
133  * Returns: 0 on sucess
134  *
135  * State: the initial state of the new path is set to 0
136  */
137 int mesh_path_add(u8 *dst, struct net_device *dev)
138 {
139         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
140         struct mesh_path *mpath, *new_mpath;
141         struct mpath_node *node, *new_node;
142         struct hlist_head *bucket;
143         struct hlist_node *n;
144         int grow = 0;
145         int err = 0;
146         u32 hash_idx;
147
148         if (memcmp(dst, dev->dev_addr, ETH_ALEN) == 0)
149                 /* never add ourselves as neighbours */
150                 return -ENOTSUPP;
151
152         if (is_multicast_ether_addr(dst))
153                 return -ENOTSUPP;
154
155         if (atomic_add_unless(&sdata->u.sta.mpaths, 1, MESH_MAX_MPATHS) == 0)
156                 return -ENOSPC;
157
158         read_lock(&pathtbl_resize_lock);
159
160         new_mpath = kzalloc(sizeof(struct mesh_path), GFP_KERNEL);
161         if (!new_mpath) {
162                 atomic_dec(&sdata->u.sta.mpaths);
163                 err = -ENOMEM;
164                 goto endadd2;
165         }
166         memcpy(new_mpath->dst, dst, ETH_ALEN);
167         new_mpath->dev = dev;
168         new_mpath->flags = 0;
169         skb_queue_head_init(&new_mpath->frame_queue);
170         new_node = kmalloc(sizeof(struct mpath_node), GFP_KERNEL);
171         new_node->mpath = new_mpath;
172         new_mpath->timer.data = (unsigned long) new_mpath;
173         new_mpath->timer.function = mesh_path_timer;
174         new_mpath->exp_time = jiffies;
175         spin_lock_init(&new_mpath->state_lock);
176         init_timer(&new_mpath->timer);
177
178         hash_idx = mesh_table_hash(dst, dev, mesh_paths);
179         bucket = &mesh_paths->hash_buckets[hash_idx];
180
181         spin_lock(&mesh_paths->hashwlock[hash_idx]);
182
183         hlist_for_each_entry(node, n, bucket, list) {
184                 mpath = node->mpath;
185                 if (mpath->dev == dev && memcmp(dst, mpath->dst, ETH_ALEN)
186                                 == 0) {
187                         err = -EEXIST;
188                         atomic_dec(&sdata->u.sta.mpaths);
189                         kfree(new_node);
190                         kfree(new_mpath);
191                         goto endadd;
192                 }
193         }
194
195         hlist_add_head_rcu(&new_node->list, bucket);
196         if (atomic_inc_return(&mesh_paths->entries) >=
197                 mesh_paths->mean_chain_len * (mesh_paths->hash_mask + 1))
198                 grow = 1;
199
200 endadd:
201         spin_unlock(&mesh_paths->hashwlock[hash_idx]);
202 endadd2:
203         read_unlock(&pathtbl_resize_lock);
204         if (!err && grow) {
205                 struct mesh_table *oldtbl, *newtbl;
206
207                 write_lock(&pathtbl_resize_lock);
208                 oldtbl = mesh_paths;
209                 newtbl = mesh_table_grow(mesh_paths);
210                 if (!newtbl) {
211                         write_unlock(&pathtbl_resize_lock);
212                         return -ENOMEM;
213                 }
214                 rcu_assign_pointer(mesh_paths, newtbl);
215                 synchronize_rcu();
216                 mesh_table_free(oldtbl, false);
217                 write_unlock(&pathtbl_resize_lock);
218         }
219         return err;
220 }
221
222
223 /**
224  * mesh_plink_broken - deactivates paths and sends perr when a link breaks
225  *
226  * @sta: broken peer link
227  *
228  * This function must be called from the rate control algorithm if enough
229  * delivery errors suggest that a peer link is no longer usable.
230  */
231 void mesh_plink_broken(struct sta_info *sta)
232 {
233         struct mesh_path *mpath;
234         struct mpath_node *node;
235         struct hlist_node *p;
236         struct net_device *dev = sta->sdata->dev;
237         int i;
238
239         rcu_read_lock();
240         for_each_mesh_entry(mesh_paths, p, node, i) {
241                 mpath = node->mpath;
242                 spin_lock_bh(&mpath->state_lock);
243                 if (mpath->next_hop == sta &&
244                     mpath->flags & MESH_PATH_ACTIVE &&
245                     !(mpath->flags & MESH_PATH_FIXED)) {
246                         mpath->flags &= ~MESH_PATH_ACTIVE;
247                         ++mpath->dsn;
248                         spin_unlock_bh(&mpath->state_lock);
249                         mesh_path_error_tx(mpath->dst,
250                                         cpu_to_le32(mpath->dsn),
251                                         dev->broadcast, dev);
252                 } else
253                 spin_unlock_bh(&mpath->state_lock);
254         }
255         rcu_read_unlock();
256 }
257 EXPORT_SYMBOL(mesh_plink_broken);
258
259 /**
260  * mesh_path_flush_by_nexthop - Deletes mesh paths if their next hop matches
261  *
262  * @sta - mesh peer to match
263  *
264  * RCU notes: this function is called when a mesh plink transitions from ESTAB
265  * to any other state, since ESTAB state is the only one that allows path
266  * creation. This will happen before the sta can be freed (because
267  * sta_info_destroy() calls this) so any reader in a rcu read block will be
268  * protected against the plink disappearing.
269  */
270 void mesh_path_flush_by_nexthop(struct sta_info *sta)
271 {
272         struct mesh_path *mpath;
273         struct mpath_node *node;
274         struct hlist_node *p;
275         int i;
276
277         for_each_mesh_entry(mesh_paths, p, node, i) {
278                 mpath = node->mpath;
279                 if (mpath->next_hop == sta)
280                         mesh_path_del(mpath->dst, mpath->dev, true);
281         }
282 }
283
284 void mesh_path_flush(struct net_device *dev)
285 {
286         struct mesh_path *mpath;
287         struct mpath_node *node;
288         struct hlist_node *p;
289         int i;
290
291         for_each_mesh_entry(mesh_paths, p, node, i) {
292                 mpath = node->mpath;
293                 if (mpath->dev == dev)
294                         mesh_path_del(mpath->dst, mpath->dev, false);
295         }
296 }
297
298 static void mesh_path_node_reclaim(struct rcu_head *rp)
299 {
300         struct mpath_node *node = container_of(rp, struct mpath_node, rcu);
301         struct ieee80211_sub_if_data *sdata =
302                 IEEE80211_DEV_TO_SUB_IF(node->mpath->dev);
303
304         atomic_dec(&sdata->u.sta.mpaths);
305         kfree(node->mpath);
306         kfree(node);
307 }
308
309 /**
310  * mesh_path_del - delete a mesh path from the table
311  *
312  * @addr: dst address (ETH_ALEN length)
313  * @dev: local interface
314  *
315  * Returns: 0 if succesful
316  *
317  * State: if the path is being resolved, the deletion will be postponed until
318  * the path resolution completes or times out, unless the force parameter
319  * is given.
320  */
321 int mesh_path_del(u8 *addr, struct net_device *dev, bool force)
322 {
323         struct mesh_path *mpath;
324         struct mpath_node *node;
325         struct hlist_head *bucket;
326         struct hlist_node *n;
327         int hash_idx;
328         int err = 0;
329
330         read_lock(&pathtbl_resize_lock);
331         hash_idx = mesh_table_hash(addr, dev, mesh_paths);
332         bucket = &mesh_paths->hash_buckets[hash_idx];
333
334         spin_lock(&mesh_paths->hashwlock[hash_idx]);
335         hlist_for_each_entry(node, n, bucket, list) {
336                 mpath = node->mpath;
337                 if (mpath->dev == dev &&
338                                 memcmp(addr, mpath->dst, ETH_ALEN) == 0) {
339                         spin_lock_bh(&mpath->state_lock);
340                         if (!force && mpath->flags & MESH_PATH_RESOLVING) {
341                                 mpath->flags |= MESH_PATH_DELETE;
342                         } else {
343                                 mpath->flags |= MESH_PATH_RESOLVING;
344                                 hlist_del_rcu(&node->list);
345                                 call_rcu(&node->rcu, mesh_path_node_reclaim);
346                                 atomic_dec(&mesh_paths->entries);
347                         }
348                         spin_unlock_bh(&mpath->state_lock);
349                         goto enddel;
350                 }
351         }
352
353         err = -ENXIO;
354 enddel:
355         spin_unlock(&mesh_paths->hashwlock[hash_idx]);
356         read_unlock(&pathtbl_resize_lock);
357         return err;
358 }
359
360 /**
361  * mesh_path_tx_pending - sends pending frames in a mesh path queue
362  *
363  * @mpath: mesh path to activate
364  *
365  * Locking: the state_lock of the mpath structure must NOT be held when calling
366  * this function.
367  */
368 void mesh_path_tx_pending(struct mesh_path *mpath)
369 {
370         struct sk_buff *skb;
371
372         while ((skb = skb_dequeue(&mpath->frame_queue)) &&
373                         (mpath->flags & MESH_PATH_ACTIVE))
374                 dev_queue_xmit(skb);
375 }
376
377 /**
378  * mesh_path_discard_frame - discard a frame whose path could not be resolved
379  *
380  * @skb: frame to discard
381  * @dev: network device the frame was to be sent through
382  *
383  * If the frame was beign forwarded from another MP, a PERR frame will be sent
384  * to the precursor.
385  *
386  * Locking: the function must me called within a rcu_read_lock region
387  */
388 void mesh_path_discard_frame(struct sk_buff *skb, struct net_device *dev)
389 {
390         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
391         struct mesh_path *mpath;
392         u32 dsn = 0;
393
394         if (skb->pkt_type == PACKET_OTHERHOST) {
395                 struct ieee80211s_hdr *prev_meshhdr;
396                 int mshhdrlen;
397                 u8 *ra, *da;
398
399                 prev_meshhdr = ((struct ieee80211s_hdr *)skb->cb);
400                 mshhdrlen = ieee80211_get_mesh_hdrlen(prev_meshhdr);
401                 da = skb->data;
402                 ra = MESH_PREQ(skb);
403                 mpath = mesh_path_lookup(da, dev);
404                 if (mpath)
405                         dsn = ++mpath->dsn;
406                 mesh_path_error_tx(skb->data, cpu_to_le32(dsn), ra, dev);
407         }
408
409         kfree_skb(skb);
410         sdata->u.sta.mshstats.dropped_frames_no_route++;
411 }
412
413 /**
414  * mesh_path_flush_pending - free the pending queue of a mesh path
415  *
416  * @mpath: mesh path whose queue has to be freed
417  *
418  * Locking: the function must me called withing a rcu_read_lock region
419  */
420 void mesh_path_flush_pending(struct mesh_path *mpath)
421 {
422         struct ieee80211_sub_if_data *sdata;
423         struct sk_buff *skb;
424
425         sdata = IEEE80211_DEV_TO_SUB_IF(mpath->dev);
426
427         while ((skb = skb_dequeue(&mpath->frame_queue)) &&
428                         (mpath->flags & MESH_PATH_ACTIVE))
429                 mesh_path_discard_frame(skb, mpath->dev);
430 }
431
432 /**
433  * mesh_path_fix_nexthop - force a specific next hop for a mesh path
434  *
435  * @mpath: the mesh path to modify
436  * @next_hop: the next hop to force
437  *
438  * Locking: this function must be called holding mpath->state_lock
439  */
440 void mesh_path_fix_nexthop(struct mesh_path *mpath, struct sta_info *next_hop)
441 {
442         spin_lock_bh(&mpath->state_lock);
443         mesh_path_assign_nexthop(mpath, next_hop);
444         mpath->dsn = 0xffff;
445         mpath->metric = 0;
446         mpath->hop_count = 0;
447         mpath->exp_time = 0;
448         mpath->flags |= MESH_PATH_FIXED;
449         mesh_path_activate(mpath);
450         spin_unlock_bh(&mpath->state_lock);
451         mesh_path_tx_pending(mpath);
452 }
453
454 static void mesh_path_node_free(struct hlist_node *p, bool free_leafs)
455 {
456         struct mesh_path *mpath;
457         struct mpath_node *node = hlist_entry(p, struct mpath_node, list);
458         mpath = node->mpath;
459         hlist_del_rcu(p);
460         synchronize_rcu();
461         if (free_leafs)
462                 kfree(mpath);
463         kfree(node);
464 }
465
466 static void mesh_path_node_copy(struct hlist_node *p, struct mesh_table *newtbl)
467 {
468         struct mesh_path *mpath;
469         struct mpath_node *node, *new_node;
470         u32 hash_idx;
471
472         node = hlist_entry(p, struct mpath_node, list);
473         mpath = node->mpath;
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]);
479 }
480
481 int mesh_pathtbl_init(void)
482 {
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;
487         if (!mesh_paths)
488                 return -ENOMEM;
489         return 0;
490 }
491
492 void mesh_path_expire(struct net_device *dev)
493 {
494         struct mesh_path *mpath;
495         struct mpath_node *node;
496         struct hlist_node *p;
497         int i;
498
499         read_lock(&pathtbl_resize_lock);
500         for_each_mesh_entry(mesh_paths, p, node, i) {
501                 if (node->mpath->dev != dev)
502                         continue;
503                 mpath = node->mpath;
504                 spin_lock_bh(&mpath->state_lock);
505                 if ((!(mpath->flags & MESH_PATH_RESOLVING)) &&
506                     (!(mpath->flags & MESH_PATH_FIXED)) &&
507                         time_after(jiffies,
508                          mpath->exp_time + MESH_PATH_EXPIRE)) {
509                         spin_unlock_bh(&mpath->state_lock);
510                         mesh_path_del(mpath->dst, mpath->dev, false);
511                 } else
512                         spin_unlock_bh(&mpath->state_lock);
513         }
514         read_unlock(&pathtbl_resize_lock);
515 }
516
517 void mesh_pathtbl_unregister(void)
518 {
519         mesh_table_free(mesh_paths, true);
520 }