xref: /openbmc/linux/net/mac80211/mesh_pathtbl.c (revision 8b5cb7e4)
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"
17eb2b9311SLuis Carlos Cobo 
1874932959SBob Copeland static void mesh_path_free_rcu(struct mesh_table *tbl, struct mesh_path *mpath);
1974932959SBob Copeland 
2060854fd9SBob Copeland static u32 mesh_table_hash(const void *addr, u32 len, u32 seed)
2160854fd9SBob Copeland {
2260854fd9SBob Copeland 	/* Use last four bytes of hw addr as hash index */
2340586e3fSFelix Fietkau 	return jhash_1word(__get_unaligned_cpu32((u8 *)addr + 2), seed);
2460854fd9SBob Copeland }
25eb2b9311SLuis Carlos Cobo 
2660854fd9SBob Copeland static const struct rhashtable_params mesh_rht_params = {
2760854fd9SBob Copeland 	.nelem_hint = 2,
2860854fd9SBob Copeland 	.automatic_shrinking = true,
2960854fd9SBob Copeland 	.key_len = ETH_ALEN,
3060854fd9SBob Copeland 	.key_offset = offsetof(struct mesh_path, dst),
3160854fd9SBob Copeland 	.head_offset = offsetof(struct mesh_path, rhash),
3260854fd9SBob Copeland 	.hashfn = mesh_table_hash,
3360854fd9SBob Copeland };
34eb2b9311SLuis Carlos Cobo 
35bf7cd94dSJohannes Berg static inline bool mpath_expired(struct mesh_path *mpath)
36bf7cd94dSJohannes Berg {
37bf7cd94dSJohannes Berg 	return (mpath->flags & MESH_PATH_ACTIVE) &&
38bf7cd94dSJohannes Berg 	       time_after(jiffies, mpath->exp_time) &&
39bf7cd94dSJohannes Berg 	       !(mpath->flags & MESH_PATH_FIXED);
40bf7cd94dSJohannes Berg }
41eb2b9311SLuis Carlos Cobo 
4274932959SBob Copeland static void mesh_path_rht_free(void *ptr, void *tblptr)
43349eb8cfSJohannes Berg {
4460854fd9SBob Copeland 	struct mesh_path *mpath = ptr;
4574932959SBob Copeland 	struct mesh_table *tbl = tblptr;
4674932959SBob Copeland 
4774932959SBob Copeland 	mesh_path_free_rcu(tbl, mpath);
48349eb8cfSJohannes Berg }
49349eb8cfSJohannes Berg 
50*8b5cb7e4SPavel Skripkin static void mesh_table_init(struct mesh_table *tbl)
51349eb8cfSJohannes Berg {
52*8b5cb7e4SPavel Skripkin 	INIT_HLIST_HEAD(&tbl->known_gates);
53*8b5cb7e4SPavel Skripkin 	INIT_HLIST_HEAD(&tbl->walk_head);
54*8b5cb7e4SPavel Skripkin 	atomic_set(&tbl->entries,  0);
55*8b5cb7e4SPavel Skripkin 	spin_lock_init(&tbl->gates_lock);
56*8b5cb7e4SPavel Skripkin 	spin_lock_init(&tbl->walk_lock);
576b86bd62SJohannes Berg 
58*8b5cb7e4SPavel Skripkin 	/* rhashtable_init() may fail only in case of wrong
59*8b5cb7e4SPavel Skripkin 	 * mesh_rht_params
60*8b5cb7e4SPavel Skripkin 	 */
61*8b5cb7e4SPavel Skripkin 	WARN_ON(rhashtable_init(&tbl->rhead, &mesh_rht_params));
626b86bd62SJohannes Berg }
636b86bd62SJohannes Berg 
6460854fd9SBob Copeland static void mesh_table_free(struct mesh_table *tbl)
6518889231SJavier Cardona {
6660854fd9SBob Copeland 	rhashtable_free_and_destroy(&tbl->rhead,
6774932959SBob Copeland 				    mesh_path_rht_free, tbl);
6818889231SJavier Cardona }
6918889231SJavier Cardona 
70eb2b9311SLuis Carlos Cobo /**
71eb2b9311SLuis Carlos Cobo  * mesh_path_assign_nexthop - update mesh path next hop
72eb2b9311SLuis Carlos Cobo  *
73eb2b9311SLuis Carlos Cobo  * @mpath: mesh path to update
74eb2b9311SLuis Carlos Cobo  * @sta: next hop to assign
75eb2b9311SLuis Carlos Cobo  *
76eb2b9311SLuis Carlos Cobo  * Locking: mpath->state_lock must be held when calling this function
77eb2b9311SLuis Carlos Cobo  */
78eb2b9311SLuis Carlos Cobo void mesh_path_assign_nexthop(struct mesh_path *mpath, struct sta_info *sta)
79eb2b9311SLuis Carlos Cobo {
8010c836d7SJavier Cardona 	struct sk_buff *skb;
8110c836d7SJavier Cardona 	struct ieee80211_hdr *hdr;
8210c836d7SJavier Cardona 	unsigned long flags;
8310c836d7SJavier Cardona 
84d0709a65SJohannes Berg 	rcu_assign_pointer(mpath->next_hop, sta);
8510c836d7SJavier Cardona 
8610c836d7SJavier Cardona 	spin_lock_irqsave(&mpath->frame_queue.lock, flags);
87b22bd522SThomas Pedersen 	skb_queue_walk(&mpath->frame_queue, skb) {
8810c836d7SJavier Cardona 		hdr = (struct ieee80211_hdr *) skb->data;
8910c836d7SJavier Cardona 		memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN);
907e3c8866SThomas Pedersen 		memcpy(hdr->addr2, mpath->sdata->vif.addr, ETH_ALEN);
913f52b7e3SMarco Porsch 		ieee80211_mps_set_frame_flags(sta->sdata, sta, hdr);
9210c836d7SJavier Cardona 	}
9310c836d7SJavier Cardona 
9410c836d7SJavier Cardona 	spin_unlock_irqrestore(&mpath->frame_queue.lock, flags);
95eb2b9311SLuis Carlos Cobo }
96eb2b9311SLuis Carlos Cobo 
975ee68e5bSJavier Cardona static void prepare_for_gate(struct sk_buff *skb, char *dst_addr,
985ee68e5bSJavier Cardona 			     struct mesh_path *gate_mpath)
995ee68e5bSJavier Cardona {
1005ee68e5bSJavier Cardona 	struct ieee80211_hdr *hdr;
1015ee68e5bSJavier Cardona 	struct ieee80211s_hdr *mshdr;
1025ee68e5bSJavier Cardona 	int mesh_hdrlen, hdrlen;
1035ee68e5bSJavier Cardona 	char *next_hop;
1045ee68e5bSJavier Cardona 
1055ee68e5bSJavier Cardona 	hdr = (struct ieee80211_hdr *) skb->data;
1065ee68e5bSJavier Cardona 	hdrlen = ieee80211_hdrlen(hdr->frame_control);
1075ee68e5bSJavier Cardona 	mshdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
1085ee68e5bSJavier Cardona 
1095ee68e5bSJavier Cardona 	if (!(mshdr->flags & MESH_FLAGS_AE)) {
1105ee68e5bSJavier Cardona 		/* size of the fixed part of the mesh header */
1115ee68e5bSJavier Cardona 		mesh_hdrlen = 6;
1125ee68e5bSJavier Cardona 
1135ee68e5bSJavier Cardona 		/* make room for the two extended addresses */
1145ee68e5bSJavier Cardona 		skb_push(skb, 2 * ETH_ALEN);
1155ee68e5bSJavier Cardona 		memmove(skb->data, hdr, hdrlen + mesh_hdrlen);
1165ee68e5bSJavier Cardona 
1175ee68e5bSJavier Cardona 		hdr = (struct ieee80211_hdr *) skb->data;
1185ee68e5bSJavier Cardona 
1195ee68e5bSJavier Cardona 		/* we preserve the previous mesh header and only add
120ab4040dfSZheng Yongjun 		 * the new addresses */
1215ee68e5bSJavier Cardona 		mshdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
1225ee68e5bSJavier Cardona 		mshdr->flags = MESH_FLAGS_AE_A5_A6;
1235ee68e5bSJavier Cardona 		memcpy(mshdr->eaddr1, hdr->addr3, ETH_ALEN);
1245ee68e5bSJavier Cardona 		memcpy(mshdr->eaddr2, hdr->addr4, ETH_ALEN);
1255ee68e5bSJavier Cardona 	}
1265ee68e5bSJavier Cardona 
1275ee68e5bSJavier Cardona 	/* update next hop */
1285ee68e5bSJavier Cardona 	hdr = (struct ieee80211_hdr *) skb->data;
1295ee68e5bSJavier Cardona 	rcu_read_lock();
1305ee68e5bSJavier Cardona 	next_hop = rcu_dereference(gate_mpath->next_hop)->sta.addr;
1315ee68e5bSJavier Cardona 	memcpy(hdr->addr1, next_hop, ETH_ALEN);
1325ee68e5bSJavier Cardona 	rcu_read_unlock();
1337e3c8866SThomas Pedersen 	memcpy(hdr->addr2, gate_mpath->sdata->vif.addr, ETH_ALEN);
1345ee68e5bSJavier Cardona 	memcpy(hdr->addr3, dst_addr, ETH_ALEN);
1355ee68e5bSJavier Cardona }
1365ee68e5bSJavier Cardona 
1375ee68e5bSJavier Cardona /**
1385ee68e5bSJavier Cardona  * mesh_path_move_to_queue - Move or copy frames from one mpath queue to another
1395ee68e5bSJavier Cardona  *
1405ee68e5bSJavier Cardona  * This function is used to transfer or copy frames from an unresolved mpath to
1415ee68e5bSJavier Cardona  * a gate mpath.  The function also adds the Address Extension field and
1425ee68e5bSJavier Cardona  * updates the next hop.
1435ee68e5bSJavier Cardona  *
1445ee68e5bSJavier Cardona  * If a frame already has an Address Extension field, only the next hop and
1455ee68e5bSJavier Cardona  * destination addresses are updated.
1465ee68e5bSJavier Cardona  *
1475ee68e5bSJavier Cardona  * The gate mpath must be an active mpath with a valid mpath->next_hop.
1485ee68e5bSJavier Cardona  *
1499fd00b4dSAndrew Lunn  * @gate_mpath: An active mpath the frames will be sent to (i.e. the gate)
1505ee68e5bSJavier Cardona  * @from_mpath: The failed mpath
1515ee68e5bSJavier Cardona  * @copy: When true, copy all the frames to the new mpath queue.  When false,
1525ee68e5bSJavier Cardona  * move them.
1535ee68e5bSJavier Cardona  */
1545ee68e5bSJavier Cardona static void mesh_path_move_to_queue(struct mesh_path *gate_mpath,
1555ee68e5bSJavier Cardona 				    struct mesh_path *from_mpath,
1565ee68e5bSJavier Cardona 				    bool copy)
1575ee68e5bSJavier Cardona {
1584bd4c2ddSThomas Pedersen 	struct sk_buff *skb, *fskb, *tmp;
1594bd4c2ddSThomas Pedersen 	struct sk_buff_head failq;
1605ee68e5bSJavier Cardona 	unsigned long flags;
1615ee68e5bSJavier Cardona 
1628c5bb1faSJohannes Berg 	if (WARN_ON(gate_mpath == from_mpath))
1638c5bb1faSJohannes Berg 		return;
1648c5bb1faSJohannes Berg 	if (WARN_ON(!gate_mpath->next_hop))
1658c5bb1faSJohannes Berg 		return;
1665ee68e5bSJavier Cardona 
1675ee68e5bSJavier Cardona 	__skb_queue_head_init(&failq);
1685ee68e5bSJavier Cardona 
1695ee68e5bSJavier Cardona 	spin_lock_irqsave(&from_mpath->frame_queue.lock, flags);
1705ee68e5bSJavier Cardona 	skb_queue_splice_init(&from_mpath->frame_queue, &failq);
1715ee68e5bSJavier Cardona 	spin_unlock_irqrestore(&from_mpath->frame_queue.lock, flags);
1725ee68e5bSJavier Cardona 
1734bd4c2ddSThomas Pedersen 	skb_queue_walk_safe(&failq, fskb, tmp) {
1744bd4c2ddSThomas Pedersen 		if (skb_queue_len(&gate_mpath->frame_queue) >=
1754bd4c2ddSThomas Pedersen 				  MESH_FRAME_QUEUE_LEN) {
1764bd4c2ddSThomas Pedersen 			mpath_dbg(gate_mpath->sdata, "mpath queue full!\n");
1774bd4c2ddSThomas Pedersen 			break;
178817a53d9SJohn W. Linville 		}
1795ee68e5bSJavier Cardona 
1804bd4c2ddSThomas Pedersen 		skb = skb_copy(fskb, GFP_ATOMIC);
1814bd4c2ddSThomas Pedersen 		if (WARN_ON(!skb))
1824bd4c2ddSThomas Pedersen 			break;
1834bd4c2ddSThomas Pedersen 
1845ee68e5bSJavier Cardona 		prepare_for_gate(skb, gate_mpath->dst, gate_mpath);
1854bd4c2ddSThomas Pedersen 		skb_queue_tail(&gate_mpath->frame_queue, skb);
1864bd4c2ddSThomas Pedersen 
1874bd4c2ddSThomas Pedersen 		if (copy)
1884bd4c2ddSThomas Pedersen 			continue;
1894bd4c2ddSThomas Pedersen 
1904bd4c2ddSThomas Pedersen 		__skb_unlink(fskb, &failq);
1914bd4c2ddSThomas Pedersen 		kfree_skb(fskb);
1925ee68e5bSJavier Cardona 	}
1935ee68e5bSJavier Cardona 
194bdcbd8e0SJohannes Berg 	mpath_dbg(gate_mpath->sdata, "Mpath queue for gate %pM has %d frames\n",
195bdcbd8e0SJohannes Berg 		  gate_mpath->dst, skb_queue_len(&gate_mpath->frame_queue));
1965ee68e5bSJavier Cardona 
1975ee68e5bSJavier Cardona 	if (!copy)
1985ee68e5bSJavier Cardona 		return;
1995ee68e5bSJavier Cardona 
2005ee68e5bSJavier Cardona 	spin_lock_irqsave(&from_mpath->frame_queue.lock, flags);
2015ee68e5bSJavier Cardona 	skb_queue_splice(&failq, &from_mpath->frame_queue);
2025ee68e5bSJavier Cardona 	spin_unlock_irqrestore(&from_mpath->frame_queue.lock, flags);
2035ee68e5bSJavier Cardona }
2045ee68e5bSJavier Cardona 
205eb2b9311SLuis Carlos Cobo 
2064a3cb702SJohannes Berg static struct mesh_path *mpath_lookup(struct mesh_table *tbl, const u8 *dst,
207239289e4SJavier Cardona 				      struct ieee80211_sub_if_data *sdata)
208239289e4SJavier Cardona {
209239289e4SJavier Cardona 	struct mesh_path *mpath;
210239289e4SJavier Cardona 
211ef618b1bSFelix Fietkau 	mpath = rhashtable_lookup(&tbl->rhead, dst, mesh_rht_params);
21260854fd9SBob Copeland 
21360854fd9SBob Copeland 	if (mpath && mpath_expired(mpath)) {
214239289e4SJavier Cardona 		spin_lock_bh(&mpath->state_lock);
215239289e4SJavier Cardona 		mpath->flags &= ~MESH_PATH_ACTIVE;
216239289e4SJavier Cardona 		spin_unlock_bh(&mpath->state_lock);
217239289e4SJavier Cardona 	}
218239289e4SJavier Cardona 	return mpath;
219239289e4SJavier Cardona }
220239289e4SJavier Cardona 
221eb2b9311SLuis Carlos Cobo /**
222eb2b9311SLuis Carlos Cobo  * mesh_path_lookup - look up a path in the mesh path table
223f698d856SJasper Bryant-Greene  * @sdata: local subif
224bf7cd94dSJohannes Berg  * @dst: hardware address (ETH_ALEN length) of destination
225eb2b9311SLuis Carlos Cobo  *
226eb2b9311SLuis Carlos Cobo  * Returns: pointer to the mesh path structure, or NULL if not found
227eb2b9311SLuis Carlos Cobo  *
228eb2b9311SLuis Carlos Cobo  * Locking: must be called within a read rcu section.
229eb2b9311SLuis Carlos Cobo  */
230bf7cd94dSJohannes Berg struct mesh_path *
231bf7cd94dSJohannes Berg mesh_path_lookup(struct ieee80211_sub_if_data *sdata, const u8 *dst)
232eb2b9311SLuis Carlos Cobo {
233*8b5cb7e4SPavel Skripkin 	return mpath_lookup(&sdata->u.mesh.mesh_paths, dst, sdata);
234eb2b9311SLuis Carlos Cobo }
235eb2b9311SLuis Carlos Cobo 
236bf7cd94dSJohannes Berg struct mesh_path *
237bf7cd94dSJohannes Berg mpp_path_lookup(struct ieee80211_sub_if_data *sdata, const u8 *dst)
23879617deeSYanBo {
239*8b5cb7e4SPavel Skripkin 	return mpath_lookup(&sdata->u.mesh.mpp_paths, dst, sdata);
24079617deeSYanBo }
24179617deeSYanBo 
24260854fd9SBob Copeland static struct mesh_path *
24360854fd9SBob Copeland __mesh_path_lookup_by_idx(struct mesh_table *tbl, int idx)
24460854fd9SBob Copeland {
245b4c3fbe6SHerbert Xu 	int i = 0;
246b4c3fbe6SHerbert Xu 	struct mesh_path *mpath;
24760854fd9SBob Copeland 
248b4c3fbe6SHerbert Xu 	hlist_for_each_entry_rcu(mpath, &tbl->walk_head, walk_list) {
24960854fd9SBob Copeland 		if (i++ == idx)
25060854fd9SBob Copeland 			break;
25160854fd9SBob Copeland 	}
25260854fd9SBob Copeland 
253b4c3fbe6SHerbert Xu 	if (!mpath)
25460854fd9SBob Copeland 		return NULL;
25560854fd9SBob Copeland 
25660854fd9SBob Copeland 	if (mpath_expired(mpath)) {
25760854fd9SBob Copeland 		spin_lock_bh(&mpath->state_lock);
25860854fd9SBob Copeland 		mpath->flags &= ~MESH_PATH_ACTIVE;
25960854fd9SBob Copeland 		spin_unlock_bh(&mpath->state_lock);
26060854fd9SBob Copeland 	}
26160854fd9SBob Copeland 	return mpath;
26260854fd9SBob Copeland }
26379617deeSYanBo 
264eb2b9311SLuis Carlos Cobo /**
265eb2b9311SLuis Carlos Cobo  * mesh_path_lookup_by_idx - look up a path in the mesh path table by its index
266eb2b9311SLuis Carlos Cobo  * @idx: index
267f698d856SJasper Bryant-Greene  * @sdata: local subif, or NULL for all entries
268eb2b9311SLuis Carlos Cobo  *
269eb2b9311SLuis Carlos Cobo  * Returns: pointer to the mesh path structure, or NULL if not found.
270eb2b9311SLuis Carlos Cobo  *
271eb2b9311SLuis Carlos Cobo  * Locking: must be called within a read rcu section.
272eb2b9311SLuis Carlos Cobo  */
273bf7cd94dSJohannes Berg struct mesh_path *
274bf7cd94dSJohannes Berg mesh_path_lookup_by_idx(struct ieee80211_sub_if_data *sdata, int idx)
275eb2b9311SLuis Carlos Cobo {
276*8b5cb7e4SPavel Skripkin 	return __mesh_path_lookup_by_idx(&sdata->u.mesh.mesh_paths, idx);
277eb2b9311SLuis Carlos Cobo }
278eb2b9311SLuis Carlos Cobo 
2795ee68e5bSJavier Cardona /**
280a2db2ed3SHenning Rogge  * mpp_path_lookup_by_idx - look up a path in the proxy path table by its index
281a2db2ed3SHenning Rogge  * @idx: index
282a2db2ed3SHenning Rogge  * @sdata: local subif, or NULL for all entries
283a2db2ed3SHenning Rogge  *
284a2db2ed3SHenning Rogge  * Returns: pointer to the proxy path structure, or NULL if not found.
285a2db2ed3SHenning Rogge  *
286a2db2ed3SHenning Rogge  * Locking: must be called within a read rcu section.
287a2db2ed3SHenning Rogge  */
288a2db2ed3SHenning Rogge struct mesh_path *
289a2db2ed3SHenning Rogge mpp_path_lookup_by_idx(struct ieee80211_sub_if_data *sdata, int idx)
290a2db2ed3SHenning Rogge {
291*8b5cb7e4SPavel Skripkin 	return __mesh_path_lookup_by_idx(&sdata->u.mesh.mpp_paths, idx);
292a2db2ed3SHenning Rogge }
293a2db2ed3SHenning Rogge 
294a2db2ed3SHenning Rogge /**
29530be52e4SJohannes Berg  * mesh_path_add_gate - add the given mpath to a mesh gate to our path table
29630be52e4SJohannes Berg  * @mpath: gate path to add to table
2975ee68e5bSJavier Cardona  */
29830be52e4SJohannes Berg int mesh_path_add_gate(struct mesh_path *mpath)
2995ee68e5bSJavier Cardona {
30030be52e4SJohannes Berg 	struct mesh_table *tbl;
3015ee68e5bSJavier Cardona 	int err;
3025ee68e5bSJavier Cardona 
3035ee68e5bSJavier Cardona 	rcu_read_lock();
304*8b5cb7e4SPavel Skripkin 	tbl = &mpath->sdata->u.mesh.mesh_paths;
3055ee68e5bSJavier Cardona 
306947c2a0eSBob Copeland 	spin_lock_bh(&mpath->state_lock);
307947c2a0eSBob Copeland 	if (mpath->is_gate) {
3085ee68e5bSJavier Cardona 		err = -EEXIST;
309947c2a0eSBob Copeland 		spin_unlock_bh(&mpath->state_lock);
3105ee68e5bSJavier Cardona 		goto err_rcu;
3115ee68e5bSJavier Cardona 	}
3125ee68e5bSJavier Cardona 	mpath->is_gate = true;
3135ee68e5bSJavier Cardona 	mpath->sdata->u.mesh.num_gates++;
314947c2a0eSBob Copeland 
315947c2a0eSBob Copeland 	spin_lock(&tbl->gates_lock);
31618b27ff7SBob Copeland 	hlist_add_head_rcu(&mpath->gate_list, &tbl->known_gates);
317947c2a0eSBob Copeland 	spin_unlock(&tbl->gates_lock);
318947c2a0eSBob Copeland 
319947c2a0eSBob Copeland 	spin_unlock_bh(&mpath->state_lock);
320947c2a0eSBob Copeland 
321bdcbd8e0SJohannes Berg 	mpath_dbg(mpath->sdata,
322bdcbd8e0SJohannes Berg 		  "Mesh path: Recorded new gate: %pM. %d known gates\n",
323bdcbd8e0SJohannes Berg 		  mpath->dst, mpath->sdata->u.mesh.num_gates);
324bf7cd94dSJohannes Berg 	err = 0;
3255ee68e5bSJavier Cardona err_rcu:
3265ee68e5bSJavier Cardona 	rcu_read_unlock();
3275ee68e5bSJavier Cardona 	return err;
3285ee68e5bSJavier Cardona }
3295ee68e5bSJavier Cardona 
3305ee68e5bSJavier Cardona /**
3315ee68e5bSJavier Cardona  * mesh_gate_del - remove a mesh gate from the list of known gates
3325ee68e5bSJavier Cardona  * @tbl: table which holds our list of known gates
3335ee68e5bSJavier Cardona  * @mpath: gate mpath
3345ee68e5bSJavier Cardona  */
335bf7cd94dSJohannes Berg static void mesh_gate_del(struct mesh_table *tbl, struct mesh_path *mpath)
3365ee68e5bSJavier Cardona {
337947c2a0eSBob Copeland 	lockdep_assert_held(&mpath->state_lock);
338947c2a0eSBob Copeland 	if (!mpath->is_gate)
339947c2a0eSBob Copeland 		return;
3405ee68e5bSJavier Cardona 
3415ee68e5bSJavier Cardona 	mpath->is_gate = false;
342947c2a0eSBob Copeland 	spin_lock_bh(&tbl->gates_lock);
343947c2a0eSBob Copeland 	hlist_del_rcu(&mpath->gate_list);
344947c2a0eSBob Copeland 	mpath->sdata->u.mesh.num_gates--;
345947c2a0eSBob Copeland 	spin_unlock_bh(&tbl->gates_lock);
346947c2a0eSBob Copeland 
347bdcbd8e0SJohannes Berg 	mpath_dbg(mpath->sdata,
348bdcbd8e0SJohannes Berg 		  "Mesh path: Deleted gate: %pM. %d known gates\n",
3495ee68e5bSJavier Cardona 		  mpath->dst, mpath->sdata->u.mesh.num_gates);
3505ee68e5bSJavier Cardona }
3515ee68e5bSJavier Cardona 
3525ee68e5bSJavier Cardona /**
3535ee68e5bSJavier Cardona  * mesh_gate_num - number of gates known to this interface
3545ee68e5bSJavier Cardona  * @sdata: subif data
3555ee68e5bSJavier Cardona  */
3565ee68e5bSJavier Cardona int mesh_gate_num(struct ieee80211_sub_if_data *sdata)
3575ee68e5bSJavier Cardona {
3585ee68e5bSJavier Cardona 	return sdata->u.mesh.num_gates;
3595ee68e5bSJavier Cardona }
3605ee68e5bSJavier Cardona 
361b15dc38bSBob Copeland static
362b15dc38bSBob Copeland struct mesh_path *mesh_path_new(struct ieee80211_sub_if_data *sdata,
363b15dc38bSBob Copeland 				const u8 *dst, gfp_t gfp_flags)
364b15dc38bSBob Copeland {
365b15dc38bSBob Copeland 	struct mesh_path *new_mpath;
366b15dc38bSBob Copeland 
367b15dc38bSBob Copeland 	new_mpath = kzalloc(sizeof(struct mesh_path), gfp_flags);
368b15dc38bSBob Copeland 	if (!new_mpath)
369b15dc38bSBob Copeland 		return NULL;
370b15dc38bSBob Copeland 
371b15dc38bSBob Copeland 	memcpy(new_mpath->dst, dst, ETH_ALEN);
372b15dc38bSBob Copeland 	eth_broadcast_addr(new_mpath->rann_snd_addr);
373b15dc38bSBob Copeland 	new_mpath->is_root = false;
374b15dc38bSBob Copeland 	new_mpath->sdata = sdata;
375b15dc38bSBob Copeland 	new_mpath->flags = 0;
376b15dc38bSBob Copeland 	skb_queue_head_init(&new_mpath->frame_queue);
377b15dc38bSBob Copeland 	new_mpath->exp_time = jiffies;
378b15dc38bSBob Copeland 	spin_lock_init(&new_mpath->state_lock);
37934f11cd3SKees Cook 	timer_setup(&new_mpath->timer, mesh_path_timer, 0);
380b15dc38bSBob Copeland 
381b15dc38bSBob Copeland 	return new_mpath;
382b15dc38bSBob Copeland }
383b15dc38bSBob Copeland 
384eb2b9311SLuis Carlos Cobo /**
385eb2b9311SLuis Carlos Cobo  * mesh_path_add - allocate and add a new path to the mesh path table
386bf7cd94dSJohannes Berg  * @dst: destination address of the path (ETH_ALEN length)
387f698d856SJasper Bryant-Greene  * @sdata: local subif
388eb2b9311SLuis Carlos Cobo  *
389af901ca1SAndré Goddard Rosa  * Returns: 0 on success
390eb2b9311SLuis Carlos Cobo  *
391eb2b9311SLuis Carlos Cobo  * State: the initial state of the new path is set to 0
392eb2b9311SLuis Carlos Cobo  */
393ae76eef0SBob Copeland struct mesh_path *mesh_path_add(struct ieee80211_sub_if_data *sdata,
394ae76eef0SBob Copeland 				const u8 *dst)
395eb2b9311SLuis Carlos Cobo {
396349eb8cfSJohannes Berg 	struct mesh_table *tbl;
397eb2b9311SLuis Carlos Cobo 	struct mesh_path *mpath, *new_mpath;
398eb2b9311SLuis Carlos Cobo 
399b203ca39SJoe Perches 	if (ether_addr_equal(dst, sdata->vif.addr))
400eb2b9311SLuis Carlos Cobo 		/* never add ourselves as neighbours */
401ae76eef0SBob Copeland 		return ERR_PTR(-ENOTSUPP);
402eb2b9311SLuis Carlos Cobo 
403eb2b9311SLuis Carlos Cobo 	if (is_multicast_ether_addr(dst))
404ae76eef0SBob Copeland 		return ERR_PTR(-ENOTSUPP);
405eb2b9311SLuis Carlos Cobo 
406472dbc45SJohannes Berg 	if (atomic_add_unless(&sdata->u.mesh.mpaths, 1, MESH_MAX_MPATHS) == 0)
407ae76eef0SBob Copeland 		return ERR_PTR(-ENOSPC);
408ae76eef0SBob Copeland 
409b15dc38bSBob Copeland 	new_mpath = mesh_path_new(sdata, dst, GFP_ATOMIC);
410402d7752SPavel Emelyanov 	if (!new_mpath)
41160854fd9SBob Copeland 		return ERR_PTR(-ENOMEM);
412402d7752SPavel Emelyanov 
413*8b5cb7e4SPavel Skripkin 	tbl = &sdata->u.mesh.mesh_paths;
414b4c3fbe6SHerbert Xu 	spin_lock_bh(&tbl->walk_lock);
41536922931SHerbert Xu 	mpath = rhashtable_lookup_get_insert_fast(&tbl->rhead,
41660854fd9SBob Copeland 						  &new_mpath->rhash,
41760854fd9SBob Copeland 						  mesh_rht_params);
41836922931SHerbert Xu 	if (!mpath)
419b4c3fbe6SHerbert Xu 		hlist_add_head(&new_mpath->walk_list, &tbl->walk_head);
420b4c3fbe6SHerbert Xu 	spin_unlock_bh(&tbl->walk_lock);
421f5ea9120SJohannes Berg 
42236922931SHerbert Xu 	if (mpath) {
4234ff3a9d1SHerbert Xu 		kfree(new_mpath);
4244ff3a9d1SHerbert Xu 
42536922931SHerbert Xu 		if (IS_ERR(mpath))
42636922931SHerbert Xu 			return mpath;
427ae76eef0SBob Copeland 
42860854fd9SBob Copeland 		new_mpath = mpath;
429eb2b9311SLuis Carlos Cobo 	}
4304ff3a9d1SHerbert Xu 
43160854fd9SBob Copeland 	sdata->u.mesh.mesh_paths_generation++;
43260854fd9SBob Copeland 	return new_mpath;
43318889231SJavier Cardona }
434eb2b9311SLuis Carlos Cobo 
435bf7cd94dSJohannes Berg int mpp_path_add(struct ieee80211_sub_if_data *sdata,
436bf7cd94dSJohannes Berg 		 const u8 *dst, const u8 *mpp)
43779617deeSYanBo {
438349eb8cfSJohannes Berg 	struct mesh_table *tbl;
43960854fd9SBob Copeland 	struct mesh_path *new_mpath;
44060854fd9SBob Copeland 	int ret;
44179617deeSYanBo 
442b203ca39SJoe Perches 	if (ether_addr_equal(dst, sdata->vif.addr))
44379617deeSYanBo 		/* never add ourselves as neighbours */
44479617deeSYanBo 		return -ENOTSUPP;
44579617deeSYanBo 
44679617deeSYanBo 	if (is_multicast_ether_addr(dst))
44779617deeSYanBo 		return -ENOTSUPP;
44879617deeSYanBo 
449b15dc38bSBob Copeland 	new_mpath = mesh_path_new(sdata, dst, GFP_ATOMIC);
45079617deeSYanBo 
45160854fd9SBob Copeland 	if (!new_mpath)
45260854fd9SBob Copeland 		return -ENOMEM;
45379617deeSYanBo 
45479617deeSYanBo 	memcpy(new_mpath->mpp, mpp, ETH_ALEN);
455*8b5cb7e4SPavel Skripkin 	tbl = &sdata->u.mesh.mpp_paths;
456b4c3fbe6SHerbert Xu 
457b4c3fbe6SHerbert Xu 	spin_lock_bh(&tbl->walk_lock);
45860854fd9SBob Copeland 	ret = rhashtable_lookup_insert_fast(&tbl->rhead,
45960854fd9SBob Copeland 					    &new_mpath->rhash,
46060854fd9SBob Copeland 					    mesh_rht_params);
461b4c3fbe6SHerbert Xu 	if (!ret)
462b4c3fbe6SHerbert Xu 		hlist_add_head_rcu(&new_mpath->walk_list, &tbl->walk_head);
463b4c3fbe6SHerbert Xu 	spin_unlock_bh(&tbl->walk_lock);
464a2db2ed3SHenning Rogge 
4654ff3a9d1SHerbert Xu 	if (ret)
4664ff3a9d1SHerbert Xu 		kfree(new_mpath);
4674ff3a9d1SHerbert Xu 
4682bdaf386SBob Copeland 	sdata->u.mesh.mpp_paths_generation++;
46960854fd9SBob Copeland 	return ret;
47079617deeSYanBo }
47179617deeSYanBo 
47279617deeSYanBo 
473eb2b9311SLuis Carlos Cobo /**
474eb2b9311SLuis Carlos Cobo  * mesh_plink_broken - deactivates paths and sends perr when a link breaks
475eb2b9311SLuis Carlos Cobo  *
476eb2b9311SLuis Carlos Cobo  * @sta: broken peer link
477eb2b9311SLuis Carlos Cobo  *
478eb2b9311SLuis Carlos Cobo  * This function must be called from the rate control algorithm if enough
479eb2b9311SLuis Carlos Cobo  * delivery errors suggest that a peer link is no longer usable.
480eb2b9311SLuis Carlos Cobo  */
481eb2b9311SLuis Carlos Cobo void mesh_plink_broken(struct sta_info *sta)
482eb2b9311SLuis Carlos Cobo {
48360854fd9SBob Copeland 	struct ieee80211_sub_if_data *sdata = sta->sdata;
484*8b5cb7e4SPavel Skripkin 	struct mesh_table *tbl = &sdata->u.mesh.mesh_paths;
48515ff6365SJohannes Berg 	static const u8 bcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
486eb2b9311SLuis Carlos Cobo 	struct mesh_path *mpath;
487eb2b9311SLuis Carlos Cobo 
488b4c3fbe6SHerbert Xu 	rcu_read_lock();
489b4c3fbe6SHerbert Xu 	hlist_for_each_entry_rcu(mpath, &tbl->walk_head, walk_list) {
4902688eba9SAndreea-Cristina Bernat 		if (rcu_access_pointer(mpath->next_hop) == sta &&
491eb2b9311SLuis Carlos Cobo 		    mpath->flags & MESH_PATH_ACTIVE &&
492eb2b9311SLuis Carlos Cobo 		    !(mpath->flags & MESH_PATH_FIXED)) {
493f5e50cd0SJavier Cardona 			spin_lock_bh(&mpath->state_lock);
494eb2b9311SLuis Carlos Cobo 			mpath->flags &= ~MESH_PATH_ACTIVE;
495d19b3bf6SRui Paulo 			++mpath->sn;
496eb2b9311SLuis Carlos Cobo 			spin_unlock_bh(&mpath->state_lock);
497bf7cd94dSJohannes Berg 			mesh_path_error_tx(sdata,
498bf7cd94dSJohannes Berg 				sdata->u.mesh.mshcfg.element_ttl,
499f63f8421SChun-Yeow Yeoh 				mpath->dst, mpath->sn,
500f63f8421SChun-Yeow Yeoh 				WLAN_REASON_MESH_PATH_DEST_UNREACHABLE, bcast);
501f5e50cd0SJavier Cardona 		}
502eb2b9311SLuis Carlos Cobo 	}
503b4c3fbe6SHerbert Xu 	rcu_read_unlock();
504eb2b9311SLuis Carlos Cobo }
505eb2b9311SLuis Carlos Cobo 
50674932959SBob Copeland static void mesh_path_free_rcu(struct mesh_table *tbl,
50774932959SBob Copeland 			       struct mesh_path *mpath)
50819c50b3dSJavier Cardona {
50960854fd9SBob Copeland 	struct ieee80211_sub_if_data *sdata = mpath->sdata;
51019c50b3dSJavier Cardona 
511947c2a0eSBob Copeland 	spin_lock_bh(&mpath->state_lock);
51274932959SBob Copeland 	mpath->flags |= MESH_PATH_RESOLVING | MESH_PATH_DELETED;
51319c50b3dSJavier Cardona 	mesh_gate_del(tbl, mpath);
514947c2a0eSBob Copeland 	spin_unlock_bh(&mpath->state_lock);
51574932959SBob Copeland 	del_timer_sync(&mpath->timer);
516c2e703a5SJohannes Berg 	atomic_dec(&sdata->u.mesh.mpaths);
51719c50b3dSJavier Cardona 	atomic_dec(&tbl->entries);
5185e43540cSRemi Pommarel 	mesh_path_flush_pending(mpath);
51974932959SBob Copeland 	kfree_rcu(mpath, rcu);
52074932959SBob Copeland }
52174932959SBob Copeland 
52274932959SBob Copeland static void __mesh_path_del(struct mesh_table *tbl, struct mesh_path *mpath)
52374932959SBob Copeland {
524b4c3fbe6SHerbert Xu 	hlist_del_rcu(&mpath->walk_list);
52574932959SBob Copeland 	rhashtable_remove_fast(&tbl->rhead, &mpath->rhash, mesh_rht_params);
52674932959SBob Copeland 	mesh_path_free_rcu(tbl, mpath);
52719c50b3dSJavier Cardona }
52819c50b3dSJavier Cardona 
529eb2b9311SLuis Carlos Cobo /**
530eb2b9311SLuis Carlos Cobo  * mesh_path_flush_by_nexthop - Deletes mesh paths if their next hop matches
531eb2b9311SLuis Carlos Cobo  *
5322c53040fSBen Hutchings  * @sta: mesh peer to match
533eb2b9311SLuis Carlos Cobo  *
534b4e08ea1SLuis Carlos Cobo  * RCU notes: this function is called when a mesh plink transitions from
535b4e08ea1SLuis Carlos Cobo  * PLINK_ESTAB to any other state, since PLINK_ESTAB state is the only one that
536b4e08ea1SLuis Carlos Cobo  * allows path creation. This will happen before the sta can be freed (because
537d0709a65SJohannes Berg  * sta_info_destroy() calls this) so any reader in a rcu read block will be
538d0709a65SJohannes Berg  * protected against the plink disappearing.
539eb2b9311SLuis Carlos Cobo  */
540eb2b9311SLuis Carlos Cobo void mesh_path_flush_by_nexthop(struct sta_info *sta)
541eb2b9311SLuis Carlos Cobo {
5422bdaf386SBob Copeland 	struct ieee80211_sub_if_data *sdata = sta->sdata;
543*8b5cb7e4SPavel Skripkin 	struct mesh_table *tbl = &sdata->u.mesh.mesh_paths;
544eb2b9311SLuis Carlos Cobo 	struct mesh_path *mpath;
545b4c3fbe6SHerbert Xu 	struct hlist_node *n;
546eb2b9311SLuis Carlos Cobo 
547b4c3fbe6SHerbert Xu 	spin_lock_bh(&tbl->walk_lock);
548b4c3fbe6SHerbert Xu 	hlist_for_each_entry_safe(mpath, n, &tbl->walk_head, walk_list) {
54960854fd9SBob Copeland 		if (rcu_access_pointer(mpath->next_hop) == sta)
55060854fd9SBob Copeland 			__mesh_path_del(tbl, mpath);
551eb2b9311SLuis Carlos Cobo 	}
552b4c3fbe6SHerbert Xu 	spin_unlock_bh(&tbl->walk_lock);
553eb2b9311SLuis Carlos Cobo }
554eb2b9311SLuis Carlos Cobo 
555bf5a70e1SHenning Rogge static void mpp_flush_by_proxy(struct ieee80211_sub_if_data *sdata,
556bf5a70e1SHenning Rogge 			       const u8 *proxy)
557bf5a70e1SHenning Rogge {
558*8b5cb7e4SPavel Skripkin 	struct mesh_table *tbl = &sdata->u.mesh.mpp_paths;
55960854fd9SBob Copeland 	struct mesh_path *mpath;
560b4c3fbe6SHerbert Xu 	struct hlist_node *n;
561bf5a70e1SHenning Rogge 
562b4c3fbe6SHerbert Xu 	spin_lock_bh(&tbl->walk_lock);
563b4c3fbe6SHerbert Xu 	hlist_for_each_entry_safe(mpath, n, &tbl->walk_head, walk_list) {
56460854fd9SBob Copeland 		if (ether_addr_equal(mpath->mpp, proxy))
56560854fd9SBob Copeland 			__mesh_path_del(tbl, mpath);
566bf5a70e1SHenning Rogge 	}
567b4c3fbe6SHerbert Xu 	spin_unlock_bh(&tbl->walk_lock);
568bf5a70e1SHenning Rogge }
569bf5a70e1SHenning Rogge 
57060854fd9SBob Copeland static void table_flush_by_iface(struct mesh_table *tbl)
571eb2b9311SLuis Carlos Cobo {
572eb2b9311SLuis Carlos Cobo 	struct mesh_path *mpath;
573b4c3fbe6SHerbert Xu 	struct hlist_node *n;
574eb2b9311SLuis Carlos Cobo 
575b4c3fbe6SHerbert Xu 	spin_lock_bh(&tbl->walk_lock);
576b4c3fbe6SHerbert Xu 	hlist_for_each_entry_safe(mpath, n, &tbl->walk_head, walk_list) {
57760854fd9SBob Copeland 		__mesh_path_del(tbl, mpath);
578eb2b9311SLuis Carlos Cobo 	}
579b4c3fbe6SHerbert Xu 	spin_unlock_bh(&tbl->walk_lock);
580eb2b9311SLuis Carlos Cobo }
581eb2b9311SLuis Carlos Cobo 
582ece1a2e7SJavier Cardona /**
583ece1a2e7SJavier Cardona  * mesh_path_flush_by_iface - Deletes all mesh paths associated with a given iface
584ece1a2e7SJavier Cardona  *
585ece1a2e7SJavier Cardona  * This function deletes both mesh paths as well as mesh portal paths.
586ece1a2e7SJavier Cardona  *
5872c53040fSBen Hutchings  * @sdata: interface data to match
588ece1a2e7SJavier Cardona  *
589ece1a2e7SJavier Cardona  */
590ece1a2e7SJavier Cardona void mesh_path_flush_by_iface(struct ieee80211_sub_if_data *sdata)
591eb2b9311SLuis Carlos Cobo {
592*8b5cb7e4SPavel Skripkin 	table_flush_by_iface(&sdata->u.mesh.mesh_paths);
593*8b5cb7e4SPavel Skripkin 	table_flush_by_iface(&sdata->u.mesh.mpp_paths);
594eb2b9311SLuis Carlos Cobo }
595eb2b9311SLuis Carlos Cobo 
596eb2b9311SLuis Carlos Cobo /**
5974cc955deSHenning Rogge  * table_path_del - delete a path from the mesh or mpp table
598eb2b9311SLuis Carlos Cobo  *
5994cc955deSHenning Rogge  * @tbl: mesh or mpp path table
600f698d856SJasper Bryant-Greene  * @sdata: local subif
6014cc955deSHenning Rogge  * @addr: dst address (ETH_ALEN length)
602eb2b9311SLuis Carlos Cobo  *
603af901ca1SAndré Goddard Rosa  * Returns: 0 if successful
604eb2b9311SLuis Carlos Cobo  */
60560854fd9SBob Copeland static int table_path_del(struct mesh_table *tbl,
6064cc955deSHenning Rogge 			  struct ieee80211_sub_if_data *sdata,
6074cc955deSHenning Rogge 			  const u8 *addr)
608eb2b9311SLuis Carlos Cobo {
609eb2b9311SLuis Carlos Cobo 	struct mesh_path *mpath;
610eb2b9311SLuis Carlos Cobo 
611b4c3fbe6SHerbert Xu 	spin_lock_bh(&tbl->walk_lock);
61260854fd9SBob Copeland 	mpath = rhashtable_lookup_fast(&tbl->rhead, addr, mesh_rht_params);
61360854fd9SBob Copeland 	if (!mpath) {
614f2ffff08SWei Yongjun 		spin_unlock_bh(&tbl->walk_lock);
61560854fd9SBob Copeland 		return -ENXIO;
616eb2b9311SLuis Carlos Cobo 	}
617eb2b9311SLuis Carlos Cobo 
61860854fd9SBob Copeland 	__mesh_path_del(tbl, mpath);
619b4c3fbe6SHerbert Xu 	spin_unlock_bh(&tbl->walk_lock);
62060854fd9SBob Copeland 	return 0;
6214cc955deSHenning Rogge }
6224cc955deSHenning Rogge 
62360854fd9SBob Copeland 
6244cc955deSHenning Rogge /**
6254cc955deSHenning Rogge  * mesh_path_del - delete a mesh path from the table
6264cc955deSHenning Rogge  *
6274cc955deSHenning Rogge  * @addr: dst address (ETH_ALEN length)
6284cc955deSHenning Rogge  * @sdata: local subif
6294cc955deSHenning Rogge  *
6304cc955deSHenning Rogge  * Returns: 0 if successful
6314cc955deSHenning Rogge  */
6324cc955deSHenning Rogge int mesh_path_del(struct ieee80211_sub_if_data *sdata, const u8 *addr)
6334cc955deSHenning Rogge {
63460854fd9SBob Copeland 	int err;
6354cc955deSHenning Rogge 
6364cc955deSHenning Rogge 	/* flush relevant mpp entries first */
6374cc955deSHenning Rogge 	mpp_flush_by_proxy(sdata, addr);
6384cc955deSHenning Rogge 
639*8b5cb7e4SPavel Skripkin 	err = table_path_del(&sdata->u.mesh.mesh_paths, sdata, addr);
6402bdaf386SBob Copeland 	sdata->u.mesh.mesh_paths_generation++;
641ab1c7906SHenning Rogge 	return err;
642ab1c7906SHenning Rogge }
643ab1c7906SHenning Rogge 
644ab1c7906SHenning Rogge /**
645eb2b9311SLuis Carlos Cobo  * mesh_path_tx_pending - sends pending frames in a mesh path queue
646eb2b9311SLuis Carlos Cobo  *
647eb2b9311SLuis Carlos Cobo  * @mpath: mesh path to activate
648eb2b9311SLuis Carlos Cobo  *
649eb2b9311SLuis Carlos Cobo  * Locking: the state_lock of the mpath structure must NOT be held when calling
650eb2b9311SLuis Carlos Cobo  * this function.
651eb2b9311SLuis Carlos Cobo  */
652eb2b9311SLuis Carlos Cobo void mesh_path_tx_pending(struct mesh_path *mpath)
653eb2b9311SLuis Carlos Cobo {
654249b405cSJavier Cardona 	if (mpath->flags & MESH_PATH_ACTIVE)
655249b405cSJavier Cardona 		ieee80211_add_pending_skbs(mpath->sdata->local,
656249b405cSJavier Cardona 				&mpath->frame_queue);
657eb2b9311SLuis Carlos Cobo }
658eb2b9311SLuis Carlos Cobo 
659eb2b9311SLuis Carlos Cobo /**
6605ee68e5bSJavier Cardona  * mesh_path_send_to_gates - sends pending frames to all known mesh gates
6615ee68e5bSJavier Cardona  *
6625ee68e5bSJavier Cardona  * @mpath: mesh path whose queue will be emptied
6635ee68e5bSJavier Cardona  *
6645ee68e5bSJavier Cardona  * If there is only one gate, the frames are transferred from the failed mpath
6655ee68e5bSJavier Cardona  * queue to that gate's queue.  If there are more than one gates, the frames
6665ee68e5bSJavier Cardona  * are copied from each gate to the next.  After frames are copied, the
6675ee68e5bSJavier Cardona  * mpath queues are emptied onto the transmission queue.
6685ee68e5bSJavier Cardona  */
6695ee68e5bSJavier Cardona int mesh_path_send_to_gates(struct mesh_path *mpath)
6705ee68e5bSJavier Cardona {
6715ee68e5bSJavier Cardona 	struct ieee80211_sub_if_data *sdata = mpath->sdata;
6725ee68e5bSJavier Cardona 	struct mesh_table *tbl;
6735ee68e5bSJavier Cardona 	struct mesh_path *from_mpath = mpath;
67460854fd9SBob Copeland 	struct mesh_path *gate;
6755ee68e5bSJavier Cardona 	bool copy = false;
6765ee68e5bSJavier Cardona 
677*8b5cb7e4SPavel Skripkin 	tbl = &sdata->u.mesh.mesh_paths;
6785ee68e5bSJavier Cardona 
67960854fd9SBob Copeland 	rcu_read_lock();
68018b27ff7SBob Copeland 	hlist_for_each_entry_rcu(gate, &tbl->known_gates, gate_list) {
681947c2a0eSBob Copeland 		if (gate->flags & MESH_PATH_ACTIVE) {
682947c2a0eSBob Copeland 			mpath_dbg(sdata, "Forwarding to %pM\n", gate->dst);
683947c2a0eSBob Copeland 			mesh_path_move_to_queue(gate, from_mpath, copy);
684947c2a0eSBob Copeland 			from_mpath = gate;
6855ee68e5bSJavier Cardona 			copy = true;
6865ee68e5bSJavier Cardona 		} else {
687bdcbd8e0SJohannes Berg 			mpath_dbg(sdata,
688d671b2a0SJohannes Berg 				  "Not forwarding to %pM (flags %#x)\n",
689947c2a0eSBob Copeland 				  gate->dst, gate->flags);
6905ee68e5bSJavier Cardona 		}
6915ee68e5bSJavier Cardona 	}
6925ee68e5bSJavier Cardona 
69318b27ff7SBob Copeland 	hlist_for_each_entry_rcu(gate, &tbl->known_gates, gate_list) {
694947c2a0eSBob Copeland 		mpath_dbg(sdata, "Sending to %pM\n", gate->dst);
695947c2a0eSBob Copeland 		mesh_path_tx_pending(gate);
6965ee68e5bSJavier Cardona 	}
69760854fd9SBob Copeland 	rcu_read_unlock();
6985ee68e5bSJavier Cardona 
6995ee68e5bSJavier Cardona 	return (from_mpath == mpath) ? -EHOSTUNREACH : 0;
7005ee68e5bSJavier Cardona }
7015ee68e5bSJavier Cardona 
7025ee68e5bSJavier Cardona /**
703eb2b9311SLuis Carlos Cobo  * mesh_path_discard_frame - discard a frame whose path could not be resolved
704eb2b9311SLuis Carlos Cobo  *
705eb2b9311SLuis Carlos Cobo  * @skb: frame to discard
706f698d856SJasper Bryant-Greene  * @sdata: network subif the frame was to be sent through
707eb2b9311SLuis Carlos Cobo  *
708eb2b9311SLuis Carlos Cobo  * Locking: the function must me called within a rcu_read_lock region
709eb2b9311SLuis Carlos Cobo  */
710bf7cd94dSJohannes Berg void mesh_path_discard_frame(struct ieee80211_sub_if_data *sdata,
711bf7cd94dSJohannes Berg 			     struct sk_buff *skb)
712eb2b9311SLuis Carlos Cobo {
713eb2b9311SLuis Carlos Cobo 	kfree_skb(skb);
714472dbc45SJohannes Berg 	sdata->u.mesh.mshstats.dropped_frames_no_route++;
715eb2b9311SLuis Carlos Cobo }
716eb2b9311SLuis Carlos Cobo 
717eb2b9311SLuis Carlos Cobo /**
718eb2b9311SLuis Carlos Cobo  * mesh_path_flush_pending - free the pending queue of a mesh path
719eb2b9311SLuis Carlos Cobo  *
720eb2b9311SLuis Carlos Cobo  * @mpath: mesh path whose queue has to be freed
721eb2b9311SLuis Carlos Cobo  *
72225985edcSLucas De Marchi  * Locking: the function must me called within a rcu_read_lock region
723eb2b9311SLuis Carlos Cobo  */
724eb2b9311SLuis Carlos Cobo void mesh_path_flush_pending(struct mesh_path *mpath)
725eb2b9311SLuis Carlos Cobo {
726eb2b9311SLuis Carlos Cobo 	struct sk_buff *skb;
727eb2b9311SLuis Carlos Cobo 
72800e3f25cSJavier Cardona 	while ((skb = skb_dequeue(&mpath->frame_queue)) != NULL)
729bf7cd94dSJohannes Berg 		mesh_path_discard_frame(mpath->sdata, skb);
730eb2b9311SLuis Carlos Cobo }
731eb2b9311SLuis Carlos Cobo 
732eb2b9311SLuis Carlos Cobo /**
733eb2b9311SLuis Carlos Cobo  * mesh_path_fix_nexthop - force a specific next hop for a mesh path
734eb2b9311SLuis Carlos Cobo  *
735eb2b9311SLuis Carlos Cobo  * @mpath: the mesh path to modify
736eb2b9311SLuis Carlos Cobo  * @next_hop: the next hop to force
737eb2b9311SLuis Carlos Cobo  *
738eb2b9311SLuis Carlos Cobo  * Locking: this function must be called holding mpath->state_lock
739eb2b9311SLuis Carlos Cobo  */
740eb2b9311SLuis Carlos Cobo void mesh_path_fix_nexthop(struct mesh_path *mpath, struct sta_info *next_hop)
741eb2b9311SLuis Carlos Cobo {
742eb2b9311SLuis Carlos Cobo 	spin_lock_bh(&mpath->state_lock);
743eb2b9311SLuis Carlos Cobo 	mesh_path_assign_nexthop(mpath, next_hop);
744d19b3bf6SRui Paulo 	mpath->sn = 0xffff;
745eb2b9311SLuis Carlos Cobo 	mpath->metric = 0;
746eb2b9311SLuis Carlos Cobo 	mpath->hop_count = 0;
747eb2b9311SLuis Carlos Cobo 	mpath->exp_time = 0;
7485df20f21SPedersen, Thomas 	mpath->flags = MESH_PATH_FIXED | MESH_PATH_SN_VALID;
749eb2b9311SLuis Carlos Cobo 	mesh_path_activate(mpath);
750eb2b9311SLuis Carlos Cobo 	spin_unlock_bh(&mpath->state_lock);
7513eb0928fSManoharan, Rajkumar 	ewma_mesh_fail_avg_init(&next_hop->mesh->fail_avg);
7523eb0928fSManoharan, Rajkumar 	/* init it at a low value - 0 start is tricky */
7533eb0928fSManoharan, Rajkumar 	ewma_mesh_fail_avg_add(&next_hop->mesh->fail_avg, 1);
754eb2b9311SLuis Carlos Cobo 	mesh_path_tx_pending(mpath);
755eb2b9311SLuis Carlos Cobo }
756eb2b9311SLuis Carlos Cobo 
757*8b5cb7e4SPavel Skripkin void mesh_pathtbl_init(struct ieee80211_sub_if_data *sdata)
758eb2b9311SLuis Carlos Cobo {
759*8b5cb7e4SPavel Skripkin 	mesh_table_init(&sdata->u.mesh.mesh_paths);
760*8b5cb7e4SPavel Skripkin 	mesh_table_init(&sdata->u.mesh.mpp_paths);
761eb2b9311SLuis Carlos Cobo }
762eb2b9311SLuis Carlos Cobo 
76360854fd9SBob Copeland static
76460854fd9SBob Copeland void mesh_path_tbl_expire(struct ieee80211_sub_if_data *sdata,
76560854fd9SBob Copeland 			  struct mesh_table *tbl)
76660854fd9SBob Copeland {
76760854fd9SBob Copeland 	struct mesh_path *mpath;
768b4c3fbe6SHerbert Xu 	struct hlist_node *n;
76960854fd9SBob Copeland 
770b4c3fbe6SHerbert Xu 	spin_lock_bh(&tbl->walk_lock);
771b4c3fbe6SHerbert Xu 	hlist_for_each_entry_safe(mpath, n, &tbl->walk_head, walk_list) {
77260854fd9SBob Copeland 		if ((!(mpath->flags & MESH_PATH_RESOLVING)) &&
77360854fd9SBob Copeland 		    (!(mpath->flags & MESH_PATH_FIXED)) &&
77460854fd9SBob Copeland 		     time_after(jiffies, mpath->exp_time + MESH_PATH_EXPIRE))
77560854fd9SBob Copeland 			__mesh_path_del(tbl, mpath);
77660854fd9SBob Copeland 	}
777b4c3fbe6SHerbert Xu 	spin_unlock_bh(&tbl->walk_lock);
77860854fd9SBob Copeland }
77960854fd9SBob Copeland 
780f698d856SJasper Bryant-Greene void mesh_path_expire(struct ieee80211_sub_if_data *sdata)
781eb2b9311SLuis Carlos Cobo {
782*8b5cb7e4SPavel Skripkin 	mesh_path_tbl_expire(sdata, &sdata->u.mesh.mesh_paths);
783*8b5cb7e4SPavel Skripkin 	mesh_path_tbl_expire(sdata, &sdata->u.mesh.mpp_paths);
784eb2b9311SLuis Carlos Cobo }
785eb2b9311SLuis Carlos Cobo 
7862bdaf386SBob Copeland void mesh_pathtbl_unregister(struct ieee80211_sub_if_data *sdata)
787eb2b9311SLuis Carlos Cobo {
788*8b5cb7e4SPavel Skripkin 	mesh_table_free(&sdata->u.mesh.mesh_paths);
789*8b5cb7e4SPavel Skripkin 	mesh_table_free(&sdata->u.mesh.mpp_paths);
790eb2b9311SLuis Carlos Cobo }
791