xref: /openbmc/linux/net/mac80211/mesh_pathtbl.c (revision 63d5f89b)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2eb2b9311SLuis Carlos Cobo /*
3264d9b7dSRui Paulo  * Copyright (c) 2008, 2009 open80211s Ltd.
4eb2b9311SLuis Carlos Cobo  * Author:     Luis Carlos Cobo <luisca@cozybit.com>
5eb2b9311SLuis Carlos Cobo  */
6eb2b9311SLuis Carlos Cobo 
7eb2b9311SLuis Carlos Cobo #include <linux/etherdevice.h>
8eb2b9311SLuis Carlos Cobo #include <linux/list.h>
9eb2b9311SLuis Carlos Cobo #include <linux/random.h>
105a0e3ad6STejun Heo #include <linux/slab.h>
11eb2b9311SLuis Carlos Cobo #include <linux/spinlock.h>
12eb2b9311SLuis Carlos Cobo #include <linux/string.h>
13eb2b9311SLuis Carlos Cobo #include <net/mac80211.h>
144777be41SJavier Cardona #include "wme.h"
15eb2b9311SLuis Carlos Cobo #include "ieee80211_i.h"
16eb2b9311SLuis Carlos Cobo #include "mesh.h"
17d5edb9aeSFelix Fietkau #include <linux/rhashtable.h>
18eb2b9311SLuis Carlos Cobo 
1974932959SBob Copeland static void mesh_path_free_rcu(struct mesh_table *tbl, struct mesh_path *mpath);
2074932959SBob Copeland 
mesh_table_hash(const void * addr,u32 len,u32 seed)2160854fd9SBob Copeland static u32 mesh_table_hash(const void *addr, u32 len, u32 seed)
2260854fd9SBob Copeland {
2360854fd9SBob Copeland 	/* Use last four bytes of hw addr as hash index */
2440586e3fSFelix Fietkau 	return jhash_1word(__get_unaligned_cpu32((u8 *)addr + 2), seed);
2560854fd9SBob Copeland }
26eb2b9311SLuis Carlos Cobo 
2760854fd9SBob Copeland static const struct rhashtable_params mesh_rht_params = {
2860854fd9SBob Copeland 	.nelem_hint = 2,
2960854fd9SBob Copeland 	.automatic_shrinking = true,
3060854fd9SBob Copeland 	.key_len = ETH_ALEN,
3160854fd9SBob Copeland 	.key_offset = offsetof(struct mesh_path, dst),
3260854fd9SBob Copeland 	.head_offset = offsetof(struct mesh_path, rhash),
3360854fd9SBob Copeland 	.hashfn = mesh_table_hash,
3460854fd9SBob Copeland };
35eb2b9311SLuis Carlos Cobo 
36d5edb9aeSFelix Fietkau static const struct rhashtable_params fast_tx_rht_params = {
37d5edb9aeSFelix Fietkau 	.nelem_hint = 10,
38d5edb9aeSFelix Fietkau 	.automatic_shrinking = true,
39154be74eSFelix Fietkau 	.key_len = sizeof_field(struct ieee80211_mesh_fast_tx, key),
40154be74eSFelix Fietkau 	.key_offset = offsetof(struct ieee80211_mesh_fast_tx, key),
41d5edb9aeSFelix Fietkau 	.head_offset = offsetof(struct ieee80211_mesh_fast_tx, rhash),
42d5edb9aeSFelix Fietkau 	.hashfn = mesh_table_hash,
43d5edb9aeSFelix Fietkau };
44d5edb9aeSFelix Fietkau 
__mesh_fast_tx_entry_free(void * ptr,void * tblptr)45d5edb9aeSFelix Fietkau static void __mesh_fast_tx_entry_free(void *ptr, void *tblptr)
46d5edb9aeSFelix Fietkau {
47d5edb9aeSFelix Fietkau 	struct ieee80211_mesh_fast_tx *entry = ptr;
48d5edb9aeSFelix Fietkau 
49d5edb9aeSFelix Fietkau 	kfree_rcu(entry, fast_tx.rcu_head);
50d5edb9aeSFelix Fietkau }
51d5edb9aeSFelix Fietkau 
mesh_fast_tx_deinit(struct ieee80211_sub_if_data * sdata)52d5edb9aeSFelix Fietkau static void mesh_fast_tx_deinit(struct ieee80211_sub_if_data *sdata)
53d5edb9aeSFelix Fietkau {
54d5edb9aeSFelix Fietkau 	struct mesh_tx_cache *cache;
55d5edb9aeSFelix Fietkau 
56d5edb9aeSFelix Fietkau 	cache = &sdata->u.mesh.tx_cache;
57d5edb9aeSFelix Fietkau 	rhashtable_free_and_destroy(&cache->rht,
58d5edb9aeSFelix Fietkau 				    __mesh_fast_tx_entry_free, NULL);
59d5edb9aeSFelix Fietkau }
60d5edb9aeSFelix Fietkau 
mesh_fast_tx_init(struct ieee80211_sub_if_data * sdata)61d5edb9aeSFelix Fietkau static void mesh_fast_tx_init(struct ieee80211_sub_if_data *sdata)
62d5edb9aeSFelix Fietkau {
63d5edb9aeSFelix Fietkau 	struct mesh_tx_cache *cache;
64d5edb9aeSFelix Fietkau 
65d5edb9aeSFelix Fietkau 	cache = &sdata->u.mesh.tx_cache;
66d5edb9aeSFelix Fietkau 	rhashtable_init(&cache->rht, &fast_tx_rht_params);
67d5edb9aeSFelix Fietkau 	INIT_HLIST_HEAD(&cache->walk_head);
68d5edb9aeSFelix Fietkau 	spin_lock_init(&cache->walk_lock);
69d5edb9aeSFelix Fietkau }
70d5edb9aeSFelix Fietkau 
mpath_expired(struct mesh_path * mpath)71bf7cd94dSJohannes Berg static inline bool mpath_expired(struct mesh_path *mpath)
72bf7cd94dSJohannes Berg {
73bf7cd94dSJohannes Berg 	return (mpath->flags & MESH_PATH_ACTIVE) &&
74bf7cd94dSJohannes Berg 	       time_after(jiffies, mpath->exp_time) &&
75bf7cd94dSJohannes Berg 	       !(mpath->flags & MESH_PATH_FIXED);
76bf7cd94dSJohannes Berg }
77eb2b9311SLuis Carlos Cobo 
mesh_path_rht_free(void * ptr,void * tblptr)7874932959SBob Copeland static void mesh_path_rht_free(void *ptr, void *tblptr)
79349eb8cfSJohannes Berg {
8060854fd9SBob Copeland 	struct mesh_path *mpath = ptr;
8174932959SBob Copeland 	struct mesh_table *tbl = tblptr;
8274932959SBob Copeland 
8374932959SBob Copeland 	mesh_path_free_rcu(tbl, mpath);
84349eb8cfSJohannes Berg }
85349eb8cfSJohannes Berg 
mesh_table_init(struct mesh_table * tbl)868b5cb7e4SPavel Skripkin static void mesh_table_init(struct mesh_table *tbl)
87349eb8cfSJohannes Berg {
888b5cb7e4SPavel Skripkin 	INIT_HLIST_HEAD(&tbl->known_gates);
898b5cb7e4SPavel Skripkin 	INIT_HLIST_HEAD(&tbl->walk_head);
908b5cb7e4SPavel Skripkin 	atomic_set(&tbl->entries,  0);
918b5cb7e4SPavel Skripkin 	spin_lock_init(&tbl->gates_lock);
928b5cb7e4SPavel Skripkin 	spin_lock_init(&tbl->walk_lock);
936b86bd62SJohannes Berg 
948b5cb7e4SPavel Skripkin 	/* rhashtable_init() may fail only in case of wrong
958b5cb7e4SPavel Skripkin 	 * mesh_rht_params
968b5cb7e4SPavel Skripkin 	 */
978b5cb7e4SPavel Skripkin 	WARN_ON(rhashtable_init(&tbl->rhead, &mesh_rht_params));
986b86bd62SJohannes Berg }
996b86bd62SJohannes Berg 
mesh_table_free(struct mesh_table * tbl)10060854fd9SBob Copeland static void mesh_table_free(struct mesh_table *tbl)
10118889231SJavier Cardona {
10260854fd9SBob Copeland 	rhashtable_free_and_destroy(&tbl->rhead,
10374932959SBob Copeland 				    mesh_path_rht_free, tbl);
10418889231SJavier Cardona }
10518889231SJavier Cardona 
106eb2b9311SLuis Carlos Cobo /**
107eb2b9311SLuis Carlos Cobo  * mesh_path_assign_nexthop - update mesh path next hop
108eb2b9311SLuis Carlos Cobo  *
109eb2b9311SLuis Carlos Cobo  * @mpath: mesh path to update
110eb2b9311SLuis Carlos Cobo  * @sta: next hop to assign
111eb2b9311SLuis Carlos Cobo  *
112eb2b9311SLuis Carlos Cobo  * Locking: mpath->state_lock must be held when calling this function
113eb2b9311SLuis Carlos Cobo  */
mesh_path_assign_nexthop(struct mesh_path * mpath,struct sta_info * sta)114eb2b9311SLuis Carlos Cobo void mesh_path_assign_nexthop(struct mesh_path *mpath, struct sta_info *sta)
115eb2b9311SLuis Carlos Cobo {
11610c836d7SJavier Cardona 	struct sk_buff *skb;
11710c836d7SJavier Cardona 	struct ieee80211_hdr *hdr;
11810c836d7SJavier Cardona 	unsigned long flags;
11910c836d7SJavier Cardona 
120d0709a65SJohannes Berg 	rcu_assign_pointer(mpath->next_hop, sta);
12110c836d7SJavier Cardona 
12210c836d7SJavier Cardona 	spin_lock_irqsave(&mpath->frame_queue.lock, flags);
123b22bd522SThomas Pedersen 	skb_queue_walk(&mpath->frame_queue, skb) {
12410c836d7SJavier Cardona 		hdr = (struct ieee80211_hdr *) skb->data;
12510c836d7SJavier Cardona 		memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN);
1267e3c8866SThomas Pedersen 		memcpy(hdr->addr2, mpath->sdata->vif.addr, ETH_ALEN);
1273f52b7e3SMarco Porsch 		ieee80211_mps_set_frame_flags(sta->sdata, sta, hdr);
12810c836d7SJavier Cardona 	}
12910c836d7SJavier Cardona 
13010c836d7SJavier Cardona 	spin_unlock_irqrestore(&mpath->frame_queue.lock, flags);
131eb2b9311SLuis Carlos Cobo }
132eb2b9311SLuis Carlos Cobo 
prepare_for_gate(struct sk_buff * skb,char * dst_addr,struct mesh_path * gate_mpath)1335ee68e5bSJavier Cardona static void prepare_for_gate(struct sk_buff *skb, char *dst_addr,
1345ee68e5bSJavier Cardona 			     struct mesh_path *gate_mpath)
1355ee68e5bSJavier Cardona {
1365ee68e5bSJavier Cardona 	struct ieee80211_hdr *hdr;
1375ee68e5bSJavier Cardona 	struct ieee80211s_hdr *mshdr;
1385ee68e5bSJavier Cardona 	int mesh_hdrlen, hdrlen;
1395ee68e5bSJavier Cardona 	char *next_hop;
1405ee68e5bSJavier Cardona 
1415ee68e5bSJavier Cardona 	hdr = (struct ieee80211_hdr *) skb->data;
1425ee68e5bSJavier Cardona 	hdrlen = ieee80211_hdrlen(hdr->frame_control);
1435ee68e5bSJavier Cardona 	mshdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
1445ee68e5bSJavier Cardona 
1455ee68e5bSJavier Cardona 	if (!(mshdr->flags & MESH_FLAGS_AE)) {
1465ee68e5bSJavier Cardona 		/* size of the fixed part of the mesh header */
1475ee68e5bSJavier Cardona 		mesh_hdrlen = 6;
1485ee68e5bSJavier Cardona 
1495ee68e5bSJavier Cardona 		/* make room for the two extended addresses */
1505ee68e5bSJavier Cardona 		skb_push(skb, 2 * ETH_ALEN);
1515ee68e5bSJavier Cardona 		memmove(skb->data, hdr, hdrlen + mesh_hdrlen);
1525ee68e5bSJavier Cardona 
1535ee68e5bSJavier Cardona 		hdr = (struct ieee80211_hdr *) skb->data;
1545ee68e5bSJavier Cardona 
1555ee68e5bSJavier Cardona 		/* we preserve the previous mesh header and only add
156ab4040dfSZheng Yongjun 		 * the new addresses */
1575ee68e5bSJavier Cardona 		mshdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
1585ee68e5bSJavier Cardona 		mshdr->flags = MESH_FLAGS_AE_A5_A6;
1595ee68e5bSJavier Cardona 		memcpy(mshdr->eaddr1, hdr->addr3, ETH_ALEN);
1605ee68e5bSJavier Cardona 		memcpy(mshdr->eaddr2, hdr->addr4, ETH_ALEN);
1615ee68e5bSJavier Cardona 	}
1625ee68e5bSJavier Cardona 
1635ee68e5bSJavier Cardona 	/* update next hop */
1645ee68e5bSJavier Cardona 	hdr = (struct ieee80211_hdr *) skb->data;
1655ee68e5bSJavier Cardona 	rcu_read_lock();
1665ee68e5bSJavier Cardona 	next_hop = rcu_dereference(gate_mpath->next_hop)->sta.addr;
1675ee68e5bSJavier Cardona 	memcpy(hdr->addr1, next_hop, ETH_ALEN);
1685ee68e5bSJavier Cardona 	rcu_read_unlock();
1697e3c8866SThomas Pedersen 	memcpy(hdr->addr2, gate_mpath->sdata->vif.addr, ETH_ALEN);
1705ee68e5bSJavier Cardona 	memcpy(hdr->addr3, dst_addr, ETH_ALEN);
1715ee68e5bSJavier Cardona }
1725ee68e5bSJavier Cardona 
1735ee68e5bSJavier Cardona /**
1745ee68e5bSJavier Cardona  * mesh_path_move_to_queue - Move or copy frames from one mpath queue to another
1755ee68e5bSJavier Cardona  *
1765ee68e5bSJavier Cardona  * This function is used to transfer or copy frames from an unresolved mpath to
1775ee68e5bSJavier Cardona  * a gate mpath.  The function also adds the Address Extension field and
1785ee68e5bSJavier Cardona  * updates the next hop.
1795ee68e5bSJavier Cardona  *
1805ee68e5bSJavier Cardona  * If a frame already has an Address Extension field, only the next hop and
1815ee68e5bSJavier Cardona  * destination addresses are updated.
1825ee68e5bSJavier Cardona  *
1835ee68e5bSJavier Cardona  * The gate mpath must be an active mpath with a valid mpath->next_hop.
1845ee68e5bSJavier Cardona  *
1859fd00b4dSAndrew Lunn  * @gate_mpath: An active mpath the frames will be sent to (i.e. the gate)
1865ee68e5bSJavier Cardona  * @from_mpath: The failed mpath
1875ee68e5bSJavier Cardona  * @copy: When true, copy all the frames to the new mpath queue.  When false,
1885ee68e5bSJavier Cardona  * move them.
1895ee68e5bSJavier Cardona  */
mesh_path_move_to_queue(struct mesh_path * gate_mpath,struct mesh_path * from_mpath,bool copy)1905ee68e5bSJavier Cardona static void mesh_path_move_to_queue(struct mesh_path *gate_mpath,
1915ee68e5bSJavier Cardona 				    struct mesh_path *from_mpath,
1925ee68e5bSJavier Cardona 				    bool copy)
1935ee68e5bSJavier Cardona {
1944bd4c2ddSThomas Pedersen 	struct sk_buff *skb, *fskb, *tmp;
1954bd4c2ddSThomas Pedersen 	struct sk_buff_head failq;
1965ee68e5bSJavier Cardona 	unsigned long flags;
1975ee68e5bSJavier Cardona 
1988c5bb1faSJohannes Berg 	if (WARN_ON(gate_mpath == from_mpath))
1998c5bb1faSJohannes Berg 		return;
2008c5bb1faSJohannes Berg 	if (WARN_ON(!gate_mpath->next_hop))
2018c5bb1faSJohannes Berg 		return;
2025ee68e5bSJavier Cardona 
2035ee68e5bSJavier Cardona 	__skb_queue_head_init(&failq);
2045ee68e5bSJavier Cardona 
2055ee68e5bSJavier Cardona 	spin_lock_irqsave(&from_mpath->frame_queue.lock, flags);
2065ee68e5bSJavier Cardona 	skb_queue_splice_init(&from_mpath->frame_queue, &failq);
2075ee68e5bSJavier Cardona 	spin_unlock_irqrestore(&from_mpath->frame_queue.lock, flags);
2085ee68e5bSJavier Cardona 
2094bd4c2ddSThomas Pedersen 	skb_queue_walk_safe(&failq, fskb, tmp) {
2104bd4c2ddSThomas Pedersen 		if (skb_queue_len(&gate_mpath->frame_queue) >=
2114bd4c2ddSThomas Pedersen 				  MESH_FRAME_QUEUE_LEN) {
2124bd4c2ddSThomas Pedersen 			mpath_dbg(gate_mpath->sdata, "mpath queue full!\n");
2134bd4c2ddSThomas Pedersen 			break;
214817a53d9SJohn W. Linville 		}
2155ee68e5bSJavier Cardona 
2164bd4c2ddSThomas Pedersen 		skb = skb_copy(fskb, GFP_ATOMIC);
2174bd4c2ddSThomas Pedersen 		if (WARN_ON(!skb))
2184bd4c2ddSThomas Pedersen 			break;
2194bd4c2ddSThomas Pedersen 
2205ee68e5bSJavier Cardona 		prepare_for_gate(skb, gate_mpath->dst, gate_mpath);
2214bd4c2ddSThomas Pedersen 		skb_queue_tail(&gate_mpath->frame_queue, skb);
2224bd4c2ddSThomas Pedersen 
2234bd4c2ddSThomas Pedersen 		if (copy)
2244bd4c2ddSThomas Pedersen 			continue;
2254bd4c2ddSThomas Pedersen 
2264bd4c2ddSThomas Pedersen 		__skb_unlink(fskb, &failq);
2274bd4c2ddSThomas Pedersen 		kfree_skb(fskb);
2285ee68e5bSJavier Cardona 	}
2295ee68e5bSJavier Cardona 
230bdcbd8e0SJohannes Berg 	mpath_dbg(gate_mpath->sdata, "Mpath queue for gate %pM has %d frames\n",
231bdcbd8e0SJohannes Berg 		  gate_mpath->dst, skb_queue_len(&gate_mpath->frame_queue));
2325ee68e5bSJavier Cardona 
2335ee68e5bSJavier Cardona 	if (!copy)
2345ee68e5bSJavier Cardona 		return;
2355ee68e5bSJavier Cardona 
2365ee68e5bSJavier Cardona 	spin_lock_irqsave(&from_mpath->frame_queue.lock, flags);
2375ee68e5bSJavier Cardona 	skb_queue_splice(&failq, &from_mpath->frame_queue);
2385ee68e5bSJavier Cardona 	spin_unlock_irqrestore(&from_mpath->frame_queue.lock, flags);
2395ee68e5bSJavier Cardona }
2405ee68e5bSJavier Cardona 
241eb2b9311SLuis Carlos Cobo 
mpath_lookup(struct mesh_table * tbl,const u8 * dst,struct ieee80211_sub_if_data * sdata)2424a3cb702SJohannes Berg static struct mesh_path *mpath_lookup(struct mesh_table *tbl, const u8 *dst,
243239289e4SJavier Cardona 				      struct ieee80211_sub_if_data *sdata)
244239289e4SJavier Cardona {
245239289e4SJavier Cardona 	struct mesh_path *mpath;
246239289e4SJavier Cardona 
247ef618b1bSFelix Fietkau 	mpath = rhashtable_lookup(&tbl->rhead, dst, mesh_rht_params);
24860854fd9SBob Copeland 
24960854fd9SBob Copeland 	if (mpath && mpath_expired(mpath)) {
250239289e4SJavier Cardona 		spin_lock_bh(&mpath->state_lock);
251239289e4SJavier Cardona 		mpath->flags &= ~MESH_PATH_ACTIVE;
252239289e4SJavier Cardona 		spin_unlock_bh(&mpath->state_lock);
253239289e4SJavier Cardona 	}
254239289e4SJavier Cardona 	return mpath;
255239289e4SJavier Cardona }
256239289e4SJavier Cardona 
257eb2b9311SLuis Carlos Cobo /**
258eb2b9311SLuis Carlos Cobo  * mesh_path_lookup - look up a path in the mesh path table
259f698d856SJasper Bryant-Greene  * @sdata: local subif
260bf7cd94dSJohannes Berg  * @dst: hardware address (ETH_ALEN length) of destination
261eb2b9311SLuis Carlos Cobo  *
262eb2b9311SLuis Carlos Cobo  * Returns: pointer to the mesh path structure, or NULL if not found
263eb2b9311SLuis Carlos Cobo  *
264eb2b9311SLuis Carlos Cobo  * Locking: must be called within a read rcu section.
265eb2b9311SLuis Carlos Cobo  */
266bf7cd94dSJohannes Berg struct mesh_path *
mesh_path_lookup(struct ieee80211_sub_if_data * sdata,const u8 * dst)267bf7cd94dSJohannes Berg mesh_path_lookup(struct ieee80211_sub_if_data *sdata, const u8 *dst)
268eb2b9311SLuis Carlos Cobo {
2698b5cb7e4SPavel Skripkin 	return mpath_lookup(&sdata->u.mesh.mesh_paths, dst, sdata);
270eb2b9311SLuis Carlos Cobo }
271eb2b9311SLuis Carlos Cobo 
272bf7cd94dSJohannes Berg struct mesh_path *
mpp_path_lookup(struct ieee80211_sub_if_data * sdata,const u8 * dst)273bf7cd94dSJohannes Berg mpp_path_lookup(struct ieee80211_sub_if_data *sdata, const u8 *dst)
27479617deeSYanBo {
2758b5cb7e4SPavel Skripkin 	return mpath_lookup(&sdata->u.mesh.mpp_paths, dst, sdata);
27679617deeSYanBo }
27779617deeSYanBo 
27860854fd9SBob Copeland static struct mesh_path *
__mesh_path_lookup_by_idx(struct mesh_table * tbl,int idx)27960854fd9SBob Copeland __mesh_path_lookup_by_idx(struct mesh_table *tbl, int idx)
28060854fd9SBob Copeland {
281b4c3fbe6SHerbert Xu 	int i = 0;
282b4c3fbe6SHerbert Xu 	struct mesh_path *mpath;
28360854fd9SBob Copeland 
284b4c3fbe6SHerbert Xu 	hlist_for_each_entry_rcu(mpath, &tbl->walk_head, walk_list) {
28560854fd9SBob Copeland 		if (i++ == idx)
28660854fd9SBob Copeland 			break;
28760854fd9SBob Copeland 	}
28860854fd9SBob Copeland 
289b4c3fbe6SHerbert Xu 	if (!mpath)
29060854fd9SBob Copeland 		return NULL;
29160854fd9SBob Copeland 
29260854fd9SBob Copeland 	if (mpath_expired(mpath)) {
29360854fd9SBob Copeland 		spin_lock_bh(&mpath->state_lock);
29460854fd9SBob Copeland 		mpath->flags &= ~MESH_PATH_ACTIVE;
29560854fd9SBob Copeland 		spin_unlock_bh(&mpath->state_lock);
29660854fd9SBob Copeland 	}
29760854fd9SBob Copeland 	return mpath;
29860854fd9SBob Copeland }
29979617deeSYanBo 
300eb2b9311SLuis Carlos Cobo /**
301eb2b9311SLuis Carlos Cobo  * mesh_path_lookup_by_idx - look up a path in the mesh path table by its index
302eb2b9311SLuis Carlos Cobo  * @idx: index
303f698d856SJasper Bryant-Greene  * @sdata: local subif, or NULL for all entries
304eb2b9311SLuis Carlos Cobo  *
305eb2b9311SLuis Carlos Cobo  * Returns: pointer to the mesh path structure, or NULL if not found.
306eb2b9311SLuis Carlos Cobo  *
307eb2b9311SLuis Carlos Cobo  * Locking: must be called within a read rcu section.
308eb2b9311SLuis Carlos Cobo  */
309bf7cd94dSJohannes Berg struct mesh_path *
mesh_path_lookup_by_idx(struct ieee80211_sub_if_data * sdata,int idx)310bf7cd94dSJohannes Berg mesh_path_lookup_by_idx(struct ieee80211_sub_if_data *sdata, int idx)
311eb2b9311SLuis Carlos Cobo {
3128b5cb7e4SPavel Skripkin 	return __mesh_path_lookup_by_idx(&sdata->u.mesh.mesh_paths, idx);
313eb2b9311SLuis Carlos Cobo }
314eb2b9311SLuis Carlos Cobo 
3155ee68e5bSJavier Cardona /**
316a2db2ed3SHenning Rogge  * mpp_path_lookup_by_idx - look up a path in the proxy path table by its index
317a2db2ed3SHenning Rogge  * @idx: index
318a2db2ed3SHenning Rogge  * @sdata: local subif, or NULL for all entries
319a2db2ed3SHenning Rogge  *
320a2db2ed3SHenning Rogge  * Returns: pointer to the proxy path structure, or NULL if not found.
321a2db2ed3SHenning Rogge  *
322a2db2ed3SHenning Rogge  * Locking: must be called within a read rcu section.
323a2db2ed3SHenning Rogge  */
324a2db2ed3SHenning Rogge struct mesh_path *
mpp_path_lookup_by_idx(struct ieee80211_sub_if_data * sdata,int idx)325a2db2ed3SHenning Rogge mpp_path_lookup_by_idx(struct ieee80211_sub_if_data *sdata, int idx)
326a2db2ed3SHenning Rogge {
3278b5cb7e4SPavel Skripkin 	return __mesh_path_lookup_by_idx(&sdata->u.mesh.mpp_paths, idx);
328a2db2ed3SHenning Rogge }
329a2db2ed3SHenning Rogge 
330a2db2ed3SHenning Rogge /**
33130be52e4SJohannes Berg  * mesh_path_add_gate - add the given mpath to a mesh gate to our path table
33230be52e4SJohannes Berg  * @mpath: gate path to add to table
3335ee68e5bSJavier Cardona  */
mesh_path_add_gate(struct mesh_path * mpath)33430be52e4SJohannes Berg int mesh_path_add_gate(struct mesh_path *mpath)
3355ee68e5bSJavier Cardona {
33630be52e4SJohannes Berg 	struct mesh_table *tbl;
3375ee68e5bSJavier Cardona 	int err;
3385ee68e5bSJavier Cardona 
3395ee68e5bSJavier Cardona 	rcu_read_lock();
3408b5cb7e4SPavel Skripkin 	tbl = &mpath->sdata->u.mesh.mesh_paths;
3415ee68e5bSJavier Cardona 
342947c2a0eSBob Copeland 	spin_lock_bh(&mpath->state_lock);
343947c2a0eSBob Copeland 	if (mpath->is_gate) {
3445ee68e5bSJavier Cardona 		err = -EEXIST;
345947c2a0eSBob Copeland 		spin_unlock_bh(&mpath->state_lock);
3465ee68e5bSJavier Cardona 		goto err_rcu;
3475ee68e5bSJavier Cardona 	}
3485ee68e5bSJavier Cardona 	mpath->is_gate = true;
3495ee68e5bSJavier Cardona 	mpath->sdata->u.mesh.num_gates++;
350947c2a0eSBob Copeland 
351947c2a0eSBob Copeland 	spin_lock(&tbl->gates_lock);
35218b27ff7SBob Copeland 	hlist_add_head_rcu(&mpath->gate_list, &tbl->known_gates);
353947c2a0eSBob Copeland 	spin_unlock(&tbl->gates_lock);
354947c2a0eSBob Copeland 
355947c2a0eSBob Copeland 	spin_unlock_bh(&mpath->state_lock);
356947c2a0eSBob Copeland 
357bdcbd8e0SJohannes Berg 	mpath_dbg(mpath->sdata,
358bdcbd8e0SJohannes Berg 		  "Mesh path: Recorded new gate: %pM. %d known gates\n",
359bdcbd8e0SJohannes Berg 		  mpath->dst, mpath->sdata->u.mesh.num_gates);
360bf7cd94dSJohannes Berg 	err = 0;
3615ee68e5bSJavier Cardona err_rcu:
3625ee68e5bSJavier Cardona 	rcu_read_unlock();
3635ee68e5bSJavier Cardona 	return err;
3645ee68e5bSJavier Cardona }
3655ee68e5bSJavier Cardona 
3665ee68e5bSJavier Cardona /**
3675ee68e5bSJavier Cardona  * mesh_gate_del - remove a mesh gate from the list of known gates
3685ee68e5bSJavier Cardona  * @tbl: table which holds our list of known gates
3695ee68e5bSJavier Cardona  * @mpath: gate mpath
3705ee68e5bSJavier Cardona  */
mesh_gate_del(struct mesh_table * tbl,struct mesh_path * mpath)371bf7cd94dSJohannes Berg static void mesh_gate_del(struct mesh_table *tbl, struct mesh_path *mpath)
3725ee68e5bSJavier Cardona {
373947c2a0eSBob Copeland 	lockdep_assert_held(&mpath->state_lock);
374947c2a0eSBob Copeland 	if (!mpath->is_gate)
375947c2a0eSBob Copeland 		return;
3765ee68e5bSJavier Cardona 
3775ee68e5bSJavier Cardona 	mpath->is_gate = false;
378947c2a0eSBob Copeland 	spin_lock_bh(&tbl->gates_lock);
379947c2a0eSBob Copeland 	hlist_del_rcu(&mpath->gate_list);
380947c2a0eSBob Copeland 	mpath->sdata->u.mesh.num_gates--;
381947c2a0eSBob Copeland 	spin_unlock_bh(&tbl->gates_lock);
382947c2a0eSBob Copeland 
383bdcbd8e0SJohannes Berg 	mpath_dbg(mpath->sdata,
384bdcbd8e0SJohannes Berg 		  "Mesh path: Deleted gate: %pM. %d known gates\n",
3855ee68e5bSJavier Cardona 		  mpath->dst, mpath->sdata->u.mesh.num_gates);
3865ee68e5bSJavier Cardona }
3875ee68e5bSJavier Cardona 
3885ee68e5bSJavier Cardona /**
3895ee68e5bSJavier Cardona  * mesh_gate_num - number of gates known to this interface
3905ee68e5bSJavier Cardona  * @sdata: subif data
3915ee68e5bSJavier Cardona  */
mesh_gate_num(struct ieee80211_sub_if_data * sdata)3925ee68e5bSJavier Cardona int mesh_gate_num(struct ieee80211_sub_if_data *sdata)
3935ee68e5bSJavier Cardona {
3945ee68e5bSJavier Cardona 	return sdata->u.mesh.num_gates;
3955ee68e5bSJavier Cardona }
3965ee68e5bSJavier Cardona 
397b15dc38bSBob Copeland static
mesh_path_new(struct ieee80211_sub_if_data * sdata,const u8 * dst,gfp_t gfp_flags)398b15dc38bSBob Copeland struct mesh_path *mesh_path_new(struct ieee80211_sub_if_data *sdata,
399b15dc38bSBob Copeland 				const u8 *dst, gfp_t gfp_flags)
400b15dc38bSBob Copeland {
401b15dc38bSBob Copeland 	struct mesh_path *new_mpath;
402b15dc38bSBob Copeland 
403b15dc38bSBob Copeland 	new_mpath = kzalloc(sizeof(struct mesh_path), gfp_flags);
404b15dc38bSBob Copeland 	if (!new_mpath)
405b15dc38bSBob Copeland 		return NULL;
406b15dc38bSBob Copeland 
407b15dc38bSBob Copeland 	memcpy(new_mpath->dst, dst, ETH_ALEN);
408b15dc38bSBob Copeland 	eth_broadcast_addr(new_mpath->rann_snd_addr);
409b15dc38bSBob Copeland 	new_mpath->is_root = false;
410b15dc38bSBob Copeland 	new_mpath->sdata = sdata;
411b15dc38bSBob Copeland 	new_mpath->flags = 0;
412b15dc38bSBob Copeland 	skb_queue_head_init(&new_mpath->frame_queue);
413b15dc38bSBob Copeland 	new_mpath->exp_time = jiffies;
414b15dc38bSBob Copeland 	spin_lock_init(&new_mpath->state_lock);
41534f11cd3SKees Cook 	timer_setup(&new_mpath->timer, mesh_path_timer, 0);
416b15dc38bSBob Copeland 
417b15dc38bSBob Copeland 	return new_mpath;
418b15dc38bSBob Copeland }
419b15dc38bSBob Copeland 
mesh_fast_tx_entry_free(struct mesh_tx_cache * cache,struct ieee80211_mesh_fast_tx * entry)420d5edb9aeSFelix Fietkau static void mesh_fast_tx_entry_free(struct mesh_tx_cache *cache,
421d5edb9aeSFelix Fietkau 				    struct ieee80211_mesh_fast_tx *entry)
422d5edb9aeSFelix Fietkau {
423d5edb9aeSFelix Fietkau 	hlist_del_rcu(&entry->walk_list);
424d5edb9aeSFelix Fietkau 	rhashtable_remove_fast(&cache->rht, &entry->rhash, fast_tx_rht_params);
425d5edb9aeSFelix Fietkau 	kfree_rcu(entry, fast_tx.rcu_head);
426d5edb9aeSFelix Fietkau }
427d5edb9aeSFelix Fietkau 
428d5edb9aeSFelix Fietkau struct ieee80211_mesh_fast_tx *
mesh_fast_tx_get(struct ieee80211_sub_if_data * sdata,struct ieee80211_mesh_fast_tx_key * key)429154be74eSFelix Fietkau mesh_fast_tx_get(struct ieee80211_sub_if_data *sdata,
430154be74eSFelix Fietkau 		 struct ieee80211_mesh_fast_tx_key *key)
431d5edb9aeSFelix Fietkau {
432d5edb9aeSFelix Fietkau 	struct ieee80211_mesh_fast_tx *entry;
433d5edb9aeSFelix Fietkau 	struct mesh_tx_cache *cache;
434d5edb9aeSFelix Fietkau 
435d5edb9aeSFelix Fietkau 	cache = &sdata->u.mesh.tx_cache;
436154be74eSFelix Fietkau 	entry = rhashtable_lookup(&cache->rht, key, fast_tx_rht_params);
437d5edb9aeSFelix Fietkau 	if (!entry)
438d5edb9aeSFelix Fietkau 		return NULL;
439d5edb9aeSFelix Fietkau 
440d5edb9aeSFelix Fietkau 	if (!(entry->mpath->flags & MESH_PATH_ACTIVE) ||
441d5edb9aeSFelix Fietkau 	    mpath_expired(entry->mpath)) {
442d5edb9aeSFelix Fietkau 		spin_lock_bh(&cache->walk_lock);
443154be74eSFelix Fietkau 		entry = rhashtable_lookup(&cache->rht, key, fast_tx_rht_params);
444d5edb9aeSFelix Fietkau 		if (entry)
445d5edb9aeSFelix Fietkau 		    mesh_fast_tx_entry_free(cache, entry);
446d5edb9aeSFelix Fietkau 		spin_unlock_bh(&cache->walk_lock);
447d5edb9aeSFelix Fietkau 		return NULL;
448d5edb9aeSFelix Fietkau 	}
449d5edb9aeSFelix Fietkau 
450d5edb9aeSFelix Fietkau 	mesh_path_refresh(sdata, entry->mpath, NULL);
451d5edb9aeSFelix Fietkau 	if (entry->mppath)
452d5edb9aeSFelix Fietkau 		entry->mppath->exp_time = jiffies;
453d5edb9aeSFelix Fietkau 	entry->timestamp = jiffies;
454d5edb9aeSFelix Fietkau 
455d5edb9aeSFelix Fietkau 	return entry;
456d5edb9aeSFelix Fietkau }
457d5edb9aeSFelix Fietkau 
mesh_fast_tx_cache(struct ieee80211_sub_if_data * sdata,struct sk_buff * skb,struct mesh_path * mpath)458d5edb9aeSFelix Fietkau void mesh_fast_tx_cache(struct ieee80211_sub_if_data *sdata,
459d5edb9aeSFelix Fietkau 			struct sk_buff *skb, struct mesh_path *mpath)
460d5edb9aeSFelix Fietkau {
461d5edb9aeSFelix Fietkau 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
462d5edb9aeSFelix Fietkau 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
463d5edb9aeSFelix Fietkau 	struct ieee80211_mesh_fast_tx *entry, *prev;
464d5edb9aeSFelix Fietkau 	struct ieee80211_mesh_fast_tx build = {};
465d5edb9aeSFelix Fietkau 	struct ieee80211s_hdr *meshhdr;
466d5edb9aeSFelix Fietkau 	struct mesh_tx_cache *cache;
467d5edb9aeSFelix Fietkau 	struct ieee80211_key *key;
468d5edb9aeSFelix Fietkau 	struct mesh_path *mppath;
469d5edb9aeSFelix Fietkau 	struct sta_info *sta;
470d5edb9aeSFelix Fietkau 	u8 *qc;
471d5edb9aeSFelix Fietkau 
472d5edb9aeSFelix Fietkau 	if (sdata->noack_map ||
473d5edb9aeSFelix Fietkau 	    !ieee80211_is_data_qos(hdr->frame_control))
474d5edb9aeSFelix Fietkau 		return;
475d5edb9aeSFelix Fietkau 
476d5edb9aeSFelix Fietkau 	build.fast_tx.hdr_len = ieee80211_hdrlen(hdr->frame_control);
477d5edb9aeSFelix Fietkau 	meshhdr = (struct ieee80211s_hdr *)(skb->data + build.fast_tx.hdr_len);
478d5edb9aeSFelix Fietkau 	build.hdrlen = ieee80211_get_mesh_hdrlen(meshhdr);
479d5edb9aeSFelix Fietkau 
480d5edb9aeSFelix Fietkau 	cache = &sdata->u.mesh.tx_cache;
481d5edb9aeSFelix Fietkau 	if (atomic_read(&cache->rht.nelems) >= MESH_FAST_TX_CACHE_MAX_SIZE)
482d5edb9aeSFelix Fietkau 		return;
483d5edb9aeSFelix Fietkau 
484d5edb9aeSFelix Fietkau 	sta = rcu_dereference(mpath->next_hop);
485d5edb9aeSFelix Fietkau 	if (!sta)
486d5edb9aeSFelix Fietkau 		return;
487d5edb9aeSFelix Fietkau 
488154be74eSFelix Fietkau 	build.key.type = MESH_FAST_TX_TYPE_LOCAL;
489d5edb9aeSFelix Fietkau 	if ((meshhdr->flags & MESH_FLAGS_AE) == MESH_FLAGS_AE_A5_A6) {
490d5edb9aeSFelix Fietkau 		/* This is required to keep the mppath alive */
491d5edb9aeSFelix Fietkau 		mppath = mpp_path_lookup(sdata, meshhdr->eaddr1);
492d5edb9aeSFelix Fietkau 		if (!mppath)
493d5edb9aeSFelix Fietkau 			return;
494d5edb9aeSFelix Fietkau 		build.mppath = mppath;
495154be74eSFelix Fietkau 		if (!ether_addr_equal(meshhdr->eaddr2, sdata->vif.addr))
496154be74eSFelix Fietkau 			build.key.type = MESH_FAST_TX_TYPE_PROXIED;
497d5edb9aeSFelix Fietkau 	} else if (ieee80211_has_a4(hdr->frame_control)) {
498d5edb9aeSFelix Fietkau 		mppath = mpath;
499d5edb9aeSFelix Fietkau 	} else {
500d5edb9aeSFelix Fietkau 		return;
501d5edb9aeSFelix Fietkau 	}
502d5edb9aeSFelix Fietkau 
503154be74eSFelix Fietkau 	if (!ether_addr_equal(hdr->addr4, sdata->vif.addr))
504154be74eSFelix Fietkau 		build.key.type = MESH_FAST_TX_TYPE_FORWARDED;
505154be74eSFelix Fietkau 
506d5edb9aeSFelix Fietkau 	/* rate limit, in case fast xmit can't be enabled */
507d5edb9aeSFelix Fietkau 	if (mppath->fast_tx_check == jiffies)
508d5edb9aeSFelix Fietkau 		return;
509d5edb9aeSFelix Fietkau 
510d5edb9aeSFelix Fietkau 	mppath->fast_tx_check = jiffies;
511d5edb9aeSFelix Fietkau 
512d5edb9aeSFelix Fietkau 	/*
513d5edb9aeSFelix Fietkau 	 * Same use of the sta lock as in ieee80211_check_fast_xmit, in order
514d5edb9aeSFelix Fietkau 	 * to protect against concurrent sta key updates.
515d5edb9aeSFelix Fietkau 	 */
516d5edb9aeSFelix Fietkau 	spin_lock_bh(&sta->lock);
517d5edb9aeSFelix Fietkau 	key = rcu_access_pointer(sta->ptk[sta->ptk_idx]);
518d5edb9aeSFelix Fietkau 	if (!key)
519d5edb9aeSFelix Fietkau 		key = rcu_access_pointer(sdata->default_unicast_key);
520d5edb9aeSFelix Fietkau 	build.fast_tx.key = key;
521d5edb9aeSFelix Fietkau 
522d5edb9aeSFelix Fietkau 	if (key) {
523d5edb9aeSFelix Fietkau 		bool gen_iv, iv_spc;
524d5edb9aeSFelix Fietkau 
525d5edb9aeSFelix Fietkau 		gen_iv = key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV;
526d5edb9aeSFelix Fietkau 		iv_spc = key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE;
527d5edb9aeSFelix Fietkau 
528d5edb9aeSFelix Fietkau 		if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) ||
529d5edb9aeSFelix Fietkau 		    (key->flags & KEY_FLAG_TAINTED))
530d5edb9aeSFelix Fietkau 			goto unlock_sta;
531d5edb9aeSFelix Fietkau 
532d5edb9aeSFelix Fietkau 		switch (key->conf.cipher) {
533d5edb9aeSFelix Fietkau 		case WLAN_CIPHER_SUITE_CCMP:
534d5edb9aeSFelix Fietkau 		case WLAN_CIPHER_SUITE_CCMP_256:
535d5edb9aeSFelix Fietkau 			if (gen_iv)
536d5edb9aeSFelix Fietkau 				build.fast_tx.pn_offs = build.fast_tx.hdr_len;
537d5edb9aeSFelix Fietkau 			if (gen_iv || iv_spc)
538d5edb9aeSFelix Fietkau 				build.fast_tx.hdr_len += IEEE80211_CCMP_HDR_LEN;
539d5edb9aeSFelix Fietkau 			break;
540d5edb9aeSFelix Fietkau 		case WLAN_CIPHER_SUITE_GCMP:
541d5edb9aeSFelix Fietkau 		case WLAN_CIPHER_SUITE_GCMP_256:
542d5edb9aeSFelix Fietkau 			if (gen_iv)
543d5edb9aeSFelix Fietkau 				build.fast_tx.pn_offs = build.fast_tx.hdr_len;
544d5edb9aeSFelix Fietkau 			if (gen_iv || iv_spc)
545d5edb9aeSFelix Fietkau 				build.fast_tx.hdr_len += IEEE80211_GCMP_HDR_LEN;
546d5edb9aeSFelix Fietkau 			break;
547d5edb9aeSFelix Fietkau 		default:
548d5edb9aeSFelix Fietkau 			goto unlock_sta;
549d5edb9aeSFelix Fietkau 		}
550d5edb9aeSFelix Fietkau 	}
551d5edb9aeSFelix Fietkau 
552154be74eSFelix Fietkau 	memcpy(build.key.addr, mppath->dst, ETH_ALEN);
553d5edb9aeSFelix Fietkau 	build.timestamp = jiffies;
554d5edb9aeSFelix Fietkau 	build.fast_tx.band = info->band;
555d5edb9aeSFelix Fietkau 	build.fast_tx.da_offs = offsetof(struct ieee80211_hdr, addr3);
556d5edb9aeSFelix Fietkau 	build.fast_tx.sa_offs = offsetof(struct ieee80211_hdr, addr4);
557d5edb9aeSFelix Fietkau 	build.mpath = mpath;
558d5edb9aeSFelix Fietkau 	memcpy(build.hdr, meshhdr, build.hdrlen);
559d5edb9aeSFelix Fietkau 	memcpy(build.hdr + build.hdrlen, rfc1042_header, sizeof(rfc1042_header));
560d5edb9aeSFelix Fietkau 	build.hdrlen += sizeof(rfc1042_header);
561d5edb9aeSFelix Fietkau 	memcpy(build.fast_tx.hdr, hdr, build.fast_tx.hdr_len);
562d5edb9aeSFelix Fietkau 
563d5edb9aeSFelix Fietkau 	hdr = (struct ieee80211_hdr *)build.fast_tx.hdr;
564d5edb9aeSFelix Fietkau 	if (build.fast_tx.key)
565d5edb9aeSFelix Fietkau 		hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
566d5edb9aeSFelix Fietkau 
567d5edb9aeSFelix Fietkau 	qc = ieee80211_get_qos_ctl(hdr);
568d5edb9aeSFelix Fietkau 	qc[1] |= IEEE80211_QOS_CTL_MESH_CONTROL_PRESENT >> 8;
569d5edb9aeSFelix Fietkau 
570d5edb9aeSFelix Fietkau 	entry = kmemdup(&build, sizeof(build), GFP_ATOMIC);
571d5edb9aeSFelix Fietkau 	if (!entry)
572d5edb9aeSFelix Fietkau 		goto unlock_sta;
573d5edb9aeSFelix Fietkau 
574d5edb9aeSFelix Fietkau 	spin_lock(&cache->walk_lock);
575d5edb9aeSFelix Fietkau 	prev = rhashtable_lookup_get_insert_fast(&cache->rht,
576d5edb9aeSFelix Fietkau 						 &entry->rhash,
577d5edb9aeSFelix Fietkau 						 fast_tx_rht_params);
578d5edb9aeSFelix Fietkau 	if (unlikely(IS_ERR(prev))) {
579d5edb9aeSFelix Fietkau 		kfree(entry);
580d5edb9aeSFelix Fietkau 		goto unlock_cache;
581d5edb9aeSFelix Fietkau 	}
582d5edb9aeSFelix Fietkau 
583d5edb9aeSFelix Fietkau 	/*
584d5edb9aeSFelix Fietkau 	 * replace any previous entry in the hash table, in case we're
585d5edb9aeSFelix Fietkau 	 * replacing it with a different type (e.g. mpath -> mpp)
586d5edb9aeSFelix Fietkau 	 */
587d5edb9aeSFelix Fietkau 	if (unlikely(prev)) {
588d5edb9aeSFelix Fietkau 		rhashtable_replace_fast(&cache->rht, &prev->rhash,
589d5edb9aeSFelix Fietkau 					&entry->rhash, fast_tx_rht_params);
590d5edb9aeSFelix Fietkau 		hlist_del_rcu(&prev->walk_list);
591d5edb9aeSFelix Fietkau 		kfree_rcu(prev, fast_tx.rcu_head);
592d5edb9aeSFelix Fietkau 	}
593d5edb9aeSFelix Fietkau 
594d5edb9aeSFelix Fietkau 	hlist_add_head(&entry->walk_list, &cache->walk_head);
595d5edb9aeSFelix Fietkau 
596d5edb9aeSFelix Fietkau unlock_cache:
597d5edb9aeSFelix Fietkau 	spin_unlock(&cache->walk_lock);
598d5edb9aeSFelix Fietkau unlock_sta:
599d5edb9aeSFelix Fietkau 	spin_unlock_bh(&sta->lock);
600d5edb9aeSFelix Fietkau }
601d5edb9aeSFelix Fietkau 
mesh_fast_tx_gc(struct ieee80211_sub_if_data * sdata)602d5edb9aeSFelix Fietkau void mesh_fast_tx_gc(struct ieee80211_sub_if_data *sdata)
603d5edb9aeSFelix Fietkau {
604d5edb9aeSFelix Fietkau 	unsigned long timeout = msecs_to_jiffies(MESH_FAST_TX_CACHE_TIMEOUT);
6051ba4d2adSColin Ian King 	struct mesh_tx_cache *cache = &sdata->u.mesh.tx_cache;
606d5edb9aeSFelix Fietkau 	struct ieee80211_mesh_fast_tx *entry;
607d5edb9aeSFelix Fietkau 	struct hlist_node *n;
608d5edb9aeSFelix Fietkau 
609d5edb9aeSFelix Fietkau 	if (atomic_read(&cache->rht.nelems) < MESH_FAST_TX_CACHE_THRESHOLD_SIZE)
610d5edb9aeSFelix Fietkau 		return;
611d5edb9aeSFelix Fietkau 
612d5edb9aeSFelix Fietkau 	spin_lock_bh(&cache->walk_lock);
613d5edb9aeSFelix Fietkau 	hlist_for_each_entry_safe(entry, n, &cache->walk_head, walk_list)
614d5edb9aeSFelix Fietkau 		if (!time_is_after_jiffies(entry->timestamp + timeout))
615d5edb9aeSFelix Fietkau 			mesh_fast_tx_entry_free(cache, entry);
616d5edb9aeSFelix Fietkau 	spin_unlock_bh(&cache->walk_lock);
617d5edb9aeSFelix Fietkau }
618d5edb9aeSFelix Fietkau 
mesh_fast_tx_flush_mpath(struct mesh_path * mpath)619d5edb9aeSFelix Fietkau void mesh_fast_tx_flush_mpath(struct mesh_path *mpath)
620d5edb9aeSFelix Fietkau {
621d5edb9aeSFelix Fietkau 	struct ieee80211_sub_if_data *sdata = mpath->sdata;
622d5edb9aeSFelix Fietkau 	struct mesh_tx_cache *cache = &sdata->u.mesh.tx_cache;
623d5edb9aeSFelix Fietkau 	struct ieee80211_mesh_fast_tx *entry;
624d5edb9aeSFelix Fietkau 	struct hlist_node *n;
625d5edb9aeSFelix Fietkau 
626d5edb9aeSFelix Fietkau 	spin_lock_bh(&cache->walk_lock);
627d5edb9aeSFelix Fietkau 	hlist_for_each_entry_safe(entry, n, &cache->walk_head, walk_list)
628d5edb9aeSFelix Fietkau 		if (entry->mpath == mpath)
629d5edb9aeSFelix Fietkau 			mesh_fast_tx_entry_free(cache, entry);
630d5edb9aeSFelix Fietkau 	spin_unlock_bh(&cache->walk_lock);
631d5edb9aeSFelix Fietkau }
632d5edb9aeSFelix Fietkau 
mesh_fast_tx_flush_sta(struct ieee80211_sub_if_data * sdata,struct sta_info * sta)633d5edb9aeSFelix Fietkau void mesh_fast_tx_flush_sta(struct ieee80211_sub_if_data *sdata,
634d5edb9aeSFelix Fietkau 			    struct sta_info *sta)
635d5edb9aeSFelix Fietkau {
636d5edb9aeSFelix Fietkau 	struct mesh_tx_cache *cache = &sdata->u.mesh.tx_cache;
637d5edb9aeSFelix Fietkau 	struct ieee80211_mesh_fast_tx *entry;
638d5edb9aeSFelix Fietkau 	struct hlist_node *n;
639d5edb9aeSFelix Fietkau 
640d5edb9aeSFelix Fietkau 	spin_lock_bh(&cache->walk_lock);
641d5edb9aeSFelix Fietkau 	hlist_for_each_entry_safe(entry, n, &cache->walk_head, walk_list)
642d5edb9aeSFelix Fietkau 		if (rcu_access_pointer(entry->mpath->next_hop) == sta)
643d5edb9aeSFelix Fietkau 			mesh_fast_tx_entry_free(cache, entry);
644d5edb9aeSFelix Fietkau 	spin_unlock_bh(&cache->walk_lock);
645d5edb9aeSFelix Fietkau }
646d5edb9aeSFelix Fietkau 
mesh_fast_tx_flush_addr(struct ieee80211_sub_if_data * sdata,const u8 * addr)647d5edb9aeSFelix Fietkau void mesh_fast_tx_flush_addr(struct ieee80211_sub_if_data *sdata,
648d5edb9aeSFelix Fietkau 			     const u8 *addr)
649d5edb9aeSFelix Fietkau {
650d5edb9aeSFelix Fietkau 	struct mesh_tx_cache *cache = &sdata->u.mesh.tx_cache;
651154be74eSFelix Fietkau 	struct ieee80211_mesh_fast_tx_key key = {};
652d5edb9aeSFelix Fietkau 	struct ieee80211_mesh_fast_tx *entry;
653154be74eSFelix Fietkau 	int i;
654d5edb9aeSFelix Fietkau 
655154be74eSFelix Fietkau 	ether_addr_copy(key.addr, addr);
656d5edb9aeSFelix Fietkau 	spin_lock_bh(&cache->walk_lock);
657154be74eSFelix Fietkau 	for (i = 0; i < NUM_MESH_FAST_TX_TYPE; i++) {
658154be74eSFelix Fietkau 		key.type = i;
659154be74eSFelix Fietkau 		entry = rhashtable_lookup_fast(&cache->rht, &key, fast_tx_rht_params);
660d5edb9aeSFelix Fietkau 		if (entry)
661d5edb9aeSFelix Fietkau 			mesh_fast_tx_entry_free(cache, entry);
662154be74eSFelix Fietkau 	}
663d5edb9aeSFelix Fietkau 	spin_unlock_bh(&cache->walk_lock);
664d5edb9aeSFelix Fietkau }
665d5edb9aeSFelix Fietkau 
666eb2b9311SLuis Carlos Cobo /**
667eb2b9311SLuis Carlos Cobo  * mesh_path_add - allocate and add a new path to the mesh path table
668bf7cd94dSJohannes Berg  * @dst: destination address of the path (ETH_ALEN length)
669f698d856SJasper Bryant-Greene  * @sdata: local subif
670eb2b9311SLuis Carlos Cobo  *
671af901ca1SAndré Goddard Rosa  * Returns: 0 on success
672eb2b9311SLuis Carlos Cobo  *
673eb2b9311SLuis Carlos Cobo  * State: the initial state of the new path is set to 0
674eb2b9311SLuis Carlos Cobo  */
mesh_path_add(struct ieee80211_sub_if_data * sdata,const u8 * dst)675ae76eef0SBob Copeland struct mesh_path *mesh_path_add(struct ieee80211_sub_if_data *sdata,
676ae76eef0SBob Copeland 				const u8 *dst)
677eb2b9311SLuis Carlos Cobo {
678349eb8cfSJohannes Berg 	struct mesh_table *tbl;
679eb2b9311SLuis Carlos Cobo 	struct mesh_path *mpath, *new_mpath;
680eb2b9311SLuis Carlos Cobo 
681b203ca39SJoe Perches 	if (ether_addr_equal(dst, sdata->vif.addr))
682eb2b9311SLuis Carlos Cobo 		/* never add ourselves as neighbours */
683ae76eef0SBob Copeland 		return ERR_PTR(-ENOTSUPP);
684eb2b9311SLuis Carlos Cobo 
685eb2b9311SLuis Carlos Cobo 	if (is_multicast_ether_addr(dst))
686ae76eef0SBob Copeland 		return ERR_PTR(-ENOTSUPP);
687eb2b9311SLuis Carlos Cobo 
688472dbc45SJohannes Berg 	if (atomic_add_unless(&sdata->u.mesh.mpaths, 1, MESH_MAX_MPATHS) == 0)
689ae76eef0SBob Copeland 		return ERR_PTR(-ENOSPC);
690ae76eef0SBob Copeland 
691b15dc38bSBob Copeland 	new_mpath = mesh_path_new(sdata, dst, GFP_ATOMIC);
692402d7752SPavel Emelyanov 	if (!new_mpath)
69360854fd9SBob Copeland 		return ERR_PTR(-ENOMEM);
694402d7752SPavel Emelyanov 
6958b5cb7e4SPavel Skripkin 	tbl = &sdata->u.mesh.mesh_paths;
696b4c3fbe6SHerbert Xu 	spin_lock_bh(&tbl->walk_lock);
69736922931SHerbert Xu 	mpath = rhashtable_lookup_get_insert_fast(&tbl->rhead,
69860854fd9SBob Copeland 						  &new_mpath->rhash,
69960854fd9SBob Copeland 						  mesh_rht_params);
70036922931SHerbert Xu 	if (!mpath)
701b4c3fbe6SHerbert Xu 		hlist_add_head(&new_mpath->walk_list, &tbl->walk_head);
702b4c3fbe6SHerbert Xu 	spin_unlock_bh(&tbl->walk_lock);
703f5ea9120SJohannes Berg 
70436922931SHerbert Xu 	if (mpath) {
7054ff3a9d1SHerbert Xu 		kfree(new_mpath);
7064ff3a9d1SHerbert Xu 
70736922931SHerbert Xu 		if (IS_ERR(mpath))
70836922931SHerbert Xu 			return mpath;
709ae76eef0SBob Copeland 
71060854fd9SBob Copeland 		new_mpath = mpath;
711eb2b9311SLuis Carlos Cobo 	}
7124ff3a9d1SHerbert Xu 
71360854fd9SBob Copeland 	sdata->u.mesh.mesh_paths_generation++;
71460854fd9SBob Copeland 	return new_mpath;
71518889231SJavier Cardona }
716eb2b9311SLuis Carlos Cobo 
mpp_path_add(struct ieee80211_sub_if_data * sdata,const u8 * dst,const u8 * mpp)717bf7cd94dSJohannes Berg int mpp_path_add(struct ieee80211_sub_if_data *sdata,
718bf7cd94dSJohannes Berg 		 const u8 *dst, const u8 *mpp)
71979617deeSYanBo {
720349eb8cfSJohannes Berg 	struct mesh_table *tbl;
72160854fd9SBob Copeland 	struct mesh_path *new_mpath;
72260854fd9SBob Copeland 	int ret;
72379617deeSYanBo 
724b203ca39SJoe Perches 	if (ether_addr_equal(dst, sdata->vif.addr))
72579617deeSYanBo 		/* never add ourselves as neighbours */
72679617deeSYanBo 		return -ENOTSUPP;
72779617deeSYanBo 
72879617deeSYanBo 	if (is_multicast_ether_addr(dst))
72979617deeSYanBo 		return -ENOTSUPP;
73079617deeSYanBo 
731b15dc38bSBob Copeland 	new_mpath = mesh_path_new(sdata, dst, GFP_ATOMIC);
73279617deeSYanBo 
73360854fd9SBob Copeland 	if (!new_mpath)
73460854fd9SBob Copeland 		return -ENOMEM;
73579617deeSYanBo 
73679617deeSYanBo 	memcpy(new_mpath->mpp, mpp, ETH_ALEN);
7378b5cb7e4SPavel Skripkin 	tbl = &sdata->u.mesh.mpp_paths;
738b4c3fbe6SHerbert Xu 
739b4c3fbe6SHerbert Xu 	spin_lock_bh(&tbl->walk_lock);
74060854fd9SBob Copeland 	ret = rhashtable_lookup_insert_fast(&tbl->rhead,
74160854fd9SBob Copeland 					    &new_mpath->rhash,
74260854fd9SBob Copeland 					    mesh_rht_params);
743b4c3fbe6SHerbert Xu 	if (!ret)
744b4c3fbe6SHerbert Xu 		hlist_add_head_rcu(&new_mpath->walk_list, &tbl->walk_head);
745b4c3fbe6SHerbert Xu 	spin_unlock_bh(&tbl->walk_lock);
746a2db2ed3SHenning Rogge 
7474ff3a9d1SHerbert Xu 	if (ret)
7484ff3a9d1SHerbert Xu 		kfree(new_mpath);
749d5edb9aeSFelix Fietkau 	else
750d5edb9aeSFelix Fietkau 		mesh_fast_tx_flush_addr(sdata, dst);
7514ff3a9d1SHerbert Xu 
7522bdaf386SBob Copeland 	sdata->u.mesh.mpp_paths_generation++;
75360854fd9SBob Copeland 	return ret;
75479617deeSYanBo }
75579617deeSYanBo 
75679617deeSYanBo 
757eb2b9311SLuis Carlos Cobo /**
758eb2b9311SLuis Carlos Cobo  * mesh_plink_broken - deactivates paths and sends perr when a link breaks
759eb2b9311SLuis Carlos Cobo  *
760eb2b9311SLuis Carlos Cobo  * @sta: broken peer link
761eb2b9311SLuis Carlos Cobo  *
762eb2b9311SLuis Carlos Cobo  * This function must be called from the rate control algorithm if enough
763eb2b9311SLuis Carlos Cobo  * delivery errors suggest that a peer link is no longer usable.
764eb2b9311SLuis Carlos Cobo  */
mesh_plink_broken(struct sta_info * sta)765eb2b9311SLuis Carlos Cobo void mesh_plink_broken(struct sta_info *sta)
766eb2b9311SLuis Carlos Cobo {
76760854fd9SBob Copeland 	struct ieee80211_sub_if_data *sdata = sta->sdata;
7688b5cb7e4SPavel Skripkin 	struct mesh_table *tbl = &sdata->u.mesh.mesh_paths;
76915ff6365SJohannes Berg 	static const u8 bcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
770eb2b9311SLuis Carlos Cobo 	struct mesh_path *mpath;
771eb2b9311SLuis Carlos Cobo 
772b4c3fbe6SHerbert Xu 	rcu_read_lock();
773b4c3fbe6SHerbert Xu 	hlist_for_each_entry_rcu(mpath, &tbl->walk_head, walk_list) {
7742688eba9SAndreea-Cristina Bernat 		if (rcu_access_pointer(mpath->next_hop) == sta &&
775eb2b9311SLuis Carlos Cobo 		    mpath->flags & MESH_PATH_ACTIVE &&
776eb2b9311SLuis Carlos Cobo 		    !(mpath->flags & MESH_PATH_FIXED)) {
777f5e50cd0SJavier Cardona 			spin_lock_bh(&mpath->state_lock);
778eb2b9311SLuis Carlos Cobo 			mpath->flags &= ~MESH_PATH_ACTIVE;
779d19b3bf6SRui Paulo 			++mpath->sn;
780eb2b9311SLuis Carlos Cobo 			spin_unlock_bh(&mpath->state_lock);
781bf7cd94dSJohannes Berg 			mesh_path_error_tx(sdata,
782bf7cd94dSJohannes Berg 				sdata->u.mesh.mshcfg.element_ttl,
783f63f8421SChun-Yeow Yeoh 				mpath->dst, mpath->sn,
784f63f8421SChun-Yeow Yeoh 				WLAN_REASON_MESH_PATH_DEST_UNREACHABLE, bcast);
785f5e50cd0SJavier Cardona 		}
786eb2b9311SLuis Carlos Cobo 	}
787b4c3fbe6SHerbert Xu 	rcu_read_unlock();
788eb2b9311SLuis Carlos Cobo }
789eb2b9311SLuis Carlos Cobo 
mesh_path_free_rcu(struct mesh_table * tbl,struct mesh_path * mpath)79074932959SBob Copeland static void mesh_path_free_rcu(struct mesh_table *tbl,
79174932959SBob Copeland 			       struct mesh_path *mpath)
79219c50b3dSJavier Cardona {
79360854fd9SBob Copeland 	struct ieee80211_sub_if_data *sdata = mpath->sdata;
79419c50b3dSJavier Cardona 
795947c2a0eSBob Copeland 	spin_lock_bh(&mpath->state_lock);
79674932959SBob Copeland 	mpath->flags |= MESH_PATH_RESOLVING | MESH_PATH_DELETED;
79719c50b3dSJavier Cardona 	mesh_gate_del(tbl, mpath);
798947c2a0eSBob Copeland 	spin_unlock_bh(&mpath->state_lock);
799292a089dSSteven Rostedt (Google) 	timer_shutdown_sync(&mpath->timer);
800c2e703a5SJohannes Berg 	atomic_dec(&sdata->u.mesh.mpaths);
80119c50b3dSJavier Cardona 	atomic_dec(&tbl->entries);
8025e43540cSRemi Pommarel 	mesh_path_flush_pending(mpath);
80374932959SBob Copeland 	kfree_rcu(mpath, rcu);
80474932959SBob Copeland }
80574932959SBob Copeland 
__mesh_path_del(struct mesh_table * tbl,struct mesh_path * mpath)80674932959SBob Copeland static void __mesh_path_del(struct mesh_table *tbl, struct mesh_path *mpath)
80774932959SBob Copeland {
808b4c3fbe6SHerbert Xu 	hlist_del_rcu(&mpath->walk_list);
80974932959SBob Copeland 	rhashtable_remove_fast(&tbl->rhead, &mpath->rhash, mesh_rht_params);
810d5edb9aeSFelix Fietkau 	if (tbl == &mpath->sdata->u.mesh.mpp_paths)
811d5edb9aeSFelix Fietkau 		mesh_fast_tx_flush_addr(mpath->sdata, mpath->dst);
812d5edb9aeSFelix Fietkau 	else
813d5edb9aeSFelix Fietkau 		mesh_fast_tx_flush_mpath(mpath);
81474932959SBob Copeland 	mesh_path_free_rcu(tbl, mpath);
81519c50b3dSJavier Cardona }
81619c50b3dSJavier Cardona 
817eb2b9311SLuis Carlos Cobo /**
818eb2b9311SLuis Carlos Cobo  * mesh_path_flush_by_nexthop - Deletes mesh paths if their next hop matches
819eb2b9311SLuis Carlos Cobo  *
8202c53040fSBen Hutchings  * @sta: mesh peer to match
821eb2b9311SLuis Carlos Cobo  *
822b4e08ea1SLuis Carlos Cobo  * RCU notes: this function is called when a mesh plink transitions from
823b4e08ea1SLuis Carlos Cobo  * PLINK_ESTAB to any other state, since PLINK_ESTAB state is the only one that
824b4e08ea1SLuis Carlos Cobo  * allows path creation. This will happen before the sta can be freed (because
825d0709a65SJohannes Berg  * sta_info_destroy() calls this) so any reader in a rcu read block will be
826d0709a65SJohannes Berg  * protected against the plink disappearing.
827eb2b9311SLuis Carlos Cobo  */
mesh_path_flush_by_nexthop(struct sta_info * sta)828eb2b9311SLuis Carlos Cobo void mesh_path_flush_by_nexthop(struct sta_info *sta)
829eb2b9311SLuis Carlos Cobo {
8302bdaf386SBob Copeland 	struct ieee80211_sub_if_data *sdata = sta->sdata;
8318b5cb7e4SPavel Skripkin 	struct mesh_table *tbl = &sdata->u.mesh.mesh_paths;
832eb2b9311SLuis Carlos Cobo 	struct mesh_path *mpath;
833b4c3fbe6SHerbert Xu 	struct hlist_node *n;
834eb2b9311SLuis Carlos Cobo 
835b4c3fbe6SHerbert Xu 	spin_lock_bh(&tbl->walk_lock);
836b4c3fbe6SHerbert Xu 	hlist_for_each_entry_safe(mpath, n, &tbl->walk_head, walk_list) {
83760854fd9SBob Copeland 		if (rcu_access_pointer(mpath->next_hop) == sta)
83860854fd9SBob Copeland 			__mesh_path_del(tbl, mpath);
839eb2b9311SLuis Carlos Cobo 	}
840b4c3fbe6SHerbert Xu 	spin_unlock_bh(&tbl->walk_lock);
841eb2b9311SLuis Carlos Cobo }
842eb2b9311SLuis Carlos Cobo 
mpp_flush_by_proxy(struct ieee80211_sub_if_data * sdata,const u8 * proxy)843bf5a70e1SHenning Rogge static void mpp_flush_by_proxy(struct ieee80211_sub_if_data *sdata,
844bf5a70e1SHenning Rogge 			       const u8 *proxy)
845bf5a70e1SHenning Rogge {
8468b5cb7e4SPavel Skripkin 	struct mesh_table *tbl = &sdata->u.mesh.mpp_paths;
84760854fd9SBob Copeland 	struct mesh_path *mpath;
848b4c3fbe6SHerbert Xu 	struct hlist_node *n;
849bf5a70e1SHenning Rogge 
850b4c3fbe6SHerbert Xu 	spin_lock_bh(&tbl->walk_lock);
851b4c3fbe6SHerbert Xu 	hlist_for_each_entry_safe(mpath, n, &tbl->walk_head, walk_list) {
85260854fd9SBob Copeland 		if (ether_addr_equal(mpath->mpp, proxy))
85360854fd9SBob Copeland 			__mesh_path_del(tbl, mpath);
854bf5a70e1SHenning Rogge 	}
855b4c3fbe6SHerbert Xu 	spin_unlock_bh(&tbl->walk_lock);
856bf5a70e1SHenning Rogge }
857bf5a70e1SHenning Rogge 
table_flush_by_iface(struct mesh_table * tbl)85860854fd9SBob Copeland static void table_flush_by_iface(struct mesh_table *tbl)
859eb2b9311SLuis Carlos Cobo {
860eb2b9311SLuis Carlos Cobo 	struct mesh_path *mpath;
861b4c3fbe6SHerbert Xu 	struct hlist_node *n;
862eb2b9311SLuis Carlos Cobo 
863b4c3fbe6SHerbert Xu 	spin_lock_bh(&tbl->walk_lock);
864b4c3fbe6SHerbert Xu 	hlist_for_each_entry_safe(mpath, n, &tbl->walk_head, walk_list) {
86560854fd9SBob Copeland 		__mesh_path_del(tbl, mpath);
866eb2b9311SLuis Carlos Cobo 	}
867b4c3fbe6SHerbert Xu 	spin_unlock_bh(&tbl->walk_lock);
868eb2b9311SLuis Carlos Cobo }
869eb2b9311SLuis Carlos Cobo 
870ece1a2e7SJavier Cardona /**
871ece1a2e7SJavier Cardona  * mesh_path_flush_by_iface - Deletes all mesh paths associated with a given iface
872ece1a2e7SJavier Cardona  *
873ece1a2e7SJavier Cardona  * This function deletes both mesh paths as well as mesh portal paths.
874ece1a2e7SJavier Cardona  *
8752c53040fSBen Hutchings  * @sdata: interface data to match
876ece1a2e7SJavier Cardona  *
877ece1a2e7SJavier Cardona  */
mesh_path_flush_by_iface(struct ieee80211_sub_if_data * sdata)878ece1a2e7SJavier Cardona void mesh_path_flush_by_iface(struct ieee80211_sub_if_data *sdata)
879eb2b9311SLuis Carlos Cobo {
8808b5cb7e4SPavel Skripkin 	table_flush_by_iface(&sdata->u.mesh.mesh_paths);
8818b5cb7e4SPavel Skripkin 	table_flush_by_iface(&sdata->u.mesh.mpp_paths);
882eb2b9311SLuis Carlos Cobo }
883eb2b9311SLuis Carlos Cobo 
884eb2b9311SLuis Carlos Cobo /**
8854cc955deSHenning Rogge  * table_path_del - delete a path from the mesh or mpp table
886eb2b9311SLuis Carlos Cobo  *
8874cc955deSHenning Rogge  * @tbl: mesh or mpp path table
888f698d856SJasper Bryant-Greene  * @sdata: local subif
8894cc955deSHenning Rogge  * @addr: dst address (ETH_ALEN length)
890eb2b9311SLuis Carlos Cobo  *
891af901ca1SAndré Goddard Rosa  * Returns: 0 if successful
892eb2b9311SLuis Carlos Cobo  */
table_path_del(struct mesh_table * tbl,struct ieee80211_sub_if_data * sdata,const u8 * addr)89360854fd9SBob Copeland static int table_path_del(struct mesh_table *tbl,
8944cc955deSHenning Rogge 			  struct ieee80211_sub_if_data *sdata,
8954cc955deSHenning Rogge 			  const u8 *addr)
896eb2b9311SLuis Carlos Cobo {
897eb2b9311SLuis Carlos Cobo 	struct mesh_path *mpath;
898eb2b9311SLuis Carlos Cobo 
899b4c3fbe6SHerbert Xu 	spin_lock_bh(&tbl->walk_lock);
90060854fd9SBob Copeland 	mpath = rhashtable_lookup_fast(&tbl->rhead, addr, mesh_rht_params);
90160854fd9SBob Copeland 	if (!mpath) {
902f2ffff08SWei Yongjun 		spin_unlock_bh(&tbl->walk_lock);
90360854fd9SBob Copeland 		return -ENXIO;
904eb2b9311SLuis Carlos Cobo 	}
905eb2b9311SLuis Carlos Cobo 
90660854fd9SBob Copeland 	__mesh_path_del(tbl, mpath);
907b4c3fbe6SHerbert Xu 	spin_unlock_bh(&tbl->walk_lock);
90860854fd9SBob Copeland 	return 0;
9094cc955deSHenning Rogge }
9104cc955deSHenning Rogge 
91160854fd9SBob Copeland 
9124cc955deSHenning Rogge /**
9134cc955deSHenning Rogge  * mesh_path_del - delete a mesh path from the table
9144cc955deSHenning Rogge  *
9154cc955deSHenning Rogge  * @addr: dst address (ETH_ALEN length)
9164cc955deSHenning Rogge  * @sdata: local subif
9174cc955deSHenning Rogge  *
9184cc955deSHenning Rogge  * Returns: 0 if successful
9194cc955deSHenning Rogge  */
mesh_path_del(struct ieee80211_sub_if_data * sdata,const u8 * addr)9204cc955deSHenning Rogge int mesh_path_del(struct ieee80211_sub_if_data *sdata, const u8 *addr)
9214cc955deSHenning Rogge {
92260854fd9SBob Copeland 	int err;
9234cc955deSHenning Rogge 
9244cc955deSHenning Rogge 	/* flush relevant mpp entries first */
9254cc955deSHenning Rogge 	mpp_flush_by_proxy(sdata, addr);
9264cc955deSHenning Rogge 
9278b5cb7e4SPavel Skripkin 	err = table_path_del(&sdata->u.mesh.mesh_paths, sdata, addr);
9282bdaf386SBob Copeland 	sdata->u.mesh.mesh_paths_generation++;
929ab1c7906SHenning Rogge 	return err;
930ab1c7906SHenning Rogge }
931ab1c7906SHenning Rogge 
932ab1c7906SHenning Rogge /**
933eb2b9311SLuis Carlos Cobo  * mesh_path_tx_pending - sends pending frames in a mesh path queue
934eb2b9311SLuis Carlos Cobo  *
935eb2b9311SLuis Carlos Cobo  * @mpath: mesh path to activate
936eb2b9311SLuis Carlos Cobo  *
937eb2b9311SLuis Carlos Cobo  * Locking: the state_lock of the mpath structure must NOT be held when calling
938eb2b9311SLuis Carlos Cobo  * this function.
939eb2b9311SLuis Carlos Cobo  */
mesh_path_tx_pending(struct mesh_path * mpath)940eb2b9311SLuis Carlos Cobo void mesh_path_tx_pending(struct mesh_path *mpath)
941eb2b9311SLuis Carlos Cobo {
942249b405cSJavier Cardona 	if (mpath->flags & MESH_PATH_ACTIVE)
943249b405cSJavier Cardona 		ieee80211_add_pending_skbs(mpath->sdata->local,
944249b405cSJavier Cardona 				&mpath->frame_queue);
945eb2b9311SLuis Carlos Cobo }
946eb2b9311SLuis Carlos Cobo 
947eb2b9311SLuis Carlos Cobo /**
9485ee68e5bSJavier Cardona  * mesh_path_send_to_gates - sends pending frames to all known mesh gates
9495ee68e5bSJavier Cardona  *
9505ee68e5bSJavier Cardona  * @mpath: mesh path whose queue will be emptied
9515ee68e5bSJavier Cardona  *
9525ee68e5bSJavier Cardona  * If there is only one gate, the frames are transferred from the failed mpath
9535ee68e5bSJavier Cardona  * queue to that gate's queue.  If there are more than one gates, the frames
9545ee68e5bSJavier Cardona  * are copied from each gate to the next.  After frames are copied, the
9555ee68e5bSJavier Cardona  * mpath queues are emptied onto the transmission queue.
9565ee68e5bSJavier Cardona  */
mesh_path_send_to_gates(struct mesh_path * mpath)9575ee68e5bSJavier Cardona int mesh_path_send_to_gates(struct mesh_path *mpath)
9585ee68e5bSJavier Cardona {
9595ee68e5bSJavier Cardona 	struct ieee80211_sub_if_data *sdata = mpath->sdata;
9605ee68e5bSJavier Cardona 	struct mesh_table *tbl;
9615ee68e5bSJavier Cardona 	struct mesh_path *from_mpath = mpath;
96260854fd9SBob Copeland 	struct mesh_path *gate;
9635ee68e5bSJavier Cardona 	bool copy = false;
9645ee68e5bSJavier Cardona 
9658b5cb7e4SPavel Skripkin 	tbl = &sdata->u.mesh.mesh_paths;
9665ee68e5bSJavier Cardona 
96760854fd9SBob Copeland 	rcu_read_lock();
96818b27ff7SBob Copeland 	hlist_for_each_entry_rcu(gate, &tbl->known_gates, gate_list) {
969947c2a0eSBob Copeland 		if (gate->flags & MESH_PATH_ACTIVE) {
970947c2a0eSBob Copeland 			mpath_dbg(sdata, "Forwarding to %pM\n", gate->dst);
971947c2a0eSBob Copeland 			mesh_path_move_to_queue(gate, from_mpath, copy);
972947c2a0eSBob Copeland 			from_mpath = gate;
9735ee68e5bSJavier Cardona 			copy = true;
9745ee68e5bSJavier Cardona 		} else {
975bdcbd8e0SJohannes Berg 			mpath_dbg(sdata,
976d671b2a0SJohannes Berg 				  "Not forwarding to %pM (flags %#x)\n",
977947c2a0eSBob Copeland 				  gate->dst, gate->flags);
9785ee68e5bSJavier Cardona 		}
9795ee68e5bSJavier Cardona 	}
9805ee68e5bSJavier Cardona 
98118b27ff7SBob Copeland 	hlist_for_each_entry_rcu(gate, &tbl->known_gates, gate_list) {
982947c2a0eSBob Copeland 		mpath_dbg(sdata, "Sending to %pM\n", gate->dst);
983947c2a0eSBob Copeland 		mesh_path_tx_pending(gate);
9845ee68e5bSJavier Cardona 	}
98560854fd9SBob Copeland 	rcu_read_unlock();
9865ee68e5bSJavier Cardona 
9875ee68e5bSJavier Cardona 	return (from_mpath == mpath) ? -EHOSTUNREACH : 0;
9885ee68e5bSJavier Cardona }
9895ee68e5bSJavier Cardona 
9905ee68e5bSJavier Cardona /**
991eb2b9311SLuis Carlos Cobo  * mesh_path_discard_frame - discard a frame whose path could not be resolved
992eb2b9311SLuis Carlos Cobo  *
993eb2b9311SLuis Carlos Cobo  * @skb: frame to discard
994f698d856SJasper Bryant-Greene  * @sdata: network subif the frame was to be sent through
995eb2b9311SLuis Carlos Cobo  *
996eb2b9311SLuis Carlos Cobo  * Locking: the function must me called within a rcu_read_lock region
997eb2b9311SLuis Carlos Cobo  */
mesh_path_discard_frame(struct ieee80211_sub_if_data * sdata,struct sk_buff * skb)998bf7cd94dSJohannes Berg void mesh_path_discard_frame(struct ieee80211_sub_if_data *sdata,
999bf7cd94dSJohannes Berg 			     struct sk_buff *skb)
1000eb2b9311SLuis Carlos Cobo {
100139e7b5deSNicolas Cavallari 	ieee80211_free_txskb(&sdata->local->hw, skb);
1002472dbc45SJohannes Berg 	sdata->u.mesh.mshstats.dropped_frames_no_route++;
1003eb2b9311SLuis Carlos Cobo }
1004eb2b9311SLuis Carlos Cobo 
1005eb2b9311SLuis Carlos Cobo /**
1006eb2b9311SLuis Carlos Cobo  * mesh_path_flush_pending - free the pending queue of a mesh path
1007eb2b9311SLuis Carlos Cobo  *
1008eb2b9311SLuis Carlos Cobo  * @mpath: mesh path whose queue has to be freed
1009eb2b9311SLuis Carlos Cobo  *
101025985edcSLucas De Marchi  * Locking: the function must me called within a rcu_read_lock region
1011eb2b9311SLuis Carlos Cobo  */
mesh_path_flush_pending(struct mesh_path * mpath)1012eb2b9311SLuis Carlos Cobo void mesh_path_flush_pending(struct mesh_path *mpath)
1013eb2b9311SLuis Carlos Cobo {
101463d5f89bSNicolas Escande 	struct ieee80211_sub_if_data *sdata = mpath->sdata;
101563d5f89bSNicolas Escande 	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
101663d5f89bSNicolas Escande 	struct mesh_preq_queue *preq, *tmp;
1017eb2b9311SLuis Carlos Cobo 	struct sk_buff *skb;
1018eb2b9311SLuis Carlos Cobo 
101900e3f25cSJavier Cardona 	while ((skb = skb_dequeue(&mpath->frame_queue)) != NULL)
1020bf7cd94dSJohannes Berg 		mesh_path_discard_frame(mpath->sdata, skb);
102163d5f89bSNicolas Escande 
102263d5f89bSNicolas Escande 	spin_lock_bh(&ifmsh->mesh_preq_queue_lock);
102363d5f89bSNicolas Escande 	list_for_each_entry_safe(preq, tmp, &ifmsh->preq_queue.list, list) {
102463d5f89bSNicolas Escande 		if (ether_addr_equal(mpath->dst, preq->dst)) {
102563d5f89bSNicolas Escande 			list_del(&preq->list);
102663d5f89bSNicolas Escande 			kfree(preq);
102763d5f89bSNicolas Escande 			--ifmsh->preq_queue_len;
102863d5f89bSNicolas Escande 		}
102963d5f89bSNicolas Escande 	}
103063d5f89bSNicolas Escande 	spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
1031eb2b9311SLuis Carlos Cobo }
1032eb2b9311SLuis Carlos Cobo 
1033eb2b9311SLuis Carlos Cobo /**
1034eb2b9311SLuis Carlos Cobo  * mesh_path_fix_nexthop - force a specific next hop for a mesh path
1035eb2b9311SLuis Carlos Cobo  *
1036eb2b9311SLuis Carlos Cobo  * @mpath: the mesh path to modify
1037eb2b9311SLuis Carlos Cobo  * @next_hop: the next hop to force
1038eb2b9311SLuis Carlos Cobo  *
1039eb2b9311SLuis Carlos Cobo  * Locking: this function must be called holding mpath->state_lock
1040eb2b9311SLuis Carlos Cobo  */
mesh_path_fix_nexthop(struct mesh_path * mpath,struct sta_info * next_hop)1041eb2b9311SLuis Carlos Cobo void mesh_path_fix_nexthop(struct mesh_path *mpath, struct sta_info *next_hop)
1042eb2b9311SLuis Carlos Cobo {
1043eb2b9311SLuis Carlos Cobo 	spin_lock_bh(&mpath->state_lock);
1044eb2b9311SLuis Carlos Cobo 	mesh_path_assign_nexthop(mpath, next_hop);
1045d19b3bf6SRui Paulo 	mpath->sn = 0xffff;
1046eb2b9311SLuis Carlos Cobo 	mpath->metric = 0;
1047eb2b9311SLuis Carlos Cobo 	mpath->hop_count = 0;
1048eb2b9311SLuis Carlos Cobo 	mpath->exp_time = 0;
10495df20f21SPedersen, Thomas 	mpath->flags = MESH_PATH_FIXED | MESH_PATH_SN_VALID;
1050eb2b9311SLuis Carlos Cobo 	mesh_path_activate(mpath);
1051d5edb9aeSFelix Fietkau 	mesh_fast_tx_flush_mpath(mpath);
1052eb2b9311SLuis Carlos Cobo 	spin_unlock_bh(&mpath->state_lock);
10533eb0928fSManoharan, Rajkumar 	ewma_mesh_fail_avg_init(&next_hop->mesh->fail_avg);
10543eb0928fSManoharan, Rajkumar 	/* init it at a low value - 0 start is tricky */
10553eb0928fSManoharan, Rajkumar 	ewma_mesh_fail_avg_add(&next_hop->mesh->fail_avg, 1);
1056eb2b9311SLuis Carlos Cobo 	mesh_path_tx_pending(mpath);
1057eb2b9311SLuis Carlos Cobo }
1058eb2b9311SLuis Carlos Cobo 
mesh_pathtbl_init(struct ieee80211_sub_if_data * sdata)10598b5cb7e4SPavel Skripkin void mesh_pathtbl_init(struct ieee80211_sub_if_data *sdata)
1060eb2b9311SLuis Carlos Cobo {
10618b5cb7e4SPavel Skripkin 	mesh_table_init(&sdata->u.mesh.mesh_paths);
10628b5cb7e4SPavel Skripkin 	mesh_table_init(&sdata->u.mesh.mpp_paths);
1063d5edb9aeSFelix Fietkau 	mesh_fast_tx_init(sdata);
1064eb2b9311SLuis Carlos Cobo }
1065eb2b9311SLuis Carlos Cobo 
106660854fd9SBob Copeland static
mesh_path_tbl_expire(struct ieee80211_sub_if_data * sdata,struct mesh_table * tbl)106760854fd9SBob Copeland void mesh_path_tbl_expire(struct ieee80211_sub_if_data *sdata,
106860854fd9SBob Copeland 			  struct mesh_table *tbl)
106960854fd9SBob Copeland {
107060854fd9SBob Copeland 	struct mesh_path *mpath;
1071b4c3fbe6SHerbert Xu 	struct hlist_node *n;
107260854fd9SBob Copeland 
1073b4c3fbe6SHerbert Xu 	spin_lock_bh(&tbl->walk_lock);
1074b4c3fbe6SHerbert Xu 	hlist_for_each_entry_safe(mpath, n, &tbl->walk_head, walk_list) {
107560854fd9SBob Copeland 		if ((!(mpath->flags & MESH_PATH_RESOLVING)) &&
107660854fd9SBob Copeland 		    (!(mpath->flags & MESH_PATH_FIXED)) &&
107760854fd9SBob Copeland 		     time_after(jiffies, mpath->exp_time + MESH_PATH_EXPIRE))
107860854fd9SBob Copeland 			__mesh_path_del(tbl, mpath);
107960854fd9SBob Copeland 	}
1080b4c3fbe6SHerbert Xu 	spin_unlock_bh(&tbl->walk_lock);
108160854fd9SBob Copeland }
108260854fd9SBob Copeland 
mesh_path_expire(struct ieee80211_sub_if_data * sdata)1083f698d856SJasper Bryant-Greene void mesh_path_expire(struct ieee80211_sub_if_data *sdata)
1084eb2b9311SLuis Carlos Cobo {
10858b5cb7e4SPavel Skripkin 	mesh_path_tbl_expire(sdata, &sdata->u.mesh.mesh_paths);
10868b5cb7e4SPavel Skripkin 	mesh_path_tbl_expire(sdata, &sdata->u.mesh.mpp_paths);
1087eb2b9311SLuis Carlos Cobo }
1088eb2b9311SLuis Carlos Cobo 
mesh_pathtbl_unregister(struct ieee80211_sub_if_data * sdata)10892bdaf386SBob Copeland void mesh_pathtbl_unregister(struct ieee80211_sub_if_data *sdata)
1090eb2b9311SLuis Carlos Cobo {
1091d5edb9aeSFelix Fietkau 	mesh_fast_tx_deinit(sdata);
10928b5cb7e4SPavel Skripkin 	mesh_table_free(&sdata->u.mesh.mesh_paths);
10938b5cb7e4SPavel Skripkin 	mesh_table_free(&sdata->u.mesh.mpp_paths);
1094eb2b9311SLuis Carlos Cobo }
1095