1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (c) 2008, 2009 open80211s Ltd.
4*4882a593Smuzhiyun * Author: Luis Carlos Cobo <luisca@cozybit.com>
5*4882a593Smuzhiyun */
6*4882a593Smuzhiyun
7*4882a593Smuzhiyun #include <linux/etherdevice.h>
8*4882a593Smuzhiyun #include <linux/list.h>
9*4882a593Smuzhiyun #include <linux/random.h>
10*4882a593Smuzhiyun #include <linux/slab.h>
11*4882a593Smuzhiyun #include <linux/spinlock.h>
12*4882a593Smuzhiyun #include <linux/string.h>
13*4882a593Smuzhiyun #include <net/mac80211.h>
14*4882a593Smuzhiyun #include "wme.h"
15*4882a593Smuzhiyun #include "ieee80211_i.h"
16*4882a593Smuzhiyun #include "mesh.h"
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun static void mesh_path_free_rcu(struct mesh_table *tbl, struct mesh_path *mpath);
19*4882a593Smuzhiyun
mesh_table_hash(const void * addr,u32 len,u32 seed)20*4882a593Smuzhiyun static u32 mesh_table_hash(const void *addr, u32 len, u32 seed)
21*4882a593Smuzhiyun {
22*4882a593Smuzhiyun /* Use last four bytes of hw addr as hash index */
23*4882a593Smuzhiyun return jhash_1word(__get_unaligned_cpu32((u8 *)addr + 2), seed);
24*4882a593Smuzhiyun }
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun static const struct rhashtable_params mesh_rht_params = {
27*4882a593Smuzhiyun .nelem_hint = 2,
28*4882a593Smuzhiyun .automatic_shrinking = true,
29*4882a593Smuzhiyun .key_len = ETH_ALEN,
30*4882a593Smuzhiyun .key_offset = offsetof(struct mesh_path, dst),
31*4882a593Smuzhiyun .head_offset = offsetof(struct mesh_path, rhash),
32*4882a593Smuzhiyun .hashfn = mesh_table_hash,
33*4882a593Smuzhiyun };
34*4882a593Smuzhiyun
mpath_expired(struct mesh_path * mpath)35*4882a593Smuzhiyun static inline bool mpath_expired(struct mesh_path *mpath)
36*4882a593Smuzhiyun {
37*4882a593Smuzhiyun return (mpath->flags & MESH_PATH_ACTIVE) &&
38*4882a593Smuzhiyun time_after(jiffies, mpath->exp_time) &&
39*4882a593Smuzhiyun !(mpath->flags & MESH_PATH_FIXED);
40*4882a593Smuzhiyun }
41*4882a593Smuzhiyun
mesh_path_rht_free(void * ptr,void * tblptr)42*4882a593Smuzhiyun static void mesh_path_rht_free(void *ptr, void *tblptr)
43*4882a593Smuzhiyun {
44*4882a593Smuzhiyun struct mesh_path *mpath = ptr;
45*4882a593Smuzhiyun struct mesh_table *tbl = tblptr;
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun mesh_path_free_rcu(tbl, mpath);
48*4882a593Smuzhiyun }
49*4882a593Smuzhiyun
mesh_table_alloc(void)50*4882a593Smuzhiyun static struct mesh_table *mesh_table_alloc(void)
51*4882a593Smuzhiyun {
52*4882a593Smuzhiyun struct mesh_table *newtbl;
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun newtbl = kmalloc(sizeof(struct mesh_table), GFP_ATOMIC);
55*4882a593Smuzhiyun if (!newtbl)
56*4882a593Smuzhiyun return NULL;
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun INIT_HLIST_HEAD(&newtbl->known_gates);
59*4882a593Smuzhiyun INIT_HLIST_HEAD(&newtbl->walk_head);
60*4882a593Smuzhiyun atomic_set(&newtbl->entries, 0);
61*4882a593Smuzhiyun spin_lock_init(&newtbl->gates_lock);
62*4882a593Smuzhiyun spin_lock_init(&newtbl->walk_lock);
63*4882a593Smuzhiyun if (rhashtable_init(&newtbl->rhead, &mesh_rht_params)) {
64*4882a593Smuzhiyun kfree(newtbl);
65*4882a593Smuzhiyun return NULL;
66*4882a593Smuzhiyun }
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun return newtbl;
69*4882a593Smuzhiyun }
70*4882a593Smuzhiyun
mesh_table_free(struct mesh_table * tbl)71*4882a593Smuzhiyun static void mesh_table_free(struct mesh_table *tbl)
72*4882a593Smuzhiyun {
73*4882a593Smuzhiyun rhashtable_free_and_destroy(&tbl->rhead,
74*4882a593Smuzhiyun mesh_path_rht_free, tbl);
75*4882a593Smuzhiyun kfree(tbl);
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun /**
79*4882a593Smuzhiyun * mesh_path_assign_nexthop - update mesh path next hop
80*4882a593Smuzhiyun *
81*4882a593Smuzhiyun * @mpath: mesh path to update
82*4882a593Smuzhiyun * @sta: next hop to assign
83*4882a593Smuzhiyun *
84*4882a593Smuzhiyun * Locking: mpath->state_lock must be held when calling this function
85*4882a593Smuzhiyun */
mesh_path_assign_nexthop(struct mesh_path * mpath,struct sta_info * sta)86*4882a593Smuzhiyun void mesh_path_assign_nexthop(struct mesh_path *mpath, struct sta_info *sta)
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun struct sk_buff *skb;
89*4882a593Smuzhiyun struct ieee80211_hdr *hdr;
90*4882a593Smuzhiyun unsigned long flags;
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun rcu_assign_pointer(mpath->next_hop, sta);
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun spin_lock_irqsave(&mpath->frame_queue.lock, flags);
95*4882a593Smuzhiyun skb_queue_walk(&mpath->frame_queue, skb) {
96*4882a593Smuzhiyun hdr = (struct ieee80211_hdr *) skb->data;
97*4882a593Smuzhiyun memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN);
98*4882a593Smuzhiyun memcpy(hdr->addr2, mpath->sdata->vif.addr, ETH_ALEN);
99*4882a593Smuzhiyun ieee80211_mps_set_frame_flags(sta->sdata, sta, hdr);
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun spin_unlock_irqrestore(&mpath->frame_queue.lock, flags);
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun
prepare_for_gate(struct sk_buff * skb,char * dst_addr,struct mesh_path * gate_mpath)105*4882a593Smuzhiyun static void prepare_for_gate(struct sk_buff *skb, char *dst_addr,
106*4882a593Smuzhiyun struct mesh_path *gate_mpath)
107*4882a593Smuzhiyun {
108*4882a593Smuzhiyun struct ieee80211_hdr *hdr;
109*4882a593Smuzhiyun struct ieee80211s_hdr *mshdr;
110*4882a593Smuzhiyun int mesh_hdrlen, hdrlen;
111*4882a593Smuzhiyun char *next_hop;
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun hdr = (struct ieee80211_hdr *) skb->data;
114*4882a593Smuzhiyun hdrlen = ieee80211_hdrlen(hdr->frame_control);
115*4882a593Smuzhiyun mshdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun if (!(mshdr->flags & MESH_FLAGS_AE)) {
118*4882a593Smuzhiyun /* size of the fixed part of the mesh header */
119*4882a593Smuzhiyun mesh_hdrlen = 6;
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun /* make room for the two extended addresses */
122*4882a593Smuzhiyun skb_push(skb, 2 * ETH_ALEN);
123*4882a593Smuzhiyun memmove(skb->data, hdr, hdrlen + mesh_hdrlen);
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun hdr = (struct ieee80211_hdr *) skb->data;
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun /* we preserve the previous mesh header and only add
128*4882a593Smuzhiyun * the new addreses */
129*4882a593Smuzhiyun mshdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
130*4882a593Smuzhiyun mshdr->flags = MESH_FLAGS_AE_A5_A6;
131*4882a593Smuzhiyun memcpy(mshdr->eaddr1, hdr->addr3, ETH_ALEN);
132*4882a593Smuzhiyun memcpy(mshdr->eaddr2, hdr->addr4, ETH_ALEN);
133*4882a593Smuzhiyun }
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun /* update next hop */
136*4882a593Smuzhiyun hdr = (struct ieee80211_hdr *) skb->data;
137*4882a593Smuzhiyun rcu_read_lock();
138*4882a593Smuzhiyun next_hop = rcu_dereference(gate_mpath->next_hop)->sta.addr;
139*4882a593Smuzhiyun memcpy(hdr->addr1, next_hop, ETH_ALEN);
140*4882a593Smuzhiyun rcu_read_unlock();
141*4882a593Smuzhiyun memcpy(hdr->addr2, gate_mpath->sdata->vif.addr, ETH_ALEN);
142*4882a593Smuzhiyun memcpy(hdr->addr3, dst_addr, ETH_ALEN);
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun /**
146*4882a593Smuzhiyun * mesh_path_move_to_queue - Move or copy frames from one mpath queue to another
147*4882a593Smuzhiyun *
148*4882a593Smuzhiyun * This function is used to transfer or copy frames from an unresolved mpath to
149*4882a593Smuzhiyun * a gate mpath. The function also adds the Address Extension field and
150*4882a593Smuzhiyun * updates the next hop.
151*4882a593Smuzhiyun *
152*4882a593Smuzhiyun * If a frame already has an Address Extension field, only the next hop and
153*4882a593Smuzhiyun * destination addresses are updated.
154*4882a593Smuzhiyun *
155*4882a593Smuzhiyun * The gate mpath must be an active mpath with a valid mpath->next_hop.
156*4882a593Smuzhiyun *
157*4882a593Smuzhiyun * @gate_mpath: An active mpath the frames will be sent to (i.e. the gate)
158*4882a593Smuzhiyun * @from_mpath: The failed mpath
159*4882a593Smuzhiyun * @copy: When true, copy all the frames to the new mpath queue. When false,
160*4882a593Smuzhiyun * move them.
161*4882a593Smuzhiyun */
mesh_path_move_to_queue(struct mesh_path * gate_mpath,struct mesh_path * from_mpath,bool copy)162*4882a593Smuzhiyun static void mesh_path_move_to_queue(struct mesh_path *gate_mpath,
163*4882a593Smuzhiyun struct mesh_path *from_mpath,
164*4882a593Smuzhiyun bool copy)
165*4882a593Smuzhiyun {
166*4882a593Smuzhiyun struct sk_buff *skb, *fskb, *tmp;
167*4882a593Smuzhiyun struct sk_buff_head failq;
168*4882a593Smuzhiyun unsigned long flags;
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun if (WARN_ON(gate_mpath == from_mpath))
171*4882a593Smuzhiyun return;
172*4882a593Smuzhiyun if (WARN_ON(!gate_mpath->next_hop))
173*4882a593Smuzhiyun return;
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun __skb_queue_head_init(&failq);
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun spin_lock_irqsave(&from_mpath->frame_queue.lock, flags);
178*4882a593Smuzhiyun skb_queue_splice_init(&from_mpath->frame_queue, &failq);
179*4882a593Smuzhiyun spin_unlock_irqrestore(&from_mpath->frame_queue.lock, flags);
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun skb_queue_walk_safe(&failq, fskb, tmp) {
182*4882a593Smuzhiyun if (skb_queue_len(&gate_mpath->frame_queue) >=
183*4882a593Smuzhiyun MESH_FRAME_QUEUE_LEN) {
184*4882a593Smuzhiyun mpath_dbg(gate_mpath->sdata, "mpath queue full!\n");
185*4882a593Smuzhiyun break;
186*4882a593Smuzhiyun }
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun skb = skb_copy(fskb, GFP_ATOMIC);
189*4882a593Smuzhiyun if (WARN_ON(!skb))
190*4882a593Smuzhiyun break;
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun prepare_for_gate(skb, gate_mpath->dst, gate_mpath);
193*4882a593Smuzhiyun skb_queue_tail(&gate_mpath->frame_queue, skb);
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun if (copy)
196*4882a593Smuzhiyun continue;
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun __skb_unlink(fskb, &failq);
199*4882a593Smuzhiyun kfree_skb(fskb);
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun mpath_dbg(gate_mpath->sdata, "Mpath queue for gate %pM has %d frames\n",
203*4882a593Smuzhiyun gate_mpath->dst, skb_queue_len(&gate_mpath->frame_queue));
204*4882a593Smuzhiyun
205*4882a593Smuzhiyun if (!copy)
206*4882a593Smuzhiyun return;
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun spin_lock_irqsave(&from_mpath->frame_queue.lock, flags);
209*4882a593Smuzhiyun skb_queue_splice(&failq, &from_mpath->frame_queue);
210*4882a593Smuzhiyun spin_unlock_irqrestore(&from_mpath->frame_queue.lock, flags);
211*4882a593Smuzhiyun }
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun
mpath_lookup(struct mesh_table * tbl,const u8 * dst,struct ieee80211_sub_if_data * sdata)214*4882a593Smuzhiyun static struct mesh_path *mpath_lookup(struct mesh_table *tbl, const u8 *dst,
215*4882a593Smuzhiyun struct ieee80211_sub_if_data *sdata)
216*4882a593Smuzhiyun {
217*4882a593Smuzhiyun struct mesh_path *mpath;
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun mpath = rhashtable_lookup(&tbl->rhead, dst, mesh_rht_params);
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun if (mpath && mpath_expired(mpath)) {
222*4882a593Smuzhiyun spin_lock_bh(&mpath->state_lock);
223*4882a593Smuzhiyun mpath->flags &= ~MESH_PATH_ACTIVE;
224*4882a593Smuzhiyun spin_unlock_bh(&mpath->state_lock);
225*4882a593Smuzhiyun }
226*4882a593Smuzhiyun return mpath;
227*4882a593Smuzhiyun }
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun /**
230*4882a593Smuzhiyun * mesh_path_lookup - look up a path in the mesh path table
231*4882a593Smuzhiyun * @sdata: local subif
232*4882a593Smuzhiyun * @dst: hardware address (ETH_ALEN length) of destination
233*4882a593Smuzhiyun *
234*4882a593Smuzhiyun * Returns: pointer to the mesh path structure, or NULL if not found
235*4882a593Smuzhiyun *
236*4882a593Smuzhiyun * Locking: must be called within a read rcu section.
237*4882a593Smuzhiyun */
238*4882a593Smuzhiyun struct mesh_path *
mesh_path_lookup(struct ieee80211_sub_if_data * sdata,const u8 * dst)239*4882a593Smuzhiyun mesh_path_lookup(struct ieee80211_sub_if_data *sdata, const u8 *dst)
240*4882a593Smuzhiyun {
241*4882a593Smuzhiyun return mpath_lookup(sdata->u.mesh.mesh_paths, dst, sdata);
242*4882a593Smuzhiyun }
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun struct mesh_path *
mpp_path_lookup(struct ieee80211_sub_if_data * sdata,const u8 * dst)245*4882a593Smuzhiyun mpp_path_lookup(struct ieee80211_sub_if_data *sdata, const u8 *dst)
246*4882a593Smuzhiyun {
247*4882a593Smuzhiyun return mpath_lookup(sdata->u.mesh.mpp_paths, dst, sdata);
248*4882a593Smuzhiyun }
249*4882a593Smuzhiyun
250*4882a593Smuzhiyun static struct mesh_path *
__mesh_path_lookup_by_idx(struct mesh_table * tbl,int idx)251*4882a593Smuzhiyun __mesh_path_lookup_by_idx(struct mesh_table *tbl, int idx)
252*4882a593Smuzhiyun {
253*4882a593Smuzhiyun int i = 0;
254*4882a593Smuzhiyun struct mesh_path *mpath;
255*4882a593Smuzhiyun
256*4882a593Smuzhiyun hlist_for_each_entry_rcu(mpath, &tbl->walk_head, walk_list) {
257*4882a593Smuzhiyun if (i++ == idx)
258*4882a593Smuzhiyun break;
259*4882a593Smuzhiyun }
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun if (!mpath)
262*4882a593Smuzhiyun return NULL;
263*4882a593Smuzhiyun
264*4882a593Smuzhiyun if (mpath_expired(mpath)) {
265*4882a593Smuzhiyun spin_lock_bh(&mpath->state_lock);
266*4882a593Smuzhiyun mpath->flags &= ~MESH_PATH_ACTIVE;
267*4882a593Smuzhiyun spin_unlock_bh(&mpath->state_lock);
268*4882a593Smuzhiyun }
269*4882a593Smuzhiyun return mpath;
270*4882a593Smuzhiyun }
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun /**
273*4882a593Smuzhiyun * mesh_path_lookup_by_idx - look up a path in the mesh path table by its index
274*4882a593Smuzhiyun * @idx: index
275*4882a593Smuzhiyun * @sdata: local subif, or NULL for all entries
276*4882a593Smuzhiyun *
277*4882a593Smuzhiyun * Returns: pointer to the mesh path structure, or NULL if not found.
278*4882a593Smuzhiyun *
279*4882a593Smuzhiyun * Locking: must be called within a read rcu section.
280*4882a593Smuzhiyun */
281*4882a593Smuzhiyun struct mesh_path *
mesh_path_lookup_by_idx(struct ieee80211_sub_if_data * sdata,int idx)282*4882a593Smuzhiyun mesh_path_lookup_by_idx(struct ieee80211_sub_if_data *sdata, int idx)
283*4882a593Smuzhiyun {
284*4882a593Smuzhiyun return __mesh_path_lookup_by_idx(sdata->u.mesh.mesh_paths, idx);
285*4882a593Smuzhiyun }
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun /**
288*4882a593Smuzhiyun * mpp_path_lookup_by_idx - look up a path in the proxy path table by its index
289*4882a593Smuzhiyun * @idx: index
290*4882a593Smuzhiyun * @sdata: local subif, or NULL for all entries
291*4882a593Smuzhiyun *
292*4882a593Smuzhiyun * Returns: pointer to the proxy path structure, or NULL if not found.
293*4882a593Smuzhiyun *
294*4882a593Smuzhiyun * Locking: must be called within a read rcu section.
295*4882a593Smuzhiyun */
296*4882a593Smuzhiyun struct mesh_path *
mpp_path_lookup_by_idx(struct ieee80211_sub_if_data * sdata,int idx)297*4882a593Smuzhiyun mpp_path_lookup_by_idx(struct ieee80211_sub_if_data *sdata, int idx)
298*4882a593Smuzhiyun {
299*4882a593Smuzhiyun return __mesh_path_lookup_by_idx(sdata->u.mesh.mpp_paths, idx);
300*4882a593Smuzhiyun }
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun /**
303*4882a593Smuzhiyun * mesh_path_add_gate - add the given mpath to a mesh gate to our path table
304*4882a593Smuzhiyun * @mpath: gate path to add to table
305*4882a593Smuzhiyun */
mesh_path_add_gate(struct mesh_path * mpath)306*4882a593Smuzhiyun int mesh_path_add_gate(struct mesh_path *mpath)
307*4882a593Smuzhiyun {
308*4882a593Smuzhiyun struct mesh_table *tbl;
309*4882a593Smuzhiyun int err;
310*4882a593Smuzhiyun
311*4882a593Smuzhiyun rcu_read_lock();
312*4882a593Smuzhiyun tbl = mpath->sdata->u.mesh.mesh_paths;
313*4882a593Smuzhiyun
314*4882a593Smuzhiyun spin_lock_bh(&mpath->state_lock);
315*4882a593Smuzhiyun if (mpath->is_gate) {
316*4882a593Smuzhiyun err = -EEXIST;
317*4882a593Smuzhiyun spin_unlock_bh(&mpath->state_lock);
318*4882a593Smuzhiyun goto err_rcu;
319*4882a593Smuzhiyun }
320*4882a593Smuzhiyun mpath->is_gate = true;
321*4882a593Smuzhiyun mpath->sdata->u.mesh.num_gates++;
322*4882a593Smuzhiyun
323*4882a593Smuzhiyun spin_lock(&tbl->gates_lock);
324*4882a593Smuzhiyun hlist_add_head_rcu(&mpath->gate_list, &tbl->known_gates);
325*4882a593Smuzhiyun spin_unlock(&tbl->gates_lock);
326*4882a593Smuzhiyun
327*4882a593Smuzhiyun spin_unlock_bh(&mpath->state_lock);
328*4882a593Smuzhiyun
329*4882a593Smuzhiyun mpath_dbg(mpath->sdata,
330*4882a593Smuzhiyun "Mesh path: Recorded new gate: %pM. %d known gates\n",
331*4882a593Smuzhiyun mpath->dst, mpath->sdata->u.mesh.num_gates);
332*4882a593Smuzhiyun err = 0;
333*4882a593Smuzhiyun err_rcu:
334*4882a593Smuzhiyun rcu_read_unlock();
335*4882a593Smuzhiyun return err;
336*4882a593Smuzhiyun }
337*4882a593Smuzhiyun
338*4882a593Smuzhiyun /**
339*4882a593Smuzhiyun * mesh_gate_del - remove a mesh gate from the list of known gates
340*4882a593Smuzhiyun * @tbl: table which holds our list of known gates
341*4882a593Smuzhiyun * @mpath: gate mpath
342*4882a593Smuzhiyun */
mesh_gate_del(struct mesh_table * tbl,struct mesh_path * mpath)343*4882a593Smuzhiyun static void mesh_gate_del(struct mesh_table *tbl, struct mesh_path *mpath)
344*4882a593Smuzhiyun {
345*4882a593Smuzhiyun lockdep_assert_held(&mpath->state_lock);
346*4882a593Smuzhiyun if (!mpath->is_gate)
347*4882a593Smuzhiyun return;
348*4882a593Smuzhiyun
349*4882a593Smuzhiyun mpath->is_gate = false;
350*4882a593Smuzhiyun spin_lock_bh(&tbl->gates_lock);
351*4882a593Smuzhiyun hlist_del_rcu(&mpath->gate_list);
352*4882a593Smuzhiyun mpath->sdata->u.mesh.num_gates--;
353*4882a593Smuzhiyun spin_unlock_bh(&tbl->gates_lock);
354*4882a593Smuzhiyun
355*4882a593Smuzhiyun mpath_dbg(mpath->sdata,
356*4882a593Smuzhiyun "Mesh path: Deleted gate: %pM. %d known gates\n",
357*4882a593Smuzhiyun mpath->dst, mpath->sdata->u.mesh.num_gates);
358*4882a593Smuzhiyun }
359*4882a593Smuzhiyun
360*4882a593Smuzhiyun /**
361*4882a593Smuzhiyun * mesh_gate_num - number of gates known to this interface
362*4882a593Smuzhiyun * @sdata: subif data
363*4882a593Smuzhiyun */
mesh_gate_num(struct ieee80211_sub_if_data * sdata)364*4882a593Smuzhiyun int mesh_gate_num(struct ieee80211_sub_if_data *sdata)
365*4882a593Smuzhiyun {
366*4882a593Smuzhiyun return sdata->u.mesh.num_gates;
367*4882a593Smuzhiyun }
368*4882a593Smuzhiyun
369*4882a593Smuzhiyun static
mesh_path_new(struct ieee80211_sub_if_data * sdata,const u8 * dst,gfp_t gfp_flags)370*4882a593Smuzhiyun struct mesh_path *mesh_path_new(struct ieee80211_sub_if_data *sdata,
371*4882a593Smuzhiyun const u8 *dst, gfp_t gfp_flags)
372*4882a593Smuzhiyun {
373*4882a593Smuzhiyun struct mesh_path *new_mpath;
374*4882a593Smuzhiyun
375*4882a593Smuzhiyun new_mpath = kzalloc(sizeof(struct mesh_path), gfp_flags);
376*4882a593Smuzhiyun if (!new_mpath)
377*4882a593Smuzhiyun return NULL;
378*4882a593Smuzhiyun
379*4882a593Smuzhiyun memcpy(new_mpath->dst, dst, ETH_ALEN);
380*4882a593Smuzhiyun eth_broadcast_addr(new_mpath->rann_snd_addr);
381*4882a593Smuzhiyun new_mpath->is_root = false;
382*4882a593Smuzhiyun new_mpath->sdata = sdata;
383*4882a593Smuzhiyun new_mpath->flags = 0;
384*4882a593Smuzhiyun skb_queue_head_init(&new_mpath->frame_queue);
385*4882a593Smuzhiyun new_mpath->exp_time = jiffies;
386*4882a593Smuzhiyun spin_lock_init(&new_mpath->state_lock);
387*4882a593Smuzhiyun timer_setup(&new_mpath->timer, mesh_path_timer, 0);
388*4882a593Smuzhiyun
389*4882a593Smuzhiyun return new_mpath;
390*4882a593Smuzhiyun }
391*4882a593Smuzhiyun
392*4882a593Smuzhiyun /**
393*4882a593Smuzhiyun * mesh_path_add - allocate and add a new path to the mesh path table
394*4882a593Smuzhiyun * @dst: destination address of the path (ETH_ALEN length)
395*4882a593Smuzhiyun * @sdata: local subif
396*4882a593Smuzhiyun *
397*4882a593Smuzhiyun * Returns: 0 on success
398*4882a593Smuzhiyun *
399*4882a593Smuzhiyun * State: the initial state of the new path is set to 0
400*4882a593Smuzhiyun */
mesh_path_add(struct ieee80211_sub_if_data * sdata,const u8 * dst)401*4882a593Smuzhiyun struct mesh_path *mesh_path_add(struct ieee80211_sub_if_data *sdata,
402*4882a593Smuzhiyun const u8 *dst)
403*4882a593Smuzhiyun {
404*4882a593Smuzhiyun struct mesh_table *tbl;
405*4882a593Smuzhiyun struct mesh_path *mpath, *new_mpath;
406*4882a593Smuzhiyun
407*4882a593Smuzhiyun if (ether_addr_equal(dst, sdata->vif.addr))
408*4882a593Smuzhiyun /* never add ourselves as neighbours */
409*4882a593Smuzhiyun return ERR_PTR(-ENOTSUPP);
410*4882a593Smuzhiyun
411*4882a593Smuzhiyun if (is_multicast_ether_addr(dst))
412*4882a593Smuzhiyun return ERR_PTR(-ENOTSUPP);
413*4882a593Smuzhiyun
414*4882a593Smuzhiyun if (atomic_add_unless(&sdata->u.mesh.mpaths, 1, MESH_MAX_MPATHS) == 0)
415*4882a593Smuzhiyun return ERR_PTR(-ENOSPC);
416*4882a593Smuzhiyun
417*4882a593Smuzhiyun new_mpath = mesh_path_new(sdata, dst, GFP_ATOMIC);
418*4882a593Smuzhiyun if (!new_mpath)
419*4882a593Smuzhiyun return ERR_PTR(-ENOMEM);
420*4882a593Smuzhiyun
421*4882a593Smuzhiyun tbl = sdata->u.mesh.mesh_paths;
422*4882a593Smuzhiyun spin_lock_bh(&tbl->walk_lock);
423*4882a593Smuzhiyun mpath = rhashtable_lookup_get_insert_fast(&tbl->rhead,
424*4882a593Smuzhiyun &new_mpath->rhash,
425*4882a593Smuzhiyun mesh_rht_params);
426*4882a593Smuzhiyun if (!mpath)
427*4882a593Smuzhiyun hlist_add_head(&new_mpath->walk_list, &tbl->walk_head);
428*4882a593Smuzhiyun spin_unlock_bh(&tbl->walk_lock);
429*4882a593Smuzhiyun
430*4882a593Smuzhiyun if (mpath) {
431*4882a593Smuzhiyun kfree(new_mpath);
432*4882a593Smuzhiyun
433*4882a593Smuzhiyun if (IS_ERR(mpath))
434*4882a593Smuzhiyun return mpath;
435*4882a593Smuzhiyun
436*4882a593Smuzhiyun new_mpath = mpath;
437*4882a593Smuzhiyun }
438*4882a593Smuzhiyun
439*4882a593Smuzhiyun sdata->u.mesh.mesh_paths_generation++;
440*4882a593Smuzhiyun return new_mpath;
441*4882a593Smuzhiyun }
442*4882a593Smuzhiyun
mpp_path_add(struct ieee80211_sub_if_data * sdata,const u8 * dst,const u8 * mpp)443*4882a593Smuzhiyun int mpp_path_add(struct ieee80211_sub_if_data *sdata,
444*4882a593Smuzhiyun const u8 *dst, const u8 *mpp)
445*4882a593Smuzhiyun {
446*4882a593Smuzhiyun struct mesh_table *tbl;
447*4882a593Smuzhiyun struct mesh_path *new_mpath;
448*4882a593Smuzhiyun int ret;
449*4882a593Smuzhiyun
450*4882a593Smuzhiyun if (ether_addr_equal(dst, sdata->vif.addr))
451*4882a593Smuzhiyun /* never add ourselves as neighbours */
452*4882a593Smuzhiyun return -ENOTSUPP;
453*4882a593Smuzhiyun
454*4882a593Smuzhiyun if (is_multicast_ether_addr(dst))
455*4882a593Smuzhiyun return -ENOTSUPP;
456*4882a593Smuzhiyun
457*4882a593Smuzhiyun new_mpath = mesh_path_new(sdata, dst, GFP_ATOMIC);
458*4882a593Smuzhiyun
459*4882a593Smuzhiyun if (!new_mpath)
460*4882a593Smuzhiyun return -ENOMEM;
461*4882a593Smuzhiyun
462*4882a593Smuzhiyun memcpy(new_mpath->mpp, mpp, ETH_ALEN);
463*4882a593Smuzhiyun tbl = sdata->u.mesh.mpp_paths;
464*4882a593Smuzhiyun
465*4882a593Smuzhiyun spin_lock_bh(&tbl->walk_lock);
466*4882a593Smuzhiyun ret = rhashtable_lookup_insert_fast(&tbl->rhead,
467*4882a593Smuzhiyun &new_mpath->rhash,
468*4882a593Smuzhiyun mesh_rht_params);
469*4882a593Smuzhiyun if (!ret)
470*4882a593Smuzhiyun hlist_add_head_rcu(&new_mpath->walk_list, &tbl->walk_head);
471*4882a593Smuzhiyun spin_unlock_bh(&tbl->walk_lock);
472*4882a593Smuzhiyun
473*4882a593Smuzhiyun if (ret)
474*4882a593Smuzhiyun kfree(new_mpath);
475*4882a593Smuzhiyun
476*4882a593Smuzhiyun sdata->u.mesh.mpp_paths_generation++;
477*4882a593Smuzhiyun return ret;
478*4882a593Smuzhiyun }
479*4882a593Smuzhiyun
480*4882a593Smuzhiyun
481*4882a593Smuzhiyun /**
482*4882a593Smuzhiyun * mesh_plink_broken - deactivates paths and sends perr when a link breaks
483*4882a593Smuzhiyun *
484*4882a593Smuzhiyun * @sta: broken peer link
485*4882a593Smuzhiyun *
486*4882a593Smuzhiyun * This function must be called from the rate control algorithm if enough
487*4882a593Smuzhiyun * delivery errors suggest that a peer link is no longer usable.
488*4882a593Smuzhiyun */
mesh_plink_broken(struct sta_info * sta)489*4882a593Smuzhiyun void mesh_plink_broken(struct sta_info *sta)
490*4882a593Smuzhiyun {
491*4882a593Smuzhiyun struct ieee80211_sub_if_data *sdata = sta->sdata;
492*4882a593Smuzhiyun struct mesh_table *tbl = sdata->u.mesh.mesh_paths;
493*4882a593Smuzhiyun static const u8 bcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
494*4882a593Smuzhiyun struct mesh_path *mpath;
495*4882a593Smuzhiyun
496*4882a593Smuzhiyun rcu_read_lock();
497*4882a593Smuzhiyun hlist_for_each_entry_rcu(mpath, &tbl->walk_head, walk_list) {
498*4882a593Smuzhiyun if (rcu_access_pointer(mpath->next_hop) == sta &&
499*4882a593Smuzhiyun mpath->flags & MESH_PATH_ACTIVE &&
500*4882a593Smuzhiyun !(mpath->flags & MESH_PATH_FIXED)) {
501*4882a593Smuzhiyun spin_lock_bh(&mpath->state_lock);
502*4882a593Smuzhiyun mpath->flags &= ~MESH_PATH_ACTIVE;
503*4882a593Smuzhiyun ++mpath->sn;
504*4882a593Smuzhiyun spin_unlock_bh(&mpath->state_lock);
505*4882a593Smuzhiyun mesh_path_error_tx(sdata,
506*4882a593Smuzhiyun sdata->u.mesh.mshcfg.element_ttl,
507*4882a593Smuzhiyun mpath->dst, mpath->sn,
508*4882a593Smuzhiyun WLAN_REASON_MESH_PATH_DEST_UNREACHABLE, bcast);
509*4882a593Smuzhiyun }
510*4882a593Smuzhiyun }
511*4882a593Smuzhiyun rcu_read_unlock();
512*4882a593Smuzhiyun }
513*4882a593Smuzhiyun
mesh_path_free_rcu(struct mesh_table * tbl,struct mesh_path * mpath)514*4882a593Smuzhiyun static void mesh_path_free_rcu(struct mesh_table *tbl,
515*4882a593Smuzhiyun struct mesh_path *mpath)
516*4882a593Smuzhiyun {
517*4882a593Smuzhiyun struct ieee80211_sub_if_data *sdata = mpath->sdata;
518*4882a593Smuzhiyun
519*4882a593Smuzhiyun spin_lock_bh(&mpath->state_lock);
520*4882a593Smuzhiyun mpath->flags |= MESH_PATH_RESOLVING | MESH_PATH_DELETED;
521*4882a593Smuzhiyun mesh_gate_del(tbl, mpath);
522*4882a593Smuzhiyun spin_unlock_bh(&mpath->state_lock);
523*4882a593Smuzhiyun del_timer_sync(&mpath->timer);
524*4882a593Smuzhiyun atomic_dec(&sdata->u.mesh.mpaths);
525*4882a593Smuzhiyun atomic_dec(&tbl->entries);
526*4882a593Smuzhiyun mesh_path_flush_pending(mpath);
527*4882a593Smuzhiyun kfree_rcu(mpath, rcu);
528*4882a593Smuzhiyun }
529*4882a593Smuzhiyun
__mesh_path_del(struct mesh_table * tbl,struct mesh_path * mpath)530*4882a593Smuzhiyun static void __mesh_path_del(struct mesh_table *tbl, struct mesh_path *mpath)
531*4882a593Smuzhiyun {
532*4882a593Smuzhiyun hlist_del_rcu(&mpath->walk_list);
533*4882a593Smuzhiyun rhashtable_remove_fast(&tbl->rhead, &mpath->rhash, mesh_rht_params);
534*4882a593Smuzhiyun mesh_path_free_rcu(tbl, mpath);
535*4882a593Smuzhiyun }
536*4882a593Smuzhiyun
537*4882a593Smuzhiyun /**
538*4882a593Smuzhiyun * mesh_path_flush_by_nexthop - Deletes mesh paths if their next hop matches
539*4882a593Smuzhiyun *
540*4882a593Smuzhiyun * @sta: mesh peer to match
541*4882a593Smuzhiyun *
542*4882a593Smuzhiyun * RCU notes: this function is called when a mesh plink transitions from
543*4882a593Smuzhiyun * PLINK_ESTAB to any other state, since PLINK_ESTAB state is the only one that
544*4882a593Smuzhiyun * allows path creation. This will happen before the sta can be freed (because
545*4882a593Smuzhiyun * sta_info_destroy() calls this) so any reader in a rcu read block will be
546*4882a593Smuzhiyun * protected against the plink disappearing.
547*4882a593Smuzhiyun */
mesh_path_flush_by_nexthop(struct sta_info * sta)548*4882a593Smuzhiyun void mesh_path_flush_by_nexthop(struct sta_info *sta)
549*4882a593Smuzhiyun {
550*4882a593Smuzhiyun struct ieee80211_sub_if_data *sdata = sta->sdata;
551*4882a593Smuzhiyun struct mesh_table *tbl = sdata->u.mesh.mesh_paths;
552*4882a593Smuzhiyun struct mesh_path *mpath;
553*4882a593Smuzhiyun struct hlist_node *n;
554*4882a593Smuzhiyun
555*4882a593Smuzhiyun spin_lock_bh(&tbl->walk_lock);
556*4882a593Smuzhiyun hlist_for_each_entry_safe(mpath, n, &tbl->walk_head, walk_list) {
557*4882a593Smuzhiyun if (rcu_access_pointer(mpath->next_hop) == sta)
558*4882a593Smuzhiyun __mesh_path_del(tbl, mpath);
559*4882a593Smuzhiyun }
560*4882a593Smuzhiyun spin_unlock_bh(&tbl->walk_lock);
561*4882a593Smuzhiyun }
562*4882a593Smuzhiyun
mpp_flush_by_proxy(struct ieee80211_sub_if_data * sdata,const u8 * proxy)563*4882a593Smuzhiyun static void mpp_flush_by_proxy(struct ieee80211_sub_if_data *sdata,
564*4882a593Smuzhiyun const u8 *proxy)
565*4882a593Smuzhiyun {
566*4882a593Smuzhiyun struct mesh_table *tbl = sdata->u.mesh.mpp_paths;
567*4882a593Smuzhiyun struct mesh_path *mpath;
568*4882a593Smuzhiyun struct hlist_node *n;
569*4882a593Smuzhiyun
570*4882a593Smuzhiyun spin_lock_bh(&tbl->walk_lock);
571*4882a593Smuzhiyun hlist_for_each_entry_safe(mpath, n, &tbl->walk_head, walk_list) {
572*4882a593Smuzhiyun if (ether_addr_equal(mpath->mpp, proxy))
573*4882a593Smuzhiyun __mesh_path_del(tbl, mpath);
574*4882a593Smuzhiyun }
575*4882a593Smuzhiyun spin_unlock_bh(&tbl->walk_lock);
576*4882a593Smuzhiyun }
577*4882a593Smuzhiyun
table_flush_by_iface(struct mesh_table * tbl)578*4882a593Smuzhiyun static void table_flush_by_iface(struct mesh_table *tbl)
579*4882a593Smuzhiyun {
580*4882a593Smuzhiyun struct mesh_path *mpath;
581*4882a593Smuzhiyun struct hlist_node *n;
582*4882a593Smuzhiyun
583*4882a593Smuzhiyun spin_lock_bh(&tbl->walk_lock);
584*4882a593Smuzhiyun hlist_for_each_entry_safe(mpath, n, &tbl->walk_head, walk_list) {
585*4882a593Smuzhiyun __mesh_path_del(tbl, mpath);
586*4882a593Smuzhiyun }
587*4882a593Smuzhiyun spin_unlock_bh(&tbl->walk_lock);
588*4882a593Smuzhiyun }
589*4882a593Smuzhiyun
590*4882a593Smuzhiyun /**
591*4882a593Smuzhiyun * mesh_path_flush_by_iface - Deletes all mesh paths associated with a given iface
592*4882a593Smuzhiyun *
593*4882a593Smuzhiyun * This function deletes both mesh paths as well as mesh portal paths.
594*4882a593Smuzhiyun *
595*4882a593Smuzhiyun * @sdata: interface data to match
596*4882a593Smuzhiyun *
597*4882a593Smuzhiyun */
mesh_path_flush_by_iface(struct ieee80211_sub_if_data * sdata)598*4882a593Smuzhiyun void mesh_path_flush_by_iface(struct ieee80211_sub_if_data *sdata)
599*4882a593Smuzhiyun {
600*4882a593Smuzhiyun table_flush_by_iface(sdata->u.mesh.mesh_paths);
601*4882a593Smuzhiyun table_flush_by_iface(sdata->u.mesh.mpp_paths);
602*4882a593Smuzhiyun }
603*4882a593Smuzhiyun
604*4882a593Smuzhiyun /**
605*4882a593Smuzhiyun * table_path_del - delete a path from the mesh or mpp table
606*4882a593Smuzhiyun *
607*4882a593Smuzhiyun * @tbl: mesh or mpp path table
608*4882a593Smuzhiyun * @sdata: local subif
609*4882a593Smuzhiyun * @addr: dst address (ETH_ALEN length)
610*4882a593Smuzhiyun *
611*4882a593Smuzhiyun * Returns: 0 if successful
612*4882a593Smuzhiyun */
table_path_del(struct mesh_table * tbl,struct ieee80211_sub_if_data * sdata,const u8 * addr)613*4882a593Smuzhiyun static int table_path_del(struct mesh_table *tbl,
614*4882a593Smuzhiyun struct ieee80211_sub_if_data *sdata,
615*4882a593Smuzhiyun const u8 *addr)
616*4882a593Smuzhiyun {
617*4882a593Smuzhiyun struct mesh_path *mpath;
618*4882a593Smuzhiyun
619*4882a593Smuzhiyun spin_lock_bh(&tbl->walk_lock);
620*4882a593Smuzhiyun mpath = rhashtable_lookup_fast(&tbl->rhead, addr, mesh_rht_params);
621*4882a593Smuzhiyun if (!mpath) {
622*4882a593Smuzhiyun spin_unlock_bh(&tbl->walk_lock);
623*4882a593Smuzhiyun return -ENXIO;
624*4882a593Smuzhiyun }
625*4882a593Smuzhiyun
626*4882a593Smuzhiyun __mesh_path_del(tbl, mpath);
627*4882a593Smuzhiyun spin_unlock_bh(&tbl->walk_lock);
628*4882a593Smuzhiyun return 0;
629*4882a593Smuzhiyun }
630*4882a593Smuzhiyun
631*4882a593Smuzhiyun
632*4882a593Smuzhiyun /**
633*4882a593Smuzhiyun * mesh_path_del - delete a mesh path from the table
634*4882a593Smuzhiyun *
635*4882a593Smuzhiyun * @addr: dst address (ETH_ALEN length)
636*4882a593Smuzhiyun * @sdata: local subif
637*4882a593Smuzhiyun *
638*4882a593Smuzhiyun * Returns: 0 if successful
639*4882a593Smuzhiyun */
mesh_path_del(struct ieee80211_sub_if_data * sdata,const u8 * addr)640*4882a593Smuzhiyun int mesh_path_del(struct ieee80211_sub_if_data *sdata, const u8 *addr)
641*4882a593Smuzhiyun {
642*4882a593Smuzhiyun int err;
643*4882a593Smuzhiyun
644*4882a593Smuzhiyun /* flush relevant mpp entries first */
645*4882a593Smuzhiyun mpp_flush_by_proxy(sdata, addr);
646*4882a593Smuzhiyun
647*4882a593Smuzhiyun err = table_path_del(sdata->u.mesh.mesh_paths, sdata, addr);
648*4882a593Smuzhiyun sdata->u.mesh.mesh_paths_generation++;
649*4882a593Smuzhiyun return err;
650*4882a593Smuzhiyun }
651*4882a593Smuzhiyun
652*4882a593Smuzhiyun /**
653*4882a593Smuzhiyun * mesh_path_tx_pending - sends pending frames in a mesh path queue
654*4882a593Smuzhiyun *
655*4882a593Smuzhiyun * @mpath: mesh path to activate
656*4882a593Smuzhiyun *
657*4882a593Smuzhiyun * Locking: the state_lock of the mpath structure must NOT be held when calling
658*4882a593Smuzhiyun * this function.
659*4882a593Smuzhiyun */
mesh_path_tx_pending(struct mesh_path * mpath)660*4882a593Smuzhiyun void mesh_path_tx_pending(struct mesh_path *mpath)
661*4882a593Smuzhiyun {
662*4882a593Smuzhiyun if (mpath->flags & MESH_PATH_ACTIVE)
663*4882a593Smuzhiyun ieee80211_add_pending_skbs(mpath->sdata->local,
664*4882a593Smuzhiyun &mpath->frame_queue);
665*4882a593Smuzhiyun }
666*4882a593Smuzhiyun
667*4882a593Smuzhiyun /**
668*4882a593Smuzhiyun * mesh_path_send_to_gates - sends pending frames to all known mesh gates
669*4882a593Smuzhiyun *
670*4882a593Smuzhiyun * @mpath: mesh path whose queue will be emptied
671*4882a593Smuzhiyun *
672*4882a593Smuzhiyun * If there is only one gate, the frames are transferred from the failed mpath
673*4882a593Smuzhiyun * queue to that gate's queue. If there are more than one gates, the frames
674*4882a593Smuzhiyun * are copied from each gate to the next. After frames are copied, the
675*4882a593Smuzhiyun * mpath queues are emptied onto the transmission queue.
676*4882a593Smuzhiyun */
mesh_path_send_to_gates(struct mesh_path * mpath)677*4882a593Smuzhiyun int mesh_path_send_to_gates(struct mesh_path *mpath)
678*4882a593Smuzhiyun {
679*4882a593Smuzhiyun struct ieee80211_sub_if_data *sdata = mpath->sdata;
680*4882a593Smuzhiyun struct mesh_table *tbl;
681*4882a593Smuzhiyun struct mesh_path *from_mpath = mpath;
682*4882a593Smuzhiyun struct mesh_path *gate;
683*4882a593Smuzhiyun bool copy = false;
684*4882a593Smuzhiyun
685*4882a593Smuzhiyun tbl = sdata->u.mesh.mesh_paths;
686*4882a593Smuzhiyun
687*4882a593Smuzhiyun rcu_read_lock();
688*4882a593Smuzhiyun hlist_for_each_entry_rcu(gate, &tbl->known_gates, gate_list) {
689*4882a593Smuzhiyun if (gate->flags & MESH_PATH_ACTIVE) {
690*4882a593Smuzhiyun mpath_dbg(sdata, "Forwarding to %pM\n", gate->dst);
691*4882a593Smuzhiyun mesh_path_move_to_queue(gate, from_mpath, copy);
692*4882a593Smuzhiyun from_mpath = gate;
693*4882a593Smuzhiyun copy = true;
694*4882a593Smuzhiyun } else {
695*4882a593Smuzhiyun mpath_dbg(sdata,
696*4882a593Smuzhiyun "Not forwarding to %pM (flags %#x)\n",
697*4882a593Smuzhiyun gate->dst, gate->flags);
698*4882a593Smuzhiyun }
699*4882a593Smuzhiyun }
700*4882a593Smuzhiyun
701*4882a593Smuzhiyun hlist_for_each_entry_rcu(gate, &tbl->known_gates, gate_list) {
702*4882a593Smuzhiyun mpath_dbg(sdata, "Sending to %pM\n", gate->dst);
703*4882a593Smuzhiyun mesh_path_tx_pending(gate);
704*4882a593Smuzhiyun }
705*4882a593Smuzhiyun rcu_read_unlock();
706*4882a593Smuzhiyun
707*4882a593Smuzhiyun return (from_mpath == mpath) ? -EHOSTUNREACH : 0;
708*4882a593Smuzhiyun }
709*4882a593Smuzhiyun
710*4882a593Smuzhiyun /**
711*4882a593Smuzhiyun * mesh_path_discard_frame - discard a frame whose path could not be resolved
712*4882a593Smuzhiyun *
713*4882a593Smuzhiyun * @skb: frame to discard
714*4882a593Smuzhiyun * @sdata: network subif the frame was to be sent through
715*4882a593Smuzhiyun *
716*4882a593Smuzhiyun * Locking: the function must me called within a rcu_read_lock region
717*4882a593Smuzhiyun */
mesh_path_discard_frame(struct ieee80211_sub_if_data * sdata,struct sk_buff * skb)718*4882a593Smuzhiyun void mesh_path_discard_frame(struct ieee80211_sub_if_data *sdata,
719*4882a593Smuzhiyun struct sk_buff *skb)
720*4882a593Smuzhiyun {
721*4882a593Smuzhiyun ieee80211_free_txskb(&sdata->local->hw, skb);
722*4882a593Smuzhiyun sdata->u.mesh.mshstats.dropped_frames_no_route++;
723*4882a593Smuzhiyun }
724*4882a593Smuzhiyun
725*4882a593Smuzhiyun /**
726*4882a593Smuzhiyun * mesh_path_flush_pending - free the pending queue of a mesh path
727*4882a593Smuzhiyun *
728*4882a593Smuzhiyun * @mpath: mesh path whose queue has to be freed
729*4882a593Smuzhiyun *
730*4882a593Smuzhiyun * Locking: the function must me called within a rcu_read_lock region
731*4882a593Smuzhiyun */
mesh_path_flush_pending(struct mesh_path * mpath)732*4882a593Smuzhiyun void mesh_path_flush_pending(struct mesh_path *mpath)
733*4882a593Smuzhiyun {
734*4882a593Smuzhiyun struct sk_buff *skb;
735*4882a593Smuzhiyun
736*4882a593Smuzhiyun while ((skb = skb_dequeue(&mpath->frame_queue)) != NULL)
737*4882a593Smuzhiyun mesh_path_discard_frame(mpath->sdata, skb);
738*4882a593Smuzhiyun }
739*4882a593Smuzhiyun
740*4882a593Smuzhiyun /**
741*4882a593Smuzhiyun * mesh_path_fix_nexthop - force a specific next hop for a mesh path
742*4882a593Smuzhiyun *
743*4882a593Smuzhiyun * @mpath: the mesh path to modify
744*4882a593Smuzhiyun * @next_hop: the next hop to force
745*4882a593Smuzhiyun *
746*4882a593Smuzhiyun * Locking: this function must be called holding mpath->state_lock
747*4882a593Smuzhiyun */
mesh_path_fix_nexthop(struct mesh_path * mpath,struct sta_info * next_hop)748*4882a593Smuzhiyun void mesh_path_fix_nexthop(struct mesh_path *mpath, struct sta_info *next_hop)
749*4882a593Smuzhiyun {
750*4882a593Smuzhiyun spin_lock_bh(&mpath->state_lock);
751*4882a593Smuzhiyun mesh_path_assign_nexthop(mpath, next_hop);
752*4882a593Smuzhiyun mpath->sn = 0xffff;
753*4882a593Smuzhiyun mpath->metric = 0;
754*4882a593Smuzhiyun mpath->hop_count = 0;
755*4882a593Smuzhiyun mpath->exp_time = 0;
756*4882a593Smuzhiyun mpath->flags = MESH_PATH_FIXED | MESH_PATH_SN_VALID;
757*4882a593Smuzhiyun mesh_path_activate(mpath);
758*4882a593Smuzhiyun spin_unlock_bh(&mpath->state_lock);
759*4882a593Smuzhiyun ewma_mesh_fail_avg_init(&next_hop->mesh->fail_avg);
760*4882a593Smuzhiyun /* init it at a low value - 0 start is tricky */
761*4882a593Smuzhiyun ewma_mesh_fail_avg_add(&next_hop->mesh->fail_avg, 1);
762*4882a593Smuzhiyun mesh_path_tx_pending(mpath);
763*4882a593Smuzhiyun }
764*4882a593Smuzhiyun
mesh_pathtbl_init(struct ieee80211_sub_if_data * sdata)765*4882a593Smuzhiyun int mesh_pathtbl_init(struct ieee80211_sub_if_data *sdata)
766*4882a593Smuzhiyun {
767*4882a593Smuzhiyun struct mesh_table *tbl_path, *tbl_mpp;
768*4882a593Smuzhiyun int ret;
769*4882a593Smuzhiyun
770*4882a593Smuzhiyun tbl_path = mesh_table_alloc();
771*4882a593Smuzhiyun if (!tbl_path)
772*4882a593Smuzhiyun return -ENOMEM;
773*4882a593Smuzhiyun
774*4882a593Smuzhiyun tbl_mpp = mesh_table_alloc();
775*4882a593Smuzhiyun if (!tbl_mpp) {
776*4882a593Smuzhiyun ret = -ENOMEM;
777*4882a593Smuzhiyun goto free_path;
778*4882a593Smuzhiyun }
779*4882a593Smuzhiyun
780*4882a593Smuzhiyun sdata->u.mesh.mesh_paths = tbl_path;
781*4882a593Smuzhiyun sdata->u.mesh.mpp_paths = tbl_mpp;
782*4882a593Smuzhiyun
783*4882a593Smuzhiyun return 0;
784*4882a593Smuzhiyun
785*4882a593Smuzhiyun free_path:
786*4882a593Smuzhiyun mesh_table_free(tbl_path);
787*4882a593Smuzhiyun return ret;
788*4882a593Smuzhiyun }
789*4882a593Smuzhiyun
790*4882a593Smuzhiyun static
mesh_path_tbl_expire(struct ieee80211_sub_if_data * sdata,struct mesh_table * tbl)791*4882a593Smuzhiyun void mesh_path_tbl_expire(struct ieee80211_sub_if_data *sdata,
792*4882a593Smuzhiyun struct mesh_table *tbl)
793*4882a593Smuzhiyun {
794*4882a593Smuzhiyun struct mesh_path *mpath;
795*4882a593Smuzhiyun struct hlist_node *n;
796*4882a593Smuzhiyun
797*4882a593Smuzhiyun spin_lock_bh(&tbl->walk_lock);
798*4882a593Smuzhiyun hlist_for_each_entry_safe(mpath, n, &tbl->walk_head, walk_list) {
799*4882a593Smuzhiyun if ((!(mpath->flags & MESH_PATH_RESOLVING)) &&
800*4882a593Smuzhiyun (!(mpath->flags & MESH_PATH_FIXED)) &&
801*4882a593Smuzhiyun time_after(jiffies, mpath->exp_time + MESH_PATH_EXPIRE))
802*4882a593Smuzhiyun __mesh_path_del(tbl, mpath);
803*4882a593Smuzhiyun }
804*4882a593Smuzhiyun spin_unlock_bh(&tbl->walk_lock);
805*4882a593Smuzhiyun }
806*4882a593Smuzhiyun
mesh_path_expire(struct ieee80211_sub_if_data * sdata)807*4882a593Smuzhiyun void mesh_path_expire(struct ieee80211_sub_if_data *sdata)
808*4882a593Smuzhiyun {
809*4882a593Smuzhiyun mesh_path_tbl_expire(sdata, sdata->u.mesh.mesh_paths);
810*4882a593Smuzhiyun mesh_path_tbl_expire(sdata, sdata->u.mesh.mpp_paths);
811*4882a593Smuzhiyun }
812*4882a593Smuzhiyun
mesh_pathtbl_unregister(struct ieee80211_sub_if_data * sdata)813*4882a593Smuzhiyun void mesh_pathtbl_unregister(struct ieee80211_sub_if_data *sdata)
814*4882a593Smuzhiyun {
815*4882a593Smuzhiyun mesh_table_free(sdata->u.mesh.mesh_paths);
816*4882a593Smuzhiyun mesh_table_free(sdata->u.mesh.mpp_paths);
817*4882a593Smuzhiyun }
818