The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/net80211/ieee80211_hwmp.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*- 
    2  * Copyright (c) 2009 The FreeBSD Foundation 
    3  * All rights reserved. 
    4  * 
    5  * This software was developed by Rui Paulo under sponsorship from the
    6  * FreeBSD Foundation. 
    7  *  
    8  * Redistribution and use in source and binary forms, with or without 
    9  * modification, are permitted provided that the following conditions 
   10  * are met: 
   11  * 1. Redistributions of source code must retain the above copyright 
   12  *    notice, this list of conditions and the following disclaimer. 
   13  * 2. Redistributions in binary form must reproduce the above copyright 
   14  *    notice, this list of conditions and the following disclaimer in the 
   15  *    documentation and/or other materials provided with the distribution. 
   16  * 
   17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 
   18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
   19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
   20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 
   21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
   22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
   23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
   24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
   25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
   26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
   27  * SUCH DAMAGE. 
   28  */ 
   29 #include <sys/cdefs.h>
   30 #ifdef __FreeBSD__
   31 __FBSDID("$FreeBSD: releng/11.0/sys/net80211/ieee80211_hwmp.c 300232 2016-05-19 21:08:33Z avos $");
   32 #endif
   33 
   34 /*
   35  * IEEE 802.11s Hybrid Wireless Mesh Protocol, HWMP.
   36  *
   37  * Based on March 2009, D3.0 802.11s draft spec.
   38  */
   39 #include "opt_inet.h"
   40 #include "opt_wlan.h"
   41 
   42 #include <sys/param.h>
   43 #include <sys/systm.h>
   44 #include <sys/mbuf.h>
   45 #include <sys/malloc.h>
   46 #include <sys/kernel.h>
   47 
   48 #include <sys/socket.h>
   49 #include <sys/sockio.h>
   50 #include <sys/endian.h>
   51 #include <sys/errno.h>
   52 #include <sys/proc.h>
   53 #include <sys/sysctl.h>
   54 
   55 #include <net/if.h>
   56 #include <net/if_media.h>
   57 #include <net/if_llc.h>
   58 #include <net/ethernet.h>
   59 
   60 #include <net/bpf.h>
   61 
   62 #include <net80211/ieee80211_var.h>
   63 #include <net80211/ieee80211_action.h>
   64 #include <net80211/ieee80211_input.h>
   65 #include <net80211/ieee80211_mesh.h>
   66 
   67 static void     hwmp_vattach(struct ieee80211vap *);
   68 static void     hwmp_vdetach(struct ieee80211vap *);
   69 static int      hwmp_newstate(struct ieee80211vap *,
   70                     enum ieee80211_state, int);
   71 static int      hwmp_send_action(struct ieee80211vap *,
   72                     const uint8_t [IEEE80211_ADDR_LEN],
   73                     uint8_t *, size_t);
   74 static uint8_t * hwmp_add_meshpreq(uint8_t *,
   75                     const struct ieee80211_meshpreq_ie *);
   76 static uint8_t * hwmp_add_meshprep(uint8_t *,
   77                     const struct ieee80211_meshprep_ie *);
   78 static uint8_t * hwmp_add_meshperr(uint8_t *,
   79                     const struct ieee80211_meshperr_ie *);
   80 static uint8_t * hwmp_add_meshrann(uint8_t *,
   81                     const struct ieee80211_meshrann_ie *);
   82 static void     hwmp_rootmode_setup(struct ieee80211vap *);
   83 static void     hwmp_rootmode_cb(void *);
   84 static void     hwmp_rootmode_rann_cb(void *);
   85 static void     hwmp_recv_preq(struct ieee80211vap *, struct ieee80211_node *,
   86                     const struct ieee80211_frame *,
   87                     const struct ieee80211_meshpreq_ie *);
   88 static int      hwmp_send_preq(struct ieee80211vap *,
   89                     const uint8_t [IEEE80211_ADDR_LEN],
   90                     struct ieee80211_meshpreq_ie *,
   91                     struct timeval *, struct timeval *);
   92 static void     hwmp_recv_prep(struct ieee80211vap *, struct ieee80211_node *,
   93                     const struct ieee80211_frame *,
   94                     const struct ieee80211_meshprep_ie *);
   95 static int      hwmp_send_prep(struct ieee80211vap *,
   96                     const uint8_t [IEEE80211_ADDR_LEN],
   97                     struct ieee80211_meshprep_ie *);
   98 static void     hwmp_recv_perr(struct ieee80211vap *, struct ieee80211_node *,
   99                     const struct ieee80211_frame *,
  100                     const struct ieee80211_meshperr_ie *);
  101 static int      hwmp_send_perr(struct ieee80211vap *,
  102                     const uint8_t [IEEE80211_ADDR_LEN],
  103                     struct ieee80211_meshperr_ie *);
  104 static void     hwmp_senderror(struct ieee80211vap *,
  105                     const uint8_t [IEEE80211_ADDR_LEN],
  106                     struct ieee80211_mesh_route *, int);
  107 static void     hwmp_recv_rann(struct ieee80211vap *, struct ieee80211_node *,
  108                    const struct ieee80211_frame *,
  109                    const struct ieee80211_meshrann_ie *);
  110 static int      hwmp_send_rann(struct ieee80211vap *,
  111                     const uint8_t [IEEE80211_ADDR_LEN],
  112                     struct ieee80211_meshrann_ie *);
  113 static struct ieee80211_node *
  114                 hwmp_discover(struct ieee80211vap *,
  115                     const uint8_t [IEEE80211_ADDR_LEN], struct mbuf *);
  116 static void     hwmp_peerdown(struct ieee80211_node *);
  117 
  118 static struct timeval ieee80211_hwmp_preqminint = { 0, 100000 };
  119 static struct timeval ieee80211_hwmp_perrminint = { 0, 100000 };
  120 
  121 
  122 /* NB: the Target Address set in a Proactive PREQ is the broadcast address. */
  123 static const uint8_t    broadcastaddr[IEEE80211_ADDR_LEN] =
  124         { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  125 
  126 typedef uint32_t ieee80211_hwmp_seq;
  127 #define HWMP_SEQ_LT(a, b)       ((int32_t)((a)-(b)) < 0)
  128 #define HWMP_SEQ_LEQ(a, b)      ((int32_t)((a)-(b)) <= 0)
  129 #define HWMP_SEQ_EQ(a, b)       ((int32_t)((a)-(b)) == 0)
  130 #define HWMP_SEQ_GT(a, b)       ((int32_t)((a)-(b)) > 0)
  131 
  132 #define HWMP_SEQ_MAX(a, b)      (a > b ? a : b)
  133 
  134 /*
  135  * Private extension of ieee80211_mesh_route.
  136  */
  137 struct ieee80211_hwmp_route {
  138         ieee80211_hwmp_seq      hr_seq;         /* last HWMP seq seen from dst*/
  139         ieee80211_hwmp_seq      hr_preqid;      /* last PREQ ID seen from dst */
  140         ieee80211_hwmp_seq      hr_origseq;     /* seq. no. on our latest PREQ*/
  141         struct timeval          hr_lastpreq;    /* last time we sent a PREQ */
  142         struct timeval          hr_lastrootconf; /* last sent PREQ root conf */
  143         int                     hr_preqretries; /* number of discoveries */
  144         int                     hr_lastdiscovery; /* last discovery in ticks */
  145 };
  146 struct ieee80211_hwmp_state {
  147         ieee80211_hwmp_seq      hs_seq;         /* next seq to be used */
  148         ieee80211_hwmp_seq      hs_preqid;      /* next PREQ ID to be used */
  149         int                     hs_rootmode;    /* proactive HWMP */
  150         struct timeval          hs_lastperr;    /* last time we sent a PERR */
  151         struct callout          hs_roottimer;
  152         uint8_t                 hs_maxhops;     /* max hop count */
  153 };
  154 
  155 static SYSCTL_NODE(_net_wlan, OID_AUTO, hwmp, CTLFLAG_RD, 0,
  156     "IEEE 802.11s HWMP parameters");
  157 static int      ieee80211_hwmp_targetonly = 0;
  158 SYSCTL_INT(_net_wlan_hwmp, OID_AUTO, targetonly, CTLFLAG_RW,
  159     &ieee80211_hwmp_targetonly, 0, "Set TO bit on generated PREQs");
  160 static int      ieee80211_hwmp_pathtimeout = -1;
  161 SYSCTL_PROC(_net_wlan_hwmp, OID_AUTO, pathlifetime, CTLTYPE_INT | CTLFLAG_RW,
  162     &ieee80211_hwmp_pathtimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
  163     "path entry lifetime (ms)");
  164 static int      ieee80211_hwmp_maxpreq_retries = -1;
  165 SYSCTL_PROC(_net_wlan_hwmp, OID_AUTO, maxpreq_retries, CTLTYPE_INT | CTLFLAG_RW,
  166     &ieee80211_hwmp_maxpreq_retries, 0, ieee80211_sysctl_msecs_ticks, "I",
  167     "maximum number of preq retries");
  168 static int      ieee80211_hwmp_net_diameter_traversaltime = -1;
  169 SYSCTL_PROC(_net_wlan_hwmp, OID_AUTO, net_diameter_traversal_time,
  170     CTLTYPE_INT | CTLFLAG_RW, &ieee80211_hwmp_net_diameter_traversaltime, 0,
  171     ieee80211_sysctl_msecs_ticks, "I",
  172     "estimate travelse time across the MBSS (ms)");
  173 static int      ieee80211_hwmp_roottimeout = -1;
  174 SYSCTL_PROC(_net_wlan_hwmp, OID_AUTO, roottimeout, CTLTYPE_INT | CTLFLAG_RW,
  175     &ieee80211_hwmp_roottimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
  176     "root PREQ timeout (ms)");
  177 static int      ieee80211_hwmp_rootint = -1;
  178 SYSCTL_PROC(_net_wlan_hwmp, OID_AUTO, rootint, CTLTYPE_INT | CTLFLAG_RW,
  179     &ieee80211_hwmp_rootint, 0, ieee80211_sysctl_msecs_ticks, "I",
  180     "root interval (ms)");
  181 static int      ieee80211_hwmp_rannint = -1;
  182 SYSCTL_PROC(_net_wlan_hwmp, OID_AUTO, rannint, CTLTYPE_INT | CTLFLAG_RW,
  183     &ieee80211_hwmp_rannint, 0, ieee80211_sysctl_msecs_ticks, "I",
  184     "root announcement interval (ms)");
  185 static struct timeval ieee80211_hwmp_rootconfint = { 0, 0 };
  186 static int      ieee80211_hwmp_rootconfint_internal = -1;
  187 SYSCTL_PROC(_net_wlan_hwmp, OID_AUTO, rootconfint, CTLTYPE_INT | CTLFLAG_RD,
  188     &ieee80211_hwmp_rootconfint_internal, 0, ieee80211_sysctl_msecs_ticks, "I",
  189     "root confirmation interval (ms) (read-only)");
  190 
  191 #define IEEE80211_HWMP_DEFAULT_MAXHOPS  31
  192 
  193 static  ieee80211_recv_action_func hwmp_recv_action_meshpath;
  194 
  195 static struct ieee80211_mesh_proto_path mesh_proto_hwmp = {
  196         .mpp_descr      = "HWMP",
  197         .mpp_ie         = IEEE80211_MESHCONF_PATH_HWMP,
  198         .mpp_discover   = hwmp_discover,
  199         .mpp_peerdown   = hwmp_peerdown,
  200         .mpp_senderror  = hwmp_senderror,
  201         .mpp_vattach    = hwmp_vattach,
  202         .mpp_vdetach    = hwmp_vdetach,
  203         .mpp_newstate   = hwmp_newstate,
  204         .mpp_privlen    = sizeof(struct ieee80211_hwmp_route),
  205 };
  206 SYSCTL_PROC(_net_wlan_hwmp, OID_AUTO, inact, CTLTYPE_INT | CTLFLAG_RW,
  207         &mesh_proto_hwmp.mpp_inact, 0, ieee80211_sysctl_msecs_ticks, "I",
  208         "mesh route inactivity timeout (ms)");
  209 
  210 
  211 static void
  212 ieee80211_hwmp_init(void)
  213 {
  214         /* Default values as per amendment */
  215         ieee80211_hwmp_pathtimeout = msecs_to_ticks(5*1000);
  216         ieee80211_hwmp_roottimeout = msecs_to_ticks(5*1000);
  217         ieee80211_hwmp_rootint = msecs_to_ticks(2*1000);
  218         ieee80211_hwmp_rannint = msecs_to_ticks(1*1000);
  219         ieee80211_hwmp_rootconfint_internal = msecs_to_ticks(2*1000);
  220         ieee80211_hwmp_maxpreq_retries = 3;
  221         /*
  222          * (TU): A measurement of time equal to 1024 μs,
  223          * 500 TU is 512 ms.
  224          */
  225         ieee80211_hwmp_net_diameter_traversaltime = msecs_to_ticks(512);
  226 
  227         /*
  228          * NB: I dont know how to make SYSCTL_PROC that calls ms to ticks
  229          * and return a struct timeval...
  230          */
  231         ieee80211_hwmp_rootconfint.tv_usec =
  232             ieee80211_hwmp_rootconfint_internal * 1000;
  233 
  234         /*
  235          * Register action frame handler.
  236          */
  237         ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESH,
  238             IEEE80211_ACTION_MESH_HWMP, hwmp_recv_action_meshpath);
  239 
  240         /* NB: default is 5 secs per spec */
  241         mesh_proto_hwmp.mpp_inact = msecs_to_ticks(5*1000);
  242 
  243         /*
  244          * Register HWMP.
  245          */
  246         ieee80211_mesh_register_proto_path(&mesh_proto_hwmp);
  247 }
  248 SYSINIT(wlan_hwmp, SI_SUB_DRIVERS, SI_ORDER_SECOND, ieee80211_hwmp_init, NULL);
  249 
  250 static void
  251 hwmp_vattach(struct ieee80211vap *vap)
  252 {
  253         struct ieee80211_hwmp_state *hs;
  254 
  255         KASSERT(vap->iv_opmode == IEEE80211_M_MBSS,
  256             ("not a mesh vap, opmode %d", vap->iv_opmode));
  257 
  258         hs = IEEE80211_MALLOC(sizeof(struct ieee80211_hwmp_state), M_80211_VAP,
  259             IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
  260         if (hs == NULL) {
  261                 printf("%s: couldn't alloc HWMP state\n", __func__);
  262                 return;
  263         }
  264         hs->hs_maxhops = IEEE80211_HWMP_DEFAULT_MAXHOPS;
  265         callout_init(&hs->hs_roottimer, 1);
  266         vap->iv_hwmp = hs;
  267 }
  268 
  269 static void
  270 hwmp_vdetach(struct ieee80211vap *vap)
  271 {
  272         struct ieee80211_hwmp_state *hs = vap->iv_hwmp;
  273 
  274         callout_drain(&hs->hs_roottimer);
  275         IEEE80211_FREE(vap->iv_hwmp, M_80211_VAP);
  276         vap->iv_hwmp = NULL;
  277 } 
  278 
  279 static int
  280 hwmp_newstate(struct ieee80211vap *vap, enum ieee80211_state ostate, int arg)
  281 {
  282         enum ieee80211_state nstate = vap->iv_state;
  283         struct ieee80211_hwmp_state *hs = vap->iv_hwmp;
  284 
  285         IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
  286             __func__, ieee80211_state_name[ostate],
  287             ieee80211_state_name[nstate], arg);
  288 
  289         if (nstate != IEEE80211_S_RUN && ostate == IEEE80211_S_RUN)
  290                 callout_drain(&hs->hs_roottimer);
  291         if (nstate == IEEE80211_S_RUN)
  292                 hwmp_rootmode_setup(vap);
  293         return 0;
  294 }
  295 
  296 /*
  297  * Verify the length of an HWMP PREQ and return the number
  298  * of destinations >= 1, if verification fails -1 is returned.
  299  */
  300 static int
  301 verify_mesh_preq_len(struct ieee80211vap *vap,
  302     const struct ieee80211_frame *wh, const uint8_t *iefrm)
  303 {
  304         int alloc_sz = -1;
  305         int ndest = -1;
  306         if (iefrm[2] & IEEE80211_MESHPREQ_FLAGS_AE) {
  307                 /* Originator External Address  present */
  308                 alloc_sz =  IEEE80211_MESHPREQ_BASE_SZ_AE;
  309                 ndest = iefrm[IEEE80211_MESHPREQ_TCNT_OFFSET_AE];
  310         } else {
  311                 /* w/o Originator External Address */
  312                 alloc_sz =  IEEE80211_MESHPREQ_BASE_SZ;
  313                 ndest = iefrm[IEEE80211_MESHPREQ_TCNT_OFFSET];
  314         }
  315         alloc_sz += ndest * IEEE80211_MESHPREQ_TRGT_SZ;
  316 
  317         if(iefrm[1] != (alloc_sz)) {
  318                 IEEE80211_DISCARD(vap,
  319                     IEEE80211_MSG_ACTION | IEEE80211_MSG_HWMP,
  320                     wh, NULL, "PREQ (AE=%s) with wrong len",
  321                     iefrm[2] & IEEE80211_MESHPREQ_FLAGS_AE ? "1" : "");
  322                 return (-1);
  323         }
  324         return ndest;
  325 }
  326 
  327 /*
  328  * Verify the length of an HWMP PREP and returns 1 on success,
  329  * otherwise -1.
  330  */
  331 static int
  332 verify_mesh_prep_len(struct ieee80211vap *vap,
  333     const struct ieee80211_frame *wh, const uint8_t *iefrm)
  334 {
  335         int alloc_sz = -1;
  336         if (iefrm[2] & IEEE80211_MESHPREP_FLAGS_AE) {
  337                 if (iefrm[1] == IEEE80211_MESHPREP_BASE_SZ_AE)
  338                         alloc_sz = IEEE80211_MESHPREP_BASE_SZ_AE;
  339         } else if (iefrm[1] == IEEE80211_MESHPREP_BASE_SZ)
  340                 alloc_sz = IEEE80211_MESHPREP_BASE_SZ;
  341         if(alloc_sz < 0) {
  342                 IEEE80211_DISCARD(vap,
  343                     IEEE80211_MSG_ACTION | IEEE80211_MSG_HWMP,
  344                     wh, NULL, "PREP (AE=%s) with wrong len",
  345                     iefrm[2] & IEEE80211_MESHPREP_FLAGS_AE ? "1" : "");
  346                 return (-1);
  347         }
  348         return (1);
  349 }
  350 
  351 /*
  352  * Verify the length of an HWMP PERR and return the number
  353  * of destinations >= 1, if verification fails -1 is returned.
  354  */
  355 static int
  356 verify_mesh_perr_len(struct ieee80211vap *vap,
  357     const struct ieee80211_frame *wh, const uint8_t *iefrm)
  358 {
  359         int alloc_sz = -1;
  360         const uint8_t *iefrm_t = iefrm;
  361         uint8_t ndest = iefrm_t[IEEE80211_MESHPERR_NDEST_OFFSET];
  362         int i;
  363 
  364         if(ndest > IEEE80211_MESHPERR_MAXDEST) {
  365                 IEEE80211_DISCARD(vap,
  366                     IEEE80211_MSG_ACTION | IEEE80211_MSG_HWMP,
  367                     wh, NULL, "PERR with wrong number of destionat (>19), %u",
  368                     ndest);
  369                 return (-1);
  370         }
  371 
  372         iefrm_t += IEEE80211_MESHPERR_NDEST_OFFSET + 1; /* flag is next field */
  373         /* We need to check each destionation flag to know size */
  374         for(i = 0; i<ndest; i++) {
  375                 if ((*iefrm_t) & IEEE80211_MESHPERR_FLAGS_AE)
  376                         iefrm_t += IEEE80211_MESHPERR_DEST_SZ_AE;
  377                 else
  378                         iefrm_t += IEEE80211_MESHPERR_DEST_SZ;
  379         }
  380 
  381         alloc_sz = (iefrm_t - iefrm) - 2; /* action + code */
  382         if(alloc_sz !=  iefrm[1]) {
  383                 IEEE80211_DISCARD(vap,
  384                     IEEE80211_MSG_ACTION | IEEE80211_MSG_HWMP,
  385                     wh, NULL, "%s", "PERR with wrong len");
  386                 return (-1);
  387         }
  388         return ndest;
  389 }
  390 
  391 static int
  392 hwmp_recv_action_meshpath(struct ieee80211_node *ni,
  393         const struct ieee80211_frame *wh,
  394         const uint8_t *frm, const uint8_t *efrm)
  395 {
  396         struct ieee80211vap *vap = ni->ni_vap;
  397         struct ieee80211_meshpreq_ie *preq;
  398         struct ieee80211_meshprep_ie *prep;
  399         struct ieee80211_meshperr_ie *perr;
  400         struct ieee80211_meshrann_ie rann;
  401         const uint8_t *iefrm = frm + 2; /* action + code */
  402         const uint8_t *iefrm_t = iefrm; /* temporary pointer */
  403         int ndest = -1;
  404         int found = 0;
  405 
  406         while (efrm - iefrm > 1) {
  407                 IEEE80211_VERIFY_LENGTH(efrm - iefrm, iefrm[1] + 2, return 0);
  408                 switch (*iefrm) {
  409                 case IEEE80211_ELEMID_MESHPREQ:
  410                 {
  411                         int i = 0;
  412 
  413                         iefrm_t = iefrm;
  414                         ndest = verify_mesh_preq_len(vap, wh, iefrm_t);
  415                         if (ndest < 0) {
  416                                 vap->iv_stats.is_rx_mgtdiscard++;
  417                                 break;
  418                         }
  419                         preq = IEEE80211_MALLOC(sizeof(*preq) +
  420                             (ndest - 1) * sizeof(*preq->preq_targets),
  421                             M_80211_MESH_PREQ,
  422                             IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
  423                         KASSERT(preq != NULL, ("preq == NULL"));
  424 
  425                         preq->preq_ie = *iefrm_t++;
  426                         preq->preq_len = *iefrm_t++;
  427                         preq->preq_flags = *iefrm_t++;
  428                         preq->preq_hopcount = *iefrm_t++;
  429                         preq->preq_ttl = *iefrm_t++;
  430                         preq->preq_id = le32dec(iefrm_t); iefrm_t += 4;
  431                         IEEE80211_ADDR_COPY(preq->preq_origaddr, iefrm_t);
  432                         iefrm_t += 6;
  433                         preq->preq_origseq = le32dec(iefrm_t); iefrm_t += 4;
  434                         /* NB: may have Originator Proxied Address */
  435                         if (preq->preq_flags & IEEE80211_MESHPREQ_FLAGS_AE)  {
  436                                 IEEE80211_ADDR_COPY(
  437                                     preq->preq_orig_ext_addr, iefrm_t);
  438                                 iefrm_t += 6;
  439                         }
  440                         preq->preq_lifetime = le32dec(iefrm_t); iefrm_t += 4;
  441                         preq->preq_metric = le32dec(iefrm_t); iefrm_t += 4;
  442                         preq->preq_tcount = *iefrm_t++;
  443                         
  444                         for (i = 0; i < preq->preq_tcount; i++) {
  445                                 preq->preq_targets[i].target_flags = *iefrm_t++;
  446                                 IEEE80211_ADDR_COPY(
  447                                     preq->preq_targets[i].target_addr, iefrm_t);
  448                                 iefrm_t += 6;
  449                                 preq->preq_targets[i].target_seq =
  450                                     le32dec(iefrm_t);
  451                                 iefrm_t += 4;
  452                         }
  453 
  454                         hwmp_recv_preq(vap, ni, wh, preq);
  455                         IEEE80211_FREE(preq, M_80211_MESH_PREQ);
  456                         found++;
  457                         break;
  458                 }
  459                 case IEEE80211_ELEMID_MESHPREP:
  460                 {
  461                         iefrm_t = iefrm;
  462                         ndest = verify_mesh_prep_len(vap, wh, iefrm_t);
  463                         if (ndest < 0) {
  464                                 vap->iv_stats.is_rx_mgtdiscard++;
  465                                 break;
  466                         }
  467                         prep = IEEE80211_MALLOC(sizeof(*prep),
  468                             M_80211_MESH_PREP,
  469                             IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
  470                         KASSERT(prep != NULL, ("prep == NULL"));
  471 
  472                         prep->prep_ie = *iefrm_t++;
  473                         prep->prep_len = *iefrm_t++;
  474                         prep->prep_flags = *iefrm_t++;
  475                         prep->prep_hopcount = *iefrm_t++;
  476                         prep->prep_ttl = *iefrm_t++;
  477                         IEEE80211_ADDR_COPY(prep->prep_targetaddr, iefrm_t);
  478                         iefrm_t += 6;
  479                         prep->prep_targetseq = le32dec(iefrm_t); iefrm_t += 4;
  480                         /* NB: May have Target Proxied Address */
  481                         if (prep->prep_flags & IEEE80211_MESHPREP_FLAGS_AE)  {
  482                                 IEEE80211_ADDR_COPY(
  483                                     prep->prep_target_ext_addr, iefrm_t);
  484                                 iefrm_t += 6;
  485                         }
  486                         prep->prep_lifetime = le32dec(iefrm_t); iefrm_t += 4;
  487                         prep->prep_metric = le32dec(iefrm_t); iefrm_t += 4;
  488                         IEEE80211_ADDR_COPY(prep->prep_origaddr, iefrm_t);
  489                         iefrm_t += 6;
  490                         prep->prep_origseq = le32dec(iefrm_t); iefrm_t += 4;
  491 
  492                         hwmp_recv_prep(vap, ni, wh, prep);
  493                         IEEE80211_FREE(prep, M_80211_MESH_PREP);
  494                         found++;
  495                         break;
  496                 }
  497                 case IEEE80211_ELEMID_MESHPERR:
  498                 {
  499                         int i = 0;
  500 
  501                         iefrm_t = iefrm;
  502                         ndest = verify_mesh_perr_len(vap, wh, iefrm_t);
  503                         if (ndest < 0) {
  504                                 vap->iv_stats.is_rx_mgtdiscard++;
  505                                 break;
  506                         }
  507                         perr = IEEE80211_MALLOC(sizeof(*perr) +
  508                             (ndest - 1) * sizeof(*perr->perr_dests),
  509                             M_80211_MESH_PERR,
  510                             IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
  511                         KASSERT(perr != NULL, ("perr == NULL"));
  512 
  513                         perr->perr_ie = *iefrm_t++;
  514                         perr->perr_len = *iefrm_t++;
  515                         perr->perr_ttl = *iefrm_t++;
  516                         perr->perr_ndests = *iefrm_t++;
  517 
  518                         for (i = 0; i<perr->perr_ndests; i++) {
  519                                 perr->perr_dests[i].dest_flags = *iefrm_t++;
  520                                 IEEE80211_ADDR_COPY(
  521                                     perr->perr_dests[i].dest_addr, iefrm_t);
  522                                 iefrm_t += 6;
  523                                 perr->perr_dests[i].dest_seq = le32dec(iefrm_t);
  524                                 iefrm_t += 4;
  525                                 /* NB: May have Target Proxied Address */
  526                                 if (perr->perr_dests[i].dest_flags &
  527                                     IEEE80211_MESHPERR_FLAGS_AE) {
  528                                         IEEE80211_ADDR_COPY(
  529                                             perr->perr_dests[i].dest_ext_addr,
  530                                             iefrm_t);
  531                                         iefrm_t += 6;
  532                                 }
  533                                 perr->perr_dests[i].dest_rcode =
  534                                     le16dec(iefrm_t);
  535                                 iefrm_t += 2;
  536                         }
  537 
  538                         hwmp_recv_perr(vap, ni, wh, perr);
  539                         IEEE80211_FREE(perr, M_80211_MESH_PERR);
  540                         found++;
  541                         break;
  542                 }
  543                 case IEEE80211_ELEMID_MESHRANN:
  544                 {
  545                         const struct ieee80211_meshrann_ie *mrann =
  546                             (const struct ieee80211_meshrann_ie *) iefrm;
  547                         if (mrann->rann_len !=
  548                             sizeof(struct ieee80211_meshrann_ie) - 2) {
  549                                 IEEE80211_DISCARD(vap,
  550                                     IEEE80211_MSG_ACTION | IEEE80211_MSG_HWMP,
  551                                     wh, NULL, "%s", "RAN with wrong len");
  552                                     vap->iv_stats.is_rx_mgtdiscard++;
  553                                 return 1;
  554                         }
  555                         memcpy(&rann, mrann, sizeof(rann));
  556                         rann.rann_seq = le32dec(&mrann->rann_seq);
  557                         rann.rann_interval = le32dec(&mrann->rann_interval);
  558                         rann.rann_metric = le32dec(&mrann->rann_metric);
  559                         hwmp_recv_rann(vap, ni, wh, &rann);
  560                         found++;
  561                         break;
  562                 }
  563                 }
  564                 iefrm += iefrm[1] + 2;
  565         }
  566         if (!found) {
  567                 IEEE80211_DISCARD(vap,
  568                     IEEE80211_MSG_ACTION | IEEE80211_MSG_HWMP,
  569                     wh, NULL, "%s", "PATH SEL action without IE");
  570                 vap->iv_stats.is_rx_mgtdiscard++;
  571         }
  572         return 0;
  573 }
  574 
  575 static int
  576 hwmp_send_action(struct ieee80211vap *vap,
  577     const uint8_t da[IEEE80211_ADDR_LEN],
  578     uint8_t *ie, size_t len)
  579 {
  580         struct ieee80211_node *ni;
  581         struct ieee80211com *ic;
  582         struct ieee80211_bpf_params params;
  583         struct mbuf *m;
  584         uint8_t *frm;
  585         int ret;
  586 
  587         if (IEEE80211_IS_MULTICAST(da)) {
  588                 ni = ieee80211_ref_node(vap->iv_bss);
  589 #ifdef IEEE80211_DEBUG_REFCNT
  590                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
  591                 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
  592                 __func__, __LINE__,
  593                 ni, ether_sprintf(ni->ni_macaddr),
  594                 ieee80211_node_refcnt(ni)+1);
  595 #endif
  596                 ieee80211_ref_node(ni);
  597         }
  598         else
  599                 ni = ieee80211_mesh_find_txnode(vap, da);
  600 
  601         if (vap->iv_state == IEEE80211_S_CAC) {
  602                 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, ni,
  603                     "block %s frame in CAC state", "HWMP action");
  604                 vap->iv_stats.is_tx_badstate++;
  605                 return EIO;     /* XXX */
  606         }
  607 
  608         KASSERT(ni != NULL, ("null node"));
  609         ic = ni->ni_ic;
  610 
  611         m = ieee80211_getmgtframe(&frm,
  612             ic->ic_headroom + sizeof(struct ieee80211_frame),
  613             sizeof(struct ieee80211_action) + len
  614         );
  615         if (m == NULL) {
  616                 ieee80211_free_node(ni);
  617                 vap->iv_stats.is_tx_nobuf++;
  618                 return ENOMEM;
  619         }
  620         *frm++ = IEEE80211_ACTION_CAT_MESH;
  621         *frm++ = IEEE80211_ACTION_MESH_HWMP;
  622         switch (*ie) {
  623         case IEEE80211_ELEMID_MESHPREQ:
  624                 frm = hwmp_add_meshpreq(frm,
  625                     (struct ieee80211_meshpreq_ie *)ie);
  626                 break;
  627         case IEEE80211_ELEMID_MESHPREP:
  628                 frm = hwmp_add_meshprep(frm,
  629                     (struct ieee80211_meshprep_ie *)ie);
  630                 break;
  631         case IEEE80211_ELEMID_MESHPERR:
  632                 frm = hwmp_add_meshperr(frm,
  633                     (struct ieee80211_meshperr_ie *)ie);
  634                 break;
  635         case IEEE80211_ELEMID_MESHRANN:
  636                 frm = hwmp_add_meshrann(frm,
  637                     (struct ieee80211_meshrann_ie *)ie);
  638                 break;
  639         }
  640 
  641         m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
  642         M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
  643         if (m == NULL) {
  644                 ieee80211_free_node(ni);
  645                 vap->iv_stats.is_tx_nobuf++;
  646                 return ENOMEM;
  647         }
  648 
  649         IEEE80211_TX_LOCK(ic);
  650 
  651         ieee80211_send_setup(ni, m,
  652             IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_ACTION,
  653             IEEE80211_NONQOS_TID, vap->iv_myaddr, da, vap->iv_myaddr);
  654 
  655         m->m_flags |= M_ENCAP;          /* mark encapsulated */
  656         IEEE80211_NODE_STAT(ni, tx_mgmt);
  657 
  658         memset(&params, 0, sizeof(params));
  659         params.ibp_pri = WME_AC_VO;
  660         params.ibp_rate0 = ni->ni_txparms->mgmtrate;
  661         if (IEEE80211_IS_MULTICAST(da))
  662                 params.ibp_try0 = 1;
  663         else
  664                 params.ibp_try0 = ni->ni_txparms->maxretry;
  665         params.ibp_power = ni->ni_txpower;
  666         ret = ieee80211_raw_output(vap, ni, m, &params);
  667         IEEE80211_TX_UNLOCK(ic);
  668         return (ret);
  669 }
  670 
  671 #define ADDSHORT(frm, v) do {           \
  672         le16enc(frm, v);                \
  673         frm += 2;                       \
  674 } while (0)
  675 #define ADDWORD(frm, v) do {            \
  676         le32enc(frm, v);                \
  677         frm += 4;                       \
  678 } while (0)
  679 /*
  680  * Add a Mesh Path Request IE to a frame.
  681  */
  682 #define PREQ_TFLAGS(n)  preq->preq_targets[n].target_flags
  683 #define PREQ_TADDR(n)   preq->preq_targets[n].target_addr
  684 #define PREQ_TSEQ(n)    preq->preq_targets[n].target_seq
  685 static uint8_t *
  686 hwmp_add_meshpreq(uint8_t *frm, const struct ieee80211_meshpreq_ie *preq)
  687 {
  688         int i;
  689 
  690         *frm++ = IEEE80211_ELEMID_MESHPREQ;
  691         *frm++ = preq->preq_len;        /* len already calculated */
  692         *frm++ = preq->preq_flags;
  693         *frm++ = preq->preq_hopcount;
  694         *frm++ = preq->preq_ttl;
  695         ADDWORD(frm, preq->preq_id);
  696         IEEE80211_ADDR_COPY(frm, preq->preq_origaddr); frm += 6;
  697         ADDWORD(frm, preq->preq_origseq);
  698         if (preq->preq_flags & IEEE80211_MESHPREQ_FLAGS_AE) {
  699                 IEEE80211_ADDR_COPY(frm, preq->preq_orig_ext_addr);
  700                 frm += 6;
  701         }
  702         ADDWORD(frm, preq->preq_lifetime);
  703         ADDWORD(frm, preq->preq_metric);
  704         *frm++ = preq->preq_tcount;
  705         for (i = 0; i < preq->preq_tcount; i++) {
  706                 *frm++ = PREQ_TFLAGS(i);
  707                 IEEE80211_ADDR_COPY(frm, PREQ_TADDR(i));
  708                 frm += 6;
  709                 ADDWORD(frm, PREQ_TSEQ(i));
  710         }
  711         return frm;
  712 }
  713 #undef  PREQ_TFLAGS
  714 #undef  PREQ_TADDR
  715 #undef  PREQ_TSEQ
  716 
  717 /*
  718  * Add a Mesh Path Reply IE to a frame.
  719  */
  720 static uint8_t *
  721 hwmp_add_meshprep(uint8_t *frm, const struct ieee80211_meshprep_ie *prep)
  722 {
  723         *frm++ = IEEE80211_ELEMID_MESHPREP;
  724         *frm++ = prep->prep_len;        /* len already calculated */
  725         *frm++ = prep->prep_flags;
  726         *frm++ = prep->prep_hopcount;
  727         *frm++ = prep->prep_ttl;
  728         IEEE80211_ADDR_COPY(frm, prep->prep_targetaddr); frm += 6;
  729         ADDWORD(frm, prep->prep_targetseq);
  730         if (prep->prep_flags & IEEE80211_MESHPREP_FLAGS_AE) {
  731                 IEEE80211_ADDR_COPY(frm, prep->prep_target_ext_addr);
  732                 frm += 6;
  733         }
  734         ADDWORD(frm, prep->prep_lifetime);
  735         ADDWORD(frm, prep->prep_metric);
  736         IEEE80211_ADDR_COPY(frm, prep->prep_origaddr); frm += 6;
  737         ADDWORD(frm, prep->prep_origseq);
  738         return frm;
  739 }
  740 
  741 /*
  742  * Add a Mesh Path Error IE to a frame.
  743  */
  744 #define PERR_DFLAGS(n)  perr->perr_dests[n].dest_flags
  745 #define PERR_DADDR(n)   perr->perr_dests[n].dest_addr
  746 #define PERR_DSEQ(n)    perr->perr_dests[n].dest_seq
  747 #define PERR_EXTADDR(n) perr->perr_dests[n].dest_ext_addr
  748 #define PERR_DRCODE(n)  perr->perr_dests[n].dest_rcode
  749 static uint8_t *
  750 hwmp_add_meshperr(uint8_t *frm, const struct ieee80211_meshperr_ie *perr)
  751 {
  752         int i;
  753 
  754         *frm++ = IEEE80211_ELEMID_MESHPERR;
  755         *frm++ = perr->perr_len;        /* len already calculated */
  756         *frm++ = perr->perr_ttl;
  757         *frm++ = perr->perr_ndests;
  758         for (i = 0; i < perr->perr_ndests; i++) {
  759                 *frm++ = PERR_DFLAGS(i);
  760                 IEEE80211_ADDR_COPY(frm, PERR_DADDR(i));
  761                 frm += 6;
  762                 ADDWORD(frm, PERR_DSEQ(i));
  763                 if (PERR_DFLAGS(i) & IEEE80211_MESHPERR_FLAGS_AE) {
  764                         IEEE80211_ADDR_COPY(frm, PERR_EXTADDR(i));
  765                         frm += 6;
  766                 }
  767                 ADDSHORT(frm, PERR_DRCODE(i));
  768         }
  769         return frm;
  770 }
  771 #undef  PERR_DFLAGS
  772 #undef  PERR_DADDR
  773 #undef  PERR_DSEQ
  774 #undef  PERR_EXTADDR
  775 #undef  PERR_DRCODE
  776 
  777 /*
  778  * Add a Root Annoucement IE to a frame.
  779  */
  780 static uint8_t *
  781 hwmp_add_meshrann(uint8_t *frm, const struct ieee80211_meshrann_ie *rann)
  782 {
  783         *frm++ = IEEE80211_ELEMID_MESHRANN;
  784         *frm++ = rann->rann_len;
  785         *frm++ = rann->rann_flags;
  786         *frm++ = rann->rann_hopcount;
  787         *frm++ = rann->rann_ttl;
  788         IEEE80211_ADDR_COPY(frm, rann->rann_addr); frm += 6;
  789         ADDWORD(frm, rann->rann_seq);
  790         ADDWORD(frm, rann->rann_interval);
  791         ADDWORD(frm, rann->rann_metric);
  792         return frm;
  793 }
  794 
  795 static void
  796 hwmp_rootmode_setup(struct ieee80211vap *vap)
  797 {
  798         struct ieee80211_hwmp_state *hs = vap->iv_hwmp;
  799         struct ieee80211_mesh_state *ms = vap->iv_mesh;
  800 
  801         switch (hs->hs_rootmode) {
  802         case IEEE80211_HWMP_ROOTMODE_DISABLED:
  803                 callout_drain(&hs->hs_roottimer);
  804                 ms->ms_flags &= ~IEEE80211_MESHFLAGS_ROOT;
  805                 break;
  806         case IEEE80211_HWMP_ROOTMODE_NORMAL:
  807         case IEEE80211_HWMP_ROOTMODE_PROACTIVE:
  808                 callout_reset(&hs->hs_roottimer, ieee80211_hwmp_rootint,
  809                     hwmp_rootmode_cb, vap);
  810                 ms->ms_flags |= IEEE80211_MESHFLAGS_ROOT;
  811                 break;
  812         case IEEE80211_HWMP_ROOTMODE_RANN:
  813                 callout_reset(&hs->hs_roottimer, ieee80211_hwmp_rannint,
  814                     hwmp_rootmode_rann_cb, vap);
  815                 ms->ms_flags |= IEEE80211_MESHFLAGS_ROOT;
  816                 break;
  817         }
  818 }
  819 
  820 /*
  821  * Send a broadcast Path Request to find all nodes on the mesh. We are
  822  * called when the vap is configured as a HWMP root node.
  823  */
  824 #define PREQ_TFLAGS(n)  preq.preq_targets[n].target_flags
  825 #define PREQ_TADDR(n)   preq.preq_targets[n].target_addr
  826 #define PREQ_TSEQ(n)    preq.preq_targets[n].target_seq
  827 static void
  828 hwmp_rootmode_cb(void *arg)
  829 {
  830         struct ieee80211vap *vap = (struct ieee80211vap *)arg;
  831         struct ieee80211_hwmp_state *hs = vap->iv_hwmp;
  832         struct ieee80211_mesh_state *ms = vap->iv_mesh;
  833         struct ieee80211_meshpreq_ie preq;
  834 
  835         IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, vap->iv_bss,
  836             "%s", "send broadcast PREQ");
  837 
  838         preq.preq_flags = 0;
  839         if (ms->ms_flags & IEEE80211_MESHFLAGS_GATE)
  840                 preq.preq_flags |= IEEE80211_MESHPREQ_FLAGS_GATE;
  841         if (hs->hs_rootmode == IEEE80211_HWMP_ROOTMODE_PROACTIVE)
  842                 preq.preq_flags |= IEEE80211_MESHPREQ_FLAGS_PP;
  843         preq.preq_hopcount = 0;
  844         preq.preq_ttl = ms->ms_ttl;
  845         preq.preq_id = ++hs->hs_preqid;
  846         IEEE80211_ADDR_COPY(preq.preq_origaddr, vap->iv_myaddr);
  847         preq.preq_origseq = ++hs->hs_seq;
  848         preq.preq_lifetime = ticks_to_msecs(ieee80211_hwmp_roottimeout);
  849         preq.preq_metric = IEEE80211_MESHLMETRIC_INITIALVAL;
  850         preq.preq_tcount = 1;
  851         IEEE80211_ADDR_COPY(PREQ_TADDR(0), broadcastaddr);
  852         PREQ_TFLAGS(0) = IEEE80211_MESHPREQ_TFLAGS_TO |
  853             IEEE80211_MESHPREQ_TFLAGS_USN;
  854         PREQ_TSEQ(0) = 0;
  855         vap->iv_stats.is_hwmp_rootreqs++;
  856         /* NB: we enforce rate check ourself */
  857         hwmp_send_preq(vap, broadcastaddr, &preq, NULL, NULL);
  858         hwmp_rootmode_setup(vap);
  859 }
  860 #undef  PREQ_TFLAGS
  861 #undef  PREQ_TADDR
  862 #undef  PREQ_TSEQ
  863 
  864 /*
  865  * Send a Root Annoucement (RANN) to find all the nodes on the mesh. We are
  866  * called when the vap is configured as a HWMP RANN root node.
  867  */
  868 static void
  869 hwmp_rootmode_rann_cb(void *arg)
  870 {
  871         struct ieee80211vap *vap = (struct ieee80211vap *)arg;
  872         struct ieee80211_hwmp_state *hs = vap->iv_hwmp;
  873         struct ieee80211_mesh_state *ms = vap->iv_mesh;
  874         struct ieee80211_meshrann_ie rann;
  875 
  876         IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, vap->iv_bss,
  877             "%s", "send broadcast RANN");
  878 
  879         rann.rann_flags = 0;
  880         if (ms->ms_flags & IEEE80211_MESHFLAGS_GATE)
  881                 rann.rann_flags |= IEEE80211_MESHFLAGS_GATE;
  882         rann.rann_hopcount = 0;
  883         rann.rann_ttl = ms->ms_ttl;
  884         IEEE80211_ADDR_COPY(rann.rann_addr, vap->iv_myaddr);
  885         rann.rann_seq = ++hs->hs_seq;
  886         rann.rann_interval = ieee80211_hwmp_rannint;
  887         rann.rann_metric = IEEE80211_MESHLMETRIC_INITIALVAL;
  888 
  889         vap->iv_stats.is_hwmp_rootrann++;
  890         hwmp_send_rann(vap, broadcastaddr, &rann);
  891         hwmp_rootmode_setup(vap);
  892 }
  893 
  894 /*
  895  * Update forwarding information to TA if metric improves.
  896  */
  897 static void
  898 hwmp_update_transmitter(struct ieee80211vap *vap, struct ieee80211_node *ni,
  899     const char *hwmp_frame)
  900 {
  901         struct ieee80211_mesh_state *ms = vap->iv_mesh;
  902         struct ieee80211_mesh_route *rttran = NULL;     /* Transmitter */
  903         int metric = 0;
  904 
  905         rttran = ieee80211_mesh_rt_find(vap, ni->ni_macaddr);
  906         if (rttran == NULL) {
  907                 rttran = ieee80211_mesh_rt_add(vap, ni->ni_macaddr);
  908                 if (rttran == NULL) {
  909                         IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
  910                             "unable to add path to transmitter %6D of %s",
  911                             ni->ni_macaddr, ":", hwmp_frame);
  912                         vap->iv_stats.is_mesh_rtaddfailed++;
  913                         return;
  914                 }
  915         }
  916         metric = ms->ms_pmetric->mpm_metric(ni);
  917         if (!(rttran->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) ||
  918             rttran->rt_metric > metric)
  919         {
  920                 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
  921                     "%s path to transmiter %6D of %s, metric %d:%d",
  922                     rttran->rt_flags & IEEE80211_MESHRT_FLAGS_VALID ?
  923                     "prefer" : "update", ni->ni_macaddr, ":", hwmp_frame,
  924                     rttran->rt_metric, metric);
  925                 IEEE80211_ADDR_COPY(rttran->rt_nexthop, ni->ni_macaddr);
  926                 rttran->rt_metric = metric;
  927                 rttran->rt_nhops  = 1;
  928                 ieee80211_mesh_rt_update(rttran, ms->ms_ppath->mpp_inact);
  929                 rttran->rt_flags = IEEE80211_MESHRT_FLAGS_VALID;
  930         }
  931 }
  932 
  933 #define PREQ_TFLAGS(n)  preq->preq_targets[n].target_flags
  934 #define PREQ_TADDR(n)   preq->preq_targets[n].target_addr
  935 #define PREQ_TSEQ(n)    preq->preq_targets[n].target_seq
  936 static void
  937 hwmp_recv_preq(struct ieee80211vap *vap, struct ieee80211_node *ni,
  938     const struct ieee80211_frame *wh, const struct ieee80211_meshpreq_ie *preq)
  939 {
  940         struct ieee80211_mesh_state *ms = vap->iv_mesh;
  941         struct ieee80211_mesh_route *rtorig = NULL;
  942         struct ieee80211_mesh_route *rtorig_ext = NULL;
  943         struct ieee80211_mesh_route *rttarg = NULL;
  944         struct ieee80211_hwmp_route *hrorig = NULL;
  945         struct ieee80211_hwmp_route *hrtarg = NULL;
  946         struct ieee80211_hwmp_state *hs = vap->iv_hwmp;
  947         ieee80211_hwmp_seq preqid;      /* last seen preqid for orig */
  948         uint32_t metric = 0;
  949 
  950         /*
  951          * Ignore PREQs from us. Could happen because someone forward it
  952          * back to us.
  953          */
  954         if (IEEE80211_ADDR_EQ(vap->iv_myaddr, preq->preq_origaddr))
  955                 return;
  956 
  957         IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
  958             "received PREQ, orig %6D, targ(0) %6D", preq->preq_origaddr, ":",
  959             PREQ_TADDR(0), ":");
  960 
  961         /*
  962          * Acceptance criteria: (if the PREQ is not for us or not broadcast,
  963          * or an external mac address not proxied by us),
  964          * AND forwarding is disabled, discard this PREQ.
  965          */
  966         rttarg = ieee80211_mesh_rt_find(vap, PREQ_TADDR(0));
  967         if (!(ms->ms_flags & IEEE80211_MESHFLAGS_FWD) &&
  968             (!IEEE80211_ADDR_EQ(vap->iv_myaddr, PREQ_TADDR(0)) ||
  969             !IEEE80211_IS_MULTICAST(PREQ_TADDR(0)) ||
  970             (rttarg != NULL &&
  971             rttarg->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY &&
  972             IEEE80211_ADDR_EQ(vap->iv_myaddr, rttarg->rt_mesh_gate)))) {
  973                 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_HWMP,
  974                     preq->preq_origaddr, NULL, "%s", "not accepting PREQ");
  975                 return;
  976         }
  977         /*
  978          * Acceptance criteria: if unicast addressed 
  979          * AND no valid forwarding for Target of PREQ, discard this PREQ.
  980          */
  981         if(rttarg != NULL)
  982                 hrtarg = IEEE80211_MESH_ROUTE_PRIV(rttarg,
  983                     struct ieee80211_hwmp_route);
  984         /* Address mode: ucast */
  985         if(preq->preq_flags & IEEE80211_MESHPREQ_FLAGS_AM &&
  986             rttarg == NULL &&
  987             !IEEE80211_ADDR_EQ(vap->iv_myaddr, PREQ_TADDR(0))) {
  988                 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_HWMP,
  989                     preq->preq_origaddr, NULL,
  990                     "unicast addressed PREQ of unknown target %6D",
  991                     PREQ_TADDR(0), ":");
  992                 return;
  993         }
  994 
  995         /* PREQ ACCEPTED */
  996 
  997         rtorig = ieee80211_mesh_rt_find(vap, preq->preq_origaddr);
  998         if (rtorig == NULL) {
  999                 rtorig = ieee80211_mesh_rt_add(vap, preq->preq_origaddr);
 1000                 if (rtorig == NULL) {
 1001                         IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
 1002                             "unable to add orig path to %6D",
 1003                             preq->preq_origaddr, ":");
 1004                         vap->iv_stats.is_mesh_rtaddfailed++;
 1005                         return;
 1006                 }
 1007                 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
 1008                     "adding originator %6D", preq->preq_origaddr, ":");
 1009         }
 1010         hrorig = IEEE80211_MESH_ROUTE_PRIV(rtorig, struct ieee80211_hwmp_route);
 1011 
 1012         /* record last seen preqid */
 1013         preqid = hrorig->hr_preqid;
 1014         hrorig->hr_preqid = HWMP_SEQ_MAX(hrorig->hr_preqid, preq->preq_id);
 1015 
 1016         /* Data creation and update of forwarding information
 1017          * according to Table 11C-8 for originator mesh STA.
 1018          */
 1019         metric = preq->preq_metric + ms->ms_pmetric->mpm_metric(ni);
 1020         if (HWMP_SEQ_GT(preq->preq_origseq, hrorig->hr_seq) ||
 1021             (HWMP_SEQ_EQ(preq->preq_origseq, hrorig->hr_seq) &&
 1022             metric < rtorig->rt_metric)) {
 1023                 hrorig->hr_seq = preq->preq_origseq;
 1024                 IEEE80211_ADDR_COPY(rtorig->rt_nexthop, wh->i_addr2);
 1025                 rtorig->rt_metric = metric;
 1026                 rtorig->rt_nhops  = preq->preq_hopcount + 1;
 1027                 ieee80211_mesh_rt_update(rtorig, preq->preq_lifetime);
 1028                 /* Path to orig is valid now.
 1029                  * NB: we know it can't be Proxy, and if it is GATE
 1030                  * it will be marked below.
 1031                  */
 1032                 rtorig->rt_flags = IEEE80211_MESHRT_FLAGS_VALID;
 1033         } else if ((hrtarg != NULL &&
 1034             !HWMP_SEQ_EQ(hrtarg->hr_seq, PREQ_TSEQ(0))) ||
 1035             (rtorig->rt_flags & IEEE80211_MESHRT_FLAGS_VALID &&
 1036             preqid >= preq->preq_id)) {
 1037                 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
 1038                     "discard PREQ from %6D, old seqno %u <= %u,"
 1039                     " or old preqid %u < %u",
 1040                     preq->preq_origaddr, ":",
 1041                     preq->preq_origseq, hrorig->hr_seq,
 1042                     preq->preq_id, preqid);
 1043                 return;
 1044         }
 1045 
 1046         /* Update forwarding information to TA if metric improves. */
 1047         hwmp_update_transmitter(vap, ni, "PREQ");
 1048 
 1049         /*
 1050          * Check if the PREQ is addressed to us.
 1051          * or a Proxy currently gated by us.
 1052          */
 1053         if (IEEE80211_ADDR_EQ(vap->iv_myaddr, PREQ_TADDR(0)) ||
 1054             (ms->ms_flags & IEEE80211_MESHFLAGS_GATE &&
 1055             rttarg != NULL &&
 1056             IEEE80211_ADDR_EQ(vap->iv_myaddr, rttarg->rt_mesh_gate) &&
 1057             rttarg->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY &&
 1058             rttarg->rt_flags & IEEE80211_MESHRT_FLAGS_VALID)) {
 1059                 struct ieee80211_meshprep_ie prep;
 1060 
 1061                 /*
 1062                  * When we are the target we shall update our own HWMP seq
 1063                  * number with max of (current and preq->seq) + 1
 1064                  */
 1065                 hs->hs_seq = HWMP_SEQ_MAX(hs->hs_seq, PREQ_TSEQ(0)) + 1;
 1066 
 1067                 prep.prep_flags = 0;
 1068                 prep.prep_hopcount = 0;
 1069                 prep.prep_metric = IEEE80211_MESHLMETRIC_INITIALVAL;
 1070                 IEEE80211_ADDR_COPY(prep.prep_targetaddr, vap->iv_myaddr);
 1071                 if (rttarg != NULL && /* if NULL it means we are the target */
 1072                     rttarg->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) {
 1073                         IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
 1074                             "reply for proxy %6D", rttarg->rt_dest, ":");
 1075                         prep.prep_flags |= IEEE80211_MESHPREP_FLAGS_AE;
 1076                         IEEE80211_ADDR_COPY(prep.prep_target_ext_addr,
 1077                             rttarg->rt_dest);
 1078                         /* update proxy seqno to HWMP seqno */
 1079                         rttarg->rt_ext_seq = hs->hs_seq;
 1080                         prep.prep_hopcount = rttarg->rt_nhops;
 1081                         prep.prep_metric = rttarg->rt_metric;
 1082                         IEEE80211_ADDR_COPY(prep.prep_targetaddr, rttarg->rt_mesh_gate);
 1083                 }
 1084                 /*
 1085                  * Build and send a PREP frame.
 1086                  */
 1087                 prep.prep_ttl = ms->ms_ttl;
 1088                 prep.prep_targetseq = hs->hs_seq;
 1089                 prep.prep_lifetime = preq->preq_lifetime;
 1090                 IEEE80211_ADDR_COPY(prep.prep_origaddr, preq->preq_origaddr);
 1091                 prep.prep_origseq = preq->preq_origseq;
 1092 
 1093                 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
 1094                     "reply to %6D", preq->preq_origaddr, ":");
 1095                 hwmp_send_prep(vap, wh->i_addr2, &prep);
 1096                 return;
 1097         }
 1098         /* we may update our proxy information for the orig external */
 1099         else if (preq->preq_flags & IEEE80211_MESHPREQ_FLAGS_AE) {
 1100                 rtorig_ext =
 1101                     ieee80211_mesh_rt_find(vap, preq->preq_orig_ext_addr);
 1102                 if (rtorig_ext == NULL) {
 1103                         rtorig_ext = ieee80211_mesh_rt_add(vap,
 1104                             preq->preq_orig_ext_addr);
 1105                         if (rtorig_ext == NULL) {
 1106                                 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
 1107                                     "unable to add orig ext proxy to %6D",
 1108                                     preq->preq_orig_ext_addr, ":");
 1109                                 vap->iv_stats.is_mesh_rtaddfailed++;
 1110                                 return;
 1111                         }
 1112                         IEEE80211_ADDR_COPY(rtorig_ext->rt_mesh_gate,
 1113                             preq->preq_origaddr);
 1114                 }
 1115                 rtorig_ext->rt_ext_seq = preq->preq_origseq;
 1116                 ieee80211_mesh_rt_update(rtorig_ext, preq->preq_lifetime);
 1117         }
 1118         /*
 1119          * Proactive PREQ: reply with a proactive PREP to the
 1120          * root STA if requested.
 1121          */
 1122         if (IEEE80211_ADDR_EQ(PREQ_TADDR(0), broadcastaddr) &&
 1123             (PREQ_TFLAGS(0) & IEEE80211_MESHPREQ_TFLAGS_TO)) {
 1124                 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
 1125                     "root mesh station @ %6D", preq->preq_origaddr, ":");
 1126 
 1127                 /* Check if root is a mesh gate, mark it */
 1128                 if (preq->preq_flags & IEEE80211_MESHPREQ_FLAGS_GATE) {
 1129                         struct ieee80211_mesh_gate_route *gr;
 1130 
 1131                         rtorig->rt_flags |= IEEE80211_MESHRT_FLAGS_GATE;
 1132                         gr = ieee80211_mesh_mark_gate(vap, preq->preq_origaddr,
 1133                             rtorig);
 1134                         gr->gr_lastseq = 0; /* NOT GANN */
 1135                 }
 1136 
 1137                 /*
 1138                  * Reply with a PREP if we don't have a path to the root
 1139                  * or if the root sent us a proactive PREQ.
 1140                  */
 1141                 if ((rtorig->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0 ||
 1142                     (preq->preq_flags & IEEE80211_MESHPREQ_FLAGS_PP)) {
 1143                         struct ieee80211_meshprep_ie prep;
 1144 
 1145                         prep.prep_flags = 0;
 1146                         prep.prep_hopcount = 0;
 1147                         prep.prep_ttl = ms->ms_ttl;
 1148                         IEEE80211_ADDR_COPY(prep.prep_origaddr,
 1149                             preq->preq_origaddr);
 1150                         prep.prep_origseq = preq->preq_origseq;
 1151                         prep.prep_lifetime = preq->preq_lifetime;
 1152                         prep.prep_metric = IEEE80211_MESHLMETRIC_INITIALVAL;
 1153                         IEEE80211_ADDR_COPY(prep.prep_targetaddr,
 1154                             vap->iv_myaddr);
 1155                         prep.prep_targetseq = ++hs->hs_seq;
 1156                         hwmp_send_prep(vap, rtorig->rt_nexthop, &prep);
 1157                 }
 1158         }
 1159 
 1160         /*
 1161          * Forwarding and Intermediate reply for PREQs with 1 target.
 1162          */
 1163         if ((preq->preq_tcount == 1) && (preq->preq_ttl > 1) &&
 1164             (ms->ms_flags & IEEE80211_MESHFLAGS_FWD)) {
 1165                 struct ieee80211_meshpreq_ie ppreq; /* propagated PREQ */
 1166 
 1167                 memcpy(&ppreq, preq, sizeof(ppreq));
 1168 
 1169                 /*
 1170                  * We have a valid route to this node.
 1171                  * NB: if target is proxy dont reply.
 1172                  */
 1173                 if (rttarg != NULL &&
 1174                     rttarg->rt_flags & IEEE80211_MESHRT_FLAGS_VALID &&
 1175                     !(rttarg->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY)) {
 1176                         /*
 1177                          * Check if we can send an intermediate Path Reply,
 1178                          * i.e., Target Only bit is not set and target is not
 1179                          * the MAC broadcast address.
 1180                          */
 1181                         if (!(PREQ_TFLAGS(0) & IEEE80211_MESHPREQ_TFLAGS_TO) &&
 1182                             !IEEE80211_ADDR_EQ(PREQ_TADDR(0), broadcastaddr)) {
 1183                                 struct ieee80211_meshprep_ie prep;
 1184 
 1185                                 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
 1186                                     "intermediate reply for PREQ from %6D",
 1187                                     preq->preq_origaddr, ":");
 1188                                 prep.prep_flags = 0;
 1189                                 prep.prep_hopcount = rttarg->rt_nhops;
 1190                                 prep.prep_ttl = ms->ms_ttl;
 1191                                 IEEE80211_ADDR_COPY(&prep.prep_targetaddr,
 1192                                     PREQ_TADDR(0));
 1193                                 prep.prep_targetseq = hrtarg->hr_seq;
 1194                                 prep.prep_lifetime = preq->preq_lifetime;
 1195                                 prep.prep_metric =rttarg->rt_metric;
 1196                                 IEEE80211_ADDR_COPY(&prep.prep_origaddr,
 1197                                     preq->preq_origaddr);
 1198                                 prep.prep_origseq = hrorig->hr_seq;
 1199                                 hwmp_send_prep(vap, rtorig->rt_nexthop, &prep);
 1200 
 1201                                 /*
 1202                                  * Set TO and unset RF bits because we have
 1203                                  * sent a PREP.
 1204                                  */
 1205                                 ppreq.preq_targets[0].target_flags |=
 1206                                     IEEE80211_MESHPREQ_TFLAGS_TO;
 1207                         }
 1208                 }
 1209 
 1210                 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
 1211                     "forward PREQ from %6D",
 1212                     preq->preq_origaddr, ":");
 1213                 ppreq.preq_hopcount += 1;
 1214                 ppreq.preq_ttl -= 1;
 1215                 ppreq.preq_metric += ms->ms_pmetric->mpm_metric(ni);
 1216 
 1217                 /* don't do PREQ ratecheck when we propagate */
 1218                 hwmp_send_preq(vap, broadcastaddr, &ppreq, NULL, NULL);
 1219         }
 1220 }
 1221 #undef  PREQ_TFLAGS
 1222 #undef  PREQ_TADDR
 1223 #undef  PREQ_TSEQ
 1224 
 1225 static int
 1226 hwmp_send_preq(struct ieee80211vap *vap,
 1227     const uint8_t da[IEEE80211_ADDR_LEN],
 1228     struct ieee80211_meshpreq_ie *preq,
 1229     struct timeval *last, struct timeval *minint)
 1230 {
 1231 
 1232         /*
 1233          * Enforce PREQ interval.
 1234          * NB: Proactive ROOT PREQs rate is handled by cb task.
 1235          */
 1236         if (last != NULL && minint != NULL) {
 1237                 if (ratecheck(last, minint) == 0)
 1238                         return EALREADY; /* XXX: we should postpone */
 1239                 getmicrouptime(last);
 1240         }
 1241 
 1242         /*
 1243          * mesh preq action frame format
 1244          *     [6] da
 1245          *     [6] sa
 1246          *     [6] addr3 = sa
 1247          *     [1] action
 1248          *     [1] category
 1249          *     [tlv] mesh path request
 1250          */
 1251         preq->preq_ie = IEEE80211_ELEMID_MESHPREQ;
 1252         preq->preq_len = (preq->preq_flags & IEEE80211_MESHPREQ_FLAGS_AE ?
 1253             IEEE80211_MESHPREQ_BASE_SZ_AE : IEEE80211_MESHPREQ_BASE_SZ) +
 1254             preq->preq_tcount * IEEE80211_MESHPREQ_TRGT_SZ;
 1255         return hwmp_send_action(vap, da, (uint8_t *)preq, preq->preq_len+2);
 1256 }
 1257 
 1258 static void
 1259 hwmp_recv_prep(struct ieee80211vap *vap, struct ieee80211_node *ni,
 1260     const struct ieee80211_frame *wh, const struct ieee80211_meshprep_ie *prep)
 1261 {
 1262 #define IS_PROXY(rt)    (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY)
 1263 #define PROXIED_BY_US(rt)               \
 1264     (IEEE80211_ADDR_EQ(vap->iv_myaddr, rt->rt_mesh_gate))
 1265         struct ieee80211_mesh_state *ms = vap->iv_mesh;
 1266         struct ieee80211_hwmp_state *hs = vap->iv_hwmp;
 1267         struct ieee80211_mesh_route *rt = NULL;
 1268         struct ieee80211_mesh_route *rtorig = NULL;
 1269         struct ieee80211_mesh_route *rtext = NULL;
 1270         struct ieee80211_hwmp_route *hr;
 1271         struct ieee80211com *ic = vap->iv_ic;
 1272         struct mbuf *m, *next;
 1273         uint32_t metric = 0;
 1274         const uint8_t *addr;
 1275 
 1276         IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
 1277             "received PREP, orig %6D, targ %6D", prep->prep_origaddr, ":",
 1278             prep->prep_targetaddr, ":");
 1279 
 1280         /*
 1281          * Acceptance criteria: (If the corresponding PREP was not generated
 1282          * by us OR not generated by an external mac that is not proxied by us)
 1283          * AND forwarding is disabled, discard this PREP.
 1284          */
 1285         rtorig = ieee80211_mesh_rt_find(vap, prep->prep_origaddr);
 1286         if ((!IEEE80211_ADDR_EQ(vap->iv_myaddr, prep->prep_origaddr) ||
 1287             (rtorig != NULL && IS_PROXY(rtorig) && !PROXIED_BY_US(rtorig))) &&
 1288             !(ms->ms_flags & IEEE80211_MESHFLAGS_FWD)){
 1289                 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
 1290                     "discard PREP, orig(%6D) not proxied or generated by us",
 1291                     prep->prep_origaddr, ":");
 1292                 return;
 1293         }
 1294 
 1295         /* PREP ACCEPTED */
 1296 
 1297         /*
 1298          * If accepted shall create or update the active forwarding information
 1299          * it maintains for the target mesh STA of the PREP (according to the
 1300          * rules defined in 13.10.8.4). If the conditions for creating or
 1301          * updating the forwarding information have not been met in those
 1302          * rules, no further steps are applied to the PREP.
 1303          */
 1304         rt = ieee80211_mesh_rt_find(vap, prep->prep_targetaddr);
 1305         if (rt == NULL) {
 1306                 rt = ieee80211_mesh_rt_add(vap, prep->prep_targetaddr);
 1307                 if (rt == NULL) {
 1308                         IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
 1309                             "unable to add PREP path to %6D",
 1310                             prep->prep_targetaddr, ":");
 1311                         vap->iv_stats.is_mesh_rtaddfailed++;
 1312                         return;
 1313                 }
 1314                 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
 1315                     "adding target %6D", prep->prep_targetaddr, ":");
 1316         }
 1317         hr = IEEE80211_MESH_ROUTE_PRIV(rt, struct ieee80211_hwmp_route);
 1318         /* update path metric */
 1319         metric = prep->prep_metric + ms->ms_pmetric->mpm_metric(ni);
 1320         if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID)) {
 1321                 if (HWMP_SEQ_LT(prep->prep_targetseq, hr->hr_seq)) {
 1322                         IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
 1323                             "discard PREP from %6D, old seq no %u < %u",
 1324                             prep->prep_targetaddr, ":",
 1325                             prep->prep_targetseq, hr->hr_seq);
 1326                         return;
 1327                 } else if (HWMP_SEQ_LEQ(prep->prep_targetseq, hr->hr_seq) &&
 1328                     metric > rt->rt_metric) {
 1329                         IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
 1330                             "discard PREP from %6D, new metric %u > %u",
 1331                             prep->prep_targetaddr, ":",
 1332                             metric, rt->rt_metric);
 1333                         return;
 1334                 }
 1335         }
 1336 
 1337         IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
 1338             "%s path to %6D, hopcount %d:%d metric %d:%d",
 1339             rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID ?
 1340             "prefer" : "update",
 1341             prep->prep_targetaddr, ":",
 1342             rt->rt_nhops, prep->prep_hopcount + 1,
 1343             rt->rt_metric, metric);
 1344 
 1345         hr->hr_seq = prep->prep_targetseq;
 1346         hr->hr_preqretries = 0;
 1347         IEEE80211_ADDR_COPY(rt->rt_nexthop, ni->ni_macaddr);
 1348         rt->rt_metric = metric;
 1349         rt->rt_nhops = prep->prep_hopcount + 1;
 1350         ieee80211_mesh_rt_update(rt, prep->prep_lifetime);
 1351         if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_DISCOVER) {
 1352                 /* discovery complete */
 1353                 rt->rt_flags &= ~IEEE80211_MESHRT_FLAGS_DISCOVER;
 1354         }
 1355         rt->rt_flags |= IEEE80211_MESHRT_FLAGS_VALID; /* mark valid */
 1356 
 1357         /* Update forwarding information to TA if metric improves */
 1358         hwmp_update_transmitter(vap, ni, "PREP");
 1359 
 1360         /*
 1361          * If it's NOT for us, propagate the PREP
 1362          */
 1363         if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, prep->prep_origaddr) &&
 1364             prep->prep_ttl > 1 &&
 1365             prep->prep_hopcount < hs->hs_maxhops) {
 1366                 struct ieee80211_meshprep_ie pprep; /* propagated PREP */
 1367                 /*
 1368                  * NB: We should already have setup the path to orig
 1369                  * mesh STA when we propagated PREQ to target mesh STA,
 1370                  * no PREP is generated without a corresponding PREQ.
 1371                  * XXX: for now just ignore.
 1372                  */
 1373                 if (rtorig == NULL) {
 1374                         IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
 1375                             "received PREP for an unknown orig(%6D)",
 1376                             prep->prep_origaddr, ":");
 1377                         return;
 1378                 }
 1379 
 1380                 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
 1381                     "propagate PREP from %6D",
 1382                     prep->prep_targetaddr, ":");
 1383 
 1384                 memcpy(&pprep, prep, sizeof(pprep));
 1385                 pprep.prep_hopcount += 1;
 1386                 pprep.prep_ttl -= 1;
 1387                 pprep.prep_metric += ms->ms_pmetric->mpm_metric(ni);
 1388                 hwmp_send_prep(vap, rtorig->rt_nexthop, &pprep);
 1389 
 1390                 /* precursor list for the Target Mesh STA Address is updated */
 1391         }
 1392 
 1393         /*
 1394          * Check if we received a PREP w/ AE and store target external address.
 1395          * We may store target external address if recevied PREP w/ AE
 1396          * and we are not final destination
 1397          */
 1398         if (prep->prep_flags & IEEE80211_MESHPREP_FLAGS_AE) {
 1399                 rtext = ieee80211_mesh_rt_find(vap,
 1400                         prep->prep_target_ext_addr);
 1401                 if (rtext == NULL) {
 1402                         rtext = ieee80211_mesh_rt_add(vap,
 1403                                 prep->prep_target_ext_addr);
 1404                         if (rtext == NULL) {
 1405                                 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
 1406                                     "unable to add PREP path to proxy %6D",
 1407                                     prep->prep_targetaddr, ":");
 1408                                 vap->iv_stats.is_mesh_rtaddfailed++;
 1409                                 return;
 1410                         }
 1411                 }
 1412                 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
 1413                     "%s path to %6D, hopcount %d:%d metric %d:%d",
 1414                     rtext->rt_flags & IEEE80211_MESHRT_FLAGS_VALID ?
 1415                     "prefer" : "update",
 1416                     prep->prep_target_ext_addr, ":",
 1417                     rtext->rt_nhops, prep->prep_hopcount + 1,
 1418                     rtext->rt_metric, metric);
 1419 
 1420                 rtext->rt_flags = IEEE80211_MESHRT_FLAGS_PROXY |
 1421                         IEEE80211_MESHRT_FLAGS_VALID;
 1422                 IEEE80211_ADDR_COPY(rtext->rt_dest,
 1423                     prep->prep_target_ext_addr);
 1424                 IEEE80211_ADDR_COPY(rtext->rt_mesh_gate,
 1425                     prep->prep_targetaddr);
 1426                 IEEE80211_ADDR_COPY(rtext->rt_nexthop, wh->i_addr2);
 1427                 rtext->rt_metric = metric;
 1428                 rtext->rt_lifetime = prep->prep_lifetime;
 1429                 rtext->rt_nhops = prep->prep_hopcount + 1;
 1430                 rtext->rt_ext_seq = prep->prep_origseq; /* new proxy seq */
 1431                 /*
 1432                  * XXX: proxy entries have no HWMP priv data,
 1433                  * nullify them to be sure?
 1434                  */
 1435         }
 1436         /*
 1437          * Check for frames queued awaiting path discovery.
 1438          * XXX probably can tell exactly and avoid remove call
 1439          * NB: hash may have false matches, if so they will get
 1440          *     stuck back on the stageq because there won't be
 1441          *     a path.
 1442          */
 1443         addr = prep->prep_flags & IEEE80211_MESHPREP_FLAGS_AE ?
 1444             prep->prep_target_ext_addr : prep->prep_targetaddr;
 1445         m = ieee80211_ageq_remove(&ic->ic_stageq,
 1446             (struct ieee80211_node *)(uintptr_t)
 1447             ieee80211_mac_hash(ic, addr)); /* either dest or ext_dest */
 1448 
 1449         /*
 1450          * All frames in the stageq here should be non-M_ENCAP; or things
 1451          * will get very unhappy.
 1452          */
 1453         for (; m != NULL; m = next) {
 1454                 next = m->m_nextpkt;
 1455                 m->m_nextpkt = NULL;
 1456                 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
 1457                     "flush queued frame %p len %d", m, m->m_pkthdr.len);
 1458                 /*
 1459                  * If the mbuf has M_ENCAP set, ensure we free it.
 1460                  * Note that after if_transmit() is called, m is invalid.
 1461                  */
 1462                 (void) ieee80211_vap_xmitpkt(vap, m);
 1463         }
 1464 #undef  IS_PROXY
 1465 #undef  PROXIED_BY_US
 1466 }
 1467 
 1468 static int
 1469 hwmp_send_prep(struct ieee80211vap *vap,
 1470     const uint8_t da[IEEE80211_ADDR_LEN],
 1471     struct ieee80211_meshprep_ie *prep)
 1472 {
 1473         /* NB: there's no PREP minimum interval. */
 1474 
 1475         /*
 1476          * mesh prep action frame format
 1477          *     [6] da
 1478          *     [6] sa
 1479          *     [6] addr3 = sa
 1480          *     [1] action
 1481          *     [1] category
 1482          *     [tlv] mesh path reply
 1483          */
 1484         prep->prep_ie = IEEE80211_ELEMID_MESHPREP;
 1485         prep->prep_len = prep->prep_flags & IEEE80211_MESHPREP_FLAGS_AE ?
 1486             IEEE80211_MESHPREP_BASE_SZ_AE : IEEE80211_MESHPREP_BASE_SZ;
 1487         return hwmp_send_action(vap, da, (uint8_t *)prep, prep->prep_len + 2);
 1488 }
 1489 
 1490 #define PERR_DFLAGS(n)  perr.perr_dests[n].dest_flags
 1491 #define PERR_DADDR(n)   perr.perr_dests[n].dest_addr
 1492 #define PERR_DSEQ(n)    perr.perr_dests[n].dest_seq
 1493 #define PERR_DRCODE(n)  perr.perr_dests[n].dest_rcode
 1494 static void
 1495 hwmp_peerdown(struct ieee80211_node *ni)
 1496 {
 1497         struct ieee80211vap *vap = ni->ni_vap;
 1498         struct ieee80211_mesh_state *ms = vap->iv_mesh;
 1499         struct ieee80211_meshperr_ie perr;
 1500         struct ieee80211_mesh_route *rt;
 1501         struct ieee80211_hwmp_route *hr;
 1502 
 1503         rt = ieee80211_mesh_rt_find(vap, ni->ni_macaddr);
 1504         if (rt == NULL)
 1505                 return;
 1506         hr = IEEE80211_MESH_ROUTE_PRIV(rt, struct ieee80211_hwmp_route);
 1507         IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
 1508             "%s", "delete route entry");
 1509         perr.perr_ttl = ms->ms_ttl;
 1510         perr.perr_ndests = 1;
 1511         PERR_DFLAGS(0) = 0;
 1512         if (hr->hr_seq == 0)
 1513                 PERR_DFLAGS(0) |= IEEE80211_MESHPERR_DFLAGS_USN;
 1514         PERR_DFLAGS(0) |= IEEE80211_MESHPERR_DFLAGS_RC;
 1515         IEEE80211_ADDR_COPY(PERR_DADDR(0), rt->rt_dest);
 1516         PERR_DSEQ(0) = ++hr->hr_seq;
 1517         PERR_DRCODE(0) = IEEE80211_REASON_MESH_PERR_DEST_UNREACH;
 1518         /* NB: flush everything passing through peer */
 1519         ieee80211_mesh_rt_flush_peer(vap, ni->ni_macaddr);
 1520         hwmp_send_perr(vap, broadcastaddr, &perr);
 1521 }
 1522 #undef  PERR_DFLAGS
 1523 #undef  PERR_DADDR
 1524 #undef  PERR_DSEQ
 1525 #undef  PERR_DRCODE
 1526 
 1527 #define PERR_DFLAGS(n)          perr->perr_dests[n].dest_flags
 1528 #define PERR_DADDR(n)           perr->perr_dests[n].dest_addr
 1529 #define PERR_DSEQ(n)            perr->perr_dests[n].dest_seq
 1530 #define PERR_DEXTADDR(n)        perr->perr_dests[n].dest_ext_addr
 1531 static void
 1532 hwmp_recv_perr(struct ieee80211vap *vap, struct ieee80211_node *ni,
 1533     const struct ieee80211_frame *wh, const struct ieee80211_meshperr_ie *perr)
 1534 {
 1535         struct ieee80211_mesh_state *ms = vap->iv_mesh;
 1536         struct ieee80211_mesh_route *rt = NULL;
 1537         struct ieee80211_mesh_route *rt_ext = NULL;
 1538         struct ieee80211_hwmp_route *hr;
 1539         struct ieee80211_meshperr_ie *pperr = NULL;
 1540         int i, j = 0, forward = 0;
 1541 
 1542         IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
 1543             "received PERR from %6D", wh->i_addr2, ":");
 1544 
 1545         /*
 1546          * if forwarding is true, prepare pperr
 1547          */
 1548         if (ms->ms_flags & IEEE80211_MESHFLAGS_FWD) {
 1549                 forward = 1;
 1550                 pperr = IEEE80211_MALLOC(sizeof(*perr) + 31*sizeof(*perr->perr_dests),
 1551                     M_80211_MESH_PERR, IEEE80211_M_NOWAIT); /* XXX: magic number, 32 err dests */
 1552         }
 1553 
 1554         /*
 1555          * Acceptance criteria: check if we have forwarding information
 1556          * stored about destination, and that nexthop == TA of this PERR.
 1557          * NB: we also build a new PERR to propagate in case we should forward.
 1558          */
 1559         for (i = 0; i < perr->perr_ndests; i++) {
 1560                 rt = ieee80211_mesh_rt_find(vap, PERR_DADDR(i));
 1561                 if (rt == NULL)
 1562                         continue;
 1563                 if (!IEEE80211_ADDR_EQ(rt->rt_nexthop, wh->i_addr2))
 1564                         continue;
 1565 
 1566                 /* found and accepted a PERR ndest element, process it... */
 1567                 if (forward)
 1568                         memcpy(&pperr->perr_dests[j], &perr->perr_dests[i],
 1569                             sizeof(*perr->perr_dests));
 1570                 hr = IEEE80211_MESH_ROUTE_PRIV(rt, struct ieee80211_hwmp_route);
 1571                 switch(PERR_DFLAGS(i)) {
 1572                 case (IEEE80211_REASON_MESH_PERR_NO_FI):
 1573                         if (PERR_DSEQ(i) == 0) {
 1574                                 hr->hr_seq++;
 1575                                 if (forward) {
 1576                                         pperr->perr_dests[j].dest_seq =
 1577                                             hr->hr_seq;
 1578                                 }
 1579                         } else {
 1580                                 hr->hr_seq = PERR_DSEQ(i);
 1581                         }
 1582                         rt->rt_flags &= ~IEEE80211_MESHRT_FLAGS_VALID;
 1583                         j++;
 1584                         break;
 1585                 case (IEEE80211_REASON_MESH_PERR_DEST_UNREACH):
 1586                         if(HWMP_SEQ_GT(PERR_DSEQ(i), hr->hr_seq)) {
 1587                                 hr->hr_seq = PERR_DSEQ(i);
 1588                                 rt->rt_flags &= ~IEEE80211_MESHRT_FLAGS_VALID;
 1589                                 j++;
 1590                         }
 1591                         break;
 1592                 case (IEEE80211_REASON_MESH_PERR_NO_PROXY):
 1593                         rt_ext = ieee80211_mesh_rt_find(vap, PERR_DEXTADDR(i));
 1594                         if (rt_ext != NULL) {
 1595                                 rt_ext->rt_flags &=
 1596                                     ~IEEE80211_MESHRT_FLAGS_VALID;
 1597                                 j++;
 1598                         }
 1599                         break;
 1600                 default:
 1601                         IEEE80211_DISCARD(vap, IEEE80211_MSG_HWMP, wh, NULL,
 1602                             "PERR, unknown reason code %u\n", PERR_DFLAGS(i));
 1603                         goto done; /* XXX: stats?? */
 1604                 }
 1605                 ieee80211_mesh_rt_flush_peer(vap, PERR_DADDR(i));
 1606                 KASSERT(j < 32, ("PERR, error ndest >= 32 (%u)", j));
 1607         }
 1608         if (j == 0) {
 1609                 IEEE80211_DISCARD(vap, IEEE80211_MSG_HWMP, wh, NULL, "%s",
 1610                     "PERR not accepted");
 1611                 goto done; /* XXX: stats?? */
 1612         }
 1613 
 1614         /*
 1615          * Propagate the PERR if we previously found it on our routing table.
 1616          */
 1617         if (forward && perr->perr_ttl > 1) {
 1618                 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
 1619                     "propagate PERR from %6D", wh->i_addr2, ":");
 1620                 pperr->perr_ndests = j;
 1621                 pperr->perr_ttl--;
 1622                 hwmp_send_perr(vap, broadcastaddr, pperr);
 1623         }
 1624 done:
 1625         if (pperr != NULL)
 1626                 IEEE80211_FREE(pperr, M_80211_MESH_PERR);
 1627 }
 1628 #undef  PERR_DFLAGS
 1629 #undef  PERR_DADDR
 1630 #undef  PERR_DSEQ
 1631 #undef  PERR_DEXTADDR
 1632 
 1633 static int
 1634 hwmp_send_perr(struct ieee80211vap *vap,
 1635     const uint8_t da[IEEE80211_ADDR_LEN],
 1636     struct ieee80211_meshperr_ie *perr)
 1637 {
 1638         struct ieee80211_hwmp_state *hs = vap->iv_hwmp;
 1639         int i;
 1640         uint8_t length = 0;
 1641 
 1642         /*
 1643          * Enforce PERR interval.
 1644          */
 1645         if (ratecheck(&hs->hs_lastperr, &ieee80211_hwmp_perrminint) == 0)
 1646                 return EALREADY;
 1647         getmicrouptime(&hs->hs_lastperr);
 1648 
 1649         /*
 1650          * mesh perr action frame format
 1651          *     [6] da
 1652          *     [6] sa
 1653          *     [6] addr3 = sa
 1654          *     [1] action
 1655          *     [1] category
 1656          *     [tlv] mesh path error
 1657          */
 1658         perr->perr_ie = IEEE80211_ELEMID_MESHPERR;
 1659         length = IEEE80211_MESHPERR_BASE_SZ;
 1660         for (i = 0; i<perr->perr_ndests; i++) {
 1661                 if (perr->perr_dests[i].dest_flags &
 1662                     IEEE80211_MESHPERR_FLAGS_AE) {
 1663                         length += IEEE80211_MESHPERR_DEST_SZ_AE;
 1664                         continue ;
 1665                 }
 1666                 length += IEEE80211_MESHPERR_DEST_SZ;
 1667         }
 1668         perr->perr_len =length;
 1669         return hwmp_send_action(vap, da, (uint8_t *)perr, perr->perr_len+2);
 1670 }
 1671 
 1672 /*
 1673  * Called from the rest of the net80211 code (mesh code for example).
 1674  * NB: IEEE80211_REASON_MESH_PERR_DEST_UNREACH can be trigger by the fact that
 1675  * a mesh STA is unable to forward an MSDU/MMPDU to a next-hop mesh STA.
 1676  */
 1677 #define PERR_DFLAGS(n)          perr.perr_dests[n].dest_flags
 1678 #define PERR_DADDR(n)           perr.perr_dests[n].dest_addr
 1679 #define PERR_DSEQ(n)            perr.perr_dests[n].dest_seq
 1680 #define PERR_DEXTADDR(n)        perr.perr_dests[n].dest_ext_addr
 1681 #define PERR_DRCODE(n)          perr.perr_dests[n].dest_rcode
 1682 static void
 1683 hwmp_senderror(struct ieee80211vap *vap,
 1684     const uint8_t addr[IEEE80211_ADDR_LEN],
 1685     struct ieee80211_mesh_route *rt, int rcode)
 1686 {
 1687         struct ieee80211_mesh_state *ms = vap->iv_mesh;
 1688         struct ieee80211_hwmp_route *hr = NULL;
 1689         struct ieee80211_meshperr_ie perr;
 1690 
 1691         if (rt != NULL)
 1692                 hr = IEEE80211_MESH_ROUTE_PRIV(rt,
 1693                     struct ieee80211_hwmp_route);
 1694 
 1695         perr.perr_ndests = 1;
 1696         perr.perr_ttl = ms->ms_ttl;
 1697         PERR_DFLAGS(0) = 0;
 1698         PERR_DRCODE(0) = rcode;
 1699 
 1700         switch (rcode) {
 1701         case IEEE80211_REASON_MESH_PERR_NO_FI:
 1702                 IEEE80211_ADDR_COPY(PERR_DADDR(0), addr);
 1703                 PERR_DSEQ(0) = 0; /* reserved */
 1704                 break;
 1705         case IEEE80211_REASON_MESH_PERR_NO_PROXY:
 1706                 KASSERT(rt != NULL, ("no proxy info for sending PERR"));
 1707                 KASSERT(rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY,
 1708                     ("route is not marked proxy"));
 1709                 PERR_DFLAGS(0) |= IEEE80211_MESHPERR_FLAGS_AE;
 1710                 IEEE80211_ADDR_COPY(PERR_DADDR(0), vap->iv_myaddr);
 1711                 PERR_DSEQ(0) = rt->rt_ext_seq;
 1712                 IEEE80211_ADDR_COPY(PERR_DEXTADDR(0), addr);
 1713                 break;
 1714         case IEEE80211_REASON_MESH_PERR_DEST_UNREACH:
 1715                 KASSERT(rt != NULL, ("no route info for sending PERR"));
 1716                 IEEE80211_ADDR_COPY(PERR_DADDR(0), addr);
 1717                 PERR_DSEQ(0) = hr->hr_seq;
 1718                 break;
 1719         default:
 1720                 KASSERT(0, ("unknown reason code for HWMP PERR (%u)", rcode));
 1721         }
 1722         hwmp_send_perr(vap, broadcastaddr, &perr);
 1723 }
 1724 #undef  PERR_DFLAGS
 1725 #undef  PEER_DADDR
 1726 #undef  PERR_DSEQ
 1727 #undef  PERR_DEXTADDR
 1728 #undef  PERR_DRCODE
 1729 
 1730 static void
 1731 hwmp_recv_rann(struct ieee80211vap *vap, struct ieee80211_node *ni,
 1732     const struct ieee80211_frame *wh, const struct ieee80211_meshrann_ie *rann)
 1733 {
 1734         struct ieee80211_mesh_state *ms = vap->iv_mesh;
 1735         struct ieee80211_hwmp_state *hs = vap->iv_hwmp;
 1736         struct ieee80211_mesh_route *rt = NULL;
 1737         struct ieee80211_hwmp_route *hr;
 1738         struct ieee80211_meshpreq_ie preq;
 1739         struct ieee80211_meshrann_ie prann;
 1740 
 1741         if (IEEE80211_ADDR_EQ(rann->rann_addr, vap->iv_myaddr))
 1742                 return;
 1743 
 1744         rt = ieee80211_mesh_rt_find(vap, rann->rann_addr);
 1745         if (rt != NULL && rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) {
 1746                 hr = IEEE80211_MESH_ROUTE_PRIV(rt, struct ieee80211_hwmp_route);
 1747 
 1748                 /* Acceptance criteria: if RANN.seq < stored seq, discard RANN */
 1749                 if (HWMP_SEQ_LT(rann->rann_seq, hr->hr_seq)) {
 1750                         IEEE80211_DISCARD(vap, IEEE80211_MSG_HWMP, wh, NULL,
 1751                         "RANN seq %u < %u", rann->rann_seq, hr->hr_seq);
 1752                         return;
 1753                 }
 1754 
 1755                 /* Acceptance criteria: if RANN.seq == stored seq AND
 1756                 * RANN.metric > stored metric, discard RANN */
 1757                 if (HWMP_SEQ_EQ(rann->rann_seq, hr->hr_seq) &&
 1758                 rann->rann_metric > rt->rt_metric) {
 1759                         IEEE80211_DISCARD(vap, IEEE80211_MSG_HWMP, wh, NULL,
 1760                         "RANN metric %u > %u", rann->rann_metric, rt->rt_metric);
 1761                         return;
 1762                 }
 1763         }
 1764 
 1765         /* RANN ACCEPTED */
 1766 
 1767         ieee80211_hwmp_rannint = rann->rann_interval; /* XXX: mtx lock? */
 1768 
 1769         if (rt == NULL) {
 1770                 rt = ieee80211_mesh_rt_add(vap, rann->rann_addr);
 1771                 if (rt == NULL) {
 1772                         IEEE80211_DISCARD(vap, IEEE80211_MSG_HWMP, wh, NULL,
 1773                             "unable to add mac for RANN root %6D",
 1774                             rann->rann_addr, ":");
 1775                             vap->iv_stats.is_mesh_rtaddfailed++;
 1776                         return;
 1777                 }
 1778         }
 1779         hr = IEEE80211_MESH_ROUTE_PRIV(rt, struct ieee80211_hwmp_route);
 1780         /* Check if root is a mesh gate, mark it */
 1781         if (rann->rann_flags & IEEE80211_MESHRANN_FLAGS_GATE) {
 1782                 struct ieee80211_mesh_gate_route *gr;
 1783 
 1784                 rt->rt_flags |= IEEE80211_MESHRT_FLAGS_GATE;
 1785                 gr = ieee80211_mesh_mark_gate(vap, rann->rann_addr,
 1786                         rt);
 1787                 gr->gr_lastseq = 0; /* NOT GANN */
 1788         }
 1789         /* discovery timeout */
 1790         ieee80211_mesh_rt_update(rt,
 1791             ticks_to_msecs(ieee80211_hwmp_roottimeout));
 1792 
 1793         preq.preq_flags = IEEE80211_MESHPREQ_FLAGS_AM;
 1794         preq.preq_hopcount = 0;
 1795         preq.preq_ttl = ms->ms_ttl;
 1796         preq.preq_id = 0; /* reserved */
 1797         IEEE80211_ADDR_COPY(preq.preq_origaddr, vap->iv_myaddr);
 1798         preq.preq_origseq = ++hs->hs_seq;
 1799         preq.preq_lifetime = ieee80211_hwmp_roottimeout;
 1800         preq.preq_metric = IEEE80211_MESHLMETRIC_INITIALVAL;
 1801         preq.preq_tcount = 1;
 1802         preq.preq_targets[0].target_flags = IEEE80211_MESHPREQ_TFLAGS_TO;
 1803         /* NB: IEEE80211_MESHPREQ_TFLAGS_USN = 0 implicitly implied */
 1804         IEEE80211_ADDR_COPY(preq.preq_targets[0].target_addr, rann->rann_addr);
 1805         preq.preq_targets[0].target_seq = rann->rann_seq;
 1806         /* XXX: if rootconfint have not passed, we built this preq in vain */
 1807         hwmp_send_preq(vap, wh->i_addr2, &preq, &hr->hr_lastrootconf,
 1808             &ieee80211_hwmp_rootconfint);
 1809 
 1810         /* propagate a RANN */
 1811         if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID &&
 1812             rann->rann_ttl > 1 &&
 1813             ms->ms_flags & IEEE80211_MESHFLAGS_FWD) {
 1814                 hr->hr_seq = rann->rann_seq;
 1815                 memcpy(&prann, rann, sizeof(prann));
 1816                 prann.rann_hopcount += 1;
 1817                 prann.rann_ttl -= 1;
 1818                 prann.rann_metric += ms->ms_pmetric->mpm_metric(ni);
 1819                 hwmp_send_rann(vap, broadcastaddr, &prann);
 1820         }
 1821 }
 1822 
 1823 static int
 1824 hwmp_send_rann(struct ieee80211vap *vap,
 1825     const uint8_t da[IEEE80211_ADDR_LEN],
 1826     struct ieee80211_meshrann_ie *rann)
 1827 {
 1828         /*
 1829          * mesh rann action frame format
 1830          *     [6] da
 1831          *     [6] sa
 1832          *     [6] addr3 = sa
 1833          *     [1] action
 1834          *     [1] category
 1835          *     [tlv] root annoucement
 1836          */
 1837         rann->rann_ie = IEEE80211_ELEMID_MESHRANN;
 1838         rann->rann_len = IEEE80211_MESHRANN_BASE_SZ;
 1839         return hwmp_send_action(vap, da, (uint8_t *)rann, rann->rann_len + 2);
 1840 }
 1841 
 1842 #define PREQ_TFLAGS(n)  preq.preq_targets[n].target_flags
 1843 #define PREQ_TADDR(n)   preq.preq_targets[n].target_addr
 1844 #define PREQ_TSEQ(n)    preq.preq_targets[n].target_seq
 1845 static void
 1846 hwmp_rediscover_cb(void *arg)
 1847 {
 1848         struct ieee80211_mesh_route *rt = arg;
 1849         struct ieee80211vap *vap = rt->rt_vap;
 1850         struct ieee80211_hwmp_state *hs = vap->iv_hwmp;
 1851         struct ieee80211_mesh_state *ms = vap->iv_mesh;
 1852         struct ieee80211_hwmp_route *hr;
 1853         struct ieee80211_meshpreq_ie preq; /* Optimize: storing first preq? */
 1854 
 1855         if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID))
 1856                 return ; /* nothing to do */
 1857 
 1858         hr = IEEE80211_MESH_ROUTE_PRIV(rt, struct ieee80211_hwmp_route);
 1859         if (hr->hr_preqretries >=
 1860                 ieee80211_hwmp_maxpreq_retries) {
 1861                 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY,
 1862                         rt->rt_dest, "%s",
 1863                         "max number of discovery, send queued frames to GATE");
 1864                 ieee80211_mesh_forward_to_gates(vap, rt);
 1865                 vap->iv_stats.is_mesh_fwd_nopath++;
 1866                 return ; /* XXX: flush queue? */
 1867         }
 1868 
 1869         hr->hr_preqretries++;
 1870 
 1871 
 1872         IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_HWMP, rt->rt_dest,
 1873             "start path rediscovery , target seq %u", hr->hr_seq);
 1874         /*
 1875          * Try to discover the path for this node.
 1876          * Group addressed PREQ Case A
 1877          */
 1878         preq.preq_flags = 0;
 1879         preq.preq_hopcount = 0;
 1880         preq.preq_ttl = ms->ms_ttl;
 1881         preq.preq_id = ++hs->hs_preqid;
 1882         IEEE80211_ADDR_COPY(preq.preq_origaddr, vap->iv_myaddr);
 1883         preq.preq_origseq = hr->hr_origseq;
 1884         preq.preq_lifetime = ticks_to_msecs(ieee80211_hwmp_pathtimeout);
 1885         preq.preq_metric = IEEE80211_MESHLMETRIC_INITIALVAL;
 1886         preq.preq_tcount = 1;
 1887         IEEE80211_ADDR_COPY(PREQ_TADDR(0), rt->rt_dest);
 1888         PREQ_TFLAGS(0) = 0;
 1889         if (ieee80211_hwmp_targetonly)
 1890                 PREQ_TFLAGS(0) |= IEEE80211_MESHPREQ_TFLAGS_TO;
 1891         PREQ_TFLAGS(0) |= IEEE80211_MESHPREQ_TFLAGS_USN;
 1892         PREQ_TSEQ(0) = 0; /* RESERVED when USN flag is set */
 1893         /* XXX check return value */
 1894         hwmp_send_preq(vap, broadcastaddr, &preq, &hr->hr_lastpreq,
 1895             &ieee80211_hwmp_preqminint);
 1896         callout_reset(&rt->rt_discovery,
 1897                 ieee80211_hwmp_net_diameter_traversaltime * 2,
 1898                 hwmp_rediscover_cb, rt);
 1899 }
 1900 
 1901 static struct ieee80211_node *
 1902 hwmp_discover(struct ieee80211vap *vap,
 1903     const uint8_t dest[IEEE80211_ADDR_LEN], struct mbuf *m)
 1904 {
 1905         struct ieee80211_hwmp_state *hs = vap->iv_hwmp;
 1906         struct ieee80211_mesh_state *ms = vap->iv_mesh;
 1907         struct ieee80211_mesh_route *rt = NULL;
 1908         struct ieee80211_hwmp_route *hr;
 1909         struct ieee80211_meshpreq_ie preq;
 1910         struct ieee80211_node *ni;
 1911         int sendpreq = 0;
 1912 
 1913         KASSERT(vap->iv_opmode == IEEE80211_M_MBSS,
 1914             ("not a mesh vap, opmode %d", vap->iv_opmode));
 1915 
 1916         KASSERT(!IEEE80211_ADDR_EQ(vap->iv_myaddr, dest),
 1917             ("%s: discovering self!", __func__));
 1918 
 1919         ni = NULL;
 1920         if (!IEEE80211_IS_MULTICAST(dest)) {
 1921                 rt = ieee80211_mesh_rt_find(vap, dest);
 1922                 if (rt == NULL) {
 1923                         rt = ieee80211_mesh_rt_add(vap, dest);
 1924                         if (rt == NULL) {
 1925                                 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP,
 1926                                     ni, "unable to add discovery path to %6D",
 1927                                     dest, ":");
 1928                                 vap->iv_stats.is_mesh_rtaddfailed++;
 1929                                 goto done;
 1930                         }
 1931                 }
 1932                 hr = IEEE80211_MESH_ROUTE_PRIV(rt,
 1933                     struct ieee80211_hwmp_route);
 1934                 if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_DISCOVER) {
 1935                         IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_HWMP, dest,
 1936                             "%s", "already discovering queue frame until path found");
 1937                         sendpreq = 1;
 1938                         goto done;
 1939                 }
 1940                 if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0) {
 1941                         if (hr->hr_lastdiscovery != 0 &&
 1942                             (ticks - hr->hr_lastdiscovery <
 1943                             (ieee80211_hwmp_net_diameter_traversaltime * 2))) {
 1944                                 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
 1945                                     dest, NULL, "%s",
 1946                                     "too frequent discovery requeust");
 1947                                 sendpreq = 1;
 1948                                 goto done;
 1949                         }
 1950                         hr->hr_lastdiscovery = ticks;
 1951                         if (hr->hr_preqretries >=
 1952                             ieee80211_hwmp_maxpreq_retries) {
 1953                                 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
 1954                                     dest, NULL, "%s",
 1955                                     "no valid path , max number of discovery");
 1956                                 vap->iv_stats.is_mesh_fwd_nopath++;
 1957                                 goto done;
 1958                         }
 1959                         rt->rt_flags = IEEE80211_MESHRT_FLAGS_DISCOVER;
 1960                         hr->hr_preqretries++;
 1961                         if (hr->hr_origseq == 0)
 1962                                 hr->hr_origseq = ++hs->hs_seq;
 1963                         rt->rt_metric = IEEE80211_MESHLMETRIC_INITIALVAL;
 1964                         sendpreq = 1;
 1965                         IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_HWMP, dest,
 1966                             "start path discovery (src %s), target seq %u",
 1967                             m == NULL ? "<none>" : ether_sprintf(
 1968                             mtod(m, struct ether_header *)->ether_shost),
 1969                             hr->hr_seq);
 1970                         /*
 1971                          * Try to discover the path for this node.
 1972                          * Group addressed PREQ Case A
 1973                          */
 1974                         preq.preq_flags = 0;
 1975                         preq.preq_hopcount = 0;
 1976                         preq.preq_ttl = ms->ms_ttl;
 1977                         preq.preq_id = ++hs->hs_preqid;
 1978                         IEEE80211_ADDR_COPY(preq.preq_origaddr, vap->iv_myaddr);
 1979                         preq.preq_origseq = hr->hr_origseq;
 1980                         preq.preq_lifetime =
 1981                             ticks_to_msecs(ieee80211_hwmp_pathtimeout);
 1982                         preq.preq_metric = IEEE80211_MESHLMETRIC_INITIALVAL;
 1983                         preq.preq_tcount = 1;
 1984                         IEEE80211_ADDR_COPY(PREQ_TADDR(0), dest);
 1985                         PREQ_TFLAGS(0) = 0;
 1986                         if (ieee80211_hwmp_targetonly)
 1987                                 PREQ_TFLAGS(0) |= IEEE80211_MESHPREQ_TFLAGS_TO;
 1988                         PREQ_TFLAGS(0) |= IEEE80211_MESHPREQ_TFLAGS_USN;
 1989                         PREQ_TSEQ(0) = 0; /* RESERVED when USN flag is set */
 1990                         /* XXX check return value */
 1991                         hwmp_send_preq(vap, broadcastaddr, &preq,
 1992                             &hr->hr_lastpreq, &ieee80211_hwmp_preqminint);
 1993                         callout_reset(&rt->rt_discovery,
 1994                             ieee80211_hwmp_net_diameter_traversaltime * 2,
 1995                             hwmp_rediscover_cb, rt);
 1996                 }
 1997                 if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID)
 1998                         ni = ieee80211_find_txnode(vap, rt->rt_nexthop);
 1999         } else {
 2000                 ni = ieee80211_find_txnode(vap, dest);
 2001                 /* NB: if null then we leak mbuf */
 2002                 KASSERT(ni != NULL, ("leak mcast frame"));
 2003                 return ni;
 2004         }
 2005 done:
 2006         if (ni == NULL && m != NULL) {
 2007                 if (sendpreq) {
 2008                         struct ieee80211com *ic = vap->iv_ic;
 2009                         /*
 2010                          * Queue packet for transmit when path discovery
 2011                          * completes.  If discovery never completes the
 2012                          * frame will be flushed by way of the aging timer.
 2013                          */
 2014                         IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_HWMP, dest,
 2015                             "%s", "queue frame until path found");
 2016                         m->m_pkthdr.rcvif = (void *)(uintptr_t)
 2017                             ieee80211_mac_hash(ic, dest);
 2018                         /* XXX age chosen randomly */
 2019                         ieee80211_ageq_append(&ic->ic_stageq, m,
 2020                             IEEE80211_INACT_WAIT);
 2021                 } else {
 2022                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_HWMP,
 2023                             dest, NULL, "%s", "no valid path to this node");
 2024                         m_freem(m);
 2025                 }
 2026         }
 2027         return ni;
 2028 }
 2029 #undef  PREQ_TFLAGS
 2030 #undef  PREQ_TADDR
 2031 #undef  PREQ_TSEQ
 2032 
 2033 static int
 2034 hwmp_ioctl_get80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
 2035 {
 2036         struct ieee80211_hwmp_state *hs = vap->iv_hwmp;
 2037         int error;
 2038 
 2039         if (vap->iv_opmode != IEEE80211_M_MBSS)
 2040                 return ENOSYS;
 2041         error = 0;
 2042         switch (ireq->i_type) {
 2043         case IEEE80211_IOC_HWMP_ROOTMODE:
 2044                 ireq->i_val = hs->hs_rootmode;
 2045                 break;
 2046         case IEEE80211_IOC_HWMP_MAXHOPS:
 2047                 ireq->i_val = hs->hs_maxhops;
 2048                 break;
 2049         default:
 2050                 return ENOSYS;
 2051         }
 2052         return error;
 2053 }
 2054 IEEE80211_IOCTL_GET(hwmp, hwmp_ioctl_get80211);
 2055 
 2056 static int
 2057 hwmp_ioctl_set80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
 2058 {
 2059         struct ieee80211_hwmp_state *hs = vap->iv_hwmp;
 2060         int error;
 2061 
 2062         if (vap->iv_opmode != IEEE80211_M_MBSS)
 2063                 return ENOSYS;
 2064         error = 0;
 2065         switch (ireq->i_type) {
 2066         case IEEE80211_IOC_HWMP_ROOTMODE:
 2067                 if (ireq->i_val < 0 || ireq->i_val > 3)
 2068                         return EINVAL;
 2069                 hs->hs_rootmode = ireq->i_val;
 2070                 hwmp_rootmode_setup(vap);
 2071                 break;
 2072         case IEEE80211_IOC_HWMP_MAXHOPS:
 2073                 if (ireq->i_val <= 0 || ireq->i_val > 255)
 2074                         return EINVAL;
 2075                 hs->hs_maxhops = ireq->i_val;
 2076                 break;
 2077         default:
 2078                 return ENOSYS;
 2079         }
 2080         return error;
 2081 }
 2082 IEEE80211_IOCTL_SET(hwmp, hwmp_ioctl_set80211);

Cache object: 031cd96f2331c08dfcefa92001953729


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.