]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/mac80211/mesh_pathtbl.c
mac80211: fix mpath timer NULL function
[mirror_ubuntu-artful-kernel.git] / net / mac80211 / mesh_pathtbl.c
CommitLineData
eb2b9311 1/*
264d9b7d 2 * Copyright (c) 2008, 2009 open80211s Ltd.
eb2b9311
LCC
3 * Author: Luis Carlos Cobo <luisca@cozybit.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
9
10#include <linux/etherdevice.h>
11#include <linux/list.h>
eb2b9311 12#include <linux/random.h>
5a0e3ad6 13#include <linux/slab.h>
eb2b9311
LCC
14#include <linux/spinlock.h>
15#include <linux/string.h>
16#include <net/mac80211.h>
17#include "ieee80211_i.h"
18#include "mesh.h"
19
7646887a
JC
20#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
eb2b9311
LCC
26/* 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
349eb8cf
JB
45static struct mesh_table __rcu *mesh_paths;
46static struct mesh_table __rcu *mpp_paths; /* Store paths for MPP&MAP */
eb2b9311 47
f5ea9120 48int mesh_paths_generation;
6b86bd62
JB
49
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
349eb8cf
JB
57static 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
69/*
70 * CAREFUL -- "tbl" must not be an expression,
71 * in particular not an rcu_dereference(), since
72 * it's used twice. So it is illegal to do
73 * for_each_mesh_entry(rcu_dereference(...), ...)
74 */
75#define for_each_mesh_entry(tbl, p, node, i) \
76 for (i = 0; i <= tbl->hash_mask; i++) \
77 hlist_for_each_entry_rcu(node, p, &tbl->hash_buckets[i], list)
78
79
6b86bd62
JB
80static struct mesh_table *mesh_table_alloc(int size_order)
81{
82 int i;
83 struct mesh_table *newtbl;
84
d676ff49 85 newtbl = kmalloc(sizeof(struct mesh_table), GFP_ATOMIC);
6b86bd62
JB
86 if (!newtbl)
87 return NULL;
88
89 newtbl->hash_buckets = kzalloc(sizeof(struct hlist_head) *
d676ff49 90 (1 << size_order), GFP_ATOMIC);
6b86bd62
JB
91
92 if (!newtbl->hash_buckets) {
93 kfree(newtbl);
94 return NULL;
95 }
96
97 newtbl->hashwlock = kmalloc(sizeof(spinlock_t) *
d676ff49 98 (1 << size_order), GFP_ATOMIC);
6b86bd62
JB
99 if (!newtbl->hashwlock) {
100 kfree(newtbl->hash_buckets);
101 kfree(newtbl);
102 return NULL;
103 }
104
105 newtbl->size_order = size_order;
106 newtbl->hash_mask = (1 << size_order) - 1;
107 atomic_set(&newtbl->entries, 0);
108 get_random_bytes(&newtbl->hash_rnd,
109 sizeof(newtbl->hash_rnd));
110 for (i = 0; i <= newtbl->hash_mask; i++)
111 spin_lock_init(&newtbl->hashwlock[i]);
112
113 return newtbl;
114}
115
18889231
JC
116static void __mesh_table_free(struct mesh_table *tbl)
117{
118 kfree(tbl->hash_buckets);
119 kfree(tbl->hashwlock);
120 kfree(tbl);
121}
122
6b86bd62 123static void mesh_table_free(struct mesh_table *tbl, bool free_leafs)
18889231
JC
124{
125 struct hlist_head *mesh_hash;
126 struct hlist_node *p, *q;
127 int i;
128
129 mesh_hash = tbl->hash_buckets;
130 for (i = 0; i <= tbl->hash_mask; i++) {
9b84b808 131 spin_lock_bh(&tbl->hashwlock[i]);
18889231
JC
132 hlist_for_each_safe(p, q, &mesh_hash[i]) {
133 tbl->free_node(p, free_leafs);
134 atomic_dec(&tbl->entries);
135 }
9b84b808 136 spin_unlock_bh(&tbl->hashwlock[i]);
18889231
JC
137 }
138 __mesh_table_free(tbl);
139}
140
a3e6b12c 141static int mesh_table_grow(struct mesh_table *oldtbl,
6b86bd62 142 struct mesh_table *newtbl)
18889231 143{
18889231
JC
144 struct hlist_head *oldhash;
145 struct hlist_node *p, *q;
146 int i;
147
a3e6b12c
I
148 if (atomic_read(&oldtbl->entries)
149 < oldtbl->mean_chain_len * (oldtbl->hash_mask + 1))
150 return -EAGAIN;
18889231 151
a3e6b12c
I
152 newtbl->free_node = oldtbl->free_node;
153 newtbl->mean_chain_len = oldtbl->mean_chain_len;
154 newtbl->copy_node = oldtbl->copy_node;
155 atomic_set(&newtbl->entries, atomic_read(&oldtbl->entries));
18889231 156
a3e6b12c
I
157 oldhash = oldtbl->hash_buckets;
158 for (i = 0; i <= oldtbl->hash_mask; i++)
18889231 159 hlist_for_each(p, &oldhash[i])
a3e6b12c 160 if (oldtbl->copy_node(p, newtbl) < 0)
18889231
JC
161 goto errcopy;
162
a3e6b12c 163 return 0;
18889231
JC
164
165errcopy:
166 for (i = 0; i <= newtbl->hash_mask; i++) {
167 hlist_for_each_safe(p, q, &newtbl->hash_buckets[i])
a3e6b12c 168 oldtbl->free_node(p, 0);
18889231 169 }
a3e6b12c 170 return -ENOMEM;
18889231
JC
171}
172
6b86bd62
JB
173static u32 mesh_table_hash(u8 *addr, struct ieee80211_sub_if_data *sdata,
174 struct mesh_table *tbl)
175{
176 /* Use last four bytes of hw addr and interface index as hash index */
177 return jhash_2words(*(u32 *)(addr+2), sdata->dev->ifindex, tbl->hash_rnd)
178 & tbl->hash_mask;
179}
f5ea9120 180
eb2b9311
LCC
181
182/**
183 *
184 * mesh_path_assign_nexthop - update mesh path next hop
185 *
186 * @mpath: mesh path to update
187 * @sta: next hop to assign
188 *
189 * Locking: mpath->state_lock must be held when calling this function
190 */
191void mesh_path_assign_nexthop(struct mesh_path *mpath, struct sta_info *sta)
192{
10c836d7
JC
193 struct sk_buff *skb;
194 struct ieee80211_hdr *hdr;
195 struct sk_buff_head tmpq;
196 unsigned long flags;
197
d0709a65 198 rcu_assign_pointer(mpath->next_hop, sta);
10c836d7
JC
199
200 __skb_queue_head_init(&tmpq);
201
202 spin_lock_irqsave(&mpath->frame_queue.lock, flags);
203
204 while ((skb = __skb_dequeue(&mpath->frame_queue)) != NULL) {
205 hdr = (struct ieee80211_hdr *) skb->data;
206 memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN);
207 __skb_queue_tail(&tmpq, skb);
208 }
209
210 skb_queue_splice(&tmpq, &mpath->frame_queue);
211 spin_unlock_irqrestore(&mpath->frame_queue.lock, flags);
eb2b9311
LCC
212}
213
214
215/**
216 * mesh_path_lookup - look up a path in the mesh path table
217 * @dst: hardware address (ETH_ALEN length) of destination
f698d856 218 * @sdata: local subif
eb2b9311
LCC
219 *
220 * Returns: pointer to the mesh path structure, or NULL if not found
221 *
222 * Locking: must be called within a read rcu section.
223 */
f698d856 224struct mesh_path *mesh_path_lookup(u8 *dst, struct ieee80211_sub_if_data *sdata)
eb2b9311
LCC
225{
226 struct mesh_path *mpath;
227 struct hlist_node *n;
228 struct hlist_head *bucket;
229 struct mesh_table *tbl;
230 struct mpath_node *node;
231
232 tbl = rcu_dereference(mesh_paths);
233
f698d856 234 bucket = &tbl->hash_buckets[mesh_table_hash(dst, sdata, tbl)];
eb2b9311
LCC
235 hlist_for_each_entry_rcu(node, n, bucket, list) {
236 mpath = node->mpath;
f698d856 237 if (mpath->sdata == sdata &&
eb2b9311
LCC
238 memcmp(dst, mpath->dst, ETH_ALEN) == 0) {
239 if (MPATH_EXPIRED(mpath)) {
240 spin_lock_bh(&mpath->state_lock);
241 if (MPATH_EXPIRED(mpath))
242 mpath->flags &= ~MESH_PATH_ACTIVE;
243 spin_unlock_bh(&mpath->state_lock);
244 }
245 return mpath;
246 }
247 }
248 return NULL;
249}
250
79617dee
Y
251struct mesh_path *mpp_path_lookup(u8 *dst, struct ieee80211_sub_if_data *sdata)
252{
253 struct mesh_path *mpath;
254 struct hlist_node *n;
255 struct hlist_head *bucket;
256 struct mesh_table *tbl;
257 struct mpath_node *node;
258
259 tbl = rcu_dereference(mpp_paths);
260
261 bucket = &tbl->hash_buckets[mesh_table_hash(dst, sdata, tbl)];
262 hlist_for_each_entry_rcu(node, n, bucket, list) {
263 mpath = node->mpath;
264 if (mpath->sdata == sdata &&
265 memcmp(dst, mpath->dst, ETH_ALEN) == 0) {
266 if (MPATH_EXPIRED(mpath)) {
267 spin_lock_bh(&mpath->state_lock);
268 if (MPATH_EXPIRED(mpath))
269 mpath->flags &= ~MESH_PATH_ACTIVE;
270 spin_unlock_bh(&mpath->state_lock);
271 }
272 return mpath;
273 }
274 }
275 return NULL;
276}
277
278
eb2b9311
LCC
279/**
280 * mesh_path_lookup_by_idx - look up a path in the mesh path table by its index
281 * @idx: index
f698d856 282 * @sdata: local subif, or NULL for all entries
eb2b9311
LCC
283 *
284 * Returns: pointer to the mesh path structure, or NULL if not found.
285 *
286 * Locking: must be called within a read rcu section.
287 */
f698d856 288struct mesh_path *mesh_path_lookup_by_idx(int idx, struct ieee80211_sub_if_data *sdata)
eb2b9311 289{
349eb8cf 290 struct mesh_table *tbl = rcu_dereference(mesh_paths);
eb2b9311
LCC
291 struct mpath_node *node;
292 struct hlist_node *p;
293 int i;
294 int j = 0;
295
349eb8cf 296 for_each_mesh_entry(tbl, p, node, i) {
f698d856 297 if (sdata && node->mpath->sdata != sdata)
2a8ca29a 298 continue;
eb2b9311
LCC
299 if (j++ == idx) {
300 if (MPATH_EXPIRED(node->mpath)) {
301 spin_lock_bh(&node->mpath->state_lock);
302 if (MPATH_EXPIRED(node->mpath))
303 node->mpath->flags &= ~MESH_PATH_ACTIVE;
304 spin_unlock_bh(&node->mpath->state_lock);
305 }
306 return node->mpath;
307 }
2a8ca29a 308 }
eb2b9311
LCC
309
310 return NULL;
311}
312
313/**
314 * mesh_path_add - allocate and add a new path to the mesh path table
315 * @addr: destination address of the path (ETH_ALEN length)
f698d856 316 * @sdata: local subif
eb2b9311 317 *
af901ca1 318 * Returns: 0 on success
eb2b9311
LCC
319 *
320 * State: the initial state of the new path is set to 0
321 */
f698d856 322int mesh_path_add(u8 *dst, struct ieee80211_sub_if_data *sdata)
eb2b9311 323{
18889231
JC
324 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
325 struct ieee80211_local *local = sdata->local;
349eb8cf 326 struct mesh_table *tbl;
eb2b9311
LCC
327 struct mesh_path *mpath, *new_mpath;
328 struct mpath_node *node, *new_node;
329 struct hlist_head *bucket;
330 struct hlist_node *n;
331 int grow = 0;
332 int err = 0;
333 u32 hash_idx;
334
47846c9b 335 if (memcmp(dst, sdata->vif.addr, ETH_ALEN) == 0)
eb2b9311
LCC
336 /* never add ourselves as neighbours */
337 return -ENOTSUPP;
338
339 if (is_multicast_ether_addr(dst))
340 return -ENOTSUPP;
341
472dbc45 342 if (atomic_add_unless(&sdata->u.mesh.mpaths, 1, MESH_MAX_MPATHS) == 0)
eb2b9311
LCC
343 return -ENOSPC;
344
402d7752 345 err = -ENOMEM;
18889231 346 new_mpath = kzalloc(sizeof(struct mesh_path), GFP_ATOMIC);
402d7752
PE
347 if (!new_mpath)
348 goto err_path_alloc;
349
18889231 350 new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
402d7752
PE
351 if (!new_node)
352 goto err_node_alloc;
f84e71a9 353
9b84b808 354 read_lock_bh(&pathtbl_resize_lock);
eb2b9311 355 memcpy(new_mpath->dst, dst, ETH_ALEN);
f698d856 356 new_mpath->sdata = sdata;
eb2b9311
LCC
357 new_mpath->flags = 0;
358 skb_queue_head_init(&new_mpath->frame_queue);
eb2b9311
LCC
359 new_node->mpath = new_mpath;
360 new_mpath->timer.data = (unsigned long) new_mpath;
361 new_mpath->timer.function = mesh_path_timer;
362 new_mpath->exp_time = jiffies;
363 spin_lock_init(&new_mpath->state_lock);
364 init_timer(&new_mpath->timer);
365
349eb8cf 366 tbl = resize_dereference_mesh_paths();
eb2b9311 367
349eb8cf
JB
368 hash_idx = mesh_table_hash(dst, sdata, tbl);
369 bucket = &tbl->hash_buckets[hash_idx];
eb2b9311 370
349eb8cf 371 spin_lock_bh(&tbl->hashwlock[hash_idx]);
eb2b9311 372
402d7752 373 err = -EEXIST;
eb2b9311
LCC
374 hlist_for_each_entry(node, n, bucket, list) {
375 mpath = node->mpath;
f698d856 376 if (mpath->sdata == sdata && memcmp(dst, mpath->dst, ETH_ALEN) == 0)
402d7752 377 goto err_exists;
eb2b9311
LCC
378 }
379
380 hlist_add_head_rcu(&new_node->list, bucket);
349eb8cf
JB
381 if (atomic_inc_return(&tbl->entries) >=
382 tbl->mean_chain_len * (tbl->hash_mask + 1))
eb2b9311
LCC
383 grow = 1;
384
f5ea9120
JB
385 mesh_paths_generation++;
386
349eb8cf 387 spin_unlock_bh(&tbl->hashwlock[hash_idx]);
9b84b808 388 read_unlock_bh(&pathtbl_resize_lock);
402d7752 389 if (grow) {
18889231 390 set_bit(MESH_WORK_GROW_MPATH_TABLE, &ifmsh->wrkq_flags);
64592c8f 391 ieee80211_queue_work(&local->hw, &sdata->work);
eb2b9311 392 }
402d7752
PE
393 return 0;
394
395err_exists:
349eb8cf 396 spin_unlock_bh(&tbl->hashwlock[hash_idx]);
9b84b808 397 read_unlock_bh(&pathtbl_resize_lock);
402d7752
PE
398 kfree(new_node);
399err_node_alloc:
400 kfree(new_mpath);
401err_path_alloc:
472dbc45 402 atomic_dec(&sdata->u.mesh.mpaths);
eb2b9311
LCC
403 return err;
404}
405
1928ecab
JB
406static void mesh_table_free_rcu(struct rcu_head *rcu)
407{
408 struct mesh_table *tbl = container_of(rcu, struct mesh_table, rcu_head);
409
410 mesh_table_free(tbl, false);
411}
412
18889231
JC
413void mesh_mpath_table_grow(void)
414{
415 struct mesh_table *oldtbl, *newtbl;
416
9b84b808 417 write_lock_bh(&pathtbl_resize_lock);
349eb8cf
JB
418 oldtbl = resize_dereference_mesh_paths();
419 newtbl = mesh_table_alloc(oldtbl->size_order + 1);
1928ecab
JB
420 if (!newtbl)
421 goto out;
349eb8cf 422 if (mesh_table_grow(oldtbl, newtbl) < 0) {
a3e6b12c 423 __mesh_table_free(newtbl);
1928ecab 424 goto out;
18889231
JC
425 }
426 rcu_assign_pointer(mesh_paths, newtbl);
18889231 427
1928ecab
JB
428 call_rcu(&oldtbl->rcu_head, mesh_table_free_rcu);
429
430 out:
431 write_unlock_bh(&pathtbl_resize_lock);
18889231
JC
432}
433
434void mesh_mpp_table_grow(void)
435{
436 struct mesh_table *oldtbl, *newtbl;
437
9b84b808 438 write_lock_bh(&pathtbl_resize_lock);
349eb8cf
JB
439 oldtbl = resize_dereference_mpp_paths();
440 newtbl = mesh_table_alloc(oldtbl->size_order + 1);
1928ecab
JB
441 if (!newtbl)
442 goto out;
349eb8cf 443 if (mesh_table_grow(oldtbl, newtbl) < 0) {
a3e6b12c 444 __mesh_table_free(newtbl);
1928ecab 445 goto out;
18889231
JC
446 }
447 rcu_assign_pointer(mpp_paths, newtbl);
1928ecab 448 call_rcu(&oldtbl->rcu_head, mesh_table_free_rcu);
18889231 449
1928ecab
JB
450 out:
451 write_unlock_bh(&pathtbl_resize_lock);
18889231 452}
eb2b9311 453
79617dee
Y
454int mpp_path_add(u8 *dst, u8 *mpp, struct ieee80211_sub_if_data *sdata)
455{
18889231
JC
456 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
457 struct ieee80211_local *local = sdata->local;
349eb8cf 458 struct mesh_table *tbl;
79617dee
Y
459 struct mesh_path *mpath, *new_mpath;
460 struct mpath_node *node, *new_node;
461 struct hlist_head *bucket;
462 struct hlist_node *n;
463 int grow = 0;
464 int err = 0;
465 u32 hash_idx;
466
47846c9b 467 if (memcmp(dst, sdata->vif.addr, ETH_ALEN) == 0)
79617dee
Y
468 /* never add ourselves as neighbours */
469 return -ENOTSUPP;
470
471 if (is_multicast_ether_addr(dst))
472 return -ENOTSUPP;
473
474 err = -ENOMEM;
18889231 475 new_mpath = kzalloc(sizeof(struct mesh_path), GFP_ATOMIC);
79617dee
Y
476 if (!new_mpath)
477 goto err_path_alloc;
478
18889231 479 new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
79617dee
Y
480 if (!new_node)
481 goto err_node_alloc;
482
9b84b808 483 read_lock_bh(&pathtbl_resize_lock);
79617dee
Y
484 memcpy(new_mpath->dst, dst, ETH_ALEN);
485 memcpy(new_mpath->mpp, mpp, ETH_ALEN);
486 new_mpath->sdata = sdata;
487 new_mpath->flags = 0;
488 skb_queue_head_init(&new_mpath->frame_queue);
489 new_node->mpath = new_mpath;
490 new_mpath->exp_time = jiffies;
491 spin_lock_init(&new_mpath->state_lock);
492
349eb8cf 493 tbl = resize_dereference_mpp_paths();
79617dee 494
349eb8cf
JB
495 hash_idx = mesh_table_hash(dst, sdata, tbl);
496 bucket = &tbl->hash_buckets[hash_idx];
497
498 spin_lock_bh(&tbl->hashwlock[hash_idx]);
79617dee
Y
499
500 err = -EEXIST;
501 hlist_for_each_entry(node, n, bucket, list) {
502 mpath = node->mpath;
503 if (mpath->sdata == sdata && memcmp(dst, mpath->dst, ETH_ALEN) == 0)
504 goto err_exists;
505 }
506
507 hlist_add_head_rcu(&new_node->list, bucket);
349eb8cf
JB
508 if (atomic_inc_return(&tbl->entries) >=
509 tbl->mean_chain_len * (tbl->hash_mask + 1))
79617dee
Y
510 grow = 1;
511
349eb8cf 512 spin_unlock_bh(&tbl->hashwlock[hash_idx]);
9b84b808 513 read_unlock_bh(&pathtbl_resize_lock);
79617dee 514 if (grow) {
18889231 515 set_bit(MESH_WORK_GROW_MPP_TABLE, &ifmsh->wrkq_flags);
64592c8f 516 ieee80211_queue_work(&local->hw, &sdata->work);
79617dee
Y
517 }
518 return 0;
519
520err_exists:
349eb8cf 521 spin_unlock_bh(&tbl->hashwlock[hash_idx]);
9b84b808 522 read_unlock_bh(&pathtbl_resize_lock);
79617dee
Y
523 kfree(new_node);
524err_node_alloc:
525 kfree(new_mpath);
526err_path_alloc:
527 return err;
528}
529
530
eb2b9311
LCC
531/**
532 * mesh_plink_broken - deactivates paths and sends perr when a link breaks
533 *
534 * @sta: broken peer link
535 *
536 * This function must be called from the rate control algorithm if enough
537 * delivery errors suggest that a peer link is no longer usable.
538 */
539void mesh_plink_broken(struct sta_info *sta)
540{
349eb8cf 541 struct mesh_table *tbl;
15ff6365 542 static const u8 bcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
eb2b9311
LCC
543 struct mesh_path *mpath;
544 struct mpath_node *node;
545 struct hlist_node *p;
f698d856 546 struct ieee80211_sub_if_data *sdata = sta->sdata;
eb2b9311 547 int i;
25d49e4d 548 __le16 reason = cpu_to_le16(WLAN_REASON_MESH_PATH_DEST_UNREACHABLE);
eb2b9311
LCC
549
550 rcu_read_lock();
349eb8cf
JB
551 tbl = rcu_dereference(mesh_paths);
552 for_each_mesh_entry(tbl, p, node, i) {
eb2b9311
LCC
553 mpath = node->mpath;
554 spin_lock_bh(&mpath->state_lock);
349eb8cf 555 if (rcu_dereference(mpath->next_hop) == sta &&
eb2b9311
LCC
556 mpath->flags & MESH_PATH_ACTIVE &&
557 !(mpath->flags & MESH_PATH_FIXED)) {
558 mpath->flags &= ~MESH_PATH_ACTIVE;
d19b3bf6 559 ++mpath->sn;
eb2b9311 560 spin_unlock_bh(&mpath->state_lock);
45904f21
JC
561 mesh_path_error_tx(sdata->u.mesh.mshcfg.element_ttl,
562 mpath->dst, cpu_to_le32(mpath->sn),
25d49e4d 563 reason, bcast, sdata);
eb2b9311
LCC
564 } else
565 spin_unlock_bh(&mpath->state_lock);
566 }
567 rcu_read_unlock();
568}
569
570/**
571 * mesh_path_flush_by_nexthop - Deletes mesh paths if their next hop matches
572 *
573 * @sta - mesh peer to match
574 *
b4e08ea1
LCC
575 * RCU notes: this function is called when a mesh plink transitions from
576 * PLINK_ESTAB to any other state, since PLINK_ESTAB state is the only one that
577 * allows path creation. This will happen before the sta can be freed (because
d0709a65
JB
578 * sta_info_destroy() calls this) so any reader in a rcu read block will be
579 * protected against the plink disappearing.
eb2b9311
LCC
580 */
581void mesh_path_flush_by_nexthop(struct sta_info *sta)
582{
349eb8cf 583 struct mesh_table *tbl;
eb2b9311
LCC
584 struct mesh_path *mpath;
585 struct mpath_node *node;
586 struct hlist_node *p;
587 int i;
588
349eb8cf
JB
589 rcu_read_lock();
590 tbl = rcu_dereference(mesh_paths);
591 for_each_mesh_entry(tbl, p, node, i) {
eb2b9311 592 mpath = node->mpath;
349eb8cf 593 if (rcu_dereference(mpath->next_hop) == sta)
f698d856 594 mesh_path_del(mpath->dst, mpath->sdata);
eb2b9311 595 }
349eb8cf 596 rcu_read_unlock();
eb2b9311
LCC
597}
598
f698d856 599void mesh_path_flush(struct ieee80211_sub_if_data *sdata)
eb2b9311 600{
349eb8cf 601 struct mesh_table *tbl;
eb2b9311
LCC
602 struct mesh_path *mpath;
603 struct mpath_node *node;
604 struct hlist_node *p;
605 int i;
606
349eb8cf
JB
607 rcu_read_lock();
608 tbl = rcu_dereference(mesh_paths);
609 for_each_mesh_entry(tbl, p, node, i) {
eb2b9311 610 mpath = node->mpath;
f698d856
JBG
611 if (mpath->sdata == sdata)
612 mesh_path_del(mpath->dst, mpath->sdata);
eb2b9311 613 }
349eb8cf 614 rcu_read_unlock();
eb2b9311
LCC
615}
616
617static void mesh_path_node_reclaim(struct rcu_head *rp)
618{
619 struct mpath_node *node = container_of(rp, struct mpath_node, rcu);
f698d856 620 struct ieee80211_sub_if_data *sdata = node->mpath->sdata;
d0709a65 621
86d7f9f3
JC
622 if (node->mpath->timer.function)
623 del_timer_sync(&node->mpath->timer);
472dbc45 624 atomic_dec(&sdata->u.mesh.mpaths);
eb2b9311
LCC
625 kfree(node->mpath);
626 kfree(node);
627}
628
629/**
630 * mesh_path_del - delete a mesh path from the table
631 *
632 * @addr: dst address (ETH_ALEN length)
f698d856 633 * @sdata: local subif
eb2b9311 634 *
af901ca1 635 * Returns: 0 if successful
eb2b9311 636 */
f698d856 637int mesh_path_del(u8 *addr, struct ieee80211_sub_if_data *sdata)
eb2b9311 638{
349eb8cf 639 struct mesh_table *tbl;
eb2b9311
LCC
640 struct mesh_path *mpath;
641 struct mpath_node *node;
642 struct hlist_head *bucket;
643 struct hlist_node *n;
644 int hash_idx;
645 int err = 0;
646
9b84b808 647 read_lock_bh(&pathtbl_resize_lock);
349eb8cf
JB
648 tbl = resize_dereference_mesh_paths();
649 hash_idx = mesh_table_hash(addr, sdata, tbl);
650 bucket = &tbl->hash_buckets[hash_idx];
eb2b9311 651
349eb8cf 652 spin_lock_bh(&tbl->hashwlock[hash_idx]);
eb2b9311
LCC
653 hlist_for_each_entry(node, n, bucket, list) {
654 mpath = node->mpath;
f698d856 655 if (mpath->sdata == sdata &&
349eb8cf 656 memcmp(addr, mpath->dst, ETH_ALEN) == 0) {
5e34069c 657 spin_lock(&mpath->state_lock);
cfa22c71
LCC
658 mpath->flags |= MESH_PATH_RESOLVING;
659 hlist_del_rcu(&node->list);
660 call_rcu(&node->rcu, mesh_path_node_reclaim);
349eb8cf 661 atomic_dec(&tbl->entries);
5e34069c 662 spin_unlock(&mpath->state_lock);
eb2b9311
LCC
663 goto enddel;
664 }
665 }
666
667 err = -ENXIO;
668enddel:
f5ea9120 669 mesh_paths_generation++;
349eb8cf 670 spin_unlock_bh(&tbl->hashwlock[hash_idx]);
9b84b808 671 read_unlock_bh(&pathtbl_resize_lock);
eb2b9311
LCC
672 return err;
673}
674
675/**
676 * mesh_path_tx_pending - sends pending frames in a mesh path queue
677 *
678 * @mpath: mesh path to activate
679 *
680 * Locking: the state_lock of the mpath structure must NOT be held when calling
681 * this function.
682 */
683void mesh_path_tx_pending(struct mesh_path *mpath)
684{
249b405c
JC
685 if (mpath->flags & MESH_PATH_ACTIVE)
686 ieee80211_add_pending_skbs(mpath->sdata->local,
687 &mpath->frame_queue);
eb2b9311
LCC
688}
689
690/**
691 * mesh_path_discard_frame - discard a frame whose path could not be resolved
692 *
693 * @skb: frame to discard
f698d856 694 * @sdata: network subif the frame was to be sent through
eb2b9311 695 *
35946a57
JC
696 * If the frame was being forwarded from another MP, a PERR frame will be sent
697 * to the precursor. The precursor's address (i.e. the previous hop) was saved
698 * in addr1 of the frame-to-be-forwarded, and would only be overwritten once
699 * the destination is successfully resolved.
eb2b9311
LCC
700 *
701 * Locking: the function must me called within a rcu_read_lock region
702 */
f698d856
JBG
703void mesh_path_discard_frame(struct sk_buff *skb,
704 struct ieee80211_sub_if_data *sdata)
eb2b9311 705{
e32f85f7 706 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
eb2b9311 707 struct mesh_path *mpath;
d19b3bf6 708 u32 sn = 0;
25d49e4d 709 __le16 reason = cpu_to_le16(WLAN_REASON_MESH_PATH_NOFORWARD);
eb2b9311 710
47846c9b 711 if (memcmp(hdr->addr4, sdata->vif.addr, ETH_ALEN) != 0) {
eb2b9311
LCC
712 u8 *ra, *da;
713
e32f85f7 714 da = hdr->addr3;
35946a57 715 ra = hdr->addr1;
f698d856 716 mpath = mesh_path_lookup(da, sdata);
eb2b9311 717 if (mpath)
d19b3bf6 718 sn = ++mpath->sn;
45904f21 719 mesh_path_error_tx(sdata->u.mesh.mshcfg.element_ttl, skb->data,
25d49e4d 720 cpu_to_le32(sn), reason, ra, sdata);
eb2b9311
LCC
721 }
722
723 kfree_skb(skb);
472dbc45 724 sdata->u.mesh.mshstats.dropped_frames_no_route++;
eb2b9311
LCC
725}
726
727/**
728 * mesh_path_flush_pending - free the pending queue of a mesh path
729 *
730 * @mpath: mesh path whose queue has to be freed
731 *
25985edc 732 * Locking: the function must me called within a rcu_read_lock region
eb2b9311
LCC
733 */
734void mesh_path_flush_pending(struct mesh_path *mpath)
735{
eb2b9311
LCC
736 struct sk_buff *skb;
737
eb2b9311
LCC
738 while ((skb = skb_dequeue(&mpath->frame_queue)) &&
739 (mpath->flags & MESH_PATH_ACTIVE))
f698d856 740 mesh_path_discard_frame(skb, mpath->sdata);
eb2b9311
LCC
741}
742
743/**
744 * mesh_path_fix_nexthop - force a specific next hop for a mesh path
745 *
746 * @mpath: the mesh path to modify
747 * @next_hop: the next hop to force
748 *
749 * Locking: this function must be called holding mpath->state_lock
750 */
751void mesh_path_fix_nexthop(struct mesh_path *mpath, struct sta_info *next_hop)
752{
753 spin_lock_bh(&mpath->state_lock);
754 mesh_path_assign_nexthop(mpath, next_hop);
d19b3bf6 755 mpath->sn = 0xffff;
eb2b9311
LCC
756 mpath->metric = 0;
757 mpath->hop_count = 0;
758 mpath->exp_time = 0;
759 mpath->flags |= MESH_PATH_FIXED;
760 mesh_path_activate(mpath);
761 spin_unlock_bh(&mpath->state_lock);
762 mesh_path_tx_pending(mpath);
763}
764
765static void mesh_path_node_free(struct hlist_node *p, bool free_leafs)
766{
767 struct mesh_path *mpath;
768 struct mpath_node *node = hlist_entry(p, struct mpath_node, list);
769 mpath = node->mpath;
770 hlist_del_rcu(p);
d0df9eec 771 if (free_leafs) {
86d7f9f3
JC
772 if (mpath->timer.function)
773 del_timer_sync(&mpath->timer);
eb2b9311 774 kfree(mpath);
d0df9eec 775 }
eb2b9311
LCC
776 kfree(node);
777}
778
4caf86c6 779static int mesh_path_node_copy(struct hlist_node *p, struct mesh_table *newtbl)
eb2b9311
LCC
780{
781 struct mesh_path *mpath;
782 struct mpath_node *node, *new_node;
783 u32 hash_idx;
784
8566dc3f 785 new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
00242c40
PE
786 if (new_node == NULL)
787 return -ENOMEM;
788
eb2b9311
LCC
789 node = hlist_entry(p, struct mpath_node, list);
790 mpath = node->mpath;
eb2b9311 791 new_node->mpath = mpath;
f698d856 792 hash_idx = mesh_table_hash(mpath->dst, mpath->sdata, newtbl);
eb2b9311
LCC
793 hlist_add_head(&new_node->list,
794 &newtbl->hash_buckets[hash_idx]);
4caf86c6 795 return 0;
eb2b9311
LCC
796}
797
798int mesh_pathtbl_init(void)
799{
349eb8cf
JB
800 struct mesh_table *tbl_path, *tbl_mpp;
801
802 tbl_path = mesh_table_alloc(INIT_PATHS_SIZE_ORDER);
803 if (!tbl_path)
79617dee 804 return -ENOMEM;
349eb8cf
JB
805 tbl_path->free_node = &mesh_path_node_free;
806 tbl_path->copy_node = &mesh_path_node_copy;
807 tbl_path->mean_chain_len = MEAN_CHAIN_LEN;
79617dee 808
349eb8cf
JB
809 tbl_mpp = mesh_table_alloc(INIT_PATHS_SIZE_ORDER);
810 if (!tbl_mpp) {
811 mesh_table_free(tbl_path, true);
eb2b9311 812 return -ENOMEM;
79617dee 813 }
349eb8cf
JB
814 tbl_mpp->free_node = &mesh_path_node_free;
815 tbl_mpp->copy_node = &mesh_path_node_copy;
816 tbl_mpp->mean_chain_len = MEAN_CHAIN_LEN;
817
818 /* Need no locking since this is during init */
819 RCU_INIT_POINTER(mesh_paths, tbl_path);
820 RCU_INIT_POINTER(mpp_paths, tbl_mpp);
79617dee 821
eb2b9311
LCC
822 return 0;
823}
824
f698d856 825void mesh_path_expire(struct ieee80211_sub_if_data *sdata)
eb2b9311 826{
349eb8cf 827 struct mesh_table *tbl;
eb2b9311
LCC
828 struct mesh_path *mpath;
829 struct mpath_node *node;
830 struct hlist_node *p;
831 int i;
832
349eb8cf
JB
833 rcu_read_lock();
834 tbl = rcu_dereference(mesh_paths);
835 for_each_mesh_entry(tbl, p, node, i) {
f698d856 836 if (node->mpath->sdata != sdata)
eb2b9311
LCC
837 continue;
838 mpath = node->mpath;
839 spin_lock_bh(&mpath->state_lock);
840 if ((!(mpath->flags & MESH_PATH_RESOLVING)) &&
841 (!(mpath->flags & MESH_PATH_FIXED)) &&
349eb8cf 842 time_after(jiffies, mpath->exp_time + MESH_PATH_EXPIRE)) {
eb2b9311 843 spin_unlock_bh(&mpath->state_lock);
f698d856 844 mesh_path_del(mpath->dst, mpath->sdata);
eb2b9311
LCC
845 } else
846 spin_unlock_bh(&mpath->state_lock);
847 }
349eb8cf 848 rcu_read_unlock();
eb2b9311
LCC
849}
850
851void mesh_pathtbl_unregister(void)
852{
349eb8cf
JB
853 /* no need for locking during exit path */
854 mesh_table_free(rcu_dereference_raw(mesh_paths), true);
855 mesh_table_free(rcu_dereference_raw(mpp_paths), true);
eb2b9311 856}