blob: 7797f55eec46ec3e95817b090972c1acf84e3ecb [file] [log] [blame]
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001/*
Rui Paulo264d9b72009-11-09 23:46:58 +00002 * Copyright (c) 2008, 2009 open80211s Ltd.
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01003 * 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>
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +010012#include <linux/random.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +010014#include <linux/spinlock.h>
15#include <linux/string.h>
16#include <net/mac80211.h>
17#include "ieee80211_i.h"
18#include "mesh.h"
19
Javier Cardona76468872011-08-09 16:45:04 -070020#ifdef CONFIG_MAC80211_VERBOSE_MPATH_DEBUG
21#define mpath_dbg(fmt, args...) printk(KERN_DEBUG fmt, ##args)
22#else
23#define mpath_dbg(fmt, args...) do { (void)(0); } while (0)
24#endif
25
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +010026/* There will be initially 2^INIT_PATHS_SIZE_ORDER buckets */
27#define INIT_PATHS_SIZE_ORDER 2
28
29/* Keep the mean chain length below this constant */
30#define MEAN_CHAIN_LEN 2
31
32#define MPATH_EXPIRED(mpath) ((mpath->flags & MESH_PATH_ACTIVE) && \
33 time_after(jiffies, mpath->exp_time) && \
34 !(mpath->flags & MESH_PATH_FIXED))
35
36struct mpath_node {
37 struct hlist_node list;
38 struct rcu_head rcu;
39 /* This indirection allows two different tables to point to the same
40 * mesh_path structure, useful when resizing
41 */
42 struct mesh_path *mpath;
43};
44
Johannes Berg349eb8c2011-05-14 11:56:16 +020045static struct mesh_table __rcu *mesh_paths;
46static struct mesh_table __rcu *mpp_paths; /* Store paths for MPP&MAP */
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +010047
Johannes Bergf5ea9122009-08-07 16:17:38 +020048int mesh_paths_generation;
Johannes Berg6b86bd62011-05-12 13:38:50 +020049
50/* This lock will have the grow table function as writer and add / delete nodes
51 * as readers. When reading the table (i.e. doing lookups) we are well protected
Javier Cardona19c50b32011-08-29 13:23:07 -070052 * by RCU. We need to take this lock when modying the number of buckets
53 * on one of the path tables but we don't need to if adding or removing mpaths
54 * from hash buckets.
Johannes Berg6b86bd62011-05-12 13:38:50 +020055 */
56static DEFINE_RWLOCK(pathtbl_resize_lock);
57
58
Johannes Berg349eb8c2011-05-14 11:56:16 +020059static inline struct mesh_table *resize_dereference_mesh_paths(void)
60{
61 return rcu_dereference_protected(mesh_paths,
62 lockdep_is_held(&pathtbl_resize_lock));
63}
64
65static inline struct mesh_table *resize_dereference_mpp_paths(void)
66{
67 return rcu_dereference_protected(mpp_paths,
68 lockdep_is_held(&pathtbl_resize_lock));
69}
70
Javier Cardona5ee68e52011-08-09 16:45:08 -070071static int mesh_gate_add(struct mesh_table *tbl, struct mesh_path *mpath);
72
Johannes Berg349eb8c2011-05-14 11:56:16 +020073/*
74 * CAREFUL -- "tbl" must not be an expression,
75 * in particular not an rcu_dereference(), since
76 * it's used twice. So it is illegal to do
77 * for_each_mesh_entry(rcu_dereference(...), ...)
78 */
79#define for_each_mesh_entry(tbl, p, node, i) \
80 for (i = 0; i <= tbl->hash_mask; i++) \
81 hlist_for_each_entry_rcu(node, p, &tbl->hash_buckets[i], list)
82
83
Johannes Berg6b86bd62011-05-12 13:38:50 +020084static struct mesh_table *mesh_table_alloc(int size_order)
85{
86 int i;
87 struct mesh_table *newtbl;
88
Javier Cardonad676ff42011-05-17 16:13:34 -070089 newtbl = kmalloc(sizeof(struct mesh_table), GFP_ATOMIC);
Johannes Berg6b86bd62011-05-12 13:38:50 +020090 if (!newtbl)
91 return NULL;
92
93 newtbl->hash_buckets = kzalloc(sizeof(struct hlist_head) *
Javier Cardonad676ff42011-05-17 16:13:34 -070094 (1 << size_order), GFP_ATOMIC);
Johannes Berg6b86bd62011-05-12 13:38:50 +020095
96 if (!newtbl->hash_buckets) {
97 kfree(newtbl);
98 return NULL;
99 }
100
101 newtbl->hashwlock = kmalloc(sizeof(spinlock_t) *
Javier Cardonad676ff42011-05-17 16:13:34 -0700102 (1 << size_order), GFP_ATOMIC);
Johannes Berg6b86bd62011-05-12 13:38:50 +0200103 if (!newtbl->hashwlock) {
104 kfree(newtbl->hash_buckets);
105 kfree(newtbl);
106 return NULL;
107 }
108
109 newtbl->size_order = size_order;
110 newtbl->hash_mask = (1 << size_order) - 1;
111 atomic_set(&newtbl->entries, 0);
112 get_random_bytes(&newtbl->hash_rnd,
113 sizeof(newtbl->hash_rnd));
114 for (i = 0; i <= newtbl->hash_mask; i++)
115 spin_lock_init(&newtbl->hashwlock[i]);
Javier Cardona5ee68e52011-08-09 16:45:08 -0700116 spin_lock_init(&newtbl->gates_lock);
Johannes Berg6b86bd62011-05-12 13:38:50 +0200117
118 return newtbl;
119}
120
Javier Cardona18889232009-08-10 12:15:52 -0700121static void __mesh_table_free(struct mesh_table *tbl)
122{
123 kfree(tbl->hash_buckets);
124 kfree(tbl->hashwlock);
125 kfree(tbl);
126}
127
Johannes Berg6b86bd62011-05-12 13:38:50 +0200128static void mesh_table_free(struct mesh_table *tbl, bool free_leafs)
Javier Cardona18889232009-08-10 12:15:52 -0700129{
130 struct hlist_head *mesh_hash;
131 struct hlist_node *p, *q;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700132 struct mpath_node *gate;
Javier Cardona18889232009-08-10 12:15:52 -0700133 int i;
134
135 mesh_hash = tbl->hash_buckets;
136 for (i = 0; i <= tbl->hash_mask; i++) {
Javier Cardona9b84b8082011-05-03 16:57:16 -0700137 spin_lock_bh(&tbl->hashwlock[i]);
Javier Cardona18889232009-08-10 12:15:52 -0700138 hlist_for_each_safe(p, q, &mesh_hash[i]) {
139 tbl->free_node(p, free_leafs);
140 atomic_dec(&tbl->entries);
141 }
Javier Cardona9b84b8082011-05-03 16:57:16 -0700142 spin_unlock_bh(&tbl->hashwlock[i]);
Javier Cardona18889232009-08-10 12:15:52 -0700143 }
Javier Cardona5ee68e52011-08-09 16:45:08 -0700144 if (free_leafs) {
145 spin_lock_bh(&tbl->gates_lock);
146 hlist_for_each_entry_safe(gate, p, q,
147 tbl->known_gates, list) {
148 hlist_del(&gate->list);
149 kfree(gate);
150 }
151 kfree(tbl->known_gates);
152 spin_unlock_bh(&tbl->gates_lock);
153 }
154
Javier Cardona18889232009-08-10 12:15:52 -0700155 __mesh_table_free(tbl);
156}
157
cozybit Inca3e6b122011-04-13 11:10:28 -0700158static int mesh_table_grow(struct mesh_table *oldtbl,
Johannes Berg6b86bd62011-05-12 13:38:50 +0200159 struct mesh_table *newtbl)
Javier Cardona18889232009-08-10 12:15:52 -0700160{
Javier Cardona18889232009-08-10 12:15:52 -0700161 struct hlist_head *oldhash;
162 struct hlist_node *p, *q;
163 int i;
164
cozybit Inca3e6b122011-04-13 11:10:28 -0700165 if (atomic_read(&oldtbl->entries)
166 < oldtbl->mean_chain_len * (oldtbl->hash_mask + 1))
167 return -EAGAIN;
Javier Cardona18889232009-08-10 12:15:52 -0700168
cozybit Inca3e6b122011-04-13 11:10:28 -0700169 newtbl->free_node = oldtbl->free_node;
170 newtbl->mean_chain_len = oldtbl->mean_chain_len;
171 newtbl->copy_node = oldtbl->copy_node;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700172 newtbl->known_gates = oldtbl->known_gates;
cozybit Inca3e6b122011-04-13 11:10:28 -0700173 atomic_set(&newtbl->entries, atomic_read(&oldtbl->entries));
Javier Cardona18889232009-08-10 12:15:52 -0700174
cozybit Inca3e6b122011-04-13 11:10:28 -0700175 oldhash = oldtbl->hash_buckets;
176 for (i = 0; i <= oldtbl->hash_mask; i++)
Javier Cardona18889232009-08-10 12:15:52 -0700177 hlist_for_each(p, &oldhash[i])
cozybit Inca3e6b122011-04-13 11:10:28 -0700178 if (oldtbl->copy_node(p, newtbl) < 0)
Javier Cardona18889232009-08-10 12:15:52 -0700179 goto errcopy;
180
cozybit Inca3e6b122011-04-13 11:10:28 -0700181 return 0;
Javier Cardona18889232009-08-10 12:15:52 -0700182
183errcopy:
184 for (i = 0; i <= newtbl->hash_mask; i++) {
185 hlist_for_each_safe(p, q, &newtbl->hash_buckets[i])
cozybit Inca3e6b122011-04-13 11:10:28 -0700186 oldtbl->free_node(p, 0);
Javier Cardona18889232009-08-10 12:15:52 -0700187 }
cozybit Inca3e6b122011-04-13 11:10:28 -0700188 return -ENOMEM;
Javier Cardona18889232009-08-10 12:15:52 -0700189}
190
Johannes Berg6b86bd62011-05-12 13:38:50 +0200191static u32 mesh_table_hash(u8 *addr, struct ieee80211_sub_if_data *sdata,
192 struct mesh_table *tbl)
193{
194 /* Use last four bytes of hw addr and interface index as hash index */
195 return jhash_2words(*(u32 *)(addr+2), sdata->dev->ifindex, tbl->hash_rnd)
196 & tbl->hash_mask;
197}
Johannes Bergf5ea9122009-08-07 16:17:38 +0200198
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100199
200/**
201 *
202 * mesh_path_assign_nexthop - update mesh path next hop
203 *
204 * @mpath: mesh path to update
205 * @sta: next hop to assign
206 *
207 * Locking: mpath->state_lock must be held when calling this function
208 */
209void mesh_path_assign_nexthop(struct mesh_path *mpath, struct sta_info *sta)
210{
Javier Cardona10c836d2009-07-09 14:42:16 -0700211 struct sk_buff *skb;
212 struct ieee80211_hdr *hdr;
213 struct sk_buff_head tmpq;
214 unsigned long flags;
215
Johannes Bergd0709a62008-02-25 16:27:46 +0100216 rcu_assign_pointer(mpath->next_hop, sta);
Javier Cardona10c836d2009-07-09 14:42:16 -0700217
218 __skb_queue_head_init(&tmpq);
219
220 spin_lock_irqsave(&mpath->frame_queue.lock, flags);
221
222 while ((skb = __skb_dequeue(&mpath->frame_queue)) != NULL) {
223 hdr = (struct ieee80211_hdr *) skb->data;
224 memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN);
225 __skb_queue_tail(&tmpq, skb);
226 }
227
228 skb_queue_splice(&tmpq, &mpath->frame_queue);
229 spin_unlock_irqrestore(&mpath->frame_queue.lock, flags);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100230}
231
Javier Cardona5ee68e52011-08-09 16:45:08 -0700232static void prepare_for_gate(struct sk_buff *skb, char *dst_addr,
233 struct mesh_path *gate_mpath)
234{
235 struct ieee80211_hdr *hdr;
236 struct ieee80211s_hdr *mshdr;
237 int mesh_hdrlen, hdrlen;
238 char *next_hop;
239
240 hdr = (struct ieee80211_hdr *) skb->data;
241 hdrlen = ieee80211_hdrlen(hdr->frame_control);
242 mshdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
243
244 if (!(mshdr->flags & MESH_FLAGS_AE)) {
245 /* size of the fixed part of the mesh header */
246 mesh_hdrlen = 6;
247
248 /* make room for the two extended addresses */
249 skb_push(skb, 2 * ETH_ALEN);
250 memmove(skb->data, hdr, hdrlen + mesh_hdrlen);
251
252 hdr = (struct ieee80211_hdr *) skb->data;
253
254 /* we preserve the previous mesh header and only add
255 * the new addreses */
256 mshdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
257 mshdr->flags = MESH_FLAGS_AE_A5_A6;
258 memcpy(mshdr->eaddr1, hdr->addr3, ETH_ALEN);
259 memcpy(mshdr->eaddr2, hdr->addr4, ETH_ALEN);
260 }
261
262 /* update next hop */
263 hdr = (struct ieee80211_hdr *) skb->data;
264 rcu_read_lock();
265 next_hop = rcu_dereference(gate_mpath->next_hop)->sta.addr;
266 memcpy(hdr->addr1, next_hop, ETH_ALEN);
267 rcu_read_unlock();
268 memcpy(hdr->addr3, dst_addr, ETH_ALEN);
269}
270
271/**
272 *
273 * mesh_path_move_to_queue - Move or copy frames from one mpath queue to another
274 *
275 * This function is used to transfer or copy frames from an unresolved mpath to
276 * a gate mpath. The function also adds the Address Extension field and
277 * updates the next hop.
278 *
279 * If a frame already has an Address Extension field, only the next hop and
280 * destination addresses are updated.
281 *
282 * The gate mpath must be an active mpath with a valid mpath->next_hop.
283 *
284 * @mpath: An active mpath the frames will be sent to (i.e. the gate)
285 * @from_mpath: The failed mpath
286 * @copy: When true, copy all the frames to the new mpath queue. When false,
287 * move them.
288 */
289static void mesh_path_move_to_queue(struct mesh_path *gate_mpath,
290 struct mesh_path *from_mpath,
291 bool copy)
292{
Thomas Pedersenc6133662011-08-25 10:36:14 -0700293 struct sk_buff *skb, *cp_skb = NULL;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700294 struct sk_buff_head gateq, failq;
295 unsigned long flags;
296 int num_skbs;
297
298 BUG_ON(gate_mpath == from_mpath);
299 BUG_ON(!gate_mpath->next_hop);
300
301 __skb_queue_head_init(&gateq);
302 __skb_queue_head_init(&failq);
303
304 spin_lock_irqsave(&from_mpath->frame_queue.lock, flags);
305 skb_queue_splice_init(&from_mpath->frame_queue, &failq);
306 spin_unlock_irqrestore(&from_mpath->frame_queue.lock, flags);
307
308 num_skbs = skb_queue_len(&failq);
309
310 while (num_skbs--) {
311 skb = __skb_dequeue(&failq);
John W. Linville817a53d2011-08-24 15:12:41 -0400312 if (copy) {
Javier Cardona5ee68e52011-08-09 16:45:08 -0700313 cp_skb = skb_copy(skb, GFP_ATOMIC);
John W. Linville817a53d2011-08-24 15:12:41 -0400314 if (cp_skb)
315 __skb_queue_tail(&failq, cp_skb);
316 }
Javier Cardona5ee68e52011-08-09 16:45:08 -0700317
318 prepare_for_gate(skb, gate_mpath->dst, gate_mpath);
319 __skb_queue_tail(&gateq, skb);
Javier Cardona5ee68e52011-08-09 16:45:08 -0700320 }
321
322 spin_lock_irqsave(&gate_mpath->frame_queue.lock, flags);
323 skb_queue_splice(&gateq, &gate_mpath->frame_queue);
324 mpath_dbg("Mpath queue for gate %pM has %d frames\n",
325 gate_mpath->dst,
326 skb_queue_len(&gate_mpath->frame_queue));
327 spin_unlock_irqrestore(&gate_mpath->frame_queue.lock, flags);
328
329 if (!copy)
330 return;
331
332 spin_lock_irqsave(&from_mpath->frame_queue.lock, flags);
333 skb_queue_splice(&failq, &from_mpath->frame_queue);
334 spin_unlock_irqrestore(&from_mpath->frame_queue.lock, flags);
335}
336
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100337
338/**
339 * mesh_path_lookup - look up a path in the mesh path table
340 * @dst: hardware address (ETH_ALEN length) of destination
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200341 * @sdata: local subif
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100342 *
343 * Returns: pointer to the mesh path structure, or NULL if not found
344 *
345 * Locking: must be called within a read rcu section.
346 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200347struct mesh_path *mesh_path_lookup(u8 *dst, struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100348{
349 struct mesh_path *mpath;
350 struct hlist_node *n;
351 struct hlist_head *bucket;
352 struct mesh_table *tbl;
353 struct mpath_node *node;
354
355 tbl = rcu_dereference(mesh_paths);
356
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200357 bucket = &tbl->hash_buckets[mesh_table_hash(dst, sdata, tbl)];
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100358 hlist_for_each_entry_rcu(node, n, bucket, list) {
359 mpath = node->mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200360 if (mpath->sdata == sdata &&
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100361 memcmp(dst, mpath->dst, ETH_ALEN) == 0) {
362 if (MPATH_EXPIRED(mpath)) {
363 spin_lock_bh(&mpath->state_lock);
Javier Cardonaad99d142011-08-29 13:23:06 -0700364 mpath->flags &= ~MESH_PATH_ACTIVE;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100365 spin_unlock_bh(&mpath->state_lock);
366 }
367 return mpath;
368 }
369 }
370 return NULL;
371}
372
YanBo79617de2008-09-22 13:30:32 +0800373struct mesh_path *mpp_path_lookup(u8 *dst, struct ieee80211_sub_if_data *sdata)
374{
375 struct mesh_path *mpath;
376 struct hlist_node *n;
377 struct hlist_head *bucket;
378 struct mesh_table *tbl;
379 struct mpath_node *node;
380
381 tbl = rcu_dereference(mpp_paths);
382
383 bucket = &tbl->hash_buckets[mesh_table_hash(dst, sdata, tbl)];
384 hlist_for_each_entry_rcu(node, n, bucket, list) {
385 mpath = node->mpath;
386 if (mpath->sdata == sdata &&
387 memcmp(dst, mpath->dst, ETH_ALEN) == 0) {
388 if (MPATH_EXPIRED(mpath)) {
389 spin_lock_bh(&mpath->state_lock);
Javier Cardonaad99d142011-08-29 13:23:06 -0700390 mpath->flags &= ~MESH_PATH_ACTIVE;
YanBo79617de2008-09-22 13:30:32 +0800391 spin_unlock_bh(&mpath->state_lock);
392 }
393 return mpath;
394 }
395 }
396 return NULL;
397}
398
399
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100400/**
401 * mesh_path_lookup_by_idx - look up a path in the mesh path table by its index
402 * @idx: index
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200403 * @sdata: local subif, or NULL for all entries
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100404 *
405 * Returns: pointer to the mesh path structure, or NULL if not found.
406 *
407 * Locking: must be called within a read rcu section.
408 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200409struct mesh_path *mesh_path_lookup_by_idx(int idx, struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100410{
Johannes Berg349eb8c2011-05-14 11:56:16 +0200411 struct mesh_table *tbl = rcu_dereference(mesh_paths);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100412 struct mpath_node *node;
413 struct hlist_node *p;
414 int i;
415 int j = 0;
416
Johannes Berg349eb8c2011-05-14 11:56:16 +0200417 for_each_mesh_entry(tbl, p, node, i) {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200418 if (sdata && node->mpath->sdata != sdata)
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800419 continue;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100420 if (j++ == idx) {
421 if (MPATH_EXPIRED(node->mpath)) {
422 spin_lock_bh(&node->mpath->state_lock);
Javier Cardonaad99d142011-08-29 13:23:06 -0700423 node->mpath->flags &= ~MESH_PATH_ACTIVE;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100424 spin_unlock_bh(&node->mpath->state_lock);
425 }
426 return node->mpath;
427 }
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800428 }
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100429
430 return NULL;
431}
432
Javier Cardona5ee68e52011-08-09 16:45:08 -0700433static void mesh_gate_node_reclaim(struct rcu_head *rp)
434{
435 struct mpath_node *node = container_of(rp, struct mpath_node, rcu);
436 kfree(node);
437}
438
439/**
440 * mesh_gate_add - mark mpath as path to a mesh gate and add to known_gates
441 * @mesh_tbl: table which contains known_gates list
442 * @mpath: mpath to known mesh gate
443 *
444 * Returns: 0 on success
445 *
446 */
447static int mesh_gate_add(struct mesh_table *tbl, struct mesh_path *mpath)
448{
449 struct mpath_node *gate, *new_gate;
450 struct hlist_node *n;
451 int err;
452
453 rcu_read_lock();
454 tbl = rcu_dereference(tbl);
455
456 hlist_for_each_entry_rcu(gate, n, tbl->known_gates, list)
457 if (gate->mpath == mpath) {
458 err = -EEXIST;
459 goto err_rcu;
460 }
461
462 new_gate = kzalloc(sizeof(struct mpath_node), GFP_ATOMIC);
463 if (!new_gate) {
464 err = -ENOMEM;
465 goto err_rcu;
466 }
467
468 mpath->is_gate = true;
469 mpath->sdata->u.mesh.num_gates++;
470 new_gate->mpath = mpath;
471 spin_lock_bh(&tbl->gates_lock);
472 hlist_add_head_rcu(&new_gate->list, tbl->known_gates);
473 spin_unlock_bh(&tbl->gates_lock);
474 rcu_read_unlock();
475 mpath_dbg("Mesh path (%s): Recorded new gate: %pM. %d known gates\n",
476 mpath->sdata->name, mpath->dst,
477 mpath->sdata->u.mesh.num_gates);
478 return 0;
479err_rcu:
480 rcu_read_unlock();
481 return err;
482}
483
484/**
485 * mesh_gate_del - remove a mesh gate from the list of known gates
486 * @tbl: table which holds our list of known gates
487 * @mpath: gate mpath
488 *
489 * Returns: 0 on success
490 *
491 * Locking: must be called inside rcu_read_lock() section
492 */
493static int mesh_gate_del(struct mesh_table *tbl, struct mesh_path *mpath)
494{
495 struct mpath_node *gate;
496 struct hlist_node *p, *q;
497
498 tbl = rcu_dereference(tbl);
499
500 hlist_for_each_entry_safe(gate, p, q, tbl->known_gates, list)
501 if (gate->mpath == mpath) {
502 spin_lock_bh(&tbl->gates_lock);
503 hlist_del_rcu(&gate->list);
504 call_rcu(&gate->rcu, mesh_gate_node_reclaim);
505 spin_unlock_bh(&tbl->gates_lock);
506 mpath->sdata->u.mesh.num_gates--;
507 mpath->is_gate = false;
508 mpath_dbg("Mesh path (%s): Deleted gate: %pM. "
509 "%d known gates\n", mpath->sdata->name,
510 mpath->dst, mpath->sdata->u.mesh.num_gates);
511 break;
512 }
513
514 return 0;
515}
516
517/**
518 *
519 * mesh_path_add_gate - add the given mpath to a mesh gate to our path table
520 * @mpath: gate path to add to table
521 */
522int mesh_path_add_gate(struct mesh_path *mpath)
523{
524 return mesh_gate_add(mesh_paths, mpath);
525}
526
527/**
528 * mesh_gate_num - number of gates known to this interface
529 * @sdata: subif data
530 */
531int mesh_gate_num(struct ieee80211_sub_if_data *sdata)
532{
533 return sdata->u.mesh.num_gates;
534}
535
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100536/**
537 * mesh_path_add - allocate and add a new path to the mesh path table
538 * @addr: destination address of the path (ETH_ALEN length)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200539 * @sdata: local subif
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100540 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200541 * Returns: 0 on success
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100542 *
543 * State: the initial state of the new path is set to 0
544 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200545int mesh_path_add(u8 *dst, struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100546{
Javier Cardona18889232009-08-10 12:15:52 -0700547 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
548 struct ieee80211_local *local = sdata->local;
Johannes Berg349eb8c2011-05-14 11:56:16 +0200549 struct mesh_table *tbl;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100550 struct mesh_path *mpath, *new_mpath;
551 struct mpath_node *node, *new_node;
552 struct hlist_head *bucket;
553 struct hlist_node *n;
554 int grow = 0;
555 int err = 0;
556 u32 hash_idx;
557
Johannes Berg47846c92009-11-25 17:46:19 +0100558 if (memcmp(dst, sdata->vif.addr, ETH_ALEN) == 0)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100559 /* never add ourselves as neighbours */
560 return -ENOTSUPP;
561
562 if (is_multicast_ether_addr(dst))
563 return -ENOTSUPP;
564
Johannes Berg472dbc42008-09-11 00:01:49 +0200565 if (atomic_add_unless(&sdata->u.mesh.mpaths, 1, MESH_MAX_MPATHS) == 0)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100566 return -ENOSPC;
567
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400568 err = -ENOMEM;
Javier Cardona18889232009-08-10 12:15:52 -0700569 new_mpath = kzalloc(sizeof(struct mesh_path), GFP_ATOMIC);
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400570 if (!new_mpath)
571 goto err_path_alloc;
572
Javier Cardona18889232009-08-10 12:15:52 -0700573 new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400574 if (!new_node)
575 goto err_node_alloc;
Pavel Emelyanovf84e71a2008-05-06 18:46:36 +0400576
Javier Cardona9b84b8082011-05-03 16:57:16 -0700577 read_lock_bh(&pathtbl_resize_lock);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100578 memcpy(new_mpath->dst, dst, ETH_ALEN);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200579 new_mpath->sdata = sdata;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100580 new_mpath->flags = 0;
581 skb_queue_head_init(&new_mpath->frame_queue);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100582 new_node->mpath = new_mpath;
583 new_mpath->timer.data = (unsigned long) new_mpath;
584 new_mpath->timer.function = mesh_path_timer;
585 new_mpath->exp_time = jiffies;
586 spin_lock_init(&new_mpath->state_lock);
587 init_timer(&new_mpath->timer);
588
Johannes Berg349eb8c2011-05-14 11:56:16 +0200589 tbl = resize_dereference_mesh_paths();
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100590
Johannes Berg349eb8c2011-05-14 11:56:16 +0200591 hash_idx = mesh_table_hash(dst, sdata, tbl);
592 bucket = &tbl->hash_buckets[hash_idx];
593
594 spin_lock_bh(&tbl->hashwlock[hash_idx]);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100595
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400596 err = -EEXIST;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100597 hlist_for_each_entry(node, n, bucket, list) {
598 mpath = node->mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200599 if (mpath->sdata == sdata && memcmp(dst, mpath->dst, ETH_ALEN) == 0)
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400600 goto err_exists;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100601 }
602
603 hlist_add_head_rcu(&new_node->list, bucket);
Johannes Berg349eb8c2011-05-14 11:56:16 +0200604 if (atomic_inc_return(&tbl->entries) >=
605 tbl->mean_chain_len * (tbl->hash_mask + 1))
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100606 grow = 1;
607
Johannes Bergf5ea9122009-08-07 16:17:38 +0200608 mesh_paths_generation++;
609
Johannes Berg349eb8c2011-05-14 11:56:16 +0200610 spin_unlock_bh(&tbl->hashwlock[hash_idx]);
Javier Cardona9b84b8082011-05-03 16:57:16 -0700611 read_unlock_bh(&pathtbl_resize_lock);
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400612 if (grow) {
Javier Cardona18889232009-08-10 12:15:52 -0700613 set_bit(MESH_WORK_GROW_MPATH_TABLE, &ifmsh->wrkq_flags);
Johannes Berg64592c82010-06-10 10:21:31 +0200614 ieee80211_queue_work(&local->hw, &sdata->work);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100615 }
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400616 return 0;
617
618err_exists:
Johannes Berg349eb8c2011-05-14 11:56:16 +0200619 spin_unlock_bh(&tbl->hashwlock[hash_idx]);
Javier Cardona9b84b8082011-05-03 16:57:16 -0700620 read_unlock_bh(&pathtbl_resize_lock);
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400621 kfree(new_node);
622err_node_alloc:
623 kfree(new_mpath);
624err_path_alloc:
Johannes Berg472dbc42008-09-11 00:01:49 +0200625 atomic_dec(&sdata->u.mesh.mpaths);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100626 return err;
627}
628
Johannes Berg1928eca2011-05-14 11:00:52 +0200629static void mesh_table_free_rcu(struct rcu_head *rcu)
630{
631 struct mesh_table *tbl = container_of(rcu, struct mesh_table, rcu_head);
632
633 mesh_table_free(tbl, false);
634}
635
Javier Cardona18889232009-08-10 12:15:52 -0700636void mesh_mpath_table_grow(void)
637{
638 struct mesh_table *oldtbl, *newtbl;
639
Javier Cardona9b84b8082011-05-03 16:57:16 -0700640 write_lock_bh(&pathtbl_resize_lock);
Johannes Berg349eb8c2011-05-14 11:56:16 +0200641 oldtbl = resize_dereference_mesh_paths();
642 newtbl = mesh_table_alloc(oldtbl->size_order + 1);
Johannes Berg1928eca2011-05-14 11:00:52 +0200643 if (!newtbl)
644 goto out;
Johannes Berg349eb8c2011-05-14 11:56:16 +0200645 if (mesh_table_grow(oldtbl, newtbl) < 0) {
cozybit Inca3e6b122011-04-13 11:10:28 -0700646 __mesh_table_free(newtbl);
Johannes Berg1928eca2011-05-14 11:00:52 +0200647 goto out;
Javier Cardona18889232009-08-10 12:15:52 -0700648 }
649 rcu_assign_pointer(mesh_paths, newtbl);
Javier Cardona18889232009-08-10 12:15:52 -0700650
Johannes Berg1928eca2011-05-14 11:00:52 +0200651 call_rcu(&oldtbl->rcu_head, mesh_table_free_rcu);
652
653 out:
654 write_unlock_bh(&pathtbl_resize_lock);
Javier Cardona18889232009-08-10 12:15:52 -0700655}
656
657void mesh_mpp_table_grow(void)
658{
659 struct mesh_table *oldtbl, *newtbl;
660
Javier Cardona9b84b8082011-05-03 16:57:16 -0700661 write_lock_bh(&pathtbl_resize_lock);
Johannes Berg349eb8c2011-05-14 11:56:16 +0200662 oldtbl = resize_dereference_mpp_paths();
663 newtbl = mesh_table_alloc(oldtbl->size_order + 1);
Johannes Berg1928eca2011-05-14 11:00:52 +0200664 if (!newtbl)
665 goto out;
Johannes Berg349eb8c2011-05-14 11:56:16 +0200666 if (mesh_table_grow(oldtbl, newtbl) < 0) {
cozybit Inca3e6b122011-04-13 11:10:28 -0700667 __mesh_table_free(newtbl);
Johannes Berg1928eca2011-05-14 11:00:52 +0200668 goto out;
Javier Cardona18889232009-08-10 12:15:52 -0700669 }
670 rcu_assign_pointer(mpp_paths, newtbl);
Johannes Berg1928eca2011-05-14 11:00:52 +0200671 call_rcu(&oldtbl->rcu_head, mesh_table_free_rcu);
Javier Cardona18889232009-08-10 12:15:52 -0700672
Johannes Berg1928eca2011-05-14 11:00:52 +0200673 out:
674 write_unlock_bh(&pathtbl_resize_lock);
Javier Cardona18889232009-08-10 12:15:52 -0700675}
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100676
YanBo79617de2008-09-22 13:30:32 +0800677int mpp_path_add(u8 *dst, u8 *mpp, struct ieee80211_sub_if_data *sdata)
678{
Javier Cardona18889232009-08-10 12:15:52 -0700679 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
680 struct ieee80211_local *local = sdata->local;
Johannes Berg349eb8c2011-05-14 11:56:16 +0200681 struct mesh_table *tbl;
YanBo79617de2008-09-22 13:30:32 +0800682 struct mesh_path *mpath, *new_mpath;
683 struct mpath_node *node, *new_node;
684 struct hlist_head *bucket;
685 struct hlist_node *n;
686 int grow = 0;
687 int err = 0;
688 u32 hash_idx;
689
Johannes Berg47846c92009-11-25 17:46:19 +0100690 if (memcmp(dst, sdata->vif.addr, ETH_ALEN) == 0)
YanBo79617de2008-09-22 13:30:32 +0800691 /* never add ourselves as neighbours */
692 return -ENOTSUPP;
693
694 if (is_multicast_ether_addr(dst))
695 return -ENOTSUPP;
696
697 err = -ENOMEM;
Javier Cardona18889232009-08-10 12:15:52 -0700698 new_mpath = kzalloc(sizeof(struct mesh_path), GFP_ATOMIC);
YanBo79617de2008-09-22 13:30:32 +0800699 if (!new_mpath)
700 goto err_path_alloc;
701
Javier Cardona18889232009-08-10 12:15:52 -0700702 new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
YanBo79617de2008-09-22 13:30:32 +0800703 if (!new_node)
704 goto err_node_alloc;
705
Javier Cardona9b84b8082011-05-03 16:57:16 -0700706 read_lock_bh(&pathtbl_resize_lock);
YanBo79617de2008-09-22 13:30:32 +0800707 memcpy(new_mpath->dst, dst, ETH_ALEN);
708 memcpy(new_mpath->mpp, mpp, ETH_ALEN);
709 new_mpath->sdata = sdata;
710 new_mpath->flags = 0;
711 skb_queue_head_init(&new_mpath->frame_queue);
712 new_node->mpath = new_mpath;
Thomas Pedersenc6133662011-08-25 10:36:14 -0700713 init_timer(&new_mpath->timer);
YanBo79617de2008-09-22 13:30:32 +0800714 new_mpath->exp_time = jiffies;
715 spin_lock_init(&new_mpath->state_lock);
716
Johannes Berg349eb8c2011-05-14 11:56:16 +0200717 tbl = resize_dereference_mpp_paths();
YanBo79617de2008-09-22 13:30:32 +0800718
Johannes Berg349eb8c2011-05-14 11:56:16 +0200719 hash_idx = mesh_table_hash(dst, sdata, tbl);
720 bucket = &tbl->hash_buckets[hash_idx];
721
722 spin_lock_bh(&tbl->hashwlock[hash_idx]);
YanBo79617de2008-09-22 13:30:32 +0800723
724 err = -EEXIST;
725 hlist_for_each_entry(node, n, bucket, list) {
726 mpath = node->mpath;
727 if (mpath->sdata == sdata && memcmp(dst, mpath->dst, ETH_ALEN) == 0)
728 goto err_exists;
729 }
730
731 hlist_add_head_rcu(&new_node->list, bucket);
Johannes Berg349eb8c2011-05-14 11:56:16 +0200732 if (atomic_inc_return(&tbl->entries) >=
733 tbl->mean_chain_len * (tbl->hash_mask + 1))
YanBo79617de2008-09-22 13:30:32 +0800734 grow = 1;
735
Johannes Berg349eb8c2011-05-14 11:56:16 +0200736 spin_unlock_bh(&tbl->hashwlock[hash_idx]);
Javier Cardona9b84b8082011-05-03 16:57:16 -0700737 read_unlock_bh(&pathtbl_resize_lock);
YanBo79617de2008-09-22 13:30:32 +0800738 if (grow) {
Javier Cardona18889232009-08-10 12:15:52 -0700739 set_bit(MESH_WORK_GROW_MPP_TABLE, &ifmsh->wrkq_flags);
Johannes Berg64592c82010-06-10 10:21:31 +0200740 ieee80211_queue_work(&local->hw, &sdata->work);
YanBo79617de2008-09-22 13:30:32 +0800741 }
742 return 0;
743
744err_exists:
Johannes Berg349eb8c2011-05-14 11:56:16 +0200745 spin_unlock_bh(&tbl->hashwlock[hash_idx]);
Javier Cardona9b84b8082011-05-03 16:57:16 -0700746 read_unlock_bh(&pathtbl_resize_lock);
YanBo79617de2008-09-22 13:30:32 +0800747 kfree(new_node);
748err_node_alloc:
749 kfree(new_mpath);
750err_path_alloc:
751 return err;
752}
753
754
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100755/**
756 * mesh_plink_broken - deactivates paths and sends perr when a link breaks
757 *
758 * @sta: broken peer link
759 *
760 * This function must be called from the rate control algorithm if enough
761 * delivery errors suggest that a peer link is no longer usable.
762 */
763void mesh_plink_broken(struct sta_info *sta)
764{
Johannes Berg349eb8c2011-05-14 11:56:16 +0200765 struct mesh_table *tbl;
Johannes Berg15ff6362009-11-17 13:34:04 +0100766 static const u8 bcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100767 struct mesh_path *mpath;
768 struct mpath_node *node;
769 struct hlist_node *p;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200770 struct ieee80211_sub_if_data *sdata = sta->sdata;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100771 int i;
Thomas Pedersen25d49e42011-08-11 19:35:15 -0700772 __le16 reason = cpu_to_le16(WLAN_REASON_MESH_PATH_DEST_UNREACHABLE);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100773
774 rcu_read_lock();
Johannes Berg349eb8c2011-05-14 11:56:16 +0200775 tbl = rcu_dereference(mesh_paths);
776 for_each_mesh_entry(tbl, p, node, i) {
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100777 mpath = node->mpath;
Johannes Berg349eb8c2011-05-14 11:56:16 +0200778 if (rcu_dereference(mpath->next_hop) == sta &&
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100779 mpath->flags & MESH_PATH_ACTIVE &&
780 !(mpath->flags & MESH_PATH_FIXED)) {
Javier Cardonaf5e50cd2011-08-29 13:23:05 -0700781 spin_lock_bh(&mpath->state_lock);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100782 mpath->flags &= ~MESH_PATH_ACTIVE;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000783 ++mpath->sn;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100784 spin_unlock_bh(&mpath->state_lock);
Javier Cardona45904f22010-12-03 09:20:40 +0100785 mesh_path_error_tx(sdata->u.mesh.mshcfg.element_ttl,
786 mpath->dst, cpu_to_le32(mpath->sn),
Thomas Pedersen25d49e42011-08-11 19:35:15 -0700787 reason, bcast, sdata);
Javier Cardonaf5e50cd2011-08-29 13:23:05 -0700788 }
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100789 }
790 rcu_read_unlock();
791}
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100792
Javier Cardona19c50b32011-08-29 13:23:07 -0700793static void mesh_path_node_reclaim(struct rcu_head *rp)
794{
795 struct mpath_node *node = container_of(rp, struct mpath_node, rcu);
796 struct ieee80211_sub_if_data *sdata = node->mpath->sdata;
797
798 del_timer_sync(&node->mpath->timer);
799 atomic_dec(&sdata->u.mesh.mpaths);
800 kfree(node->mpath);
801 kfree(node);
802}
803
804/* needs to be called with the corresponding hashwlock taken */
805static void __mesh_path_del(struct mesh_table *tbl, struct mpath_node *node)
806{
807 struct mesh_path *mpath;
808 mpath = node->mpath;
809 spin_lock(&mpath->state_lock);
810 mpath->flags |= MESH_PATH_RESOLVING;
811 if (mpath->is_gate)
812 mesh_gate_del(tbl, mpath);
813 hlist_del_rcu(&node->list);
814 call_rcu(&node->rcu, mesh_path_node_reclaim);
815 spin_unlock(&mpath->state_lock);
816 atomic_dec(&tbl->entries);
817}
818
Javier Cardonacd72e812011-08-29 13:23:08 -0700819/**
820 * mesh_path_flush_by_nexthop - Deletes mesh paths if their next hop matches
821 *
822 * @sta - mesh peer to match
823 *
824 * RCU notes: this function is called when a mesh plink transitions from
825 * PLINK_ESTAB to any other state, since PLINK_ESTAB state is the only one that
826 * allows path creation. This will happen before the sta can be freed (because
827 * sta_info_destroy() calls this) so any reader in a rcu read block will be
828 * protected against the plink disappearing.
829 */
830void mesh_path_flush_by_nexthop(struct sta_info *sta)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100831{
Johannes Berg349eb8c2011-05-14 11:56:16 +0200832 struct mesh_table *tbl;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100833 struct mesh_path *mpath;
834 struct mpath_node *node;
835 struct hlist_node *p;
836 int i;
837
Johannes Berg349eb8c2011-05-14 11:56:16 +0200838 rcu_read_lock();
839 tbl = rcu_dereference(mesh_paths);
840 for_each_mesh_entry(tbl, p, node, i) {
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100841 mpath = node->mpath;
Javier Cardonacd72e812011-08-29 13:23:08 -0700842 if (rcu_dereference(mpath->next_hop) == sta) {
Javier Cardona19c50b32011-08-29 13:23:07 -0700843 spin_lock_bh(&tbl->hashwlock[i]);
844 __mesh_path_del(tbl, node);
845 spin_unlock_bh(&tbl->hashwlock[i]);
846 }
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100847 }
Johannes Berg349eb8c2011-05-14 11:56:16 +0200848 rcu_read_unlock();
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100849}
850
Javier Cardonacd72e812011-08-29 13:23:08 -0700851static void table_flush_by_iface(struct mesh_table *tbl,
852 struct ieee80211_sub_if_data *sdata)
Javier Cardonaece1a2e2011-08-29 13:23:04 -0700853{
Javier Cardonaece1a2e2011-08-29 13:23:04 -0700854 struct mesh_path *mpath;
855 struct mpath_node *node;
856 struct hlist_node *p;
857 int i;
858
Javier Cardonacd72e812011-08-29 13:23:08 -0700859 WARN_ON(!rcu_read_lock_held());
Javier Cardonaece1a2e2011-08-29 13:23:04 -0700860 for_each_mesh_entry(tbl, p, node, i) {
861 mpath = node->mpath;
Javier Cardonacd72e812011-08-29 13:23:08 -0700862 if (mpath->sdata != sdata)
863 continue;
Javier Cardonaece1a2e2011-08-29 13:23:04 -0700864 spin_lock_bh(&tbl->hashwlock[i]);
Javier Cardona19c50b32011-08-29 13:23:07 -0700865 __mesh_path_del(tbl, node);
Javier Cardonaece1a2e2011-08-29 13:23:04 -0700866 spin_unlock_bh(&tbl->hashwlock[i]);
867 }
Javier Cardonaece1a2e2011-08-29 13:23:04 -0700868}
869
870/**
871 * mesh_path_flush_by_iface - Deletes all mesh paths associated with a given iface
872 *
873 * This function deletes both mesh paths as well as mesh portal paths.
874 *
875 * @sdata - interface data to match
876 *
877 */
878void mesh_path_flush_by_iface(struct ieee80211_sub_if_data *sdata)
879{
Javier Cardonacd72e812011-08-29 13:23:08 -0700880 struct mesh_table *tbl;
881
882 rcu_read_lock();
883 tbl = rcu_dereference(mesh_paths);
884 table_flush_by_iface(tbl, sdata);
885 tbl = rcu_dereference(mpp_paths);
886 table_flush_by_iface(tbl, sdata);
887 rcu_read_unlock();
Javier Cardonaece1a2e2011-08-29 13:23:04 -0700888}
889
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100890/**
891 * mesh_path_del - delete a mesh path from the table
892 *
893 * @addr: dst address (ETH_ALEN length)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200894 * @sdata: local subif
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100895 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200896 * Returns: 0 if successful
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100897 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200898int mesh_path_del(u8 *addr, struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100899{
Johannes Berg349eb8c2011-05-14 11:56:16 +0200900 struct mesh_table *tbl;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100901 struct mesh_path *mpath;
902 struct mpath_node *node;
903 struct hlist_head *bucket;
904 struct hlist_node *n;
905 int hash_idx;
906 int err = 0;
907
Javier Cardona9b84b8082011-05-03 16:57:16 -0700908 read_lock_bh(&pathtbl_resize_lock);
Johannes Berg349eb8c2011-05-14 11:56:16 +0200909 tbl = resize_dereference_mesh_paths();
910 hash_idx = mesh_table_hash(addr, sdata, tbl);
911 bucket = &tbl->hash_buckets[hash_idx];
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100912
Johannes Berg349eb8c2011-05-14 11:56:16 +0200913 spin_lock_bh(&tbl->hashwlock[hash_idx]);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100914 hlist_for_each_entry(node, n, bucket, list) {
915 mpath = node->mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200916 if (mpath->sdata == sdata &&
Johannes Berg349eb8c2011-05-14 11:56:16 +0200917 memcmp(addr, mpath->dst, ETH_ALEN) == 0) {
Javier Cardona19c50b32011-08-29 13:23:07 -0700918 __mesh_path_del(tbl, node);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100919 goto enddel;
920 }
921 }
922
923 err = -ENXIO;
924enddel:
Johannes Bergf5ea9122009-08-07 16:17:38 +0200925 mesh_paths_generation++;
Johannes Berg349eb8c2011-05-14 11:56:16 +0200926 spin_unlock_bh(&tbl->hashwlock[hash_idx]);
Javier Cardona9b84b8082011-05-03 16:57:16 -0700927 read_unlock_bh(&pathtbl_resize_lock);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100928 return err;
929}
930
931/**
932 * mesh_path_tx_pending - sends pending frames in a mesh path queue
933 *
934 * @mpath: mesh path to activate
935 *
936 * Locking: the state_lock of the mpath structure must NOT be held when calling
937 * this function.
938 */
939void mesh_path_tx_pending(struct mesh_path *mpath)
940{
Javier Cardona249b4052009-07-07 10:55:03 -0700941 if (mpath->flags & MESH_PATH_ACTIVE)
942 ieee80211_add_pending_skbs(mpath->sdata->local,
943 &mpath->frame_queue);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100944}
945
946/**
Javier Cardona5ee68e52011-08-09 16:45:08 -0700947 * mesh_path_send_to_gates - sends pending frames to all known mesh gates
948 *
949 * @mpath: mesh path whose queue will be emptied
950 *
951 * If there is only one gate, the frames are transferred from the failed mpath
952 * queue to that gate's queue. If there are more than one gates, the frames
953 * are copied from each gate to the next. After frames are copied, the
954 * mpath queues are emptied onto the transmission queue.
955 */
956int mesh_path_send_to_gates(struct mesh_path *mpath)
957{
958 struct ieee80211_sub_if_data *sdata = mpath->sdata;
959 struct hlist_node *n;
960 struct mesh_table *tbl;
961 struct mesh_path *from_mpath = mpath;
962 struct mpath_node *gate = NULL;
963 bool copy = false;
964 struct hlist_head *known_gates;
965
966 rcu_read_lock();
967 tbl = rcu_dereference(mesh_paths);
968 known_gates = tbl->known_gates;
969 rcu_read_unlock();
970
971 if (!known_gates)
972 return -EHOSTUNREACH;
973
974 hlist_for_each_entry_rcu(gate, n, known_gates, list) {
975 if (gate->mpath->sdata != sdata)
976 continue;
977
978 if (gate->mpath->flags & MESH_PATH_ACTIVE) {
979 mpath_dbg("Forwarding to %pM\n", gate->mpath->dst);
980 mesh_path_move_to_queue(gate->mpath, from_mpath, copy);
981 from_mpath = gate->mpath;
982 copy = true;
983 } else {
984 mpath_dbg("Not forwarding %p\n", gate->mpath);
985 mpath_dbg("flags %x\n", gate->mpath->flags);
986 }
987 }
988
989 hlist_for_each_entry_rcu(gate, n, known_gates, list)
990 if (gate->mpath->sdata == sdata) {
991 mpath_dbg("Sending to %pM\n", gate->mpath->dst);
992 mesh_path_tx_pending(gate->mpath);
993 }
994
995 return (from_mpath == mpath) ? -EHOSTUNREACH : 0;
996}
997
998/**
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100999 * mesh_path_discard_frame - discard a frame whose path could not be resolved
1000 *
1001 * @skb: frame to discard
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001002 * @sdata: network subif the frame was to be sent through
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001003 *
Javier Cardona35946a52009-07-13 17:00:10 -07001004 * If the frame was being forwarded from another MP, a PERR frame will be sent
1005 * to the precursor. The precursor's address (i.e. the previous hop) was saved
1006 * in addr1 of the frame-to-be-forwarded, and would only be overwritten once
1007 * the destination is successfully resolved.
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001008 *
1009 * Locking: the function must me called within a rcu_read_lock region
1010 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001011void mesh_path_discard_frame(struct sk_buff *skb,
1012 struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001013{
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001014 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001015 struct mesh_path *mpath;
Rui Paulod19b3bf2009-11-09 23:46:55 +00001016 u32 sn = 0;
Thomas Pedersen25d49e42011-08-11 19:35:15 -07001017 __le16 reason = cpu_to_le16(WLAN_REASON_MESH_PATH_NOFORWARD);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001018
Johannes Berg47846c92009-11-25 17:46:19 +01001019 if (memcmp(hdr->addr4, sdata->vif.addr, ETH_ALEN) != 0) {
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001020 u8 *ra, *da;
1021
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001022 da = hdr->addr3;
Javier Cardona35946a52009-07-13 17:00:10 -07001023 ra = hdr->addr1;
Javier Cardonaaf089c12011-08-29 13:23:03 -07001024 rcu_read_lock();
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001025 mpath = mesh_path_lookup(da, sdata);
Javier Cardonaaf089c12011-08-29 13:23:03 -07001026 if (mpath) {
1027 spin_lock_bh(&mpath->state_lock);
Rui Paulod19b3bf2009-11-09 23:46:55 +00001028 sn = ++mpath->sn;
Javier Cardonaaf089c12011-08-29 13:23:03 -07001029 spin_unlock_bh(&mpath->state_lock);
1030 }
1031 rcu_read_unlock();
Javier Cardona45904f22010-12-03 09:20:40 +01001032 mesh_path_error_tx(sdata->u.mesh.mshcfg.element_ttl, skb->data,
Thomas Pedersen25d49e42011-08-11 19:35:15 -07001033 cpu_to_le32(sn), reason, ra, sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001034 }
1035
1036 kfree_skb(skb);
Johannes Berg472dbc42008-09-11 00:01:49 +02001037 sdata->u.mesh.mshstats.dropped_frames_no_route++;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001038}
1039
1040/**
1041 * mesh_path_flush_pending - free the pending queue of a mesh path
1042 *
1043 * @mpath: mesh path whose queue has to be freed
1044 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001045 * Locking: the function must me called within a rcu_read_lock region
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001046 */
1047void mesh_path_flush_pending(struct mesh_path *mpath)
1048{
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001049 struct sk_buff *skb;
1050
Javier Cardona00e3f252011-08-09 16:45:07 -07001051 while ((skb = skb_dequeue(&mpath->frame_queue)) != NULL)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001052 mesh_path_discard_frame(skb, mpath->sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001053}
1054
1055/**
1056 * mesh_path_fix_nexthop - force a specific next hop for a mesh path
1057 *
1058 * @mpath: the mesh path to modify
1059 * @next_hop: the next hop to force
1060 *
1061 * Locking: this function must be called holding mpath->state_lock
1062 */
1063void mesh_path_fix_nexthop(struct mesh_path *mpath, struct sta_info *next_hop)
1064{
1065 spin_lock_bh(&mpath->state_lock);
1066 mesh_path_assign_nexthop(mpath, next_hop);
Rui Paulod19b3bf2009-11-09 23:46:55 +00001067 mpath->sn = 0xffff;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001068 mpath->metric = 0;
1069 mpath->hop_count = 0;
1070 mpath->exp_time = 0;
1071 mpath->flags |= MESH_PATH_FIXED;
1072 mesh_path_activate(mpath);
1073 spin_unlock_bh(&mpath->state_lock);
1074 mesh_path_tx_pending(mpath);
1075}
1076
1077static void mesh_path_node_free(struct hlist_node *p, bool free_leafs)
1078{
1079 struct mesh_path *mpath;
1080 struct mpath_node *node = hlist_entry(p, struct mpath_node, list);
1081 mpath = node->mpath;
1082 hlist_del_rcu(p);
Javier Cardonad0df9ee2011-05-13 14:16:07 -07001083 if (free_leafs) {
Thomas Pedersenc6133662011-08-25 10:36:14 -07001084 del_timer_sync(&mpath->timer);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001085 kfree(mpath);
Javier Cardonad0df9ee2011-05-13 14:16:07 -07001086 }
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001087 kfree(node);
1088}
1089
Pavel Emelyanov4caf86c2008-05-07 19:47:01 +04001090static int mesh_path_node_copy(struct hlist_node *p, struct mesh_table *newtbl)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001091{
1092 struct mesh_path *mpath;
1093 struct mpath_node *node, *new_node;
1094 u32 hash_idx;
1095
Pavel Emelyanov8566dc32008-05-07 19:51:51 +04001096 new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
Pavel Emelyanov00242c42008-05-07 19:48:14 +04001097 if (new_node == NULL)
1098 return -ENOMEM;
1099
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001100 node = hlist_entry(p, struct mpath_node, list);
1101 mpath = node->mpath;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001102 new_node->mpath = mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001103 hash_idx = mesh_table_hash(mpath->dst, mpath->sdata, newtbl);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001104 hlist_add_head(&new_node->list,
1105 &newtbl->hash_buckets[hash_idx]);
Pavel Emelyanov4caf86c2008-05-07 19:47:01 +04001106 return 0;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001107}
1108
1109int mesh_pathtbl_init(void)
1110{
Johannes Berg349eb8c2011-05-14 11:56:16 +02001111 struct mesh_table *tbl_path, *tbl_mpp;
YanBo79617de2008-09-22 13:30:32 +08001112
Johannes Berg349eb8c2011-05-14 11:56:16 +02001113 tbl_path = mesh_table_alloc(INIT_PATHS_SIZE_ORDER);
1114 if (!tbl_path)
1115 return -ENOMEM;
1116 tbl_path->free_node = &mesh_path_node_free;
1117 tbl_path->copy_node = &mesh_path_node_copy;
1118 tbl_path->mean_chain_len = MEAN_CHAIN_LEN;
Javier Cardona5ee68e52011-08-09 16:45:08 -07001119 tbl_path->known_gates = kzalloc(sizeof(struct hlist_head), GFP_ATOMIC);
1120 INIT_HLIST_HEAD(tbl_path->known_gates);
1121
Johannes Berg349eb8c2011-05-14 11:56:16 +02001122
1123 tbl_mpp = mesh_table_alloc(INIT_PATHS_SIZE_ORDER);
1124 if (!tbl_mpp) {
1125 mesh_table_free(tbl_path, true);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001126 return -ENOMEM;
YanBo79617de2008-09-22 13:30:32 +08001127 }
Johannes Berg349eb8c2011-05-14 11:56:16 +02001128 tbl_mpp->free_node = &mesh_path_node_free;
1129 tbl_mpp->copy_node = &mesh_path_node_copy;
1130 tbl_mpp->mean_chain_len = MEAN_CHAIN_LEN;
Javier Cardona5ee68e52011-08-09 16:45:08 -07001131 tbl_mpp->known_gates = kzalloc(sizeof(struct hlist_head), GFP_ATOMIC);
1132 INIT_HLIST_HEAD(tbl_mpp->known_gates);
Johannes Berg349eb8c2011-05-14 11:56:16 +02001133
1134 /* Need no locking since this is during init */
1135 RCU_INIT_POINTER(mesh_paths, tbl_path);
1136 RCU_INIT_POINTER(mpp_paths, tbl_mpp);
YanBo79617de2008-09-22 13:30:32 +08001137
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001138 return 0;
1139}
1140
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001141void mesh_path_expire(struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001142{
Johannes Berg349eb8c2011-05-14 11:56:16 +02001143 struct mesh_table *tbl;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001144 struct mesh_path *mpath;
1145 struct mpath_node *node;
1146 struct hlist_node *p;
1147 int i;
1148
Johannes Berg349eb8c2011-05-14 11:56:16 +02001149 rcu_read_lock();
1150 tbl = rcu_dereference(mesh_paths);
1151 for_each_mesh_entry(tbl, p, node, i) {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001152 if (node->mpath->sdata != sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001153 continue;
1154 mpath = node->mpath;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001155 if ((!(mpath->flags & MESH_PATH_RESOLVING)) &&
1156 (!(mpath->flags & MESH_PATH_FIXED)) &&
Javier Cardonaf5e50cd2011-08-29 13:23:05 -07001157 time_after(jiffies, mpath->exp_time + MESH_PATH_EXPIRE))
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001158 mesh_path_del(mpath->dst, mpath->sdata);
Javier Cardona19c50b32011-08-29 13:23:07 -07001159 }
Johannes Berg349eb8c2011-05-14 11:56:16 +02001160 rcu_read_unlock();
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001161}
1162
1163void mesh_pathtbl_unregister(void)
1164{
Johannes Berg349eb8c2011-05-14 11:56:16 +02001165 /* no need for locking during exit path */
1166 mesh_table_free(rcu_dereference_raw(mesh_paths), true);
1167 mesh_table_free(rcu_dereference_raw(mpp_paths), true);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001168}