blob: 7f54c5042235dd75fb87963fc7e2da04554d219f [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
Javier Cardona5ee68e52011-08-09 16:45:08 -070072static int mesh_gate_add(struct mesh_table *tbl, struct mesh_path *mpath);
73
Johannes Berg349eb8c2011-05-14 11:56:16 +020074/*
75 * CAREFUL -- "tbl" must not be an expression,
76 * in particular not an rcu_dereference(), since
77 * it's used twice. So it is illegal to do
78 * for_each_mesh_entry(rcu_dereference(...), ...)
79 */
80#define for_each_mesh_entry(tbl, p, node, i) \
81 for (i = 0; i <= tbl->hash_mask; i++) \
82 hlist_for_each_entry_rcu(node, p, &tbl->hash_buckets[i], list)
83
84
Johannes Berg6b86bd62011-05-12 13:38:50 +020085static struct mesh_table *mesh_table_alloc(int size_order)
86{
87 int i;
88 struct mesh_table *newtbl;
89
Javier Cardonad676ff42011-05-17 16:13:34 -070090 newtbl = kmalloc(sizeof(struct mesh_table), GFP_ATOMIC);
Johannes Berg6b86bd62011-05-12 13:38:50 +020091 if (!newtbl)
92 return NULL;
93
94 newtbl->hash_buckets = kzalloc(sizeof(struct hlist_head) *
Javier Cardonad676ff42011-05-17 16:13:34 -070095 (1 << size_order), GFP_ATOMIC);
Johannes Berg6b86bd62011-05-12 13:38:50 +020096
97 if (!newtbl->hash_buckets) {
98 kfree(newtbl);
99 return NULL;
100 }
101
102 newtbl->hashwlock = kmalloc(sizeof(spinlock_t) *
Javier Cardonad676ff42011-05-17 16:13:34 -0700103 (1 << size_order), GFP_ATOMIC);
Johannes Berg6b86bd62011-05-12 13:38:50 +0200104 if (!newtbl->hashwlock) {
105 kfree(newtbl->hash_buckets);
106 kfree(newtbl);
107 return NULL;
108 }
109
110 newtbl->size_order = size_order;
111 newtbl->hash_mask = (1 << size_order) - 1;
112 atomic_set(&newtbl->entries, 0);
113 get_random_bytes(&newtbl->hash_rnd,
114 sizeof(newtbl->hash_rnd));
115 for (i = 0; i <= newtbl->hash_mask; i++)
116 spin_lock_init(&newtbl->hashwlock[i]);
Javier Cardona5ee68e52011-08-09 16:45:08 -0700117 spin_lock_init(&newtbl->gates_lock);
Johannes Berg6b86bd62011-05-12 13:38:50 +0200118
119 return newtbl;
120}
121
Javier Cardona18889232009-08-10 12:15:52 -0700122static void __mesh_table_free(struct mesh_table *tbl)
123{
124 kfree(tbl->hash_buckets);
125 kfree(tbl->hashwlock);
126 kfree(tbl);
127}
128
Johannes Berg6b86bd62011-05-12 13:38:50 +0200129static void mesh_table_free(struct mesh_table *tbl, bool free_leafs)
Javier Cardona18889232009-08-10 12:15:52 -0700130{
131 struct hlist_head *mesh_hash;
132 struct hlist_node *p, *q;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700133 struct mpath_node *gate;
Javier Cardona18889232009-08-10 12:15:52 -0700134 int i;
135
136 mesh_hash = tbl->hash_buckets;
137 for (i = 0; i <= tbl->hash_mask; i++) {
Javier Cardona9b84b8082011-05-03 16:57:16 -0700138 spin_lock_bh(&tbl->hashwlock[i]);
Javier Cardona18889232009-08-10 12:15:52 -0700139 hlist_for_each_safe(p, q, &mesh_hash[i]) {
140 tbl->free_node(p, free_leafs);
141 atomic_dec(&tbl->entries);
142 }
Javier Cardona9b84b8082011-05-03 16:57:16 -0700143 spin_unlock_bh(&tbl->hashwlock[i]);
Javier Cardona18889232009-08-10 12:15:52 -0700144 }
Javier Cardona5ee68e52011-08-09 16:45:08 -0700145 if (free_leafs) {
146 spin_lock_bh(&tbl->gates_lock);
147 hlist_for_each_entry_safe(gate, p, q,
148 tbl->known_gates, list) {
149 hlist_del(&gate->list);
150 kfree(gate);
151 }
152 kfree(tbl->known_gates);
153 spin_unlock_bh(&tbl->gates_lock);
154 }
155
Javier Cardona18889232009-08-10 12:15:52 -0700156 __mesh_table_free(tbl);
157}
158
cozybit Inca3e6b122011-04-13 11:10:28 -0700159static int mesh_table_grow(struct mesh_table *oldtbl,
Johannes Berg6b86bd62011-05-12 13:38:50 +0200160 struct mesh_table *newtbl)
Javier Cardona18889232009-08-10 12:15:52 -0700161{
Javier Cardona18889232009-08-10 12:15:52 -0700162 struct hlist_head *oldhash;
163 struct hlist_node *p, *q;
164 int i;
165
cozybit Inca3e6b122011-04-13 11:10:28 -0700166 if (atomic_read(&oldtbl->entries)
167 < oldtbl->mean_chain_len * (oldtbl->hash_mask + 1))
168 return -EAGAIN;
Javier Cardona18889232009-08-10 12:15:52 -0700169
cozybit Inca3e6b122011-04-13 11:10:28 -0700170 newtbl->free_node = oldtbl->free_node;
171 newtbl->mean_chain_len = oldtbl->mean_chain_len;
172 newtbl->copy_node = oldtbl->copy_node;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700173 newtbl->known_gates = oldtbl->known_gates;
cozybit Inca3e6b122011-04-13 11:10:28 -0700174 atomic_set(&newtbl->entries, atomic_read(&oldtbl->entries));
Javier Cardona18889232009-08-10 12:15:52 -0700175
cozybit Inca3e6b122011-04-13 11:10:28 -0700176 oldhash = oldtbl->hash_buckets;
177 for (i = 0; i <= oldtbl->hash_mask; i++)
Javier Cardona18889232009-08-10 12:15:52 -0700178 hlist_for_each(p, &oldhash[i])
cozybit Inca3e6b122011-04-13 11:10:28 -0700179 if (oldtbl->copy_node(p, newtbl) < 0)
Javier Cardona18889232009-08-10 12:15:52 -0700180 goto errcopy;
181
cozybit Inca3e6b122011-04-13 11:10:28 -0700182 return 0;
Javier Cardona18889232009-08-10 12:15:52 -0700183
184errcopy:
185 for (i = 0; i <= newtbl->hash_mask; i++) {
186 hlist_for_each_safe(p, q, &newtbl->hash_buckets[i])
cozybit Inca3e6b122011-04-13 11:10:28 -0700187 oldtbl->free_node(p, 0);
Javier Cardona18889232009-08-10 12:15:52 -0700188 }
cozybit Inca3e6b122011-04-13 11:10:28 -0700189 return -ENOMEM;
Javier Cardona18889232009-08-10 12:15:52 -0700190}
191
Johannes Berg6b86bd62011-05-12 13:38:50 +0200192static u32 mesh_table_hash(u8 *addr, struct ieee80211_sub_if_data *sdata,
193 struct mesh_table *tbl)
194{
195 /* Use last four bytes of hw addr and interface index as hash index */
196 return jhash_2words(*(u32 *)(addr+2), sdata->dev->ifindex, tbl->hash_rnd)
197 & tbl->hash_mask;
198}
Johannes Bergf5ea9122009-08-07 16:17:38 +0200199
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100200
201/**
202 *
203 * mesh_path_assign_nexthop - update mesh path next hop
204 *
205 * @mpath: mesh path to update
206 * @sta: next hop to assign
207 *
208 * Locking: mpath->state_lock must be held when calling this function
209 */
210void mesh_path_assign_nexthop(struct mesh_path *mpath, struct sta_info *sta)
211{
Javier Cardona10c836d2009-07-09 14:42:16 -0700212 struct sk_buff *skb;
213 struct ieee80211_hdr *hdr;
214 struct sk_buff_head tmpq;
215 unsigned long flags;
Javier Cardona4777be42011-09-07 17:49:52 -0700216 struct ieee80211_sub_if_data *sdata = mpath->sdata;
Javier Cardona10c836d2009-07-09 14:42:16 -0700217
Johannes Bergd0709a62008-02-25 16:27:46 +0100218 rcu_assign_pointer(mpath->next_hop, sta);
Javier Cardona10c836d2009-07-09 14:42:16 -0700219
220 __skb_queue_head_init(&tmpq);
221
222 spin_lock_irqsave(&mpath->frame_queue.lock, flags);
223
224 while ((skb = __skb_dequeue(&mpath->frame_queue)) != NULL) {
225 hdr = (struct ieee80211_hdr *) skb->data;
226 memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN);
Javier Cardona4777be42011-09-07 17:49:52 -0700227 skb_set_queue_mapping(skb, ieee80211_select_queue(sdata, skb));
Javier Cardona2154c812011-09-07 17:49:53 -0700228 ieee80211_set_qos_hdr(sdata, skb);
Javier Cardona10c836d2009-07-09 14:42:16 -0700229 __skb_queue_tail(&tmpq, skb);
230 }
231
232 skb_queue_splice(&tmpq, &mpath->frame_queue);
233 spin_unlock_irqrestore(&mpath->frame_queue.lock, flags);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100234}
235
Javier Cardona5ee68e52011-08-09 16:45:08 -0700236static void prepare_for_gate(struct sk_buff *skb, char *dst_addr,
237 struct mesh_path *gate_mpath)
238{
239 struct ieee80211_hdr *hdr;
240 struct ieee80211s_hdr *mshdr;
241 int mesh_hdrlen, hdrlen;
242 char *next_hop;
243
244 hdr = (struct ieee80211_hdr *) skb->data;
245 hdrlen = ieee80211_hdrlen(hdr->frame_control);
246 mshdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
247
248 if (!(mshdr->flags & MESH_FLAGS_AE)) {
249 /* size of the fixed part of the mesh header */
250 mesh_hdrlen = 6;
251
252 /* make room for the two extended addresses */
253 skb_push(skb, 2 * ETH_ALEN);
254 memmove(skb->data, hdr, hdrlen + mesh_hdrlen);
255
256 hdr = (struct ieee80211_hdr *) skb->data;
257
258 /* we preserve the previous mesh header and only add
259 * the new addreses */
260 mshdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
261 mshdr->flags = MESH_FLAGS_AE_A5_A6;
262 memcpy(mshdr->eaddr1, hdr->addr3, ETH_ALEN);
263 memcpy(mshdr->eaddr2, hdr->addr4, ETH_ALEN);
264 }
265
266 /* update next hop */
267 hdr = (struct ieee80211_hdr *) skb->data;
268 rcu_read_lock();
269 next_hop = rcu_dereference(gate_mpath->next_hop)->sta.addr;
270 memcpy(hdr->addr1, next_hop, ETH_ALEN);
271 rcu_read_unlock();
272 memcpy(hdr->addr3, dst_addr, ETH_ALEN);
273}
274
275/**
276 *
277 * mesh_path_move_to_queue - Move or copy frames from one mpath queue to another
278 *
279 * This function is used to transfer or copy frames from an unresolved mpath to
280 * a gate mpath. The function also adds the Address Extension field and
281 * updates the next hop.
282 *
283 * If a frame already has an Address Extension field, only the next hop and
284 * destination addresses are updated.
285 *
286 * The gate mpath must be an active mpath with a valid mpath->next_hop.
287 *
288 * @mpath: An active mpath the frames will be sent to (i.e. the gate)
289 * @from_mpath: The failed mpath
290 * @copy: When true, copy all the frames to the new mpath queue. When false,
291 * move them.
292 */
293static void mesh_path_move_to_queue(struct mesh_path *gate_mpath,
294 struct mesh_path *from_mpath,
295 bool copy)
296{
Thomas Pedersenc61336612011-08-25 10:36:14 -0700297 struct sk_buff *skb, *cp_skb = NULL;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700298 struct sk_buff_head gateq, failq;
299 unsigned long flags;
300 int num_skbs;
301
302 BUG_ON(gate_mpath == from_mpath);
303 BUG_ON(!gate_mpath->next_hop);
304
305 __skb_queue_head_init(&gateq);
306 __skb_queue_head_init(&failq);
307
308 spin_lock_irqsave(&from_mpath->frame_queue.lock, flags);
309 skb_queue_splice_init(&from_mpath->frame_queue, &failq);
310 spin_unlock_irqrestore(&from_mpath->frame_queue.lock, flags);
311
312 num_skbs = skb_queue_len(&failq);
313
314 while (num_skbs--) {
315 skb = __skb_dequeue(&failq);
John W. Linville817a53d2011-08-24 15:12:41 -0400316 if (copy) {
Javier Cardona5ee68e52011-08-09 16:45:08 -0700317 cp_skb = skb_copy(skb, GFP_ATOMIC);
John W. Linville817a53d2011-08-24 15:12:41 -0400318 if (cp_skb)
319 __skb_queue_tail(&failq, cp_skb);
320 }
Javier Cardona5ee68e52011-08-09 16:45:08 -0700321
322 prepare_for_gate(skb, gate_mpath->dst, gate_mpath);
323 __skb_queue_tail(&gateq, skb);
Javier Cardona5ee68e52011-08-09 16:45:08 -0700324 }
325
326 spin_lock_irqsave(&gate_mpath->frame_queue.lock, flags);
327 skb_queue_splice(&gateq, &gate_mpath->frame_queue);
328 mpath_dbg("Mpath queue for gate %pM has %d frames\n",
329 gate_mpath->dst,
330 skb_queue_len(&gate_mpath->frame_queue));
331 spin_unlock_irqrestore(&gate_mpath->frame_queue.lock, flags);
332
333 if (!copy)
334 return;
335
336 spin_lock_irqsave(&from_mpath->frame_queue.lock, flags);
337 skb_queue_splice(&failq, &from_mpath->frame_queue);
338 spin_unlock_irqrestore(&from_mpath->frame_queue.lock, flags);
339}
340
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100341
Javier Cardona239289e2011-08-29 13:23:09 -0700342static struct mesh_path *path_lookup(struct mesh_table *tbl, u8 *dst,
343 struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100344{
345 struct mesh_path *mpath;
346 struct hlist_node *n;
347 struct hlist_head *bucket;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100348 struct mpath_node *node;
349
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200350 bucket = &tbl->hash_buckets[mesh_table_hash(dst, sdata, tbl)];
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100351 hlist_for_each_entry_rcu(node, n, bucket, list) {
352 mpath = node->mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200353 if (mpath->sdata == sdata &&
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100354 memcmp(dst, mpath->dst, ETH_ALEN) == 0) {
355 if (MPATH_EXPIRED(mpath)) {
356 spin_lock_bh(&mpath->state_lock);
Javier Cardonaad99d142011-08-29 13:23:06 -0700357 mpath->flags &= ~MESH_PATH_ACTIVE;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100358 spin_unlock_bh(&mpath->state_lock);
359 }
360 return mpath;
361 }
362 }
363 return NULL;
364}
365
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100366/**
367 * mesh_path_lookup - look up a path in the mesh path table
368 * @dst: hardware address (ETH_ALEN length) of destination
369 * @sdata: local subif
370 *
371 * Returns: pointer to the mesh path structure, or NULL if not found
372 *
373 * Locking: must be called within a read rcu section.
374 */
375struct mesh_path *mesh_path_lookup(u8 *dst, struct ieee80211_sub_if_data *sdata)
376{
Javier Cardona239289e2011-08-29 13:23:09 -0700377 return path_lookup(rcu_dereference(mesh_paths), dst, sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100378}
379
YanBo79617de2008-09-22 13:30:32 +0800380struct mesh_path *mpp_path_lookup(u8 *dst, struct ieee80211_sub_if_data *sdata)
381{
Javier Cardona239289e2011-08-29 13:23:09 -0700382 return path_lookup(rcu_dereference(mpp_paths), dst, sdata);
YanBo79617de2008-09-22 13:30:32 +0800383}
384
385
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100386/**
387 * mesh_path_lookup_by_idx - look up a path in the mesh path table by its index
388 * @idx: index
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200389 * @sdata: local subif, or NULL for all entries
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100390 *
391 * Returns: pointer to the mesh path structure, or NULL if not found.
392 *
393 * Locking: must be called within a read rcu section.
394 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200395struct mesh_path *mesh_path_lookup_by_idx(int idx, struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100396{
Johannes Berg349eb8c2011-05-14 11:56:16 +0200397 struct mesh_table *tbl = rcu_dereference(mesh_paths);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100398 struct mpath_node *node;
399 struct hlist_node *p;
400 int i;
401 int j = 0;
402
Johannes Berg349eb8c2011-05-14 11:56:16 +0200403 for_each_mesh_entry(tbl, p, node, i) {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200404 if (sdata && node->mpath->sdata != sdata)
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800405 continue;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100406 if (j++ == idx) {
407 if (MPATH_EXPIRED(node->mpath)) {
408 spin_lock_bh(&node->mpath->state_lock);
Javier Cardonaad99d142011-08-29 13:23:06 -0700409 node->mpath->flags &= ~MESH_PATH_ACTIVE;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100410 spin_unlock_bh(&node->mpath->state_lock);
411 }
412 return node->mpath;
413 }
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800414 }
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100415
416 return NULL;
417}
418
Javier Cardona5ee68e52011-08-09 16:45:08 -0700419static void mesh_gate_node_reclaim(struct rcu_head *rp)
420{
421 struct mpath_node *node = container_of(rp, struct mpath_node, rcu);
422 kfree(node);
423}
424
425/**
426 * mesh_gate_add - mark mpath as path to a mesh gate and add to known_gates
427 * @mesh_tbl: table which contains known_gates list
428 * @mpath: mpath to known mesh gate
429 *
430 * Returns: 0 on success
431 *
432 */
433static int mesh_gate_add(struct mesh_table *tbl, struct mesh_path *mpath)
434{
435 struct mpath_node *gate, *new_gate;
436 struct hlist_node *n;
437 int err;
438
439 rcu_read_lock();
440 tbl = rcu_dereference(tbl);
441
442 hlist_for_each_entry_rcu(gate, n, tbl->known_gates, list)
443 if (gate->mpath == mpath) {
444 err = -EEXIST;
445 goto err_rcu;
446 }
447
448 new_gate = kzalloc(sizeof(struct mpath_node), GFP_ATOMIC);
449 if (!new_gate) {
450 err = -ENOMEM;
451 goto err_rcu;
452 }
453
454 mpath->is_gate = true;
455 mpath->sdata->u.mesh.num_gates++;
456 new_gate->mpath = mpath;
457 spin_lock_bh(&tbl->gates_lock);
458 hlist_add_head_rcu(&new_gate->list, tbl->known_gates);
459 spin_unlock_bh(&tbl->gates_lock);
460 rcu_read_unlock();
461 mpath_dbg("Mesh path (%s): Recorded new gate: %pM. %d known gates\n",
462 mpath->sdata->name, mpath->dst,
463 mpath->sdata->u.mesh.num_gates);
464 return 0;
465err_rcu:
466 rcu_read_unlock();
467 return err;
468}
469
470/**
471 * mesh_gate_del - remove a mesh gate from the list of known gates
472 * @tbl: table which holds our list of known gates
473 * @mpath: gate mpath
474 *
475 * Returns: 0 on success
476 *
477 * Locking: must be called inside rcu_read_lock() section
478 */
479static int mesh_gate_del(struct mesh_table *tbl, struct mesh_path *mpath)
480{
481 struct mpath_node *gate;
482 struct hlist_node *p, *q;
483
484 tbl = rcu_dereference(tbl);
485
486 hlist_for_each_entry_safe(gate, p, q, tbl->known_gates, list)
487 if (gate->mpath == mpath) {
488 spin_lock_bh(&tbl->gates_lock);
489 hlist_del_rcu(&gate->list);
490 call_rcu(&gate->rcu, mesh_gate_node_reclaim);
491 spin_unlock_bh(&tbl->gates_lock);
492 mpath->sdata->u.mesh.num_gates--;
493 mpath->is_gate = false;
494 mpath_dbg("Mesh path (%s): Deleted gate: %pM. "
495 "%d known gates\n", mpath->sdata->name,
496 mpath->dst, mpath->sdata->u.mesh.num_gates);
497 break;
498 }
499
500 return 0;
501}
502
503/**
504 *
505 * mesh_path_add_gate - add the given mpath to a mesh gate to our path table
506 * @mpath: gate path to add to table
507 */
508int mesh_path_add_gate(struct mesh_path *mpath)
509{
510 return mesh_gate_add(mesh_paths, mpath);
511}
512
513/**
514 * mesh_gate_num - number of gates known to this interface
515 * @sdata: subif data
516 */
517int mesh_gate_num(struct ieee80211_sub_if_data *sdata)
518{
519 return sdata->u.mesh.num_gates;
520}
521
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100522/**
523 * mesh_path_add - allocate and add a new path to the mesh path table
524 * @addr: destination address of the path (ETH_ALEN length)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200525 * @sdata: local subif
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100526 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200527 * Returns: 0 on success
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100528 *
529 * State: the initial state of the new path is set to 0
530 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200531int mesh_path_add(u8 *dst, struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100532{
Javier Cardona18889232009-08-10 12:15:52 -0700533 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
534 struct ieee80211_local *local = sdata->local;
Johannes Berg349eb8c2011-05-14 11:56:16 +0200535 struct mesh_table *tbl;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100536 struct mesh_path *mpath, *new_mpath;
537 struct mpath_node *node, *new_node;
538 struct hlist_head *bucket;
539 struct hlist_node *n;
540 int grow = 0;
541 int err = 0;
542 u32 hash_idx;
543
Johannes Berg47846c92009-11-25 17:46:19 +0100544 if (memcmp(dst, sdata->vif.addr, ETH_ALEN) == 0)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100545 /* never add ourselves as neighbours */
546 return -ENOTSUPP;
547
548 if (is_multicast_ether_addr(dst))
549 return -ENOTSUPP;
550
Johannes Berg472dbc42008-09-11 00:01:49 +0200551 if (atomic_add_unless(&sdata->u.mesh.mpaths, 1, MESH_MAX_MPATHS) == 0)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100552 return -ENOSPC;
553
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400554 err = -ENOMEM;
Javier Cardona18889232009-08-10 12:15:52 -0700555 new_mpath = kzalloc(sizeof(struct mesh_path), GFP_ATOMIC);
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400556 if (!new_mpath)
557 goto err_path_alloc;
558
Javier Cardona18889232009-08-10 12:15:52 -0700559 new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400560 if (!new_node)
561 goto err_node_alloc;
Pavel Emelyanovf84e71a2008-05-06 18:46:36 +0400562
Javier Cardona9b84b8082011-05-03 16:57:16 -0700563 read_lock_bh(&pathtbl_resize_lock);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100564 memcpy(new_mpath->dst, dst, ETH_ALEN);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200565 new_mpath->sdata = sdata;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100566 new_mpath->flags = 0;
567 skb_queue_head_init(&new_mpath->frame_queue);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100568 new_node->mpath = new_mpath;
569 new_mpath->timer.data = (unsigned long) new_mpath;
570 new_mpath->timer.function = mesh_path_timer;
571 new_mpath->exp_time = jiffies;
572 spin_lock_init(&new_mpath->state_lock);
573 init_timer(&new_mpath->timer);
574
Johannes Berg349eb8c2011-05-14 11:56:16 +0200575 tbl = resize_dereference_mesh_paths();
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100576
Johannes Berg349eb8c2011-05-14 11:56:16 +0200577 hash_idx = mesh_table_hash(dst, sdata, tbl);
578 bucket = &tbl->hash_buckets[hash_idx];
579
580 spin_lock_bh(&tbl->hashwlock[hash_idx]);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100581
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400582 err = -EEXIST;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100583 hlist_for_each_entry(node, n, bucket, list) {
584 mpath = node->mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200585 if (mpath->sdata == sdata && memcmp(dst, mpath->dst, ETH_ALEN) == 0)
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400586 goto err_exists;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100587 }
588
589 hlist_add_head_rcu(&new_node->list, bucket);
Johannes Berg349eb8c2011-05-14 11:56:16 +0200590 if (atomic_inc_return(&tbl->entries) >=
591 tbl->mean_chain_len * (tbl->hash_mask + 1))
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100592 grow = 1;
593
Johannes Bergf5ea9122009-08-07 16:17:38 +0200594 mesh_paths_generation++;
595
Johannes Berg349eb8c2011-05-14 11:56:16 +0200596 spin_unlock_bh(&tbl->hashwlock[hash_idx]);
Javier Cardona9b84b8082011-05-03 16:57:16 -0700597 read_unlock_bh(&pathtbl_resize_lock);
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400598 if (grow) {
Javier Cardona18889232009-08-10 12:15:52 -0700599 set_bit(MESH_WORK_GROW_MPATH_TABLE, &ifmsh->wrkq_flags);
Johannes Berg64592c82010-06-10 10:21:31 +0200600 ieee80211_queue_work(&local->hw, &sdata->work);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100601 }
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400602 return 0;
603
604err_exists:
Johannes Berg349eb8c2011-05-14 11:56:16 +0200605 spin_unlock_bh(&tbl->hashwlock[hash_idx]);
Javier Cardona9b84b8082011-05-03 16:57:16 -0700606 read_unlock_bh(&pathtbl_resize_lock);
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400607 kfree(new_node);
608err_node_alloc:
609 kfree(new_mpath);
610err_path_alloc:
Johannes Berg472dbc42008-09-11 00:01:49 +0200611 atomic_dec(&sdata->u.mesh.mpaths);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100612 return err;
613}
614
Johannes Berg1928eca2011-05-14 11:00:52 +0200615static void mesh_table_free_rcu(struct rcu_head *rcu)
616{
617 struct mesh_table *tbl = container_of(rcu, struct mesh_table, rcu_head);
618
619 mesh_table_free(tbl, false);
620}
621
Javier Cardona18889232009-08-10 12:15:52 -0700622void mesh_mpath_table_grow(void)
623{
624 struct mesh_table *oldtbl, *newtbl;
625
Javier Cardona9b84b8082011-05-03 16:57:16 -0700626 write_lock_bh(&pathtbl_resize_lock);
Johannes Berg349eb8c2011-05-14 11:56:16 +0200627 oldtbl = resize_dereference_mesh_paths();
628 newtbl = mesh_table_alloc(oldtbl->size_order + 1);
Johannes Berg1928eca2011-05-14 11:00:52 +0200629 if (!newtbl)
630 goto out;
Johannes Berg349eb8c2011-05-14 11:56:16 +0200631 if (mesh_table_grow(oldtbl, newtbl) < 0) {
cozybit Inca3e6b122011-04-13 11:10:28 -0700632 __mesh_table_free(newtbl);
Johannes Berg1928eca2011-05-14 11:00:52 +0200633 goto out;
Javier Cardona18889232009-08-10 12:15:52 -0700634 }
635 rcu_assign_pointer(mesh_paths, newtbl);
Javier Cardona18889232009-08-10 12:15:52 -0700636
Johannes Berg1928eca2011-05-14 11:00:52 +0200637 call_rcu(&oldtbl->rcu_head, mesh_table_free_rcu);
638
639 out:
640 write_unlock_bh(&pathtbl_resize_lock);
Javier Cardona18889232009-08-10 12:15:52 -0700641}
642
643void mesh_mpp_table_grow(void)
644{
645 struct mesh_table *oldtbl, *newtbl;
646
Javier Cardona9b84b8082011-05-03 16:57:16 -0700647 write_lock_bh(&pathtbl_resize_lock);
Johannes Berg349eb8c2011-05-14 11:56:16 +0200648 oldtbl = resize_dereference_mpp_paths();
649 newtbl = mesh_table_alloc(oldtbl->size_order + 1);
Johannes Berg1928eca2011-05-14 11:00:52 +0200650 if (!newtbl)
651 goto out;
Johannes Berg349eb8c2011-05-14 11:56:16 +0200652 if (mesh_table_grow(oldtbl, newtbl) < 0) {
cozybit Inca3e6b122011-04-13 11:10:28 -0700653 __mesh_table_free(newtbl);
Johannes Berg1928eca2011-05-14 11:00:52 +0200654 goto out;
Javier Cardona18889232009-08-10 12:15:52 -0700655 }
656 rcu_assign_pointer(mpp_paths, newtbl);
Johannes Berg1928eca2011-05-14 11:00:52 +0200657 call_rcu(&oldtbl->rcu_head, mesh_table_free_rcu);
Javier Cardona18889232009-08-10 12:15:52 -0700658
Johannes Berg1928eca2011-05-14 11:00:52 +0200659 out:
660 write_unlock_bh(&pathtbl_resize_lock);
Javier Cardona18889232009-08-10 12:15:52 -0700661}
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100662
YanBo79617de2008-09-22 13:30:32 +0800663int mpp_path_add(u8 *dst, u8 *mpp, struct ieee80211_sub_if_data *sdata)
664{
Javier Cardona18889232009-08-10 12:15:52 -0700665 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
666 struct ieee80211_local *local = sdata->local;
Johannes Berg349eb8c2011-05-14 11:56:16 +0200667 struct mesh_table *tbl;
YanBo79617de2008-09-22 13:30:32 +0800668 struct mesh_path *mpath, *new_mpath;
669 struct mpath_node *node, *new_node;
670 struct hlist_head *bucket;
671 struct hlist_node *n;
672 int grow = 0;
673 int err = 0;
674 u32 hash_idx;
675
Johannes Berg47846c92009-11-25 17:46:19 +0100676 if (memcmp(dst, sdata->vif.addr, ETH_ALEN) == 0)
YanBo79617de2008-09-22 13:30:32 +0800677 /* never add ourselves as neighbours */
678 return -ENOTSUPP;
679
680 if (is_multicast_ether_addr(dst))
681 return -ENOTSUPP;
682
683 err = -ENOMEM;
Javier Cardona18889232009-08-10 12:15:52 -0700684 new_mpath = kzalloc(sizeof(struct mesh_path), GFP_ATOMIC);
YanBo79617de2008-09-22 13:30:32 +0800685 if (!new_mpath)
686 goto err_path_alloc;
687
Javier Cardona18889232009-08-10 12:15:52 -0700688 new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
YanBo79617de2008-09-22 13:30:32 +0800689 if (!new_node)
690 goto err_node_alloc;
691
Javier Cardona9b84b8082011-05-03 16:57:16 -0700692 read_lock_bh(&pathtbl_resize_lock);
YanBo79617de2008-09-22 13:30:32 +0800693 memcpy(new_mpath->dst, dst, ETH_ALEN);
694 memcpy(new_mpath->mpp, mpp, ETH_ALEN);
695 new_mpath->sdata = sdata;
696 new_mpath->flags = 0;
697 skb_queue_head_init(&new_mpath->frame_queue);
698 new_node->mpath = new_mpath;
Thomas Pedersenc61336612011-08-25 10:36:14 -0700699 init_timer(&new_mpath->timer);
YanBo79617de2008-09-22 13:30:32 +0800700 new_mpath->exp_time = jiffies;
701 spin_lock_init(&new_mpath->state_lock);
702
Johannes Berg349eb8c2011-05-14 11:56:16 +0200703 tbl = resize_dereference_mpp_paths();
YanBo79617de2008-09-22 13:30:32 +0800704
Johannes Berg349eb8c2011-05-14 11:56:16 +0200705 hash_idx = mesh_table_hash(dst, sdata, tbl);
706 bucket = &tbl->hash_buckets[hash_idx];
707
708 spin_lock_bh(&tbl->hashwlock[hash_idx]);
YanBo79617de2008-09-22 13:30:32 +0800709
710 err = -EEXIST;
711 hlist_for_each_entry(node, n, bucket, list) {
712 mpath = node->mpath;
713 if (mpath->sdata == sdata && memcmp(dst, mpath->dst, ETH_ALEN) == 0)
714 goto err_exists;
715 }
716
717 hlist_add_head_rcu(&new_node->list, bucket);
Johannes Berg349eb8c2011-05-14 11:56:16 +0200718 if (atomic_inc_return(&tbl->entries) >=
719 tbl->mean_chain_len * (tbl->hash_mask + 1))
YanBo79617de2008-09-22 13:30:32 +0800720 grow = 1;
721
Johannes Berg349eb8c2011-05-14 11:56:16 +0200722 spin_unlock_bh(&tbl->hashwlock[hash_idx]);
Javier Cardona9b84b8082011-05-03 16:57:16 -0700723 read_unlock_bh(&pathtbl_resize_lock);
YanBo79617de2008-09-22 13:30:32 +0800724 if (grow) {
Javier Cardona18889232009-08-10 12:15:52 -0700725 set_bit(MESH_WORK_GROW_MPP_TABLE, &ifmsh->wrkq_flags);
Johannes Berg64592c82010-06-10 10:21:31 +0200726 ieee80211_queue_work(&local->hw, &sdata->work);
YanBo79617de2008-09-22 13:30:32 +0800727 }
728 return 0;
729
730err_exists:
Johannes Berg349eb8c2011-05-14 11:56:16 +0200731 spin_unlock_bh(&tbl->hashwlock[hash_idx]);
Javier Cardona9b84b8082011-05-03 16:57:16 -0700732 read_unlock_bh(&pathtbl_resize_lock);
YanBo79617de2008-09-22 13:30:32 +0800733 kfree(new_node);
734err_node_alloc:
735 kfree(new_mpath);
736err_path_alloc:
737 return err;
738}
739
740
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100741/**
742 * mesh_plink_broken - deactivates paths and sends perr when a link breaks
743 *
744 * @sta: broken peer link
745 *
746 * This function must be called from the rate control algorithm if enough
747 * delivery errors suggest that a peer link is no longer usable.
748 */
749void mesh_plink_broken(struct sta_info *sta)
750{
Johannes Berg349eb8c2011-05-14 11:56:16 +0200751 struct mesh_table *tbl;
Johannes Berg15ff6362009-11-17 13:34:04 +0100752 static const u8 bcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100753 struct mesh_path *mpath;
754 struct mpath_node *node;
755 struct hlist_node *p;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200756 struct ieee80211_sub_if_data *sdata = sta->sdata;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100757 int i;
Thomas Pedersen25d49e42011-08-11 19:35:15 -0700758 __le16 reason = cpu_to_le16(WLAN_REASON_MESH_PATH_DEST_UNREACHABLE);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100759
760 rcu_read_lock();
Johannes Berg349eb8c2011-05-14 11:56:16 +0200761 tbl = rcu_dereference(mesh_paths);
762 for_each_mesh_entry(tbl, p, node, i) {
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100763 mpath = node->mpath;
Johannes Berg349eb8c2011-05-14 11:56:16 +0200764 if (rcu_dereference(mpath->next_hop) == sta &&
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100765 mpath->flags & MESH_PATH_ACTIVE &&
766 !(mpath->flags & MESH_PATH_FIXED)) {
Javier Cardonaf5e50cd2011-08-29 13:23:05 -0700767 spin_lock_bh(&mpath->state_lock);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100768 mpath->flags &= ~MESH_PATH_ACTIVE;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000769 ++mpath->sn;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100770 spin_unlock_bh(&mpath->state_lock);
Javier Cardona45904f22010-12-03 09:20:40 +0100771 mesh_path_error_tx(sdata->u.mesh.mshcfg.element_ttl,
772 mpath->dst, cpu_to_le32(mpath->sn),
Thomas Pedersen25d49e42011-08-11 19:35:15 -0700773 reason, bcast, sdata);
Javier Cardonaf5e50cd2011-08-29 13:23:05 -0700774 }
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100775 }
776 rcu_read_unlock();
777}
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100778
Javier Cardona19c50b32011-08-29 13:23:07 -0700779static void mesh_path_node_reclaim(struct rcu_head *rp)
780{
781 struct mpath_node *node = container_of(rp, struct mpath_node, rcu);
782 struct ieee80211_sub_if_data *sdata = node->mpath->sdata;
783
784 del_timer_sync(&node->mpath->timer);
785 atomic_dec(&sdata->u.mesh.mpaths);
786 kfree(node->mpath);
787 kfree(node);
788}
789
790/* needs to be called with the corresponding hashwlock taken */
791static void __mesh_path_del(struct mesh_table *tbl, struct mpath_node *node)
792{
793 struct mesh_path *mpath;
794 mpath = node->mpath;
795 spin_lock(&mpath->state_lock);
796 mpath->flags |= MESH_PATH_RESOLVING;
797 if (mpath->is_gate)
798 mesh_gate_del(tbl, mpath);
799 hlist_del_rcu(&node->list);
800 call_rcu(&node->rcu, mesh_path_node_reclaim);
801 spin_unlock(&mpath->state_lock);
802 atomic_dec(&tbl->entries);
803}
804
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100805/**
806 * mesh_path_flush_by_nexthop - Deletes mesh paths if their next hop matches
807 *
808 * @sta - mesh peer to match
809 *
Luis Carlos Cobob4e08ea2008-02-29 15:46:08 -0800810 * RCU notes: this function is called when a mesh plink transitions from
811 * PLINK_ESTAB to any other state, since PLINK_ESTAB state is the only one that
812 * allows path creation. This will happen before the sta can be freed (because
Johannes Bergd0709a62008-02-25 16:27:46 +0100813 * sta_info_destroy() calls this) so any reader in a rcu read block will be
814 * protected against the plink disappearing.
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100815 */
816void mesh_path_flush_by_nexthop(struct sta_info *sta)
817{
Johannes Berg349eb8c2011-05-14 11:56:16 +0200818 struct mesh_table *tbl;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100819 struct mesh_path *mpath;
820 struct mpath_node *node;
821 struct hlist_node *p;
822 int i;
823
Johannes Berg349eb8c2011-05-14 11:56:16 +0200824 rcu_read_lock();
Javier Cardona239289e2011-08-29 13:23:09 -0700825 read_lock_bh(&pathtbl_resize_lock);
826 tbl = resize_dereference_mesh_paths();
Johannes Berg349eb8c2011-05-14 11:56:16 +0200827 for_each_mesh_entry(tbl, p, node, i) {
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100828 mpath = node->mpath;
Javier Cardonacd72e812011-08-29 13:23:08 -0700829 if (rcu_dereference(mpath->next_hop) == sta) {
Javier Cardona19c50b32011-08-29 13:23:07 -0700830 spin_lock_bh(&tbl->hashwlock[i]);
831 __mesh_path_del(tbl, node);
832 spin_unlock_bh(&tbl->hashwlock[i]);
833 }
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100834 }
Javier Cardona239289e2011-08-29 13:23:09 -0700835 read_unlock_bh(&pathtbl_resize_lock);
Johannes Berg349eb8c2011-05-14 11:56:16 +0200836 rcu_read_unlock();
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100837}
838
Javier Cardonacd72e812011-08-29 13:23:08 -0700839static void table_flush_by_iface(struct mesh_table *tbl,
840 struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100841{
842 struct mesh_path *mpath;
843 struct mpath_node *node;
844 struct hlist_node *p;
845 int i;
846
Javier Cardonacd72e812011-08-29 13:23:08 -0700847 WARN_ON(!rcu_read_lock_held());
Johannes Berg349eb8c2011-05-14 11:56:16 +0200848 for_each_mesh_entry(tbl, p, node, i) {
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100849 mpath = node->mpath;
Javier Cardonacd72e812011-08-29 13:23:08 -0700850 if (mpath->sdata != sdata)
851 continue;
Javier Cardonaece1a2e2011-08-29 13:23:04 -0700852 spin_lock_bh(&tbl->hashwlock[i]);
Javier Cardona19c50b32011-08-29 13:23:07 -0700853 __mesh_path_del(tbl, node);
Javier Cardonaece1a2e2011-08-29 13:23:04 -0700854 spin_unlock_bh(&tbl->hashwlock[i]);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100855 }
856}
857
Javier Cardonaece1a2e2011-08-29 13:23:04 -0700858/**
859 * mesh_path_flush_by_iface - Deletes all mesh paths associated with a given iface
860 *
861 * This function deletes both mesh paths as well as mesh portal paths.
862 *
863 * @sdata - interface data to match
864 *
865 */
866void mesh_path_flush_by_iface(struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100867{
Javier Cardonacd72e812011-08-29 13:23:08 -0700868 struct mesh_table *tbl;
Johannes Bergd0709a62008-02-25 16:27:46 +0100869
Javier Cardonacd72e812011-08-29 13:23:08 -0700870 rcu_read_lock();
Javier Cardona239289e2011-08-29 13:23:09 -0700871 read_lock_bh(&pathtbl_resize_lock);
872 tbl = resize_dereference_mesh_paths();
Javier Cardonacd72e812011-08-29 13:23:08 -0700873 table_flush_by_iface(tbl, sdata);
Javier Cardona239289e2011-08-29 13:23:09 -0700874 tbl = resize_dereference_mpp_paths();
Javier Cardonacd72e812011-08-29 13:23:08 -0700875 table_flush_by_iface(tbl, sdata);
Javier Cardona239289e2011-08-29 13:23:09 -0700876 read_unlock_bh(&pathtbl_resize_lock);
Javier Cardonacd72e812011-08-29 13:23:08 -0700877 rcu_read_unlock();
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100878}
879
880/**
881 * mesh_path_del - delete a mesh path from the table
882 *
883 * @addr: dst address (ETH_ALEN length)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200884 * @sdata: local subif
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100885 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200886 * Returns: 0 if successful
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100887 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200888int mesh_path_del(u8 *addr, struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100889{
Johannes Berg349eb8c2011-05-14 11:56:16 +0200890 struct mesh_table *tbl;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100891 struct mesh_path *mpath;
892 struct mpath_node *node;
893 struct hlist_head *bucket;
894 struct hlist_node *n;
895 int hash_idx;
896 int err = 0;
897
Javier Cardona9b84b8082011-05-03 16:57:16 -0700898 read_lock_bh(&pathtbl_resize_lock);
Johannes Berg349eb8c2011-05-14 11:56:16 +0200899 tbl = resize_dereference_mesh_paths();
900 hash_idx = mesh_table_hash(addr, sdata, tbl);
901 bucket = &tbl->hash_buckets[hash_idx];
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100902
Johannes Berg349eb8c2011-05-14 11:56:16 +0200903 spin_lock_bh(&tbl->hashwlock[hash_idx]);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100904 hlist_for_each_entry(node, n, bucket, list) {
905 mpath = node->mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200906 if (mpath->sdata == sdata &&
Johannes Berg349eb8c2011-05-14 11:56:16 +0200907 memcmp(addr, mpath->dst, ETH_ALEN) == 0) {
Javier Cardona19c50b32011-08-29 13:23:07 -0700908 __mesh_path_del(tbl, node);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100909 goto enddel;
910 }
911 }
912
913 err = -ENXIO;
914enddel:
Johannes Bergf5ea9122009-08-07 16:17:38 +0200915 mesh_paths_generation++;
Johannes Berg349eb8c2011-05-14 11:56:16 +0200916 spin_unlock_bh(&tbl->hashwlock[hash_idx]);
Javier Cardona9b84b8082011-05-03 16:57:16 -0700917 read_unlock_bh(&pathtbl_resize_lock);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100918 return err;
919}
920
921/**
922 * mesh_path_tx_pending - sends pending frames in a mesh path queue
923 *
924 * @mpath: mesh path to activate
925 *
926 * Locking: the state_lock of the mpath structure must NOT be held when calling
927 * this function.
928 */
929void mesh_path_tx_pending(struct mesh_path *mpath)
930{
Javier Cardona249b4052009-07-07 10:55:03 -0700931 if (mpath->flags & MESH_PATH_ACTIVE)
932 ieee80211_add_pending_skbs(mpath->sdata->local,
933 &mpath->frame_queue);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100934}
935
936/**
Javier Cardona5ee68e52011-08-09 16:45:08 -0700937 * mesh_path_send_to_gates - sends pending frames to all known mesh gates
938 *
939 * @mpath: mesh path whose queue will be emptied
940 *
941 * If there is only one gate, the frames are transferred from the failed mpath
942 * queue to that gate's queue. If there are more than one gates, the frames
943 * are copied from each gate to the next. After frames are copied, the
944 * mpath queues are emptied onto the transmission queue.
945 */
946int mesh_path_send_to_gates(struct mesh_path *mpath)
947{
948 struct ieee80211_sub_if_data *sdata = mpath->sdata;
949 struct hlist_node *n;
950 struct mesh_table *tbl;
951 struct mesh_path *from_mpath = mpath;
952 struct mpath_node *gate = NULL;
953 bool copy = false;
954 struct hlist_head *known_gates;
955
956 rcu_read_lock();
957 tbl = rcu_dereference(mesh_paths);
958 known_gates = tbl->known_gates;
959 rcu_read_unlock();
960
961 if (!known_gates)
962 return -EHOSTUNREACH;
963
964 hlist_for_each_entry_rcu(gate, n, known_gates, list) {
965 if (gate->mpath->sdata != sdata)
966 continue;
967
968 if (gate->mpath->flags & MESH_PATH_ACTIVE) {
969 mpath_dbg("Forwarding to %pM\n", gate->mpath->dst);
970 mesh_path_move_to_queue(gate->mpath, from_mpath, copy);
971 from_mpath = gate->mpath;
972 copy = true;
973 } else {
974 mpath_dbg("Not forwarding %p\n", gate->mpath);
975 mpath_dbg("flags %x\n", gate->mpath->flags);
976 }
977 }
978
979 hlist_for_each_entry_rcu(gate, n, known_gates, list)
980 if (gate->mpath->sdata == sdata) {
981 mpath_dbg("Sending to %pM\n", gate->mpath->dst);
982 mesh_path_tx_pending(gate->mpath);
983 }
984
985 return (from_mpath == mpath) ? -EHOSTUNREACH : 0;
986}
987
988/**
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100989 * mesh_path_discard_frame - discard a frame whose path could not be resolved
990 *
991 * @skb: frame to discard
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200992 * @sdata: network subif the frame was to be sent through
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100993 *
Javier Cardona35946a52009-07-13 17:00:10 -0700994 * If the frame was being forwarded from another MP, a PERR frame will be sent
995 * to the precursor. The precursor's address (i.e. the previous hop) was saved
996 * in addr1 of the frame-to-be-forwarded, and would only be overwritten once
997 * the destination is successfully resolved.
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100998 *
999 * Locking: the function must me called within a rcu_read_lock region
1000 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001001void mesh_path_discard_frame(struct sk_buff *skb,
1002 struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001003{
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001004 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001005 struct mesh_path *mpath;
Rui Paulod19b3bf2009-11-09 23:46:55 +00001006 u32 sn = 0;
Thomas Pedersen25d49e42011-08-11 19:35:15 -07001007 __le16 reason = cpu_to_le16(WLAN_REASON_MESH_PATH_NOFORWARD);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001008
Johannes Berg47846c92009-11-25 17:46:19 +01001009 if (memcmp(hdr->addr4, sdata->vif.addr, ETH_ALEN) != 0) {
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001010 u8 *ra, *da;
1011
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001012 da = hdr->addr3;
Javier Cardona35946a52009-07-13 17:00:10 -07001013 ra = hdr->addr1;
Javier Cardonaaf089c12011-08-29 13:23:03 -07001014 rcu_read_lock();
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001015 mpath = mesh_path_lookup(da, sdata);
Javier Cardonaaf089c12011-08-29 13:23:03 -07001016 if (mpath) {
1017 spin_lock_bh(&mpath->state_lock);
Rui Paulod19b3bf2009-11-09 23:46:55 +00001018 sn = ++mpath->sn;
Javier Cardonaaf089c12011-08-29 13:23:03 -07001019 spin_unlock_bh(&mpath->state_lock);
1020 }
1021 rcu_read_unlock();
Javier Cardona45904f22010-12-03 09:20:40 +01001022 mesh_path_error_tx(sdata->u.mesh.mshcfg.element_ttl, skb->data,
Thomas Pedersen25d49e42011-08-11 19:35:15 -07001023 cpu_to_le32(sn), reason, ra, sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001024 }
1025
1026 kfree_skb(skb);
Johannes Berg472dbc42008-09-11 00:01:49 +02001027 sdata->u.mesh.mshstats.dropped_frames_no_route++;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001028}
1029
1030/**
1031 * mesh_path_flush_pending - free the pending queue of a mesh path
1032 *
1033 * @mpath: mesh path whose queue has to be freed
1034 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001035 * Locking: the function must me called within a rcu_read_lock region
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001036 */
1037void mesh_path_flush_pending(struct mesh_path *mpath)
1038{
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001039 struct sk_buff *skb;
1040
Javier Cardona00e3f252011-08-09 16:45:07 -07001041 while ((skb = skb_dequeue(&mpath->frame_queue)) != NULL)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001042 mesh_path_discard_frame(skb, mpath->sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001043}
1044
1045/**
1046 * mesh_path_fix_nexthop - force a specific next hop for a mesh path
1047 *
1048 * @mpath: the mesh path to modify
1049 * @next_hop: the next hop to force
1050 *
1051 * Locking: this function must be called holding mpath->state_lock
1052 */
1053void mesh_path_fix_nexthop(struct mesh_path *mpath, struct sta_info *next_hop)
1054{
1055 spin_lock_bh(&mpath->state_lock);
1056 mesh_path_assign_nexthop(mpath, next_hop);
Rui Paulod19b3bf2009-11-09 23:46:55 +00001057 mpath->sn = 0xffff;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001058 mpath->metric = 0;
1059 mpath->hop_count = 0;
1060 mpath->exp_time = 0;
1061 mpath->flags |= MESH_PATH_FIXED;
1062 mesh_path_activate(mpath);
1063 spin_unlock_bh(&mpath->state_lock);
1064 mesh_path_tx_pending(mpath);
1065}
1066
1067static void mesh_path_node_free(struct hlist_node *p, bool free_leafs)
1068{
1069 struct mesh_path *mpath;
1070 struct mpath_node *node = hlist_entry(p, struct mpath_node, list);
1071 mpath = node->mpath;
1072 hlist_del_rcu(p);
Javier Cardonad0df9ee2011-05-13 14:16:07 -07001073 if (free_leafs) {
1074 del_timer_sync(&mpath->timer);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001075 kfree(mpath);
Javier Cardonad0df9ee2011-05-13 14:16:07 -07001076 }
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001077 kfree(node);
1078}
1079
Pavel Emelyanov4caf86c2008-05-07 19:47:01 +04001080static int mesh_path_node_copy(struct hlist_node *p, struct mesh_table *newtbl)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001081{
1082 struct mesh_path *mpath;
1083 struct mpath_node *node, *new_node;
1084 u32 hash_idx;
1085
Pavel Emelyanov8566dc32008-05-07 19:51:51 +04001086 new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
Pavel Emelyanov00242c42008-05-07 19:48:14 +04001087 if (new_node == NULL)
1088 return -ENOMEM;
1089
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001090 node = hlist_entry(p, struct mpath_node, list);
1091 mpath = node->mpath;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001092 new_node->mpath = mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001093 hash_idx = mesh_table_hash(mpath->dst, mpath->sdata, newtbl);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001094 hlist_add_head(&new_node->list,
1095 &newtbl->hash_buckets[hash_idx]);
Pavel Emelyanov4caf86c2008-05-07 19:47:01 +04001096 return 0;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001097}
1098
1099int mesh_pathtbl_init(void)
1100{
Johannes Berg349eb8c2011-05-14 11:56:16 +02001101 struct mesh_table *tbl_path, *tbl_mpp;
Dan Carpenter4c5ade42011-08-30 22:16:15 +03001102 int ret;
YanBo79617de2008-09-22 13:30:32 +08001103
Johannes Berg349eb8c2011-05-14 11:56:16 +02001104 tbl_path = mesh_table_alloc(INIT_PATHS_SIZE_ORDER);
1105 if (!tbl_path)
1106 return -ENOMEM;
1107 tbl_path->free_node = &mesh_path_node_free;
1108 tbl_path->copy_node = &mesh_path_node_copy;
1109 tbl_path->mean_chain_len = MEAN_CHAIN_LEN;
Javier Cardona5ee68e52011-08-09 16:45:08 -07001110 tbl_path->known_gates = kzalloc(sizeof(struct hlist_head), GFP_ATOMIC);
Dan Carpenter4c5ade42011-08-30 22:16:15 +03001111 if (!tbl_path->known_gates) {
1112 ret = -ENOMEM;
1113 goto free_path;
1114 }
Javier Cardona5ee68e52011-08-09 16:45:08 -07001115 INIT_HLIST_HEAD(tbl_path->known_gates);
1116
Johannes Berg349eb8c2011-05-14 11:56:16 +02001117
1118 tbl_mpp = mesh_table_alloc(INIT_PATHS_SIZE_ORDER);
1119 if (!tbl_mpp) {
Dan Carpenter4c5ade42011-08-30 22:16:15 +03001120 ret = -ENOMEM;
1121 goto free_path;
YanBo79617de2008-09-22 13:30:32 +08001122 }
Johannes Berg349eb8c2011-05-14 11:56:16 +02001123 tbl_mpp->free_node = &mesh_path_node_free;
1124 tbl_mpp->copy_node = &mesh_path_node_copy;
1125 tbl_mpp->mean_chain_len = MEAN_CHAIN_LEN;
Javier Cardona5ee68e52011-08-09 16:45:08 -07001126 tbl_mpp->known_gates = kzalloc(sizeof(struct hlist_head), GFP_ATOMIC);
Dan Carpenter4c5ade42011-08-30 22:16:15 +03001127 if (!tbl_mpp->known_gates) {
1128 ret = -ENOMEM;
1129 goto free_mpp;
1130 }
Javier Cardona5ee68e52011-08-09 16:45:08 -07001131 INIT_HLIST_HEAD(tbl_mpp->known_gates);
Johannes Berg349eb8c2011-05-14 11:56:16 +02001132
1133 /* Need no locking since this is during init */
1134 RCU_INIT_POINTER(mesh_paths, tbl_path);
1135 RCU_INIT_POINTER(mpp_paths, tbl_mpp);
YanBo79617de2008-09-22 13:30:32 +08001136
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001137 return 0;
Dan Carpenter4c5ade42011-08-30 22:16:15 +03001138
1139free_mpp:
1140 mesh_table_free(tbl_mpp, true);
1141free_path:
1142 mesh_table_free(tbl_path, true);
1143 return ret;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001144}
1145
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001146void mesh_path_expire(struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001147{
Johannes Berg349eb8c2011-05-14 11:56:16 +02001148 struct mesh_table *tbl;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001149 struct mesh_path *mpath;
1150 struct mpath_node *node;
1151 struct hlist_node *p;
1152 int i;
1153
Johannes Berg349eb8c2011-05-14 11:56:16 +02001154 rcu_read_lock();
1155 tbl = rcu_dereference(mesh_paths);
1156 for_each_mesh_entry(tbl, p, node, i) {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001157 if (node->mpath->sdata != sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001158 continue;
1159 mpath = node->mpath;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001160 if ((!(mpath->flags & MESH_PATH_RESOLVING)) &&
1161 (!(mpath->flags & MESH_PATH_FIXED)) &&
Javier Cardonaf5e50cd2011-08-29 13:23:05 -07001162 time_after(jiffies, mpath->exp_time + MESH_PATH_EXPIRE))
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001163 mesh_path_del(mpath->dst, mpath->sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001164 }
Johannes Berg349eb8c2011-05-14 11:56:16 +02001165 rcu_read_unlock();
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001166}
1167
1168void mesh_pathtbl_unregister(void)
1169{
Johannes Berg349eb8c2011-05-14 11:56:16 +02001170 /* no need for locking during exit path */
Eric Dumazet33d480c2011-08-11 19:30:52 +00001171 mesh_table_free(rcu_dereference_protected(mesh_paths, 1), true);
1172 mesh_table_free(rcu_dereference_protected(mpp_paths, 1), true);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001173}