blob: ede4f5242e0b7e1be9fc36cbda954a8ed9d7e5c0 [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
52 * by RCU
53 */
54static DEFINE_RWLOCK(pathtbl_resize_lock);
55
56
Johannes Berg349eb8c2011-05-14 11:56:16 +020057static inline struct mesh_table *resize_dereference_mesh_paths(void)
58{
59 return rcu_dereference_protected(mesh_paths,
60 lockdep_is_held(&pathtbl_resize_lock));
61}
62
63static inline struct mesh_table *resize_dereference_mpp_paths(void)
64{
65 return rcu_dereference_protected(mpp_paths,
66 lockdep_is_held(&pathtbl_resize_lock));
67}
68
Javier Cardona5ee68e52011-08-09 16:45:08 -070069static int mesh_gate_add(struct mesh_table *tbl, struct mesh_path *mpath);
70
Johannes Berg349eb8c2011-05-14 11:56:16 +020071/*
72 * CAREFUL -- "tbl" must not be an expression,
73 * in particular not an rcu_dereference(), since
74 * it's used twice. So it is illegal to do
75 * for_each_mesh_entry(rcu_dereference(...), ...)
76 */
77#define for_each_mesh_entry(tbl, p, node, i) \
78 for (i = 0; i <= tbl->hash_mask; i++) \
79 hlist_for_each_entry_rcu(node, p, &tbl->hash_buckets[i], list)
80
81
Johannes Berg6b86bd62011-05-12 13:38:50 +020082static struct mesh_table *mesh_table_alloc(int size_order)
83{
84 int i;
85 struct mesh_table *newtbl;
86
Javier Cardonad676ff42011-05-17 16:13:34 -070087 newtbl = kmalloc(sizeof(struct mesh_table), GFP_ATOMIC);
Johannes Berg6b86bd62011-05-12 13:38:50 +020088 if (!newtbl)
89 return NULL;
90
91 newtbl->hash_buckets = kzalloc(sizeof(struct hlist_head) *
Javier Cardonad676ff42011-05-17 16:13:34 -070092 (1 << size_order), GFP_ATOMIC);
Johannes Berg6b86bd62011-05-12 13:38:50 +020093
94 if (!newtbl->hash_buckets) {
95 kfree(newtbl);
96 return NULL;
97 }
98
99 newtbl->hashwlock = kmalloc(sizeof(spinlock_t) *
Javier Cardonad676ff42011-05-17 16:13:34 -0700100 (1 << size_order), GFP_ATOMIC);
Johannes Berg6b86bd62011-05-12 13:38:50 +0200101 if (!newtbl->hashwlock) {
102 kfree(newtbl->hash_buckets);
103 kfree(newtbl);
104 return NULL;
105 }
106
107 newtbl->size_order = size_order;
108 newtbl->hash_mask = (1 << size_order) - 1;
109 atomic_set(&newtbl->entries, 0);
110 get_random_bytes(&newtbl->hash_rnd,
111 sizeof(newtbl->hash_rnd));
112 for (i = 0; i <= newtbl->hash_mask; i++)
113 spin_lock_init(&newtbl->hashwlock[i]);
Javier Cardona5ee68e52011-08-09 16:45:08 -0700114 spin_lock_init(&newtbl->gates_lock);
Johannes Berg6b86bd62011-05-12 13:38:50 +0200115
116 return newtbl;
117}
118
Javier Cardona18889232009-08-10 12:15:52 -0700119static void __mesh_table_free(struct mesh_table *tbl)
120{
121 kfree(tbl->hash_buckets);
122 kfree(tbl->hashwlock);
123 kfree(tbl);
124}
125
Johannes Berg6b86bd62011-05-12 13:38:50 +0200126static void mesh_table_free(struct mesh_table *tbl, bool free_leafs)
Javier Cardona18889232009-08-10 12:15:52 -0700127{
128 struct hlist_head *mesh_hash;
129 struct hlist_node *p, *q;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700130 struct mpath_node *gate;
Javier Cardona18889232009-08-10 12:15:52 -0700131 int i;
132
133 mesh_hash = tbl->hash_buckets;
134 for (i = 0; i <= tbl->hash_mask; i++) {
Javier Cardona9b84b8082011-05-03 16:57:16 -0700135 spin_lock_bh(&tbl->hashwlock[i]);
Javier Cardona18889232009-08-10 12:15:52 -0700136 hlist_for_each_safe(p, q, &mesh_hash[i]) {
137 tbl->free_node(p, free_leafs);
138 atomic_dec(&tbl->entries);
139 }
Javier Cardona9b84b8082011-05-03 16:57:16 -0700140 spin_unlock_bh(&tbl->hashwlock[i]);
Javier Cardona18889232009-08-10 12:15:52 -0700141 }
Javier Cardona5ee68e52011-08-09 16:45:08 -0700142 if (free_leafs) {
143 spin_lock_bh(&tbl->gates_lock);
144 hlist_for_each_entry_safe(gate, p, q,
145 tbl->known_gates, list) {
146 hlist_del(&gate->list);
147 kfree(gate);
148 }
149 kfree(tbl->known_gates);
150 spin_unlock_bh(&tbl->gates_lock);
151 }
152
Javier Cardona18889232009-08-10 12:15:52 -0700153 __mesh_table_free(tbl);
154}
155
cozybit Inca3e6b122011-04-13 11:10:28 -0700156static int mesh_table_grow(struct mesh_table *oldtbl,
Johannes Berg6b86bd62011-05-12 13:38:50 +0200157 struct mesh_table *newtbl)
Javier Cardona18889232009-08-10 12:15:52 -0700158{
Javier Cardona18889232009-08-10 12:15:52 -0700159 struct hlist_head *oldhash;
160 struct hlist_node *p, *q;
161 int i;
162
cozybit Inca3e6b122011-04-13 11:10:28 -0700163 if (atomic_read(&oldtbl->entries)
164 < oldtbl->mean_chain_len * (oldtbl->hash_mask + 1))
165 return -EAGAIN;
Javier Cardona18889232009-08-10 12:15:52 -0700166
cozybit Inca3e6b122011-04-13 11:10:28 -0700167 newtbl->free_node = oldtbl->free_node;
168 newtbl->mean_chain_len = oldtbl->mean_chain_len;
169 newtbl->copy_node = oldtbl->copy_node;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700170 newtbl->known_gates = oldtbl->known_gates;
cozybit Inca3e6b122011-04-13 11:10:28 -0700171 atomic_set(&newtbl->entries, atomic_read(&oldtbl->entries));
Javier Cardona18889232009-08-10 12:15:52 -0700172
cozybit Inca3e6b122011-04-13 11:10:28 -0700173 oldhash = oldtbl->hash_buckets;
174 for (i = 0; i <= oldtbl->hash_mask; i++)
Javier Cardona18889232009-08-10 12:15:52 -0700175 hlist_for_each(p, &oldhash[i])
cozybit Inca3e6b122011-04-13 11:10:28 -0700176 if (oldtbl->copy_node(p, newtbl) < 0)
Javier Cardona18889232009-08-10 12:15:52 -0700177 goto errcopy;
178
cozybit Inca3e6b122011-04-13 11:10:28 -0700179 return 0;
Javier Cardona18889232009-08-10 12:15:52 -0700180
181errcopy:
182 for (i = 0; i <= newtbl->hash_mask; i++) {
183 hlist_for_each_safe(p, q, &newtbl->hash_buckets[i])
cozybit Inca3e6b122011-04-13 11:10:28 -0700184 oldtbl->free_node(p, 0);
Javier Cardona18889232009-08-10 12:15:52 -0700185 }
cozybit Inca3e6b122011-04-13 11:10:28 -0700186 return -ENOMEM;
Javier Cardona18889232009-08-10 12:15:52 -0700187}
188
Johannes Berg6b86bd62011-05-12 13:38:50 +0200189static u32 mesh_table_hash(u8 *addr, struct ieee80211_sub_if_data *sdata,
190 struct mesh_table *tbl)
191{
192 /* Use last four bytes of hw addr and interface index as hash index */
193 return jhash_2words(*(u32 *)(addr+2), sdata->dev->ifindex, tbl->hash_rnd)
194 & tbl->hash_mask;
195}
Johannes Bergf5ea9122009-08-07 16:17:38 +0200196
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100197
198/**
199 *
200 * mesh_path_assign_nexthop - update mesh path next hop
201 *
202 * @mpath: mesh path to update
203 * @sta: next hop to assign
204 *
205 * Locking: mpath->state_lock must be held when calling this function
206 */
207void mesh_path_assign_nexthop(struct mesh_path *mpath, struct sta_info *sta)
208{
Javier Cardona10c836d2009-07-09 14:42:16 -0700209 struct sk_buff *skb;
210 struct ieee80211_hdr *hdr;
211 struct sk_buff_head tmpq;
212 unsigned long flags;
213
Johannes Bergd0709a62008-02-25 16:27:46 +0100214 rcu_assign_pointer(mpath->next_hop, sta);
Javier Cardona10c836d2009-07-09 14:42:16 -0700215
216 __skb_queue_head_init(&tmpq);
217
218 spin_lock_irqsave(&mpath->frame_queue.lock, flags);
219
220 while ((skb = __skb_dequeue(&mpath->frame_queue)) != NULL) {
221 hdr = (struct ieee80211_hdr *) skb->data;
222 memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN);
223 __skb_queue_tail(&tmpq, skb);
224 }
225
226 skb_queue_splice(&tmpq, &mpath->frame_queue);
227 spin_unlock_irqrestore(&mpath->frame_queue.lock, flags);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100228}
229
Javier Cardona5ee68e52011-08-09 16:45:08 -0700230static void prepare_for_gate(struct sk_buff *skb, char *dst_addr,
231 struct mesh_path *gate_mpath)
232{
233 struct ieee80211_hdr *hdr;
234 struct ieee80211s_hdr *mshdr;
235 int mesh_hdrlen, hdrlen;
236 char *next_hop;
237
238 hdr = (struct ieee80211_hdr *) skb->data;
239 hdrlen = ieee80211_hdrlen(hdr->frame_control);
240 mshdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
241
242 if (!(mshdr->flags & MESH_FLAGS_AE)) {
243 /* size of the fixed part of the mesh header */
244 mesh_hdrlen = 6;
245
246 /* make room for the two extended addresses */
247 skb_push(skb, 2 * ETH_ALEN);
248 memmove(skb->data, hdr, hdrlen + mesh_hdrlen);
249
250 hdr = (struct ieee80211_hdr *) skb->data;
251
252 /* we preserve the previous mesh header and only add
253 * the new addreses */
254 mshdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
255 mshdr->flags = MESH_FLAGS_AE_A5_A6;
256 memcpy(mshdr->eaddr1, hdr->addr3, ETH_ALEN);
257 memcpy(mshdr->eaddr2, hdr->addr4, ETH_ALEN);
258 }
259
260 /* update next hop */
261 hdr = (struct ieee80211_hdr *) skb->data;
262 rcu_read_lock();
263 next_hop = rcu_dereference(gate_mpath->next_hop)->sta.addr;
264 memcpy(hdr->addr1, next_hop, ETH_ALEN);
265 rcu_read_unlock();
266 memcpy(hdr->addr3, dst_addr, ETH_ALEN);
267}
268
269/**
270 *
271 * mesh_path_move_to_queue - Move or copy frames from one mpath queue to another
272 *
273 * This function is used to transfer or copy frames from an unresolved mpath to
274 * a gate mpath. The function also adds the Address Extension field and
275 * updates the next hop.
276 *
277 * If a frame already has an Address Extension field, only the next hop and
278 * destination addresses are updated.
279 *
280 * The gate mpath must be an active mpath with a valid mpath->next_hop.
281 *
282 * @mpath: An active mpath the frames will be sent to (i.e. the gate)
283 * @from_mpath: The failed mpath
284 * @copy: When true, copy all the frames to the new mpath queue. When false,
285 * move them.
286 */
287static void mesh_path_move_to_queue(struct mesh_path *gate_mpath,
288 struct mesh_path *from_mpath,
289 bool copy)
290{
Thomas Pedersenc61336612011-08-25 10:36:14 -0700291 struct sk_buff *skb, *cp_skb = NULL;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700292 struct sk_buff_head gateq, failq;
293 unsigned long flags;
294 int num_skbs;
295
296 BUG_ON(gate_mpath == from_mpath);
297 BUG_ON(!gate_mpath->next_hop);
298
299 __skb_queue_head_init(&gateq);
300 __skb_queue_head_init(&failq);
301
302 spin_lock_irqsave(&from_mpath->frame_queue.lock, flags);
303 skb_queue_splice_init(&from_mpath->frame_queue, &failq);
304 spin_unlock_irqrestore(&from_mpath->frame_queue.lock, flags);
305
306 num_skbs = skb_queue_len(&failq);
307
308 while (num_skbs--) {
309 skb = __skb_dequeue(&failq);
John W. Linville817a53d2011-08-24 15:12:41 -0400310 if (copy) {
Javier Cardona5ee68e52011-08-09 16:45:08 -0700311 cp_skb = skb_copy(skb, GFP_ATOMIC);
John W. Linville817a53d2011-08-24 15:12:41 -0400312 if (cp_skb)
313 __skb_queue_tail(&failq, cp_skb);
314 }
Javier Cardona5ee68e52011-08-09 16:45:08 -0700315
316 prepare_for_gate(skb, gate_mpath->dst, gate_mpath);
317 __skb_queue_tail(&gateq, skb);
Javier Cardona5ee68e52011-08-09 16:45:08 -0700318 }
319
320 spin_lock_irqsave(&gate_mpath->frame_queue.lock, flags);
321 skb_queue_splice(&gateq, &gate_mpath->frame_queue);
322 mpath_dbg("Mpath queue for gate %pM has %d frames\n",
323 gate_mpath->dst,
324 skb_queue_len(&gate_mpath->frame_queue));
325 spin_unlock_irqrestore(&gate_mpath->frame_queue.lock, flags);
326
327 if (!copy)
328 return;
329
330 spin_lock_irqsave(&from_mpath->frame_queue.lock, flags);
331 skb_queue_splice(&failq, &from_mpath->frame_queue);
332 spin_unlock_irqrestore(&from_mpath->frame_queue.lock, flags);
333}
334
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100335
336/**
337 * mesh_path_lookup - look up a path in the mesh path table
338 * @dst: hardware address (ETH_ALEN length) of destination
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200339 * @sdata: local subif
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100340 *
341 * Returns: pointer to the mesh path structure, or NULL if not found
342 *
343 * Locking: must be called within a read rcu section.
344 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200345struct mesh_path *mesh_path_lookup(u8 *dst, struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100346{
347 struct mesh_path *mpath;
348 struct hlist_node *n;
349 struct hlist_head *bucket;
350 struct mesh_table *tbl;
351 struct mpath_node *node;
352
353 tbl = rcu_dereference(mesh_paths);
354
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200355 bucket = &tbl->hash_buckets[mesh_table_hash(dst, sdata, tbl)];
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100356 hlist_for_each_entry_rcu(node, n, bucket, list) {
357 mpath = node->mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200358 if (mpath->sdata == sdata &&
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100359 memcmp(dst, mpath->dst, ETH_ALEN) == 0) {
360 if (MPATH_EXPIRED(mpath)) {
361 spin_lock_bh(&mpath->state_lock);
362 if (MPATH_EXPIRED(mpath))
363 mpath->flags &= ~MESH_PATH_ACTIVE;
364 spin_unlock_bh(&mpath->state_lock);
365 }
366 return mpath;
367 }
368 }
369 return NULL;
370}
371
YanBo79617de2008-09-22 13:30:32 +0800372struct mesh_path *mpp_path_lookup(u8 *dst, struct ieee80211_sub_if_data *sdata)
373{
374 struct mesh_path *mpath;
375 struct hlist_node *n;
376 struct hlist_head *bucket;
377 struct mesh_table *tbl;
378 struct mpath_node *node;
379
380 tbl = rcu_dereference(mpp_paths);
381
382 bucket = &tbl->hash_buckets[mesh_table_hash(dst, sdata, tbl)];
383 hlist_for_each_entry_rcu(node, n, bucket, list) {
384 mpath = node->mpath;
385 if (mpath->sdata == sdata &&
386 memcmp(dst, mpath->dst, ETH_ALEN) == 0) {
387 if (MPATH_EXPIRED(mpath)) {
388 spin_lock_bh(&mpath->state_lock);
389 if (MPATH_EXPIRED(mpath))
390 mpath->flags &= ~MESH_PATH_ACTIVE;
391 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);
423 if (MPATH_EXPIRED(node->mpath))
424 node->mpath->flags &= ~MESH_PATH_ACTIVE;
425 spin_unlock_bh(&node->mpath->state_lock);
426 }
427 return node->mpath;
428 }
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800429 }
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100430
431 return NULL;
432}
433
Javier Cardona5ee68e52011-08-09 16:45:08 -0700434static void mesh_gate_node_reclaim(struct rcu_head *rp)
435{
436 struct mpath_node *node = container_of(rp, struct mpath_node, rcu);
437 kfree(node);
438}
439
440/**
441 * mesh_gate_add - mark mpath as path to a mesh gate and add to known_gates
442 * @mesh_tbl: table which contains known_gates list
443 * @mpath: mpath to known mesh gate
444 *
445 * Returns: 0 on success
446 *
447 */
448static int mesh_gate_add(struct mesh_table *tbl, struct mesh_path *mpath)
449{
450 struct mpath_node *gate, *new_gate;
451 struct hlist_node *n;
452 int err;
453
454 rcu_read_lock();
455 tbl = rcu_dereference(tbl);
456
457 hlist_for_each_entry_rcu(gate, n, tbl->known_gates, list)
458 if (gate->mpath == mpath) {
459 err = -EEXIST;
460 goto err_rcu;
461 }
462
463 new_gate = kzalloc(sizeof(struct mpath_node), GFP_ATOMIC);
464 if (!new_gate) {
465 err = -ENOMEM;
466 goto err_rcu;
467 }
468
469 mpath->is_gate = true;
470 mpath->sdata->u.mesh.num_gates++;
471 new_gate->mpath = mpath;
472 spin_lock_bh(&tbl->gates_lock);
473 hlist_add_head_rcu(&new_gate->list, tbl->known_gates);
474 spin_unlock_bh(&tbl->gates_lock);
475 rcu_read_unlock();
476 mpath_dbg("Mesh path (%s): Recorded new gate: %pM. %d known gates\n",
477 mpath->sdata->name, mpath->dst,
478 mpath->sdata->u.mesh.num_gates);
479 return 0;
480err_rcu:
481 rcu_read_unlock();
482 return err;
483}
484
485/**
486 * mesh_gate_del - remove a mesh gate from the list of known gates
487 * @tbl: table which holds our list of known gates
488 * @mpath: gate mpath
489 *
490 * Returns: 0 on success
491 *
492 * Locking: must be called inside rcu_read_lock() section
493 */
494static int mesh_gate_del(struct mesh_table *tbl, struct mesh_path *mpath)
495{
496 struct mpath_node *gate;
497 struct hlist_node *p, *q;
498
499 tbl = rcu_dereference(tbl);
500
501 hlist_for_each_entry_safe(gate, p, q, tbl->known_gates, list)
502 if (gate->mpath == mpath) {
503 spin_lock_bh(&tbl->gates_lock);
504 hlist_del_rcu(&gate->list);
505 call_rcu(&gate->rcu, mesh_gate_node_reclaim);
506 spin_unlock_bh(&tbl->gates_lock);
507 mpath->sdata->u.mesh.num_gates--;
508 mpath->is_gate = false;
509 mpath_dbg("Mesh path (%s): Deleted gate: %pM. "
510 "%d known gates\n", mpath->sdata->name,
511 mpath->dst, mpath->sdata->u.mesh.num_gates);
512 break;
513 }
514
515 return 0;
516}
517
518/**
519 *
520 * mesh_path_add_gate - add the given mpath to a mesh gate to our path table
521 * @mpath: gate path to add to table
522 */
523int mesh_path_add_gate(struct mesh_path *mpath)
524{
525 return mesh_gate_add(mesh_paths, mpath);
526}
527
528/**
529 * mesh_gate_num - number of gates known to this interface
530 * @sdata: subif data
531 */
532int mesh_gate_num(struct ieee80211_sub_if_data *sdata)
533{
534 return sdata->u.mesh.num_gates;
535}
536
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100537/**
538 * mesh_path_add - allocate and add a new path to the mesh path table
539 * @addr: destination address of the path (ETH_ALEN length)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200540 * @sdata: local subif
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100541 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200542 * Returns: 0 on success
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100543 *
544 * State: the initial state of the new path is set to 0
545 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200546int mesh_path_add(u8 *dst, struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100547{
Javier Cardona18889232009-08-10 12:15:52 -0700548 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
549 struct ieee80211_local *local = sdata->local;
Johannes Berg349eb8c2011-05-14 11:56:16 +0200550 struct mesh_table *tbl;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100551 struct mesh_path *mpath, *new_mpath;
552 struct mpath_node *node, *new_node;
553 struct hlist_head *bucket;
554 struct hlist_node *n;
555 int grow = 0;
556 int err = 0;
557 u32 hash_idx;
558
Johannes Berg47846c92009-11-25 17:46:19 +0100559 if (memcmp(dst, sdata->vif.addr, ETH_ALEN) == 0)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100560 /* never add ourselves as neighbours */
561 return -ENOTSUPP;
562
563 if (is_multicast_ether_addr(dst))
564 return -ENOTSUPP;
565
Johannes Berg472dbc42008-09-11 00:01:49 +0200566 if (atomic_add_unless(&sdata->u.mesh.mpaths, 1, MESH_MAX_MPATHS) == 0)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100567 return -ENOSPC;
568
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400569 err = -ENOMEM;
Javier Cardona18889232009-08-10 12:15:52 -0700570 new_mpath = kzalloc(sizeof(struct mesh_path), GFP_ATOMIC);
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400571 if (!new_mpath)
572 goto err_path_alloc;
573
Javier Cardona18889232009-08-10 12:15:52 -0700574 new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400575 if (!new_node)
576 goto err_node_alloc;
Pavel Emelyanovf84e71a2008-05-06 18:46:36 +0400577
Javier Cardona9b84b8082011-05-03 16:57:16 -0700578 read_lock_bh(&pathtbl_resize_lock);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100579 memcpy(new_mpath->dst, dst, ETH_ALEN);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200580 new_mpath->sdata = sdata;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100581 new_mpath->flags = 0;
582 skb_queue_head_init(&new_mpath->frame_queue);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100583 new_node->mpath = new_mpath;
584 new_mpath->timer.data = (unsigned long) new_mpath;
585 new_mpath->timer.function = mesh_path_timer;
586 new_mpath->exp_time = jiffies;
587 spin_lock_init(&new_mpath->state_lock);
588 init_timer(&new_mpath->timer);
589
Johannes Berg349eb8c2011-05-14 11:56:16 +0200590 tbl = resize_dereference_mesh_paths();
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100591
Johannes Berg349eb8c2011-05-14 11:56:16 +0200592 hash_idx = mesh_table_hash(dst, sdata, tbl);
593 bucket = &tbl->hash_buckets[hash_idx];
594
595 spin_lock_bh(&tbl->hashwlock[hash_idx]);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100596
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400597 err = -EEXIST;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100598 hlist_for_each_entry(node, n, bucket, list) {
599 mpath = node->mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200600 if (mpath->sdata == sdata && memcmp(dst, mpath->dst, ETH_ALEN) == 0)
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400601 goto err_exists;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100602 }
603
604 hlist_add_head_rcu(&new_node->list, bucket);
Johannes Berg349eb8c2011-05-14 11:56:16 +0200605 if (atomic_inc_return(&tbl->entries) >=
606 tbl->mean_chain_len * (tbl->hash_mask + 1))
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100607 grow = 1;
608
Johannes Bergf5ea9122009-08-07 16:17:38 +0200609 mesh_paths_generation++;
610
Johannes Berg349eb8c2011-05-14 11:56:16 +0200611 spin_unlock_bh(&tbl->hashwlock[hash_idx]);
Javier Cardona9b84b8082011-05-03 16:57:16 -0700612 read_unlock_bh(&pathtbl_resize_lock);
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400613 if (grow) {
Javier Cardona18889232009-08-10 12:15:52 -0700614 set_bit(MESH_WORK_GROW_MPATH_TABLE, &ifmsh->wrkq_flags);
Johannes Berg64592c82010-06-10 10:21:31 +0200615 ieee80211_queue_work(&local->hw, &sdata->work);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100616 }
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400617 return 0;
618
619err_exists:
Johannes Berg349eb8c2011-05-14 11:56:16 +0200620 spin_unlock_bh(&tbl->hashwlock[hash_idx]);
Javier Cardona9b84b8082011-05-03 16:57:16 -0700621 read_unlock_bh(&pathtbl_resize_lock);
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400622 kfree(new_node);
623err_node_alloc:
624 kfree(new_mpath);
625err_path_alloc:
Johannes Berg472dbc42008-09-11 00:01:49 +0200626 atomic_dec(&sdata->u.mesh.mpaths);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100627 return err;
628}
629
Johannes Berg1928eca2011-05-14 11:00:52 +0200630static void mesh_table_free_rcu(struct rcu_head *rcu)
631{
632 struct mesh_table *tbl = container_of(rcu, struct mesh_table, rcu_head);
633
634 mesh_table_free(tbl, false);
635}
636
Javier Cardona18889232009-08-10 12:15:52 -0700637void mesh_mpath_table_grow(void)
638{
639 struct mesh_table *oldtbl, *newtbl;
640
Javier Cardona9b84b8082011-05-03 16:57:16 -0700641 write_lock_bh(&pathtbl_resize_lock);
Johannes Berg349eb8c2011-05-14 11:56:16 +0200642 oldtbl = resize_dereference_mesh_paths();
643 newtbl = mesh_table_alloc(oldtbl->size_order + 1);
Johannes Berg1928eca2011-05-14 11:00:52 +0200644 if (!newtbl)
645 goto out;
Johannes Berg349eb8c2011-05-14 11:56:16 +0200646 if (mesh_table_grow(oldtbl, newtbl) < 0) {
cozybit Inca3e6b122011-04-13 11:10:28 -0700647 __mesh_table_free(newtbl);
Johannes Berg1928eca2011-05-14 11:00:52 +0200648 goto out;
Javier Cardona18889232009-08-10 12:15:52 -0700649 }
650 rcu_assign_pointer(mesh_paths, newtbl);
Javier Cardona18889232009-08-10 12:15:52 -0700651
Johannes Berg1928eca2011-05-14 11:00:52 +0200652 call_rcu(&oldtbl->rcu_head, mesh_table_free_rcu);
653
654 out:
655 write_unlock_bh(&pathtbl_resize_lock);
Javier Cardona18889232009-08-10 12:15:52 -0700656}
657
658void mesh_mpp_table_grow(void)
659{
660 struct mesh_table *oldtbl, *newtbl;
661
Javier Cardona9b84b8082011-05-03 16:57:16 -0700662 write_lock_bh(&pathtbl_resize_lock);
Johannes Berg349eb8c2011-05-14 11:56:16 +0200663 oldtbl = resize_dereference_mpp_paths();
664 newtbl = mesh_table_alloc(oldtbl->size_order + 1);
Johannes Berg1928eca2011-05-14 11:00:52 +0200665 if (!newtbl)
666 goto out;
Johannes Berg349eb8c2011-05-14 11:56:16 +0200667 if (mesh_table_grow(oldtbl, newtbl) < 0) {
cozybit Inca3e6b122011-04-13 11:10:28 -0700668 __mesh_table_free(newtbl);
Johannes Berg1928eca2011-05-14 11:00:52 +0200669 goto out;
Javier Cardona18889232009-08-10 12:15:52 -0700670 }
671 rcu_assign_pointer(mpp_paths, newtbl);
Johannes Berg1928eca2011-05-14 11:00:52 +0200672 call_rcu(&oldtbl->rcu_head, mesh_table_free_rcu);
Javier Cardona18889232009-08-10 12:15:52 -0700673
Johannes Berg1928eca2011-05-14 11:00:52 +0200674 out:
675 write_unlock_bh(&pathtbl_resize_lock);
Javier Cardona18889232009-08-10 12:15:52 -0700676}
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100677
YanBo79617de2008-09-22 13:30:32 +0800678int mpp_path_add(u8 *dst, u8 *mpp, struct ieee80211_sub_if_data *sdata)
679{
Javier Cardona18889232009-08-10 12:15:52 -0700680 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
681 struct ieee80211_local *local = sdata->local;
Johannes Berg349eb8c2011-05-14 11:56:16 +0200682 struct mesh_table *tbl;
YanBo79617de2008-09-22 13:30:32 +0800683 struct mesh_path *mpath, *new_mpath;
684 struct mpath_node *node, *new_node;
685 struct hlist_head *bucket;
686 struct hlist_node *n;
687 int grow = 0;
688 int err = 0;
689 u32 hash_idx;
690
Johannes Berg47846c92009-11-25 17:46:19 +0100691 if (memcmp(dst, sdata->vif.addr, ETH_ALEN) == 0)
YanBo79617de2008-09-22 13:30:32 +0800692 /* never add ourselves as neighbours */
693 return -ENOTSUPP;
694
695 if (is_multicast_ether_addr(dst))
696 return -ENOTSUPP;
697
698 err = -ENOMEM;
Javier Cardona18889232009-08-10 12:15:52 -0700699 new_mpath = kzalloc(sizeof(struct mesh_path), GFP_ATOMIC);
YanBo79617de2008-09-22 13:30:32 +0800700 if (!new_mpath)
701 goto err_path_alloc;
702
Javier Cardona18889232009-08-10 12:15:52 -0700703 new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
YanBo79617de2008-09-22 13:30:32 +0800704 if (!new_node)
705 goto err_node_alloc;
706
Javier Cardona9b84b8082011-05-03 16:57:16 -0700707 read_lock_bh(&pathtbl_resize_lock);
YanBo79617de2008-09-22 13:30:32 +0800708 memcpy(new_mpath->dst, dst, ETH_ALEN);
709 memcpy(new_mpath->mpp, mpp, ETH_ALEN);
710 new_mpath->sdata = sdata;
711 new_mpath->flags = 0;
712 skb_queue_head_init(&new_mpath->frame_queue);
713 new_node->mpath = new_mpath;
Thomas Pedersenc61336612011-08-25 10:36:14 -0700714 init_timer(&new_mpath->timer);
YanBo79617de2008-09-22 13:30:32 +0800715 new_mpath->exp_time = jiffies;
716 spin_lock_init(&new_mpath->state_lock);
717
Johannes Berg349eb8c2011-05-14 11:56:16 +0200718 tbl = resize_dereference_mpp_paths();
YanBo79617de2008-09-22 13:30:32 +0800719
Johannes Berg349eb8c2011-05-14 11:56:16 +0200720 hash_idx = mesh_table_hash(dst, sdata, tbl);
721 bucket = &tbl->hash_buckets[hash_idx];
722
723 spin_lock_bh(&tbl->hashwlock[hash_idx]);
YanBo79617de2008-09-22 13:30:32 +0800724
725 err = -EEXIST;
726 hlist_for_each_entry(node, n, bucket, list) {
727 mpath = node->mpath;
728 if (mpath->sdata == sdata && memcmp(dst, mpath->dst, ETH_ALEN) == 0)
729 goto err_exists;
730 }
731
732 hlist_add_head_rcu(&new_node->list, bucket);
Johannes Berg349eb8c2011-05-14 11:56:16 +0200733 if (atomic_inc_return(&tbl->entries) >=
734 tbl->mean_chain_len * (tbl->hash_mask + 1))
YanBo79617de2008-09-22 13:30:32 +0800735 grow = 1;
736
Johannes Berg349eb8c2011-05-14 11:56:16 +0200737 spin_unlock_bh(&tbl->hashwlock[hash_idx]);
Javier Cardona9b84b8082011-05-03 16:57:16 -0700738 read_unlock_bh(&pathtbl_resize_lock);
YanBo79617de2008-09-22 13:30:32 +0800739 if (grow) {
Javier Cardona18889232009-08-10 12:15:52 -0700740 set_bit(MESH_WORK_GROW_MPP_TABLE, &ifmsh->wrkq_flags);
Johannes Berg64592c82010-06-10 10:21:31 +0200741 ieee80211_queue_work(&local->hw, &sdata->work);
YanBo79617de2008-09-22 13:30:32 +0800742 }
743 return 0;
744
745err_exists:
Johannes Berg349eb8c2011-05-14 11:56:16 +0200746 spin_unlock_bh(&tbl->hashwlock[hash_idx]);
Javier Cardona9b84b8082011-05-03 16:57:16 -0700747 read_unlock_bh(&pathtbl_resize_lock);
YanBo79617de2008-09-22 13:30:32 +0800748 kfree(new_node);
749err_node_alloc:
750 kfree(new_mpath);
751err_path_alloc:
752 return err;
753}
754
755
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100756/**
757 * mesh_plink_broken - deactivates paths and sends perr when a link breaks
758 *
759 * @sta: broken peer link
760 *
761 * This function must be called from the rate control algorithm if enough
762 * delivery errors suggest that a peer link is no longer usable.
763 */
764void mesh_plink_broken(struct sta_info *sta)
765{
Johannes Berg349eb8c2011-05-14 11:56:16 +0200766 struct mesh_table *tbl;
Johannes Berg15ff6362009-11-17 13:34:04 +0100767 static const u8 bcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100768 struct mesh_path *mpath;
769 struct mpath_node *node;
770 struct hlist_node *p;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200771 struct ieee80211_sub_if_data *sdata = sta->sdata;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100772 int i;
Thomas Pedersen25d49e42011-08-11 19:35:15 -0700773 __le16 reason = cpu_to_le16(WLAN_REASON_MESH_PATH_DEST_UNREACHABLE);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100774
775 rcu_read_lock();
Johannes Berg349eb8c2011-05-14 11:56:16 +0200776 tbl = rcu_dereference(mesh_paths);
777 for_each_mesh_entry(tbl, p, node, i) {
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100778 mpath = node->mpath;
779 spin_lock_bh(&mpath->state_lock);
Johannes Berg349eb8c2011-05-14 11:56:16 +0200780 if (rcu_dereference(mpath->next_hop) == sta &&
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100781 mpath->flags & MESH_PATH_ACTIVE &&
782 !(mpath->flags & MESH_PATH_FIXED)) {
783 mpath->flags &= ~MESH_PATH_ACTIVE;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000784 ++mpath->sn;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100785 spin_unlock_bh(&mpath->state_lock);
Javier Cardona45904f22010-12-03 09:20:40 +0100786 mesh_path_error_tx(sdata->u.mesh.mshcfg.element_ttl,
787 mpath->dst, cpu_to_le32(mpath->sn),
Thomas Pedersen25d49e42011-08-11 19:35:15 -0700788 reason, bcast, sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100789 } else
790 spin_unlock_bh(&mpath->state_lock);
791 }
792 rcu_read_unlock();
793}
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100794
795/**
796 * mesh_path_flush_by_nexthop - Deletes mesh paths if their next hop matches
797 *
798 * @sta - mesh peer to match
799 *
Luis Carlos Cobob4e08ea2008-02-29 15:46:08 -0800800 * RCU notes: this function is called when a mesh plink transitions from
801 * PLINK_ESTAB to any other state, since PLINK_ESTAB state is the only one that
802 * allows path creation. This will happen before the sta can be freed (because
Johannes Bergd0709a62008-02-25 16:27:46 +0100803 * sta_info_destroy() calls this) so any reader in a rcu read block will be
804 * protected against the plink disappearing.
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100805 */
806void mesh_path_flush_by_nexthop(struct sta_info *sta)
807{
Johannes Berg349eb8c2011-05-14 11:56:16 +0200808 struct mesh_table *tbl;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100809 struct mesh_path *mpath;
810 struct mpath_node *node;
811 struct hlist_node *p;
812 int i;
813
Johannes Berg349eb8c2011-05-14 11:56:16 +0200814 rcu_read_lock();
815 tbl = rcu_dereference(mesh_paths);
816 for_each_mesh_entry(tbl, p, node, i) {
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100817 mpath = node->mpath;
Johannes Berg349eb8c2011-05-14 11:56:16 +0200818 if (rcu_dereference(mpath->next_hop) == sta)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200819 mesh_path_del(mpath->dst, mpath->sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100820 }
Johannes Berg349eb8c2011-05-14 11:56:16 +0200821 rcu_read_unlock();
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100822}
823
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200824void mesh_path_flush(struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100825{
Johannes Berg349eb8c2011-05-14 11:56:16 +0200826 struct mesh_table *tbl;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100827 struct mesh_path *mpath;
828 struct mpath_node *node;
829 struct hlist_node *p;
830 int i;
831
Johannes Berg349eb8c2011-05-14 11:56:16 +0200832 rcu_read_lock();
833 tbl = rcu_dereference(mesh_paths);
834 for_each_mesh_entry(tbl, p, node, i) {
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100835 mpath = node->mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200836 if (mpath->sdata == sdata)
837 mesh_path_del(mpath->dst, mpath->sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100838 }
Johannes Berg349eb8c2011-05-14 11:56:16 +0200839 rcu_read_unlock();
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100840}
841
842static void mesh_path_node_reclaim(struct rcu_head *rp)
843{
844 struct mpath_node *node = container_of(rp, struct mpath_node, rcu);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200845 struct ieee80211_sub_if_data *sdata = node->mpath->sdata;
Johannes Bergd0709a62008-02-25 16:27:46 +0100846
Thomas Pedersenc61336612011-08-25 10:36:14 -0700847 del_timer_sync(&node->mpath->timer);
Johannes Berg472dbc42008-09-11 00:01:49 +0200848 atomic_dec(&sdata->u.mesh.mpaths);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100849 kfree(node->mpath);
850 kfree(node);
851}
852
853/**
854 * mesh_path_del - delete a mesh path from the table
855 *
856 * @addr: dst address (ETH_ALEN length)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200857 * @sdata: local subif
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100858 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200859 * Returns: 0 if successful
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100860 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200861int mesh_path_del(u8 *addr, struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100862{
Johannes Berg349eb8c2011-05-14 11:56:16 +0200863 struct mesh_table *tbl;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100864 struct mesh_path *mpath;
865 struct mpath_node *node;
866 struct hlist_head *bucket;
867 struct hlist_node *n;
868 int hash_idx;
869 int err = 0;
870
Javier Cardona9b84b8082011-05-03 16:57:16 -0700871 read_lock_bh(&pathtbl_resize_lock);
Johannes Berg349eb8c2011-05-14 11:56:16 +0200872 tbl = resize_dereference_mesh_paths();
873 hash_idx = mesh_table_hash(addr, sdata, tbl);
874 bucket = &tbl->hash_buckets[hash_idx];
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100875
Johannes Berg349eb8c2011-05-14 11:56:16 +0200876 spin_lock_bh(&tbl->hashwlock[hash_idx]);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100877 hlist_for_each_entry(node, n, bucket, list) {
878 mpath = node->mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200879 if (mpath->sdata == sdata &&
Johannes Berg349eb8c2011-05-14 11:56:16 +0200880 memcmp(addr, mpath->dst, ETH_ALEN) == 0) {
Javier Cardonaa6965c42011-08-09 16:45:06 -0700881 spin_lock_bh(&mpath->state_lock);
Javier Cardona5ee68e52011-08-09 16:45:08 -0700882 if (mpath->is_gate)
883 mesh_gate_del(tbl, mpath);
Luis Carlos Cobocfa22c72008-02-29 15:04:13 -0800884 mpath->flags |= MESH_PATH_RESOLVING;
885 hlist_del_rcu(&node->list);
886 call_rcu(&node->rcu, mesh_path_node_reclaim);
Johannes Berg349eb8c2011-05-14 11:56:16 +0200887 atomic_dec(&tbl->entries);
Javier Cardonaa6965c42011-08-09 16:45:06 -0700888 spin_unlock_bh(&mpath->state_lock);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100889 goto enddel;
890 }
891 }
892
893 err = -ENXIO;
894enddel:
Johannes Bergf5ea9122009-08-07 16:17:38 +0200895 mesh_paths_generation++;
Johannes Berg349eb8c2011-05-14 11:56:16 +0200896 spin_unlock_bh(&tbl->hashwlock[hash_idx]);
Javier Cardona9b84b8082011-05-03 16:57:16 -0700897 read_unlock_bh(&pathtbl_resize_lock);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100898 return err;
899}
900
901/**
902 * mesh_path_tx_pending - sends pending frames in a mesh path queue
903 *
904 * @mpath: mesh path to activate
905 *
906 * Locking: the state_lock of the mpath structure must NOT be held when calling
907 * this function.
908 */
909void mesh_path_tx_pending(struct mesh_path *mpath)
910{
Javier Cardona249b4052009-07-07 10:55:03 -0700911 if (mpath->flags & MESH_PATH_ACTIVE)
912 ieee80211_add_pending_skbs(mpath->sdata->local,
913 &mpath->frame_queue);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100914}
915
916/**
Javier Cardona5ee68e52011-08-09 16:45:08 -0700917 * mesh_path_send_to_gates - sends pending frames to all known mesh gates
918 *
919 * @mpath: mesh path whose queue will be emptied
920 *
921 * If there is only one gate, the frames are transferred from the failed mpath
922 * queue to that gate's queue. If there are more than one gates, the frames
923 * are copied from each gate to the next. After frames are copied, the
924 * mpath queues are emptied onto the transmission queue.
925 */
926int mesh_path_send_to_gates(struct mesh_path *mpath)
927{
928 struct ieee80211_sub_if_data *sdata = mpath->sdata;
929 struct hlist_node *n;
930 struct mesh_table *tbl;
931 struct mesh_path *from_mpath = mpath;
932 struct mpath_node *gate = NULL;
933 bool copy = false;
934 struct hlist_head *known_gates;
935
936 rcu_read_lock();
937 tbl = rcu_dereference(mesh_paths);
938 known_gates = tbl->known_gates;
939 rcu_read_unlock();
940
941 if (!known_gates)
942 return -EHOSTUNREACH;
943
944 hlist_for_each_entry_rcu(gate, n, known_gates, list) {
945 if (gate->mpath->sdata != sdata)
946 continue;
947
948 if (gate->mpath->flags & MESH_PATH_ACTIVE) {
949 mpath_dbg("Forwarding to %pM\n", gate->mpath->dst);
950 mesh_path_move_to_queue(gate->mpath, from_mpath, copy);
951 from_mpath = gate->mpath;
952 copy = true;
953 } else {
954 mpath_dbg("Not forwarding %p\n", gate->mpath);
955 mpath_dbg("flags %x\n", gate->mpath->flags);
956 }
957 }
958
959 hlist_for_each_entry_rcu(gate, n, known_gates, list)
960 if (gate->mpath->sdata == sdata) {
961 mpath_dbg("Sending to %pM\n", gate->mpath->dst);
962 mesh_path_tx_pending(gate->mpath);
963 }
964
965 return (from_mpath == mpath) ? -EHOSTUNREACH : 0;
966}
967
968/**
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100969 * mesh_path_discard_frame - discard a frame whose path could not be resolved
970 *
971 * @skb: frame to discard
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200972 * @sdata: network subif the frame was to be sent through
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100973 *
Javier Cardona35946a52009-07-13 17:00:10 -0700974 * If the frame was being forwarded from another MP, a PERR frame will be sent
975 * to the precursor. The precursor's address (i.e. the previous hop) was saved
976 * in addr1 of the frame-to-be-forwarded, and would only be overwritten once
977 * the destination is successfully resolved.
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100978 *
979 * Locking: the function must me called within a rcu_read_lock region
980 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200981void mesh_path_discard_frame(struct sk_buff *skb,
982 struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100983{
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +0200984 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100985 struct mesh_path *mpath;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000986 u32 sn = 0;
Thomas Pedersen25d49e42011-08-11 19:35:15 -0700987 __le16 reason = cpu_to_le16(WLAN_REASON_MESH_PATH_NOFORWARD);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100988
Johannes Berg47846c92009-11-25 17:46:19 +0100989 if (memcmp(hdr->addr4, sdata->vif.addr, ETH_ALEN) != 0) {
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100990 u8 *ra, *da;
991
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +0200992 da = hdr->addr3;
Javier Cardona35946a52009-07-13 17:00:10 -0700993 ra = hdr->addr1;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200994 mpath = mesh_path_lookup(da, sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100995 if (mpath)
Rui Paulod19b3bf2009-11-09 23:46:55 +0000996 sn = ++mpath->sn;
Javier Cardona45904f22010-12-03 09:20:40 +0100997 mesh_path_error_tx(sdata->u.mesh.mshcfg.element_ttl, skb->data,
Thomas Pedersen25d49e42011-08-11 19:35:15 -0700998 cpu_to_le32(sn), reason, ra, sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100999 }
1000
1001 kfree_skb(skb);
Johannes Berg472dbc42008-09-11 00:01:49 +02001002 sdata->u.mesh.mshstats.dropped_frames_no_route++;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001003}
1004
1005/**
1006 * mesh_path_flush_pending - free the pending queue of a mesh path
1007 *
1008 * @mpath: mesh path whose queue has to be freed
1009 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001010 * Locking: the function must me called within a rcu_read_lock region
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001011 */
1012void mesh_path_flush_pending(struct mesh_path *mpath)
1013{
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001014 struct sk_buff *skb;
1015
Javier Cardona00e3f252011-08-09 16:45:07 -07001016 while ((skb = skb_dequeue(&mpath->frame_queue)) != NULL)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001017 mesh_path_discard_frame(skb, mpath->sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001018}
1019
1020/**
1021 * mesh_path_fix_nexthop - force a specific next hop for a mesh path
1022 *
1023 * @mpath: the mesh path to modify
1024 * @next_hop: the next hop to force
1025 *
1026 * Locking: this function must be called holding mpath->state_lock
1027 */
1028void mesh_path_fix_nexthop(struct mesh_path *mpath, struct sta_info *next_hop)
1029{
1030 spin_lock_bh(&mpath->state_lock);
1031 mesh_path_assign_nexthop(mpath, next_hop);
Rui Paulod19b3bf2009-11-09 23:46:55 +00001032 mpath->sn = 0xffff;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001033 mpath->metric = 0;
1034 mpath->hop_count = 0;
1035 mpath->exp_time = 0;
1036 mpath->flags |= MESH_PATH_FIXED;
1037 mesh_path_activate(mpath);
1038 spin_unlock_bh(&mpath->state_lock);
1039 mesh_path_tx_pending(mpath);
1040}
1041
1042static void mesh_path_node_free(struct hlist_node *p, bool free_leafs)
1043{
1044 struct mesh_path *mpath;
1045 struct mpath_node *node = hlist_entry(p, struct mpath_node, list);
1046 mpath = node->mpath;
1047 hlist_del_rcu(p);
Javier Cardonad0df9ee2011-05-13 14:16:07 -07001048 if (free_leafs) {
Thomas Pedersenc61336612011-08-25 10:36:14 -07001049 del_timer_sync(&mpath->timer);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001050 kfree(mpath);
Javier Cardonad0df9ee2011-05-13 14:16:07 -07001051 }
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001052 kfree(node);
1053}
1054
Pavel Emelyanov4caf86c2008-05-07 19:47:01 +04001055static int mesh_path_node_copy(struct hlist_node *p, struct mesh_table *newtbl)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001056{
1057 struct mesh_path *mpath;
1058 struct mpath_node *node, *new_node;
1059 u32 hash_idx;
1060
Pavel Emelyanov8566dc32008-05-07 19:51:51 +04001061 new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
Pavel Emelyanov00242c42008-05-07 19:48:14 +04001062 if (new_node == NULL)
1063 return -ENOMEM;
1064
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001065 node = hlist_entry(p, struct mpath_node, list);
1066 mpath = node->mpath;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001067 new_node->mpath = mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001068 hash_idx = mesh_table_hash(mpath->dst, mpath->sdata, newtbl);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001069 hlist_add_head(&new_node->list,
1070 &newtbl->hash_buckets[hash_idx]);
Pavel Emelyanov4caf86c2008-05-07 19:47:01 +04001071 return 0;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001072}
1073
1074int mesh_pathtbl_init(void)
1075{
Johannes Berg349eb8c2011-05-14 11:56:16 +02001076 struct mesh_table *tbl_path, *tbl_mpp;
YanBo79617de2008-09-22 13:30:32 +08001077
Johannes Berg349eb8c2011-05-14 11:56:16 +02001078 tbl_path = mesh_table_alloc(INIT_PATHS_SIZE_ORDER);
1079 if (!tbl_path)
1080 return -ENOMEM;
1081 tbl_path->free_node = &mesh_path_node_free;
1082 tbl_path->copy_node = &mesh_path_node_copy;
1083 tbl_path->mean_chain_len = MEAN_CHAIN_LEN;
Javier Cardona5ee68e52011-08-09 16:45:08 -07001084 tbl_path->known_gates = kzalloc(sizeof(struct hlist_head), GFP_ATOMIC);
1085 INIT_HLIST_HEAD(tbl_path->known_gates);
1086
Johannes Berg349eb8c2011-05-14 11:56:16 +02001087
1088 tbl_mpp = mesh_table_alloc(INIT_PATHS_SIZE_ORDER);
1089 if (!tbl_mpp) {
1090 mesh_table_free(tbl_path, true);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001091 return -ENOMEM;
YanBo79617de2008-09-22 13:30:32 +08001092 }
Johannes Berg349eb8c2011-05-14 11:56:16 +02001093 tbl_mpp->free_node = &mesh_path_node_free;
1094 tbl_mpp->copy_node = &mesh_path_node_copy;
1095 tbl_mpp->mean_chain_len = MEAN_CHAIN_LEN;
Javier Cardona5ee68e52011-08-09 16:45:08 -07001096 tbl_mpp->known_gates = kzalloc(sizeof(struct hlist_head), GFP_ATOMIC);
1097 INIT_HLIST_HEAD(tbl_mpp->known_gates);
Johannes Berg349eb8c2011-05-14 11:56:16 +02001098
1099 /* Need no locking since this is during init */
1100 RCU_INIT_POINTER(mesh_paths, tbl_path);
1101 RCU_INIT_POINTER(mpp_paths, tbl_mpp);
YanBo79617de2008-09-22 13:30:32 +08001102
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001103 return 0;
1104}
1105
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001106void mesh_path_expire(struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001107{
Johannes Berg349eb8c2011-05-14 11:56:16 +02001108 struct mesh_table *tbl;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001109 struct mesh_path *mpath;
1110 struct mpath_node *node;
1111 struct hlist_node *p;
1112 int i;
1113
Johannes Berg349eb8c2011-05-14 11:56:16 +02001114 rcu_read_lock();
1115 tbl = rcu_dereference(mesh_paths);
1116 for_each_mesh_entry(tbl, p, node, i) {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001117 if (node->mpath->sdata != sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001118 continue;
1119 mpath = node->mpath;
1120 spin_lock_bh(&mpath->state_lock);
1121 if ((!(mpath->flags & MESH_PATH_RESOLVING)) &&
1122 (!(mpath->flags & MESH_PATH_FIXED)) &&
Johannes Berg349eb8c2011-05-14 11:56:16 +02001123 time_after(jiffies, mpath->exp_time + MESH_PATH_EXPIRE)) {
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001124 spin_unlock_bh(&mpath->state_lock);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001125 mesh_path_del(mpath->dst, mpath->sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001126 } else
1127 spin_unlock_bh(&mpath->state_lock);
1128 }
Johannes Berg349eb8c2011-05-14 11:56:16 +02001129 rcu_read_unlock();
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001130}
1131
1132void mesh_pathtbl_unregister(void)
1133{
Johannes Berg349eb8c2011-05-14 11:56:16 +02001134 /* no need for locking during exit path */
1135 mesh_table_free(rcu_dereference_raw(mesh_paths), true);
1136 mesh_table_free(rcu_dereference_raw(mpp_paths), true);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001137}