]>
Commit | Line | Data |
---|---|---|
eb2b9311 LCC |
1 | /* |
2 | * Copyright (c) 2008 open80211s Ltd. | |
3 | * Author: Luis Carlos Cobo <[email protected]> | |
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 LCC |
12 | #include <linux/random.h> |
13 | #include <linux/spinlock.h> | |
14 | #include <linux/string.h> | |
15 | #include <net/mac80211.h> | |
16 | #include "ieee80211_i.h" | |
17 | #include "mesh.h" | |
18 | ||
19 | /* There will be initially 2^INIT_PATHS_SIZE_ORDER buckets */ | |
20 | #define INIT_PATHS_SIZE_ORDER 2 | |
21 | ||
22 | /* Keep the mean chain length below this constant */ | |
23 | #define MEAN_CHAIN_LEN 2 | |
24 | ||
25 | #define MPATH_EXPIRED(mpath) ((mpath->flags & MESH_PATH_ACTIVE) && \ | |
26 | time_after(jiffies, mpath->exp_time) && \ | |
27 | !(mpath->flags & MESH_PATH_FIXED)) | |
28 | ||
29 | struct mpath_node { | |
30 | struct hlist_node list; | |
31 | struct rcu_head rcu; | |
32 | /* This indirection allows two different tables to point to the same | |
33 | * mesh_path structure, useful when resizing | |
34 | */ | |
35 | struct mesh_path *mpath; | |
36 | }; | |
37 | ||
38 | static struct mesh_table *mesh_paths; | |
79617dee | 39 | static struct mesh_table *mpp_paths; /* Store paths for MPP&MAP */ |
eb2b9311 LCC |
40 | |
41 | /* This lock will have the grow table function as writer and add / delete nodes | |
42 | * as readers. When reading the table (i.e. doing lookups) we are well protected | |
43 | * by RCU | |
44 | */ | |
45 | static DEFINE_RWLOCK(pathtbl_resize_lock); | |
46 | ||
47 | /** | |
48 | * | |
49 | * mesh_path_assign_nexthop - update mesh path next hop | |
50 | * | |
51 | * @mpath: mesh path to update | |
52 | * @sta: next hop to assign | |
53 | * | |
54 | * Locking: mpath->state_lock must be held when calling this function | |
55 | */ | |
56 | void mesh_path_assign_nexthop(struct mesh_path *mpath, struct sta_info *sta) | |
57 | { | |
10c836d7 JC |
58 | struct sk_buff *skb; |
59 | struct ieee80211_hdr *hdr; | |
60 | struct sk_buff_head tmpq; | |
61 | unsigned long flags; | |
62 | ||
d0709a65 | 63 | rcu_assign_pointer(mpath->next_hop, sta); |
10c836d7 JC |
64 | |
65 | __skb_queue_head_init(&tmpq); | |
66 | ||
67 | spin_lock_irqsave(&mpath->frame_queue.lock, flags); | |
68 | ||
69 | while ((skb = __skb_dequeue(&mpath->frame_queue)) != NULL) { | |
70 | hdr = (struct ieee80211_hdr *) skb->data; | |
71 | memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN); | |
72 | __skb_queue_tail(&tmpq, skb); | |
73 | } | |
74 | ||
75 | skb_queue_splice(&tmpq, &mpath->frame_queue); | |
76 | spin_unlock_irqrestore(&mpath->frame_queue.lock, flags); | |
eb2b9311 LCC |
77 | } |
78 | ||
79 | ||
80 | /** | |
81 | * mesh_path_lookup - look up a path in the mesh path table | |
82 | * @dst: hardware address (ETH_ALEN length) of destination | |
f698d856 | 83 | * @sdata: local subif |
eb2b9311 LCC |
84 | * |
85 | * Returns: pointer to the mesh path structure, or NULL if not found | |
86 | * | |
87 | * Locking: must be called within a read rcu section. | |
88 | */ | |
f698d856 | 89 | struct mesh_path *mesh_path_lookup(u8 *dst, struct ieee80211_sub_if_data *sdata) |
eb2b9311 LCC |
90 | { |
91 | struct mesh_path *mpath; | |
92 | struct hlist_node *n; | |
93 | struct hlist_head *bucket; | |
94 | struct mesh_table *tbl; | |
95 | struct mpath_node *node; | |
96 | ||
97 | tbl = rcu_dereference(mesh_paths); | |
98 | ||
f698d856 | 99 | bucket = &tbl->hash_buckets[mesh_table_hash(dst, sdata, tbl)]; |
eb2b9311 LCC |
100 | hlist_for_each_entry_rcu(node, n, bucket, list) { |
101 | mpath = node->mpath; | |
f698d856 | 102 | if (mpath->sdata == sdata && |
eb2b9311 LCC |
103 | memcmp(dst, mpath->dst, ETH_ALEN) == 0) { |
104 | if (MPATH_EXPIRED(mpath)) { | |
105 | spin_lock_bh(&mpath->state_lock); | |
106 | if (MPATH_EXPIRED(mpath)) | |
107 | mpath->flags &= ~MESH_PATH_ACTIVE; | |
108 | spin_unlock_bh(&mpath->state_lock); | |
109 | } | |
110 | return mpath; | |
111 | } | |
112 | } | |
113 | return NULL; | |
114 | } | |
115 | ||
79617dee Y |
116 | struct mesh_path *mpp_path_lookup(u8 *dst, struct ieee80211_sub_if_data *sdata) |
117 | { | |
118 | struct mesh_path *mpath; | |
119 | struct hlist_node *n; | |
120 | struct hlist_head *bucket; | |
121 | struct mesh_table *tbl; | |
122 | struct mpath_node *node; | |
123 | ||
124 | tbl = rcu_dereference(mpp_paths); | |
125 | ||
126 | bucket = &tbl->hash_buckets[mesh_table_hash(dst, sdata, tbl)]; | |
127 | hlist_for_each_entry_rcu(node, n, bucket, list) { | |
128 | mpath = node->mpath; | |
129 | if (mpath->sdata == sdata && | |
130 | memcmp(dst, mpath->dst, ETH_ALEN) == 0) { | |
131 | if (MPATH_EXPIRED(mpath)) { | |
132 | spin_lock_bh(&mpath->state_lock); | |
133 | if (MPATH_EXPIRED(mpath)) | |
134 | mpath->flags &= ~MESH_PATH_ACTIVE; | |
135 | spin_unlock_bh(&mpath->state_lock); | |
136 | } | |
137 | return mpath; | |
138 | } | |
139 | } | |
140 | return NULL; | |
141 | } | |
142 | ||
143 | ||
eb2b9311 LCC |
144 | /** |
145 | * mesh_path_lookup_by_idx - look up a path in the mesh path table by its index | |
146 | * @idx: index | |
f698d856 | 147 | * @sdata: local subif, or NULL for all entries |
eb2b9311 LCC |
148 | * |
149 | * Returns: pointer to the mesh path structure, or NULL if not found. | |
150 | * | |
151 | * Locking: must be called within a read rcu section. | |
152 | */ | |
f698d856 | 153 | struct mesh_path *mesh_path_lookup_by_idx(int idx, struct ieee80211_sub_if_data *sdata) |
eb2b9311 LCC |
154 | { |
155 | struct mpath_node *node; | |
156 | struct hlist_node *p; | |
157 | int i; | |
158 | int j = 0; | |
159 | ||
2a8ca29a | 160 | for_each_mesh_entry(mesh_paths, p, node, i) { |
f698d856 | 161 | if (sdata && node->mpath->sdata != sdata) |
2a8ca29a | 162 | continue; |
eb2b9311 LCC |
163 | if (j++ == idx) { |
164 | if (MPATH_EXPIRED(node->mpath)) { | |
165 | spin_lock_bh(&node->mpath->state_lock); | |
166 | if (MPATH_EXPIRED(node->mpath)) | |
167 | node->mpath->flags &= ~MESH_PATH_ACTIVE; | |
168 | spin_unlock_bh(&node->mpath->state_lock); | |
169 | } | |
170 | return node->mpath; | |
171 | } | |
2a8ca29a | 172 | } |
eb2b9311 LCC |
173 | |
174 | return NULL; | |
175 | } | |
176 | ||
177 | /** | |
178 | * mesh_path_add - allocate and add a new path to the mesh path table | |
179 | * @addr: destination address of the path (ETH_ALEN length) | |
f698d856 | 180 | * @sdata: local subif |
eb2b9311 LCC |
181 | * |
182 | * Returns: 0 on sucess | |
183 | * | |
184 | * State: the initial state of the new path is set to 0 | |
185 | */ | |
f698d856 | 186 | int mesh_path_add(u8 *dst, struct ieee80211_sub_if_data *sdata) |
eb2b9311 | 187 | { |
eb2b9311 LCC |
188 | struct mesh_path *mpath, *new_mpath; |
189 | struct mpath_node *node, *new_node; | |
190 | struct hlist_head *bucket; | |
191 | struct hlist_node *n; | |
192 | int grow = 0; | |
193 | int err = 0; | |
194 | u32 hash_idx; | |
195 | ||
e2e414d9 JB |
196 | might_sleep(); |
197 | ||
f698d856 | 198 | if (memcmp(dst, sdata->dev->dev_addr, ETH_ALEN) == 0) |
eb2b9311 LCC |
199 | /* never add ourselves as neighbours */ |
200 | return -ENOTSUPP; | |
201 | ||
202 | if (is_multicast_ether_addr(dst)) | |
203 | return -ENOTSUPP; | |
204 | ||
472dbc45 | 205 | if (atomic_add_unless(&sdata->u.mesh.mpaths, 1, MESH_MAX_MPATHS) == 0) |
eb2b9311 LCC |
206 | return -ENOSPC; |
207 | ||
402d7752 | 208 | err = -ENOMEM; |
eb2b9311 | 209 | new_mpath = kzalloc(sizeof(struct mesh_path), GFP_KERNEL); |
402d7752 PE |
210 | if (!new_mpath) |
211 | goto err_path_alloc; | |
212 | ||
0eb03d5a | 213 | new_node = kmalloc(sizeof(struct mpath_node), GFP_KERNEL); |
402d7752 PE |
214 | if (!new_node) |
215 | goto err_node_alloc; | |
f84e71a9 PE |
216 | |
217 | read_lock(&pathtbl_resize_lock); | |
eb2b9311 | 218 | memcpy(new_mpath->dst, dst, ETH_ALEN); |
f698d856 | 219 | new_mpath->sdata = sdata; |
eb2b9311 LCC |
220 | new_mpath->flags = 0; |
221 | skb_queue_head_init(&new_mpath->frame_queue); | |
eb2b9311 LCC |
222 | new_node->mpath = new_mpath; |
223 | new_mpath->timer.data = (unsigned long) new_mpath; | |
224 | new_mpath->timer.function = mesh_path_timer; | |
225 | new_mpath->exp_time = jiffies; | |
226 | spin_lock_init(&new_mpath->state_lock); | |
227 | init_timer(&new_mpath->timer); | |
228 | ||
f698d856 | 229 | hash_idx = mesh_table_hash(dst, sdata, mesh_paths); |
eb2b9311 LCC |
230 | bucket = &mesh_paths->hash_buckets[hash_idx]; |
231 | ||
232 | spin_lock(&mesh_paths->hashwlock[hash_idx]); | |
233 | ||
402d7752 | 234 | err = -EEXIST; |
eb2b9311 LCC |
235 | hlist_for_each_entry(node, n, bucket, list) { |
236 | mpath = node->mpath; | |
f698d856 | 237 | if (mpath->sdata == sdata && memcmp(dst, mpath->dst, ETH_ALEN) == 0) |
402d7752 | 238 | goto err_exists; |
eb2b9311 LCC |
239 | } |
240 | ||
241 | hlist_add_head_rcu(&new_node->list, bucket); | |
242 | if (atomic_inc_return(&mesh_paths->entries) >= | |
243 | mesh_paths->mean_chain_len * (mesh_paths->hash_mask + 1)) | |
244 | grow = 1; | |
245 | ||
eb2b9311 | 246 | spin_unlock(&mesh_paths->hashwlock[hash_idx]); |
eb2b9311 | 247 | read_unlock(&pathtbl_resize_lock); |
402d7752 | 248 | if (grow) { |
eb2b9311 LCC |
249 | struct mesh_table *oldtbl, *newtbl; |
250 | ||
251 | write_lock(&pathtbl_resize_lock); | |
252 | oldtbl = mesh_paths; | |
253 | newtbl = mesh_table_grow(mesh_paths); | |
254 | if (!newtbl) { | |
255 | write_unlock(&pathtbl_resize_lock); | |
3282aea9 | 256 | return 0; |
eb2b9311 LCC |
257 | } |
258 | rcu_assign_pointer(mesh_paths, newtbl); | |
6d6936e2 PE |
259 | write_unlock(&pathtbl_resize_lock); |
260 | ||
eb2b9311 LCC |
261 | synchronize_rcu(); |
262 | mesh_table_free(oldtbl, false); | |
eb2b9311 | 263 | } |
402d7752 PE |
264 | return 0; |
265 | ||
266 | err_exists: | |
267 | spin_unlock(&mesh_paths->hashwlock[hash_idx]); | |
268 | read_unlock(&pathtbl_resize_lock); | |
269 | kfree(new_node); | |
270 | err_node_alloc: | |
271 | kfree(new_mpath); | |
272 | err_path_alloc: | |
472dbc45 | 273 | atomic_dec(&sdata->u.mesh.mpaths); |
eb2b9311 LCC |
274 | return err; |
275 | } | |
276 | ||
277 | ||
79617dee Y |
278 | int mpp_path_add(u8 *dst, u8 *mpp, struct ieee80211_sub_if_data *sdata) |
279 | { | |
280 | struct mesh_path *mpath, *new_mpath; | |
281 | struct mpath_node *node, *new_node; | |
282 | struct hlist_head *bucket; | |
283 | struct hlist_node *n; | |
284 | int grow = 0; | |
285 | int err = 0; | |
286 | u32 hash_idx; | |
287 | ||
e2e414d9 | 288 | might_sleep(); |
79617dee Y |
289 | |
290 | if (memcmp(dst, sdata->dev->dev_addr, ETH_ALEN) == 0) | |
291 | /* never add ourselves as neighbours */ | |
292 | return -ENOTSUPP; | |
293 | ||
294 | if (is_multicast_ether_addr(dst)) | |
295 | return -ENOTSUPP; | |
296 | ||
297 | err = -ENOMEM; | |
298 | new_mpath = kzalloc(sizeof(struct mesh_path), GFP_KERNEL); | |
299 | if (!new_mpath) | |
300 | goto err_path_alloc; | |
301 | ||
302 | new_node = kmalloc(sizeof(struct mpath_node), GFP_KERNEL); | |
303 | if (!new_node) | |
304 | goto err_node_alloc; | |
305 | ||
306 | read_lock(&pathtbl_resize_lock); | |
307 | memcpy(new_mpath->dst, dst, ETH_ALEN); | |
308 | memcpy(new_mpath->mpp, mpp, ETH_ALEN); | |
309 | new_mpath->sdata = sdata; | |
310 | new_mpath->flags = 0; | |
311 | skb_queue_head_init(&new_mpath->frame_queue); | |
312 | new_node->mpath = new_mpath; | |
313 | new_mpath->exp_time = jiffies; | |
314 | spin_lock_init(&new_mpath->state_lock); | |
315 | ||
316 | hash_idx = mesh_table_hash(dst, sdata, mpp_paths); | |
317 | bucket = &mpp_paths->hash_buckets[hash_idx]; | |
318 | ||
319 | spin_lock(&mpp_paths->hashwlock[hash_idx]); | |
320 | ||
321 | err = -EEXIST; | |
322 | hlist_for_each_entry(node, n, bucket, list) { | |
323 | mpath = node->mpath; | |
324 | if (mpath->sdata == sdata && memcmp(dst, mpath->dst, ETH_ALEN) == 0) | |
325 | goto err_exists; | |
326 | } | |
327 | ||
328 | hlist_add_head_rcu(&new_node->list, bucket); | |
329 | if (atomic_inc_return(&mpp_paths->entries) >= | |
330 | mpp_paths->mean_chain_len * (mpp_paths->hash_mask + 1)) | |
331 | grow = 1; | |
332 | ||
333 | spin_unlock(&mpp_paths->hashwlock[hash_idx]); | |
334 | read_unlock(&pathtbl_resize_lock); | |
335 | if (grow) { | |
336 | struct mesh_table *oldtbl, *newtbl; | |
337 | ||
338 | write_lock(&pathtbl_resize_lock); | |
339 | oldtbl = mpp_paths; | |
340 | newtbl = mesh_table_grow(mpp_paths); | |
341 | if (!newtbl) { | |
342 | write_unlock(&pathtbl_resize_lock); | |
343 | return 0; | |
344 | } | |
345 | rcu_assign_pointer(mpp_paths, newtbl); | |
346 | write_unlock(&pathtbl_resize_lock); | |
347 | ||
348 | synchronize_rcu(); | |
349 | mesh_table_free(oldtbl, false); | |
350 | } | |
351 | return 0; | |
352 | ||
353 | err_exists: | |
354 | spin_unlock(&mpp_paths->hashwlock[hash_idx]); | |
355 | read_unlock(&pathtbl_resize_lock); | |
356 | kfree(new_node); | |
357 | err_node_alloc: | |
358 | kfree(new_mpath); | |
359 | err_path_alloc: | |
360 | return err; | |
361 | } | |
362 | ||
363 | ||
eb2b9311 LCC |
364 | /** |
365 | * mesh_plink_broken - deactivates paths and sends perr when a link breaks | |
366 | * | |
367 | * @sta: broken peer link | |
368 | * | |
369 | * This function must be called from the rate control algorithm if enough | |
370 | * delivery errors suggest that a peer link is no longer usable. | |
371 | */ | |
372 | void mesh_plink_broken(struct sta_info *sta) | |
373 | { | |
374 | struct mesh_path *mpath; | |
375 | struct mpath_node *node; | |
376 | struct hlist_node *p; | |
f698d856 | 377 | struct ieee80211_sub_if_data *sdata = sta->sdata; |
eb2b9311 LCC |
378 | int i; |
379 | ||
380 | rcu_read_lock(); | |
381 | for_each_mesh_entry(mesh_paths, p, node, i) { | |
382 | mpath = node->mpath; | |
383 | spin_lock_bh(&mpath->state_lock); | |
384 | if (mpath->next_hop == sta && | |
385 | mpath->flags & MESH_PATH_ACTIVE && | |
386 | !(mpath->flags & MESH_PATH_FIXED)) { | |
387 | mpath->flags &= ~MESH_PATH_ACTIVE; | |
388 | ++mpath->dsn; | |
389 | spin_unlock_bh(&mpath->state_lock); | |
390 | mesh_path_error_tx(mpath->dst, | |
391 | cpu_to_le32(mpath->dsn), | |
f698d856 | 392 | sdata->dev->broadcast, sdata); |
eb2b9311 LCC |
393 | } else |
394 | spin_unlock_bh(&mpath->state_lock); | |
395 | } | |
396 | rcu_read_unlock(); | |
397 | } | |
398 | ||
399 | /** | |
400 | * mesh_path_flush_by_nexthop - Deletes mesh paths if their next hop matches | |
401 | * | |
402 | * @sta - mesh peer to match | |
403 | * | |
b4e08ea1 LCC |
404 | * RCU notes: this function is called when a mesh plink transitions from |
405 | * PLINK_ESTAB to any other state, since PLINK_ESTAB state is the only one that | |
406 | * allows path creation. This will happen before the sta can be freed (because | |
d0709a65 JB |
407 | * sta_info_destroy() calls this) so any reader in a rcu read block will be |
408 | * protected against the plink disappearing. | |
eb2b9311 LCC |
409 | */ |
410 | void mesh_path_flush_by_nexthop(struct sta_info *sta) | |
411 | { | |
412 | struct mesh_path *mpath; | |
413 | struct mpath_node *node; | |
414 | struct hlist_node *p; | |
415 | int i; | |
416 | ||
417 | for_each_mesh_entry(mesh_paths, p, node, i) { | |
418 | mpath = node->mpath; | |
419 | if (mpath->next_hop == sta) | |
f698d856 | 420 | mesh_path_del(mpath->dst, mpath->sdata); |
eb2b9311 LCC |
421 | } |
422 | } | |
423 | ||
f698d856 | 424 | void mesh_path_flush(struct ieee80211_sub_if_data *sdata) |
eb2b9311 LCC |
425 | { |
426 | struct mesh_path *mpath; | |
427 | struct mpath_node *node; | |
428 | struct hlist_node *p; | |
429 | int i; | |
430 | ||
431 | for_each_mesh_entry(mesh_paths, p, node, i) { | |
432 | mpath = node->mpath; | |
f698d856 JBG |
433 | if (mpath->sdata == sdata) |
434 | mesh_path_del(mpath->dst, mpath->sdata); | |
eb2b9311 LCC |
435 | } |
436 | } | |
437 | ||
438 | static void mesh_path_node_reclaim(struct rcu_head *rp) | |
439 | { | |
440 | struct mpath_node *node = container_of(rp, struct mpath_node, rcu); | |
f698d856 | 441 | struct ieee80211_sub_if_data *sdata = node->mpath->sdata; |
d0709a65 | 442 | |
89a1ad69 | 443 | del_timer_sync(&node->mpath->timer); |
472dbc45 | 444 | atomic_dec(&sdata->u.mesh.mpaths); |
eb2b9311 LCC |
445 | kfree(node->mpath); |
446 | kfree(node); | |
447 | } | |
448 | ||
449 | /** | |
450 | * mesh_path_del - delete a mesh path from the table | |
451 | * | |
452 | * @addr: dst address (ETH_ALEN length) | |
f698d856 | 453 | * @sdata: local subif |
eb2b9311 LCC |
454 | * |
455 | * Returns: 0 if succesful | |
eb2b9311 | 456 | */ |
f698d856 | 457 | int mesh_path_del(u8 *addr, struct ieee80211_sub_if_data *sdata) |
eb2b9311 LCC |
458 | { |
459 | struct mesh_path *mpath; | |
460 | struct mpath_node *node; | |
461 | struct hlist_head *bucket; | |
462 | struct hlist_node *n; | |
463 | int hash_idx; | |
464 | int err = 0; | |
465 | ||
466 | read_lock(&pathtbl_resize_lock); | |
f698d856 | 467 | hash_idx = mesh_table_hash(addr, sdata, mesh_paths); |
eb2b9311 LCC |
468 | bucket = &mesh_paths->hash_buckets[hash_idx]; |
469 | ||
470 | spin_lock(&mesh_paths->hashwlock[hash_idx]); | |
471 | hlist_for_each_entry(node, n, bucket, list) { | |
472 | mpath = node->mpath; | |
f698d856 | 473 | if (mpath->sdata == sdata && |
eb2b9311 LCC |
474 | memcmp(addr, mpath->dst, ETH_ALEN) == 0) { |
475 | spin_lock_bh(&mpath->state_lock); | |
cfa22c71 LCC |
476 | mpath->flags |= MESH_PATH_RESOLVING; |
477 | hlist_del_rcu(&node->list); | |
478 | call_rcu(&node->rcu, mesh_path_node_reclaim); | |
479 | atomic_dec(&mesh_paths->entries); | |
eb2b9311 LCC |
480 | spin_unlock_bh(&mpath->state_lock); |
481 | goto enddel; | |
482 | } | |
483 | } | |
484 | ||
485 | err = -ENXIO; | |
486 | enddel: | |
487 | spin_unlock(&mesh_paths->hashwlock[hash_idx]); | |
488 | read_unlock(&pathtbl_resize_lock); | |
489 | return err; | |
490 | } | |
491 | ||
492 | /** | |
493 | * mesh_path_tx_pending - sends pending frames in a mesh path queue | |
494 | * | |
495 | * @mpath: mesh path to activate | |
496 | * | |
497 | * Locking: the state_lock of the mpath structure must NOT be held when calling | |
498 | * this function. | |
499 | */ | |
500 | void mesh_path_tx_pending(struct mesh_path *mpath) | |
501 | { | |
502 | struct sk_buff *skb; | |
503 | ||
504 | while ((skb = skb_dequeue(&mpath->frame_queue)) && | |
505 | (mpath->flags & MESH_PATH_ACTIVE)) | |
506 | dev_queue_xmit(skb); | |
507 | } | |
508 | ||
509 | /** | |
510 | * mesh_path_discard_frame - discard a frame whose path could not be resolved | |
511 | * | |
512 | * @skb: frame to discard | |
f698d856 | 513 | * @sdata: network subif the frame was to be sent through |
eb2b9311 | 514 | * |
35946a57 JC |
515 | * If the frame was being forwarded from another MP, a PERR frame will be sent |
516 | * to the precursor. The precursor's address (i.e. the previous hop) was saved | |
517 | * in addr1 of the frame-to-be-forwarded, and would only be overwritten once | |
518 | * the destination is successfully resolved. | |
eb2b9311 LCC |
519 | * |
520 | * Locking: the function must me called within a rcu_read_lock region | |
521 | */ | |
f698d856 JBG |
522 | void mesh_path_discard_frame(struct sk_buff *skb, |
523 | struct ieee80211_sub_if_data *sdata) | |
eb2b9311 | 524 | { |
e32f85f7 | 525 | struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data; |
eb2b9311 LCC |
526 | struct mesh_path *mpath; |
527 | u32 dsn = 0; | |
528 | ||
f698d856 | 529 | if (memcmp(hdr->addr4, sdata->dev->dev_addr, ETH_ALEN) != 0) { |
eb2b9311 LCC |
530 | u8 *ra, *da; |
531 | ||
e32f85f7 | 532 | da = hdr->addr3; |
35946a57 | 533 | ra = hdr->addr1; |
f698d856 | 534 | mpath = mesh_path_lookup(da, sdata); |
eb2b9311 LCC |
535 | if (mpath) |
536 | dsn = ++mpath->dsn; | |
f698d856 | 537 | mesh_path_error_tx(skb->data, cpu_to_le32(dsn), ra, sdata); |
eb2b9311 LCC |
538 | } |
539 | ||
540 | kfree_skb(skb); | |
472dbc45 | 541 | sdata->u.mesh.mshstats.dropped_frames_no_route++; |
eb2b9311 LCC |
542 | } |
543 | ||
544 | /** | |
545 | * mesh_path_flush_pending - free the pending queue of a mesh path | |
546 | * | |
547 | * @mpath: mesh path whose queue has to be freed | |
548 | * | |
549 | * Locking: the function must me called withing a rcu_read_lock region | |
550 | */ | |
551 | void mesh_path_flush_pending(struct mesh_path *mpath) | |
552 | { | |
eb2b9311 LCC |
553 | struct sk_buff *skb; |
554 | ||
eb2b9311 LCC |
555 | while ((skb = skb_dequeue(&mpath->frame_queue)) && |
556 | (mpath->flags & MESH_PATH_ACTIVE)) | |
f698d856 | 557 | mesh_path_discard_frame(skb, mpath->sdata); |
eb2b9311 LCC |
558 | } |
559 | ||
560 | /** | |
561 | * mesh_path_fix_nexthop - force a specific next hop for a mesh path | |
562 | * | |
563 | * @mpath: the mesh path to modify | |
564 | * @next_hop: the next hop to force | |
565 | * | |
566 | * Locking: this function must be called holding mpath->state_lock | |
567 | */ | |
568 | void mesh_path_fix_nexthop(struct mesh_path *mpath, struct sta_info *next_hop) | |
569 | { | |
570 | spin_lock_bh(&mpath->state_lock); | |
571 | mesh_path_assign_nexthop(mpath, next_hop); | |
572 | mpath->dsn = 0xffff; | |
573 | mpath->metric = 0; | |
574 | mpath->hop_count = 0; | |
575 | mpath->exp_time = 0; | |
576 | mpath->flags |= MESH_PATH_FIXED; | |
577 | mesh_path_activate(mpath); | |
578 | spin_unlock_bh(&mpath->state_lock); | |
579 | mesh_path_tx_pending(mpath); | |
580 | } | |
581 | ||
582 | static void mesh_path_node_free(struct hlist_node *p, bool free_leafs) | |
583 | { | |
584 | struct mesh_path *mpath; | |
585 | struct mpath_node *node = hlist_entry(p, struct mpath_node, list); | |
586 | mpath = node->mpath; | |
587 | hlist_del_rcu(p); | |
eb2b9311 LCC |
588 | if (free_leafs) |
589 | kfree(mpath); | |
590 | kfree(node); | |
591 | } | |
592 | ||
4caf86c6 | 593 | static int mesh_path_node_copy(struct hlist_node *p, struct mesh_table *newtbl) |
eb2b9311 LCC |
594 | { |
595 | struct mesh_path *mpath; | |
596 | struct mpath_node *node, *new_node; | |
597 | u32 hash_idx; | |
598 | ||
8566dc3f | 599 | new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC); |
00242c40 PE |
600 | if (new_node == NULL) |
601 | return -ENOMEM; | |
602 | ||
eb2b9311 LCC |
603 | node = hlist_entry(p, struct mpath_node, list); |
604 | mpath = node->mpath; | |
eb2b9311 | 605 | new_node->mpath = mpath; |
f698d856 | 606 | hash_idx = mesh_table_hash(mpath->dst, mpath->sdata, newtbl); |
eb2b9311 LCC |
607 | hlist_add_head(&new_node->list, |
608 | &newtbl->hash_buckets[hash_idx]); | |
4caf86c6 | 609 | return 0; |
eb2b9311 LCC |
610 | } |
611 | ||
612 | int mesh_pathtbl_init(void) | |
613 | { | |
614 | mesh_paths = mesh_table_alloc(INIT_PATHS_SIZE_ORDER); | |
79617dee Y |
615 | if (!mesh_paths) |
616 | return -ENOMEM; | |
eb2b9311 LCC |
617 | mesh_paths->free_node = &mesh_path_node_free; |
618 | mesh_paths->copy_node = &mesh_path_node_copy; | |
619 | mesh_paths->mean_chain_len = MEAN_CHAIN_LEN; | |
79617dee Y |
620 | |
621 | mpp_paths = mesh_table_alloc(INIT_PATHS_SIZE_ORDER); | |
622 | if (!mpp_paths) { | |
623 | mesh_table_free(mesh_paths, true); | |
eb2b9311 | 624 | return -ENOMEM; |
79617dee Y |
625 | } |
626 | mpp_paths->free_node = &mesh_path_node_free; | |
627 | mpp_paths->copy_node = &mesh_path_node_copy; | |
628 | mpp_paths->mean_chain_len = MEAN_CHAIN_LEN; | |
629 | ||
eb2b9311 LCC |
630 | return 0; |
631 | } | |
632 | ||
f698d856 | 633 | void mesh_path_expire(struct ieee80211_sub_if_data *sdata) |
eb2b9311 LCC |
634 | { |
635 | struct mesh_path *mpath; | |
636 | struct mpath_node *node; | |
637 | struct hlist_node *p; | |
638 | int i; | |
639 | ||
640 | read_lock(&pathtbl_resize_lock); | |
641 | for_each_mesh_entry(mesh_paths, p, node, i) { | |
f698d856 | 642 | if (node->mpath->sdata != sdata) |
eb2b9311 LCC |
643 | continue; |
644 | mpath = node->mpath; | |
645 | spin_lock_bh(&mpath->state_lock); | |
646 | if ((!(mpath->flags & MESH_PATH_RESOLVING)) && | |
647 | (!(mpath->flags & MESH_PATH_FIXED)) && | |
648 | time_after(jiffies, | |
649 | mpath->exp_time + MESH_PATH_EXPIRE)) { | |
650 | spin_unlock_bh(&mpath->state_lock); | |
f698d856 | 651 | mesh_path_del(mpath->dst, mpath->sdata); |
eb2b9311 LCC |
652 | } else |
653 | spin_unlock_bh(&mpath->state_lock); | |
654 | } | |
655 | read_unlock(&pathtbl_resize_lock); | |
656 | } | |
657 | ||
658 | void mesh_pathtbl_unregister(void) | |
659 | { | |
660 | mesh_table_free(mesh_paths, true); | |
79617dee | 661 | mesh_table_free(mpp_paths, true); |
eb2b9311 | 662 | } |