blob: 7bd2a76aef0ec7b56cb0117ff6b5a06feecfcb54 [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);
224 __skb_queue_tail(&tmpq, skb);
225 }
226
227 skb_queue_splice(&tmpq, &mpath->frame_queue);
228 spin_unlock_irqrestore(&mpath->frame_queue.lock, flags);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100229}
230
Javier Cardona5ee68e52011-08-09 16:45:08 -0700231static void prepare_for_gate(struct sk_buff *skb, char *dst_addr,
232 struct mesh_path *gate_mpath)
233{
234 struct ieee80211_hdr *hdr;
235 struct ieee80211s_hdr *mshdr;
236 int mesh_hdrlen, hdrlen;
237 char *next_hop;
238
239 hdr = (struct ieee80211_hdr *) skb->data;
240 hdrlen = ieee80211_hdrlen(hdr->frame_control);
241 mshdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
242
243 if (!(mshdr->flags & MESH_FLAGS_AE)) {
244 /* size of the fixed part of the mesh header */
245 mesh_hdrlen = 6;
246
247 /* make room for the two extended addresses */
248 skb_push(skb, 2 * ETH_ALEN);
249 memmove(skb->data, hdr, hdrlen + mesh_hdrlen);
250
251 hdr = (struct ieee80211_hdr *) skb->data;
252
253 /* we preserve the previous mesh header and only add
254 * the new addreses */
255 mshdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
256 mshdr->flags = MESH_FLAGS_AE_A5_A6;
257 memcpy(mshdr->eaddr1, hdr->addr3, ETH_ALEN);
258 memcpy(mshdr->eaddr2, hdr->addr4, ETH_ALEN);
259 }
260
261 /* update next hop */
262 hdr = (struct ieee80211_hdr *) skb->data;
263 rcu_read_lock();
264 next_hop = rcu_dereference(gate_mpath->next_hop)->sta.addr;
265 memcpy(hdr->addr1, next_hop, ETH_ALEN);
266 rcu_read_unlock();
267 memcpy(hdr->addr3, dst_addr, ETH_ALEN);
268}
269
270/**
271 *
272 * mesh_path_move_to_queue - Move or copy frames from one mpath queue to another
273 *
274 * This function is used to transfer or copy frames from an unresolved mpath to
275 * a gate mpath. The function also adds the Address Extension field and
276 * updates the next hop.
277 *
278 * If a frame already has an Address Extension field, only the next hop and
279 * destination addresses are updated.
280 *
281 * The gate mpath must be an active mpath with a valid mpath->next_hop.
282 *
283 * @mpath: An active mpath the frames will be sent to (i.e. the gate)
284 * @from_mpath: The failed mpath
285 * @copy: When true, copy all the frames to the new mpath queue. When false,
286 * move them.
287 */
288static void mesh_path_move_to_queue(struct mesh_path *gate_mpath,
289 struct mesh_path *from_mpath,
290 bool copy)
291{
Thomas Pedersenc6133662011-08-25 10:36:14 -0700292 struct sk_buff *skb, *cp_skb = NULL;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700293 struct sk_buff_head gateq, failq;
294 unsigned long flags;
295 int num_skbs;
296
297 BUG_ON(gate_mpath == from_mpath);
298 BUG_ON(!gate_mpath->next_hop);
299
300 __skb_queue_head_init(&gateq);
301 __skb_queue_head_init(&failq);
302
303 spin_lock_irqsave(&from_mpath->frame_queue.lock, flags);
304 skb_queue_splice_init(&from_mpath->frame_queue, &failq);
305 spin_unlock_irqrestore(&from_mpath->frame_queue.lock, flags);
306
307 num_skbs = skb_queue_len(&failq);
308
309 while (num_skbs--) {
310 skb = __skb_dequeue(&failq);
John W. Linville817a53d2011-08-24 15:12:41 -0400311 if (copy) {
Javier Cardona5ee68e52011-08-09 16:45:08 -0700312 cp_skb = skb_copy(skb, GFP_ATOMIC);
John W. Linville817a53d2011-08-24 15:12:41 -0400313 if (cp_skb)
314 __skb_queue_tail(&failq, cp_skb);
315 }
Javier Cardona5ee68e52011-08-09 16:45:08 -0700316
317 prepare_for_gate(skb, gate_mpath->dst, gate_mpath);
318 __skb_queue_tail(&gateq, skb);
Javier Cardona5ee68e52011-08-09 16:45:08 -0700319 }
320
321 spin_lock_irqsave(&gate_mpath->frame_queue.lock, flags);
322 skb_queue_splice(&gateq, &gate_mpath->frame_queue);
323 mpath_dbg("Mpath queue for gate %pM has %d frames\n",
324 gate_mpath->dst,
325 skb_queue_len(&gate_mpath->frame_queue));
326 spin_unlock_irqrestore(&gate_mpath->frame_queue.lock, flags);
327
328 if (!copy)
329 return;
330
331 spin_lock_irqsave(&from_mpath->frame_queue.lock, flags);
332 skb_queue_splice(&failq, &from_mpath->frame_queue);
333 spin_unlock_irqrestore(&from_mpath->frame_queue.lock, flags);
334}
335
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100336
Javier Cardona239289e2011-08-29 13:23:09 -0700337static struct mesh_path *path_lookup(struct mesh_table *tbl, u8 *dst,
338 struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100339{
340 struct mesh_path *mpath;
341 struct hlist_node *n;
342 struct hlist_head *bucket;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100343 struct mpath_node *node;
344
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200345 bucket = &tbl->hash_buckets[mesh_table_hash(dst, sdata, tbl)];
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100346 hlist_for_each_entry_rcu(node, n, bucket, list) {
347 mpath = node->mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200348 if (mpath->sdata == sdata &&
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100349 memcmp(dst, mpath->dst, ETH_ALEN) == 0) {
350 if (MPATH_EXPIRED(mpath)) {
351 spin_lock_bh(&mpath->state_lock);
Javier Cardonaad99d142011-08-29 13:23:06 -0700352 mpath->flags &= ~MESH_PATH_ACTIVE;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100353 spin_unlock_bh(&mpath->state_lock);
354 }
355 return mpath;
356 }
357 }
358 return NULL;
359}
360
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100361/**
362 * mesh_path_lookup - look up a path in the mesh path table
363 * @dst: hardware address (ETH_ALEN length) of destination
364 * @sdata: local subif
365 *
366 * Returns: pointer to the mesh path structure, or NULL if not found
367 *
368 * Locking: must be called within a read rcu section.
369 */
370struct mesh_path *mesh_path_lookup(u8 *dst, struct ieee80211_sub_if_data *sdata)
371{
Javier Cardona239289e2011-08-29 13:23:09 -0700372 return path_lookup(rcu_dereference(mesh_paths), dst, sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100373}
374
YanBo79617de2008-09-22 13:30:32 +0800375struct mesh_path *mpp_path_lookup(u8 *dst, struct ieee80211_sub_if_data *sdata)
376{
Javier Cardona239289e2011-08-29 13:23:09 -0700377 return path_lookup(rcu_dereference(mpp_paths), dst, sdata);
YanBo79617de2008-09-22 13:30:32 +0800378}
379
380
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100381/**
382 * mesh_path_lookup_by_idx - look up a path in the mesh path table by its index
383 * @idx: index
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200384 * @sdata: local subif, or NULL for all entries
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100385 *
386 * Returns: pointer to the mesh path structure, or NULL if not found.
387 *
388 * Locking: must be called within a read rcu section.
389 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200390struct mesh_path *mesh_path_lookup_by_idx(int idx, struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100391{
Johannes Berg349eb8c2011-05-14 11:56:16 +0200392 struct mesh_table *tbl = rcu_dereference(mesh_paths);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100393 struct mpath_node *node;
394 struct hlist_node *p;
395 int i;
396 int j = 0;
397
Johannes Berg349eb8c2011-05-14 11:56:16 +0200398 for_each_mesh_entry(tbl, p, node, i) {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200399 if (sdata && node->mpath->sdata != sdata)
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800400 continue;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100401 if (j++ == idx) {
402 if (MPATH_EXPIRED(node->mpath)) {
403 spin_lock_bh(&node->mpath->state_lock);
Javier Cardonaad99d142011-08-29 13:23:06 -0700404 node->mpath->flags &= ~MESH_PATH_ACTIVE;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100405 spin_unlock_bh(&node->mpath->state_lock);
406 }
407 return node->mpath;
408 }
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800409 }
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100410
411 return NULL;
412}
413
Javier Cardona5ee68e52011-08-09 16:45:08 -0700414static void mesh_gate_node_reclaim(struct rcu_head *rp)
415{
416 struct mpath_node *node = container_of(rp, struct mpath_node, rcu);
417 kfree(node);
418}
419
420/**
Johannes Berg30be52e2011-11-21 11:23:50 +0100421 * mesh_path_add_gate - add the given mpath to a mesh gate to our path table
422 * @mpath: gate path to add to table
Javier Cardona5ee68e52011-08-09 16:45:08 -0700423 */
Johannes Berg30be52e2011-11-21 11:23:50 +0100424int mesh_path_add_gate(struct mesh_path *mpath)
Javier Cardona5ee68e52011-08-09 16:45:08 -0700425{
Johannes Berg30be52e2011-11-21 11:23:50 +0100426 struct mesh_table *tbl;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700427 struct mpath_node *gate, *new_gate;
428 struct hlist_node *n;
429 int err;
430
431 rcu_read_lock();
Johannes Berg30be52e2011-11-21 11:23:50 +0100432 tbl = rcu_dereference(mesh_paths);
Javier Cardona5ee68e52011-08-09 16:45:08 -0700433
434 hlist_for_each_entry_rcu(gate, n, tbl->known_gates, list)
435 if (gate->mpath == mpath) {
436 err = -EEXIST;
437 goto err_rcu;
438 }
439
440 new_gate = kzalloc(sizeof(struct mpath_node), GFP_ATOMIC);
441 if (!new_gate) {
442 err = -ENOMEM;
443 goto err_rcu;
444 }
445
446 mpath->is_gate = true;
447 mpath->sdata->u.mesh.num_gates++;
448 new_gate->mpath = mpath;
449 spin_lock_bh(&tbl->gates_lock);
450 hlist_add_head_rcu(&new_gate->list, tbl->known_gates);
451 spin_unlock_bh(&tbl->gates_lock);
452 rcu_read_unlock();
453 mpath_dbg("Mesh path (%s): Recorded new gate: %pM. %d known gates\n",
454 mpath->sdata->name, mpath->dst,
455 mpath->sdata->u.mesh.num_gates);
456 return 0;
457err_rcu:
458 rcu_read_unlock();
459 return err;
460}
461
462/**
463 * mesh_gate_del - remove a mesh gate from the list of known gates
464 * @tbl: table which holds our list of known gates
465 * @mpath: gate mpath
466 *
467 * Returns: 0 on success
468 *
469 * Locking: must be called inside rcu_read_lock() section
470 */
471static int mesh_gate_del(struct mesh_table *tbl, struct mesh_path *mpath)
472{
473 struct mpath_node *gate;
474 struct hlist_node *p, *q;
475
Javier Cardona5ee68e52011-08-09 16:45:08 -0700476 hlist_for_each_entry_safe(gate, p, q, tbl->known_gates, list)
477 if (gate->mpath == mpath) {
478 spin_lock_bh(&tbl->gates_lock);
479 hlist_del_rcu(&gate->list);
480 call_rcu(&gate->rcu, mesh_gate_node_reclaim);
481 spin_unlock_bh(&tbl->gates_lock);
482 mpath->sdata->u.mesh.num_gates--;
483 mpath->is_gate = false;
484 mpath_dbg("Mesh path (%s): Deleted gate: %pM. "
485 "%d known gates\n", mpath->sdata->name,
486 mpath->dst, mpath->sdata->u.mesh.num_gates);
487 break;
488 }
489
490 return 0;
491}
492
493/**
Javier Cardona5ee68e52011-08-09 16:45:08 -0700494 * mesh_gate_num - number of gates known to this interface
495 * @sdata: subif data
496 */
497int mesh_gate_num(struct ieee80211_sub_if_data *sdata)
498{
499 return sdata->u.mesh.num_gates;
500}
501
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100502/**
503 * mesh_path_add - allocate and add a new path to the mesh path table
504 * @addr: destination address of the path (ETH_ALEN length)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200505 * @sdata: local subif
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100506 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200507 * Returns: 0 on success
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100508 *
509 * State: the initial state of the new path is set to 0
510 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200511int mesh_path_add(u8 *dst, struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100512{
Javier Cardona18889232009-08-10 12:15:52 -0700513 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
514 struct ieee80211_local *local = sdata->local;
Johannes Berg349eb8c2011-05-14 11:56:16 +0200515 struct mesh_table *tbl;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100516 struct mesh_path *mpath, *new_mpath;
517 struct mpath_node *node, *new_node;
518 struct hlist_head *bucket;
519 struct hlist_node *n;
520 int grow = 0;
521 int err = 0;
522 u32 hash_idx;
523
Johannes Berg47846c92009-11-25 17:46:19 +0100524 if (memcmp(dst, sdata->vif.addr, ETH_ALEN) == 0)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100525 /* never add ourselves as neighbours */
526 return -ENOTSUPP;
527
528 if (is_multicast_ether_addr(dst))
529 return -ENOTSUPP;
530
Johannes Berg472dbc42008-09-11 00:01:49 +0200531 if (atomic_add_unless(&sdata->u.mesh.mpaths, 1, MESH_MAX_MPATHS) == 0)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100532 return -ENOSPC;
533
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400534 err = -ENOMEM;
Javier Cardona18889232009-08-10 12:15:52 -0700535 new_mpath = kzalloc(sizeof(struct mesh_path), GFP_ATOMIC);
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400536 if (!new_mpath)
537 goto err_path_alloc;
538
Javier Cardona18889232009-08-10 12:15:52 -0700539 new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400540 if (!new_node)
541 goto err_node_alloc;
Pavel Emelyanovf84e71a2008-05-06 18:46:36 +0400542
Javier Cardona9b84b8082011-05-03 16:57:16 -0700543 read_lock_bh(&pathtbl_resize_lock);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100544 memcpy(new_mpath->dst, dst, ETH_ALEN);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200545 new_mpath->sdata = sdata;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100546 new_mpath->flags = 0;
547 skb_queue_head_init(&new_mpath->frame_queue);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100548 new_node->mpath = new_mpath;
549 new_mpath->timer.data = (unsigned long) new_mpath;
550 new_mpath->timer.function = mesh_path_timer;
551 new_mpath->exp_time = jiffies;
552 spin_lock_init(&new_mpath->state_lock);
553 init_timer(&new_mpath->timer);
554
Johannes Berg349eb8c2011-05-14 11:56:16 +0200555 tbl = resize_dereference_mesh_paths();
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100556
Johannes Berg349eb8c2011-05-14 11:56:16 +0200557 hash_idx = mesh_table_hash(dst, sdata, tbl);
558 bucket = &tbl->hash_buckets[hash_idx];
559
560 spin_lock_bh(&tbl->hashwlock[hash_idx]);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100561
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400562 err = -EEXIST;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100563 hlist_for_each_entry(node, n, bucket, list) {
564 mpath = node->mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200565 if (mpath->sdata == sdata && memcmp(dst, mpath->dst, ETH_ALEN) == 0)
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400566 goto err_exists;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100567 }
568
569 hlist_add_head_rcu(&new_node->list, bucket);
Johannes Berg349eb8c2011-05-14 11:56:16 +0200570 if (atomic_inc_return(&tbl->entries) >=
571 tbl->mean_chain_len * (tbl->hash_mask + 1))
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100572 grow = 1;
573
Johannes Bergf5ea9122009-08-07 16:17:38 +0200574 mesh_paths_generation++;
575
Johannes Berg349eb8c2011-05-14 11:56:16 +0200576 spin_unlock_bh(&tbl->hashwlock[hash_idx]);
Javier Cardona9b84b8082011-05-03 16:57:16 -0700577 read_unlock_bh(&pathtbl_resize_lock);
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400578 if (grow) {
Javier Cardona18889232009-08-10 12:15:52 -0700579 set_bit(MESH_WORK_GROW_MPATH_TABLE, &ifmsh->wrkq_flags);
Johannes Berg64592c82010-06-10 10:21:31 +0200580 ieee80211_queue_work(&local->hw, &sdata->work);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100581 }
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400582 return 0;
583
584err_exists:
Johannes Berg349eb8c2011-05-14 11:56:16 +0200585 spin_unlock_bh(&tbl->hashwlock[hash_idx]);
Javier Cardona9b84b8082011-05-03 16:57:16 -0700586 read_unlock_bh(&pathtbl_resize_lock);
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400587 kfree(new_node);
588err_node_alloc:
589 kfree(new_mpath);
590err_path_alloc:
Johannes Berg472dbc42008-09-11 00:01:49 +0200591 atomic_dec(&sdata->u.mesh.mpaths);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100592 return err;
593}
594
Johannes Berg1928eca2011-05-14 11:00:52 +0200595static void mesh_table_free_rcu(struct rcu_head *rcu)
596{
597 struct mesh_table *tbl = container_of(rcu, struct mesh_table, rcu_head);
598
599 mesh_table_free(tbl, false);
600}
601
Javier Cardona18889232009-08-10 12:15:52 -0700602void mesh_mpath_table_grow(void)
603{
604 struct mesh_table *oldtbl, *newtbl;
605
Javier Cardona9b84b8082011-05-03 16:57:16 -0700606 write_lock_bh(&pathtbl_resize_lock);
Johannes Berg349eb8c2011-05-14 11:56:16 +0200607 oldtbl = resize_dereference_mesh_paths();
608 newtbl = mesh_table_alloc(oldtbl->size_order + 1);
Johannes Berg1928eca2011-05-14 11:00:52 +0200609 if (!newtbl)
610 goto out;
Johannes Berg349eb8c2011-05-14 11:56:16 +0200611 if (mesh_table_grow(oldtbl, newtbl) < 0) {
cozybit Inca3e6b122011-04-13 11:10:28 -0700612 __mesh_table_free(newtbl);
Johannes Berg1928eca2011-05-14 11:00:52 +0200613 goto out;
Javier Cardona18889232009-08-10 12:15:52 -0700614 }
615 rcu_assign_pointer(mesh_paths, newtbl);
Javier Cardona18889232009-08-10 12:15:52 -0700616
Johannes Berg1928eca2011-05-14 11:00:52 +0200617 call_rcu(&oldtbl->rcu_head, mesh_table_free_rcu);
618
619 out:
620 write_unlock_bh(&pathtbl_resize_lock);
Javier Cardona18889232009-08-10 12:15:52 -0700621}
622
623void mesh_mpp_table_grow(void)
624{
625 struct mesh_table *oldtbl, *newtbl;
626
Javier Cardona9b84b8082011-05-03 16:57:16 -0700627 write_lock_bh(&pathtbl_resize_lock);
Johannes Berg349eb8c2011-05-14 11:56:16 +0200628 oldtbl = resize_dereference_mpp_paths();
629 newtbl = mesh_table_alloc(oldtbl->size_order + 1);
Johannes Berg1928eca2011-05-14 11:00:52 +0200630 if (!newtbl)
631 goto out;
Johannes Berg349eb8c2011-05-14 11:56:16 +0200632 if (mesh_table_grow(oldtbl, newtbl) < 0) {
cozybit Inca3e6b122011-04-13 11:10:28 -0700633 __mesh_table_free(newtbl);
Johannes Berg1928eca2011-05-14 11:00:52 +0200634 goto out;
Javier Cardona18889232009-08-10 12:15:52 -0700635 }
636 rcu_assign_pointer(mpp_paths, newtbl);
Johannes Berg1928eca2011-05-14 11:00:52 +0200637 call_rcu(&oldtbl->rcu_head, mesh_table_free_rcu);
Javier Cardona18889232009-08-10 12:15:52 -0700638
Johannes Berg1928eca2011-05-14 11:00:52 +0200639 out:
640 write_unlock_bh(&pathtbl_resize_lock);
Javier Cardona18889232009-08-10 12:15:52 -0700641}
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100642
YanBo79617de2008-09-22 13:30:32 +0800643int mpp_path_add(u8 *dst, u8 *mpp, struct ieee80211_sub_if_data *sdata)
644{
Javier Cardona18889232009-08-10 12:15:52 -0700645 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
646 struct ieee80211_local *local = sdata->local;
Johannes Berg349eb8c2011-05-14 11:56:16 +0200647 struct mesh_table *tbl;
YanBo79617de2008-09-22 13:30:32 +0800648 struct mesh_path *mpath, *new_mpath;
649 struct mpath_node *node, *new_node;
650 struct hlist_head *bucket;
651 struct hlist_node *n;
652 int grow = 0;
653 int err = 0;
654 u32 hash_idx;
655
Johannes Berg47846c92009-11-25 17:46:19 +0100656 if (memcmp(dst, sdata->vif.addr, ETH_ALEN) == 0)
YanBo79617de2008-09-22 13:30:32 +0800657 /* never add ourselves as neighbours */
658 return -ENOTSUPP;
659
660 if (is_multicast_ether_addr(dst))
661 return -ENOTSUPP;
662
663 err = -ENOMEM;
Javier Cardona18889232009-08-10 12:15:52 -0700664 new_mpath = kzalloc(sizeof(struct mesh_path), GFP_ATOMIC);
YanBo79617de2008-09-22 13:30:32 +0800665 if (!new_mpath)
666 goto err_path_alloc;
667
Javier Cardona18889232009-08-10 12:15:52 -0700668 new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
YanBo79617de2008-09-22 13:30:32 +0800669 if (!new_node)
670 goto err_node_alloc;
671
Javier Cardona9b84b8082011-05-03 16:57:16 -0700672 read_lock_bh(&pathtbl_resize_lock);
YanBo79617de2008-09-22 13:30:32 +0800673 memcpy(new_mpath->dst, dst, ETH_ALEN);
674 memcpy(new_mpath->mpp, mpp, ETH_ALEN);
675 new_mpath->sdata = sdata;
676 new_mpath->flags = 0;
677 skb_queue_head_init(&new_mpath->frame_queue);
678 new_node->mpath = new_mpath;
Thomas Pedersenc6133662011-08-25 10:36:14 -0700679 init_timer(&new_mpath->timer);
YanBo79617de2008-09-22 13:30:32 +0800680 new_mpath->exp_time = jiffies;
681 spin_lock_init(&new_mpath->state_lock);
682
Johannes Berg349eb8c2011-05-14 11:56:16 +0200683 tbl = resize_dereference_mpp_paths();
YanBo79617de2008-09-22 13:30:32 +0800684
Johannes Berg349eb8c2011-05-14 11:56:16 +0200685 hash_idx = mesh_table_hash(dst, sdata, tbl);
686 bucket = &tbl->hash_buckets[hash_idx];
687
688 spin_lock_bh(&tbl->hashwlock[hash_idx]);
YanBo79617de2008-09-22 13:30:32 +0800689
690 err = -EEXIST;
691 hlist_for_each_entry(node, n, bucket, list) {
692 mpath = node->mpath;
693 if (mpath->sdata == sdata && memcmp(dst, mpath->dst, ETH_ALEN) == 0)
694 goto err_exists;
695 }
696
697 hlist_add_head_rcu(&new_node->list, bucket);
Johannes Berg349eb8c2011-05-14 11:56:16 +0200698 if (atomic_inc_return(&tbl->entries) >=
699 tbl->mean_chain_len * (tbl->hash_mask + 1))
YanBo79617de2008-09-22 13:30:32 +0800700 grow = 1;
701
Johannes Berg349eb8c2011-05-14 11:56:16 +0200702 spin_unlock_bh(&tbl->hashwlock[hash_idx]);
Javier Cardona9b84b8082011-05-03 16:57:16 -0700703 read_unlock_bh(&pathtbl_resize_lock);
YanBo79617de2008-09-22 13:30:32 +0800704 if (grow) {
Javier Cardona18889232009-08-10 12:15:52 -0700705 set_bit(MESH_WORK_GROW_MPP_TABLE, &ifmsh->wrkq_flags);
Johannes Berg64592c82010-06-10 10:21:31 +0200706 ieee80211_queue_work(&local->hw, &sdata->work);
YanBo79617de2008-09-22 13:30:32 +0800707 }
708 return 0;
709
710err_exists:
Johannes Berg349eb8c2011-05-14 11:56:16 +0200711 spin_unlock_bh(&tbl->hashwlock[hash_idx]);
Javier Cardona9b84b8082011-05-03 16:57:16 -0700712 read_unlock_bh(&pathtbl_resize_lock);
YanBo79617de2008-09-22 13:30:32 +0800713 kfree(new_node);
714err_node_alloc:
715 kfree(new_mpath);
716err_path_alloc:
717 return err;
718}
719
720
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100721/**
722 * mesh_plink_broken - deactivates paths and sends perr when a link breaks
723 *
724 * @sta: broken peer link
725 *
726 * This function must be called from the rate control algorithm if enough
727 * delivery errors suggest that a peer link is no longer usable.
728 */
729void mesh_plink_broken(struct sta_info *sta)
730{
Johannes Berg349eb8c2011-05-14 11:56:16 +0200731 struct mesh_table *tbl;
Johannes Berg15ff6362009-11-17 13:34:04 +0100732 static const u8 bcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100733 struct mesh_path *mpath;
734 struct mpath_node *node;
735 struct hlist_node *p;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200736 struct ieee80211_sub_if_data *sdata = sta->sdata;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100737 int i;
Thomas Pedersen25d49e42011-08-11 19:35:15 -0700738 __le16 reason = cpu_to_le16(WLAN_REASON_MESH_PATH_DEST_UNREACHABLE);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100739
740 rcu_read_lock();
Johannes Berg349eb8c2011-05-14 11:56:16 +0200741 tbl = rcu_dereference(mesh_paths);
742 for_each_mesh_entry(tbl, p, node, i) {
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100743 mpath = node->mpath;
Johannes Berg349eb8c2011-05-14 11:56:16 +0200744 if (rcu_dereference(mpath->next_hop) == sta &&
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100745 mpath->flags & MESH_PATH_ACTIVE &&
746 !(mpath->flags & MESH_PATH_FIXED)) {
Javier Cardonaf5e50cd2011-08-29 13:23:05 -0700747 spin_lock_bh(&mpath->state_lock);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100748 mpath->flags &= ~MESH_PATH_ACTIVE;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000749 ++mpath->sn;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100750 spin_unlock_bh(&mpath->state_lock);
Javier Cardona45904f22010-12-03 09:20:40 +0100751 mesh_path_error_tx(sdata->u.mesh.mshcfg.element_ttl,
752 mpath->dst, cpu_to_le32(mpath->sn),
Thomas Pedersen25d49e42011-08-11 19:35:15 -0700753 reason, bcast, sdata);
Javier Cardonaf5e50cd2011-08-29 13:23:05 -0700754 }
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100755 }
756 rcu_read_unlock();
757}
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100758
Javier Cardona19c50b32011-08-29 13:23:07 -0700759static void mesh_path_node_reclaim(struct rcu_head *rp)
760{
761 struct mpath_node *node = container_of(rp, struct mpath_node, rcu);
762 struct ieee80211_sub_if_data *sdata = node->mpath->sdata;
763
764 del_timer_sync(&node->mpath->timer);
765 atomic_dec(&sdata->u.mesh.mpaths);
766 kfree(node->mpath);
767 kfree(node);
768}
769
770/* needs to be called with the corresponding hashwlock taken */
771static void __mesh_path_del(struct mesh_table *tbl, struct mpath_node *node)
772{
773 struct mesh_path *mpath;
774 mpath = node->mpath;
775 spin_lock(&mpath->state_lock);
776 mpath->flags |= MESH_PATH_RESOLVING;
777 if (mpath->is_gate)
778 mesh_gate_del(tbl, mpath);
779 hlist_del_rcu(&node->list);
780 call_rcu(&node->rcu, mesh_path_node_reclaim);
781 spin_unlock(&mpath->state_lock);
782 atomic_dec(&tbl->entries);
783}
784
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100785/**
786 * mesh_path_flush_by_nexthop - Deletes mesh paths if their next hop matches
787 *
788 * @sta - mesh peer to match
789 *
Luis Carlos Cobob4e08ea2008-02-29 15:46:08 -0800790 * RCU notes: this function is called when a mesh plink transitions from
791 * PLINK_ESTAB to any other state, since PLINK_ESTAB state is the only one that
792 * allows path creation. This will happen before the sta can be freed (because
Johannes Bergd0709a62008-02-25 16:27:46 +0100793 * sta_info_destroy() calls this) so any reader in a rcu read block will be
794 * protected against the plink disappearing.
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100795 */
796void mesh_path_flush_by_nexthop(struct sta_info *sta)
797{
Johannes Berg349eb8c2011-05-14 11:56:16 +0200798 struct mesh_table *tbl;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100799 struct mesh_path *mpath;
800 struct mpath_node *node;
801 struct hlist_node *p;
802 int i;
803
Johannes Berg349eb8c2011-05-14 11:56:16 +0200804 rcu_read_lock();
Javier Cardona239289e2011-08-29 13:23:09 -0700805 read_lock_bh(&pathtbl_resize_lock);
806 tbl = resize_dereference_mesh_paths();
Johannes Berg349eb8c2011-05-14 11:56:16 +0200807 for_each_mesh_entry(tbl, p, node, i) {
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100808 mpath = node->mpath;
Javier Cardonacd72e812011-08-29 13:23:08 -0700809 if (rcu_dereference(mpath->next_hop) == sta) {
Javier Cardona19c50b32011-08-29 13:23:07 -0700810 spin_lock_bh(&tbl->hashwlock[i]);
811 __mesh_path_del(tbl, node);
812 spin_unlock_bh(&tbl->hashwlock[i]);
813 }
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100814 }
Javier Cardona239289e2011-08-29 13:23:09 -0700815 read_unlock_bh(&pathtbl_resize_lock);
Johannes Berg349eb8c2011-05-14 11:56:16 +0200816 rcu_read_unlock();
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100817}
818
Javier Cardonacd72e812011-08-29 13:23:08 -0700819static void table_flush_by_iface(struct mesh_table *tbl,
820 struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100821{
822 struct mesh_path *mpath;
823 struct mpath_node *node;
824 struct hlist_node *p;
825 int i;
826
Javier Cardonacd72e812011-08-29 13:23:08 -0700827 WARN_ON(!rcu_read_lock_held());
Johannes Berg349eb8c2011-05-14 11:56:16 +0200828 for_each_mesh_entry(tbl, p, node, i) {
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100829 mpath = node->mpath;
Javier Cardonacd72e812011-08-29 13:23:08 -0700830 if (mpath->sdata != sdata)
831 continue;
Javier Cardonaece1a2e2011-08-29 13:23:04 -0700832 spin_lock_bh(&tbl->hashwlock[i]);
Javier Cardona19c50b32011-08-29 13:23:07 -0700833 __mesh_path_del(tbl, node);
Javier Cardonaece1a2e2011-08-29 13:23:04 -0700834 spin_unlock_bh(&tbl->hashwlock[i]);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100835 }
836}
837
Javier Cardonaece1a2e2011-08-29 13:23:04 -0700838/**
839 * mesh_path_flush_by_iface - Deletes all mesh paths associated with a given iface
840 *
841 * This function deletes both mesh paths as well as mesh portal paths.
842 *
843 * @sdata - interface data to match
844 *
845 */
846void mesh_path_flush_by_iface(struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100847{
Javier Cardonacd72e812011-08-29 13:23:08 -0700848 struct mesh_table *tbl;
Johannes Bergd0709a62008-02-25 16:27:46 +0100849
Javier Cardonacd72e812011-08-29 13:23:08 -0700850 rcu_read_lock();
Javier Cardona239289e2011-08-29 13:23:09 -0700851 read_lock_bh(&pathtbl_resize_lock);
852 tbl = resize_dereference_mesh_paths();
Javier Cardonacd72e812011-08-29 13:23:08 -0700853 table_flush_by_iface(tbl, sdata);
Javier Cardona239289e2011-08-29 13:23:09 -0700854 tbl = resize_dereference_mpp_paths();
Javier Cardonacd72e812011-08-29 13:23:08 -0700855 table_flush_by_iface(tbl, sdata);
Javier Cardona239289e2011-08-29 13:23:09 -0700856 read_unlock_bh(&pathtbl_resize_lock);
Javier Cardonacd72e812011-08-29 13:23:08 -0700857 rcu_read_unlock();
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100858}
859
860/**
861 * mesh_path_del - delete a mesh path from the table
862 *
863 * @addr: dst address (ETH_ALEN length)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200864 * @sdata: local subif
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100865 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200866 * Returns: 0 if successful
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100867 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200868int mesh_path_del(u8 *addr, struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100869{
Johannes Berg349eb8c2011-05-14 11:56:16 +0200870 struct mesh_table *tbl;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100871 struct mesh_path *mpath;
872 struct mpath_node *node;
873 struct hlist_head *bucket;
874 struct hlist_node *n;
875 int hash_idx;
876 int err = 0;
877
Javier Cardona9b84b8082011-05-03 16:57:16 -0700878 read_lock_bh(&pathtbl_resize_lock);
Johannes Berg349eb8c2011-05-14 11:56:16 +0200879 tbl = resize_dereference_mesh_paths();
880 hash_idx = mesh_table_hash(addr, sdata, tbl);
881 bucket = &tbl->hash_buckets[hash_idx];
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100882
Johannes Berg349eb8c2011-05-14 11:56:16 +0200883 spin_lock_bh(&tbl->hashwlock[hash_idx]);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100884 hlist_for_each_entry(node, n, bucket, list) {
885 mpath = node->mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200886 if (mpath->sdata == sdata &&
Johannes Berg349eb8c2011-05-14 11:56:16 +0200887 memcmp(addr, mpath->dst, ETH_ALEN) == 0) {
Javier Cardona19c50b32011-08-29 13:23:07 -0700888 __mesh_path_del(tbl, node);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100889 goto enddel;
890 }
891 }
892
893 err = -ENXIO;
894enddel:
Johannes Bergf5ea9122009-08-07 16:17:38 +0200895 mesh_paths_generation++;
Johannes Berg349eb8c2011-05-14 11:56:16 +0200896 spin_unlock_bh(&tbl->hashwlock[hash_idx]);
Javier Cardona9b84b8082011-05-03 16:57:16 -0700897 read_unlock_bh(&pathtbl_resize_lock);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100898 return err;
899}
900
901/**
902 * mesh_path_tx_pending - sends pending frames in a mesh path queue
903 *
904 * @mpath: mesh path to activate
905 *
906 * Locking: the state_lock of the mpath structure must NOT be held when calling
907 * this function.
908 */
909void mesh_path_tx_pending(struct mesh_path *mpath)
910{
Javier Cardona249b4052009-07-07 10:55:03 -0700911 if (mpath->flags & MESH_PATH_ACTIVE)
912 ieee80211_add_pending_skbs(mpath->sdata->local,
913 &mpath->frame_queue);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100914}
915
916/**
Javier Cardona5ee68e52011-08-09 16:45:08 -0700917 * mesh_path_send_to_gates - sends pending frames to all known mesh gates
918 *
919 * @mpath: mesh path whose queue will be emptied
920 *
921 * If there is only one gate, the frames are transferred from the failed mpath
922 * queue to that gate's queue. If there are more than one gates, the frames
923 * are copied from each gate to the next. After frames are copied, the
924 * mpath queues are emptied onto the transmission queue.
925 */
926int mesh_path_send_to_gates(struct mesh_path *mpath)
927{
928 struct ieee80211_sub_if_data *sdata = mpath->sdata;
929 struct hlist_node *n;
930 struct mesh_table *tbl;
931 struct mesh_path *from_mpath = mpath;
932 struct mpath_node *gate = NULL;
933 bool copy = false;
934 struct hlist_head *known_gates;
935
936 rcu_read_lock();
937 tbl = rcu_dereference(mesh_paths);
938 known_gates = tbl->known_gates;
939 rcu_read_unlock();
940
941 if (!known_gates)
942 return -EHOSTUNREACH;
943
944 hlist_for_each_entry_rcu(gate, n, known_gates, list) {
945 if (gate->mpath->sdata != sdata)
946 continue;
947
948 if (gate->mpath->flags & MESH_PATH_ACTIVE) {
949 mpath_dbg("Forwarding to %pM\n", gate->mpath->dst);
950 mesh_path_move_to_queue(gate->mpath, from_mpath, copy);
951 from_mpath = gate->mpath;
952 copy = true;
953 } else {
954 mpath_dbg("Not forwarding %p\n", gate->mpath);
955 mpath_dbg("flags %x\n", gate->mpath->flags);
956 }
957 }
958
959 hlist_for_each_entry_rcu(gate, n, known_gates, list)
960 if (gate->mpath->sdata == sdata) {
961 mpath_dbg("Sending to %pM\n", gate->mpath->dst);
962 mesh_path_tx_pending(gate->mpath);
963 }
964
965 return (from_mpath == mpath) ? -EHOSTUNREACH : 0;
966}
967
968/**
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100969 * mesh_path_discard_frame - discard a frame whose path could not be resolved
970 *
971 * @skb: frame to discard
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200972 * @sdata: network subif the frame was to be sent through
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100973 *
Javier Cardona35946a52009-07-13 17:00:10 -0700974 * If the frame was being forwarded from another MP, a PERR frame will be sent
975 * to the precursor. The precursor's address (i.e. the previous hop) was saved
976 * in addr1 of the frame-to-be-forwarded, and would only be overwritten once
977 * the destination is successfully resolved.
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100978 *
979 * Locking: the function must me called within a rcu_read_lock region
980 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200981void mesh_path_discard_frame(struct sk_buff *skb,
982 struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100983{
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +0200984 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100985 struct mesh_path *mpath;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000986 u32 sn = 0;
Thomas Pedersen25d49e42011-08-11 19:35:15 -0700987 __le16 reason = cpu_to_le16(WLAN_REASON_MESH_PATH_NOFORWARD);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100988
Johannes Berg47846c92009-11-25 17:46:19 +0100989 if (memcmp(hdr->addr4, sdata->vif.addr, ETH_ALEN) != 0) {
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100990 u8 *ra, *da;
991
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +0200992 da = hdr->addr3;
Javier Cardona35946a52009-07-13 17:00:10 -0700993 ra = hdr->addr1;
Javier Cardonaaf089c12011-08-29 13:23:03 -0700994 rcu_read_lock();
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200995 mpath = mesh_path_lookup(da, sdata);
Javier Cardonaaf089c12011-08-29 13:23:03 -0700996 if (mpath) {
997 spin_lock_bh(&mpath->state_lock);
Rui Paulod19b3bf2009-11-09 23:46:55 +0000998 sn = ++mpath->sn;
Javier Cardonaaf089c12011-08-29 13:23:03 -0700999 spin_unlock_bh(&mpath->state_lock);
1000 }
1001 rcu_read_unlock();
Javier Cardona45904f22010-12-03 09:20:40 +01001002 mesh_path_error_tx(sdata->u.mesh.mshcfg.element_ttl, skb->data,
Thomas Pedersen25d49e42011-08-11 19:35:15 -07001003 cpu_to_le32(sn), reason, ra, sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001004 }
1005
1006 kfree_skb(skb);
Johannes Berg472dbc42008-09-11 00:01:49 +02001007 sdata->u.mesh.mshstats.dropped_frames_no_route++;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001008}
1009
1010/**
1011 * mesh_path_flush_pending - free the pending queue of a mesh path
1012 *
1013 * @mpath: mesh path whose queue has to be freed
1014 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001015 * Locking: the function must me called within a rcu_read_lock region
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001016 */
1017void mesh_path_flush_pending(struct mesh_path *mpath)
1018{
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001019 struct sk_buff *skb;
1020
Javier Cardona00e3f252011-08-09 16:45:07 -07001021 while ((skb = skb_dequeue(&mpath->frame_queue)) != NULL)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001022 mesh_path_discard_frame(skb, mpath->sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001023}
1024
1025/**
1026 * mesh_path_fix_nexthop - force a specific next hop for a mesh path
1027 *
1028 * @mpath: the mesh path to modify
1029 * @next_hop: the next hop to force
1030 *
1031 * Locking: this function must be called holding mpath->state_lock
1032 */
1033void mesh_path_fix_nexthop(struct mesh_path *mpath, struct sta_info *next_hop)
1034{
1035 spin_lock_bh(&mpath->state_lock);
1036 mesh_path_assign_nexthop(mpath, next_hop);
Rui Paulod19b3bf2009-11-09 23:46:55 +00001037 mpath->sn = 0xffff;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001038 mpath->metric = 0;
1039 mpath->hop_count = 0;
1040 mpath->exp_time = 0;
1041 mpath->flags |= MESH_PATH_FIXED;
1042 mesh_path_activate(mpath);
1043 spin_unlock_bh(&mpath->state_lock);
1044 mesh_path_tx_pending(mpath);
1045}
1046
1047static void mesh_path_node_free(struct hlist_node *p, bool free_leafs)
1048{
1049 struct mesh_path *mpath;
1050 struct mpath_node *node = hlist_entry(p, struct mpath_node, list);
1051 mpath = node->mpath;
1052 hlist_del_rcu(p);
Javier Cardonad0df9ee2011-05-13 14:16:07 -07001053 if (free_leafs) {
1054 del_timer_sync(&mpath->timer);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001055 kfree(mpath);
Javier Cardonad0df9ee2011-05-13 14:16:07 -07001056 }
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001057 kfree(node);
1058}
1059
Pavel Emelyanov4caf86c2008-05-07 19:47:01 +04001060static int mesh_path_node_copy(struct hlist_node *p, struct mesh_table *newtbl)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001061{
1062 struct mesh_path *mpath;
1063 struct mpath_node *node, *new_node;
1064 u32 hash_idx;
1065
Pavel Emelyanov8566dc32008-05-07 19:51:51 +04001066 new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
Pavel Emelyanov00242c42008-05-07 19:48:14 +04001067 if (new_node == NULL)
1068 return -ENOMEM;
1069
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001070 node = hlist_entry(p, struct mpath_node, list);
1071 mpath = node->mpath;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001072 new_node->mpath = mpath;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001073 hash_idx = mesh_table_hash(mpath->dst, mpath->sdata, newtbl);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001074 hlist_add_head(&new_node->list,
1075 &newtbl->hash_buckets[hash_idx]);
Pavel Emelyanov4caf86c2008-05-07 19:47:01 +04001076 return 0;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001077}
1078
1079int mesh_pathtbl_init(void)
1080{
Johannes Berg349eb8c2011-05-14 11:56:16 +02001081 struct mesh_table *tbl_path, *tbl_mpp;
Dan Carpenter4c5ade42011-08-30 22:16:15 +03001082 int ret;
YanBo79617de2008-09-22 13:30:32 +08001083
Johannes Berg349eb8c2011-05-14 11:56:16 +02001084 tbl_path = mesh_table_alloc(INIT_PATHS_SIZE_ORDER);
1085 if (!tbl_path)
1086 return -ENOMEM;
1087 tbl_path->free_node = &mesh_path_node_free;
1088 tbl_path->copy_node = &mesh_path_node_copy;
1089 tbl_path->mean_chain_len = MEAN_CHAIN_LEN;
Javier Cardona5ee68e52011-08-09 16:45:08 -07001090 tbl_path->known_gates = kzalloc(sizeof(struct hlist_head), GFP_ATOMIC);
Dan Carpenter4c5ade42011-08-30 22:16:15 +03001091 if (!tbl_path->known_gates) {
1092 ret = -ENOMEM;
1093 goto free_path;
1094 }
Javier Cardona5ee68e52011-08-09 16:45:08 -07001095 INIT_HLIST_HEAD(tbl_path->known_gates);
1096
Johannes Berg349eb8c2011-05-14 11:56:16 +02001097
1098 tbl_mpp = mesh_table_alloc(INIT_PATHS_SIZE_ORDER);
1099 if (!tbl_mpp) {
Dan Carpenter4c5ade42011-08-30 22:16:15 +03001100 ret = -ENOMEM;
1101 goto free_path;
YanBo79617de2008-09-22 13:30:32 +08001102 }
Johannes Berg349eb8c2011-05-14 11:56:16 +02001103 tbl_mpp->free_node = &mesh_path_node_free;
1104 tbl_mpp->copy_node = &mesh_path_node_copy;
1105 tbl_mpp->mean_chain_len = MEAN_CHAIN_LEN;
Javier Cardona5ee68e52011-08-09 16:45:08 -07001106 tbl_mpp->known_gates = kzalloc(sizeof(struct hlist_head), GFP_ATOMIC);
Dan Carpenter4c5ade42011-08-30 22:16:15 +03001107 if (!tbl_mpp->known_gates) {
1108 ret = -ENOMEM;
1109 goto free_mpp;
1110 }
Javier Cardona5ee68e52011-08-09 16:45:08 -07001111 INIT_HLIST_HEAD(tbl_mpp->known_gates);
Johannes Berg349eb8c2011-05-14 11:56:16 +02001112
1113 /* Need no locking since this is during init */
1114 RCU_INIT_POINTER(mesh_paths, tbl_path);
1115 RCU_INIT_POINTER(mpp_paths, tbl_mpp);
YanBo79617de2008-09-22 13:30:32 +08001116
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001117 return 0;
Dan Carpenter4c5ade42011-08-30 22:16:15 +03001118
1119free_mpp:
1120 mesh_table_free(tbl_mpp, true);
1121free_path:
1122 mesh_table_free(tbl_path, true);
1123 return ret;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001124}
1125
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001126void mesh_path_expire(struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001127{
Johannes Berg349eb8c2011-05-14 11:56:16 +02001128 struct mesh_table *tbl;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001129 struct mesh_path *mpath;
1130 struct mpath_node *node;
1131 struct hlist_node *p;
1132 int i;
1133
Johannes Berg349eb8c2011-05-14 11:56:16 +02001134 rcu_read_lock();
1135 tbl = rcu_dereference(mesh_paths);
1136 for_each_mesh_entry(tbl, p, node, i) {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001137 if (node->mpath->sdata != sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001138 continue;
1139 mpath = node->mpath;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001140 if ((!(mpath->flags & MESH_PATH_RESOLVING)) &&
1141 (!(mpath->flags & MESH_PATH_FIXED)) &&
Javier Cardonaf5e50cd2011-08-29 13:23:05 -07001142 time_after(jiffies, mpath->exp_time + MESH_PATH_EXPIRE))
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001143 mesh_path_del(mpath->dst, mpath->sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001144 }
Johannes Berg349eb8c2011-05-14 11:56:16 +02001145 rcu_read_unlock();
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001146}
1147
1148void mesh_pathtbl_unregister(void)
1149{
Johannes Berg349eb8c2011-05-14 11:56:16 +02001150 /* no need for locking during exit path */
Eric Dumazet33d480c2011-08-11 19:30:52 +00001151 mesh_table_free(rcu_dereference_protected(mesh_paths, 1), true);
1152 mesh_table_free(rcu_dereference_protected(mpp_paths, 1), true);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001153}