blob: 7776ae5a8f15776d32699348d345c749b8d1b928 [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
20/* There will be initially 2^INIT_PATHS_SIZE_ORDER buckets */
21#define INIT_PATHS_SIZE_ORDER 2
22
23/* Keep the mean chain length below this constant */
24#define MEAN_CHAIN_LEN 2
25
26#define MPATH_EXPIRED(mpath) ((mpath->flags & MESH_PATH_ACTIVE) && \
27 time_after(jiffies, mpath->exp_time) && \
28 !(mpath->flags & MESH_PATH_FIXED))
29
30struct mpath_node {
31 struct hlist_node list;
32 struct rcu_head rcu;
33 /* This indirection allows two different tables to point to the same
34 * mesh_path structure, useful when resizing
35 */
36 struct mesh_path *mpath;
37};
38
39static struct mesh_table *mesh_paths;
YanBo79617de2008-09-22 13:30:32 +080040static struct mesh_table *mpp_paths; /* Store paths for MPP&MAP */
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +010041
Johannes Bergf5ea9122009-08-07 16:17:38 +020042int mesh_paths_generation;
Javier Cardona18889232009-08-10 12:15:52 -070043static void __mesh_table_free(struct mesh_table *tbl)
44{
45 kfree(tbl->hash_buckets);
46 kfree(tbl->hashwlock);
47 kfree(tbl);
48}
49
50void mesh_table_free(struct mesh_table *tbl, bool free_leafs)
51{
52 struct hlist_head *mesh_hash;
53 struct hlist_node *p, *q;
54 int i;
55
56 mesh_hash = tbl->hash_buckets;
57 for (i = 0; i <= tbl->hash_mask; i++) {
58 spin_lock(&tbl->hashwlock[i]);
59 hlist_for_each_safe(p, q, &mesh_hash[i]) {
60 tbl->free_node(p, free_leafs);
61 atomic_dec(&tbl->entries);
62 }
63 spin_unlock(&tbl->hashwlock[i]);
64 }
65 __mesh_table_free(tbl);
66}
67
cozybit Inca3e6b122011-04-13 11:10:28 -070068static int mesh_table_grow(struct mesh_table *oldtbl,
69 struct mesh_table *newtbl)
Javier Cardona18889232009-08-10 12:15:52 -070070{
Javier Cardona18889232009-08-10 12:15:52 -070071 struct hlist_head *oldhash;
72 struct hlist_node *p, *q;
73 int i;
74
cozybit Inca3e6b122011-04-13 11:10:28 -070075 if (atomic_read(&oldtbl->entries)
76 < oldtbl->mean_chain_len * (oldtbl->hash_mask + 1))
77 return -EAGAIN;
Javier Cardona18889232009-08-10 12:15:52 -070078
Javier Cardona18889232009-08-10 12:15:52 -070079
cozybit Inca3e6b122011-04-13 11:10:28 -070080 newtbl->free_node = oldtbl->free_node;
81 newtbl->mean_chain_len = oldtbl->mean_chain_len;
82 newtbl->copy_node = oldtbl->copy_node;
83 atomic_set(&newtbl->entries, atomic_read(&oldtbl->entries));
Javier Cardona18889232009-08-10 12:15:52 -070084
cozybit Inca3e6b122011-04-13 11:10:28 -070085 oldhash = oldtbl->hash_buckets;
86 for (i = 0; i <= oldtbl->hash_mask; i++)
Javier Cardona18889232009-08-10 12:15:52 -070087 hlist_for_each(p, &oldhash[i])
cozybit Inca3e6b122011-04-13 11:10:28 -070088 if (oldtbl->copy_node(p, newtbl) < 0)
Javier Cardona18889232009-08-10 12:15:52 -070089 goto errcopy;
90
cozybit Inca3e6b122011-04-13 11:10:28 -070091 return 0;
Javier Cardona18889232009-08-10 12:15:52 -070092
93errcopy:
94 for (i = 0; i <= newtbl->hash_mask; i++) {
95 hlist_for_each_safe(p, q, &newtbl->hash_buckets[i])
cozybit Inca3e6b122011-04-13 11:10:28 -070096 oldtbl->free_node(p, 0);
Javier Cardona18889232009-08-10 12:15:52 -070097 }
cozybit Inca3e6b122011-04-13 11:10:28 -070098 return -ENOMEM;
Javier Cardona18889232009-08-10 12:15:52 -070099}
100
Johannes Bergf5ea9122009-08-07 16:17:38 +0200101
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100102/* This lock will have the grow table function as writer and add / delete nodes
103 * as readers. When reading the table (i.e. doing lookups) we are well protected
104 * by RCU
105 */
106static DEFINE_RWLOCK(pathtbl_resize_lock);
107
108/**
109 *
110 * mesh_path_assign_nexthop - update mesh path next hop
111 *
112 * @mpath: mesh path to update
113 * @sta: next hop to assign
114 *
115 * Locking: mpath->state_lock must be held when calling this function
116 */
117void mesh_path_assign_nexthop(struct mesh_path *mpath, struct sta_info *sta)
118{
Javier Cardona10c836d2009-07-09 14:42:16 -0700119 struct sk_buff *skb;
120 struct ieee80211_hdr *hdr;
121 struct sk_buff_head tmpq;
122 unsigned long flags;
123
Johannes Bergd0709a62008-02-25 16:27:46 +0100124 rcu_assign_pointer(mpath->next_hop, sta);
Javier Cardona10c836d2009-07-09 14:42:16 -0700125
126 __skb_queue_head_init(&tmpq);
127
128 spin_lock_irqsave(&mpath->frame_queue.lock, flags);
129
130 while ((skb = __skb_dequeue(&mpath->frame_queue)) != NULL) {
131 hdr = (struct ieee80211_hdr *) skb->data;
132 memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN);
133 __skb_queue_tail(&tmpq, skb);
134 }
135
136 skb_queue_splice(&tmpq, &mpath->frame_queue);
137 spin_unlock_irqrestore(&mpath->frame_queue.lock, flags);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100138}
139
140
141/**
142 * mesh_path_lookup - look up a path in the mesh path table
143 * @dst: hardware address (ETH_ALEN length) of destination
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200144 * @sdata: local subif
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100145 *
146 * Returns: pointer to the mesh path structure, or NULL if not found
147 *
148 * Locking: must be called within a read rcu section.
149 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200150struct mesh_path *mesh_path_lookup(u8 *dst, struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100151{
152 struct mesh_path *mpath;
153 struct hlist_node *n;
154 struct hlist_head *bucket;
155 struct mesh_table *tbl;
156 struct mpath_node *node;
157
158 tbl = rcu_dereference(mesh_paths);
159
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200160 bucket = &tbl->hash_buckets[mesh_table_hash(dst, sdata, tbl)];
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100161 hlist_for_each_entry_rcu(node, n, bucket, list) {
162 mpath = node->mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200163 if (mpath->sdata == sdata &&
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100164 memcmp(dst, mpath->dst, ETH_ALEN) == 0) {
165 if (MPATH_EXPIRED(mpath)) {
166 spin_lock_bh(&mpath->state_lock);
167 if (MPATH_EXPIRED(mpath))
168 mpath->flags &= ~MESH_PATH_ACTIVE;
169 spin_unlock_bh(&mpath->state_lock);
170 }
171 return mpath;
172 }
173 }
174 return NULL;
175}
176
YanBo79617de2008-09-22 13:30:32 +0800177struct mesh_path *mpp_path_lookup(u8 *dst, struct ieee80211_sub_if_data *sdata)
178{
179 struct mesh_path *mpath;
180 struct hlist_node *n;
181 struct hlist_head *bucket;
182 struct mesh_table *tbl;
183 struct mpath_node *node;
184
185 tbl = rcu_dereference(mpp_paths);
186
187 bucket = &tbl->hash_buckets[mesh_table_hash(dst, sdata, tbl)];
188 hlist_for_each_entry_rcu(node, n, bucket, list) {
189 mpath = node->mpath;
190 if (mpath->sdata == sdata &&
191 memcmp(dst, mpath->dst, ETH_ALEN) == 0) {
192 if (MPATH_EXPIRED(mpath)) {
193 spin_lock_bh(&mpath->state_lock);
194 if (MPATH_EXPIRED(mpath))
195 mpath->flags &= ~MESH_PATH_ACTIVE;
196 spin_unlock_bh(&mpath->state_lock);
197 }
198 return mpath;
199 }
200 }
201 return NULL;
202}
203
204
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100205/**
206 * mesh_path_lookup_by_idx - look up a path in the mesh path table by its index
207 * @idx: index
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200208 * @sdata: local subif, or NULL for all entries
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100209 *
210 * Returns: pointer to the mesh path structure, or NULL if not found.
211 *
212 * Locking: must be called within a read rcu section.
213 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200214struct mesh_path *mesh_path_lookup_by_idx(int idx, struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100215{
216 struct mpath_node *node;
217 struct hlist_node *p;
218 int i;
219 int j = 0;
220
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800221 for_each_mesh_entry(mesh_paths, p, node, i) {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200222 if (sdata && node->mpath->sdata != sdata)
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800223 continue;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100224 if (j++ == idx) {
225 if (MPATH_EXPIRED(node->mpath)) {
226 spin_lock_bh(&node->mpath->state_lock);
227 if (MPATH_EXPIRED(node->mpath))
228 node->mpath->flags &= ~MESH_PATH_ACTIVE;
229 spin_unlock_bh(&node->mpath->state_lock);
230 }
231 return node->mpath;
232 }
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800233 }
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100234
235 return NULL;
236}
237
238/**
239 * mesh_path_add - allocate and add a new path to the mesh path table
240 * @addr: destination address of the path (ETH_ALEN length)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200241 * @sdata: local subif
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100242 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200243 * Returns: 0 on success
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100244 *
245 * State: the initial state of the new path is set to 0
246 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200247int mesh_path_add(u8 *dst, struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100248{
Javier Cardona18889232009-08-10 12:15:52 -0700249 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
250 struct ieee80211_local *local = sdata->local;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100251 struct mesh_path *mpath, *new_mpath;
252 struct mpath_node *node, *new_node;
253 struct hlist_head *bucket;
254 struct hlist_node *n;
255 int grow = 0;
256 int err = 0;
257 u32 hash_idx;
258
Johannes Berg47846c92009-11-25 17:46:19 +0100259 if (memcmp(dst, sdata->vif.addr, ETH_ALEN) == 0)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100260 /* never add ourselves as neighbours */
261 return -ENOTSUPP;
262
263 if (is_multicast_ether_addr(dst))
264 return -ENOTSUPP;
265
Johannes Berg472dbc42008-09-11 00:01:49 +0200266 if (atomic_add_unless(&sdata->u.mesh.mpaths, 1, MESH_MAX_MPATHS) == 0)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100267 return -ENOSPC;
268
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400269 err = -ENOMEM;
Javier Cardona18889232009-08-10 12:15:52 -0700270 new_mpath = kzalloc(sizeof(struct mesh_path), GFP_ATOMIC);
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400271 if (!new_mpath)
272 goto err_path_alloc;
273
Javier Cardona18889232009-08-10 12:15:52 -0700274 new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400275 if (!new_node)
276 goto err_node_alloc;
Pavel Emelyanovf84e71a2008-05-06 18:46:36 +0400277
278 read_lock(&pathtbl_resize_lock);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100279 memcpy(new_mpath->dst, dst, ETH_ALEN);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200280 new_mpath->sdata = sdata;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100281 new_mpath->flags = 0;
282 skb_queue_head_init(&new_mpath->frame_queue);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100283 new_node->mpath = new_mpath;
284 new_mpath->timer.data = (unsigned long) new_mpath;
285 new_mpath->timer.function = mesh_path_timer;
286 new_mpath->exp_time = jiffies;
287 spin_lock_init(&new_mpath->state_lock);
288 init_timer(&new_mpath->timer);
289
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200290 hash_idx = mesh_table_hash(dst, sdata, mesh_paths);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100291 bucket = &mesh_paths->hash_buckets[hash_idx];
292
293 spin_lock(&mesh_paths->hashwlock[hash_idx]);
294
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400295 err = -EEXIST;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100296 hlist_for_each_entry(node, n, bucket, list) {
297 mpath = node->mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200298 if (mpath->sdata == sdata && memcmp(dst, mpath->dst, ETH_ALEN) == 0)
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400299 goto err_exists;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100300 }
301
302 hlist_add_head_rcu(&new_node->list, bucket);
303 if (atomic_inc_return(&mesh_paths->entries) >=
304 mesh_paths->mean_chain_len * (mesh_paths->hash_mask + 1))
305 grow = 1;
306
Johannes Bergf5ea9122009-08-07 16:17:38 +0200307 mesh_paths_generation++;
308
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100309 spin_unlock(&mesh_paths->hashwlock[hash_idx]);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100310 read_unlock(&pathtbl_resize_lock);
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400311 if (grow) {
Javier Cardona18889232009-08-10 12:15:52 -0700312 set_bit(MESH_WORK_GROW_MPATH_TABLE, &ifmsh->wrkq_flags);
Johannes Berg64592c82010-06-10 10:21:31 +0200313 ieee80211_queue_work(&local->hw, &sdata->work);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100314 }
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400315 return 0;
316
317err_exists:
318 spin_unlock(&mesh_paths->hashwlock[hash_idx]);
319 read_unlock(&pathtbl_resize_lock);
320 kfree(new_node);
321err_node_alloc:
322 kfree(new_mpath);
323err_path_alloc:
Johannes Berg472dbc42008-09-11 00:01:49 +0200324 atomic_dec(&sdata->u.mesh.mpaths);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100325 return err;
326}
327
Javier Cardona18889232009-08-10 12:15:52 -0700328void mesh_mpath_table_grow(void)
329{
330 struct mesh_table *oldtbl, *newtbl;
331
cozybit Inca3e6b122011-04-13 11:10:28 -0700332 newtbl = mesh_table_alloc(mesh_paths->size_order + 1);
333 if (!newtbl)
334 return;
Javier Cardona18889232009-08-10 12:15:52 -0700335 write_lock(&pathtbl_resize_lock);
336 oldtbl = mesh_paths;
cozybit Inca3e6b122011-04-13 11:10:28 -0700337 if (mesh_table_grow(mesh_paths, newtbl) < 0) {
338 __mesh_table_free(newtbl);
Javier Cardona18889232009-08-10 12:15:52 -0700339 write_unlock(&pathtbl_resize_lock);
340 return;
341 }
342 rcu_assign_pointer(mesh_paths, newtbl);
343 write_unlock(&pathtbl_resize_lock);
344
345 synchronize_rcu();
346 mesh_table_free(oldtbl, false);
347}
348
349void mesh_mpp_table_grow(void)
350{
351 struct mesh_table *oldtbl, *newtbl;
352
cozybit Inca3e6b122011-04-13 11:10:28 -0700353 newtbl = mesh_table_alloc(mpp_paths->size_order + 1);
354 if (!newtbl)
355 return;
Javier Cardona18889232009-08-10 12:15:52 -0700356 write_lock(&pathtbl_resize_lock);
357 oldtbl = mpp_paths;
cozybit Inca3e6b122011-04-13 11:10:28 -0700358 if (mesh_table_grow(mpp_paths, newtbl) < 0) {
359 __mesh_table_free(newtbl);
Javier Cardona18889232009-08-10 12:15:52 -0700360 write_unlock(&pathtbl_resize_lock);
361 return;
362 }
363 rcu_assign_pointer(mpp_paths, newtbl);
364 write_unlock(&pathtbl_resize_lock);
365
366 synchronize_rcu();
367 mesh_table_free(oldtbl, false);
368}
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100369
YanBo79617de2008-09-22 13:30:32 +0800370int mpp_path_add(u8 *dst, u8 *mpp, struct ieee80211_sub_if_data *sdata)
371{
Javier Cardona18889232009-08-10 12:15:52 -0700372 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
373 struct ieee80211_local *local = sdata->local;
YanBo79617de2008-09-22 13:30:32 +0800374 struct mesh_path *mpath, *new_mpath;
375 struct mpath_node *node, *new_node;
376 struct hlist_head *bucket;
377 struct hlist_node *n;
378 int grow = 0;
379 int err = 0;
380 u32 hash_idx;
381
Johannes Berg47846c92009-11-25 17:46:19 +0100382 if (memcmp(dst, sdata->vif.addr, ETH_ALEN) == 0)
YanBo79617de2008-09-22 13:30:32 +0800383 /* never add ourselves as neighbours */
384 return -ENOTSUPP;
385
386 if (is_multicast_ether_addr(dst))
387 return -ENOTSUPP;
388
389 err = -ENOMEM;
Javier Cardona18889232009-08-10 12:15:52 -0700390 new_mpath = kzalloc(sizeof(struct mesh_path), GFP_ATOMIC);
YanBo79617de2008-09-22 13:30:32 +0800391 if (!new_mpath)
392 goto err_path_alloc;
393
Javier Cardona18889232009-08-10 12:15:52 -0700394 new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
YanBo79617de2008-09-22 13:30:32 +0800395 if (!new_node)
396 goto err_node_alloc;
397
398 read_lock(&pathtbl_resize_lock);
399 memcpy(new_mpath->dst, dst, ETH_ALEN);
400 memcpy(new_mpath->mpp, mpp, ETH_ALEN);
401 new_mpath->sdata = sdata;
402 new_mpath->flags = 0;
403 skb_queue_head_init(&new_mpath->frame_queue);
404 new_node->mpath = new_mpath;
405 new_mpath->exp_time = jiffies;
406 spin_lock_init(&new_mpath->state_lock);
407
408 hash_idx = mesh_table_hash(dst, sdata, mpp_paths);
409 bucket = &mpp_paths->hash_buckets[hash_idx];
410
411 spin_lock(&mpp_paths->hashwlock[hash_idx]);
412
413 err = -EEXIST;
414 hlist_for_each_entry(node, n, bucket, list) {
415 mpath = node->mpath;
416 if (mpath->sdata == sdata && memcmp(dst, mpath->dst, ETH_ALEN) == 0)
417 goto err_exists;
418 }
419
420 hlist_add_head_rcu(&new_node->list, bucket);
421 if (atomic_inc_return(&mpp_paths->entries) >=
422 mpp_paths->mean_chain_len * (mpp_paths->hash_mask + 1))
423 grow = 1;
424
425 spin_unlock(&mpp_paths->hashwlock[hash_idx]);
426 read_unlock(&pathtbl_resize_lock);
427 if (grow) {
Javier Cardona18889232009-08-10 12:15:52 -0700428 set_bit(MESH_WORK_GROW_MPP_TABLE, &ifmsh->wrkq_flags);
Johannes Berg64592c82010-06-10 10:21:31 +0200429 ieee80211_queue_work(&local->hw, &sdata->work);
YanBo79617de2008-09-22 13:30:32 +0800430 }
431 return 0;
432
433err_exists:
434 spin_unlock(&mpp_paths->hashwlock[hash_idx]);
435 read_unlock(&pathtbl_resize_lock);
436 kfree(new_node);
437err_node_alloc:
438 kfree(new_mpath);
439err_path_alloc:
440 return err;
441}
442
443
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100444/**
445 * mesh_plink_broken - deactivates paths and sends perr when a link breaks
446 *
447 * @sta: broken peer link
448 *
449 * This function must be called from the rate control algorithm if enough
450 * delivery errors suggest that a peer link is no longer usable.
451 */
452void mesh_plink_broken(struct sta_info *sta)
453{
Johannes Berg15ff6362009-11-17 13:34:04 +0100454 static const u8 bcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100455 struct mesh_path *mpath;
456 struct mpath_node *node;
457 struct hlist_node *p;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200458 struct ieee80211_sub_if_data *sdata = sta->sdata;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100459 int i;
460
461 rcu_read_lock();
462 for_each_mesh_entry(mesh_paths, p, node, i) {
463 mpath = node->mpath;
464 spin_lock_bh(&mpath->state_lock);
465 if (mpath->next_hop == sta &&
466 mpath->flags & MESH_PATH_ACTIVE &&
467 !(mpath->flags & MESH_PATH_FIXED)) {
468 mpath->flags &= ~MESH_PATH_ACTIVE;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000469 ++mpath->sn;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100470 spin_unlock_bh(&mpath->state_lock);
Javier Cardona45904f22010-12-03 09:20:40 +0100471 mesh_path_error_tx(sdata->u.mesh.mshcfg.element_ttl,
472 mpath->dst, cpu_to_le32(mpath->sn),
Rui Paulo9f130842009-11-19 15:49:10 +0000473 cpu_to_le16(PERR_RCODE_DEST_UNREACH),
Johannes Berg15ff6362009-11-17 13:34:04 +0100474 bcast, sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100475 } else
476 spin_unlock_bh(&mpath->state_lock);
477 }
478 rcu_read_unlock();
479}
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100480
481/**
482 * mesh_path_flush_by_nexthop - Deletes mesh paths if their next hop matches
483 *
484 * @sta - mesh peer to match
485 *
Luis Carlos Cobob4e08ea2008-02-29 15:46:08 -0800486 * RCU notes: this function is called when a mesh plink transitions from
487 * PLINK_ESTAB to any other state, since PLINK_ESTAB state is the only one that
488 * allows path creation. This will happen before the sta can be freed (because
Johannes Bergd0709a62008-02-25 16:27:46 +0100489 * sta_info_destroy() calls this) so any reader in a rcu read block will be
490 * protected against the plink disappearing.
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100491 */
492void mesh_path_flush_by_nexthop(struct sta_info *sta)
493{
494 struct mesh_path *mpath;
495 struct mpath_node *node;
496 struct hlist_node *p;
497 int i;
498
499 for_each_mesh_entry(mesh_paths, p, node, i) {
500 mpath = node->mpath;
501 if (mpath->next_hop == sta)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200502 mesh_path_del(mpath->dst, mpath->sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100503 }
504}
505
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200506void mesh_path_flush(struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100507{
508 struct mesh_path *mpath;
509 struct mpath_node *node;
510 struct hlist_node *p;
511 int i;
512
513 for_each_mesh_entry(mesh_paths, p, node, i) {
514 mpath = node->mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200515 if (mpath->sdata == sdata)
516 mesh_path_del(mpath->dst, mpath->sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100517 }
518}
519
520static void mesh_path_node_reclaim(struct rcu_head *rp)
521{
522 struct mpath_node *node = container_of(rp, struct mpath_node, rcu);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200523 struct ieee80211_sub_if_data *sdata = node->mpath->sdata;
Johannes Bergd0709a62008-02-25 16:27:46 +0100524
Luis Carlos Cobo89a1ad62008-02-29 14:49:37 -0800525 del_timer_sync(&node->mpath->timer);
Johannes Berg472dbc42008-09-11 00:01:49 +0200526 atomic_dec(&sdata->u.mesh.mpaths);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100527 kfree(node->mpath);
528 kfree(node);
529}
530
531/**
532 * mesh_path_del - delete a mesh path from the table
533 *
534 * @addr: dst address (ETH_ALEN length)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200535 * @sdata: local subif
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100536 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200537 * Returns: 0 if successful
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100538 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200539int mesh_path_del(u8 *addr, struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100540{
541 struct mesh_path *mpath;
542 struct mpath_node *node;
543 struct hlist_head *bucket;
544 struct hlist_node *n;
545 int hash_idx;
546 int err = 0;
547
548 read_lock(&pathtbl_resize_lock);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200549 hash_idx = mesh_table_hash(addr, sdata, mesh_paths);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100550 bucket = &mesh_paths->hash_buckets[hash_idx];
551
552 spin_lock(&mesh_paths->hashwlock[hash_idx]);
553 hlist_for_each_entry(node, n, bucket, list) {
554 mpath = node->mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200555 if (mpath->sdata == sdata &&
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100556 memcmp(addr, mpath->dst, ETH_ALEN) == 0) {
557 spin_lock_bh(&mpath->state_lock);
Luis Carlos Cobocfa22c72008-02-29 15:04:13 -0800558 mpath->flags |= MESH_PATH_RESOLVING;
559 hlist_del_rcu(&node->list);
560 call_rcu(&node->rcu, mesh_path_node_reclaim);
561 atomic_dec(&mesh_paths->entries);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100562 spin_unlock_bh(&mpath->state_lock);
563 goto enddel;
564 }
565 }
566
567 err = -ENXIO;
568enddel:
Johannes Bergf5ea9122009-08-07 16:17:38 +0200569 mesh_paths_generation++;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100570 spin_unlock(&mesh_paths->hashwlock[hash_idx]);
571 read_unlock(&pathtbl_resize_lock);
572 return err;
573}
574
575/**
576 * mesh_path_tx_pending - sends pending frames in a mesh path queue
577 *
578 * @mpath: mesh path to activate
579 *
580 * Locking: the state_lock of the mpath structure must NOT be held when calling
581 * this function.
582 */
583void mesh_path_tx_pending(struct mesh_path *mpath)
584{
Javier Cardona249b4052009-07-07 10:55:03 -0700585 if (mpath->flags & MESH_PATH_ACTIVE)
586 ieee80211_add_pending_skbs(mpath->sdata->local,
587 &mpath->frame_queue);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100588}
589
590/**
591 * mesh_path_discard_frame - discard a frame whose path could not be resolved
592 *
593 * @skb: frame to discard
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200594 * @sdata: network subif the frame was to be sent through
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100595 *
Javier Cardona35946a52009-07-13 17:00:10 -0700596 * If the frame was being forwarded from another MP, a PERR frame will be sent
597 * to the precursor. The precursor's address (i.e. the previous hop) was saved
598 * in addr1 of the frame-to-be-forwarded, and would only be overwritten once
599 * the destination is successfully resolved.
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100600 *
601 * Locking: the function must me called within a rcu_read_lock region
602 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200603void mesh_path_discard_frame(struct sk_buff *skb,
604 struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100605{
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +0200606 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100607 struct mesh_path *mpath;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000608 u32 sn = 0;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100609
Johannes Berg47846c92009-11-25 17:46:19 +0100610 if (memcmp(hdr->addr4, sdata->vif.addr, ETH_ALEN) != 0) {
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100611 u8 *ra, *da;
612
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +0200613 da = hdr->addr3;
Javier Cardona35946a52009-07-13 17:00:10 -0700614 ra = hdr->addr1;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200615 mpath = mesh_path_lookup(da, sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100616 if (mpath)
Rui Paulod19b3bf2009-11-09 23:46:55 +0000617 sn = ++mpath->sn;
Javier Cardona45904f22010-12-03 09:20:40 +0100618 mesh_path_error_tx(sdata->u.mesh.mshcfg.element_ttl, skb->data,
619 cpu_to_le32(sn),
Rui Paulo9f130842009-11-19 15:49:10 +0000620 cpu_to_le16(PERR_RCODE_NO_ROUTE), ra, sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100621 }
622
623 kfree_skb(skb);
Johannes Berg472dbc42008-09-11 00:01:49 +0200624 sdata->u.mesh.mshstats.dropped_frames_no_route++;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100625}
626
627/**
628 * mesh_path_flush_pending - free the pending queue of a mesh path
629 *
630 * @mpath: mesh path whose queue has to be freed
631 *
632 * Locking: the function must me called withing a rcu_read_lock region
633 */
634void mesh_path_flush_pending(struct mesh_path *mpath)
635{
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100636 struct sk_buff *skb;
637
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100638 while ((skb = skb_dequeue(&mpath->frame_queue)) &&
639 (mpath->flags & MESH_PATH_ACTIVE))
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200640 mesh_path_discard_frame(skb, mpath->sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100641}
642
643/**
644 * mesh_path_fix_nexthop - force a specific next hop for a mesh path
645 *
646 * @mpath: the mesh path to modify
647 * @next_hop: the next hop to force
648 *
649 * Locking: this function must be called holding mpath->state_lock
650 */
651void mesh_path_fix_nexthop(struct mesh_path *mpath, struct sta_info *next_hop)
652{
653 spin_lock_bh(&mpath->state_lock);
654 mesh_path_assign_nexthop(mpath, next_hop);
Rui Paulod19b3bf2009-11-09 23:46:55 +0000655 mpath->sn = 0xffff;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100656 mpath->metric = 0;
657 mpath->hop_count = 0;
658 mpath->exp_time = 0;
659 mpath->flags |= MESH_PATH_FIXED;
660 mesh_path_activate(mpath);
661 spin_unlock_bh(&mpath->state_lock);
662 mesh_path_tx_pending(mpath);
663}
664
665static void mesh_path_node_free(struct hlist_node *p, bool free_leafs)
666{
667 struct mesh_path *mpath;
668 struct mpath_node *node = hlist_entry(p, struct mpath_node, list);
669 mpath = node->mpath;
670 hlist_del_rcu(p);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100671 if (free_leafs)
672 kfree(mpath);
673 kfree(node);
674}
675
Pavel Emelyanov4caf86c2008-05-07 19:47:01 +0400676static int mesh_path_node_copy(struct hlist_node *p, struct mesh_table *newtbl)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100677{
678 struct mesh_path *mpath;
679 struct mpath_node *node, *new_node;
680 u32 hash_idx;
681
Pavel Emelyanov8566dc32008-05-07 19:51:51 +0400682 new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
Pavel Emelyanov00242c42008-05-07 19:48:14 +0400683 if (new_node == NULL)
684 return -ENOMEM;
685
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100686 node = hlist_entry(p, struct mpath_node, list);
687 mpath = node->mpath;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100688 new_node->mpath = mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200689 hash_idx = mesh_table_hash(mpath->dst, mpath->sdata, newtbl);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100690 hlist_add_head(&new_node->list,
691 &newtbl->hash_buckets[hash_idx]);
Pavel Emelyanov4caf86c2008-05-07 19:47:01 +0400692 return 0;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100693}
694
695int mesh_pathtbl_init(void)
696{
697 mesh_paths = mesh_table_alloc(INIT_PATHS_SIZE_ORDER);
YanBo79617de2008-09-22 13:30:32 +0800698 if (!mesh_paths)
699 return -ENOMEM;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100700 mesh_paths->free_node = &mesh_path_node_free;
701 mesh_paths->copy_node = &mesh_path_node_copy;
702 mesh_paths->mean_chain_len = MEAN_CHAIN_LEN;
YanBo79617de2008-09-22 13:30:32 +0800703
704 mpp_paths = mesh_table_alloc(INIT_PATHS_SIZE_ORDER);
705 if (!mpp_paths) {
706 mesh_table_free(mesh_paths, true);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100707 return -ENOMEM;
YanBo79617de2008-09-22 13:30:32 +0800708 }
709 mpp_paths->free_node = &mesh_path_node_free;
710 mpp_paths->copy_node = &mesh_path_node_copy;
711 mpp_paths->mean_chain_len = MEAN_CHAIN_LEN;
712
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100713 return 0;
714}
715
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200716void mesh_path_expire(struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100717{
718 struct mesh_path *mpath;
719 struct mpath_node *node;
720 struct hlist_node *p;
721 int i;
722
723 read_lock(&pathtbl_resize_lock);
724 for_each_mesh_entry(mesh_paths, p, node, i) {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200725 if (node->mpath->sdata != sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100726 continue;
727 mpath = node->mpath;
728 spin_lock_bh(&mpath->state_lock);
729 if ((!(mpath->flags & MESH_PATH_RESOLVING)) &&
730 (!(mpath->flags & MESH_PATH_FIXED)) &&
731 time_after(jiffies,
732 mpath->exp_time + MESH_PATH_EXPIRE)) {
733 spin_unlock_bh(&mpath->state_lock);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200734 mesh_path_del(mpath->dst, mpath->sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100735 } else
736 spin_unlock_bh(&mpath->state_lock);
737 }
738 read_unlock(&pathtbl_resize_lock);
739}
740
741void mesh_pathtbl_unregister(void)
742{
743 mesh_table_free(mesh_paths, true);
YanBo79617de2008-09-22 13:30:32 +0800744 mesh_table_free(mpp_paths, true);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100745}