xref: /openbmc/linux/net/mac80211/mesh_pathtbl.c (revision 5e43540c)
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 
5060854fd9SBob Copeland static struct mesh_table *mesh_table_alloc(void)
51349eb8cfSJohannes Berg {
526b86bd62SJohannes Berg 	struct mesh_table *newtbl;
536b86bd62SJohannes Berg 
54d676ff49SJavier Cardona 	newtbl = kmalloc(sizeof(struct mesh_table), GFP_ATOMIC);
556b86bd62SJohannes Berg 	if (!newtbl)
566b86bd62SJohannes Berg 		return NULL;
576b86bd62SJohannes Berg 
5818b27ff7SBob Copeland 	INIT_HLIST_HEAD(&newtbl->known_gates);
59b4c3fbe6SHerbert Xu 	INIT_HLIST_HEAD(&newtbl->walk_head);
606b86bd62SJohannes Berg 	atomic_set(&newtbl->entries,  0);
615ee68e5bSJavier Cardona 	spin_lock_init(&newtbl->gates_lock);
62b4c3fbe6SHerbert Xu 	spin_lock_init(&newtbl->walk_lock);
636b86bd62SJohannes Berg 
646b86bd62SJohannes Berg 	return newtbl;
656b86bd62SJohannes Berg }
666b86bd62SJohannes Berg 
6760854fd9SBob Copeland static void mesh_table_free(struct mesh_table *tbl)
6818889231SJavier Cardona {
6960854fd9SBob Copeland 	rhashtable_free_and_destroy(&tbl->rhead,
7074932959SBob Copeland 				    mesh_path_rht_free, tbl);
7118889231SJavier Cardona 	kfree(tbl);
7218889231SJavier Cardona }
7318889231SJavier Cardona 
74eb2b9311SLuis Carlos Cobo /**
75eb2b9311SLuis Carlos Cobo  *
76eb2b9311SLuis Carlos Cobo  * mesh_path_assign_nexthop - update mesh path next hop
77eb2b9311SLuis Carlos Cobo  *
78eb2b9311SLuis Carlos Cobo  * @mpath: mesh path to update
79eb2b9311SLuis Carlos Cobo  * @sta: next hop to assign
80eb2b9311SLuis Carlos Cobo  *
81eb2b9311SLuis Carlos Cobo  * Locking: mpath->state_lock must be held when calling this function
82eb2b9311SLuis Carlos Cobo  */
83eb2b9311SLuis Carlos Cobo void mesh_path_assign_nexthop(struct mesh_path *mpath, struct sta_info *sta)
84eb2b9311SLuis Carlos Cobo {
8510c836d7SJavier Cardona 	struct sk_buff *skb;
8610c836d7SJavier Cardona 	struct ieee80211_hdr *hdr;
8710c836d7SJavier Cardona 	unsigned long flags;
8810c836d7SJavier Cardona 
89d0709a65SJohannes Berg 	rcu_assign_pointer(mpath->next_hop, sta);
9010c836d7SJavier Cardona 
9110c836d7SJavier Cardona 	spin_lock_irqsave(&mpath->frame_queue.lock, flags);
92b22bd522SThomas Pedersen 	skb_queue_walk(&mpath->frame_queue, skb) {
9310c836d7SJavier Cardona 		hdr = (struct ieee80211_hdr *) skb->data;
9410c836d7SJavier Cardona 		memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN);
957e3c8866SThomas Pedersen 		memcpy(hdr->addr2, mpath->sdata->vif.addr, ETH_ALEN);
963f52b7e3SMarco Porsch 		ieee80211_mps_set_frame_flags(sta->sdata, sta, hdr);
9710c836d7SJavier Cardona 	}
9810c836d7SJavier Cardona 
9910c836d7SJavier Cardona 	spin_unlock_irqrestore(&mpath->frame_queue.lock, flags);
100eb2b9311SLuis Carlos Cobo }
101eb2b9311SLuis Carlos Cobo 
1025ee68e5bSJavier Cardona static void prepare_for_gate(struct sk_buff *skb, char *dst_addr,
1035ee68e5bSJavier Cardona 			     struct mesh_path *gate_mpath)
1045ee68e5bSJavier Cardona {
1055ee68e5bSJavier Cardona 	struct ieee80211_hdr *hdr;
1065ee68e5bSJavier Cardona 	struct ieee80211s_hdr *mshdr;
1075ee68e5bSJavier Cardona 	int mesh_hdrlen, hdrlen;
1085ee68e5bSJavier Cardona 	char *next_hop;
1095ee68e5bSJavier Cardona 
1105ee68e5bSJavier Cardona 	hdr = (struct ieee80211_hdr *) skb->data;
1115ee68e5bSJavier Cardona 	hdrlen = ieee80211_hdrlen(hdr->frame_control);
1125ee68e5bSJavier Cardona 	mshdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
1135ee68e5bSJavier Cardona 
1145ee68e5bSJavier Cardona 	if (!(mshdr->flags & MESH_FLAGS_AE)) {
1155ee68e5bSJavier Cardona 		/* size of the fixed part of the mesh header */
1165ee68e5bSJavier Cardona 		mesh_hdrlen = 6;
1175ee68e5bSJavier Cardona 
1185ee68e5bSJavier Cardona 		/* make room for the two extended addresses */
1195ee68e5bSJavier Cardona 		skb_push(skb, 2 * ETH_ALEN);
1205ee68e5bSJavier Cardona 		memmove(skb->data, hdr, hdrlen + mesh_hdrlen);
1215ee68e5bSJavier Cardona 
1225ee68e5bSJavier Cardona 		hdr = (struct ieee80211_hdr *) skb->data;
1235ee68e5bSJavier Cardona 
1245ee68e5bSJavier Cardona 		/* we preserve the previous mesh header and only add
1255ee68e5bSJavier Cardona 		 * the new addreses */
1265ee68e5bSJavier Cardona 		mshdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
1275ee68e5bSJavier Cardona 		mshdr->flags = MESH_FLAGS_AE_A5_A6;
1285ee68e5bSJavier Cardona 		memcpy(mshdr->eaddr1, hdr->addr3, ETH_ALEN);
1295ee68e5bSJavier Cardona 		memcpy(mshdr->eaddr2, hdr->addr4, ETH_ALEN);
1305ee68e5bSJavier Cardona 	}
1315ee68e5bSJavier Cardona 
1325ee68e5bSJavier Cardona 	/* update next hop */
1335ee68e5bSJavier Cardona 	hdr = (struct ieee80211_hdr *) skb->data;
1345ee68e5bSJavier Cardona 	rcu_read_lock();
1355ee68e5bSJavier Cardona 	next_hop = rcu_dereference(gate_mpath->next_hop)->sta.addr;
1365ee68e5bSJavier Cardona 	memcpy(hdr->addr1, next_hop, ETH_ALEN);
1375ee68e5bSJavier Cardona 	rcu_read_unlock();
1387e3c8866SThomas Pedersen 	memcpy(hdr->addr2, gate_mpath->sdata->vif.addr, ETH_ALEN);
1395ee68e5bSJavier Cardona 	memcpy(hdr->addr3, dst_addr, ETH_ALEN);
1405ee68e5bSJavier Cardona }
1415ee68e5bSJavier Cardona 
1425ee68e5bSJavier Cardona /**
1435ee68e5bSJavier Cardona  *
1445ee68e5bSJavier Cardona  * mesh_path_move_to_queue - Move or copy frames from one mpath queue to another
1455ee68e5bSJavier Cardona  *
1465ee68e5bSJavier Cardona  * This function is used to transfer or copy frames from an unresolved mpath to
1475ee68e5bSJavier Cardona  * a gate mpath.  The function also adds the Address Extension field and
1485ee68e5bSJavier Cardona  * updates the next hop.
1495ee68e5bSJavier Cardona  *
1505ee68e5bSJavier Cardona  * If a frame already has an Address Extension field, only the next hop and
1515ee68e5bSJavier Cardona  * destination addresses are updated.
1525ee68e5bSJavier Cardona  *
1535ee68e5bSJavier Cardona  * The gate mpath must be an active mpath with a valid mpath->next_hop.
1545ee68e5bSJavier Cardona  *
1555ee68e5bSJavier Cardona  * @mpath: An active mpath the frames will be sent to (i.e. the gate)
1565ee68e5bSJavier Cardona  * @from_mpath: The failed mpath
1575ee68e5bSJavier Cardona  * @copy: When true, copy all the frames to the new mpath queue.  When false,
1585ee68e5bSJavier Cardona  * move them.
1595ee68e5bSJavier Cardona  */
1605ee68e5bSJavier Cardona static void mesh_path_move_to_queue(struct mesh_path *gate_mpath,
1615ee68e5bSJavier Cardona 				    struct mesh_path *from_mpath,
1625ee68e5bSJavier Cardona 				    bool copy)
1635ee68e5bSJavier Cardona {
1644bd4c2ddSThomas Pedersen 	struct sk_buff *skb, *fskb, *tmp;
1654bd4c2ddSThomas Pedersen 	struct sk_buff_head failq;
1665ee68e5bSJavier Cardona 	unsigned long flags;
1675ee68e5bSJavier Cardona 
1688c5bb1faSJohannes Berg 	if (WARN_ON(gate_mpath == from_mpath))
1698c5bb1faSJohannes Berg 		return;
1708c5bb1faSJohannes Berg 	if (WARN_ON(!gate_mpath->next_hop))
1718c5bb1faSJohannes Berg 		return;
1725ee68e5bSJavier Cardona 
1735ee68e5bSJavier Cardona 	__skb_queue_head_init(&failq);
1745ee68e5bSJavier Cardona 
1755ee68e5bSJavier Cardona 	spin_lock_irqsave(&from_mpath->frame_queue.lock, flags);
1765ee68e5bSJavier Cardona 	skb_queue_splice_init(&from_mpath->frame_queue, &failq);
1775ee68e5bSJavier Cardona 	spin_unlock_irqrestore(&from_mpath->frame_queue.lock, flags);
1785ee68e5bSJavier Cardona 
1794bd4c2ddSThomas Pedersen 	skb_queue_walk_safe(&failq, fskb, tmp) {
1804bd4c2ddSThomas Pedersen 		if (skb_queue_len(&gate_mpath->frame_queue) >=
1814bd4c2ddSThomas Pedersen 				  MESH_FRAME_QUEUE_LEN) {
1824bd4c2ddSThomas Pedersen 			mpath_dbg(gate_mpath->sdata, "mpath queue full!\n");
1834bd4c2ddSThomas Pedersen 			break;
184817a53d9SJohn W. Linville 		}
1855ee68e5bSJavier Cardona 
1864bd4c2ddSThomas Pedersen 		skb = skb_copy(fskb, GFP_ATOMIC);
1874bd4c2ddSThomas Pedersen 		if (WARN_ON(!skb))
1884bd4c2ddSThomas Pedersen 			break;
1894bd4c2ddSThomas Pedersen 
1905ee68e5bSJavier Cardona 		prepare_for_gate(skb, gate_mpath->dst, gate_mpath);
1914bd4c2ddSThomas Pedersen 		skb_queue_tail(&gate_mpath->frame_queue, skb);
1924bd4c2ddSThomas Pedersen 
1934bd4c2ddSThomas Pedersen 		if (copy)
1944bd4c2ddSThomas Pedersen 			continue;
1954bd4c2ddSThomas Pedersen 
1964bd4c2ddSThomas Pedersen 		__skb_unlink(fskb, &failq);
1974bd4c2ddSThomas Pedersen 		kfree_skb(fskb);
1985ee68e5bSJavier Cardona 	}
1995ee68e5bSJavier Cardona 
200bdcbd8e0SJohannes Berg 	mpath_dbg(gate_mpath->sdata, "Mpath queue for gate %pM has %d frames\n",
201bdcbd8e0SJohannes Berg 		  gate_mpath->dst, skb_queue_len(&gate_mpath->frame_queue));
2025ee68e5bSJavier Cardona 
2035ee68e5bSJavier Cardona 	if (!copy)
2045ee68e5bSJavier Cardona 		return;
2055ee68e5bSJavier Cardona 
2065ee68e5bSJavier Cardona 	spin_lock_irqsave(&from_mpath->frame_queue.lock, flags);
2075ee68e5bSJavier Cardona 	skb_queue_splice(&failq, &from_mpath->frame_queue);
2085ee68e5bSJavier Cardona 	spin_unlock_irqrestore(&from_mpath->frame_queue.lock, flags);
2095ee68e5bSJavier Cardona }
2105ee68e5bSJavier Cardona 
211eb2b9311SLuis Carlos Cobo 
2124a3cb702SJohannes Berg static struct mesh_path *mpath_lookup(struct mesh_table *tbl, const u8 *dst,
213239289e4SJavier Cardona 				      struct ieee80211_sub_if_data *sdata)
214239289e4SJavier Cardona {
215239289e4SJavier Cardona 	struct mesh_path *mpath;
216239289e4SJavier Cardona 
217ef618b1bSFelix Fietkau 	mpath = rhashtable_lookup(&tbl->rhead, dst, mesh_rht_params);
21860854fd9SBob Copeland 
21960854fd9SBob Copeland 	if (mpath && mpath_expired(mpath)) {
220239289e4SJavier Cardona 		spin_lock_bh(&mpath->state_lock);
221239289e4SJavier Cardona 		mpath->flags &= ~MESH_PATH_ACTIVE;
222239289e4SJavier Cardona 		spin_unlock_bh(&mpath->state_lock);
223239289e4SJavier Cardona 	}
224239289e4SJavier Cardona 	return mpath;
225239289e4SJavier Cardona }
226239289e4SJavier Cardona 
227eb2b9311SLuis Carlos Cobo /**
228eb2b9311SLuis Carlos Cobo  * mesh_path_lookup - look up a path in the mesh path table
229f698d856SJasper Bryant-Greene  * @sdata: local subif
230bf7cd94dSJohannes Berg  * @dst: hardware address (ETH_ALEN length) of destination
231eb2b9311SLuis Carlos Cobo  *
232eb2b9311SLuis Carlos Cobo  * Returns: pointer to the mesh path structure, or NULL if not found
233eb2b9311SLuis Carlos Cobo  *
234eb2b9311SLuis Carlos Cobo  * Locking: must be called within a read rcu section.
235eb2b9311SLuis Carlos Cobo  */
236bf7cd94dSJohannes Berg struct mesh_path *
237bf7cd94dSJohannes Berg mesh_path_lookup(struct ieee80211_sub_if_data *sdata, const u8 *dst)
238eb2b9311SLuis Carlos Cobo {
23960854fd9SBob Copeland 	return mpath_lookup(sdata->u.mesh.mesh_paths, dst, sdata);
240eb2b9311SLuis Carlos Cobo }
241eb2b9311SLuis Carlos Cobo 
242bf7cd94dSJohannes Berg struct mesh_path *
243bf7cd94dSJohannes Berg mpp_path_lookup(struct ieee80211_sub_if_data *sdata, const u8 *dst)
24479617deeSYanBo {
24560854fd9SBob Copeland 	return mpath_lookup(sdata->u.mesh.mpp_paths, dst, sdata);
24679617deeSYanBo }
24779617deeSYanBo 
24860854fd9SBob Copeland static struct mesh_path *
24960854fd9SBob Copeland __mesh_path_lookup_by_idx(struct mesh_table *tbl, int idx)
25060854fd9SBob Copeland {
251b4c3fbe6SHerbert Xu 	int i = 0;
252b4c3fbe6SHerbert Xu 	struct mesh_path *mpath;
25360854fd9SBob Copeland 
254b4c3fbe6SHerbert Xu 	hlist_for_each_entry_rcu(mpath, &tbl->walk_head, walk_list) {
25560854fd9SBob Copeland 		if (i++ == idx)
25660854fd9SBob Copeland 			break;
25760854fd9SBob Copeland 	}
25860854fd9SBob Copeland 
259b4c3fbe6SHerbert Xu 	if (!mpath)
26060854fd9SBob Copeland 		return NULL;
26160854fd9SBob Copeland 
26260854fd9SBob Copeland 	if (mpath_expired(mpath)) {
26360854fd9SBob Copeland 		spin_lock_bh(&mpath->state_lock);
26460854fd9SBob Copeland 		mpath->flags &= ~MESH_PATH_ACTIVE;
26560854fd9SBob Copeland 		spin_unlock_bh(&mpath->state_lock);
26660854fd9SBob Copeland 	}
26760854fd9SBob Copeland 	return mpath;
26860854fd9SBob Copeland }
26979617deeSYanBo 
270eb2b9311SLuis Carlos Cobo /**
271eb2b9311SLuis Carlos Cobo  * mesh_path_lookup_by_idx - look up a path in the mesh path table by its index
272eb2b9311SLuis Carlos Cobo  * @idx: index
273f698d856SJasper Bryant-Greene  * @sdata: local subif, or NULL for all entries
274eb2b9311SLuis Carlos Cobo  *
275eb2b9311SLuis Carlos Cobo  * Returns: pointer to the mesh path structure, or NULL if not found.
276eb2b9311SLuis Carlos Cobo  *
277eb2b9311SLuis Carlos Cobo  * Locking: must be called within a read rcu section.
278eb2b9311SLuis Carlos Cobo  */
279bf7cd94dSJohannes Berg struct mesh_path *
280bf7cd94dSJohannes Berg mesh_path_lookup_by_idx(struct ieee80211_sub_if_data *sdata, int idx)
281eb2b9311SLuis Carlos Cobo {
28260854fd9SBob Copeland 	return __mesh_path_lookup_by_idx(sdata->u.mesh.mesh_paths, idx);
283eb2b9311SLuis Carlos Cobo }
284eb2b9311SLuis Carlos Cobo 
2855ee68e5bSJavier Cardona /**
286a2db2ed3SHenning Rogge  * mpp_path_lookup_by_idx - look up a path in the proxy path table by its index
287a2db2ed3SHenning Rogge  * @idx: index
288a2db2ed3SHenning Rogge  * @sdata: local subif, or NULL for all entries
289a2db2ed3SHenning Rogge  *
290a2db2ed3SHenning Rogge  * Returns: pointer to the proxy path structure, or NULL if not found.
291a2db2ed3SHenning Rogge  *
292a2db2ed3SHenning Rogge  * Locking: must be called within a read rcu section.
293a2db2ed3SHenning Rogge  */
294a2db2ed3SHenning Rogge struct mesh_path *
295a2db2ed3SHenning Rogge mpp_path_lookup_by_idx(struct ieee80211_sub_if_data *sdata, int idx)
296a2db2ed3SHenning Rogge {
29760854fd9SBob Copeland 	return __mesh_path_lookup_by_idx(sdata->u.mesh.mpp_paths, idx);
298a2db2ed3SHenning Rogge }
299a2db2ed3SHenning Rogge 
300a2db2ed3SHenning Rogge /**
30130be52e4SJohannes Berg  * mesh_path_add_gate - add the given mpath to a mesh gate to our path table
30230be52e4SJohannes Berg  * @mpath: gate path to add to table
3035ee68e5bSJavier Cardona  */
30430be52e4SJohannes Berg int mesh_path_add_gate(struct mesh_path *mpath)
3055ee68e5bSJavier Cardona {
30630be52e4SJohannes Berg 	struct mesh_table *tbl;
3075ee68e5bSJavier Cardona 	int err;
3085ee68e5bSJavier Cardona 
3095ee68e5bSJavier Cardona 	rcu_read_lock();
31060854fd9SBob Copeland 	tbl = mpath->sdata->u.mesh.mesh_paths;
3115ee68e5bSJavier Cardona 
312947c2a0eSBob Copeland 	spin_lock_bh(&mpath->state_lock);
313947c2a0eSBob Copeland 	if (mpath->is_gate) {
3145ee68e5bSJavier Cardona 		err = -EEXIST;
315947c2a0eSBob Copeland 		spin_unlock_bh(&mpath->state_lock);
3165ee68e5bSJavier Cardona 		goto err_rcu;
3175ee68e5bSJavier Cardona 	}
3185ee68e5bSJavier Cardona 	mpath->is_gate = true;
3195ee68e5bSJavier Cardona 	mpath->sdata->u.mesh.num_gates++;
320947c2a0eSBob Copeland 
321947c2a0eSBob Copeland 	spin_lock(&tbl->gates_lock);
32218b27ff7SBob Copeland 	hlist_add_head_rcu(&mpath->gate_list, &tbl->known_gates);
323947c2a0eSBob Copeland 	spin_unlock(&tbl->gates_lock);
324947c2a0eSBob Copeland 
325947c2a0eSBob Copeland 	spin_unlock_bh(&mpath->state_lock);
326947c2a0eSBob Copeland 
327bdcbd8e0SJohannes Berg 	mpath_dbg(mpath->sdata,
328bdcbd8e0SJohannes Berg 		  "Mesh path: Recorded new gate: %pM. %d known gates\n",
329bdcbd8e0SJohannes Berg 		  mpath->dst, mpath->sdata->u.mesh.num_gates);
330bf7cd94dSJohannes Berg 	err = 0;
3315ee68e5bSJavier Cardona err_rcu:
3325ee68e5bSJavier Cardona 	rcu_read_unlock();
3335ee68e5bSJavier Cardona 	return err;
3345ee68e5bSJavier Cardona }
3355ee68e5bSJavier Cardona 
3365ee68e5bSJavier Cardona /**
3375ee68e5bSJavier Cardona  * mesh_gate_del - remove a mesh gate from the list of known gates
3385ee68e5bSJavier Cardona  * @tbl: table which holds our list of known gates
3395ee68e5bSJavier Cardona  * @mpath: gate mpath
3405ee68e5bSJavier Cardona  */
341bf7cd94dSJohannes Berg static void mesh_gate_del(struct mesh_table *tbl, struct mesh_path *mpath)
3425ee68e5bSJavier Cardona {
343947c2a0eSBob Copeland 	lockdep_assert_held(&mpath->state_lock);
344947c2a0eSBob Copeland 	if (!mpath->is_gate)
345947c2a0eSBob Copeland 		return;
3465ee68e5bSJavier Cardona 
3475ee68e5bSJavier Cardona 	mpath->is_gate = false;
348947c2a0eSBob Copeland 	spin_lock_bh(&tbl->gates_lock);
349947c2a0eSBob Copeland 	hlist_del_rcu(&mpath->gate_list);
350947c2a0eSBob Copeland 	mpath->sdata->u.mesh.num_gates--;
351947c2a0eSBob Copeland 	spin_unlock_bh(&tbl->gates_lock);
352947c2a0eSBob Copeland 
353bdcbd8e0SJohannes Berg 	mpath_dbg(mpath->sdata,
354bdcbd8e0SJohannes Berg 		  "Mesh path: Deleted gate: %pM. %d known gates\n",
3555ee68e5bSJavier Cardona 		  mpath->dst, mpath->sdata->u.mesh.num_gates);
3565ee68e5bSJavier Cardona }
3575ee68e5bSJavier Cardona 
3585ee68e5bSJavier Cardona /**
3595ee68e5bSJavier Cardona  * mesh_gate_num - number of gates known to this interface
3605ee68e5bSJavier Cardona  * @sdata: subif data
3615ee68e5bSJavier Cardona  */
3625ee68e5bSJavier Cardona int mesh_gate_num(struct ieee80211_sub_if_data *sdata)
3635ee68e5bSJavier Cardona {
3645ee68e5bSJavier Cardona 	return sdata->u.mesh.num_gates;
3655ee68e5bSJavier Cardona }
3665ee68e5bSJavier Cardona 
367b15dc38bSBob Copeland static
368b15dc38bSBob Copeland struct mesh_path *mesh_path_new(struct ieee80211_sub_if_data *sdata,
369b15dc38bSBob Copeland 				const u8 *dst, gfp_t gfp_flags)
370b15dc38bSBob Copeland {
371b15dc38bSBob Copeland 	struct mesh_path *new_mpath;
372b15dc38bSBob Copeland 
373b15dc38bSBob Copeland 	new_mpath = kzalloc(sizeof(struct mesh_path), gfp_flags);
374b15dc38bSBob Copeland 	if (!new_mpath)
375b15dc38bSBob Copeland 		return NULL;
376b15dc38bSBob Copeland 
377b15dc38bSBob Copeland 	memcpy(new_mpath->dst, dst, ETH_ALEN);
378b15dc38bSBob Copeland 	eth_broadcast_addr(new_mpath->rann_snd_addr);
379b15dc38bSBob Copeland 	new_mpath->is_root = false;
380b15dc38bSBob Copeland 	new_mpath->sdata = sdata;
381b15dc38bSBob Copeland 	new_mpath->flags = 0;
382b15dc38bSBob Copeland 	skb_queue_head_init(&new_mpath->frame_queue);
383b15dc38bSBob Copeland 	new_mpath->exp_time = jiffies;
384b15dc38bSBob Copeland 	spin_lock_init(&new_mpath->state_lock);
38534f11cd3SKees Cook 	timer_setup(&new_mpath->timer, mesh_path_timer, 0);
386b15dc38bSBob Copeland 
387b15dc38bSBob Copeland 	return new_mpath;
388b15dc38bSBob Copeland }
389b15dc38bSBob Copeland 
390eb2b9311SLuis Carlos Cobo /**
391eb2b9311SLuis Carlos Cobo  * mesh_path_add - allocate and add a new path to the mesh path table
392bf7cd94dSJohannes Berg  * @dst: destination address of the path (ETH_ALEN length)
393f698d856SJasper Bryant-Greene  * @sdata: local subif
394eb2b9311SLuis Carlos Cobo  *
395af901ca1SAndré Goddard Rosa  * Returns: 0 on success
396eb2b9311SLuis Carlos Cobo  *
397eb2b9311SLuis Carlos Cobo  * State: the initial state of the new path is set to 0
398eb2b9311SLuis Carlos Cobo  */
399ae76eef0SBob Copeland struct mesh_path *mesh_path_add(struct ieee80211_sub_if_data *sdata,
400ae76eef0SBob Copeland 				const u8 *dst)
401eb2b9311SLuis Carlos Cobo {
402349eb8cfSJohannes Berg 	struct mesh_table *tbl;
403eb2b9311SLuis Carlos Cobo 	struct mesh_path *mpath, *new_mpath;
404eb2b9311SLuis Carlos Cobo 
405b203ca39SJoe Perches 	if (ether_addr_equal(dst, sdata->vif.addr))
406eb2b9311SLuis Carlos Cobo 		/* never add ourselves as neighbours */
407ae76eef0SBob Copeland 		return ERR_PTR(-ENOTSUPP);
408eb2b9311SLuis Carlos Cobo 
409eb2b9311SLuis Carlos Cobo 	if (is_multicast_ether_addr(dst))
410ae76eef0SBob Copeland 		return ERR_PTR(-ENOTSUPP);
411eb2b9311SLuis Carlos Cobo 
412472dbc45SJohannes Berg 	if (atomic_add_unless(&sdata->u.mesh.mpaths, 1, MESH_MAX_MPATHS) == 0)
413ae76eef0SBob Copeland 		return ERR_PTR(-ENOSPC);
414ae76eef0SBob Copeland 
415b15dc38bSBob Copeland 	new_mpath = mesh_path_new(sdata, dst, GFP_ATOMIC);
416402d7752SPavel Emelyanov 	if (!new_mpath)
41760854fd9SBob Copeland 		return ERR_PTR(-ENOMEM);
418402d7752SPavel Emelyanov 
41960854fd9SBob Copeland 	tbl = sdata->u.mesh.mesh_paths;
420b4c3fbe6SHerbert Xu 	spin_lock_bh(&tbl->walk_lock);
42136922931SHerbert Xu 	mpath = rhashtable_lookup_get_insert_fast(&tbl->rhead,
42260854fd9SBob Copeland 						  &new_mpath->rhash,
42360854fd9SBob Copeland 						  mesh_rht_params);
42436922931SHerbert Xu 	if (!mpath)
425b4c3fbe6SHerbert Xu 		hlist_add_head(&new_mpath->walk_list, &tbl->walk_head);
426b4c3fbe6SHerbert Xu 	spin_unlock_bh(&tbl->walk_lock);
427f5ea9120SJohannes Berg 
42836922931SHerbert Xu 	if (mpath) {
4294ff3a9d1SHerbert Xu 		kfree(new_mpath);
4304ff3a9d1SHerbert Xu 
43136922931SHerbert Xu 		if (IS_ERR(mpath))
43236922931SHerbert Xu 			return mpath;
433ae76eef0SBob Copeland 
43460854fd9SBob Copeland 		new_mpath = mpath;
435eb2b9311SLuis Carlos Cobo 	}
4364ff3a9d1SHerbert Xu 
43760854fd9SBob Copeland 	sdata->u.mesh.mesh_paths_generation++;
43860854fd9SBob Copeland 	return new_mpath;
43918889231SJavier Cardona }
440eb2b9311SLuis Carlos Cobo 
441bf7cd94dSJohannes Berg int mpp_path_add(struct ieee80211_sub_if_data *sdata,
442bf7cd94dSJohannes Berg 		 const u8 *dst, const u8 *mpp)
44379617deeSYanBo {
444349eb8cfSJohannes Berg 	struct mesh_table *tbl;
44560854fd9SBob Copeland 	struct mesh_path *new_mpath;
44660854fd9SBob Copeland 	int ret;
44779617deeSYanBo 
448b203ca39SJoe Perches 	if (ether_addr_equal(dst, sdata->vif.addr))
44979617deeSYanBo 		/* never add ourselves as neighbours */
45079617deeSYanBo 		return -ENOTSUPP;
45179617deeSYanBo 
45279617deeSYanBo 	if (is_multicast_ether_addr(dst))
45379617deeSYanBo 		return -ENOTSUPP;
45479617deeSYanBo 
455b15dc38bSBob Copeland 	new_mpath = mesh_path_new(sdata, dst, GFP_ATOMIC);
45679617deeSYanBo 
45760854fd9SBob Copeland 	if (!new_mpath)
45860854fd9SBob Copeland 		return -ENOMEM;
45979617deeSYanBo 
46079617deeSYanBo 	memcpy(new_mpath->mpp, mpp, ETH_ALEN);
46160854fd9SBob Copeland 	tbl = sdata->u.mesh.mpp_paths;
462b4c3fbe6SHerbert Xu 
463b4c3fbe6SHerbert Xu 	spin_lock_bh(&tbl->walk_lock);
46460854fd9SBob Copeland 	ret = rhashtable_lookup_insert_fast(&tbl->rhead,
46560854fd9SBob Copeland 					    &new_mpath->rhash,
46660854fd9SBob Copeland 					    mesh_rht_params);
467b4c3fbe6SHerbert Xu 	if (!ret)
468b4c3fbe6SHerbert Xu 		hlist_add_head_rcu(&new_mpath->walk_list, &tbl->walk_head);
469b4c3fbe6SHerbert Xu 	spin_unlock_bh(&tbl->walk_lock);
470a2db2ed3SHenning Rogge 
4714ff3a9d1SHerbert Xu 	if (ret)
4724ff3a9d1SHerbert Xu 		kfree(new_mpath);
4734ff3a9d1SHerbert Xu 
4742bdaf386SBob Copeland 	sdata->u.mesh.mpp_paths_generation++;
47560854fd9SBob Copeland 	return ret;
47679617deeSYanBo }
47779617deeSYanBo 
47879617deeSYanBo 
479eb2b9311SLuis Carlos Cobo /**
480eb2b9311SLuis Carlos Cobo  * mesh_plink_broken - deactivates paths and sends perr when a link breaks
481eb2b9311SLuis Carlos Cobo  *
482eb2b9311SLuis Carlos Cobo  * @sta: broken peer link
483eb2b9311SLuis Carlos Cobo  *
484eb2b9311SLuis Carlos Cobo  * This function must be called from the rate control algorithm if enough
485eb2b9311SLuis Carlos Cobo  * delivery errors suggest that a peer link is no longer usable.
486eb2b9311SLuis Carlos Cobo  */
487eb2b9311SLuis Carlos Cobo void mesh_plink_broken(struct sta_info *sta)
488eb2b9311SLuis Carlos Cobo {
48960854fd9SBob Copeland 	struct ieee80211_sub_if_data *sdata = sta->sdata;
49060854fd9SBob Copeland 	struct mesh_table *tbl = sdata->u.mesh.mesh_paths;
49115ff6365SJohannes Berg 	static const u8 bcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
492eb2b9311SLuis Carlos Cobo 	struct mesh_path *mpath;
493eb2b9311SLuis Carlos Cobo 
494b4c3fbe6SHerbert Xu 	rcu_read_lock();
495b4c3fbe6SHerbert Xu 	hlist_for_each_entry_rcu(mpath, &tbl->walk_head, walk_list) {
4962688eba9SAndreea-Cristina Bernat 		if (rcu_access_pointer(mpath->next_hop) == sta &&
497eb2b9311SLuis Carlos Cobo 		    mpath->flags & MESH_PATH_ACTIVE &&
498eb2b9311SLuis Carlos Cobo 		    !(mpath->flags & MESH_PATH_FIXED)) {
499f5e50cd0SJavier Cardona 			spin_lock_bh(&mpath->state_lock);
500eb2b9311SLuis Carlos Cobo 			mpath->flags &= ~MESH_PATH_ACTIVE;
501d19b3bf6SRui Paulo 			++mpath->sn;
502eb2b9311SLuis Carlos Cobo 			spin_unlock_bh(&mpath->state_lock);
503bf7cd94dSJohannes Berg 			mesh_path_error_tx(sdata,
504bf7cd94dSJohannes Berg 				sdata->u.mesh.mshcfg.element_ttl,
505f63f8421SChun-Yeow Yeoh 				mpath->dst, mpath->sn,
506f63f8421SChun-Yeow Yeoh 				WLAN_REASON_MESH_PATH_DEST_UNREACHABLE, bcast);
507f5e50cd0SJavier Cardona 		}
508eb2b9311SLuis Carlos Cobo 	}
509b4c3fbe6SHerbert Xu 	rcu_read_unlock();
510eb2b9311SLuis Carlos Cobo }
511eb2b9311SLuis Carlos Cobo 
51274932959SBob Copeland static void mesh_path_free_rcu(struct mesh_table *tbl,
51374932959SBob Copeland 			       struct mesh_path *mpath)
51419c50b3dSJavier Cardona {
51560854fd9SBob Copeland 	struct ieee80211_sub_if_data *sdata = mpath->sdata;
51619c50b3dSJavier Cardona 
517947c2a0eSBob Copeland 	spin_lock_bh(&mpath->state_lock);
51874932959SBob Copeland 	mpath->flags |= MESH_PATH_RESOLVING | MESH_PATH_DELETED;
51919c50b3dSJavier Cardona 	mesh_gate_del(tbl, mpath);
520947c2a0eSBob Copeland 	spin_unlock_bh(&mpath->state_lock);
52174932959SBob Copeland 	del_timer_sync(&mpath->timer);
522c2e703a5SJohannes Berg 	atomic_dec(&sdata->u.mesh.mpaths);
52319c50b3dSJavier Cardona 	atomic_dec(&tbl->entries);
5245e43540cSRemi Pommarel 	mesh_path_flush_pending(mpath);
52574932959SBob Copeland 	kfree_rcu(mpath, rcu);
52674932959SBob Copeland }
52774932959SBob Copeland 
52874932959SBob Copeland static void __mesh_path_del(struct mesh_table *tbl, struct mesh_path *mpath)
52974932959SBob Copeland {
530b4c3fbe6SHerbert Xu 	hlist_del_rcu(&mpath->walk_list);
53174932959SBob Copeland 	rhashtable_remove_fast(&tbl->rhead, &mpath->rhash, mesh_rht_params);
53274932959SBob Copeland 	mesh_path_free_rcu(tbl, mpath);
53319c50b3dSJavier Cardona }
53419c50b3dSJavier Cardona 
535eb2b9311SLuis Carlos Cobo /**
536eb2b9311SLuis Carlos Cobo  * mesh_path_flush_by_nexthop - Deletes mesh paths if their next hop matches
537eb2b9311SLuis Carlos Cobo  *
5382c53040fSBen Hutchings  * @sta: mesh peer to match
539eb2b9311SLuis Carlos Cobo  *
540b4e08ea1SLuis Carlos Cobo  * RCU notes: this function is called when a mesh plink transitions from
541b4e08ea1SLuis Carlos Cobo  * PLINK_ESTAB to any other state, since PLINK_ESTAB state is the only one that
542b4e08ea1SLuis Carlos Cobo  * allows path creation. This will happen before the sta can be freed (because
543d0709a65SJohannes Berg  * sta_info_destroy() calls this) so any reader in a rcu read block will be
544d0709a65SJohannes Berg  * protected against the plink disappearing.
545eb2b9311SLuis Carlos Cobo  */
546eb2b9311SLuis Carlos Cobo void mesh_path_flush_by_nexthop(struct sta_info *sta)
547eb2b9311SLuis Carlos Cobo {
5482bdaf386SBob Copeland 	struct ieee80211_sub_if_data *sdata = sta->sdata;
54960854fd9SBob Copeland 	struct mesh_table *tbl = sdata->u.mesh.mesh_paths;
550eb2b9311SLuis Carlos Cobo 	struct mesh_path *mpath;
551b4c3fbe6SHerbert Xu 	struct hlist_node *n;
552eb2b9311SLuis Carlos Cobo 
553b4c3fbe6SHerbert Xu 	spin_lock_bh(&tbl->walk_lock);
554b4c3fbe6SHerbert Xu 	hlist_for_each_entry_safe(mpath, n, &tbl->walk_head, walk_list) {
55560854fd9SBob Copeland 		if (rcu_access_pointer(mpath->next_hop) == sta)
55660854fd9SBob Copeland 			__mesh_path_del(tbl, mpath);
557eb2b9311SLuis Carlos Cobo 	}
558b4c3fbe6SHerbert Xu 	spin_unlock_bh(&tbl->walk_lock);
559eb2b9311SLuis Carlos Cobo }
560eb2b9311SLuis Carlos Cobo 
561bf5a70e1SHenning Rogge static void mpp_flush_by_proxy(struct ieee80211_sub_if_data *sdata,
562bf5a70e1SHenning Rogge 			       const u8 *proxy)
563bf5a70e1SHenning Rogge {
56460854fd9SBob Copeland 	struct mesh_table *tbl = sdata->u.mesh.mpp_paths;
56560854fd9SBob Copeland 	struct mesh_path *mpath;
566b4c3fbe6SHerbert Xu 	struct hlist_node *n;
567bf5a70e1SHenning Rogge 
568b4c3fbe6SHerbert Xu 	spin_lock_bh(&tbl->walk_lock);
569b4c3fbe6SHerbert Xu 	hlist_for_each_entry_safe(mpath, n, &tbl->walk_head, walk_list) {
57060854fd9SBob Copeland 		if (ether_addr_equal(mpath->mpp, proxy))
57160854fd9SBob Copeland 			__mesh_path_del(tbl, mpath);
572bf5a70e1SHenning Rogge 	}
573b4c3fbe6SHerbert Xu 	spin_unlock_bh(&tbl->walk_lock);
574bf5a70e1SHenning Rogge }
575bf5a70e1SHenning Rogge 
57660854fd9SBob Copeland static void table_flush_by_iface(struct mesh_table *tbl)
577eb2b9311SLuis Carlos Cobo {
578eb2b9311SLuis Carlos Cobo 	struct mesh_path *mpath;
579b4c3fbe6SHerbert Xu 	struct hlist_node *n;
580eb2b9311SLuis Carlos Cobo 
581b4c3fbe6SHerbert Xu 	spin_lock_bh(&tbl->walk_lock);
582b4c3fbe6SHerbert Xu 	hlist_for_each_entry_safe(mpath, n, &tbl->walk_head, walk_list) {
58360854fd9SBob Copeland 		__mesh_path_del(tbl, mpath);
584eb2b9311SLuis Carlos Cobo 	}
585b4c3fbe6SHerbert Xu 	spin_unlock_bh(&tbl->walk_lock);
586eb2b9311SLuis Carlos Cobo }
587eb2b9311SLuis Carlos Cobo 
588ece1a2e7SJavier Cardona /**
589ece1a2e7SJavier Cardona  * mesh_path_flush_by_iface - Deletes all mesh paths associated with a given iface
590ece1a2e7SJavier Cardona  *
591ece1a2e7SJavier Cardona  * This function deletes both mesh paths as well as mesh portal paths.
592ece1a2e7SJavier Cardona  *
5932c53040fSBen Hutchings  * @sdata: interface data to match
594ece1a2e7SJavier Cardona  *
595ece1a2e7SJavier Cardona  */
596ece1a2e7SJavier Cardona void mesh_path_flush_by_iface(struct ieee80211_sub_if_data *sdata)
597eb2b9311SLuis Carlos Cobo {
59860854fd9SBob Copeland 	table_flush_by_iface(sdata->u.mesh.mesh_paths);
59960854fd9SBob Copeland 	table_flush_by_iface(sdata->u.mesh.mpp_paths);
600eb2b9311SLuis Carlos Cobo }
601eb2b9311SLuis Carlos Cobo 
602eb2b9311SLuis Carlos Cobo /**
6034cc955deSHenning Rogge  * table_path_del - delete a path from the mesh or mpp table
604eb2b9311SLuis Carlos Cobo  *
6054cc955deSHenning Rogge  * @tbl: mesh or mpp path table
606f698d856SJasper Bryant-Greene  * @sdata: local subif
6074cc955deSHenning Rogge  * @addr: dst address (ETH_ALEN length)
608eb2b9311SLuis Carlos Cobo  *
609af901ca1SAndré Goddard Rosa  * Returns: 0 if successful
610eb2b9311SLuis Carlos Cobo  */
61160854fd9SBob Copeland static int table_path_del(struct mesh_table *tbl,
6124cc955deSHenning Rogge 			  struct ieee80211_sub_if_data *sdata,
6134cc955deSHenning Rogge 			  const u8 *addr)
614eb2b9311SLuis Carlos Cobo {
615eb2b9311SLuis Carlos Cobo 	struct mesh_path *mpath;
616eb2b9311SLuis Carlos Cobo 
617b4c3fbe6SHerbert Xu 	spin_lock_bh(&tbl->walk_lock);
61860854fd9SBob Copeland 	mpath = rhashtable_lookup_fast(&tbl->rhead, addr, mesh_rht_params);
61960854fd9SBob Copeland 	if (!mpath) {
620f2ffff08SWei Yongjun 		spin_unlock_bh(&tbl->walk_lock);
62160854fd9SBob Copeland 		return -ENXIO;
622eb2b9311SLuis Carlos Cobo 	}
623eb2b9311SLuis Carlos Cobo 
62460854fd9SBob Copeland 	__mesh_path_del(tbl, mpath);
625b4c3fbe6SHerbert Xu 	spin_unlock_bh(&tbl->walk_lock);
62660854fd9SBob Copeland 	return 0;
6274cc955deSHenning Rogge }
6284cc955deSHenning Rogge 
62960854fd9SBob Copeland 
6304cc955deSHenning Rogge /**
6314cc955deSHenning Rogge  * mesh_path_del - delete a mesh path from the table
6324cc955deSHenning Rogge  *
6334cc955deSHenning Rogge  * @addr: dst address (ETH_ALEN length)
6344cc955deSHenning Rogge  * @sdata: local subif
6354cc955deSHenning Rogge  *
6364cc955deSHenning Rogge  * Returns: 0 if successful
6374cc955deSHenning Rogge  */
6384cc955deSHenning Rogge int mesh_path_del(struct ieee80211_sub_if_data *sdata, const u8 *addr)
6394cc955deSHenning Rogge {
64060854fd9SBob Copeland 	int err;
6414cc955deSHenning Rogge 
6424cc955deSHenning Rogge 	/* flush relevant mpp entries first */
6434cc955deSHenning Rogge 	mpp_flush_by_proxy(sdata, addr);
6444cc955deSHenning Rogge 
6452bdaf386SBob Copeland 	err = table_path_del(sdata->u.mesh.mesh_paths, sdata, addr);
6462bdaf386SBob Copeland 	sdata->u.mesh.mesh_paths_generation++;
647ab1c7906SHenning Rogge 	return err;
648ab1c7906SHenning Rogge }
649ab1c7906SHenning Rogge 
650ab1c7906SHenning Rogge /**
651eb2b9311SLuis Carlos Cobo  * mesh_path_tx_pending - sends pending frames in a mesh path queue
652eb2b9311SLuis Carlos Cobo  *
653eb2b9311SLuis Carlos Cobo  * @mpath: mesh path to activate
654eb2b9311SLuis Carlos Cobo  *
655eb2b9311SLuis Carlos Cobo  * Locking: the state_lock of the mpath structure must NOT be held when calling
656eb2b9311SLuis Carlos Cobo  * this function.
657eb2b9311SLuis Carlos Cobo  */
658eb2b9311SLuis Carlos Cobo void mesh_path_tx_pending(struct mesh_path *mpath)
659eb2b9311SLuis Carlos Cobo {
660249b405cSJavier Cardona 	if (mpath->flags & MESH_PATH_ACTIVE)
661249b405cSJavier Cardona 		ieee80211_add_pending_skbs(mpath->sdata->local,
662249b405cSJavier Cardona 				&mpath->frame_queue);
663eb2b9311SLuis Carlos Cobo }
664eb2b9311SLuis Carlos Cobo 
665eb2b9311SLuis Carlos Cobo /**
6665ee68e5bSJavier Cardona  * mesh_path_send_to_gates - sends pending frames to all known mesh gates
6675ee68e5bSJavier Cardona  *
6685ee68e5bSJavier Cardona  * @mpath: mesh path whose queue will be emptied
6695ee68e5bSJavier Cardona  *
6705ee68e5bSJavier Cardona  * If there is only one gate, the frames are transferred from the failed mpath
6715ee68e5bSJavier Cardona  * queue to that gate's queue.  If there are more than one gates, the frames
6725ee68e5bSJavier Cardona  * are copied from each gate to the next.  After frames are copied, the
6735ee68e5bSJavier Cardona  * mpath queues are emptied onto the transmission queue.
6745ee68e5bSJavier Cardona  */
6755ee68e5bSJavier Cardona int mesh_path_send_to_gates(struct mesh_path *mpath)
6765ee68e5bSJavier Cardona {
6775ee68e5bSJavier Cardona 	struct ieee80211_sub_if_data *sdata = mpath->sdata;
6785ee68e5bSJavier Cardona 	struct mesh_table *tbl;
6795ee68e5bSJavier Cardona 	struct mesh_path *from_mpath = mpath;
68060854fd9SBob Copeland 	struct mesh_path *gate;
6815ee68e5bSJavier Cardona 	bool copy = false;
6825ee68e5bSJavier Cardona 
68360854fd9SBob Copeland 	tbl = sdata->u.mesh.mesh_paths;
6845ee68e5bSJavier Cardona 
68560854fd9SBob Copeland 	rcu_read_lock();
68618b27ff7SBob Copeland 	hlist_for_each_entry_rcu(gate, &tbl->known_gates, gate_list) {
687947c2a0eSBob Copeland 		if (gate->flags & MESH_PATH_ACTIVE) {
688947c2a0eSBob Copeland 			mpath_dbg(sdata, "Forwarding to %pM\n", gate->dst);
689947c2a0eSBob Copeland 			mesh_path_move_to_queue(gate, from_mpath, copy);
690947c2a0eSBob Copeland 			from_mpath = gate;
6915ee68e5bSJavier Cardona 			copy = true;
6925ee68e5bSJavier Cardona 		} else {
693bdcbd8e0SJohannes Berg 			mpath_dbg(sdata,
694d671b2a0SJohannes Berg 				  "Not forwarding to %pM (flags %#x)\n",
695947c2a0eSBob Copeland 				  gate->dst, gate->flags);
6965ee68e5bSJavier Cardona 		}
6975ee68e5bSJavier Cardona 	}
6985ee68e5bSJavier Cardona 
69918b27ff7SBob Copeland 	hlist_for_each_entry_rcu(gate, &tbl->known_gates, gate_list) {
700947c2a0eSBob Copeland 		mpath_dbg(sdata, "Sending to %pM\n", gate->dst);
701947c2a0eSBob Copeland 		mesh_path_tx_pending(gate);
7025ee68e5bSJavier Cardona 	}
70360854fd9SBob Copeland 	rcu_read_unlock();
7045ee68e5bSJavier Cardona 
7055ee68e5bSJavier Cardona 	return (from_mpath == mpath) ? -EHOSTUNREACH : 0;
7065ee68e5bSJavier Cardona }
7075ee68e5bSJavier Cardona 
7085ee68e5bSJavier Cardona /**
709eb2b9311SLuis Carlos Cobo  * mesh_path_discard_frame - discard a frame whose path could not be resolved
710eb2b9311SLuis Carlos Cobo  *
711eb2b9311SLuis Carlos Cobo  * @skb: frame to discard
712f698d856SJasper Bryant-Greene  * @sdata: network subif the frame was to be sent through
713eb2b9311SLuis Carlos Cobo  *
714eb2b9311SLuis Carlos Cobo  * Locking: the function must me called within a rcu_read_lock region
715eb2b9311SLuis Carlos Cobo  */
716bf7cd94dSJohannes Berg void mesh_path_discard_frame(struct ieee80211_sub_if_data *sdata,
717bf7cd94dSJohannes Berg 			     struct sk_buff *skb)
718eb2b9311SLuis Carlos Cobo {
719eb2b9311SLuis Carlos Cobo 	kfree_skb(skb);
720472dbc45SJohannes Berg 	sdata->u.mesh.mshstats.dropped_frames_no_route++;
721eb2b9311SLuis Carlos Cobo }
722eb2b9311SLuis Carlos Cobo 
723eb2b9311SLuis Carlos Cobo /**
724eb2b9311SLuis Carlos Cobo  * mesh_path_flush_pending - free the pending queue of a mesh path
725eb2b9311SLuis Carlos Cobo  *
726eb2b9311SLuis Carlos Cobo  * @mpath: mesh path whose queue has to be freed
727eb2b9311SLuis Carlos Cobo  *
72825985edcSLucas De Marchi  * Locking: the function must me called within a rcu_read_lock region
729eb2b9311SLuis Carlos Cobo  */
730eb2b9311SLuis Carlos Cobo void mesh_path_flush_pending(struct mesh_path *mpath)
731eb2b9311SLuis Carlos Cobo {
732eb2b9311SLuis Carlos Cobo 	struct sk_buff *skb;
733eb2b9311SLuis Carlos Cobo 
73400e3f25cSJavier Cardona 	while ((skb = skb_dequeue(&mpath->frame_queue)) != NULL)
735bf7cd94dSJohannes Berg 		mesh_path_discard_frame(mpath->sdata, skb);
736eb2b9311SLuis Carlos Cobo }
737eb2b9311SLuis Carlos Cobo 
738eb2b9311SLuis Carlos Cobo /**
739eb2b9311SLuis Carlos Cobo  * mesh_path_fix_nexthop - force a specific next hop for a mesh path
740eb2b9311SLuis Carlos Cobo  *
741eb2b9311SLuis Carlos Cobo  * @mpath: the mesh path to modify
742eb2b9311SLuis Carlos Cobo  * @next_hop: the next hop to force
743eb2b9311SLuis Carlos Cobo  *
744eb2b9311SLuis Carlos Cobo  * Locking: this function must be called holding mpath->state_lock
745eb2b9311SLuis Carlos Cobo  */
746eb2b9311SLuis Carlos Cobo void mesh_path_fix_nexthop(struct mesh_path *mpath, struct sta_info *next_hop)
747eb2b9311SLuis Carlos Cobo {
748eb2b9311SLuis Carlos Cobo 	spin_lock_bh(&mpath->state_lock);
749eb2b9311SLuis Carlos Cobo 	mesh_path_assign_nexthop(mpath, next_hop);
750d19b3bf6SRui Paulo 	mpath->sn = 0xffff;
751eb2b9311SLuis Carlos Cobo 	mpath->metric = 0;
752eb2b9311SLuis Carlos Cobo 	mpath->hop_count = 0;
753eb2b9311SLuis Carlos Cobo 	mpath->exp_time = 0;
7545df20f21SPedersen, Thomas 	mpath->flags = MESH_PATH_FIXED | MESH_PATH_SN_VALID;
755eb2b9311SLuis Carlos Cobo 	mesh_path_activate(mpath);
756eb2b9311SLuis Carlos Cobo 	spin_unlock_bh(&mpath->state_lock);
7573eb0928fSManoharan, Rajkumar 	ewma_mesh_fail_avg_init(&next_hop->mesh->fail_avg);
7583eb0928fSManoharan, Rajkumar 	/* init it at a low value - 0 start is tricky */
7593eb0928fSManoharan, Rajkumar 	ewma_mesh_fail_avg_add(&next_hop->mesh->fail_avg, 1);
760eb2b9311SLuis Carlos Cobo 	mesh_path_tx_pending(mpath);
761eb2b9311SLuis Carlos Cobo }
762eb2b9311SLuis Carlos Cobo 
7632bdaf386SBob Copeland int mesh_pathtbl_init(struct ieee80211_sub_if_data *sdata)
764eb2b9311SLuis Carlos Cobo {
765349eb8cfSJohannes Berg 	struct mesh_table *tbl_path, *tbl_mpp;
7664c5ade41SDan Carpenter 	int ret;
76779617deeSYanBo 
76860854fd9SBob Copeland 	tbl_path = mesh_table_alloc();
769349eb8cfSJohannes Berg 	if (!tbl_path)
770349eb8cfSJohannes Berg 		return -ENOMEM;
7715ee68e5bSJavier Cardona 
77260854fd9SBob Copeland 	tbl_mpp = mesh_table_alloc();
773349eb8cfSJohannes Berg 	if (!tbl_mpp) {
7744c5ade41SDan Carpenter 		ret = -ENOMEM;
7754c5ade41SDan Carpenter 		goto free_path;
77679617deeSYanBo 	}
777349eb8cfSJohannes Berg 
77860854fd9SBob Copeland 	rhashtable_init(&tbl_path->rhead, &mesh_rht_params);
77960854fd9SBob Copeland 	rhashtable_init(&tbl_mpp->rhead, &mesh_rht_params);
7802bdaf386SBob Copeland 
78160854fd9SBob Copeland 	sdata->u.mesh.mesh_paths = tbl_path;
78260854fd9SBob Copeland 	sdata->u.mesh.mpp_paths = tbl_mpp;
78379617deeSYanBo 
784eb2b9311SLuis Carlos Cobo 	return 0;
7854c5ade41SDan Carpenter 
7864c5ade41SDan Carpenter free_path:
78760854fd9SBob Copeland 	mesh_table_free(tbl_path);
7884c5ade41SDan Carpenter 	return ret;
789eb2b9311SLuis Carlos Cobo }
790eb2b9311SLuis Carlos Cobo 
79160854fd9SBob Copeland static
79260854fd9SBob Copeland void mesh_path_tbl_expire(struct ieee80211_sub_if_data *sdata,
79360854fd9SBob Copeland 			  struct mesh_table *tbl)
79460854fd9SBob Copeland {
79560854fd9SBob Copeland 	struct mesh_path *mpath;
796b4c3fbe6SHerbert Xu 	struct hlist_node *n;
79760854fd9SBob Copeland 
798b4c3fbe6SHerbert Xu 	spin_lock_bh(&tbl->walk_lock);
799b4c3fbe6SHerbert Xu 	hlist_for_each_entry_safe(mpath, n, &tbl->walk_head, walk_list) {
80060854fd9SBob Copeland 		if ((!(mpath->flags & MESH_PATH_RESOLVING)) &&
80160854fd9SBob Copeland 		    (!(mpath->flags & MESH_PATH_FIXED)) &&
80260854fd9SBob Copeland 		     time_after(jiffies, mpath->exp_time + MESH_PATH_EXPIRE))
80360854fd9SBob Copeland 			__mesh_path_del(tbl, mpath);
80460854fd9SBob Copeland 	}
805b4c3fbe6SHerbert Xu 	spin_unlock_bh(&tbl->walk_lock);
80660854fd9SBob Copeland }
80760854fd9SBob Copeland 
808f698d856SJasper Bryant-Greene void mesh_path_expire(struct ieee80211_sub_if_data *sdata)
809eb2b9311SLuis Carlos Cobo {
81060854fd9SBob Copeland 	mesh_path_tbl_expire(sdata, sdata->u.mesh.mesh_paths);
81160854fd9SBob Copeland 	mesh_path_tbl_expire(sdata, sdata->u.mesh.mpp_paths);
812eb2b9311SLuis Carlos Cobo }
813eb2b9311SLuis Carlos Cobo 
8142bdaf386SBob Copeland void mesh_pathtbl_unregister(struct ieee80211_sub_if_data *sdata)
815eb2b9311SLuis Carlos Cobo {
81660854fd9SBob Copeland 	mesh_table_free(sdata->u.mesh.mesh_paths);
81760854fd9SBob Copeland 	mesh_table_free(sdata->u.mesh.mpp_paths);
818eb2b9311SLuis Carlos Cobo }
819