2 * Copyright (c) 2008, 2009 open80211s Ltd.
3 * Author: Luis Carlos Cobo <luisca@cozybit.com>
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.
10 #include <linux/etherdevice.h>
11 #include <linux/list.h>
12 #include <linux/random.h>
13 #include <linux/slab.h>
14 #include <linux/spinlock.h>
15 #include <linux/string.h>
16 #include <net/mac80211.h>
18 #include "ieee80211_i.h"
21 #ifdef CONFIG_MAC80211_VERBOSE_MPATH_DEBUG
22 #define mpath_dbg(fmt, args...) printk(KERN_DEBUG fmt, ##args)
24 #define mpath_dbg(fmt, args...) do { (void)(0); } while (0)
27 /* There will be initially 2^INIT_PATHS_SIZE_ORDER buckets */
28 #define INIT_PATHS_SIZE_ORDER 2
30 /* Keep the mean chain length below this constant */
31 #define MEAN_CHAIN_LEN 2
33 #define MPATH_EXPIRED(mpath) ((mpath->flags & MESH_PATH_ACTIVE) && \
34 time_after(jiffies, mpath->exp_time) && \
35 !(mpath->flags & MESH_PATH_FIXED))
38 struct hlist_node list;
40 /* This indirection allows two different tables to point to the same
41 * mesh_path structure, useful when resizing
43 struct mesh_path *mpath;
46 static struct mesh_table __rcu *mesh_paths;
47 static struct mesh_table __rcu *mpp_paths; /* Store paths for MPP&MAP */
49 int mesh_paths_generation;
51 /* This lock will have the grow table function as writer and add / delete nodes
52 * 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.
57 static DEFINE_RWLOCK(pathtbl_resize_lock);
60 static inline struct mesh_table *resize_dereference_mesh_paths(void)
62 return rcu_dereference_protected(mesh_paths,
63 lockdep_is_held(&pathtbl_resize_lock));
66 static inline struct mesh_table *resize_dereference_mpp_paths(void)
68 return rcu_dereference_protected(mpp_paths,
69 lockdep_is_held(&pathtbl_resize_lock));
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(...), ...)
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)
83 static struct mesh_table *mesh_table_alloc(int size_order)
86 struct mesh_table *newtbl;
88 newtbl = kmalloc(sizeof(struct mesh_table), GFP_ATOMIC);
92 newtbl->hash_buckets = kzalloc(sizeof(struct hlist_head) *
93 (1 << size_order), GFP_ATOMIC);
95 if (!newtbl->hash_buckets) {
100 newtbl->hashwlock = kmalloc(sizeof(spinlock_t) *
101 (1 << size_order), GFP_ATOMIC);
102 if (!newtbl->hashwlock) {
103 kfree(newtbl->hash_buckets);
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]);
115 spin_lock_init(&newtbl->gates_lock);
120 static void __mesh_table_free(struct mesh_table *tbl)
122 kfree(tbl->hash_buckets);
123 kfree(tbl->hashwlock);
127 static void mesh_table_free(struct mesh_table *tbl, bool free_leafs)
129 struct hlist_head *mesh_hash;
130 struct hlist_node *p, *q;
131 struct mpath_node *gate;
134 mesh_hash = tbl->hash_buckets;
135 for (i = 0; i <= tbl->hash_mask; i++) {
136 spin_lock_bh(&tbl->hashwlock[i]);
137 hlist_for_each_safe(p, q, &mesh_hash[i]) {
138 tbl->free_node(p, free_leafs);
139 atomic_dec(&tbl->entries);
141 spin_unlock_bh(&tbl->hashwlock[i]);
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);
150 kfree(tbl->known_gates);
151 spin_unlock_bh(&tbl->gates_lock);
154 __mesh_table_free(tbl);
157 static int mesh_table_grow(struct mesh_table *oldtbl,
158 struct mesh_table *newtbl)
160 struct hlist_head *oldhash;
161 struct hlist_node *p, *q;
164 if (atomic_read(&oldtbl->entries)
165 < oldtbl->mean_chain_len * (oldtbl->hash_mask + 1))
168 newtbl->free_node = oldtbl->free_node;
169 newtbl->mean_chain_len = oldtbl->mean_chain_len;
170 newtbl->copy_node = oldtbl->copy_node;
171 newtbl->known_gates = oldtbl->known_gates;
172 atomic_set(&newtbl->entries, atomic_read(&oldtbl->entries));
174 oldhash = oldtbl->hash_buckets;
175 for (i = 0; i <= oldtbl->hash_mask; i++)
176 hlist_for_each(p, &oldhash[i])
177 if (oldtbl->copy_node(p, newtbl) < 0)
183 for (i = 0; i <= newtbl->hash_mask; i++) {
184 hlist_for_each_safe(p, q, &newtbl->hash_buckets[i])
185 oldtbl->free_node(p, 0);
190 static u32 mesh_table_hash(u8 *addr, struct ieee80211_sub_if_data *sdata,
191 struct mesh_table *tbl)
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)
201 * mesh_path_assign_nexthop - update mesh path next hop
203 * @mpath: mesh path to update
204 * @sta: next hop to assign
206 * Locking: mpath->state_lock must be held when calling this function
208 void mesh_path_assign_nexthop(struct mesh_path *mpath, struct sta_info *sta)
211 struct ieee80211_hdr *hdr;
212 struct sk_buff_head tmpq;
215 rcu_assign_pointer(mpath->next_hop, sta);
217 __skb_queue_head_init(&tmpq);
219 spin_lock_irqsave(&mpath->frame_queue.lock, flags);
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 memcpy(hdr->addr2, mpath->sdata->vif.addr, ETH_ALEN);
225 __skb_queue_tail(&tmpq, skb);
228 skb_queue_splice(&tmpq, &mpath->frame_queue);
229 spin_unlock_irqrestore(&mpath->frame_queue.lock, flags);
232 static void prepare_for_gate(struct sk_buff *skb, char *dst_addr,
233 struct mesh_path *gate_mpath)
235 struct ieee80211_hdr *hdr;
236 struct ieee80211s_hdr *mshdr;
237 int mesh_hdrlen, hdrlen;
240 hdr = (struct ieee80211_hdr *) skb->data;
241 hdrlen = ieee80211_hdrlen(hdr->frame_control);
242 mshdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
244 if (!(mshdr->flags & MESH_FLAGS_AE)) {
245 /* size of the fixed part of the mesh header */
248 /* make room for the two extended addresses */
249 skb_push(skb, 2 * ETH_ALEN);
250 memmove(skb->data, hdr, hdrlen + mesh_hdrlen);
252 hdr = (struct ieee80211_hdr *) skb->data;
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);
262 /* update next hop */
263 hdr = (struct ieee80211_hdr *) skb->data;
265 next_hop = rcu_dereference(gate_mpath->next_hop)->sta.addr;
266 memcpy(hdr->addr1, next_hop, ETH_ALEN);
268 memcpy(hdr->addr2, gate_mpath->sdata->vif.addr, ETH_ALEN);
269 memcpy(hdr->addr3, dst_addr, ETH_ALEN);
274 * mesh_path_move_to_queue - Move or copy frames from one mpath queue to another
276 * This function is used to transfer or copy frames from an unresolved mpath to
277 * a gate mpath. The function also adds the Address Extension field and
278 * updates the next hop.
280 * If a frame already has an Address Extension field, only the next hop and
281 * destination addresses are updated.
283 * The gate mpath must be an active mpath with a valid mpath->next_hop.
285 * @mpath: An active mpath the frames will be sent to (i.e. the gate)
286 * @from_mpath: The failed mpath
287 * @copy: When true, copy all the frames to the new mpath queue. When false,
290 static void mesh_path_move_to_queue(struct mesh_path *gate_mpath,
291 struct mesh_path *from_mpath,
294 struct sk_buff *skb, *cp_skb = NULL;
295 struct sk_buff_head gateq, failq;
299 BUG_ON(gate_mpath == from_mpath);
300 BUG_ON(!gate_mpath->next_hop);
302 __skb_queue_head_init(&gateq);
303 __skb_queue_head_init(&failq);
305 spin_lock_irqsave(&from_mpath->frame_queue.lock, flags);
306 skb_queue_splice_init(&from_mpath->frame_queue, &failq);
307 spin_unlock_irqrestore(&from_mpath->frame_queue.lock, flags);
309 num_skbs = skb_queue_len(&failq);
312 skb = __skb_dequeue(&failq);
314 cp_skb = skb_copy(skb, GFP_ATOMIC);
316 __skb_queue_tail(&failq, cp_skb);
319 prepare_for_gate(skb, gate_mpath->dst, gate_mpath);
320 __skb_queue_tail(&gateq, skb);
323 spin_lock_irqsave(&gate_mpath->frame_queue.lock, flags);
324 skb_queue_splice(&gateq, &gate_mpath->frame_queue);
325 mpath_dbg("Mpath queue for gate %pM has %d frames\n",
327 skb_queue_len(&gate_mpath->frame_queue));
328 spin_unlock_irqrestore(&gate_mpath->frame_queue.lock, flags);
333 spin_lock_irqsave(&from_mpath->frame_queue.lock, flags);
334 skb_queue_splice(&failq, &from_mpath->frame_queue);
335 spin_unlock_irqrestore(&from_mpath->frame_queue.lock, flags);
339 static struct mesh_path *mpath_lookup(struct mesh_table *tbl, u8 *dst,
340 struct ieee80211_sub_if_data *sdata)
342 struct mesh_path *mpath;
343 struct hlist_node *n;
344 struct hlist_head *bucket;
345 struct mpath_node *node;
347 bucket = &tbl->hash_buckets[mesh_table_hash(dst, sdata, tbl)];
348 hlist_for_each_entry_rcu(node, n, bucket, list) {
350 if (mpath->sdata == sdata &&
351 ether_addr_equal(dst, mpath->dst)) {
352 if (MPATH_EXPIRED(mpath)) {
353 spin_lock_bh(&mpath->state_lock);
354 mpath->flags &= ~MESH_PATH_ACTIVE;
355 spin_unlock_bh(&mpath->state_lock);
364 * mesh_path_lookup - look up a path in the mesh path table
365 * @dst: hardware address (ETH_ALEN length) of destination
366 * @sdata: local subif
368 * Returns: pointer to the mesh path structure, or NULL if not found
370 * Locking: must be called within a read rcu section.
372 struct mesh_path *mesh_path_lookup(u8 *dst, struct ieee80211_sub_if_data *sdata)
374 return mpath_lookup(rcu_dereference(mesh_paths), dst, sdata);
377 struct mesh_path *mpp_path_lookup(u8 *dst, struct ieee80211_sub_if_data *sdata)
379 return mpath_lookup(rcu_dereference(mpp_paths), dst, sdata);
384 * mesh_path_lookup_by_idx - look up a path in the mesh path table by its index
386 * @sdata: local subif, or NULL for all entries
388 * Returns: pointer to the mesh path structure, or NULL if not found.
390 * Locking: must be called within a read rcu section.
392 struct mesh_path *mesh_path_lookup_by_idx(int idx, struct ieee80211_sub_if_data *sdata)
394 struct mesh_table *tbl = rcu_dereference(mesh_paths);
395 struct mpath_node *node;
396 struct hlist_node *p;
400 for_each_mesh_entry(tbl, p, node, i) {
401 if (sdata && node->mpath->sdata != sdata)
404 if (MPATH_EXPIRED(node->mpath)) {
405 spin_lock_bh(&node->mpath->state_lock);
406 node->mpath->flags &= ~MESH_PATH_ACTIVE;
407 spin_unlock_bh(&node->mpath->state_lock);
417 * mesh_path_add_gate - add the given mpath to a mesh gate to our path table
418 * @mpath: gate path to add to table
420 int mesh_path_add_gate(struct mesh_path *mpath)
422 struct mesh_table *tbl;
423 struct mpath_node *gate, *new_gate;
424 struct hlist_node *n;
428 tbl = rcu_dereference(mesh_paths);
430 hlist_for_each_entry_rcu(gate, n, tbl->known_gates, list)
431 if (gate->mpath == mpath) {
436 new_gate = kzalloc(sizeof(struct mpath_node), GFP_ATOMIC);
442 mpath->is_gate = true;
443 mpath->sdata->u.mesh.num_gates++;
444 new_gate->mpath = mpath;
445 spin_lock_bh(&tbl->gates_lock);
446 hlist_add_head_rcu(&new_gate->list, tbl->known_gates);
447 spin_unlock_bh(&tbl->gates_lock);
449 mpath_dbg("Mesh path (%s): Recorded new gate: %pM. %d known gates\n",
450 mpath->sdata->name, mpath->dst,
451 mpath->sdata->u.mesh.num_gates);
459 * mesh_gate_del - remove a mesh gate from the list of known gates
460 * @tbl: table which holds our list of known gates
463 * Returns: 0 on success
465 * Locking: must be called inside rcu_read_lock() section
467 static int mesh_gate_del(struct mesh_table *tbl, struct mesh_path *mpath)
469 struct mpath_node *gate;
470 struct hlist_node *p, *q;
472 hlist_for_each_entry_safe(gate, p, q, tbl->known_gates, list)
473 if (gate->mpath == mpath) {
474 spin_lock_bh(&tbl->gates_lock);
475 hlist_del_rcu(&gate->list);
476 kfree_rcu(gate, rcu);
477 spin_unlock_bh(&tbl->gates_lock);
478 mpath->sdata->u.mesh.num_gates--;
479 mpath->is_gate = false;
480 mpath_dbg("Mesh path (%s): Deleted gate: %pM. "
481 "%d known gates\n", mpath->sdata->name,
482 mpath->dst, mpath->sdata->u.mesh.num_gates);
490 * mesh_gate_num - number of gates known to this interface
493 int mesh_gate_num(struct ieee80211_sub_if_data *sdata)
495 return sdata->u.mesh.num_gates;
499 * mesh_path_add - allocate and add a new path to the mesh path table
500 * @addr: destination address of the path (ETH_ALEN length)
501 * @sdata: local subif
503 * Returns: 0 on success
505 * State: the initial state of the new path is set to 0
507 int mesh_path_add(u8 *dst, struct ieee80211_sub_if_data *sdata)
509 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
510 struct ieee80211_local *local = sdata->local;
511 struct mesh_table *tbl;
512 struct mesh_path *mpath, *new_mpath;
513 struct mpath_node *node, *new_node;
514 struct hlist_head *bucket;
515 struct hlist_node *n;
520 if (ether_addr_equal(dst, sdata->vif.addr))
521 /* never add ourselves as neighbours */
524 if (is_multicast_ether_addr(dst))
527 if (atomic_add_unless(&sdata->u.mesh.mpaths, 1, MESH_MAX_MPATHS) == 0)
531 new_mpath = kzalloc(sizeof(struct mesh_path), GFP_ATOMIC);
535 new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
539 read_lock_bh(&pathtbl_resize_lock);
540 memcpy(new_mpath->dst, dst, ETH_ALEN);
541 memset(new_mpath->rann_snd_addr, 0xff, ETH_ALEN);
542 new_mpath->is_root = false;
543 new_mpath->sdata = sdata;
544 new_mpath->flags = 0;
545 skb_queue_head_init(&new_mpath->frame_queue);
546 new_node->mpath = new_mpath;
547 new_mpath->timer.data = (unsigned long) new_mpath;
548 new_mpath->timer.function = mesh_path_timer;
549 new_mpath->exp_time = jiffies;
550 spin_lock_init(&new_mpath->state_lock);
551 init_timer(&new_mpath->timer);
553 tbl = resize_dereference_mesh_paths();
555 hash_idx = mesh_table_hash(dst, sdata, tbl);
556 bucket = &tbl->hash_buckets[hash_idx];
558 spin_lock(&tbl->hashwlock[hash_idx]);
561 hlist_for_each_entry(node, n, bucket, list) {
563 if (mpath->sdata == sdata &&
564 ether_addr_equal(dst, mpath->dst))
568 hlist_add_head_rcu(&new_node->list, bucket);
569 if (atomic_inc_return(&tbl->entries) >=
570 tbl->mean_chain_len * (tbl->hash_mask + 1))
573 mesh_paths_generation++;
575 spin_unlock(&tbl->hashwlock[hash_idx]);
576 read_unlock_bh(&pathtbl_resize_lock);
578 set_bit(MESH_WORK_GROW_MPATH_TABLE, &ifmsh->wrkq_flags);
579 ieee80211_queue_work(&local->hw, &sdata->work);
584 spin_unlock(&tbl->hashwlock[hash_idx]);
585 read_unlock_bh(&pathtbl_resize_lock);
590 atomic_dec(&sdata->u.mesh.mpaths);
594 static void mesh_table_free_rcu(struct rcu_head *rcu)
596 struct mesh_table *tbl = container_of(rcu, struct mesh_table, rcu_head);
598 mesh_table_free(tbl, false);
601 void mesh_mpath_table_grow(void)
603 struct mesh_table *oldtbl, *newtbl;
605 write_lock_bh(&pathtbl_resize_lock);
606 oldtbl = resize_dereference_mesh_paths();
607 newtbl = mesh_table_alloc(oldtbl->size_order + 1);
610 if (mesh_table_grow(oldtbl, newtbl) < 0) {
611 __mesh_table_free(newtbl);
614 rcu_assign_pointer(mesh_paths, newtbl);
616 call_rcu(&oldtbl->rcu_head, mesh_table_free_rcu);
619 write_unlock_bh(&pathtbl_resize_lock);
622 void mesh_mpp_table_grow(void)
624 struct mesh_table *oldtbl, *newtbl;
626 write_lock_bh(&pathtbl_resize_lock);
627 oldtbl = resize_dereference_mpp_paths();
628 newtbl = mesh_table_alloc(oldtbl->size_order + 1);
631 if (mesh_table_grow(oldtbl, newtbl) < 0) {
632 __mesh_table_free(newtbl);
635 rcu_assign_pointer(mpp_paths, newtbl);
636 call_rcu(&oldtbl->rcu_head, mesh_table_free_rcu);
639 write_unlock_bh(&pathtbl_resize_lock);
642 int mpp_path_add(u8 *dst, u8 *mpp, struct ieee80211_sub_if_data *sdata)
644 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
645 struct ieee80211_local *local = sdata->local;
646 struct mesh_table *tbl;
647 struct mesh_path *mpath, *new_mpath;
648 struct mpath_node *node, *new_node;
649 struct hlist_head *bucket;
650 struct hlist_node *n;
655 if (ether_addr_equal(dst, sdata->vif.addr))
656 /* never add ourselves as neighbours */
659 if (is_multicast_ether_addr(dst))
663 new_mpath = kzalloc(sizeof(struct mesh_path), GFP_ATOMIC);
667 new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
671 read_lock_bh(&pathtbl_resize_lock);
672 memcpy(new_mpath->dst, dst, ETH_ALEN);
673 memcpy(new_mpath->mpp, mpp, ETH_ALEN);
674 new_mpath->sdata = sdata;
675 new_mpath->flags = 0;
676 skb_queue_head_init(&new_mpath->frame_queue);
677 new_node->mpath = new_mpath;
678 init_timer(&new_mpath->timer);
679 new_mpath->exp_time = jiffies;
680 spin_lock_init(&new_mpath->state_lock);
682 tbl = resize_dereference_mpp_paths();
684 hash_idx = mesh_table_hash(dst, sdata, tbl);
685 bucket = &tbl->hash_buckets[hash_idx];
687 spin_lock(&tbl->hashwlock[hash_idx]);
690 hlist_for_each_entry(node, n, bucket, list) {
692 if (mpath->sdata == sdata &&
693 ether_addr_equal(dst, mpath->dst))
697 hlist_add_head_rcu(&new_node->list, bucket);
698 if (atomic_inc_return(&tbl->entries) >=
699 tbl->mean_chain_len * (tbl->hash_mask + 1))
702 spin_unlock(&tbl->hashwlock[hash_idx]);
703 read_unlock_bh(&pathtbl_resize_lock);
705 set_bit(MESH_WORK_GROW_MPP_TABLE, &ifmsh->wrkq_flags);
706 ieee80211_queue_work(&local->hw, &sdata->work);
711 spin_unlock(&tbl->hashwlock[hash_idx]);
712 read_unlock_bh(&pathtbl_resize_lock);
722 * mesh_plink_broken - deactivates paths and sends perr when a link breaks
724 * @sta: broken peer link
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.
729 void mesh_plink_broken(struct sta_info *sta)
731 struct mesh_table *tbl;
732 static const u8 bcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
733 struct mesh_path *mpath;
734 struct mpath_node *node;
735 struct hlist_node *p;
736 struct ieee80211_sub_if_data *sdata = sta->sdata;
738 __le16 reason = cpu_to_le16(WLAN_REASON_MESH_PATH_DEST_UNREACHABLE);
741 tbl = rcu_dereference(mesh_paths);
742 for_each_mesh_entry(tbl, p, node, i) {
744 if (rcu_dereference(mpath->next_hop) == sta &&
745 mpath->flags & MESH_PATH_ACTIVE &&
746 !(mpath->flags & MESH_PATH_FIXED)) {
747 spin_lock_bh(&mpath->state_lock);
748 mpath->flags &= ~MESH_PATH_ACTIVE;
750 spin_unlock_bh(&mpath->state_lock);
751 mesh_path_error_tx(sdata->u.mesh.mshcfg.element_ttl,
752 mpath->dst, cpu_to_le32(mpath->sn),
753 reason, bcast, sdata);
759 static void mesh_path_node_reclaim(struct rcu_head *rp)
761 struct mpath_node *node = container_of(rp, struct mpath_node, rcu);
762 struct ieee80211_sub_if_data *sdata = node->mpath->sdata;
764 del_timer_sync(&node->mpath->timer);
765 atomic_dec(&sdata->u.mesh.mpaths);
770 /* needs to be called with the corresponding hashwlock taken */
771 static void __mesh_path_del(struct mesh_table *tbl, struct mpath_node *node)
773 struct mesh_path *mpath;
775 spin_lock(&mpath->state_lock);
776 mpath->flags |= MESH_PATH_RESOLVING;
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);
786 * mesh_path_flush_by_nexthop - Deletes mesh paths if their next hop matches
788 * @sta - mesh peer to match
790 * 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
793 * sta_info_destroy() calls this) so any reader in a rcu read block will be
794 * protected against the plink disappearing.
796 void mesh_path_flush_by_nexthop(struct sta_info *sta)
798 struct mesh_table *tbl;
799 struct mesh_path *mpath;
800 struct mpath_node *node;
801 struct hlist_node *p;
805 read_lock_bh(&pathtbl_resize_lock);
806 tbl = resize_dereference_mesh_paths();
807 for_each_mesh_entry(tbl, p, node, i) {
809 if (rcu_dereference(mpath->next_hop) == sta) {
810 spin_lock(&tbl->hashwlock[i]);
811 __mesh_path_del(tbl, node);
812 spin_unlock(&tbl->hashwlock[i]);
815 read_unlock_bh(&pathtbl_resize_lock);
819 static void table_flush_by_iface(struct mesh_table *tbl,
820 struct ieee80211_sub_if_data *sdata)
822 struct mesh_path *mpath;
823 struct mpath_node *node;
824 struct hlist_node *p;
827 WARN_ON(!rcu_read_lock_held());
828 for_each_mesh_entry(tbl, p, node, i) {
830 if (mpath->sdata != sdata)
832 spin_lock_bh(&tbl->hashwlock[i]);
833 __mesh_path_del(tbl, node);
834 spin_unlock_bh(&tbl->hashwlock[i]);
839 * mesh_path_flush_by_iface - Deletes all mesh paths associated with a given iface
841 * This function deletes both mesh paths as well as mesh portal paths.
843 * @sdata - interface data to match
846 void mesh_path_flush_by_iface(struct ieee80211_sub_if_data *sdata)
848 struct mesh_table *tbl;
851 read_lock_bh(&pathtbl_resize_lock);
852 tbl = resize_dereference_mesh_paths();
853 table_flush_by_iface(tbl, sdata);
854 tbl = resize_dereference_mpp_paths();
855 table_flush_by_iface(tbl, sdata);
856 read_unlock_bh(&pathtbl_resize_lock);
861 * mesh_path_del - delete a mesh path from the table
863 * @addr: dst address (ETH_ALEN length)
864 * @sdata: local subif
866 * Returns: 0 if successful
868 int mesh_path_del(u8 *addr, struct ieee80211_sub_if_data *sdata)
870 struct mesh_table *tbl;
871 struct mesh_path *mpath;
872 struct mpath_node *node;
873 struct hlist_head *bucket;
874 struct hlist_node *n;
878 read_lock_bh(&pathtbl_resize_lock);
879 tbl = resize_dereference_mesh_paths();
880 hash_idx = mesh_table_hash(addr, sdata, tbl);
881 bucket = &tbl->hash_buckets[hash_idx];
883 spin_lock(&tbl->hashwlock[hash_idx]);
884 hlist_for_each_entry(node, n, bucket, list) {
886 if (mpath->sdata == sdata &&
887 ether_addr_equal(addr, mpath->dst)) {
888 __mesh_path_del(tbl, node);
895 mesh_paths_generation++;
896 spin_unlock(&tbl->hashwlock[hash_idx]);
897 read_unlock_bh(&pathtbl_resize_lock);
902 * mesh_path_tx_pending - sends pending frames in a mesh path queue
904 * @mpath: mesh path to activate
906 * Locking: the state_lock of the mpath structure must NOT be held when calling
909 void mesh_path_tx_pending(struct mesh_path *mpath)
911 if (mpath->flags & MESH_PATH_ACTIVE)
912 ieee80211_add_pending_skbs(mpath->sdata->local,
913 &mpath->frame_queue);
917 * mesh_path_send_to_gates - sends pending frames to all known mesh gates
919 * @mpath: mesh path whose queue will be emptied
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.
926 int mesh_path_send_to_gates(struct mesh_path *mpath)
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;
934 struct hlist_head *known_gates;
937 tbl = rcu_dereference(mesh_paths);
938 known_gates = tbl->known_gates;
942 return -EHOSTUNREACH;
944 hlist_for_each_entry_rcu(gate, n, known_gates, list) {
945 if (gate->mpath->sdata != sdata)
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;
954 mpath_dbg("Not forwarding %p\n", gate->mpath);
955 mpath_dbg("flags %x\n", gate->mpath->flags);
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);
965 return (from_mpath == mpath) ? -EHOSTUNREACH : 0;
969 * mesh_path_discard_frame - discard a frame whose path could not be resolved
971 * @skb: frame to discard
972 * @sdata: network subif the frame was to be sent through
974 * Locking: the function must me called within a rcu_read_lock region
976 void mesh_path_discard_frame(struct sk_buff *skb,
977 struct ieee80211_sub_if_data *sdata)
980 sdata->u.mesh.mshstats.dropped_frames_no_route++;
984 * mesh_path_flush_pending - free the pending queue of a mesh path
986 * @mpath: mesh path whose queue has to be freed
988 * Locking: the function must me called within a rcu_read_lock region
990 void mesh_path_flush_pending(struct mesh_path *mpath)
994 while ((skb = skb_dequeue(&mpath->frame_queue)) != NULL)
995 mesh_path_discard_frame(skb, mpath->sdata);
999 * mesh_path_fix_nexthop - force a specific next hop for a mesh path
1001 * @mpath: the mesh path to modify
1002 * @next_hop: the next hop to force
1004 * Locking: this function must be called holding mpath->state_lock
1006 void mesh_path_fix_nexthop(struct mesh_path *mpath, struct sta_info *next_hop)
1008 spin_lock_bh(&mpath->state_lock);
1009 mesh_path_assign_nexthop(mpath, next_hop);
1012 mpath->hop_count = 0;
1013 mpath->exp_time = 0;
1014 mpath->flags |= MESH_PATH_FIXED;
1015 mesh_path_activate(mpath);
1016 spin_unlock_bh(&mpath->state_lock);
1017 mesh_path_tx_pending(mpath);
1020 static void mesh_path_node_free(struct hlist_node *p, bool free_leafs)
1022 struct mesh_path *mpath;
1023 struct mpath_node *node = hlist_entry(p, struct mpath_node, list);
1024 mpath = node->mpath;
1027 del_timer_sync(&mpath->timer);
1033 static int mesh_path_node_copy(struct hlist_node *p, struct mesh_table *newtbl)
1035 struct mesh_path *mpath;
1036 struct mpath_node *node, *new_node;
1039 new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
1040 if (new_node == NULL)
1043 node = hlist_entry(p, struct mpath_node, list);
1044 mpath = node->mpath;
1045 new_node->mpath = mpath;
1046 hash_idx = mesh_table_hash(mpath->dst, mpath->sdata, newtbl);
1047 hlist_add_head(&new_node->list,
1048 &newtbl->hash_buckets[hash_idx]);
1052 int mesh_pathtbl_init(void)
1054 struct mesh_table *tbl_path, *tbl_mpp;
1057 tbl_path = mesh_table_alloc(INIT_PATHS_SIZE_ORDER);
1060 tbl_path->free_node = &mesh_path_node_free;
1061 tbl_path->copy_node = &mesh_path_node_copy;
1062 tbl_path->mean_chain_len = MEAN_CHAIN_LEN;
1063 tbl_path->known_gates = kzalloc(sizeof(struct hlist_head), GFP_ATOMIC);
1064 if (!tbl_path->known_gates) {
1068 INIT_HLIST_HEAD(tbl_path->known_gates);
1071 tbl_mpp = mesh_table_alloc(INIT_PATHS_SIZE_ORDER);
1076 tbl_mpp->free_node = &mesh_path_node_free;
1077 tbl_mpp->copy_node = &mesh_path_node_copy;
1078 tbl_mpp->mean_chain_len = MEAN_CHAIN_LEN;
1079 tbl_mpp->known_gates = kzalloc(sizeof(struct hlist_head), GFP_ATOMIC);
1080 if (!tbl_mpp->known_gates) {
1084 INIT_HLIST_HEAD(tbl_mpp->known_gates);
1086 /* Need no locking since this is during init */
1087 RCU_INIT_POINTER(mesh_paths, tbl_path);
1088 RCU_INIT_POINTER(mpp_paths, tbl_mpp);
1093 mesh_table_free(tbl_mpp, true);
1095 mesh_table_free(tbl_path, true);
1099 void mesh_path_expire(struct ieee80211_sub_if_data *sdata)
1101 struct mesh_table *tbl;
1102 struct mesh_path *mpath;
1103 struct mpath_node *node;
1104 struct hlist_node *p;
1108 tbl = rcu_dereference(mesh_paths);
1109 for_each_mesh_entry(tbl, p, node, i) {
1110 if (node->mpath->sdata != sdata)
1112 mpath = node->mpath;
1113 if ((!(mpath->flags & MESH_PATH_RESOLVING)) &&
1114 (!(mpath->flags & MESH_PATH_FIXED)) &&
1115 time_after(jiffies, mpath->exp_time + MESH_PATH_EXPIRE))
1116 mesh_path_del(mpath->dst, mpath->sdata);
1121 void mesh_pathtbl_unregister(void)
1123 /* no need for locking during exit path */
1124 mesh_table_free(rcu_dereference_protected(mesh_paths, 1), true);
1125 mesh_table_free(rcu_dereference_protected(mpp_paths, 1), true);