]>
Commit | Line | Data |
---|---|---|
eb2b9311 | 1 | /* |
264d9b7d | 2 | * Copyright (c) 2008, 2009 open80211s Ltd. |
eb2b9311 LCC |
3 | * Author: Luis Carlos Cobo <luisca@cozybit.com> |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License version 2 as | |
7 | * published by the Free Software Foundation. | |
8 | */ | |
9 | ||
10 | #include <linux/etherdevice.h> | |
11 | #include <linux/list.h> | |
eb2b9311 | 12 | #include <linux/random.h> |
5a0e3ad6 | 13 | #include <linux/slab.h> |
eb2b9311 LCC |
14 | #include <linux/spinlock.h> |
15 | #include <linux/string.h> | |
16 | #include <net/mac80211.h> | |
17 | #include "ieee80211_i.h" | |
18 | #include "mesh.h" | |
19 | ||
7646887a JC |
20 | #ifdef CONFIG_MAC80211_VERBOSE_MPATH_DEBUG |
21 | #define mpath_dbg(fmt, args...) printk(KERN_DEBUG fmt, ##args) | |
22 | #else | |
23 | #define mpath_dbg(fmt, args...) do { (void)(0); } while (0) | |
24 | #endif | |
25 | ||
eb2b9311 LCC |
26 | /* There will be initially 2^INIT_PATHS_SIZE_ORDER buckets */ |
27 | #define INIT_PATHS_SIZE_ORDER 2 | |
28 | ||
29 | /* Keep the mean chain length below this constant */ | |
30 | #define MEAN_CHAIN_LEN 2 | |
31 | ||
32 | #define MPATH_EXPIRED(mpath) ((mpath->flags & MESH_PATH_ACTIVE) && \ | |
33 | time_after(jiffies, mpath->exp_time) && \ | |
34 | !(mpath->flags & MESH_PATH_FIXED)) | |
35 | ||
36 | struct mpath_node { | |
37 | struct hlist_node list; | |
38 | struct rcu_head rcu; | |
39 | /* This indirection allows two different tables to point to the same | |
40 | * mesh_path structure, useful when resizing | |
41 | */ | |
42 | struct mesh_path *mpath; | |
43 | }; | |
44 | ||
349eb8cf JB |
45 | static struct mesh_table __rcu *mesh_paths; |
46 | static struct mesh_table __rcu *mpp_paths; /* Store paths for MPP&MAP */ | |
eb2b9311 | 47 | |
f5ea9120 | 48 | int mesh_paths_generation; |
6b86bd62 JB |
49 | |
50 | /* This lock will have the grow table function as writer and add / delete nodes | |
51 | * as readers. When reading the table (i.e. doing lookups) we are well protected | |
19c50b3d JC |
52 | * by RCU. We need to take this lock when modying the number of buckets |
53 | * on one of the path tables but we don't need to if adding or removing mpaths | |
54 | * from hash buckets. | |
6b86bd62 JB |
55 | */ |
56 | static DEFINE_RWLOCK(pathtbl_resize_lock); | |
57 | ||
58 | ||
349eb8cf JB |
59 | static inline struct mesh_table *resize_dereference_mesh_paths(void) |
60 | { | |
61 | return rcu_dereference_protected(mesh_paths, | |
62 | lockdep_is_held(&pathtbl_resize_lock)); | |
63 | } | |
64 | ||
65 | static inline struct mesh_table *resize_dereference_mpp_paths(void) | |
66 | { | |
67 | return rcu_dereference_protected(mpp_paths, | |
68 | lockdep_is_held(&pathtbl_resize_lock)); | |
69 | } | |
70 | ||
5ee68e5b JC |
71 | static int mesh_gate_add(struct mesh_table *tbl, struct mesh_path *mpath); |
72 | ||
349eb8cf JB |
73 | /* |
74 | * CAREFUL -- "tbl" must not be an expression, | |
75 | * in particular not an rcu_dereference(), since | |
76 | * it's used twice. So it is illegal to do | |
77 | * for_each_mesh_entry(rcu_dereference(...), ...) | |
78 | */ | |
79 | #define for_each_mesh_entry(tbl, p, node, i) \ | |
80 | for (i = 0; i <= tbl->hash_mask; i++) \ | |
81 | hlist_for_each_entry_rcu(node, p, &tbl->hash_buckets[i], list) | |
82 | ||
83 | ||
6b86bd62 JB |
84 | static struct mesh_table *mesh_table_alloc(int size_order) |
85 | { | |
86 | int i; | |
87 | struct mesh_table *newtbl; | |
88 | ||
d676ff49 | 89 | newtbl = kmalloc(sizeof(struct mesh_table), GFP_ATOMIC); |
6b86bd62 JB |
90 | if (!newtbl) |
91 | return NULL; | |
92 | ||
93 | newtbl->hash_buckets = kzalloc(sizeof(struct hlist_head) * | |
d676ff49 | 94 | (1 << size_order), GFP_ATOMIC); |
6b86bd62 JB |
95 | |
96 | if (!newtbl->hash_buckets) { | |
97 | kfree(newtbl); | |
98 | return NULL; | |
99 | } | |
100 | ||
101 | newtbl->hashwlock = kmalloc(sizeof(spinlock_t) * | |
d676ff49 | 102 | (1 << size_order), GFP_ATOMIC); |
6b86bd62 JB |
103 | if (!newtbl->hashwlock) { |
104 | kfree(newtbl->hash_buckets); | |
105 | kfree(newtbl); | |
106 | return NULL; | |
107 | } | |
108 | ||
109 | newtbl->size_order = size_order; | |
110 | newtbl->hash_mask = (1 << size_order) - 1; | |
111 | atomic_set(&newtbl->entries, 0); | |
112 | get_random_bytes(&newtbl->hash_rnd, | |
113 | sizeof(newtbl->hash_rnd)); | |
114 | for (i = 0; i <= newtbl->hash_mask; i++) | |
115 | spin_lock_init(&newtbl->hashwlock[i]); | |
5ee68e5b | 116 | spin_lock_init(&newtbl->gates_lock); |
6b86bd62 JB |
117 | |
118 | return newtbl; | |
119 | } | |
120 | ||
18889231 JC |
121 | static void __mesh_table_free(struct mesh_table *tbl) |
122 | { | |
123 | kfree(tbl->hash_buckets); | |
124 | kfree(tbl->hashwlock); | |
125 | kfree(tbl); | |
126 | } | |
127 | ||
6b86bd62 | 128 | static void mesh_table_free(struct mesh_table *tbl, bool free_leafs) |
18889231 JC |
129 | { |
130 | struct hlist_head *mesh_hash; | |
131 | struct hlist_node *p, *q; | |
5ee68e5b | 132 | struct mpath_node *gate; |
18889231 JC |
133 | int i; |
134 | ||
135 | mesh_hash = tbl->hash_buckets; | |
136 | for (i = 0; i <= tbl->hash_mask; i++) { | |
9b84b808 | 137 | spin_lock_bh(&tbl->hashwlock[i]); |
18889231 JC |
138 | hlist_for_each_safe(p, q, &mesh_hash[i]) { |
139 | tbl->free_node(p, free_leafs); | |
140 | atomic_dec(&tbl->entries); | |
141 | } | |
9b84b808 | 142 | spin_unlock_bh(&tbl->hashwlock[i]); |
18889231 | 143 | } |
5ee68e5b JC |
144 | if (free_leafs) { |
145 | spin_lock_bh(&tbl->gates_lock); | |
146 | hlist_for_each_entry_safe(gate, p, q, | |
147 | tbl->known_gates, list) { | |
148 | hlist_del(&gate->list); | |
149 | kfree(gate); | |
150 | } | |
151 | kfree(tbl->known_gates); | |
152 | spin_unlock_bh(&tbl->gates_lock); | |
153 | } | |
154 | ||
18889231 JC |
155 | __mesh_table_free(tbl); |
156 | } | |
157 | ||
a3e6b12c | 158 | static int mesh_table_grow(struct mesh_table *oldtbl, |
6b86bd62 | 159 | struct mesh_table *newtbl) |
18889231 | 160 | { |
18889231 JC |
161 | struct hlist_head *oldhash; |
162 | struct hlist_node *p, *q; | |
163 | int i; | |
164 | ||
a3e6b12c I |
165 | if (atomic_read(&oldtbl->entries) |
166 | < oldtbl->mean_chain_len * (oldtbl->hash_mask + 1)) | |
167 | return -EAGAIN; | |
18889231 | 168 | |
a3e6b12c I |
169 | newtbl->free_node = oldtbl->free_node; |
170 | newtbl->mean_chain_len = oldtbl->mean_chain_len; | |
171 | newtbl->copy_node = oldtbl->copy_node; | |
5ee68e5b | 172 | newtbl->known_gates = oldtbl->known_gates; |
a3e6b12c | 173 | atomic_set(&newtbl->entries, atomic_read(&oldtbl->entries)); |
18889231 | 174 | |
a3e6b12c I |
175 | oldhash = oldtbl->hash_buckets; |
176 | for (i = 0; i <= oldtbl->hash_mask; i++) | |
18889231 | 177 | hlist_for_each(p, &oldhash[i]) |
a3e6b12c | 178 | if (oldtbl->copy_node(p, newtbl) < 0) |
18889231 JC |
179 | goto errcopy; |
180 | ||
a3e6b12c | 181 | return 0; |
18889231 JC |
182 | |
183 | errcopy: | |
184 | for (i = 0; i <= newtbl->hash_mask; i++) { | |
185 | hlist_for_each_safe(p, q, &newtbl->hash_buckets[i]) | |
a3e6b12c | 186 | oldtbl->free_node(p, 0); |
18889231 | 187 | } |
a3e6b12c | 188 | return -ENOMEM; |
18889231 JC |
189 | } |
190 | ||
6b86bd62 JB |
191 | static u32 mesh_table_hash(u8 *addr, struct ieee80211_sub_if_data *sdata, |
192 | struct mesh_table *tbl) | |
193 | { | |
194 | /* Use last four bytes of hw addr and interface index as hash index */ | |
195 | return jhash_2words(*(u32 *)(addr+2), sdata->dev->ifindex, tbl->hash_rnd) | |
196 | & tbl->hash_mask; | |
197 | } | |
f5ea9120 | 198 | |
eb2b9311 LCC |
199 | |
200 | /** | |
201 | * | |
202 | * mesh_path_assign_nexthop - update mesh path next hop | |
203 | * | |
204 | * @mpath: mesh path to update | |
205 | * @sta: next hop to assign | |
206 | * | |
207 | * Locking: mpath->state_lock must be held when calling this function | |
208 | */ | |
209 | void mesh_path_assign_nexthop(struct mesh_path *mpath, struct sta_info *sta) | |
210 | { | |
10c836d7 JC |
211 | struct sk_buff *skb; |
212 | struct ieee80211_hdr *hdr; | |
213 | struct sk_buff_head tmpq; | |
214 | unsigned long flags; | |
215 | ||
d0709a65 | 216 | rcu_assign_pointer(mpath->next_hop, sta); |
10c836d7 JC |
217 | |
218 | __skb_queue_head_init(&tmpq); | |
219 | ||
220 | spin_lock_irqsave(&mpath->frame_queue.lock, flags); | |
221 | ||
222 | while ((skb = __skb_dequeue(&mpath->frame_queue)) != NULL) { | |
223 | hdr = (struct ieee80211_hdr *) skb->data; | |
224 | memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN); | |
225 | __skb_queue_tail(&tmpq, skb); | |
226 | } | |
227 | ||
228 | skb_queue_splice(&tmpq, &mpath->frame_queue); | |
229 | spin_unlock_irqrestore(&mpath->frame_queue.lock, flags); | |
eb2b9311 LCC |
230 | } |
231 | ||
5ee68e5b JC |
232 | static void prepare_for_gate(struct sk_buff *skb, char *dst_addr, |
233 | struct mesh_path *gate_mpath) | |
234 | { | |
235 | struct ieee80211_hdr *hdr; | |
236 | struct ieee80211s_hdr *mshdr; | |
237 | int mesh_hdrlen, hdrlen; | |
238 | char *next_hop; | |
239 | ||
240 | hdr = (struct ieee80211_hdr *) skb->data; | |
241 | hdrlen = ieee80211_hdrlen(hdr->frame_control); | |
242 | mshdr = (struct ieee80211s_hdr *) (skb->data + hdrlen); | |
243 | ||
244 | if (!(mshdr->flags & MESH_FLAGS_AE)) { | |
245 | /* size of the fixed part of the mesh header */ | |
246 | mesh_hdrlen = 6; | |
247 | ||
248 | /* make room for the two extended addresses */ | |
249 | skb_push(skb, 2 * ETH_ALEN); | |
250 | memmove(skb->data, hdr, hdrlen + mesh_hdrlen); | |
251 | ||
252 | hdr = (struct ieee80211_hdr *) skb->data; | |
253 | ||
254 | /* we preserve the previous mesh header and only add | |
255 | * the new addreses */ | |
256 | mshdr = (struct ieee80211s_hdr *) (skb->data + hdrlen); | |
257 | mshdr->flags = MESH_FLAGS_AE_A5_A6; | |
258 | memcpy(mshdr->eaddr1, hdr->addr3, ETH_ALEN); | |
259 | memcpy(mshdr->eaddr2, hdr->addr4, ETH_ALEN); | |
260 | } | |
261 | ||
262 | /* update next hop */ | |
263 | hdr = (struct ieee80211_hdr *) skb->data; | |
264 | rcu_read_lock(); | |
265 | next_hop = rcu_dereference(gate_mpath->next_hop)->sta.addr; | |
266 | memcpy(hdr->addr1, next_hop, ETH_ALEN); | |
267 | rcu_read_unlock(); | |
268 | memcpy(hdr->addr3, dst_addr, ETH_ALEN); | |
269 | } | |
270 | ||
271 | /** | |
272 | * | |
273 | * mesh_path_move_to_queue - Move or copy frames from one mpath queue to another | |
274 | * | |
275 | * This function is used to transfer or copy frames from an unresolved mpath to | |
276 | * a gate mpath. The function also adds the Address Extension field and | |
277 | * updates the next hop. | |
278 | * | |
279 | * If a frame already has an Address Extension field, only the next hop and | |
280 | * destination addresses are updated. | |
281 | * | |
282 | * The gate mpath must be an active mpath with a valid mpath->next_hop. | |
283 | * | |
284 | * @mpath: An active mpath the frames will be sent to (i.e. the gate) | |
285 | * @from_mpath: The failed mpath | |
286 | * @copy: When true, copy all the frames to the new mpath queue. When false, | |
287 | * move them. | |
288 | */ | |
289 | static void mesh_path_move_to_queue(struct mesh_path *gate_mpath, | |
290 | struct mesh_path *from_mpath, | |
291 | bool copy) | |
292 | { | |
c6133661 | 293 | struct sk_buff *skb, *cp_skb = NULL; |
5ee68e5b JC |
294 | struct sk_buff_head gateq, failq; |
295 | unsigned long flags; | |
296 | int num_skbs; | |
297 | ||
298 | BUG_ON(gate_mpath == from_mpath); | |
299 | BUG_ON(!gate_mpath->next_hop); | |
300 | ||
301 | __skb_queue_head_init(&gateq); | |
302 | __skb_queue_head_init(&failq); | |
303 | ||
304 | spin_lock_irqsave(&from_mpath->frame_queue.lock, flags); | |
305 | skb_queue_splice_init(&from_mpath->frame_queue, &failq); | |
306 | spin_unlock_irqrestore(&from_mpath->frame_queue.lock, flags); | |
307 | ||
308 | num_skbs = skb_queue_len(&failq); | |
309 | ||
310 | while (num_skbs--) { | |
311 | skb = __skb_dequeue(&failq); | |
817a53d9 | 312 | if (copy) { |
5ee68e5b | 313 | cp_skb = skb_copy(skb, GFP_ATOMIC); |
817a53d9 JL |
314 | if (cp_skb) |
315 | __skb_queue_tail(&failq, cp_skb); | |
316 | } | |
5ee68e5b JC |
317 | |
318 | prepare_for_gate(skb, gate_mpath->dst, gate_mpath); | |
319 | __skb_queue_tail(&gateq, skb); | |
5ee68e5b JC |
320 | } |
321 | ||
322 | spin_lock_irqsave(&gate_mpath->frame_queue.lock, flags); | |
323 | skb_queue_splice(&gateq, &gate_mpath->frame_queue); | |
324 | mpath_dbg("Mpath queue for gate %pM has %d frames\n", | |
325 | gate_mpath->dst, | |
326 | skb_queue_len(&gate_mpath->frame_queue)); | |
327 | spin_unlock_irqrestore(&gate_mpath->frame_queue.lock, flags); | |
328 | ||
329 | if (!copy) | |
330 | return; | |
331 | ||
332 | spin_lock_irqsave(&from_mpath->frame_queue.lock, flags); | |
333 | skb_queue_splice(&failq, &from_mpath->frame_queue); | |
334 | spin_unlock_irqrestore(&from_mpath->frame_queue.lock, flags); | |
335 | } | |
336 | ||
eb2b9311 LCC |
337 | |
338 | /** | |
339 | * mesh_path_lookup - look up a path in the mesh path table | |
340 | * @dst: hardware address (ETH_ALEN length) of destination | |
f698d856 | 341 | * @sdata: local subif |
eb2b9311 LCC |
342 | * |
343 | * Returns: pointer to the mesh path structure, or NULL if not found | |
344 | * | |
345 | * Locking: must be called within a read rcu section. | |
346 | */ | |
f698d856 | 347 | struct mesh_path *mesh_path_lookup(u8 *dst, struct ieee80211_sub_if_data *sdata) |
eb2b9311 LCC |
348 | { |
349 | struct mesh_path *mpath; | |
350 | struct hlist_node *n; | |
351 | struct hlist_head *bucket; | |
352 | struct mesh_table *tbl; | |
353 | struct mpath_node *node; | |
354 | ||
355 | tbl = rcu_dereference(mesh_paths); | |
356 | ||
f698d856 | 357 | bucket = &tbl->hash_buckets[mesh_table_hash(dst, sdata, tbl)]; |
eb2b9311 LCC |
358 | hlist_for_each_entry_rcu(node, n, bucket, list) { |
359 | mpath = node->mpath; | |
f698d856 | 360 | if (mpath->sdata == sdata && |
eb2b9311 LCC |
361 | memcmp(dst, mpath->dst, ETH_ALEN) == 0) { |
362 | if (MPATH_EXPIRED(mpath)) { | |
363 | spin_lock_bh(&mpath->state_lock); | |
ad99d141 | 364 | mpath->flags &= ~MESH_PATH_ACTIVE; |
eb2b9311 LCC |
365 | spin_unlock_bh(&mpath->state_lock); |
366 | } | |
367 | return mpath; | |
368 | } | |
369 | } | |
370 | return NULL; | |
371 | } | |
372 | ||
79617dee Y |
373 | struct mesh_path *mpp_path_lookup(u8 *dst, struct ieee80211_sub_if_data *sdata) |
374 | { | |
375 | struct mesh_path *mpath; | |
376 | struct hlist_node *n; | |
377 | struct hlist_head *bucket; | |
378 | struct mesh_table *tbl; | |
379 | struct mpath_node *node; | |
380 | ||
381 | tbl = rcu_dereference(mpp_paths); | |
382 | ||
383 | bucket = &tbl->hash_buckets[mesh_table_hash(dst, sdata, tbl)]; | |
384 | hlist_for_each_entry_rcu(node, n, bucket, list) { | |
385 | mpath = node->mpath; | |
386 | if (mpath->sdata == sdata && | |
387 | memcmp(dst, mpath->dst, ETH_ALEN) == 0) { | |
388 | if (MPATH_EXPIRED(mpath)) { | |
389 | spin_lock_bh(&mpath->state_lock); | |
ad99d141 | 390 | mpath->flags &= ~MESH_PATH_ACTIVE; |
79617dee Y |
391 | spin_unlock_bh(&mpath->state_lock); |
392 | } | |
393 | return mpath; | |
394 | } | |
395 | } | |
396 | return NULL; | |
397 | } | |
398 | ||
399 | ||
eb2b9311 LCC |
400 | /** |
401 | * mesh_path_lookup_by_idx - look up a path in the mesh path table by its index | |
402 | * @idx: index | |
f698d856 | 403 | * @sdata: local subif, or NULL for all entries |
eb2b9311 LCC |
404 | * |
405 | * Returns: pointer to the mesh path structure, or NULL if not found. | |
406 | * | |
407 | * Locking: must be called within a read rcu section. | |
408 | */ | |
f698d856 | 409 | struct mesh_path *mesh_path_lookup_by_idx(int idx, struct ieee80211_sub_if_data *sdata) |
eb2b9311 | 410 | { |
349eb8cf | 411 | struct mesh_table *tbl = rcu_dereference(mesh_paths); |
eb2b9311 LCC |
412 | struct mpath_node *node; |
413 | struct hlist_node *p; | |
414 | int i; | |
415 | int j = 0; | |
416 | ||
349eb8cf | 417 | for_each_mesh_entry(tbl, p, node, i) { |
f698d856 | 418 | if (sdata && node->mpath->sdata != sdata) |
2a8ca29a | 419 | continue; |
eb2b9311 LCC |
420 | if (j++ == idx) { |
421 | if (MPATH_EXPIRED(node->mpath)) { | |
422 | spin_lock_bh(&node->mpath->state_lock); | |
ad99d141 | 423 | node->mpath->flags &= ~MESH_PATH_ACTIVE; |
eb2b9311 LCC |
424 | spin_unlock_bh(&node->mpath->state_lock); |
425 | } | |
426 | return node->mpath; | |
427 | } | |
2a8ca29a | 428 | } |
eb2b9311 LCC |
429 | |
430 | return NULL; | |
431 | } | |
432 | ||
5ee68e5b JC |
433 | static void mesh_gate_node_reclaim(struct rcu_head *rp) |
434 | { | |
435 | struct mpath_node *node = container_of(rp, struct mpath_node, rcu); | |
436 | kfree(node); | |
437 | } | |
438 | ||
439 | /** | |
440 | * mesh_gate_add - mark mpath as path to a mesh gate and add to known_gates | |
441 | * @mesh_tbl: table which contains known_gates list | |
442 | * @mpath: mpath to known mesh gate | |
443 | * | |
444 | * Returns: 0 on success | |
445 | * | |
446 | */ | |
447 | static int mesh_gate_add(struct mesh_table *tbl, struct mesh_path *mpath) | |
448 | { | |
449 | struct mpath_node *gate, *new_gate; | |
450 | struct hlist_node *n; | |
451 | int err; | |
452 | ||
453 | rcu_read_lock(); | |
454 | tbl = rcu_dereference(tbl); | |
455 | ||
456 | hlist_for_each_entry_rcu(gate, n, tbl->known_gates, list) | |
457 | if (gate->mpath == mpath) { | |
458 | err = -EEXIST; | |
459 | goto err_rcu; | |
460 | } | |
461 | ||
462 | new_gate = kzalloc(sizeof(struct mpath_node), GFP_ATOMIC); | |
463 | if (!new_gate) { | |
464 | err = -ENOMEM; | |
465 | goto err_rcu; | |
466 | } | |
467 | ||
468 | mpath->is_gate = true; | |
469 | mpath->sdata->u.mesh.num_gates++; | |
470 | new_gate->mpath = mpath; | |
471 | spin_lock_bh(&tbl->gates_lock); | |
472 | hlist_add_head_rcu(&new_gate->list, tbl->known_gates); | |
473 | spin_unlock_bh(&tbl->gates_lock); | |
474 | rcu_read_unlock(); | |
475 | mpath_dbg("Mesh path (%s): Recorded new gate: %pM. %d known gates\n", | |
476 | mpath->sdata->name, mpath->dst, | |
477 | mpath->sdata->u.mesh.num_gates); | |
478 | return 0; | |
479 | err_rcu: | |
480 | rcu_read_unlock(); | |
481 | return err; | |
482 | } | |
483 | ||
484 | /** | |
485 | * mesh_gate_del - remove a mesh gate from the list of known gates | |
486 | * @tbl: table which holds our list of known gates | |
487 | * @mpath: gate mpath | |
488 | * | |
489 | * Returns: 0 on success | |
490 | * | |
491 | * Locking: must be called inside rcu_read_lock() section | |
492 | */ | |
493 | static int mesh_gate_del(struct mesh_table *tbl, struct mesh_path *mpath) | |
494 | { | |
495 | struct mpath_node *gate; | |
496 | struct hlist_node *p, *q; | |
497 | ||
498 | tbl = rcu_dereference(tbl); | |
499 | ||
500 | hlist_for_each_entry_safe(gate, p, q, tbl->known_gates, list) | |
501 | if (gate->mpath == mpath) { | |
502 | spin_lock_bh(&tbl->gates_lock); | |
503 | hlist_del_rcu(&gate->list); | |
504 | call_rcu(&gate->rcu, mesh_gate_node_reclaim); | |
505 | spin_unlock_bh(&tbl->gates_lock); | |
506 | mpath->sdata->u.mesh.num_gates--; | |
507 | mpath->is_gate = false; | |
508 | mpath_dbg("Mesh path (%s): Deleted gate: %pM. " | |
509 | "%d known gates\n", mpath->sdata->name, | |
510 | mpath->dst, mpath->sdata->u.mesh.num_gates); | |
511 | break; | |
512 | } | |
513 | ||
514 | return 0; | |
515 | } | |
516 | ||
517 | /** | |
518 | * | |
519 | * mesh_path_add_gate - add the given mpath to a mesh gate to our path table | |
520 | * @mpath: gate path to add to table | |
521 | */ | |
522 | int mesh_path_add_gate(struct mesh_path *mpath) | |
523 | { | |
524 | return mesh_gate_add(mesh_paths, mpath); | |
525 | } | |
526 | ||
527 | /** | |
528 | * mesh_gate_num - number of gates known to this interface | |
529 | * @sdata: subif data | |
530 | */ | |
531 | int mesh_gate_num(struct ieee80211_sub_if_data *sdata) | |
532 | { | |
533 | return sdata->u.mesh.num_gates; | |
534 | } | |
535 | ||
eb2b9311 LCC |
536 | /** |
537 | * mesh_path_add - allocate and add a new path to the mesh path table | |
538 | * @addr: destination address of the path (ETH_ALEN length) | |
f698d856 | 539 | * @sdata: local subif |
eb2b9311 | 540 | * |
af901ca1 | 541 | * Returns: 0 on success |
eb2b9311 LCC |
542 | * |
543 | * State: the initial state of the new path is set to 0 | |
544 | */ | |
f698d856 | 545 | int mesh_path_add(u8 *dst, struct ieee80211_sub_if_data *sdata) |
eb2b9311 | 546 | { |
18889231 JC |
547 | struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh; |
548 | struct ieee80211_local *local = sdata->local; | |
349eb8cf | 549 | struct mesh_table *tbl; |
eb2b9311 LCC |
550 | struct mesh_path *mpath, *new_mpath; |
551 | struct mpath_node *node, *new_node; | |
552 | struct hlist_head *bucket; | |
553 | struct hlist_node *n; | |
554 | int grow = 0; | |
555 | int err = 0; | |
556 | u32 hash_idx; | |
557 | ||
47846c9b | 558 | if (memcmp(dst, sdata->vif.addr, ETH_ALEN) == 0) |
eb2b9311 LCC |
559 | /* never add ourselves as neighbours */ |
560 | return -ENOTSUPP; | |
561 | ||
562 | if (is_multicast_ether_addr(dst)) | |
563 | return -ENOTSUPP; | |
564 | ||
472dbc45 | 565 | if (atomic_add_unless(&sdata->u.mesh.mpaths, 1, MESH_MAX_MPATHS) == 0) |
eb2b9311 LCC |
566 | return -ENOSPC; |
567 | ||
402d7752 | 568 | err = -ENOMEM; |
18889231 | 569 | new_mpath = kzalloc(sizeof(struct mesh_path), GFP_ATOMIC); |
402d7752 PE |
570 | if (!new_mpath) |
571 | goto err_path_alloc; | |
572 | ||
18889231 | 573 | new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC); |
402d7752 PE |
574 | if (!new_node) |
575 | goto err_node_alloc; | |
f84e71a9 | 576 | |
9b84b808 | 577 | read_lock_bh(&pathtbl_resize_lock); |
eb2b9311 | 578 | memcpy(new_mpath->dst, dst, ETH_ALEN); |
f698d856 | 579 | new_mpath->sdata = sdata; |
eb2b9311 LCC |
580 | new_mpath->flags = 0; |
581 | skb_queue_head_init(&new_mpath->frame_queue); | |
eb2b9311 LCC |
582 | new_node->mpath = new_mpath; |
583 | new_mpath->timer.data = (unsigned long) new_mpath; | |
584 | new_mpath->timer.function = mesh_path_timer; | |
585 | new_mpath->exp_time = jiffies; | |
586 | spin_lock_init(&new_mpath->state_lock); | |
587 | init_timer(&new_mpath->timer); | |
588 | ||
349eb8cf | 589 | tbl = resize_dereference_mesh_paths(); |
eb2b9311 | 590 | |
349eb8cf JB |
591 | hash_idx = mesh_table_hash(dst, sdata, tbl); |
592 | bucket = &tbl->hash_buckets[hash_idx]; | |
eb2b9311 | 593 | |
349eb8cf | 594 | spin_lock_bh(&tbl->hashwlock[hash_idx]); |
eb2b9311 | 595 | |
402d7752 | 596 | err = -EEXIST; |
eb2b9311 LCC |
597 | hlist_for_each_entry(node, n, bucket, list) { |
598 | mpath = node->mpath; | |
f698d856 | 599 | if (mpath->sdata == sdata && memcmp(dst, mpath->dst, ETH_ALEN) == 0) |
402d7752 | 600 | goto err_exists; |
eb2b9311 LCC |
601 | } |
602 | ||
603 | hlist_add_head_rcu(&new_node->list, bucket); | |
349eb8cf JB |
604 | if (atomic_inc_return(&tbl->entries) >= |
605 | tbl->mean_chain_len * (tbl->hash_mask + 1)) | |
eb2b9311 LCC |
606 | grow = 1; |
607 | ||
f5ea9120 JB |
608 | mesh_paths_generation++; |
609 | ||
349eb8cf | 610 | spin_unlock_bh(&tbl->hashwlock[hash_idx]); |
9b84b808 | 611 | read_unlock_bh(&pathtbl_resize_lock); |
402d7752 | 612 | if (grow) { |
18889231 | 613 | set_bit(MESH_WORK_GROW_MPATH_TABLE, &ifmsh->wrkq_flags); |
64592c8f | 614 | ieee80211_queue_work(&local->hw, &sdata->work); |
eb2b9311 | 615 | } |
402d7752 PE |
616 | return 0; |
617 | ||
618 | err_exists: | |
349eb8cf | 619 | spin_unlock_bh(&tbl->hashwlock[hash_idx]); |
9b84b808 | 620 | read_unlock_bh(&pathtbl_resize_lock); |
402d7752 PE |
621 | kfree(new_node); |
622 | err_node_alloc: | |
623 | kfree(new_mpath); | |
624 | err_path_alloc: | |
472dbc45 | 625 | atomic_dec(&sdata->u.mesh.mpaths); |
eb2b9311 LCC |
626 | return err; |
627 | } | |
628 | ||
1928ecab JB |
629 | static void mesh_table_free_rcu(struct rcu_head *rcu) |
630 | { | |
631 | struct mesh_table *tbl = container_of(rcu, struct mesh_table, rcu_head); | |
632 | ||
633 | mesh_table_free(tbl, false); | |
634 | } | |
635 | ||
18889231 JC |
636 | void mesh_mpath_table_grow(void) |
637 | { | |
638 | struct mesh_table *oldtbl, *newtbl; | |
639 | ||
9b84b808 | 640 | write_lock_bh(&pathtbl_resize_lock); |
349eb8cf JB |
641 | oldtbl = resize_dereference_mesh_paths(); |
642 | newtbl = mesh_table_alloc(oldtbl->size_order + 1); | |
1928ecab JB |
643 | if (!newtbl) |
644 | goto out; | |
349eb8cf | 645 | if (mesh_table_grow(oldtbl, newtbl) < 0) { |
a3e6b12c | 646 | __mesh_table_free(newtbl); |
1928ecab | 647 | goto out; |
18889231 JC |
648 | } |
649 | rcu_assign_pointer(mesh_paths, newtbl); | |
18889231 | 650 | |
1928ecab JB |
651 | call_rcu(&oldtbl->rcu_head, mesh_table_free_rcu); |
652 | ||
653 | out: | |
654 | write_unlock_bh(&pathtbl_resize_lock); | |
18889231 JC |
655 | } |
656 | ||
657 | void mesh_mpp_table_grow(void) | |
658 | { | |
659 | struct mesh_table *oldtbl, *newtbl; | |
660 | ||
9b84b808 | 661 | write_lock_bh(&pathtbl_resize_lock); |
349eb8cf JB |
662 | oldtbl = resize_dereference_mpp_paths(); |
663 | newtbl = mesh_table_alloc(oldtbl->size_order + 1); | |
1928ecab JB |
664 | if (!newtbl) |
665 | goto out; | |
349eb8cf | 666 | if (mesh_table_grow(oldtbl, newtbl) < 0) { |
a3e6b12c | 667 | __mesh_table_free(newtbl); |
1928ecab | 668 | goto out; |
18889231 JC |
669 | } |
670 | rcu_assign_pointer(mpp_paths, newtbl); | |
1928ecab | 671 | call_rcu(&oldtbl->rcu_head, mesh_table_free_rcu); |
18889231 | 672 | |
1928ecab JB |
673 | out: |
674 | write_unlock_bh(&pathtbl_resize_lock); | |
18889231 | 675 | } |
eb2b9311 | 676 | |
79617dee Y |
677 | int mpp_path_add(u8 *dst, u8 *mpp, struct ieee80211_sub_if_data *sdata) |
678 | { | |
18889231 JC |
679 | struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh; |
680 | struct ieee80211_local *local = sdata->local; | |
349eb8cf | 681 | struct mesh_table *tbl; |
79617dee Y |
682 | struct mesh_path *mpath, *new_mpath; |
683 | struct mpath_node *node, *new_node; | |
684 | struct hlist_head *bucket; | |
685 | struct hlist_node *n; | |
686 | int grow = 0; | |
687 | int err = 0; | |
688 | u32 hash_idx; | |
689 | ||
47846c9b | 690 | if (memcmp(dst, sdata->vif.addr, ETH_ALEN) == 0) |
79617dee Y |
691 | /* never add ourselves as neighbours */ |
692 | return -ENOTSUPP; | |
693 | ||
694 | if (is_multicast_ether_addr(dst)) | |
695 | return -ENOTSUPP; | |
696 | ||
697 | err = -ENOMEM; | |
18889231 | 698 | new_mpath = kzalloc(sizeof(struct mesh_path), GFP_ATOMIC); |
79617dee Y |
699 | if (!new_mpath) |
700 | goto err_path_alloc; | |
701 | ||
18889231 | 702 | new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC); |
79617dee Y |
703 | if (!new_node) |
704 | goto err_node_alloc; | |
705 | ||
9b84b808 | 706 | read_lock_bh(&pathtbl_resize_lock); |
79617dee Y |
707 | memcpy(new_mpath->dst, dst, ETH_ALEN); |
708 | memcpy(new_mpath->mpp, mpp, ETH_ALEN); | |
709 | new_mpath->sdata = sdata; | |
710 | new_mpath->flags = 0; | |
711 | skb_queue_head_init(&new_mpath->frame_queue); | |
712 | new_node->mpath = new_mpath; | |
c6133661 | 713 | init_timer(&new_mpath->timer); |
79617dee Y |
714 | new_mpath->exp_time = jiffies; |
715 | spin_lock_init(&new_mpath->state_lock); | |
716 | ||
349eb8cf | 717 | tbl = resize_dereference_mpp_paths(); |
79617dee | 718 | |
349eb8cf JB |
719 | hash_idx = mesh_table_hash(dst, sdata, tbl); |
720 | bucket = &tbl->hash_buckets[hash_idx]; | |
721 | ||
722 | spin_lock_bh(&tbl->hashwlock[hash_idx]); | |
79617dee Y |
723 | |
724 | err = -EEXIST; | |
725 | hlist_for_each_entry(node, n, bucket, list) { | |
726 | mpath = node->mpath; | |
727 | if (mpath->sdata == sdata && memcmp(dst, mpath->dst, ETH_ALEN) == 0) | |
728 | goto err_exists; | |
729 | } | |
730 | ||
731 | hlist_add_head_rcu(&new_node->list, bucket); | |
349eb8cf JB |
732 | if (atomic_inc_return(&tbl->entries) >= |
733 | tbl->mean_chain_len * (tbl->hash_mask + 1)) | |
79617dee Y |
734 | grow = 1; |
735 | ||
349eb8cf | 736 | spin_unlock_bh(&tbl->hashwlock[hash_idx]); |
9b84b808 | 737 | read_unlock_bh(&pathtbl_resize_lock); |
79617dee | 738 | if (grow) { |
18889231 | 739 | set_bit(MESH_WORK_GROW_MPP_TABLE, &ifmsh->wrkq_flags); |
64592c8f | 740 | ieee80211_queue_work(&local->hw, &sdata->work); |
79617dee Y |
741 | } |
742 | return 0; | |
743 | ||
744 | err_exists: | |
349eb8cf | 745 | spin_unlock_bh(&tbl->hashwlock[hash_idx]); |
9b84b808 | 746 | read_unlock_bh(&pathtbl_resize_lock); |
79617dee Y |
747 | kfree(new_node); |
748 | err_node_alloc: | |
749 | kfree(new_mpath); | |
750 | err_path_alloc: | |
751 | return err; | |
752 | } | |
753 | ||
754 | ||
eb2b9311 LCC |
755 | /** |
756 | * mesh_plink_broken - deactivates paths and sends perr when a link breaks | |
757 | * | |
758 | * @sta: broken peer link | |
759 | * | |
760 | * This function must be called from the rate control algorithm if enough | |
761 | * delivery errors suggest that a peer link is no longer usable. | |
762 | */ | |
763 | void mesh_plink_broken(struct sta_info *sta) | |
764 | { | |
349eb8cf | 765 | struct mesh_table *tbl; |
15ff6365 | 766 | static const u8 bcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; |
eb2b9311 LCC |
767 | struct mesh_path *mpath; |
768 | struct mpath_node *node; | |
769 | struct hlist_node *p; | |
f698d856 | 770 | struct ieee80211_sub_if_data *sdata = sta->sdata; |
eb2b9311 | 771 | int i; |
25d49e4d | 772 | __le16 reason = cpu_to_le16(WLAN_REASON_MESH_PATH_DEST_UNREACHABLE); |
eb2b9311 LCC |
773 | |
774 | rcu_read_lock(); | |
349eb8cf JB |
775 | tbl = rcu_dereference(mesh_paths); |
776 | for_each_mesh_entry(tbl, p, node, i) { | |
eb2b9311 | 777 | mpath = node->mpath; |
349eb8cf | 778 | if (rcu_dereference(mpath->next_hop) == sta && |
eb2b9311 LCC |
779 | mpath->flags & MESH_PATH_ACTIVE && |
780 | !(mpath->flags & MESH_PATH_FIXED)) { | |
f5e50cd0 | 781 | spin_lock_bh(&mpath->state_lock); |
eb2b9311 | 782 | mpath->flags &= ~MESH_PATH_ACTIVE; |
d19b3bf6 | 783 | ++mpath->sn; |
eb2b9311 | 784 | spin_unlock_bh(&mpath->state_lock); |
45904f21 JC |
785 | mesh_path_error_tx(sdata->u.mesh.mshcfg.element_ttl, |
786 | mpath->dst, cpu_to_le32(mpath->sn), | |
25d49e4d | 787 | reason, bcast, sdata); |
f5e50cd0 | 788 | } |
eb2b9311 LCC |
789 | } |
790 | rcu_read_unlock(); | |
791 | } | |
792 | ||
793 | /** | |
794 | * mesh_path_flush_by_nexthop - Deletes mesh paths if their next hop matches | |
795 | * | |
796 | * @sta - mesh peer to match | |
797 | * | |
b4e08ea1 LCC |
798 | * RCU notes: this function is called when a mesh plink transitions from |
799 | * PLINK_ESTAB to any other state, since PLINK_ESTAB state is the only one that | |
800 | * allows path creation. This will happen before the sta can be freed (because | |
d0709a65 JB |
801 | * sta_info_destroy() calls this) so any reader in a rcu read block will be |
802 | * protected against the plink disappearing. | |
eb2b9311 LCC |
803 | */ |
804 | void mesh_path_flush_by_nexthop(struct sta_info *sta) | |
805 | { | |
349eb8cf | 806 | struct mesh_table *tbl; |
eb2b9311 LCC |
807 | struct mesh_path *mpath; |
808 | struct mpath_node *node; | |
809 | struct hlist_node *p; | |
810 | int i; | |
811 | ||
349eb8cf JB |
812 | rcu_read_lock(); |
813 | tbl = rcu_dereference(mesh_paths); | |
814 | for_each_mesh_entry(tbl, p, node, i) { | |
eb2b9311 | 815 | mpath = node->mpath; |
349eb8cf | 816 | if (rcu_dereference(mpath->next_hop) == sta) |
f698d856 | 817 | mesh_path_del(mpath->dst, mpath->sdata); |
eb2b9311 | 818 | } |
349eb8cf | 819 | rcu_read_unlock(); |
eb2b9311 LCC |
820 | } |
821 | ||
19c50b3d JC |
822 | static void mesh_path_node_reclaim(struct rcu_head *rp) |
823 | { | |
824 | struct mpath_node *node = container_of(rp, struct mpath_node, rcu); | |
825 | struct ieee80211_sub_if_data *sdata = node->mpath->sdata; | |
826 | ||
827 | del_timer_sync(&node->mpath->timer); | |
828 | atomic_dec(&sdata->u.mesh.mpaths); | |
829 | kfree(node->mpath); | |
830 | kfree(node); | |
831 | } | |
832 | ||
833 | /* needs to be called with the corresponding hashwlock taken */ | |
834 | static void __mesh_path_del(struct mesh_table *tbl, struct mpath_node *node) | |
835 | { | |
836 | struct mesh_path *mpath; | |
837 | mpath = node->mpath; | |
838 | spin_lock(&mpath->state_lock); | |
839 | mpath->flags |= MESH_PATH_RESOLVING; | |
840 | if (mpath->is_gate) | |
841 | mesh_gate_del(tbl, mpath); | |
842 | hlist_del_rcu(&node->list); | |
843 | call_rcu(&node->rcu, mesh_path_node_reclaim); | |
844 | spin_unlock(&mpath->state_lock); | |
845 | atomic_dec(&tbl->entries); | |
846 | } | |
847 | ||
ece1a2e7 | 848 | static void mesh_path_flush(struct ieee80211_sub_if_data *sdata) |
eb2b9311 | 849 | { |
349eb8cf | 850 | struct mesh_table *tbl; |
eb2b9311 LCC |
851 | struct mesh_path *mpath; |
852 | struct mpath_node *node; | |
853 | struct hlist_node *p; | |
854 | int i; | |
855 | ||
349eb8cf JB |
856 | rcu_read_lock(); |
857 | tbl = rcu_dereference(mesh_paths); | |
858 | for_each_mesh_entry(tbl, p, node, i) { | |
eb2b9311 | 859 | mpath = node->mpath; |
19c50b3d JC |
860 | if (mpath->sdata == sdata) { |
861 | spin_lock_bh(&tbl->hashwlock[i]); | |
862 | __mesh_path_del(tbl, node); | |
863 | spin_unlock_bh(&tbl->hashwlock[i]); | |
864 | } | |
eb2b9311 | 865 | } |
349eb8cf | 866 | rcu_read_unlock(); |
eb2b9311 LCC |
867 | } |
868 | ||
ece1a2e7 JC |
869 | static void mpp_path_flush(struct ieee80211_sub_if_data *sdata) |
870 | { | |
871 | struct mesh_table *tbl; | |
872 | struct mesh_path *mpath; | |
873 | struct mpath_node *node; | |
874 | struct hlist_node *p; | |
875 | int i; | |
876 | ||
877 | read_lock_bh(&pathtbl_resize_lock); | |
878 | tbl = rcu_dereference_protected(mpp_paths, | |
879 | lockdep_is_held(pathtbl_resize_lock)); | |
880 | for_each_mesh_entry(tbl, p, node, i) { | |
881 | mpath = node->mpath; | |
ece1a2e7 | 882 | spin_lock_bh(&tbl->hashwlock[i]); |
19c50b3d | 883 | __mesh_path_del(tbl, node); |
ece1a2e7 JC |
884 | spin_unlock_bh(&tbl->hashwlock[i]); |
885 | } | |
886 | read_unlock_bh(&pathtbl_resize_lock); | |
887 | } | |
888 | ||
889 | /** | |
890 | * mesh_path_flush_by_iface - Deletes all mesh paths associated with a given iface | |
891 | * | |
892 | * This function deletes both mesh paths as well as mesh portal paths. | |
893 | * | |
894 | * @sdata - interface data to match | |
895 | * | |
896 | */ | |
897 | void mesh_path_flush_by_iface(struct ieee80211_sub_if_data *sdata) | |
898 | { | |
899 | mesh_path_flush(sdata); | |
900 | mpp_path_flush(sdata); | |
901 | } | |
902 | ||
eb2b9311 LCC |
903 | /** |
904 | * mesh_path_del - delete a mesh path from the table | |
905 | * | |
906 | * @addr: dst address (ETH_ALEN length) | |
f698d856 | 907 | * @sdata: local subif |
eb2b9311 | 908 | * |
af901ca1 | 909 | * Returns: 0 if successful |
eb2b9311 | 910 | */ |
f698d856 | 911 | int mesh_path_del(u8 *addr, struct ieee80211_sub_if_data *sdata) |
eb2b9311 | 912 | { |
349eb8cf | 913 | struct mesh_table *tbl; |
eb2b9311 LCC |
914 | struct mesh_path *mpath; |
915 | struct mpath_node *node; | |
916 | struct hlist_head *bucket; | |
917 | struct hlist_node *n; | |
918 | int hash_idx; | |
919 | int err = 0; | |
920 | ||
9b84b808 | 921 | read_lock_bh(&pathtbl_resize_lock); |
349eb8cf JB |
922 | tbl = resize_dereference_mesh_paths(); |
923 | hash_idx = mesh_table_hash(addr, sdata, tbl); | |
924 | bucket = &tbl->hash_buckets[hash_idx]; | |
eb2b9311 | 925 | |
349eb8cf | 926 | spin_lock_bh(&tbl->hashwlock[hash_idx]); |
eb2b9311 LCC |
927 | hlist_for_each_entry(node, n, bucket, list) { |
928 | mpath = node->mpath; | |
f698d856 | 929 | if (mpath->sdata == sdata && |
349eb8cf | 930 | memcmp(addr, mpath->dst, ETH_ALEN) == 0) { |
19c50b3d | 931 | __mesh_path_del(tbl, node); |
eb2b9311 LCC |
932 | goto enddel; |
933 | } | |
934 | } | |
935 | ||
936 | err = -ENXIO; | |
937 | enddel: | |
f5ea9120 | 938 | mesh_paths_generation++; |
349eb8cf | 939 | spin_unlock_bh(&tbl->hashwlock[hash_idx]); |
9b84b808 | 940 | read_unlock_bh(&pathtbl_resize_lock); |
eb2b9311 LCC |
941 | return err; |
942 | } | |
943 | ||
944 | /** | |
945 | * mesh_path_tx_pending - sends pending frames in a mesh path queue | |
946 | * | |
947 | * @mpath: mesh path to activate | |
948 | * | |
949 | * Locking: the state_lock of the mpath structure must NOT be held when calling | |
950 | * this function. | |
951 | */ | |
952 | void mesh_path_tx_pending(struct mesh_path *mpath) | |
953 | { | |
249b405c JC |
954 | if (mpath->flags & MESH_PATH_ACTIVE) |
955 | ieee80211_add_pending_skbs(mpath->sdata->local, | |
956 | &mpath->frame_queue); | |
eb2b9311 LCC |
957 | } |
958 | ||
5ee68e5b JC |
959 | /** |
960 | * mesh_path_send_to_gates - sends pending frames to all known mesh gates | |
961 | * | |
962 | * @mpath: mesh path whose queue will be emptied | |
963 | * | |
964 | * If there is only one gate, the frames are transferred from the failed mpath | |
965 | * queue to that gate's queue. If there are more than one gates, the frames | |
966 | * are copied from each gate to the next. After frames are copied, the | |
967 | * mpath queues are emptied onto the transmission queue. | |
968 | */ | |
969 | int mesh_path_send_to_gates(struct mesh_path *mpath) | |
970 | { | |
971 | struct ieee80211_sub_if_data *sdata = mpath->sdata; | |
972 | struct hlist_node *n; | |
973 | struct mesh_table *tbl; | |
974 | struct mesh_path *from_mpath = mpath; | |
975 | struct mpath_node *gate = NULL; | |
976 | bool copy = false; | |
977 | struct hlist_head *known_gates; | |
978 | ||
979 | rcu_read_lock(); | |
980 | tbl = rcu_dereference(mesh_paths); | |
981 | known_gates = tbl->known_gates; | |
982 | rcu_read_unlock(); | |
983 | ||
984 | if (!known_gates) | |
985 | return -EHOSTUNREACH; | |
986 | ||
987 | hlist_for_each_entry_rcu(gate, n, known_gates, list) { | |
988 | if (gate->mpath->sdata != sdata) | |
989 | continue; | |
990 | ||
991 | if (gate->mpath->flags & MESH_PATH_ACTIVE) { | |
992 | mpath_dbg("Forwarding to %pM\n", gate->mpath->dst); | |
993 | mesh_path_move_to_queue(gate->mpath, from_mpath, copy); | |
994 | from_mpath = gate->mpath; | |
995 | copy = true; | |
996 | } else { | |
997 | mpath_dbg("Not forwarding %p\n", gate->mpath); | |
998 | mpath_dbg("flags %x\n", gate->mpath->flags); | |
999 | } | |
1000 | } | |
1001 | ||
1002 | hlist_for_each_entry_rcu(gate, n, known_gates, list) | |
1003 | if (gate->mpath->sdata == sdata) { | |
1004 | mpath_dbg("Sending to %pM\n", gate->mpath->dst); | |
1005 | mesh_path_tx_pending(gate->mpath); | |
1006 | } | |
1007 | ||
1008 | return (from_mpath == mpath) ? -EHOSTUNREACH : 0; | |
1009 | } | |
1010 | ||
eb2b9311 LCC |
1011 | /** |
1012 | * mesh_path_discard_frame - discard a frame whose path could not be resolved | |
1013 | * | |
1014 | * @skb: frame to discard | |
f698d856 | 1015 | * @sdata: network subif the frame was to be sent through |
eb2b9311 | 1016 | * |
35946a57 JC |
1017 | * If the frame was being forwarded from another MP, a PERR frame will be sent |
1018 | * to the precursor. The precursor's address (i.e. the previous hop) was saved | |
1019 | * in addr1 of the frame-to-be-forwarded, and would only be overwritten once | |
1020 | * the destination is successfully resolved. | |
eb2b9311 LCC |
1021 | * |
1022 | * Locking: the function must me called within a rcu_read_lock region | |
1023 | */ | |
f698d856 JBG |
1024 | void mesh_path_discard_frame(struct sk_buff *skb, |
1025 | struct ieee80211_sub_if_data *sdata) | |
eb2b9311 | 1026 | { |
e32f85f7 | 1027 | struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data; |
eb2b9311 | 1028 | struct mesh_path *mpath; |
d19b3bf6 | 1029 | u32 sn = 0; |
25d49e4d | 1030 | __le16 reason = cpu_to_le16(WLAN_REASON_MESH_PATH_NOFORWARD); |
eb2b9311 | 1031 | |
47846c9b | 1032 | if (memcmp(hdr->addr4, sdata->vif.addr, ETH_ALEN) != 0) { |
eb2b9311 LCC |
1033 | u8 *ra, *da; |
1034 | ||
e32f85f7 | 1035 | da = hdr->addr3; |
35946a57 | 1036 | ra = hdr->addr1; |
af089c15 | 1037 | rcu_read_lock(); |
f698d856 | 1038 | mpath = mesh_path_lookup(da, sdata); |
af089c15 JC |
1039 | if (mpath) { |
1040 | spin_lock_bh(&mpath->state_lock); | |
d19b3bf6 | 1041 | sn = ++mpath->sn; |
af089c15 JC |
1042 | spin_unlock_bh(&mpath->state_lock); |
1043 | } | |
1044 | rcu_read_unlock(); | |
45904f21 | 1045 | mesh_path_error_tx(sdata->u.mesh.mshcfg.element_ttl, skb->data, |
25d49e4d | 1046 | cpu_to_le32(sn), reason, ra, sdata); |
eb2b9311 LCC |
1047 | } |
1048 | ||
1049 | kfree_skb(skb); | |
472dbc45 | 1050 | sdata->u.mesh.mshstats.dropped_frames_no_route++; |
eb2b9311 LCC |
1051 | } |
1052 | ||
1053 | /** | |
1054 | * mesh_path_flush_pending - free the pending queue of a mesh path | |
1055 | * | |
1056 | * @mpath: mesh path whose queue has to be freed | |
1057 | * | |
25985edc | 1058 | * Locking: the function must me called within a rcu_read_lock region |
eb2b9311 LCC |
1059 | */ |
1060 | void mesh_path_flush_pending(struct mesh_path *mpath) | |
1061 | { | |
eb2b9311 LCC |
1062 | struct sk_buff *skb; |
1063 | ||
00e3f25c | 1064 | while ((skb = skb_dequeue(&mpath->frame_queue)) != NULL) |
f698d856 | 1065 | mesh_path_discard_frame(skb, mpath->sdata); |
eb2b9311 LCC |
1066 | } |
1067 | ||
1068 | /** | |
1069 | * mesh_path_fix_nexthop - force a specific next hop for a mesh path | |
1070 | * | |
1071 | * @mpath: the mesh path to modify | |
1072 | * @next_hop: the next hop to force | |
1073 | * | |
1074 | * Locking: this function must be called holding mpath->state_lock | |
1075 | */ | |
1076 | void mesh_path_fix_nexthop(struct mesh_path *mpath, struct sta_info *next_hop) | |
1077 | { | |
1078 | spin_lock_bh(&mpath->state_lock); | |
1079 | mesh_path_assign_nexthop(mpath, next_hop); | |
d19b3bf6 | 1080 | mpath->sn = 0xffff; |
eb2b9311 LCC |
1081 | mpath->metric = 0; |
1082 | mpath->hop_count = 0; | |
1083 | mpath->exp_time = 0; | |
1084 | mpath->flags |= MESH_PATH_FIXED; | |
1085 | mesh_path_activate(mpath); | |
1086 | spin_unlock_bh(&mpath->state_lock); | |
1087 | mesh_path_tx_pending(mpath); | |
1088 | } | |
1089 | ||
1090 | static void mesh_path_node_free(struct hlist_node *p, bool free_leafs) | |
1091 | { | |
1092 | struct mesh_path *mpath; | |
1093 | struct mpath_node *node = hlist_entry(p, struct mpath_node, list); | |
1094 | mpath = node->mpath; | |
1095 | hlist_del_rcu(p); | |
d0df9eec | 1096 | if (free_leafs) { |
c6133661 | 1097 | del_timer_sync(&mpath->timer); |
eb2b9311 | 1098 | kfree(mpath); |
d0df9eec | 1099 | } |
eb2b9311 LCC |
1100 | kfree(node); |
1101 | } | |
1102 | ||
4caf86c6 | 1103 | static int mesh_path_node_copy(struct hlist_node *p, struct mesh_table *newtbl) |
eb2b9311 LCC |
1104 | { |
1105 | struct mesh_path *mpath; | |
1106 | struct mpath_node *node, *new_node; | |
1107 | u32 hash_idx; | |
1108 | ||
8566dc3f | 1109 | new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC); |
00242c40 PE |
1110 | if (new_node == NULL) |
1111 | return -ENOMEM; | |
1112 | ||
eb2b9311 LCC |
1113 | node = hlist_entry(p, struct mpath_node, list); |
1114 | mpath = node->mpath; | |
eb2b9311 | 1115 | new_node->mpath = mpath; |
f698d856 | 1116 | hash_idx = mesh_table_hash(mpath->dst, mpath->sdata, newtbl); |
eb2b9311 LCC |
1117 | hlist_add_head(&new_node->list, |
1118 | &newtbl->hash_buckets[hash_idx]); | |
4caf86c6 | 1119 | return 0; |
eb2b9311 LCC |
1120 | } |
1121 | ||
1122 | int mesh_pathtbl_init(void) | |
1123 | { | |
349eb8cf JB |
1124 | struct mesh_table *tbl_path, *tbl_mpp; |
1125 | ||
1126 | tbl_path = mesh_table_alloc(INIT_PATHS_SIZE_ORDER); | |
1127 | if (!tbl_path) | |
79617dee | 1128 | return -ENOMEM; |
349eb8cf JB |
1129 | tbl_path->free_node = &mesh_path_node_free; |
1130 | tbl_path->copy_node = &mesh_path_node_copy; | |
1131 | tbl_path->mean_chain_len = MEAN_CHAIN_LEN; | |
5ee68e5b JC |
1132 | tbl_path->known_gates = kzalloc(sizeof(struct hlist_head), GFP_ATOMIC); |
1133 | INIT_HLIST_HEAD(tbl_path->known_gates); | |
1134 | ||
79617dee | 1135 | |
349eb8cf JB |
1136 | tbl_mpp = mesh_table_alloc(INIT_PATHS_SIZE_ORDER); |
1137 | if (!tbl_mpp) { | |
1138 | mesh_table_free(tbl_path, true); | |
eb2b9311 | 1139 | return -ENOMEM; |
79617dee | 1140 | } |
349eb8cf JB |
1141 | tbl_mpp->free_node = &mesh_path_node_free; |
1142 | tbl_mpp->copy_node = &mesh_path_node_copy; | |
1143 | tbl_mpp->mean_chain_len = MEAN_CHAIN_LEN; | |
5ee68e5b JC |
1144 | tbl_mpp->known_gates = kzalloc(sizeof(struct hlist_head), GFP_ATOMIC); |
1145 | INIT_HLIST_HEAD(tbl_mpp->known_gates); | |
349eb8cf JB |
1146 | |
1147 | /* Need no locking since this is during init */ | |
1148 | RCU_INIT_POINTER(mesh_paths, tbl_path); | |
1149 | RCU_INIT_POINTER(mpp_paths, tbl_mpp); | |
79617dee | 1150 | |
eb2b9311 LCC |
1151 | return 0; |
1152 | } | |
1153 | ||
f698d856 | 1154 | void mesh_path_expire(struct ieee80211_sub_if_data *sdata) |
eb2b9311 | 1155 | { |
349eb8cf | 1156 | struct mesh_table *tbl; |
eb2b9311 LCC |
1157 | struct mesh_path *mpath; |
1158 | struct mpath_node *node; | |
1159 | struct hlist_node *p; | |
1160 | int i; | |
1161 | ||
349eb8cf JB |
1162 | rcu_read_lock(); |
1163 | tbl = rcu_dereference(mesh_paths); | |
1164 | for_each_mesh_entry(tbl, p, node, i) { | |
f698d856 | 1165 | if (node->mpath->sdata != sdata) |
eb2b9311 LCC |
1166 | continue; |
1167 | mpath = node->mpath; | |
eb2b9311 LCC |
1168 | if ((!(mpath->flags & MESH_PATH_RESOLVING)) && |
1169 | (!(mpath->flags & MESH_PATH_FIXED)) && | |
f5e50cd0 | 1170 | time_after(jiffies, mpath->exp_time + MESH_PATH_EXPIRE)) |
f698d856 | 1171 | mesh_path_del(mpath->dst, mpath->sdata); |
19c50b3d | 1172 | } |
349eb8cf | 1173 | rcu_read_unlock(); |
eb2b9311 LCC |
1174 | } |
1175 | ||
1176 | void mesh_pathtbl_unregister(void) | |
1177 | { | |
349eb8cf JB |
1178 | /* no need for locking during exit path */ |
1179 | mesh_table_free(rcu_dereference_raw(mesh_paths), true); | |
1180 | mesh_table_free(rcu_dereference_raw(mpp_paths), true); | |
eb2b9311 | 1181 | } |