blob: be1361b5f7ada32953a080a573bb6574d0fe9ac9 [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>
Javier Cardona4777be42011-09-07 17:49:52 -070017#include "wme.h"
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +010018#include "ieee80211_i.h"
19#include "mesh.h"
20
Javier Cardona76468872011-08-09 16:45:04 -070021#ifdef CONFIG_MAC80211_VERBOSE_MPATH_DEBUG
22#define mpath_dbg(fmt, args...) printk(KERN_DEBUG fmt, ##args)
23#else
24#define mpath_dbg(fmt, args...) do { (void)(0); } while (0)
25#endif
26
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +010027/* There will be initially 2^INIT_PATHS_SIZE_ORDER buckets */
28#define INIT_PATHS_SIZE_ORDER 2
29
30/* Keep the mean chain length below this constant */
31#define MEAN_CHAIN_LEN 2
32
33#define MPATH_EXPIRED(mpath) ((mpath->flags & MESH_PATH_ACTIVE) && \
34 time_after(jiffies, mpath->exp_time) && \
35 !(mpath->flags & MESH_PATH_FIXED))
36
37struct mpath_node {
38 struct hlist_node list;
39 struct rcu_head rcu;
40 /* This indirection allows two different tables to point to the same
41 * mesh_path structure, useful when resizing
42 */
43 struct mesh_path *mpath;
44};
45
Johannes Berg349eb8c2011-05-14 11:56:16 +020046static struct mesh_table __rcu *mesh_paths;
47static struct mesh_table __rcu *mpp_paths; /* Store paths for MPP&MAP */
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +010048
Johannes Bergf5ea9122009-08-07 16:17:38 +020049int mesh_paths_generation;
Johannes Berg6b86bd62011-05-12 13:38:50 +020050
51/* This lock will have the grow table function as writer and add / delete nodes
Javier Cardona239289e2011-08-29 13:23:09 -070052 * as readers. RCU provides sufficient protection only when reading the table
53 * (i.e. doing lookups). Adding or adding or removing nodes requires we take
54 * the read lock or we risk operating on an old table. The write lock is only
55 * needed when modifying the number of buckets a table.
Johannes Berg6b86bd62011-05-12 13:38:50 +020056 */
57static DEFINE_RWLOCK(pathtbl_resize_lock);
58
59
Johannes Berg349eb8c2011-05-14 11:56:16 +020060static inline struct mesh_table *resize_dereference_mesh_paths(void)
61{
62 return rcu_dereference_protected(mesh_paths,
63 lockdep_is_held(&pathtbl_resize_lock));
64}
65
66static inline struct mesh_table *resize_dereference_mpp_paths(void)
67{
68 return rcu_dereference_protected(mpp_paths,
69 lockdep_is_held(&pathtbl_resize_lock));
70}
71
72/*
73 * CAREFUL -- "tbl" must not be an expression,
74 * in particular not an rcu_dereference(), since
75 * it's used twice. So it is illegal to do
76 * for_each_mesh_entry(rcu_dereference(...), ...)
77 */
78#define for_each_mesh_entry(tbl, p, node, i) \
79 for (i = 0; i <= tbl->hash_mask; i++) \
80 hlist_for_each_entry_rcu(node, p, &tbl->hash_buckets[i], list)
81
82
Johannes Berg6b86bd62011-05-12 13:38:50 +020083static struct mesh_table *mesh_table_alloc(int size_order)
84{
85 int i;
86 struct mesh_table *newtbl;
87
Javier Cardonad676ff42011-05-17 16:13:34 -070088 newtbl = kmalloc(sizeof(struct mesh_table), GFP_ATOMIC);
Johannes Berg6b86bd62011-05-12 13:38:50 +020089 if (!newtbl)
90 return NULL;
91
92 newtbl->hash_buckets = kzalloc(sizeof(struct hlist_head) *
Javier Cardonad676ff42011-05-17 16:13:34 -070093 (1 << size_order), GFP_ATOMIC);
Johannes Berg6b86bd62011-05-12 13:38:50 +020094
95 if (!newtbl->hash_buckets) {
96 kfree(newtbl);
97 return NULL;
98 }
99
100 newtbl->hashwlock = kmalloc(sizeof(spinlock_t) *
Javier Cardonad676ff42011-05-17 16:13:34 -0700101 (1 << size_order), GFP_ATOMIC);
Johannes Berg6b86bd62011-05-12 13:38:50 +0200102 if (!newtbl->hashwlock) {
103 kfree(newtbl->hash_buckets);
104 kfree(newtbl);
105 return NULL;
106 }
107
108 newtbl->size_order = size_order;
109 newtbl->hash_mask = (1 << size_order) - 1;
110 atomic_set(&newtbl->entries, 0);
111 get_random_bytes(&newtbl->hash_rnd,
112 sizeof(newtbl->hash_rnd));
113 for (i = 0; i <= newtbl->hash_mask; i++)
114 spin_lock_init(&newtbl->hashwlock[i]);
Javier Cardona5ee68e52011-08-09 16:45:08 -0700115 spin_lock_init(&newtbl->gates_lock);
Johannes Berg6b86bd62011-05-12 13:38:50 +0200116
117 return newtbl;
118}
119
Javier Cardona18889232009-08-10 12:15:52 -0700120static void __mesh_table_free(struct mesh_table *tbl)
121{
122 kfree(tbl->hash_buckets);
123 kfree(tbl->hashwlock);
124 kfree(tbl);
125}
126
Johannes Berg6b86bd62011-05-12 13:38:50 +0200127static void mesh_table_free(struct mesh_table *tbl, bool free_leafs)
Javier Cardona18889232009-08-10 12:15:52 -0700128{
129 struct hlist_head *mesh_hash;
130 struct hlist_node *p, *q;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700131 struct mpath_node *gate;
Javier Cardona18889232009-08-10 12:15:52 -0700132 int i;
133
134 mesh_hash = tbl->hash_buckets;
135 for (i = 0; i <= tbl->hash_mask; i++) {
Javier Cardona9b84b8082011-05-03 16:57:16 -0700136 spin_lock_bh(&tbl->hashwlock[i]);
Javier Cardona18889232009-08-10 12:15:52 -0700137 hlist_for_each_safe(p, q, &mesh_hash[i]) {
138 tbl->free_node(p, free_leafs);
139 atomic_dec(&tbl->entries);
140 }
Javier Cardona9b84b8082011-05-03 16:57:16 -0700141 spin_unlock_bh(&tbl->hashwlock[i]);
Javier Cardona18889232009-08-10 12:15:52 -0700142 }
Javier Cardona5ee68e52011-08-09 16:45:08 -0700143 if (free_leafs) {
144 spin_lock_bh(&tbl->gates_lock);
145 hlist_for_each_entry_safe(gate, p, q,
146 tbl->known_gates, list) {
147 hlist_del(&gate->list);
148 kfree(gate);
149 }
150 kfree(tbl->known_gates);
151 spin_unlock_bh(&tbl->gates_lock);
152 }
153
Javier Cardona18889232009-08-10 12:15:52 -0700154 __mesh_table_free(tbl);
155}
156
cozybit Inca3e6b122011-04-13 11:10:28 -0700157static int mesh_table_grow(struct mesh_table *oldtbl,
Johannes Berg6b86bd62011-05-12 13:38:50 +0200158 struct mesh_table *newtbl)
Javier Cardona18889232009-08-10 12:15:52 -0700159{
Javier Cardona18889232009-08-10 12:15:52 -0700160 struct hlist_head *oldhash;
161 struct hlist_node *p, *q;
162 int i;
163
cozybit Inca3e6b122011-04-13 11:10:28 -0700164 if (atomic_read(&oldtbl->entries)
165 < oldtbl->mean_chain_len * (oldtbl->hash_mask + 1))
166 return -EAGAIN;
Javier Cardona18889232009-08-10 12:15:52 -0700167
cozybit Inca3e6b122011-04-13 11:10:28 -0700168 newtbl->free_node = oldtbl->free_node;
169 newtbl->mean_chain_len = oldtbl->mean_chain_len;
170 newtbl->copy_node = oldtbl->copy_node;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700171 newtbl->known_gates = oldtbl->known_gates;
cozybit Inca3e6b122011-04-13 11:10:28 -0700172 atomic_set(&newtbl->entries, atomic_read(&oldtbl->entries));
Javier Cardona18889232009-08-10 12:15:52 -0700173
cozybit Inca3e6b122011-04-13 11:10:28 -0700174 oldhash = oldtbl->hash_buckets;
175 for (i = 0; i <= oldtbl->hash_mask; i++)
Javier Cardona18889232009-08-10 12:15:52 -0700176 hlist_for_each(p, &oldhash[i])
cozybit Inca3e6b122011-04-13 11:10:28 -0700177 if (oldtbl->copy_node(p, newtbl) < 0)
Javier Cardona18889232009-08-10 12:15:52 -0700178 goto errcopy;
179
cozybit Inca3e6b122011-04-13 11:10:28 -0700180 return 0;
Javier Cardona18889232009-08-10 12:15:52 -0700181
182errcopy:
183 for (i = 0; i <= newtbl->hash_mask; i++) {
184 hlist_for_each_safe(p, q, &newtbl->hash_buckets[i])
cozybit Inca3e6b122011-04-13 11:10:28 -0700185 oldtbl->free_node(p, 0);
Javier Cardona18889232009-08-10 12:15:52 -0700186 }
cozybit Inca3e6b122011-04-13 11:10:28 -0700187 return -ENOMEM;
Javier Cardona18889232009-08-10 12:15:52 -0700188}
189
Johannes Berg6b86bd62011-05-12 13:38:50 +0200190static u32 mesh_table_hash(u8 *addr, struct ieee80211_sub_if_data *sdata,
191 struct mesh_table *tbl)
192{
193 /* Use last four bytes of hw addr and interface index as hash index */
194 return jhash_2words(*(u32 *)(addr+2), sdata->dev->ifindex, tbl->hash_rnd)
195 & tbl->hash_mask;
196}
Johannes Bergf5ea9122009-08-07 16:17:38 +0200197
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100198
199/**
200 *
201 * mesh_path_assign_nexthop - update mesh path next hop
202 *
203 * @mpath: mesh path to update
204 * @sta: next hop to assign
205 *
206 * Locking: mpath->state_lock must be held when calling this function
207 */
208void mesh_path_assign_nexthop(struct mesh_path *mpath, struct sta_info *sta)
209{
Javier Cardona10c836d2009-07-09 14:42:16 -0700210 struct sk_buff *skb;
211 struct ieee80211_hdr *hdr;
212 struct sk_buff_head tmpq;
213 unsigned long flags;
214
Johannes Bergd0709a62008-02-25 16:27:46 +0100215 rcu_assign_pointer(mpath->next_hop, sta);
Javier Cardona10c836d2009-07-09 14:42:16 -0700216
217 __skb_queue_head_init(&tmpq);
218
219 spin_lock_irqsave(&mpath->frame_queue.lock, flags);
220
221 while ((skb = __skb_dequeue(&mpath->frame_queue)) != NULL) {
222 hdr = (struct ieee80211_hdr *) skb->data;
223 memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN);
Thomas Pedersen7e3c8862011-11-24 17:15:21 -0800224 memcpy(hdr->addr2, mpath->sdata->vif.addr, ETH_ALEN);
Javier Cardona10c836d2009-07-09 14:42:16 -0700225 __skb_queue_tail(&tmpq, skb);
226 }
227
228 skb_queue_splice(&tmpq, &mpath->frame_queue);
229 spin_unlock_irqrestore(&mpath->frame_queue.lock, flags);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100230}
231
Javier Cardona5ee68e52011-08-09 16:45:08 -0700232static void prepare_for_gate(struct sk_buff *skb, char *dst_addr,
233 struct mesh_path *gate_mpath)
234{
235 struct ieee80211_hdr *hdr;
236 struct ieee80211s_hdr *mshdr;
237 int mesh_hdrlen, hdrlen;
238 char *next_hop;
239
240 hdr = (struct ieee80211_hdr *) skb->data;
241 hdrlen = ieee80211_hdrlen(hdr->frame_control);
242 mshdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
243
244 if (!(mshdr->flags & MESH_FLAGS_AE)) {
245 /* size of the fixed part of the mesh header */
246 mesh_hdrlen = 6;
247
248 /* make room for the two extended addresses */
249 skb_push(skb, 2 * ETH_ALEN);
250 memmove(skb->data, hdr, hdrlen + mesh_hdrlen);
251
252 hdr = (struct ieee80211_hdr *) skb->data;
253
254 /* we preserve the previous mesh header and only add
255 * the new addreses */
256 mshdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
257 mshdr->flags = MESH_FLAGS_AE_A5_A6;
258 memcpy(mshdr->eaddr1, hdr->addr3, ETH_ALEN);
259 memcpy(mshdr->eaddr2, hdr->addr4, ETH_ALEN);
260 }
261
262 /* update next hop */
263 hdr = (struct ieee80211_hdr *) skb->data;
264 rcu_read_lock();
265 next_hop = rcu_dereference(gate_mpath->next_hop)->sta.addr;
266 memcpy(hdr->addr1, next_hop, ETH_ALEN);
267 rcu_read_unlock();
Thomas Pedersen7e3c8862011-11-24 17:15:21 -0800268 memcpy(hdr->addr2, gate_mpath->sdata->vif.addr, ETH_ALEN);
Javier Cardona5ee68e52011-08-09 16:45:08 -0700269 memcpy(hdr->addr3, dst_addr, ETH_ALEN);
270}
271
272/**
273 *
274 * mesh_path_move_to_queue - Move or copy frames from one mpath queue to another
275 *
276 * This function is used to transfer or copy frames from an unresolved mpath to
277 * a gate mpath. The function also adds the Address Extension field and
278 * updates the next hop.
279 *
280 * If a frame already has an Address Extension field, only the next hop and
281 * destination addresses are updated.
282 *
283 * The gate mpath must be an active mpath with a valid mpath->next_hop.
284 *
285 * @mpath: An active mpath the frames will be sent to (i.e. the gate)
286 * @from_mpath: The failed mpath
287 * @copy: When true, copy all the frames to the new mpath queue. When false,
288 * move them.
289 */
290static void mesh_path_move_to_queue(struct mesh_path *gate_mpath,
291 struct mesh_path *from_mpath,
292 bool copy)
293{
Thomas Pedersenc61336612011-08-25 10:36:14 -0700294 struct sk_buff *skb, *cp_skb = NULL;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700295 struct sk_buff_head gateq, failq;
296 unsigned long flags;
297 int num_skbs;
298
299 BUG_ON(gate_mpath == from_mpath);
300 BUG_ON(!gate_mpath->next_hop);
301
302 __skb_queue_head_init(&gateq);
303 __skb_queue_head_init(&failq);
304
305 spin_lock_irqsave(&from_mpath->frame_queue.lock, flags);
306 skb_queue_splice_init(&from_mpath->frame_queue, &failq);
307 spin_unlock_irqrestore(&from_mpath->frame_queue.lock, flags);
308
309 num_skbs = skb_queue_len(&failq);
310
311 while (num_skbs--) {
312 skb = __skb_dequeue(&failq);
John W. Linville817a53d2011-08-24 15:12:41 -0400313 if (copy) {
Javier Cardona5ee68e52011-08-09 16:45:08 -0700314 cp_skb = skb_copy(skb, GFP_ATOMIC);
John W. Linville817a53d2011-08-24 15:12:41 -0400315 if (cp_skb)
316 __skb_queue_tail(&failq, cp_skb);
317 }
Javier Cardona5ee68e52011-08-09 16:45:08 -0700318
319 prepare_for_gate(skb, gate_mpath->dst, gate_mpath);
320 __skb_queue_tail(&gateq, skb);
Javier Cardona5ee68e52011-08-09 16:45:08 -0700321 }
322
323 spin_lock_irqsave(&gate_mpath->frame_queue.lock, flags);
324 skb_queue_splice(&gateq, &gate_mpath->frame_queue);
325 mpath_dbg("Mpath queue for gate %pM has %d frames\n",
326 gate_mpath->dst,
327 skb_queue_len(&gate_mpath->frame_queue));
328 spin_unlock_irqrestore(&gate_mpath->frame_queue.lock, flags);
329
330 if (!copy)
331 return;
332
333 spin_lock_irqsave(&from_mpath->frame_queue.lock, flags);
334 skb_queue_splice(&failq, &from_mpath->frame_queue);
335 spin_unlock_irqrestore(&from_mpath->frame_queue.lock, flags);
336}
337
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100338
Luis R. Rodriguez5ad20dd2012-02-07 21:09:25 -0800339static struct mesh_path *mpath_lookup(struct mesh_table *tbl, u8 *dst,
Javier Cardona239289e2011-08-29 13:23:09 -0700340 struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100341{
342 struct mesh_path *mpath;
343 struct hlist_node *n;
344 struct hlist_head *bucket;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100345 struct mpath_node *node;
346
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200347 bucket = &tbl->hash_buckets[mesh_table_hash(dst, sdata, tbl)];
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100348 hlist_for_each_entry_rcu(node, n, bucket, list) {
349 mpath = node->mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200350 if (mpath->sdata == sdata &&
Felix Fietkau888d04d2012-03-01 15:22:09 +0100351 compare_ether_addr(dst, mpath->dst) == 0) {
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100352 if (MPATH_EXPIRED(mpath)) {
353 spin_lock_bh(&mpath->state_lock);
Javier Cardonaad99d142011-08-29 13:23:06 -0700354 mpath->flags &= ~MESH_PATH_ACTIVE;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100355 spin_unlock_bh(&mpath->state_lock);
356 }
357 return mpath;
358 }
359 }
360 return NULL;
361}
362
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100363/**
364 * mesh_path_lookup - look up a path in the mesh path table
365 * @dst: hardware address (ETH_ALEN length) of destination
366 * @sdata: local subif
367 *
368 * Returns: pointer to the mesh path structure, or NULL if not found
369 *
370 * Locking: must be called within a read rcu section.
371 */
372struct mesh_path *mesh_path_lookup(u8 *dst, struct ieee80211_sub_if_data *sdata)
373{
Luis R. Rodriguez5ad20dd2012-02-07 21:09:25 -0800374 return mpath_lookup(rcu_dereference(mesh_paths), dst, sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100375}
376
YanBo79617de2008-09-22 13:30:32 +0800377struct mesh_path *mpp_path_lookup(u8 *dst, struct ieee80211_sub_if_data *sdata)
378{
Luis R. Rodriguez5ad20dd2012-02-07 21:09:25 -0800379 return mpath_lookup(rcu_dereference(mpp_paths), dst, sdata);
YanBo79617de2008-09-22 13:30:32 +0800380}
381
382
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100383/**
384 * mesh_path_lookup_by_idx - look up a path in the mesh path table by its index
385 * @idx: index
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200386 * @sdata: local subif, or NULL for all entries
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100387 *
388 * Returns: pointer to the mesh path structure, or NULL if not found.
389 *
390 * Locking: must be called within a read rcu section.
391 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200392struct mesh_path *mesh_path_lookup_by_idx(int idx, struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100393{
Johannes Berg349eb8c2011-05-14 11:56:16 +0200394 struct mesh_table *tbl = rcu_dereference(mesh_paths);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100395 struct mpath_node *node;
396 struct hlist_node *p;
397 int i;
398 int j = 0;
399
Johannes Berg349eb8c2011-05-14 11:56:16 +0200400 for_each_mesh_entry(tbl, p, node, i) {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200401 if (sdata && node->mpath->sdata != sdata)
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800402 continue;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100403 if (j++ == idx) {
404 if (MPATH_EXPIRED(node->mpath)) {
405 spin_lock_bh(&node->mpath->state_lock);
Javier Cardonaad99d142011-08-29 13:23:06 -0700406 node->mpath->flags &= ~MESH_PATH_ACTIVE;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100407 spin_unlock_bh(&node->mpath->state_lock);
408 }
409 return node->mpath;
410 }
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800411 }
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100412
413 return NULL;
414}
415
Javier Cardona5ee68e52011-08-09 16:45:08 -0700416static void mesh_gate_node_reclaim(struct rcu_head *rp)
417{
418 struct mpath_node *node = container_of(rp, struct mpath_node, rcu);
419 kfree(node);
420}
421
422/**
Johannes Berg30be52e2011-11-21 11:23:50 +0100423 * mesh_path_add_gate - add the given mpath to a mesh gate to our path table
424 * @mpath: gate path to add to table
Javier Cardona5ee68e52011-08-09 16:45:08 -0700425 */
Johannes Berg30be52e2011-11-21 11:23:50 +0100426int mesh_path_add_gate(struct mesh_path *mpath)
Javier Cardona5ee68e52011-08-09 16:45:08 -0700427{
Johannes Berg30be52e2011-11-21 11:23:50 +0100428 struct mesh_table *tbl;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700429 struct mpath_node *gate, *new_gate;
430 struct hlist_node *n;
431 int err;
432
433 rcu_read_lock();
Johannes Berg30be52e2011-11-21 11:23:50 +0100434 tbl = rcu_dereference(mesh_paths);
Javier Cardona5ee68e52011-08-09 16:45:08 -0700435
436 hlist_for_each_entry_rcu(gate, n, tbl->known_gates, list)
437 if (gate->mpath == mpath) {
438 err = -EEXIST;
439 goto err_rcu;
440 }
441
442 new_gate = kzalloc(sizeof(struct mpath_node), GFP_ATOMIC);
443 if (!new_gate) {
444 err = -ENOMEM;
445 goto err_rcu;
446 }
447
448 mpath->is_gate = true;
449 mpath->sdata->u.mesh.num_gates++;
450 new_gate->mpath = mpath;
451 spin_lock_bh(&tbl->gates_lock);
452 hlist_add_head_rcu(&new_gate->list, tbl->known_gates);
453 spin_unlock_bh(&tbl->gates_lock);
454 rcu_read_unlock();
455 mpath_dbg("Mesh path (%s): Recorded new gate: %pM. %d known gates\n",
456 mpath->sdata->name, mpath->dst,
457 mpath->sdata->u.mesh.num_gates);
458 return 0;
459err_rcu:
460 rcu_read_unlock();
461 return err;
462}
463
464/**
465 * mesh_gate_del - remove a mesh gate from the list of known gates
466 * @tbl: table which holds our list of known gates
467 * @mpath: gate mpath
468 *
469 * Returns: 0 on success
470 *
471 * Locking: must be called inside rcu_read_lock() section
472 */
473static int mesh_gate_del(struct mesh_table *tbl, struct mesh_path *mpath)
474{
475 struct mpath_node *gate;
476 struct hlist_node *p, *q;
477
Javier Cardona5ee68e52011-08-09 16:45:08 -0700478 hlist_for_each_entry_safe(gate, p, q, tbl->known_gates, list)
479 if (gate->mpath == mpath) {
480 spin_lock_bh(&tbl->gates_lock);
481 hlist_del_rcu(&gate->list);
482 call_rcu(&gate->rcu, mesh_gate_node_reclaim);
483 spin_unlock_bh(&tbl->gates_lock);
484 mpath->sdata->u.mesh.num_gates--;
485 mpath->is_gate = false;
486 mpath_dbg("Mesh path (%s): Deleted gate: %pM. "
487 "%d known gates\n", mpath->sdata->name,
488 mpath->dst, mpath->sdata->u.mesh.num_gates);
489 break;
490 }
491
492 return 0;
493}
494
495/**
Javier Cardona5ee68e52011-08-09 16:45:08 -0700496 * mesh_gate_num - number of gates known to this interface
497 * @sdata: subif data
498 */
499int mesh_gate_num(struct ieee80211_sub_if_data *sdata)
500{
501 return sdata->u.mesh.num_gates;
502}
503
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100504/**
505 * mesh_path_add - allocate and add a new path to the mesh path table
506 * @addr: destination address of the path (ETH_ALEN length)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200507 * @sdata: local subif
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100508 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200509 * Returns: 0 on success
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100510 *
511 * State: the initial state of the new path is set to 0
512 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200513int mesh_path_add(u8 *dst, struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100514{
Javier Cardona18889232009-08-10 12:15:52 -0700515 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
516 struct ieee80211_local *local = sdata->local;
Johannes Berg349eb8c2011-05-14 11:56:16 +0200517 struct mesh_table *tbl;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100518 struct mesh_path *mpath, *new_mpath;
519 struct mpath_node *node, *new_node;
520 struct hlist_head *bucket;
521 struct hlist_node *n;
522 int grow = 0;
523 int err = 0;
524 u32 hash_idx;
525
Felix Fietkau888d04d2012-03-01 15:22:09 +0100526 if (compare_ether_addr(dst, sdata->vif.addr) == 0)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100527 /* never add ourselves as neighbours */
528 return -ENOTSUPP;
529
530 if (is_multicast_ether_addr(dst))
531 return -ENOTSUPP;
532
Johannes Berg472dbc42008-09-11 00:01:49 +0200533 if (atomic_add_unless(&sdata->u.mesh.mpaths, 1, MESH_MAX_MPATHS) == 0)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100534 return -ENOSPC;
535
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400536 err = -ENOMEM;
Javier Cardona18889232009-08-10 12:15:52 -0700537 new_mpath = kzalloc(sizeof(struct mesh_path), GFP_ATOMIC);
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400538 if (!new_mpath)
539 goto err_path_alloc;
540
Javier Cardona18889232009-08-10 12:15:52 -0700541 new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400542 if (!new_node)
543 goto err_node_alloc;
Pavel Emelyanovf84e71a2008-05-06 18:46:36 +0400544
Javier Cardona9b84b8082011-05-03 16:57:16 -0700545 read_lock_bh(&pathtbl_resize_lock);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100546 memcpy(new_mpath->dst, dst, ETH_ALEN);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200547 new_mpath->sdata = sdata;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100548 new_mpath->flags = 0;
549 skb_queue_head_init(&new_mpath->frame_queue);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100550 new_node->mpath = new_mpath;
551 new_mpath->timer.data = (unsigned long) new_mpath;
552 new_mpath->timer.function = mesh_path_timer;
553 new_mpath->exp_time = jiffies;
554 spin_lock_init(&new_mpath->state_lock);
555 init_timer(&new_mpath->timer);
556
Johannes Berg349eb8c2011-05-14 11:56:16 +0200557 tbl = resize_dereference_mesh_paths();
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100558
Johannes Berg349eb8c2011-05-14 11:56:16 +0200559 hash_idx = mesh_table_hash(dst, sdata, tbl);
560 bucket = &tbl->hash_buckets[hash_idx];
561
Thomas Pedersenf06c7882012-03-06 16:42:09 -0800562 spin_lock(&tbl->hashwlock[hash_idx]);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100563
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400564 err = -EEXIST;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100565 hlist_for_each_entry(node, n, bucket, list) {
566 mpath = node->mpath;
Felix Fietkau888d04d2012-03-01 15:22:09 +0100567 if (mpath->sdata == sdata &&
568 compare_ether_addr(dst, mpath->dst) == 0)
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400569 goto err_exists;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100570 }
571
572 hlist_add_head_rcu(&new_node->list, bucket);
Johannes Berg349eb8c2011-05-14 11:56:16 +0200573 if (atomic_inc_return(&tbl->entries) >=
574 tbl->mean_chain_len * (tbl->hash_mask + 1))
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100575 grow = 1;
576
Johannes Bergf5ea9122009-08-07 16:17:38 +0200577 mesh_paths_generation++;
578
Thomas Pedersenf06c7882012-03-06 16:42:09 -0800579 spin_unlock(&tbl->hashwlock[hash_idx]);
Javier Cardona9b84b8082011-05-03 16:57:16 -0700580 read_unlock_bh(&pathtbl_resize_lock);
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400581 if (grow) {
Javier Cardona18889232009-08-10 12:15:52 -0700582 set_bit(MESH_WORK_GROW_MPATH_TABLE, &ifmsh->wrkq_flags);
Johannes Berg64592c82010-06-10 10:21:31 +0200583 ieee80211_queue_work(&local->hw, &sdata->work);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100584 }
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400585 return 0;
586
587err_exists:
Thomas Pedersenf06c7882012-03-06 16:42:09 -0800588 spin_unlock(&tbl->hashwlock[hash_idx]);
Javier Cardona9b84b8082011-05-03 16:57:16 -0700589 read_unlock_bh(&pathtbl_resize_lock);
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400590 kfree(new_node);
591err_node_alloc:
592 kfree(new_mpath);
593err_path_alloc:
Johannes Berg472dbc42008-09-11 00:01:49 +0200594 atomic_dec(&sdata->u.mesh.mpaths);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100595 return err;
596}
597
Johannes Berg1928eca2011-05-14 11:00:52 +0200598static void mesh_table_free_rcu(struct rcu_head *rcu)
599{
600 struct mesh_table *tbl = container_of(rcu, struct mesh_table, rcu_head);
601
602 mesh_table_free(tbl, false);
603}
604
Javier Cardona18889232009-08-10 12:15:52 -0700605void mesh_mpath_table_grow(void)
606{
607 struct mesh_table *oldtbl, *newtbl;
608
Javier Cardona9b84b8082011-05-03 16:57:16 -0700609 write_lock_bh(&pathtbl_resize_lock);
Johannes Berg349eb8c2011-05-14 11:56:16 +0200610 oldtbl = resize_dereference_mesh_paths();
611 newtbl = mesh_table_alloc(oldtbl->size_order + 1);
Johannes Berg1928eca2011-05-14 11:00:52 +0200612 if (!newtbl)
613 goto out;
Johannes Berg349eb8c2011-05-14 11:56:16 +0200614 if (mesh_table_grow(oldtbl, newtbl) < 0) {
cozybit Inca3e6b122011-04-13 11:10:28 -0700615 __mesh_table_free(newtbl);
Johannes Berg1928eca2011-05-14 11:00:52 +0200616 goto out;
Javier Cardona18889232009-08-10 12:15:52 -0700617 }
618 rcu_assign_pointer(mesh_paths, newtbl);
Javier Cardona18889232009-08-10 12:15:52 -0700619
Johannes Berg1928eca2011-05-14 11:00:52 +0200620 call_rcu(&oldtbl->rcu_head, mesh_table_free_rcu);
621
622 out:
623 write_unlock_bh(&pathtbl_resize_lock);
Javier Cardona18889232009-08-10 12:15:52 -0700624}
625
626void mesh_mpp_table_grow(void)
627{
628 struct mesh_table *oldtbl, *newtbl;
629
Javier Cardona9b84b8082011-05-03 16:57:16 -0700630 write_lock_bh(&pathtbl_resize_lock);
Johannes Berg349eb8c2011-05-14 11:56:16 +0200631 oldtbl = resize_dereference_mpp_paths();
632 newtbl = mesh_table_alloc(oldtbl->size_order + 1);
Johannes Berg1928eca2011-05-14 11:00:52 +0200633 if (!newtbl)
634 goto out;
Johannes Berg349eb8c2011-05-14 11:56:16 +0200635 if (mesh_table_grow(oldtbl, newtbl) < 0) {
cozybit Inca3e6b122011-04-13 11:10:28 -0700636 __mesh_table_free(newtbl);
Johannes Berg1928eca2011-05-14 11:00:52 +0200637 goto out;
Javier Cardona18889232009-08-10 12:15:52 -0700638 }
639 rcu_assign_pointer(mpp_paths, newtbl);
Johannes Berg1928eca2011-05-14 11:00:52 +0200640 call_rcu(&oldtbl->rcu_head, mesh_table_free_rcu);
Javier Cardona18889232009-08-10 12:15:52 -0700641
Johannes Berg1928eca2011-05-14 11:00:52 +0200642 out:
643 write_unlock_bh(&pathtbl_resize_lock);
Javier Cardona18889232009-08-10 12:15:52 -0700644}
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100645
YanBo79617de2008-09-22 13:30:32 +0800646int mpp_path_add(u8 *dst, u8 *mpp, struct ieee80211_sub_if_data *sdata)
647{
Javier Cardona18889232009-08-10 12:15:52 -0700648 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
649 struct ieee80211_local *local = sdata->local;
Johannes Berg349eb8c2011-05-14 11:56:16 +0200650 struct mesh_table *tbl;
YanBo79617de2008-09-22 13:30:32 +0800651 struct mesh_path *mpath, *new_mpath;
652 struct mpath_node *node, *new_node;
653 struct hlist_head *bucket;
654 struct hlist_node *n;
655 int grow = 0;
656 int err = 0;
657 u32 hash_idx;
658
Felix Fietkau888d04d2012-03-01 15:22:09 +0100659 if (compare_ether_addr(dst, sdata->vif.addr) == 0)
YanBo79617de2008-09-22 13:30:32 +0800660 /* never add ourselves as neighbours */
661 return -ENOTSUPP;
662
663 if (is_multicast_ether_addr(dst))
664 return -ENOTSUPP;
665
666 err = -ENOMEM;
Javier Cardona18889232009-08-10 12:15:52 -0700667 new_mpath = kzalloc(sizeof(struct mesh_path), GFP_ATOMIC);
YanBo79617de2008-09-22 13:30:32 +0800668 if (!new_mpath)
669 goto err_path_alloc;
670
Javier Cardona18889232009-08-10 12:15:52 -0700671 new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
YanBo79617de2008-09-22 13:30:32 +0800672 if (!new_node)
673 goto err_node_alloc;
674
Javier Cardona9b84b8082011-05-03 16:57:16 -0700675 read_lock_bh(&pathtbl_resize_lock);
YanBo79617de2008-09-22 13:30:32 +0800676 memcpy(new_mpath->dst, dst, ETH_ALEN);
677 memcpy(new_mpath->mpp, mpp, ETH_ALEN);
678 new_mpath->sdata = sdata;
679 new_mpath->flags = 0;
680 skb_queue_head_init(&new_mpath->frame_queue);
681 new_node->mpath = new_mpath;
Thomas Pedersenc61336612011-08-25 10:36:14 -0700682 init_timer(&new_mpath->timer);
YanBo79617de2008-09-22 13:30:32 +0800683 new_mpath->exp_time = jiffies;
684 spin_lock_init(&new_mpath->state_lock);
685
Johannes Berg349eb8c2011-05-14 11:56:16 +0200686 tbl = resize_dereference_mpp_paths();
YanBo79617de2008-09-22 13:30:32 +0800687
Johannes Berg349eb8c2011-05-14 11:56:16 +0200688 hash_idx = mesh_table_hash(dst, sdata, tbl);
689 bucket = &tbl->hash_buckets[hash_idx];
690
Thomas Pedersenf06c7882012-03-06 16:42:09 -0800691 spin_lock(&tbl->hashwlock[hash_idx]);
YanBo79617de2008-09-22 13:30:32 +0800692
693 err = -EEXIST;
694 hlist_for_each_entry(node, n, bucket, list) {
695 mpath = node->mpath;
Felix Fietkau888d04d2012-03-01 15:22:09 +0100696 if (mpath->sdata == sdata &&
697 compare_ether_addr(dst, mpath->dst) == 0)
YanBo79617de2008-09-22 13:30:32 +0800698 goto err_exists;
699 }
700
701 hlist_add_head_rcu(&new_node->list, bucket);
Johannes Berg349eb8c2011-05-14 11:56:16 +0200702 if (atomic_inc_return(&tbl->entries) >=
703 tbl->mean_chain_len * (tbl->hash_mask + 1))
YanBo79617de2008-09-22 13:30:32 +0800704 grow = 1;
705
Thomas Pedersenf06c7882012-03-06 16:42:09 -0800706 spin_unlock(&tbl->hashwlock[hash_idx]);
Javier Cardona9b84b8082011-05-03 16:57:16 -0700707 read_unlock_bh(&pathtbl_resize_lock);
YanBo79617de2008-09-22 13:30:32 +0800708 if (grow) {
Javier Cardona18889232009-08-10 12:15:52 -0700709 set_bit(MESH_WORK_GROW_MPP_TABLE, &ifmsh->wrkq_flags);
Johannes Berg64592c82010-06-10 10:21:31 +0200710 ieee80211_queue_work(&local->hw, &sdata->work);
YanBo79617de2008-09-22 13:30:32 +0800711 }
712 return 0;
713
714err_exists:
Thomas Pedersenf06c7882012-03-06 16:42:09 -0800715 spin_unlock(&tbl->hashwlock[hash_idx]);
Javier Cardona9b84b8082011-05-03 16:57:16 -0700716 read_unlock_bh(&pathtbl_resize_lock);
YanBo79617de2008-09-22 13:30:32 +0800717 kfree(new_node);
718err_node_alloc:
719 kfree(new_mpath);
720err_path_alloc:
721 return err;
722}
723
724
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100725/**
726 * mesh_plink_broken - deactivates paths and sends perr when a link breaks
727 *
728 * @sta: broken peer link
729 *
730 * This function must be called from the rate control algorithm if enough
731 * delivery errors suggest that a peer link is no longer usable.
732 */
733void mesh_plink_broken(struct sta_info *sta)
734{
Johannes Berg349eb8c2011-05-14 11:56:16 +0200735 struct mesh_table *tbl;
Johannes Berg15ff6362009-11-17 13:34:04 +0100736 static const u8 bcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100737 struct mesh_path *mpath;
738 struct mpath_node *node;
739 struct hlist_node *p;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200740 struct ieee80211_sub_if_data *sdata = sta->sdata;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100741 int i;
Thomas Pedersen25d49e42011-08-11 19:35:15 -0700742 __le16 reason = cpu_to_le16(WLAN_REASON_MESH_PATH_DEST_UNREACHABLE);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100743
744 rcu_read_lock();
Johannes Berg349eb8c2011-05-14 11:56:16 +0200745 tbl = rcu_dereference(mesh_paths);
746 for_each_mesh_entry(tbl, p, node, i) {
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100747 mpath = node->mpath;
Johannes Berg349eb8c2011-05-14 11:56:16 +0200748 if (rcu_dereference(mpath->next_hop) == sta &&
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100749 mpath->flags & MESH_PATH_ACTIVE &&
750 !(mpath->flags & MESH_PATH_FIXED)) {
Javier Cardonaf5e50cd2011-08-29 13:23:05 -0700751 spin_lock_bh(&mpath->state_lock);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100752 mpath->flags &= ~MESH_PATH_ACTIVE;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000753 ++mpath->sn;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100754 spin_unlock_bh(&mpath->state_lock);
Javier Cardona45904f22010-12-03 09:20:40 +0100755 mesh_path_error_tx(sdata->u.mesh.mshcfg.element_ttl,
756 mpath->dst, cpu_to_le32(mpath->sn),
Thomas Pedersen25d49e42011-08-11 19:35:15 -0700757 reason, bcast, sdata);
Javier Cardonaf5e50cd2011-08-29 13:23:05 -0700758 }
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100759 }
760 rcu_read_unlock();
761}
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100762
Javier Cardona19c50b32011-08-29 13:23:07 -0700763static void mesh_path_node_reclaim(struct rcu_head *rp)
764{
765 struct mpath_node *node = container_of(rp, struct mpath_node, rcu);
766 struct ieee80211_sub_if_data *sdata = node->mpath->sdata;
767
768 del_timer_sync(&node->mpath->timer);
769 atomic_dec(&sdata->u.mesh.mpaths);
770 kfree(node->mpath);
771 kfree(node);
772}
773
774/* needs to be called with the corresponding hashwlock taken */
775static void __mesh_path_del(struct mesh_table *tbl, struct mpath_node *node)
776{
777 struct mesh_path *mpath;
778 mpath = node->mpath;
779 spin_lock(&mpath->state_lock);
780 mpath->flags |= MESH_PATH_RESOLVING;
781 if (mpath->is_gate)
782 mesh_gate_del(tbl, mpath);
783 hlist_del_rcu(&node->list);
784 call_rcu(&node->rcu, mesh_path_node_reclaim);
785 spin_unlock(&mpath->state_lock);
786 atomic_dec(&tbl->entries);
787}
788
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100789/**
790 * mesh_path_flush_by_nexthop - Deletes mesh paths if their next hop matches
791 *
792 * @sta - mesh peer to match
793 *
Luis Carlos Cobob4e08ea2008-02-29 15:46:08 -0800794 * RCU notes: this function is called when a mesh plink transitions from
795 * PLINK_ESTAB to any other state, since PLINK_ESTAB state is the only one that
796 * allows path creation. This will happen before the sta can be freed (because
Johannes Bergd0709a62008-02-25 16:27:46 +0100797 * sta_info_destroy() calls this) so any reader in a rcu read block will be
798 * protected against the plink disappearing.
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100799 */
800void mesh_path_flush_by_nexthop(struct sta_info *sta)
801{
Johannes Berg349eb8c2011-05-14 11:56:16 +0200802 struct mesh_table *tbl;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100803 struct mesh_path *mpath;
804 struct mpath_node *node;
805 struct hlist_node *p;
806 int i;
807
Johannes Berg349eb8c2011-05-14 11:56:16 +0200808 rcu_read_lock();
Javier Cardona239289e2011-08-29 13:23:09 -0700809 read_lock_bh(&pathtbl_resize_lock);
810 tbl = resize_dereference_mesh_paths();
Johannes Berg349eb8c2011-05-14 11:56:16 +0200811 for_each_mesh_entry(tbl, p, node, i) {
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100812 mpath = node->mpath;
Javier Cardonacd72e812011-08-29 13:23:08 -0700813 if (rcu_dereference(mpath->next_hop) == sta) {
Thomas Pedersenf06c7882012-03-06 16:42:09 -0800814 spin_lock(&tbl->hashwlock[i]);
Javier Cardona19c50b32011-08-29 13:23:07 -0700815 __mesh_path_del(tbl, node);
Thomas Pedersenf06c7882012-03-06 16:42:09 -0800816 spin_unlock(&tbl->hashwlock[i]);
Javier Cardona19c50b32011-08-29 13:23:07 -0700817 }
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100818 }
Javier Cardona239289e2011-08-29 13:23:09 -0700819 read_unlock_bh(&pathtbl_resize_lock);
Johannes Berg349eb8c2011-05-14 11:56:16 +0200820 rcu_read_unlock();
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100821}
822
Javier Cardonacd72e812011-08-29 13:23:08 -0700823static void table_flush_by_iface(struct mesh_table *tbl,
824 struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100825{
826 struct mesh_path *mpath;
827 struct mpath_node *node;
828 struct hlist_node *p;
829 int i;
830
Javier Cardonacd72e812011-08-29 13:23:08 -0700831 WARN_ON(!rcu_read_lock_held());
Johannes Berg349eb8c2011-05-14 11:56:16 +0200832 for_each_mesh_entry(tbl, p, node, i) {
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100833 mpath = node->mpath;
Javier Cardonacd72e812011-08-29 13:23:08 -0700834 if (mpath->sdata != sdata)
835 continue;
Javier Cardonaece1a2e2011-08-29 13:23:04 -0700836 spin_lock_bh(&tbl->hashwlock[i]);
Javier Cardona19c50b32011-08-29 13:23:07 -0700837 __mesh_path_del(tbl, node);
Javier Cardonaece1a2e2011-08-29 13:23:04 -0700838 spin_unlock_bh(&tbl->hashwlock[i]);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100839 }
840}
841
Javier Cardonaece1a2e2011-08-29 13:23:04 -0700842/**
843 * mesh_path_flush_by_iface - Deletes all mesh paths associated with a given iface
844 *
845 * This function deletes both mesh paths as well as mesh portal paths.
846 *
847 * @sdata - interface data to match
848 *
849 */
850void mesh_path_flush_by_iface(struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100851{
Javier Cardonacd72e812011-08-29 13:23:08 -0700852 struct mesh_table *tbl;
Johannes Bergd0709a62008-02-25 16:27:46 +0100853
Javier Cardonacd72e812011-08-29 13:23:08 -0700854 rcu_read_lock();
Javier Cardona239289e2011-08-29 13:23:09 -0700855 read_lock_bh(&pathtbl_resize_lock);
856 tbl = resize_dereference_mesh_paths();
Javier Cardonacd72e812011-08-29 13:23:08 -0700857 table_flush_by_iface(tbl, sdata);
Javier Cardona239289e2011-08-29 13:23:09 -0700858 tbl = resize_dereference_mpp_paths();
Javier Cardonacd72e812011-08-29 13:23:08 -0700859 table_flush_by_iface(tbl, sdata);
Javier Cardona239289e2011-08-29 13:23:09 -0700860 read_unlock_bh(&pathtbl_resize_lock);
Javier Cardonacd72e812011-08-29 13:23:08 -0700861 rcu_read_unlock();
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100862}
863
864/**
865 * mesh_path_del - delete a mesh path from the table
866 *
867 * @addr: dst address (ETH_ALEN length)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200868 * @sdata: local subif
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100869 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200870 * Returns: 0 if successful
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100871 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200872int mesh_path_del(u8 *addr, struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100873{
Johannes Berg349eb8c2011-05-14 11:56:16 +0200874 struct mesh_table *tbl;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100875 struct mesh_path *mpath;
876 struct mpath_node *node;
877 struct hlist_head *bucket;
878 struct hlist_node *n;
879 int hash_idx;
880 int err = 0;
881
Javier Cardona9b84b8082011-05-03 16:57:16 -0700882 read_lock_bh(&pathtbl_resize_lock);
Johannes Berg349eb8c2011-05-14 11:56:16 +0200883 tbl = resize_dereference_mesh_paths();
884 hash_idx = mesh_table_hash(addr, sdata, tbl);
885 bucket = &tbl->hash_buckets[hash_idx];
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100886
Thomas Pedersenf06c7882012-03-06 16:42:09 -0800887 spin_lock(&tbl->hashwlock[hash_idx]);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100888 hlist_for_each_entry(node, n, bucket, list) {
889 mpath = node->mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200890 if (mpath->sdata == sdata &&
Felix Fietkau888d04d2012-03-01 15:22:09 +0100891 compare_ether_addr(addr, mpath->dst) == 0) {
Javier Cardona19c50b32011-08-29 13:23:07 -0700892 __mesh_path_del(tbl, node);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100893 goto enddel;
894 }
895 }
896
897 err = -ENXIO;
898enddel:
Johannes Bergf5ea9122009-08-07 16:17:38 +0200899 mesh_paths_generation++;
Thomas Pedersenf06c7882012-03-06 16:42:09 -0800900 spin_unlock(&tbl->hashwlock[hash_idx]);
Javier Cardona9b84b8082011-05-03 16:57:16 -0700901 read_unlock_bh(&pathtbl_resize_lock);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100902 return err;
903}
904
905/**
906 * mesh_path_tx_pending - sends pending frames in a mesh path queue
907 *
908 * @mpath: mesh path to activate
909 *
910 * Locking: the state_lock of the mpath structure must NOT be held when calling
911 * this function.
912 */
913void mesh_path_tx_pending(struct mesh_path *mpath)
914{
Javier Cardona249b4052009-07-07 10:55:03 -0700915 if (mpath->flags & MESH_PATH_ACTIVE)
916 ieee80211_add_pending_skbs(mpath->sdata->local,
917 &mpath->frame_queue);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100918}
919
920/**
Javier Cardona5ee68e52011-08-09 16:45:08 -0700921 * mesh_path_send_to_gates - sends pending frames to all known mesh gates
922 *
923 * @mpath: mesh path whose queue will be emptied
924 *
925 * If there is only one gate, the frames are transferred from the failed mpath
926 * queue to that gate's queue. If there are more than one gates, the frames
927 * are copied from each gate to the next. After frames are copied, the
928 * mpath queues are emptied onto the transmission queue.
929 */
930int mesh_path_send_to_gates(struct mesh_path *mpath)
931{
932 struct ieee80211_sub_if_data *sdata = mpath->sdata;
933 struct hlist_node *n;
934 struct mesh_table *tbl;
935 struct mesh_path *from_mpath = mpath;
936 struct mpath_node *gate = NULL;
937 bool copy = false;
938 struct hlist_head *known_gates;
939
940 rcu_read_lock();
941 tbl = rcu_dereference(mesh_paths);
942 known_gates = tbl->known_gates;
943 rcu_read_unlock();
944
945 if (!known_gates)
946 return -EHOSTUNREACH;
947
948 hlist_for_each_entry_rcu(gate, n, known_gates, list) {
949 if (gate->mpath->sdata != sdata)
950 continue;
951
952 if (gate->mpath->flags & MESH_PATH_ACTIVE) {
953 mpath_dbg("Forwarding to %pM\n", gate->mpath->dst);
954 mesh_path_move_to_queue(gate->mpath, from_mpath, copy);
955 from_mpath = gate->mpath;
956 copy = true;
957 } else {
958 mpath_dbg("Not forwarding %p\n", gate->mpath);
959 mpath_dbg("flags %x\n", gate->mpath->flags);
960 }
961 }
962
963 hlist_for_each_entry_rcu(gate, n, known_gates, list)
964 if (gate->mpath->sdata == sdata) {
965 mpath_dbg("Sending to %pM\n", gate->mpath->dst);
966 mesh_path_tx_pending(gate->mpath);
967 }
968
969 return (from_mpath == mpath) ? -EHOSTUNREACH : 0;
970}
971
972/**
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100973 * mesh_path_discard_frame - discard a frame whose path could not be resolved
974 *
975 * @skb: frame to discard
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200976 * @sdata: network subif the frame was to be sent through
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100977 *
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100978 * Locking: the function must me called within a rcu_read_lock region
979 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200980void mesh_path_discard_frame(struct sk_buff *skb,
981 struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100982{
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100983 kfree_skb(skb);
Johannes Berg472dbc42008-09-11 00:01:49 +0200984 sdata->u.mesh.mshstats.dropped_frames_no_route++;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100985}
986
987/**
988 * mesh_path_flush_pending - free the pending queue of a mesh path
989 *
990 * @mpath: mesh path whose queue has to be freed
991 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300992 * Locking: the function must me called within a rcu_read_lock region
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100993 */
994void mesh_path_flush_pending(struct mesh_path *mpath)
995{
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100996 struct sk_buff *skb;
997
Javier Cardona00e3f252011-08-09 16:45:07 -0700998 while ((skb = skb_dequeue(&mpath->frame_queue)) != NULL)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200999 mesh_path_discard_frame(skb, mpath->sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001000}
1001
1002/**
1003 * mesh_path_fix_nexthop - force a specific next hop for a mesh path
1004 *
1005 * @mpath: the mesh path to modify
1006 * @next_hop: the next hop to force
1007 *
1008 * Locking: this function must be called holding mpath->state_lock
1009 */
1010void mesh_path_fix_nexthop(struct mesh_path *mpath, struct sta_info *next_hop)
1011{
1012 spin_lock_bh(&mpath->state_lock);
1013 mesh_path_assign_nexthop(mpath, next_hop);
Rui Paulod19b3bf2009-11-09 23:46:55 +00001014 mpath->sn = 0xffff;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001015 mpath->metric = 0;
1016 mpath->hop_count = 0;
1017 mpath->exp_time = 0;
1018 mpath->flags |= MESH_PATH_FIXED;
1019 mesh_path_activate(mpath);
1020 spin_unlock_bh(&mpath->state_lock);
1021 mesh_path_tx_pending(mpath);
1022}
1023
1024static void mesh_path_node_free(struct hlist_node *p, bool free_leafs)
1025{
1026 struct mesh_path *mpath;
1027 struct mpath_node *node = hlist_entry(p, struct mpath_node, list);
1028 mpath = node->mpath;
1029 hlist_del_rcu(p);
Javier Cardonad0df9ee2011-05-13 14:16:07 -07001030 if (free_leafs) {
1031 del_timer_sync(&mpath->timer);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001032 kfree(mpath);
Javier Cardonad0df9ee2011-05-13 14:16:07 -07001033 }
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001034 kfree(node);
1035}
1036
Pavel Emelyanov4caf86c2008-05-07 19:47:01 +04001037static int mesh_path_node_copy(struct hlist_node *p, struct mesh_table *newtbl)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001038{
1039 struct mesh_path *mpath;
1040 struct mpath_node *node, *new_node;
1041 u32 hash_idx;
1042
Pavel Emelyanov8566dc32008-05-07 19:51:51 +04001043 new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
Pavel Emelyanov00242c42008-05-07 19:48:14 +04001044 if (new_node == NULL)
1045 return -ENOMEM;
1046
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001047 node = hlist_entry(p, struct mpath_node, list);
1048 mpath = node->mpath;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001049 new_node->mpath = mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001050 hash_idx = mesh_table_hash(mpath->dst, mpath->sdata, newtbl);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001051 hlist_add_head(&new_node->list,
1052 &newtbl->hash_buckets[hash_idx]);
Pavel Emelyanov4caf86c2008-05-07 19:47:01 +04001053 return 0;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001054}
1055
1056int mesh_pathtbl_init(void)
1057{
Johannes Berg349eb8c2011-05-14 11:56:16 +02001058 struct mesh_table *tbl_path, *tbl_mpp;
Dan Carpenter4c5ade42011-08-30 22:16:15 +03001059 int ret;
YanBo79617de2008-09-22 13:30:32 +08001060
Johannes Berg349eb8c2011-05-14 11:56:16 +02001061 tbl_path = mesh_table_alloc(INIT_PATHS_SIZE_ORDER);
1062 if (!tbl_path)
1063 return -ENOMEM;
1064 tbl_path->free_node = &mesh_path_node_free;
1065 tbl_path->copy_node = &mesh_path_node_copy;
1066 tbl_path->mean_chain_len = MEAN_CHAIN_LEN;
Javier Cardona5ee68e52011-08-09 16:45:08 -07001067 tbl_path->known_gates = kzalloc(sizeof(struct hlist_head), GFP_ATOMIC);
Dan Carpenter4c5ade42011-08-30 22:16:15 +03001068 if (!tbl_path->known_gates) {
1069 ret = -ENOMEM;
1070 goto free_path;
1071 }
Javier Cardona5ee68e52011-08-09 16:45:08 -07001072 INIT_HLIST_HEAD(tbl_path->known_gates);
1073
Johannes Berg349eb8c2011-05-14 11:56:16 +02001074
1075 tbl_mpp = mesh_table_alloc(INIT_PATHS_SIZE_ORDER);
1076 if (!tbl_mpp) {
Dan Carpenter4c5ade42011-08-30 22:16:15 +03001077 ret = -ENOMEM;
1078 goto free_path;
YanBo79617de2008-09-22 13:30:32 +08001079 }
Johannes Berg349eb8c2011-05-14 11:56:16 +02001080 tbl_mpp->free_node = &mesh_path_node_free;
1081 tbl_mpp->copy_node = &mesh_path_node_copy;
1082 tbl_mpp->mean_chain_len = MEAN_CHAIN_LEN;
Javier Cardona5ee68e52011-08-09 16:45:08 -07001083 tbl_mpp->known_gates = kzalloc(sizeof(struct hlist_head), GFP_ATOMIC);
Dan Carpenter4c5ade42011-08-30 22:16:15 +03001084 if (!tbl_mpp->known_gates) {
1085 ret = -ENOMEM;
1086 goto free_mpp;
1087 }
Javier Cardona5ee68e52011-08-09 16:45:08 -07001088 INIT_HLIST_HEAD(tbl_mpp->known_gates);
Johannes Berg349eb8c2011-05-14 11:56:16 +02001089
1090 /* Need no locking since this is during init */
1091 RCU_INIT_POINTER(mesh_paths, tbl_path);
1092 RCU_INIT_POINTER(mpp_paths, tbl_mpp);
YanBo79617de2008-09-22 13:30:32 +08001093
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001094 return 0;
Dan Carpenter4c5ade42011-08-30 22:16:15 +03001095
1096free_mpp:
1097 mesh_table_free(tbl_mpp, true);
1098free_path:
1099 mesh_table_free(tbl_path, true);
1100 return ret;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001101}
1102
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001103void mesh_path_expire(struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001104{
Johannes Berg349eb8c2011-05-14 11:56:16 +02001105 struct mesh_table *tbl;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001106 struct mesh_path *mpath;
1107 struct mpath_node *node;
1108 struct hlist_node *p;
1109 int i;
1110
Johannes Berg349eb8c2011-05-14 11:56:16 +02001111 rcu_read_lock();
1112 tbl = rcu_dereference(mesh_paths);
1113 for_each_mesh_entry(tbl, p, node, i) {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001114 if (node->mpath->sdata != sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001115 continue;
1116 mpath = node->mpath;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001117 if ((!(mpath->flags & MESH_PATH_RESOLVING)) &&
1118 (!(mpath->flags & MESH_PATH_FIXED)) &&
Javier Cardonaf5e50cd2011-08-29 13:23:05 -07001119 time_after(jiffies, mpath->exp_time + MESH_PATH_EXPIRE))
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001120 mesh_path_del(mpath->dst, mpath->sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001121 }
Johannes Berg349eb8c2011-05-14 11:56:16 +02001122 rcu_read_unlock();
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001123}
1124
1125void mesh_pathtbl_unregister(void)
1126{
Johannes Berg349eb8c2011-05-14 11:56:16 +02001127 /* no need for locking during exit path */
Eric Dumazet33d480c2011-08-11 19:30:52 +00001128 mesh_table_free(rcu_dereference_protected(mesh_paths, 1), true);
1129 mesh_table_free(rcu_dereference_protected(mpp_paths, 1), true);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001130}