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