blob: 1c9412a29ca3bdcf196c2020b2abc4175e8e4b9a [file] [log] [blame]
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01001/*
Rui Paulo264d9b72009-11-09 23:46:58 +00002 * Copyright (c) 2008, 2009 open80211s Ltd.
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +01003 * Author: Luis Carlos Cobo <luisca@cozybit.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
9
10#include <linux/etherdevice.h>
11#include <linux/list.h>
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +010012#include <linux/random.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +010014#include <linux/spinlock.h>
15#include <linux/string.h>
16#include <net/mac80211.h>
Javier Cardona4777be42011-09-07 17:49:52 -070017#include "wme.h"
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +010018#include "ieee80211_i.h"
19#include "mesh.h"
20
Bob Copeland74932952016-03-18 22:03:24 -040021static void mesh_path_free_rcu(struct mesh_table *tbl, struct mesh_path *mpath);
22
Bob Copeland60854fd2016-03-02 10:09:20 -050023static u32 mesh_table_hash(const void *addr, u32 len, u32 seed)
24{
25 /* Use last four bytes of hw addr as hash index */
26 return jhash_1word(*(u32 *)(addr+2), seed);
27}
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +010028
Bob Copeland60854fd2016-03-02 10:09:20 -050029static const struct rhashtable_params mesh_rht_params = {
30 .nelem_hint = 2,
31 .automatic_shrinking = true,
32 .key_len = ETH_ALEN,
33 .key_offset = offsetof(struct mesh_path, dst),
34 .head_offset = offsetof(struct mesh_path, rhash),
35 .hashfn = mesh_table_hash,
36};
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +010037
Johannes Bergbf7cd942013-02-15 14:40:31 +010038static inline bool mpath_expired(struct mesh_path *mpath)
39{
40 return (mpath->flags & MESH_PATH_ACTIVE) &&
41 time_after(jiffies, mpath->exp_time) &&
42 !(mpath->flags & MESH_PATH_FIXED);
43}
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +010044
Bob Copeland74932952016-03-18 22:03:24 -040045static void mesh_path_rht_free(void *ptr, void *tblptr)
Johannes Berg349eb8c2011-05-14 11:56:16 +020046{
Bob Copeland60854fd2016-03-02 10:09:20 -050047 struct mesh_path *mpath = ptr;
Bob Copeland74932952016-03-18 22:03:24 -040048 struct mesh_table *tbl = tblptr;
49
50 mesh_path_free_rcu(tbl, mpath);
Johannes Berg349eb8c2011-05-14 11:56:16 +020051}
52
Bob Copeland60854fd2016-03-02 10:09:20 -050053static struct mesh_table *mesh_table_alloc(void)
Johannes Berg349eb8c2011-05-14 11:56:16 +020054{
Johannes Berg6b86bd62011-05-12 13:38:50 +020055 struct mesh_table *newtbl;
56
Javier Cardonad676ff42011-05-17 16:13:34 -070057 newtbl = kmalloc(sizeof(struct mesh_table), GFP_ATOMIC);
Johannes Berg6b86bd62011-05-12 13:38:50 +020058 if (!newtbl)
59 return NULL;
60
Bob Copeland60854fd2016-03-02 10:09:20 -050061 newtbl->known_gates = kzalloc(sizeof(struct hlist_head), GFP_ATOMIC);
62 if (!newtbl->known_gates) {
Johannes Berg6b86bd62011-05-12 13:38:50 +020063 kfree(newtbl);
64 return NULL;
65 }
Bob Copeland60854fd2016-03-02 10:09:20 -050066 INIT_HLIST_HEAD(newtbl->known_gates);
Johannes Berg6b86bd62011-05-12 13:38:50 +020067 atomic_set(&newtbl->entries, 0);
Javier Cardona5ee68e52011-08-09 16:45:08 -070068 spin_lock_init(&newtbl->gates_lock);
Johannes Berg6b86bd62011-05-12 13:38:50 +020069
70 return newtbl;
71}
72
Bob Copeland60854fd2016-03-02 10:09:20 -050073static void mesh_table_free(struct mesh_table *tbl)
Javier Cardona18889232009-08-10 12:15:52 -070074{
Bob Copeland60854fd2016-03-02 10:09:20 -050075 rhashtable_free_and_destroy(&tbl->rhead,
Bob Copeland74932952016-03-18 22:03:24 -040076 mesh_path_rht_free, tbl);
Javier Cardona18889232009-08-10 12:15:52 -070077 kfree(tbl);
78}
79
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +010080/**
81 *
82 * mesh_path_assign_nexthop - update mesh path next hop
83 *
84 * @mpath: mesh path to update
85 * @sta: next hop to assign
86 *
87 * Locking: mpath->state_lock must be held when calling this function
88 */
89void mesh_path_assign_nexthop(struct mesh_path *mpath, struct sta_info *sta)
90{
Javier Cardona10c836d2009-07-09 14:42:16 -070091 struct sk_buff *skb;
92 struct ieee80211_hdr *hdr;
Javier Cardona10c836d2009-07-09 14:42:16 -070093 unsigned long flags;
94
Johannes Bergd0709a62008-02-25 16:27:46 +010095 rcu_assign_pointer(mpath->next_hop, sta);
Javier Cardona10c836d2009-07-09 14:42:16 -070096
Javier Cardona10c836d2009-07-09 14:42:16 -070097 spin_lock_irqsave(&mpath->frame_queue.lock, flags);
Thomas Pedersenb22bd522012-08-09 18:15:39 -070098 skb_queue_walk(&mpath->frame_queue, skb) {
Javier Cardona10c836d2009-07-09 14:42:16 -070099 hdr = (struct ieee80211_hdr *) skb->data;
100 memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN);
Thomas Pedersen7e3c8862011-11-24 17:15:21 -0800101 memcpy(hdr->addr2, mpath->sdata->vif.addr, ETH_ALEN);
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100102 ieee80211_mps_set_frame_flags(sta->sdata, sta, hdr);
Javier Cardona10c836d2009-07-09 14:42:16 -0700103 }
104
Javier Cardona10c836d2009-07-09 14:42:16 -0700105 spin_unlock_irqrestore(&mpath->frame_queue.lock, flags);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100106}
107
Javier Cardona5ee68e52011-08-09 16:45:08 -0700108static void prepare_for_gate(struct sk_buff *skb, char *dst_addr,
109 struct mesh_path *gate_mpath)
110{
111 struct ieee80211_hdr *hdr;
112 struct ieee80211s_hdr *mshdr;
113 int mesh_hdrlen, hdrlen;
114 char *next_hop;
115
116 hdr = (struct ieee80211_hdr *) skb->data;
117 hdrlen = ieee80211_hdrlen(hdr->frame_control);
118 mshdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
119
120 if (!(mshdr->flags & MESH_FLAGS_AE)) {
121 /* size of the fixed part of the mesh header */
122 mesh_hdrlen = 6;
123
124 /* make room for the two extended addresses */
125 skb_push(skb, 2 * ETH_ALEN);
126 memmove(skb->data, hdr, hdrlen + mesh_hdrlen);
127
128 hdr = (struct ieee80211_hdr *) skb->data;
129
130 /* we preserve the previous mesh header and only add
131 * the new addreses */
132 mshdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
133 mshdr->flags = MESH_FLAGS_AE_A5_A6;
134 memcpy(mshdr->eaddr1, hdr->addr3, ETH_ALEN);
135 memcpy(mshdr->eaddr2, hdr->addr4, ETH_ALEN);
136 }
137
138 /* update next hop */
139 hdr = (struct ieee80211_hdr *) skb->data;
140 rcu_read_lock();
141 next_hop = rcu_dereference(gate_mpath->next_hop)->sta.addr;
142 memcpy(hdr->addr1, next_hop, ETH_ALEN);
143 rcu_read_unlock();
Thomas Pedersen7e3c8862011-11-24 17:15:21 -0800144 memcpy(hdr->addr2, gate_mpath->sdata->vif.addr, ETH_ALEN);
Javier Cardona5ee68e52011-08-09 16:45:08 -0700145 memcpy(hdr->addr3, dst_addr, ETH_ALEN);
146}
147
148/**
149 *
150 * mesh_path_move_to_queue - Move or copy frames from one mpath queue to another
151 *
152 * This function is used to transfer or copy frames from an unresolved mpath to
153 * a gate mpath. The function also adds the Address Extension field and
154 * updates the next hop.
155 *
156 * If a frame already has an Address Extension field, only the next hop and
157 * destination addresses are updated.
158 *
159 * The gate mpath must be an active mpath with a valid mpath->next_hop.
160 *
161 * @mpath: An active mpath the frames will be sent to (i.e. the gate)
162 * @from_mpath: The failed mpath
163 * @copy: When true, copy all the frames to the new mpath queue. When false,
164 * move them.
165 */
166static void mesh_path_move_to_queue(struct mesh_path *gate_mpath,
167 struct mesh_path *from_mpath,
168 bool copy)
169{
Thomas Pedersen4bd4c2d2012-08-09 18:15:40 -0700170 struct sk_buff *skb, *fskb, *tmp;
171 struct sk_buff_head failq;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700172 unsigned long flags;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700173
Johannes Berg8c5bb1f2014-04-29 17:55:26 +0200174 if (WARN_ON(gate_mpath == from_mpath))
175 return;
176 if (WARN_ON(!gate_mpath->next_hop))
177 return;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700178
Javier Cardona5ee68e52011-08-09 16:45:08 -0700179 __skb_queue_head_init(&failq);
180
181 spin_lock_irqsave(&from_mpath->frame_queue.lock, flags);
182 skb_queue_splice_init(&from_mpath->frame_queue, &failq);
183 spin_unlock_irqrestore(&from_mpath->frame_queue.lock, flags);
184
Thomas Pedersen4bd4c2d2012-08-09 18:15:40 -0700185 skb_queue_walk_safe(&failq, fskb, tmp) {
186 if (skb_queue_len(&gate_mpath->frame_queue) >=
187 MESH_FRAME_QUEUE_LEN) {
188 mpath_dbg(gate_mpath->sdata, "mpath queue full!\n");
189 break;
John W. Linville817a53d2011-08-24 15:12:41 -0400190 }
Javier Cardona5ee68e52011-08-09 16:45:08 -0700191
Thomas Pedersen4bd4c2d2012-08-09 18:15:40 -0700192 skb = skb_copy(fskb, GFP_ATOMIC);
193 if (WARN_ON(!skb))
194 break;
195
Javier Cardona5ee68e52011-08-09 16:45:08 -0700196 prepare_for_gate(skb, gate_mpath->dst, gate_mpath);
Thomas Pedersen4bd4c2d2012-08-09 18:15:40 -0700197 skb_queue_tail(&gate_mpath->frame_queue, skb);
198
199 if (copy)
200 continue;
201
202 __skb_unlink(fskb, &failq);
203 kfree_skb(fskb);
Javier Cardona5ee68e52011-08-09 16:45:08 -0700204 }
205
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200206 mpath_dbg(gate_mpath->sdata, "Mpath queue for gate %pM has %d frames\n",
207 gate_mpath->dst, skb_queue_len(&gate_mpath->frame_queue));
Javier Cardona5ee68e52011-08-09 16:45:08 -0700208
209 if (!copy)
210 return;
211
212 spin_lock_irqsave(&from_mpath->frame_queue.lock, flags);
213 skb_queue_splice(&failq, &from_mpath->frame_queue);
214 spin_unlock_irqrestore(&from_mpath->frame_queue.lock, flags);
215}
216
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100217
Johannes Berg4a3cb702013-02-12 16:43:19 +0100218static struct mesh_path *mpath_lookup(struct mesh_table *tbl, const u8 *dst,
219 struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100220{
221 struct mesh_path *mpath;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100222
Bob Copeland60854fd2016-03-02 10:09:20 -0500223 mpath = rhashtable_lookup_fast(&tbl->rhead, dst, mesh_rht_params);
224
225 if (mpath && mpath_expired(mpath)) {
226 spin_lock_bh(&mpath->state_lock);
227 mpath->flags &= ~MESH_PATH_ACTIVE;
228 spin_unlock_bh(&mpath->state_lock);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100229 }
Bob Copeland60854fd2016-03-02 10:09:20 -0500230 return mpath;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100231}
232
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100233/**
234 * mesh_path_lookup - look up a path in the mesh path table
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100235 * @sdata: local subif
Johannes Bergbf7cd942013-02-15 14:40:31 +0100236 * @dst: hardware address (ETH_ALEN length) of destination
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100237 *
238 * Returns: pointer to the mesh path structure, or NULL if not found
239 *
240 * Locking: must be called within a read rcu section.
241 */
Johannes Bergbf7cd942013-02-15 14:40:31 +0100242struct mesh_path *
243mesh_path_lookup(struct ieee80211_sub_if_data *sdata, const u8 *dst)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100244{
Bob Copeland60854fd2016-03-02 10:09:20 -0500245 return mpath_lookup(sdata->u.mesh.mesh_paths, dst, sdata);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100246}
247
Johannes Bergbf7cd942013-02-15 14:40:31 +0100248struct mesh_path *
249mpp_path_lookup(struct ieee80211_sub_if_data *sdata, const u8 *dst)
YanBo79617de2008-09-22 13:30:32 +0800250{
Bob Copeland60854fd2016-03-02 10:09:20 -0500251 return mpath_lookup(sdata->u.mesh.mpp_paths, dst, sdata);
YanBo79617de2008-09-22 13:30:32 +0800252}
253
Bob Copeland60854fd2016-03-02 10:09:20 -0500254static struct mesh_path *
255__mesh_path_lookup_by_idx(struct mesh_table *tbl, int idx)
256{
257 int i = 0, ret;
258 struct mesh_path *mpath = NULL;
259 struct rhashtable_iter iter;
260
261 ret = rhashtable_walk_init(&tbl->rhead, &iter, GFP_ATOMIC);
262 if (ret)
263 return NULL;
264
265 ret = rhashtable_walk_start(&iter);
266 if (ret && ret != -EAGAIN)
267 goto err;
268
269 while ((mpath = rhashtable_walk_next(&iter))) {
270 if (IS_ERR(mpath) && PTR_ERR(mpath) == -EAGAIN)
271 continue;
272 if (IS_ERR(mpath))
273 break;
274 if (i++ == idx)
275 break;
276 }
277err:
278 rhashtable_walk_stop(&iter);
279 rhashtable_walk_exit(&iter);
280
281 if (IS_ERR(mpath) || !mpath)
282 return NULL;
283
284 if (mpath_expired(mpath)) {
285 spin_lock_bh(&mpath->state_lock);
286 mpath->flags &= ~MESH_PATH_ACTIVE;
287 spin_unlock_bh(&mpath->state_lock);
288 }
289 return mpath;
290}
YanBo79617de2008-09-22 13:30:32 +0800291
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100292/**
293 * mesh_path_lookup_by_idx - look up a path in the mesh path table by its index
294 * @idx: index
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200295 * @sdata: local subif, or NULL for all entries
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100296 *
297 * Returns: pointer to the mesh path structure, or NULL if not found.
298 *
299 * Locking: must be called within a read rcu section.
300 */
Johannes Bergbf7cd942013-02-15 14:40:31 +0100301struct mesh_path *
302mesh_path_lookup_by_idx(struct ieee80211_sub_if_data *sdata, int idx)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100303{
Bob Copeland60854fd2016-03-02 10:09:20 -0500304 return __mesh_path_lookup_by_idx(sdata->u.mesh.mesh_paths, idx);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100305}
306
Javier Cardona5ee68e52011-08-09 16:45:08 -0700307/**
Henning Roggea2db2ed2014-09-12 08:58:50 +0200308 * mpp_path_lookup_by_idx - look up a path in the proxy path table by its index
309 * @idx: index
310 * @sdata: local subif, or NULL for all entries
311 *
312 * Returns: pointer to the proxy path structure, or NULL if not found.
313 *
314 * Locking: must be called within a read rcu section.
315 */
316struct mesh_path *
317mpp_path_lookup_by_idx(struct ieee80211_sub_if_data *sdata, int idx)
318{
Bob Copeland60854fd2016-03-02 10:09:20 -0500319 return __mesh_path_lookup_by_idx(sdata->u.mesh.mpp_paths, idx);
Henning Roggea2db2ed2014-09-12 08:58:50 +0200320}
321
322/**
Johannes Berg30be52e2011-11-21 11:23:50 +0100323 * mesh_path_add_gate - add the given mpath to a mesh gate to our path table
324 * @mpath: gate path to add to table
Javier Cardona5ee68e52011-08-09 16:45:08 -0700325 */
Johannes Berg30be52e2011-11-21 11:23:50 +0100326int mesh_path_add_gate(struct mesh_path *mpath)
Javier Cardona5ee68e52011-08-09 16:45:08 -0700327{
Johannes Berg30be52e2011-11-21 11:23:50 +0100328 struct mesh_table *tbl;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700329 int err;
330
331 rcu_read_lock();
Bob Copeland60854fd2016-03-02 10:09:20 -0500332 tbl = mpath->sdata->u.mesh.mesh_paths;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700333
Bob Copeland947c2a02016-02-28 20:03:59 -0500334 spin_lock_bh(&mpath->state_lock);
335 if (mpath->is_gate) {
336 err = -EEXIST;
337 spin_unlock_bh(&mpath->state_lock);
Javier Cardona5ee68e52011-08-09 16:45:08 -0700338 goto err_rcu;
339 }
Javier Cardona5ee68e52011-08-09 16:45:08 -0700340 mpath->is_gate = true;
341 mpath->sdata->u.mesh.num_gates++;
Bob Copeland947c2a02016-02-28 20:03:59 -0500342
343 spin_lock(&tbl->gates_lock);
344 hlist_add_head_rcu(&mpath->gate_list, tbl->known_gates);
345 spin_unlock(&tbl->gates_lock);
346
347 spin_unlock_bh(&mpath->state_lock);
348
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200349 mpath_dbg(mpath->sdata,
350 "Mesh path: Recorded new gate: %pM. %d known gates\n",
351 mpath->dst, mpath->sdata->u.mesh.num_gates);
Johannes Bergbf7cd942013-02-15 14:40:31 +0100352 err = 0;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700353err_rcu:
354 rcu_read_unlock();
355 return err;
356}
357
358/**
359 * mesh_gate_del - remove a mesh gate from the list of known gates
360 * @tbl: table which holds our list of known gates
361 * @mpath: gate mpath
Javier Cardona5ee68e52011-08-09 16:45:08 -0700362 */
Johannes Bergbf7cd942013-02-15 14:40:31 +0100363static void mesh_gate_del(struct mesh_table *tbl, struct mesh_path *mpath)
Javier Cardona5ee68e52011-08-09 16:45:08 -0700364{
Bob Copeland947c2a02016-02-28 20:03:59 -0500365 lockdep_assert_held(&mpath->state_lock);
366 if (!mpath->is_gate)
367 return;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700368
Bob Copeland947c2a02016-02-28 20:03:59 -0500369 mpath->is_gate = false;
370 spin_lock_bh(&tbl->gates_lock);
371 hlist_del_rcu(&mpath->gate_list);
372 mpath->sdata->u.mesh.num_gates--;
373 spin_unlock_bh(&tbl->gates_lock);
374
375 mpath_dbg(mpath->sdata,
376 "Mesh path: Deleted gate: %pM. %d known gates\n",
377 mpath->dst, mpath->sdata->u.mesh.num_gates);
Javier Cardona5ee68e52011-08-09 16:45:08 -0700378}
379
380/**
Javier Cardona5ee68e52011-08-09 16:45:08 -0700381 * mesh_gate_num - number of gates known to this interface
382 * @sdata: subif data
383 */
384int mesh_gate_num(struct ieee80211_sub_if_data *sdata)
385{
386 return sdata->u.mesh.num_gates;
387}
388
Bob Copelandb15dc382016-02-28 20:03:58 -0500389static
390struct mesh_path *mesh_path_new(struct ieee80211_sub_if_data *sdata,
391 const u8 *dst, gfp_t gfp_flags)
392{
393 struct mesh_path *new_mpath;
394
395 new_mpath = kzalloc(sizeof(struct mesh_path), gfp_flags);
396 if (!new_mpath)
397 return NULL;
398
399 memcpy(new_mpath->dst, dst, ETH_ALEN);
400 eth_broadcast_addr(new_mpath->rann_snd_addr);
401 new_mpath->is_root = false;
402 new_mpath->sdata = sdata;
403 new_mpath->flags = 0;
404 skb_queue_head_init(&new_mpath->frame_queue);
405 new_mpath->timer.data = (unsigned long) new_mpath;
406 new_mpath->timer.function = mesh_path_timer;
407 new_mpath->exp_time = jiffies;
408 spin_lock_init(&new_mpath->state_lock);
409 init_timer(&new_mpath->timer);
410
411 return new_mpath;
412}
413
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100414/**
415 * mesh_path_add - allocate and add a new path to the mesh path table
Johannes Bergbf7cd942013-02-15 14:40:31 +0100416 * @dst: destination address of the path (ETH_ALEN length)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200417 * @sdata: local subif
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100418 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200419 * Returns: 0 on success
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100420 *
421 * State: the initial state of the new path is set to 0
422 */
Bob Copelandae76eef2013-03-29 09:38:39 -0400423struct mesh_path *mesh_path_add(struct ieee80211_sub_if_data *sdata,
424 const u8 *dst)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100425{
Johannes Berg349eb8c2011-05-14 11:56:16 +0200426 struct mesh_table *tbl;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100427 struct mesh_path *mpath, *new_mpath;
Bob Copeland60854fd2016-03-02 10:09:20 -0500428 int ret;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100429
Joe Perchesb203ca32012-05-08 18:56:52 +0000430 if (ether_addr_equal(dst, sdata->vif.addr))
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100431 /* never add ourselves as neighbours */
Bob Copelandae76eef2013-03-29 09:38:39 -0400432 return ERR_PTR(-ENOTSUPP);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100433
434 if (is_multicast_ether_addr(dst))
Bob Copelandae76eef2013-03-29 09:38:39 -0400435 return ERR_PTR(-ENOTSUPP);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100436
Johannes Berg472dbc42008-09-11 00:01:49 +0200437 if (atomic_add_unless(&sdata->u.mesh.mpaths, 1, MESH_MAX_MPATHS) == 0)
Bob Copelandae76eef2013-03-29 09:38:39 -0400438 return ERR_PTR(-ENOSPC);
439
Bob Copelandb15dc382016-02-28 20:03:58 -0500440 new_mpath = mesh_path_new(sdata, dst, GFP_ATOMIC);
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400441 if (!new_mpath)
Bob Copeland60854fd2016-03-02 10:09:20 -0500442 return ERR_PTR(-ENOMEM);
Pavel Emelyanov402d7752008-05-06 18:53:43 +0400443
Bob Copeland60854fd2016-03-02 10:09:20 -0500444 tbl = sdata->u.mesh.mesh_paths;
445 do {
446 ret = rhashtable_lookup_insert_fast(&tbl->rhead,
447 &new_mpath->rhash,
448 mesh_rht_params);
Pavel Emelyanovf84e71a2008-05-06 18:46:36 +0400449
Bob Copeland60854fd2016-03-02 10:09:20 -0500450 if (ret == -EEXIST)
451 mpath = rhashtable_lookup_fast(&tbl->rhead,
452 dst,
453 mesh_rht_params);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100454
Bob Copeland60854fd2016-03-02 10:09:20 -0500455 } while (unlikely(ret == -EEXIST && !mpath));
456
457 if (ret && ret != -EEXIST)
458 return ERR_PTR(ret);
459
460 /* At this point either new_mpath was added, or we found a
461 * matching entry already in the table; in the latter case
462 * free the unnecessary new entry.
463 */
464 if (ret == -EEXIST) {
465 kfree(new_mpath);
466 new_mpath = mpath;
467 }
Bob Copeland2bdaf382016-02-28 20:03:56 -0500468 sdata->u.mesh.mesh_paths_generation++;
Bob Copeland60854fd2016-03-02 10:09:20 -0500469 return new_mpath;
Javier Cardona18889232009-08-10 12:15:52 -0700470}
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100471
Johannes Bergbf7cd942013-02-15 14:40:31 +0100472int mpp_path_add(struct ieee80211_sub_if_data *sdata,
473 const u8 *dst, const u8 *mpp)
YanBo79617de2008-09-22 13:30:32 +0800474{
Johannes Berg349eb8c2011-05-14 11:56:16 +0200475 struct mesh_table *tbl;
Bob Copeland60854fd2016-03-02 10:09:20 -0500476 struct mesh_path *new_mpath;
477 int ret;
YanBo79617de2008-09-22 13:30:32 +0800478
Joe Perchesb203ca32012-05-08 18:56:52 +0000479 if (ether_addr_equal(dst, sdata->vif.addr))
YanBo79617de2008-09-22 13:30:32 +0800480 /* never add ourselves as neighbours */
481 return -ENOTSUPP;
482
483 if (is_multicast_ether_addr(dst))
484 return -ENOTSUPP;
485
Bob Copelandb15dc382016-02-28 20:03:58 -0500486 new_mpath = mesh_path_new(sdata, dst, GFP_ATOMIC);
YanBo79617de2008-09-22 13:30:32 +0800487
Bob Copeland60854fd2016-03-02 10:09:20 -0500488 if (!new_mpath)
489 return -ENOMEM;
YanBo79617de2008-09-22 13:30:32 +0800490
YanBo79617de2008-09-22 13:30:32 +0800491 memcpy(new_mpath->mpp, mpp, ETH_ALEN);
Bob Copeland60854fd2016-03-02 10:09:20 -0500492 tbl = sdata->u.mesh.mpp_paths;
493 ret = rhashtable_lookup_insert_fast(&tbl->rhead,
494 &new_mpath->rhash,
495 mesh_rht_params);
Henning Roggea2db2ed2014-09-12 08:58:50 +0200496
Bob Copeland2bdaf382016-02-28 20:03:56 -0500497 sdata->u.mesh.mpp_paths_generation++;
Bob Copeland60854fd2016-03-02 10:09:20 -0500498 return ret;
YanBo79617de2008-09-22 13:30:32 +0800499}
500
501
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100502/**
503 * mesh_plink_broken - deactivates paths and sends perr when a link breaks
504 *
505 * @sta: broken peer link
506 *
507 * This function must be called from the rate control algorithm if enough
508 * delivery errors suggest that a peer link is no longer usable.
509 */
510void mesh_plink_broken(struct sta_info *sta)
511{
Bob Copeland60854fd2016-03-02 10:09:20 -0500512 struct ieee80211_sub_if_data *sdata = sta->sdata;
513 struct mesh_table *tbl = sdata->u.mesh.mesh_paths;
Johannes Berg15ff6362009-11-17 13:34:04 +0100514 static const u8 bcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100515 struct mesh_path *mpath;
Bob Copeland60854fd2016-03-02 10:09:20 -0500516 struct rhashtable_iter iter;
517 int ret;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100518
Bob Copeland60854fd2016-03-02 10:09:20 -0500519 ret = rhashtable_walk_init(&tbl->rhead, &iter, GFP_ATOMIC);
520 if (ret)
521 return;
522
523 ret = rhashtable_walk_start(&iter);
524 if (ret && ret != -EAGAIN)
525 goto out;
526
527 while ((mpath = rhashtable_walk_next(&iter))) {
528 if (IS_ERR(mpath) && PTR_ERR(mpath) == -EAGAIN)
529 continue;
530 if (IS_ERR(mpath))
531 break;
Andreea-Cristina Bernat2688eba2014-08-17 16:18:02 +0300532 if (rcu_access_pointer(mpath->next_hop) == sta &&
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100533 mpath->flags & MESH_PATH_ACTIVE &&
534 !(mpath->flags & MESH_PATH_FIXED)) {
Javier Cardonaf5e50cd2011-08-29 13:23:05 -0700535 spin_lock_bh(&mpath->state_lock);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100536 mpath->flags &= ~MESH_PATH_ACTIVE;
Rui Paulod19b3bf2009-11-09 23:46:55 +0000537 ++mpath->sn;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100538 spin_unlock_bh(&mpath->state_lock);
Johannes Bergbf7cd942013-02-15 14:40:31 +0100539 mesh_path_error_tx(sdata,
Chun-Yeow Yeohf63f8422013-11-13 15:39:12 +0800540 sdata->u.mesh.mshcfg.element_ttl,
541 mpath->dst, mpath->sn,
542 WLAN_REASON_MESH_PATH_DEST_UNREACHABLE, bcast);
Javier Cardonaf5e50cd2011-08-29 13:23:05 -0700543 }
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100544 }
Bob Copeland60854fd2016-03-02 10:09:20 -0500545out:
546 rhashtable_walk_stop(&iter);
547 rhashtable_walk_exit(&iter);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100548}
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100549
Bob Copeland74932952016-03-18 22:03:24 -0400550static void mesh_path_free_rcu(struct mesh_table *tbl,
551 struct mesh_path *mpath)
Javier Cardona19c50b32011-08-29 13:23:07 -0700552{
Bob Copeland60854fd2016-03-02 10:09:20 -0500553 struct ieee80211_sub_if_data *sdata = mpath->sdata;
Javier Cardona19c50b32011-08-29 13:23:07 -0700554
Bob Copeland947c2a02016-02-28 20:03:59 -0500555 spin_lock_bh(&mpath->state_lock);
Bob Copeland74932952016-03-18 22:03:24 -0400556 mpath->flags |= MESH_PATH_RESOLVING | MESH_PATH_DELETED;
Bob Copeland60854fd2016-03-02 10:09:20 -0500557 mesh_gate_del(tbl, mpath);
Bob Copeland947c2a02016-02-28 20:03:59 -0500558 spin_unlock_bh(&mpath->state_lock);
Bob Copeland74932952016-03-18 22:03:24 -0400559 del_timer_sync(&mpath->timer);
Johannes Bergc2e703a2015-11-17 14:25:21 +0100560 atomic_dec(&sdata->u.mesh.mpaths);
Javier Cardona19c50b32011-08-29 13:23:07 -0700561 atomic_dec(&tbl->entries);
Bob Copeland74932952016-03-18 22:03:24 -0400562 kfree_rcu(mpath, rcu);
563}
564
565static void __mesh_path_del(struct mesh_table *tbl, struct mesh_path *mpath)
566{
567 rhashtable_remove_fast(&tbl->rhead, &mpath->rhash, mesh_rht_params);
568 mesh_path_free_rcu(tbl, mpath);
Javier Cardona19c50b32011-08-29 13:23:07 -0700569}
570
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100571/**
572 * mesh_path_flush_by_nexthop - Deletes mesh paths if their next hop matches
573 *
Ben Hutchings2c530402012-07-10 10:55:09 +0000574 * @sta: mesh peer to match
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100575 *
Luis Carlos Cobob4e08ea2008-02-29 15:46:08 -0800576 * RCU notes: this function is called when a mesh plink transitions from
577 * PLINK_ESTAB to any other state, since PLINK_ESTAB state is the only one that
578 * allows path creation. This will happen before the sta can be freed (because
Johannes Bergd0709a62008-02-25 16:27:46 +0100579 * sta_info_destroy() calls this) so any reader in a rcu read block will be
580 * protected against the plink disappearing.
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100581 */
582void mesh_path_flush_by_nexthop(struct sta_info *sta)
583{
Bob Copeland2bdaf382016-02-28 20:03:56 -0500584 struct ieee80211_sub_if_data *sdata = sta->sdata;
Bob Copeland60854fd2016-03-02 10:09:20 -0500585 struct mesh_table *tbl = sdata->u.mesh.mesh_paths;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100586 struct mesh_path *mpath;
Bob Copeland60854fd2016-03-02 10:09:20 -0500587 struct rhashtable_iter iter;
588 int ret;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100589
Bob Copeland60854fd2016-03-02 10:09:20 -0500590 ret = rhashtable_walk_init(&tbl->rhead, &iter, GFP_ATOMIC);
591 if (ret)
592 return;
593
594 ret = rhashtable_walk_start(&iter);
595 if (ret && ret != -EAGAIN)
596 goto out;
597
598 while ((mpath = rhashtable_walk_next(&iter))) {
599 if (IS_ERR(mpath) && PTR_ERR(mpath) == -EAGAIN)
600 continue;
601 if (IS_ERR(mpath))
602 break;
603
604 if (rcu_access_pointer(mpath->next_hop) == sta)
605 __mesh_path_del(tbl, mpath);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100606 }
Bob Copeland60854fd2016-03-02 10:09:20 -0500607out:
608 rhashtable_walk_stop(&iter);
609 rhashtable_walk_exit(&iter);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100610}
611
Henning Roggebf5a70e2016-02-03 13:58:36 +0100612static void mpp_flush_by_proxy(struct ieee80211_sub_if_data *sdata,
613 const u8 *proxy)
614{
Bob Copeland60854fd2016-03-02 10:09:20 -0500615 struct mesh_table *tbl = sdata->u.mesh.mpp_paths;
616 struct mesh_path *mpath;
617 struct rhashtable_iter iter;
618 int ret;
Henning Roggebf5a70e2016-02-03 13:58:36 +0100619
Bob Copeland60854fd2016-03-02 10:09:20 -0500620 ret = rhashtable_walk_init(&tbl->rhead, &iter, GFP_ATOMIC);
621 if (ret)
622 return;
623
624 ret = rhashtable_walk_start(&iter);
625 if (ret && ret != -EAGAIN)
626 goto out;
627
628 while ((mpath = rhashtable_walk_next(&iter))) {
629 if (IS_ERR(mpath) && PTR_ERR(mpath) == -EAGAIN)
630 continue;
631 if (IS_ERR(mpath))
632 break;
633
634 if (ether_addr_equal(mpath->mpp, proxy))
635 __mesh_path_del(tbl, mpath);
Henning Roggebf5a70e2016-02-03 13:58:36 +0100636 }
Bob Copeland60854fd2016-03-02 10:09:20 -0500637out:
638 rhashtable_walk_stop(&iter);
639 rhashtable_walk_exit(&iter);
Henning Roggebf5a70e2016-02-03 13:58:36 +0100640}
641
Bob Copeland60854fd2016-03-02 10:09:20 -0500642static void table_flush_by_iface(struct mesh_table *tbl)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100643{
644 struct mesh_path *mpath;
Bob Copeland60854fd2016-03-02 10:09:20 -0500645 struct rhashtable_iter iter;
646 int ret;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100647
Bob Copeland60854fd2016-03-02 10:09:20 -0500648 ret = rhashtable_walk_init(&tbl->rhead, &iter, GFP_ATOMIC);
649 if (ret)
650 return;
651
652 ret = rhashtable_walk_start(&iter);
653 if (ret && ret != -EAGAIN)
654 goto out;
655
656 while ((mpath = rhashtable_walk_next(&iter))) {
657 if (IS_ERR(mpath) && PTR_ERR(mpath) == -EAGAIN)
658 continue;
659 if (IS_ERR(mpath))
660 break;
661 __mesh_path_del(tbl, mpath);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100662 }
Bob Copeland60854fd2016-03-02 10:09:20 -0500663out:
664 rhashtable_walk_stop(&iter);
665 rhashtable_walk_exit(&iter);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100666}
667
Javier Cardonaece1a2e2011-08-29 13:23:04 -0700668/**
669 * mesh_path_flush_by_iface - Deletes all mesh paths associated with a given iface
670 *
671 * This function deletes both mesh paths as well as mesh portal paths.
672 *
Ben Hutchings2c530402012-07-10 10:55:09 +0000673 * @sdata: interface data to match
Javier Cardonaece1a2e2011-08-29 13:23:04 -0700674 *
675 */
676void mesh_path_flush_by_iface(struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100677{
Bob Copeland60854fd2016-03-02 10:09:20 -0500678 table_flush_by_iface(sdata->u.mesh.mesh_paths);
679 table_flush_by_iface(sdata->u.mesh.mpp_paths);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100680}
681
682/**
Henning Rogge4cc955d2016-02-03 13:58:38 +0100683 * table_path_del - delete a path from the mesh or mpp table
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100684 *
Henning Rogge4cc955d2016-02-03 13:58:38 +0100685 * @tbl: mesh or mpp path table
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200686 * @sdata: local subif
Henning Rogge4cc955d2016-02-03 13:58:38 +0100687 * @addr: dst address (ETH_ALEN length)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100688 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200689 * Returns: 0 if successful
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100690 */
Bob Copeland60854fd2016-03-02 10:09:20 -0500691static int table_path_del(struct mesh_table *tbl,
Henning Rogge4cc955d2016-02-03 13:58:38 +0100692 struct ieee80211_sub_if_data *sdata,
693 const u8 *addr)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100694{
695 struct mesh_path *mpath;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100696
Bob Copeland60854fd2016-03-02 10:09:20 -0500697 rcu_read_lock();
698 mpath = rhashtable_lookup_fast(&tbl->rhead, addr, mesh_rht_params);
699 if (!mpath) {
700 rcu_read_unlock();
701 return -ENXIO;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100702 }
703
Bob Copeland60854fd2016-03-02 10:09:20 -0500704 __mesh_path_del(tbl, mpath);
705 rcu_read_unlock();
706 return 0;
Henning Rogge4cc955d2016-02-03 13:58:38 +0100707}
708
Bob Copeland60854fd2016-03-02 10:09:20 -0500709
Henning Rogge4cc955d2016-02-03 13:58:38 +0100710/**
711 * mesh_path_del - delete a mesh path from the table
712 *
713 * @addr: dst address (ETH_ALEN length)
714 * @sdata: local subif
715 *
716 * Returns: 0 if successful
717 */
718int mesh_path_del(struct ieee80211_sub_if_data *sdata, const u8 *addr)
719{
Bob Copeland60854fd2016-03-02 10:09:20 -0500720 int err;
Henning Rogge4cc955d2016-02-03 13:58:38 +0100721
722 /* flush relevant mpp entries first */
723 mpp_flush_by_proxy(sdata, addr);
724
Bob Copeland2bdaf382016-02-28 20:03:56 -0500725 err = table_path_del(sdata->u.mesh.mesh_paths, sdata, addr);
726 sdata->u.mesh.mesh_paths_generation++;
Henning Roggeab1c7902016-02-03 13:58:37 +0100727 return err;
728}
729
730/**
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100731 * mesh_path_tx_pending - sends pending frames in a mesh path queue
732 *
733 * @mpath: mesh path to activate
734 *
735 * Locking: the state_lock of the mpath structure must NOT be held when calling
736 * this function.
737 */
738void mesh_path_tx_pending(struct mesh_path *mpath)
739{
Javier Cardona249b4052009-07-07 10:55:03 -0700740 if (mpath->flags & MESH_PATH_ACTIVE)
741 ieee80211_add_pending_skbs(mpath->sdata->local,
742 &mpath->frame_queue);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100743}
744
745/**
Javier Cardona5ee68e52011-08-09 16:45:08 -0700746 * mesh_path_send_to_gates - sends pending frames to all known mesh gates
747 *
748 * @mpath: mesh path whose queue will be emptied
749 *
750 * If there is only one gate, the frames are transferred from the failed mpath
751 * queue to that gate's queue. If there are more than one gates, the frames
752 * are copied from each gate to the next. After frames are copied, the
753 * mpath queues are emptied onto the transmission queue.
754 */
755int mesh_path_send_to_gates(struct mesh_path *mpath)
756{
757 struct ieee80211_sub_if_data *sdata = mpath->sdata;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700758 struct mesh_table *tbl;
759 struct mesh_path *from_mpath = mpath;
Bob Copeland60854fd2016-03-02 10:09:20 -0500760 struct mesh_path *gate;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700761 bool copy = false;
762 struct hlist_head *known_gates;
763
Bob Copeland60854fd2016-03-02 10:09:20 -0500764 tbl = sdata->u.mesh.mesh_paths;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700765 known_gates = tbl->known_gates;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700766
767 if (!known_gates)
768 return -EHOSTUNREACH;
769
Bob Copeland60854fd2016-03-02 10:09:20 -0500770 rcu_read_lock();
Bob Copeland947c2a02016-02-28 20:03:59 -0500771 hlist_for_each_entry_rcu(gate, known_gates, gate_list) {
772 if (gate->flags & MESH_PATH_ACTIVE) {
773 mpath_dbg(sdata, "Forwarding to %pM\n", gate->dst);
774 mesh_path_move_to_queue(gate, from_mpath, copy);
775 from_mpath = gate;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700776 copy = true;
777 } else {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200778 mpath_dbg(sdata,
Johannes Bergd671b2a2015-11-06 11:30:46 +0100779 "Not forwarding to %pM (flags %#x)\n",
Bob Copeland947c2a02016-02-28 20:03:59 -0500780 gate->dst, gate->flags);
Javier Cardona5ee68e52011-08-09 16:45:08 -0700781 }
782 }
783
Bob Copeland947c2a02016-02-28 20:03:59 -0500784 hlist_for_each_entry_rcu(gate, known_gates, gate_list) {
785 mpath_dbg(sdata, "Sending to %pM\n", gate->dst);
786 mesh_path_tx_pending(gate);
Bob Copeland2bdaf382016-02-28 20:03:56 -0500787 }
Bob Copeland60854fd2016-03-02 10:09:20 -0500788 rcu_read_unlock();
Javier Cardona5ee68e52011-08-09 16:45:08 -0700789
790 return (from_mpath == mpath) ? -EHOSTUNREACH : 0;
791}
792
793/**
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100794 * mesh_path_discard_frame - discard a frame whose path could not be resolved
795 *
796 * @skb: frame to discard
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200797 * @sdata: network subif the frame was to be sent through
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100798 *
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100799 * Locking: the function must me called within a rcu_read_lock region
800 */
Johannes Bergbf7cd942013-02-15 14:40:31 +0100801void mesh_path_discard_frame(struct ieee80211_sub_if_data *sdata,
802 struct sk_buff *skb)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100803{
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100804 kfree_skb(skb);
Johannes Berg472dbc42008-09-11 00:01:49 +0200805 sdata->u.mesh.mshstats.dropped_frames_no_route++;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100806}
807
808/**
809 * mesh_path_flush_pending - free the pending queue of a mesh path
810 *
811 * @mpath: mesh path whose queue has to be freed
812 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300813 * Locking: the function must me called within a rcu_read_lock region
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100814 */
815void mesh_path_flush_pending(struct mesh_path *mpath)
816{
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100817 struct sk_buff *skb;
818
Javier Cardona00e3f252011-08-09 16:45:07 -0700819 while ((skb = skb_dequeue(&mpath->frame_queue)) != NULL)
Johannes Bergbf7cd942013-02-15 14:40:31 +0100820 mesh_path_discard_frame(mpath->sdata, skb);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100821}
822
823/**
824 * mesh_path_fix_nexthop - force a specific next hop for a mesh path
825 *
826 * @mpath: the mesh path to modify
827 * @next_hop: the next hop to force
828 *
829 * Locking: this function must be called holding mpath->state_lock
830 */
831void mesh_path_fix_nexthop(struct mesh_path *mpath, struct sta_info *next_hop)
832{
833 spin_lock_bh(&mpath->state_lock);
834 mesh_path_assign_nexthop(mpath, next_hop);
Rui Paulod19b3bf2009-11-09 23:46:55 +0000835 mpath->sn = 0xffff;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100836 mpath->metric = 0;
837 mpath->hop_count = 0;
838 mpath->exp_time = 0;
839 mpath->flags |= MESH_PATH_FIXED;
840 mesh_path_activate(mpath);
841 spin_unlock_bh(&mpath->state_lock);
842 mesh_path_tx_pending(mpath);
843}
844
Bob Copeland2bdaf382016-02-28 20:03:56 -0500845int mesh_pathtbl_init(struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100846{
Johannes Berg349eb8c2011-05-14 11:56:16 +0200847 struct mesh_table *tbl_path, *tbl_mpp;
Dan Carpenter4c5ade42011-08-30 22:16:15 +0300848 int ret;
YanBo79617de2008-09-22 13:30:32 +0800849
Bob Copeland60854fd2016-03-02 10:09:20 -0500850 tbl_path = mesh_table_alloc();
Johannes Berg349eb8c2011-05-14 11:56:16 +0200851 if (!tbl_path)
852 return -ENOMEM;
Javier Cardona5ee68e52011-08-09 16:45:08 -0700853
Bob Copeland60854fd2016-03-02 10:09:20 -0500854 tbl_mpp = mesh_table_alloc();
Johannes Berg349eb8c2011-05-14 11:56:16 +0200855 if (!tbl_mpp) {
Dan Carpenter4c5ade42011-08-30 22:16:15 +0300856 ret = -ENOMEM;
857 goto free_path;
YanBo79617de2008-09-22 13:30:32 +0800858 }
Johannes Berg349eb8c2011-05-14 11:56:16 +0200859
Bob Copeland60854fd2016-03-02 10:09:20 -0500860 rhashtable_init(&tbl_path->rhead, &mesh_rht_params);
861 rhashtable_init(&tbl_mpp->rhead, &mesh_rht_params);
Bob Copeland2bdaf382016-02-28 20:03:56 -0500862
Bob Copeland60854fd2016-03-02 10:09:20 -0500863 sdata->u.mesh.mesh_paths = tbl_path;
864 sdata->u.mesh.mpp_paths = tbl_mpp;
YanBo79617de2008-09-22 13:30:32 +0800865
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100866 return 0;
Dan Carpenter4c5ade42011-08-30 22:16:15 +0300867
Dan Carpenter4c5ade42011-08-30 22:16:15 +0300868free_path:
Bob Copeland60854fd2016-03-02 10:09:20 -0500869 mesh_table_free(tbl_path);
Dan Carpenter4c5ade42011-08-30 22:16:15 +0300870 return ret;
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100871}
872
Bob Copeland60854fd2016-03-02 10:09:20 -0500873static
874void mesh_path_tbl_expire(struct ieee80211_sub_if_data *sdata,
875 struct mesh_table *tbl)
876{
877 struct mesh_path *mpath;
878 struct rhashtable_iter iter;
879 int ret;
880
881 ret = rhashtable_walk_init(&tbl->rhead, &iter, GFP_KERNEL);
882 if (ret)
883 return;
884
885 ret = rhashtable_walk_start(&iter);
886 if (ret && ret != -EAGAIN)
887 goto out;
888
889 while ((mpath = rhashtable_walk_next(&iter))) {
890 if (IS_ERR(mpath) && PTR_ERR(mpath) == -EAGAIN)
891 continue;
892 if (IS_ERR(mpath))
893 break;
894 if ((!(mpath->flags & MESH_PATH_RESOLVING)) &&
895 (!(mpath->flags & MESH_PATH_FIXED)) &&
896 time_after(jiffies, mpath->exp_time + MESH_PATH_EXPIRE))
897 __mesh_path_del(tbl, mpath);
898 }
899out:
900 rhashtable_walk_stop(&iter);
901 rhashtable_walk_exit(&iter);
902}
903
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200904void mesh_path_expire(struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100905{
Bob Copeland60854fd2016-03-02 10:09:20 -0500906 mesh_path_tbl_expire(sdata, sdata->u.mesh.mesh_paths);
907 mesh_path_tbl_expire(sdata, sdata->u.mesh.mpp_paths);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100908}
909
Bob Copeland2bdaf382016-02-28 20:03:56 -0500910void mesh_pathtbl_unregister(struct ieee80211_sub_if_data *sdata)
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100911{
Bob Copeland60854fd2016-03-02 10:09:20 -0500912 mesh_table_free(sdata->u.mesh.mesh_paths);
913 mesh_table_free(sdata->u.mesh.mpp_paths);
Luis Carlos Coboeb2b9312008-02-23 15:17:14 +0100914}