Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (c) 2008, 2009 open80211s Ltd.
0004  * Author:     Luis Carlos Cobo <luisca@cozybit.com>
0005  */
0006 
0007 #include <linux/etherdevice.h>
0008 #include <linux/list.h>
0009 #include <linux/random.h>
0010 #include <linux/slab.h>
0011 #include <linux/spinlock.h>
0012 #include <linux/string.h>
0013 #include <net/mac80211.h>
0014 #include "wme.h"
0015 #include "ieee80211_i.h"
0016 #include "mesh.h"
0017 
0018 static void mesh_path_free_rcu(struct mesh_table *tbl, struct mesh_path *mpath);
0019 
0020 static u32 mesh_table_hash(const void *addr, u32 len, u32 seed)
0021 {
0022     /* Use last four bytes of hw addr as hash index */
0023     return jhash_1word(__get_unaligned_cpu32((u8 *)addr + 2), seed);
0024 }
0025 
0026 static const struct rhashtable_params mesh_rht_params = {
0027     .nelem_hint = 2,
0028     .automatic_shrinking = true,
0029     .key_len = ETH_ALEN,
0030     .key_offset = offsetof(struct mesh_path, dst),
0031     .head_offset = offsetof(struct mesh_path, rhash),
0032     .hashfn = mesh_table_hash,
0033 };
0034 
0035 static inline bool mpath_expired(struct mesh_path *mpath)
0036 {
0037     return (mpath->flags & MESH_PATH_ACTIVE) &&
0038            time_after(jiffies, mpath->exp_time) &&
0039            !(mpath->flags & MESH_PATH_FIXED);
0040 }
0041 
0042 static void mesh_path_rht_free(void *ptr, void *tblptr)
0043 {
0044     struct mesh_path *mpath = ptr;
0045     struct mesh_table *tbl = tblptr;
0046 
0047     mesh_path_free_rcu(tbl, mpath);
0048 }
0049 
0050 static void mesh_table_init(struct mesh_table *tbl)
0051 {
0052     INIT_HLIST_HEAD(&tbl->known_gates);
0053     INIT_HLIST_HEAD(&tbl->walk_head);
0054     atomic_set(&tbl->entries,  0);
0055     spin_lock_init(&tbl->gates_lock);
0056     spin_lock_init(&tbl->walk_lock);
0057 
0058     /* rhashtable_init() may fail only in case of wrong
0059      * mesh_rht_params
0060      */
0061     WARN_ON(rhashtable_init(&tbl->rhead, &mesh_rht_params));
0062 }
0063 
0064 static void mesh_table_free(struct mesh_table *tbl)
0065 {
0066     rhashtable_free_and_destroy(&tbl->rhead,
0067                     mesh_path_rht_free, tbl);
0068 }
0069 
0070 /**
0071  * mesh_path_assign_nexthop - update mesh path next hop
0072  *
0073  * @mpath: mesh path to update
0074  * @sta: next hop to assign
0075  *
0076  * Locking: mpath->state_lock must be held when calling this function
0077  */
0078 void mesh_path_assign_nexthop(struct mesh_path *mpath, struct sta_info *sta)
0079 {
0080     struct sk_buff *skb;
0081     struct ieee80211_hdr *hdr;
0082     unsigned long flags;
0083 
0084     rcu_assign_pointer(mpath->next_hop, sta);
0085 
0086     spin_lock_irqsave(&mpath->frame_queue.lock, flags);
0087     skb_queue_walk(&mpath->frame_queue, skb) {
0088         hdr = (struct ieee80211_hdr *) skb->data;
0089         memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN);
0090         memcpy(hdr->addr2, mpath->sdata->vif.addr, ETH_ALEN);
0091         ieee80211_mps_set_frame_flags(sta->sdata, sta, hdr);
0092     }
0093 
0094     spin_unlock_irqrestore(&mpath->frame_queue.lock, flags);
0095 }
0096 
0097 static void prepare_for_gate(struct sk_buff *skb, char *dst_addr,
0098                  struct mesh_path *gate_mpath)
0099 {
0100     struct ieee80211_hdr *hdr;
0101     struct ieee80211s_hdr *mshdr;
0102     int mesh_hdrlen, hdrlen;
0103     char *next_hop;
0104 
0105     hdr = (struct ieee80211_hdr *) skb->data;
0106     hdrlen = ieee80211_hdrlen(hdr->frame_control);
0107     mshdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
0108 
0109     if (!(mshdr->flags & MESH_FLAGS_AE)) {
0110         /* size of the fixed part of the mesh header */
0111         mesh_hdrlen = 6;
0112 
0113         /* make room for the two extended addresses */
0114         skb_push(skb, 2 * ETH_ALEN);
0115         memmove(skb->data, hdr, hdrlen + mesh_hdrlen);
0116 
0117         hdr = (struct ieee80211_hdr *) skb->data;
0118 
0119         /* we preserve the previous mesh header and only add
0120          * the new addresses */
0121         mshdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
0122         mshdr->flags = MESH_FLAGS_AE_A5_A6;
0123         memcpy(mshdr->eaddr1, hdr->addr3, ETH_ALEN);
0124         memcpy(mshdr->eaddr2, hdr->addr4, ETH_ALEN);
0125     }
0126 
0127     /* update next hop */
0128     hdr = (struct ieee80211_hdr *) skb->data;
0129     rcu_read_lock();
0130     next_hop = rcu_dereference(gate_mpath->next_hop)->sta.addr;
0131     memcpy(hdr->addr1, next_hop, ETH_ALEN);
0132     rcu_read_unlock();
0133     memcpy(hdr->addr2, gate_mpath->sdata->vif.addr, ETH_ALEN);
0134     memcpy(hdr->addr3, dst_addr, ETH_ALEN);
0135 }
0136 
0137 /**
0138  * mesh_path_move_to_queue - Move or copy frames from one mpath queue to another
0139  *
0140  * This function is used to transfer or copy frames from an unresolved mpath to
0141  * a gate mpath.  The function also adds the Address Extension field and
0142  * updates the next hop.
0143  *
0144  * If a frame already has an Address Extension field, only the next hop and
0145  * destination addresses are updated.
0146  *
0147  * The gate mpath must be an active mpath with a valid mpath->next_hop.
0148  *
0149  * @gate_mpath: An active mpath the frames will be sent to (i.e. the gate)
0150  * @from_mpath: The failed mpath
0151  * @copy: When true, copy all the frames to the new mpath queue.  When false,
0152  * move them.
0153  */
0154 static void mesh_path_move_to_queue(struct mesh_path *gate_mpath,
0155                     struct mesh_path *from_mpath,
0156                     bool copy)
0157 {
0158     struct sk_buff *skb, *fskb, *tmp;
0159     struct sk_buff_head failq;
0160     unsigned long flags;
0161 
0162     if (WARN_ON(gate_mpath == from_mpath))
0163         return;
0164     if (WARN_ON(!gate_mpath->next_hop))
0165         return;
0166 
0167     __skb_queue_head_init(&failq);
0168 
0169     spin_lock_irqsave(&from_mpath->frame_queue.lock, flags);
0170     skb_queue_splice_init(&from_mpath->frame_queue, &failq);
0171     spin_unlock_irqrestore(&from_mpath->frame_queue.lock, flags);
0172 
0173     skb_queue_walk_safe(&failq, fskb, tmp) {
0174         if (skb_queue_len(&gate_mpath->frame_queue) >=
0175                   MESH_FRAME_QUEUE_LEN) {
0176             mpath_dbg(gate_mpath->sdata, "mpath queue full!\n");
0177             break;
0178         }
0179 
0180         skb = skb_copy(fskb, GFP_ATOMIC);
0181         if (WARN_ON(!skb))
0182             break;
0183 
0184         prepare_for_gate(skb, gate_mpath->dst, gate_mpath);
0185         skb_queue_tail(&gate_mpath->frame_queue, skb);
0186 
0187         if (copy)
0188             continue;
0189 
0190         __skb_unlink(fskb, &failq);
0191         kfree_skb(fskb);
0192     }
0193 
0194     mpath_dbg(gate_mpath->sdata, "Mpath queue for gate %pM has %d frames\n",
0195           gate_mpath->dst, skb_queue_len(&gate_mpath->frame_queue));
0196 
0197     if (!copy)
0198         return;
0199 
0200     spin_lock_irqsave(&from_mpath->frame_queue.lock, flags);
0201     skb_queue_splice(&failq, &from_mpath->frame_queue);
0202     spin_unlock_irqrestore(&from_mpath->frame_queue.lock, flags);
0203 }
0204 
0205 
0206 static struct mesh_path *mpath_lookup(struct mesh_table *tbl, const u8 *dst,
0207                       struct ieee80211_sub_if_data *sdata)
0208 {
0209     struct mesh_path *mpath;
0210 
0211     mpath = rhashtable_lookup(&tbl->rhead, dst, mesh_rht_params);
0212 
0213     if (mpath && mpath_expired(mpath)) {
0214         spin_lock_bh(&mpath->state_lock);
0215         mpath->flags &= ~MESH_PATH_ACTIVE;
0216         spin_unlock_bh(&mpath->state_lock);
0217     }
0218     return mpath;
0219 }
0220 
0221 /**
0222  * mesh_path_lookup - look up a path in the mesh path table
0223  * @sdata: local subif
0224  * @dst: hardware address (ETH_ALEN length) of destination
0225  *
0226  * Returns: pointer to the mesh path structure, or NULL if not found
0227  *
0228  * Locking: must be called within a read rcu section.
0229  */
0230 struct mesh_path *
0231 mesh_path_lookup(struct ieee80211_sub_if_data *sdata, const u8 *dst)
0232 {
0233     return mpath_lookup(&sdata->u.mesh.mesh_paths, dst, sdata);
0234 }
0235 
0236 struct mesh_path *
0237 mpp_path_lookup(struct ieee80211_sub_if_data *sdata, const u8 *dst)
0238 {
0239     return mpath_lookup(&sdata->u.mesh.mpp_paths, dst, sdata);
0240 }
0241 
0242 static struct mesh_path *
0243 __mesh_path_lookup_by_idx(struct mesh_table *tbl, int idx)
0244 {
0245     int i = 0;
0246     struct mesh_path *mpath;
0247 
0248     hlist_for_each_entry_rcu(mpath, &tbl->walk_head, walk_list) {
0249         if (i++ == idx)
0250             break;
0251     }
0252 
0253     if (!mpath)
0254         return NULL;
0255 
0256     if (mpath_expired(mpath)) {
0257         spin_lock_bh(&mpath->state_lock);
0258         mpath->flags &= ~MESH_PATH_ACTIVE;
0259         spin_unlock_bh(&mpath->state_lock);
0260     }
0261     return mpath;
0262 }
0263 
0264 /**
0265  * mesh_path_lookup_by_idx - look up a path in the mesh path table by its index
0266  * @idx: index
0267  * @sdata: local subif, or NULL for all entries
0268  *
0269  * Returns: pointer to the mesh path structure, or NULL if not found.
0270  *
0271  * Locking: must be called within a read rcu section.
0272  */
0273 struct mesh_path *
0274 mesh_path_lookup_by_idx(struct ieee80211_sub_if_data *sdata, int idx)
0275 {
0276     return __mesh_path_lookup_by_idx(&sdata->u.mesh.mesh_paths, idx);
0277 }
0278 
0279 /**
0280  * mpp_path_lookup_by_idx - look up a path in the proxy path table by its index
0281  * @idx: index
0282  * @sdata: local subif, or NULL for all entries
0283  *
0284  * Returns: pointer to the proxy path structure, or NULL if not found.
0285  *
0286  * Locking: must be called within a read rcu section.
0287  */
0288 struct mesh_path *
0289 mpp_path_lookup_by_idx(struct ieee80211_sub_if_data *sdata, int idx)
0290 {
0291     return __mesh_path_lookup_by_idx(&sdata->u.mesh.mpp_paths, idx);
0292 }
0293 
0294 /**
0295  * mesh_path_add_gate - add the given mpath to a mesh gate to our path table
0296  * @mpath: gate path to add to table
0297  */
0298 int mesh_path_add_gate(struct mesh_path *mpath)
0299 {
0300     struct mesh_table *tbl;
0301     int err;
0302 
0303     rcu_read_lock();
0304     tbl = &mpath->sdata->u.mesh.mesh_paths;
0305 
0306     spin_lock_bh(&mpath->state_lock);
0307     if (mpath->is_gate) {
0308         err = -EEXIST;
0309         spin_unlock_bh(&mpath->state_lock);
0310         goto err_rcu;
0311     }
0312     mpath->is_gate = true;
0313     mpath->sdata->u.mesh.num_gates++;
0314 
0315     spin_lock(&tbl->gates_lock);
0316     hlist_add_head_rcu(&mpath->gate_list, &tbl->known_gates);
0317     spin_unlock(&tbl->gates_lock);
0318 
0319     spin_unlock_bh(&mpath->state_lock);
0320 
0321     mpath_dbg(mpath->sdata,
0322           "Mesh path: Recorded new gate: %pM. %d known gates\n",
0323           mpath->dst, mpath->sdata->u.mesh.num_gates);
0324     err = 0;
0325 err_rcu:
0326     rcu_read_unlock();
0327     return err;
0328 }
0329 
0330 /**
0331  * mesh_gate_del - remove a mesh gate from the list of known gates
0332  * @tbl: table which holds our list of known gates
0333  * @mpath: gate mpath
0334  */
0335 static void mesh_gate_del(struct mesh_table *tbl, struct mesh_path *mpath)
0336 {
0337     lockdep_assert_held(&mpath->state_lock);
0338     if (!mpath->is_gate)
0339         return;
0340 
0341     mpath->is_gate = false;
0342     spin_lock_bh(&tbl->gates_lock);
0343     hlist_del_rcu(&mpath->gate_list);
0344     mpath->sdata->u.mesh.num_gates--;
0345     spin_unlock_bh(&tbl->gates_lock);
0346 
0347     mpath_dbg(mpath->sdata,
0348           "Mesh path: Deleted gate: %pM. %d known gates\n",
0349           mpath->dst, mpath->sdata->u.mesh.num_gates);
0350 }
0351 
0352 /**
0353  * mesh_gate_num - number of gates known to this interface
0354  * @sdata: subif data
0355  */
0356 int mesh_gate_num(struct ieee80211_sub_if_data *sdata)
0357 {
0358     return sdata->u.mesh.num_gates;
0359 }
0360 
0361 static
0362 struct mesh_path *mesh_path_new(struct ieee80211_sub_if_data *sdata,
0363                 const u8 *dst, gfp_t gfp_flags)
0364 {
0365     struct mesh_path *new_mpath;
0366 
0367     new_mpath = kzalloc(sizeof(struct mesh_path), gfp_flags);
0368     if (!new_mpath)
0369         return NULL;
0370 
0371     memcpy(new_mpath->dst, dst, ETH_ALEN);
0372     eth_broadcast_addr(new_mpath->rann_snd_addr);
0373     new_mpath->is_root = false;
0374     new_mpath->sdata = sdata;
0375     new_mpath->flags = 0;
0376     skb_queue_head_init(&new_mpath->frame_queue);
0377     new_mpath->exp_time = jiffies;
0378     spin_lock_init(&new_mpath->state_lock);
0379     timer_setup(&new_mpath->timer, mesh_path_timer, 0);
0380 
0381     return new_mpath;
0382 }
0383 
0384 /**
0385  * mesh_path_add - allocate and add a new path to the mesh path table
0386  * @dst: destination address of the path (ETH_ALEN length)
0387  * @sdata: local subif
0388  *
0389  * Returns: 0 on success
0390  *
0391  * State: the initial state of the new path is set to 0
0392  */
0393 struct mesh_path *mesh_path_add(struct ieee80211_sub_if_data *sdata,
0394                 const u8 *dst)
0395 {
0396     struct mesh_table *tbl;
0397     struct mesh_path *mpath, *new_mpath;
0398 
0399     if (ether_addr_equal(dst, sdata->vif.addr))
0400         /* never add ourselves as neighbours */
0401         return ERR_PTR(-ENOTSUPP);
0402 
0403     if (is_multicast_ether_addr(dst))
0404         return ERR_PTR(-ENOTSUPP);
0405 
0406     if (atomic_add_unless(&sdata->u.mesh.mpaths, 1, MESH_MAX_MPATHS) == 0)
0407         return ERR_PTR(-ENOSPC);
0408 
0409     new_mpath = mesh_path_new(sdata, dst, GFP_ATOMIC);
0410     if (!new_mpath)
0411         return ERR_PTR(-ENOMEM);
0412 
0413     tbl = &sdata->u.mesh.mesh_paths;
0414     spin_lock_bh(&tbl->walk_lock);
0415     mpath = rhashtable_lookup_get_insert_fast(&tbl->rhead,
0416                           &new_mpath->rhash,
0417                           mesh_rht_params);
0418     if (!mpath)
0419         hlist_add_head(&new_mpath->walk_list, &tbl->walk_head);
0420     spin_unlock_bh(&tbl->walk_lock);
0421 
0422     if (mpath) {
0423         kfree(new_mpath);
0424 
0425         if (IS_ERR(mpath))
0426             return mpath;
0427 
0428         new_mpath = mpath;
0429     }
0430 
0431     sdata->u.mesh.mesh_paths_generation++;
0432     return new_mpath;
0433 }
0434 
0435 int mpp_path_add(struct ieee80211_sub_if_data *sdata,
0436          const u8 *dst, const u8 *mpp)
0437 {
0438     struct mesh_table *tbl;
0439     struct mesh_path *new_mpath;
0440     int ret;
0441 
0442     if (ether_addr_equal(dst, sdata->vif.addr))
0443         /* never add ourselves as neighbours */
0444         return -ENOTSUPP;
0445 
0446     if (is_multicast_ether_addr(dst))
0447         return -ENOTSUPP;
0448 
0449     new_mpath = mesh_path_new(sdata, dst, GFP_ATOMIC);
0450 
0451     if (!new_mpath)
0452         return -ENOMEM;
0453 
0454     memcpy(new_mpath->mpp, mpp, ETH_ALEN);
0455     tbl = &sdata->u.mesh.mpp_paths;
0456 
0457     spin_lock_bh(&tbl->walk_lock);
0458     ret = rhashtable_lookup_insert_fast(&tbl->rhead,
0459                         &new_mpath->rhash,
0460                         mesh_rht_params);
0461     if (!ret)
0462         hlist_add_head_rcu(&new_mpath->walk_list, &tbl->walk_head);
0463     spin_unlock_bh(&tbl->walk_lock);
0464 
0465     if (ret)
0466         kfree(new_mpath);
0467 
0468     sdata->u.mesh.mpp_paths_generation++;
0469     return ret;
0470 }
0471 
0472 
0473 /**
0474  * mesh_plink_broken - deactivates paths and sends perr when a link breaks
0475  *
0476  * @sta: broken peer link
0477  *
0478  * This function must be called from the rate control algorithm if enough
0479  * delivery errors suggest that a peer link is no longer usable.
0480  */
0481 void mesh_plink_broken(struct sta_info *sta)
0482 {
0483     struct ieee80211_sub_if_data *sdata = sta->sdata;
0484     struct mesh_table *tbl = &sdata->u.mesh.mesh_paths;
0485     static const u8 bcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
0486     struct mesh_path *mpath;
0487 
0488     rcu_read_lock();
0489     hlist_for_each_entry_rcu(mpath, &tbl->walk_head, walk_list) {
0490         if (rcu_access_pointer(mpath->next_hop) == sta &&
0491             mpath->flags & MESH_PATH_ACTIVE &&
0492             !(mpath->flags & MESH_PATH_FIXED)) {
0493             spin_lock_bh(&mpath->state_lock);
0494             mpath->flags &= ~MESH_PATH_ACTIVE;
0495             ++mpath->sn;
0496             spin_unlock_bh(&mpath->state_lock);
0497             mesh_path_error_tx(sdata,
0498                 sdata->u.mesh.mshcfg.element_ttl,
0499                 mpath->dst, mpath->sn,
0500                 WLAN_REASON_MESH_PATH_DEST_UNREACHABLE, bcast);
0501         }
0502     }
0503     rcu_read_unlock();
0504 }
0505 
0506 static void mesh_path_free_rcu(struct mesh_table *tbl,
0507                    struct mesh_path *mpath)
0508 {
0509     struct ieee80211_sub_if_data *sdata = mpath->sdata;
0510 
0511     spin_lock_bh(&mpath->state_lock);
0512     mpath->flags |= MESH_PATH_RESOLVING | MESH_PATH_DELETED;
0513     mesh_gate_del(tbl, mpath);
0514     spin_unlock_bh(&mpath->state_lock);
0515     del_timer_sync(&mpath->timer);
0516     atomic_dec(&sdata->u.mesh.mpaths);
0517     atomic_dec(&tbl->entries);
0518     mesh_path_flush_pending(mpath);
0519     kfree_rcu(mpath, rcu);
0520 }
0521 
0522 static void __mesh_path_del(struct mesh_table *tbl, struct mesh_path *mpath)
0523 {
0524     hlist_del_rcu(&mpath->walk_list);
0525     rhashtable_remove_fast(&tbl->rhead, &mpath->rhash, mesh_rht_params);
0526     mesh_path_free_rcu(tbl, mpath);
0527 }
0528 
0529 /**
0530  * mesh_path_flush_by_nexthop - Deletes mesh paths if their next hop matches
0531  *
0532  * @sta: mesh peer to match
0533  *
0534  * RCU notes: this function is called when a mesh plink transitions from
0535  * PLINK_ESTAB to any other state, since PLINK_ESTAB state is the only one that
0536  * allows path creation. This will happen before the sta can be freed (because
0537  * sta_info_destroy() calls this) so any reader in a rcu read block will be
0538  * protected against the plink disappearing.
0539  */
0540 void mesh_path_flush_by_nexthop(struct sta_info *sta)
0541 {
0542     struct ieee80211_sub_if_data *sdata = sta->sdata;
0543     struct mesh_table *tbl = &sdata->u.mesh.mesh_paths;
0544     struct mesh_path *mpath;
0545     struct hlist_node *n;
0546 
0547     spin_lock_bh(&tbl->walk_lock);
0548     hlist_for_each_entry_safe(mpath, n, &tbl->walk_head, walk_list) {
0549         if (rcu_access_pointer(mpath->next_hop) == sta)
0550             __mesh_path_del(tbl, mpath);
0551     }
0552     spin_unlock_bh(&tbl->walk_lock);
0553 }
0554 
0555 static void mpp_flush_by_proxy(struct ieee80211_sub_if_data *sdata,
0556                    const u8 *proxy)
0557 {
0558     struct mesh_table *tbl = &sdata->u.mesh.mpp_paths;
0559     struct mesh_path *mpath;
0560     struct hlist_node *n;
0561 
0562     spin_lock_bh(&tbl->walk_lock);
0563     hlist_for_each_entry_safe(mpath, n, &tbl->walk_head, walk_list) {
0564         if (ether_addr_equal(mpath->mpp, proxy))
0565             __mesh_path_del(tbl, mpath);
0566     }
0567     spin_unlock_bh(&tbl->walk_lock);
0568 }
0569 
0570 static void table_flush_by_iface(struct mesh_table *tbl)
0571 {
0572     struct mesh_path *mpath;
0573     struct hlist_node *n;
0574 
0575     spin_lock_bh(&tbl->walk_lock);
0576     hlist_for_each_entry_safe(mpath, n, &tbl->walk_head, walk_list) {
0577         __mesh_path_del(tbl, mpath);
0578     }
0579     spin_unlock_bh(&tbl->walk_lock);
0580 }
0581 
0582 /**
0583  * mesh_path_flush_by_iface - Deletes all mesh paths associated with a given iface
0584  *
0585  * This function deletes both mesh paths as well as mesh portal paths.
0586  *
0587  * @sdata: interface data to match
0588  *
0589  */
0590 void mesh_path_flush_by_iface(struct ieee80211_sub_if_data *sdata)
0591 {
0592     table_flush_by_iface(&sdata->u.mesh.mesh_paths);
0593     table_flush_by_iface(&sdata->u.mesh.mpp_paths);
0594 }
0595 
0596 /**
0597  * table_path_del - delete a path from the mesh or mpp table
0598  *
0599  * @tbl: mesh or mpp path table
0600  * @sdata: local subif
0601  * @addr: dst address (ETH_ALEN length)
0602  *
0603  * Returns: 0 if successful
0604  */
0605 static int table_path_del(struct mesh_table *tbl,
0606               struct ieee80211_sub_if_data *sdata,
0607               const u8 *addr)
0608 {
0609     struct mesh_path *mpath;
0610 
0611     spin_lock_bh(&tbl->walk_lock);
0612     mpath = rhashtable_lookup_fast(&tbl->rhead, addr, mesh_rht_params);
0613     if (!mpath) {
0614         spin_unlock_bh(&tbl->walk_lock);
0615         return -ENXIO;
0616     }
0617 
0618     __mesh_path_del(tbl, mpath);
0619     spin_unlock_bh(&tbl->walk_lock);
0620     return 0;
0621 }
0622 
0623 
0624 /**
0625  * mesh_path_del - delete a mesh path from the table
0626  *
0627  * @addr: dst address (ETH_ALEN length)
0628  * @sdata: local subif
0629  *
0630  * Returns: 0 if successful
0631  */
0632 int mesh_path_del(struct ieee80211_sub_if_data *sdata, const u8 *addr)
0633 {
0634     int err;
0635 
0636     /* flush relevant mpp entries first */
0637     mpp_flush_by_proxy(sdata, addr);
0638 
0639     err = table_path_del(&sdata->u.mesh.mesh_paths, sdata, addr);
0640     sdata->u.mesh.mesh_paths_generation++;
0641     return err;
0642 }
0643 
0644 /**
0645  * mesh_path_tx_pending - sends pending frames in a mesh path queue
0646  *
0647  * @mpath: mesh path to activate
0648  *
0649  * Locking: the state_lock of the mpath structure must NOT be held when calling
0650  * this function.
0651  */
0652 void mesh_path_tx_pending(struct mesh_path *mpath)
0653 {
0654     if (mpath->flags & MESH_PATH_ACTIVE)
0655         ieee80211_add_pending_skbs(mpath->sdata->local,
0656                 &mpath->frame_queue);
0657 }
0658 
0659 /**
0660  * mesh_path_send_to_gates - sends pending frames to all known mesh gates
0661  *
0662  * @mpath: mesh path whose queue will be emptied
0663  *
0664  * If there is only one gate, the frames are transferred from the failed mpath
0665  * queue to that gate's queue.  If there are more than one gates, the frames
0666  * are copied from each gate to the next.  After frames are copied, the
0667  * mpath queues are emptied onto the transmission queue.
0668  */
0669 int mesh_path_send_to_gates(struct mesh_path *mpath)
0670 {
0671     struct ieee80211_sub_if_data *sdata = mpath->sdata;
0672     struct mesh_table *tbl;
0673     struct mesh_path *from_mpath = mpath;
0674     struct mesh_path *gate;
0675     bool copy = false;
0676 
0677     tbl = &sdata->u.mesh.mesh_paths;
0678 
0679     rcu_read_lock();
0680     hlist_for_each_entry_rcu(gate, &tbl->known_gates, gate_list) {
0681         if (gate->flags & MESH_PATH_ACTIVE) {
0682             mpath_dbg(sdata, "Forwarding to %pM\n", gate->dst);
0683             mesh_path_move_to_queue(gate, from_mpath, copy);
0684             from_mpath = gate;
0685             copy = true;
0686         } else {
0687             mpath_dbg(sdata,
0688                   "Not forwarding to %pM (flags %#x)\n",
0689                   gate->dst, gate->flags);
0690         }
0691     }
0692 
0693     hlist_for_each_entry_rcu(gate, &tbl->known_gates, gate_list) {
0694         mpath_dbg(sdata, "Sending to %pM\n", gate->dst);
0695         mesh_path_tx_pending(gate);
0696     }
0697     rcu_read_unlock();
0698 
0699     return (from_mpath == mpath) ? -EHOSTUNREACH : 0;
0700 }
0701 
0702 /**
0703  * mesh_path_discard_frame - discard a frame whose path could not be resolved
0704  *
0705  * @skb: frame to discard
0706  * @sdata: network subif the frame was to be sent through
0707  *
0708  * Locking: the function must me called within a rcu_read_lock region
0709  */
0710 void mesh_path_discard_frame(struct ieee80211_sub_if_data *sdata,
0711                  struct sk_buff *skb)
0712 {
0713     kfree_skb(skb);
0714     sdata->u.mesh.mshstats.dropped_frames_no_route++;
0715 }
0716 
0717 /**
0718  * mesh_path_flush_pending - free the pending queue of a mesh path
0719  *
0720  * @mpath: mesh path whose queue has to be freed
0721  *
0722  * Locking: the function must me called within a rcu_read_lock region
0723  */
0724 void mesh_path_flush_pending(struct mesh_path *mpath)
0725 {
0726     struct sk_buff *skb;
0727 
0728     while ((skb = skb_dequeue(&mpath->frame_queue)) != NULL)
0729         mesh_path_discard_frame(mpath->sdata, skb);
0730 }
0731 
0732 /**
0733  * mesh_path_fix_nexthop - force a specific next hop for a mesh path
0734  *
0735  * @mpath: the mesh path to modify
0736  * @next_hop: the next hop to force
0737  *
0738  * Locking: this function must be called holding mpath->state_lock
0739  */
0740 void mesh_path_fix_nexthop(struct mesh_path *mpath, struct sta_info *next_hop)
0741 {
0742     spin_lock_bh(&mpath->state_lock);
0743     mesh_path_assign_nexthop(mpath, next_hop);
0744     mpath->sn = 0xffff;
0745     mpath->metric = 0;
0746     mpath->hop_count = 0;
0747     mpath->exp_time = 0;
0748     mpath->flags = MESH_PATH_FIXED | MESH_PATH_SN_VALID;
0749     mesh_path_activate(mpath);
0750     spin_unlock_bh(&mpath->state_lock);
0751     ewma_mesh_fail_avg_init(&next_hop->mesh->fail_avg);
0752     /* init it at a low value - 0 start is tricky */
0753     ewma_mesh_fail_avg_add(&next_hop->mesh->fail_avg, 1);
0754     mesh_path_tx_pending(mpath);
0755 }
0756 
0757 void mesh_pathtbl_init(struct ieee80211_sub_if_data *sdata)
0758 {
0759     mesh_table_init(&sdata->u.mesh.mesh_paths);
0760     mesh_table_init(&sdata->u.mesh.mpp_paths);
0761 }
0762 
0763 static
0764 void mesh_path_tbl_expire(struct ieee80211_sub_if_data *sdata,
0765               struct mesh_table *tbl)
0766 {
0767     struct mesh_path *mpath;
0768     struct hlist_node *n;
0769 
0770     spin_lock_bh(&tbl->walk_lock);
0771     hlist_for_each_entry_safe(mpath, n, &tbl->walk_head, walk_list) {
0772         if ((!(mpath->flags & MESH_PATH_RESOLVING)) &&
0773             (!(mpath->flags & MESH_PATH_FIXED)) &&
0774              time_after(jiffies, mpath->exp_time + MESH_PATH_EXPIRE))
0775             __mesh_path_del(tbl, mpath);
0776     }
0777     spin_unlock_bh(&tbl->walk_lock);
0778 }
0779 
0780 void mesh_path_expire(struct ieee80211_sub_if_data *sdata)
0781 {
0782     mesh_path_tbl_expire(sdata, &sdata->u.mesh.mesh_paths);
0783     mesh_path_tbl_expire(sdata, &sdata->u.mesh.mpp_paths);
0784 }
0785 
0786 void mesh_pathtbl_unregister(struct ieee80211_sub_if_data *sdata)
0787 {
0788     mesh_table_free(&sdata->u.mesh.mesh_paths);
0789     mesh_table_free(&sdata->u.mesh.mpp_paths);
0790 }